ccf8cad4cb3204c7ae98a7613800d36bf3c4c19c
[reactos.git] / reactos / subsys / system / taskmgr / trayicon.c
1 /*
2 * ReactOS Task Manager
3 *
4 * trayicon.c
5 *
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * 2005 Klemens Friedl <frik85@reactos.at>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include <precomp.h>
25
26 HICON TrayIcon_GetProcessorUsageIcon(void)
27 {
28 HICON hTrayIcon = NULL;
29 HDC hScreenDC = NULL;
30 HDC hDC = NULL;
31 HBITMAP hBitmap = NULL;
32 HBITMAP hOldBitmap = NULL;
33 HBITMAP hBitmapMask = NULL;
34 ICONINFO iconInfo;
35 ULONG ProcessorUsage;
36 int nLinesToDraw;
37 HBRUSH hBitmapBrush = NULL;
38 RECT rc;
39
40 /*
41 * Get a handle to the screen DC
42 */
43 hScreenDC = GetDC(NULL);
44 if (!hScreenDC)
45 goto done;
46
47 /*
48 * Create our own DC from it
49 */
50 hDC = CreateCompatibleDC(hScreenDC);
51 if (!hDC)
52 goto done;
53
54 /*
55 * Load the bitmaps
56 */
57 hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYICON));
58 hBitmapMask = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYMASK));
59 if (!hBitmap || !hBitmapMask)
60 goto done;
61
62 hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
63 if (!hBitmapBrush)
64 goto done;
65
66 /*
67 * Select the bitmap into our device context
68 * so we can draw on it.
69 */
70 hOldBitmap = (HBITMAP) SelectObject(hDC, hBitmap);
71
72 /*
73 * Get the cpu usage
74 */
75 ProcessorUsage = PerfDataGetProcessorUsage();
76
77 /*
78 * Calculate how many lines to draw
79 * since we have 11 rows of space
80 * to draw the cpu usage instead of
81 * just having 10.
82 */
83 nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
84 rc.left = 3;
85 rc.top = 12 - nLinesToDraw;
86 rc.right = 13;
87 rc.bottom = 13;
88
89 /*
90 * Now draw the cpu usage
91 */
92 if (nLinesToDraw)
93 FillRect(hDC, &rc, hBitmapBrush);
94
95 /*
96 * Now that we are done drawing put the
97 * old bitmap back.
98 */
99 SelectObject(hDC, hOldBitmap);
100 hOldBitmap = NULL;
101
102 iconInfo.fIcon = TRUE;
103 iconInfo.xHotspot = 0;
104 iconInfo.yHotspot = 0;
105 iconInfo.hbmMask = hBitmapMask;
106 iconInfo.hbmColor = hBitmap;
107
108 hTrayIcon = CreateIconIndirect(&iconInfo);
109
110 done:
111 /*
112 * Cleanup
113 */
114 if (hScreenDC)
115 ReleaseDC(NULL, hScreenDC);
116 if (hOldBitmap)
117 SelectObject(hDC, hOldBitmap);
118 if (hDC)
119 DeleteDC(hDC);
120 if (hBitmapBrush)
121 DeleteObject(hBitmapBrush);
122 if (hBitmap)
123 DeleteObject(hBitmap);
124 if (hBitmapMask)
125 DeleteObject(hBitmapMask);
126
127 /*
128 * Return the newly created tray icon (if successful)
129 */
130 return hTrayIcon;
131 }
132
133 BOOL TrayIcon_ShellAddTrayIcon(void)
134 {
135 NOTIFYICONDATA nid;
136 HICON hIcon = NULL;
137 BOOL bRetVal;
138 TCHAR szMsg[256];
139
140 memset(&nid, 0, sizeof(NOTIFYICONDATA));
141
142 hIcon = TrayIcon_GetProcessorUsageIcon();
143
144 nid.cbSize = sizeof(NOTIFYICONDATA);
145 nid.hWnd = hMainWnd;
146 nid.uID = 0;
147 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
148 nid.uCallbackMessage = WM_ONTRAYICON;
149 nid.hIcon = hIcon;
150
151
152 LoadString( GetModuleHandle(NULL), IDS_MSG_TRAYICONCPUUSAGE, szMsg, sizeof(szMsg) / sizeof(szMsg[0]));
153 wsprintf(nid.szTip, szMsg, PerfDataGetProcessorUsage());
154
155 bRetVal = Shell_NotifyIcon(NIM_ADD, &nid);
156
157 if (hIcon)
158 DestroyIcon(hIcon);
159
160 return bRetVal;
161 }
162
163 BOOL TrayIcon_ShellRemoveTrayIcon(void)
164 {
165 NOTIFYICONDATA nid;
166 BOOL bRetVal;
167
168 memset(&nid, 0, sizeof(NOTIFYICONDATA));
169
170 nid.cbSize = sizeof(NOTIFYICONDATA);
171 nid.hWnd = hMainWnd;
172 nid.uID = 0;
173 nid.uFlags = 0;
174 nid.uCallbackMessage = WM_ONTRAYICON;
175
176 bRetVal = Shell_NotifyIcon(NIM_DELETE, &nid);
177
178 return bRetVal;
179 }
180
181 BOOL TrayIcon_ShellUpdateTrayIcon(void)
182 {
183 NOTIFYICONDATA nid;
184 HICON hIcon = NULL;
185 BOOL bRetVal;
186 TCHAR szTemp[256];
187
188 memset(&nid, 0, sizeof(NOTIFYICONDATA));
189
190 hIcon = TrayIcon_GetProcessorUsageIcon();
191
192 nid.cbSize = sizeof(NOTIFYICONDATA);
193 nid.hWnd = hMainWnd;
194 nid.uID = 0;
195 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
196 nid.uCallbackMessage = WM_ONTRAYICON;
197 nid.hIcon = hIcon;
198 LoadString(hInst, IDS_MSG_TRAYICONCPUUSAGE, szTemp, 256);
199 wsprintf(nid.szTip, szTemp, PerfDataGetProcessorUsage());
200
201 bRetVal = Shell_NotifyIcon(NIM_MODIFY, &nid);
202
203 if (hIcon)
204 DestroyIcon(hIcon);
205
206 return bRetVal;
207 }