summaryrefslogtreecommitdiff
path: root/gdk/x11/gdkgeometry-x11.c
blob: 74c46eb2bf0f41c53a5359fe0d0002791a48e04b (plain)
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
36
37
38
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
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
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
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
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 */

/* gdkgeometry-x11.c: emulation of 32 bit coordinates within the
 * limits of X. 
 *
 * By Owen Taylor <otaylor@redhat.com>
 * Copyright Red Hat, Inc. 2000
 *
 * The algorithms implemented in this file are an extension of the
 * idea of guffaw scrolling, a technique (and name) taken from the classic
 * Netscape source code. The basic idea of guffaw scrolling is a trick
 * to get around a limitation of X: there is no way of scrolling the
 * contents of a window. Guffaw scrolling exploits the X concepts of
 * window gravity and bit gravity:
 *
 *  window gravity: the window gravity of a window affects what happens
 *   to a windows position when _its parent_ is resized, or
 *   moved and resized simultaneously.
 *
 *  bit gravity: the bit gravity of a window affects what happens to
 *  the pixels of a window when _it_ is is resized, or moved and
 *  resized simultaneously.
 *
 * These were basically intended to do things like have right
 * justified widgets in a window automatically stay right justified
 * when the window was resized, but there is also the special
 * "StaticGravity" which means "do nothing." We can exploit
 * StaticGravity to scroll a window:
 *
 *     |  VISIBLE  |
 * 
 *     |abcdefghijk|
 *     |abcdefghijk    |   (1) Resize bigger
 * |    efghijk    |       (2) Move  
 *     |efghijk    |       (3) Move-resize back to the original size
 *
 * Or, going the other way:

 *     |abcdefghijk|
 * |    abcdefghijk|       (1) Move-resize bigger
 *     |    abcdefghijk|   (2) Move  
 *     |    abcdefg|       (4) Resize back to the original size
 *
 * By using this technique, we can simulate scrolling around in a
 * large virtual space without having to actually have windows that
 * big; for the pixels of the window, this is all we have to do.  For
 * subwindows, we have to take care of one other detail - since
 * coordinates in X are limited to 16 bits, subwindows scrolled off
 * will wrap around and come back eventually. So, we have to take care
 * to unmap windows that go outside the 16-bit range and remap them as
 * they come back in.
 *
 * Since we are temporarily making the window bigger, this only looks
 * good if the edges of the window are obscured. Typically, we do
 * this by making the window we are scrolling the immediate child
 * of a "clip window".
 *
 * But, this isn't a perfect API for applications for several reasons:
 *
 *  - We have to use this inefficient technique even for small windows
 *    if the window _could_ be big.
 *  - Applications have to use a special scrolling API.
 *
 * What we'd like is to simply have windows with 32 bit coordinates
 * so applications could scroll in the classic way - just move a big
 * window around.
 *
 * It turns out that StaticGravity can also be used to achieve emulation
 * of 32 bit coordinates with only 16 bit coordinates if we expand
 * our horizons just a bit; what guffaw scrolling really is is a way
 * to move the contents of a window a different amount than we move
 * the borders of of the window. In the above example pictures we
 * ended up with the borders of the window not moving at all, but
 * that isn't necessary.
 *
 * So, what we do is set up a mapping from virtual 32 bit window position/size
 * to:
 *
 *  - Real window position/size
 *  - Offset between virtual coordinates and real coordinates for the window
 *  - Map state (mapped or unmapped)
 *
 * By the following rules:
 *
 *  - If the window is less than 32767 pixels in width (resp. height), we use it's
 *    virtual width and position.
 *  - Otherwise, we use a width of 32767 and determine the position of the window
 *    so that the portion of the real window [16384, 16383] in _toplevel window
 *    coordinates_ is the same as the portion of the real window 
 *
 * This is implemented in gdk_window_compute_position(). Then the algorithm
 * for a moving a window (_window_move_resize_child ()) is:
 * 
 *  - Compute the new window mappings for the window and all subwindows
 *  - Expand out the boundary of the window and all subwindows by the amount
 *    that the real/virtual offset changes for each window. 
 *    (compute_intermediate_position() computes expanded boundary)
 *  - Move the toplevel by the amount that it's contents need to translate.
 *  - Move/resize the window and all subwindows to the newly computed
 *    positions.
 *
 * If we just are scrolling (gdk_window_guffaw_scroll()), then things
 * are similar, except that the final mappings for the toplevel are
 * the same as the initial mappings, but we act as if it moved by the
 * amount we are scrolling by.
 *
 * Note that we don't have to worry about a clip window in
 * _gdk_window_move_resize() since we have set up our translation so
 * that things in the range [16384,16383] in toplevel window
 * coordinates look exactly as they would if we were simply moving the
 * windows, and nothing outside this range is going to be visible
 * unless the user has a _really_ huge screen.
 */

#include <config.h>
#include "gdk.h"		/* For gdk_rectangle_intersect */
#include "gdkprivate-x11.h"
#include "gdkx.h"
#include "gdkregion.h"
#include "gdkinternals.h"
#include "gdkscreen-x11.h"
#include "gdkdisplay-x11.h"
#include "gdkwindow-x11.h"
#include "gdkalias.h"

