-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckGrid.c
2097 lines (1889 loc) · 58 KB
/
ckGrid.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
/*
* ckGrid.c --
*
* Grid based geometry manager.
*
* Copyright (c) 1996 by Sun Microsystems, Inc.
*
* 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"
/*
* LayoutInfo structure. We shouldn't be using hard-wired limits!
*/
#define MAXGRIDSIZE 128
#ifndef MAXINT
# define MAXINT 0x7fff
#endif
#define MINWEIGHT 0.0001 /* weight totals < this are considered to be zero */
/*
* Special characters to support relative layouts
*/
#define REL_SKIP 'x' /* skip this column */
#define REL_HORIZ '-' /* extend previous widget horizontally */
#define REL_VERT '^' /* extend previous widget verticallly */
/*
* structure to hold collected constraints temporarily:
* needs to use a "Constrain" thingy
*/
typedef struct {
int width, height; /* number of cells horizontally, vertically */
int lastRow; /* last cell with a window in it */
int minWidth[MAXGRIDSIZE]; /* largest minWidth in each column */
int minHeight[MAXGRIDSIZE]; /* largest minHeight in each row */
double weightX[MAXGRIDSIZE];/* largest weight in each column */
double weightY[MAXGRIDSIZE];/* largest weight in each row */
} LayoutInfo;
/* structure for holding row and column constraints */
typedef struct {
int used; /* maximum element used */
int max; /* maximum element allocated */
int *minsize; /* array of minimum column/row sizes */
double *weight; /* array of column/row weights */
} Constrain;
/* For each window that the gridbag cares about (either because
* the window is managed by the gridbag or because the window
* has slaves that are managed by the gridbag), there is a
* structure of the following type:
*/
typedef struct GridBag {
CkWindow *winPtr; /* Pointer to window. NULL means that
* the window has been deleted, but the
* packet hasn't had a chance to clean up
* yet because the structure is still in
* use. */
struct GridBag *masterPtr; /* Master window within which this window
* is managed (NULL means this window
* isn't managed by the gridbag). */
struct GridBag *nextPtr; /* Next window managed within same
* parent. List is priority-ordered:
* first on list gets layed out first. */
struct GridBag *slavePtr; /* First in list of slaves managed
* inside this window (NULL means
* no gridbag slaves). */
int gridColumn, gridRow;
int gridWidth, gridHeight;
int tempX, tempY;
int tempWidth, tempHeight;
double weightX, weightY;
int minWidth, minHeight;
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 startx, starty; /* starting location of layout */
int *abortPtr; /* If non-NULL, it means that there is a nested
* call to ArrangeGrid already working on
* this window. *abortPtr may be set to 1 to
* abort that nested call. This happens, for
* example, if winPtr or any of its slaves
* is deleted. */
int flags; /* Miscellaneous flags; see below
* for definitions. */
Constrain row, column; /* column and row constraints */
int valid;
LayoutInfo *layoutCache;
} GridBag;
/*
* Flag values for GridBag structures:
*
* REQUESTED_RELAYOUT: 1 means a Tk_DoWhenIdle request
* has already been made to re-arrange
* all the slaves of this window.
* STICK_NORTH 1 means this window sticks to the edgth of its
* STICK_EAST cavity
* STICK_SOUTH
* STICK_WEST
*
* DONT_PROPAGATE: 1 means don't set this window's requested
* size. 0 means if this window is a master
* then Ck will set its requested size to fit
* the needs of its slaves.
*/
#define STICK_NORTH 1
#define STICK_EAST 2
#define STICK_SOUTH 4
#define STICK_WEST 8
#define STICK_ALL (STICK_NORTH|STICK_EAST|STICK_SOUTH|STICK_WEST)
#define REQUESTED_RELAYOUT 16
#define DONT_PROPAGATE 32
/*
* Hash table used to map from CkWindow pointers to corresponding
* GridBag structures:
*/
static Tcl_HashTable gridBagHashTable;
/*
* Have statics in this module been initialized?
*/
static int initialized = 0;
/*
* Prototypes for procedures used only in this file:
*/
static void ArrangeGrid _ANSI_ARGS_((ClientData clientData));
static int ConfigureSlaves _ANSI_ARGS_((Tcl_Interp *interp,
CkWindow *winPtr, int argc, char *argv[]));
static void DestroyGridBag _ANSI_ARGS_((char *memPtr));
static void GetCachedLayoutInfo _ANSI_ARGS_((GridBag *masterPtr));
static GridBag * GetGridBag _ANSI_ARGS_((CkWindow *winPtr));
static void GetLayoutInfo _ANSI_ARGS_((GridBag *masterPtr,
LayoutInfo *r));
static void GetMinSize _ANSI_ARGS_((GridBag *masterPtr,
LayoutInfo *info, int *minw, int *minh));
static void GridBagStructureProc _ANSI_ARGS_((
ClientData clientData, CkEvent *eventPtr));
static void GridLostSlaveProc _ANSI_ARGS_((ClientData clientData,
CkWindow *winPtr));
static void GridReqProc _ANSI_ARGS_((ClientData clientData,
CkWindow *winPtr));
static void GridBagStructureProc _ANSI_ARGS_((
ClientData clientData, CkEvent *eventPtr));
static void StickyToString _ANSI_ARGS_((int flags, char *result));
static int StringToSticky _ANSI_ARGS_((char *string));
static void Unlink _ANSI_ARGS_((GridBag *gridPtr));
static Ck_GeomMgr gridMgrType = {
"grid", /* name */
GridReqProc, /* requestProc */
GridLostSlaveProc, /* lostSlaveProc */
};
/*
*--------------------------------------------------------------
*
* Ck_GridCmd --
*
* This procedure is invoked to process the "grid" 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_GridCmd(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 = (CkWindow *) clientData;
size_t length;
char c;
if ((argc >= 2) && (argv[1][0] == '.')) {
return ConfigureSlaves(interp, winPtr, 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 == 'b') && (strncmp(argv[1], "bbox", length) == 0)) {
CkWindow *master;
GridBag *masterPtr;
int row, column;
int i, x, y;
int prevX, prevY;
int width, height;
double weight;
int diff;
char buf[256];
if (argc != 5) {
Tcl_AppendResult(interp, "Wrong number of arguments: ",
"must be \"",argv[0],
" bbox <master> <column> <row>\"", (char *) NULL);
return TCL_ERROR;
}
master = Ck_NameToWindow(interp, argv[2], winPtr);
if (master == NULL) {
return TCL_ERROR;
}
if (Tcl_GetInt(interp, argv[3], &column) != TCL_OK) {
return TCL_ERROR;
}
if (Tcl_GetInt(interp, argv[4], &row) != TCL_OK) {
return TCL_ERROR;
}
masterPtr = GetGridBag(master);
/* make sure the grid is up to snuff */
while ((masterPtr->flags & REQUESTED_RELAYOUT)) {
Tk_CancelIdleCall(ArrangeGrid, (ClientData) masterPtr);
ArrangeGrid((ClientData) masterPtr);
}
GetCachedLayoutInfo(masterPtr);
if (row < 0 || column < 0) {
return TCL_OK;
}
if (column >= masterPtr->layoutCache->width ||
row >= masterPtr->layoutCache->height) {
return TCL_OK;
}
x = masterPtr->startx;
y = masterPtr->starty;
GetMinSize(masterPtr, masterPtr->layoutCache, &width, &height);
diff = masterPtr->winPtr->width - (width + masterPtr->iPadX);
for (weight=0.0, i=0; i<masterPtr->layoutCache->width; i++)
weight += masterPtr->layoutCache->weightX[i];
prevX = 0; /* Needed to prevent gcc warning. */
for (i=0; i<=column; i++) {
int dx = 0;
if (weight > MINWEIGHT) {
dx = (int)((((double)diff) *
masterPtr->layoutCache->weightX[i]) / weight);
}
prevX = x;
x += masterPtr->layoutCache->minWidth[i] + dx;
}
diff = masterPtr->winPtr->height - (height + masterPtr->iPadY);
for (weight=0.0, i=0; i<masterPtr->layoutCache->width; i++) {
weight += masterPtr->layoutCache->weightY[i];
}
prevY = 0; /* Needed to prevent gcc warning. */
for (i=0; i<=row; i++) {
int dy = 0;
if (weight > MINWEIGHT) {
dy = (int)((((double)diff) *
masterPtr->layoutCache->weightY[i]) / weight);
}
prevY = y;
y += masterPtr->layoutCache->minHeight[i] + dy;
}
sprintf(buf,"%d %d %d %d",prevX,prevY,x - prevX,y - prevY);
Tcl_SetObjResult(interp, Tcl_NewStringObj(buf,-1));
} else 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, winPtr, argc-2, argv+2);
} else if ((c == 'f') && (strncmp(argv[1], "forget", length) == 0)) {
CkWindow *slave;
GridBag *slavePtr;
int i;
for (i = 2; i < argc; i++) {
slave = Ck_NameToWindow(interp, argv[i], winPtr);
if (slave == NULL) {
return TCL_ERROR;
}
slavePtr = GetGridBag(slave);
if (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)) {
GridBag *slavePtr;
CkWindow *slave;
char buffer[64];
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], winPtr);
if (slave == NULL) {
return TCL_ERROR;
}
slavePtr = GetGridBag(slave);
if (slavePtr->masterPtr == NULL) {
return TCL_OK;
}
#if 0
Tcl_AppendElement(interp, "-in");
Tcl_AppendElement(interp, slavePtr->masterPtr->winPtr->pathName);
#endif
sprintf(buffer, " -column %d -row %d -columnspan %d -rowspan %d",
slavePtr->gridColumn, slavePtr->gridRow,
slavePtr->gridWidth, slavePtr->gridHeight);
Tcl_AppendResult(interp, buffer, (char *) NULL);
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, (char *) NULL);
StickyToString(slavePtr->flags,buffer);
Tcl_AppendResult(interp, " -sticky ", buffer, (char *) NULL);
#if 0
sprintf(buffer, " -weightx %.2f -weighty %.2f",
slavePtr->weightX, slavePtr->weightY);
Tcl_AppendResult(interp, buffer, (char *) NULL);
#endif
} else if ((c == 'p') && (strncmp(argv[1], "propagate", length) == 0)) {
CkWindow *master;
GridBag *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], winPtr);
if (master == NULL) {
return TCL_ERROR;
}
masterPtr = GetGridBag(master);
if (argc == 3) {
Tcl_SetObjResult( interp, Tcl_NewStringObj((masterPtr->flags & DONT_PROPAGATE) ? "0" : "1", -1));
return TCL_OK;
}
if (Tcl_GetBoolean(interp, argv[3], &propagate) != TCL_OK) {
return TCL_ERROR;
}
if (propagate) {
masterPtr->flags &= ~DONT_PROPAGATE;
/*
* Re-arrange the master to allow new geometry information to
* propagate upwards to the master\'s master.
*/
if (masterPtr->abortPtr != NULL) {
*masterPtr->abortPtr = 1;
}
masterPtr->valid = 0;
if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
masterPtr->flags |= REQUESTED_RELAYOUT;
Tk_DoWhenIdle(ArrangeGrid, (ClientData) masterPtr);
}
} else {
masterPtr->flags |= DONT_PROPAGATE;
}
} else if ((c == 's') && (strncmp(argv[1], "size", length) == 0)) {
CkWindow *master;
GridBag *masterPtr;
char buf[256];
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " size window\"", (char *) NULL);
return TCL_ERROR;
}
master = Ck_NameToWindow(interp, argv[2], winPtr);
if (master == NULL)
return TCL_ERROR;
masterPtr = GetGridBag(master);
GetCachedLayoutInfo(masterPtr);
sprintf(buf, "%d %d", masterPtr->layoutCache->width,
masterPtr->layoutCache->height);
Tcl_SetObjResult( interp, Tcl_NewStringObj( buf, -1));
} else if ((c == 's') && (strncmp(argv[1], "slaves", length) == 0)) {
CkWindow *master;
GridBag *masterPtr, *slavePtr;
int i, value;
int row = -1, column = -1;
if (argc < 3 || argc%2 ==0) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " slaves window ?-option value...?\"",
(char *) NULL);
return TCL_ERROR;
}
for (i=3; i<argc; i+=2) {
if (*argv[i] != '-' || (length = strlen(argv[i])) < 2) {
Tcl_AppendResult(interp, "Invalid args: should be \"",
argv[0], " slaves window ?-option value...?\"",
(char *) NULL);
return TCL_ERROR;
}
if (Tcl_GetInt(interp, argv[i+1], &value) != TCL_OK) {
return TCL_ERROR;
}
if (value < 0) {
Tcl_AppendResult(interp, argv[i],
" is an invalid value: should NOT be < 0",
(char *) NULL);
return TCL_ERROR;
}
if (strncmp(argv[i], "-column", length) == 0) {
column = value;
} else if (strncmp(argv[i], "-row", length) == 0) {
row = value;
} else {
Tcl_AppendResult(interp, argv[i],
" is an invalid option: should be \"",
"-row, -column\"",
(char *) NULL);
return TCL_ERROR;
}
}
master = Ck_NameToWindow(interp, argv[2], winPtr);
if (master == NULL) {
return TCL_ERROR;
}
masterPtr = GetGridBag(master);
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
if (column>=0 && (slavePtr->gridColumn > column
|| slavePtr->gridColumn+slavePtr->gridWidth-1 < column)) {
continue;
}
if (row>=0 && (slavePtr->gridRow > row ||
slavePtr->gridRow+slavePtr->gridHeight-1 < row)) {
continue;
}
Tcl_AppendElement(interp, slavePtr->winPtr->pathName);
}
/*
* grid columnconfigure <master> <index> -option
* grid columnconfigure <master> <index> -option value -option value
* grid rowconfigure <master> <index> -option
* grid rowconfigure <master> <index> -option value -option value
*/
} else if (((c == 'c') &&
(strncmp(argv[1], "columnconfigure", length) == 0)) ||
((c == 'r') && (strncmp(argv[1], "rowconfigure", length) == 0))) {
CkWindow *master;
GridBag *masterPtr;
Constrain *con;
int index, i, size;
double weight;
if (argc != 5 && (argc < 5 || argc%2 == 1)) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ", argv[1], " master index ?-option value...?\"",
(char *)NULL);
return TCL_ERROR;
}
master = Ck_NameToWindow(interp, argv[2], winPtr);
if (master == NULL) {
return TCL_ERROR;
}
masterPtr = GetGridBag(master);
con = (c=='c') ? &(masterPtr->column) : &(masterPtr->row);
if (Tcl_GetInt(interp, argv[3], &index) != TCL_OK) {
return TCL_ERROR;
}
if (index < 0 || index >= MAXGRIDSIZE) {
Tcl_AppendResult(interp, argv[3], " is out of range",
(char *)NULL);
return TCL_ERROR;
}
/*
* make sure the row/column constraint array is allocated. This
* Should be changed to avoid hard-wired limits. We'll wimp out
* for now.
*/
if (con->max == 0) {
unsigned int size;
con->max = MAXGRIDSIZE;
con->used = 0;
size = MAXGRIDSIZE * sizeof(con->minsize[0]);
con->minsize = (int *) ckalloc(size);
memset(con->minsize, 0, size);
size = MAXGRIDSIZE * sizeof(con->weight[0]);
con->weight = (double *) ckalloc(size);
memset(con->weight, 0, size);
}
for (i=4; i<argc; i+=2) {
if (*argv[i] != '-' || (length = strlen(argv[i])) < 2) {
Tcl_AppendResult(interp, "Invalid arg: \"",
argv[0], "\" expecting -minsize or -weight",
(char *) NULL);
return TCL_ERROR;
}
if (strncmp(argv[i], "-minsize", length) == 0) {
if (argc == 5) {
size = con->used <= index ? 0 : con->minsize[index];
Tcl_SetObjResult( interp, Tcl_NewIntObj(size));
} else if (Ck_GetCoord(interp, master, argv[i + 1], &size)
!= TCL_OK) {
return TCL_ERROR;
} else {
con->minsize[index] = size;
if (size > 0 && index >= con->used) {
con->used = index+1;
} else if (size == 0 && index+1 == con->used) {
while (index >= 0 && (con->minsize[index]==0) &&
(con->weight[index] == 0.0)) {
index--;
}
con->used = index + 1;
}
}
} else if (strncmp(argv[i], "-weight", length) == 0) {
if (argc == 5) {
weight = con->used <= index ? 0 : con->weight[index];
Tcl_SetObjResult( interp, Tcl_NewDoubleObj( (double) ((int) weight * 100) / 100.0));
} else if (Tcl_GetDouble(interp, argv[i+1], &weight)
!= TCL_OK) {
return TCL_ERROR;
} else {
con->weight[index] = weight;
if (weight > MINWEIGHT && index >= con->used) {
con->used = index+1;
} else if (weight == 0.0 && index+1 == con->used) {
while (index >= 0 && (con->minsize[index]==0) &&
(con->weight[index] == 0.0)) {
index--;
}
con->used = index + 1;
}
}
} else {
Tcl_AppendResult(interp, argv[i],
" is an invalid option: should be \"",
"-minsize, -weight\"",
(char *) NULL);
return TCL_ERROR;
}
}
/* if we changed a property, re-arrange the table */
if (argc != 5) {
if (masterPtr->abortPtr != NULL) {
*masterPtr->abortPtr = 1;
}
masterPtr->valid = 0;
if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
masterPtr->flags |= REQUESTED_RELAYOUT;
Tk_DoWhenIdle(ArrangeGrid, (ClientData) masterPtr);
}
}
} else if ((c == 'l') && (strncmp(argv[1], "location", length) == 0)) {
CkWindow *master;
GridBag *masterPtr;
int x, y, i, j, w, h;
int width, height;
double weight;
int diff;
char buf[256];
if (argc != 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " location master x y\"", (char *)NULL);
return TCL_ERROR;
}
master = Ck_NameToWindow(interp, argv[2], winPtr);
if (master == NULL) {
return TCL_ERROR;
}
masterPtr = GetGridBag(master);
if (Ck_GetCoord(interp, master, argv[3], &x) != TCL_OK) {
return TCL_ERROR;
}
if (Ck_GetCoord(interp, master, argv[4], &y) != TCL_OK) {
return TCL_ERROR;
}
/* make sure the grid is up to snuff */
while ((masterPtr->flags & REQUESTED_RELAYOUT)) {
Tk_CancelIdleCall(ArrangeGrid, (ClientData) masterPtr);
ArrangeGrid((ClientData) masterPtr);
}
GetCachedLayoutInfo(masterPtr);
GetMinSize(masterPtr, masterPtr->layoutCache, &width, &height);
diff = masterPtr->winPtr->width - (width + masterPtr->iPadX);
for (weight=0.0, i=0; i<masterPtr->layoutCache->width; i++) {
weight += masterPtr->layoutCache->weightX[i];
}
w = masterPtr->startx;
if (w > x) {
i = -1;
} else {
for (i = 0; i < masterPtr->layoutCache->width; i++) {
int dx = 0;
if (weight > MINWEIGHT) {
dx = (int)((((double)diff) *
masterPtr->layoutCache->weightX[i]) / weight);
}
w += masterPtr->layoutCache->minWidth[i] + dx;
if (w > x) {
break;
}
}
}
diff = masterPtr->winPtr->height - (height + masterPtr->iPadY);
for (weight = 0.0, j = 0; j < masterPtr->layoutCache->height; j++)
weight += masterPtr->layoutCache->weightY[j];
h = masterPtr->starty;
if (h > y) {
j = -1;
} else {
for (j = 0; j < masterPtr->layoutCache->height; j++) {
int dy = 0;
if (weight > MINWEIGHT) {
dy = (int)((((double)diff) *
masterPtr->layoutCache->weightY[j]) / weight);
}
h += masterPtr->layoutCache->minHeight[j] + dy;
if (h > y) {
break;
}
}
}
sprintf(buf, "%d %d", i, j);
Tcl_SetObjResult( interp, Tcl_NewStringObj(buf, -1));
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be bbox, columnconfigure, configure, forget, ",
"info, location, propagate, rowconfigure, size, or slaves",
(char *) NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* GridReqProc --
*
* This procedure is invoked by Ck_GeometryRequest for
* windows managed by the gridbag.
*
* Results:
* None.
*
* Side effects:
* Arranges for winPtr, and all its managed siblings, to
* be re-arranged at the next idle point.
*
*--------------------------------------------------------------
*/
static void
GridReqProc(clientData, winPtr)
ClientData clientData; /* GridBag's information about
* window that got new preferred
* geometry. */
CkWindow *winPtr; /* Other Ck-related information
* about the window. */
{
GridBag *gridPtr = (GridBag *) clientData;
gridPtr = gridPtr->masterPtr;
gridPtr->valid = 0;
if (!(gridPtr->flags & REQUESTED_RELAYOUT)) {
gridPtr->flags |= REQUESTED_RELAYOUT;
Tk_DoWhenIdle(ArrangeGrid, (ClientData) gridPtr);
}
}
/*
*--------------------------------------------------------------
*
* GridLostSlaveProc --
*
* This procedure is invoked by Ck whenever some other geometry
* claims control over a slave that used to be managed by us.
*
* Results:
* None.
*
* Side effects:
* Forgets all grid-related information about the slave.
*
*--------------------------------------------------------------
*/
static void
GridLostSlaveProc(clientData, winPtr)
ClientData clientData; /* GridBag structure for slave window that
* was stolen away. */
CkWindow *winPtr; /* Pointer to the slave window. */
{
GridBag *slavePtr = (GridBag *) clientData;
if (slavePtr->masterPtr->winPtr != slavePtr->winPtr->parentPtr) {
Ck_UnmaintainGeometry(slavePtr->winPtr, slavePtr->masterPtr->winPtr);
}
Unlink(slavePtr);
Ck_UnmapWindow(slavePtr->winPtr);
}
/*
*--------------------------------------------------------------
*
* Fill in an instance of the above structure for the current set
* of managed children. This requires two passes through the
* set of children, first to figure out what cells they occupy
* and how many rows and columns there are, and then to distribute
* the weights and min sizes amoung the rows/columns.
*
* This also caches the minsizes for all the children when they are
* first encountered.
*
*--------------------------------------------------------------
*/
static void
GetLayoutInfo(masterPtr, r)
GridBag *masterPtr;
LayoutInfo *r;
{
GridBag *slavePtr;
int i, k, px, py, pixels_diff, nextSize;
double weight_diff, weight;
int curX, curY, curWidth, curHeight, curRow, curCol;
int xMax[MAXGRIDSIZE];
int yMax[MAXGRIDSIZE];
/*
* Pass #1
*
* Figure out the dimensions of the layout grid.
*/
r->width = r->height = 0;
curRow = curCol = -1;
memset(xMax, 0, sizeof(int) * MAXGRIDSIZE);
memset(yMax, 0, sizeof(int) * MAXGRIDSIZE);
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
curX = slavePtr->gridColumn;
curY = slavePtr->gridRow;
curWidth = slavePtr->gridWidth;
curHeight = slavePtr->gridHeight;
/* Adjust the grid width and height */
for (px = curX + curWidth; r->width < px; r->width++) {
/* Null body. */
}
for (py = curY + curHeight; r->height < py; r->height++) {
/* Null body. */
}
/* Adjust the xMax and yMax arrays */
for (i = curX; i < (curX + curWidth); i++) {
yMax[i] = py;
}
for (i = curY; i < (curY + curHeight); i++) {
xMax[i] = px;
}
/* Cache the current slave's size. */
slavePtr->minWidth = slavePtr->winPtr->reqWidth;
slavePtr->minHeight = slavePtr->winPtr->reqHeight;
}
/*
* Apply minimum row/column dimensions
*/
if (r->width < masterPtr->column.used) {
r->width = masterPtr->column.used;
}
r->lastRow = r->height;
if (r->height < masterPtr->row.used) {
r->height = masterPtr->row.used;
}
/*
* Pass #2
*/
curRow = curCol = -1;
memset(xMax, 0, sizeof(int) * MAXGRIDSIZE);
memset(yMax, 0, sizeof(int) * MAXGRIDSIZE);
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
curX = slavePtr->gridColumn;
curY = slavePtr->gridRow;
curWidth = slavePtr->gridWidth;
curHeight = slavePtr->gridHeight;
px = curX + curWidth;
py = curY + curHeight;
for (i = curX; i < (curX + curWidth); i++) {
yMax[i] = py;
}
for (i = curY; i < (curY + curHeight); i++) {
xMax[i] = px;
}
/* Assign the new values to the gridbag slave */
slavePtr->tempX = curX;
slavePtr->tempY = curY;
slavePtr->tempWidth = curWidth;
slavePtr->tempHeight = curHeight;
}
/*
* Pass #3
*
* Distribute the minimun widths and weights:
*/
/* Initialize arrays to zero */
memset(r->minWidth, 0, r->width * sizeof(int));
memset(r->minHeight, 0, r->height * sizeof(int));
memset(r->weightX, 0, r->width * sizeof(double));
memset(r->weightY, 0, r->height * sizeof(double));
nextSize = MAXINT;
for (i = 1; i != MAXINT; i = nextSize, nextSize = MAXINT) {
for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
slavePtr = slavePtr->nextPtr) {
if (slavePtr->tempWidth == i) {
px = slavePtr->tempX + slavePtr->tempWidth; /* right column */
/*
* Figure out if we should use this slave's weight.
* If the weight is less than the total weight spanned by
* the width of the cell, then discard the weight.
* Otherwise split it the difference
* according to the existing weights.
*/
weight_diff = slavePtr->weightX;
for (k = slavePtr->tempX; k < px; k++)
weight_diff -= r->weightX[k];
if (weight_diff > 0.0) {
weight = 0.0;
for (k = slavePtr->tempX; k < px; k++)
weight += r->weightX[k];
for (k = slavePtr->tempX; weight > MINWEIGHT; k++) {
double wt = r->weightX[k];
double dx = (wt * weight_diff) / weight;
r->weightX[k] += dx;
weight_diff -= dx;
weight -= wt;
}
/* Assign the remainder to the rightmost cell */
r->weightX[px-1] += weight_diff;
}
/*
* Calculate the minWidth array values.
* First, figure out how wide the current slave needs to be.
* Then, see if it will fit within the current minWidth values.
* If it won't fit, add the difference according to the
* weightX array.
*/
pixels_diff = slavePtr->minWidth + slavePtr->padX +
slavePtr->iPadX;
for (k = slavePtr->tempX; k < px; k++)
pixels_diff -= r->minWidth[k];
if (pixels_diff > 0) {
weight = 0.0;
for (k = slavePtr->tempX; k < px; k++)
weight += r->weightX[k];
for (k = slavePtr->tempX; weight > MINWEIGHT; k++) {
double wt = r->weightX[k];
int dx = (int)((wt * ((double)pixels_diff)) / weight);
r->minWidth[k] += dx;
pixels_diff -= dx;
weight -= wt;
}
/* Any leftovers go into the rightmost cell */
r->minWidth[px-1] += pixels_diff;
}
} else if (slavePtr->tempWidth > i &&
slavePtr->tempWidth < nextSize)
nextSize = slavePtr->tempWidth;
if (slavePtr->tempHeight == i) {
py = slavePtr->tempY + slavePtr->tempHeight; /* bottom row */
/*
* Figure out if we should use this slave's weight.
* If the weight is less than the total weight spanned by
* the height of the cell, then discard the weight.
* Otherwise split it the difference according to the
* existing weights.
*/
weight_diff = slavePtr->weightY;
for (k = slavePtr->tempY; k < py; k++)
weight_diff -= r->weightY[k];
if (weight_diff > 0.0) {
weight = 0.0;
for (k = slavePtr->tempY; k < py; k++)
weight += r->weightY[k];
for (k = slavePtr->tempY; weight > MINWEIGHT; k++) {
double wt = r->weightY[k];
double dy = (wt * weight_diff) / weight;
r->weightY[k] += dy;
weight_diff -= dy;
weight -= wt;
}
/* Assign the remainder to the bottom cell */
r->weightY[py-1] += weight_diff;
}
/*
* Calculate the minHeight array values.
* First, figure out how tall the current slave needs to be.
* Then, see if it will fit within the current minHeight
* values. If it won't fit, add the difference according to
* the weightY array.
*/
pixels_diff = slavePtr->minHeight + slavePtr->padY +
slavePtr->iPadY;
for (k = slavePtr->tempY; k < py; k++)
pixels_diff -= r->minHeight[k];
if (pixels_diff > 0) {
weight = 0.0;