taskmgr: Fix includes order and avoid tchar.h and memory.h.
[wine] / programs / taskmgr / trayicon.c
1 /*
2  *  ReactOS Task Manager
3  *
4  *  trayicon.c
5  *
6  *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <windows.h>
27 #include <commctrl.h>
28 #include <winnt.h>
29 #include <shellapi.h>
30
31 #include "wine/unicode.h"
32 #include "taskmgr.h"
33 #include "perfdata.h"
34
35 static HICON TrayIcon_GetProcessorUsageIcon(void)
36 {
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;
48
49     /*
50      * Get a handle to the screen DC
51      */
52     hScreenDC = GetDC(NULL);
53     if (!hScreenDC)
54         goto done;
55     
56     /*
57      * Create our own DC from it
58      */
59     hDC = CreateCompatibleDC(hScreenDC);
60     if (!hDC)
61         goto done;
62
63     /*
64      * Load the bitmaps
65      */
66     hBitmap = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYICON));
67     hBitmapMask = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYMASK));
68     if (!hBitmap || !hBitmapMask)
69         goto done;
70
71     hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
72     if (!hBitmapBrush)
73         goto done;
74     
75     /*
76      * Select the bitmap into our device context
77      * so we can draw on it.
78      */
79     hOldBitmap = SelectObject(hDC, hBitmap);
80
81     /*
82      * Get the cpu usage
83      */
84     ProcessorUsage = PerfDataGetProcessorUsage();
85
86     /*
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.
91      */
92     nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
93     rc.left = 3;
94     rc.top = 12 - nLinesToDraw;
95     rc.right = 13;
96     rc.bottom = 13;
97
98     /*
99      * Now draw the cpu usage
100      */
101     if (nLinesToDraw)
102         FillRect(hDC, &rc, hBitmapBrush);
103
104     /*
105      * Now that we are done drawing put the
106      * old bitmap back.
107      */
108     SelectObject(hDC, hOldBitmap);
109
110     iconInfo.fIcon = TRUE;
111     iconInfo.xHotspot = 0;
112     iconInfo.yHotspot = 0;
113     iconInfo.hbmMask = hBitmapMask;
114     iconInfo.hbmColor = hBitmap;
115
116     hTrayIcon = CreateIconIndirect(&iconInfo);
117
118 done:
119     /*
120      * Cleanup
121      */
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);
132     
133     /*
134      * Return the newly created tray icon (if successful)
135      */
136     return hTrayIcon;
137 }
138
139 BOOL TrayIcon_ShellAddTrayIcon(void)
140 {
141     NOTIFYICONDATAW    nid;
142     HICON            hIcon = NULL;
143     BOOL            bRetVal;
144     WCHAR           wszCPU_Usage[255];
145
146     LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
147
148     memset(&nid, 0, sizeof(NOTIFYICONDATAW));
149
150     hIcon = TrayIcon_GetProcessorUsageIcon();
151
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());
159
160     bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
161
162     if (hIcon)
163         DestroyIcon(hIcon);
164
165     return bRetVal;
166 }
167
168 BOOL TrayIcon_ShellRemoveTrayIcon(void)
169 {
170     NOTIFYICONDATAW    nid;
171     BOOL            bRetVal;
172     
173     memset(&nid, 0, sizeof(NOTIFYICONDATAW));
174     
175     nid.cbSize = sizeof(NOTIFYICONDATAW);
176     nid.hWnd = hMainWnd;
177     nid.uID = 0;
178     nid.uFlags = 0;
179     nid.uCallbackMessage = WM_ONTRAYICON;
180     
181     bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
182     
183     return bRetVal;
184 }
185
186 BOOL TrayIcon_ShellUpdateTrayIcon(void)
187 {
188     NOTIFYICONDATAW    nid;
189     HICON            hIcon = NULL;
190     BOOL            bRetVal;
191     WCHAR           wszCPU_Usage[255];
192
193     LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
194     
195     memset(&nid, 0, sizeof(NOTIFYICONDATAW));
196     
197     hIcon = TrayIcon_GetProcessorUsageIcon();
198     
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());
206     
207     bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
208     
209     if (hIcon)
210         DestroyIcon(hIcon);
211     
212     return bRetVal;
213 }