message_box_win.cc 10.0 KB

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