typedef struct _GdkWindowQueueItem GdkWindowQueueItem;
typedef struct _GdkWindowParentPos GdkWindowParentPos;

typedef enum {
  GDK_WINDOW_QUEUE_TRANSLATE,
  GDK_WINDOW_QUEUE_ANTIEXPOSE
} GdkWindowQueueType;

struct _GdkWindowQueueItem
{
  GdkWindow *window;
  gulong serial;
  GdkWindowQueueType type;
  union {
    struct {
      GdkRegion *area;
      gint dx;
      gint dy;
    } translate;
    struct {
      GdkRegion *area;
    } antiexpose;
  } u;
};

struct _GdkWindowParentPos
{
  gint x;
  gint y;
  gint x11_x;
  gint x11_y;
  GdkRectangle clip_rect;
};

static void gdk_window_compute_position   (GdkWindowImplX11      *window,
					   GdkWindowParentPos *parent_pos,
					   GdkXPositionInfo   *info);
static void gdk_window_compute_parent_pos (GdkWindowImplX11      *window,
					   GdkWindowParentPos *parent_pos);
static void gdk_window_premove            (GdkWindow          *window,
					   GdkWindowParentPos *parent_pos);
static void gdk_window_postmove           (GdkWindow          *window,
					   GdkWindowParentPos *parent_pos);
static void gdk_window_queue_translation  (GdkWindow          *window,
					   GdkRegion	      *area,
					   gint                dx,
					   gint                dy);
static void gdk_window_clip_changed       (GdkWindow          *window,
					   GdkRectangle       *old_clip,
					   GdkRectangle       *new_clip);

void
_gdk_windowing_window_get_offsets (GdkWindow *window,
				   gint      *x_offset,
				   gint      *y_offset)
{
  GdkWindowImplX11 *impl =
    GDK_WINDOW_IMPL_X11 (GDK_WINDOW_OBJECT (window)->impl);

  *x_offset = impl->position_info.x_offset;
  *y_offset = impl->position_info.y_offset;
}

void
_gdk_window_init_position (GdkWindow *window)
{
  GdkWindowParentPos parent_pos;
  GdkWindowImplX11 *impl;
  
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  impl =
    GDK_WINDOW_IMPL_X11 (GDK_WINDOW_OBJECT (window)->impl);
  
  gdk_window_compute_parent_pos (impl, &parent_pos);
  gdk_window_compute_position (impl, &parent_pos, &impl->position_info);
}

static void
gdk_window_copy_area_scroll (GdkWindow    *window,
			     GdkRectangle *dest_rect,
			     gint          dx,
			     gint          dy)
{
  GdkWindowObject *obj = GDK_WINDOW_OBJECT (window);
  GList *l;

  if (dest_rect->width > 0 && dest_rect->height > 0)
    {
      GdkGC *gc;

      gc = _gdk_drawable_get_scratch_gc (window, TRUE);
      
      gdk_window_queue_translation (window, NULL, dx, dy);
   
      XCopyArea (GDK_WINDOW_XDISPLAY (window),
		 GDK_WINDOW_XID (window),
		 GDK_WINDOW_XID (window),
		 gdk_x11_gc_get_xgc (gc),
		 dest_rect->x - dx, dest_rect->y - dy,
		 dest_rect->width, dest_rect->height,
		 dest_rect->x, dest_rect->y);
    }

  for (l = obj->children; l; l = l->next)
    {
      GdkWindow *child = (GdkWindow*) l->data;
      GdkWindowObject *child_obj = GDK_WINDOW_OBJECT (child);

      gdk_window_move (child, child_obj->x + dx, child_obj->y + dy);
    }
}

static void
compute_intermediate_position (GdkXPositionInfo *position_info,
			       GdkXPositionInfo *new_info,
			       gint              d_xoffset,
			       gint              d_yoffset,
			       GdkRectangle     *new_position)
{
  gint new_x0, new_x1, new_y0, new_y1;
  
  /* Wrap d_xoffset, d_yoffset into [-32768,32767] range. For the
   * purposes of subwindow movement, it doesn't matter if we are
   * off by a factor of 65536, and if we don't do this range
   * reduction, we'll end up with invalid widths.
   */
  d_xoffset = (gint16)d_xoffset;
  d_yoffset = (gint16)d_yoffset;
  
  if (d_xoffset < 0)
    {
      new_x0 = position_info->x + d_xoffset;
      new_x1 = position_info->x + position_info->width;
    }
  else
    {
      new_x0 = position_info->x;
      new_x1 = position_info->x + new_info->width + d_xoffset;
    }

  new_position->x = new_x0;
  new_position->width = new_x1 - new_x0;
  
  if (d_yoffset < 0)
    {
      new_y0 = position_info->y + d_yoffset;
      new_y1 = position_info->y + position_info->height;
    }
  else
    {
      new_y0 = position_info->y;
      new_y1 = position_info->y + new_info->height + d_yoffset;
    }
  
  new_position->y = new_y0;
  new_position->height = new_y1 - new_y0;
}

