ieframe: Moved dialog resources to ieframe.
[wine/testsucceed.git] / programs / taskmgr / trayicon.c
blob8ef9ed45d30f29d92918574252a2fd2696eac7af
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 = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYICON));
68 hBitmapMask = LoadBitmapW(hInst, MAKEINTRESOURCEW(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[255];
147 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
149 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
151 hIcon = TrayIcon_GetProcessorUsageIcon();
153 nid.cbSize = sizeof(NOTIFYICONDATAW);
154 nid.hWnd = hMainWnd;
155 nid.uID = 0;
156 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
157 nid.uCallbackMessage = WM_ONTRAYICON;
158 nid.hIcon = hIcon;
159 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
161 bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
163 if (hIcon)
164 DestroyIcon(hIcon);
166 return bRetVal;
169 BOOL TrayIcon_ShellRemoveTrayIcon(void)
171 NOTIFYICONDATAW nid;
172 BOOL bRetVal;
174 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
176 nid.cbSize = sizeof(NOTIFYICONDATAW);
177 nid.hWnd = hMainWnd;
178 nid.uID = 0;
179 nid.uFlags = 0;
180 nid.uCallbackMessage = WM_ONTRAYICON;
182 bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
184 return bRetVal;
187 BOOL TrayIcon_ShellUpdateTrayIcon(void)
189 NOTIFYICONDATAW nid;
190 HICON hIcon = NULL;
191 BOOL bRetVal;
192 WCHAR wszCPU_Usage[255];
194 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
196 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
198 hIcon = TrayIcon_GetProcessorUsageIcon();
200 nid.cbSize = sizeof(NOTIFYICONDATAW);
201 nid.hWnd = hMainWnd;
202 nid.uID = 0;
203 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
204 nid.uCallbackMessage = WM_ONTRAYICON;
205 nid.hIcon = hIcon;
206 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
208 bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
210 if (hIcon)
211 DestroyIcon(hIcon);
213 return bRetVal;