winealsa: Add control 'Digital' as alias for microphone.
[wine] / dlls / shell32 / systray.c
1 /*
2  * Systray handling
3  *
4  * Copyright 1999 Kai Morich    <kai.morich@bigfoot.de>
5  * Copyright 2004 Mike Hearn, for CodeWeavers
6  * Copyright 2005 Robert Shearman
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 NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winnls.h"
32 #include "winuser.h"
33 #include "shellapi.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(systray);
38
39 static const WCHAR classname[] = /* Shell_TrayWnd */ {'S','h','e','l','l','_','T','r','a','y','W','n','d','\0'};
40
41 /*************************************************************************
42  * Shell_NotifyIcon                     [SHELL32.296]
43  * Shell_NotifyIconA                    [SHELL32.297]
44  */
45 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid)
46 {
47     NOTIFYICONDATAW nidW;
48     INT cbSize;
49
50     /* Validate the cbSize as Windows XP does */
51     if (pnid->cbSize != NOTIFYICONDATAA_V1_SIZE &&
52         pnid->cbSize != NOTIFYICONDATAA_V2_SIZE &&
53         pnid->cbSize != NOTIFYICONDATAA_V3_SIZE &&
54         pnid->cbSize != sizeof(NOTIFYICONDATAA))
55     {
56         WARN("Invalid cbSize (%d) - using only Win95 fields (size=%d)\n",
57             pnid->cbSize, NOTIFYICONDATAA_V1_SIZE);
58         cbSize = NOTIFYICONDATAA_V1_SIZE;
59     }
60     else
61         cbSize = pnid->cbSize;
62
63     ZeroMemory(&nidW, sizeof(nidW));
64     nidW.cbSize = sizeof(nidW);
65     nidW.hWnd   = pnid->hWnd;
66     nidW.uID    = pnid->uID;
67     nidW.uFlags = pnid->uFlags;
68     nidW.uCallbackMessage = pnid->uCallbackMessage;
69     nidW.hIcon  = pnid->hIcon;
70
71     /* szTip */
72     if (pnid->uFlags & NIF_TIP)
73         MultiByteToWideChar(CP_ACP, 0, pnid->szTip, -1, nidW.szTip, sizeof(nidW.szTip)/sizeof(WCHAR));
74
75     if (cbSize >= NOTIFYICONDATAA_V2_SIZE)
76     {
77         nidW.dwState      = pnid->dwState;
78         nidW.dwStateMask  = pnid->dwStateMask;
79
80         /* szInfo, szInfoTitle */
81         if (pnid->uFlags & NIF_INFO)
82         {
83             MultiByteToWideChar(CP_ACP, 0, pnid->szInfo, -1,  nidW.szInfo, sizeof(nidW.szInfo)/sizeof(WCHAR));
84             MultiByteToWideChar(CP_ACP, 0, pnid->szInfoTitle, -1, nidW.szInfoTitle, sizeof(nidW.szInfoTitle)/sizeof(WCHAR));
85         }
86
87         nidW.u.uTimeout = pnid->u.uTimeout;
88         nidW.dwInfoFlags = pnid->dwInfoFlags;
89     }
90     
91     if (cbSize >= NOTIFYICONDATAA_V3_SIZE)
92         nidW.guidItem = pnid->guidItem;
93
94     if (cbSize >= sizeof(NOTIFYICONDATAA))
95         nidW.hBalloonIcon = pnid->hBalloonIcon;
96     return Shell_NotifyIconW(dwMessage, &nidW);
97 }
98
99 /*************************************************************************
100  * Shell_NotifyIconW                    [SHELL32.298]
101  */
102 BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW nid)
103 {
104     HWND tray;
105     COPYDATASTRUCT cds;
106     char *buffer = NULL;
107
108     TRACE("dwMessage = %d, nid->cbSize=%d\n", dwMessage, nid->cbSize);
109
110     /* Validate the cbSize so that WM_COPYDATA doesn't crash the application */
111     if (nid->cbSize != NOTIFYICONDATAW_V1_SIZE &&
112         nid->cbSize != NOTIFYICONDATAW_V2_SIZE &&
113         nid->cbSize != NOTIFYICONDATAW_V3_SIZE &&
114         nid->cbSize != sizeof(NOTIFYICONDATAW))
115     {
116         NOTIFYICONDATAW newNid;
117
118         WARN("Invalid cbSize (%d) - using only Win95 fields (size=%d)\n",
119             nid->cbSize, NOTIFYICONDATAW_V1_SIZE);
120         CopyMemory(&newNid, nid, NOTIFYICONDATAW_V1_SIZE);
121         newNid.cbSize = NOTIFYICONDATAW_V1_SIZE;
122         return Shell_NotifyIconW(dwMessage, &newNid);
123     }
124
125     tray = FindWindowExW(0, NULL, classname, NULL);
126     if (!tray) return FALSE;
127
128     cds.dwData = dwMessage;
129
130     /* FIXME: if statement only needed because we don't support interprocess
131      * icon handles */
132     if (nid->uFlags & NIF_ICON)
133     {
134         ICONINFO iconinfo;
135         BITMAP bmMask;
136         BITMAP bmColour;
137         LONG cbMaskBits;
138         LONG cbColourBits;
139
140         if (!GetIconInfo(nid->hIcon, &iconinfo))
141             goto noicon;
142
143         if (!GetObjectW(iconinfo.hbmMask, sizeof(bmMask), &bmMask) ||
144             !GetObjectW(iconinfo.hbmColor, sizeof(bmColour), &bmColour))
145         {
146             DeleteObject(iconinfo.hbmMask);
147             DeleteObject(iconinfo.hbmColor);
148             goto noicon;
149         }
150
151         cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel) / 8;
152         cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel) / 8;
153         cds.cbData = nid->cbSize + 2*sizeof(BITMAP) + cbMaskBits + cbColourBits;
154         buffer = HeapAlloc(GetProcessHeap(), 0, cds.cbData);
155         if (!buffer)
156         {
157             DeleteObject(iconinfo.hbmMask);
158             DeleteObject(iconinfo.hbmColor);
159             return FALSE;
160         }
161         cds.lpData = buffer;
162
163         memcpy(buffer, nid, nid->cbSize);
164         buffer += nid->cbSize;
165         memcpy(buffer, &bmMask, sizeof(bmMask));
166         buffer += sizeof(bmMask);
167         memcpy(buffer, &bmColour, sizeof(bmColour));
168         buffer += sizeof(bmColour);
169         GetBitmapBits(iconinfo.hbmMask, cbMaskBits, buffer);
170         buffer += cbMaskBits;
171         GetBitmapBits(iconinfo.hbmColor, cbColourBits, buffer);
172
173         /* Reset pointer to allocated block so it can be freed later.
174          * Note that cds.lpData cannot be passed to HeapFree since it
175          * points to nid when no icon info is found. */
176         buffer = cds.lpData;
177
178         DeleteObject(iconinfo.hbmMask);
179         DeleteObject(iconinfo.hbmColor);
180     }
181     else
182     {
183 noicon:
184         cds.cbData = nid->cbSize;
185         cds.lpData = nid;
186     }
187
188     SendMessageW(tray, WM_COPYDATA, (WPARAM)nid->hWnd, (LPARAM)&cds);
189
190     /* FIXME: if statement only needed because we don't support interprocess
191      * icon handles */
192     HeapFree(GetProcessHeap(), 0, buffer);
193
194     return TRUE;
195 }