static void
translate_pos (GdkWindowParentPos *dest, GdkWindowParentPos *src,
               GdkWindowObject *obj, GdkXPositionInfo *pos_info,
               gboolean set_clip)
{
  dest->x = src->x + obj->x;
  dest->y = src->y + obj->y;
  dest->x11_x = src->x11_x + pos_info->x;
  dest->x11_y = src->x11_y + pos_info->y;

  if (set_clip)
      dest->clip_rect = pos_info->clip_rect;
}

static void
move (GdkWindow *window, GdkXPositionInfo *pos)
{
  XMoveWindow (GDK_WINDOW_XDISPLAY (window),
               GDK_WINDOW_XID (window), pos->x, pos->y);
}

static void
move_relative (GdkWindow *window, GdkRectangle *rect,
               gint dx, gint dy)
{
  XMoveWindow (GDK_WINDOW_XDISPLAY (window),
               GDK_WINDOW_XID (window),
               rect->x + dx, rect->y + dy);
}

static void
move_resize (GdkWindow *window, GdkRectangle *pos)
{
  XMoveResizeWindow (GDK_WINDOW_XDISPLAY (window),
                     GDK_WINDOW_XID (window),
                     pos->x, pos->y, pos->width, pos->height);
}

static void
gdk_window_guffaw_scroll (GdkWindow    *window,
			  gint          dx,
			  gint          dy)
{
  GdkWindowObject *obj = GDK_WINDOW_OBJECT (window);
  GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (obj->impl);

  gint d_xoffset = -dx;
  gint d_yoffset = -dy;
  GdkRectangle new_position;
  GdkXPositionInfo new_info;
  GdkWindowParentPos parent_pos;
  GList *l;
  
  gdk_window_compute_parent_pos (impl, &parent_pos);
  gdk_window_compute_position (impl, &parent_pos, &new_info);

  translate_pos (&parent_pos, &parent_pos, obj, &new_info, TRUE);

  _gdk_x11_window_tmp_unset_bg (window, FALSE);;

  if (dx > 0 || dy > 0)
    gdk_window_queue_translation (window, NULL, MAX (dx, 0), MAX (dy, 0));
	
  gdk_window_set_static_gravities (window, TRUE);

  compute_intermediate_position (&impl->position_info, &new_info, d_xoffset, d_yoffset,
				 &new_position);
  
  move_resize (window, &new_position);
  
  for (l = obj->children; l; l = l->next)
    {
      GdkWindow *child = (GdkWindow*) l->data;
      GdkWindowObject *child_obj = GDK_WINDOW_OBJECT (child);

      child_obj->x -= d_xoffset;
      child_obj->y -= d_yoffset;

      gdk_window_premove (child, &parent_pos);
    }
  
  move_relative (window, &new_position, -d_xoffset, -d_yoffset);
  
  if (dx < 0 || dy < 0)
    gdk_window_queue_translation (window, NULL, MIN (dx, 0), MIN (dy, 0));
  
  move_resize (window, (GdkRectangle *) &impl->position_info);
  
  if (impl->position_info.no_bg)
    _gdk_x11_window_tmp_reset_bg (window, FALSE);
  
  impl->position_info = new_info;
  
  g_list_foreach (obj->children, (GFunc) gdk_window_postmove, &parent_pos);
}

/**
 * gdk_window_scroll:
 * @window: a #GdkWindow
 * @dx: Amount to scroll in the X direction
 * @dy: Amount to scroll in the Y direction
 * 
 * Scroll the contents of @window, both pixels and children, by the given
 * amount. @window itself does not move.  Portions of the window that the scroll
 * operation brings in from offscreen areas are invalidated. The invalidated
 * region may be bigger than what would strictly be necessary.  (For X11, a
 * minimum area will be invalidated if the window has no subwindows, or if the
 * edges of the window's parent do not extend beyond the edges of the window. In
 * other cases, a multi-step process is used to scroll the window which may
 * produce temporary visual artifacts and unnecessary invalidations.)
 **/
void
gdk_window_scroll (GdkWindow *window,
		   gint       dx,
		   gint       dy)
{
  gboolean can_guffaw_scroll = FALSE;
  GdkRegion *invalidate_region;
  GdkWindowImplX11 *impl;
  GdkWindowObject *obj;
  GdkRectangle src_rect, dest_rect;
  
  g_return_if_fail (GDK_IS_WINDOW (window));

  if (GDK_WINDOW_DESTROYED (window))
    return;
  
  obj = GDK_WINDOW_OBJECT (window);
  impl = GDK_WINDOW_IMPL_X11 (obj->impl);  

  if (dx == 0 && dy == 0)
    return;
  
  /* Move the current invalid region */
  if (obj->update_area)
    gdk_region_offset (obj->update_area, dx, dy);
  
  /* impl->position_info.clip_rect isn't meaningful for toplevels */
  if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
    src_rect = impl->position_info.clip_rect;
  else
    {
      src_rect.x = 0;
      src_rect.y = 0;
      src_rect.width = impl->width;
      src_rect.height = impl->height;
    }
  
  invalidate_region = gdk_region_rectangle (&src_rect);

  dest_rect = src_rect;
  dest_rect.x += dx;
  dest_rect.y += dy;
  gdk_rectangle_intersect (&dest_rect, &src_rect, &dest_rect);

  if (dest_rect.width > 0 && dest_rect.height > 0)
    {
      GdkRegion *tmp_region;

      tmp_region = gdk_region_rectangle (&dest_rect);
      gdk_region_subtract (invalidate_region, tmp_region);
      gdk_region_destroy (tmp_region);
    }
  
  gdk_window_invalidate_region (window, invalidate_region, TRUE);
  gdk_region_destroy (invalidate_region);

  /* We can guffaw scroll if we are a child window, and the parent
   * does not extend beyond our edges. Otherwise, we use XCopyArea, then
   * move any children later
   */
  if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
    {
      GdkWindowImplX11 *parent_impl = GDK_WINDOW_IMPL_X11 (obj->parent->impl);  
      can_guffaw_scroll = ((dx == 0 || (obj->x <= 0 && obj->x + impl->width >= parent_impl->width)) &&
			   (dy == 0 || (obj->y <= 0 && obj->y + impl->height >= parent_impl->height)));
    }

  if (!obj->children || !can_guffaw_scroll)
    gdk_window_copy_area_scroll (window, &dest_rect, dx, dy);
  else
    gdk_window_guffaw_scroll (window, dx, dy);
}

