Debian Codegraph @

clutter/win32/clutter-stage-win32.c

raw
1 /* Clutter.
2 * An OpenGL based 'interactive canvas' library.
3 * Authored By Matthew Allum <[email protected]>
4 * Copyright (C) 2006-2007 OpenedHand
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "clutter-backend-win32.h"
27 #include "clutter-stage-win32.h"
28 #include "clutter-win32.h"
29
30 #include "clutter-actor-private.h"
31 #include "clutter-main.h"
32 #include "clutter-feature.h"
33 #include "clutter-event.h"
34 #include "clutter-enum-types.h"
35 #include "clutter-private.h"
36 #include "clutter-debug.h"
37 #include "clutter-stage-private.h"
38
39 #include "cogl/cogl.h"
40
41 #include <windows.h>
42
43 static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface);
44
45 enum
46 {
47 PROP_0,
48
49 PROP_BACKEND,
50 PROP_WRAPPER
51 };
52
53 G_DEFINE_TYPE_WITH_CODE (ClutterStageWin32,
54 clutter_stage_win32,
55 G_TYPE_OBJECT,
56 G_IMPLEMENT_INTERFACE
57 (CLUTTER_TYPE_STAGE_WINDOW,
58 clutter_stage_window_iface_init));
59
60 static void
61 clutter_stage_win32_show (ClutterStageWindow *stage_window,
62 gboolean do_raise)
63 {
64 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
65
66 if (stage_win32->hwnd)
67 {
68 ShowWindow (stage_win32->hwnd, do_raise ? SW_SHOW : SW_SHOWNA);
69
70 if (stage_win32->accept_focus)
71 SetForegroundWindow (stage_win32->hwnd);
72
73 clutter_actor_map (CLUTTER_ACTOR (stage_win32->wrapper));
74 }
75 }
76
77 static void
78 clutter_stage_win32_hide (ClutterStageWindow *stage_window)
79 {
80 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
81
82 if (stage_win32->hwnd)
83 {
84 clutter_actor_unmap (CLUTTER_ACTOR (stage_win32->wrapper));
85 ShowWindow (stage_win32->hwnd, SW_HIDE);
86 }
87 }
88
89 static void
90 clutter_stage_win32_get_geometry (ClutterStageWindow *stage_window,
91 cairo_rectangle_int_t *geometry)
92 {
93 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
94
95 if (_clutter_stage_is_fullscreen (stage_win32->wrapper))
96 {
97 geometry->width = stage_win32->fullscreen_rect.right
98 - stage_win32->fullscreen_rect.left;
99 geometry->height = stage_win32->fullscreen_rect.bottom
100 - stage_win32->fullscreen_rect.top;
101 return;
102 }
103
104 geometry->width = stage_win32->win_width;
105 geometry->height = stage_win32->win_height;
106 }
107
108 static void
109 get_fullscreen_rect (ClutterStageWin32 *stage_win32)
110 {
111 HMONITOR monitor;
112 MONITORINFO monitor_info;
113
114 /* If we already have a window then try to use the same monitor that
115 is already on */
116 if (stage_win32->hwnd)
117 monitor = MonitorFromWindow (stage_win32->hwnd, MONITOR_DEFAULTTONEAREST);
118 else
119 {
120 /* Otherwise just guess that they will want the monitor where
121 the cursor is */
122 POINT cursor;
123 GetCursorPos (&cursor);
124 monitor = MonitorFromPoint (cursor, MONITOR_DEFAULTTONEAREST);
125 }
126
127 monitor_info.cbSize = sizeof (monitor_info);
128 GetMonitorInfoW (monitor, &monitor_info);
129 stage_win32->fullscreen_rect = monitor_info.rcMonitor;
130 }
131
132 static void
133 get_full_window_size (ClutterStageWin32 *stage_win32,
134 int width_in, int height_in,
135 int *width_out, int *height_out)
136 {
137 gboolean resizable
138 = clutter_stage_get_user_resizable (stage_win32->wrapper);
139 /* The window size passed to CreateWindow includes the window
140 decorations */
141 gint frame_width, frame_height;
142
143 #if !defined (_MSC_VER) || (_MSC_VER < 1700)
144 frame_width = GetSystemMetrics (resizable ? SM_CXSIZEFRAME : SM_CXFIXEDFRAME);
145 frame_height = GetSystemMetrics (resizable ? SM_CYSIZEFRAME : SM_CYFIXEDFRAME);
146 #else
147 /* MSVC 2012 and later returns wrong values from GetSystemMetrics()
148 * http://connect.microsoft.com/VisualStudio/feedback/details/753224/regression-getsystemmetrics-delivers-different-values
149 *
150 * For AdjustWindowRectEx(), it doesn't matter much whether the Window is resizble.
151 */
152
153 RECT cxrect = {0, 0, 0, 0};
154 AdjustWindowRectEx (&cxrect, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_THICKFRAME | WS_DLGFRAME, FALSE, 0);
155
156 frame_width = abs (cxrect.bottom);
157 frame_height = abs (cxrect.left);
158 #endif
159 *width_out = width_in + frame_width * 2;
160 *height_out = height_in + frame_height * 2 + GetSystemMetrics (SM_CYCAPTION);
161 }
162
163 void
164 _clutter_stage_win32_get_min_max_info (ClutterStageWin32 *stage_win32,
165 MINMAXINFO *min_max_info)
166 {
167 /* If the window isn't resizable then set the max and min size to
168 the current size */
169 if (!clutter_stage_get_user_resizable (CLUTTER_STAGE (stage_win32->wrapper)))
170 {
171 int full_width, full_height;
172 get_full_window_size (stage_win32,
173 stage_win32->win_width, stage_win32->win_height,
174 &full_width, &full_height);
175 min_max_info->ptMaxTrackSize.x = full_width;
176 min_max_info->ptMinTrackSize.x = full_width;
177 min_max_info->ptMaxTrackSize.y = full_height;
178 min_max_info->ptMinTrackSize.y = full_height;
179 }
180 }
181
182 static void
183 clutter_stage_win32_resize (ClutterStageWindow *stage_window,
184 gint width,
185 gint height)
186 {
187 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
188 gboolean resize;
189
190 resize = clutter_stage_get_user_resizable (stage_win32->wrapper);
191
192 if (width != stage_win32->win_width || height != stage_win32->win_height)
193 {
194 /* Ignore size requests if we are in full screen mode */
195 if (!_clutter_stage_is_fullscreen (stage_win32->wrapper))
196 {
197 stage_win32->win_width = width;
198 stage_win32->win_height = height;
199
200 if (stage_win32->hwnd != NULL && !stage_win32->is_foreign_win)
201 {
202 int full_width, full_height;
203
204 get_full_window_size (stage_win32,
205 width, height,
206 &full_width, &full_height);
207
208 SetWindowPos (stage_win32->hwnd, NULL,
209 0, 0,
210 full_width, full_height,
211 SWP_NOZORDER | SWP_NOMOVE);
212 }
213 }
214 }
215 }
216
217 static void
218 clutter_stage_win32_set_title (ClutterStageWindow *stage_window,
219 const gchar *title)
220 {
221 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
222
223 /* Empty window titles not allowed, so set it to just a period. */
224 if (title == NULL || !title[0])
225 title = ".";
226
227 if (stage_win32->wtitle != NULL)
228 g_free (stage_win32->wtitle);
229 stage_win32->wtitle = g_utf8_to_utf16 (title, -1, NULL, NULL, NULL);
230
231 /* If the window is not yet created, the title will be set during the
232 window creation */
233 if (stage_win32->hwnd != NULL)
234 SetWindowTextW (stage_win32->hwnd, stage_win32->wtitle);
235 }
236
237 void
238 _clutter_stage_win32_update_cursor (ClutterStageWin32 *stage_win32)
239 {
240 HCURSOR cursor;
241
242 if (stage_win32->is_cursor_visible)
243 cursor = (HCURSOR) GetClassLongPtrW (stage_win32->hwnd, GCLP_HCURSOR);
244 else
245 {
246 ClutterBackend *backend = clutter_get_default_backend ();
247 /* The documentation implies that we can just use
248 SetCursor(NULL) to get rid of the cursor but apparently this
249 doesn't work very well so instead we create an invisible
250 cursor */
251 cursor = _clutter_backend_win32_get_invisible_cursor (backend);
252 }
253
254 SetCursor (cursor);
255 }
256
257 static void
258 clutter_stage_win32_set_cursor_visible (ClutterStageWindow *stage_window,
259 gboolean cursor_visible)
260 {
261 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
262
263 if (stage_win32->is_cursor_visible != cursor_visible)
264 {
265 POINT cursor_pos;
266 RECT client_rect;
267
268 stage_win32->is_cursor_visible = cursor_visible;
269
270 /* If the cursor is already over the client area of the window
271 then we need to update it immediately */
272 GetCursorPos (&cursor_pos);
273 if (WindowFromPoint (cursor_pos) == stage_win32->hwnd &&
274 ScreenToClient (stage_win32->hwnd, &cursor_pos) &&
275 GetClientRect (stage_win32->hwnd, &client_rect) &&
276 cursor_pos.x >= client_rect.left &&
277 cursor_pos.y >= client_rect.top &&
278 cursor_pos.x < client_rect.right &&
279 cursor_pos.y < client_rect.bottom)
280 _clutter_stage_win32_update_cursor (stage_win32);
281 }
282 }
283
284 static LONG
285 get_requested_window_style (ClutterStageWin32 *stage_win32,
286 gboolean want_fullscreen)
287 {
288 ClutterStage *wrapper = stage_win32->wrapper;
289
290 /* Fullscreen mode shouldn't have any borders */
291 if (want_fullscreen)
292 return WS_POPUP;
293 /* Otherwise it's an overlapped window but if it isn't resizable
294 then it shouldn't have a thick frame */
295 else if (clutter_stage_get_user_resizable (wrapper))
296 return WS_OVERLAPPEDWINDOW;
297 else
298 return WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX;
299 }
300
301 static LONG
302 get_window_style (ClutterStageWin32 *stage_win32)
303 {
304 ClutterStage *wrapper = stage_win32->wrapper;
305
306 return get_requested_window_style (stage_win32,
307 _clutter_stage_is_fullscreen (wrapper));
308 }
309
310 static void
311 clutter_stage_win32_set_user_resize (ClutterStageWindow *stage_window,
312 gboolean value)
313 {
314 HWND hwnd = CLUTTER_STAGE_WIN32 (stage_window)->hwnd;
315 LONG old_style = GetWindowLongW (hwnd, GWL_STYLE);
316
317 /* Update the window style but preserve the visibility */
318 SetWindowLongW (hwnd, GWL_STYLE,
319 get_window_style (CLUTTER_STAGE_WIN32 (stage_window))
320 | (old_style & WS_VISIBLE));
321 /* Queue a redraw of the frame */
322 RedrawWindow (hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
323 }
324
325 static void
326 clutter_stage_win32_set_accept_focus (ClutterStageWindow *stage_window,
327 gboolean accept_focus)
328 {
329 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
330
331 accept_focus = !!accept_focus;
332
333 stage_win32->accept_focus = accept_focus;
334 }
335
336 static ClutterActor *
337 clutter_stage_win32_get_wrapper (ClutterStageWindow *stage_window)
338 {
339 return CLUTTER_ACTOR (CLUTTER_STAGE_WIN32 (stage_window)->wrapper);
340 }
341
342 static void
343 clutter_stage_win32_set_fullscreen (ClutterStageWindow *stage_window,
344 gboolean value)
345 {
346 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
347 HWND hwnd = CLUTTER_STAGE_WIN32 (stage_window)->hwnd;
348 LONG old_style = GetWindowLongW (hwnd, GWL_STYLE);
349 ClutterStageStateEvent event;
350
351 if (hwnd)
352 {
353 /* Update the window style but preserve the visibility */
354 SetWindowLongW (hwnd, GWL_STYLE,
355 get_requested_window_style (stage_win32, value)
356 | (old_style & WS_VISIBLE));
357 /* Update the window size */
358 if (value)
359 {
360 get_fullscreen_rect (stage_win32);
361 SetWindowPos (hwnd, HWND_TOP,
362 stage_win32->fullscreen_rect.left,
363 stage_win32->fullscreen_rect.top,
364 stage_win32->fullscreen_rect.right
365 - stage_win32->fullscreen_rect.left,
366 stage_win32->fullscreen_rect.bottom
367 - stage_win32->fullscreen_rect.top,
368 0);
369 }
370 else
371 {
372 int full_width, full_height;
373
374 get_full_window_size (stage_win32,
375 stage_win32->win_width,
376 stage_win32->win_height,
377 &full_width, &full_height);
378
379 SetWindowPos (stage_win32->hwnd, NULL,
380 0, 0,
381 full_width, full_height,
382 SWP_NOZORDER | SWP_NOMOVE);
383 }
384 }
385
386 /* Report the state change */
387 if (value)
388 {
389 _clutter_stage_update_state (stage_win32->wrapper,
390 0,
391 CLUTTER_STAGE_STATE_FULLSCREEN);
392 }
393 else
394 {
395 _clutter_stage_update_state (stage_win32->wrapper,
396 CLUTTER_STAGE_STATE_FULLSCREEN,
397 0);
398 }
399 }
400
401 static ATOM
402 clutter_stage_win32_get_window_class ()
403 {
404 static ATOM klass = 0;
405
406 if (klass == 0)
407 {
408 WNDCLASSW wndclass;
409 memset (&wndclass, 0, sizeof (wndclass));
410 wndclass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
411 wndclass.lpfnWndProc = _clutter_stage_win32_window_proc;
412 wndclass.cbWndExtra = sizeof (LONG_PTR);
413 wndclass.hInstance = GetModuleHandleW (NULL);
414 wndclass.hIcon = LoadIconW (NULL, (LPWSTR) IDI_APPLICATION);
415 wndclass.hCursor = LoadCursorW (NULL, (LPWSTR) IDC_ARROW);
416 wndclass.hbrBackground = NULL;
417 wndclass.lpszMenuName = NULL;
418 wndclass.lpszClassName = L"ClutterStageWin32";
419 klass = RegisterClassW (&wndclass);
420 }
421
422 return klass;
423 }
424
425 static gboolean
426 clutter_stage_win32_realize (ClutterStageWindow *stage_window)
427 {
428 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
429 ClutterBackend *backend;
430 ClutterBackendWin32 *backend_win32;
431 CoglFramebuffer *framebuffer;
432 gfloat width;
433 gfloat height;
434 GError *error = NULL;
435
436 CLUTTER_NOTE (MISC, "Realizing main stage");
437
438 backend = CLUTTER_BACKEND (stage_win32->backend);
439 backend_win32 = CLUTTER_BACKEND_WIN32 (backend);
440
441 clutter_actor_get_size (CLUTTER_ACTOR (stage_win32->wrapper),
442 &width, &height);
443
444 stage_win32->onscreen = cogl_onscreen_new (backend->cogl_context,
445 width, height);
446
447 if (stage_win32->hwnd == NULL)
448 {
449 ATOM window_class = clutter_stage_win32_get_window_class ();
450 int win_xpos, win_ypos, win_width, win_height;
451
452 if (window_class == 0)
453 {
454 g_critical ("Unable to register window class");
455 goto fail;
456 }
457
458 /* If we're in fullscreen mode then use the fullscreen rect
459 instead */
460 if (_clutter_stage_is_fullscreen (stage_win32->wrapper))
461 {
462 get_fullscreen_rect (stage_win32);
463 win_xpos = stage_win32->fullscreen_rect.left;
464 win_ypos = stage_win32->fullscreen_rect.top;
465 win_width = stage_win32->fullscreen_rect.right - win_xpos;
466 win_height = stage_win32->fullscreen_rect.bottom - win_ypos;
467 }
468 else
469 {
470 win_xpos = win_ypos = CW_USEDEFAULT;
471
472 get_full_window_size (stage_win32,
473 stage_win32->win_width,
474 stage_win32->win_height,
475 &win_width, &win_height);
476 }
477
478 if (stage_win32->wtitle == NULL)
479 stage_win32->wtitle = g_utf8_to_utf16 (".", -1, NULL, NULL, NULL);
480
481 stage_win32->hwnd = CreateWindowW ((LPWSTR) MAKEINTATOM (window_class),
482 stage_win32->wtitle,
483 get_window_style (stage_win32),
484 win_xpos,
485 win_ypos,
486 win_width,
487 win_height,
488 NULL, NULL,
489 GetModuleHandle (NULL),
490 NULL);
491
492 if (stage_win32->hwnd == NULL)
493 {
494 g_critical ("Unable to create stage window");
495 goto fail;
496 }
497
498 /* Store a pointer to the actor in the extra bytes of the window
499 so we can quickly access it in the window procedure */
500 SetWindowLongPtrW (stage_win32->hwnd, 0, (LONG_PTR) stage_win32);
501 }
502
503 cogl_win32_onscreen_set_foreign_window (stage_win32->onscreen,
504 stage_win32->hwnd);
505
506 cogl_onscreen_set_swap_throttled (stage_win32->onscreen,
507 _clutter_get_sync_to_vblank ());
508
509 framebuffer = COGL_FRAMEBUFFER (stage_win32->onscreen);
510 if (!cogl_framebuffer_allocate (framebuffer, &error))
511 {
512 g_warning ("Failed to allocate stage: %s", error->message);
513 g_error_free (error);
514 cogl_object_unref (stage_win32->onscreen);
515 stage_win32->onscreen = NULL;
516 goto fail;
517 }
518
519 /* Create a context. This will be a no-op if we already have one */
520 if (!_clutter_backend_create_context (CLUTTER_BACKEND (backend_win32),
521 &error))
522 {
523 g_critical ("Unable to realize stage: %s", error->message);
524 g_error_free (error);
525 goto fail;
526 }
527
528 CLUTTER_NOTE (BACKEND, "Successfully realized stage");
529
530 return TRUE;
531
532 fail:
533 return FALSE;
534 }
535
536 static void
537 clutter_stage_win32_unprepare_window (ClutterStageWin32 *stage_win32)
538 {
539 if (!stage_win32->is_foreign_win && stage_win32->hwnd)
540 {
541 /* Drop the pointer to this stage in the window so that any
542 further messages won't be processed. The stage might be being
543 destroyed so otherwise the messages would be handled with an
544 invalid stage instance */
545 SetWindowLongPtrW (stage_win32->hwnd, 0, (LONG_PTR) 0);
546 DestroyWindow (stage_win32->hwnd);
547 }
548 }
549
550 static void
551 clutter_stage_win32_unrealize (ClutterStageWindow *stage_window)
552 {
553 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
554
555 CLUTTER_NOTE (BACKEND, "Unrealizing stage");
556
557 clutter_stage_win32_unprepare_window (stage_win32);
558 }
559
560 static void
561 clutter_stage_win32_redraw (ClutterStageWindow *stage_window)
562 {
563 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
564
565 /* this will cause the stage implementation to be painted */
566 _clutter_stage_do_paint (stage_win32->wrapper, NULL);
567 cogl_flush ();
568
569 if (stage_win32->onscreen)
570 cogl_onscreen_swap_buffers (COGL_FRAMEBUFFER (stage_win32->onscreen));
571 }
572
573 static CoglFramebuffer *
574 clutter_stage_win32_get_active_framebuffer (ClutterStageWindow *stage_window)
575 {
576 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window);
577
578 return COGL_FRAMEBUFFER (stage_win32->onscreen);
579 }
580
581 static void
582 clutter_stage_win32_set_property (GObject *gobject,
583 guint prop_id,
584 const GValue *value,
585 GParamSpec *pspec)
586 {
587 ClutterStageWin32 *self = CLUTTER_STAGE_WIN32 (gobject);
588
589 switch (prop_id)
590 {
591 case PROP_BACKEND:
592 self->backend = g_value_get_object (value);
593 break;
594
595 case PROP_WRAPPER:
596 self->wrapper = g_value_get_object (value);
597 break;
598
599 default:
600 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
601 }
602 }
603
604 static void
605 clutter_stage_win32_dispose (GObject *gobject)
606 {
607 ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (gobject);
608
609 /* Make sure that context and window are destroyed in case unrealize
610 * hasn't been called yet.
611 */
612 if (stage_win32->hwnd)
613 clutter_stage_win32_unprepare_window (stage_win32);
614
615 if (stage_win32->wtitle)
616 {
617 g_free (stage_win32->wtitle);
618 stage_win32->wtitle = NULL;
619 }
620
621 G_OBJECT_CLASS (clutter_stage_win32_parent_class)->dispose (gobject);
622 }
623
624 static void
625 clutter_stage_win32_class_init (ClutterStageWin32Class *klass)
626 {
627 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
628
629 gobject_class->set_property = clutter_stage_win32_set_property;
630 gobject_class->dispose = clutter_stage_win32_dispose;
631
632 g_object_class_override_property (gobject_class, PROP_BACKEND, "backend");
633 g_object_class_override_property (gobject_class, PROP_WRAPPER, "wrapper");
634 }
635
636 static void
637 clutter_stage_win32_init (ClutterStageWin32 *stage)
638 {
639 stage->hwnd = NULL;
640 stage->win_width = 640;
641 stage->win_height = 480;
642 stage->backend = NULL;
643 stage->scroll_pos = 0;
644 stage->wtitle = NULL;
645 stage->wrapper = NULL;
646
647 stage->is_foreign_win = FALSE;
648 stage->is_cursor_visible = TRUE;
649 stage->accept_focus = TRUE;
650 }
651
652 static void
653 clutter_stage_window_iface_init (ClutterStageWindowIface *iface)
654 {
655 iface->get_wrapper = clutter_stage_win32_get_wrapper;
656 iface->set_title = clutter_stage_win32_set_title;
657 iface->set_fullscreen = clutter_stage_win32_set_fullscreen;
658 iface->set_cursor_visible = clutter_stage_win32_set_cursor_visible;
659 iface->set_user_resizable = clutter_stage_win32_set_user_resize;
660 iface->set_accept_focus = clutter_stage_win32_set_accept_focus;
661 iface->show = clutter_stage_win32_show;
662 iface->hide = clutter_stage_win32_hide;
663 iface->resize = clutter_stage_win32_resize;
664 iface->get_geometry = clutter_stage_win32_get_geometry;
665 iface->realize = clutter_stage_win32_realize;
666 iface->unrealize = clutter_stage_win32_unrealize;
667 iface->redraw = clutter_stage_win32_redraw;
668 iface->get_active_framebuffer = clutter_stage_win32_get_active_framebuffer;
669 }
670
671 /**
672 * clutter_win32_get_stage_window:
673 * @stage: a #ClutterStage
674 *
675 * Gets the stage's window handle
676 *
677 * Return value: An HWND for the stage window.
678 *
679 * Since: 0.8
680 */
681 HWND
682 clutter_win32_get_stage_window (ClutterStage *stage)
683 {
684 ClutterStageWindow *impl;
685
686 g_return_val_if_fail (CLUTTER_IS_STAGE (stage), NULL);
687
688 impl = _clutter_stage_get_window (stage);
689
690 g_return_val_if_fail (CLUTTER_IS_STAGE_WIN32 (impl), NULL);
691
692 return CLUTTER_STAGE_WIN32 (impl)->hwnd;
693 }
694
695 /**
696 * clutter_win32_get_stage_from_window:
697 * @hwnd: a window handle
698 *
699 * Gets the stage for a particular window.
700 *
701 * Return value: The stage or NULL if a stage does not exist for the
702 * window.
703 *
704 * Since: 0.8
705 */
706 ClutterStage *
707 clutter_win32_get_stage_from_window (HWND hwnd)
708 {
709 /* Check whether the window handle is an instance of the stage
710 window class */
711 if ((ATOM) GetClassLongPtrW (hwnd, GCW_ATOM)
712 == clutter_stage_win32_get_window_class ())
713 /* If it is there should be a pointer to the stage in the window
714 extra data */
715 return CLUTTER_STAGE_WIN32 (GetWindowLongPtrW (hwnd, 0))->wrapper;
716 else
717 {
718 /* Otherwise it might be a foreign window so we should check the
719 stage list */
720 ClutterStageManager *stage_manager;
721 const GSList *stages, *l;
722
723 stage_manager = clutter_stage_manager_get_default ();
724 stages = clutter_stage_manager_peek_stages (stage_manager);
725
726 for (l = stages; l != NULL; l = l->next)
727 {
728 ClutterStage *stage = l->data;
729 ClutterStageWindow *impl;
730
731 impl = _clutter_stage_get_window (stage);
732 g_assert (CLUTTER_IS_STAGE_WIN32 (impl));
733
734 if (CLUTTER_STAGE_WIN32 (impl)->hwnd == hwnd)
735 return stage;
736 }
737 }
738
739 return NULL;
740 }
741
742 typedef struct {
743 ClutterStageWin32 *stage_win32;
744 cairo_rectangle_int_t geom;
745 HWND hwnd;
746 guint destroy_old_hwnd : 1;
747 } ForeignWindowData;
748
749 static void
750 set_foreign_window_callback (ClutterActor *actor,
751 void *data)
752 {
753 ForeignWindowData *fwd = data;
754
755 CLUTTER_NOTE (BACKEND, "Setting foreign window (0x%x)",
756 (guint) fwd->hwnd);
757
758 if (fwd->destroy_old_hwnd && fwd->stage_win32->hwnd != NULL)
759 {
760 CLUTTER_NOTE (BACKEND, "Destroying previous window (0x%x)",
761 (guint) fwd->stage_win32->hwnd);
762 DestroyWindow (fwd->stage_win32->hwnd);
763 }
764
765 fwd->stage_win32->hwnd = fwd->hwnd;
766 fwd->stage_win32->is_foreign_win = TRUE;
767
768 fwd->stage_win32->win_width = fwd->geom.width;
769 fwd->stage_win32->win_height = fwd->geom.height;
770
771 clutter_actor_set_size (actor, fwd->geom.width, fwd->geom.height);
772
773 /* calling this with the stage unrealized will unset the stage
774 * from the GL context; once the stage is realized the GL context
775 * will be set again
776 */
777 clutter_stage_ensure_current (CLUTTER_STAGE (actor));
778 }
779
780 /**
781 * clutter_win32_set_stage_foreign:
782 * @stage: a #ClutterStage
783 * @hwnd: an existing window handle
784 *
785 * Target the #ClutterStage to use an existing external window handle.
786 *
787 * Return value: %TRUE if foreign window is valid
788 *
789 * Since: 0.8
790 */
791 gboolean
792 clutter_win32_set_stage_foreign (ClutterStage *stage,
793 HWND hwnd)
794 {
795 ClutterStageWin32 *stage_win32;
796 ClutterStageWindow *impl;
797 ClutterActor *actor;
798 RECT client_rect;
799 ForeignWindowData fwd;
800
801 g_return_val_if_fail (CLUTTER_IS_STAGE (stage), FALSE);
802 g_return_val_if_fail (hwnd != NULL, FALSE);
803
804 impl = _clutter_stage_get_window (stage);
805 if (!CLUTTER_IS_STAGE_WIN32 (impl))
806 {
807 g_critical ("The Clutter backend is not a Windows backend");
808 return FALSE;
809 }
810
811 stage_win32 = CLUTTER_STAGE_WIN32 (impl);
812
813 if (!GetClientRect (hwnd, &client_rect))
814 {
815 g_warning ("Unable to retrieve the new window geometry");
816 return FALSE;
817 }
818
819 fwd.stage_win32 = stage_win32;
820 fwd.hwnd = hwnd;
821
822 /* destroy the old HWND, if we have one and it's ours */
823 if (stage_win32->hwnd != NULL && !stage_win32->is_foreign_win)
824 fwd.destroy_old_hwnd = TRUE;
825 else
826 fwd.destroy_old_hwnd = FALSE;
827
828 fwd.geom.x = 0;
829 fwd.geom.y = 0;
830 fwd.geom.width = client_rect.right - client_rect.left;
831 fwd.geom.height = client_rect.bottom - client_rect.top;
832
833 actor = CLUTTER_ACTOR (stage);
834
835 _clutter_actor_rerealize (actor,
836 set_foreign_window_callback,
837 &fwd);
838
839 /* Queue a relayout - so the stage will be allocated the new
840 * window size.
841 *
842 * Note also that when the stage gets allocated the new
843 * window size that will result in the stage's
844 * priv->viewport being changed, which will in turn result
845 * in the Cogl viewport changing when _clutter_do_redraw
846 * calls _clutter_stage_maybe_setup_viewport().
847 */
848 clutter_actor_queue_relayout (CLUTTER_ACTOR (stage));
849
850 return TRUE;
851 }
852
853 void
854 clutter_stage_win32_map (ClutterStageWin32 *stage_win32)
855 {
856 clutter_actor_map (CLUTTER_ACTOR (stage_win32->wrapper));
857
858 clutter_actor_queue_relayout (CLUTTER_ACTOR (stage_win32->wrapper));
859 }
860
861 void
862 clutter_stage_win32_unmap (ClutterStageWin32 *stage_win32)
863 {
864 clutter_actor_unmap (CLUTTER_ACTOR (stage_win32->wrapper));
865 }