winemac: Also activate if a window is ordered front shortly after tray icon clicked.
[wine/wine-gecko.git] / dlls / winemac.drv / window.c
blob5e0393616489f06068c7a75f4fe64346c49ba8b6
1 /*
2 * MACDRV windowing driver
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.
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.
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.
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
24 #include "config.h"
26 #include "macdrv.h"
27 #include "winuser.h"
28 #include "wine/unicode.h"
29 #include "wine/server.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(macdrv);
34 static CRITICAL_SECTION win_data_section;
35 static CRITICAL_SECTION_DEBUG critsect_debug =
37 0, 0, &win_data_section,
38 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
39 0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
41 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
43 static CFMutableDictionaryRef win_datas;
45 DWORD activate_on_focus_time;
48 void CDECL macdrv_SetFocus(HWND hwnd);
51 /***********************************************************************
52 * get_cocoa_window_features
54 static void get_cocoa_window_features(struct macdrv_win_data *data,
55 DWORD style, DWORD ex_style,
56 struct macdrv_window_features* wf)
58 memset(wf, 0, sizeof(*wf));
60 if (IsRectEmpty(&data->window_rect)) return;
62 if ((style & WS_CAPTION) == WS_CAPTION && !(ex_style & WS_EX_LAYERED))
64 wf->shadow = 1;
65 if (!data->shaped)
67 wf->title_bar = 1;
68 if (style & WS_SYSMENU) wf->close_button = 1;
69 if (style & WS_MINIMIZEBOX) wf->minimize_button = 1;
70 if (style & WS_MAXIMIZEBOX) wf->resizable = 1;
71 if (ex_style & WS_EX_TOOLWINDOW) wf->utility = 1;
74 if (ex_style & WS_EX_DLGMODALFRAME) wf->shadow = 1;
75 else if (style & WS_THICKFRAME)
77 wf->shadow = 1;
78 if (!data->shaped) wf->resizable = 1;
80 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) wf->shadow = 1;
84 /*******************************************************************
85 * can_activate_window
87 * Check if we can activate the specified window.
89 static inline BOOL can_activate_window(HWND hwnd)
91 LONG style = GetWindowLongW(hwnd, GWL_STYLE);
92 RECT rect;
94 if (!(style & WS_VISIBLE)) return FALSE;
95 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
96 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_NOACTIVATE) return FALSE;
97 if (hwnd == GetDesktopWindow()) return FALSE;
98 if (GetWindowRect(hwnd, &rect) && IsRectEmpty(&rect)) return FALSE;
99 return !(style & WS_DISABLED);
103 /***********************************************************************
104 * get_cocoa_window_state
106 static void get_cocoa_window_state(struct macdrv_win_data *data,
107 DWORD style, DWORD ex_style,
108 struct macdrv_window_state* state)
110 memset(state, 0, sizeof(*state));
111 state->disabled = (style & WS_DISABLED) != 0;
112 state->no_activate = !can_activate_window(data->hwnd);
113 state->floating = (ex_style & WS_EX_TOPMOST) != 0;
114 state->excluded_by_expose = state->excluded_by_cycle =
115 !(ex_style & WS_EX_APPWINDOW) &&
116 (GetWindow(data->hwnd, GW_OWNER) || (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE)));
117 state->minimized = (style & WS_MINIMIZE) != 0;
121 /***********************************************************************
122 * get_mac_rect_offset
124 * Helper for macdrv_window_to_mac_rect and macdrv_mac_to_window_rect.
126 static void get_mac_rect_offset(struct macdrv_win_data *data, DWORD style, RECT *rect)
128 DWORD ex_style, style_mask = 0, ex_style_mask = 0;
130 rect->top = rect->bottom = rect->left = rect->right = 0;
132 ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
134 if (!data->shaped)
136 struct macdrv_window_features wf;
137 get_cocoa_window_features(data, style, ex_style, &wf);
139 if (wf.title_bar) style_mask |= WS_CAPTION;
140 if (wf.shadow)
142 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
143 ex_style_mask |= WS_EX_DLGMODALFRAME;
147 AdjustWindowRectEx(rect, style & style_mask, FALSE, ex_style & ex_style_mask);
149 TRACE("%p/%p style %08x ex_style %08x shaped %d -> %s\n", data->hwnd, data->cocoa_window,
150 style, ex_style, data->shaped, wine_dbgstr_rect(rect));
154 /***********************************************************************
155 * macdrv_window_to_mac_rect
157 * Convert a rect from client to Mac window coordinates
159 static void macdrv_window_to_mac_rect(struct macdrv_win_data *data, DWORD style, RECT *rect)
161 RECT rc;
163 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return;
164 if (IsRectEmpty(rect)) return;
166 get_mac_rect_offset(data, style, &rc);
168 rect->left -= rc.left;
169 rect->right -= rc.right;
170 rect->top -= rc.top;
171 rect->bottom -= rc.bottom;
172 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
173 if (rect->left >= rect->right) rect->right = rect->left + 1;
177 /***********************************************************************
178 * macdrv_mac_to_window_rect
180 * Opposite of macdrv_window_to_mac_rect
182 static void macdrv_mac_to_window_rect(struct macdrv_win_data *data, RECT *rect)
184 RECT rc;
185 DWORD style = GetWindowLongW(data->hwnd, GWL_STYLE);
187 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return;
188 if (IsRectEmpty(rect)) return;
190 get_mac_rect_offset(data, style, &rc);
192 rect->left += rc.left;
193 rect->right += rc.right;
194 rect->top += rc.top;
195 rect->bottom += rc.bottom;
196 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
197 if (rect->left >= rect->right) rect->right = rect->left + 1;
201 /***********************************************************************
202 * constrain_window_frame
204 * Alter a window frame rectangle to fit within a) Cocoa's documented
205 * limits, and b) sane sizes, like twice the desktop rect.
207 static void constrain_window_frame(CGRect* frame)
209 CGRect desktop_rect = macdrv_get_desktop_rect();
210 int max_width, max_height;
212 max_width = min(32000, 2 * CGRectGetWidth(desktop_rect));
213 max_height = min(32000, 2 * CGRectGetHeight(desktop_rect));
215 if (frame->origin.x < -16000) frame->origin.x = -16000;
216 if (frame->origin.y < -16000) frame->origin.y = -16000;
217 if (frame->origin.x > 16000) frame->origin.x = 16000;
218 if (frame->origin.y > 16000) frame->origin.y = 16000;
219 if (frame->size.width > max_width) frame->size.width = max_width;
220 if (frame->size.height > max_height) frame->size.height = max_height;
224 /***********************************************************************
225 * alloc_win_data
227 static struct macdrv_win_data *alloc_win_data(HWND hwnd)
229 struct macdrv_win_data *data;
231 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
233 data->hwnd = hwnd;
234 data->color_key = CLR_INVALID;
235 EnterCriticalSection(&win_data_section);
236 if (!win_datas)
237 win_datas = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
238 CFDictionarySetValue(win_datas, hwnd, data);
240 return data;
244 /***********************************************************************
245 * get_win_data
247 * Lock and return the data structure associated with a window.
249 struct macdrv_win_data *get_win_data(HWND hwnd)
251 struct macdrv_win_data *data;
253 if (!hwnd) return NULL;
254 EnterCriticalSection(&win_data_section);
255 if (win_datas && (data = (struct macdrv_win_data*)CFDictionaryGetValue(win_datas, hwnd)))
256 return data;
257 LeaveCriticalSection(&win_data_section);
258 return NULL;
262 /***********************************************************************
263 * release_win_data
265 * Release the data returned by get_win_data.
267 void release_win_data(struct macdrv_win_data *data)
269 if (data) LeaveCriticalSection(&win_data_section);
273 /***********************************************************************
274 * macdrv_get_cocoa_window
276 * Return the Mac window associated with the full area of a window
278 macdrv_window macdrv_get_cocoa_window(HWND hwnd, BOOL require_on_screen)
280 struct macdrv_win_data *data = get_win_data(hwnd);
281 macdrv_window ret = NULL;
282 if (data && (data->on_screen || !require_on_screen))
283 ret = data->cocoa_window;
284 release_win_data(data);
285 return ret;
289 /***********************************************************************
290 * set_cocoa_window_properties
292 * Set the window properties for a Cocoa window based on its Windows
293 * properties.
295 static void set_cocoa_window_properties(struct macdrv_win_data *data)
297 DWORD style, ex_style;
298 HWND owner;
299 macdrv_window owner_win;
300 struct macdrv_window_features wf;
301 struct macdrv_window_state state;
303 style = GetWindowLongW(data->hwnd, GWL_STYLE);
304 ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
306 owner = GetWindow(data->hwnd, GW_OWNER);
307 owner_win = macdrv_get_cocoa_window(owner, TRUE);
308 macdrv_set_cocoa_parent_window(data->cocoa_window, owner_win);
310 get_cocoa_window_features(data, style, ex_style, &wf);
311 macdrv_set_cocoa_window_features(data->cocoa_window, &wf);
313 get_cocoa_window_state(data, style, ex_style, &state);
314 macdrv_set_cocoa_window_state(data->cocoa_window, &state);
315 data->minimized = state.minimized;
319 /***********************************************************************
320 * sync_window_region
322 * Update the window region.
324 static void sync_window_region(struct macdrv_win_data *data, HRGN win_region)
326 HRGN hrgn = win_region;
327 RGNDATA *region_data;
328 const CGRect* rects;
329 int count;
331 if (!data->cocoa_window) return;
332 data->shaped = FALSE;
334 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
336 if (!(hrgn = CreateRectRgn(0, 0, 0, 0))) return;
337 if (GetWindowRgn(data->hwnd, hrgn) == ERROR)
339 DeleteObject(hrgn);
340 hrgn = 0;
344 if (hrgn && GetWindowLongW(data->hwnd, GWL_EXSTYLE) & WS_EX_LAYOUTRTL)
345 MirrorRgn(data->hwnd, hrgn);
346 if (hrgn)
348 OffsetRgn(hrgn, data->window_rect.left - data->whole_rect.left,
349 data->window_rect.top - data->whole_rect.top);
351 region_data = get_region_data(hrgn, 0);
352 if (region_data)
354 rects = (CGRect*)region_data->Buffer;
355 count = region_data->rdh.nCount;
356 /* Special case optimization. If the region entirely encloses the Cocoa
357 window, it's the same as there being no region. It's potentially
358 hard/slow to test this for arbitrary regions, so we just check for
359 very simple regions. */
360 if (count == 1 && CGRectContainsRect(rects[0], cgrect_from_rect(data->whole_rect)))
362 TRACE("optimizing for simple region that contains Cocoa content rect\n");
363 rects = NULL;
364 count = 0;
367 else
369 rects = NULL;
370 count = 0;
373 TRACE("win %p/%p win_region %p rects %p count %d\n", data->hwnd, data->cocoa_window, win_region, rects, count);
374 macdrv_set_window_shape(data->cocoa_window, rects, count);
376 HeapFree(GetProcessHeap(), 0, region_data);
377 data->shaped = (region_data != NULL);
379 if (hrgn && hrgn != win_region) DeleteObject(hrgn);
383 /***********************************************************************
384 * add_bounds_rect
386 static inline void add_bounds_rect(RECT *bounds, const RECT *rect)
388 if (rect->left >= rect->right || rect->top >= rect->bottom) return;
389 bounds->left = min(bounds->left, rect->left);
390 bounds->top = min(bounds->top, rect->top);
391 bounds->right = max(bounds->right, rect->right);
392 bounds->bottom = max(bounds->bottom, rect->bottom);
396 /***********************************************************************
397 * sync_window_opacity
399 static void sync_window_opacity(struct macdrv_win_data *data, COLORREF key, BYTE alpha,
400 BOOL per_pixel_alpha, DWORD flags)
402 CGFloat opacity = 1.0;
403 BOOL needs_flush = FALSE;
405 if (flags & LWA_ALPHA) opacity = alpha / 255.0;
407 TRACE("setting window %p/%p alpha to %g\n", data->hwnd, data->cocoa_window, opacity);
408 macdrv_set_window_alpha(data->cocoa_window, opacity);
410 if (flags & LWA_COLORKEY)
412 /* FIXME: treat PALETTEINDEX and DIBINDEX as black */
413 if ((key & (1 << 24)) || key >> 16 == 0x10ff)
414 key = RGB(0, 0, 0);
416 else
417 key = CLR_INVALID;
419 if (data->color_key != key)
421 if (key == CLR_INVALID)
423 TRACE("clearing color-key for window %p/%p\n", data->hwnd, data->cocoa_window);
424 macdrv_clear_window_color_key(data->cocoa_window);
426 else
428 TRACE("setting color-key for window %p/%p to RGB %d,%d,%d\n", data->hwnd, data->cocoa_window,
429 GetRValue(key), GetGValue(key), GetBValue(key));
430 macdrv_set_window_color_key(data->cocoa_window, GetRValue(key), GetGValue(key), GetBValue(key));
433 data->color_key = key;
434 needs_flush = TRUE;
437 if (!data->per_pixel_alpha != !per_pixel_alpha)
439 macdrv_window_use_per_pixel_alpha(data->cocoa_window, per_pixel_alpha);
440 data->per_pixel_alpha = per_pixel_alpha;
441 needs_flush = TRUE;
444 if (needs_flush && data->surface)
446 RECT *bounds;
447 RECT rect;
449 rect = data->whole_rect;
450 OffsetRect(&rect, -data->whole_rect.left, -data->whole_rect.top);
451 data->surface->funcs->lock(data->surface);
452 bounds = data->surface->funcs->get_bounds(data->surface);
453 add_bounds_rect(bounds, &rect);
454 data->surface->funcs->unlock(data->surface);
459 /**********************************************************************
460 * create_cocoa_window
462 * Create the whole Mac window for a given window
464 static void create_cocoa_window(struct macdrv_win_data *data)
466 struct macdrv_thread_data *thread_data = macdrv_init_thread_data();
467 WCHAR text[1024];
468 struct macdrv_window_features wf;
469 CGRect frame;
470 DWORD style, ex_style;
471 HRGN win_rgn;
472 COLORREF key;
473 BYTE alpha;
474 DWORD layered_flags;
476 if ((win_rgn = CreateRectRgn(0, 0, 0, 0)) &&
477 GetWindowRgn(data->hwnd, win_rgn) == ERROR)
479 DeleteObject(win_rgn);
480 win_rgn = 0;
482 data->shaped = (win_rgn != 0);
484 style = GetWindowLongW(data->hwnd, GWL_STYLE);
485 ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
487 data->whole_rect = data->window_rect;
488 macdrv_window_to_mac_rect(data, style, &data->whole_rect);
490 memset(&wf, 0, sizeof(wf));
491 get_cocoa_window_features(data, style, ex_style, &wf);
493 frame = cgrect_from_rect(data->whole_rect);
494 constrain_window_frame(&frame);
496 TRACE("creating %p window %s whole %s client %s\n", data->hwnd, wine_dbgstr_rect(&data->window_rect),
497 wine_dbgstr_rect(&data->whole_rect), wine_dbgstr_rect(&data->client_rect));
499 data->cocoa_window = macdrv_create_cocoa_window(&wf, frame, data->hwnd, thread_data->queue);
500 if (!data->cocoa_window) goto done;
502 set_cocoa_window_properties(data);
504 /* set the window text */
505 if (!InternalGetWindowText(data->hwnd, text, sizeof(text)/sizeof(WCHAR))) text[0] = 0;
506 macdrv_set_cocoa_window_title(data->cocoa_window, text, strlenW(text));
508 /* set the window region */
509 if (win_rgn) sync_window_region(data, win_rgn);
511 /* set the window opacity */
512 if (!GetLayeredWindowAttributes(data->hwnd, &key, &alpha, &layered_flags)) layered_flags = 0;
513 sync_window_opacity(data, key, alpha, FALSE, layered_flags);
515 done:
516 if (win_rgn) DeleteObject(win_rgn);
520 /**********************************************************************
521 * destroy_cocoa_window
523 * Destroy the whole Mac window for a given window.
525 static void destroy_cocoa_window(struct macdrv_win_data *data)
527 if (!data->cocoa_window) return;
529 TRACE("win %p Cocoa win %p\n", data->hwnd, data->cocoa_window);
531 macdrv_destroy_cocoa_window(data->cocoa_window);
532 data->cocoa_window = 0;
533 data->on_screen = FALSE;
534 data->color_key = CLR_INVALID;
535 if (data->surface) window_surface_release(data->surface);
536 data->surface = NULL;
537 if (data->unminimized_surface) window_surface_release(data->unminimized_surface);
538 data->unminimized_surface = NULL;
542 /***********************************************************************
543 * macdrv_create_win_data
545 * Create a Mac data window structure for an existing window.
547 static struct macdrv_win_data *macdrv_create_win_data(HWND hwnd, const RECT *window_rect,
548 const RECT *client_rect)
550 struct macdrv_win_data *data;
551 HWND parent;
553 if (GetWindowThreadProcessId(hwnd, NULL) != GetCurrentThreadId()) return NULL;
555 if (!(parent = GetAncestor(hwnd, GA_PARENT))) /* desktop */
557 macdrv_init_thread_data();
558 return NULL;
561 /* don't create win data for HWND_MESSAGE windows */
562 if (parent != GetDesktopWindow() && !GetAncestor(parent, GA_PARENT)) return NULL;
564 if (!(data = alloc_win_data(hwnd))) return NULL;
566 data->whole_rect = data->window_rect = *window_rect;
567 data->client_rect = *client_rect;
569 if (parent == GetDesktopWindow())
571 create_cocoa_window(data);
572 TRACE("win %p/%p window %s whole %s client %s\n",
573 hwnd, data->cocoa_window, wine_dbgstr_rect(&data->window_rect),
574 wine_dbgstr_rect(&data->whole_rect), wine_dbgstr_rect(&data->client_rect));
577 return data;
581 /***********************************************************************
582 * show_window
584 static void show_window(struct macdrv_win_data *data)
586 HWND prev = NULL;
587 HWND next = NULL;
588 macdrv_window prev_window = NULL;
589 macdrv_window next_window = NULL;
590 BOOL activate = FALSE;
592 /* find window that this one must be after */
593 prev = GetWindow(data->hwnd, GW_HWNDPREV);
594 while (prev && !((GetWindowLongW(prev, GWL_STYLE) & WS_VISIBLE) &&
595 (prev_window = macdrv_get_cocoa_window(prev, TRUE))))
596 prev = GetWindow(prev, GW_HWNDPREV);
597 if (!prev_window)
599 /* find window that this one must be before */
600 next = GetWindow(data->hwnd, GW_HWNDNEXT);
601 while (next && !((GetWindowLongW(next, GWL_STYLE) & WS_VISIBLE) &&
602 (next_window = macdrv_get_cocoa_window(next, TRUE))))
603 next = GetWindow(next, GW_HWNDNEXT);
606 TRACE("win %p/%p below %p/%p above %p/%p\n",
607 data->hwnd, data->cocoa_window, prev, prev_window, next, next_window);
609 if (!prev_window)
610 activate = activate_on_focus_time && (GetTickCount() - activate_on_focus_time < 2000);
611 data->on_screen = macdrv_order_cocoa_window(data->cocoa_window, prev_window, next_window, activate);
612 if (data->on_screen)
614 HWND hwndFocus = GetFocus();
615 if (hwndFocus && (data->hwnd == hwndFocus || IsChild(data->hwnd, hwndFocus)))
616 macdrv_SetFocus(hwndFocus);
617 if (activate)
618 activate_on_focus_time = 0;
623 /***********************************************************************
624 * hide_window
626 static void hide_window(struct macdrv_win_data *data)
628 TRACE("win %p/%p\n", data->hwnd, data->cocoa_window);
630 macdrv_hide_cocoa_window(data->cocoa_window);
631 data->on_screen = FALSE;
635 /***********************************************************************
636 * get_region_data
638 * Calls GetRegionData on the given region and converts the rectangle
639 * array to CGRect format. The returned buffer must be freed by
640 * caller using HeapFree(GetProcessHeap(),...).
641 * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
643 RGNDATA *get_region_data(HRGN hrgn, HDC hdc_lptodp)
645 RGNDATA *data;
646 DWORD size;
647 int i;
648 RECT *rect;
649 CGRect *cgrect;
651 if (!hrgn || !(size = GetRegionData(hrgn, 0, NULL))) return NULL;
652 if (sizeof(CGRect) > sizeof(RECT))
654 /* add extra size for CGRect array */
655 int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
656 size += count * (sizeof(CGRect) - sizeof(RECT));
658 if (!(data = HeapAlloc(GetProcessHeap(), 0, size))) return NULL;
659 if (!GetRegionData(hrgn, size, data))
661 HeapFree(GetProcessHeap(), 0, data);
662 return NULL;
665 rect = (RECT *)data->Buffer;
666 cgrect = (CGRect *)data->Buffer;
667 if (hdc_lptodp) /* map to device coordinates */
669 LPtoDP(hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2);
670 for (i = 0; i < data->rdh.nCount; i++)
672 if (rect[i].right < rect[i].left)
674 INT tmp = rect[i].right;
675 rect[i].right = rect[i].left;
676 rect[i].left = tmp;
678 if (rect[i].bottom < rect[i].top)
680 INT tmp = rect[i].bottom;
681 rect[i].bottom = rect[i].top;
682 rect[i].top = tmp;
687 if (sizeof(CGRect) > sizeof(RECT))
689 /* need to start from the end */
690 for (i = data->rdh.nCount-1; i >= 0; i--)
691 cgrect[i] = cgrect_from_rect(rect[i]);
693 else
695 for (i = 0; i < data->rdh.nCount; i++)
696 cgrect[i] = cgrect_from_rect(rect[i]);
698 return data;
702 /***********************************************************************
703 * sync_window_position
705 * Synchronize the Mac window position with the Windows one
707 static void sync_window_position(struct macdrv_win_data *data, UINT swp_flags)
709 CGRect frame;
711 if (data->minimized) return;
713 frame = cgrect_from_rect(data->whole_rect);
714 constrain_window_frame(&frame);
716 data->on_screen = macdrv_set_cocoa_window_frame(data->cocoa_window, &frame);
717 if (data->shaped) sync_window_region(data, (HRGN)1);
719 TRACE("win %p/%p pos %s\n", data->hwnd, data->cocoa_window,
720 wine_dbgstr_rect(&data->whole_rect));
722 if (data->on_screen && (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW)))
723 show_window(data);
727 /***********************************************************************
728 * move_window_bits
730 * Move the window bits when a window is moved.
732 static void move_window_bits(HWND hwnd, macdrv_window window, const RECT *old_rect, const RECT *new_rect,
733 const RECT *old_client_rect, const RECT *new_client_rect,
734 const RECT *new_window_rect)
736 RECT src_rect = *old_rect;
737 RECT dst_rect = *new_rect;
738 HDC hdc_src, hdc_dst;
739 HRGN rgn;
740 HWND parent = 0;
742 if (!window)
744 OffsetRect(&dst_rect, -new_window_rect->left, -new_window_rect->top);
745 parent = GetAncestor(hwnd, GA_PARENT);
746 hdc_src = GetDCEx(parent, 0, DCX_CACHE);
747 hdc_dst = GetDCEx(hwnd, 0, DCX_CACHE | DCX_WINDOW);
749 else
751 OffsetRect(&dst_rect, -new_client_rect->left, -new_client_rect->top);
752 /* make src rect relative to the old position of the window */
753 OffsetRect(&src_rect, -old_client_rect->left, -old_client_rect->top);
754 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
755 hdc_src = hdc_dst = GetDCEx(hwnd, 0, DCX_CACHE);
758 rgn = CreateRectRgnIndirect(&dst_rect);
759 SelectClipRgn(hdc_dst, rgn);
760 DeleteObject(rgn);
761 ExcludeUpdateRgn(hdc_dst, hwnd);
763 TRACE("copying bits for win %p/%p %s -> %s\n", hwnd, window,
764 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect));
765 BitBlt(hdc_dst, dst_rect.left, dst_rect.top,
766 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
767 hdc_src, src_rect.left, src_rect.top, SRCCOPY);
769 ReleaseDC(hwnd, hdc_dst);
770 if (hdc_src != hdc_dst) ReleaseDC(parent, hdc_src);
774 /**********************************************************************
775 * CreateDesktopWindow (MACDRV.@)
777 BOOL CDECL macdrv_CreateDesktopWindow(HWND hwnd)
779 unsigned int width, height;
781 TRACE("%p\n", hwnd);
783 /* retrieve the real size of the desktop */
784 SERVER_START_REQ(get_window_rectangles)
786 req->handle = wine_server_user_handle(hwnd);
787 req->relative = COORDS_CLIENT;
788 wine_server_call(req);
789 width = reply->window.right;
790 height = reply->window.bottom;
792 SERVER_END_REQ;
794 if (!width && !height) /* not initialized yet */
796 CGRect rect = macdrv_get_desktop_rect();
798 SERVER_START_REQ(set_window_pos)
800 req->handle = wine_server_user_handle(hwnd);
801 req->previous = 0;
802 req->swp_flags = SWP_NOZORDER;
803 req->window.left = CGRectGetMinX(rect);
804 req->window.top = CGRectGetMinY(rect);
805 req->window.right = CGRectGetMaxX(rect);
806 req->window.bottom = CGRectGetMaxY(rect);
807 req->client = req->window;
808 wine_server_call(req);
810 SERVER_END_REQ;
813 return TRUE;
817 /**********************************************************************
818 * CreateWindow (MACDRV.@)
820 BOOL CDECL macdrv_CreateWindow(HWND hwnd)
822 return TRUE;
826 /***********************************************************************
827 * DestroyWindow (MACDRV.@)
829 void CDECL macdrv_DestroyWindow(HWND hwnd)
831 struct macdrv_win_data *data;
833 TRACE("%p\n", hwnd);
835 if (!(data = get_win_data(hwnd))) return;
837 if (data->gl_view) macdrv_dispose_view(data->gl_view);
838 destroy_cocoa_window(data);
840 CFDictionaryRemoveValue(win_datas, hwnd);
841 release_win_data(data);
842 HeapFree(GetProcessHeap(), 0, data);
846 /*****************************************************************
847 * SetFocus (MACDRV.@)
849 * Set the Mac focus.
851 void CDECL macdrv_SetFocus(HWND hwnd)
853 struct macdrv_thread_data *thread_data = macdrv_thread_data();
854 struct macdrv_win_data *data;
856 TRACE("%p\n", hwnd);
858 if (!thread_data) return;
859 thread_data->dead_key_state = 0;
861 if (!(hwnd = GetAncestor(hwnd, GA_ROOT))) return;
862 if (!(data = get_win_data(hwnd))) return;
864 if (data->cocoa_window && data->on_screen)
866 BOOL activate = activate_on_focus_time && (GetTickCount() - activate_on_focus_time < 2000);
867 /* Set Mac focus */
868 macdrv_give_cocoa_window_focus(data->cocoa_window, activate);
869 activate_on_focus_time = 0;
872 release_win_data(data);
876 /***********************************************************************
877 * SetLayeredWindowAttributes (MACDRV.@)
879 * Set transparency attributes for a layered window.
881 void CDECL macdrv_SetLayeredWindowAttributes(HWND hwnd, COLORREF key, BYTE alpha, DWORD flags)
883 struct macdrv_win_data *data = get_win_data(hwnd);
885 TRACE("hwnd %p key %#08x alpha %#02x flags %x\n", hwnd, key, alpha, flags);
887 if (data)
889 data->layered = TRUE;
890 if (data->cocoa_window)
892 sync_window_opacity(data, key, alpha, FALSE, flags);
893 /* since layered attributes are now set, can now show the window */
894 if ((GetWindowLongW(hwnd, GWL_STYLE) & WS_VISIBLE) && !data->on_screen)
895 show_window(data);
897 release_win_data(data);
899 else
900 FIXME("setting layered attributes on window %p of other process not supported\n", hwnd);
904 /*****************************************************************
905 * SetParent (MACDRV.@)
907 void CDECL macdrv_SetParent(HWND hwnd, HWND parent, HWND old_parent)
909 struct macdrv_win_data *data;
911 TRACE("%p, %p, %p\n", hwnd, parent, old_parent);
913 if (parent == old_parent) return;
914 if (!(data = get_win_data(hwnd))) return;
916 if (parent != GetDesktopWindow()) /* a child window */
918 if (old_parent == GetDesktopWindow())
920 /* destroy the old Mac window */
921 destroy_cocoa_window(data);
924 else /* new top level window */
925 create_cocoa_window(data);
926 release_win_data(data);
928 set_gl_view_parent(hwnd, parent);
932 /***********************************************************************
933 * SetWindowRgn (MACDRV.@)
935 * Assign specified region to window (for non-rectangular windows)
937 int CDECL macdrv_SetWindowRgn(HWND hwnd, HRGN hrgn, BOOL redraw)
939 struct macdrv_win_data *data;
941 TRACE("%p, %p, %d\n", hwnd, hrgn, redraw);
943 if ((data = get_win_data(hwnd)))
945 sync_window_region(data, hrgn);
946 release_win_data(data);
948 else
950 DWORD procid;
952 GetWindowThreadProcessId(hwnd, &procid);
953 if (procid != GetCurrentProcessId())
954 SendMessageW(hwnd, WM_MACDRV_SET_WIN_REGION, 0, 0);
957 return TRUE;
961 /***********************************************************************
962 * SetWindowStyle (MACDRV.@)
964 * Update the state of the Cocoa window to reflect a style change
966 void CDECL macdrv_SetWindowStyle(HWND hwnd, INT offset, STYLESTRUCT *style)
968 struct macdrv_win_data *data;
970 TRACE("hwnd %p offset %d styleOld 0x%08x styleNew 0x%08x\n", hwnd, offset, style->styleOld, style->styleNew);
972 if (hwnd == GetDesktopWindow()) return;
973 if (!(data = get_win_data(hwnd))) return;
975 if (data->cocoa_window)
977 DWORD changed = style->styleNew ^ style->styleOld;
979 set_cocoa_window_properties(data);
981 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
983 data->layered = FALSE;
984 data->ulw_layered = FALSE;
985 sync_window_opacity(data, 0, 0, FALSE, 0);
986 if (data->surface) set_surface_use_alpha(data->surface, FALSE);
990 release_win_data(data);
994 /*****************************************************************
995 * SetWindowText (MACDRV.@)
997 void CDECL macdrv_SetWindowText(HWND hwnd, LPCWSTR text)
999 macdrv_window win;
1001 TRACE("%p, %s\n", hwnd, debugstr_w(text));
1003 if ((win = macdrv_get_cocoa_window(hwnd, FALSE)))
1004 macdrv_set_cocoa_window_title(win, text, strlenW(text));
1008 /***********************************************************************
1009 * ShowWindow (MACDRV.@)
1011 UINT CDECL macdrv_ShowWindow(HWND hwnd, INT cmd, RECT *rect, UINT swp)
1013 struct macdrv_thread_data *thread_data = macdrv_thread_data();
1014 struct macdrv_win_data *data = get_win_data(hwnd);
1015 CGRect frame;
1017 if (!data || !data->cocoa_window) goto done;
1018 if (IsRectEmpty(rect)) goto done;
1019 if (GetWindowLongW(hwnd, GWL_STYLE) & WS_MINIMIZE)
1021 if (rect->left != -32000 || rect->top != -32000)
1023 OffsetRect(rect, -32000 - rect->left, -32000 - rect->top);
1024 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
1026 goto done;
1028 if (!data->on_screen) goto done;
1030 /* only fetch the new rectangle if the ShowWindow was a result of an external event */
1032 if (!thread_data->current_event || thread_data->current_event->window != data->cocoa_window)
1033 goto done;
1035 if (thread_data->current_event->type != WINDOW_FRAME_CHANGED &&
1036 thread_data->current_event->type != WINDOW_DID_MINIMIZE &&
1037 thread_data->current_event->type != WINDOW_DID_UNMINIMIZE)
1038 goto done;
1040 TRACE("win %p/%p cmd %d at %s flags %08x\n",
1041 hwnd, data->cocoa_window, cmd, wine_dbgstr_rect(rect), swp);
1043 macdrv_get_cocoa_window_frame(data->cocoa_window, &frame);
1044 *rect = rect_from_cgrect(frame);
1045 macdrv_mac_to_window_rect(data, rect);
1046 TRACE("rect %s -> %s\n", wine_dbgstr_cgrect(frame), wine_dbgstr_rect(rect));
1047 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
1049 done:
1050 release_win_data(data);
1051 return swp;
1055 /***********************************************************************
1056 * SysCommand (MACDRV.@)
1058 * Perform WM_SYSCOMMAND handling.
1060 LRESULT CDECL macdrv_SysCommand(HWND hwnd, WPARAM wparam, LPARAM lparam)
1062 struct macdrv_win_data *data;
1063 LRESULT ret = -1;
1065 TRACE("%p, %x, %lx\n", hwnd, (unsigned)wparam, lparam);
1067 if (!(data = get_win_data(hwnd))) goto done;
1068 if (!data->cocoa_window || !data->on_screen) goto done;
1070 /* prevent a simple ALT press+release from activating the system menu,
1071 as that can get confusing */
1072 if ((wparam & 0xfff0) == SC_KEYMENU && !(WCHAR)lparam && !GetMenu(hwnd) &&
1073 (GetWindowLongW(hwnd, GWL_STYLE) & WS_SYSMENU))
1075 TRACE("ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam);
1076 ret = 0;
1079 done:
1080 release_win_data(data);
1081 return ret;
1085 /***********************************************************************
1086 * UpdateLayeredWindow (MACDRV.@)
1088 BOOL CDECL macdrv_UpdateLayeredWindow(HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
1089 const RECT *window_rect)
1091 struct window_surface *surface;
1092 struct macdrv_win_data *data;
1093 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
1094 BYTE alpha;
1095 char buffer[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1096 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
1097 void *src_bits, *dst_bits;
1098 RECT rect;
1099 HDC hdc = 0;
1100 HBITMAP dib;
1101 BOOL ret = FALSE;
1103 if (!(data = get_win_data(hwnd))) return FALSE;
1105 data->layered = TRUE;
1106 data->ulw_layered = TRUE;
1108 rect = *window_rect;
1109 OffsetRect(&rect, -window_rect->left, -window_rect->top);
1111 surface = data->surface;
1112 if (!surface || memcmp(&surface->rect, &rect, sizeof(RECT)))
1114 data->surface = create_surface(data->cocoa_window, &rect, NULL, TRUE);
1115 set_window_surface(data->cocoa_window, data->surface);
1116 if (surface) window_surface_release(surface);
1117 surface = data->surface;
1118 if (data->unminimized_surface)
1120 window_surface_release(data->unminimized_surface);
1121 data->unminimized_surface = NULL;
1124 else set_surface_use_alpha(surface, TRUE);
1126 if (surface) window_surface_add_ref(surface);
1127 release_win_data(data);
1129 if (!surface) return FALSE;
1130 if (!info->hdcSrc)
1132 window_surface_release(surface);
1133 return TRUE;
1136 if (info->dwFlags & ULW_ALPHA)
1138 /* Apply SourceConstantAlpha via window alpha, not blend. */
1139 alpha = info->pblend->SourceConstantAlpha;
1140 blend = *info->pblend;
1141 blend.SourceConstantAlpha = 0xff;
1143 else
1144 alpha = 0xff;
1146 dst_bits = surface->funcs->get_info(surface, bmi);
1148 if (!(dib = CreateDIBSection(info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0))) goto done;
1149 if (!(hdc = CreateCompatibleDC(0))) goto done;
1151 SelectObject(hdc, dib);
1152 if (info->prcDirty)
1154 IntersectRect(&rect, &rect, info->prcDirty);
1155 surface->funcs->lock(surface);
1156 memcpy(src_bits, dst_bits, bmi->bmiHeader.biSizeImage);
1157 surface->funcs->unlock(surface);
1158 PatBlt(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS);
1160 if (!(ret = GdiAlphaBlend(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
1161 info->hdcSrc,
1162 rect.left + (info->pptSrc ? info->pptSrc->x : 0),
1163 rect.top + (info->pptSrc ? info->pptSrc->y : 0),
1164 rect.right - rect.left, rect.bottom - rect.top,
1165 blend)))
1166 goto done;
1168 if ((data = get_win_data(hwnd)))
1170 if (surface == data->surface)
1172 surface->funcs->lock(surface);
1173 memcpy(dst_bits, src_bits, bmi->bmiHeader.biSizeImage);
1174 add_bounds_rect(surface->funcs->get_bounds(surface), &rect);
1175 surface->funcs->unlock(surface);
1176 surface->funcs->flush(surface);
1179 /* The ULW flags are a superset of the LWA flags. */
1180 sync_window_opacity(data, info->crKey, alpha, TRUE, info->dwFlags);
1182 release_win_data(data);
1185 done:
1186 window_surface_release(surface);
1187 if (hdc) DeleteDC(hdc);
1188 if (dib) DeleteObject(dib);
1189 return ret;
1193 /**********************************************************************
1194 * WindowMessage (MACDRV.@)
1196 LRESULT CDECL macdrv_WindowMessage(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1198 struct macdrv_win_data *data;
1200 TRACE("%p, %u, %u, %lu\n", hwnd, msg, (unsigned)wp, lp);
1202 switch(msg)
1204 case WM_MACDRV_SET_WIN_REGION:
1205 if ((data = get_win_data(hwnd)))
1207 sync_window_region(data, (HRGN)1);
1208 release_win_data(data);
1210 return 0;
1211 case WM_MACDRV_UPDATE_DESKTOP_RECT:
1212 if (hwnd == GetDesktopWindow())
1214 CGRect new_desktop_rect;
1215 RECT current_desktop_rect;
1217 macdrv_reset_device_metrics();
1218 new_desktop_rect = macdrv_get_desktop_rect();
1219 if (!GetWindowRect(hwnd, &current_desktop_rect) ||
1220 !CGRectEqualToRect(cgrect_from_rect(current_desktop_rect), new_desktop_rect))
1222 SendMessageTimeoutW(HWND_BROADCAST, WM_MACDRV_RESET_DEVICE_METRICS, 0, 0,
1223 SMTO_ABORTIFHUNG, 2000, NULL);
1224 SetWindowPos(hwnd, 0, CGRectGetMinX(new_desktop_rect), CGRectGetMinY(new_desktop_rect),
1225 CGRectGetWidth(new_desktop_rect), CGRectGetHeight(new_desktop_rect),
1226 SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE);
1227 SendMessageTimeoutW(HWND_BROADCAST, WM_MACDRV_DISPLAYCHANGE, wp, lp,
1228 SMTO_ABORTIFHUNG, 2000, NULL);
1231 return 0;
1232 case WM_MACDRV_RESET_DEVICE_METRICS:
1233 macdrv_reset_device_metrics();
1234 return 0;
1235 case WM_MACDRV_DISPLAYCHANGE:
1236 if ((data = get_win_data(hwnd)))
1238 if (data->cocoa_window && data->on_screen)
1239 sync_window_position(data, SWP_NOZORDER | SWP_NOACTIVATE);
1240 release_win_data(data);
1242 SendMessageW(hwnd, WM_DISPLAYCHANGE, wp, lp);
1243 return 0;
1244 case WM_MACDRV_ACTIVATE_ON_FOLLOWING_FOCUS:
1245 activate_on_focus_time = GetTickCount();
1246 if (!activate_on_focus_time) activate_on_focus_time = 1;
1247 TRACE("WM_MACDRV_ACTIVATE_ON_FOLLOWING_FOCUS time %u\n", activate_on_focus_time);
1248 return 0;
1251 FIXME("unrecognized window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp);
1252 return 0;
1256 static inline RECT get_surface_rect(const RECT *visible_rect)
1258 RECT rect;
1259 RECT desktop_rect = rect_from_cgrect(macdrv_get_desktop_rect());
1261 IntersectRect(&rect, visible_rect, &desktop_rect);
1262 OffsetRect(&rect, -visible_rect->left, -visible_rect->top);
1263 rect.left &= ~127;
1264 rect.top &= ~127;
1265 rect.right = max(rect.left + 128, (rect.right + 127) & ~127);
1266 rect.bottom = max(rect.top + 128, (rect.bottom + 127) & ~127);
1267 return rect;
1271 /***********************************************************************
1272 * WindowPosChanging (MACDRV.@)
1274 void CDECL macdrv_WindowPosChanging(HWND hwnd, HWND insert_after, UINT swp_flags,
1275 const RECT *window_rect, const RECT *client_rect,
1276 RECT *visible_rect, struct window_surface **surface)
1278 struct macdrv_win_data *data = get_win_data(hwnd);
1279 DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
1280 RECT surface_rect;
1282 TRACE("%p after %p swp %04x window %s client %s visible %s surface %p\n", hwnd, insert_after,
1283 swp_flags, wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
1284 wine_dbgstr_rect(visible_rect), surface);
1286 if (!data && !(data = macdrv_create_win_data(hwnd, window_rect, client_rect))) return;
1288 *visible_rect = *window_rect;
1289 macdrv_window_to_mac_rect(data, style, visible_rect);
1290 TRACE("visible_rect %s -> %s\n", wine_dbgstr_rect(window_rect),
1291 wine_dbgstr_rect(visible_rect));
1293 /* create the window surface if necessary */
1294 if (!data->cocoa_window) goto done;
1295 if (swp_flags & SWP_HIDEWINDOW) goto done;
1296 if (data->ulw_layered) goto done;
1298 if (*surface) window_surface_release(*surface);
1299 *surface = NULL;
1301 surface_rect = get_surface_rect(visible_rect);
1302 if (data->surface)
1304 if (!memcmp(&data->surface->rect, &surface_rect, sizeof(surface_rect)))
1306 /* existing surface is good enough */
1307 surface_clip_to_visible_rect(data->surface, visible_rect);
1308 window_surface_add_ref(data->surface);
1309 *surface = data->surface;
1310 goto done;
1313 else if (!(swp_flags & SWP_SHOWWINDOW) && !(style & WS_VISIBLE)) goto done;
1315 *surface = create_surface(data->cocoa_window, &surface_rect, data->surface, FALSE);
1317 done:
1318 release_win_data(data);
1322 /***********************************************************************
1323 * WindowPosChanged (MACDRV.@)
1325 void CDECL macdrv_WindowPosChanged(HWND hwnd, HWND insert_after, UINT swp_flags,
1326 const RECT *window_rect, const RECT *client_rect,
1327 const RECT *visible_rect, const RECT *valid_rects,
1328 struct window_surface *surface)
1330 struct macdrv_thread_data *thread_data;
1331 struct macdrv_win_data *data;
1332 DWORD new_style = GetWindowLongW(hwnd, GWL_STYLE);
1333 RECT old_window_rect, old_whole_rect, old_client_rect;
1335 if (!(data = get_win_data(hwnd))) return;
1337 thread_data = macdrv_thread_data();
1339 old_window_rect = data->window_rect;
1340 old_whole_rect = data->whole_rect;
1341 old_client_rect = data->client_rect;
1342 data->window_rect = *window_rect;
1343 data->whole_rect = *visible_rect;
1344 data->client_rect = *client_rect;
1345 if (!data->ulw_layered)
1347 if (surface) window_surface_add_ref(surface);
1348 if (new_style & WS_MINIMIZE)
1350 if (!data->unminimized_surface && data->surface)
1352 data->unminimized_surface = data->surface;
1353 window_surface_add_ref(data->unminimized_surface);
1356 else
1358 set_window_surface(data->cocoa_window, surface);
1359 if (data->unminimized_surface)
1361 window_surface_release(data->unminimized_surface);
1362 data->unminimized_surface = NULL;
1365 if (data->surface) window_surface_release(data->surface);
1366 data->surface = surface;
1369 TRACE("win %p/%p window %s whole %s client %s style %08x flags %08x surface %p\n",
1370 hwnd, data->cocoa_window, wine_dbgstr_rect(window_rect),
1371 wine_dbgstr_rect(visible_rect), wine_dbgstr_rect(client_rect),
1372 new_style, swp_flags, surface);
1374 if (!IsRectEmpty(&valid_rects[0]))
1376 macdrv_window window = data->cocoa_window;
1377 int x_offset = old_whole_rect.left - data->whole_rect.left;
1378 int y_offset = old_whole_rect.top - data->whole_rect.top;
1380 /* if all that happened is that the whole window moved, copy everything */
1381 if (!(swp_flags & SWP_FRAMECHANGED) &&
1382 old_whole_rect.right - data->whole_rect.right == x_offset &&
1383 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
1384 old_client_rect.left - data->client_rect.left == x_offset &&
1385 old_client_rect.right - data->client_rect.right == x_offset &&
1386 old_client_rect.top - data->client_rect.top == y_offset &&
1387 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
1388 !memcmp(&valid_rects[0], &data->client_rect, sizeof(RECT)))
1390 /* A Cocoa window's bits are moved automatically */
1391 if (!window && (x_offset != 0 || y_offset != 0))
1393 release_win_data(data);
1394 move_window_bits(hwnd, window, &old_whole_rect, visible_rect,
1395 &old_client_rect, client_rect, window_rect);
1396 if (!(data = get_win_data(hwnd))) return;
1399 else
1401 release_win_data(data);
1402 move_window_bits(hwnd, window, &valid_rects[1], &valid_rects[0],
1403 &old_client_rect, client_rect, window_rect);
1404 if (!(data = get_win_data(hwnd))) return;
1408 sync_gl_view(data);
1410 if (!data->cocoa_window) goto done;
1412 if (data->on_screen)
1414 if ((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE))
1415 hide_window(data);
1418 /* check if we are currently processing an event relevant to this window */
1419 if (!thread_data || !thread_data->current_event ||
1420 thread_data->current_event->window != data->cocoa_window ||
1421 (thread_data->current_event->type != WINDOW_FRAME_CHANGED &&
1422 thread_data->current_event->type != WINDOW_DID_MINIMIZE &&
1423 thread_data->current_event->type != WINDOW_DID_UNMINIMIZE))
1425 sync_window_position(data, swp_flags);
1426 set_cocoa_window_properties(data);
1429 if (new_style & WS_VISIBLE)
1431 if (!data->on_screen || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
1432 set_cocoa_window_properties(data);
1434 /* layered windows are not shown until their attributes are set */
1435 if (!data->on_screen &&
1436 (data->layered || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED)))
1437 show_window(data);
1440 done:
1441 release_win_data(data);
1445 /***********************************************************************
1446 * macdrv_window_close_requested
1448 * Handler for WINDOW_CLOSE_REQUESTED events.
1450 void macdrv_window_close_requested(HWND hwnd)
1452 /* Ignore the delete window request if the window has been disabled. This
1453 * is to disallow applications from being closed while in a modal state.
1455 if (IsWindowEnabled(hwnd))
1457 HMENU hSysMenu;
1459 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return;
1460 hSysMenu = GetSystemMenu(hwnd, FALSE);
1461 if (hSysMenu)
1463 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
1464 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
1465 return;
1467 if (GetActiveWindow() != hwnd)
1469 LRESULT ma = SendMessageW(hwnd, WM_MOUSEACTIVATE,
1470 (WPARAM)GetAncestor(hwnd, GA_ROOT),
1471 MAKELPARAM(HTCLOSE, WM_NCLBUTTONDOWN));
1472 switch(ma)
1474 case MA_NOACTIVATEANDEAT:
1475 case MA_ACTIVATEANDEAT:
1476 return;
1477 case MA_NOACTIVATE:
1478 break;
1479 case MA_ACTIVATE:
1480 case 0:
1481 SetActiveWindow(hwnd);
1482 break;
1483 default:
1484 WARN("unknown WM_MOUSEACTIVATE code %d\n", (int) ma);
1485 break;
1489 PostMessageW(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
1494 /***********************************************************************
1495 * macdrv_window_frame_changed
1497 * Handler for WINDOW_FRAME_CHANGED events.
1499 void macdrv_window_frame_changed(HWND hwnd, CGRect frame)
1501 struct macdrv_win_data *data;
1502 RECT rect;
1503 HWND parent;
1504 UINT flags = SWP_NOACTIVATE | SWP_NOZORDER;
1505 int width, height;
1507 if (!hwnd) return;
1508 if (!(data = get_win_data(hwnd))) return;
1509 if (!data->on_screen || data->minimized)
1511 release_win_data(data);
1512 return;
1515 /* Get geometry */
1517 parent = GetAncestor(hwnd, GA_PARENT);
1519 TRACE("win %p/%p new Cocoa frame %s\n", hwnd, data->cocoa_window, wine_dbgstr_cgrect(frame));
1521 rect = rect_from_cgrect(frame);
1522 macdrv_mac_to_window_rect(data, &rect);
1523 MapWindowPoints(0, parent, (POINT *)&rect, 2);
1525 width = rect.right - rect.left;
1526 height = rect.bottom - rect.top;
1528 if (data->window_rect.left == rect.left && data->window_rect.top == rect.top)
1529 flags |= SWP_NOMOVE;
1530 else
1531 TRACE("%p moving from (%d,%d) to (%d,%d)\n", hwnd, data->window_rect.left,
1532 data->window_rect.top, rect.left, rect.top);
1534 if ((data->window_rect.right - data->window_rect.left == width &&
1535 data->window_rect.bottom - data->window_rect.top == height) ||
1536 (IsRectEmpty(&data->window_rect) && width <= 0 && height <= 0))
1537 flags |= SWP_NOSIZE;
1538 else
1539 TRACE("%p resizing from (%dx%d) to (%dx%d)\n", hwnd, data->window_rect.right - data->window_rect.left,
1540 data->window_rect.bottom - data->window_rect.top, width, height);
1542 release_win_data(data);
1544 if (!(flags & SWP_NOSIZE) || !(flags & SWP_NOMOVE))
1545 SetWindowPos(hwnd, 0, rect.left, rect.top, width, height, flags);
1549 /***********************************************************************
1550 * macdrv_window_got_focus
1552 * Handler for WINDOW_GOT_FOCUS events.
1554 void macdrv_window_got_focus(HWND hwnd, const macdrv_event *event)
1556 LONG style = GetWindowLongW(hwnd, GWL_STYLE);
1558 if (!hwnd) return;
1560 TRACE("win %p/%p serial %lu enabled %d visible %d style %08x focus %p active %p fg %p\n",
1561 hwnd, event->window, event->window_got_focus.serial, IsWindowEnabled(hwnd),
1562 IsWindowVisible(hwnd), style, GetFocus(), GetActiveWindow(), GetForegroundWindow());
1564 if (can_activate_window(hwnd) && !(style & WS_MINIMIZE))
1566 /* simulate a mouse click on the caption to find out
1567 * whether the window wants to be activated */
1568 LRESULT ma = SendMessageW(hwnd, WM_MOUSEACTIVATE,
1569 (WPARAM)GetAncestor(hwnd, GA_ROOT),
1570 MAKELONG(HTCAPTION,WM_LBUTTONDOWN));
1571 if (ma != MA_NOACTIVATEANDEAT && ma != MA_NOACTIVATE)
1573 TRACE("setting foreground window to %p\n", hwnd);
1574 SetForegroundWindow(hwnd);
1575 return;
1579 TRACE("win %p/%p rejecting focus\n", hwnd, event->window);
1580 macdrv_window_rejected_focus(event);
1584 /***********************************************************************
1585 * macdrv_window_lost_focus
1587 * Handler for WINDOW_LOST_FOCUS events.
1589 void macdrv_window_lost_focus(HWND hwnd, const macdrv_event *event)
1591 if (!hwnd) return;
1593 TRACE("win %p/%p fg %p\n", hwnd, event->window, GetForegroundWindow());
1595 if (hwnd == GetForegroundWindow())
1597 SendMessageW(hwnd, WM_CANCELMODE, 0, 0);
1598 if (hwnd == GetForegroundWindow())
1599 SetForegroundWindow(GetDesktopWindow());
1604 /***********************************************************************
1605 * macdrv_app_deactivated
1607 * Handler for APP_DEACTIVATED events.
1609 void macdrv_app_deactivated(void)
1611 if (GetActiveWindow() == GetForegroundWindow())
1613 TRACE("setting fg to desktop\n");
1614 SetForegroundWindow(GetDesktopWindow());
1619 /***********************************************************************
1620 * macdrv_window_did_minimize
1622 * Handler for WINDOW_DID_MINIMIZE events.
1624 void macdrv_window_did_minimize(HWND hwnd)
1626 struct macdrv_win_data *data;
1627 DWORD style;
1629 TRACE("win %p\n", hwnd);
1631 if (!(data = get_win_data(hwnd))) return;
1632 if (data->minimized) goto done;
1634 style = GetWindowLongW(hwnd, GWL_STYLE);
1636 data->minimized = TRUE;
1637 if ((style & WS_MINIMIZEBOX) && !(style & WS_DISABLED))
1639 TRACE("minimizing win %p/%p\n", hwnd, data->cocoa_window);
1640 release_win_data(data);
1641 SendMessageW(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
1642 return;
1644 TRACE("not minimizing win %p/%p style %08x\n", hwnd, data->cocoa_window, style);
1646 done:
1647 release_win_data(data);
1651 /***********************************************************************
1652 * macdrv_window_did_unminimize
1654 * Handler for WINDOW_DID_UNMINIMIZE events.
1656 void macdrv_window_did_unminimize(HWND hwnd)
1658 struct macdrv_win_data *data;
1659 DWORD style;
1661 TRACE("win %p\n", hwnd);
1663 if (!(data = get_win_data(hwnd))) return;
1664 if (!data->minimized) goto done;
1666 style = GetWindowLongW(hwnd, GWL_STYLE);
1668 data->minimized = FALSE;
1669 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1671 TRACE("restoring win %p/%p\n", hwnd, data->cocoa_window);
1672 release_win_data(data);
1673 SendMessageW(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
1674 return;
1677 TRACE("not restoring win %p/%p style %08x\n", hwnd, data->cocoa_window, style);
1679 done:
1680 release_win_data(data);
1684 struct quit_info {
1685 HWND *wins;
1686 UINT capacity;
1687 UINT count;
1688 UINT done;
1689 DWORD flags;
1690 BOOL result;
1691 BOOL replied;
1695 static BOOL CALLBACK get_process_windows(HWND hwnd, LPARAM lp)
1697 struct quit_info *qi = (struct quit_info*)lp;
1698 DWORD pid;
1700 GetWindowThreadProcessId(hwnd, &pid);
1701 if (pid == GetCurrentProcessId())
1703 if (qi->count >= qi->capacity)
1705 UINT new_cap = qi->capacity * 2;
1706 HWND *new_wins = HeapReAlloc(GetProcessHeap(), 0, qi->wins,
1707 new_cap * sizeof(*qi->wins));
1708 if (!new_wins) return FALSE;
1709 qi->wins = new_wins;
1710 qi->capacity = new_cap;
1713 qi->wins[qi->count++] = hwnd;
1716 return TRUE;
1720 static void CALLBACK quit_callback(HWND hwnd, UINT msg, ULONG_PTR data, LRESULT result)
1722 struct quit_info *qi = (struct quit_info*)data;
1724 qi->done++;
1726 if (msg == WM_QUERYENDSESSION)
1728 TRACE("got WM_QUERYENDSESSION result %ld from win %p (%u of %u done)\n", result,
1729 hwnd, qi->done, qi->count);
1731 if (!result && qi->result)
1733 qi->result = FALSE;
1735 /* On the first FALSE from WM_QUERYENDSESSION, we already know the
1736 ultimate reply. Might as well tell Cocoa now. */
1737 if (!qi->replied)
1739 qi->replied = TRUE;
1740 TRACE("giving quit reply %d\n", qi->result);
1741 macdrv_quit_reply(qi->result);
1745 if (qi->done >= qi->count)
1747 UINT i;
1749 qi->done = 0;
1750 for (i = 0; i < qi->count; i++)
1752 TRACE("sending WM_ENDSESSION to win %p result %d flags 0x%08x\n", qi->wins[i],
1753 qi->result, qi->flags);
1754 if (!SendMessageCallbackW(qi->wins[i], WM_ENDSESSION, qi->result, qi->flags,
1755 quit_callback, (ULONG_PTR)qi))
1757 WARN("failed to send WM_ENDSESSION to win %p; error 0x%08x\n",
1758 qi->wins[i], GetLastError());
1759 quit_callback(qi->wins[i], WM_ENDSESSION, (ULONG_PTR)qi, 0);
1764 else /* WM_ENDSESSION */
1766 TRACE("finished WM_ENDSESSION for win %p (%u of %u done)\n", hwnd, qi->done, qi->count);
1768 if (qi->done >= qi->count)
1770 if (!qi->replied)
1772 TRACE("giving quit reply %d\n", qi->result);
1773 macdrv_quit_reply(qi->result);
1776 TRACE("%sterminating process\n", qi->result ? "" : "not ");
1777 if (qi->result)
1778 TerminateProcess(GetCurrentProcess(), 0);
1780 HeapFree(GetProcessHeap(), 0, qi->wins);
1781 HeapFree(GetProcessHeap(), 0, qi);
1787 /***********************************************************************
1788 * macdrv_app_quit_requested
1790 * Handler for APP_QUIT_REQUESTED events.
1792 void macdrv_app_quit_requested(const macdrv_event *event)
1794 struct quit_info *qi;
1795 UINT i;
1797 TRACE("reason %d\n", event->app_quit_requested.reason);
1799 qi = HeapAlloc(GetProcessHeap(), 0, sizeof(*qi));
1800 if (!qi)
1801 goto fail;
1803 qi->capacity = 32;
1804 qi->wins = HeapAlloc(GetProcessHeap(), 0, qi->capacity * sizeof(*qi->wins));
1805 qi->count = qi->done = 0;
1807 if (!qi->wins || !EnumWindows(get_process_windows, (LPARAM)qi))
1808 goto fail;
1810 switch (event->app_quit_requested.reason)
1812 case QUIT_REASON_LOGOUT:
1813 default:
1814 qi->flags = ENDSESSION_LOGOFF;
1815 break;
1816 case QUIT_REASON_RESTART:
1817 case QUIT_REASON_SHUTDOWN:
1818 qi->flags = 0;
1819 break;
1822 qi->result = TRUE;
1823 qi->replied = FALSE;
1825 for (i = 0; i < qi->count; i++)
1827 TRACE("sending WM_QUERYENDSESSION to win %p\n", qi->wins[i]);
1828 if (!SendMessageCallbackW(qi->wins[i], WM_QUERYENDSESSION, 0, qi->flags,
1829 quit_callback, (ULONG_PTR)qi))
1831 WARN("failed to send WM_QUERYENDSESSION to win %p; error 0x%08x; assuming refusal\n",
1832 qi->wins[i], GetLastError());
1833 quit_callback(qi->wins[i], WM_QUERYENDSESSION, (ULONG_PTR)qi, FALSE);
1837 /* quit_callback() will clean up qi */
1838 return;
1840 fail:
1841 WARN("failed to allocate window list\n");
1842 if (qi)
1844 HeapFree(GetProcessHeap(), 0, qi->wins);
1845 HeapFree(GetProcessHeap(), 0, qi);
1847 macdrv_quit_reply(FALSE);