winemac: Send WM_ENTER/EXITSIZEMOVE messages at start/end of a resizing operation.
[wine/multimedia.git] / dlls / winemac.drv / window.c
blob4cb80829d35d92aa0d890bd7367cfbf7ec5fc89d
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 static 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 IsRectEmpty(&data->window_rect) ||
116 (!(ex_style & WS_EX_APPWINDOW) &&
117 (GetWindow(data->hwnd, GW_OWNER) || (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))));
118 state->minimized = (style & WS_MINIMIZE) != 0;
122 /***********************************************************************
123 * get_mac_rect_offset
125 * Helper for macdrv_window_to_mac_rect and macdrv_mac_to_window_rect.
127 static void get_mac_rect_offset(struct macdrv_win_data *data, DWORD style, RECT *rect)
129 DWORD ex_style, style_mask = 0, ex_style_mask = 0;
131 rect->top = rect->bottom = rect->left = rect->right = 0;
133 ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
135 if (!data->shaped)
137 struct macdrv_window_features wf;
138 get_cocoa_window_features(data, style, ex_style, &wf);
140 if (wf.title_bar) style_mask |= WS_CAPTION;
141 if (wf.shadow)
143 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
144 ex_style_mask |= WS_EX_DLGMODALFRAME;
148 AdjustWindowRectEx(rect, style & style_mask, FALSE, ex_style & ex_style_mask);
150 TRACE("%p/%p style %08x ex_style %08x shaped %d -> %s\n", data->hwnd, data->cocoa_window,
151 style, ex_style, data->shaped, wine_dbgstr_rect(rect));
155 /***********************************************************************
156 * macdrv_window_to_mac_rect
158 * Convert a rect from client to Mac window coordinates
160 static void macdrv_window_to_mac_rect(struct macdrv_win_data *data, DWORD style, RECT *rect)
162 RECT rc;
164 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return;
165 if (IsRectEmpty(rect)) return;
167 get_mac_rect_offset(data, style, &rc);
169 rect->left -= rc.left;
170 rect->right -= rc.right;
171 rect->top -= rc.top;
172 rect->bottom -= rc.bottom;
173 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
174 if (rect->left >= rect->right) rect->right = rect->left + 1;
178 /***********************************************************************
179 * macdrv_mac_to_window_rect
181 * Opposite of macdrv_window_to_mac_rect
183 static void macdrv_mac_to_window_rect(struct macdrv_win_data *data, RECT *rect)
185 RECT rc;
186 DWORD style = GetWindowLongW(data->hwnd, GWL_STYLE);
188 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return;
189 if (IsRectEmpty(rect)) return;
191 get_mac_rect_offset(data, style, &rc);
193 rect->left += rc.left;
194 rect->right += rc.right;
195 rect->top += rc.top;
196 rect->bottom += rc.bottom;
197 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
198 if (rect->left >= rect->right) rect->right = rect->left + 1;
202 /***********************************************************************
203 * constrain_window_frame
205 * Alter a window frame rectangle to fit within a) Cocoa's documented
206 * limits, and b) sane sizes, like twice the desktop rect.
208 static void constrain_window_frame(CGRect* frame)
210 CGRect desktop_rect = macdrv_get_desktop_rect();
211 int max_width, max_height;
213 max_width = min(32000, 2 * CGRectGetWidth(desktop_rect));
214 max_height = min(32000, 2 * CGRectGetHeight(desktop_rect));
216 if (frame->origin.x < -16000) frame->origin.x = -16000;
217 if (frame->origin.y < -16000) frame->origin.y = -16000;
218 if (frame->origin.x > 16000) frame->origin.x = 16000;
219 if (frame->origin.y > 16000) frame->origin.y = 16000;
220 if (frame->size.width > max_width) frame->size.width = max_width;
221 if (frame->size.height > max_height) frame->size.height = max_height;
225 /***********************************************************************
226 * alloc_win_data
228 static struct macdrv_win_data *alloc_win_data(HWND hwnd)
230 struct macdrv_win_data *data;
232 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
234 data->hwnd = hwnd;
235 data->color_key = CLR_INVALID;
236 EnterCriticalSection(&win_data_section);
237 if (!win_datas)
238 win_datas = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
239 CFDictionarySetValue(win_datas, hwnd, data);
241 return data;
245 /***********************************************************************
246 * get_win_data
248 * Lock and return the data structure associated with a window.
250 struct macdrv_win_data *get_win_data(HWND hwnd)
252 struct macdrv_win_data *data;
254 if (!hwnd) return NULL;
255 EnterCriticalSection(&win_data_section);
256 if (win_datas && (data = (struct macdrv_win_data*)CFDictionaryGetValue(win_datas, hwnd)))
257 return data;
258 LeaveCriticalSection(&win_data_section);
259 return NULL;
263 /***********************************************************************
264 * release_win_data
266 * Release the data returned by get_win_data.
268 void release_win_data(struct macdrv_win_data *data)
270 if (data) LeaveCriticalSection(&win_data_section);
274 /***********************************************************************
275 * macdrv_get_cocoa_window
277 * Return the Mac window associated with the full area of a window
279 macdrv_window macdrv_get_cocoa_window(HWND hwnd, BOOL require_on_screen)
281 struct macdrv_win_data *data = get_win_data(hwnd);
282 macdrv_window ret = NULL;
283 if (data && (data->on_screen || !require_on_screen))
284 ret = data->cocoa_window;
285 release_win_data(data);
286 return ret;
290 /***********************************************************************
291 * set_cocoa_window_properties
293 * Set the window properties for a Cocoa window based on its Windows
294 * properties.
296 static void set_cocoa_window_properties(struct macdrv_win_data *data)
298 DWORD style, ex_style;
299 HWND owner;
300 macdrv_window owner_win;
301 struct macdrv_window_features wf;
302 struct macdrv_window_state state;
304 style = GetWindowLongW(data->hwnd, GWL_STYLE);
305 ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
307 owner = GetWindow(data->hwnd, GW_OWNER);
308 owner_win = macdrv_get_cocoa_window(owner, TRUE);
309 macdrv_set_cocoa_parent_window(data->cocoa_window, owner_win);
311 get_cocoa_window_features(data, style, ex_style, &wf);
312 macdrv_set_cocoa_window_features(data->cocoa_window, &wf);
314 get_cocoa_window_state(data, style, ex_style, &state);
315 macdrv_set_cocoa_window_state(data->cocoa_window, &state);
316 data->minimized = state.minimized;
320 /***********************************************************************
321 * sync_window_region
323 * Update the window region.
325 static void sync_window_region(struct macdrv_win_data *data, HRGN win_region)
327 HRGN hrgn = win_region;
328 RGNDATA *region_data;
329 const CGRect* rects;
330 int count;
332 if (!data->cocoa_window) return;
333 data->shaped = FALSE;
335 if (IsRectEmpty(&data->window_rect)) /* set an empty shape */
337 TRACE("win %p/%p setting empty shape for zero-sized window\n", data->hwnd, data->cocoa_window);
338 macdrv_set_window_shape(data->cocoa_window, &CGRectZero, 1);
339 return;
342 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
344 if (!(hrgn = CreateRectRgn(0, 0, 0, 0))) return;
345 if (GetWindowRgn(data->hwnd, hrgn) == ERROR)
347 DeleteObject(hrgn);
348 hrgn = 0;
352 if (hrgn && GetWindowLongW(data->hwnd, GWL_EXSTYLE) & WS_EX_LAYOUTRTL)
353 MirrorRgn(data->hwnd, hrgn);
354 if (hrgn)
356 OffsetRgn(hrgn, data->window_rect.left - data->whole_rect.left,
357 data->window_rect.top - data->whole_rect.top);
359 region_data = get_region_data(hrgn, 0);
360 if (region_data)
362 rects = (CGRect*)region_data->Buffer;
363 count = region_data->rdh.nCount;
364 /* Special case optimization. If the region entirely encloses the Cocoa
365 window, it's the same as there being no region. It's potentially
366 hard/slow to test this for arbitrary regions, so we just check for
367 very simple regions. */
368 if (count == 1 && CGRectContainsRect(rects[0], cgrect_from_rect(data->whole_rect)))
370 TRACE("optimizing for simple region that contains Cocoa content rect\n");
371 rects = NULL;
372 count = 0;
375 else
377 rects = NULL;
378 count = 0;
381 TRACE("win %p/%p win_region %p rects %p count %d\n", data->hwnd, data->cocoa_window, win_region, rects, count);
382 macdrv_set_window_shape(data->cocoa_window, rects, count);
384 HeapFree(GetProcessHeap(), 0, region_data);
385 data->shaped = (region_data != NULL);
387 if (hrgn && hrgn != win_region) DeleteObject(hrgn);
391 /***********************************************************************
392 * add_bounds_rect
394 static inline void add_bounds_rect(RECT *bounds, const RECT *rect)
396 if (rect->left >= rect->right || rect->top >= rect->bottom) return;
397 bounds->left = min(bounds->left, rect->left);
398 bounds->top = min(bounds->top, rect->top);
399 bounds->right = max(bounds->right, rect->right);
400 bounds->bottom = max(bounds->bottom, rect->bottom);
404 /***********************************************************************
405 * sync_window_opacity
407 static void sync_window_opacity(struct macdrv_win_data *data, COLORREF key, BYTE alpha,
408 BOOL per_pixel_alpha, DWORD flags)
410 CGFloat opacity = 1.0;
411 BOOL needs_flush = FALSE;
413 if (flags & LWA_ALPHA) opacity = alpha / 255.0;
415 TRACE("setting window %p/%p alpha to %g\n", data->hwnd, data->cocoa_window, opacity);
416 macdrv_set_window_alpha(data->cocoa_window, opacity);
418 if (flags & LWA_COLORKEY)
420 /* FIXME: treat PALETTEINDEX and DIBINDEX as black */
421 if ((key & (1 << 24)) || key >> 16 == 0x10ff)
422 key = RGB(0, 0, 0);
424 else
425 key = CLR_INVALID;
427 if (data->color_key != key)
429 if (key == CLR_INVALID)
431 TRACE("clearing color-key for window %p/%p\n", data->hwnd, data->cocoa_window);
432 macdrv_clear_window_color_key(data->cocoa_window);
434 else
436 TRACE("setting color-key for window %p/%p to RGB %d,%d,%d\n", data->hwnd, data->cocoa_window,
437 GetRValue(key), GetGValue(key), GetBValue(key));
438 macdrv_set_window_color_key(data->cocoa_window, GetRValue(key), GetGValue(key), GetBValue(key));
441 data->color_key = key;
442 needs_flush = TRUE;
445 if (!data->per_pixel_alpha != !per_pixel_alpha)
447 macdrv_window_use_per_pixel_alpha(data->cocoa_window, per_pixel_alpha);
448 data->per_pixel_alpha = per_pixel_alpha;
449 needs_flush = TRUE;
452 if (needs_flush && data->surface)
454 RECT *bounds;
455 RECT rect;
457 rect = data->whole_rect;
458 OffsetRect(&rect, -data->whole_rect.left, -data->whole_rect.top);
459 data->surface->funcs->lock(data->surface);
460 bounds = data->surface->funcs->get_bounds(data->surface);
461 add_bounds_rect(bounds, &rect);
462 data->surface->funcs->unlock(data->surface);
467 /***********************************************************************
468 * sync_window_min_max_info
470 static void sync_window_min_max_info(HWND hwnd)
472 LONG style = GetWindowLongW(hwnd, GWL_STYLE);
473 LONG exstyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
474 RECT win_rect, primary_monitor_rect;
475 MINMAXINFO minmax;
476 LONG adjustedStyle;
477 INT xinc, yinc;
478 WINDOWPLACEMENT wpl;
479 HMONITOR monitor;
480 struct macdrv_win_data *data;
481 RECT min_rect, max_rect;
482 CGSize min_size, max_size;
484 TRACE("win %p\n", hwnd);
486 if (!macdrv_get_cocoa_window(hwnd, FALSE)) return;
488 GetWindowRect(hwnd, &win_rect);
489 minmax.ptReserved.x = win_rect.left;
490 minmax.ptReserved.y = win_rect.top;
492 if ((style & WS_CAPTION) == WS_CAPTION)
493 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
494 else
495 adjustedStyle = style;
497 primary_monitor_rect.left = primary_monitor_rect.top = 0;
498 primary_monitor_rect.right = GetSystemMetrics(SM_CXSCREEN);
499 primary_monitor_rect.bottom = GetSystemMetrics(SM_CYSCREEN);
500 AdjustWindowRectEx(&primary_monitor_rect, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
502 xinc = -primary_monitor_rect.left;
503 yinc = -primary_monitor_rect.top;
505 minmax.ptMaxSize.x = primary_monitor_rect.right - primary_monitor_rect.left;
506 minmax.ptMaxSize.y = primary_monitor_rect.bottom - primary_monitor_rect.top;
507 minmax.ptMaxPosition.x = -xinc;
508 minmax.ptMaxPosition.y = -yinc;
509 if (style & (WS_DLGFRAME | WS_BORDER))
511 minmax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
512 minmax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
514 else
516 minmax.ptMinTrackSize.x = 2 * xinc;
517 minmax.ptMinTrackSize.y = 2 * yinc;
519 minmax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
520 minmax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
522 wpl.length = sizeof(wpl);
523 if (GetWindowPlacement(hwnd, &wpl) && (wpl.ptMaxPosition.x != -1 || wpl.ptMaxPosition.y != -1))
525 minmax.ptMaxPosition = wpl.ptMaxPosition;
527 /* Convert from GetWindowPlacement's workspace coordinates to screen coordinates. */
528 minmax.ptMaxPosition.x -= wpl.rcNormalPosition.left - win_rect.left;
529 minmax.ptMaxPosition.y -= wpl.rcNormalPosition.top - win_rect.top;
532 TRACE("initial ptMaxSize %s ptMaxPosition %s ptMinTrackSize %s ptMaxTrackSize %s\n", wine_dbgstr_point(&minmax.ptMaxSize),
533 wine_dbgstr_point(&minmax.ptMaxPosition), wine_dbgstr_point(&minmax.ptMinTrackSize), wine_dbgstr_point(&minmax.ptMaxTrackSize));
535 SendMessageW(hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&minmax);
537 TRACE("app's ptMaxSize %s ptMaxPosition %s ptMinTrackSize %s ptMaxTrackSize %s\n", wine_dbgstr_point(&minmax.ptMaxSize),
538 wine_dbgstr_point(&minmax.ptMaxPosition), wine_dbgstr_point(&minmax.ptMinTrackSize), wine_dbgstr_point(&minmax.ptMaxTrackSize));
540 /* if the app didn't change the values, adapt them for the window's monitor */
541 if ((monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY)))
543 MONITORINFO mon_info;
544 RECT monitor_rect;
546 mon_info.cbSize = sizeof(mon_info);
547 GetMonitorInfoW(monitor, &mon_info);
549 if ((style & WS_MAXIMIZEBOX) && ((style & WS_CAPTION) == WS_CAPTION || !(style & WS_POPUP)))
550 monitor_rect = mon_info.rcWork;
551 else
552 monitor_rect = mon_info.rcMonitor;
554 if (minmax.ptMaxSize.x == primary_monitor_rect.right - primary_monitor_rect.left &&
555 minmax.ptMaxSize.y == primary_monitor_rect.bottom - primary_monitor_rect.top)
557 minmax.ptMaxSize.x = (monitor_rect.right - monitor_rect.left) + 2 * xinc;
558 minmax.ptMaxSize.y = (monitor_rect.bottom - monitor_rect.top) + 2 * yinc;
560 if (minmax.ptMaxPosition.x == -xinc && minmax.ptMaxPosition.y == -yinc)
562 minmax.ptMaxPosition.x = monitor_rect.left - xinc;
563 minmax.ptMaxPosition.y = monitor_rect.top - yinc;
567 minmax.ptMaxTrackSize.x = max(minmax.ptMaxTrackSize.x, minmax.ptMinTrackSize.x);
568 minmax.ptMaxTrackSize.y = max(minmax.ptMaxTrackSize.y, minmax.ptMinTrackSize.y);
570 TRACE("adjusted ptMaxSize %s ptMaxPosition %s ptMinTrackSize %s ptMaxTrackSize %s\n", wine_dbgstr_point(&minmax.ptMaxSize),
571 wine_dbgstr_point(&minmax.ptMaxPosition), wine_dbgstr_point(&minmax.ptMinTrackSize), wine_dbgstr_point(&minmax.ptMaxTrackSize));
573 if ((data = get_win_data(hwnd)) && data->cocoa_window)
575 SetRect(&min_rect, 0, 0, minmax.ptMinTrackSize.x, minmax.ptMinTrackSize.y);
576 SetRect(&max_rect, 0, 0, minmax.ptMaxTrackSize.x, minmax.ptMaxTrackSize.y);
577 macdrv_window_to_mac_rect(data, style, &min_rect);
578 macdrv_window_to_mac_rect(data, style, &max_rect);
579 min_size = CGSizeMake(min_rect.right - min_rect.left, min_rect.bottom - min_rect.top);
580 max_size = CGSizeMake(max_rect.right - max_rect.left, max_rect.bottom - max_rect.top);
582 TRACE("min_size (%g,%g) max_size (%g,%g)\n", min_size.width, min_size.height, max_size.width, max_size.height);
583 macdrv_set_window_min_max_sizes(data->cocoa_window, min_size, max_size);
586 release_win_data(data);
590 /**********************************************************************
591 * create_cocoa_window
593 * Create the whole Mac window for a given window
595 static void create_cocoa_window(struct macdrv_win_data *data)
597 struct macdrv_thread_data *thread_data = macdrv_init_thread_data();
598 WCHAR text[1024];
599 struct macdrv_window_features wf;
600 CGRect frame;
601 DWORD style, ex_style;
602 HRGN win_rgn;
603 COLORREF key;
604 BYTE alpha;
605 DWORD layered_flags;
607 if ((win_rgn = CreateRectRgn(0, 0, 0, 0)) &&
608 GetWindowRgn(data->hwnd, win_rgn) == ERROR)
610 DeleteObject(win_rgn);
611 win_rgn = 0;
613 data->shaped = (win_rgn != 0);
615 style = GetWindowLongW(data->hwnd, GWL_STYLE);
616 ex_style = GetWindowLongW(data->hwnd, GWL_EXSTYLE);
618 data->whole_rect = data->window_rect;
619 macdrv_window_to_mac_rect(data, style, &data->whole_rect);
621 get_cocoa_window_features(data, style, ex_style, &wf);
623 frame = cgrect_from_rect(data->whole_rect);
624 constrain_window_frame(&frame);
625 if (frame.size.width < 1 || frame.size.height < 1)
626 frame.size.width = frame.size.height = 1;
628 TRACE("creating %p window %s whole %s client %s\n", data->hwnd, wine_dbgstr_rect(&data->window_rect),
629 wine_dbgstr_rect(&data->whole_rect), wine_dbgstr_rect(&data->client_rect));
631 data->cocoa_window = macdrv_create_cocoa_window(&wf, frame, data->hwnd, thread_data->queue);
632 if (!data->cocoa_window) goto done;
634 set_cocoa_window_properties(data);
636 /* set the window text */
637 if (!InternalGetWindowText(data->hwnd, text, sizeof(text)/sizeof(WCHAR))) text[0] = 0;
638 macdrv_set_cocoa_window_title(data->cocoa_window, text, strlenW(text));
640 /* set the window region */
641 if (win_rgn || IsRectEmpty(&data->window_rect)) sync_window_region(data, win_rgn);
643 /* set the window opacity */
644 if (!GetLayeredWindowAttributes(data->hwnd, &key, &alpha, &layered_flags)) layered_flags = 0;
645 sync_window_opacity(data, key, alpha, FALSE, layered_flags);
647 done:
648 if (win_rgn) DeleteObject(win_rgn);
652 /**********************************************************************
653 * destroy_cocoa_window
655 * Destroy the whole Mac window for a given window.
657 static void destroy_cocoa_window(struct macdrv_win_data *data)
659 if (!data->cocoa_window) return;
661 TRACE("win %p Cocoa win %p\n", data->hwnd, data->cocoa_window);
663 macdrv_destroy_cocoa_window(data->cocoa_window);
664 data->cocoa_window = 0;
665 data->on_screen = FALSE;
666 data->color_key = CLR_INVALID;
667 if (data->surface) window_surface_release(data->surface);
668 data->surface = NULL;
669 if (data->unminimized_surface) window_surface_release(data->unminimized_surface);
670 data->unminimized_surface = NULL;
674 /***********************************************************************
675 * macdrv_create_win_data
677 * Create a Mac data window structure for an existing window.
679 static struct macdrv_win_data *macdrv_create_win_data(HWND hwnd, const RECT *window_rect,
680 const RECT *client_rect)
682 struct macdrv_win_data *data;
683 HWND parent;
685 if (GetWindowThreadProcessId(hwnd, NULL) != GetCurrentThreadId()) return NULL;
687 if (!(parent = GetAncestor(hwnd, GA_PARENT))) /* desktop */
689 macdrv_init_thread_data();
690 return NULL;
693 /* don't create win data for HWND_MESSAGE windows */
694 if (parent != GetDesktopWindow() && !GetAncestor(parent, GA_PARENT)) return NULL;
696 if (!(data = alloc_win_data(hwnd))) return NULL;
698 data->whole_rect = data->window_rect = *window_rect;
699 data->client_rect = *client_rect;
701 if (parent == GetDesktopWindow())
703 create_cocoa_window(data);
704 TRACE("win %p/%p window %s whole %s client %s\n",
705 hwnd, data->cocoa_window, wine_dbgstr_rect(&data->window_rect),
706 wine_dbgstr_rect(&data->whole_rect), wine_dbgstr_rect(&data->client_rect));
709 return data;
713 /***********************************************************************
714 * show_window
716 static void show_window(struct macdrv_win_data *data)
718 HWND prev = NULL;
719 HWND next = NULL;
720 macdrv_window prev_window = NULL;
721 macdrv_window next_window = NULL;
722 BOOL activate = FALSE;
724 /* find window that this one must be after */
725 prev = GetWindow(data->hwnd, GW_HWNDPREV);
726 while (prev && !((GetWindowLongW(prev, GWL_STYLE) & (WS_VISIBLE | WS_MINIMIZE)) == WS_VISIBLE &&
727 (prev_window = macdrv_get_cocoa_window(prev, TRUE))))
728 prev = GetWindow(prev, GW_HWNDPREV);
729 if (!prev_window)
731 /* find window that this one must be before */
732 next = GetWindow(data->hwnd, GW_HWNDNEXT);
733 while (next && !((GetWindowLongW(next, GWL_STYLE) & (WS_VISIBLE | WS_MINIMIZE)) == WS_VISIBLE &&
734 (next_window = macdrv_get_cocoa_window(next, TRUE))))
735 next = GetWindow(next, GW_HWNDNEXT);
738 TRACE("win %p/%p below %p/%p above %p/%p\n",
739 data->hwnd, data->cocoa_window, prev, prev_window, next, next_window);
741 if (!prev_window)
742 activate = activate_on_focus_time && (GetTickCount() - activate_on_focus_time < 2000);
743 data->on_screen = macdrv_order_cocoa_window(data->cocoa_window, prev_window, next_window, activate);
744 if (data->on_screen)
746 HWND hwndFocus = GetFocus();
747 if (hwndFocus && (data->hwnd == hwndFocus || IsChild(data->hwnd, hwndFocus)))
748 macdrv_SetFocus(hwndFocus);
749 if (activate)
750 activate_on_focus_time = 0;
755 /***********************************************************************
756 * hide_window
758 static void hide_window(struct macdrv_win_data *data)
760 TRACE("win %p/%p\n", data->hwnd, data->cocoa_window);
762 macdrv_hide_cocoa_window(data->cocoa_window);
763 data->on_screen = FALSE;
767 /***********************************************************************
768 * get_region_data
770 * Calls GetRegionData on the given region and converts the rectangle
771 * array to CGRect format. The returned buffer must be freed by
772 * caller using HeapFree(GetProcessHeap(),...).
773 * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
775 RGNDATA *get_region_data(HRGN hrgn, HDC hdc_lptodp)
777 RGNDATA *data;
778 DWORD size;
779 int i;
780 RECT *rect;
781 CGRect *cgrect;
783 if (!hrgn || !(size = GetRegionData(hrgn, 0, NULL))) return NULL;
784 if (sizeof(CGRect) > sizeof(RECT))
786 /* add extra size for CGRect array */
787 int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
788 size += count * (sizeof(CGRect) - sizeof(RECT));
790 if (!(data = HeapAlloc(GetProcessHeap(), 0, size))) return NULL;
791 if (!GetRegionData(hrgn, size, data))
793 HeapFree(GetProcessHeap(), 0, data);
794 return NULL;
797 rect = (RECT *)data->Buffer;
798 cgrect = (CGRect *)data->Buffer;
799 if (hdc_lptodp) /* map to device coordinates */
801 LPtoDP(hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2);
802 for (i = 0; i < data->rdh.nCount; i++)
804 if (rect[i].right < rect[i].left)
806 INT tmp = rect[i].right;
807 rect[i].right = rect[i].left;
808 rect[i].left = tmp;
810 if (rect[i].bottom < rect[i].top)
812 INT tmp = rect[i].bottom;
813 rect[i].bottom = rect[i].top;
814 rect[i].top = tmp;
819 if (sizeof(CGRect) > sizeof(RECT))
821 /* need to start from the end */
822 for (i = data->rdh.nCount-1; i >= 0; i--)
823 cgrect[i] = cgrect_from_rect(rect[i]);
825 else
827 for (i = 0; i < data->rdh.nCount; i++)
828 cgrect[i] = cgrect_from_rect(rect[i]);
830 return data;
834 /***********************************************************************
835 * sync_window_position
837 * Synchronize the Mac window position with the Windows one
839 static void sync_window_position(struct macdrv_win_data *data, UINT swp_flags, const RECT *old_window_rect)
841 CGRect frame;
843 if (data->minimized) return;
845 frame = cgrect_from_rect(data->whole_rect);
846 constrain_window_frame(&frame);
847 if (frame.size.width < 1 || frame.size.height < 1)
848 frame.size.width = frame.size.height = 1;
850 data->on_screen = macdrv_set_cocoa_window_frame(data->cocoa_window, &frame);
851 if (old_window_rect && IsRectEmpty(old_window_rect) != IsRectEmpty(&data->window_rect))
852 sync_window_region(data, (HRGN)1);
854 TRACE("win %p/%p whole_rect %s frame %s\n", data->hwnd, data->cocoa_window,
855 wine_dbgstr_rect(&data->whole_rect), wine_dbgstr_cgrect(frame));
857 if (data->on_screen && (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW)))
858 show_window(data);
862 /***********************************************************************
863 * move_window_bits
865 * Move the window bits when a window is moved.
867 static void move_window_bits(HWND hwnd, macdrv_window window, const RECT *old_rect, const RECT *new_rect,
868 const RECT *old_client_rect, const RECT *new_client_rect,
869 const RECT *new_window_rect)
871 RECT src_rect = *old_rect;
872 RECT dst_rect = *new_rect;
873 HDC hdc_src, hdc_dst;
874 HRGN rgn;
875 HWND parent = 0;
877 if (!window)
879 OffsetRect(&dst_rect, -new_window_rect->left, -new_window_rect->top);
880 parent = GetAncestor(hwnd, GA_PARENT);
881 hdc_src = GetDCEx(parent, 0, DCX_CACHE);
882 hdc_dst = GetDCEx(hwnd, 0, DCX_CACHE | DCX_WINDOW);
884 else
886 OffsetRect(&dst_rect, -new_client_rect->left, -new_client_rect->top);
887 /* make src rect relative to the old position of the window */
888 OffsetRect(&src_rect, -old_client_rect->left, -old_client_rect->top);
889 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
890 hdc_src = hdc_dst = GetDCEx(hwnd, 0, DCX_CACHE);
893 rgn = CreateRectRgnIndirect(&dst_rect);
894 SelectClipRgn(hdc_dst, rgn);
895 DeleteObject(rgn);
896 ExcludeUpdateRgn(hdc_dst, hwnd);
898 TRACE("copying bits for win %p/%p %s -> %s\n", hwnd, window,
899 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect));
900 BitBlt(hdc_dst, dst_rect.left, dst_rect.top,
901 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
902 hdc_src, src_rect.left, src_rect.top, SRCCOPY);
904 ReleaseDC(hwnd, hdc_dst);
905 if (hdc_src != hdc_dst) ReleaseDC(parent, hdc_src);
909 /**********************************************************************
910 * CreateDesktopWindow (MACDRV.@)
912 BOOL CDECL macdrv_CreateDesktopWindow(HWND hwnd)
914 unsigned int width, height;
916 TRACE("%p\n", hwnd);
918 /* retrieve the real size of the desktop */
919 SERVER_START_REQ(get_window_rectangles)
921 req->handle = wine_server_user_handle(hwnd);
922 req->relative = COORDS_CLIENT;
923 wine_server_call(req);
924 width = reply->window.right;
925 height = reply->window.bottom;
927 SERVER_END_REQ;
929 if (!width && !height) /* not initialized yet */
931 CGRect rect = macdrv_get_desktop_rect();
933 SERVER_START_REQ(set_window_pos)
935 req->handle = wine_server_user_handle(hwnd);
936 req->previous = 0;
937 req->swp_flags = SWP_NOZORDER;
938 req->window.left = CGRectGetMinX(rect);
939 req->window.top = CGRectGetMinY(rect);
940 req->window.right = CGRectGetMaxX(rect);
941 req->window.bottom = CGRectGetMaxY(rect);
942 req->client = req->window;
943 wine_server_call(req);
945 SERVER_END_REQ;
948 return TRUE;
952 /**********************************************************************
953 * CreateWindow (MACDRV.@)
955 BOOL CDECL macdrv_CreateWindow(HWND hwnd)
957 return TRUE;
961 /***********************************************************************
962 * DestroyWindow (MACDRV.@)
964 void CDECL macdrv_DestroyWindow(HWND hwnd)
966 struct macdrv_win_data *data;
968 TRACE("%p\n", hwnd);
970 if (!(data = get_win_data(hwnd))) return;
972 if (data->gl_view) macdrv_dispose_view(data->gl_view);
973 destroy_cocoa_window(data);
975 CFDictionaryRemoveValue(win_datas, hwnd);
976 release_win_data(data);
977 HeapFree(GetProcessHeap(), 0, data);
981 /*****************************************************************
982 * SetFocus (MACDRV.@)
984 * Set the Mac focus.
986 void CDECL macdrv_SetFocus(HWND hwnd)
988 struct macdrv_thread_data *thread_data = macdrv_thread_data();
989 struct macdrv_win_data *data;
991 TRACE("%p\n", hwnd);
993 if (!thread_data) return;
994 thread_data->dead_key_state = 0;
996 if (!(hwnd = GetAncestor(hwnd, GA_ROOT))) return;
997 if (!(data = get_win_data(hwnd))) return;
999 if (data->cocoa_window && data->on_screen)
1001 BOOL activate = activate_on_focus_time && (GetTickCount() - activate_on_focus_time < 2000);
1002 /* Set Mac focus */
1003 macdrv_give_cocoa_window_focus(data->cocoa_window, activate);
1004 activate_on_focus_time = 0;
1007 release_win_data(data);
1011 /***********************************************************************
1012 * SetLayeredWindowAttributes (MACDRV.@)
1014 * Set transparency attributes for a layered window.
1016 void CDECL macdrv_SetLayeredWindowAttributes(HWND hwnd, COLORREF key, BYTE alpha, DWORD flags)
1018 struct macdrv_win_data *data = get_win_data(hwnd);
1020 TRACE("hwnd %p key %#08x alpha %#02x flags %x\n", hwnd, key, alpha, flags);
1022 if (data)
1024 data->layered = TRUE;
1025 if (data->cocoa_window)
1027 sync_window_opacity(data, key, alpha, FALSE, flags);
1028 /* since layered attributes are now set, can now show the window */
1029 if ((GetWindowLongW(hwnd, GWL_STYLE) & WS_VISIBLE) && !data->on_screen)
1030 show_window(data);
1032 release_win_data(data);
1034 else
1035 FIXME("setting layered attributes on window %p of other process not supported\n", hwnd);
1039 /*****************************************************************
1040 * SetParent (MACDRV.@)
1042 void CDECL macdrv_SetParent(HWND hwnd, HWND parent, HWND old_parent)
1044 struct macdrv_win_data *data;
1046 TRACE("%p, %p, %p\n", hwnd, parent, old_parent);
1048 if (parent == old_parent) return;
1049 if (!(data = get_win_data(hwnd))) return;
1051 if (parent != GetDesktopWindow()) /* a child window */
1053 if (old_parent == GetDesktopWindow())
1055 /* destroy the old Mac window */
1056 destroy_cocoa_window(data);
1059 else /* new top level window */
1060 create_cocoa_window(data);
1061 release_win_data(data);
1063 set_gl_view_parent(hwnd, parent);
1067 /***********************************************************************
1068 * SetWindowRgn (MACDRV.@)
1070 * Assign specified region to window (for non-rectangular windows)
1072 int CDECL macdrv_SetWindowRgn(HWND hwnd, HRGN hrgn, BOOL redraw)
1074 struct macdrv_win_data *data;
1076 TRACE("%p, %p, %d\n", hwnd, hrgn, redraw);
1078 if ((data = get_win_data(hwnd)))
1080 sync_window_region(data, hrgn);
1081 release_win_data(data);
1083 else
1085 DWORD procid;
1087 GetWindowThreadProcessId(hwnd, &procid);
1088 if (procid != GetCurrentProcessId())
1089 SendMessageW(hwnd, WM_MACDRV_SET_WIN_REGION, 0, 0);
1092 return TRUE;
1096 /***********************************************************************
1097 * SetWindowStyle (MACDRV.@)
1099 * Update the state of the Cocoa window to reflect a style change
1101 void CDECL macdrv_SetWindowStyle(HWND hwnd, INT offset, STYLESTRUCT *style)
1103 struct macdrv_win_data *data;
1105 TRACE("hwnd %p offset %d styleOld 0x%08x styleNew 0x%08x\n", hwnd, offset, style->styleOld, style->styleNew);
1107 if (hwnd == GetDesktopWindow()) return;
1108 if (!(data = get_win_data(hwnd))) return;
1110 if (data->cocoa_window)
1112 DWORD changed = style->styleNew ^ style->styleOld;
1114 set_cocoa_window_properties(data);
1116 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1118 data->layered = FALSE;
1119 data->ulw_layered = FALSE;
1120 sync_window_opacity(data, 0, 0, FALSE, 0);
1121 if (data->surface) set_surface_use_alpha(data->surface, FALSE);
1125 release_win_data(data);
1129 /*****************************************************************
1130 * SetWindowText (MACDRV.@)
1132 void CDECL macdrv_SetWindowText(HWND hwnd, LPCWSTR text)
1134 macdrv_window win;
1136 TRACE("%p, %s\n", hwnd, debugstr_w(text));
1138 if ((win = macdrv_get_cocoa_window(hwnd, FALSE)))
1139 macdrv_set_cocoa_window_title(win, text, strlenW(text));
1143 /***********************************************************************
1144 * ShowWindow (MACDRV.@)
1146 UINT CDECL macdrv_ShowWindow(HWND hwnd, INT cmd, RECT *rect, UINT swp)
1148 struct macdrv_thread_data *thread_data = macdrv_thread_data();
1149 struct macdrv_win_data *data = get_win_data(hwnd);
1150 CGRect frame;
1152 TRACE("win %p/%p cmd %d at %s flags %08x\n",
1153 hwnd, data ? data->cocoa_window : NULL, cmd, wine_dbgstr_rect(rect), swp);
1155 if (!data || !data->cocoa_window) goto done;
1156 if (IsRectEmpty(rect)) goto done;
1157 if (GetWindowLongW(hwnd, GWL_STYLE) & WS_MINIMIZE)
1159 if (rect->left != -32000 || rect->top != -32000)
1161 OffsetRect(rect, -32000 - rect->left, -32000 - rect->top);
1162 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
1164 goto done;
1166 if (!data->on_screen) goto done;
1168 /* only fetch the new rectangle if the ShowWindow was a result of an external event */
1170 if (!thread_data->current_event || thread_data->current_event->window != data->cocoa_window)
1171 goto done;
1173 if (thread_data->current_event->type != WINDOW_FRAME_CHANGED &&
1174 thread_data->current_event->type != WINDOW_DID_MINIMIZE &&
1175 thread_data->current_event->type != WINDOW_DID_UNMINIMIZE)
1176 goto done;
1178 macdrv_get_cocoa_window_frame(data->cocoa_window, &frame);
1179 *rect = rect_from_cgrect(frame);
1180 macdrv_mac_to_window_rect(data, rect);
1181 TRACE("rect %s -> %s\n", wine_dbgstr_cgrect(frame), wine_dbgstr_rect(rect));
1182 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
1184 done:
1185 release_win_data(data);
1186 return swp;
1190 /***********************************************************************
1191 * SysCommand (MACDRV.@)
1193 * Perform WM_SYSCOMMAND handling.
1195 LRESULT CDECL macdrv_SysCommand(HWND hwnd, WPARAM wparam, LPARAM lparam)
1197 struct macdrv_win_data *data;
1198 LRESULT ret = -1;
1200 TRACE("%p, %x, %lx\n", hwnd, (unsigned)wparam, lparam);
1202 if (!(data = get_win_data(hwnd))) goto done;
1203 if (!data->cocoa_window || !data->on_screen) goto done;
1205 /* prevent a simple ALT press+release from activating the system menu,
1206 as that can get confusing */
1207 if ((wparam & 0xfff0) == SC_KEYMENU && !(WCHAR)lparam && !GetMenu(hwnd) &&
1208 (GetWindowLongW(hwnd, GWL_STYLE) & WS_SYSMENU))
1210 TRACE("ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam);
1211 ret = 0;
1214 done:
1215 release_win_data(data);
1216 return ret;
1220 /***********************************************************************
1221 * UpdateLayeredWindow (MACDRV.@)
1223 BOOL CDECL macdrv_UpdateLayeredWindow(HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
1224 const RECT *window_rect)
1226 struct window_surface *surface;
1227 struct macdrv_win_data *data;
1228 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
1229 BYTE alpha;
1230 char buffer[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1231 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
1232 void *src_bits, *dst_bits;
1233 RECT rect;
1234 HDC hdc = 0;
1235 HBITMAP dib;
1236 BOOL ret = FALSE;
1238 if (!(data = get_win_data(hwnd))) return FALSE;
1240 data->layered = TRUE;
1241 data->ulw_layered = TRUE;
1243 rect = *window_rect;
1244 OffsetRect(&rect, -window_rect->left, -window_rect->top);
1246 surface = data->surface;
1247 if (!surface || memcmp(&surface->rect, &rect, sizeof(RECT)))
1249 data->surface = create_surface(data->cocoa_window, &rect, NULL, TRUE);
1250 set_window_surface(data->cocoa_window, data->surface);
1251 if (surface) window_surface_release(surface);
1252 surface = data->surface;
1253 if (data->unminimized_surface)
1255 window_surface_release(data->unminimized_surface);
1256 data->unminimized_surface = NULL;
1259 else set_surface_use_alpha(surface, TRUE);
1261 if (surface) window_surface_add_ref(surface);
1262 release_win_data(data);
1264 if (!surface) return FALSE;
1265 if (!info->hdcSrc)
1267 window_surface_release(surface);
1268 return TRUE;
1271 if (info->dwFlags & ULW_ALPHA)
1273 /* Apply SourceConstantAlpha via window alpha, not blend. */
1274 alpha = info->pblend->SourceConstantAlpha;
1275 blend = *info->pblend;
1276 blend.SourceConstantAlpha = 0xff;
1278 else
1279 alpha = 0xff;
1281 dst_bits = surface->funcs->get_info(surface, bmi);
1283 if (!(dib = CreateDIBSection(info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0))) goto done;
1284 if (!(hdc = CreateCompatibleDC(0))) goto done;
1286 SelectObject(hdc, dib);
1287 if (info->prcDirty)
1289 IntersectRect(&rect, &rect, info->prcDirty);
1290 surface->funcs->lock(surface);
1291 memcpy(src_bits, dst_bits, bmi->bmiHeader.biSizeImage);
1292 surface->funcs->unlock(surface);
1293 PatBlt(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS);
1295 if (!(ret = GdiAlphaBlend(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
1296 info->hdcSrc,
1297 rect.left + (info->pptSrc ? info->pptSrc->x : 0),
1298 rect.top + (info->pptSrc ? info->pptSrc->y : 0),
1299 rect.right - rect.left, rect.bottom - rect.top,
1300 blend)))
1301 goto done;
1303 if ((data = get_win_data(hwnd)))
1305 if (surface == data->surface)
1307 surface->funcs->lock(surface);
1308 memcpy(dst_bits, src_bits, bmi->bmiHeader.biSizeImage);
1309 add_bounds_rect(surface->funcs->get_bounds(surface), &rect);
1310 surface->funcs->unlock(surface);
1311 surface->funcs->flush(surface);
1314 /* The ULW flags are a superset of the LWA flags. */
1315 sync_window_opacity(data, info->crKey, alpha, TRUE, info->dwFlags);
1317 release_win_data(data);
1320 done:
1321 window_surface_release(surface);
1322 if (hdc) DeleteDC(hdc);
1323 if (dib) DeleteObject(dib);
1324 return ret;
1328 /**********************************************************************
1329 * WindowMessage (MACDRV.@)
1331 LRESULT CDECL macdrv_WindowMessage(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1333 struct macdrv_win_data *data;
1335 TRACE("%p, %u, %u, %lu\n", hwnd, msg, (unsigned)wp, lp);
1337 switch(msg)
1339 case WM_MACDRV_SET_WIN_REGION:
1340 if ((data = get_win_data(hwnd)))
1342 sync_window_region(data, (HRGN)1);
1343 release_win_data(data);
1345 return 0;
1346 case WM_MACDRV_UPDATE_DESKTOP_RECT:
1347 if (hwnd == GetDesktopWindow())
1349 CGRect new_desktop_rect;
1350 RECT current_desktop_rect;
1352 macdrv_reset_device_metrics();
1353 new_desktop_rect = macdrv_get_desktop_rect();
1354 if (!GetWindowRect(hwnd, &current_desktop_rect) ||
1355 !CGRectEqualToRect(cgrect_from_rect(current_desktop_rect), new_desktop_rect))
1357 SendMessageTimeoutW(HWND_BROADCAST, WM_MACDRV_RESET_DEVICE_METRICS, 0, 0,
1358 SMTO_ABORTIFHUNG, 2000, NULL);
1359 SetWindowPos(hwnd, 0, CGRectGetMinX(new_desktop_rect), CGRectGetMinY(new_desktop_rect),
1360 CGRectGetWidth(new_desktop_rect), CGRectGetHeight(new_desktop_rect),
1361 SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE);
1362 SendMessageTimeoutW(HWND_BROADCAST, WM_MACDRV_DISPLAYCHANGE, wp, lp,
1363 SMTO_ABORTIFHUNG, 2000, NULL);
1366 return 0;
1367 case WM_MACDRV_RESET_DEVICE_METRICS:
1368 macdrv_reset_device_metrics();
1369 return 0;
1370 case WM_MACDRV_DISPLAYCHANGE:
1371 if ((data = get_win_data(hwnd)))
1373 if (data->cocoa_window && data->on_screen)
1374 sync_window_position(data, SWP_NOZORDER | SWP_NOACTIVATE, NULL);
1375 release_win_data(data);
1377 SendMessageW(hwnd, WM_DISPLAYCHANGE, wp, lp);
1378 return 0;
1379 case WM_MACDRV_ACTIVATE_ON_FOLLOWING_FOCUS:
1380 activate_on_focus_time = GetTickCount();
1381 if (!activate_on_focus_time) activate_on_focus_time = 1;
1382 TRACE("WM_MACDRV_ACTIVATE_ON_FOLLOWING_FOCUS time %u\n", activate_on_focus_time);
1383 return 0;
1386 FIXME("unrecognized window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp);
1387 return 0;
1391 static inline RECT get_surface_rect(const RECT *visible_rect)
1393 RECT rect;
1394 RECT desktop_rect = rect_from_cgrect(macdrv_get_desktop_rect());
1396 IntersectRect(&rect, visible_rect, &desktop_rect);
1397 OffsetRect(&rect, -visible_rect->left, -visible_rect->top);
1398 rect.left &= ~127;
1399 rect.top &= ~127;
1400 rect.right = max(rect.left + 128, (rect.right + 127) & ~127);
1401 rect.bottom = max(rect.top + 128, (rect.bottom + 127) & ~127);
1402 return rect;
1406 /***********************************************************************
1407 * WindowPosChanging (MACDRV.@)
1409 void CDECL macdrv_WindowPosChanging(HWND hwnd, HWND insert_after, UINT swp_flags,
1410 const RECT *window_rect, const RECT *client_rect,
1411 RECT *visible_rect, struct window_surface **surface)
1413 struct macdrv_win_data *data = get_win_data(hwnd);
1414 DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
1415 RECT surface_rect;
1417 TRACE("%p after %p swp %04x window %s client %s visible %s surface %p\n", hwnd, insert_after,
1418 swp_flags, wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
1419 wine_dbgstr_rect(visible_rect), surface);
1421 if (!data && !(data = macdrv_create_win_data(hwnd, window_rect, client_rect))) return;
1423 *visible_rect = *window_rect;
1424 macdrv_window_to_mac_rect(data, style, visible_rect);
1425 TRACE("visible_rect %s -> %s\n", wine_dbgstr_rect(window_rect),
1426 wine_dbgstr_rect(visible_rect));
1428 /* create the window surface if necessary */
1429 if (!data->cocoa_window) goto done;
1430 if (swp_flags & SWP_HIDEWINDOW) goto done;
1431 if (data->ulw_layered) goto done;
1433 if (*surface) window_surface_release(*surface);
1434 *surface = NULL;
1436 surface_rect = get_surface_rect(visible_rect);
1437 if (data->surface)
1439 if (!memcmp(&data->surface->rect, &surface_rect, sizeof(surface_rect)))
1441 /* existing surface is good enough */
1442 surface_clip_to_visible_rect(data->surface, visible_rect);
1443 window_surface_add_ref(data->surface);
1444 *surface = data->surface;
1445 goto done;
1448 else if (!(swp_flags & SWP_SHOWWINDOW) && !(style & WS_VISIBLE)) goto done;
1450 *surface = create_surface(data->cocoa_window, &surface_rect, data->surface, FALSE);
1452 done:
1453 release_win_data(data);
1457 /***********************************************************************
1458 * WindowPosChanged (MACDRV.@)
1460 void CDECL macdrv_WindowPosChanged(HWND hwnd, HWND insert_after, UINT swp_flags,
1461 const RECT *window_rect, const RECT *client_rect,
1462 const RECT *visible_rect, const RECT *valid_rects,
1463 struct window_surface *surface)
1465 struct macdrv_thread_data *thread_data;
1466 struct macdrv_win_data *data;
1467 DWORD new_style = GetWindowLongW(hwnd, GWL_STYLE);
1468 RECT old_window_rect, old_whole_rect, old_client_rect;
1470 if (!(data = get_win_data(hwnd))) return;
1472 thread_data = macdrv_thread_data();
1474 old_window_rect = data->window_rect;
1475 old_whole_rect = data->whole_rect;
1476 old_client_rect = data->client_rect;
1477 data->window_rect = *window_rect;
1478 data->whole_rect = *visible_rect;
1479 data->client_rect = *client_rect;
1480 if (!data->ulw_layered)
1482 if (surface) window_surface_add_ref(surface);
1483 if (new_style & WS_MINIMIZE)
1485 if (!data->unminimized_surface && data->surface)
1487 data->unminimized_surface = data->surface;
1488 window_surface_add_ref(data->unminimized_surface);
1491 else
1493 set_window_surface(data->cocoa_window, surface);
1494 if (data->unminimized_surface)
1496 window_surface_release(data->unminimized_surface);
1497 data->unminimized_surface = NULL;
1500 if (data->surface) window_surface_release(data->surface);
1501 data->surface = surface;
1504 TRACE("win %p/%p window %s whole %s client %s style %08x flags %08x surface %p\n",
1505 hwnd, data->cocoa_window, wine_dbgstr_rect(window_rect),
1506 wine_dbgstr_rect(visible_rect), wine_dbgstr_rect(client_rect),
1507 new_style, swp_flags, surface);
1509 if (!IsRectEmpty(&valid_rects[0]))
1511 macdrv_window window = data->cocoa_window;
1512 int x_offset = old_whole_rect.left - data->whole_rect.left;
1513 int y_offset = old_whole_rect.top - data->whole_rect.top;
1515 /* if all that happened is that the whole window moved, copy everything */
1516 if (!(swp_flags & SWP_FRAMECHANGED) &&
1517 old_whole_rect.right - data->whole_rect.right == x_offset &&
1518 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
1519 old_client_rect.left - data->client_rect.left == x_offset &&
1520 old_client_rect.right - data->client_rect.right == x_offset &&
1521 old_client_rect.top - data->client_rect.top == y_offset &&
1522 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
1523 !memcmp(&valid_rects[0], &data->client_rect, sizeof(RECT)))
1525 /* A Cocoa window's bits are moved automatically */
1526 if (!window && (x_offset != 0 || y_offset != 0))
1528 release_win_data(data);
1529 move_window_bits(hwnd, window, &old_whole_rect, visible_rect,
1530 &old_client_rect, client_rect, window_rect);
1531 if (!(data = get_win_data(hwnd))) return;
1534 else
1536 release_win_data(data);
1537 move_window_bits(hwnd, window, &valid_rects[1], &valid_rects[0],
1538 &old_client_rect, client_rect, window_rect);
1539 if (!(data = get_win_data(hwnd))) return;
1543 sync_gl_view(data);
1545 if (!data->cocoa_window) goto done;
1547 if (data->on_screen)
1549 if ((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE))
1550 hide_window(data);
1553 /* check if we are currently processing an event relevant to this window */
1554 if (!thread_data || !thread_data->current_event ||
1555 thread_data->current_event->window != data->cocoa_window ||
1556 (thread_data->current_event->type != WINDOW_FRAME_CHANGED &&
1557 thread_data->current_event->type != WINDOW_DID_MINIMIZE &&
1558 thread_data->current_event->type != WINDOW_DID_UNMINIMIZE))
1560 sync_window_position(data, swp_flags, &old_window_rect);
1561 set_cocoa_window_properties(data);
1564 if (new_style & WS_VISIBLE)
1566 if (!data->on_screen || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
1567 set_cocoa_window_properties(data);
1569 /* layered windows are not shown until their attributes are set */
1570 if (!data->on_screen &&
1571 (data->layered || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED)))
1572 show_window(data);
1575 done:
1576 release_win_data(data);
1580 /***********************************************************************
1581 * macdrv_window_close_requested
1583 * Handler for WINDOW_CLOSE_REQUESTED events.
1585 void macdrv_window_close_requested(HWND hwnd)
1587 /* Ignore the delete window request if the window has been disabled. This
1588 * is to disallow applications from being closed while in a modal state.
1590 if (IsWindowEnabled(hwnd))
1592 HMENU hSysMenu;
1594 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return;
1595 hSysMenu = GetSystemMenu(hwnd, FALSE);
1596 if (hSysMenu)
1598 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
1599 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
1600 return;
1602 if (GetActiveWindow() != hwnd)
1604 LRESULT ma = SendMessageW(hwnd, WM_MOUSEACTIVATE,
1605 (WPARAM)GetAncestor(hwnd, GA_ROOT),
1606 MAKELPARAM(HTCLOSE, WM_NCLBUTTONDOWN));
1607 switch(ma)
1609 case MA_NOACTIVATEANDEAT:
1610 case MA_ACTIVATEANDEAT:
1611 return;
1612 case MA_NOACTIVATE:
1613 break;
1614 case MA_ACTIVATE:
1615 case 0:
1616 SetActiveWindow(hwnd);
1617 break;
1618 default:
1619 WARN("unknown WM_MOUSEACTIVATE code %d\n", (int) ma);
1620 break;
1624 PostMessageW(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
1629 /***********************************************************************
1630 * macdrv_window_frame_changed
1632 * Handler for WINDOW_FRAME_CHANGED events.
1634 void macdrv_window_frame_changed(HWND hwnd, CGRect frame)
1636 struct macdrv_win_data *data;
1637 RECT rect;
1638 HWND parent;
1639 UINT flags = SWP_NOACTIVATE | SWP_NOZORDER;
1640 int width, height;
1642 if (!hwnd) return;
1643 if (!(data = get_win_data(hwnd))) return;
1644 if (!data->on_screen || data->minimized)
1646 release_win_data(data);
1647 return;
1650 /* Get geometry */
1652 parent = GetAncestor(hwnd, GA_PARENT);
1654 TRACE("win %p/%p new Cocoa frame %s\n", hwnd, data->cocoa_window, wine_dbgstr_cgrect(frame));
1656 rect = rect_from_cgrect(frame);
1657 macdrv_mac_to_window_rect(data, &rect);
1658 MapWindowPoints(0, parent, (POINT *)&rect, 2);
1660 width = rect.right - rect.left;
1661 height = rect.bottom - rect.top;
1663 if (data->window_rect.left == rect.left && data->window_rect.top == rect.top)
1664 flags |= SWP_NOMOVE;
1665 else
1666 TRACE("%p moving from (%d,%d) to (%d,%d)\n", hwnd, data->window_rect.left,
1667 data->window_rect.top, rect.left, rect.top);
1669 if ((data->window_rect.right - data->window_rect.left == width &&
1670 data->window_rect.bottom - data->window_rect.top == height) ||
1671 (IsRectEmpty(&data->window_rect) && width == 1 && height == 1))
1672 flags |= SWP_NOSIZE;
1673 else
1674 TRACE("%p resizing from (%dx%d) to (%dx%d)\n", hwnd, data->window_rect.right - data->window_rect.left,
1675 data->window_rect.bottom - data->window_rect.top, width, height);
1677 release_win_data(data);
1679 if (!(flags & SWP_NOSIZE) || !(flags & SWP_NOMOVE))
1680 SetWindowPos(hwnd, 0, rect.left, rect.top, width, height, flags);
1684 /***********************************************************************
1685 * macdrv_window_got_focus
1687 * Handler for WINDOW_GOT_FOCUS events.
1689 void macdrv_window_got_focus(HWND hwnd, const macdrv_event *event)
1691 LONG style = GetWindowLongW(hwnd, GWL_STYLE);
1693 if (!hwnd) return;
1695 TRACE("win %p/%p serial %lu enabled %d visible %d style %08x focus %p active %p fg %p\n",
1696 hwnd, event->window, event->window_got_focus.serial, IsWindowEnabled(hwnd),
1697 IsWindowVisible(hwnd), style, GetFocus(), GetActiveWindow(), GetForegroundWindow());
1699 if (can_activate_window(hwnd) && !(style & WS_MINIMIZE))
1701 /* simulate a mouse click on the caption to find out
1702 * whether the window wants to be activated */
1703 LRESULT ma = SendMessageW(hwnd, WM_MOUSEACTIVATE,
1704 (WPARAM)GetAncestor(hwnd, GA_ROOT),
1705 MAKELONG(HTCAPTION,WM_LBUTTONDOWN));
1706 if (ma != MA_NOACTIVATEANDEAT && ma != MA_NOACTIVATE)
1708 TRACE("setting foreground window to %p\n", hwnd);
1709 SetForegroundWindow(hwnd);
1710 return;
1714 TRACE("win %p/%p rejecting focus\n", hwnd, event->window);
1715 macdrv_window_rejected_focus(event);
1719 /***********************************************************************
1720 * macdrv_window_lost_focus
1722 * Handler for WINDOW_LOST_FOCUS events.
1724 void macdrv_window_lost_focus(HWND hwnd, const macdrv_event *event)
1726 if (!hwnd) return;
1728 TRACE("win %p/%p fg %p\n", hwnd, event->window, GetForegroundWindow());
1730 if (hwnd == GetForegroundWindow())
1732 SendMessageW(hwnd, WM_CANCELMODE, 0, 0);
1733 if (hwnd == GetForegroundWindow())
1734 SetForegroundWindow(GetDesktopWindow());
1739 /***********************************************************************
1740 * macdrv_app_deactivated
1742 * Handler for APP_DEACTIVATED events.
1744 void macdrv_app_deactivated(void)
1746 if (GetActiveWindow() == GetForegroundWindow())
1748 TRACE("setting fg to desktop\n");
1749 SetForegroundWindow(GetDesktopWindow());
1754 /***********************************************************************
1755 * macdrv_window_did_minimize
1757 * Handler for WINDOW_DID_MINIMIZE events.
1759 void macdrv_window_did_minimize(HWND hwnd)
1761 struct macdrv_win_data *data;
1762 DWORD style;
1764 TRACE("win %p\n", hwnd);
1766 if (!(data = get_win_data(hwnd))) return;
1767 if (data->minimized) goto done;
1769 style = GetWindowLongW(hwnd, GWL_STYLE);
1771 data->minimized = TRUE;
1772 if ((style & WS_MINIMIZEBOX) && !(style & WS_DISABLED))
1774 TRACE("minimizing win %p/%p\n", hwnd, data->cocoa_window);
1775 release_win_data(data);
1776 SendMessageW(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
1777 return;
1779 TRACE("not minimizing win %p/%p style %08x\n", hwnd, data->cocoa_window, style);
1781 done:
1782 release_win_data(data);
1786 /***********************************************************************
1787 * macdrv_window_did_unminimize
1789 * Handler for WINDOW_DID_UNMINIMIZE events.
1791 void macdrv_window_did_unminimize(HWND hwnd)
1793 struct macdrv_win_data *data;
1794 DWORD style;
1796 TRACE("win %p\n", hwnd);
1798 if (!(data = get_win_data(hwnd))) return;
1799 if (!data->minimized) goto done;
1801 style = GetWindowLongW(hwnd, GWL_STYLE);
1803 data->minimized = FALSE;
1804 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1806 TRACE("restoring win %p/%p\n", hwnd, data->cocoa_window);
1807 release_win_data(data);
1808 SendMessageW(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
1809 return;
1812 TRACE("not restoring win %p/%p style %08x\n", hwnd, data->cocoa_window, style);
1814 done:
1815 release_win_data(data);
1819 struct quit_info {
1820 HWND *wins;
1821 UINT capacity;
1822 UINT count;
1823 UINT done;
1824 DWORD flags;
1825 BOOL result;
1826 BOOL replied;
1830 static BOOL CALLBACK get_process_windows(HWND hwnd, LPARAM lp)
1832 struct quit_info *qi = (struct quit_info*)lp;
1833 DWORD pid;
1835 GetWindowThreadProcessId(hwnd, &pid);
1836 if (pid == GetCurrentProcessId())
1838 if (qi->count >= qi->capacity)
1840 UINT new_cap = qi->capacity * 2;
1841 HWND *new_wins = HeapReAlloc(GetProcessHeap(), 0, qi->wins,
1842 new_cap * sizeof(*qi->wins));
1843 if (!new_wins) return FALSE;
1844 qi->wins = new_wins;
1845 qi->capacity = new_cap;
1848 qi->wins[qi->count++] = hwnd;
1851 return TRUE;
1855 static void CALLBACK quit_callback(HWND hwnd, UINT msg, ULONG_PTR data, LRESULT result)
1857 struct quit_info *qi = (struct quit_info*)data;
1859 qi->done++;
1861 if (msg == WM_QUERYENDSESSION)
1863 TRACE("got WM_QUERYENDSESSION result %ld from win %p (%u of %u done)\n", result,
1864 hwnd, qi->done, qi->count);
1866 if (!result && qi->result)
1868 qi->result = FALSE;
1870 /* On the first FALSE from WM_QUERYENDSESSION, we already know the
1871 ultimate reply. Might as well tell Cocoa now. */
1872 if (!qi->replied)
1874 qi->replied = TRUE;
1875 TRACE("giving quit reply %d\n", qi->result);
1876 macdrv_quit_reply(qi->result);
1880 if (qi->done >= qi->count)
1882 UINT i;
1884 qi->done = 0;
1885 for (i = 0; i < qi->count; i++)
1887 TRACE("sending WM_ENDSESSION to win %p result %d flags 0x%08x\n", qi->wins[i],
1888 qi->result, qi->flags);
1889 if (!SendMessageCallbackW(qi->wins[i], WM_ENDSESSION, qi->result, qi->flags,
1890 quit_callback, (ULONG_PTR)qi))
1892 WARN("failed to send WM_ENDSESSION to win %p; error 0x%08x\n",
1893 qi->wins[i], GetLastError());
1894 quit_callback(qi->wins[i], WM_ENDSESSION, (ULONG_PTR)qi, 0);
1899 else /* WM_ENDSESSION */
1901 TRACE("finished WM_ENDSESSION for win %p (%u of %u done)\n", hwnd, qi->done, qi->count);
1903 if (qi->done >= qi->count)
1905 if (!qi->replied)
1907 TRACE("giving quit reply %d\n", qi->result);
1908 macdrv_quit_reply(qi->result);
1911 TRACE("%sterminating process\n", qi->result ? "" : "not ");
1912 if (qi->result)
1913 TerminateProcess(GetCurrentProcess(), 0);
1915 HeapFree(GetProcessHeap(), 0, qi->wins);
1916 HeapFree(GetProcessHeap(), 0, qi);
1922 /***********************************************************************
1923 * macdrv_app_quit_requested
1925 * Handler for APP_QUIT_REQUESTED events.
1927 void macdrv_app_quit_requested(const macdrv_event *event)
1929 struct quit_info *qi;
1930 UINT i;
1932 TRACE("reason %d\n", event->app_quit_requested.reason);
1934 qi = HeapAlloc(GetProcessHeap(), 0, sizeof(*qi));
1935 if (!qi)
1936 goto fail;
1938 qi->capacity = 32;
1939 qi->wins = HeapAlloc(GetProcessHeap(), 0, qi->capacity * sizeof(*qi->wins));
1940 qi->count = qi->done = 0;
1942 if (!qi->wins || !EnumWindows(get_process_windows, (LPARAM)qi))
1943 goto fail;
1945 switch (event->app_quit_requested.reason)
1947 case QUIT_REASON_LOGOUT:
1948 default:
1949 qi->flags = ENDSESSION_LOGOFF;
1950 break;
1951 case QUIT_REASON_RESTART:
1952 case QUIT_REASON_SHUTDOWN:
1953 qi->flags = 0;
1954 break;
1957 qi->result = TRUE;
1958 qi->replied = FALSE;
1960 for (i = 0; i < qi->count; i++)
1962 TRACE("sending WM_QUERYENDSESSION to win %p\n", qi->wins[i]);
1963 if (!SendMessageCallbackW(qi->wins[i], WM_QUERYENDSESSION, 0, qi->flags,
1964 quit_callback, (ULONG_PTR)qi))
1966 WARN("failed to send WM_QUERYENDSESSION to win %p; error 0x%08x; assuming refusal\n",
1967 qi->wins[i], GetLastError());
1968 quit_callback(qi->wins[i], WM_QUERYENDSESSION, (ULONG_PTR)qi, FALSE);
1972 /* quit_callback() will clean up qi */
1973 return;
1975 fail:
1976 WARN("failed to allocate window list\n");
1977 if (qi)
1979 HeapFree(GetProcessHeap(), 0, qi->wins);
1980 HeapFree(GetProcessHeap(), 0, qi);
1982 macdrv_quit_reply(FALSE);
1986 /***********************************************************************
1987 * query_resize_start
1989 * Handler for QUERY_RESIZE_START query.
1991 BOOL query_resize_start(HWND hwnd)
1993 TRACE("hwnd %p\n", hwnd);
1995 sync_window_min_max_info(hwnd);
1996 SendMessageW(hwnd, WM_ENTERSIZEMOVE, 0, 0);
1998 return TRUE;
2002 /***********************************************************************
2003 * query_resize_end
2005 * Handler for QUERY_RESIZE_END query.
2007 BOOL query_resize_end(HWND hwnd)
2009 TRACE("hwnd %p\n", hwnd);
2010 SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0);
2011 return TRUE;