winemac: Implement WGL_EXT_swap_control.
[wine] / dlls / winemac.drv / window.c
1 /*
2  * MACDRV windowing driver
3  *
4  * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5  * Copyright 1993 David Metcalfe
6  * Copyright 1995, 1996 Alex Korobka
7  * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25
26 #include "macdrv.h"
27 #include "winuser.h"
28 #include "wine/unicode.h"
29 #include "wine/server.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(macdrv);
32
33
34 static CRITICAL_SECTION win_data_section;
35 static CRITICAL_SECTION_DEBUG critsect_debug =
36 {
37     0, 0, &win_data_section,
38     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
39       0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
40 };
41 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
42
43 static CFMutableDictionaryRef win_datas;
44
45
46 void CDECL macdrv_SetFocus(HWND hwnd);
47
48
49 /***********************************************************************
50  *              get_cocoa_window_features
51  */
52 static void get_cocoa_window_features(struct macdrv_win_data *data,
53                                       DWORD style, DWORD ex_style,
54                                       struct macdrv_window_features* wf)
55 {
56     memset(wf, 0, sizeof(*wf));
57
58     if (IsRectEmpty(&data->window_rect)) return;
59
60     if ((style & WS_CAPTION) == WS_CAPTION && !(ex_style & WS_EX_LAYERED))
61     {
62         wf->shadow = 1;
63         if (!data->shaped)
64         {
65             wf->title_bar = 1;
66             if (style & WS_SYSMENU) wf->close_button = 1;
67             if (style & WS_MINIMIZEBOX) wf->minimize_button = 1;
68             if (style & WS_MAXIMIZEBOX) wf->resizable = 1;
69             if (ex_style & WS_EX_TOOLWINDOW) wf->utility = 1;
70         }
71     }
72     if (ex_style & WS_EX_DLGMODALFRAME) wf->shadow = 1;
73     else if (style & WS_THICKFRAME)
74     {
75         wf->shadow = 1;
76         if (!data->shaped) wf->resizable = 1;
77     }
78     else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) wf->shadow = 1;
79 }
80
81
82 /*******************************************************************
83  *              can_activate_window
84  *
85  * Check if we can activate the specified window.
86  */
87 static inline BOOL can_activate_window(HWND hwnd)
88 {
89     LONG style = GetWindowLongW(hwnd, GWL_STYLE);
90     RECT rect;
91
92     if (!(style & WS_VISIBLE)) return FALSE;
93     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
94     if (style & WS_MINIMIZE) return FALSE;
95     if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_NOACTIVATE) return FALSE;
96     if (hwnd == GetDesktopWindow()) return FALSE;
97     if (GetWindowRect(hwnd, &rect) && IsRectEmpty(&rect)) return FALSE;
98     return !(style & WS_DISABLED);
99 }
100
101
102 /***********************************************************************
103  *              get_cocoa_window_state
104  */
105 static void get_cocoa_window_state(struct macdrv_win_data *data,
106                                    DWORD style, DWORD ex_style,
107                                    struct macdrv_window_state* state)
108 {
109     memset(state, 0, sizeof(*state));
110     state->disabled = (style & WS_DISABLED) != 0;
111     state->no_activate = !can_activate_window(data->hwnd);
112     state->floating = (ex_style & WS_EX_TOPMOST) != 0;
113     state->excluded_by_expose = state->excluded_by_cycle =
114         !(ex_style & WS_EX_APPWINDOW) &&
115         (GetWindow(data->hwnd, GW_OWNER) || (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE)));
116     state->minimized = (style & WS_MINIMIZE) != 0;
117 }
118
119
120 /***********************************************************************
121  *              get_mac_rect_offset
122  *
123  * Helper for macdrv_window_to_mac_rect and macdrv_mac_to_window_rect.
124  */
125 static void get_mac_rect_offset(struct macdrv_win_data *data, DWORD style, RECT *rect)
126 {
127     DWORD ex_style, style_mask = 0, ex_style_mask = 0;
128
129     rect->top = rect->bottom = rect->left = rect->right = 0;
130
131     ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
132
133     if (!data->shaped)
134     {
135         struct macdrv_window_features wf;
136         get_cocoa_window_features(data, style, ex_style, &wf);
137
138         if (wf.title_bar) style_mask |= WS_CAPTION;
139         if (wf.shadow)
140         {
141             style_mask |= WS_DLGFRAME | WS_THICKFRAME;
142             ex_style_mask |= WS_EX_DLGMODALFRAME;
143         }
144     }
145
146     AdjustWindowRectEx(rect, style & style_mask, FALSE, ex_style & ex_style_mask);
147
148     TRACE("%p/%p style %08x ex_style %08x shaped %d -> %s\n", data->hwnd, data->cocoa_window,
149           style, ex_style, data->shaped, wine_dbgstr_rect(rect));
150 }
151
152
153 /***********************************************************************
154  *              macdrv_window_to_mac_rect
155  *
156  * Convert a rect from client to Mac window coordinates
157  */
158 static void macdrv_window_to_mac_rect(struct macdrv_win_data *data, DWORD style, RECT *rect)
159 {
160     RECT rc;
161
162     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return;
163     if (IsRectEmpty(rect)) return;
164
165     get_mac_rect_offset(data, style, &rc);
166
167     rect->left   -= rc.left;
168     rect->right  -= rc.right;
169     rect->top    -= rc.top;
170     rect->bottom -= rc.bottom;
171     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
172     if (rect->left >= rect->right) rect->right = rect->left + 1;
173 }
174
175
176 /***********************************************************************
177  *              macdrv_mac_to_window_rect
178  *
179  * Opposite of macdrv_window_to_mac_rect
180  */
181 static void macdrv_mac_to_window_rect(struct macdrv_win_data *data, RECT *rect)
182 {
183     RECT rc;
184     DWORD style = GetWindowLongW(data->hwnd, GWL_STYLE);
185
186     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return;
187     if (IsRectEmpty(rect)) return;
188
189     get_mac_rect_offset(data, style, &rc);
190
191     rect->left   += rc.left;
192     rect->right  += rc.right;
193     rect->top    += rc.top;
194     rect->bottom += rc.bottom;
195     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
196     if (rect->left >= rect->right) rect->right = rect->left + 1;
197 }
198
199
200 /***********************************************************************
201  *              constrain_window_frame
202  *
203  * Alter a window frame rectangle to fit within a) Cocoa's documented
204  * limits, and b) sane sizes, like twice the desktop rect.
205  */
206 static void constrain_window_frame(CGRect* frame)
207 {
208     CGRect desktop_rect = macdrv_get_desktop_rect();
209     int max_width, max_height;
210
211     max_width = min(32000, 2 * CGRectGetWidth(desktop_rect));
212     max_height = min(32000, 2 * CGRectGetHeight(desktop_rect));
213
214     if (frame->origin.x < -16000) frame->origin.x = -16000;
215     if (frame->origin.y < -16000) frame->origin.y = -16000;
216     if (frame->origin.x > 16000) frame->origin.x = 16000;
217     if (frame->origin.y > 16000) frame->origin.y = 16000;
218     if (frame->size.width > max_width) frame->size.width = max_width;
219     if (frame->size.height > max_height) frame->size.height = max_height;
220 }
221
222
223 /***********************************************************************
224  *              alloc_win_data
225  */
226 static struct macdrv_win_data *alloc_win_data(HWND hwnd)
227 {
228     struct macdrv_win_data *data;
229
230     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
231     {
232         data->hwnd = hwnd;
233         data->color_key = CLR_INVALID;
234         EnterCriticalSection(&win_data_section);
235         if (!win_datas)
236             win_datas = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
237         CFDictionarySetValue(win_datas, hwnd, data);
238     }
239     return data;
240 }
241
242
243 /***********************************************************************
244  *              get_win_data
245  *
246  * Lock and return the data structure associated with a window.
247  */
248 struct macdrv_win_data *get_win_data(HWND hwnd)
249 {
250     struct macdrv_win_data *data;
251
252     if (!hwnd) return NULL;
253     EnterCriticalSection(&win_data_section);
254     if (win_datas && (data = (struct macdrv_win_data*)CFDictionaryGetValue(win_datas, hwnd)))
255         return data;
256     LeaveCriticalSection(&win_data_section);
257     return NULL;
258 }
259
260
261 /***********************************************************************
262  *              release_win_data
263  *
264  * Release the data returned by get_win_data.
265  */
266 void release_win_data(struct macdrv_win_data *data)
267 {
268     if (data) LeaveCriticalSection(&win_data_section);
269 }
270
271
272 /***********************************************************************
273  *              macdrv_get_cocoa_window
274  *
275  * Return the Mac window associated with the full area of a window
276  */
277 macdrv_window macdrv_get_cocoa_window(HWND hwnd, BOOL require_on_screen)
278 {
279     struct macdrv_win_data *data = get_win_data(hwnd);
280     macdrv_window ret = NULL;
281     if (data && (data->on_screen || !require_on_screen))
282         ret = data->cocoa_window;
283     release_win_data(data);
284     return ret;
285 }
286
287
288 /***********************************************************************
289  *              set_cocoa_window_properties
290  *
291  * Set the window properties for a Cocoa window based on its Windows
292  * properties.
293  */
294 static void set_cocoa_window_properties(struct macdrv_win_data *data)
295 {
296     DWORD style, ex_style;
297     HWND owner;
298     macdrv_window owner_win;
299     struct macdrv_window_features wf;
300     struct macdrv_window_state state;
301
302     style = GetWindowLongW(data->hwnd, GWL_STYLE);
303     ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
304
305     owner = GetWindow(data->hwnd, GW_OWNER);
306     owner_win = macdrv_get_cocoa_window(owner, TRUE);
307     macdrv_set_cocoa_parent_window(data->cocoa_window, owner_win);
308
309     get_cocoa_window_features(data, style, ex_style, &wf);
310     macdrv_set_cocoa_window_features(data->cocoa_window, &wf);
311
312     get_cocoa_window_state(data, style, ex_style, &state);
313     macdrv_set_cocoa_window_state(data->cocoa_window, &state);
314     data->minimized = state.minimized;
315 }
316
317
318 /***********************************************************************
319  *              sync_window_region
320  *
321  * Update the window region.
322  */
323 static void sync_window_region(struct macdrv_win_data *data, HRGN win_region)
324 {
325     HRGN hrgn = win_region;
326     RGNDATA *region_data;
327     const CGRect* rects;
328     int count;
329
330     if (!data->cocoa_window) return;
331     data->shaped = FALSE;
332
333     if (hrgn == (HRGN)1)  /* hack: win_region == 1 means retrieve region from server */
334     {
335         if (!(hrgn = CreateRectRgn(0, 0, 0, 0))) return;
336         if (GetWindowRgn(data->hwnd, hrgn) == ERROR)
337         {
338             DeleteObject(hrgn);
339             hrgn = 0;
340         }
341     }
342
343     if (hrgn && GetWindowLongW(data->hwnd, GWL_EXSTYLE) & WS_EX_LAYOUTRTL)
344         MirrorRgn(data->hwnd, hrgn);
345     if (hrgn)
346     {
347         OffsetRgn(hrgn, data->window_rect.left - data->whole_rect.left,
348                   data->window_rect.top - data->whole_rect.top);
349     }
350     region_data = get_region_data(hrgn, 0);
351     if (region_data)
352     {
353         rects = (CGRect*)region_data->Buffer;
354         count = region_data->rdh.nCount;
355         /* Special case optimization.  If the region entirely encloses the Cocoa
356            window, it's the same as there being no region.  It's potentially
357            hard/slow to test this for arbitrary regions, so we just check for
358            very simple regions. */
359         if (count == 1 && CGRectContainsRect(rects[0], cgrect_from_rect(data->whole_rect)))
360         {
361             TRACE("optimizing for simple region that contains Cocoa content rect\n");
362             rects = NULL;
363             count = 0;
364         }
365     }
366     else
367     {
368         rects = NULL;
369         count = 0;
370     }
371
372     TRACE("win %p/%p win_region %p rects %p count %d\n", data->hwnd, data->cocoa_window, win_region, rects, count);
373     macdrv_set_window_shape(data->cocoa_window, rects, count);
374
375     HeapFree(GetProcessHeap(), 0, region_data);
376     data->shaped = (region_data != NULL);
377
378     if (hrgn && hrgn != win_region) DeleteObject(hrgn);
379 }
380
381
382 /***********************************************************************
383  *              add_bounds_rect
384  */
385 static inline void add_bounds_rect(RECT *bounds, const RECT *rect)
386 {
387     if (rect->left >= rect->right || rect->top >= rect->bottom) return;
388     bounds->left   = min(bounds->left, rect->left);
389     bounds->top    = min(bounds->top, rect->top);
390     bounds->right  = max(bounds->right, rect->right);
391     bounds->bottom = max(bounds->bottom, rect->bottom);
392 }
393
394
395 /***********************************************************************
396  *              sync_window_opacity
397  */
398 static void sync_window_opacity(struct macdrv_win_data *data, COLORREF key, BYTE alpha,
399                                 BOOL per_pixel_alpha, DWORD flags)
400 {
401     CGFloat opacity = 1.0;
402     BOOL needs_flush = FALSE;
403
404     if (flags & LWA_ALPHA) opacity = alpha / 255.0;
405
406     TRACE("setting window %p/%p alpha to %g\n", data->hwnd, data->cocoa_window, opacity);
407     macdrv_set_window_alpha(data->cocoa_window, opacity);
408
409     if (flags & LWA_COLORKEY)
410     {
411         /* FIXME: treat PALETTEINDEX and DIBINDEX as black */
412         if ((key & (1 << 24)) || key >> 16 == 0x10ff)
413             key = RGB(0, 0, 0);
414     }
415     else
416         key = CLR_INVALID;
417
418     if (data->color_key != key)
419     {
420         if (key == CLR_INVALID)
421         {
422             TRACE("clearing color-key for window %p/%p\n", data->hwnd, data->cocoa_window);
423             macdrv_clear_window_color_key(data->cocoa_window);
424         }
425         else
426         {
427             TRACE("setting color-key for window %p/%p to RGB %d,%d,%d\n", data->hwnd, data->cocoa_window,
428                   GetRValue(key), GetGValue(key), GetBValue(key));
429             macdrv_set_window_color_key(data->cocoa_window, GetRValue(key), GetGValue(key), GetBValue(key));
430         }
431
432         data->color_key = key;
433         needs_flush = TRUE;
434     }
435
436     if (!data->per_pixel_alpha != !per_pixel_alpha)
437     {
438         macdrv_window_use_per_pixel_alpha(data->cocoa_window, per_pixel_alpha);
439         data->per_pixel_alpha = per_pixel_alpha;
440         needs_flush = TRUE;
441     }
442
443     if (needs_flush && data->surface)
444     {
445         RECT *bounds;
446         RECT rect;
447
448         rect = data->whole_rect;
449         OffsetRect(&rect, -data->whole_rect.left, -data->whole_rect.top);
450         data->surface->funcs->lock(data->surface);
451         bounds = data->surface->funcs->get_bounds(data->surface);
452         add_bounds_rect(bounds, &rect);
453         data->surface->funcs->unlock(data->surface);
454     }
455 }
456
457
458 /**********************************************************************
459  *              create_cocoa_window
460  *
461  * Create the whole Mac window for a given window
462  */
463 static void create_cocoa_window(struct macdrv_win_data *data)
464 {
465     struct macdrv_thread_data *thread_data = macdrv_init_thread_data();
466     WCHAR text[1024];
467     struct macdrv_window_features wf;
468     CGRect frame;
469     DWORD style, ex_style;
470     HRGN win_rgn;
471     COLORREF key;
472     BYTE alpha;
473     DWORD layered_flags;
474
475     if ((win_rgn = CreateRectRgn(0, 0, 0, 0)) &&
476         GetWindowRgn(data->hwnd, win_rgn) == ERROR)
477     {
478         DeleteObject(win_rgn);
479         win_rgn = 0;
480     }
481     data->shaped = (win_rgn != 0);
482
483     style = GetWindowLongW(data->hwnd, GWL_STYLE);
484     ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
485
486     data->whole_rect = data->window_rect;
487     macdrv_window_to_mac_rect(data, style, &data->whole_rect);
488
489     memset(&wf, 0, sizeof(wf));
490     get_cocoa_window_features(data, style, ex_style, &wf);
491
492     frame = cgrect_from_rect(data->whole_rect);
493     constrain_window_frame(&frame);
494
495     TRACE("creating %p window %s whole %s client %s\n", data->hwnd, wine_dbgstr_rect(&data->window_rect),
496           wine_dbgstr_rect(&data->whole_rect), wine_dbgstr_rect(&data->client_rect));
497
498     data->cocoa_window = macdrv_create_cocoa_window(&wf, frame, data->hwnd, thread_data->queue);
499     if (!data->cocoa_window) goto done;
500
501     set_cocoa_window_properties(data);
502
503     /* set the window text */
504     if (!InternalGetWindowText(data->hwnd, text, sizeof(text)/sizeof(WCHAR))) text[0] = 0;
505     macdrv_set_cocoa_window_title(data->cocoa_window, text, strlenW(text));
506
507     /* set the window region */
508     if (win_rgn) sync_window_region(data, win_rgn);
509
510     /* set the window opacity */
511     if (!GetLayeredWindowAttributes(data->hwnd, &key, &alpha, &layered_flags)) layered_flags = 0;
512     sync_window_opacity(data, key, alpha, FALSE, layered_flags);
513
514 done:
515     if (win_rgn) DeleteObject(win_rgn);
516 }
517
518
519 /**********************************************************************
520  *              destroy_cocoa_window
521  *
522  * Destroy the whole Mac window for a given window.
523  */
524 static void destroy_cocoa_window(struct macdrv_win_data *data)
525 {
526     if (!data->cocoa_window) return;
527
528     TRACE("win %p Cocoa win %p\n", data->hwnd, data->cocoa_window);
529
530     macdrv_destroy_cocoa_window(data->cocoa_window);
531     data->cocoa_window = 0;
532     data->on_screen = FALSE;
533     data->color_key = CLR_INVALID;
534     if (data->surface) window_surface_release(data->surface);
535     data->surface = NULL;
536 }
537
538
539 /***********************************************************************
540  *              macdrv_create_win_data
541  *
542  * Create a Mac data window structure for an existing window.
543  */
544 static struct macdrv_win_data *macdrv_create_win_data(HWND hwnd, const RECT *window_rect,
545                                                       const RECT *client_rect)
546 {
547     struct macdrv_win_data *data;
548     HWND parent;
549
550     if (GetWindowThreadProcessId(hwnd, NULL) != GetCurrentThreadId()) return NULL;
551
552     if (!(parent = GetAncestor(hwnd, GA_PARENT)))  /* desktop */
553     {
554         macdrv_init_thread_data();
555         return NULL;
556     }
557
558     /* don't create win data for HWND_MESSAGE windows */
559     if (parent != GetDesktopWindow() && !GetAncestor(parent, GA_PARENT)) return NULL;
560
561     if (!(data = alloc_win_data(hwnd))) return NULL;
562
563     data->whole_rect = data->window_rect = *window_rect;
564     data->client_rect = *client_rect;
565
566     if (parent == GetDesktopWindow())
567     {
568         create_cocoa_window(data);
569         TRACE("win %p/%p window %s whole %s client %s\n",
570                hwnd, data->cocoa_window, wine_dbgstr_rect(&data->window_rect),
571                wine_dbgstr_rect(&data->whole_rect), wine_dbgstr_rect(&data->client_rect));
572     }
573
574     return data;
575 }
576
577
578 /***********************************************************************
579  *              show_window
580  */
581 static void show_window(struct macdrv_win_data *data)
582 {
583     HWND prev = NULL;
584     HWND next = NULL;
585     macdrv_window prev_window = NULL;
586     macdrv_window next_window = NULL;
587
588     /* find window that this one must be after */
589     prev = GetWindow(data->hwnd, GW_HWNDPREV);
590     while (prev && !((GetWindowLongW(prev, GWL_STYLE) & WS_VISIBLE) &&
591                      (prev_window = macdrv_get_cocoa_window(prev, TRUE))))
592         prev = GetWindow(prev, GW_HWNDPREV);
593     if (!prev_window)
594     {
595         /* find window that this one must be before */
596         next = GetWindow(data->hwnd, GW_HWNDNEXT);
597         while (next && !((GetWindowLongW(next, GWL_STYLE) & WS_VISIBLE) &&
598                          (next_window = macdrv_get_cocoa_window(next, TRUE))))
599             next = GetWindow(next, GW_HWNDNEXT);
600     }
601
602     TRACE("win %p/%p below %p/%p above %p/%p\n",
603           data->hwnd, data->cocoa_window, prev, prev_window, next, next_window);
604
605     data->on_screen = macdrv_order_cocoa_window(data->cocoa_window, prev_window, next_window);
606     if (data->on_screen)
607     {
608         HWND hwndFocus = GetFocus();
609         if (hwndFocus && (data->hwnd == hwndFocus || IsChild(data->hwnd, hwndFocus)))
610             macdrv_SetFocus(hwndFocus);
611     }
612 }
613
614
615 /***********************************************************************
616  *              hide_window
617  */
618 static void hide_window(struct macdrv_win_data *data)
619 {
620     TRACE("win %p/%p\n", data->hwnd, data->cocoa_window);
621
622     macdrv_hide_cocoa_window(data->cocoa_window);
623     data->on_screen = FALSE;
624 }
625
626
627 /***********************************************************************
628  *              get_region_data
629  *
630  * Calls GetRegionData on the given region and converts the rectangle
631  * array to CGRect format. The returned buffer must be freed by
632  * caller using HeapFree(GetProcessHeap(),...).
633  * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
634  */
635 RGNDATA *get_region_data(HRGN hrgn, HDC hdc_lptodp)
636 {
637     RGNDATA *data;
638     DWORD size;
639     int i;
640     RECT *rect;
641     CGRect *cgrect;
642
643     if (!hrgn || !(size = GetRegionData(hrgn, 0, NULL))) return NULL;
644     if (sizeof(CGRect) > sizeof(RECT))
645     {
646         /* add extra size for CGRect array */
647         int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
648         size += count * (sizeof(CGRect) - sizeof(RECT));
649     }
650     if (!(data = HeapAlloc(GetProcessHeap(), 0, size))) return NULL;
651     if (!GetRegionData(hrgn, size, data))
652     {
653         HeapFree(GetProcessHeap(), 0, data);
654         return NULL;
655     }
656
657     rect = (RECT *)data->Buffer;
658     cgrect = (CGRect *)data->Buffer;
659     if (hdc_lptodp)  /* map to device coordinates */
660     {
661         LPtoDP(hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2);
662         for (i = 0; i < data->rdh.nCount; i++)
663         {
664             if (rect[i].right < rect[i].left)
665             {
666                 INT tmp = rect[i].right;
667                 rect[i].right = rect[i].left;
668                 rect[i].left = tmp;
669             }
670             if (rect[i].bottom < rect[i].top)
671             {
672                 INT tmp = rect[i].bottom;
673                 rect[i].bottom = rect[i].top;
674                 rect[i].top = tmp;
675             }
676         }
677     }
678
679     if (sizeof(CGRect) > sizeof(RECT))
680     {
681         /* need to start from the end */
682         for (i = data->rdh.nCount-1; i >= 0; i--)
683             cgrect[i] = cgrect_from_rect(rect[i]);
684     }
685     else
686     {
687         for (i = 0; i < data->rdh.nCount; i++)
688             cgrect[i] = cgrect_from_rect(rect[i]);
689     }
690     return data;
691 }
692
693
694 /***********************************************************************
695  *              sync_window_position
696  *
697  * Synchronize the Mac window position with the Windows one
698  */
699 static void sync_window_position(struct macdrv_win_data *data, UINT swp_flags)
700 {
701     CGRect frame;
702
703     if (data->minimized) return;
704
705     frame = cgrect_from_rect(data->whole_rect);
706     constrain_window_frame(&frame);
707
708     data->on_screen = macdrv_set_cocoa_window_frame(data->cocoa_window, &frame);
709     if (data->shaped) sync_window_region(data, (HRGN)1);
710
711     TRACE("win %p/%p pos %s\n", data->hwnd, data->cocoa_window,
712           wine_dbgstr_rect(&data->whole_rect));
713
714     if (data->on_screen && (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW)))
715         show_window(data);
716 }
717
718
719 /***********************************************************************
720  *              move_window_bits
721  *
722  * Move the window bits when a window is moved.
723  */
724 static void move_window_bits(HWND hwnd, macdrv_window window, const RECT *old_rect, const RECT *new_rect,
725                              const RECT *old_client_rect, const RECT *new_client_rect,
726                              const RECT *new_window_rect)
727 {
728     RECT src_rect = *old_rect;
729     RECT dst_rect = *new_rect;
730     HDC hdc_src, hdc_dst;
731     HRGN rgn;
732     HWND parent = 0;
733
734     if (!window)
735     {
736         OffsetRect(&dst_rect, -new_window_rect->left, -new_window_rect->top);
737         parent = GetAncestor(hwnd, GA_PARENT);
738         hdc_src = GetDCEx(parent, 0, DCX_CACHE);
739         hdc_dst = GetDCEx(hwnd, 0, DCX_CACHE | DCX_WINDOW);
740     }
741     else
742     {
743         OffsetRect(&dst_rect, -new_client_rect->left, -new_client_rect->top);
744         /* make src rect relative to the old position of the window */
745         OffsetRect(&src_rect, -old_client_rect->left, -old_client_rect->top);
746         if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
747         hdc_src = hdc_dst = GetDCEx(hwnd, 0, DCX_CACHE);
748     }
749
750     rgn = CreateRectRgnIndirect(&dst_rect);
751     SelectClipRgn(hdc_dst, rgn);
752     DeleteObject(rgn);
753     ExcludeUpdateRgn(hdc_dst, hwnd);
754
755     TRACE("copying bits for win %p/%p %s -> %s\n", hwnd, window,
756           wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect));
757     BitBlt(hdc_dst, dst_rect.left, dst_rect.top,
758            dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
759            hdc_src, src_rect.left, src_rect.top, SRCCOPY);
760
761     ReleaseDC(hwnd, hdc_dst);
762     if (hdc_src != hdc_dst) ReleaseDC(parent, hdc_src);
763 }
764
765
766 /**********************************************************************
767  *              CreateDesktopWindow   (MACDRV.@)
768  */
769 BOOL CDECL macdrv_CreateDesktopWindow(HWND hwnd)
770 {
771     unsigned int width, height;
772
773     TRACE("%p\n", hwnd);
774
775     /* retrieve the real size of the desktop */
776     SERVER_START_REQ(get_window_rectangles)
777     {
778         req->handle = wine_server_user_handle(hwnd);
779         req->relative = COORDS_CLIENT;
780         wine_server_call(req);
781         width  = reply->window.right;
782         height = reply->window.bottom;
783     }
784     SERVER_END_REQ;
785
786     if (!width && !height)  /* not initialized yet */
787     {
788         CGRect rect = macdrv_get_desktop_rect();
789
790         SERVER_START_REQ(set_window_pos)
791         {
792             req->handle        = wine_server_user_handle(hwnd);
793             req->previous      = 0;
794             req->swp_flags     = SWP_NOZORDER;
795             req->window.left   = CGRectGetMinX(rect);
796             req->window.top    = CGRectGetMinY(rect);
797             req->window.right  = CGRectGetMaxX(rect);
798             req->window.bottom = CGRectGetMaxY(rect);
799             req->client        = req->window;
800             wine_server_call(req);
801         }
802         SERVER_END_REQ;
803     }
804
805     return TRUE;
806 }
807
808
809 /**********************************************************************
810  *              CreateWindow   (MACDRV.@)
811  */
812 BOOL CDECL macdrv_CreateWindow(HWND hwnd)
813 {
814     return TRUE;
815 }
816
817
818 /***********************************************************************
819  *              DestroyWindow   (MACDRV.@)
820  */
821 void CDECL macdrv_DestroyWindow(HWND hwnd)
822 {
823     struct macdrv_win_data *data;
824
825     TRACE("%p\n", hwnd);
826
827     if (!(data = get_win_data(hwnd))) return;
828
829     if (data->gl_view) macdrv_dispose_view(data->gl_view);
830     destroy_cocoa_window(data);
831
832     CFDictionaryRemoveValue(win_datas, hwnd);
833     release_win_data(data);
834     HeapFree(GetProcessHeap(), 0, data);
835 }
836
837
838 /*****************************************************************
839  *              SetFocus   (MACDRV.@)
840  *
841  * Set the Mac focus.
842  */
843 void CDECL macdrv_SetFocus(HWND hwnd)
844 {
845     struct macdrv_thread_data *thread_data = macdrv_thread_data();
846     struct macdrv_win_data *data;
847
848     TRACE("%p\n", hwnd);
849
850     if (!thread_data) return;
851     thread_data->dead_key_state = 0;
852
853     if (!(hwnd = GetAncestor(hwnd, GA_ROOT))) return;
854     if (!(data = get_win_data(hwnd))) return;
855
856     if (data->cocoa_window && data->on_screen)
857     {
858         /* Set Mac focus */
859         macdrv_give_cocoa_window_focus(data->cocoa_window);
860     }
861
862     release_win_data(data);
863 }
864
865
866 /***********************************************************************
867  *              SetLayeredWindowAttributes  (MACDRV.@)
868  *
869  * Set transparency attributes for a layered window.
870  */
871 void CDECL macdrv_SetLayeredWindowAttributes(HWND hwnd, COLORREF key, BYTE alpha, DWORD flags)
872 {
873     struct macdrv_win_data *data = get_win_data(hwnd);
874
875     TRACE("hwnd %p key %#08x alpha %#02x flags %x\n", hwnd, key, alpha, flags);
876
877     if (data)
878     {
879         data->layered = TRUE;
880         if (data->cocoa_window)
881         {
882             sync_window_opacity(data, key, alpha, FALSE, flags);
883             /* since layered attributes are now set, can now show the window */
884             if ((GetWindowLongW(hwnd, GWL_STYLE) & WS_VISIBLE) && !data->on_screen)
885                 show_window(data);
886         }
887         release_win_data(data);
888     }
889     else
890         FIXME("setting layered attributes on window %p of other process not supported\n", hwnd);
891 }
892
893
894 /*****************************************************************
895  *              SetParent   (MACDRV.@)
896  */
897 void CDECL macdrv_SetParent(HWND hwnd, HWND parent, HWND old_parent)
898 {
899     struct macdrv_win_data *data;
900
901     TRACE("%p, %p, %p\n", hwnd, parent, old_parent);
902
903     if (parent == old_parent) return;
904     if (!(data = get_win_data(hwnd))) return;
905
906     if (parent != GetDesktopWindow()) /* a child window */
907     {
908         if (old_parent == GetDesktopWindow())
909         {
910             /* destroy the old Mac window */
911             destroy_cocoa_window(data);
912         }
913     }
914     else  /* new top level window */
915         create_cocoa_window(data);
916     release_win_data(data);
917
918     set_gl_view_parent(hwnd, parent);
919 }
920
921
922 /***********************************************************************
923  *              SetWindowRgn  (MACDRV.@)
924  *
925  * Assign specified region to window (for non-rectangular windows)
926  */
927 int CDECL macdrv_SetWindowRgn(HWND hwnd, HRGN hrgn, BOOL redraw)
928 {
929     struct macdrv_win_data *data;
930
931     TRACE("%p, %p, %d\n", hwnd, hrgn, redraw);
932
933     if ((data = get_win_data(hwnd)))
934     {
935         sync_window_region(data, hrgn);
936         release_win_data(data);
937     }
938     else
939     {
940         DWORD procid;
941
942         GetWindowThreadProcessId(hwnd, &procid);
943         if (procid != GetCurrentProcessId())
944             SendMessageW(hwnd, WM_MACDRV_SET_WIN_REGION, 0, 0);
945     }
946
947     return TRUE;
948 }
949
950
951 /***********************************************************************
952  *              SetWindowStyle   (MACDRV.@)
953  *
954  * Update the state of the Cocoa window to reflect a style change
955  */
956 void CDECL macdrv_SetWindowStyle(HWND hwnd, INT offset, STYLESTRUCT *style)
957 {
958     struct macdrv_win_data *data;
959
960     TRACE("%p, %d, %p\n", hwnd, offset, style);
961
962     if (hwnd == GetDesktopWindow()) return;
963     if (!(data = get_win_data(hwnd))) return;
964
965     if (data->cocoa_window)
966     {
967         DWORD changed = style->styleNew ^ style->styleOld;
968
969         set_cocoa_window_properties(data);
970
971         if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
972         {
973             data->layered = FALSE;
974             sync_window_opacity(data, 0, 0, FALSE, 0);
975             if (data->surface) set_surface_use_alpha(data->surface, FALSE);
976         }
977     }
978
979     release_win_data(data);
980 }
981
982
983 /*****************************************************************
984  *              SetWindowText   (MACDRV.@)
985  */
986 void CDECL macdrv_SetWindowText(HWND hwnd, LPCWSTR text)
987 {
988     macdrv_window win;
989
990     TRACE("%p, %s\n", hwnd, debugstr_w(text));
991
992     if ((win = macdrv_get_cocoa_window(hwnd, FALSE)))
993         macdrv_set_cocoa_window_title(win, text, strlenW(text));
994 }
995
996
997 /***********************************************************************
998  *              ShowWindow   (MACDRV.@)
999  */
1000 UINT CDECL macdrv_ShowWindow(HWND hwnd, INT cmd, RECT *rect, UINT swp)
1001 {
1002     struct macdrv_thread_data *thread_data = macdrv_thread_data();
1003     struct macdrv_win_data *data = get_win_data(hwnd);
1004     CGRect frame;
1005
1006     if (!data || !data->cocoa_window) goto done;
1007     if (IsRectEmpty(rect)) goto done;
1008     if (GetWindowLongW(hwnd, GWL_STYLE) & WS_MINIMIZE)
1009     {
1010         if (rect->left != -32000 || rect->top != -32000)
1011         {
1012             OffsetRect(rect, -32000 - rect->left, -32000 - rect->top);
1013             swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
1014         }
1015         goto done;
1016     }
1017     if (!data->on_screen) goto done;
1018
1019     /* only fetch the new rectangle if the ShowWindow was a result of an external event */
1020
1021     if (!thread_data->current_event || thread_data->current_event->window != data->cocoa_window)
1022         goto done;
1023
1024     if (thread_data->current_event->type != WINDOW_FRAME_CHANGED &&
1025         thread_data->current_event->type != WINDOW_DID_MINIMIZE &&
1026         thread_data->current_event->type != WINDOW_DID_UNMINIMIZE)
1027         goto done;
1028
1029     TRACE("win %p/%p cmd %d at %s flags %08x\n",
1030           hwnd, data->cocoa_window, cmd, wine_dbgstr_rect(rect), swp);
1031
1032     macdrv_get_cocoa_window_frame(data->cocoa_window, &frame);
1033     *rect = rect_from_cgrect(frame);
1034     macdrv_mac_to_window_rect(data, rect);
1035     TRACE("rect %s -> %s\n", wine_dbgstr_cgrect(frame), wine_dbgstr_rect(rect));
1036     swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
1037
1038 done:
1039     release_win_data(data);
1040     return swp;
1041 }
1042
1043
1044 /***********************************************************************
1045  *              SysCommand   (MACDRV.@)
1046  *
1047  * Perform WM_SYSCOMMAND handling.
1048  */
1049 LRESULT CDECL macdrv_SysCommand(HWND hwnd, WPARAM wparam, LPARAM lparam)
1050 {
1051     struct macdrv_win_data *data;
1052     LRESULT ret = -1;
1053
1054     TRACE("%p, %x, %lx\n", hwnd, (unsigned)wparam, lparam);
1055
1056     if (!(data = get_win_data(hwnd))) goto done;
1057     if (!data->cocoa_window || !data->on_screen) goto done;
1058
1059     /* prevent a simple ALT press+release from activating the system menu,
1060        as that can get confusing */
1061     if ((wparam & 0xfff0) == SC_KEYMENU && !(WCHAR)lparam && !GetMenu(hwnd) &&
1062         (GetWindowLongW(hwnd, GWL_STYLE) & WS_SYSMENU))
1063     {
1064         TRACE("ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam);
1065         ret = 0;
1066     }
1067
1068 done:
1069     release_win_data(data);
1070     return ret;
1071 }
1072
1073
1074 /***********************************************************************
1075  *              UpdateLayeredWindow   (MACDRV.@)
1076  */
1077 BOOL CDECL macdrv_UpdateLayeredWindow(HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
1078                                       const RECT *window_rect)
1079 {
1080     struct window_surface *surface;
1081     struct macdrv_win_data *data;
1082     BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
1083     BYTE alpha;
1084     char buffer[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1085     BITMAPINFO *bmi = (BITMAPINFO *)buffer;
1086     void *src_bits, *dst_bits;
1087     RECT rect;
1088     HDC hdc = 0;
1089     HBITMAP dib;
1090     BOOL ret = FALSE;
1091
1092     if (!(data = get_win_data(hwnd))) return FALSE;
1093
1094     data->layered = TRUE;
1095
1096     rect = *window_rect;
1097     OffsetRect(&rect, -window_rect->left, -window_rect->top);
1098
1099     surface = data->surface;
1100     if (!surface || memcmp(&surface->rect, &rect, sizeof(RECT)))
1101     {
1102         data->surface = create_surface(data->cocoa_window, &rect, TRUE);
1103         set_window_surface(data->cocoa_window, data->surface);
1104         if (surface) window_surface_release(surface);
1105         surface = data->surface;
1106     }
1107     else set_surface_use_alpha(surface, TRUE);
1108
1109     if (surface) window_surface_add_ref(surface);
1110     release_win_data(data);
1111
1112     if (!surface) return FALSE;
1113     if (!info->hdcSrc)
1114     {
1115         window_surface_release(surface);
1116         return TRUE;
1117     }
1118
1119     if (info->dwFlags & ULW_ALPHA)
1120     {
1121         /* Apply SourceConstantAlpha via window alpha, not blend. */
1122         alpha = info->pblend->SourceConstantAlpha;
1123         blend = *info->pblend;
1124         blend.SourceConstantAlpha = 0xff;
1125     }
1126     else
1127         alpha = 0xff;
1128
1129     dst_bits = surface->funcs->get_info(surface, bmi);
1130
1131     if (!(dib = CreateDIBSection(info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0))) goto done;
1132     if (!(hdc = CreateCompatibleDC(0))) goto done;
1133
1134     SelectObject(hdc, dib);
1135     if (info->prcDirty)
1136     {
1137         IntersectRect(&rect, &rect, info->prcDirty);
1138         surface->funcs->lock(surface);
1139         memcpy(src_bits, dst_bits, bmi->bmiHeader.biSizeImage);
1140         surface->funcs->unlock(surface);
1141         PatBlt(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS);
1142     }
1143     if (!(ret = GdiAlphaBlend(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
1144                               info->hdcSrc,
1145                               rect.left + (info->pptSrc ? info->pptSrc->x : 0),
1146                               rect.top + (info->pptSrc ? info->pptSrc->y : 0),
1147                               rect.right - rect.left, rect.bottom - rect.top,
1148                               blend)))
1149         goto done;
1150
1151     if ((data = get_win_data(hwnd)))
1152     {
1153         if (surface == data->surface)
1154         {
1155             surface->funcs->lock(surface);
1156             memcpy(dst_bits, src_bits, bmi->bmiHeader.biSizeImage);
1157             add_bounds_rect(surface->funcs->get_bounds(surface), &rect);
1158             surface->funcs->unlock(surface);
1159             surface->funcs->flush(surface);
1160         }
1161
1162         /* The ULW flags are a superset of the LWA flags. */
1163         sync_window_opacity(data, info->crKey, alpha, TRUE, info->dwFlags);
1164
1165         release_win_data(data);
1166     }
1167
1168 done:
1169     window_surface_release(surface);
1170     if (hdc) DeleteDC(hdc);
1171     if (dib) DeleteObject(dib);
1172     return ret;
1173 }
1174
1175
1176 /**********************************************************************
1177  *              WindowMessage   (MACDRV.@)
1178  */
1179 LRESULT CDECL macdrv_WindowMessage(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1180 {
1181     struct macdrv_win_data *data;
1182
1183     TRACE("%p, %u, %u, %lu\n", hwnd, msg, (unsigned)wp, lp);
1184
1185     switch(msg)
1186     {
1187     case WM_MACDRV_SET_WIN_REGION:
1188         if ((data = get_win_data(hwnd)))
1189         {
1190             sync_window_region(data, (HRGN)1);
1191             release_win_data(data);
1192         }
1193         return 0;
1194     case WM_MACDRV_UPDATE_DESKTOP_RECT:
1195         if (hwnd == GetDesktopWindow())
1196         {
1197             CGRect new_desktop_rect;
1198             RECT current_desktop_rect;
1199
1200             macdrv_reset_device_metrics();
1201             new_desktop_rect = macdrv_get_desktop_rect();
1202             if (!GetWindowRect(hwnd, &current_desktop_rect) ||
1203                 !CGRectEqualToRect(cgrect_from_rect(current_desktop_rect), new_desktop_rect))
1204             {
1205                 SendMessageTimeoutW(HWND_BROADCAST, WM_MACDRV_RESET_DEVICE_METRICS, 0, 0,
1206                                     SMTO_ABORTIFHUNG, 2000, NULL);
1207                 SetWindowPos(hwnd, 0, CGRectGetMinX(new_desktop_rect), CGRectGetMinY(new_desktop_rect),
1208                              CGRectGetWidth(new_desktop_rect), CGRectGetHeight(new_desktop_rect),
1209                              SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE);
1210                 SendMessageTimeoutW(HWND_BROADCAST, WM_MACDRV_DISPLAYCHANGE, wp, lp,
1211                                     SMTO_ABORTIFHUNG, 2000, NULL);
1212             }
1213         }
1214         return 0;
1215     case WM_MACDRV_RESET_DEVICE_METRICS:
1216         macdrv_reset_device_metrics();
1217         return 0;
1218     case WM_MACDRV_DISPLAYCHANGE:
1219         if ((data = get_win_data(hwnd)))
1220         {
1221             if (data->cocoa_window && data->on_screen)
1222                 sync_window_position(data, SWP_NOZORDER | SWP_NOACTIVATE);
1223             release_win_data(data);
1224         }
1225         SendMessageW(hwnd, WM_DISPLAYCHANGE, wp, lp);
1226         return 0;
1227     }
1228
1229     FIXME("unrecognized window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp);
1230     return 0;
1231 }
1232
1233
1234 static inline RECT get_surface_rect(const RECT *visible_rect)
1235 {
1236     RECT rect;
1237     RECT desktop_rect = rect_from_cgrect(macdrv_get_desktop_rect());
1238
1239     IntersectRect(&rect, visible_rect, &desktop_rect);
1240     OffsetRect(&rect, -visible_rect->left, -visible_rect->top);
1241     rect.left &= ~127;
1242     rect.top  &= ~127;
1243     rect.right  = max(rect.left + 128, (rect.right + 127) & ~127);
1244     rect.bottom = max(rect.top + 128, (rect.bottom + 127) & ~127);
1245     return rect;
1246 }
1247
1248
1249 /***********************************************************************
1250  *              WindowPosChanging   (MACDRV.@)
1251  */
1252 void CDECL macdrv_WindowPosChanging(HWND hwnd, HWND insert_after, UINT swp_flags,
1253                                     const RECT *window_rect, const RECT *client_rect,
1254                                     RECT *visible_rect, struct window_surface **surface)
1255 {
1256     struct macdrv_win_data *data = get_win_data(hwnd);
1257     DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
1258     RECT surface_rect;
1259
1260     TRACE("%p after %p swp %04x window %s client %s visible %s surface %p\n", hwnd, insert_after,
1261           swp_flags, wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
1262           wine_dbgstr_rect(visible_rect), surface);
1263
1264     if (!data && !(data = macdrv_create_win_data(hwnd, window_rect, client_rect))) return;
1265
1266     *visible_rect = *window_rect;
1267     macdrv_window_to_mac_rect(data, style, visible_rect);
1268     TRACE("visible_rect %s -> %s\n", wine_dbgstr_rect(window_rect),
1269           wine_dbgstr_rect(visible_rect));
1270
1271     /* create the window surface if necessary */
1272     if (!data->cocoa_window) goto done;
1273     if (swp_flags & SWP_HIDEWINDOW) goto done;
1274
1275     if (*surface) window_surface_release(*surface);
1276     *surface = NULL;
1277
1278     surface_rect = get_surface_rect(visible_rect);
1279     if (data->surface)
1280     {
1281         if (!memcmp(&data->surface->rect, &surface_rect, sizeof(surface_rect)))
1282         {
1283             /* existing surface is good enough */
1284             window_surface_add_ref(data->surface);
1285             *surface = data->surface;
1286             goto done;
1287         }
1288     }
1289     else if (!(swp_flags & SWP_SHOWWINDOW) && !(style & WS_VISIBLE)) goto done;
1290
1291     *surface = create_surface(data->cocoa_window, &surface_rect, FALSE);
1292
1293 done:
1294     release_win_data(data);
1295 }
1296
1297
1298 /***********************************************************************
1299  *              WindowPosChanged   (MACDRV.@)
1300  */
1301 void CDECL macdrv_WindowPosChanged(HWND hwnd, HWND insert_after, UINT swp_flags,
1302                                    const RECT *window_rect, const RECT *client_rect,
1303                                    const RECT *visible_rect, const RECT *valid_rects,
1304                                    struct window_surface *surface)
1305 {
1306     struct macdrv_thread_data *thread_data;
1307     struct macdrv_win_data *data;
1308     DWORD new_style = GetWindowLongW(hwnd, GWL_STYLE);
1309     RECT old_window_rect, old_whole_rect, old_client_rect;
1310
1311     if (!(data = get_win_data(hwnd))) return;
1312
1313     thread_data = macdrv_thread_data();
1314
1315     old_window_rect = data->window_rect;
1316     old_whole_rect  = data->whole_rect;
1317     old_client_rect = data->client_rect;
1318     data->window_rect = *window_rect;
1319     data->whole_rect  = *visible_rect;
1320     data->client_rect = *client_rect;
1321     if (surface)
1322         window_surface_add_ref(surface);
1323     set_window_surface(data->cocoa_window, surface);
1324     if (data->surface) window_surface_release(data->surface);
1325     data->surface = surface;
1326
1327     TRACE("win %p/%p window %s whole %s client %s style %08x flags %08x surface %p\n",
1328            hwnd, data->cocoa_window, wine_dbgstr_rect(window_rect),
1329            wine_dbgstr_rect(visible_rect), wine_dbgstr_rect(client_rect),
1330            new_style, swp_flags, surface);
1331
1332     if (!IsRectEmpty(&valid_rects[0]))
1333     {
1334         macdrv_window window = data->cocoa_window;
1335         int x_offset = old_whole_rect.left - data->whole_rect.left;
1336         int y_offset = old_whole_rect.top - data->whole_rect.top;
1337
1338         /* if all that happened is that the whole window moved, copy everything */
1339         if (!(swp_flags & SWP_FRAMECHANGED) &&
1340             old_whole_rect.right   - data->whole_rect.right   == x_offset &&
1341             old_whole_rect.bottom  - data->whole_rect.bottom  == y_offset &&
1342             old_client_rect.left   - data->client_rect.left   == x_offset &&
1343             old_client_rect.right  - data->client_rect.right  == x_offset &&
1344             old_client_rect.top    - data->client_rect.top    == y_offset &&
1345             old_client_rect.bottom - data->client_rect.bottom == y_offset &&
1346             !memcmp(&valid_rects[0], &data->client_rect, sizeof(RECT)))
1347         {
1348             /* A Cocoa window's bits are moved automatically */
1349             if (!window && (x_offset != 0 || y_offset != 0))
1350             {
1351                 release_win_data(data);
1352                 move_window_bits(hwnd, window, &old_whole_rect, visible_rect,
1353                                  &old_client_rect, client_rect, window_rect);
1354                 if (!(data = get_win_data(hwnd))) return;
1355             }
1356         }
1357         else
1358         {
1359             release_win_data(data);
1360             move_window_bits(hwnd, window, &valid_rects[1], &valid_rects[0],
1361                              &old_client_rect, client_rect, window_rect);
1362             if (!(data = get_win_data(hwnd))) return;
1363         }
1364     }
1365
1366     sync_gl_view(data);
1367
1368     if (!data->cocoa_window) goto done;
1369
1370     if (data->on_screen)
1371     {
1372         if ((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE))
1373             hide_window(data);
1374     }
1375
1376     /* check if we are currently processing an event relevant to this window */
1377     if (!thread_data || !thread_data->current_event ||
1378         thread_data->current_event->window != data->cocoa_window ||
1379         (thread_data->current_event->type != WINDOW_FRAME_CHANGED &&
1380          thread_data->current_event->type != WINDOW_DID_MINIMIZE &&
1381          thread_data->current_event->type != WINDOW_DID_UNMINIMIZE))
1382     {
1383         sync_window_position(data, swp_flags);
1384         set_cocoa_window_properties(data);
1385     }
1386
1387     if (new_style & WS_VISIBLE)
1388     {
1389         if (!data->on_screen || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
1390             set_cocoa_window_properties(data);
1391
1392         /* layered windows are not shown until their attributes are set */
1393         if (!data->on_screen &&
1394             (data->layered || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED)))
1395             show_window(data);
1396     }
1397
1398 done:
1399     release_win_data(data);
1400 }
1401
1402
1403 /***********************************************************************
1404  *              macdrv_window_close_requested
1405  *
1406  * Handler for WINDOW_CLOSE_REQUESTED events.
1407  */
1408 void macdrv_window_close_requested(HWND hwnd)
1409 {
1410     /* Ignore the delete window request if the window has been disabled. This
1411      * is to disallow applications from being closed while in a modal state.
1412      */
1413     if (IsWindowEnabled(hwnd))
1414     {
1415         HMENU hSysMenu;
1416
1417         if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return;
1418         hSysMenu = GetSystemMenu(hwnd, FALSE);
1419         if (hSysMenu)
1420         {
1421             UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
1422             if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
1423                 return;
1424         }
1425         if (GetActiveWindow() != hwnd)
1426         {
1427             LRESULT ma = SendMessageW(hwnd, WM_MOUSEACTIVATE,
1428                                       (WPARAM)GetAncestor(hwnd, GA_ROOT),
1429                                       MAKELPARAM(HTCLOSE, WM_NCLBUTTONDOWN));
1430             switch(ma)
1431             {
1432                 case MA_NOACTIVATEANDEAT:
1433                 case MA_ACTIVATEANDEAT:
1434                     return;
1435                 case MA_NOACTIVATE:
1436                     break;
1437                 case MA_ACTIVATE:
1438                 case 0:
1439                     SetActiveWindow(hwnd);
1440                     break;
1441                 default:
1442                     WARN("unknown WM_MOUSEACTIVATE code %d\n", (int) ma);
1443                     break;
1444             }
1445         }
1446
1447         PostMessageW(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
1448     }
1449 }
1450
1451
1452 /***********************************************************************
1453  *              macdrv_window_frame_changed
1454  *
1455  * Handler for WINDOW_FRAME_CHANGED events.
1456  */
1457 void macdrv_window_frame_changed(HWND hwnd, CGRect frame)
1458 {
1459     struct macdrv_win_data *data;
1460     RECT rect;
1461     HWND parent;
1462     UINT flags = SWP_NOACTIVATE | SWP_NOZORDER;
1463     int width, height;
1464
1465     if (!hwnd) return;
1466     if (!(data = get_win_data(hwnd))) return;
1467     if (!data->on_screen)
1468     {
1469         release_win_data(data);
1470         return;
1471     }
1472
1473     /* Get geometry */
1474
1475     parent = GetAncestor(hwnd, GA_PARENT);
1476
1477     TRACE("win %p/%p new Cocoa frame %s\n", hwnd, data->cocoa_window, wine_dbgstr_cgrect(frame));
1478
1479     rect = rect_from_cgrect(frame);
1480     macdrv_mac_to_window_rect(data, &rect);
1481     MapWindowPoints(0, parent, (POINT *)&rect, 2);
1482
1483     width = rect.right - rect.left;
1484     height = rect.bottom - rect.top;
1485
1486     if (data->window_rect.left == rect.left && data->window_rect.top == rect.top)
1487         flags |= SWP_NOMOVE;
1488     else
1489         TRACE("%p moving from (%d,%d) to (%d,%d)\n", hwnd, data->window_rect.left,
1490               data->window_rect.top, rect.left, rect.top);
1491
1492     if ((data->window_rect.right - data->window_rect.left == width &&
1493          data->window_rect.bottom - data->window_rect.top == height) ||
1494         (IsRectEmpty(&data->window_rect) && width <= 0 && height <= 0))
1495         flags |= SWP_NOSIZE;
1496     else
1497         TRACE("%p resizing from (%dx%d) to (%dx%d)\n", hwnd, data->window_rect.right - data->window_rect.left,
1498               data->window_rect.bottom - data->window_rect.top, width, height);
1499
1500     release_win_data(data);
1501
1502     if (!(flags & SWP_NOSIZE) || !(flags & SWP_NOMOVE))
1503         SetWindowPos(hwnd, 0, rect.left, rect.top, width, height, flags);
1504 }
1505
1506
1507 /***********************************************************************
1508  *              macdrv_window_got_focus
1509  *
1510  * Handler for WINDOW_GOT_FOCUS events.
1511  */
1512 void macdrv_window_got_focus(HWND hwnd, const macdrv_event *event)
1513 {
1514     if (!hwnd) return;
1515
1516     TRACE("win %p/%p serial %lu enabled %d visible %d style %08x focus %p active %p fg %p\n",
1517           hwnd, event->window, event->window_got_focus.serial, IsWindowEnabled(hwnd),
1518           IsWindowVisible(hwnd), GetWindowLongW(hwnd, GWL_STYLE), GetFocus(),
1519           GetActiveWindow(), GetForegroundWindow());
1520
1521     if (can_activate_window(hwnd))
1522     {
1523         /* simulate a mouse click on the caption to find out
1524          * whether the window wants to be activated */
1525         LRESULT ma = SendMessageW(hwnd, WM_MOUSEACTIVATE,
1526                                   (WPARAM)GetAncestor(hwnd, GA_ROOT),
1527                                   MAKELONG(HTCAPTION,WM_LBUTTONDOWN));
1528         if (ma != MA_NOACTIVATEANDEAT && ma != MA_NOACTIVATE)
1529         {
1530             TRACE("setting foreground window to %p\n", hwnd);
1531             SetForegroundWindow(hwnd);
1532             return;
1533         }
1534     }
1535
1536     TRACE("win %p/%p rejecting focus\n", hwnd, event->window);
1537     macdrv_window_rejected_focus(event);
1538 }
1539
1540
1541 /***********************************************************************
1542  *              macdrv_window_lost_focus
1543  *
1544  * Handler for WINDOW_LOST_FOCUS events.
1545  */
1546 void macdrv_window_lost_focus(HWND hwnd, const macdrv_event *event)
1547 {
1548     if (!hwnd) return;
1549
1550     TRACE("win %p/%p fg %p\n", hwnd, event->window, GetForegroundWindow());
1551
1552     if (hwnd == GetForegroundWindow())
1553         SendMessageW(hwnd, WM_CANCELMODE, 0, 0);
1554 }
1555
1556
1557 /***********************************************************************
1558  *              macdrv_app_deactivated
1559  *
1560  * Handler for APP_DEACTIVATED events.
1561  */
1562 void macdrv_app_deactivated(void)
1563 {
1564     if (GetActiveWindow() == GetForegroundWindow())
1565     {
1566         TRACE("setting fg to desktop\n");
1567         SetForegroundWindow(GetDesktopWindow());
1568     }
1569 }
1570
1571
1572 /***********************************************************************
1573  *              macdrv_window_did_minimize
1574  *
1575  * Handler for WINDOW_DID_MINIMIZE events.
1576  */
1577 void macdrv_window_did_minimize(HWND hwnd)
1578 {
1579     struct macdrv_win_data *data;
1580     DWORD style;
1581
1582     TRACE("win %p\n", hwnd);
1583
1584     if (!(data = get_win_data(hwnd))) return;
1585     if (data->minimized) goto done;
1586
1587     style = GetWindowLongW(hwnd, GWL_STYLE);
1588
1589     data->minimized = TRUE;
1590     if ((style & WS_MINIMIZEBOX) && !(style & WS_DISABLED))
1591     {
1592         TRACE("minimizing win %p/%p\n", hwnd, data->cocoa_window);
1593         release_win_data(data);
1594         SendMessageW(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
1595         return;
1596     }
1597     TRACE("not minimizing win %p/%p style %08x\n", hwnd, data->cocoa_window, style);
1598
1599 done:
1600     release_win_data(data);
1601 }
1602
1603
1604 /***********************************************************************
1605  *              macdrv_window_did_unminimize
1606  *
1607  * Handler for WINDOW_DID_UNMINIMIZE events.
1608  */
1609 void macdrv_window_did_unminimize(HWND hwnd)
1610 {
1611     struct macdrv_win_data *data;
1612     DWORD style;
1613
1614     TRACE("win %p\n", hwnd);
1615
1616     if (!(data = get_win_data(hwnd))) return;
1617     if (!data->minimized) goto done;
1618
1619     style = GetWindowLongW(hwnd, GWL_STYLE);
1620
1621     data->minimized = FALSE;
1622     if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1623     {
1624         TRACE("restoring win %p/%p\n", hwnd, data->cocoa_window);
1625         release_win_data(data);
1626         SendMessageW(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
1627         return;
1628     }
1629
1630     TRACE("not restoring win %p/%p style %08x\n", hwnd, data->cocoa_window, style);
1631
1632 done:
1633     release_win_data(data);
1634 }