/**
 * gdk_window_move_region:
 * @window: a #GdkWindow
 * @region: The #GdkRegion to move
 * @dx: Amount to move in the X direction
 * @dy: Amount to move in the Y direction
 * 
 * Move the part of @window indicated by @region by @dy pixels in the Y 
 * direction and @dx pixels in the X direction. The portions of @region 
 * that not covered by the new position of @region are invalidated.
 * 
 * Child windows are not moved.
 * 
 * Since: 2.8
 **/
void
gdk_window_move_region (GdkWindow       *window,
			const GdkRegion *region,
			gint             dx,
			gint             dy)
{
  GdkWindowImplX11 *impl;
  GdkWindowObject *private;
  GdkRegion *window_clip;
  GdkRegion *src_region;
  GdkRegion *brought_in;
  GdkRegion *dest_region;
  GdkRegion *moving_invalid_region;
  GdkRectangle dest_extents;
  GdkGC *gc;
  
  g_return_if_fail (GDK_IS_WINDOW (window));
  g_return_if_fail (region != NULL);
  
  if (GDK_WINDOW_DESTROYED (window))
    return;
  
  private = GDK_WINDOW_OBJECT (window);
  impl = GDK_WINDOW_IMPL_X11 (private->impl);  

  if (dx == 0 && dy == 0)
    return;

  window_clip = gdk_region_rectangle (&impl->position_info.clip_rect);

  /* compute source regions */
  src_region = gdk_region_copy (region);
  brought_in = gdk_region_copy (region);
  gdk_region_intersect (src_region, window_clip);

  gdk_region_subtract (brought_in, src_region);
  gdk_region_offset (brought_in, dx, dy);

  /* compute destination regions */
  dest_region = gdk_region_copy (src_region);
  gdk_region_offset (dest_region, dx, dy);
  gdk_region_intersect (dest_region, window_clip);
  gdk_region_get_clipbox (dest_region, &dest_extents);

  gdk_region_destroy (window_clip);

  /* calculating moving part of current invalid area */
  moving_invalid_region = NULL;
  if (private->update_area)
    {
      moving_invalid_region = gdk_region_copy (private->update_area);
      gdk_region_intersect (moving_invalid_region, src_region);
      gdk_region_offset (moving_invalid_region, dx, dy);
    }
  
  /* invalidate all of the src region */
  gdk_window_invalidate_region (window, src_region, FALSE);

  /* un-invalidate destination region */
  if (private->update_area)
    gdk_region_subtract (private->update_area, dest_region);
  
  /* invalidate moving parts of existing update area */
  if (moving_invalid_region)
    {
      gdk_window_invalidate_region (window, moving_invalid_region, FALSE);
      gdk_region_destroy (moving_invalid_region);
    }

  /* invalidate area brought in from off-screen */
  gdk_window_invalidate_region (window, brought_in, FALSE);
  gdk_region_destroy (brought_in);

  /* Actually do the moving */
  gdk_window_queue_translation (window, src_region, dx, dy);

  gc = _gdk_drawable_get_scratch_gc (window, TRUE);
  gdk_gc_set_clip_region (gc, dest_region);
  
  XCopyArea (GDK_WINDOW_XDISPLAY (window),
	     GDK_WINDOW_XID (window),
	     GDK_WINDOW_XID (window),
	     GDK_GC_XGC (gc),
	     dest_extents.x - dx, dest_extents.y - dy,
	     dest_extents.width, dest_extents.height,
	     dest_extents.x, dest_extents.y);

  /* Unset clip region of cached GC */
  gdk_gc_set_clip_region (gc, NULL);
  
  gdk_region_destroy (src_region);
  gdk_region_destroy (dest_region);
}

static void
reset_backgrounds (GdkWindow *window)
{
  GdkWindowObject *obj = (GdkWindowObject *)window;

  _gdk_x11_window_tmp_reset_bg (window, FALSE);
  
  if (obj->parent)
    _gdk_x11_window_tmp_reset_bg ((GdkWindow *)obj->parent, FALSE);
}

