gdkwindow-win32.c 56.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * Copyright (C) 1998-1999 Tor Lillqvist
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#include "gdkevents.h"
#include "gdkpixmap.h"
#include "gdkwindow.h"
36
#include "gdkprivate-win32.h"
37
#include "gdkinputprivate.h"
38
39
40
41
42
#include "gdkwin32.h"

static gboolean gdk_window_gravity_works (void);
static void     gdk_window_set_static_win_gravity (GdkWindow *window, 
						   gboolean   on);
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

/* The Win API function AdjustWindowRect may return negative values
 * resulting in obscured title bars. This helper function is coreccting it.
 */
BOOL
SafeAdjustWindowRectEx (RECT* lpRect, 
			DWORD dwStyle, 
			BOOL bMenu, 
			DWORD dwExStyle)
{
  if (!AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle))
    return FALSE;
  if (lpRect->left < 0)
    {
      lpRect->right -= lpRect->left;
      lpRect->left = 0;
    }
  if (lpRect->top < 0)
    {
      lpRect->bottom -= lpRect->top;
      lpRect->top = 0;
    }
  return TRUE;
}

68
69
static void
gdk_win32_window_destroy (GdkDrawable *drawable)
70
{
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  if (!GDK_DRAWABLE_DESTROYED (drawable))
    {
      if (GDK_DRAWABLE_TYPE (drawable) == GDK_WINDOW_FOREIGN)
	gdk_xid_table_remove (GDK_DRAWABLE_XID (drawable));
      else
	g_warning ("losing last reference to undestroyed window\n");
    }

  if (GDK_WINDOW_WIN32DATA (drawable)->bg_type == GDK_WIN32_BG_PIXMAP
      && GDK_WINDOW_WIN32DATA (drawable)->bg_pixmap != NULL)
    gdk_drawable_unref (GDK_WINDOW_WIN32DATA (drawable)->bg_pixmap);

  g_free (GDK_DRAWABLE_WIN32DATA (drawable));
}
85

86
87
88
89
90
91
92
93
static GdkWindow *
gdk_win32_window_alloc (void)
{
  GdkWindow *window;
  GdkWindowPrivate *private;
  
  static GdkDrawableClass klass;
  static gboolean initialized = FALSE;
94

95
  if (!initialized)
96
    {
97
98
99
100
      initialized = TRUE;
      
      klass = _gdk_win32_drawable_class;
      klass.destroy = gdk_win32_window_destroy;
101
102
    }

103
104
105
106
  window = _gdk_window_alloc ();
  private = (GdkWindowPrivate *) window;

  private->drawable.klass = &klass;
107
  private->drawable.klass_data = g_new (GdkWindowWin32Data, 1);
108
109
110
111
112
113
114
115
116
117
118
119
120
121

  GDK_WINDOW_WIN32DATA (window)->event_mask = 0;
  GDK_WINDOW_WIN32DATA (window)->bg_type = GDK_WIN32_BG_NORMAL;
  GDK_WINDOW_WIN32DATA (window)->xcursor = NULL;
  GDK_WINDOW_WIN32DATA (window)->hint_flags = 0;
  GDK_WINDOW_WIN32DATA (window)->extension_events = 0;
  GDK_WINDOW_WIN32DATA (window)->extension_events_selected = FALSE;
  
  GDK_WINDOW_WIN32DATA (window)->input_locale = GetKeyboardLayout (0);
  TranslateCharsetInfo ((DWORD FAR *) GetACP (),
			&GDK_WINDOW_WIN32DATA (window)->charset_info,
			TCI_SRCCODEPAGE);

  return window;
122
123
124
125
126
}

void
gdk_window_init (void)
{
127
  GdkWindowPrivate *private;
128
129
130
131
132
133
134
135
  RECT r;
  guint width;
  guint height;

  SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
  width  = r.right - r.left;
  height = r.bottom - r.top;

136
137
138
139
140
141
142
143
144
  gdk_parent_root = gdk_win32_window_alloc ();
  private = (GdkWindowPrivate *) gdk_parent_root;
  
  GDK_DRAWABLE_WIN32DATA (gdk_parent_root)->xid = gdk_root_window;
  private->drawable.window_type = GDK_WINDOW_ROOT;
  private->drawable.width = width;
  private->drawable.height = height;

  gdk_xid_table_insert (&gdk_root_window, gdk_parent_root);
145
146
147
148
149
150
151
152
153
154
}

/* RegisterGdkClass
 *   is a wrapper function for RegisterWindowClassEx.
 *   It creates at least one unique class for every 
 *   GdkWindowType. If support for single window-specific icons
 *   is ever needed (e.g Dialog specific), every such window should
 *   get its own class
 */
ATOM
155
RegisterGdkClass (GdkDrawableType wtype)
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{
  static ATOM klassTOPLEVEL = 0;
  static ATOM klassDIALOG   = 0;
  static ATOM klassCHILD    = 0;
  static ATOM klassTEMP     = 0;
  static HICON hAppIcon = NULL;
  static WNDCLASSEX wcl; 
  ATOM klass = 0;

  wcl.cbSize = sizeof(WNDCLASSEX);     
  wcl.style = 0; /* DON'T set CS_<H,V>REDRAW. It causes total redraw
                  * on WM_SIZE and WM_MOVE. Flicker, Performance!
                  */
  wcl.lpfnWndProc = gdk_WindowProc;
  wcl.cbClsExtra = 0;
  wcl.cbWndExtra = 0;
  wcl.hInstance = gdk_ProgInstance;
  wcl.hIcon = 0;
  /* initialize once! */
  if (0 == hAppIcon)
    {
      gchar sLoc [_MAX_PATH+1];
      HINSTANCE hInst = GetModuleHandle(NULL);

      if (0 != GetModuleFileName(hInst, sLoc, _MAX_PATH))
	{
	  hAppIcon = ExtractIcon(hInst, sLoc, 0);
	  if (0 == hAppIcon)
	    {
	      char *gdklibname = g_strdup_printf ("gdk-%s.dll", GDK_VERSION);

	      hAppIcon = ExtractIcon(hInst, gdklibname, 0);
	      g_free (gdklibname);
	    }
	  
	  if (0 == hAppIcon) 
	    hAppIcon = LoadIcon (NULL, IDI_APPLICATION);
	}
    }

  wcl.lpszMenuName = NULL;
  wcl.hIconSm = 0;

  /* initialize once per class */
#define ONCE_PER_CLASS() \
  wcl.hIcon = CopyIcon (hAppIcon); \
  wcl.hIconSm = CopyIcon (hAppIcon); \
  wcl.hbrBackground = CreateSolidBrush( RGB(0,0,0)); \
  wcl.hCursor = LoadCursor (NULL, IDC_ARROW); 
  
  switch (wtype)
  {
    case GDK_WINDOW_TOPLEVEL:
      if (0 == klassTOPLEVEL)
      {
	wcl.lpszClassName = "gdkWindowToplevel";

	ONCE_PER_CLASS();
	klassTOPLEVEL = RegisterClassEx(&wcl);
      }
      klass = klassTOPLEVEL;
      break;
    case GDK_WINDOW_CHILD:
      if (0 == klassCHILD)
      {
	wcl.lpszClassName = "gdkWindowChild";

        wcl.style |= CS_PARENTDC; /* MSDN: ... enhances system performance. */
	ONCE_PER_CLASS();
	klassCHILD = RegisterClassEx(&wcl);
      }
      klass = klassCHILD;
      break;
    case GDK_WINDOW_DIALOG:
      if (0 == klassDIALOG)
      {
	wcl.lpszClassName = "gdkWindowDialog";
        wcl.style |= CS_SAVEBITS;
	ONCE_PER_CLASS();
	klassDIALOG = RegisterClassEx(&wcl);
      }
      klass = klassDIALOG;
      break;
    case GDK_WINDOW_TEMP:
      if (0 == klassTEMP)
      {
	wcl.lpszClassName = "gdkWindowTemp";
        wcl.style |= CS_SAVEBITS;
	ONCE_PER_CLASS();
	klassTEMP = RegisterClassEx(&wcl);
      }
      klass = klassTEMP;
      break;
    case GDK_WINDOW_ROOT:
      g_error ("cannot make windows of type GDK_WINDOW_ROOT");
      break;
    case GDK_DRAWABLE_PIXMAP:
      g_error ("cannot make windows of type GDK_DRAWABLE_PIXMAP (use gdk_pixmap_new)");
      break;
  }

  return klass;
258
}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

