summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/simple_message_box_views.cc
blob: f49f6888924fb817cd86c9365c5bc49270934ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/simple_message_box_views.h"

#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/simple_message_box.h"
#include "chrome/browser/ui/views/window.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "views/controls/message_box_view.h"
#include "views/widget/widget.h"

#if defined(TOUCH_UI) || defined(USE_AURA)
#include "views/focus/accelerator_handler.h"
#endif

namespace browser {

void ShowErrorBox(gfx::NativeWindow parent,
                  const string16& title,
                  const string16& message) {
  SimpleMessageBoxViews::ShowErrorBox(parent, title, message);
}

bool ShowYesNoBox(gfx::NativeWindow parent,
                  const string16& title,
                  const string16& message) {
  return SimpleMessageBoxViews::ShowYesNoBox(parent, title, message);
}

}  // namespace browser

////////////////////////////////////////////////////////////////////////////////
// SimpleMessageBoxViews, public:

// static
void SimpleMessageBoxViews::ShowErrorBox(gfx::NativeWindow parent_window,
                                         const string16& title,
                                         const string16& message) {
  int dialog_flags = ui::MessageBoxFlags::kFlagHasMessage |
      ui::MessageBoxFlags::kFlagHasOKButton;

  // This is a reference counted object so it is given an initial increment
  // in the constructor with a corresponding decrement in DeleteDelegate().
  new SimpleMessageBoxViews(parent_window, dialog_flags, title, message);
}

bool SimpleMessageBoxViews::ShowYesNoBox(gfx::NativeWindow parent_window,
                                         const string16& title,
                                         const string16& message) {
  int dialog_flags = ui::MessageBoxFlags::kIsConfirmMessageBox;

  // This is a reference counted object so it is given an initial increment
  // in the constructor plus an extra one below to ensure the dialog persists
  // until we retrieve the user response..
  scoped_refptr<SimpleMessageBoxViews> dialog =
      new SimpleMessageBoxViews(parent_window, dialog_flags, title, message);

  // Make sure Chrome doesn't attempt to shut down with the dialog up.
  g_browser_process->AddRefModule();

  bool old_state = MessageLoopForUI::current()->NestableTasksAllowed();
  MessageLoopForUI::current()->SetNestableTasksAllowed(true);
  MessageLoopForUI::current()->Run(dialog);
  MessageLoopForUI::current()->SetNestableTasksAllowed(old_state);

  g_browser_process->ReleaseModule();

  return dialog->Accepted();
}

bool SimpleMessageBoxViews::Cancel() {
  disposition_ = DISPOSITION_CANCEL;
  return true;
}

bool SimpleMessageBoxViews::Accept() {
  disposition_ = DISPOSITION_OK;
  return true;
}

int SimpleMessageBoxViews::GetDialogButtons() const {
  // NOTE: It seems unsafe to assume that the flags for OK/cancel will always
  // have the same value as the button ids.
  return (dialog_flags_ & ui::MessageBoxFlags::kFlagHasOKButton
      ? ui::MessageBoxFlags::DIALOGBUTTON_OK : 0) |
      (dialog_flags_ & ui::MessageBoxFlags::kFlagHasCancelButton
          ? ui::MessageBoxFlags::DIALOGBUTTON_CANCEL : 0);
}

string16 SimpleMessageBoxViews::GetDialogButtonLabel(
    ui::MessageBoxFlags::DialogButton button) const {
  return button == ui::MessageBoxFlags::DIALOGBUTTON_OK ?
      l10n_util::GetStringUTF16(IDS_OK) :
      l10n_util::GetStringUTF16(IDS_CLOSE);
}

bool SimpleMessageBoxViews::ShouldShowWindowTitle() const {
  return true;
}

string16 SimpleMessageBoxViews::GetWindowTitle() const {
  return message_box_title_;
}

void SimpleMessageBoxViews::DeleteDelegate() {
  Release();
}

bool SimpleMessageBoxViews::IsModal() const {
  return true;
}

views::View* SimpleMessageBoxViews::GetContentsView() {
  return message_box_view_;
}

views::Widget* SimpleMessageBoxViews::GetWidget() {
  return message_box_view_->GetWidget();
}

const views::Widget* SimpleMessageBoxViews::GetWidget() const {
  return message_box_view_->GetWidget();
}

////////////////////////////////////////////////////////////////////////////////
// SimpleMessageBoxViews, private:

SimpleMessageBoxViews::SimpleMessageBoxViews(gfx::NativeWindow parent_window,
                                             int dialog_flags,
                                             const string16& title,
                                             const string16& message)
    : dialog_flags_(dialog_flags),
      disposition_(DISPOSITION_UNKNOWN) {
  message_box_title_ = title;
  message_box_view_ = new views::MessageBoxView(
      dialog_flags,
      UTF16ToWide(message),
      std::wstring());
  browser::CreateViewsWindow(parent_window, this)->Show();

  // Add reference to be released in DeleteDelegate().
  AddRef();
}

SimpleMessageBoxViews::~SimpleMessageBoxViews() {
}

#if defined(OS_WIN)
bool SimpleMessageBoxViews::Dispatch(const MSG& msg) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
  return disposition_ == DISPOSITION_UNKNOWN;
}
#elif defined(TOUCH_UI) || defined(USE_AURA)
base::MessagePumpDispatcher::DispatchStatus
    SimpleMessageBoxViews::Dispatch(XEvent* xev) {
  if (!views::DispatchXEvent(xev))
    return EVENT_IGNORED;

  if (disposition_ == DISPOSITION_UNKNOWN)
    return base::MessagePumpDispatcher::EVENT_PROCESSED;
  return base::MessagePumpDispatcher::EVENT_QUIT;
}
#else
bool SimpleMessageBoxViews::Dispatch(GdkEvent* event) {
  gtk_main_do_event(event);
  return disposition_ == DISPOSITION_UNKNOWN;
}
#endif