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
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
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
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
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
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
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
|
/* $XFree86: xc/programs/twm/add_window.c,v 1.13 2003/08/04 10:32:30 eich Exp $ */
/*****************************************************************************/
/*
Copyright 1989,1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*/
/** Copyright 1988 by Evans & Sutherland Computer Corporation, **/
/** Salt Lake City, Utah **/
/** Cambridge, Massachusetts **/
/** **/
/** All Rights Reserved **/
/** **/
/** Permission to use, copy, modify, and distribute this software and **/
/** its documentation for any purpose and without fee is hereby **/
/** granted, provided that the above copyright notice appear in all **/
/** copies and that both that copyright notice and this permis- **/
/** sion notice appear in supporting documentation, and that the **/
/** name of Evans & Sutherland not be used in advertising **/
/** in publicity pertaining to distribution of the software without **/
/** specific, written prior permission. **/
/** **/
/** EVANS & SUTHERLAND DISCLAIMs ALL WARRANTIES WITH REGARD **/
/** TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- **/
/** ABILITY AND FITNESS, IN NO EVENT SHALL EVANS & SUTHERLAND **/
/** BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAM- **/
/** AGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA **/
/** OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER **/
/** TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE **/
/** OR PERFORMANCE OF THIS SOFTWARE. **/
/*****************************************************************************/
/**********************************************************************
*
* $Xorg: add_window.c,v 1.5 2001/02/09 02:05:36 xorgcvs Exp $
*
* Add a new window, put the titlbar and other stuff around
* the window
*
* 31-Mar-88 Tom LaStrange Initial Version.
*
**********************************************************************/
#include <stdio.h>
#include "twm.h"
#include <X11/Xatom.h>
#include "util.h"
#include "resize.h"
#include "parse.h"
#include "list.h"
#include "events.h"
#include "menus.h"
#include "screen.h"
#include "iconmgr.h"
#include "session.h"
#include "add_window.h"
#define gray_width 2
#define gray_height 2
static char gray_bits[] = {
0x02, 0x01};
int AddingX;
int AddingY;
int AddingW;
int AddingH;
static int PlaceX = 50;
static int PlaceY = 50;
static void do_add_binding ( int button, int context, int modifier, int func );
static Window CreateHighlightWindow ( TwmWindow *tmp_win );
static void CreateWindowTitlebarButtons ( TwmWindow *tmp_win );
char NoName[] = "Untitled"; /* name if no name is specified */
/************************************************************************
*
* Procedure:
* GetGravityOffsets - map gravity to (x,y) offset signs for adding
* to x and y when window is mapped to get proper placement.
*
************************************************************************
*/
void
GetGravityOffsets (tmp, xp, yp)
TwmWindow *tmp; /* window from which to get gravity */
int *xp, *yp; /* return values */
{
static struct _gravity_offset {
int x, y;
} gravity_offsets[11] = {
{ 0, 0 }, /* ForgetGravity */
{ -1, -1 }, /* NorthWestGravity */
{ 0, -1 }, /* NorthGravity */
{ 1, -1 }, /* NorthEastGravity */
{ -1, 0 }, /* WestGravity */
{ 0, 0 }, /* CenterGravity */
{ 1, 0 }, /* EastGravity */
{ -1, 1 }, /* SouthWestGravity */
{ 0, 1 }, /* SouthGravity */
{ 1, 1 }, /* SouthEastGravity */
{ 0, 0 }, /* StaticGravity */
};
register int g = ((tmp->hints.flags & PWinGravity)
? tmp->hints.win_gravity : NorthWestGravity);
if (g < ForgetGravity || g > StaticGravity) {
*xp = *yp = 0;
} else {
*xp = gravity_offsets[g].x;
*yp = gravity_offsets[g].y;
}
}
/***********************************************************************
*
* Procedure:
* AddWindow - add a new window to the twm list
*
* Returned Value:
* (TwmWindow *) - pointer to the TwmWindow structure
*
* Inputs:
* w - the window id of the window to add
* iconm - flag to tell if this is an icon manager window
* iconp - pointer to icon manager struct
*
***********************************************************************
*/
TwmWindow *
AddWindow(w, iconm, iconp)
Window w;
int iconm;
IconMgr *iconp;
{
TwmWindow *tmp_win; /* new twm window structure */
int stat;
XEvent event;
unsigned long valuemask; /* mask for create windows */
XSetWindowAttributes attributes; /* attributes for create windows */
int width, height; /* tmp variable */
int ask_user; /* don't know where to put the window */
int gravx, gravy; /* gravity signs for positioning */
int namelen;
int bw2;
short saved_x, saved_y, restore_icon_x, restore_icon_y;
unsigned short saved_width, saved_height;
Bool restore_iconified = 0;
Bool restore_icon_info_present = 0;
int restoredFromPrevSession;
Bool width_ever_changed_by_user;
Bool height_ever_changed_by_user;
char *name;
#ifdef DEBUG
fprintf(stderr, "AddWindow: w = 0x%x\n", w);
#endif
/* allocate space for the twm window */
tmp_win = (TwmWindow *)calloc(1, sizeof(TwmWindow));
if (tmp_win == 0)
{
fprintf (stderr, "%s: Unable to allocate memory to manage window ID %lx.\n",
ProgramName, w);
return NULL;
}
tmp_win->w = w;
tmp_win->zoomed = ZOOM_NONE;
tmp_win->iconmgr = iconm;
tmp_win->iconmgrp = iconp;
tmp_win->cmaps.number_cwins = 0;
XSelectInput(dpy, tmp_win->w, PropertyChangeMask);
XGetWindowAttributes(dpy, tmp_win->w, &tmp_win->attr);
if (!I18N_FetchName(dpy, tmp_win->w, &name))
name = NULL;
tmp_win->class = NoClass;
XGetClassHint(dpy, tmp_win->w, &tmp_win->class);
FetchWmProtocols (tmp_win);
FetchWmColormapWindows (tmp_win);
if (name == NULL)
tmp_win->name = strdup(NoName);
else {
tmp_win->name = strdup(name);
free(name);
}
if (GetWindowConfig (tmp_win,
&saved_x, &saved_y, &saved_width, &saved_height,
&restore_iconified, &restore_icon_info_present,
&restore_icon_x, &restore_icon_y,
&width_ever_changed_by_user, &height_ever_changed_by_user))
{
tmp_win->attr.x = saved_x;
tmp_win->attr.y = saved_y;
tmp_win->widthEverChangedByUser = width_ever_changed_by_user;
tmp_win->heightEverChangedByUser = height_ever_changed_by_user;
if (width_ever_changed_by_user)
tmp_win->attr.width = saved_width;
if (height_ever_changed_by_user)
tmp_win->attr.height = saved_height;
restoredFromPrevSession = 1;
}
else
{
tmp_win->widthEverChangedByUser = False;
tmp_win->heightEverChangedByUser = False;
restoredFromPrevSession = 0;
}
/*
* do initial clip; should look at window gravity
*/
if (tmp_win->attr.width > Scr->MaxWindowWidth)
tmp_win->attr.width = Scr->MaxWindowWidth;
if (tmp_win->attr.height > Scr->MaxWindowHeight)
tmp_win->attr.height = Scr->MaxWindowHeight;
tmp_win->wmhints = XGetWMHints(dpy, tmp_win->w);
if (tmp_win->wmhints)
{
if (restore_iconified)
{
tmp_win->wmhints->initial_state = IconicState;
tmp_win->wmhints->flags |= StateHint;
}
if (restore_icon_info_present)
{
tmp_win->wmhints->icon_x = restore_icon_x;
tmp_win->wmhints->icon_y = restore_icon_y;
tmp_win->wmhints->flags |= IconPositionHint;
}
}
if (tmp_win->wmhints && (tmp_win->wmhints->flags & WindowGroupHint))
tmp_win->group = tmp_win->wmhints->window_group;
else
tmp_win->group = tmp_win->w/* NULL */;
/*
* The July 27, 1988 draft of the ICCCM ignores the size and position
* fields in the WM_NORMAL_HINTS property.
*/
tmp_win->transient = Transient(tmp_win->w, &tmp_win->transientfor);
tmp_win->nameChanged = 0;
if (tmp_win->class.res_name == NULL)
tmp_win->class.res_name = NoName;
if (tmp_win->class.res_class == NULL)
tmp_win->class.res_class = NoName;
tmp_win->full_name = strdup(tmp_win->name);
namelen = strlen (tmp_win->name);
tmp_win->highlight = Scr->Highlight &&
(!(short)(long) LookInList(Scr->NoHighlight, tmp_win->full_name,
&tmp_win->class));
tmp_win->stackmode = Scr->StackMode &&
(!(short)(long) LookInList(Scr->NoStackModeL, tmp_win->full_name,
&tmp_win->class));
tmp_win->titlehighlight = Scr->TitleHighlight &&
(!(short)(long) LookInList(Scr->NoTitleHighlight, tmp_win->full_name,
&tmp_win->class));
tmp_win->auto_raise = (short)(long) LookInList(Scr->AutoRaise,
tmp_win->full_name,
&tmp_win->class);
if (tmp_win->auto_raise) Scr->NumAutoRaises++;
tmp_win->iconify_by_unmapping = Scr->IconifyByUnmapping;
if (Scr->IconifyByUnmapping)
{
tmp_win->iconify_by_unmapping = iconm ? FALSE :
!(short)(long) LookInList(Scr->DontIconify, tmp_win->full_name,
&tmp_win->class);
}
tmp_win->iconify_by_unmapping |=
(short)(long) LookInList(Scr->IconifyByUn, tmp_win->full_name,
&tmp_win->class);
if (LookInList(Scr->WindowRingL, tmp_win->full_name, &tmp_win->class)) {
if (Scr->Ring) {
tmp_win->ring.next = Scr->Ring->ring.next;
if (Scr->Ring->ring.next->ring.prev)
Scr->Ring->ring.next->ring.prev = tmp_win;
Scr->Ring->ring.next = tmp_win;
tmp_win->ring.prev = Scr->Ring;
} else {
tmp_win->ring.next = tmp_win->ring.prev = Scr->Ring = tmp_win;
}
} else
tmp_win->ring.next = tmp_win->ring.prev = NULL;
tmp_win->ring.cursor_valid = False;
tmp_win->squeeze_info = NULL;
/*
* get the squeeze information; note that this does not have to be freed
* since it is coming from the screen list
*/
if (HasShape) {
if (!LookInList (Scr->DontSqueezeTitleL, tmp_win->full_name,
&tmp_win->class)) {
tmp_win->squeeze_info = (SqueezeInfo *)
LookInList (Scr->SqueezeTitleL, tmp_win->full_name,
&tmp_win->class);
if (!tmp_win->squeeze_info) {
static SqueezeInfo default_squeeze = { J_LEFT, 0, 0 };
if (Scr->SqueezeTitle)
tmp_win->squeeze_info = &default_squeeze;
}
}
}
tmp_win->old_bw = tmp_win->attr.border_width;
if (Scr->ClientBorderWidth) {
tmp_win->frame_bw = tmp_win->old_bw;
} else {
tmp_win->frame_bw = Scr->BorderWidth;
}
bw2 = tmp_win->frame_bw * 2;
tmp_win->title_height = Scr->TitleHeight + tmp_win->frame_bw;
if (Scr->NoTitlebar)
tmp_win->title_height = 0;
if (LookInList(Scr->MakeTitle, tmp_win->full_name, &tmp_win->class))
tmp_win->title_height = Scr->TitleHeight + tmp_win->frame_bw;
if (LookInList(Scr->NoTitle, tmp_win->full_name, &tmp_win->class))
tmp_win->title_height = 0;
/* if it is a transient window, don't put a title on it */
if (tmp_win->transient && !Scr->DecorateTransients)
tmp_win->title_height = 0;
if (LookInList(Scr->StartIconified, tmp_win->full_name, &tmp_win->class))
{
if (!tmp_win->wmhints)
{
tmp_win->wmhints = (XWMHints *)malloc(sizeof(XWMHints));
tmp_win->wmhints->flags = 0;
}
tmp_win->wmhints->initial_state = IconicState;
tmp_win->wmhints->flags |= StateHint;
}
GetWindowSizeHints (tmp_win);
if (restoredFromPrevSession)
{
/*
* When restoring window positions from the previous session,
* we always use NorthWest gravity.
*/
gravx = gravy = -1;
}
else
{
GetGravityOffsets (tmp_win, &gravx, &gravy);
}
/*
* Don't bother user if:
*
* o the window is a transient, or
*
* o a USPosition was requested, or
*
* o a PPosition was requested and UsePPosition is ON or
* NON_ZERO if the window is at other than (0,0)
*/
ask_user = TRUE;
if (tmp_win->transient ||
(tmp_win->hints.flags & USPosition) ||
((tmp_win->hints.flags & PPosition) && Scr->UsePPosition &&
(Scr->UsePPosition == PPOS_ON ||
tmp_win->attr.x != 0 || tmp_win->attr.y != 0)))
ask_user = FALSE;
/*
* do any prompting for position
*/
if (HandlingEvents && ask_user && !restoredFromPrevSession) {
if (Scr->RandomPlacement) { /* just stick it somewhere */
if ((PlaceX + tmp_win->attr.width) > Scr->MyDisplayWidth)
PlaceX = 50;
if ((PlaceY + tmp_win->attr.height) > Scr->MyDisplayHeight)
PlaceY = 50;
tmp_win->attr.x = PlaceX;
tmp_win->attr.y = PlaceY;
PlaceX += 30;
PlaceY += 30;
} else { /* else prompt */
if (!(tmp_win->wmhints && tmp_win->wmhints->flags & StateHint &&
tmp_win->wmhints->initial_state == IconicState))
{
Bool firsttime = True;
/* better wait until all the mouse buttons have been
* released.
*/
while (TRUE)
{
XUngrabServer(dpy);
XSync(dpy, 0);
XGrabServer(dpy);
JunkMask = 0;
if (!XQueryPointer (dpy, Scr->Root, &JunkRoot,
&JunkChild, &JunkX, &JunkY,
&AddingX, &AddingY, &JunkMask))
JunkMask = 0;
JunkMask &= (Button1Mask | Button2Mask | Button3Mask |
Button4Mask | Button5Mask);
/*
* watch out for changing screens
*/
if (firsttime) {
if (JunkRoot != Scr->Root) {
register int scrnum;
for (scrnum = 0; scrnum < NumScreens; scrnum++) {
if (JunkRoot == RootWindow (dpy, scrnum)) break;
}
if (scrnum != NumScreens) PreviousScreen = scrnum;
}
firsttime = False;
}
/*
* wait for buttons to come up; yuck
*/
if (JunkMask != 0) continue;
/*
* this will cause a warp to the indicated root
*/
stat = XGrabPointer(dpy, Scr->Root, False,
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask | PointerMotionHintMask,
GrabModeAsync, GrabModeAsync,
Scr->Root, UpperLeftCursor, CurrentTime);
if (stat == GrabSuccess)
break;
}
width = (SIZE_HINDENT + MyFont_TextWidth (&Scr->SizeFont,
tmp_win->name, namelen));
height = Scr->SizeFont.height + SIZE_VINDENT * 2;
XResizeWindow (dpy, Scr->SizeWindow, width + SIZE_HINDENT, height);
XMapRaised(dpy, Scr->SizeWindow);
InstallRootColormap();
MyFont_ChangeGC(Scr->DefaultC.fore, Scr->DefaultC.back,
&Scr->SizeFont);
MyFont_DrawImageString (dpy, Scr->SizeWindow, &Scr->SizeFont,
Scr->NormalGC,
SIZE_HINDENT,
SIZE_VINDENT + Scr->SizeFont.ascent,
tmp_win->name, namelen);
AddingW = tmp_win->attr.width + bw2;
AddingH = tmp_win->attr.height + tmp_win->title_height + bw2;
if (Scr->DontMoveOff) {
/*
* Make sure the initial outline comes up on the screen.
*/
if (AddingX < 0)
AddingX = 0;
if (AddingX > Scr->MyDisplayWidth - AddingW)
AddingX = Scr->MyDisplayWidth - AddingW;
if (AddingY < 0)
AddingY = 0;
if (AddingY > Scr->MyDisplayHeight - AddingH)
AddingY = Scr->MyDisplayHeight - AddingH;
}
MoveOutline(Scr->Root, AddingX, AddingY, AddingW, AddingH,
tmp_win->frame_bw, tmp_win->title_height);
while (TRUE)
{
XMaskEvent(dpy, ButtonPressMask | PointerMotionMask, &event);
if (Event.type == MotionNotify) {
/* discard any extra motion events before a release */
while(XCheckMaskEvent(dpy,
ButtonMotionMask | ButtonPressMask, &Event))
if (Event.type == ButtonPress)
break;
}
if (event.type == ButtonPress) {
AddingX = event.xbutton.x_root;
AddingY = event.xbutton.y_root;
/* DontMoveOff prohibits user form off-screen placement */
if (Scr->DontMoveOff)
{
int AddingR, AddingB;
AddingR = AddingX + AddingW;
AddingB = AddingY + AddingH;
if (AddingX < 0)
AddingX = 0;
if (AddingR > Scr->MyDisplayWidth)
AddingX = Scr->MyDisplayWidth - AddingW;
if (AddingY < 0)
AddingY = 0;
if (AddingB > Scr->MyDisplayHeight)
AddingY = Scr->MyDisplayHeight - AddingH;
}
break;
}
if (event.type != MotionNotify) {
continue;
}
XQueryPointer(dpy, Scr->Root, &JunkRoot, &JunkChild,
&JunkX, &JunkY, &AddingX, &AddingY, &JunkMask);
if (Scr->DontMoveOff)
{
int AddingR, AddingB;
AddingR = AddingX + AddingW;
AddingB = AddingY + AddingH;
if (AddingX < 0)
AddingX = 0;
if (AddingR > Scr->MyDisplayWidth)
AddingX = Scr->MyDisplayWidth - AddingW;
if (AddingY < 0)
AddingY = 0;
if (AddingB > Scr->MyDisplayHeight)
AddingY = Scr->MyDisplayHeight - AddingH;
}
MoveOutline(Scr->Root, AddingX, AddingY, AddingW, AddingH,
tmp_win->frame_bw, tmp_win->title_height);
}
if (event.xbutton.button == Button2) {
int lastx, lasty;
Scr->SizeStringOffset = width +
MyFont_TextWidth(&Scr->SizeFont, ": ", 2);
XResizeWindow (dpy, Scr->SizeWindow, Scr->SizeStringOffset +
Scr->SizeStringWidth, height);
MyFont_DrawImageString (dpy, Scr->SizeWindow, &Scr->SizeFont,
Scr->NormalGC, width,
SIZE_VINDENT + Scr->SizeFont.ascent,
": ", 2);
if (0/*Scr->AutoRelativeResize*/) {
int dx = (tmp_win->attr.width / 4);
int dy = (tmp_win->attr.height / 4);
#define HALF_AVE_CURSOR_SIZE 8 /* so that it is visible */
if (dx < HALF_AVE_CURSOR_SIZE) dx = HALF_AVE_CURSOR_SIZE;
if (dy < HALF_AVE_CURSOR_SIZE) dy = HALF_AVE_CURSOR_SIZE;
#undef HALF_AVE_CURSOR_SIZE
dx += (tmp_win->frame_bw + 1);
dy += (bw2 + tmp_win->title_height + 1);
if (AddingX + dx >= Scr->MyDisplayWidth)
dx = Scr->MyDisplayWidth - AddingX - 1;
if (AddingY + dy >= Scr->MyDisplayHeight)
dy = Scr->MyDisplayHeight - AddingY - 1;
if (dx > 0 && dy > 0)
XWarpPointer (dpy, None, None, 0, 0, 0, 0, dx, dy);
} else {
XWarpPointer (dpy, None, Scr->Root, 0, 0, 0, 0,
AddingX + AddingW/2, AddingY + AddingH/2);
}
AddStartResize(tmp_win, AddingX, AddingY, AddingW, AddingH);
lastx = -10000;
lasty = -10000;
while (TRUE)
{
XMaskEvent(dpy,
ButtonReleaseMask | ButtonMotionMask, &event);
if (Event.type == MotionNotify) {
/* discard any extra motion events before a release */
while(XCheckMaskEvent(dpy,
ButtonMotionMask | ButtonReleaseMask, &Event))
if (Event.type == ButtonRelease)
break;
}
if (event.type == ButtonRelease)
{
AddEndResize(tmp_win);
break;
}
if (event.type != MotionNotify) {
continue;
}
/*
* XXX - if we are going to do a loop, we ought to consider
* using multiple GXxor lines so that we don't need to
* grab the server.
*/
XQueryPointer(dpy, Scr->Root, &JunkRoot, &JunkChild,
&JunkX, &JunkY, &AddingX, &AddingY, &JunkMask);
if (lastx != AddingX || lasty != AddingY)
{
DoResize(AddingX, AddingY, tmp_win);
lastx = AddingX;
lasty = AddingY;
}
}
}
else if (event.xbutton.button == Button3)
{
int maxw = Scr->MyDisplayWidth - AddingX - bw2;
int maxh = Scr->MyDisplayHeight - AddingY - bw2;
/*
* Make window go to bottom of screen, and clip to right edge.
* This is useful when popping up large windows and fixed
* column text windows.
*/
if (AddingW > maxw) AddingW = maxw;
AddingH = maxh;
ConstrainSize (tmp_win, &AddingW, &AddingH); /* w/o borders */
AddingW += bw2;
AddingH += bw2;
}
else
{
XMaskEvent(dpy, ButtonReleaseMask, &event);
}
MoveOutline(Scr->Root, 0, 0, 0, 0, 0, 0);
XUnmapWindow(dpy, Scr->SizeWindow);
UninstallRootColormap();
XUngrabPointer(dpy, CurrentTime);
tmp_win->attr.x = AddingX;
tmp_win->attr.y = AddingY + tmp_win->title_height;
tmp_win->attr.width = AddingW - bw2;
tmp_win->attr.height = AddingH - tmp_win->title_height - bw2;
XUngrabServer(dpy);
}
}
} else { /* put it where asked, mod title bar */
/* if the gravity is towards the top, move it by the title height */
if (gravy < 0) tmp_win->attr.y -= gravy * tmp_win->title_height;
}
#ifdef DEBUG
fprintf(stderr, " position window %d, %d %dx%d\n",
tmp_win->attr.x,
tmp_win->attr.y,
tmp_win->attr.width,
tmp_win->attr.height);
#endif
if (!Scr->ClientBorderWidth) { /* need to adjust for twm borders */
int delta = tmp_win->attr.border_width - tmp_win->frame_bw;
tmp_win->attr.x += gravx * delta;
tmp_win->attr.y += gravy * delta;
}
tmp_win->title_width = tmp_win->attr.width;
if (tmp_win->old_bw) XSetWindowBorderWidth (dpy, tmp_win->w, 0);
tmp_win->name_width = MyFont_TextWidth(&Scr->TitleBarFont, tmp_win->name,
namelen);
if (!I18N_GetIconName(dpy, tmp_win->w, &name)) {
tmp_win->icon_name = strdup(tmp_win->name);
} else {
if (name == NULL) {
tmp_win->icon_name = strdup(tmp_win->name);
} else {
tmp_win->icon_name = strdup(name);
free(name);
}
}
tmp_win->iconified = FALSE;
tmp_win->icon = FALSE;
tmp_win->icon_on = FALSE;
XGrabServer(dpy);
/*
* Make sure the client window still exists. We don't want to leave an
* orphan frame window if it doesn't. Since we now have the server
* grabbed, the window can't disappear later without having been
* reparented, so we'll get a DestroyNotify for it. We won't have
* gotten one for anything up to here, however.
*/
if (XGetGeometry(dpy, tmp_win->w, &JunkRoot, &JunkX, &JunkY,
&JunkWidth, &JunkHeight, &JunkBW, &JunkDepth) == 0)
{
free((char *)tmp_win);
XUngrabServer(dpy);
return(NULL);
}
/* add the window into the twm list */
tmp_win->next = Scr->TwmRoot.next;
if (Scr->TwmRoot.next != NULL)
Scr->TwmRoot.next->prev = tmp_win;
tmp_win->prev = &Scr->TwmRoot;
Scr->TwmRoot.next = tmp_win;
/* get all the colors for the window */
tmp_win->border = Scr->BorderColor;
tmp_win->icon_border = Scr->IconBorderColor;
tmp_win->border_tile.fore = Scr->BorderTileC.fore;
tmp_win->border_tile.back = Scr->BorderTileC.back;
tmp_win->title.fore = Scr->TitleC.fore;
tmp_win->title.back = Scr->TitleC.back;
tmp_win->iconc.fore = Scr->IconC.fore;
tmp_win->iconc.back = Scr->IconC.back;
GetColorFromList(Scr->BorderColorL, tmp_win->full_name, &tmp_win->class,
&tmp_win->border);
GetColorFromList(Scr->IconBorderColorL, tmp_win->full_name, &tmp_win->class,
&tmp_win->icon_border);
GetColorFromList(Scr->BorderTileForegroundL, tmp_win->full_name,
&tmp_win->class, &tmp_win->border_tile.fore);
GetColorFromList(Scr->BorderTileBackgroundL, tmp_win->full_name,
&tmp_win->class, &tmp_win->border_tile.back);
GetColorFromList(Scr->TitleForegroundL, tmp_win->full_name, &tmp_win->class,
&tmp_win->title.fore);
GetColorFromList(Scr->TitleBackgroundL, tmp_win->full_name, &tmp_win->class,
&tmp_win->title.back);
GetColorFromList(Scr->IconForegroundL, tmp_win->full_name, &tmp_win->class,
&tmp_win->iconc.fore);
GetColorFromList(Scr->IconBackgroundL, tmp_win->full_name, &tmp_win->class,
&tmp_win->iconc.back);
/* create windows */
tmp_win->frame_x = tmp_win->attr.x + tmp_win->old_bw - tmp_win->frame_bw;
tmp_win->frame_y = tmp_win->attr.y - tmp_win->title_height +
tmp_win->old_bw - tmp_win->frame_bw;
tmp_win->frame_width = tmp_win->attr.width;
tmp_win->frame_height = tmp_win->attr.height + tmp_win->title_height;
valuemask = CWBackPixmap | CWBorderPixel | CWCursor | CWEventMask;
attributes.background_pixmap = None;
attributes.border_pixel = tmp_win->border;
attributes.cursor = Scr->FrameCursor;
attributes.event_mask = (SubstructureRedirectMask |
ButtonPressMask | ButtonReleaseMask |
EnterWindowMask | LeaveWindowMask);
if (tmp_win->attr.save_under) {
attributes.save_under = True;
valuemask |= CWSaveUnder;
}
tmp_win->frame = XCreateWindow (dpy, Scr->Root, tmp_win->frame_x,
tmp_win->frame_y,
(unsigned int) tmp_win->frame_width,
(unsigned int) tmp_win->frame_height,
(unsigned int) tmp_win->frame_bw,
Scr->d_depth,
(unsigned int) CopyFromParent,
Scr->d_visual, valuemask, &attributes);
if (tmp_win->title_height)
{
valuemask = (CWEventMask | CWBorderPixel | CWBackPixel);
attributes.event_mask = (KeyPressMask | ButtonPressMask |
ButtonReleaseMask | ExposureMask);
attributes.border_pixel = tmp_win->border;
attributes.background_pixel = tmp_win->title.back;
tmp_win->title_w = XCreateWindow (dpy, tmp_win->frame,
-tmp_win->frame_bw,
-tmp_win->frame_bw,
(unsigned int) tmp_win->attr.width,
(unsigned int) Scr->TitleHeight,
(unsigned int) tmp_win->frame_bw,
Scr->d_depth,
(unsigned int) CopyFromParent,
Scr->d_visual, valuemask,
&attributes);
}
else {
tmp_win->title_w = 0;
tmp_win->squeeze_info = NULL;
}
if (tmp_win->highlight)
{
tmp_win->gray = XCreatePixmapFromBitmapData(dpy, Scr->Root,
gray_bits, gray_width, gray_height,
tmp_win->border_tile.fore, tmp_win->border_tile.back,
Scr->d_depth);
SetBorder (tmp_win, False);
}
else
tmp_win->gray = None;
if (tmp_win->title_w) {
CreateWindowTitlebarButtons (tmp_win);
ComputeTitleLocation (tmp_win);
XMoveWindow (dpy, tmp_win->title_w,
tmp_win->title_x, tmp_win->title_y);
XDefineCursor(dpy, tmp_win->title_w, Scr->TitleCursor);
}
valuemask = (CWEventMask | CWDontPropagate);
attributes.event_mask = (StructureNotifyMask | PropertyChangeMask |
ColormapChangeMask | VisibilityChangeMask |
EnterWindowMask | LeaveWindowMask);
attributes.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
XChangeWindowAttributes (dpy, tmp_win->w, valuemask, &attributes);
if (HasShape)
XShapeSelectInput (dpy, tmp_win->w, ShapeNotifyMask);
if (tmp_win->title_w) {
XMapWindow (dpy, tmp_win->title_w);
}
if (HasShape) {
int xws, yws, xbs, ybs;
unsigned wws, hws, wbs, hbs;
int boundingShaped, clipShaped;
XShapeSelectInput (dpy, tmp_win->w, ShapeNotifyMask);
XShapeQueryExtents (dpy, tmp_win->w,
&boundingShaped, &xws, &yws, &wws, &hws,
&clipShaped, &xbs, &ybs, &wbs, &hbs);
tmp_win->wShaped = boundingShaped;
}
if (!tmp_win->iconmgr)
XAddToSaveSet(dpy, tmp_win->w);
XReparentWindow(dpy, tmp_win->w, tmp_win->frame, 0, tmp_win->title_height);
/*
* Reparenting generates an UnmapNotify event, followed by a MapNotify.
* Set the map state to FALSE to prevent a transition back to
* WithdrawnState in HandleUnmapNotify. Map state gets set correctly
* again in HandleMapNotify.
*/
tmp_win->mapped = FALSE;
SetupFrame (tmp_win, tmp_win->frame_x, tmp_win->frame_y,
tmp_win->frame_width, tmp_win->frame_height, -1, True);
/* wait until the window is iconified and the icon window is mapped
* before creating the icon window
*/
tmp_win->icon_w = (Window) 0;
if (!tmp_win->iconmgr)
{
GrabButtons(tmp_win);
GrabKeys(tmp_win);
}
(void) AddIconManager(tmp_win);
XSaveContext(dpy, tmp_win->w, TwmContext, (caddr_t) tmp_win);
XSaveContext(dpy, tmp_win->w, ScreenContext, (caddr_t) Scr);
XSaveContext(dpy, tmp_win->frame, TwmContext, (caddr_t) tmp_win);
XSaveContext(dpy, tmp_win->frame, ScreenContext, (caddr_t) Scr);
if (tmp_win->title_height)
{
int i;
int nb = Scr->TBInfo.nleft + Scr->TBInfo.nright;
XSaveContext(dpy, tmp_win->title_w, TwmContext, (caddr_t) tmp_win);
XSaveContext(dpy, tmp_win->title_w, ScreenContext, (caddr_t) Scr);
for (i = 0; i < nb; i++) {
XSaveContext(dpy, tmp_win->titlebuttons[i].window, TwmContext,
(caddr_t) tmp_win);
XSaveContext(dpy, tmp_win->titlebuttons[i].window, ScreenContext,
(caddr_t) Scr);
}
if (tmp_win->hilite_w)
{
XSaveContext(dpy, tmp_win->hilite_w, TwmContext, (caddr_t)tmp_win);
XSaveContext(dpy, tmp_win->hilite_w, ScreenContext, (caddr_t)Scr);
}
}
XUngrabServer(dpy);
/* if we were in the middle of a menu activated function, regrab
* the pointer
*/
if (RootFunction)
ReGrab();
return (tmp_win);
}
/***********************************************************************
*
* Procedure:
* MappedNotOverride - checks to see if we should really
* put a twm frame on the window
*
* Returned Value:
* TRUE - go ahead and frame the window
* FALSE - don't frame the window
*
* Inputs:
* w - the window to check
*
***********************************************************************
*/
int
MappedNotOverride(w)
Window w;
{
XWindowAttributes wa;
XGetWindowAttributes(dpy, w, &wa);
return ((wa.map_state != IsUnmapped) && (wa.override_redirect != True));
}
/***********************************************************************
*
* Procedure:
* AddDefaultBindings - attach default bindings so that naive users
* don't get messed up if they provide a minimal twmrc.
*/
static void do_add_binding (button, context, modifier, func)
int button, context, modifier;
int func;
{
MouseButton *mb = &Scr->Mouse[button][context][modifier];
if (mb->func) return; /* already defined */
mb->func = func;
mb->item = NULL;
}
void
AddDefaultBindings ()
{
/*
* The bindings are stored in Scr->Mouse, indexed by
* Mouse[button_number][C_context][modifier].
*/
#define NoModifierMask 0
do_add_binding (Button1, C_TITLE, NoModifierMask, F_MOVE);
do_add_binding (Button1, C_ICON, NoModifierMask, F_ICONIFY);
do_add_binding (Button1, C_ICONMGR, NoModifierMask, F_ICONIFY);
do_add_binding (Button2, C_TITLE, NoModifierMask, F_RAISELOWER);
do_add_binding (Button2, C_ICON, NoModifierMask, F_ICONIFY);
do_add_binding (Button2, C_ICONMGR, NoModifierMask, F_ICONIFY);
#undef NoModifierMask
}
/***********************************************************************
*
* Procedure:
* GrabButtons - grab needed buttons for the window
*
* Inputs:
* tmp_win - the twm window structure to use
*
***********************************************************************
*/
void
GrabButtons(tmp_win)
TwmWindow *tmp_win;
{
int i, j;
for (i = 0; i < MAX_BUTTONS+1; i++)
{
for (j = 0; j < MOD_SIZE; j++)
{
if (Scr->Mouse[i][C_WINDOW][j].func != 0)
{
/* twm used to do this grab on the application main window,
* tmp_win->w . This was not ICCCM complient and was changed.
*/
XGrabButton(dpy, i, j, tmp_win->frame,
True, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None,
Scr->FrameCursor);
}
}
}
}
/***********************************************************************
*
* Procedure:
* GrabKeys - grab needed keys for the window
*
* Inputs:
* tmp_win - the twm window structure to use
*
***********************************************************************
*/
void
GrabKeys(tmp_win)
TwmWindow *tmp_win;
{
FuncKey *tmp;
IconMgr *p;
for (tmp = Scr->FuncKeyRoot.next; tmp != NULL; tmp = tmp->next)
{
switch (tmp->cont)
{
case C_WINDOW:
XGrabKey(dpy, tmp->keycode, tmp->mods, tmp_win->w, True,
GrabModeAsync, GrabModeAsync);
break;
case C_ICON:
if (tmp_win->icon_w)
XGrabKey(dpy, tmp->keycode, tmp->mods, tmp_win->icon_w, True,
GrabModeAsync, GrabModeAsync);
case C_TITLE:
if (tmp_win->title_w)
XGrabKey(dpy, tmp->keycode, tmp->mods, tmp_win->title_w, True,
GrabModeAsync, GrabModeAsync);
break;
case C_NAME:
XGrabKey(dpy, tmp->keycode, tmp->mods, tmp_win->w, True,
GrabModeAsync, GrabModeAsync);
if (tmp_win->icon_w)
XGrabKey(dpy, tmp->keycode, tmp->mods, tmp_win->icon_w, True,
GrabModeAsync, GrabModeAsync);
if (tmp_win->title_w)
XGrabKey(dpy, tmp->keycode, tmp->mods, tmp_win->title_w, True,
GrabModeAsync, GrabModeAsync);
break;
/*
case C_ROOT:
XGrabKey(dpy, tmp->keycode, tmp->mods, Scr->Root, True,
GrabModeAsync, GrabModeAsync);
break;
*/
}
}
for (tmp = Scr->FuncKeyRoot.next; tmp != NULL; tmp = tmp->next)
{
if (tmp->cont == C_ICONMGR && !Scr->NoIconManagers)
{
for (p = &Scr->iconmgr; p != NULL; p = p->next)
{
XUngrabKey(dpy, tmp->keycode, tmp->mods, p->twm_win->w);
}
}
}
}
static Window CreateHighlightWindow (tmp_win)
TwmWindow *tmp_win;
{
XSetWindowAttributes attributes; /* attributes for create windows */
Pixmap pm = None;
GC gc;
XGCValues gcv;
unsigned long valuemask;
int h = (Scr->TitleHeight - 2 * Scr->FramePadding);
Window w;
/*
* If a special highlight pixmap was given, use that. Otherwise,
* use a nice, even gray pattern. The old horizontal lines look really
* awful on interlaced monitors (as well as resembling other looks a
* little bit too closely), but can be used by putting
*
* Pixmaps { TitleHighlight "hline2" }
*
* (or whatever the horizontal line bitmap is named) in the startup
* file. If all else fails, use the foreground color to look like a
* solid line.
*/
if (!Scr->hilitePm) {
Scr->hilitePm = XCreateBitmapFromData (dpy, tmp_win->title_w,
gray_bits, gray_width,
gray_height);
Scr->hilite_pm_width = gray_width;
Scr->hilite_pm_height = gray_height;
}
if (Scr->hilitePm) {
pm = XCreatePixmap (dpy, tmp_win->title_w,
Scr->hilite_pm_width, Scr->hilite_pm_height,
Scr->d_depth);
gcv.foreground = tmp_win->title.fore;
gcv.background = tmp_win->title.back;
gcv.graphics_exposures = False;
gc = XCreateGC (dpy, pm,
(GCForeground|GCBackground|GCGraphicsExposures),
&gcv);
if (gc) {
XCopyPlane (dpy, Scr->hilitePm, pm, gc, 0, 0,
Scr->hilite_pm_width, Scr->hilite_pm_height,
0, 0, 1);
XFreeGC (dpy, gc);
} else {
XFreePixmap (dpy, pm);
pm = None;
}
}
if (pm) {
valuemask = CWBackPixmap;
attributes.background_pixmap = pm;
} else {
valuemask = CWBackPixel;
attributes.background_pixel = tmp_win->title.fore;
}
w = XCreateWindow (dpy, tmp_win->title_w, 0, Scr->FramePadding,
(unsigned int) Scr->TBInfo.width, (unsigned int) h,
(unsigned int) 0,
Scr->d_depth, (unsigned int) CopyFromParent,
Scr->d_visual, valuemask, &attributes);
if (pm) XFreePixmap (dpy, pm);
return w;
}
void ComputeCommonTitleOffsets ()
{
int buttonwidth = (Scr->TBInfo.width + Scr->TBInfo.pad);
Scr->TBInfo.leftx = Scr->TBInfo.rightoff = Scr->FramePadding;
if (Scr->TBInfo.nleft > 0)
Scr->TBInfo.leftx += Scr->ButtonIndent;
Scr->TBInfo.titlex = (Scr->TBInfo.leftx +
(Scr->TBInfo.nleft * buttonwidth) - Scr->TBInfo.pad +
Scr->TitlePadding);
if (Scr->TBInfo.nright > 0)
Scr->TBInfo.rightoff += (Scr->ButtonIndent +
((Scr->TBInfo.nright * buttonwidth) -
Scr->TBInfo.pad));
return;
}
void ComputeWindowTitleOffsets (tmp_win, width, squeeze)
TwmWindow *tmp_win;
int width;
Bool squeeze;
{
tmp_win->highlightx = (Scr->TBInfo.titlex + tmp_win->name_width);
if (tmp_win->hilite_w || Scr->TBInfo.nright > 0)
tmp_win->highlightx += Scr->TitlePadding;
tmp_win->rightx = width - Scr->TBInfo.rightoff;
if (squeeze && tmp_win->squeeze_info) {
int rx = (tmp_win->highlightx +
(tmp_win->hilite_w
? Scr->TBInfo.width * 2 : 0) +
(Scr->TBInfo.nright > 0 ? Scr->TitlePadding : 0) +
Scr->FramePadding);
if (rx < tmp_win->rightx) tmp_win->rightx = rx;
}
return;
}
/*
* ComputeTitleLocation - calculate the position of the title window; we need
* to take the frame_bw into account since we want (0,0) of the title window
* to line up with (0,0) of the frame window.
*/
void ComputeTitleLocation (tmp)
register TwmWindow *tmp;
{
tmp->title_x = -tmp->frame_bw;
tmp->title_y = -tmp->frame_bw;
if (tmp->squeeze_info) {
register SqueezeInfo *si = tmp->squeeze_info;
int basex;
int maxwidth = tmp->frame_width;
int tw = tmp->title_width;
/*
* figure label base from squeeze info (justification fraction)
*/
if (si->denom == 0) { /* num is pixel based */
if ((basex = si->num) == 0) { /* look for special cases */
switch (si->justify) {
case J_RIGHT:
basex = maxwidth;
break;
case J_CENTER:
basex = maxwidth / 2;
break;
}
}
} else { /* num/denom is fraction */
basex = ((si->num * maxwidth) / si->denom);
if (si->num < 0) basex += maxwidth;
}
/*
* adjust for left (nop), center, right justify and clip
*/
switch (si->justify) {
case J_CENTER:
basex -= tw / 2;
break;
case J_RIGHT:
basex -= tw - 1;
break;
}
if (basex > maxwidth - tw + 1)
basex = maxwidth - tw + 1;
if (basex < 0) basex = 0;
tmp->title_x = basex - tmp->frame_bw;
}
}
static void CreateWindowTitlebarButtons (tmp_win)
TwmWindow *tmp_win;
{
unsigned long valuemask; /* mask for create windows */
XSetWindowAttributes attributes; /* attributes for create windows */
int leftx, rightx, y;
TitleButton *tb;
int nb;
if (tmp_win->title_height == 0)
{
tmp_win->hilite_w = 0;
return;
}
/*
* create the title bar windows; let the event handler deal with painting
* so that we don't have to spend two pixmaps (or deal with hashing)
*/
ComputeWindowTitleOffsets (tmp_win, tmp_win->attr.width, False);
leftx = y = Scr->TBInfo.leftx;
rightx = tmp_win->rightx;
attributes.win_gravity = NorthWestGravity;
attributes.background_pixel = tmp_win->title.back;
attributes.border_pixel = tmp_win->title.fore;
attributes.event_mask = (ButtonPressMask | ButtonReleaseMask |
ExposureMask);
attributes.cursor = Scr->ButtonCursor;
valuemask = (CWWinGravity | CWBackPixel | CWBorderPixel | CWEventMask |
CWCursor);
tmp_win->titlebuttons = NULL;
nb = Scr->TBInfo.nleft + Scr->TBInfo.nright;
if (nb > 0) {
tmp_win->titlebuttons = (TBWindow *) malloc (nb * sizeof(TBWindow));
if (!tmp_win->titlebuttons) {
fprintf (stderr, "%s: unable to allocate %d titlebuttons\n",
ProgramName, nb);
} else {
TBWindow *tbw;
int boxwidth = (Scr->TBInfo.width + Scr->TBInfo.pad);
unsigned int h = (Scr->TBInfo.width - Scr->TBInfo.border * 2);
for (tb = Scr->TBInfo.head, tbw = tmp_win->titlebuttons; tb;
tb = tb->next, tbw++) {
int x;
if (tb->rightside) {
x = rightx;
rightx += boxwidth;
attributes.win_gravity = NorthEastGravity;
} else {
x = leftx;
leftx += boxwidth;
attributes.win_gravity = NorthWestGravity;
}
tbw->window = XCreateWindow (dpy, tmp_win->title_w, x, y, h, h,
(unsigned int) Scr->TBInfo.border,
0, (unsigned int) CopyFromParent,
(Visual *) CopyFromParent,
valuemask, &attributes);
tbw->info = tb;
}
}
}
tmp_win->hilite_w = (tmp_win->titlehighlight
? CreateHighlightWindow (tmp_win) : None);
XMapSubwindows(dpy, tmp_win->title_w);
if (tmp_win->hilite_w)
XUnmapWindow(dpy, tmp_win->hilite_w);
return;
}
void
SetHighlightPixmap (filename)
char *filename;
{
Pixmap pm = GetBitmap (filename);
if (pm) {
if (Scr->hilitePm) {
XFreePixmap (dpy, Scr->hilitePm);
}
Scr->hilitePm = pm;
Scr->hilite_pm_width = JunkWidth;
Scr->hilite_pm_height = JunkHeight;
}
}
void
FetchWmProtocols (tmp)
TwmWindow *tmp;
{
unsigned long flags = 0L;
Atom *protocols = NULL;
int n;
if (XGetWMProtocols (dpy, tmp->w, &protocols, &n)) {
register int i;
register Atom *ap;
for (i = 0, ap = protocols; i < n; i++, ap++) {
if (*ap == _XA_WM_TAKE_FOCUS) flags |= DoesWmTakeFocus;
if (*ap == _XA_WM_SAVE_YOURSELF) flags |= DoesWmSaveYourself;
if (*ap == _XA_WM_DELETE_WINDOW) flags |= DoesWmDeleteWindow;
}
if (protocols) XFree ((char *) protocols);
}
tmp->protocols = flags;
}
TwmColormap *
CreateTwmColormap(c)
Colormap c;
{
TwmColormap *cmap;
cmap = (TwmColormap *) malloc(sizeof(TwmColormap));
if (!cmap ||
XSaveContext(dpy, c, ColormapContext, (caddr_t) cmap)) {
if (cmap) free((char *) cmap);
return (NULL);
}
cmap->c = c;
cmap->state = 0;
cmap->install_req = 0;
cmap->w = None;
cmap->refcnt = 1;
return (cmap);
}
ColormapWindow *
CreateColormapWindow(w, creating_parent, property_window)
Window w;
Bool creating_parent;
Bool property_window;
{
ColormapWindow *cwin;
TwmColormap *cmap;
XWindowAttributes attributes;
cwin = (ColormapWindow *) malloc(sizeof(ColormapWindow));
if (cwin) {
if (!XGetWindowAttributes(dpy, w, &attributes) ||
XSaveContext(dpy, w, ColormapContext, (caddr_t) cwin)) {
free((char *) cwin);
return (NULL);
}
if (XFindContext(dpy, attributes.colormap, ColormapContext,
(caddr_t *)&cwin->colormap) == XCNOENT) {
cwin->colormap = cmap = CreateTwmColormap(attributes.colormap);
if (!cmap) {
XDeleteContext(dpy, w, ColormapContext);
free((char *) cwin);
return (NULL);
}
} else {
cwin->colormap->refcnt++;
}
cwin->w = w;
/*
* Assume that windows in colormap list are
* obscured if we are creating the parent window.
* Otherwise, we assume they are unobscured.
*/
cwin->visibility = creating_parent ?
VisibilityPartiallyObscured : VisibilityUnobscured;
cwin->refcnt = 1;
/*
* If this is a ColormapWindow property window and we
* are not monitoring ColormapNotify or VisibilityNotify
* events, we need to.
*/
if (property_window &&
(attributes.your_event_mask &
(ColormapChangeMask|VisibilityChangeMask)) !=
(ColormapChangeMask|VisibilityChangeMask)) {
XSelectInput(dpy, w, attributes.your_event_mask |
(ColormapChangeMask|VisibilityChangeMask));
}
}
return (cwin);
}
void
FetchWmColormapWindows (tmp)
TwmWindow *tmp;
{
register int i, j;
Window *cmap_windows = NULL;
Bool can_free_cmap_windows = False;
int number_cmap_windows = 0;
ColormapWindow **cwins = NULL;
int previously_installed;
number_cmap_windows = 0;
if (/* SUPPRESS 560 */(previously_installed =
(Scr->cmapInfo.cmaps == &tmp->cmaps && tmp->cmaps.number_cwins))) {
cwins = tmp->cmaps.cwins;
for (i = 0; i < tmp->cmaps.number_cwins; i++)
cwins[i]->colormap->state = 0;
}
if (XGetWMColormapWindows (dpy, tmp->w, &cmap_windows,
&number_cmap_windows) &&
number_cmap_windows > 0) {
can_free_cmap_windows = False;
/*
* check if the top level is in the list, add to front if not
*/
for (i = 0; i < number_cmap_windows; i++) {
if (cmap_windows[i] == tmp->w) break;
}
if (i == number_cmap_windows) { /* not in list */
Window *new_cmap_windows =
(Window *) malloc (sizeof(Window) * (number_cmap_windows + 1));
if (!new_cmap_windows) {
fprintf (stderr,
"%s: unable to allocate %d element colormap window array\n",
ProgramName, number_cmap_windows+1);
goto done;
}
new_cmap_windows[0] = tmp->w; /* add to front */
for (i = 0; i < number_cmap_windows; i++) { /* append rest */
new_cmap_windows[i+1] = cmap_windows[i];
}
XFree ((char *) cmap_windows);
can_free_cmap_windows = True; /* do not use XFree any more */
cmap_windows = new_cmap_windows;
number_cmap_windows++;
}
cwins = (ColormapWindow **) malloc(sizeof(ColormapWindow *) *
number_cmap_windows);
if (cwins) {
for (i = 0; i < number_cmap_windows; i++) {
/*
* Copy any existing entries into new list.
*/
for (j = 0; j < tmp->cmaps.number_cwins; j++) {
if (tmp->cmaps.cwins[j]->w == cmap_windows[i]) {
cwins[i] = tmp->cmaps.cwins[j];
cwins[i]->refcnt++;
break;
}
}
/*
* If the colormap window is not being pointed by
* some other applications colormap window list,
* create a new entry.
*/
if (j == tmp->cmaps.number_cwins) {
if (XFindContext(dpy, cmap_windows[i], ColormapContext,
(caddr_t *)&cwins[i]) == XCNOENT) {
if ((cwins[i] = CreateColormapWindow(cmap_windows[i],
(Bool) tmp->cmaps.number_cwins == 0,
True)) == NULL) {
int k;
for (k = i + 1; k < number_cmap_windows; k++)
cmap_windows[k-1] = cmap_windows[k];
i--;
number_cmap_windows--;
}
} else
cwins[i]->refcnt++;
}
}
}
}
/* No else here, in case we bailed out of clause above.
*/
if (number_cmap_windows == 0) {
number_cmap_windows = 1;
cwins = (ColormapWindow **) malloc(sizeof(ColormapWindow *));
if (XFindContext(dpy, tmp->w, ColormapContext, (caddr_t *)&cwins[0]) ==
XCNOENT)
cwins[0] = CreateColormapWindow(tmp->w,
(Bool) tmp->cmaps.number_cwins == 0, False);
else
cwins[0]->refcnt++;
}
if (tmp->cmaps.number_cwins)
free_cwins(tmp);
tmp->cmaps.cwins = cwins;
tmp->cmaps.number_cwins = number_cmap_windows;
if (number_cmap_windows > 1)
tmp->cmaps.scoreboard =
(char *) calloc(1, ColormapsScoreboardLength(&tmp->cmaps));
if (previously_installed)
InstallWindowColormaps(PropertyNotify, (TwmWindow *) NULL);
done:
if (cmap_windows) {
if (can_free_cmap_windows)
free ((char *) cmap_windows);
else
XFree ((char *) cmap_windows);
}
return;
}
void GetWindowSizeHints (tmp)
TwmWindow *tmp;
{
long supplied = 0;
if (!XGetWMNormalHints (dpy, tmp->w, &tmp->hints, &supplied))
tmp->hints.flags = 0;
if (tmp->hints.flags & PResizeInc) {
if (tmp->hints.width_inc == 0) tmp->hints.width_inc = 1;
if (tmp->hints.height_inc == 0) tmp->hints.height_inc = 1;
}
if (!(supplied & PWinGravity) && (tmp->hints.flags & USPosition)) {
static int gravs[] = { SouthEastGravity, SouthWestGravity,
NorthEastGravity, NorthWestGravity };
int right = tmp->attr.x + tmp->attr.width + 2 * tmp->old_bw;
int bottom = tmp->attr.y + tmp->attr.height + 2 * tmp->old_bw;
tmp->hints.win_gravity =
gravs[((Scr->MyDisplayHeight - bottom < tmp->title_height) ? 0 : 2) |
((Scr->MyDisplayWidth - right < tmp->title_height) ? 0 : 1)];
tmp->hints.flags |= PWinGravity;
}
}
|