]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkwindow-win32.c
Adapt calls to g_{locale,filename}_{to,from}_utf8 to match current API.
[~andy/gtk] / gdk / win32 / gdkwindow-win32.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 1998-1999 Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include <stdlib.h>
29
30 #include "gdkevents.h"
31 #include "gdkpixmap.h"
32 #include "gdkwindow.h"
33 #include "gdkinternals.h"
34 #include "gdkprivate-win32.h"
35 #include "gdkinput-win32.h"
36
37 static gboolean gdk_window_gravity_works (void);
38 static void     gdk_window_set_static_win_gravity (GdkWindow *window, 
39                                                    gboolean   on);
40
41 static GdkColormap* gdk_window_impl_win32_get_colormap (GdkDrawable *drawable);
42 static void         gdk_window_impl_win32_set_colormap (GdkDrawable *drawable,
43                                                         GdkColormap *cmap);
44 static void         gdk_window_impl_win32_get_size     (GdkDrawable *drawable,
45                                                         gint *width,
46                                                         gint *height);
47 static GdkRegion*  gdk_window_impl_win32_get_visible_region (GdkDrawable *drawable);
48 static void gdk_window_impl_win32_init       (GdkWindowImplWin32      *window);
49 static void gdk_window_impl_win32_class_init (GdkWindowImplWin32Class *klass);
50 static void gdk_window_impl_win32_finalize   (GObject                 *object);
51
52 static gpointer parent_class = NULL;
53
54 GType
55 gdk_window_impl_win32_get_type (void)
56 {
57   static GType object_type = 0;
58
59   if (!object_type)
60     {
61       static const GTypeInfo object_info =
62       {
63         sizeof (GdkWindowImplWin32Class),
64         (GBaseInitFunc) NULL,
65         (GBaseFinalizeFunc) NULL,
66         (GClassInitFunc) gdk_window_impl_win32_class_init,
67         NULL,           /* class_finalize */
68         NULL,           /* class_data */
69         sizeof (GdkWindowImplWin32),
70         0,              /* n_preallocs */
71         (GInstanceInitFunc) gdk_window_impl_win32_init,
72       };
73       
74       object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_WIN32,
75                                             "GdkWindowImplWin32",
76                                             &object_info, 0);
77     }
78   
79   return object_type;
80 }
81
82 GType
83 _gdk_window_impl_get_type (void)
84 {
85   return gdk_window_impl_win32_get_type ();
86 }
87
88 static void
89 gdk_window_impl_win32_init (GdkWindowImplWin32 *impl)
90 {
91   impl->width = 1;
92   impl->height = 1;
93
94   impl->event_mask = 0;
95   impl->hcursor = NULL;
96   impl->hint_flags = 0;
97   impl->extension_events_selected = FALSE;
98   impl->input_locale = GetKeyboardLayout (0);
99   TranslateCharsetInfo ((DWORD FAR *) GetACP (), &impl->charset_info,
100                         TCI_SRCCODEPAGE);
101 }
102
103 static void
104 gdk_window_impl_win32_class_init (GdkWindowImplWin32Class *klass)
105 {
106   GObjectClass *object_class = G_OBJECT_CLASS (klass);
107   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
108   
109   parent_class = g_type_class_peek_parent (klass);
110
111   object_class->finalize = gdk_window_impl_win32_finalize;
112
113   drawable_class->set_colormap = gdk_window_impl_win32_set_colormap;
114   drawable_class->get_colormap = gdk_window_impl_win32_get_colormap;
115   drawable_class->get_size = gdk_window_impl_win32_get_size;
116
117   /* Visible and clip regions are the same */
118   drawable_class->get_clip_region = gdk_window_impl_win32_get_visible_region;
119   drawable_class->get_visible_region = gdk_window_impl_win32_get_visible_region;
120 }
121
122 static void
123 gdk_window_impl_win32_finalize (GObject *object)
124 {
125   GdkWindowObject *wrapper;
126   GdkDrawableImplWin32 *draw_impl;
127   GdkWindowImplWin32 *window_impl;
128   
129   g_return_if_fail (GDK_IS_WINDOW_IMPL_WIN32 (object));
130
131   draw_impl = GDK_DRAWABLE_IMPL_WIN32 (object);
132   window_impl = GDK_WINDOW_IMPL_WIN32 (object);
133   
134   wrapper = (GdkWindowObject*) draw_impl->wrapper;
135
136   if (!GDK_WINDOW_DESTROYED (wrapper))
137     {
138       gdk_win32_handle_table_remove (draw_impl->handle);
139     }
140
141   if (window_impl->hcursor != NULL)
142     {
143       if (!DestroyCursor (window_impl->hcursor))
144         WIN32_GDI_FAILED("DestroyCursor");
145       window_impl->hcursor = NULL;
146     }
147
148   G_OBJECT_CLASS (parent_class)->finalize (object);
149 }
150
151 static GdkColormap*
152 gdk_window_impl_win32_get_colormap (GdkDrawable *drawable)
153 {
154   GdkDrawableImplWin32 *drawable_impl;
155   GdkWindowImplWin32 *window_impl;
156   
157   g_return_val_if_fail (GDK_IS_WINDOW_IMPL_WIN32 (drawable), NULL);
158
159   drawable_impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
160   window_impl = GDK_WINDOW_IMPL_WIN32 (drawable);
161
162   if (!((GdkWindowObject *) drawable_impl->wrapper)->input_only && 
163       drawable_impl->colormap == NULL)
164     {
165       drawable_impl->colormap = gdk_colormap_get_system ();
166       gdk_colormap_ref (drawable_impl->colormap);
167     }
168   
169   return drawable_impl->colormap;
170 }
171
172 static void
173 gdk_window_impl_win32_set_colormap (GdkDrawable *drawable,
174                                     GdkColormap *cmap)
175 {
176   GdkWindowImplWin32 *impl;
177   GdkDrawableImplWin32 *draw_impl;
178   
179   g_return_if_fail (GDK_IS_WINDOW_IMPL_WIN32 (drawable));
180   g_return_if_fail (gdk_colormap_get_visual (cmap) != gdk_drawable_get_visual (drawable));
181
182   impl = GDK_WINDOW_IMPL_WIN32 (drawable);
183   draw_impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
184
185   GDK_DRAWABLE_GET_CLASS (draw_impl)->set_colormap (drawable, cmap);
186   
187   /* XXX */
188   g_print("gdk_window_impl_win32_set_colormap: XXX\n");
189 }
190
191 static void
192 gdk_window_impl_win32_get_size (GdkDrawable *drawable,
193                                 gint        *width,
194                                 gint        *height)
195 {
196   g_return_if_fail (GDK_IS_WINDOW_IMPL_WIN32 (drawable));
197
198   if (width)
199     *width = GDK_WINDOW_IMPL_WIN32 (drawable)->width;
200   if (height)
201     *height = GDK_WINDOW_IMPL_WIN32 (drawable)->height;
202 }
203
204 static GdkRegion*
205 gdk_window_impl_win32_get_visible_region (GdkDrawable *drawable)
206 {
207   GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (drawable);
208   GdkRectangle result_rect;
209
210   result_rect.x = 0;
211   result_rect.y = 0;
212   result_rect.width = impl->width;
213   result_rect.height = impl->height;
214
215   gdk_rectangle_intersect (&result_rect, &impl->position_info.clip_rect, &result_rect);
216
217   return gdk_region_rectangle (&result_rect);
218 }
219
220 void
221 _gdk_windowing_window_init (void)
222 {
223   GdkWindowObject *private;
224   GdkWindowImplWin32 *impl;
225   GdkDrawableImplWin32 *draw_impl;
226   RECT rect;
227   guint width;
228   guint height;
229
230   g_assert (gdk_parent_root == NULL);
231   
232   SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
233   width  = rect.right - rect.left;
234   height = rect.bottom - rect.top;
235
236   gdk_parent_root = g_object_new (GDK_TYPE_WINDOW, NULL);
237   private = (GdkWindowObject *)gdk_parent_root;
238   impl = GDK_WINDOW_IMPL_WIN32 (private->impl);
239   draw_impl = GDK_DRAWABLE_IMPL_WIN32 (private->impl);
240   
241   draw_impl->handle = gdk_root_window;
242   draw_impl->wrapper = GDK_DRAWABLE (private);
243   
244   private->window_type = GDK_WINDOW_ROOT;
245   private->depth = gdk_visual_get_system ()->depth;
246   impl->width = width;
247   impl->height = height;
248
249   gdk_win32_handle_table_insert (&gdk_root_window, gdk_parent_root);
250 }
251
252 /* The Win API function AdjustWindowRect may return negative values
253  * resulting in obscured title bars. This helper function is coreccting it.
254  */
255 BOOL
256 SafeAdjustWindowRectEx (RECT* lpRect,
257                         DWORD dwStyle, 
258                         BOOL  bMenu, 
259                         DWORD dwExStyle)
260 {
261   if (!AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle))
262     {
263       WIN32_API_FAILED ("AdjustWindowRectEx");
264       return FALSE;
265     }
266   if (lpRect->left < 0)
267     {
268       lpRect->right -= lpRect->left;
269       lpRect->left = 0;
270     }
271   if (lpRect->top < 0)
272     {
273       lpRect->bottom -= lpRect->top;
274       lpRect->top = 0;
275     }
276   return TRUE;
277 }
278
279 /* RegisterGdkClass
280  *   is a wrapper function for RegisterWindowClassEx.
281  *   It creates at least one unique class for every 
282  *   GdkWindowType. If support for single window-specific icons
283  *   is ever needed (e.g Dialog specific), every such window should
284  *   get its own class
285  */
286 ATOM
287 RegisterGdkClass (GdkWindowType wtype)
288 {
289   static ATOM klassTOPLEVEL = 0;
290   static ATOM klassDIALOG   = 0;
291   static ATOM klassCHILD    = 0;
292   static ATOM klassTEMP     = 0;
293   static HICON hAppIcon = NULL;
294   static WNDCLASSEX wcl; 
295   ATOM klass = 0;
296
297   wcl.cbSize = sizeof(WNDCLASSEX);     
298   wcl.style = 0; /* DON'T set CS_<H,V>REDRAW. It causes total redraw
299                   * on WM_SIZE and WM_MOVE. Flicker, Performance!
300                   */
301   wcl.lpfnWndProc = gdk_window_procedure;
302   wcl.cbClsExtra = 0;
303   wcl.cbWndExtra = 0;
304   wcl.hInstance = gdk_app_hmodule;
305   wcl.hIcon = 0;
306   /* initialize once! */
307   if (0 == hAppIcon)
308     {
309       gchar sLoc [_MAX_PATH+1];
310
311       if (0 != GetModuleFileName (gdk_app_hmodule, sLoc, _MAX_PATH))
312         {
313           hAppIcon = ExtractIcon (gdk_app_hmodule, sLoc, 0);
314           if (0 == hAppIcon)
315             {
316               char *gdklibname = g_strdup_printf ("gdk-%s.dll", GDK_VERSION);
317
318               hAppIcon = ExtractIcon (gdk_app_hmodule, gdklibname, 0);
319               g_free (gdklibname);
320             }
321           
322           if (0 == hAppIcon) 
323             hAppIcon = LoadIcon (NULL, IDI_APPLICATION);
324         }
325     }
326
327   wcl.lpszMenuName = NULL;
328   wcl.hIconSm = 0;
329
330   /* initialize once per class */
331 #define ONCE_PER_CLASS() \
332   wcl.hIcon = CopyIcon (hAppIcon); \
333   wcl.hIconSm = CopyIcon (hAppIcon); \
334   wcl.hbrBackground = CreateSolidBrush (RGB (0,0,0)); \
335   wcl.hCursor = LoadCursor (NULL, IDC_ARROW); 
336   
337   switch (wtype)
338   {
339   case GDK_WINDOW_TOPLEVEL:
340     if (0 == klassTOPLEVEL)
341       {
342         wcl.lpszClassName = "gdkWindowToplevel";
343         
344         ONCE_PER_CLASS();
345         klassTOPLEVEL = RegisterClassEx (&wcl);
346       }
347     klass = klassTOPLEVEL;
348     break;
349
350     case GDK_WINDOW_CHILD:
351       if (0 == klassCHILD)
352         {
353           wcl.lpszClassName = "gdkWindowChild";
354           
355           wcl.style |= CS_PARENTDC; /* MSDN: ... enhances system performance. */
356           ONCE_PER_CLASS();
357           klassCHILD = RegisterClassEx (&wcl);
358         }
359       klass = klassCHILD;
360       break;
361
362   case GDK_WINDOW_DIALOG:
363     if (0 == klassDIALOG)
364       {
365         wcl.lpszClassName = "gdkWindowDialog";
366         wcl.style |= CS_SAVEBITS;
367         ONCE_PER_CLASS();
368         klassDIALOG = RegisterClassEx (&wcl);
369       }
370     klass = klassDIALOG;
371     break;
372
373   case GDK_WINDOW_TEMP:
374     if (0 == klassTEMP)
375       {
376         wcl.lpszClassName = "gdkWindowTemp";
377         wcl.style |= CS_SAVEBITS;
378         ONCE_PER_CLASS();
379         klassTEMP = RegisterClassEx (&wcl);
380       }
381     klass = klassTEMP;
382     break;
383
384   default:
385     g_assert_not_reached ();
386     break;
387   }
388
389   if (klass == 0)
390     {
391       WIN32_API_FAILED ("RegisterClassEx");
392       g_error ("That is a fatal error");
393     }
394   return klass;
395 }
396
397 GdkWindow*
398 gdk_window_new (GdkWindow     *parent,
399                 GdkWindowAttr *attributes,
400                 gint           attributes_mask)
401 {
402   GdkWindow *window;
403   GdkWindowObject *private;
404   GdkWindowObject *parent_private;
405   GdkWindowImplWin32 *impl;
406   GdkDrawableImplWin32 *draw_impl;
407
408   GdkVisual *visual;
409
410   HANDLE hparent;
411   ATOM klass = 0;
412   DWORD dwStyle, dwExStyle;
413   RECT rect;
414   Visual *xvisual;
415
416   int width, height;
417   int x, y;
418   char *title;
419   char *mbtitle;
420
421   g_return_val_if_fail (attributes != NULL, NULL);
422
423   if (!parent)
424     parent = gdk_parent_root;
425
426   g_return_val_if_fail (GDK_IS_WINDOW (parent), NULL);
427   
428   GDK_NOTE (MISC,
429             g_print ("gdk_window_new: %s\n",
430                      (attributes->window_type == GDK_WINDOW_TOPLEVEL ? "TOPLEVEL" :
431                       (attributes->window_type == GDK_WINDOW_CHILD ? "CHILD" :
432                        (attributes->window_type == GDK_WINDOW_DIALOG ? "DIALOG" :
433                         (attributes->window_type == GDK_WINDOW_TEMP ? "TEMP" :
434                          "???"))))));
435
436   parent_private = (GdkWindowObject*) parent;
437   if (GDK_WINDOW_DESTROYED (parent))
438     return NULL;
439   
440   hparent = GDK_WINDOW_HWND (parent);
441
442   window = g_object_new (GDK_TYPE_WINDOW, NULL);
443   private = (GdkWindowObject *)window;
444   impl = GDK_WINDOW_IMPL_WIN32 (private->impl);
445   draw_impl = GDK_DRAWABLE_IMPL_WIN32 (private->impl);
446   draw_impl->wrapper = GDK_DRAWABLE (window);
447
448   private->parent = (GdkWindowObject *)parent;
449
450   if (attributes_mask & GDK_WA_X)
451     x = attributes->x;
452   else
453     x = CW_USEDEFAULT;
454   
455   if (attributes_mask & GDK_WA_Y)
456     y = attributes->y;
457   else if (attributes_mask & GDK_WA_X)
458     y = 100;                    /* ??? We must put it somewhere... */
459   else
460     y = 0;                      /* x is CW_USEDEFAULT, y doesn't matter then */
461   
462   private->x = x;
463   private->y = y;
464   impl->width = (attributes->width > 1) ? (attributes->width) : (1);
465   impl->height = (attributes->height > 1) ? (attributes->height) : (1);
466   impl->extension_events_selected = FALSE;
467   private->window_type = attributes->window_type;
468
469   _gdk_window_init_position (GDK_WINDOW (private));
470   if (impl->position_info.big)
471     private->guffaw_gravity = TRUE;
472
473   if (attributes_mask & GDK_WA_VISUAL)
474     visual = attributes->visual;
475   else
476     visual = gdk_visual_get_system ();
477   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
478
479   if (attributes_mask & GDK_WA_TITLE)
480     title = attributes->title;
481   else
482     title = g_get_prgname ();
483   if (!title || !*title)
484     title = "GDK client window";
485
486   impl->event_mask = GDK_STRUCTURE_MASK | attributes->event_mask;
487       
488   if (parent_private && parent_private->guffaw_gravity)
489     {
490       /* XXX ??? */
491     }
492
493   if (attributes->wclass == GDK_INPUT_OUTPUT)
494     {
495       dwExStyle = 0;
496
497       private->input_only = FALSE;
498       private->depth = visual->depth;
499       
500       if (attributes_mask & GDK_WA_COLORMAP)
501         {
502           draw_impl->colormap = attributes->colormap;
503           gdk_colormap_ref (attributes->colormap);
504         }
505       else
506         {
507           if ((((GdkVisualPrivate*)gdk_visual_get_system ())->xvisual) == xvisual)
508             {
509               draw_impl->colormap = gdk_colormap_get_system ();
510               gdk_colormap_ref (draw_impl->colormap);
511               GDK_NOTE (MISC, g_print ("...using system colormap %p\n",
512                                        draw_impl->colormap));
513             }
514           else
515             {
516               draw_impl->colormap = gdk_colormap_new (visual, FALSE);
517               GDK_NOTE (MISC, g_print ("...using new colormap %p\n",
518                                        draw_impl->colormap));
519             }
520         }
521     }
522   else
523     {
524       dwExStyle = WS_EX_TRANSPARENT;
525       private->depth = 0;
526       private->input_only = TRUE;
527       draw_impl->colormap = NULL;
528       GDK_NOTE (MISC, g_print ("...GDK_INPUT_ONLY, NULL colormap\n"));
529     }
530
531   if (parent_private)
532     parent_private->children = g_list_prepend (parent_private->children, window);
533
534   switch (private->window_type)
535     {
536     case GDK_WINDOW_TOPLEVEL:
537       dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
538       hparent = gdk_root_window;
539       break;
540
541     case GDK_WINDOW_CHILD:
542       dwStyle = WS_CHILDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
543       break;
544
545     case GDK_WINDOW_DIALOG:
546       dwStyle = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU | WS_CAPTION | WS_THICKFRAME | WS_CLIPCHILDREN;
547 #if 0
548       dwExStyle |= WS_EX_TOPMOST; /* //HB: want this? */
549 #endif
550       hparent = gdk_root_window;
551       break;
552
553     case GDK_WINDOW_TEMP:
554       dwStyle = WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
555       dwExStyle |= WS_EX_TOOLWINDOW;
556       break;
557
558     case GDK_WINDOW_ROOT:
559       g_error ("cannot make windows of type GDK_WINDOW_ROOT");
560       break;
561     }
562
563   klass = RegisterGdkClass (private->window_type);
564
565   if (private->window_type != GDK_WINDOW_CHILD)
566     {
567       if (x == CW_USEDEFAULT)
568         {
569           rect.left = 100;
570           rect.top = 100;
571         }
572       else
573         {
574           rect.left = x;
575           rect.top = y;
576         }
577
578       rect.right = rect.left + impl->width;
579       rect.bottom = rect.top + impl->height;
580
581       SafeAdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
582
583       if (x != CW_USEDEFAULT)
584         {
585           x = rect.left;
586           y = rect.top;
587         }
588       width = rect.right - rect.left;
589       height = rect.bottom - rect.top;
590     }
591   else
592     {
593       width = impl->width;
594       height = impl->height;
595     }
596
597   mbtitle = g_locale_from_utf8 (title, -1, NULL, NULL, NULL);
598   
599 #ifdef WITHOUT_WM_CREATE
600   draw_impl->handle = CreateWindowEx (dwExStyle,
601                                       MAKEINTRESOURCE(klass),
602                                       mbtitle,
603                                       dwStyle,
604                                       x, y, 
605                                       width, height,
606                                       hparent,
607                                       NULL,
608                                       gdk_app_hmodule,
609                                       NULL);
610 #else
611   {
612   HWND hwndNew =
613     CreateWindowEx (dwExStyle,
614                     MAKEINTRESOURCE(klass),
615                     mbtitle,
616                     dwStyle,
617                     x, y, 
618                     width, height,
619                     hparent,
620                     NULL,
621                     gdk_app_hmodule,
622                     window);
623   if (GDK_WINDOW_HWND (window) != hwndNew)
624     {
625       g_warning("gdk_window_new: gdk_event_translate::WM_CREATE (%#x, %#x) HWND mismatch.",
626                 (guint) GDK_WINDOW_HWND (window),
627                 (guint) hwndNew);
628
629       /* HB: IHMO due to a race condition the handle was increased by
630        * one, which causes much trouble. Because I can't find the 
631        * real bug, try to workaround it ...
632        * To reproduce: compile with MSVC 5, DEBUG=1
633        */
634 # if 0
635       gdk_win32_handle_table_remove (GDK_WINDOW_HWND (window));
636       GDK_WINDOW_HWND (window) = hwndNew;
637       gdk_win32_handle_table_insert (&GDK_WINDOW_HWND (window), window);
638 # else
639       /* the old behaviour, but with warning */
640       GDK_WINDOW_HWND (window) = hwndNew;
641 # endif
642
643     }
644   }
645   gdk_drawable_ref (window);
646   gdk_win32_handle_table_insert (&GDK_WINDOW_HWND (window), window);
647 #endif
648
649   GDK_NOTE (MISC,
650             g_print ("... \"%s\" %dx%d@+%d+%d %#x = %#x\n"
651                      "... locale %#x codepage %d\n",
652                      mbtitle,
653                      width, height, (x == CW_USEDEFAULT ? -9999 : x), y, 
654                      (guint) hparent,
655                      (guint) GDK_WINDOW_HWND (window),
656                      (guint) impl->input_locale,
657                      (guint) impl->charset_info.ciACP));
658
659   g_free (mbtitle);
660
661   if (draw_impl->handle == NULL)
662     {
663       WIN32_API_FAILED ("CreateWindowEx");
664       g_object_unref ((GObject *) window);
665       return NULL;
666     }
667
668 #ifdef WITHOUT_WM_CREATE
669   gdk_drawable_ref (window);
670   gdk_win32_handle_table_insert (&GDK_WINDOW_HWND (window), window);
671 #endif
672
673   gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
674                                   (attributes->cursor) :
675                                   NULL));
676
677   return window;
678 }
679
680 GdkWindow *
681 gdk_window_foreign_new (GdkNativeWindow anid)
682 {
683   GdkWindow *window;
684   GdkWindowObject *private;
685   GdkWindowObject *parent_private;
686   GdkWindowImplWin32 *impl;
687   GdkDrawableImplWin32 *draw_impl;
688
689   HANDLE parent;
690   RECT rect;
691   POINT point;
692
693   window = g_object_new (GDK_TYPE_WINDOW, NULL);
694   private = (GdkWindowObject *)window;
695   impl = GDK_WINDOW_IMPL_WIN32 (private->impl);
696   draw_impl = GDK_DRAWABLE_IMPL_WIN32 (private->impl);
697   draw_impl->wrapper = GDK_DRAWABLE (window);
698   parent = GetParent ((HWND)anid);
699   
700   private->parent = gdk_win32_handle_table_lookup ((GdkNativeWindow) parent);
701   
702   parent_private = (GdkWindowObject *)private->parent;
703   
704   if (parent_private)
705     parent_private->children = g_list_prepend (parent_private->children, window);
706
707   draw_impl->handle = (HWND) anid;
708   GetClientRect ((HWND) anid, &rect);
709   point.x = rect.left;
710   point.y = rect.right;
711   ClientToScreen ((HWND) anid, &point);
712   if (parent != gdk_root_window)
713     ScreenToClient (parent, &point);
714   private->x = point.x;
715   private->y = point.y;
716   impl->width = rect.right - rect.left;
717   impl->height = rect.bottom - rect.top;
718   private->window_type = GDK_WINDOW_FOREIGN;
719   private->destroyed = FALSE;
720   private->mapped = IsWindowVisible ((HWND) anid);
721   private->depth = gdk_visual_get_system ()->depth;
722
723   gdk_drawable_ref (window);
724   gdk_win32_handle_table_insert (&GDK_WINDOW_HWND (window), window);
725
726   return window;
727 }
728
729 void
730 _gdk_windowing_window_destroy (GdkWindow *window,
731                                gboolean   recursing,
732                                gboolean   foreign_destroy)
733 {
734   GdkWindowObject *private = (GdkWindowObject *)window;
735
736   g_return_if_fail (GDK_IS_WINDOW (window));
737   
738   GDK_NOTE (MISC, g_print ("_gdk_windowing_window_destroy %#x\n",
739                            (guint) GDK_WINDOW_HWND (window)));
740
741   if (private->extension_events != 0)
742     gdk_input_window_destroy (window);
743
744   if (private->window_type == GDK_WINDOW_FOREIGN)
745     {
746       if (!foreign_destroy && (private->parent != NULL))
747         {
748           /* It's somebody else's window, but in our hierarchy,
749            * so reparent it to the root window, and then call
750            * DestroyWindow() on it.
751            */
752           gdk_window_hide (window);
753           gdk_window_reparent (window, NULL, 0, 0);
754           
755           /* Is this too drastic? Many (most?) applications
756            * quit if any window receives WM_QUIT I think.
757            * OTOH, I don't think foreign windows are much
758            * used, so the question is maybe academic.
759            */
760           PostMessage (GDK_WINDOW_HWND (window), WM_QUIT, 0, 0);
761         }
762     }
763   else if (!recursing && !foreign_destroy)
764     DestroyWindow (GDK_WINDOW_HWND (window));
765 }
766
767 /* This function is called when the window really gone.
768  */
769 void
770 gdk_window_destroy_notify (GdkWindow *window)
771 {
772   g_return_if_fail (window != NULL);
773   g_return_if_fail (GDK_IS_WINDOW (window));
774
775   GDK_NOTE (EVENTS,
776             g_print ("gdk_window_destroy_notify: %#x  %s\n",
777                      (guint) GDK_WINDOW_HWND (window),
778                      (GDK_WINDOW_DESTROYED (window) ? "(destroyed)" : "")));
779
780   if (!GDK_WINDOW_DESTROYED (window))
781     {
782       if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_FOREIGN)
783         g_warning ("window %#x unexpectedly destroyed",
784                    (guint) GDK_WINDOW_HWND (window));
785
786       _gdk_window_destroy (window, TRUE);
787     }
788   
789   gdk_win32_handle_table_remove (GDK_WINDOW_HWND (window));
790   gdk_drawable_unref (window);
791 }
792
793 void
794 gdk_window_show (GdkWindow *window)
795 {
796   GdkWindowObject *private;
797   
798   g_return_if_fail (GDK_IS_WINDOW (window));
799   
800   private = (GdkWindowObject*) window;
801   if (!private->destroyed)
802     {
803       GDK_NOTE (MISC, g_print ("gdk_window_show: %#x\n",
804                                (guint) GDK_WINDOW_HWND (window)));
805
806       private->mapped = TRUE;
807       if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_TEMP)
808         {
809           ShowWindow (GDK_WINDOW_HWND (window), SW_SHOWNOACTIVATE);
810           SetWindowPos (GDK_WINDOW_HWND (window), HWND_TOPMOST, 0, 0, 0, 0,
811                         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
812 #if 0
813           /* Don't put on toolbar */
814           ShowWindow (GDK_WINDOW_HWND (window), SW_HIDE);
815 #endif
816         }
817       else
818         {
819           ShowWindow (GDK_WINDOW_HWND (window), SW_SHOWNORMAL);
820           ShowWindow (GDK_WINDOW_HWND (window), SW_RESTORE);
821           SetForegroundWindow (GDK_WINDOW_HWND (window));
822           BringWindowToTop (GDK_WINDOW_HWND (window));
823 #if 0
824           ShowOwnedPopups (GDK_WINDOW_HWND (window), TRUE);
825 #endif
826         }
827     }
828 }
829
830 void
831 gdk_window_hide (GdkWindow *window)
832 {
833   GdkWindowObject *private;
834   
835   g_return_if_fail (window != NULL);
836
837   private = (GdkWindowObject*) window;
838   if (!private->destroyed)
839     {
840       GDK_NOTE (MISC, g_print ("gdk_window_hide: %#x\n",
841                                (guint) GDK_WINDOW_HWND (window)));
842
843       private->mapped = FALSE;
844       if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_TOPLEVEL)
845         ShowOwnedPopups (GDK_WINDOW_HWND (window), FALSE);
846
847       ShowWindow (GDK_WINDOW_HWND (window), SW_HIDE);
848     }
849 }
850
851 void
852 gdk_window_withdraw (GdkWindow *window)
853 {
854   GdkWindowObject *private;
855   
856   g_return_if_fail (window != NULL);
857   
858   private = (GdkWindowObject*) window;
859   if (!private->destroyed)
860     {
861       GDK_NOTE (MISC, g_print ("gdk_window_withdraw: %#x\n",
862                                (guint) GDK_WINDOW_HWND (window)));
863
864       gdk_window_hide (window); /* ??? */
865     }
866 }
867
868 void
869 gdk_window_move (GdkWindow *window,
870                  gint       x,
871                  gint       y)
872 {
873   GdkWindowObject *private = (GdkWindowObject *)window;
874   GdkWindowImplWin32 *impl;
875
876   g_return_if_fail (window != NULL);
877   g_return_if_fail (GDK_IS_WINDOW (window));
878
879   impl = GDK_WINDOW_IMPL_WIN32 (private->impl);
880   
881   gdk_window_move_resize (window, x, y, impl->width, impl->height);
882 }
883
884 void
885 gdk_window_resize (GdkWindow *window,
886                    gint       width,
887                    gint       height)
888 {
889   GdkWindowObject *private = (GdkWindowObject*) window;
890   GdkWindowImplWin32 *impl;
891   int x, y;
892
893   g_return_if_fail (window != NULL);
894   g_return_if_fail (GDK_IS_WINDOW (window));
895
896   if (width < 1)
897     width = 1;
898   if (height < 1)
899     height = 1;
900
901   impl = GDK_WINDOW_IMPL_WIN32 (private->impl);
902   
903   if (!private->destroyed)
904     {
905       GDK_NOTE (MISC, g_print ("gdk_window_resize: %#x %dx%d\n",
906                                (guint) GDK_WINDOW_HWND (window),
907                                width, height));
908       
909       if (GDK_WINDOW_TYPE (private) != GDK_WINDOW_CHILD)
910         {
911           POINT pt;
912           RECT rect;
913           DWORD dwStyle;
914           DWORD dwExStyle;
915
916           pt.x = 0;
917           pt.y = 0; 
918           ClientToScreen (GDK_WINDOW_HWND (window), &pt);
919           rect.left = pt.x;
920           rect.top = pt.y;
921           rect.right = pt.x + width;
922           rect.bottom = pt.y + height;
923
924           dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
925           dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
926           if (!AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle))
927             WIN32_API_FAILED ("AdjustWindowRectEx");
928
929           x = rect.left;
930           y = rect.top;
931           width = rect.right - rect.left;
932           height = rect.bottom - rect.top;
933         }
934       else
935         {
936           x = private->x;
937           y = private->y;
938           impl->width = width;
939           impl->height = height;
940         }
941
942       private->resize_count += 1;
943
944       GDK_NOTE (MISC, g_print ("...MoveWindow(%#x,%dx%d@+%d+%d)\n",
945                                (guint) GDK_WINDOW_HWND (window),
946                                width, height, x, y));
947       if (!MoveWindow (GDK_WINDOW_HWND (window), x, y, width, height, TRUE))
948         WIN32_API_FAILED ("MoveWindow");
949     }
950 }
951
952 void
953 gdk_window_move_resize (GdkWindow *window,
954                         gint       x,
955                         gint       y,
956                         gint       width,
957                         gint       height)
958 {
959   GdkWindowObject *private = (GdkWindowObject*) window;
960   GdkWindowImplWin32 *impl;
961
962   g_return_if_fail (window != NULL);
963   g_return_if_fail (GDK_IS_WINDOW (window));
964
965   if (width < 1)
966     width = 1;
967   if (height < 1)
968     height = 1;
969   
970   impl = GDK_WINDOW_IMPL_WIN32 (private->impl);
971
972   if (!private->destroyed)
973     {
974       RECT rect;
975       DWORD dwStyle;
976       DWORD dwExStyle;
977
978       GDK_NOTE (MISC, g_print ("gdk_window_move_resize: %#x %dx%d@+%d+%d\n",
979                                (guint) GDK_WINDOW_HWND (window),
980                                width, height, x, y));
981       
982       if (GDK_WINDOW_TYPE (private) == GDK_WINDOW_CHILD)
983         _gdk_window_move_resize_child (window, x, y, width, height);
984       else
985         {
986           rect.left = x;
987           rect.top = y;
988           rect.right = x + width;
989           rect.bottom = y + height;
990
991           dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
992           dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
993           if (!AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle))
994             WIN32_API_FAILED ("AdjustWindowRectEx");
995
996           GDK_NOTE (MISC, g_print ("...MoveWindow(%#x,%ldx%ld@+%ld+%ld)\n",
997                                    (guint) GDK_WINDOW_HWND (window),
998                                    rect.right - rect.left, rect.bottom - rect.top,
999                                    rect.left, rect.top));
1000           if (!MoveWindow (GDK_WINDOW_HWND (window),
1001                            rect.left, rect.top,
1002                            rect.right - rect.left, rect.bottom - rect.top,
1003                            TRUE))
1004             WIN32_API_FAILED ("MoveWindow");
1005         }
1006     }
1007 }
1008
1009 void
1010 gdk_window_reparent (GdkWindow *window,
1011                      GdkWindow *new_parent,
1012                      gint       x,
1013                      gint       y)
1014 {
1015   GdkWindowObject *window_private;
1016   GdkWindowObject *parent_private;
1017   GdkWindowObject *old_parent_private;
1018   GdkWindowImplWin32 *impl;
1019
1020   g_return_if_fail (window != NULL);
1021
1022   if (!new_parent)
1023     new_parent = gdk_parent_root;
1024
1025   window_private = (GdkWindowObject*) window;
1026   old_parent_private = (GdkWindowObject *) window_private->parent;
1027   parent_private = (GdkWindowObject*) new_parent;
1028   impl = GDK_WINDOW_IMPL_WIN32 (window_private->impl);
1029
1030   if (!GDK_WINDOW_DESTROYED (window) && !GDK_WINDOW_DESTROYED (new_parent))
1031     {
1032       GDK_NOTE (MISC, g_print ("gdk_window_reparent: %#x %#x\n",
1033                                (guint) GDK_WINDOW_HWND (window),
1034                                (guint) GDK_WINDOW_HWND (new_parent)));
1035       if (!SetParent (GDK_WINDOW_HWND (window),
1036                       GDK_WINDOW_HWND (new_parent)))
1037         WIN32_API_FAILED ("SetParent");
1038
1039       if (!MoveWindow (GDK_WINDOW_HWND (window),
1040                        x, y, impl->width, impl->height, TRUE))
1041         WIN32_API_FAILED ("MoveWindow");
1042     }
1043   
1044   window_private->parent = (GdkWindowObject *)new_parent;
1045
1046   if (old_parent_private)
1047     old_parent_private->children =
1048       g_list_remove (old_parent_private->children, window);
1049
1050   if ((old_parent_private &&
1051        (!old_parent_private->guffaw_gravity != !parent_private->guffaw_gravity)) ||
1052       (!old_parent_private && parent_private->guffaw_gravity))
1053     gdk_window_set_static_win_gravity (window, parent_private->guffaw_gravity);
1054   
1055   parent_private->children = g_list_prepend (parent_private->children, window);
1056 }
1057
1058 void
1059 _gdk_windowing_window_clear_area (GdkWindow *window,
1060                                   gint       x,
1061                                   gint       y,
1062                                   gint       width,
1063                                   gint       height)
1064 {
1065   GdkWindowImplWin32 *impl;
1066
1067   g_return_if_fail (window != NULL);
1068   g_return_if_fail (GDK_IS_WINDOW (window));
1069   
1070   impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
1071
1072   if (!GDK_WINDOW_DESTROYED (window))
1073     {
1074       HDC hdc;
1075
1076       if (width == 0)
1077         width = impl->width - x;
1078       if (height == 0)
1079         height = impl->height - y;
1080       GDK_NOTE (MISC, g_print ("_gdk_windowing_window_clear_area: "
1081                                "%#x %dx%d@+%d+%d\n",
1082                                (guint) GDK_WINDOW_HWND (window),
1083                                width, height, x, y));
1084       hdc = GetDC (GDK_WINDOW_HWND (window));
1085       IntersectClipRect (hdc, x, y, x + width + 1, y + height + 1);
1086       SendMessage (GDK_WINDOW_HWND (window), WM_ERASEBKGND, (WPARAM) hdc, 0);
1087       ReleaseDC (GDK_WINDOW_HWND (window), hdc);
1088     }
1089 }
1090
1091 void
1092 _gdk_windowing_window_clear_area_e (GdkWindow *window,
1093                                     gint       x,
1094                                     gint       y,
1095                                     gint       width,
1096                                     gint       height)
1097 {
1098   g_return_if_fail (window != NULL);
1099   g_return_if_fail (GDK_IS_WINDOW (window));
1100   
1101   if (!GDK_WINDOW_DESTROYED (window))
1102     {
1103       RECT rect;
1104
1105       GDK_NOTE (MISC, g_print ("_gdk_windowing_window_clear_area_e: "
1106                                "%#x %dx%d@+%d+%d\n",
1107                                (guint) GDK_WINDOW_HWND (window),
1108                                width, height, x, y));
1109
1110       rect.left = x;
1111       rect.right = x + width + 1;
1112       rect.top = y;
1113       rect.bottom = y + height + 1;
1114       if (!InvalidateRect (GDK_WINDOW_HWND (window), &rect, TRUE))
1115         WIN32_GDI_FAILED ("InvalidateRect");
1116       UpdateWindow (GDK_WINDOW_HWND (window));
1117     }
1118 }
1119
1120 void
1121 gdk_window_raise (GdkWindow *window)
1122 {
1123   g_return_if_fail (window != NULL);
1124   g_return_if_fail (GDK_IS_WINDOW (window));
1125   
1126   if (!GDK_WINDOW_DESTROYED (window))
1127     {
1128       GDK_NOTE (MISC, g_print ("gdk_window_raise: %#x\n",
1129                                (guint) GDK_WINDOW_HWND (window)));
1130
1131       if (!BringWindowToTop (GDK_WINDOW_HWND (window)))
1132         WIN32_API_FAILED ("BringWindowToTop");
1133     }
1134 }
1135
1136 void
1137 gdk_window_lower (GdkWindow *window)
1138 {
1139   g_return_if_fail (window != NULL);
1140   g_return_if_fail (GDK_IS_WINDOW (window));
1141   
1142   if (!GDK_WINDOW_DESTROYED (window))
1143     {
1144       GDK_NOTE (MISC, g_print ("gdk_window_lower: %#x\n",
1145                                (guint) GDK_WINDOW_HWND (window)));
1146
1147       if (!SetWindowPos (GDK_WINDOW_HWND (window), HWND_BOTTOM, 0, 0, 0, 0,
1148                          SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE))
1149         WIN32_API_FAILED ("SetWindowPos");
1150     }
1151 }
1152
1153 void
1154 gdk_window_set_hints (GdkWindow *window,
1155                       gint       x,
1156                       gint       y,
1157                       gint       min_width,
1158                       gint       min_height,
1159                       gint       max_width,
1160                       gint       max_height,
1161                       gint       flags)
1162 {
1163   GdkWindowImplWin32 *impl;
1164   WINDOWPLACEMENT size_hints;
1165   RECT rect;
1166   DWORD dwStyle;
1167   DWORD dwExStyle;
1168   int diff;
1169   
1170   g_return_if_fail (window != NULL);
1171   g_return_if_fail (GDK_IS_WINDOW (window));
1172   
1173   if (GDK_WINDOW_DESTROYED (window))
1174     return;
1175   
1176   impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
1177
1178   GDK_NOTE (MISC, g_print ("gdk_window_set_hints: %#x %dx%d..%dx%d @+%d+%d\n",
1179                            (guint) GDK_WINDOW_HWND (window),
1180                            min_width, min_height, max_width, max_height,
1181                            x, y));
1182
1183   impl->hint_flags = flags;
1184   size_hints.length = sizeof (size_hints);
1185
1186   if (flags)
1187     {
1188       if (flags & GDK_HINT_POS)
1189         {
1190           if (!GetWindowPlacement (GDK_WINDOW_HWND (window), &size_hints))
1191             WIN32_API_FAILED ("GetWindowPlacement");
1192           else
1193             {
1194               GDK_NOTE (MISC, g_print ("...rcNormalPosition:"
1195                                        " (%ld,%ld)--(%ld,%ld)\n",
1196                                        size_hints.rcNormalPosition.left,
1197                                        size_hints.rcNormalPosition.top,
1198                                        size_hints.rcNormalPosition.right,
1199                                        size_hints.rcNormalPosition.bottom));
1200               /* What are the corresponding window coordinates for client
1201                * area coordinates x, y
1202                */
1203               rect.left = x;
1204               rect.top = y;
1205               rect.right = rect.left + 200;     /* dummy */
1206               rect.bottom = rect.top + 200;
1207               dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1208               dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1209               AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1210               size_hints.flags = 0;
1211               size_hints.showCmd = SW_SHOWNA;
1212               
1213               /* Set the normal position hint to that location, with unchanged
1214                * width and height.
1215                */
1216               diff = size_hints.rcNormalPosition.left - rect.left;
1217               size_hints.rcNormalPosition.left = rect.left;
1218               size_hints.rcNormalPosition.right -= diff;
1219               diff = size_hints.rcNormalPosition.top - rect.top;
1220               size_hints.rcNormalPosition.top = rect.top;
1221               size_hints.rcNormalPosition.bottom -= diff;
1222               GDK_NOTE (MISC, g_print ("...setting: (%ld,%ld)--(%ld,%ld)\n",
1223                                        size_hints.rcNormalPosition.left,
1224                                        size_hints.rcNormalPosition.top,
1225                                        size_hints.rcNormalPosition.right,
1226                                        size_hints.rcNormalPosition.bottom));
1227               if (!SetWindowPlacement (GDK_WINDOW_HWND (window), &size_hints))
1228                 WIN32_API_FAILED ("SetWindowPlacement");
1229               impl->hint_x = rect.left;
1230               impl->hint_y = rect.top;
1231             }
1232         }
1233
1234       if (flags & GDK_HINT_MIN_SIZE)
1235         {
1236           rect.left = 0;
1237           rect.top = 0;
1238           rect.right = min_width;
1239           rect.bottom = min_height;
1240           dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1241           dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1242           AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1243           impl->hint_min_width = rect.right - rect.left;
1244           impl->hint_min_height = rect.bottom - rect.top;
1245
1246           /* Also chek if he current size of the window is in bounds. */
1247           GetClientRect (GDK_WINDOW_HWND (window), &rect);
1248           if (rect.right < min_width && rect.bottom < min_height)
1249             gdk_window_resize (window, min_width, min_height);
1250           else if (rect.right < min_width)
1251             gdk_window_resize (window, min_width, rect.bottom);
1252           else if (rect.bottom < min_height)
1253             gdk_window_resize (window, rect.right, min_height);
1254         }
1255
1256       if (flags & GDK_HINT_MAX_SIZE)
1257         {
1258           rect.left = 0;
1259           rect.top = 0;
1260           rect.right = max_width;
1261           rect.bottom = max_height;
1262           dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1263           dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1264           AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1265           impl->hint_max_width = rect.right - rect.left;
1266           impl->hint_max_height = rect.bottom - rect.top;
1267           /* Again, check if the window is too large currently. */
1268           GetClientRect (GDK_WINDOW_HWND (window), &rect);
1269           if (rect.right > max_width && rect.bottom > max_height)
1270             gdk_window_resize (window, max_width, max_height);
1271           else if (rect.right > max_width)
1272             gdk_window_resize (window, max_width, rect.bottom);
1273           else if (rect.bottom > max_height)
1274             gdk_window_resize (window, rect.right, max_height);
1275         }
1276     }
1277 }
1278
1279 void 
1280 gdk_window_set_geometry_hints (GdkWindow      *window,
1281                                GdkGeometry    *geometry,
1282                                GdkWindowHints  geom_mask)
1283 {
1284   GdkWindowImplWin32 *impl;
1285   WINDOWPLACEMENT size_hints;
1286   RECT rect;
1287   DWORD dwStyle;
1288   DWORD dwExStyle;
1289   
1290   g_return_if_fail (window != NULL);
1291   g_return_if_fail (GDK_IS_WINDOW (window));
1292   
1293   if (GDK_WINDOW_DESTROYED (window))
1294     return;
1295
1296   impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
1297   size_hints.length = sizeof (size_hints);
1298
1299   impl->hint_flags = geom_mask;
1300
1301   if (geom_mask & GDK_HINT_POS)
1302     ; /* XXX */
1303
1304   if (geom_mask & GDK_HINT_MIN_SIZE)
1305     {
1306       rect.left = 0;
1307       rect.top = 0;
1308       rect.right = geometry->min_width;
1309       rect.bottom = geometry->min_height;
1310       dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1311       dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1312       AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1313       impl->hint_min_width = rect.right - rect.left;
1314       impl->hint_min_height = rect.bottom - rect.top;
1315
1316       /* Also check if he current size of the window is in bounds */
1317       GetClientRect (GDK_WINDOW_HWND (window), &rect);
1318       if (rect.right < geometry->min_width
1319           && rect.bottom < geometry->min_height)
1320         gdk_window_resize (window, geometry->min_width, geometry->min_height);
1321       else if (rect.right < geometry->min_width)
1322         gdk_window_resize (window, geometry->min_width, rect.bottom);
1323       else if (rect.bottom < geometry->min_height)
1324         gdk_window_resize (window, rect.right, geometry->min_height);
1325     }
1326   
1327   if (geom_mask & GDK_HINT_MAX_SIZE)
1328     {
1329       rect.left = 0;
1330       rect.top = 0;
1331       rect.right = geometry->max_width;
1332       rect.bottom = geometry->max_height;
1333       dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1334       dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1335       /* HB: dont' know why AdjustWindowRectEx is called here, ... */
1336       SafeAdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1337       impl->hint_max_width = rect.right - rect.left;
1338       impl->hint_max_height = rect.bottom - rect.top;
1339       /* ... but negative sizes are always wrong */
1340       if (impl->hint_max_width < 0) impl->hint_max_width = G_MAXSHORT;
1341       if (impl->hint_max_height < 0) impl->hint_max_height = G_MAXSHORT;
1342
1343       /* Again, check if the window is too large currently. */
1344       GetClientRect (GDK_WINDOW_HWND (window), &rect);
1345       if (rect.right > geometry->max_width
1346           && rect.bottom > geometry->max_height)
1347         gdk_window_resize (window, geometry->max_width, geometry->max_height);
1348       else if (rect.right > geometry->max_width)
1349         gdk_window_resize (window, geometry->max_width, rect.bottom);
1350       else if (rect.bottom > geometry->max_height)
1351         gdk_window_resize (window, rect.right, geometry->max_height);
1352     }
1353   
1354   /* I don't know what to do when called with zero base_width and height. */
1355   if (geom_mask & GDK_HINT_BASE_SIZE
1356       && geometry->base_width > 0
1357       && geometry->base_height > 0)
1358     {
1359       if (!GetWindowPlacement (GDK_WINDOW_HWND (window), &size_hints))
1360         WIN32_API_FAILED ("GetWindowPlacement");
1361       else
1362         {
1363           GDK_NOTE (MISC, g_print ("gdk_window_set_geometry_hints:"
1364                                    " rcNormalPosition: (%ld,%ld)--(%ld,%ld)\n",
1365                                    size_hints.rcNormalPosition.left,
1366                                    size_hints.rcNormalPosition.top,
1367                                    size_hints.rcNormalPosition.right,
1368                                    size_hints.rcNormalPosition.bottom));
1369           size_hints.rcNormalPosition.right =
1370             size_hints.rcNormalPosition.left + geometry->base_width;
1371           size_hints.rcNormalPosition.bottom =
1372             size_hints.rcNormalPosition.top + geometry->base_height;
1373           GDK_NOTE (MISC, g_print ("...setting: rcNormal: (%ld,%ld)--(%ld,%ld)\n",
1374                                    size_hints.rcNormalPosition.left,
1375                                    size_hints.rcNormalPosition.top,
1376                                    size_hints.rcNormalPosition.right,
1377                                    size_hints.rcNormalPosition.bottom));
1378           if (!SetWindowPlacement (GDK_WINDOW_HWND (window), &size_hints))
1379             WIN32_API_FAILED ("SetWindowPlacement");
1380         }
1381     }
1382   
1383   if (geom_mask & GDK_HINT_RESIZE_INC)
1384     {
1385       /* XXX */
1386     }
1387   
1388   if (geom_mask & GDK_HINT_ASPECT)
1389     {
1390       /* XXX */
1391     }
1392 }
1393
1394 void
1395 gdk_window_set_title (GdkWindow   *window,
1396                       const gchar *title)
1397 {
1398   char *mbtitle;
1399
1400   g_return_if_fail (window != NULL);
1401   g_return_if_fail (GDK_IS_WINDOW (window));
1402   g_return_if_fail (title != NULL);
1403
1404   /* Empty window titles not allowed, so set it to just a period. */
1405   if (!title[0])
1406     title = ".";
1407   
1408   GDK_NOTE (MISC, g_print ("gdk_window_set_title: %#x %s\n",
1409                            (guint) GDK_WINDOW_HWND (window), title));
1410   
1411   if (!GDK_WINDOW_DESTROYED (window))
1412     {
1413       /* As the title is in UTF-8 we must translate it
1414        * to the system codepage.
1415        */
1416       mbtitle = g_locale_from_utf8 (title, -1, NULL, NULL, NULL);
1417       if (!SetWindowText (GDK_WINDOW_HWND (window), mbtitle))
1418         WIN32_API_FAILED ("SetWindowText");
1419
1420       g_free (mbtitle);
1421     }
1422 }
1423
1424 void          
1425 gdk_window_set_role (GdkWindow   *window,
1426                      const gchar *role)
1427 {
1428   g_return_if_fail (window != NULL);
1429   g_return_if_fail (GDK_IS_WINDOW (window));
1430   
1431   GDK_NOTE (MISC, g_print ("gdk_window_set_role: %#x %s\n",
1432                            (guint) GDK_WINDOW_HWND (window),
1433                            (role ? role : "NULL")));
1434   /* XXX */
1435 }
1436
1437 void          
1438 gdk_window_set_transient_for (GdkWindow *window, 
1439                               GdkWindow *parent)
1440 {
1441   g_return_if_fail (window != NULL);
1442   g_return_if_fail (GDK_IS_WINDOW (window));
1443   
1444   GDK_NOTE (MISC, g_print ("gdk_window_set_transient_for: %#x %#x\n",
1445                            (guint) GDK_WINDOW_HWND (window),
1446                            (guint) GDK_WINDOW_HWND (parent)));
1447   /* XXX */
1448 }
1449
1450 void
1451 gdk_window_set_background (GdkWindow *window,
1452                            GdkColor  *color)
1453 {
1454   GdkWindowObject *private = (GdkWindowObject *)window;
1455   
1456   g_return_if_fail (window != NULL);
1457   g_return_if_fail (GDK_IS_WINDOW (window));
1458   
1459   GDK_NOTE (MISC, g_print ("gdk_window_set_background: %#x %s\n",
1460                            (guint) GDK_WINDOW_HWND (window), 
1461                            gdk_win32_color_to_string (color)));
1462
1463   private->bg_color = *color;
1464
1465   if (private->bg_pixmap &&
1466       private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
1467       private->bg_pixmap != GDK_NO_BG)
1468     {
1469       gdk_drawable_unref (private->bg_pixmap);
1470       private->bg_pixmap = NULL;
1471     }
1472 }
1473
1474 void
1475 gdk_window_set_back_pixmap (GdkWindow *window,
1476                             GdkPixmap *pixmap,
1477                             gint       parent_relative)
1478 {
1479   GdkWindowObject *private = (GdkWindowObject *)window;
1480
1481   g_return_if_fail (window != NULL);
1482   g_return_if_fail (GDK_IS_WINDOW (window));
1483   g_return_if_fail (pixmap == NULL || !parent_relative);
1484   
1485   if (private->bg_pixmap &&
1486       private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
1487       private->bg_pixmap != GDK_NO_BG)
1488     gdk_drawable_unref (private->bg_pixmap);
1489
1490   if (parent_relative)
1491     {
1492       private->bg_pixmap = GDK_PARENT_RELATIVE_BG;
1493     }
1494   else
1495     {
1496       if (pixmap)
1497         {
1498           gdk_drawable_ref (pixmap);
1499           private->bg_pixmap = pixmap;
1500         }
1501       else
1502         {
1503           private->bg_pixmap = GDK_NO_BG;
1504         }
1505     }
1506 }
1507
1508 void
1509 gdk_window_set_cursor (GdkWindow *window,
1510                        GdkCursor *cursor)
1511 {
1512   GdkWindowImplWin32 *impl;
1513   GdkCursorPrivate *cursor_private;
1514   HCURSOR hcursor;
1515   POINT pt;
1516   
1517   g_return_if_fail (window != NULL);
1518   g_return_if_fail (GDK_IS_WINDOW (window));
1519   
1520   impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
1521   cursor_private = (GdkCursorPrivate*) cursor;
1522   
1523   if (!GDK_WINDOW_DESTROYED (window))
1524     {
1525       if (!cursor)
1526         hcursor = NULL;
1527       else
1528         hcursor = cursor_private->hcursor;
1529
1530       GDK_NOTE (MISC, g_print ("gdk_window_set_cursor: %#x %#x\n",
1531                                (guint) GDK_WINDOW_HWND (window),
1532                                (guint) hcursor));
1533       if (impl->hcursor != NULL)
1534         {
1535           GDK_NOTE (MISC, g_print ("...DestroyCursor (%#x)\n",
1536                                    (guint) impl->hcursor));
1537           
1538           DestroyCursor (impl->hcursor);
1539           impl->hcursor = NULL;
1540         }
1541       if (hcursor != NULL)
1542         {
1543           /* We must copy the cursor as it is OK to destroy the GdkCursor
1544            * while still in use for some window. See for instance
1545            * gimp_change_win_cursor() which calls
1546            * gdk_window_set_cursor (win, cursor), and immediately
1547            * afterwards gdk_cursor_destroy (cursor).
1548            */
1549           impl->hcursor = CopyCursor (hcursor);
1550           GDK_NOTE (MISC, g_print ("...CopyCursor (%#x) = %#x\n",
1551                                    (guint) hcursor, (guint) impl->hcursor));
1552
1553           GetCursorPos (&pt);
1554           if (ChildWindowFromPoint (GDK_WINDOW_HWND (window), pt) == GDK_WINDOW_HWND (window))
1555             SetCursor (impl->hcursor);
1556         }
1557     }
1558 }
1559
1560 void
1561 gdk_window_get_geometry (GdkWindow *window,
1562                          gint      *x,
1563                          gint      *y,
1564                          gint      *width,
1565                          gint      *height,
1566                          gint      *depth)
1567 {
1568   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
1569   
1570   if (!window)
1571     window = gdk_parent_root;
1572   
1573   if (!GDK_WINDOW_DESTROYED (window))
1574     {
1575       RECT rect;
1576
1577       if (!GetClientRect (GDK_WINDOW_HWND (window), &rect))
1578         WIN32_API_FAILED ("GetClientRect");
1579
1580       if (x)
1581         *x = rect.left;
1582       if (y)
1583         *y = rect.top;
1584       if (width)
1585         *width = rect.right - rect.left;
1586       if (height)
1587         *height = rect.bottom - rect.top;
1588       if (depth)
1589         *depth = gdk_drawable_get_visual (window)->depth;
1590     }
1591 }
1592
1593 gint
1594 gdk_window_get_origin (GdkWindow *window,
1595                        gint      *x,
1596                        gint      *y)
1597 {
1598   gint return_val;
1599   gint tx = 0;
1600   gint ty = 0;
1601
1602   g_return_val_if_fail (window != NULL, 0);
1603
1604   if (!GDK_WINDOW_DESTROYED (window))
1605     {
1606       POINT pt;
1607
1608       pt.x = 0;
1609       pt.y = 0;
1610       ClientToScreen (GDK_WINDOW_HWND (window), &pt);
1611       tx = pt.x;
1612       ty = pt.y;
1613       return_val = 1;
1614     }
1615   else
1616     return_val = 0;
1617   
1618   if (x)
1619     *x = tx;
1620   if (y)
1621     *y = ty;
1622
1623   GDK_NOTE (MISC, g_print ("gdk_window_get_origin: %#x: +%d+%d\n",
1624                            (guint) GDK_WINDOW_HWND (window),
1625                            tx, ty));
1626   return return_val;
1627 }
1628
1629 gboolean
1630 gdk_window_get_deskrelative_origin (GdkWindow *window,
1631                                     gint      *x,
1632                                     gint      *y)
1633 {
1634   return gdk_window_get_origin (window, x, y);
1635 }
1636
1637 void
1638 gdk_window_get_root_origin (GdkWindow *window,
1639                             gint      *x,
1640                             gint      *y)
1641 {
1642   GdkWindowObject *rover;
1643   POINT pt;
1644
1645   g_return_if_fail (window != NULL);
1646   g_return_if_fail (GDK_IS_WINDOW (window));
1647
1648   rover = (GdkWindowObject*) window;
1649   if (x)
1650     *x = 0;
1651   if (y)
1652     *y = 0;
1653
1654   if (GDK_WINDOW_DESTROYED (window))
1655     return;
1656       
1657   while (rover->parent && ((GdkWindowObject*) rover->parent)->parent)
1658     rover = (GdkWindowObject *) rover->parent;
1659   if (rover->destroyed)
1660     return;
1661
1662   pt.x = 0;
1663   pt.y = 0;
1664   ClientToScreen (GDK_WINDOW_HWND (rover), &pt);
1665   if (x)
1666     *x = pt.x;
1667   if (y)
1668     *y = pt.y;
1669
1670   GDK_NOTE (MISC, g_print ("gdk_window_get_root_origin: %#x: (%#x) +%ld+%ld\n",
1671                            (guint) GDK_WINDOW_HWND (window),
1672                            (guint) GDK_WINDOW_HWND (rover),
1673                            pt.x, pt.y));
1674 }
1675
1676 GdkWindow*
1677 gdk_window_get_pointer (GdkWindow       *window,
1678                         gint            *x,
1679                         gint            *y,
1680                         GdkModifierType *mask)
1681 {
1682   GdkWindow *return_val;
1683   POINT pointc, point;
1684   HWND hwnd, hwndc;
1685
1686   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
1687   
1688   if (!window)
1689     window = gdk_parent_root;
1690
1691   return_val = NULL;
1692   GetCursorPos (&pointc);
1693   point = pointc;
1694   ScreenToClient (GDK_WINDOW_HWND (window), &point);
1695
1696   if (x)
1697     *x = point.x;
1698   if (y)
1699     *y = point.y;
1700
1701   hwnd = WindowFromPoint (point);
1702   point = pointc;
1703   ScreenToClient (hwnd, &point);
1704   
1705   do {
1706     hwndc = ChildWindowFromPoint (hwnd, point);
1707     ClientToScreen (hwnd, &point);
1708     ScreenToClient (hwndc, &point);
1709   } while (hwndc != hwnd && (hwnd = hwndc, 1)); /* Ouch! */
1710
1711   return_val = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
1712
1713   if (mask)
1714     {
1715       BYTE kbd[256];
1716
1717       GetKeyboardState (kbd);
1718       *mask = 0;
1719       if (kbd[VK_SHIFT] & 0x80)
1720         *mask |= GDK_SHIFT_MASK;
1721       if (kbd[VK_CAPITAL] & 0x80)
1722         *mask |= GDK_LOCK_MASK;
1723       if (kbd[VK_CONTROL] & 0x80)
1724         *mask |= GDK_CONTROL_MASK;
1725       if (kbd[VK_MENU] & 0x80)
1726         *mask |= GDK_MOD1_MASK;
1727       if (kbd[VK_LBUTTON] & 0x80)
1728         *mask |= GDK_BUTTON1_MASK;
1729       if (kbd[VK_MBUTTON] & 0x80)
1730         *mask |= GDK_BUTTON2_MASK;
1731       if (kbd[VK_RBUTTON] & 0x80)
1732         *mask |= GDK_BUTTON3_MASK;
1733     }
1734   
1735   return return_val;
1736 }
1737
1738 GdkWindow*
1739 gdk_window_at_pointer (gint *win_x,
1740                        gint *win_y)
1741 {
1742   GdkWindow *window;
1743   POINT point, pointc;
1744   HWND hwnd, hwndc;
1745   RECT rect;
1746
1747   GetCursorPos (&pointc);
1748   point = pointc;
1749   hwnd = WindowFromPoint (point);
1750
1751   if (hwnd == NULL)
1752     {
1753       window = gdk_parent_root;
1754       if (win_x)
1755         *win_x = pointc.x;
1756       if (win_y)
1757         *win_y = pointc.y;
1758       return window;
1759     }
1760       
1761   ScreenToClient (hwnd, &point);
1762
1763   do {
1764     hwndc = ChildWindowFromPoint (hwnd, point);
1765     ClientToScreen (hwnd, &point);
1766     ScreenToClient (hwndc, &point);
1767   } while (hwndc != hwnd && (hwnd = hwndc, 1));
1768
1769   window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
1770
1771   if (window && (win_x || win_y))
1772     {
1773       GetClientRect (hwnd, &rect);
1774       if (win_x)
1775         *win_x = point.x - rect.left;
1776       if (win_y)
1777         *win_y = point.y - rect.top;
1778     }
1779
1780   GDK_NOTE (MISC, g_print ("gdk_window_at_pointer: +%ld+%ld %#x%s\n",
1781                            point.x, point.y,
1782                            (guint) hwnd,
1783                            (window == NULL ? " NULL" : "")));
1784
1785   return window;
1786 }
1787
1788 GdkEventMask  
1789 gdk_window_get_events (GdkWindow *window)
1790 {
1791   g_return_val_if_fail (window != NULL, 0);
1792   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1793
1794   if (GDK_WINDOW_DESTROYED (window))
1795     return 0;
1796
1797   return GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl)->event_mask;
1798 }
1799
1800 void          
1801 gdk_window_set_events (GdkWindow   *window,
1802                        GdkEventMask event_mask)
1803 {
1804   g_return_if_fail (window != NULL);
1805   g_return_if_fail (GDK_IS_WINDOW (window));
1806
1807   if (GDK_WINDOW_DESTROYED (window))
1808     return;
1809
1810   GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl)->event_mask = event_mask;
1811 }
1812
1813 void
1814 gdk_window_shape_combine_mask (GdkWindow *window,
1815                                GdkBitmap *mask,
1816                                gint x, gint y)
1817 {
1818   g_return_if_fail (window != NULL);
1819   g_return_if_fail (GDK_IS_WINDOW (window));
1820
1821   if (!mask)
1822     {
1823       GDK_NOTE (MISC, g_print ("gdk_window_shape_combine_mask: %#x none\n",
1824                                (guint) GDK_WINDOW_HWND (window)));
1825       SetWindowRgn (GDK_WINDOW_HWND (window), NULL, TRUE);
1826     }
1827   else
1828     {
1829       HRGN hrgn;
1830       DWORD dwStyle;
1831       DWORD dwExStyle;
1832       RECT rect;
1833
1834       /* Convert mask bitmap to region */
1835       hrgn = BitmapToRegion (GDK_WINDOW_HWND (mask));
1836
1837       GDK_NOTE (MISC, g_print ("gdk_window_shape_combine_mask: %#x %#x\n",
1838                                (guint) GDK_WINDOW_HWND (window),
1839                                (guint) GDK_WINDOW_HWND (mask)));
1840
1841       /* SetWindowRgn wants window (not client) coordinates */ 
1842       dwStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1843       dwExStyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1844       GetClientRect (GDK_WINDOW_HWND (window), &rect);
1845       AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1846       OffsetRgn (hrgn, -rect.left, -rect.top);
1847
1848       OffsetRgn (hrgn, x, y);
1849
1850       /* If this is a top-level window, add the title bar to the region */
1851       if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_TOPLEVEL)
1852         {
1853           CombineRgn (hrgn, hrgn,
1854                       CreateRectRgn (0, 0, rect.right - rect.left, -rect.top),
1855                       RGN_OR);
1856         }
1857       
1858       SetWindowRgn (GDK_WINDOW_HWND (window), hrgn, TRUE);
1859     }
1860 }
1861
1862 void
1863 gdk_window_set_override_redirect (GdkWindow *window,
1864                                   gboolean   override_redirect)
1865 {
1866   g_return_if_fail (window != NULL);
1867   g_return_if_fail (GDK_IS_WINDOW (window));
1868
1869   g_warning ("gdk_window_set_override_redirect not implemented");
1870 }
1871
1872 void          
1873 gdk_window_set_icon (GdkWindow *window, 
1874                      GdkWindow *icon_window,
1875                      GdkPixmap *pixmap,
1876                      GdkBitmap *mask)
1877 {
1878   g_return_if_fail (window != NULL);
1879   g_return_if_fail (GDK_IS_WINDOW (window));
1880
1881   if (GDK_WINDOW_DESTROYED (window))
1882     return;
1883   
1884   /* Nothing to do, really. As we share window classes between windows
1885    * we can't have window-specific icons, sorry. Don't print any warning
1886    * either.
1887    */
1888 }
1889
1890 void
1891 gdk_window_set_icon_name (GdkWindow   *window, 
1892                           const gchar *name)
1893 {
1894   g_return_if_fail (window != NULL);
1895   g_return_if_fail (GDK_IS_WINDOW (window));
1896
1897   if (GDK_WINDOW_DESTROYED (window))
1898     return;
1899   
1900   if (!SetWindowText (GDK_WINDOW_HWND (window), name))
1901     WIN32_API_FAILED ("SetWindowText");
1902 }
1903
1904 void          
1905 gdk_window_set_group (GdkWindow *window, 
1906                       GdkWindow *leader)
1907 {
1908   g_return_if_fail (window != NULL);
1909   g_return_if_fail (GDK_IS_WINDOW (window));
1910   g_return_if_fail (leader != NULL);
1911   g_return_if_fail (GDK_IS_WINDOW (leader));
1912
1913   if (GDK_WINDOW_DESTROYED (window) || GDK_WINDOW_DESTROYED (leader))
1914     return;
1915   
1916   g_warning ("gdk_window_set_group not implemented");
1917 }
1918
1919 void
1920 gdk_window_set_decorations (GdkWindow      *window,
1921                             GdkWMDecoration decorations)
1922 {
1923   LONG style, exstyle;
1924
1925   g_return_if_fail (window != NULL);
1926   g_return_if_fail (GDK_IS_WINDOW (window));
1927   
1928   style = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1929   exstyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1930
1931   style &= (WS_OVERLAPPED|WS_POPUP|WS_CHILD|WS_MINIMIZE|WS_VISIBLE|WS_DISABLED
1932             |WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_MAXIMIZE);
1933
1934   exstyle &= (WS_EX_TOPMOST|WS_EX_TRANSPARENT);
1935
1936   if (decorations & GDK_DECOR_ALL)
1937     style |= (WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX);
1938   if (decorations & GDK_DECOR_BORDER)
1939     style |= (WS_BORDER);
1940   if (decorations & GDK_DECOR_RESIZEH)
1941     style |= (WS_THICKFRAME);
1942   if (decorations & GDK_DECOR_TITLE)
1943     style |= (WS_CAPTION);
1944   if (decorations & GDK_DECOR_MENU)
1945     style |= (WS_SYSMENU);
1946   if (decorations & GDK_DECOR_MINIMIZE)
1947     style |= (WS_MINIMIZEBOX);
1948   if (decorations & GDK_DECOR_MAXIMIZE)
1949     style |= (WS_MAXIMIZEBOX);
1950   
1951   SetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE, style);
1952 }
1953
1954 void
1955 gdk_window_set_functions (GdkWindow    *window,
1956                           GdkWMFunction functions)
1957 {
1958   LONG style, exstyle;
1959
1960   g_return_if_fail (window != NULL);
1961   g_return_if_fail (GDK_IS_WINDOW (window));
1962   
1963   style = GetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE);
1964   exstyle = GetWindowLong (GDK_WINDOW_HWND (window), GWL_EXSTYLE);
1965
1966   style &= (WS_OVERLAPPED|WS_POPUP|WS_CHILD|WS_MINIMIZE|WS_VISIBLE|WS_DISABLED
1967             |WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_MAXIMIZE|WS_CAPTION|WS_BORDER
1968             |WS_SYSMENU);
1969
1970   exstyle &= (WS_EX_TOPMOST|WS_EX_TRANSPARENT);
1971
1972   if (functions & GDK_FUNC_ALL)
1973     style |= (WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX);
1974   if (functions & GDK_FUNC_RESIZE)
1975     style |= (WS_THICKFRAME);
1976   if (functions & GDK_FUNC_MOVE)
1977     style |= (WS_THICKFRAME);
1978   if (functions & GDK_FUNC_MINIMIZE)
1979     style |= (WS_MINIMIZEBOX);
1980   if (functions & GDK_FUNC_MAXIMIZE)
1981     style |= (WS_MAXIMIZEBOX);
1982   
1983   SetWindowLong (GDK_WINDOW_HWND (window), GWL_STYLE, style);
1984 }
1985
1986 /* 
1987  * propagate the shapes from all child windows of a GDK window to the parent 
1988  * window. Shamelessly ripped from Enlightenment's code
1989  * 
1990  * - Raster
1991  */
1992
1993 static void
1994 QueryTree (HWND hwnd,
1995            HWND **children,
1996            gint *nchildren)
1997 {
1998   guint i, n;
1999   HWND child;
2000
2001   n = 0;
2002   do {
2003     if (n == 0)
2004       child = GetWindow (hwnd, GW_CHILD);
2005     else
2006       child = GetWindow (child, GW_HWNDNEXT);
2007     if (child != NULL)
2008       n++;
2009   } while (child != NULL);
2010
2011   if (n > 0)
2012     {
2013       *children = g_new (HWND, n);
2014       for (i = 0; i < n; i++)
2015         {
2016           if (i == 0)
2017             child = GetWindow (hwnd, GW_CHILD);
2018           else
2019             child = GetWindow (child, GW_HWNDNEXT);
2020           *children[i] = child;
2021         }
2022     }
2023 }
2024
2025 static void
2026 gdk_propagate_shapes (HANDLE   win,
2027                       gboolean merge)
2028 {
2029    RECT emptyRect;
2030    HRGN region, childRegion;
2031    HWND *list = NULL;
2032    gint i, num;
2033
2034    SetRectEmpty (&emptyRect);
2035    region = CreateRectRgnIndirect (&emptyRect);
2036    if (merge)
2037      GetWindowRgn (win, region);
2038    
2039    QueryTree (win, &list, &num);
2040    if (list != NULL)
2041      {
2042        WINDOWPLACEMENT placement;
2043
2044        placement.length = sizeof (WINDOWPLACEMENT);
2045        /* go through all child windows and combine regions */
2046        for (i = 0; i < num; i++)
2047          {
2048            GetWindowPlacement (list[i], &placement);
2049            if (placement.showCmd == SW_SHOWNORMAL)
2050              {
2051                childRegion = CreateRectRgnIndirect (&emptyRect);
2052                GetWindowRgn (list[i], childRegion);
2053                CombineRgn (region, region, childRegion, RGN_OR);
2054                DeleteObject (childRegion);
2055              }
2056           }
2057        SetWindowRgn (win, region, TRUE);
2058      }
2059    else
2060      DeleteObject (region);
2061 }
2062
2063 void
2064 gdk_window_set_child_shapes (GdkWindow *window)
2065 {
2066   g_return_if_fail (window != NULL);
2067   g_return_if_fail (GDK_IS_WINDOW (window));
2068    
2069   if (GDK_WINDOW_DESTROYED (window))
2070     return;
2071
2072   gdk_propagate_shapes (GDK_WINDOW_HWND (window), FALSE);
2073 }
2074
2075 void
2076 gdk_window_merge_child_shapes (GdkWindow *window)
2077 {
2078   g_return_if_fail (window != NULL);
2079   g_return_if_fail (GDK_IS_WINDOW (window));
2080   
2081   if (GDK_WINDOW_DESTROYED (window))
2082     return;
2083
2084   gdk_propagate_shapes (GDK_WINDOW_HWND (window), TRUE);
2085 }
2086
2087 /* Support for windows that can be guffaw-scrolled
2088  * (See http://www.gtk.org/~otaylor/whitepapers/guffaw-scrolling.txt)
2089  */
2090
2091 static gboolean
2092 gdk_window_gravity_works (void)
2093 {
2094   enum { UNKNOWN, NO, YES };
2095   static gint gravity_works = UNKNOWN;
2096   
2097   if (gravity_works == UNKNOWN)
2098     {
2099       GdkWindowAttr attr;
2100       GdkWindow *parent;
2101       GdkWindow *child;
2102       gint y;
2103       
2104       attr.window_type = GDK_WINDOW_TEMP;
2105       attr.wclass = GDK_INPUT_OUTPUT;
2106       attr.x = 0;
2107       attr.y = 0;
2108       attr.width = 100;
2109       attr.height = 100;
2110       attr.event_mask = 0;
2111       
2112       parent = gdk_window_new (NULL, &attr, GDK_WA_X | GDK_WA_Y);
2113       
2114       attr.window_type = GDK_WINDOW_CHILD;
2115       child = gdk_window_new (parent, &attr, GDK_WA_X | GDK_WA_Y);
2116       
2117       gdk_window_set_static_win_gravity (child, TRUE);
2118       
2119       gdk_window_resize (parent, 100, 110);
2120       gdk_window_move (parent, 0, -10);
2121       gdk_window_move_resize (parent, 0, 0, 100, 100);
2122       
2123       gdk_window_resize (parent, 100, 110);
2124       gdk_window_move (parent, 0, -10);
2125       gdk_window_move_resize (parent, 0, 0, 100, 100);
2126       
2127       gdk_window_get_geometry (child, NULL, &y, NULL, NULL, NULL);
2128       
2129       gdk_window_destroy (parent);
2130       gdk_window_destroy (child);
2131       
2132       gravity_works = ((y == -20) ? YES : NO);
2133     }
2134   
2135   return (gravity_works == YES);
2136 }
2137
2138 static void
2139 gdk_window_set_static_bit_gravity (GdkWindow *window, gboolean on)
2140 {
2141   g_return_if_fail (window != NULL);
2142
2143   GDK_NOTE (MISC, g_print ("gdk_window_set_static_bit_gravity: Not implemented\n"));
2144 }
2145
2146 static void
2147 gdk_window_set_static_win_gravity (GdkWindow *window, gboolean on)
2148 {
2149   g_return_if_fail (window != NULL);
2150
2151   GDK_NOTE (MISC,
2152             g_print ("gdk_window_set_static_win_gravity: Not implemented\n"));
2153 }
2154
2155 /*************************************************************
2156  * gdk_window_set_static_gravities:
2157  *     Set the bit gravity of the given window to static,
2158  *     and flag it so all children get static subwindow
2159  *     gravity.
2160  *   arguments:
2161  *     window: window for which to set static gravity
2162  *     use_static: Whether to turn static gravity on or off.
2163  *   results:
2164  *     Does the XServer support static gravity?
2165  *************************************************************/
2166
2167 gboolean 
2168 gdk_window_set_static_gravities (GdkWindow *window,
2169                                  gboolean   use_static)
2170 {
2171   GdkWindowObject *private = (GdkWindowObject *)window;
2172   GList *tmp_list;
2173   
2174   g_return_val_if_fail (window != NULL, FALSE);
2175   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
2176
2177   if (!use_static == !private->guffaw_gravity)
2178     return TRUE;
2179   
2180   if (use_static && !gdk_window_gravity_works ())
2181     return FALSE;
2182   
2183   private->guffaw_gravity = use_static;
2184   
2185   if (!GDK_WINDOW_DESTROYED (window))
2186     {
2187       gdk_window_set_static_bit_gravity (window, use_static);
2188       
2189       tmp_list = private->children;
2190       while (tmp_list)
2191         {
2192           gdk_window_set_static_win_gravity (window, use_static);
2193           
2194           tmp_list = tmp_list->next;
2195         }
2196     }
2197   
2198   return TRUE;
2199 }