1 // Copyright (c) 2013 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
5 #include "atom/browser/ui/message_box.h"
13 #include "atom/browser/browser.h"
14 #include "atom/browser/native_window_views.h"
15 #include "base/callback.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread.h"
19 #include "base/win/scoped_gdi_object.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "ui/gfx/icon_util.h"
27 // Small command ID values are already taken by Windows, we have to start from
28 // a large number to avoid conflicts with Windows.
29 const int kIDStart = 100;
31 // Get the common ID from button's name.
32 struct CommonButtonID {
36 CommonButtonID GetCommonID(const base::string16& button) {
37 base::string16 lower = base::ToLowerASCII(button);
39 return { TDCBF_OK_BUTTON, IDOK };
40 else if (lower == L"yes")
41 return { TDCBF_YES_BUTTON, IDYES };
42 else if (lower == L"no")
43 return { TDCBF_NO_BUTTON, IDNO };
44 else if (lower == L"cancel")
45 return { TDCBF_CANCEL_BUTTON, IDCANCEL };
46 else if (lower == L"retry")
47 return { TDCBF_RETRY_BUTTON, IDRETRY };
48 else if (lower == L"close")
49 return { TDCBF_CLOSE_BUTTON, IDCLOSE };
53 // Determine whether the buttons are common buttons, if so map common ID
55 void MapToCommonID(const std::vector<base::string16>& buttons,
56 std::map<int, int>* id_map,
57 TASKDIALOG_COMMON_BUTTON_FLAGS* button_flags,
58 std::vector<TASKDIALOG_BUTTON>* dialog_buttons) {
59 for (size_t i = 0; i < buttons.size(); ++i) {
60 auto common = GetCommonID(buttons[i]);
61 if (common.button != -1) {
62 // It is a common button.
63 (*id_map)[common.id] = i;
64 (*button_flags) |= common.button;
66 // It is a custom button.
67 dialog_buttons->push_back({i + kIDStart, buttons[i].c_str()});
72 int ShowMessageBoxUTF16(HWND parent,
74 const std::vector<base::string16>& buttons,
77 const base::string16& title,
78 const base::string16& message,
79 const base::string16& detail,
80 const gfx::ImageSkia& icon) {
81 TASKDIALOG_FLAGS flags =
82 TDF_SIZE_TO_CONTENT | // Show all content.
83 TDF_ALLOW_DIALOG_CANCELLATION; // Allow canceling the dialog.
85 TASKDIALOGCONFIG config = { 0 };
86 config.cbSize = sizeof(config);
87 config.hwndParent = parent;
88 config.hInstance = GetModuleHandle(NULL);
89 config.dwFlags = flags;
91 // TaskDialogIndirect doesn't allow empty name, if we set empty title it
92 // will show "electron.exe" in title.
93 base::string16 app_name = base::UTF8ToUTF16(Browser::Get()->GetName());
95 config.pszWindowTitle = app_name.c_str();
97 config.pszWindowTitle = title.c_str();
99 base::win::ScopedHICON hicon;
100 if (!icon.isNull()) {
101 hicon.Set(IconUtil::CreateHICONFromSkBitmap(*icon.bitmap()));
102 config.dwFlags |= TDF_USE_HICON_MAIN;
103 config.hMainIcon = hicon.Get();
105 // Show icon according to dialog's type.
107 case MESSAGE_BOX_TYPE_INFORMATION:
108 case MESSAGE_BOX_TYPE_QUESTION:
109 config.pszMainIcon = TD_INFORMATION_ICON;
111 case MESSAGE_BOX_TYPE_WARNING:
112 config.pszMainIcon = TD_WARNING_ICON;
114 case MESSAGE_BOX_TYPE_ERROR:
115 config.pszMainIcon = TD_ERROR_ICON;
120 // If "detail" is empty then don't make message hilighted.
121 if (detail.empty()) {
122 config.pszContent = message.c_str();
124 config.pszMainInstruction = message.c_str();
125 config.pszContent = detail.c_str();
128 // Iterate through the buttons, put common buttons in dwCommonButtons
129 // and custom buttons in pButtons.
130 std::map<int, int> id_map;
131 std::vector<TASKDIALOG_BUTTON> dialog_buttons;
132 if (options & MESSAGE_BOX_NO_LINK) {
133 for (size_t i = 0; i < buttons.size(); ++i)
134 dialog_buttons.push_back({i + kIDStart, buttons[i].c_str()});
136 MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
138 if (dialog_buttons.size() > 0) {
139 config.pButtons = &dialog_buttons.front();
140 config.cButtons = dialog_buttons.size();
141 if (!(options & MESSAGE_BOX_NO_LINK))
142 config.dwFlags |= TDF_USE_COMMAND_LINKS; // custom buttons as links.
146 TaskDialogIndirect(&config, &id, NULL, NULL);
147 if (id_map.find(id) != id_map.end()) // common button.
149 else if (id >= kIDStart) // custom button.
150 return id - kIDStart;
155 void RunMessageBoxInNewThread(base::Thread* thread,
156 NativeWindow* parent,
158 const std::vector<std::string>& buttons,
161 const std::string& title,
162 const std::string& message,
163 const std::string& detail,
164 const gfx::ImageSkia& icon,
165 const MessageBoxCallback& callback) {
166 int result = ShowMessageBox(parent, type, buttons, cancel_id, options, title,
167 message, detail, icon);
168 content::BrowserThread::PostTask(
169 content::BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
170 content::BrowserThread::DeleteSoon(
171 content::BrowserThread::UI, FROM_HERE, thread);
176 int ShowMessageBox(NativeWindow* parent,
178 const std::vector<std::string>& buttons,
181 const std::string& title,
182 const std::string& message,
183 const std::string& detail,
184 const gfx::ImageSkia& icon) {
185 std::vector<base::string16> utf16_buttons;
186 for (const auto& button : buttons)
187 utf16_buttons.push_back(base::UTF8ToUTF16(button));
189 HWND hwnd_parent = parent ?
190 static_cast<atom::NativeWindowViews*>(parent)->GetAcceleratedWidget() :
193 NativeWindow::DialogScope dialog_scope(parent);
194 return ShowMessageBoxUTF16(hwnd_parent,
199 base::UTF8ToUTF16(title),
200 base::UTF8ToUTF16(message),
201 base::UTF8ToUTF16(detail),
205 void ShowMessageBox(NativeWindow* parent,
207 const std::vector<std::string>& buttons,
210 const std::string& title,
211 const std::string& message,
212 const std::string& detail,
213 const gfx::ImageSkia& icon,
214 const MessageBoxCallback& callback) {
215 scoped_ptr<base::Thread> thread(
216 new base::Thread(ATOM_PRODUCT_NAME "MessageBoxThread"));
217 thread->init_com_with_mta(false);
218 if (!thread->Start()) {
219 callback.Run(cancel_id);
223 base::Thread* unretained = thread.release();
224 unretained->message_loop()->PostTask(
226 base::Bind(&RunMessageBoxInNewThread, base::Unretained(unretained),
227 parent, type, buttons, cancel_id, options, title, message,
228 detail, icon, callback));
231 void ShowErrorBox(const base::string16& title, const base::string16& content) {
232 ShowMessageBoxUTF16(NULL, MESSAGE_BOX_TYPE_ERROR, {}, 0, 0, L"Error", title,
233 content, gfx::ImageSkia());