static void
map_if_needed (GdkWindow *window, GdkXPositionInfo *pos_info)
{
  GdkWindowObject *obj = (GdkWindowObject *) window;
  GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (obj->impl);

  if (!impl->position_info.mapped && pos_info->mapped && GDK_WINDOW_IS_MAPPED (obj))
    XMapWindow (GDK_DRAWABLE_XDISPLAY (window), GDK_DRAWABLE_XID (window));
}

static void
unmap_if_needed (GdkWindow *window, GdkXPositionInfo *pos_info)
{
  GdkWindowObject *obj = (GdkWindowObject *) window;
  GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (obj->impl);

  if (impl->position_info.mapped && !pos_info->mapped)
    XUnmapWindow (GDK_DRAWABLE_XDISPLAY (window), GDK_DRAWABLE_XID (window));
}

void
_gdk_window_move_resize_child (GdkWindow *window,
			       gint       x,
			       gint       y,
			       gint       width,
			       gint       height)
{
  GdkWindowImplX11 *impl;
  GdkWindowObject *obj;
  GdkXPositionInfo new_info;
  GdkWindowParentPos parent_pos;
  
  gint d_xoffset, d_yoffset;
  gint dx, dy;
  gboolean is_move;
  gboolean is_resize;

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

  impl = GDK_WINDOW_IMPL_X11 (GDK_WINDOW_OBJECT (window)->impl);
  obj = GDK_WINDOW_OBJECT (window);

  dx = x - obj->x;
  dy = y - obj->y;
  
  is_move = dx != 0 || dy != 0;
  is_resize = impl->width != width || impl->height != height;

  if (!is_move && !is_resize)
    return;
  
  old_pos.x = obj->x;
  old_pos.y = obj->y;
  old_pos.width = impl->width;
  old_pos.height = impl->height;

  obj->x = x;
  obj->y = y;
  impl->width = width;
  impl->height = height;

  gdk_window_compute_parent_pos (impl, &parent_pos);
  gdk_window_compute_position (impl, &parent_pos, &new_info);

  gdk_window_clip_changed (window, &impl->position_info.clip_rect, &new_info.clip_rect);

  translate_pos (&parent_pos, &parent_pos, obj, &new_info, TRUE);

  d_xoffset = new_info.x_offset - impl->position_info.x_offset;
  d_yoffset = new_info.y_offset - impl->position_info.y_offset;
  
  if (d_xoffset != 0 || d_yoffset != 0)
    {
      GdkRectangle new_position;

      gdk_window_set_static_gravities (window, TRUE);

      if (d_xoffset < 0 || d_yoffset < 0)
	gdk_window_queue_translation (window, NULL, MIN (d_xoffset, 0), MIN (d_yoffset, 0));

      compute_intermediate_position (&impl->position_info, &new_info, d_xoffset, d_yoffset,
				     &new_position);
      
      move_resize (window, &new_position);
      
      g_list_foreach (obj->children, (GFunc) gdk_window_premove, &parent_pos);

      move_relative (window, &new_position, dx, dy);
      
      if (d_xoffset > 0 || d_yoffset > 0)
	gdk_window_queue_translation (window, NULL, MAX (d_xoffset, 0), MAX (d_yoffset, 0));
      
      move_resize (window, (GdkRectangle *) &new_info);

      reset_backgrounds (window);

      map_if_needed (window, &new_info);
      
      impl->position_info = new_info;
      
      g_list_foreach (obj->children, (GFunc) gdk_window_postmove, &parent_pos);
    }
  else
    {
      if (is_move && is_resize)
	gdk_window_set_static_gravities (window, FALSE);

      unmap_if_needed (window, &new_info);
      
      g_list_foreach (obj->children, (GFunc) gdk_window_premove, &parent_pos);

      if (is_resize)
          move_resize (window, (GdkRectangle *) &new_info);
      else
          move (window, &new_info);

      g_list_foreach (obj->children, (GFunc) gdk_window_postmove, &parent_pos);

      reset_backgrounds (window);
      
      map_if_needed (window, &new_info);

      impl->position_info = new_info;
    }

  if (GDK_WINDOW_IS_MAPPED (obj) && obj->parent && !obj->input_only)
    gdk_window_invalidate_rect ((GdkWindow *)obj->parent, &old_pos, FALSE);
}