GdkWindow*
gdk_window_new (GdkWindow     *parent,
		GdkWindowAttr *attributes,
		gint           attributes_mask)
{
  GdkWindow *window;
  GdkWindowPrivate *private;
  GdkWindowPrivate *parent_private;
  GdkVisual *visual;
  HANDLE xparent;
  Visual *xvisual;
  ATOM klass = 0;
  DWORD dwStyle, dwExStyle;
  RECT rect;
  int width, height;
  int x, y;
  char *title;
  gint titlelen;
  wchar_t *wctitle;
  gint wlen;
  char *mbtitle;

  g_return_val_if_fail (attributes != NULL, NULL);

  if (!parent)
285
    parent = gdk_parent_root;
286
287
288
289
290

  parent_private = (GdkWindowPrivate*) parent;
  if (GDK_DRAWABLE_DESTROYED (parent))
    return NULL;

291
  xparent = GDK_DRAWABLE_XID (parent);
292

293
294
  window = gdk_win32_window_alloc ();
  private = (GdkWindowPrivate *)window;
295
296
297
298
299
300
301
302
303

  private->parent = parent;

  private->x = (attributes_mask & GDK_WA_X) ? attributes->x : 0;
  private->y = (attributes_mask & GDK_WA_Y) ? attributes->y : 0;

  private->drawable.width = (attributes->width > 1) ? (attributes->width) : (1);
  private->drawable.height = (attributes->height > 1) ? (attributes->height) : (1);
  private->drawable.window_type = attributes->window_type;
304
305
  GDK_WINDOW_WIN32DATA (window)->extension_events = 0;
  GDK_WINDOW_WIN32DATA (window)->extension_events_selected = FALSE;
306
307
308
309
310
311
312
313
314
315
316

  if (attributes_mask & GDK_WA_VISUAL)
    visual = attributes->visual;
  else
    visual = gdk_visual_get_system ();
  xvisual = ((GdkVisualPrivate*) visual)->xvisual;

  if (attributes_mask & GDK_WA_TITLE)
    title = attributes->title;
  else
    title = g_get_prgname ();
317
318
  if (!title)
    title = "GDK client window";
319

320
  GDK_WINDOW_WIN32DATA (window)->event_mask = GDK_STRUCTURE_MASK | attributes->event_mask;
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
      
  if (parent_private && parent_private->guffaw_gravity)
    {
      /* XXX ??? */
    }

  if (attributes->wclass == GDK_INPUT_OUTPUT)
    {
      dwExStyle = 0;
      if (attributes_mask & GDK_WA_COLORMAP)
	private->drawable.colormap = attributes->colormap;
      else
	private->drawable.colormap = gdk_colormap_get_system ();
    }
  else
    {
      dwExStyle = WS_EX_TRANSPARENT;
      private->drawable.colormap = NULL;
339
340
      GDK_WINDOW_WIN32DATA (window)->bg_type = GDK_WIN32_BG_TRANSPARENT;
      GDK_WINDOW_WIN32DATA (window)->bg_pixmap = NULL;
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    }

  if (attributes_mask & GDK_WA_X)
    x = attributes->x;
  else
    x = CW_USEDEFAULT;

  if (attributes_mask & GDK_WA_Y)
    y = attributes->y;
  else if (attributes_mask & GDK_WA_X)
    y = 100;			/* ??? We must put it somewhere... */
  else
    y = 500;			/* x is CW_USEDEFAULT, y doesn't matter then */

  if (parent_private)
    parent_private->children = g_list_prepend (parent_private->children, window);

  switch (private->drawable.window_type)
    {
    case GDK_WINDOW_TOPLEVEL:
      dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
      xparent = gdk_root_window;
      break;
    case GDK_WINDOW_CHILD:
      dwStyle = WS_CHILDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
      break;
    case GDK_WINDOW_DIALOG:
      dwStyle = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU | WS_CAPTION | WS_THICKFRAME | WS_CLIPCHILDREN;
      dwExStyle |= WS_EX_TOPMOST; /* //HB: want this? */
      xparent = gdk_root_window;
      break;
    case GDK_WINDOW_TEMP:
      dwStyle = WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
      dwExStyle |= WS_EX_TOOLWINDOW;
      break;
    case GDK_WINDOW_ROOT:
      g_error ("cannot make windows of type GDK_WINDOW_ROOT");
      break;
    case GDK_DRAWABLE_PIXMAP:
      g_error ("cannot make windows of type GDK_DRAWABLE_PIXMAP (use gdk_pixmap_new)");
      break;
    }

  klass = RegisterGdkClass (private->drawable.window_type);
  if (!klass)
    g_error ("RegisterClassEx failed");

  if (private->drawable.window_type != GDK_WINDOW_CHILD)
    {
      if (x == CW_USEDEFAULT)
	{
	  rect.left = 100;
	  rect.top = 100;
	}
      else
	{
	  rect.left = x;
	  rect.top = y;
	}

      rect.right = rect.left + private->drawable.width;
      rect.bottom = rect.top + private->drawable.height;

      if (!SafeAdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle))
	g_warning ("gdk_window_new: AdjustWindowRectEx failed");

      if (x != CW_USEDEFAULT)
	{
	  x = rect.left;
	  y = rect.top;
	}
      width = rect.right - rect.left;
      height = rect.bottom - rect.top;
    }
  else
    {
      width = private->drawable.width;
      height = private->drawable.height;
    }

  titlelen = strlen (title);
  wctitle = g_new (wchar_t, titlelen + 1);
  mbtitle = g_new (char, 3*titlelen + 1);
  wlen = gdk_nmbstowchar_ts (wctitle, title, titlelen, titlelen);
  wctitle[wlen] = 0;
  WideCharToMultiByte (GetACP (), 0, wctitle, -1,
		       mbtitle, 3*titlelen, NULL, NULL);
  
