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/native_window_views.h"
14 #include "base/callback.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread.h"
18 #include "base/win/scoped_gdi_object.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "ui/gfx/icon_util.h"
26 // Small command ID values are already taken by Windows, we have to start from
27 // a large number to avoid conflicts with Windows.
28 const int kIDStart = 100;
30 // Get the common ID from button's name.
31 struct CommonButtonID {
35 CommonButtonID GetCommonID(const base::string16& button) {
36 base::string16 lower = base::StringToLowerASCII(button);
38 return { TDCBF_OK_BUTTON, IDOK };
39 else if (lower == L"yes")
40 return { TDCBF_YES_BUTTON, IDYES };
41 else if (lower == L"no")
42 return { TDCBF_NO_BUTTON, IDNO };
43 else if (lower == L"cancel")
44 return { TDCBF_CANCEL_BUTTON, IDCANCEL };
45 else if (lower == L"retry")
46 return { TDCBF_RETRY_BUTTON, IDRETRY };
47 else if (lower == L"close")
48 return { TDCBF_CLOSE_BUTTON, IDCLOSE };
52 // Determine whether the buttons are common buttons, if so map common ID
54 void MapToCommonID(const std::vector<base::string16>& buttons,
55 std::map<int, int>* id_map,
56 TASKDIALOG_COMMON_BUTTON_FLAGS* button_flags,
57 std::vector<TASKDIALOG_BUTTON>* dialog_buttons) {
58 for (size_t i = 0; i < buttons.size(); ++i) {
59 auto common = GetCommonID(buttons[i]);
60 if (common.button != -1) {
61 // It is a common button.
62 (*id_map)[common.id] = i;
63 (*button_flags) |= common.button;
65 // It is a custom button.
66 dialog_buttons->push_back({i + kIDStart, buttons[i].c_str()});
71 int ShowMessageBoxUTF16(HWND parent,
73 const std::vector<base::string16>& buttons,
75 const base::string16& title,
76 const base::string16& message,
77 const base::string16& detail,
78 const gfx::ImageSkia& icon) {
79 TASKDIALOG_FLAGS flags = TDF_SIZE_TO_CONTENT; // show all content.
81 flags |= TDF_ALLOW_DIALOG_CANCELLATION; // allow dialog to be cancelled.
83 TASKDIALOGCONFIG config = { 0 };
84 config.cbSize = sizeof(config);
85 config.hwndParent = parent;
86 config.hInstance = GetModuleHandle(NULL);
87 config.dwFlags = flags;
90 config.pszWindowTitle = title.c_str();
92 base::win::ScopedHICON hicon;
94 hicon.Set(IconUtil::CreateHICONFromSkBitmap(*icon.bitmap()));
95 config.dwFlags |= TDF_USE_HICON_MAIN;
96 config.hMainIcon = hicon.Get();
98 // Show icon according to dialog's type.
100 case MESSAGE_BOX_TYPE_INFORMATION:
101 config.pszMainIcon = TD_INFORMATION_ICON;
103 case MESSAGE_BOX_TYPE_WARNING:
104 config.pszMainIcon = TD_WARNING_ICON;
106 case MESSAGE_BOX_TYPE_ERROR:
107 config.pszMainIcon = TD_ERROR_ICON;
112 // If "detail" is empty then don't make message hilighted.
113 if (detail.empty()) {
114 config.pszContent = message.c_str();
116 config.pszMainInstruction = message.c_str();
117 config.pszContent = detail.c_str();
120 // Iterate through the buttons, put common buttons in dwCommonButtons
121 // and custom buttons in pButtons.
122 std::map<int, int> id_map;
123 std::vector<TASKDIALOG_BUTTON> dialog_buttons;
124 MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
125 if (dialog_buttons.size() > 0) {
126 config.pButtons = &dialog_buttons.front();
127 config.cButtons = dialog_buttons.size();
128 config.dwFlags |= TDF_USE_COMMAND_LINKS; // custom buttons as links.
132 TaskDialogIndirect(&config, &id, NULL, NULL);
133 if (id_map.find(id) != id_map.end()) // common button.
135 else if (id >= kIDStart) // custom button.
136 return id - kIDStart;
141 void RunMessageBoxInNewThread(base::Thread* thread,
142 NativeWindow* parent,
144 const std::vector<std::string>& buttons,
146 const std::string& title,
147 const std::string& message,
148 const std::string& detail,
149 const gfx::ImageSkia& icon,
150 const MessageBoxCallback& callback) {
151 int result = ShowMessageBox(parent, type, buttons, cancel_id, title, message,
153 content::BrowserThread::PostTask(
154 content::BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
155 content::BrowserThread::DeleteSoon(
156 content::BrowserThread::UI, FROM_HERE, thread);
161 int ShowMessageBox(NativeWindow* parent,
163 const std::vector<std::string>& buttons,
165 const std::string& title,
166 const std::string& message,
167 const std::string& detail,
168 const gfx::ImageSkia& icon) {
169 std::vector<base::string16> utf16_buttons;
170 for (const auto& button : buttons)
171 utf16_buttons.push_back(base::UTF8ToUTF16(button));
173 HWND hwnd_parent = parent ?
174 static_cast<atom::NativeWindowViews*>(parent)->GetAcceleratedWidget() :
177 NativeWindow::DialogScope dialog_scope(parent);
178 return ShowMessageBoxUTF16(hwnd_parent,
182 base::UTF8ToUTF16(title),
183 base::UTF8ToUTF16(message),
184 base::UTF8ToUTF16(detail),
188 void ShowMessageBox(NativeWindow* parent,
190 const std::vector<std::string>& buttons,
192 const std::string& title,
193 const std::string& message,
194 const std::string& detail,
195 const gfx::ImageSkia& icon,
196 const MessageBoxCallback& callback) {
197 scoped_ptr<base::Thread> thread(
198 new base::Thread(ATOM_PRODUCT_NAME "MessageBoxThread"));
199 thread->init_com_with_mta(false);
200 if (!thread->Start()) {
201 callback.Run(cancel_id);
205 base::Thread* unretained = thread.release();
206 unretained->message_loop()->PostTask(
208 base::Bind(&RunMessageBoxInNewThread, base::Unretained(unretained),
209 parent, type, buttons, cancel_id, title, message, detail, icon,
213 void ShowErrorBox(const base::string16& title, const base::string16& content) {
214 ShowMessageBoxUTF16(NULL, MESSAGE_BOX_TYPE_ERROR, {}, 0, L"Error", title,
215 content, gfx::ImageSkia());