push d1f5df181c120dbe494f7c89b454752c2e0dcc04
[wine/hacks.git] / programs / taskmgr / trayicon.c
blob35740dd139d06dd76c82933ab74161a62bb94589
1 /*
2 * ReactOS Task Manager
4 * trayicon.c
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library 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 GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <stdlib.h>
27 #include <memory.h>
28 #include <stdio.h>
29 #include <winnt.h>
30 #include <shellapi.h>
32 #include "wine/unicode.h"
33 #include "taskmgr.h"
34 #include "perfdata.h"
36 static HICON TrayIcon_GetProcessorUsageIcon(void)
38 HICON hTrayIcon = NULL;
39 HDC hScreenDC = NULL;
40 HDC hDC = NULL;
41 HBITMAP hBitmap = NULL;
42 HBITMAP hOldBitmap;
43 HBITMAP hBitmapMask = NULL;
44 ICONINFO iconInfo;
45 ULONG ProcessorUsage;
46 int nLinesToDraw;
47 HBRUSH hBitmapBrush = NULL;
48 RECT rc;
51 * Get a handle to the screen DC
53 hScreenDC = GetDC(NULL);
54 if (!hScreenDC)
55 goto done;
58 * Create our own DC from it
60 hDC = CreateCompatibleDC(hScreenDC);
61 if (!hDC)
62 goto done;
65 * Load the bitmaps
67 hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYICON));
68 hBitmapMask = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYMASK));
69 if (!hBitmap || !hBitmapMask)
70 goto done;
72 hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
73 if (!hBitmapBrush)
74 goto done;
77 * Select the bitmap into our device context
78 * so we can draw on it.
80 hOldBitmap = SelectObject(hDC, hBitmap);
83 * Get the cpu usage
85 ProcessorUsage = PerfDataGetProcessorUsage();
88 * Calculate how many lines to draw
89 * since we have 11 rows of space
90 * to draw the cpu usage instead of
91 * just having 10.
93 nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
94 rc.left = 3;
95 rc.top = 12 - nLinesToDraw;
96 rc.right = 13;
97 rc.bottom = 13;
100 * Now draw the cpu usage
102 if (nLinesToDraw)
103 FillRect(hDC, &rc, hBitmapBrush);
106 * Now that we are done drawing put the
107 * old bitmap back.
109 SelectObject(hDC, hOldBitmap);
111 iconInfo.fIcon = TRUE;
112 iconInfo.xHotspot = 0;
113 iconInfo.yHotspot = 0;
114 iconInfo.hbmMask = hBitmapMask;
115 iconInfo.hbmColor = hBitmap;
117 hTrayIcon = CreateIconIndirect(&iconInfo);
119 done:
121 * Cleanup
123 if (hScreenDC)
124 ReleaseDC(NULL, hScreenDC);
125 if (hDC)
126 DeleteDC(hDC);
127 if (hBitmapBrush)
128 DeleteObject(hBitmapBrush);
129 if (hBitmap)
130 DeleteObject(hBitmap);
131 if (hBitmapMask)
132 DeleteObject(hBitmapMask);
135 * Return the newly created tray icon (if successful)
137 return hTrayIcon;
140 BOOL TrayIcon_ShellAddTrayIcon(void)
142 NOTIFYICONDATAW nid;
143 HICON hIcon = NULL;
144 BOOL bRetVal;
145 WCHAR wszCPU_Usage[] = {'C','P','U',' ','U','s','a','g','e',':',' ','%','d','%','%',0};
147 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
149 hIcon = TrayIcon_GetProcessorUsageIcon();
151 nid.cbSize = sizeof(NOTIFYICONDATAW);
152 nid.hWnd = hMainWnd;
153 nid.uID = 0;
154 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
155 nid.uCallbackMessage = WM_ONTRAYICON;
156 nid.hIcon = hIcon;
157 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
159 bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
161 if (hIcon)
162 DestroyIcon(hIcon);
164 return bRetVal;
167 BOOL TrayIcon_ShellRemoveTrayIcon(void)
169 NOTIFYICONDATAW nid;
170 BOOL bRetVal;
172 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
174 nid.cbSize = sizeof(NOTIFYICONDATAW);
175 nid.hWnd = hMainWnd;
176 nid.uID = 0;
177 nid.uFlags = 0;
178 nid.uCallbackMessage = WM_ONTRAYICON;
180 bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
182 return bRetVal;
185 BOOL TrayIcon_ShellUpdateTrayIcon(void)
187 NOTIFYICONDATAW nid;
188 HICON hIcon = NULL;
189 BOOL bRetVal;
190 WCHAR wszCPU_Usage[] = {'C','P','U',' ','U','s','a','g','e',':',' ','%','d','%','%',0};
192 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
194 hIcon = TrayIcon_GetProcessorUsageIcon();
196 nid.cbSize = sizeof(NOTIFYICONDATAW);
197 nid.hWnd = hMainWnd;
198 nid.uID = 0;
199 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
200 nid.uCallbackMessage = WM_ONTRAYICON;
201 nid.hIcon = hIcon;
202 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
204 bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
206 if (hIcon)
207 DestroyIcon(hIcon);
209 return bRetVal;