429
  GDK_DRAWABLE_WIN32DATA (window)->xid =
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
    CreateWindowEx (dwExStyle,
		    MAKEINTRESOURCE(klass),
		    mbtitle,
		    dwStyle,
		    x, y, 
		    width, height,
		    xparent,
		    NULL,
		    gdk_ProgInstance,
		    NULL);

  GDK_NOTE (MISC,
	    g_print ("gdk_window_create: %s %s %dx%d@+%d+%d %#x = %#x\n"
		     "...locale %#x codepage %d\n",
		     (private->drawable.window_type == GDK_WINDOW_TOPLEVEL ? "TOPLEVEL" :
		      (private->drawable.window_type == GDK_WINDOW_CHILD ? "CHILD" :
		       (private->drawable.window_type == GDK_WINDOW_DIALOG ? "DIALOG" :
			(private->drawable.window_type == GDK_WINDOW_TEMP ? "TEMP" :
			 "???")))),
		     mbtitle,
		     width, height, (x == CW_USEDEFAULT ? -9999 : x), y, 
		     xparent,
452
453
454
		     GDK_DRAWABLE_XID (window),
		     GDK_WINDOW_WIN32DATA (window)->input_locale,
		     GDK_WINDOW_WIN32DATA (window)->charset_info.ciACP));
455
456
457
458

  g_free (mbtitle);
  g_free (wctitle);

459
  if (GDK_DRAWABLE_XID (window) == NULL)
460
461
    {
      g_warning ("gdk_window_create: CreateWindowEx failed");
462
      g_free (GDK_DRAWABLE_WIN32DATA (window));
463
464
465
466
      g_free (private);
      return NULL;
    }

467
468
  gdk_drawable_ref (window);
  gdk_xid_table_insert (&GDK_DRAWABLE_XID (window), window);
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489

  if (private->drawable.colormap)
    gdk_colormap_ref (private->drawable.colormap);

  gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
				  (attributes->cursor) :
				  NULL));

  return window;
}

GdkWindow *
gdk_window_foreign_new (guint32 anid)
{
  GdkWindow *window;
  GdkWindowPrivate *private;
  GdkWindowPrivate *parent_private;
  HANDLE parent;
  RECT rect;
  POINT point;

490
491
  window = gdk_win32_window_alloc ();
  private = (GdkWindowPrivate *)window;
492
493
494
495
496
497
498
499
500

  parent = GetParent ((HWND) anid);
  private->parent = gdk_xid_table_lookup (parent);

  parent_private = (GdkWindowPrivate *)private->parent;
  
  if (parent_private)
    parent_private->children = g_list_prepend (parent_private->children, window);

501
  GDK_DRAWABLE_WIN32DATA (window)->xid = (HWND) anid;
502
503
504
505
506
507
508
509
510
511
512
513
  GetClientRect ((HWND) anid, &rect);
  point.x = rect.left;
  point.y = rect.right;
  ClientToScreen ((HWND) anid, &point);
  if (parent != GetDesktopWindow ())
    ScreenToClient (parent, &point);
  private->x = point.x;
  private->y = point.y;
  private->drawable.width = rect.right - rect.left;
  private->drawable.height = rect.bottom - rect.top;
  private->drawable.window_type = GDK_WINDOW_FOREIGN;
  private->drawable.destroyed = FALSE;
514
  private->mapped = IsWindowVisible (GDK_DRAWABLE_XID (window));
515
516
517

  private->drawable.colormap = NULL;

518
519
  gdk_drawable_ref (window);
  gdk_xid_table_insert (&GDK_DRAWABLE_XID (window), window);
520
521
522
523
524

  return window;
}

/* Call this function when you want a window and all its children to
525
526
 * disappear.  When xdestroy is true, a request to destroy the window
 * is sent out.  When it is false, it is assumed that the window has
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
 * been or will be destroyed by destroying some ancestor of this
 * window.
 */
static void
gdk_window_internal_destroy (GdkWindow *window,
			     gboolean   xdestroy,
			     gboolean   our_destroy)
{
  GdkWindowPrivate *private;
  GdkWindowPrivate *temp_private;
  GdkWindow *temp_window;
  GList *children;
  GList *tmp;

  g_return_if_fail (window != NULL);

543
  private = (GdkWindowPrivate *) window;
544
545

  GDK_NOTE (MISC, g_print ("gdk_window_internal_destroy %#x\n",
546
			   GDK_DRAWABLE_XID (window)));
547

548
  switch (GDK_DRAWABLE_TYPE (window))
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_CHILD:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
    case GDK_WINDOW_FOREIGN:
      if (!private->drawable.destroyed)
	{
	  if (private->parent)
	    {
	      GdkWindowPrivate *parent_private = (GdkWindowPrivate *)private->parent;
	      if (parent_private->children)
		parent_private->children = g_list_remove (parent_private->children, window);
	    }

	  if (GDK_DRAWABLE_TYPE (window) != GDK_WINDOW_FOREIGN)
	    {
	      children = tmp = private->children;
	      private->children = NULL;

	      while (tmp)
		{
		  temp_window = tmp->data;
		  tmp = tmp->next;
		  
		  temp_private = (GdkWindowPrivate*) temp_window;
		  if (temp_private)
		    gdk_window_internal_destroy (temp_window, FALSE,
						 our_destroy);
		}

	      g_list_free (children);
	    }

	  if (private->extension_events != 0)
	    gdk_input_window_destroy (window);

	  if (private->filters)
	    {
	      tmp = private->filters;

	      while (tmp)
		{
		  g_free (tmp->data);
		  tmp = tmp->next;
		}

	      g_list_free (private->filters);
	      private->filters = NULL;
	    }
	  
	  if (private->drawable.window_type == GDK_WINDOW_FOREIGN)
	    {
	      if (our_destroy && (private->parent != NULL))
		{
		  /* It's somebody elses window, but in our hierarchy,
		   * so reparent it to the root window, and then send
		   * it a delete event, as if we were a WM
		   */
		  gdk_window_hide (window);
		  gdk_window_reparent (window, NULL, 0, 0);
		  
		  /* Is this too drastic? Many (most?) applications
		   * quit if any window receives WM_QUIT I think.
		   * OTOH, I don't think foreign windows are much
		   * used, so the question is maybe academic.
		   */
616
		  PostMessage (GDK_DRAWABLE_XID (window), WM_QUIT, 0, 0);
617
618
619
		}
	    }
	  else if (xdestroy)
620
	    DestroyWindow (GDK_DRAWABLE_XID (window));
621
622
623
624
625
626
627
628
629
630
631
632
633
634

	  if (private->drawable.colormap)
	    gdk_colormap_unref (private->drawable.colormap);

	  private->mapped = FALSE;
	  private->drawable.destroyed = TRUE;
	}
      break;

    case GDK_WINDOW_ROOT:
      g_error ("attempted to destroy root window");
      break;

    case GDK_DRAWABLE_PIXMAP:
635
      g_error ("called gdk_window_destroy on a pixmap (use gdk_drawable_unref)");
636
637
638
639
640
641
642
643
644
645
646
      break;
    }
}

/* Like internal_destroy, but also destroys the reference created by
   gdk_window_new. */

void
gdk_window_destroy (GdkWindow *window)
{
  gdk_window_internal_destroy (window, TRUE, TRUE);
647
  gdk_drawable_unref (window);
648
649
}

650
/* This function is called when the window really gone.  */
651
652
653
654
655
656

void
gdk_window_destroy_notify (GdkWindow *window)
{
  g_return_if_fail (window != NULL);

657
658
659
660
  GDK_NOTE (EVENTS,
	    g_print ("gdk_window_destroy_notify: %#x  %s\n",
		     GDK_DRAWABLE_XID (window),
		     (GDK_DRAWABLE_DESTROYED (window) ? "yes" : "no")));
661
662
663
664

  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      if (GDK_DRAWABLE_TYPE(window) != GDK_WINDOW_FOREIGN)
665
666
	g_warning ("window %#x unexpectedly destroyed",
		   GDK_DRAWABLE_XID (window));
667
668
669
670
671

      gdk_window_internal_destroy (window, FALSE, FALSE);
    }
  
  gdk_xid_table_remove (GDK_DRAWABLE_XID (window));
