d3dx9: Override d3dx9_get_param_value_ptr() return value only on array overflow.
[wine.git] / programs / taskmgr / trayicon.c
blobaf98474c2420c45555c452c0446e2f0659c47631
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 #include <stdio.h>
24 #include <stdlib.h>
26 #include <windows.h>
27 #include <commctrl.h>
28 #include <winnt.h>
29 #include <shellapi.h>
31 #include "wine/unicode.h"
32 #include "taskmgr.h"
33 #include "perfdata.h"
35 static HICON TrayIcon_GetProcessorUsageIcon(void)
37 HICON hTrayIcon = NULL;
38 HDC hScreenDC = NULL;
39 HDC hDC = NULL;
40 HBITMAP hBitmap = NULL;
41 HBITMAP hOldBitmap;
42 HBITMAP hBitmapMask = NULL;
43 ICONINFO iconInfo;
44 ULONG ProcessorUsage;
45 int nLinesToDraw;
46 HBRUSH hBitmapBrush = NULL;
47 RECT rc;
50 * Get a handle to the screen DC
52 hScreenDC = GetDC(NULL);
53 if (!hScreenDC)
54 goto done;
57 * Create our own DC from it
59 hDC = CreateCompatibleDC(hScreenDC);
60 if (!hDC)
61 goto done;
64 * Load the bitmaps
66 hBitmap = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYICON));
67 hBitmapMask = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYMASK));
68 if (!hBitmap || !hBitmapMask)
69 goto done;
71 hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
72 if (!hBitmapBrush)
73 goto done;
76 * Select the bitmap into our device context
77 * so we can draw on it.
79 hOldBitmap = SelectObject(hDC, hBitmap);
82 * Get the cpu usage
84 ProcessorUsage = PerfDataGetProcessorUsage();
87 * Calculate how many lines to draw
88 * since we have 11 rows of space
89 * to draw the cpu usage instead of
90 * just having 10.
92 nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
93 SetRect(&rc, 3, 12 - nLinesToDraw, 13, 13);
96 * Now draw the cpu usage
98 if (nLinesToDraw)
99 FillRect(hDC, &rc, hBitmapBrush);
102 * Now that we are done drawing put the
103 * old bitmap back.
105 SelectObject(hDC, hOldBitmap);
107 iconInfo.fIcon = TRUE;
108 iconInfo.xHotspot = 0;
109 iconInfo.yHotspot = 0;
110 iconInfo.hbmMask = hBitmapMask;
111 iconInfo.hbmColor = hBitmap;
113 hTrayIcon = CreateIconIndirect(&iconInfo);
115 done:
117 * Cleanup
119 if (hScreenDC)
120 ReleaseDC(NULL, hScreenDC);
121 if (hDC)
122 DeleteDC(hDC);
123 if (hBitmapBrush)
124 DeleteObject(hBitmapBrush);
125 if (hBitmap)
126 DeleteObject(hBitmap);
127 if (hBitmapMask)
128 DeleteObject(hBitmapMask);
131 * Return the newly created tray icon (if successful)
133 return hTrayIcon;
136 BOOL TrayIcon_ShellAddTrayIcon(void)
138 NOTIFYICONDATAW nid;
139 HICON hIcon = NULL;
140 BOOL bRetVal;
141 WCHAR wszCPU_Usage[255];
143 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
145 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
147 hIcon = TrayIcon_GetProcessorUsageIcon();
149 nid.cbSize = sizeof(NOTIFYICONDATAW);
150 nid.hWnd = hMainWnd;
151 nid.uID = 0;
152 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
153 nid.uCallbackMessage = WM_ONTRAYICON;
154 nid.hIcon = hIcon;
155 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
157 bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
159 if (hIcon)
160 DestroyIcon(hIcon);
162 return bRetVal;
165 BOOL TrayIcon_ShellRemoveTrayIcon(void)
167 NOTIFYICONDATAW nid;
168 BOOL bRetVal;
170 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
172 nid.cbSize = sizeof(NOTIFYICONDATAW);
173 nid.hWnd = hMainWnd;
174 nid.uID = 0;
175 nid.uFlags = 0;
176 nid.uCallbackMessage = WM_ONTRAYICON;
178 bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
180 return bRetVal;
183 BOOL TrayIcon_ShellUpdateTrayIcon(void)
185 NOTIFYICONDATAW nid;
186 HICON hIcon = NULL;
187 BOOL bRetVal;
188 WCHAR wszCPU_Usage[255];
190 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
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;