static void
gdk_window_compute_position (GdkWindowImplX11   *window,
			     GdkWindowParentPos *parent_pos,
			     GdkXPositionInfo   *info)
{
  GdkWindowObject *wrapper;
  int parent_x_offset;
  int parent_y_offset;

  g_return_if_fail (GDK_IS_WINDOW_IMPL_X11 (window));

  wrapper = GDK_WINDOW_OBJECT (GDK_DRAWABLE_IMPL_X11 (window)->wrapper);
  
  info->big = FALSE;
  
  if (window->width <= 32767)
    {
      info->width = window->width;
      info->x = parent_pos->x + wrapper->x - parent_pos->x11_x;
    }
  else
    {
      info->big = TRUE;
      info->width = 32767;
      if (parent_pos->x + wrapper->x < -16384)
	{
	  if (parent_pos->x + wrapper->x + window->width < 16384)
	    info->x = parent_pos->x + wrapper->x + window->width - info->width - parent_pos->x11_x;
	  else
	    info->x = -16384 - parent_pos->x11_x;
	}
      else
	info->x = parent_pos->x + wrapper->x - parent_pos->x11_x;
    }

  if (window->height <= 32767)
    {
      info->height = window->height;
      info->y = parent_pos->y + wrapper->y - parent_pos->x11_y;
    }
  else
    {
      info->big = TRUE;
      info->height = 32767;
      if (parent_pos->y + wrapper->y < -16384)
	{
	  if (parent_pos->y + wrapper->y + window->height < 16384)
	    info->y = parent_pos->y + wrapper->y + window->height - info->height - parent_pos->x11_y;
	  else
	    info->y = -16384 - parent_pos->x11_y;
	}
      else
	info->y = parent_pos->y + wrapper->y - parent_pos->x11_y;
    }

  parent_x_offset = parent_pos->x11_x - parent_pos->x;
  parent_y_offset = parent_pos->x11_y - parent_pos->y;
  
  info->x_offset = parent_x_offset + info->x - wrapper->x;
  info->y_offset = parent_y_offset + info->y - wrapper->y;

  /* We don't considering the clipping of toplevel windows and their immediate children
   * by their parents, and simply always map those windows.
   */
  if (parent_pos->clip_rect.width == G_MAXINT)
    info->mapped = TRUE;
  /* Check if the window would wrap around into the visible space in either direction */
  else if (info->x + parent_x_offset < parent_pos->clip_rect.x + parent_pos->clip_rect.width - 65536 ||
      info->x + info->width + parent_x_offset > parent_pos->clip_rect.x + 65536 ||
      info->y + parent_y_offset < parent_pos->clip_rect.y + parent_pos->clip_rect.height - 65536 ||
      info->y + info->height + parent_y_offset  > parent_pos->clip_rect.y + 65536)
    info->mapped = FALSE;
  else
    info->mapped = TRUE;

  info->no_bg = FALSE;

  if (GDK_WINDOW_TYPE (wrapper) == GDK_WINDOW_CHILD)
    {
      info->clip_rect.x = wrapper->x;
      info->clip_rect.y = wrapper->y;
      info->clip_rect.width = window->width;
      info->clip_rect.height = window->height;
      
      gdk_rectangle_intersect (&info->clip_rect, &parent_pos->clip_rect, &info->clip_rect);

      info->clip_rect.x -= wrapper->x;
      info->clip_rect.y -= wrapper->y;
    }
  else
    {
      info->clip_rect.x = 0;
      info->clip_rect.y = 0;
      info->clip_rect.width = G_MAXINT;
      info->clip_rect.height = G_MAXINT;
    }
}

static void
gdk_window_compute_parent_pos (GdkWindowImplX11      *window,
			       GdkWindowParentPos *parent_pos)
{
  GdkWindowObject *wrapper;
  GdkWindowObject *parent;
  GdkRectangle tmp_clip;
  
  int clip_xoffset = 0;
  int clip_yoffset = 0;

  g_return_if_fail (GDK_IS_WINDOW_IMPL_X11 (window));

  wrapper =
    GDK_WINDOW_OBJECT (GDK_DRAWABLE_IMPL_X11 (window)->wrapper);
  
  parent_pos->x = 0;
  parent_pos->y = 0;
  parent_pos->x11_x = 0;
  parent_pos->x11_y = 0;

  /* We take a simple approach here and simply consider toplevel
   * windows not to clip their children on the right/bottom, since the
   * size of toplevel windows is not directly under our
   * control. Clipping only really matters when scrolling and
   * generally we aren't going to be moving the immediate child of a
   * toplevel beyond the bounds of that toplevel.
   *
   * We could go ahead and recompute the clips of toplevel windows and
   * their descendents when we receive size notification, but it would
   * probably not be an improvement in most cases.
   */
  parent_pos->clip_rect.x = 0;
  parent_pos->clip_rect.y = 0;
  parent_pos->clip_rect.width = G_MAXINT;
  parent_pos->clip_rect.height = G_MAXINT;

  parent = (GdkWindowObject *)wrapper->parent;
  while (parent && parent->window_type == GDK_WINDOW_CHILD)
    {
      GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (parent->impl);
      
      tmp_clip.x = - clip_xoffset;
      tmp_clip.y = - clip_yoffset;
      tmp_clip.width = impl->width;
      tmp_clip.height = impl->height;

      gdk_rectangle_intersect (&parent_pos->clip_rect, &tmp_clip, &parent_pos->clip_rect);

      translate_pos (parent_pos, parent_pos, parent,
                     &impl->position_info, FALSE);

      clip_xoffset += parent->x;
      clip_yoffset += parent->y;

      parent = (GdkWindowObject *)parent->parent;
    }
}