672
  gdk_drawable_unref (window);
673
674
675
676
677
678
679
}

void
gdk_window_show (GdkWindow *window)
{
  g_return_if_fail (window != NULL);

680
  if (!GDK_DRAWABLE_DESTROYED (window))
681
682
    {
      GDK_NOTE (MISC, g_print ("gdk_window_show: %#x\n",
683
			       GDK_DRAWABLE_XID (window)));
684

685
686
      ((GdkWindowPrivate *) window)->mapped = TRUE;
      if (GDK_DRAWABLE_TYPE (window) == GDK_WINDOW_TEMP)
687
	{
688
689
	  ShowWindow (GDK_DRAWABLE_XID (window), SW_SHOWNOACTIVATE);
	  SetWindowPos (GDK_DRAWABLE_XID (window), HWND_TOPMOST, 0, 0, 0, 0,
690
691
			SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
#if 0
692
693
	  /* Don't put on toolbar */
	  ShowWindow (GDK_DRAWABLE_XID (window), SW_HIDE);
694
695
696
697
#endif
	}
      else
	{
698
699
700
701
	  ShowWindow (GDK_DRAWABLE_XID (window), SW_SHOWNORMAL);
	  ShowWindow (GDK_DRAWABLE_XID (window), SW_RESTORE);
	  SetForegroundWindow (GDK_DRAWABLE_XID (window));
	  BringWindowToTop (GDK_DRAWABLE_XID (window));
702
#if 0
703
	  ShowOwnedPopups (GDK_DRAWABLE_XID (window), TRUE);
704
705
706
707
708
709
710
711
712
713
#endif
	}
    }
}

void
gdk_window_hide (GdkWindow *window)
{
  g_return_if_fail (window != NULL);

714
  if (!GDK_DRAWABLE_DESTROYED (window))
715
716
    {
      GDK_NOTE (MISC, g_print ("gdk_window_hide: %#x\n",
717
			       GDK_DRAWABLE_XID (window)));
718

719
720
721
      ((GdkWindowPrivate *) window)->mapped = FALSE;
      if (GDK_DRAWABLE_TYPE (window) == GDK_WINDOW_TOPLEVEL)
	ShowOwnedPopups (GDK_DRAWABLE_XID (window), FALSE);
722
#if 1
723
      ShowWindow (GDK_DRAWABLE_XID (window), SW_HIDE);
724
#elif 0
725
      ShowWindow (GDK_DRAWABLE_XID (window), SW_MINIMIZE);
726
#else
727
      CloseWindow (GDK_DRAWABLE_XID (window));
728
729
730
731
732
733
734
735
736
#endif
    }
}

void
gdk_window_withdraw (GdkWindow *window)
{
  g_return_if_fail (window != NULL);

737
  if (!GDK_DRAWABLE_DESTROYED (window))
738
739
    {
      GDK_NOTE (MISC, g_print ("gdk_window_withdraw: %#x\n",
740
			       GDK_DRAWABLE_XID (window)));
741
742
743
744
745
746
747
748
749
750
751
752
753
754

      gdk_window_hide (window);	/* XXX */
    }
}

void
gdk_window_move (GdkWindow *window,
		 gint       x,
		 gint       y)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

755
  if (!GDK_DRAWABLE_DESTROYED (window))
756
757
758
759
    {
      RECT rect;

      GDK_NOTE (MISC, g_print ("gdk_window_move: %#x +%d+%d\n",
760
			       GDK_DRAWABLE_XID (window), x, y));
761

762
763
      private = (GdkWindowPrivate *) window;
      GetClientRect (GDK_DRAWABLE_XID (window), &rect);
764

765
      if (GDK_DRAWABLE_TYPE (window) != GDK_WINDOW_CHILD)
766
767
768
769
770
771
772
	{
	  POINT ptTL, ptBR;
	  DWORD dwStyle;
	  DWORD dwExStyle;

	  ptTL.x = 0;
	  ptTL.y = 0; 
773
	  ClientToScreen (GDK_DRAWABLE_XID (window), &ptTL);
774
775
776
777
778
	  rect.left = x;
	  rect.top = y;

	  ptBR.x = rect.right;
	  ptBR.y = rect.bottom;
779
	  ClientToScreen (GDK_DRAWABLE_XID (window), &ptBR);
780
781
782
	  rect.right = x + ptBR.x - ptTL.x;
	  rect.bottom = y + ptBR.y - ptTL.y;

783
784
	  dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
	  dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
785
786
787
788
789
790
791
792
793
794
795
796
	  if (!SafeAdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle))
	    g_warning ("gdk_window_move: AdjustWindowRectEx failed");

	  x = rect.left;
	  y = rect.top;
	}
      else
	{
	  private->x = x;
	  private->y = y;
	}
      GDK_NOTE (MISC, g_print ("...MoveWindow(%#x,%dx%d@+%d+%d)\n",
797
			       GDK_DRAWABLE_XID (window),
798
799
			       rect.right - rect.left, rect.bottom - rect.top,
			       x, y));
800
      if (!MoveWindow (GDK_DRAWABLE_XID (window),
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
		       x, y, rect.right - rect.left, rect.bottom - rect.top,
		       TRUE))
	g_warning ("gdk_window_move: MoveWindow failed");
    }
}

void
gdk_window_resize (GdkWindow *window,
		   gint       width,
		   gint       height)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  if ((gint16) width < 1)
    width = 1;
  if ((gint16) height < 1)
    height = 1;

  private = (GdkWindowPrivate*) window;

  if (!private->drawable.destroyed &&
      ((private->resize_count > 0) ||
       (private->drawable.width != (guint16) width) ||
       (private->drawable.height != (guint16) height)))
    {
      int x, y;

      GDK_NOTE (MISC, g_print ("gdk_window_resize: %#x %dx%d\n",
831
			       GDK_DRAWABLE_XID (window), width, height));
832
833
834
835
836
837
838
839
840
841
      
      if (private->drawable.window_type != GDK_WINDOW_CHILD)
	{
	  POINT pt;
	  RECT rect;
	  DWORD dwStyle;
	  DWORD dwExStyle;

	  pt.x = 0;
	  pt.y = 0; 
842
	  ClientToScreen (GDK_DRAWABLE_XID (window), &pt);
843
844
845
846
847
	  rect.left = pt.x;
	  rect.top = pt.y;
	  rect.right = pt.x + width;
	  rect.bottom = pt.y + height;

848
849
	  dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
	  dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
	  if (!AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle))
	    g_warning ("gdk_window_resize: AdjustWindowRectEx failed");

	  x = rect.left;
	  y = rect.top;
	  width = rect.right - rect.left;
	  height = rect.bottom - rect.top;
	}
      else
	{
	  x = private->x;
	  y = private->y;
	  private->drawable.width = width;
	  private->drawable.height = height;
	}

      private->resize_count += 1;

868
869
870
871
      GDK_NOTE (MISC,
		g_print ("...MoveWindow(%#x,%dx%d@+%d+%d)\n",
			 GDK_DRAWABLE_XID (window), width, height, x, y));
      if (!MoveWindow (GDK_DRAWABLE_XID (window),
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
		       x, y, width, height,
		       TRUE))
	g_warning ("gdk_window_resize: MoveWindow failed");
    }
}

