We are no longer generating .dbg.c files.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22     
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 <tchar.h>
30 #include <stdio.h>
31 #include <winnt.h>
32     
33 #include "taskmgr.h"
34 #include "perfdata.h"
35 #include "shellapi.h"
36
37 HICON TrayIcon_GetProcessorUsageIcon(void)
38 {
39     HICON        hTrayIcon = NULL;
40     HDC            hScreenDC = NULL;
41     HDC            hDC = NULL;
42     HBITMAP        hBitmap = NULL;
43     HBITMAP        hOldBitmap = NULL;
44     HBITMAP        hBitmapMask = NULL;
45     ICONINFO    iconInfo;
46     ULONG        ProcessorUsage;
47     int            nLinesToDraw;
48     HBRUSH        hBitmapBrush = NULL;
49     RECT        rc;
50
51     /*
52      * Get a handle to the screen DC
53      */
54     hScreenDC = GetDC(NULL);
55     if (!hScreenDC)
56         goto done;
57     
58     /*
59      * Create our own DC from it
60      */
61     hDC = CreateCompatibleDC(hScreenDC);
62     if (!hDC)
63         goto done;
64
65     /*
66      * Load the bitmaps
67      */
68     hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYICON));
69     hBitmapMask = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TRAYMASK));
70     if (!hBitmap || !hBitmapMask)
71         goto done;
72
73     hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
74     if (!hBitmapBrush)
75         goto done;
76     
77     /*
78      * Select the bitmap into our device context
79      * so we can draw on it.
80      */
81     hOldBitmap = (HBITMAP) SelectObject(hDC, hBitmap);
82
83     /*
84      * Get the cpu usage
85      */
86     ProcessorUsage = PerfDataGetProcessorUsage();
87
88     /*
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.
93      */
94     nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
95     rc.left = 3;
96     rc.top = 12 - nLinesToDraw;
97     rc.right = 13;
98     rc.bottom = 13;
99
100     /*
101      * Now draw the cpu usage
102      */
103     if (nLinesToDraw)
104         FillRect(hDC, &rc, hBitmapBrush);
105
106     /*
107      * Now that we are done drawing put the
108      * old bitmap back.
109      */
110     SelectObject(hDC, hOldBitmap);
111     hOldBitmap = NULL;
112     
113     iconInfo.fIcon = TRUE;
114     iconInfo.xHotspot = 0;
115     iconInfo.yHotspot = 0;
116     iconInfo.hbmMask = hBitmapMask;
117     iconInfo.hbmColor = hBitmap;
118
119     hTrayIcon = CreateIconIndirect(&iconInfo);
120
121 done:
122     /*
123      * Cleanup
124      */
125     if (hScreenDC)
126         ReleaseDC(NULL, hScreenDC);
127     if (hOldBitmap)
128         SelectObject(hDC, hOldBitmap);
129     if (hDC)
130         DeleteDC(hDC);
131     if (hBitmapBrush)
132         DeleteObject(hBitmapBrush);
133     if (hBitmap)
134         DeleteObject(hBitmap);
135     if (hBitmapMask)
136         DeleteObject(hBitmapMask);
137     
138     /*
139      * Return the newly created tray icon (if successful)
140      */
141     return hTrayIcon;
142 }
143
144 BOOL TrayIcon_ShellAddTrayIcon(void)
145 {
146     NOTIFYICONDATA    nid;
147     HICON            hIcon = NULL;
148     BOOL            bRetVal;
149
150     memset(&nid, 0, sizeof(NOTIFYICONDATA));
151
152     hIcon = TrayIcon_GetProcessorUsageIcon();
153
154     nid.cbSize = sizeof(NOTIFYICONDATA);
155     nid.hWnd = hMainWnd;
156     nid.uID = 0;
157     nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
158     nid.uCallbackMessage = WM_ONTRAYICON;
159     nid.hIcon = hIcon;
160     wsprintf(nid.szTip, _T("CPU Usage: %d%%"), PerfDataGetProcessorUsage());
161
162     bRetVal = Shell_NotifyIcon(NIM_ADD, &nid);
163
164     if (hIcon)
165         DestroyIcon(hIcon);
166
167     return bRetVal;
168 }
169
170 BOOL TrayIcon_ShellRemoveTrayIcon(void)
171 {
172     NOTIFYICONDATA    nid;
173     BOOL            bRetVal;
174     
175     memset(&nid, 0, sizeof(NOTIFYICONDATA));
176     
177     nid.cbSize = sizeof(NOTIFYICONDATA);
178     nid.hWnd = hMainWnd;
179     nid.uID = 0;
180     nid.uFlags = 0;
181     nid.uCallbackMessage = WM_ONTRAYICON;
182     
183     bRetVal = Shell_NotifyIcon(NIM_DELETE, &nid);
184     
185     return bRetVal;
186 }
187
188 BOOL TrayIcon_ShellUpdateTrayIcon(void)
189 {
190     NOTIFYICONDATA    nid;
191     HICON            hIcon = NULL;
192     BOOL            bRetVal;
193     
194     memset(&nid, 0, sizeof(NOTIFYICONDATA));
195     
196     hIcon = TrayIcon_GetProcessorUsageIcon();
197     
198     nid.cbSize = sizeof(NOTIFYICONDATA);
199     nid.hWnd = hMainWnd;
200     nid.uID = 0;
201     nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
202     nid.uCallbackMessage = WM_ONTRAYICON;
203     nid.hIcon = hIcon;
204     wsprintf(nid.szTip, _T("CPU Usage: %d%%"), PerfDataGetProcessorUsage());
205     
206     bRetVal = Shell_NotifyIcon(NIM_MODIFY, &nid);
207     
208     if (hIcon)
209         DestroyIcon(hIcon);
210     
211     return bRetVal;
212 }