static void
gdk_window_premove (GdkWindow          *window,
		    GdkWindowParentPos *parent_pos)
{
  GdkWindowImplX11 *impl;
  GdkWindowObject *obj;
  GdkXPositionInfo new_info;
  gint d_xoffset, d_yoffset;
  GdkWindowParentPos this_pos;

  obj = (GdkWindowObject *) window;
  impl = GDK_WINDOW_IMPL_X11 (obj->impl);
  
  gdk_window_compute_position (impl, parent_pos, &new_info);

  gdk_window_clip_changed (window, &impl->position_info.clip_rect, &new_info.clip_rect);

  translate_pos (&this_pos, parent_pos, obj, &new_info, TRUE);

  unmap_if_needed (window, &new_info);

  d_xoffset = new_info.x_offset - impl->position_info.x_offset;
  d_yoffset = new_info.y_offset - impl->position_info.y_offset;
  
  if (d_xoffset != 0 || d_yoffset != 0)
    {
      GdkRectangle new_position;

      if (d_xoffset < 0 || d_yoffset < 0)
	gdk_window_queue_translation (window, NULL, MIN (d_xoffset, 0), MIN (d_yoffset, 0));

      compute_intermediate_position (&impl->position_info, &new_info, d_xoffset, d_yoffset,
				     &new_position);

      move_resize (window, &new_position);
    }

  g_list_foreach (obj->children, (GFunc) gdk_window_premove, &this_pos);
}

static void
gdk_window_postmove (GdkWindow          *window,
		     GdkWindowParentPos *parent_pos)
{
  GdkWindowImplX11 *impl;
  GdkWindowObject *obj;
  GdkXPositionInfo new_info;
  gint d_xoffset, d_yoffset;
  GdkWindowParentPos this_pos;

  obj = (GdkWindowObject *) window;
  impl = GDK_WINDOW_IMPL_X11 (obj->impl);
  
  gdk_window_compute_position (impl, parent_pos, &new_info);

  translate_pos (&this_pos, parent_pos, obj, &new_info, TRUE);

  d_xoffset = new_info.x_offset - impl->position_info.x_offset;
  d_yoffset = new_info.y_offset - impl->position_info.y_offset;
  
  if (d_xoffset != 0 || d_yoffset != 0)
    {
      if (d_xoffset > 0 || d_yoffset > 0)
	gdk_window_queue_translation (window, NULL, MAX (d_xoffset, 0), MAX (d_yoffset, 0));
	
      move_resize (window, (GdkRectangle *) &new_info);
    }

  map_if_needed (window, &new_info);

  reset_backgrounds (window);

  impl->position_info = new_info;

  g_list_foreach (obj->children, (GFunc) gdk_window_postmove, &this_pos);
}

static Bool
expose_serial_predicate (Display *xdisplay,
			 XEvent  *xev,
			 XPointer arg)
{
  gulong *serial = (gulong *)arg;

  if (xev->xany.type == Expose)
    *serial = MIN (*serial, xev->xany.serial);

  return False;
}

/* Find oldest possible serial for an outstanding expose event
 */
static gulong
find_current_serial (Display *xdisplay)
{
  XEvent xev;
  gulong serial = NextRequest (xdisplay);
  
  XSync (xdisplay, False);

  XCheckIfEvent (xdisplay, &xev, expose_serial_predicate, (XPointer)&serial);

  return serial;
}

static void
queue_delete_link (GQueue *queue,
		   GList  *link)
{
  if (queue->tail == link)
    queue->tail = link->prev;
  
  queue->head = g_list_remove_link (queue->head, link);
  g_list_free_1 (link);
  queue->length--;
}

static void
queue_item_free (GdkWindowQueueItem *item)
{
  if (item->window)
    {
      g_object_remove_weak_pointer (G_OBJECT (item->window),
				    (gpointer *)&(item->window));
    }
  
  if (item->type == GDK_WINDOW_QUEUE_ANTIEXPOSE)
    gdk_region_destroy (item->u.antiexpose.area);
  else
    {
      if (item->u.translate.area)
	gdk_region_destroy (item->u.translate.area);
    }
  
  g_free (item);
}

static void
gdk_window_queue (GdkWindow          *window,
		  GdkWindowQueueItem *item)
{
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
  
  if (!display_x11->translate_queue)
    display_x11->translate_queue = g_queue_new ();

  /* Keep length of queue finite by, if it grows too long,
   * figuring out the latest relevant serial and discarding
   * irrelevant queue items.
   */
  if (display_x11->translate_queue->length >= 64)
    {
      gulong serial = find_current_serial (GDK_WINDOW_XDISPLAY (window));
      GList *tmp_list = display_x11->translate_queue->head;
      
      while (tmp_list)
	{
	  GdkWindowQueueItem *item = tmp_list->data;
	  GList *next = tmp_list->next;
	  
	  /* an overflow-safe (item->serial < serial) */
	  if (item->serial - serial > (gulong) G_MAXLONG)
	    {
	      queue_delete_link (display_x11->translate_queue, tmp_list);
	      queue_item_free (item);
	    }

	  tmp_list = next;
	}
    }

  /* Catch the case where someone isn't processing events and there
   * is an event stuck in the event queue with an old serial:
   * If we can't reduce the queue length by the above method,
   * discard anti-expose items. (We can't discard translate
   * items 
   */
  if (display_x11->translate_queue->length >= 64)
    {
      GList *tmp_list = display_x11->translate_queue->head;
      
      while (tmp_list)
	{
	  GdkWindowQueueItem *item = tmp_list->data;
	  GList *next = tmp_list->next;
	  
	  if (item->type == GDK_WINDOW_QUEUE_ANTIEXPOSE)
	    {
	      queue_delete_link (display_x11->translate_queue, tmp_list);
	      queue_item_free (item);
	    }

	  tmp_list = next;
	}
    }

  item->window = window;
  item->serial = NextRequest (GDK_WINDOW_XDISPLAY (window));
  
  g_object_add_weak_pointer (G_OBJECT (window),
			     (gpointer *)&(item->window));

  g_queue_push_tail (display_x11->translate_queue, item);
}