void
gdk_window_move_resize (GdkWindow *window,
			gint       x,
			gint       y,
			gint       width,
			gint       height)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  if ((gint16) width < 1)
    width = 1;
  if ((gint16) height < 1)
    height = 1;

894
  if (!GDK_DRAWABLE_DESTROYED (window))
895
896
897
898
899
900
    {
      RECT rect;
      DWORD dwStyle;
      DWORD dwExStyle;

      GDK_NOTE (MISC, g_print ("gdk_window_move_resize: %#x %dx%d@+%d+%d\n",
901
			       GDK_DRAWABLE_XID (window), width, height, x, y));
902
      
903
      private = (GdkWindowPrivate*) window;
904
905
906
907
908
      rect.left = x;
      rect.top = y;
      rect.right = x + width;
      rect.bottom = y + height;

909
910
      dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
      dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
911
912
913
914
915
916
917
918
919
920
921
      if (!AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle))
	g_warning ("gdk_window_move_resize: AdjustWindowRectEx failed");

      if (private->drawable.window_type == GDK_WINDOW_CHILD)
	{
	  private->x = x;
	  private->y = y;
	  private->drawable.width = width;
	  private->drawable.height = height;
	}
      GDK_NOTE (MISC, g_print ("...MoveWindow(%#x,%dx%d@+%d+%d)\n",
922
			       GDK_DRAWABLE_XID (window),
923
924
			       rect.right - rect.left, rect.bottom - rect.top,
			       rect.left, rect.top));
925
      if (!MoveWindow (GDK_DRAWABLE_XID (window),
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
		       rect.left, rect.top,
		       rect.right - rect.left, rect.bottom - rect.top,
		       TRUE))
	g_warning ("gdk_window_move_resize: MoveWindow failed");

      if (private->guffaw_gravity)
	{
	  GList *tmp_list = private->children;
	  while (tmp_list)
	    {
	      GdkWindowPrivate *child_private = tmp_list->data;
	      
	      child_private->x -= x - private->x;
	      child_private->y -= y - private->y;
	      
	      tmp_list = tmp_list->next;
	    }
	}
      
    }
}

void
gdk_window_reparent (GdkWindow *window,
		     GdkWindow *new_parent,
		     gint       x,
		     gint       y)
{
  GdkWindowPrivate *window_private;
  GdkWindowPrivate *parent_private;
  GdkWindowPrivate *old_parent_private;

  g_return_if_fail (window != NULL);

  if (!new_parent)
961
    new_parent = gdk_parent_root;
962
963

  window_private = (GdkWindowPrivate*) window;
964
  old_parent_private = (GdkWindowPrivate *) window_private->parent;
965
966
  parent_private = (GdkWindowPrivate*) new_parent;

967
968
  if (!GDK_DRAWABLE_DESTROYED (window)
      && !GDK_DRAWABLE_DESTROYED (new_parent))
969
970
    {
      GDK_NOTE (MISC, g_print ("gdk_window_reparent: %#x %#x\n",
971
972
973
974
			       GDK_DRAWABLE_XID (window),
			       GDK_DRAWABLE_XID (new_parent)));
      if (!SetParent (GDK_DRAWABLE_XID (window),
		      GDK_DRAWABLE_XID (new_parent)))
975
976
	g_warning ("gdk_window_reparent: SetParent failed");

977
      if (!MoveWindow (GDK_DRAWABLE_XID (window),
978
		       x, y,
979
980
		       window_private->drawable.width,
		       window_private->drawable.height,
981
982
983
984
985
986
987
		       TRUE))
	g_warning ("gdk_window_reparent: MoveWindow failed");
    }
  
  window_private->parent = new_parent;

  if (old_parent_private)
988
989
    old_parent_private->children =
      g_list_remove (old_parent_private->children, window);
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119

  if ((old_parent_private &&
       (!old_parent_private->guffaw_gravity != !parent_private->guffaw_gravity)) ||
      (!old_parent_private && parent_private->guffaw_gravity))
    gdk_window_set_static_win_gravity (window, parent_private->guffaw_gravity);
  
  parent_private->children = g_list_prepend (parent_private->children, window);
}

void
gdk_window_clear (GdkWindow *window)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

  if (!GDK_DRAWABLE_DESTROYED (window))
    gdk_window_clear_area (window, 0, 0, -1, -1);
}


void
gdk_window_clear_area (GdkWindow *window,
		       gint       x,
		       gint       y,
		       gint       width,
		       gint       height)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      HDC hdc;

      if (width == -1)
	width = G_MAXSHORT/2;		/* Yeah, right */
      if (height == -1)
	height = G_MAXSHORT/2;
      GDK_NOTE (MISC, g_print ("gdk_window_clear_area: %#x %dx%d@+%d+%d\n",
			       GDK_DRAWABLE_XID (window), width, height, x, y));
      hdc = GetDC (GDK_DRAWABLE_XID (window));
      IntersectClipRect (hdc, x, y, x + width, y + height);
      SendMessage (GDK_DRAWABLE_XID (window), WM_ERASEBKGND, (WPARAM) hdc, 0);
      ReleaseDC (GDK_DRAWABLE_XID (window), hdc);
    }
}

void
gdk_window_clear_area_e (GdkWindow *window,
		         gint       x,
		         gint       y,
		         gint       width,
		         gint       height)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      RECT rect;

      GDK_NOTE (MISC, g_print ("gdk_window_clear_area_e: %#x %dx%d@+%d+%d\n",
			       GDK_DRAWABLE_XID (window), width, height, x, y));

      rect.left = x;
      rect.right = x + width;
      rect.top = y;
      rect.bottom = y + height;
      if (!InvalidateRect (GDK_DRAWABLE_XID (window), &rect, TRUE))
	g_warning ("gdk_window_clear_area_e: InvalidateRect failed");
      UpdateWindow (GDK_DRAWABLE_XID (window));
    }
}

void
gdk_window_raise (GdkWindow *window)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      GDK_NOTE (MISC, g_print ("gdk_window_raise: %#x\n",
			       GDK_DRAWABLE_XID (window)));

      if (!BringWindowToTop (GDK_DRAWABLE_XID (window)))
	g_warning ("gdk_window_raise: BringWindowToTop failed");
    }
}

void
gdk_window_lower (GdkWindow *window)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      GDK_NOTE (MISC, g_print ("gdk_window_lower: %#x\n",
			       GDK_DRAWABLE_XID (window)));

      if (!SetWindowPos (GDK_DRAWABLE_XID (window), HWND_BOTTOM, 0, 0, 0, 0,
			 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE))
	g_warning ("gdk_window_lower: SetWindowPos failed");
    }
}

