-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckStack.c
959 lines (881 loc) · 26.6 KB
/
ckStack.c
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
/*
* ckStack.c --
*
* This file contains code to implement a simple geometry manager
* useful to implement card games. It defines slots where to add
* windows. Slots are defined by their (x,y) coordinates.
*
* Copyright (c) 2019 VCA
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
/*
* For each window whose geometry is managed by the stacker there is
* a structure of the following type:
*/
typedef struct Slot {
char *name;
int x, y; /* X and Y coordinates for winPtr. */
double relX, relY; /* X and Y coordinates relative to size of
* master */
Ck_Anchor anchor; /* Which point on winPtr is placed at the
* given position. */
} Slot;
typedef struct Slave {
CkWindow *winPtr; /* Pointer to window. */
struct Master *masterPtr; /* Pointer to information for window
* relative to which winPtr is placed.
* This isn't necessarily the logical
* parent of winPtr. NULL means the
* master was deleted or never assigned. */
struct Slave *nextPtr; /* Next in list of windows placed relative
* to same master (NULL for end of list). */
/*
* Geometry information for window; where there are both relative
* and absolute values for the same attribute (e.g. x and relX) only
* one of them is actually used, depending on flags.
*/
int x, y; /* X and Y coordinates for winPtr. */
double relX, relY; /* X and Y coordinates relative to size of
* master. */
int width, height; /* Absolute dimensions for winPtr. */
double relWidth, relHeight; /* Dimensions for winPtr relative to size of
* master. */
Ck_Anchor anchor; /* Which point on winPtr is placed at the
* given position. */
int flags; /* Various flags; see below for bit
* definitions. */
} Slave;
/*
* Flag definitions for Slave structures:
*
* CHILD_REL_X - 1 means use relX field; 0 means use x.
* CHILD_REL_Y - 1 means use relY field; 0 means use y;
* CHILD_WIDTH - 1 means use width field;
* CHILD_REL_WIDTH - 1 means use relWidth; if neither this nor
* CHILD_WIDTH is 1, use window's requested
* width.
* CHILD_HEIGHT - 1 means use height field;
* CHILD_REL_HEIGHT - 1 means use relHeight; if neither this nor
* CHILD_HEIGHT is 1, use window's requested
* height.
*/
#define CHILD_REL_X 1
#define CHILD_REL_Y 2
#define CHILD_WIDTH 4
#define CHILD_REL_WIDTH 8
#define CHILD_HEIGHT 0x10
#define CHILD_REL_HEIGHT 0x20
/*
* For each master window that has a slave managed by the stackr there
* is a structure of the following form:
*/
typedef struct Master {
CkWindow *winPtr; /* Pointer to master window. */
struct Slave *slavePtr; /* First in linked list of slaves
* stackd relative to this master. */
int flags; /* See below for bit definitions. */
} Master;
/*
* Flag definitions for masters:
*
* PARENT_RECONFIG_PENDING - 1 means that a call to RecomputeStackment
* is already pending via a Do_When_Idle handler.
*/
#define PARENT_RECONFIG_PENDING 1
/*
* The hash tables below both use CkWindow pointers as keys. They map
* from CkWindows to Slave and Master structures for windows, if they
* exist.
*/
static int initialized = 0;
static Tcl_HashTable masterTable;
static Tcl_HashTable slaveTable;
/*
* The following structure is the official type record for the
* placer:
*/
static void StackRequestProc _ANSI_ARGS_((ClientData clientData,
CkWindow *winPtr));
static void StackLostSlaveProc _ANSI_ARGS_((ClientData clientData,
CkWindow *winPtr));
static Ck_GeomMgr placerType = {
"stack", /* name */
StackRequestProc, /* requestProc */
StackLostSlaveProc, /* lostSlaveProc */
};
/*
* Forward declarations for procedures defined later in this file:
*/
static void SlaveStructureProc _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
static int ConfigureSlave _ANSI_ARGS_((Tcl_Interp *interp,
Slave *slavePtr, int argc, char **argv));
static Slave * FindSlave _ANSI_ARGS_((CkWindow *winPtr));
static Master * FindMaster _ANSI_ARGS_((CkWindow *winPtr));
static void MasterStructureProc _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
static void RecomputePlacement _ANSI_ARGS_((ClientData clientData));
static void UnlinkSlave _ANSI_ARGS_((Slave *slavePtr));
/*
*--------------------------------------------------------------
*
* Ck_StackCmd --
*
* This procedure is invoked to process the "stack" Tcl
* commands. See the user documentation for details on
* what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
Ck_StackCmd(clientData, interp, argc, argv)
ClientData clientData; /* Main window associated with interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
CkWindow *winPtr;
Slave *slavePtr;
Tcl_HashEntry *hPtr;
int length;
char c;
/*
* Initialize, if that hasn't been done yet.
*/
if (!initialized) {
Tcl_InitHashTable(&masterTable, TCL_ONE_WORD_KEYS);
Tcl_InitHashTable(&slaveTable, TCL_ONE_WORD_KEYS);
initialized = 1;
}
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " option|pathName args", (char *) NULL);
return TCL_ERROR;
}
c = argv[1][0];
length = strlen(argv[1]);
/*
* Handle special shortcut where window name is first argument.
*/
if (c == '.') {
winPtr = Ck_NameToWindow(interp, argv[1], (CkWindow *) clientData);
if (winPtr == NULL) {
return TCL_ERROR;
}
slavePtr = FindSlave(winPtr);
return ConfigureSlave(interp, slavePtr, argc-2, argv+2);
}
/*
* Handle more general case of option followed by window name followed
* by possible additional arguments.
*/
winPtr = Ck_NameToWindow(interp, argv[2], (CkWindow *) clientData);
if (winPtr == NULL) {
return TCL_ERROR;
}
if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)) {
if (argc < 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0],
" configure pathName option value ?option value ...?\"",
(char *) NULL);
return TCL_ERROR;
}
slavePtr = FindSlave(winPtr);
return ConfigureSlave(interp, slavePtr, argc-3, argv+3);
} else if ((c == 'f') && (strncmp(argv[1], "forget", length) == 0)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " forget pathName\"", (char *) NULL);
return TCL_ERROR;
}
hPtr = Tcl_FindHashEntry(&slaveTable, (char *) winPtr);
if (hPtr == NULL) {
return TCL_OK;
}
slavePtr = (Slave *) Tcl_GetHashValue(hPtr);
UnlinkSlave(slavePtr);
Tcl_DeleteHashEntry(hPtr);
Ck_DeleteEventHandler(winPtr, CK_EV_MAP | CK_EV_EXPOSE | CK_EV_DESTROY,
SlaveStructureProc, (ClientData) slavePtr);
Ck_ManageGeometry(winPtr, (Ck_GeomMgr *) NULL, (ClientData) NULL);
Ck_UnmapWindow(winPtr);
ckfree((char *) slavePtr);
} else if ((c == 'i') && (strncmp(argv[1], "info", length) == 0)) {
char buffer[50];
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " info pathName\"", (char *) NULL);
return TCL_ERROR;
}
hPtr = Tcl_FindHashEntry(&slaveTable, (char *) winPtr);
if (hPtr == NULL) {
return TCL_OK;
}
slavePtr = (Slave *) Tcl_GetHashValue(hPtr);
if (slavePtr->flags & CHILD_REL_X) {
sprintf(buffer, "-relx %.4g", slavePtr->relX);
} else {
sprintf(buffer, "-x %d", slavePtr->x);
}
Tcl_AppendResult(interp, buffer, (char *) NULL);
if (slavePtr->flags & CHILD_REL_Y) {
sprintf(buffer, " -rely %.4g", slavePtr->relY);
} else {
sprintf(buffer, " -y %d", slavePtr->y);
}
Tcl_AppendResult(interp, buffer, (char *) NULL);
if (slavePtr->flags & CHILD_REL_WIDTH) {
sprintf(buffer, " -relwidth %.4g", slavePtr->relWidth);
Tcl_AppendResult(interp, buffer, (char *) NULL);
} else if (slavePtr->flags & CHILD_WIDTH) {
sprintf(buffer, " -width %d", slavePtr->width);
Tcl_AppendResult(interp, buffer, (char *) NULL);
}
if (slavePtr->flags & CHILD_REL_HEIGHT) {
sprintf(buffer, " -relheight %.4g", slavePtr->relHeight);
Tcl_AppendResult(interp, buffer, (char *) NULL);
} else if (slavePtr->flags & CHILD_HEIGHT) {
sprintf(buffer, " -height %d", slavePtr->height);
Tcl_AppendResult(interp, buffer, (char *) NULL);
}
Tcl_AppendResult(interp, " -anchor ", Ck_NameOfAnchor(slavePtr->anchor),
(char *) NULL);
if (slavePtr->borderMode == BM_IGNORE) {
Tcl_AppendResult(interp, " -bordermode ignore", (char *) NULL);
}
} else if ((c == 's') && (strncmp(argv[1], "slaves", length) == 0)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " slaves pathName\"", (char *) NULL);
return TCL_ERROR;
}
hPtr = Tcl_FindHashEntry(&masterTable, (char *) winPtr);
if (hPtr != NULL) {
Master *masterPtr;
masterPtr = (Master *) Tcl_GetHashValue(hPtr);
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
Tcl_AppendElement(interp, slavePtr->winPtr->pathName);
}
}
} else {
Tcl_AppendResult(interp, "unknown or ambiguous option \"", argv[1],
"\": must be configure, forget, info, or slaves",
(char *) NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* FindSlave --
*
* Given a CkWindow *, find the Slave structure corresponding
* to that window (making a new one if necessary).
*
* Results:
* None.
*
* Side effects:
* A new Slave structure may be created.
*
*----------------------------------------------------------------------
*/
static Slave *
FindSlave(winPtr)
CkWindow *winPtr; /* Pointer to desired slave. */
{
Tcl_HashEntry *hPtr;
Slave *slavePtr;
int new;
hPtr = Tcl_CreateHashEntry(&slaveTable, (char *) winPtr, &new);
if (new) {
slavePtr = (Slave *) ckalloc(sizeof (Slave));
slavePtr->winPtr = winPtr;
slavePtr->masterPtr = NULL;
slavePtr->nextPtr = NULL;
slavePtr->x = slavePtr->y = 0;
slavePtr->relX = slavePtr->relY = 0.0;
slavePtr->width = slavePtr->height = 0;
slavePtr->relWidth = slavePtr->relHeight = 0.0;
slavePtr->anchor = CK_ANCHOR_NW;
slavePtr->borderMode = BM_INSIDE;
slavePtr->flags = 0;
Tcl_SetHashValue(hPtr, slavePtr);
Ck_CreateEventHandler(winPtr, CK_EV_MAP | CK_EV_EXPOSE | CK_EV_DESTROY,
SlaveStructureProc, (ClientData) slavePtr);
Ck_ManageGeometry(winPtr, &placerType, (ClientData) slavePtr);
} else {
slavePtr = (Slave *) Tcl_GetHashValue(hPtr);
}
return slavePtr;
}
/*
*----------------------------------------------------------------------
*
* UnlinkSlave --
*
* This procedure removes a slave window from the chain of slaves
* in its master.
*
* Results:
* None.
*
* Side effects:
* The slave list of slavePtr's master changes.
*
*----------------------------------------------------------------------
*/
static void
UnlinkSlave(slavePtr)
Slave *slavePtr; /* Slave structure to be unlinked. */
{
register Master *masterPtr;
register Slave *prevPtr;
masterPtr = slavePtr->masterPtr;
if (masterPtr == NULL) {
return;
}
if (masterPtr->slavePtr == slavePtr) {
masterPtr->slavePtr = slavePtr->nextPtr;
} else {
for (prevPtr = masterPtr->slavePtr; ;
prevPtr = prevPtr->nextPtr) {
if (prevPtr == NULL) {
panic("UnlinkSlave couldn't find slave to unlink");
}
if (prevPtr->nextPtr == slavePtr) {
prevPtr->nextPtr = slavePtr->nextPtr;
break;
}
}
}
slavePtr->masterPtr = NULL;
}
/*
*----------------------------------------------------------------------
*
* FindMaster --
*
* Given a CkWindow *, find the Master structure corresponding
* to that window (making a new one if necessary).
*
* Results:
* None.
*
* Side effects:
* A new Master structure may be created.
*
*----------------------------------------------------------------------
*/
static Master *
FindMaster(winPtr)
CkWindow *winPtr; /* Pointer to desired master. */
{
Tcl_HashEntry *hPtr;
Master *masterPtr;
int new;
hPtr = Tcl_CreateHashEntry(&masterTable, (char *) winPtr, &new);
if (new) {
masterPtr = (Master *) ckalloc(sizeof (Master));
masterPtr->winPtr = winPtr;
masterPtr->slavePtr = NULL;
masterPtr->flags = 0;
Tcl_SetHashValue(hPtr, masterPtr);
/*
* Special case: for toplevels winPtr is NULL,
* therefore don't create event handler.
*/
if (winPtr != NULL)
Ck_CreateEventHandler(masterPtr->winPtr,
CK_EV_MAP | CK_EV_EXPOSE | CK_EV_DESTROY,
MasterStructureProc, (ClientData) masterPtr);
} else {
masterPtr = (Master *) Tcl_GetHashValue(hPtr);
}
return masterPtr;
}
/*
*----------------------------------------------------------------------
*
* ConfigureSlave --
*
* This procedure is called to process an argv/argc list to
* reconfigure the placement of a window.
*
* Results:
* A standard Tcl result. If an error occurs then a message is
* left in interp->result.
*
* Side effects:
* Information in slavePtr may change, and slavePtr's master is
* scheduled for reconfiguration.
*
*----------------------------------------------------------------------
*/
static int
ConfigureSlave(interp, slavePtr, argc, argv)
Tcl_Interp *interp; /* Used for error reporting. */
Slave *slavePtr; /* Pointer to current information
* about slave. */
int argc; /* Number of config arguments. */
char **argv; /* String values for arguments. */
{
Master *masterPtr;
int c, length, result;
double d;
result = TCL_OK;
for ( ; argc > 0; argc -= 2, argv += 2) {
if (argc < 2) {
Tcl_AppendResult(interp, "extra option \"", argv[0],
"\" (option with no value?)", (char *) NULL);
result = TCL_ERROR;
goto done;
}
length = strlen(argv[0]);
c = argv[0][1];
if ((c == 'a') && (strncmp(argv[0], "-anchor", length) == 0)) {
if (Ck_GetAnchor(interp, argv[1], &slavePtr->anchor) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
} else if ((c == 'b')
&& (strncmp(argv[0], "-bordermode", length) == 0)) {
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'i') && (strncmp(argv[1], "ignore", length) == 0)
&& (length >= 2)) {
slavePtr->borderMode = BM_IGNORE;
} else if ((c == 'i') && (strncmp(argv[1], "inside", length) == 0)
&& (length >= 2)) {
slavePtr->borderMode = BM_INSIDE;
} else {
Tcl_AppendResult(interp, "bad border mode \"", argv[1],
"\": must be ignore or inside",
(char *) NULL);
result = TCL_ERROR;
goto done;
}
} else if ((c == 'h') && (strncmp(argv[0], "-height", length) == 0)) {
if (argv[1][0] == 0) {
slavePtr->flags &= ~(CHILD_REL_HEIGHT|CHILD_HEIGHT);
} else {
if (Ck_GetCoord(interp, slavePtr->winPtr, argv[1],
&slavePtr->height) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->flags &= ~CHILD_REL_HEIGHT;
slavePtr->flags |= CHILD_HEIGHT;
}
} else if ((c == 'r') && (strncmp(argv[0], "-relheight", length) == 0)
&& (length >= 5)) {
if (Tcl_GetDouble(interp, argv[1], &d) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->relHeight = d;
slavePtr->flags |= CHILD_REL_HEIGHT;
slavePtr->flags &= ~CHILD_HEIGHT;
} else if ((c == 'r') && (strncmp(argv[0], "-relwidth", length) == 0)
&& (length >= 5)) {
if (Tcl_GetDouble(interp, argv[1], &d) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->relWidth = d;
slavePtr->flags |= CHILD_REL_WIDTH;
slavePtr->flags &= ~CHILD_WIDTH;
} else if ((c == 'r') && (strncmp(argv[0], "-relx", length) == 0)
&& (length >= 5)) {
if (Tcl_GetDouble(interp, argv[1], &d) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->relX = d;
slavePtr->flags |= CHILD_REL_X;
} else if ((c == 'r') && (strncmp(argv[0], "-rely", length) == 0)
&& (length >= 5)) {
if (Tcl_GetDouble(interp, argv[1], &d) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->relY = d;
slavePtr->flags |= CHILD_REL_Y;
} else if ((c == 'w') && (strncmp(argv[0], "-width", length) == 0)) {
if (argv[1][0] == 0) {
slavePtr->flags &= ~(CHILD_REL_WIDTH|CHILD_WIDTH);
} else {
if (Ck_GetCoord(interp, slavePtr->winPtr, argv[1],
&slavePtr->width) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->flags &= ~CHILD_REL_WIDTH;
slavePtr->flags |= CHILD_WIDTH;
}
} else if ((c == 'x') && (strncmp(argv[0], "-x", length) == 0)) {
if (Ck_GetCoord(interp, slavePtr->winPtr, argv[1],
&slavePtr->x) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->flags &= ~CHILD_REL_X;
} else if ((c == 'y') && (strncmp(argv[0], "-y", length) == 0)) {
if (Ck_GetCoord(interp, slavePtr->winPtr, argv[1],
&slavePtr->y) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
slavePtr->flags &= ~CHILD_REL_Y;
} else {
Tcl_AppendResult(interp, "unknown or ambiguous option \"",
argv[0], "\": must be -anchor, -bordermode, -height, ",
"-relheight, -relwidth, -relx, -rely, -width, ",
"-x, or -y", (char *) NULL);
result = TCL_ERROR;
goto done;
}
}
/*
* Arrange for a placement recalculation in the master.
*/
done:
masterPtr = slavePtr->masterPtr;
if (masterPtr == NULL) {
masterPtr = FindMaster((slavePtr->winPtr->flags & CK_TOPLEVEL) ?
NULL : slavePtr->winPtr->parentPtr);
slavePtr->masterPtr = masterPtr;
slavePtr->nextPtr = masterPtr->slavePtr;
masterPtr->slavePtr = slavePtr;
}
if (!(masterPtr->flags & PARENT_RECONFIG_PENDING)) {
masterPtr->flags |= PARENT_RECONFIG_PENDING;
Tk_DoWhenIdle(RecomputePlacement, (ClientData) masterPtr);
}
return result;
}
/*
*----------------------------------------------------------------------
*
* RecomputePlacement --
*
* This procedure is called as a when-idle handler. It recomputes
* the geometries of all the slaves of a given master.
*
* Results:
* None.
*
* Side effects:
* Windows may change size or shape.
*
*----------------------------------------------------------------------
*/
static void
RecomputePlacement(clientData)
ClientData clientData; /* Pointer to Master record. */
{
Master *masterPtr = (Master *) clientData;
Slave *slavePtr;
CkWindow *ancestor, *realMaster;
int x, y, width, height;
int masterWidth, masterHeight, masterBW;
masterPtr->flags &= ~PARENT_RECONFIG_PENDING;
/*
* Iterate over all the slaves for the master. Each slave's
* geometry can be computed independently of the other slaves.
*/
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
/*
* Step 1: compute size and borderwidth of master, taking into
* account desired border mode.
*/
masterBW = 0;
/*
* Special case: masterPtr->winPtr == NULL, use entire screen !
*/
if (masterPtr->winPtr == NULL) {
masterWidth = slavePtr->winPtr->mainPtr->maxWidth;
masterHeight = slavePtr->winPtr->mainPtr->maxHeight;
} else {
masterWidth = masterPtr->winPtr->width;
masterHeight = masterPtr->winPtr->height;
if (slavePtr->borderMode == BM_INSIDE) {
masterBW = (masterPtr->winPtr->flags & CK_BORDER) ? 1 : 0;
}
masterWidth -= 2*masterBW;
masterHeight -= 2*masterBW;
}
/*
* Step 2: compute size of slave (outside dimensions including
* border) and location of anchor point within master.
*/
x = slavePtr->x;
if (slavePtr->flags & CHILD_REL_X) {
x = (int) ((slavePtr->relX*masterWidth) +
((slavePtr->relX > 0) ? 0.5 : -0.5));
}
x += masterBW;
y = slavePtr->y;
if (slavePtr->flags & CHILD_REL_Y) {
y = (int) ((slavePtr->relY*masterHeight) +
((slavePtr->relY > 0) ? 0.5 : -0.5));
}
y += masterBW;
if (slavePtr->flags & CHILD_REL_WIDTH) {
width = (int) ((slavePtr->relWidth*masterWidth) + 0.5);
} else if (slavePtr->flags & CHILD_WIDTH) {
width = slavePtr->width;
} else {
width = slavePtr->winPtr->reqWidth;
}
if (slavePtr->flags & CHILD_REL_HEIGHT) {
height = (int) ((slavePtr->relHeight*masterHeight) + 0.5);
} else if (slavePtr->flags & CHILD_HEIGHT) {
height = slavePtr->height;
} else {
height = slavePtr->winPtr->reqHeight;
}
/*
* Step 3: adjust the x and y positions so that the desired
* anchor point on the slave appears at that position. Also
* adjust for the border mode and master's border.
*/
switch (slavePtr->anchor) {
case CK_ANCHOR_N:
x -= width/2;
break;
case CK_ANCHOR_NE:
x -= width;
break;
case CK_ANCHOR_E:
x -= width;
y -= height/2;
break;
case CK_ANCHOR_SE:
x -= width;
y -= height;
break;
case CK_ANCHOR_S:
x -= width/2;
y -= height;
break;
case CK_ANCHOR_SW:
y -= height;
break;
case CK_ANCHOR_W:
y -= height/2;
break;
case CK_ANCHOR_NW:
break;
case CK_ANCHOR_CENTER:
x -= width/2;
y -= height/2;
break;
}
/*
* Step 4: if masterPtr isn't actually the master of slavePtr,
* then translate the x and y coordinates back into the coordinate
* system of masterPtr.
*/
for (ancestor = masterPtr->winPtr,
realMaster = slavePtr->winPtr->parentPtr;
ancestor != NULL && ancestor != realMaster;
ancestor = ancestor->parentPtr) {
x += ancestor->x;
y += ancestor->y;
}
/*
* Step 5: adjust width and height again to reflect inside dimensions
* of window rather than outside. Also make sure that the width and
* height aren't zero.
*/
if (width <= 0) {
width = 1;
}
if (height <= 0) {
height = 1;
}
/*
* Step 6: see if the window's size or location has changed; if
* so then resize and/or move it.
*/
if (width != slavePtr->winPtr->width ||
height != slavePtr->winPtr->height)
Ck_ResizeWindow(slavePtr->winPtr, width, height);
if (x != slavePtr->winPtr->x ||
y != slavePtr->winPtr->y)
Ck_MoveWindow(slavePtr->winPtr, x, y);
/*
* Temporary kludge til Ck_MoveResizeWindow available !!!
*/
if (width != slavePtr->winPtr->width ||
height != slavePtr->winPtr->height)
Ck_ResizeWindow(slavePtr->winPtr, width, height);
Ck_MapWindow(slavePtr->winPtr);
}
}
/*
*----------------------------------------------------------------------
*
* MasterStructureProc --
*
* This procedure is invoked by the event handler when
* CK_EV_MAP/CK_EV_EXPOSE/CK_EV_DESTROY events occur for
* a master window.
*
* Results:
* None.
*
* Side effects:
* Structures get cleaned up if the window was deleted. If the
* window was resized then slave geometries get recomputed.
*
*----------------------------------------------------------------------
*/
static void
MasterStructureProc(clientData, eventPtr)
ClientData clientData; /* Pointer to Master structure for window
* referred to by eventPtr. */
CkEvent *eventPtr; /* Describes what just happened. */
{
Master *masterPtr = (Master *) clientData;
Slave *slavePtr, *nextPtr;
if (eventPtr->type == CK_EV_EXPOSE ||
eventPtr->type == CK_EV_MAP) {
if ((masterPtr->slavePtr != NULL)
&& !(masterPtr->flags & PARENT_RECONFIG_PENDING)) {
masterPtr->flags |= PARENT_RECONFIG_PENDING;
Tk_DoWhenIdle(RecomputePlacement, (ClientData) masterPtr);
}
} else if (eventPtr->type == CK_EV_DESTROY) {
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = nextPtr) {
slavePtr->masterPtr = NULL;
nextPtr = slavePtr->nextPtr;
slavePtr->nextPtr = NULL;
}
Tcl_DeleteHashEntry(Tcl_FindHashEntry(&masterTable,
(char *) masterPtr->winPtr));
if (masterPtr->flags & PARENT_RECONFIG_PENDING) {
Tk_CancelIdleCall(RecomputePlacement, (ClientData) masterPtr);
}
masterPtr->winPtr = NULL;
ckfree((char *) masterPtr);
}
}
/*
*----------------------------------------------------------------------
*
* SlaveStructureProc --
*
* This procedure is invoked by the event handler when
* CK_EV_MAP/CK_EV_EXPOSE/CK_EV_DESTROY events occur for a
* slave window.
*
* Results:
* None.
*
* Side effects:
* Structures get cleaned up if the window was deleted.
*
*----------------------------------------------------------------------
*/
static void
SlaveStructureProc(clientData, eventPtr)
ClientData clientData; /* Pointer to Slave structure for window
* referred to by eventPtr. */
CkEvent *eventPtr; /* Describes what just happened. */
{
Slave *slavePtr = (Slave *) clientData;
if (eventPtr->type == CK_EV_DESTROY) {
UnlinkSlave(slavePtr);
Tcl_DeleteHashEntry(Tcl_FindHashEntry(&slaveTable,
(char *) slavePtr->winPtr));
ckfree((char *) slavePtr);
}
}
/*
*----------------------------------------------------------------------
*
* StackRequestProc --
*
* This procedure is invoked whenever a slave managed by us
* changes its requested geometry.
*
* Results:
* None.
*
* Side effects:
* The window will get relayed out, if its requested size has
* anything to do with its actual size.
*
*----------------------------------------------------------------------
*/
static void
StackRequestProc(clientData, winPtr)
ClientData clientData; /* Pointer to our record for slave. */
CkWindow *winPtr; /* Window that changed its desired
* size. */
{
Slave *slavePtr = (Slave *) clientData;
Master *masterPtr;
if (((slavePtr->flags & (CHILD_WIDTH|CHILD_REL_WIDTH)) != 0)
&& ((slavePtr->flags & (CHILD_HEIGHT|CHILD_REL_HEIGHT)) != 0)) {
return;
}
masterPtr = slavePtr->masterPtr;
if (masterPtr == NULL) {
return;
}
if (!(masterPtr->flags & PARENT_RECONFIG_PENDING)) {
masterPtr->flags |= PARENT_RECONFIG_PENDING;
Tk_DoWhenIdle(RecomputePlacement, (ClientData) masterPtr);
}
}
/*
*--------------------------------------------------------------
*
* PlaceLostSlaveProc --
*
* This procedure is invoked whenever some other geometry
* claims control over a slave that used to be managed by us.
*
* Results:
* None.
*
* Side effects:
* Forgets all placer-related information about the slave.
*
*--------------------------------------------------------------
*/
static void
PlaceLostSlaveProc(clientData, winPtr)
ClientData clientData; /* Slave structure for slave window that
* was stolen away. */
CkWindow *winPtr; /* Slave window. */
{
register Slave *slavePtr = (Slave *) clientData;
if (slavePtr->masterPtr->winPtr != slavePtr->winPtr->parentPtr) {
Ck_UnmaintainGeometry(slavePtr->winPtr, slavePtr->masterPtr->winPtr);
}
Ck_UnmapWindow(winPtr);
UnlinkSlave(slavePtr);
Tcl_DeleteHashEntry(Tcl_FindHashEntry(&slaveTable, (char *) winPtr));
Ck_DeleteEventHandler(winPtr, CK_EV_MAP | CK_EV_EXPOSE | CK_EV_DESTROY,
SlaveStructureProc, (ClientData) slavePtr);
ckfree((char *) slavePtr);
}