message_box_win.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.
  4. #include "atom/browser/ui/message_box.h"
  5. #include <windows.h> // windows.h must be included first
  6. #include <commctrl.h>
  7. #include <map>
  8. #include <vector>
  9. #include "atom/browser/browser.h"
  10. #include "atom/browser/native_window_views.h"
  11. #include "atom/browser/unresponsive_suppressor.h"
  12. #include "base/callback.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "base/task/post_task.h"
  16. #include "base/threading/thread.h"
  17. #include "base/win/scoped_gdi_object.h"
  18. #include "content/public/browser/browser_task_traits.h"
  19. #include "content/public/browser/browser_thread.h"
  20. #include "ui/gfx/icon_util.h"
  21. #include "ui/gfx/image/image_skia.h"
  22. namespace atom {
  23. namespace {
  24. // Small command ID values are already taken by Windows, we have to start from
  25. // a large number to avoid conflicts with Windows.
  26. const int kIDStart = 100;
  27. // Get the common ID from button's name.
  28. struct CommonButtonID {
  29. int button;
  30. int id;
  31. };
  32. CommonButtonID GetCommonID(const base::string16& button) {
  33. base::string16 lower = base::ToLowerASCII(button);
  34. if (lower == L"ok")
  35. return {TDCBF_OK_BUTTON, IDOK};
  36. else if (lower == L"yes")
  37. return {TDCBF_YES_BUTTON, IDYES};
  38. else if (lower == L"no")
  39. return {TDCBF_NO_BUTTON, IDNO};
  40. else if (lower == L"cancel")
  41. return {TDCBF_CANCEL_BUTTON, IDCANCEL};
  42. else if (lower == L"retry")
  43. return {TDCBF_RETRY_BUTTON, IDRETRY};
  44. else if (lower == L"close")
  45. return {TDCBF_CLOSE_BUTTON, IDCLOSE};
  46. return {-1, -1};
  47. }
  48. // Determine whether the buttons are common buttons, if so map common ID
  49. // to button ID.
  50. void MapToCommonID(const std::vector<base::string16>& buttons,
  51. std::map<int, int>* id_map,
  52. TASKDIALOG_COMMON_BUTTON_FLAGS* button_flags,
  53. std::vector<TASKDIALOG_BUTTON>* dialog_buttons) {
  54. for (size_t i = 0; i < buttons.size(); ++i) {
  55. auto common = GetCommonID(buttons[i]);
  56. if (common.button != -1) {
  57. // It is a common button.
  58. (*id_map)[common.id] = i;
  59. (*button_flags) |= common.button;
  60. } else {
  61. // It is a custom button.
  62. dialog_buttons->push_back(
  63. {static_cast<int>(i + kIDStart), buttons[i].c_str()});
  64. }
  65. }
  66. }
  67. int ShowTaskDialogUTF16(NativeWindow* parent,
  68. MessageBoxType type,
  69. const std::vector<base::string16>& buttons,
  70. int default_id,
  71. int cancel_id,
  72. int options,
  73. const base::string16& title,
  74. const base::string16& message,
  75. const base::string16& detail,
  76. const base::string16& checkbox_label,
  77. bool* checkbox_checked,
  78. const gfx::ImageSkia& icon) {
  79. TASKDIALOG_FLAGS flags =
  80. TDF_SIZE_TO_CONTENT | // Show all content.
  81. TDF_ALLOW_DIALOG_CANCELLATION; // Allow canceling the dialog.
  82. TASKDIALOGCONFIG config = {0};
  83. config.cbSize = sizeof(config);
  84. config.hInstance = GetModuleHandle(NULL);
  85. config.dwFlags = flags;
  86. if (parent) {
  87. config.hwndParent =
  88. static_cast<atom::NativeWindowViews*>(parent)->GetAcceleratedWidget();
  89. }
  90. if (default_id > 0)
  91. config.nDefaultButton = kIDStart + default_id;
  92. // TaskDialogIndirect doesn't allow empty name, if we set empty title it
  93. // will show "electron.exe" in title.
  94. base::string16 app_name = base::UTF8ToUTF16(Browser::Get()->GetName());
  95. if (title.empty())
  96. config.pszWindowTitle = app_name.c_str();
  97. else
  98. config.pszWindowTitle = title.c_str();
  99. base::win::ScopedHICON hicon;
  100. if (!icon.isNull()) {
  101. hicon = IconUtil::CreateHICONFromSkBitmap(*icon.bitmap());
  102. config.dwFlags |= TDF_USE_HICON_MAIN;
  103. config.hMainIcon = hicon.get();
  104. } else {
  105. // Show icon according to dialog's type.
  106. switch (type) {
  107. case MESSAGE_BOX_TYPE_INFORMATION:
  108. case MESSAGE_BOX_TYPE_QUESTION:
  109. config.pszMainIcon = TD_INFORMATION_ICON;
  110. break;
  111. case MESSAGE_BOX_TYPE_WARNING:
  112. config.pszMainIcon = TD_WARNING_ICON;
  113. break;
  114. case MESSAGE_BOX_TYPE_ERROR:
  115. config.pszMainIcon = TD_ERROR_ICON;
  116. break;
  117. case MESSAGE_BOX_TYPE_NONE:
  118. break;
  119. }
  120. }
  121. // If "detail" is empty then don't make message hilighted.
  122. if (detail.empty()) {
  123. config.pszContent = message.c_str();
  124. } else {
  125. config.pszMainInstruction = message.c_str();
  126. config.pszContent = detail.c_str();
  127. }
  128. if (!checkbox_label.empty()) {
  129. config.pszVerificationText = checkbox_label.c_str();
  130. if (checkbox_checked && *checkbox_checked) {
  131. config.dwFlags |= TDF_VERIFICATION_FLAG_CHECKED;
  132. }
  133. }
  134. // Iterate through the buttons, put common buttons in dwCommonButtons
  135. // and custom buttons in pButtons.
  136. std::map<int, int> id_map;
  137. std::vector<TASKDIALOG_BUTTON> dialog_buttons;
  138. if (options & MESSAGE_BOX_NO_LINK) {
  139. for (size_t i = 0; i < buttons.size(); ++i)
  140. dialog_buttons.push_back(
  141. {static_cast<int>(i + kIDStart), buttons[i].c_str()});
  142. } else {
  143. MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
  144. }
  145. if (dialog_buttons.size() > 0) {
  146. config.pButtons = &dialog_buttons.front();
  147. config.cButtons = dialog_buttons.size();
  148. if (!(options & MESSAGE_BOX_NO_LINK))
  149. config.dwFlags |= TDF_USE_COMMAND_LINKS; // custom buttons as links.
  150. }
  151. int id = 0;
  152. BOOL verificationFlagChecked = FALSE;
  153. TaskDialogIndirect(&config, &id, nullptr, &verificationFlagChecked);
  154. if (checkbox_checked) {
  155. *checkbox_checked = verificationFlagChecked;
  156. }
  157. if (id_map.find(id) != id_map.end()) // common button.
  158. return id_map[id];
  159. else if (id >= kIDStart) // custom button.
  160. return id - kIDStart;
  161. else
  162. return cancel_id;
  163. }
  164. int ShowTaskDialogUTF8(NativeWindow* parent,
  165. MessageBoxType type,
  166. const std::vector<std::string>& buttons,
  167. int default_id,
  168. int cancel_id,
  169. int options,
  170. const std::string& title,
  171. const std::string& message,
  172. const std::string& detail,
  173. const std::string& checkbox_label,
  174. bool* checkbox_checked,
  175. const gfx::ImageSkia& icon) {
  176. std::vector<base::string16> utf16_buttons;
  177. for (const auto& button : buttons)
  178. utf16_buttons.push_back(base::UTF8ToUTF16(button));
  179. return ShowTaskDialogUTF16(
  180. parent, type, utf16_buttons, default_id, cancel_id, options,
  181. base::UTF8ToUTF16(title), base::UTF8ToUTF16(message),
  182. base::UTF8ToUTF16(detail), base::UTF8ToUTF16(checkbox_label),
  183. checkbox_checked, icon);
  184. }
  185. void RunMessageBoxInNewThread(base::Thread* thread,
  186. NativeWindow* parent,
  187. MessageBoxType type,
  188. const std::vector<std::string>& buttons,
  189. int default_id,
  190. int cancel_id,
  191. int options,
  192. const std::string& title,
  193. const std::string& message,
  194. const std::string& detail,
  195. const std::string& checkbox_label,
  196. bool checkbox_checked,
  197. const gfx::ImageSkia& icon,
  198. const MessageBoxCallback& callback) {
  199. int result = ShowTaskDialogUTF8(parent, type, buttons, default_id, cancel_id,
  200. options, title, message, detail,
  201. checkbox_label, &checkbox_checked, icon);
  202. base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
  203. base::Bind(callback, result, checkbox_checked));
  204. content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
  205. thread);
  206. }
  207. } // namespace
  208. int ShowMessageBox(NativeWindow* parent,
  209. MessageBoxType type,
  210. const std::vector<std::string>& buttons,
  211. int default_id,
  212. int cancel_id,
  213. int options,
  214. const std::string& title,
  215. const std::string& message,
  216. const std::string& detail,
  217. const gfx::ImageSkia& icon) {
  218. atom::UnresponsiveSuppressor suppressor;
  219. return ShowTaskDialogUTF8(parent, type, buttons, default_id, cancel_id,
  220. options, title, message, detail, "", nullptr, icon);
  221. }
  222. void ShowMessageBox(NativeWindow* parent,
  223. MessageBoxType type,
  224. const std::vector<std::string>& buttons,
  225. int default_id,
  226. int cancel_id,
  227. int options,
  228. const std::string& title,
  229. const std::string& message,
  230. const std::string& detail,
  231. const std::string& checkbox_label,
  232. bool checkbox_checked,
  233. const gfx::ImageSkia& icon,
  234. const MessageBoxCallback& callback) {
  235. auto thread =
  236. std::make_unique<base::Thread>(ATOM_PRODUCT_NAME "MessageBoxThread");
  237. thread->init_com_with_mta(false);
  238. if (!thread->Start()) {
  239. callback.Run(cancel_id, checkbox_checked);
  240. return;
  241. }
  242. base::Thread* unretained = thread.release();
  243. unretained->task_runner()->PostTask(
  244. FROM_HERE,
  245. base::Bind(&RunMessageBoxInNewThread, base::Unretained(unretained),
  246. parent, type, buttons, default_id, cancel_id, options, title,
  247. message, detail, checkbox_label, checkbox_checked, icon,
  248. callback));
  249. }
  250. void ShowErrorBox(const base::string16& title, const base::string16& content) {
  251. atom::UnresponsiveSuppressor suppressor;
  252. ShowTaskDialogUTF16(nullptr, MESSAGE_BOX_TYPE_ERROR, {}, -1, 0, 0, L"Error",
  253. title, content, L"", nullptr, gfx::ImageSkia());
  254. }
  255. } // namespace atom