void
gdk_window_set_hints (GdkWindow *window,
		      gint       x,
		      gint       y,
		      gint       min_width,
		      gint       min_height,
		      gint       max_width,
		      gint       max_height,
		      gint       flags)
{
  WINDOWPLACEMENT size_hints;
  RECT rect;
  DWORD dwStyle;
  DWORD dwExStyle;
  int diff;
  
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (GDK_DRAWABLE_DESTROYED (window))
    return;
  
  GDK_NOTE (MISC, g_print ("gdk_window_set_hints: %#x %dx%d..%dx%d @+%d+%d\n",
1120
			   GDK_DRAWABLE_XID (window),
1121
1122
1123
			   min_width, min_height, max_width, max_height,
			   x, y));

1124
  GDK_WINDOW_WIN32DATA (window)->hint_flags = flags;
1125
1126
1127
1128
1129
  size_hints.length = sizeof (size_hints);

  if (flags)
    {
      if (flags & GDK_HINT_POS)
1130
	if (!GetWindowPlacement (GDK_DRAWABLE_XID (window), &size_hints))
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
	  g_warning ("gdk_window_set_hints: GetWindowPlacement failed");
	else
	  {
	    GDK_NOTE (MISC, g_print ("...rcNormalPosition:"
				     " (%d,%d)--(%d,%d)\n",
				     size_hints.rcNormalPosition.left,
				     size_hints.rcNormalPosition.top,
				     size_hints.rcNormalPosition.right,
				     size_hints.rcNormalPosition.bottom));
	    /* What are the corresponding window coordinates for client
	     * area coordinates x, y
	     */
	    rect.left = x;
	    rect.top = y;
	    rect.right = rect.left + 200;	/* dummy */
	    rect.bottom = rect.top + 200;
1147
1148
	    dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
	    dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
	    AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
	    size_hints.flags = 0;
	    size_hints.showCmd = SW_SHOWNA;

	    /* Set the normal position hint to that location, with unchanged
	     * width and height.
	     */
	    diff = size_hints.rcNormalPosition.left - rect.left;
	    size_hints.rcNormalPosition.left = rect.left;
	    size_hints.rcNormalPosition.right -= diff;
	    diff = size_hints.rcNormalPosition.top - rect.top;
	    size_hints.rcNormalPosition.top = rect.top;
	    size_hints.rcNormalPosition.bottom -= diff;
	    GDK_NOTE (MISC, g_print ("...setting: (%d,%d)--(%d,%d)\n",
				     size_hints.rcNormalPosition.left,
				     size_hints.rcNormalPosition.top,
				     size_hints.rcNormalPosition.right,
				     size_hints.rcNormalPosition.bottom));
1167
	    if (!SetWindowPlacement (GDK_DRAWABLE_XID (window), &size_hints))
1168
	      g_warning ("gdk_window_set_hints: SetWindowPlacement failed");
1169
1170
	    GDK_WINDOW_WIN32DATA (window)->hint_x = rect.left;
	    GDK_WINDOW_WIN32DATA (window)->hint_y = rect.top;
1171
1172
1173
1174
1175
1176
1177
1178
	  }

      if (flags & GDK_HINT_MIN_SIZE)
	{
	  rect.left = 0;
	  rect.top = 0;
	  rect.right = min_width;
	  rect.bottom = min_height;
1179
1180
	  dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
	  dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
1181
	  AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1182
1183
1184
1185
	  GDK_WINDOW_WIN32DATA (window)->hint_min_width =
	    rect.right - rect.left;
	  GDK_WINDOW_WIN32DATA (window)->hint_min_height =
	    rect.bottom - rect.top;
1186
1187

	  /* Also chek if he current size of the window is in bounds. */
1188
	  GetClientRect (GDK_DRAWABLE_XID (window), &rect);
1189
1190
1191
1192
1193
1194
1195
	  if (rect.right < min_width && rect.bottom < min_height)
	    gdk_window_resize (window, min_width, min_height);
	  else if (rect.right < min_width)
	    gdk_window_resize (window, min_width, rect.bottom);
	  else if (rect.bottom < min_height)
	    gdk_window_resize (window, rect.right, min_height);
	}
1196

1197
1198
1199
1200
1201
1202
      if (flags & GDK_HINT_MAX_SIZE)
	{
	  rect.left = 0;
	  rect.top = 0;
	  rect.right = max_width;
	  rect.bottom = max_height;
1203
1204
	  dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
	  dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
1205
	  AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1206
1207
1208
1209
	  GDK_WINDOW_WIN32DATA (window)->hint_max_width =
	    rect.right - rect.left;
	  GDK_WINDOW_WIN32DATA (window)->hint_max_height =
	    rect.bottom - rect.top;
1210
	  /* Again, check if the window is too large currently. */
1211
	  GetClientRect (GDK_DRAWABLE_XID (window), &rect);
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
	  if (rect.right > max_width && rect.bottom > max_height)
	    gdk_window_resize (window, max_width, max_height);
	  else if (rect.right > max_width)
	    gdk_window_resize (window, max_width, rect.bottom);
	  else if (rect.bottom > max_height)
	    gdk_window_resize (window, rect.right, max_height);
	}
    }
}

void 
gdk_window_set_geometry_hints (GdkWindow      *window,
			       GdkGeometry    *geometry,
			       GdkWindowHints  geom_mask)
{
  WINDOWPLACEMENT size_hints;
  RECT rect;
  DWORD dwStyle;
  DWORD dwExStyle;
  int diff;
  
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (GDK_DRAWABLE_DESTROYED (window))
    return;

  size_hints.length = sizeof (size_hints);

1241
  GDK_WINDOW_WIN32DATA (window)->hint_flags = geom_mask;
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251

  if (geom_mask & GDK_HINT_POS)
    ; /* XXX */

  if (geom_mask & GDK_HINT_MIN_SIZE)
    {
      rect.left = 0;
      rect.top = 0;
      rect.right = geometry->min_width;
      rect.bottom = geometry->min_height;
1252
1253
      dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
      dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
1254
      AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1255
1256
      GDK_WINDOW_WIN32DATA (window)->hint_min_width = rect.right - rect.left;
      GDK_WINDOW_WIN32DATA (window)->hint_min_height = rect.bottom - rect.top;
1257
1258

      /* Also check if he current size of the window is in bounds */
1259
      GetClientRect (GDK_DRAWABLE_XID (window), &rect);
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
      if (rect.right < geometry->min_width
	  && rect.bottom < geometry->min_height)
	gdk_window_resize (window, geometry->min_width, geometry->min_height);
      else if (rect.right < geometry->min_width)
	gdk_window_resize (window, geometry->min_width, rect.bottom);
      else if (rect.bottom < geometry->min_height)
	gdk_window_resize (window, rect.right, geometry->min_height);
    }
  
  if (geom_mask & GDK_HINT_MAX_SIZE)
    {
      rect.left = 0;
      rect.top = 0;
      rect.right = geometry->max_width;
      rect.bottom = geometry->max_height;
1275
1276
      dwStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_STYLE);
      dwExStyle = GetWindowLong (GDK_DRAWABLE_XID (window), GWL_EXSTYLE);
1277
      AdjustWindowRectEx (&rect, dwStyle, FALSE, dwExStyle);
1278
1279
      GDK_WINDOW_WIN32DATA (window)->hint_max_width = rect.right - rect.left;
      GDK_WINDOW_WIN32DATA (window)->hint_max_height = rect.bottom - rect.top;
1280
1281

      /* Again, check if the window is too large currently. */
1282
      GetClientRect (GDK_DRAWABLE_XID (window), &rect);
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
      if (rect.right > geometry->max_width
	  && rect.bottom > geometry->max_height)
	gdk_window_resize (window, geometry->max_width, geometry->max_height);
      else if (rect.right > geometry->max_width)
	gdk_window_resize (window, geometry->max_width, rect.bottom);
      else if (rect.bottom > geometry->max_height)
	gdk_window_resize (window, rect.right, geometry->max_height);
    }
  
  /* I don't know what to do when called with zero base_width and height. */
  if (geom_mask & GDK_HINT_BASE_SIZE
      && geometry->base_width > 0
      && geometry->base_height > 0)
