Sort includes alphabetically
[platform/framework/web/crosswalk-tizen.git] / atom / browser / ui / win / notify_icon.cc
1 // Copyright (c) 2014 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/win/notify_icon.h"
6
7 #include "atom/browser/ui/win/notify_icon_host.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/win/windows_version.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "ui/display/screen.h"
13 #include "ui/display/win/screen_win.h"
14 #include "ui/gfx/geometry/point.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/views/controls/menu/menu_runner.h"
18
19 namespace atom {
20
21 NotifyIcon::NotifyIcon(NotifyIconHost* host,
22                        UINT id,
23                        HWND window,
24                        UINT message)
25     : host_(host),
26       icon_id_(id),
27       window_(window),
28       message_id_(message),
29       menu_model_(NULL) {
30   NOTIFYICONDATA icon_data;
31   InitIconData(&icon_data);
32   icon_data.uFlags |= NIF_MESSAGE;
33   icon_data.uCallbackMessage = message_id_;
34   BOOL result = Shell_NotifyIcon(NIM_ADD, &icon_data);
35   // This can happen if the explorer process isn't running when we try to
36   // create the icon for some reason (for example, at startup).
37   if (!result)
38     LOG(WARNING) << "Unable to create status tray icon.";
39 }
40
41 NotifyIcon::~NotifyIcon() {
42   // Remove our icon.
43   host_->Remove(this);
44   NOTIFYICONDATA icon_data;
45   InitIconData(&icon_data);
46   Shell_NotifyIcon(NIM_DELETE, &icon_data);
47 }
48
49 void NotifyIcon::HandleClickEvent(int modifiers,
50                                   bool left_mouse_click,
51                                   bool double_button_click) {
52   gfx::Rect bounds = GetBounds();
53
54   if (left_mouse_click) {
55     if (double_button_click)  // double left click
56       NotifyDoubleClicked(bounds, modifiers);
57     else  // single left click
58       NotifyClicked(bounds, modifiers);
59     return;
60   } else if (!double_button_click) {  // single right click
61     if (menu_model_)
62       PopUpContextMenu(gfx::Point(), menu_model_);
63     else
64       NotifyRightClicked(bounds, modifiers);
65   }
66 }
67
68 void NotifyIcon::ResetIcon() {
69   NOTIFYICONDATA icon_data;
70   InitIconData(&icon_data);
71   // Delete any previously existing icon.
72   Shell_NotifyIcon(NIM_DELETE, &icon_data);
73   InitIconData(&icon_data);
74   icon_data.uFlags |= NIF_MESSAGE;
75   icon_data.uCallbackMessage = message_id_;
76   icon_data.hIcon = icon_.get();
77   // If we have an image, then set the NIF_ICON flag, which tells
78   // Shell_NotifyIcon() to set the image for the status icon it creates.
79   if (icon_data.hIcon)
80     icon_data.uFlags |= NIF_ICON;
81   // Re-add our icon.
82   BOOL result = Shell_NotifyIcon(NIM_ADD, &icon_data);
83   if (!result)
84     LOG(WARNING) << "Unable to re-create status tray icon.";
85 }
86
87 void NotifyIcon::SetImage(HICON image) {
88   icon_ = base::win::ScopedHICON(CopyIcon(image));
89
90   // Create the icon.
91   NOTIFYICONDATA icon_data;
92   InitIconData(&icon_data);
93   icon_data.uFlags |= NIF_ICON;
94   icon_data.hIcon = image;
95   BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
96   if (!result)
97     LOG(WARNING) << "Error setting status tray icon image";
98 }
99
100 void NotifyIcon::SetPressedImage(HICON image) {
101   // Ignore pressed images, since the standard on Windows is to not highlight
102   // pressed status icons.
103 }
104
105 void NotifyIcon::SetToolTip(const std::string& tool_tip) {
106   // Create the icon.
107   NOTIFYICONDATA icon_data;
108   InitIconData(&icon_data);
109   icon_data.uFlags |= NIF_TIP;
110   wcsncpy_s(icon_data.szTip, base::UTF8ToUTF16(tool_tip).c_str(), _TRUNCATE);
111   BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
112   if (!result)
113     LOG(WARNING) << "Unable to set tooltip for status tray icon";
114 }
115
116 void NotifyIcon::DisplayBalloon(HICON icon,
117                                 const base::string16& title,
118                                 const base::string16& contents) {
119   NOTIFYICONDATA icon_data;
120   InitIconData(&icon_data);
121   icon_data.uFlags |= NIF_INFO;
122   icon_data.dwInfoFlags = NIIF_INFO;
123   wcsncpy_s(icon_data.szInfoTitle, title.c_str(), _TRUNCATE);
124   wcsncpy_s(icon_data.szInfo, contents.c_str(), _TRUNCATE);
125   icon_data.uTimeout = 0;
126   icon_data.hBalloonIcon = icon;
127   icon_data.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
128
129   BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
130   if (!result)
131     LOG(WARNING) << "Unable to create status tray balloon.";
132 }
133
134 void NotifyIcon::PopUpContextMenu(const gfx::Point& pos,
135                                   AtomMenuModel* menu_model) {
136   // Returns if context menu isn't set.
137   if (menu_model == nullptr && menu_model_ == nullptr)
138     return;
139
140   // Set our window as the foreground window, so the context menu closes when
141   // we click away from it.
142   if (!SetForegroundWindow(window_))
143     return;
144
145   // Show menu at mouse's position by default.
146   gfx::Rect rect(pos, gfx::Size());
147   if (pos.IsOrigin())
148     rect.set_origin(display::Screen::GetScreen()->GetCursorScreenPoint());
149
150   views::MenuRunner menu_runner(
151       menu_model != nullptr ? menu_model : menu_model_,
152       views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS);
153   ignore_result(menu_runner.RunMenuAt(
154       NULL, NULL, rect, views::MENU_ANCHOR_TOPLEFT, ui::MENU_SOURCE_MOUSE));
155 }
156
157 void NotifyIcon::SetContextMenu(AtomMenuModel* menu_model) {
158   menu_model_ = menu_model;
159 }
160
161 gfx::Rect NotifyIcon::GetBounds() {
162   NOTIFYICONIDENTIFIER icon_id;
163   memset(&icon_id, 0, sizeof(NOTIFYICONIDENTIFIER));
164   icon_id.uID = icon_id_;
165   icon_id.hWnd = window_;
166   icon_id.cbSize = sizeof(NOTIFYICONIDENTIFIER);
167
168   RECT rect = { 0 };
169   Shell_NotifyIconGetRect(&icon_id, &rect);
170   return display::win::ScreenWin::ScreenToDIPRect(window_, gfx::Rect(rect));
171 }
172
173 void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) {
174   memset(icon_data, 0, sizeof(NOTIFYICONDATA));
175   icon_data->cbSize = sizeof(NOTIFYICONDATA);
176   icon_data->hWnd = window_;
177   icon_data->uID = icon_id_;
178 }
179
180 }  // namespace atom