-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckPack.c
1298 lines (1204 loc) · 37.2 KB
/
ckPack.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
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
/*
* ckPack.c --
*
* This file contains code to implement the "packer"
* geometry manager.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 Christian Werner
*
* 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"
typedef enum {TOP, BOTTOM, LEFT, RIGHT} Side;
/* For each window that the packer cares about (either because
* the window is managed by the packer or because the window
* has slaves that are managed by the packer), there is a
* structure of the following type:
*/
typedef struct Packer {
CkWindow *winPtr; /* Pointer to window. NULL means that
* the window has been deleted, but the
* packer hasn't had a chance to clean up
* yet because the structure is still in
* use. */
struct Packer *masterPtr; /* Master window within which this window
* is packed (NULL means this window
* isn't managed by the packer). */
struct Packer *nextPtr; /* Next window packed within same
* parent. List is priority-ordered:
* first on list gets packed first. */
struct Packer *slavePtr; /* First in list of slaves packed
* inside this window (NULL means
* no packed slaves). */
Side side; /* Side of parent against which
* this window is packed. */
Ck_Anchor anchor; /* If frame allocated for window is larger
* than window needs, this indicates how
* where to position window in frame. */
int padX, padY; /* Total additional pixels to leave around the
* window (half of this space is left on each
* side). This is space *outside* the window:
* we'll allocate extra space in frame but
* won't enlarge window). */
int iPadX, iPadY; /* Total extra pixels to allocate inside the
* window (half this amount will appear on
* each side). */
int *abortPtr; /* If non-NULL, it means that there is a nested
* call to ArrangePacking already working on
* this window. *abortPtr may be set to 1 to
* abort that nested call. This happens, for
* example, if tkwin or any of its slaves
* is deleted. */
int flags; /* Miscellaneous flags; see below
* for definitions. */
} Packer;
/*
* Flag values for Packer structures:
*
* REQUESTED_REPACK: 1 means a Ck_DoWhenIdle request
* has already been made to repack
* all the slaves of this window.
* FILLX: 1 means if frame allocated for window
* is wider than window needs, expand window
* to fill frame. 0 means don't make window
* any larger than needed.
* FILLY: Same as FILLX, except for height.
* EXPAND: 1 means this window's frame will absorb any
* extra space in the parent window.
* DONT_PROPAGATE: 1 means don't set this window's requested
* size. 0 means if this window is a master
* then Tk will set its requested size to fit
* the needs of its slaves.
*/
#define REQUESTED_REPACK 1
#define FILLX 2
#define FILLY 4
#define EXPAND 8
#define DONT_PROPAGATE 16
/*
* Hash table used to map from CkWindow pointers to corresponding
* Packer structures:
*/
static Tcl_HashTable packerHashTable;
/*
* Have statics in this module been initialized?
*/
static int initialized = 0;
/*
* The following structure is the official type record for the
* packer:
*/
static void PackReqProc _ANSI_ARGS_((ClientData clientData,
CkWindow *winPtr));
static void PackLostSlaveProc _ANSI_ARGS_((ClientData clientData,
CkWindow *winPtr));
static Ck_GeomMgr packerType = {
"pack", /* name */
PackReqProc, /* requestProc */
PackLostSlaveProc, /* lostSlaveProc */
};
/*
* Forward declarations for procedures defined later in this file:
*/
static void ArrangePacking _ANSI_ARGS_((ClientData clientData));
static int ConfigureSlaves _ANSI_ARGS_((Tcl_Interp *interp,
CkWindow *winPtr, int argc, char *argv[]));
static Packer * GetPacker _ANSI_ARGS_((CkWindow *winPtr));
static void PackStructureProc _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
static void Unlink _ANSI_ARGS_((Packer *packPtr));
static int XExpansion _ANSI_ARGS_((Packer *slavePtr,
int cavityWidth));
static int YExpansion _ANSI_ARGS_((Packer *slavePtr,
int cavityHeight));
/*
*--------------------------------------------------------------
*
* Ck_PackCmd --
*
* This procedure is invoked to process the "pack" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
Ck_PackCmd(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 *mainPtr = (CkWindow *) clientData;
size_t length;
int c;
if ((argc >= 2) && (argv[1][0] == '.')) {
return ConfigureSlaves(interp, mainPtr, argc-1, argv+1);
}
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " option arg ?arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)) {
if (argv[2][0] != '.') {
Tcl_AppendResult(interp, "bad argument \"", argv[2],
"\": must be name of window", (char *) NULL);
return TCL_ERROR;
}
return ConfigureSlaves(interp, mainPtr, argc-2, argv+2);
} else if ((c == 'f') && (strncmp(argv[1], "forget", length) == 0)) {
CkWindow *slave;
Packer *slavePtr;
int i;
for (i = 2; i < argc; i++) {
slave = Ck_NameToWindow(interp, argv[i], mainPtr);
if (slave == NULL) {
continue;
}
slavePtr = GetPacker(slave);
if ((slavePtr != NULL) && (slavePtr->masterPtr != NULL)) {
Ck_ManageGeometry(slave, (Ck_GeomMgr *) NULL,
(ClientData) NULL);
Unlink(slavePtr);
Ck_UnmapWindow(slavePtr->winPtr);
}
}
} else if ((c == 'i') && (strncmp(argv[1], "info", length) == 0)) {
register Packer *slavePtr;
CkWindow *slave;
char buffer[300];
static char *sideNames[] = {"top", "bottom", "left", "right"};
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " info window\"", (char *) NULL);
return TCL_ERROR;
}
slave = Ck_NameToWindow(interp, argv[2], mainPtr);
if (slave == NULL) {
return TCL_ERROR;
}
slavePtr = GetPacker(slave);
if (slavePtr->masterPtr == NULL) {
Tcl_AppendResult(interp, "window \"", argv[2],
"\" isn't packed", (char *) NULL);
return TCL_ERROR;
}
Tcl_AppendElement(interp, "-anchor");
Tcl_AppendElement(interp, Ck_NameOfAnchor(slavePtr->anchor));
Tcl_AppendResult(interp, " -expand ",
(slavePtr->flags & EXPAND) ? "1" : "0", " -fill ",
(char *) NULL);
switch (slavePtr->flags & (FILLX|FILLY)) {
case 0:
Tcl_AppendResult(interp, "none", (char *) NULL);
break;
case FILLX:
Tcl_AppendResult(interp, "x", (char *) NULL);
break;
case FILLY:
Tcl_AppendResult(interp, "y", (char *) NULL);
break;
case FILLX|FILLY:
Tcl_AppendResult(interp, "both", (char *) NULL);
break;
}
sprintf(buffer, " -ipadx %d -ipady %d -padx %d -pady %d",
slavePtr->iPadX/2, slavePtr->iPadY/2, slavePtr->padX/2,
slavePtr->padY/2);
Tcl_AppendResult(interp, buffer, " -side ", sideNames[slavePtr->side],
(char *) NULL);
} else if ((c == 'p') && (strncmp(argv[1], "propagate", length) == 0)) {
CkWindow *master;
Packer *masterPtr;
int propagate;
if (argc > 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " propagate window ?boolean?\"", (char *) NULL);
return TCL_ERROR;
}
master = Ck_NameToWindow(interp, argv[2], mainPtr);
if (master == NULL) {
return TCL_ERROR;
}
masterPtr = GetPacker(master);
if (argc == 3) {
if (masterPtr->flags & DONT_PROPAGATE) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("0",-1));;
} else {
Tcl_SetObjResult(interp, Tcl_NewStringObj("1",-1));;
}
return TCL_OK;
}
if (Tcl_GetBoolean(interp, argv[3], &propagate) != TCL_OK) {
return TCL_ERROR;
}
if (propagate) {
masterPtr->flags &= ~DONT_PROPAGATE;
/*
* Repack the master to allow new geometry information to
* propagate upwards to the master's master.
*/
if (masterPtr->abortPtr != NULL) {
*masterPtr->abortPtr = 1;
}
if (!(masterPtr->flags & REQUESTED_REPACK)) {
masterPtr->flags |= REQUESTED_REPACK;
Tk_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
}
} else {
masterPtr->flags |= DONT_PROPAGATE;
}
} else if ((c == 's') && (strncmp(argv[1], "slaves", length) == 0)) {
CkWindow *master;
Packer *masterPtr, *slavePtr;
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " slaves window\"", (char *) NULL);
return TCL_ERROR;
}
master = Ck_NameToWindow(interp, argv[2], mainPtr);
if (master == NULL) {
return TCL_ERROR;
}
masterPtr = GetPacker(master);
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
Tcl_AppendElement(interp, slavePtr->winPtr->pathName);
}
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be configure, forget, info, ",
"propagate, or slaves", (char *) NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* PackReqProc --
*
* This procedure is invoked by Ck_GeometryRequest for
* windows managed by the packer.
*
* Results:
* None.
*
* Side effects:
* Arranges for winPtr, and all its managed siblings, to
* be re-packed at the next idle point.
*
*--------------------------------------------------------------
*/
static void
PackReqProc(clientData, winPtr)
ClientData clientData; /* Packer's information about
* window that got new preferred
* geometry. */
CkWindow *winPtr; /* Other information about the window. */
{
register Packer *packPtr = (Packer *) clientData;
packPtr = packPtr->masterPtr;
if (!(packPtr->flags & REQUESTED_REPACK)) {
packPtr->flags |= REQUESTED_REPACK;
Tk_DoWhenIdle(ArrangePacking, (ClientData) packPtr);
}
}
/*
*--------------------------------------------------------------
*
* PackLostSlaveProc --
*
* 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 packer-related information about the slave.
*
*--------------------------------------------------------------
*/
static void
PackLostSlaveProc(clientData, winPtr)
ClientData clientData; /* Packer structure for slave window that
* was stolen away. */
CkWindow *winPtr; /* Pointer to window. */
{
register Packer *slavePtr = (Packer *) clientData;
if (slavePtr->masterPtr->winPtr != slavePtr->winPtr->parentPtr) {
Ck_UnmaintainGeometry(slavePtr->winPtr, slavePtr->masterPtr->winPtr);
}
Unlink(slavePtr);
Ck_UnmapWindow(slavePtr->winPtr);
}
/*
*--------------------------------------------------------------
*
* ArrangePacking --
*
* This procedure is invoked (using the Tk_DoWhenIdle
* mechanism) to re-layout a set of windows managed by
* the packer. It is invoked at idle time so that a
* series of packer requests can be merged into a single
* layout operation.
*
* Results:
* None.
*
* Side effects:
* The packed slaves of masterPtr may get resized or
* moved.
*
*--------------------------------------------------------------
*/
static void
ArrangePacking(clientData)
ClientData clientData; /* Structure describing parent whose slaves
* are to be re-layed out. */
{
register Packer *masterPtr = (Packer *) clientData;
register Packer *slavePtr;
int cavityX, cavityY, cavityWidth, cavityHeight;
/* These variables keep track of the
* as-yet-unallocated space remaining in
* the middle of the parent window. */
int frameX, frameY, frameWidth, frameHeight;
/* These variables keep track of the frame
* allocated to the current window. */
int x, y, width, height; /* These variables are used to hold the
* actual geometry of the current window. */
int intBWidth; /* Width of internal border in parent window,
* if any. */
int abort; /* May get set to non-zero to abort this
* repacking operation. */
int borderX, borderY;
int maxWidth, maxHeight, tmp;
masterPtr->flags &= ~REQUESTED_REPACK;
/*
* If the parent has no slaves anymore, then don't do anything
* at all: just leave the parent's size as-is.
*/
if (masterPtr->slavePtr == NULL) {
return;
}
/*
* Abort any nested call to ArrangePacking for this window, since
* we'll do everything necessary here, and set up so this call
* can be aborted if necessary.
*/
if (masterPtr->abortPtr != NULL) {
*masterPtr->abortPtr = 1;
}
masterPtr->abortPtr = &abort;
abort = 0;
Ck_Preserve((ClientData) masterPtr);
/*
* Pass #1: scan all the slaves to figure out the total amount
* of space needed. Two separate width and height values are
* computed:
*
* width - Holds the sum of the widths (plus padding) of
* all the slaves seen so far that were packed LEFT
* or RIGHT.
* height - Holds the sum of the heights (plus padding) of
* all the slaves seen so far that were packed TOP
* or BOTTOM.
*
* maxWidth - Gradually builds up the width needed by the master
* to just barely satisfy all the slave's needs. For
* each slave, the code computes the width needed for
* all the slaves so far and updates maxWidth if the
* new value is greater.
* maxHeight - Same as maxWidth, except keeps height info.
*/
intBWidth = (masterPtr->winPtr->flags & CK_BORDER) ? 1 : 0;
width = height = maxWidth = maxHeight = 2*intBWidth;
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
if ((slavePtr->side == TOP) || (slavePtr->side == BOTTOM)) {
tmp = slavePtr->winPtr->reqWidth
+ slavePtr->padX + slavePtr->iPadX + width;
if (tmp > maxWidth) {
maxWidth = tmp;
}
height += slavePtr->winPtr->reqHeight
+ slavePtr->padY + slavePtr->iPadY;
} else {
tmp = slavePtr->winPtr->reqHeight
+ slavePtr->padY + slavePtr->iPadY + height;
if (tmp > maxHeight) {
maxHeight = tmp;
}
width += slavePtr->winPtr->reqWidth
+ slavePtr->padX + slavePtr->iPadX;
}
}
if (width > maxWidth) {
maxWidth = width;
}
if (height > maxHeight) {
maxHeight = height;
}
/*
* If the total amount of space needed in the parent window has
* changed, and if we're propagating geometry information, then
* notify the next geometry manager up and requeue ourselves to
* start again after the parent has had a chance to
* resize us.
*/
if (((maxWidth != masterPtr->winPtr->reqWidth)
|| (maxHeight != masterPtr->winPtr->reqHeight))
&& !(masterPtr->flags & DONT_PROPAGATE)) {
Ck_GeometryRequest(masterPtr->winPtr, maxWidth, maxHeight);
masterPtr->flags |= REQUESTED_REPACK;
Tk_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
goto done;
}
/*
* Pass #2: scan the slaves a second time assigning
* new sizes. The "cavity" variables keep track of the
* unclaimed space in the cavity of the window; this
* shrinks inward as we allocate windows around the
* edges. The "frame" variables keep track of the space
* allocated to the current window and its frame. The
* current window is then placed somewhere inside the
* frame, depending on anchor.
*/
cavityX = cavityY = x = y = intBWidth;
cavityWidth = masterPtr->winPtr->width - 2*intBWidth;
cavityHeight = masterPtr->winPtr->height - 2*intBWidth;
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
if ((slavePtr->side == TOP) || (slavePtr->side == BOTTOM)) {
frameWidth = cavityWidth;
frameHeight = slavePtr->winPtr->reqHeight
+ slavePtr->padY + slavePtr->iPadY;
if (slavePtr->flags & EXPAND) {
frameHeight += YExpansion(slavePtr, cavityHeight);
}
cavityHeight -= frameHeight;
if (cavityHeight < 0) {
frameHeight += cavityHeight;
cavityHeight = 0;
}
frameX = cavityX;
if (slavePtr->side == TOP) {
frameY = cavityY;
cavityY += frameHeight;
} else {
frameY = cavityY + cavityHeight;
}
} else {
frameHeight = cavityHeight;
frameWidth = slavePtr->winPtr->reqWidth
+ slavePtr->padX + slavePtr->iPadX;
if (slavePtr->flags & EXPAND) {
frameWidth += XExpansion(slavePtr, cavityWidth);
}
cavityWidth -= frameWidth;
if (cavityWidth < 0) {
frameWidth += cavityWidth;
cavityWidth = 0;
}
frameY = cavityY;
if (slavePtr->side == LEFT) {
frameX = cavityX;
cavityX += frameWidth;
} else {
frameX = cavityX + cavityWidth;
}
}
/*
* Now that we've got the size of the frame for the window,
* compute the window's actual size and location using the
* fill, padding, and frame factors. The variables "borderX"
* and "borderY" are used to handle the differences between
* old-style packing and the new style (in old-style, iPadX
* and iPadY are always zero and padding is completely ignored
* except when computing frame size).
*/
borderX = slavePtr->padX;
borderY = slavePtr->padY;
width = slavePtr->winPtr->reqWidth + slavePtr->iPadX;
if ((slavePtr->flags & FILLX)
|| (width > (frameWidth - borderX))) {
width = frameWidth - borderX;
}
height = slavePtr->winPtr->reqHeight + slavePtr->iPadY;
if ((slavePtr->flags & FILLY)
|| (height > (frameHeight - borderY))) {
height = frameHeight - borderY;
}
borderX /= 2;
borderY /= 2;
switch (slavePtr->anchor) {
case CK_ANCHOR_N:
x = frameX + (frameWidth - width)/2;
y = frameY + borderY;
break;
case CK_ANCHOR_NE:
x = frameX + frameWidth - width - borderX;
y = frameY + borderY;
break;
case CK_ANCHOR_E:
x = frameX + frameWidth - width - borderX;
y = frameY + (frameHeight - height)/2;
break;
case CK_ANCHOR_SE:
x = frameX + frameWidth - width - borderX;
y = frameY + frameHeight - height - borderY;
break;
case CK_ANCHOR_S:
x = frameX + (frameWidth - width)/2;
y = frameY + frameHeight - height - borderY;
break;
case CK_ANCHOR_SW:
x = frameX + borderX;
y = frameY + frameHeight - height - borderY;
break;
case CK_ANCHOR_W:
x = frameX + borderX;
y = frameY + (frameHeight - height)/2;
break;
case CK_ANCHOR_NW:
x = frameX + borderX;
y = frameY + borderY;
break;
case CK_ANCHOR_CENTER:
x = frameX + (frameWidth - width)/2;
y = frameY + (frameHeight - height)/2;
break;
default:
panic("bad frame factor in ArrangePacking");
}
/*
* The final step is to set the position, size, and mapped/unmapped
* state of the slave.
*/
if (width <= 0 || height <= 0) {
Ck_UnmapWindow(slavePtr->winPtr);
} else {
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);
if (abort)
goto done;
Ck_MapWindow(slavePtr->winPtr);
}
/*
* Changes to the window's structure could cause almost anything
* to happen, including deleting the parent or child. If this
* happens, we'll be told to abort.
*/
if (abort) {
goto done;
}
}
done:
masterPtr->abortPtr = NULL;
Ck_Release((ClientData) masterPtr);
}
/*
*----------------------------------------------------------------------
*
* XExpansion --
*
* Given a list of packed slaves, the first of which is packed
* on the left or right and is expandable, compute how much to
* expand the child.
*
* Results:
* The return value is the number of additional pixels to give to
* the child.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
XExpansion(slavePtr, cavityWidth)
register Packer *slavePtr; /* First in list of remaining
* slaves. */
int cavityWidth; /* Horizontal space left for all
* remaining slaves. */
{
int numExpand, minExpand, curExpand;
int childWidth;
/*
* This procedure is tricky because windows packed top or bottom can
* be interspersed among expandable windows packed left or right.
* Scan through the list, keeping a running sum of the widths of
* all left and right windows (actually, count the cavity space not
* allocated) and a running count of all expandable left and right
* windows. At each top or bottom window, and at the end of the
* list, compute the expansion factor that seems reasonable at that
* point. Return the smallest factor seen at any of these points.
*/
minExpand = cavityWidth;
numExpand = 0;
for ( ; slavePtr != NULL; slavePtr = slavePtr->nextPtr) {
childWidth = slavePtr->winPtr->reqWidth
+ slavePtr->padX + slavePtr->iPadX;
if ((slavePtr->side == TOP) || (slavePtr->side == BOTTOM)) {
curExpand = (cavityWidth - childWidth)/numExpand;
if (curExpand < minExpand) {
minExpand = curExpand;
}
} else {
cavityWidth -= childWidth;
if (slavePtr->flags & EXPAND) {
numExpand++;
}
}
}
curExpand = cavityWidth/numExpand;
if (curExpand < minExpand) {
minExpand = curExpand;
}
return (minExpand < 0) ? 0 : minExpand;
}
/*
*----------------------------------------------------------------------
*
* YExpansion --
*
* Given a list of packed slaves, the first of which is packed
* on the top or bottom and is expandable, compute how much to
* expand the child.
*
* Results:
* The return value is the number of additional pixels to give to
* the child.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
YExpansion(slavePtr, cavityHeight)
register Packer *slavePtr; /* First in list of remaining
* slaves. */
int cavityHeight; /* Vertical space left for all
* remaining slaves. */
{
int numExpand, minExpand, curExpand;
int childHeight;
/*
* See comments for XExpansion.
*/
minExpand = cavityHeight;
numExpand = 0;
for ( ; slavePtr != NULL; slavePtr = slavePtr->nextPtr) {
childHeight = slavePtr->winPtr->reqHeight
+ slavePtr->padY + slavePtr->iPadY;
if ((slavePtr->side == LEFT) || (slavePtr->side == RIGHT)) {
curExpand = (cavityHeight - childHeight)/numExpand;
if (curExpand < minExpand) {
minExpand = curExpand;
}
} else {
cavityHeight -= childHeight;
if (slavePtr->flags & EXPAND) {
numExpand++;
}
}
}
curExpand = cavityHeight/numExpand;
if (curExpand < minExpand) {
minExpand = curExpand;
}
return (minExpand < 0) ? 0 : minExpand;
}
/*
*--------------------------------------------------------------
*
* GetPacker --
*
* This internal procedure is used to locate a Packer
* structure for a given window, creating one if one
* doesn't exist already.
*
* Results:
* The return value is a pointer to the Packer structure
* corresponding to tkwin.
*
* Side effects:
* A new packer structure may be created. If so, then
* a callback is set up to clean things up when the
* window is deleted.
*
*--------------------------------------------------------------
*/
static Packer *
GetPacker(winPtr)
CkWindow *winPtr; /* Pointer to window for which
* packer structure is desired. */
{
register Packer *packPtr;
Tcl_HashEntry *hPtr;
int new;
if (!initialized) {
initialized = 1;
Tcl_InitHashTable(&packerHashTable, TCL_ONE_WORD_KEYS);
}
/*
* See if there's already packer for this window. If not,
* then create a new one.
*/
hPtr = Tcl_CreateHashEntry(&packerHashTable, (char *) winPtr, &new);
if (!new) {
return (Packer *) Tcl_GetHashValue(hPtr);
}
packPtr = (Packer *) ckalloc(sizeof (Packer));
packPtr->winPtr = winPtr;
packPtr->masterPtr = NULL;
packPtr->nextPtr = NULL;
packPtr->slavePtr = NULL;
packPtr->side = TOP;
packPtr->anchor = CK_ANCHOR_CENTER;
packPtr->padX = packPtr->padY = 0;
packPtr->iPadX = packPtr->iPadY = 0;
packPtr->abortPtr = NULL;
packPtr->flags = 0;
Tcl_SetHashValue(hPtr, packPtr);
Ck_CreateEventHandler(winPtr,
CK_EV_DESTROY | CK_EV_MAP | CK_EV_EXPOSE,
PackStructureProc, (ClientData) packPtr);
return packPtr;
}
/*
*----------------------------------------------------------------------
*
* Unlink --
*
* Remove a packer from its parent's list of slaves.
*
* Results:
* None.
*
* Side effects:
* The parent will be scheduled for repacking.
*
*----------------------------------------------------------------------
*/
static void
Unlink(packPtr)
register Packer *packPtr; /* Window to unlink. */
{
register Packer *masterPtr, *packPtr2;
masterPtr = packPtr->masterPtr;
if (masterPtr == NULL) {
return;
}
if (masterPtr->slavePtr == packPtr) {
masterPtr->slavePtr = packPtr->nextPtr;
} else {
for (packPtr2 = masterPtr->slavePtr; ; packPtr2 = packPtr2->nextPtr) {
if (packPtr2 == NULL) {
panic("Unlink couldn't find previous window");
}
if (packPtr2->nextPtr == packPtr) {
packPtr2->nextPtr = packPtr->nextPtr;
break;
}
}
}
if (!(masterPtr->flags & REQUESTED_REPACK)) {
masterPtr->flags |= REQUESTED_REPACK;
Tk_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
}
if (masterPtr->abortPtr != NULL) {
*masterPtr->abortPtr = 1;
}
packPtr->masterPtr = NULL;
}
/*
*----------------------------------------------------------------------
*
* DestroyPacker --
*
* This procedure is invoked by Ck_EventuallyFree or Ck_Release
* to clean up the internal structure of a packer at a safe time
* (when no-one is using it anymore).
*
* Results:
* None.
*
* Side effects:
* Everything associated with the packer is freed up.
*
*----------------------------------------------------------------------
*/
static void
DestroyPacker(clientData)
ClientData clientData; /* Info about packed window that
* is now dead. */
{
register Packer *packPtr = (Packer *) clientData;
ckfree((char *) packPtr);
}
/*
*----------------------------------------------------------------------
*
* PackStructureProc --
*
* This procedure is invoked by the event dispatcher in response
* to CK_EV_MAP/CK_EV_EXPOSE/CK_EV_DESTROY events.
*
* Results:
* None.
*
* Side effects:
* If a window was just deleted, clean up all its packer-related
* information. If it was just resized, repack its slaves, if
* any.
*
*----------------------------------------------------------------------
*/
static void
PackStructureProc(clientData, eventPtr)
ClientData clientData; /* Our information about window
* referred to by eventPtr. */
CkEvent *eventPtr; /* Describes what just happened. */
{
register Packer *packPtr = (Packer *) clientData;
if (eventPtr->type == CK_EV_MAP || eventPtr->type == CK_EV_EXPOSE) {
if ((packPtr->slavePtr != NULL)
&& !(packPtr->flags & REQUESTED_REPACK)) {
packPtr->flags |= REQUESTED_REPACK;
Tk_DoWhenIdle(ArrangePacking, (ClientData) packPtr);
}
} else if (eventPtr->type == CK_EV_DESTROY) {
register Packer *slavePtr, *nextPtr;
if (packPtr->masterPtr != NULL) {
Unlink(packPtr);
}
for (slavePtr = packPtr->slavePtr; slavePtr != NULL;
slavePtr = nextPtr) {
Ck_ManageGeometry(slavePtr->winPtr, (Ck_GeomMgr *) NULL,
(ClientData) NULL);
Ck_UnmapWindow(slavePtr->winPtr);
slavePtr->masterPtr = NULL;
nextPtr = slavePtr->nextPtr;
slavePtr->nextPtr = NULL;
}
Tcl_DeleteHashEntry(Tcl_FindHashEntry(&packerHashTable,
(char *) packPtr->winPtr));
if (packPtr->flags & REQUESTED_REPACK) {
Tk_CancelIdleCall(ArrangePacking, (ClientData) packPtr);
}
packPtr->winPtr = NULL;
Ck_EventuallyFree((ClientData) packPtr, (Ck_FreeProc *) DestroyPacker);
}
}
/*
*----------------------------------------------------------------------
*
* ConfigureSlaves --
*
* This implements the guts of the "pack configure" command. Given
* a list of slaves and configuration options, it arranges for the