1296
    if (!GetWindowPlacement (GDK_DRAWABLE_XID (window), &size_hints))
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
      g_warning ("gdk_window_set_hints: GetWindowPlacement failed");
    else
      {
	GDK_NOTE (MISC, g_print ("gdk_window_set_geometry_hints:"
				 " rcNormalPosition: (%d,%d)--(%d,%d)\n",
				 size_hints.rcNormalPosition.left,
				 size_hints.rcNormalPosition.top,
				 size_hints.rcNormalPosition.right,
				 size_hints.rcNormalPosition.bottom));
	size_hints.rcNormalPosition.right =
	  size_hints.rcNormalPosition.left + geometry->base_width;
	size_hints.rcNormalPosition.bottom =
	  size_hints.rcNormalPosition.top + geometry->base_height;
	GDK_NOTE (MISC, g_print ("...setting: rcNormal: (%d,%d)--(%d,%d)\n",
				 size_hints.rcNormalPosition.left,
				 size_hints.rcNormalPosition.top,
				 size_hints.rcNormalPosition.right,
				 size_hints.rcNormalPosition.bottom));
1315
	if (!SetWindowPlacement (GDK_DRAWABLE_XID (window), &size_hints))
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
	  g_warning ("gdk_window_set_hints: SetWindowPlacement failed");
      }
  
  if (geom_mask & GDK_HINT_RESIZE_INC)
    {
      /* XXX */
    }
  
  if (geom_mask & GDK_HINT_ASPECT)
    {
      /* XXX */
    }
}

void
gdk_window_set_title (GdkWindow   *window,
		      const gchar *title)
{
  gint titlelen;
  wchar_t *wcstr;
  gint wlen;
  char *mbstr;

  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  GDK_NOTE (MISC, g_print ("gdk_window_set_title: %#x %s\n",
			   GDK_DRAWABLE_XID (window), title));
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      /* As the title most is in UTF-8 we must translate it
       * to the system codepage.
       */
      titlelen = strlen (title);
      wcstr = g_new (wchar_t, titlelen + 1);
      mbstr = g_new (char, 3*titlelen + 1);
      wlen = gdk_nmbstowchar_ts (wcstr, title, titlelen, titlelen);
      wcstr[wlen] = 0;
      WideCharToMultiByte (GetACP (), 0, wcstr, -1,
			   mbstr, 3*titlelen, NULL, NULL);

      if (!SetWindowText (GDK_DRAWABLE_XID (window), mbstr))
	g_warning ("gdk_window_set_title: SetWindowText failed");

      g_free (mbstr);
      g_free (wcstr);
    }
}

void          
gdk_window_set_role (GdkWindow   *window,
		     const gchar *role)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  GDK_NOTE (MISC, g_print ("gdk_window_set_role: %#x %s\n",
			   GDK_DRAWABLE_XID (window), (role ? role : "NULL")));
  /* XXX */
}

void          
gdk_window_set_transient_for (GdkWindow *window, 
			      GdkWindow *parent)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  GDK_NOTE (MISC, g_print ("gdk_window_set_transient_for: %#x %#x\n",
			   GDK_DRAWABLE_XID (window),
			   GDK_DRAWABLE_XID (parent)));
  /* XXX */
}

void
gdk_window_set_background (GdkWindow *window,
			   GdkColor  *color)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      GDK_NOTE (MISC, g_print ("gdk_window_set_background: %#x %s\n",
1400
			       GDK_DRAWABLE_XID (window), 
1401
1402
			       gdk_color_to_string (color)));

1403
      if (GDK_WINDOW_WIN32DATA (window)->bg_type == GDK_WIN32_BG_PIXMAP)
1404
	{
1405
	  if (GDK_WINDOW_WIN32DATA (window)->bg_pixmap != NULL)
1406
	    {
1407
1408
	      gdk_drawable_unref (GDK_WINDOW_WIN32DATA (window)->bg_pixmap);
	      GDK_WINDOW_WIN32DATA (window)->bg_pixmap = NULL;
1409
	    }
1410
	  GDK_WINDOW_WIN32DATA (window)->bg_type = GDK_WIN32_BG_NORMAL;
1411
	}
1412
1413
      GDK_WINDOW_WIN32DATA (window)->bg_type = GDK_WIN32_BG_PIXEL;
      GDK_WINDOW_WIN32DATA (window)->bg_pixel = *color;
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
    }
}

void
gdk_window_set_back_pixmap (GdkWindow *window,
			    GdkPixmap *pixmap,
			    gint       parent_relative)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
1427
      if (GDK_WINDOW_WIN32DATA (window)->bg_type == GDK_WIN32_BG_PIXMAP)
1428
	{
1429
	  if (GDK_WINDOW_WIN32DATA (window)->bg_pixmap != NULL)
1430
	    {
1431
1432
	      gdk_drawable_unref (GDK_WINDOW_WIN32DATA (window)->bg_pixmap);
	      GDK_WINDOW_WIN32DATA (window)->bg_pixmap = NULL;
1433
	    }
1434
	  GDK_WINDOW_WIN32DATA (window)->bg_type = GDK_WIN32_BG_NORMAL;
1435
1436
1437
	}
      if (parent_relative)
	{
1438
1439
	  GDK_WINDOW_WIN32DATA (window)->bg_type =
	    GDK_WIN32_BG_PARENT_RELATIVE;
1440
1441
1442
1443
1444
1445
1446
	}
      else if (!pixmap)
	{
	  
	}
      else
	{
1447
	  /* We must cache the pixmap in the GdkWindowWin32Data and
1448
1449
	   * paint it each time we get WM_ERASEBKGND
	   */
1450
1451
1452
	  GDK_WINDOW_WIN32DATA (window)->bg_type = GDK_WIN32_BG_PIXMAP;
	  GDK_WINDOW_WIN32DATA (window)->bg_pixmap = pixmap;
	  gdk_drawable_ref (pixmap);
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
	}
    }
}

void
gdk_window_set_cursor (GdkWindow *window,
		       GdkCursor *cursor)
{
  GdkCursorPrivate *cursor_private;
  HCURSOR xcursor;
  
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  cursor_private = (GdkCursorPrivate*) cursor;
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      if (!cursor)
	xcursor = LoadCursor (NULL, IDC_ARROW);
      else
	xcursor = cursor_private->xcursor;

      GDK_NOTE (MISC, g_print ("gdk_window_set_cursor: %#x %#x\n",
1477
1478
			       GDK_DRAWABLE_XID (window), xcursor));
      GDK_WINDOW_WIN32DATA (window)->xcursor = xcursor;
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
    }
}

void
gdk_window_get_geometry (GdkWindow *window,
			 gint      *x,
			 gint      *y,
			 gint      *width,
			 gint      *height,
			 gint      *depth)
{
  g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
  
  if (!window)
1493
    window = gdk_parent_root;
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
  
  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      RECT rect;

      if (!GetClientRect (GDK_DRAWABLE_XID (window), &rect))
	g_warning ("gdk_window_get_geometry: GetClientRect failed");

      if (x)
	*x = rect.left;
      if (y)
	*y = rect.top;
      if (width)
	*width = rect.right - rect.left;
      if (height)
	*height = rect.bottom - rect.top;
      if (depth)
	*depth = gdk_drawable_get_visual (window)->depth;
    }
}

