taskmgr: HGDIOBJ is interchangeable with other handle types; no casts are needed.
[wine/multimedia.git] / programs / taskmgr / trayicon.c
blob90705ecb0785857d4be142378fa35847d6f6f1ca
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 <malloc.h>
28 #include <memory.h>
29 #include <stdio.h>
30 #include <winnt.h>
31 #include <shellapi.h>
33 #include "wine/unicode.h"
34 #include "taskmgr.h"
35 #include "perfdata.h"
37 HICON TrayIcon_GetProcessorUsageIcon(void)
39 HICON hTrayIcon = NULL;
40 HDC hScreenDC = NULL;
41 HDC hDC = NULL;
42 HBITMAP hBitmap = NULL;
43 HBITMAP hOldBitmap;
44 HBITMAP hBitmapMask = NULL;
45 ICONINFO iconInfo;
46 ULONG ProcessorUsage;
47 int nLinesToDraw;
48 HBRUSH hBitmapBrush = NULL;
49 RECT rc;
52 * Get a handle to the screen DC
54 hScreenDC = GetDC(NULL);
55 if (!hScreenDC)
56 goto done;
59 * Create our own DC from it
61 hDC = CreateCompatibleDC(hScreenDC);
62 if (!hDC)
63 goto done;
66 * Load the bitmaps
68 hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYICON));
69 hBitmapMask = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYMASK));
70 if (!hBitmap || !hBitmapMask)
71 goto done;
73 hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
74 if (!hBitmapBrush)
75 goto done;
78 * Select the bitmap into our device context
79 * so we can draw on it.
81 hOldBitmap = SelectObject(hDC, hBitmap);
84 * Get the cpu usage
86 ProcessorUsage = PerfDataGetProcessorUsage();
89 * Calculate how many lines to draw
90 * since we have 11 rows of space
91 * to draw the cpu usage instead of
92 * just having 10.
94 nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
95 rc.left = 3;
96 rc.top = 12 - nLinesToDraw;
97 rc.right = 13;
98 rc.bottom = 13;
101 * Now draw the cpu usage
103 if (nLinesToDraw)
104 FillRect(hDC, &rc, hBitmapBrush);
107 * Now that we are done drawing put the
108 * old bitmap back.
110 SelectObject(hDC, hOldBitmap);
112 iconInfo.fIcon = TRUE;
113 iconInfo.xHotspot = 0;
114 iconInfo.yHotspot = 0;
115 iconInfo.hbmMask = hBitmapMask;
116 iconInfo.hbmColor = hBitmap;
118 hTrayIcon = CreateIconIndirect(&iconInfo);
120 done:
122 * Cleanup
124 if (hScreenDC)
125 ReleaseDC(NULL, hScreenDC);
126 if (hDC)
127 DeleteDC(hDC);
128 if (hBitmapBrush)
129 DeleteObject(hBitmapBrush);
130 if (hBitmap)
131 DeleteObject(hBitmap);
132 if (hBitmapMask)
133 DeleteObject(hBitmapMask);
136 * Return the newly created tray icon (if successful)
138 return hTrayIcon;
141 BOOL TrayIcon_ShellAddTrayIcon(void)
143 NOTIFYICONDATAW nid;
144 HICON hIcon = NULL;
145 BOOL bRetVal;
146 WCHAR wszCPU_Usage[] = {'C','P','U',' ','U','s','a','g','e',':',' ','%','d','%','%',0};
148 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
150 hIcon = TrayIcon_GetProcessorUsageIcon();
152 nid.cbSize = sizeof(NOTIFYICONDATAW);
153 nid.hWnd = hMainWnd;
154 nid.uID = 0;
155 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
156 nid.uCallbackMessage = WM_ONTRAYICON;
157 nid.hIcon = hIcon;
158 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
160 bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
162 if (hIcon)
163 DestroyIcon(hIcon);
165 return bRetVal;
168 BOOL TrayIcon_ShellRemoveTrayIcon(void)
170 NOTIFYICONDATAW nid;
171 BOOL bRetVal;
173 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
175 nid.cbSize = sizeof(NOTIFYICONDATAW);
176 nid.hWnd = hMainWnd;
177 nid.uID = 0;
178 nid.uFlags = 0;
179 nid.uCallbackMessage = WM_ONTRAYICON;
181 bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
183 return bRetVal;
186 BOOL TrayIcon_ShellUpdateTrayIcon(void)
188 NOTIFYICONDATAW nid;
189 HICON hIcon = NULL;
190 BOOL bRetVal;
191 WCHAR wszCPU_Usage[] = {'C','P','U',' ','U','s','a','g','e',':',' ','%','d','%','%',0};
193 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
195 hIcon = TrayIcon_GetProcessorUsageIcon();
197 nid.cbSize = sizeof(NOTIFYICONDATAW);
198 nid.hWnd = hMainWnd;
199 nid.uID = 0;
200 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
201 nid.uCallbackMessage = WM_ONTRAYICON;
202 nid.hIcon = hIcon;
203 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
205 bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
207 if (hIcon)
208 DestroyIcon(hIcon);
210 return bRetVal;