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