widl: Properly align name table entries.
[wine.git] / dlls / shell32 / systray.c
blob78717cfcdcafcf774b6222e142813fc7f1c3983c
1 /*
2 * Systray handling
4 * Copyright 1999 Kai Morich <kai.morich@bigfoot.de>
5 * Copyright 2004 Mike Hearn, for CodeWeavers
6 * Copyright 2005 Robert Shearman
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.
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.
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
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winnls.h"
29 #include "winuser.h"
30 #include "shellapi.h"
31 #include "shell32_main.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(systray);
37 struct notify_data /* platform-independent format for NOTIFYICONDATA */
39 LONG hWnd;
40 UINT uID;
41 UINT uFlags;
42 UINT uCallbackMessage;
43 WCHAR szTip[128];
44 DWORD dwState;
45 DWORD dwStateMask;
46 WCHAR szInfo[256];
47 union {
48 UINT uTimeout;
49 UINT uVersion;
50 } u;
51 WCHAR szInfoTitle[64];
52 DWORD dwInfoFlags;
53 GUID guidItem;
54 /* data for the icon bitmap */
55 UINT width;
56 UINT height;
57 UINT planes;
58 UINT bpp;
61 /*************************************************************************
62 * Shell_NotifyIcon [SHELL32.296]
63 * Shell_NotifyIconA [SHELL32.297]
65 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid)
67 NOTIFYICONDATAW nidW;
68 INT cbSize;
70 /* Validate the cbSize as Windows XP does */
71 if (pnid->cbSize != NOTIFYICONDATAA_V1_SIZE &&
72 pnid->cbSize != NOTIFYICONDATAA_V2_SIZE &&
73 pnid->cbSize != NOTIFYICONDATAA_V3_SIZE &&
74 pnid->cbSize != sizeof(NOTIFYICONDATAA))
76 WARN("Invalid cbSize (%ld) - using only Win95 fields (size=%ld)\n",
77 pnid->cbSize, NOTIFYICONDATAA_V1_SIZE);
78 cbSize = NOTIFYICONDATAA_V1_SIZE;
80 else
81 cbSize = pnid->cbSize;
83 ZeroMemory(&nidW, sizeof(nidW));
84 nidW.cbSize = sizeof(nidW);
85 nidW.hWnd = pnid->hWnd;
86 nidW.uID = pnid->uID;
87 nidW.uFlags = pnid->uFlags;
88 nidW.uCallbackMessage = pnid->uCallbackMessage;
89 nidW.hIcon = pnid->hIcon;
91 /* szTip */
92 if (pnid->uFlags & NIF_TIP)
93 MultiByteToWideChar(CP_ACP, 0, pnid->szTip, -1, nidW.szTip, ARRAY_SIZE(nidW.szTip));
95 if (cbSize >= NOTIFYICONDATAA_V2_SIZE)
97 nidW.dwState = pnid->dwState;
98 nidW.dwStateMask = pnid->dwStateMask;
100 /* szInfo, szInfoTitle */
101 if (pnid->uFlags & NIF_INFO)
103 MultiByteToWideChar(CP_ACP, 0, pnid->szInfo, -1, nidW.szInfo, ARRAY_SIZE(nidW.szInfo));
104 MultiByteToWideChar(CP_ACP, 0, pnid->szInfoTitle, -1, nidW.szInfoTitle, ARRAY_SIZE(nidW.szInfoTitle));
107 nidW.uTimeout = pnid->uTimeout;
108 nidW.dwInfoFlags = pnid->dwInfoFlags;
111 if (cbSize >= NOTIFYICONDATAA_V3_SIZE)
112 nidW.guidItem = pnid->guidItem;
114 if (cbSize >= sizeof(NOTIFYICONDATAA))
115 nidW.hBalloonIcon = pnid->hBalloonIcon;
116 return Shell_NotifyIconW(dwMessage, &nidW);
119 /*************************************************************************
120 * Shell_NotifyIconW [SHELL32.298]
122 BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW nid)
124 HWND tray;
125 COPYDATASTRUCT cds;
126 struct notify_data data_buffer;
127 struct notify_data *data = &data_buffer;
128 BOOL ret;
130 TRACE("dwMessage = %ld, nid->cbSize=%ld\n", dwMessage, nid->cbSize);
132 /* Validate the cbSize so that WM_COPYDATA doesn't crash the application */
133 if (nid->cbSize != NOTIFYICONDATAW_V1_SIZE &&
134 nid->cbSize != NOTIFYICONDATAW_V2_SIZE &&
135 nid->cbSize != NOTIFYICONDATAW_V3_SIZE &&
136 nid->cbSize != sizeof(NOTIFYICONDATAW))
138 NOTIFYICONDATAW newNid;
140 WARN("Invalid cbSize (%ld) - using only Win95 fields (size=%ld)\n",
141 nid->cbSize, NOTIFYICONDATAW_V1_SIZE);
142 CopyMemory(&newNid, nid, NOTIFYICONDATAW_V1_SIZE);
143 newNid.cbSize = NOTIFYICONDATAW_V1_SIZE;
144 return Shell_NotifyIconW(dwMessage, &newNid);
147 tray = FindWindowExW(0, NULL, L"Shell_TrayWnd", NULL);
148 if (!tray)
150 SetLastError(E_FAIL);
151 return FALSE;
154 cds.dwData = dwMessage;
155 cds.cbData = sizeof(*data);
156 memset( data, 0, sizeof(*data) );
158 /* FIXME: if statement only needed because we don't support interprocess
159 * icon handles */
160 if (nid->uFlags & NIF_ICON)
162 ICONINFO iconinfo;
163 BITMAP bmMask;
164 BITMAP bmColour;
165 LONG cbMaskBits;
166 LONG cbColourBits = 0;
167 char *buffer;
169 if (!GetIconInfo(nid->hIcon, &iconinfo))
170 goto noicon;
172 if (!GetObjectW(iconinfo.hbmMask, sizeof(bmMask), &bmMask) ||
173 (iconinfo.hbmColor && !GetObjectW(iconinfo.hbmColor, sizeof(bmColour), &bmColour)))
175 DeleteObject(iconinfo.hbmMask);
176 if (iconinfo.hbmColor) DeleteObject(iconinfo.hbmColor);
177 goto noicon;
180 cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel + 15) / 16 * 2;
181 if (iconinfo.hbmColor)
182 cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel + 15) / 16 * 2;
183 cds.cbData = sizeof(*data) + cbMaskBits + cbColourBits;
184 buffer = malloc(cds.cbData);
185 if (!buffer)
187 DeleteObject(iconinfo.hbmMask);
188 if (iconinfo.hbmColor) DeleteObject(iconinfo.hbmColor);
189 SetLastError(E_OUTOFMEMORY);
190 return FALSE;
193 data = (struct notify_data *)buffer;
194 memset( data, 0, sizeof(*data) );
195 buffer += sizeof(*data);
196 GetBitmapBits(iconinfo.hbmMask, cbMaskBits, buffer);
197 if (!iconinfo.hbmColor)
199 data->width = bmMask.bmWidth;
200 data->height = bmMask.bmHeight / 2;
201 data->planes = 1;
202 data->bpp = 1;
204 else
206 data->width = bmColour.bmWidth;
207 data->height = bmColour.bmHeight;
208 data->planes = bmColour.bmPlanes;
209 data->bpp = bmColour.bmBitsPixel;
210 buffer += cbMaskBits;
211 GetBitmapBits(iconinfo.hbmColor, cbColourBits, buffer);
212 DeleteObject(iconinfo.hbmColor);
214 DeleteObject(iconinfo.hbmMask);
217 noicon:
218 data->hWnd = HandleToLong( nid->hWnd );
219 data->uID = nid->uID;
220 data->uFlags = nid->uFlags;
221 if (data->uFlags & NIF_MESSAGE)
222 data->uCallbackMessage = nid->uCallbackMessage;
223 if (data->uFlags & NIF_TIP)
224 lstrcpynW( data->szTip, nid->szTip, ARRAY_SIZE(data->szTip));
225 if (data->uFlags & NIF_STATE)
227 data->dwState = nid->dwState;
228 data->dwStateMask = nid->dwStateMask;
230 if (data->uFlags & NIF_INFO)
232 lstrcpynW( data->szInfo, nid->szInfo, ARRAY_SIZE(data->szInfo) );
233 lstrcpynW( data->szInfoTitle, nid->szInfoTitle, ARRAY_SIZE(data->szInfoTitle));
234 data->u.uTimeout = nid->uTimeout;
235 data->dwInfoFlags = nid->dwInfoFlags;
237 if (data->uFlags & NIF_GUID)
238 data->guidItem = nid->guidItem;
239 if (dwMessage == NIM_SETVERSION)
240 data->u.uVersion = nid->uVersion;
241 /* FIXME: balloon icon */
243 cds.lpData = data;
244 ret = SendMessageW(tray, WM_COPYDATA, (WPARAM)nid->hWnd, (LPARAM)&cds);
245 if (data != &data_buffer) free(data);
246 SetLastError(ret ? S_OK : E_FAIL);
247 return ret;
250 /*************************************************************************
251 * Shell_NotifyIconGetRect [SHELL32.@]
253 HRESULT WINAPI Shell_NotifyIconGetRect(const NOTIFYICONIDENTIFIER* identifier, RECT* icon_location)
255 FIXME("stub (%p) (%p)\n", identifier, icon_location);
256 return E_NOTIMPL;