ntdll: Fetch thread id on NetBSD.
[wine/multimedia.git] / programs / taskmgr / trayicon.c
blob636d3bda9245adced1e9c5f8471939f614ca3675
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 rc.left = 3;
94 rc.top = 12 - nLinesToDraw;
95 rc.right = 13;
96 rc.bottom = 13;
99 * Now draw the cpu usage
101 if (nLinesToDraw)
102 FillRect(hDC, &rc, hBitmapBrush);
105 * Now that we are done drawing put the
106 * old bitmap back.
108 SelectObject(hDC, hOldBitmap);
110 iconInfo.fIcon = TRUE;
111 iconInfo.xHotspot = 0;
112 iconInfo.yHotspot = 0;
113 iconInfo.hbmMask = hBitmapMask;
114 iconInfo.hbmColor = hBitmap;
116 hTrayIcon = CreateIconIndirect(&iconInfo);
118 done:
120 * Cleanup
122 if (hScreenDC)
123 ReleaseDC(NULL, hScreenDC);
124 if (hDC)
125 DeleteDC(hDC);
126 if (hBitmapBrush)
127 DeleteObject(hBitmapBrush);
128 if (hBitmap)
129 DeleteObject(hBitmap);
130 if (hBitmapMask)
131 DeleteObject(hBitmapMask);
134 * Return the newly created tray icon (if successful)
136 return hTrayIcon;
139 BOOL TrayIcon_ShellAddTrayIcon(void)
141 NOTIFYICONDATAW nid;
142 HICON hIcon = NULL;
143 BOOL bRetVal;
144 WCHAR wszCPU_Usage[255];
146 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
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[255];
193 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
195 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
197 hIcon = TrayIcon_GetProcessorUsageIcon();
199 nid.cbSize = sizeof(NOTIFYICONDATAW);
200 nid.hWnd = hMainWnd;
201 nid.uID = 0;
202 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
203 nid.uCallbackMessage = WM_ONTRAYICON;
204 nid.hIcon = hIcon;
205 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
207 bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
209 if (hIcon)
210 DestroyIcon(hIcon);
212 return bRetVal;