comctl32/datetime: Fix AM/PM handling with 12-hour fields.
[wine] / programs / explorer / appbar.c
1 /*
2  * SHAppBarMessage implementation
3  *
4  * Copyright 2008 Vincent Povirk for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * TODO: freedesktop _NET_WM_STRUT integration
21  *
22  * TODO: find when a fullscreen app is in the foreground and send FULLSCREENAPP
23  *  notifications
24  *
25  * TODO: detect changes in the screen size and send ABN_POSCHANGED ?
26  *
27  * TODO: multiple monitor support
28  */
29
30 #include "windows.h"
31 #include "shellapi.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "explorer_private.h"
35
36 #include "wine/list.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(appbar);
39
40 struct appbar_data_msg  /* platform-independent data */
41 {
42     LONG      hWnd;
43     UINT      uCallbackMessage;
44     UINT      uEdge;
45     RECT      rc;
46     ULONGLONG lParam;
47 };
48
49 struct appbar_cmd
50 {
51     ULONG  return_map;
52     DWORD  return_process;
53     struct appbar_data_msg abd;
54 };
55
56 struct appbar_response
57 {
58     ULONGLONG result;
59     struct appbar_data_msg abd;
60 };
61
62 static HWND appbarmsg_window = NULL;
63
64 struct appbar_data
65 {
66     struct list entry;
67     HWND hwnd;
68     UINT callback_msg;
69     UINT edge;
70     RECT rc;
71     BOOL space_reserved;
72     /* BOOL autohide; */
73 };
74
75 static struct list appbars = LIST_INIT(appbars);
76
77 static struct appbar_data* get_appbar(HWND hwnd)
78 {
79     struct appbar_data* data;
80
81     LIST_FOR_EACH_ENTRY(data, &appbars, struct appbar_data, entry)
82     {
83         if (data->hwnd == hwnd)
84             return data;
85     }
86
87     return NULL;
88 }
89
90 /* send_poschanged: send ABN_POSCHANGED to every appbar except one */
91 static void send_poschanged(HWND hwnd)
92 {
93     struct appbar_data* data;
94     LIST_FOR_EACH_ENTRY(data, &appbars, struct appbar_data, entry)
95     {
96         if (data->hwnd != hwnd)
97         {
98             PostMessageW(data->hwnd, data->callback_msg, ABN_POSCHANGED, 0);
99         }
100     }
101 }
102
103 /* appbar_cliprect: cut out parts of the rectangle that interfere with existing appbars */
104 static void appbar_cliprect( HWND hwnd, RECT *rect )
105 {
106     struct appbar_data* data;
107     LIST_FOR_EACH_ENTRY(data, &appbars, struct appbar_data, entry)
108     {
109         if (data->hwnd == hwnd)
110         {
111             /* we only care about appbars that were added before this one */
112             return;
113         }
114         if (data->space_reserved)
115         {
116             /* move in the side that corresponds to the other appbar's edge */
117             switch (data->edge)
118             {
119             case ABE_BOTTOM:
120                 rect->bottom = min(rect->bottom, data->rc.top);
121                 break;
122             case ABE_LEFT:
123                 rect->left = max(rect->left, data->rc.right);
124                 break;
125             case ABE_RIGHT:
126                 rect->right = min(rect->right, data->rc.left);
127                 break;
128             case ABE_TOP:
129                 rect->top = max(rect->top, data->rc.bottom);
130                 break;
131             }
132         }
133     }
134 }
135
136 static UINT_PTR handle_appbarmessage(DWORD msg, struct appbar_data_msg *abd)
137 {
138     struct appbar_data* data;
139     HWND hwnd = LongToHandle( abd->hWnd );
140
141     switch (msg)
142     {
143     case ABM_NEW:
144         if (get_appbar(hwnd))
145         {
146             /* fail when adding an hwnd the second time */
147             return FALSE;
148         }
149
150         data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct appbar_data));
151         if (!data)
152         {
153             WINE_ERR("out of memory\n");
154             return FALSE;
155         }
156         data->hwnd = hwnd;
157         data->callback_msg = abd->uCallbackMessage;
158
159         list_add_tail(&appbars, &data->entry);
160
161         return TRUE;
162     case ABM_REMOVE:
163         if ((data = get_appbar(hwnd)))
164         {
165             list_remove(&data->entry);
166
167             send_poschanged(hwnd);
168
169             HeapFree(GetProcessHeap(), 0, data);
170         }
171         else
172             WINE_WARN("removing hwnd %p not on the list\n", hwnd);
173         return TRUE;
174     case ABM_QUERYPOS:
175         if (abd->uEdge > ABE_BOTTOM)
176             WINE_WARN("invalid edge %i for %p\n", abd->uEdge, hwnd);
177         appbar_cliprect( hwnd, &abd->rc );
178         return TRUE;
179     case ABM_SETPOS:
180         if (abd->uEdge > ABE_BOTTOM)
181         {
182             WINE_WARN("invalid edge %i for %p\n", abd->uEdge, hwnd);
183             return TRUE;
184         }
185         if ((data = get_appbar(hwnd)))
186         {
187             /* calculate acceptable space */
188             appbar_cliprect( hwnd, &abd->rc );
189
190             if (!EqualRect(&abd->rc, &data->rc))
191                 send_poschanged(hwnd);
192
193             /* reserve that space for this appbar */
194             data->edge = abd->uEdge;
195             data->rc = abd->rc;
196             data->space_reserved = TRUE;
197         }
198         else
199         {
200             WINE_WARN("app sent ABM_SETPOS message for %p without ABM_ADD\n", hwnd);
201         }
202         return TRUE;
203     case ABM_GETSTATE:
204         WINE_FIXME("SHAppBarMessage(ABM_GETSTATE): stub\n");
205         return ABS_ALWAYSONTOP | ABS_AUTOHIDE;
206     case ABM_GETTASKBARPOS:
207         WINE_FIXME("SHAppBarMessage(ABM_GETTASKBARPOS, hwnd=%p): stub\n", hwnd);
208         /* Report the taskbar is at the bottom of the screen. */
209         abd->rc.left = 0;
210         abd->rc.right = GetSystemMetrics(SM_CXSCREEN);
211         abd->rc.bottom = GetSystemMetrics(SM_CYSCREEN);
212         abd->rc.top = abd->rc.bottom-1;
213         abd->uEdge = ABE_BOTTOM;
214         return TRUE;
215     case ABM_ACTIVATE:
216         return TRUE;
217     case ABM_GETAUTOHIDEBAR:
218         WINE_FIXME("SHAppBarMessage(ABM_GETAUTOHIDEBAR, hwnd=%p, edge=%x): stub\n", hwnd, abd->uEdge);
219         return 0;
220     case ABM_SETAUTOHIDEBAR:
221         WINE_FIXME("SHAppBarMessage(ABM_SETAUTOHIDEBAR, hwnd=%p, edge=%x, lparam=%s): stub\n",
222                    hwnd, abd->uEdge, wine_dbgstr_longlong(abd->lParam));
223         return TRUE;
224     case ABM_WINDOWPOSCHANGED:
225         return TRUE;
226     default:
227         WINE_FIXME("SHAppBarMessage(%x) unimplemented\n", msg);
228         return FALSE;
229     }
230 }
231
232 static LRESULT CALLBACK appbar_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
233 {
234     switch (msg)
235     {
236     case WM_COPYDATA:
237         {
238             COPYDATASTRUCT* cds;
239             struct appbar_cmd cmd;
240             UINT_PTR result;
241             HANDLE return_hproc;
242             HANDLE return_map;
243             LPVOID return_view;
244             struct appbar_response* response;
245
246             cds = (COPYDATASTRUCT*)lparam;
247             if (cds->cbData != sizeof(struct appbar_cmd))
248                 return TRUE;
249             CopyMemory(&cmd, cds->lpData, cds->cbData);
250
251             result = handle_appbarmessage(cds->dwData, &cmd.abd);
252
253             return_hproc = OpenProcess(PROCESS_DUP_HANDLE, FALSE, cmd.return_process);
254             if (return_hproc == NULL)
255             {
256                 WINE_ERR("couldn't open calling process\n");
257                 return TRUE;
258             }
259
260             if (!DuplicateHandle(return_hproc, UlongToHandle(cmd.return_map),
261                                  GetCurrentProcess(), &return_map, 0, FALSE, DUPLICATE_SAME_ACCESS))
262             {
263                 WINE_ERR("couldn't duplicate handle\n");
264                 CloseHandle(return_hproc);
265                 return TRUE;
266             }
267             CloseHandle(return_hproc);
268
269             return_view = MapViewOfFile(return_map, FILE_MAP_WRITE, 0, 0, sizeof(struct appbar_response));
270
271             if (return_view)
272             {
273                 response = (struct appbar_response*)return_view;
274                 response->result = result;
275                 response->abd = cmd.abd;
276
277                 UnmapViewOfFile(return_view);
278             }
279             else
280                 WINE_ERR("couldn't map view of file\n");
281
282             CloseHandle(return_map);
283             return TRUE;
284         }
285     default:
286         break;
287     }
288
289     return DefWindowProcW(hwnd, msg, wparam, lparam);
290 }
291
292 void initialize_appbar(void)
293 {
294     WNDCLASSEXW class;
295     static const WCHAR classname[] = {'W','i','n','e','A','p','p','B','a','r',0};
296
297     /* register the appbar window class */
298     ZeroMemory(&class, sizeof(class));
299     class.cbSize = sizeof(class);
300     class.lpfnWndProc = appbar_wndproc;
301     class.hInstance = NULL;
302     class.lpszClassName = classname;
303
304     if (!RegisterClassExW(&class))
305     {
306         WINE_ERR("Could not register appbar message window class\n");
307         return;
308     }
309
310     appbarmsg_window = CreateWindowW(classname, classname, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
311     if (!appbarmsg_window)
312     {
313         WINE_ERR("Could not create appbar message window\n");
314         return;
315     }
316 }