trayicon.c 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  ReactOS Task Manager
 *
 *  trayicon.c
 *
 *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 24 25 26 27 28 29
 */
    
#define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
#include <windows.h>
#include <commctrl.h>
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <winnt.h>
30 31 32
#include <shellapi.h>

#include "wine/unicode.h"
33 34 35
#include "taskmgr.h"
#include "perfdata.h"

36
static HICON TrayIcon_GetProcessorUsageIcon(void)
37 38 39 40 41
{
    HICON        hTrayIcon = NULL;
    HDC            hScreenDC = NULL;
    HDC            hDC = NULL;
    HBITMAP        hBitmap = NULL;
42
    HBITMAP        hOldBitmap;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    HBITMAP        hBitmapMask = NULL;
    ICONINFO    iconInfo;
    ULONG        ProcessorUsage;
    int            nLinesToDraw;
    HBRUSH        hBitmapBrush = NULL;
    RECT        rc;

    /*
     * Get a handle to the screen DC
     */
    hScreenDC = GetDC(NULL);
    if (!hScreenDC)
        goto done;
    
    /*
     * Create our own DC from it
     */
    hDC = CreateCompatibleDC(hScreenDC);
    if (!hDC)
        goto done;

    /*
     * Load the bitmaps
     */
67 68
    hBitmap = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYICON));
    hBitmapMask = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYMASK));
69 70 71 72 73 74 75 76 77 78 79
    if (!hBitmap || !hBitmapMask)
        goto done;

    hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
    if (!hBitmapBrush)
        goto done;
    
    /*
     * Select the bitmap into our device context
     * so we can draw on it.
     */
80
    hOldBitmap = SelectObject(hDC, hBitmap);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    /*
     * Get the cpu usage
     */
    ProcessorUsage = PerfDataGetProcessorUsage();

    /*
     * Calculate how many lines to draw
     * since we have 11 rows of space
     * to draw the cpu usage instead of
     * just having 10.
     */
    nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
    rc.left = 3;
    rc.top = 12 - nLinesToDraw;
    rc.right = 13;
    rc.bottom = 13;

    /*
     * Now draw the cpu usage
     */
    if (nLinesToDraw)
        FillRect(hDC, &rc, hBitmapBrush);

    /*
     * Now that we are done drawing put the
     * old bitmap back.
     */
    SelectObject(hDC, hOldBitmap);
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    iconInfo.fIcon = TRUE;
    iconInfo.xHotspot = 0;
    iconInfo.yHotspot = 0;
    iconInfo.hbmMask = hBitmapMask;
    iconInfo.hbmColor = hBitmap;

    hTrayIcon = CreateIconIndirect(&iconInfo);

done:
    /*
     * Cleanup
     */
    if (hScreenDC)
        ReleaseDC(NULL, hScreenDC);
    if (hDC)
        DeleteDC(hDC);
    if (hBitmapBrush)
        DeleteObject(hBitmapBrush);
    if (hBitmap)
        DeleteObject(hBitmap);
    if (hBitmapMask)
        DeleteObject(hBitmapMask);
    
    /*
     * Return the newly created tray icon (if successful)
     */
    return hTrayIcon;
}

BOOL TrayIcon_ShellAddTrayIcon(void)
{
142
    NOTIFYICONDATAW    nid;
143 144
    HICON            hIcon = NULL;
    BOOL            bRetVal;
145 146 147
    WCHAR           wszCPU_Usage[255];

    LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
148

149
    memset(&nid, 0, sizeof(NOTIFYICONDATAW));
150 151 152

    hIcon = TrayIcon_GetProcessorUsageIcon();

153
    nid.cbSize = sizeof(NOTIFYICONDATAW);
154 155 156
    nid.hWnd = hMainWnd;
    nid.uID = 0;
    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 164 165 166 167 168 169 170

    if (hIcon)
        DestroyIcon(hIcon);

    return bRetVal;
}

BOOL TrayIcon_ShellRemoveTrayIcon(void)
{
171
    NOTIFYICONDATAW    nid;
172 173
    BOOL            bRetVal;
    
174
    memset(&nid, 0, sizeof(NOTIFYICONDATAW));
175
    
176
    nid.cbSize = sizeof(NOTIFYICONDATAW);
177 178 179
    nid.hWnd = hMainWnd;
    nid.uID = 0;
    nid.uFlags = 0;
180
    nid.uCallbackMessage = WM_ONTRAYICON;
181
    
182
    bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
183 184 185 186 187 188
    
    return bRetVal;
}

BOOL TrayIcon_ShellUpdateTrayIcon(void)
{
189
    NOTIFYICONDATAW    nid;
190 191
    HICON            hIcon = NULL;
    BOOL            bRetVal;
192 193 194
    WCHAR           wszCPU_Usage[255];

    LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
195
    
196
    memset(&nid, 0, sizeof(NOTIFYICONDATAW));
197 198 199
    
    hIcon = TrayIcon_GetProcessorUsageIcon();
    
200
    nid.cbSize = sizeof(NOTIFYICONDATAW);
201 202 203
    nid.hWnd = hMainWnd;
    nid.uID = 0;
    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 211 212 213 214
    
    if (hIcon)
        DestroyIcon(hIcon);
    
    return bRetVal;
}