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