version: Use the string value length when converting strings.
[wine.git] / dlls / shell32 / systray.c
bloba043912821f12a9984a0d52d1e61446bb1087abc
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 #define NONAMELESSUNION
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winnls.h"
31 #include "winuser.h"
32 #include "shellapi.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(systray);
38 static const WCHAR classname[] = /* Shell_TrayWnd */ {'S','h','e','l','l','_','T','r','a','y','W','n','d','\0'};
40 struct notify_data /* platform-independent format for NOTIFYICONDATA */
42 LONG hWnd;
43 UINT uID;
44 UINT uFlags;
45 UINT uCallbackMessage;
46 WCHAR szTip[128];
47 DWORD dwState;
48 DWORD dwStateMask;
49 WCHAR szInfo[256];
50 union {
51 UINT uTimeout;
52 UINT uVersion;
53 } u;
54 WCHAR szInfoTitle[64];
55 DWORD dwInfoFlags;
56 GUID guidItem;
57 /* data for the icon bitmap */
58 UINT width;
59 UINT height;
60 UINT planes;
61 UINT bpp;
64 /*************************************************************************
65 * Shell_NotifyIcon [SHELL32.296]
66 * Shell_NotifyIconA [SHELL32.297]
68 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid)
70 NOTIFYICONDATAW nidW;
71 INT cbSize;
73 /* Validate the cbSize as Windows XP does */
74 if (pnid->cbSize != NOTIFYICONDATAA_V1_SIZE &&
75 pnid->cbSize != NOTIFYICONDATAA_V2_SIZE &&
76 pnid->cbSize != NOTIFYICONDATAA_V3_SIZE &&
77 pnid->cbSize != sizeof(NOTIFYICONDATAA))
79 WARN("Invalid cbSize (%d) - using only Win95 fields (size=%d)\n",
80 pnid->cbSize, NOTIFYICONDATAA_V1_SIZE);
81 cbSize = NOTIFYICONDATAA_V1_SIZE;
83 else
84 cbSize = pnid->cbSize;
86 ZeroMemory(&nidW, sizeof(nidW));
87 nidW.cbSize = sizeof(nidW);
88 nidW.hWnd = pnid->hWnd;
89 nidW.uID = pnid->uID;
90 nidW.uFlags = pnid->uFlags;
91 nidW.uCallbackMessage = pnid->uCallbackMessage;
92 nidW.hIcon = pnid->hIcon;
94 /* szTip */
95 if (pnid->uFlags & NIF_TIP)
96 MultiByteToWideChar(CP_ACP, 0, pnid->szTip, -1, nidW.szTip, sizeof(nidW.szTip)/sizeof(WCHAR));
98 if (cbSize >= NOTIFYICONDATAA_V2_SIZE)
100 nidW.dwState = pnid->dwState;
101 nidW.dwStateMask = pnid->dwStateMask;
103 /* szInfo, szInfoTitle */
104 if (pnid->uFlags & NIF_INFO)
106 MultiByteToWideChar(CP_ACP, 0, pnid->szInfo, -1, nidW.szInfo, sizeof(nidW.szInfo)/sizeof(WCHAR));
107 MultiByteToWideChar(CP_ACP, 0, pnid->szInfoTitle, -1, nidW.szInfoTitle, sizeof(nidW.szInfoTitle)/sizeof(WCHAR));
110 nidW.u.uTimeout = pnid->u.uTimeout;
111 nidW.dwInfoFlags = pnid->dwInfoFlags;
114 if (cbSize >= NOTIFYICONDATAA_V3_SIZE)
115 nidW.guidItem = pnid->guidItem;
117 if (cbSize >= sizeof(NOTIFYICONDATAA))
118 nidW.hBalloonIcon = pnid->hBalloonIcon;
119 return Shell_NotifyIconW(dwMessage, &nidW);
122 /*************************************************************************
123 * Shell_NotifyIconW [SHELL32.298]
125 BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW nid)
127 HWND tray;
128 COPYDATASTRUCT cds;
129 struct notify_data data_buffer;
130 struct notify_data *data = &data_buffer;
131 BOOL ret;
133 TRACE("dwMessage = %d, nid->cbSize=%d\n", dwMessage, nid->cbSize);
135 /* Validate the cbSize so that WM_COPYDATA doesn't crash the application */
136 if (nid->cbSize != NOTIFYICONDATAW_V1_SIZE &&
137 nid->cbSize != NOTIFYICONDATAW_V2_SIZE &&
138 nid->cbSize != NOTIFYICONDATAW_V3_SIZE &&
139 nid->cbSize != sizeof(NOTIFYICONDATAW))
141 NOTIFYICONDATAW newNid;
143 WARN("Invalid cbSize (%d) - using only Win95 fields (size=%d)\n",
144 nid->cbSize, NOTIFYICONDATAW_V1_SIZE);
145 CopyMemory(&newNid, nid, NOTIFYICONDATAW_V1_SIZE);
146 newNid.cbSize = NOTIFYICONDATAW_V1_SIZE;
147 return Shell_NotifyIconW(dwMessage, &newNid);
150 tray = FindWindowExW(0, NULL, classname, NULL);
151 if (!tray) return FALSE;
153 cds.dwData = dwMessage;
154 cds.cbData = sizeof(*data);
155 memset( data, 0, sizeof(*data) );
157 /* FIXME: if statement only needed because we don't support interprocess
158 * icon handles */
159 if (nid->uFlags & NIF_ICON)
161 ICONINFO iconinfo;
162 BITMAP bmMask;
163 BITMAP bmColour;
164 LONG cbMaskBits;
165 LONG cbColourBits = 0;
166 char *buffer;
168 if (!GetIconInfo(nid->hIcon, &iconinfo))
169 goto noicon;
171 if (!GetObjectW(iconinfo.hbmMask, sizeof(bmMask), &bmMask) ||
172 (iconinfo.hbmColor && !GetObjectW(iconinfo.hbmColor, sizeof(bmColour), &bmColour)))
174 DeleteObject(iconinfo.hbmMask);
175 if (iconinfo.hbmColor) DeleteObject(iconinfo.hbmColor);
176 goto noicon;
179 cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel + 15) / 16 * 2;
180 if (iconinfo.hbmColor)
181 cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel + 15) / 16 * 2;
182 cds.cbData = sizeof(*data) + cbMaskBits + cbColourBits;
183 buffer = HeapAlloc(GetProcessHeap(), 0, cds.cbData);
184 if (!buffer)
186 DeleteObject(iconinfo.hbmMask);
187 if (iconinfo.hbmColor) DeleteObject(iconinfo.hbmColor);
188 return FALSE;
191 data = (struct notify_data *)buffer;
192 memset( data, 0, sizeof(*data) );
193 buffer += sizeof(*data);
194 GetBitmapBits(iconinfo.hbmMask, cbMaskBits, buffer);
195 if (!iconinfo.hbmColor)
197 data->width = bmMask.bmWidth;
198 data->height = bmMask.bmHeight / 2;
199 data->planes = 1;
200 data->bpp = 1;
202 else
204 data->width = bmColour.bmWidth;
205 data->height = bmColour.bmHeight;
206 data->planes = bmColour.bmPlanes;
207 data->bpp = bmColour.bmBitsPixel;
208 buffer += cbMaskBits;
209 GetBitmapBits(iconinfo.hbmColor, cbColourBits, buffer);
210 DeleteObject(iconinfo.hbmColor);
212 DeleteObject(iconinfo.hbmMask);
215 noicon:
216 data->hWnd = HandleToLong( nid->hWnd );
217 data->uID = nid->uID;
218 data->uFlags = nid->uFlags;
219 if (data->uFlags & NIF_MESSAGE)
220 data->uCallbackMessage = nid->uCallbackMessage;
221 if (data->uFlags & NIF_TIP)
222 lstrcpynW( data->szTip, nid->szTip, sizeof(data->szTip)/sizeof(WCHAR) );
223 if (data->uFlags & NIF_STATE)
225 data->dwState = nid->dwState;
226 data->dwStateMask = nid->dwStateMask;
228 if (data->uFlags & NIF_INFO)
230 lstrcpynW( data->szInfo, nid->szInfo, sizeof(data->szInfo)/sizeof(WCHAR) );
231 lstrcpynW( data->szInfoTitle, nid->szInfoTitle, sizeof(data->szInfoTitle)/sizeof(WCHAR) );
232 data->u.uTimeout = nid->u.uTimeout;
233 data->dwInfoFlags = nid->dwInfoFlags;
235 if (data->uFlags & NIF_GUID)
236 data->guidItem = nid->guidItem;
237 if (dwMessage == NIM_SETVERSION)
238 data->u.uVersion = nid->u.uVersion;
239 /* FIXME: balloon icon */
241 cds.lpData = data;
242 ret = SendMessageW(tray, WM_COPYDATA, (WPARAM)nid->hWnd, (LPARAM)&cds);
243 if (data != &data_buffer) HeapFree( GetProcessHeap(), 0, data );
244 return ret;