Final cleanup
[platform/framework/web/crosswalk-tizen.git] / atom / browser / ui / message_box_win.cc
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
5 #include "atom/browser/ui/message_box.h"
6
7 #include <windows.h>
8 #include <commctrl.h>
9
10 #include <map>
11 #include <vector>
12
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"
21
22 namespace atom {
23
24 namespace {
25
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;
29
30 // Get the common ID from button's name.
31 struct CommonButtonID {
32   int button;
33   int id;
34 };
35 CommonButtonID GetCommonID(const base::string16& button) {
36   base::string16 lower = base::StringToLowerASCII(button);
37   if (lower == L"ok")
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 };
49   return { -1, -1 };
50 }
51
52 // Determine whether the buttons are common buttons, if so map common ID
53 // to button 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;
64     } else {
65       // It is a custom button.
66       dialog_buttons->push_back({i + kIDStart, buttons[i].c_str()});
67     }
68   }
69 }
70
71 int ShowMessageBoxUTF16(HWND parent,
72                         MessageBoxType type,
73                         const std::vector<base::string16>& buttons,
74                         int cancel_id,
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.
80   if (cancel_id != 0)
81     flags |= TDF_ALLOW_DIALOG_CANCELLATION;  // allow dialog to be cancelled.
82
83   TASKDIALOGCONFIG config = { 0 };
84   config.cbSize     = sizeof(config);
85   config.hwndParent = parent;
86   config.hInstance  = GetModuleHandle(NULL);
87   config.dwFlags    = flags;
88
89   if (!title.empty())
90     config.pszWindowTitle = title.c_str();
91
92   base::win::ScopedHICON hicon;
93   if (!icon.isNull()) {
94     hicon.Set(IconUtil::CreateHICONFromSkBitmap(*icon.bitmap()));
95     config.dwFlags |= TDF_USE_HICON_MAIN;
96     config.hMainIcon = hicon.Get();
97   } else {
98     // Show icon according to dialog's type.
99     switch (type) {
100       case MESSAGE_BOX_TYPE_INFORMATION:
101         config.pszMainIcon = TD_INFORMATION_ICON;
102         break;
103       case MESSAGE_BOX_TYPE_WARNING:
104         config.pszMainIcon = TD_WARNING_ICON;
105         break;
106       case MESSAGE_BOX_TYPE_ERROR:
107         config.pszMainIcon = TD_ERROR_ICON;
108         break;
109     }
110   }
111
112   // If "detail" is empty then don't make message hilighted.
113   if (detail.empty()) {
114     config.pszContent = message.c_str();
115   } else {
116     config.pszMainInstruction = message.c_str();
117     config.pszContent = detail.c_str();
118   }
119
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.
129   }
130
131   int id = 0;
132   TaskDialogIndirect(&config, &id, NULL, NULL);
133   if (id_map.find(id) != id_map.end())  // common button.
134     return id_map[id];
135   else if (id >= kIDStart)  // custom button.
136     return id - kIDStart;
137   else
138     return cancel_id;
139 }
140
141 void RunMessageBoxInNewThread(base::Thread* thread,
142                               NativeWindow* parent,
143                               MessageBoxType type,
144                               const std::vector<std::string>& buttons,
145                               int cancel_id,
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,
152                               detail, icon);
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);
157 }
158
159 }  // namespace
160
161 int ShowMessageBox(NativeWindow* parent,
162                    MessageBoxType type,
163                    const std::vector<std::string>& buttons,
164                    int cancel_id,
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));
172
173   HWND hwnd_parent = parent ?
174       static_cast<atom::NativeWindowViews*>(parent)->GetAcceleratedWidget() :
175       NULL;
176
177   NativeWindow::DialogScope dialog_scope(parent);
178   return ShowMessageBoxUTF16(hwnd_parent,
179                              type,
180                              utf16_buttons,
181                              cancel_id,
182                              base::UTF8ToUTF16(title),
183                              base::UTF8ToUTF16(message),
184                              base::UTF8ToUTF16(detail),
185                              icon);
186 }
187
188 void ShowMessageBox(NativeWindow* parent,
189                     MessageBoxType type,
190                     const std::vector<std::string>& buttons,
191                     int cancel_id,
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);
202     return;
203   }
204
205   base::Thread* unretained = thread.release();
206   unretained->message_loop()->PostTask(
207       FROM_HERE,
208       base::Bind(&RunMessageBoxInNewThread, base::Unretained(unretained),
209                  parent, type, buttons, cancel_id, title, message, detail, icon,
210                  callback));
211 }
212
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());
216 }
217
218 }  // namespace atom