static void
gdk_window_queue_translation (GdkWindow *window,
			      GdkRegion *area,
			      gint       dx,
			      gint       dy)
{
  GdkWindowQueueItem *item = g_new (GdkWindowQueueItem, 1);
  item->type = GDK_WINDOW_QUEUE_TRANSLATE;
  item->u.translate.area = area ? gdk_region_copy (area) : NULL;
  item->u.translate.dx = dx;
  item->u.translate.dy = dy;

  gdk_window_queue (window, item);
}

gboolean
_gdk_windowing_window_queue_antiexpose (GdkWindow *window,
					GdkRegion *area)
{
  GdkWindowQueueItem *item = g_new (GdkWindowQueueItem, 1);
  item->type = GDK_WINDOW_QUEUE_ANTIEXPOSE;
  item->u.antiexpose.area = area;

  gdk_window_queue (window, item);

  return TRUE;
}

void
_gdk_window_process_expose (GdkWindow    *window,
			    gulong        serial,
			    GdkRectangle *area)
{
  GdkWindowImplX11 *impl;
  GdkRegion *invalidate_region = gdk_region_rectangle (area);
  GdkRegion *clip_region;
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
  impl = GDK_WINDOW_IMPL_X11 (GDK_WINDOW_OBJECT (window)->impl);

  if (display_x11->translate_queue)
    {
      GList *tmp_list = display_x11->translate_queue->head;
      
      while (tmp_list)
	{
	  GdkWindowQueueItem *item = tmp_list->data;
          GList *next = tmp_list->next;
	  
	  /* an overflow-safe (serial < item->serial) */
	  if (serial - item->serial > (gulong) G_MAXLONG)
	    {
	      if (item->window == window)
		{
		  if (item->type == GDK_WINDOW_QUEUE_TRANSLATE)
		    {
		      if (item->u.translate.area)
			{
			  GdkRegion *intersection;
			  
			  intersection = gdk_region_copy (invalidate_region);
			  gdk_region_intersect (intersection, item->u.translate.area);
			  gdk_region_subtract (invalidate_region, intersection);
			  gdk_region_offset (intersection, item->u.translate.dx, item->u.translate.dy);
			  gdk_region_union (invalidate_region, intersection);
			  gdk_region_destroy (intersection);
			}
		      else
			gdk_region_offset (invalidate_region, item->u.translate.dx, item->u.translate.dy);
		    }
		  else		/* anti-expose */
		    {
		      gdk_region_subtract (invalidate_region, item->u.antiexpose.area);
		    }
		}
	    }
	  else
	    {
	      queue_delete_link (display_x11->translate_queue, tmp_list);
	      queue_item_free (item);
	    }
	  tmp_list = next;
	}
    }

  clip_region = gdk_region_rectangle (&impl->position_info.clip_rect);
  gdk_region_intersect (invalidate_region, clip_region);

  if (!gdk_region_empty (invalidate_region))
    gdk_window_invalidate_region (window, invalidate_region, FALSE);
  
  gdk_region_destroy (invalidate_region);
  gdk_region_destroy (clip_region);
}

static void
gdk_window_clip_changed (GdkWindow *window, GdkRectangle *old_clip, GdkRectangle *new_clip)
{
  GdkWindowImplX11 *impl;
  GdkWindowObject *obj;
  GdkRegion *old_clip_region;
  GdkRegion *new_clip_region;
  
  if (((GdkWindowObject *)window)->input_only)
    return;

  obj = (GdkWindowObject *) window;
  impl = GDK_WINDOW_IMPL_X11 (obj->impl);
  
  old_clip_region = gdk_region_rectangle (old_clip);
  new_clip_region = gdk_region_rectangle (new_clip);

  /* We need to update this here because gdk_window_invalidate_region makes
   * use if it (through gdk_drawable_get_visible_region
   */
  impl->position_info.clip_rect = *new_clip;
  
  /* Trim invalid region of window to new clip rectangle
   */
  if (obj->update_area)
    gdk_region_intersect (obj->update_area, new_clip_region);

  /* Invalidate newly exposed portion of window
   */
  gdk_region_subtract (new_clip_region, old_clip_region);
  if (!gdk_region_empty (new_clip_region))
    {
      _gdk_x11_window_tmp_unset_bg (window, FALSE);;
      gdk_window_invalidate_region (window, new_clip_region, FALSE);
    }

  if (obj->parent)
    _gdk_x11_window_tmp_unset_bg ((GdkWindow *)obj->parent, FALSE);
  
  gdk_region_destroy (new_clip_region);
  gdk_region_destroy (old_clip_region);
}

#define __GDK_GEOMETRY_X11_C__
#include "gdkaliasdef.c"