gint
gdk_window_get_origin (GdkWindow *window,
		       gint      *x,
		       gint      *y)
{
  gint return_val;
  gint tx = 0;
  gint ty = 0;

  g_return_val_if_fail (window != NULL, 0);

  if (!GDK_DRAWABLE_DESTROYED (window))
    {
      POINT pt;

      pt.x = 0;
      pt.y = 0;
      ClientToScreen (GDK_DRAWABLE_XID (window), &pt);
      tx = pt.x;
      ty = pt.y;
      return_val = 1;
    }
  else
    return_val = 0;
  
  if (x)
    *x = tx;
  if (y)
    *y = ty;

  GDK_NOTE (MISC, g_print ("gdk_window_get_origin: %#x: +%d+%d\n",
			   GDK_DRAWABLE_XID (window), tx, ty));
  return return_val;
}

gboolean
gdk_window_get_deskrelative_origin (GdkWindow *window,
				    gint      *x,
				    gint      *y)
{
  return gdk_window_get_origin (window, x, y);
}

void
gdk_window_get_root_origin (GdkWindow *window,
			    gint      *x,
			    gint      *y)
{
1563
  GdkWindowPrivate *rover;
1564
1565
1566
1567
1568
  POINT pt;

  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

1569
  rover = (GdkWindowPrivate*) window;
1570
1571
1572
1573
1574
1575
1576
  if (x)
    *x = 0;
  if (y)
    *y = 0;
  if (GDK_DRAWABLE_DESTROYED (window))
    return;
      
1577
1578
1579
  while (rover->parent && ((GdkWindowPrivate*) rover->parent)->parent)
    rover = (GdkWindowPrivate *) rover->parent;
  if (rover->drawable.destroyed)
1580
1581
1582
1583
    return;

  pt.x = 0;
  pt.y = 0;
1584
  ClientToScreen (GDK_DRAWABLE_XID (rover), &pt);
1585
1586
1587
1588
1589
1590
1591
  if (x)
    *x = pt.x;
  if (y)
    *y = pt.y;

  GDK_NOTE (MISC, g_print ("gdk_window_get_root_origin: %#x: (%#x) +%d+%d\n",
			   GDK_DRAWABLE_XID (window),
1592
			   GDK_DRAWABLE_XID (rover), pt.x, pt.y));
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
}

GdkWindow*
gdk_window_get_pointer (GdkWindow       *window,
			gint            *x,
			gint            *y,
			GdkModifierType *mask)
{
  GdkWindow *return_val;
  POINT pointc, point;
  HWND hwnd, hwndc;

  g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
  
  if (!window)
1608
    window = gdk_parent_root;
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671

  return_val = NULL;
  GetCursorPos (&pointc);
  point = pointc;
  ScreenToClient (GDK_DRAWABLE_XID (window), &point);

  if (x)
    *x = point.x;
  if (y)
    *y = point.y;

  hwnd = WindowFromPoint (point);
  point = pointc;
  ScreenToClient (hwnd, &point);
  
  do {
    hwndc = ChildWindowFromPoint (hwnd, point);
    ClientToScreen (hwnd, &point);
    ScreenToClient (hwndc, &point);
  } while (hwndc != hwnd && (hwnd = hwndc, 1));	/* Ouch! */

  return_val = gdk_window_lookup (hwnd);

  if (mask)
    {
      BYTE kbd[256];

      GetKeyboardState (kbd);
      *mask = 0;
      if (kbd[VK_SHIFT] & 0x80)
	*mask |= GDK_SHIFT_MASK;
      if (kbd[VK_CAPITAL] & 0x80)
	*mask |= GDK_LOCK_MASK;
      if (kbd[VK_CONTROL] & 0x80)
	*mask |= GDK_CONTROL_MASK;
      if (kbd[VK_MENU] & 0x80)
	*mask |= GDK_MOD1_MASK;
      if (kbd[VK_LBUTTON] & 0x80)
	*mask |= GDK_BUTTON1_MASK;
      if (kbd[VK_MBUTTON] & 0x80)
	*mask |= GDK_BUTTON2_MASK;
      if (kbd[VK_RBUTTON] & 0x80)
	*mask |= GDK_BUTTON3_MASK;
    }
  
  return return_val;
}

GdkWindow*
gdk_window_at_pointer (gint *win_x,
		       gint *win_y)
{
  GdkWindow *window;
  POINT point, pointc;
  HWND hwnd, hwndc;
  RECT rect;

  GetCursorPos (&pointc);
  point = pointc;
  hwnd = WindowFromPoint (point);

  if (hwnd == NULL)
    {
1672
      window = gdk_parent_root;
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
      if (win_x)
	*win_x = pointc.x;
      if (win_y)
	*win_y = pointc.y;
      return window;
    }
      
  ScreenToClient (hwnd, &point);

  do {
    hwndc = ChildWindowFromPoint (hwnd, point);
    ClientToScreen (hwnd, &point);
    ScreenToClient (hwndc, &point);
  } while (hwndc != hwnd && (hwnd = hwndc, 1));

  window = gdk_window_lookup (hwnd);

  if (window && (win_x || win_y))
    {
      GetClientRect (hwnd, &rect);
      if (win_x)
	*win_x = point.x - rect.left;
      if (win_y)
	*win_y = point.y - rect.top;
    }

  GDK_NOTE (MISC, g_print ("gdk_window_at_pointer: +%d+%d %#x%s\n",
			   point.x, point.y, hwnd,
			   (window == NULL ? " NULL" : "")));

  return window;
}

GList*
gdk_window_get_children (GdkWindow *window)
{
  GdkWindowPrivate *private;
  GList *children;

  g_return_val_if_fail (window != NULL, NULL);
  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);

  if (GDK_DRAWABLE_DESTROYED (window))
    return NULL;

  /* XXX ??? */
  g_warning ("gdk_window_get_children not implemented");
  children = NULL;

  return children;
}

GdkEventMask  
gdk_window_get_events (GdkWindow *window)
{
  g_return_val_if_fail (window != NULL, 0);
  g_return_val_if_fail (GDK_IS_WINDOW (window), 0);

  if (GDK_DRAWABLE_DESTROYED (window))
    return 0;

1734
  return GDK_WINDOW_WIN32DATA (window)->event_mask;
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
}

void          
gdk_window_set_events (GdkWindow   *window,
		       GdkEventMask event_mask)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

  if (GDK_DRAWABLE_DESTROYED (window))
    return;

1747
  GDK_WINDOW_WIN32DATA (window)->event_mask = event_mask;
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
}

void
gdk_window_add_colormap_windows (GdkWindow *window)
{
  g_warning ("gdk_window_add_colormap_windows not implemented");
}

void
gdk_window_shape_combine_mask (GdkWindow *window,
			       GdkBitmap *mask,
			       gint x, gint y)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

  if (!mask)
    {
      GDK_NOTE (MISC, g_print ("gdk_window_shape_combine_mask: %#x none\n",
			       GDK_DRAWABLE_XID (window)));
      SetWindowRgn (GDK_DRAWABLE_XID (window), NULL, TRUE);
    }
  else
    {
      HRGN hrgn;
      DWORD dwStyle;
      DWORD dwExStyle;
      RECT rect;

      /* Convert mask bitmap to region */
1778
      hrgn = BitmapToRegion (GDK_DRAWABLE_XID (mask));
1779
1780
1781

      GDK_NOTE (MISC, g_print ("gdk_window_shape_combine_mask: %#x %#x\n",
			       GDK_DRAWABLE_XID (window),
1782
			       GDK_DRAWABLE_XID (mask)));
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824