/
tkGrid.c
3706 lines (3315 loc) · 107 KB
/
tkGrid.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
/*
* tkGrid.c --
*
* Grid based geometry manager.
*
* Copyright © 1996-1997 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 "tkInt.h"
/*
* Convenience Macros
*/
#ifdef MAX
# undef MAX
#endif
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define COLUMN (1) /* Working on column offsets. */
#define ROW (2) /* Working on row offsets. */
#define CHECK_ONLY (1) /* Check max slot constraint. */
#define CHECK_SPACE (2) /* Alloc more space, don't change max. */
/*
* Pre-allocate enough row and column slots for "typical" sized tables this
* value should be chosen so by the time the extra malloc's are required, the
* layout calculations overwehlm them. [A "slot" contains information for
* either a row or column, depending upon the context.]
*/
#define TYPICAL_SIZE 25 /* (Arbitrary guess) */
#define PREALLOC 10 /* Extra slots to allocate. */
/*
* Pre-allocate room for uniform groups during layout.
*/
#define UNIFORM_PREALLOC 10
/*
* Data structures are allocated dynamically to support arbitrary sized
* tables. However, the space is proportional to the highest numbered slot
* with some non-default property. This limit is used to head off mistakes and
* denial of service attacks by limiting the amount of storage required.
*/
#define MAX_ELEMENT 10000
/*
* Special characters to support relative layouts.
*/
#define REL_SKIP 'x' /* Skip this column. */
#define REL_HORIZ '-' /* Extend previous widget horizontally. */
#define REL_VERT '^' /* Extend widget from row above. */
/*
* Default value for 'grid anchor'.
*/
#define GRID_DEFAULT_ANCHOR TK_ANCHOR_NW
/*
* Structure to hold information for grid containers. A slot is either a row or
* column.
*/
typedef struct SlotInfo {
int minSize; /* The minimum size of this slot (in pixels).
* It is set via the rowconfigure or
* columnconfigure commands. */
int weight; /* The resize weight of this slot. (0) means
* this slot doesn't resize. Extra space in
* the layout is given distributed among slots
* inproportion to their weights. */
int pad; /* Extra padding, in pixels, required for this
* slot. This amount is "added" to the largest
* content in the slot. */
Tk_Uid uniform; /* Value of -uniform option. It is used to
* group slots that should have the same
* size. */
int offset; /* This is a cached value used for
* introspection. It is the pixel offset of
* the right or bottom edge of this slot from
* the beginning of the layout. */
int temp; /* This is a temporary value used for
* calculating adjusted weights when shrinking
* the layout below its nominal size. */
} SlotInfo;
/*
* Structure to hold information during layout calculations. There is one of
* these for each slot, an array for each of the rows or columns.
*/
typedef struct GridLayout {
struct Gridder *binNextPtr; /* The next content window in this bin. Each bin
* contains a list of all content whose spans
* are >1 and whose right edges fall in this
* slot. */
int minSize; /* Minimum size needed for this slot, in
* pixels. This is the space required to hold
* any content contained entirely in this slot,
* adjusted for any slot constrants, such as
* size or padding. */
int pad; /* Padding needed for this slot */
int weight; /* Slot weight, controls resizing. */
Tk_Uid uniform; /* Value of -uniform option. It is used to
* group slots that should have the same
* size. */
int minOffset; /* The minimum offset, in pixels, from the
* beginning of the layout to the bottom/right
* edge of the slot calculated from top/left
* to bottom/right. */
int maxOffset; /* The maximum offset, in pixels, from the
* beginning of the layout to the bottom/right
* edge of the slot calculated from
* bottom/right to top/left. */
} GridLayout;
/*
* Keep one of these for each geometry container.
*/
typedef struct {
SlotInfo *columnPtr; /* Pointer to array of column constraints. */
SlotInfo *rowPtr; /* Pointer to array of row constraints. */
int columnEnd; /* The last column occupied by any content. */
int columnMax; /* The number of columns with constraints. */
int columnSpace; /* The number of slots currently allocated for
* column constraints. */
int rowEnd; /* The last row occupied by any content. */
int rowMax; /* The number of rows with constraints. */
int rowSpace; /* The number of slots currently allocated for
* row constraints. */
int startX; /* Pixel offset of this layout within its
* container. */
int startY; /* Pixel offset of this layout within its
* container. */
Tk_Anchor anchor; /* Value of anchor option: specifies where a
* grid without weight should be placed. */
} GridContainer;
/*
* For each window that the grid cares about (either because the window is
* managed by the grid or because the window has content that are managed by
* the grid), there is a structure of the following type:
*/
typedef struct Gridder {
Tk_Window tkwin; /* Tk token for window. NULL means that the
* window has been deleted, but the gridder
* hasn't had a chance to clean up yet because
* the structure is still in use. */
struct Gridder *containerPtr; /* Container window within which this window is
* managed (NULL means this window isn't
* managed by the gridder). */
struct Gridder *nextPtr; /* Next window managed within same container.
* List order doesn't matter. */
struct Gridder *contentPtr; /* First in list of content managed inside this
* window (NULL means no grid content). */
GridContainer *containerDataPtr; /* Additional data for geometry container. */
Tcl_Obj *in; /* Store container name when removed. */
int column, row; /* Location in the grid (starting from
* zero). */
int numCols, numRows; /* Number of columns or rows this content spans.
* Should be at least 1. */
int padX, padY; /* Total additional pixels to leave around the
* window. Some is of this space is on each
* side. This is space *outside* the window:
* we'll allocate extra space in frame but
* won't enlarge window). */
int padLeft, padTop; /* The part of padX or padY to use on the left
* or top of the widget, respectively. By
* default, this is half of padX or padY. */
int iPadX, iPadY; /* Total extra pixels to allocate inside the
* window (half this amount will appear on
* each side). */
int sticky; /* which sides of its cavity this window
* sticks to. See below for definitions */
int doubleBw; /* Twice the window's last known border width.
* If this changes, the window must be
* re-arranged within its container. */
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 tkwin or any of its content
* is deleted. */
int flags; /* Miscellaneous flags; see below for
* definitions. */
/*
* These fields are used temporarily for layout calculations only.
*/
struct Gridder *binNextPtr; /* Link to next span>1 content in this bin. */
int size; /* Nominal size (width or height) in pixels of
* the content. This includes the padding. */
} Gridder;
/*
* Flag values for "sticky"ness. The 16 combinations subsume the packer's
* notion of anchor and fill.
*
* STICK_NORTH This window sticks to the top of its cavity.
* STICK_EAST This window sticks to the right edge of its
* cavity.
* STICK_SOUTH This window sticks to the bottom of its cavity.
* STICK_WEST This window sticks to the left edge of its
* cavity.
*/
#define STICK_NORTH 1
#define STICK_EAST 2
#define STICK_SOUTH 4
#define STICK_WEST 8
/*
* Structure to gather information about uniform groups during layout.
*/
typedef struct UniformGroup {
Tk_Uid group;
int minSize;
} UniformGroup;
/*
* Flag values for Grid structures:
*
* REQUESTED_RELAYOUT 1 means a Tcl_DoWhenIdle request has already
* been made to re-arrange all the content of this
* window.
* DONT_PROPAGATE 1 means don't set this window's requested
* size. 0 means if this window is a container then
* Tk will set its requested size to fit the
* needs of its content.
* ALLOCED_CONTAINER 1 means that Grid has allocated itself as
* geometry container for this window.
*/
#define REQUESTED_RELAYOUT 1
#define DONT_PROPAGATE 2
#define ALLOCED_CONTAINER 4
/*
* Prototypes for procedures used only in this file:
*/
static void AdjustForSticky(Gridder *contentPtr, int *xPtr,
int *yPtr, int *widthPtr, int *heightPtr);
static int AdjustOffsets(int width, Tcl_Size elements,
SlotInfo *slotPtr);
static void ArrangeGrid(void *clientData);
static int CheckSlotData(Gridder *containerPtr, Tcl_Size slot,
int slotType, int checkOnly);
static int ConfigureContent(Tcl_Interp *interp, Tk_Window tkwin,
Tcl_Size objc, Tcl_Obj *const objv[]);
static Tcl_FreeProc DestroyGrid;
static Gridder * GetGrid(Tk_Window tkwin);
static int GridAnchorCommand(Tk_Window tkwin, Tcl_Interp *interp,
Tcl_Size objc, Tcl_Obj *const objv[]);
static int GridBboxCommand(Tk_Window tkwin, Tcl_Interp *interp,
Tcl_Size objc, Tcl_Obj *const objv[]);
static int GridForgetRemoveCommand(Tk_Window tkwin,
Tcl_Interp *interp, Tcl_Size objc,
Tcl_Obj *const objv[]);
static int GridInfoCommand(Tk_Window tkwin, Tcl_Interp *interp,
Tcl_Size objc, Tcl_Obj *const objv[]);
static int GridLocationCommand(Tk_Window tkwin,
Tcl_Interp *interp, Tcl_Size objc,
Tcl_Obj *const objv[]);
static int GridPropagateCommand(Tk_Window tkwin,
Tcl_Interp *interp, Tcl_Size objc,
Tcl_Obj *const objv[]);
static int GridRowColumnConfigureCommand(Tk_Window tkwin,
Tcl_Interp *interp, Tcl_Size objc,
Tcl_Obj *const objv[]);
static int GridSizeCommand(Tk_Window tkwin, Tcl_Interp *interp,
Tcl_Size objc, Tcl_Obj *const objv[]);
static int GridContentCommand(Tk_Window tkwin, Tcl_Interp *interp,
Tcl_Size objc, Tcl_Obj *const objv[]);
static void GridStructureProc(void *clientData,
XEvent *eventPtr);
static void GridLostContentProc(void *clientData,
Tk_Window tkwin);
static void GridReqProc(void *clientData, Tk_Window tkwin);
static void InitContainerData(Gridder *containerPtr);
static Tcl_Obj * NewPairObj(Tcl_WideInt, Tcl_WideInt);
static Tcl_Obj * NewQuadObj(Tcl_WideInt, Tcl_WideInt, Tcl_WideInt, Tcl_WideInt);
static int ResolveConstraints(Gridder *gridPtr, int rowOrColumn,
int maxOffset);
static void SetGridSize(Gridder *gridPtr);
static int SetContentColumn(Tcl_Interp *interp, Gridder *contentPtr,
int column, int numCols);
static int SetContentRow(Tcl_Interp *interp, Gridder *contentPtr,
int row, int numRows);
static Tcl_Obj * StickyToObj(int flags);
static int StringToSticky(const char *string);
static void Unlink(Gridder *gridPtr);
static const Tk_GeomMgr gridMgrType = {
"grid", /* name */
GridReqProc, /* requestProc */
GridLostContentProc, /* lostContentProc */
};
/*
*----------------------------------------------------------------------
*
* Tk_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
Tk_GridObjCmd(
void *clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window tkwin = (Tk_Window)clientData;
static const char *const optionStrings[] = {
"anchor", "bbox", "columnconfigure", "configure",
"content", "forget", "info", "location", "propagate",
"remove", "rowconfigure", "size", "slaves", NULL
};
static const char *const optionStringsNoDep[] = {
"anchor", "bbox", "columnconfigure", "configure",
"content", "forget", "info", "location", "propagate",
"remove", "rowconfigure", "size", NULL
};
enum options {
GRID_ANCHOR, GRID_BBOX, GRID_COLUMNCONFIGURE, GRID_CONFIGURE,
GRID_CONTENT, GRID_FORGET, GRID_INFO, GRID_LOCATION, GRID_PROPAGATE,
GRID_REMOVE, GRID_ROWCONFIGURE, GRID_SIZE, GRID_SLAVES
};
int index;
if (objc >= 2) {
const char *argv1 = Tcl_GetString(objv[1]);
if ((argv1[0] == '.') || (argv1[0] == REL_SKIP) ||
(argv1[0] == REL_VERT)) {
return ConfigureContent(interp, tkwin, objc-1, objv+1);
}
}
if (objc < 3) {
Tcl_WrongNumArgs(interp, 1, objv, "option arg ?arg ...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObjStruct(NULL, objv[1], optionStrings,
sizeof(char *), "option", 0, &index) != TCL_OK) {
/*
* Call it again without the deprecated ones to get a proper error
* message. This works well since there can't be any ambiguity between
* deprecated and new options.
*/
Tcl_GetIndexFromObjStruct(interp, objv[1], optionStringsNoDep,
sizeof(char *), "option", 0, &index);
return TCL_ERROR;
}
switch ((enum options) index) {
case GRID_ANCHOR:
return GridAnchorCommand(tkwin, interp, objc, objv);
case GRID_BBOX:
return GridBboxCommand(tkwin, interp, objc, objv);
case GRID_CONFIGURE:
return ConfigureContent(interp, tkwin, objc-2, objv+2);
case GRID_FORGET:
case GRID_REMOVE:
return GridForgetRemoveCommand(tkwin, interp, objc, objv);
case GRID_INFO:
return GridInfoCommand(tkwin, interp, objc, objv);
case GRID_LOCATION:
return GridLocationCommand(tkwin, interp, objc, objv);
case GRID_PROPAGATE:
return GridPropagateCommand(tkwin, interp, objc, objv);
case GRID_SIZE:
return GridSizeCommand(tkwin, interp, objc, objv);
case GRID_SLAVES:
case GRID_CONTENT:
return GridContentCommand(tkwin, interp, objc, objv);
/*
* Sample argument combinations:
* grid columnconfigure <container> <index> -option
* grid columnconfigure <container> <index> -option value -option value
* grid rowconfigure <container> <index>
* grid rowconfigure <container> <index> -option
* grid rowconfigure <container> <index> -option value -option value.
*/
case GRID_COLUMNCONFIGURE:
case GRID_ROWCONFIGURE:
return GridRowColumnConfigureCommand(tkwin, interp, objc, objv);
}
/* This should not happen */
Tcl_SetObjResult(interp, Tcl_NewStringObj("internal error in grid", TCL_INDEX_NONE));
Tcl_SetErrorCode(interp, "TK", "API_ABUSE", NULL);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* GridAnchorCommand --
*
* Implementation of the [grid anchor] subcommand. See the user
* documentation for details on what it does.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* May recompute grid geometry.
*
*----------------------------------------------------------------------
*/
static int
GridAnchorCommand(
Tk_Window tkwin, /* Main window of the application. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window container;
Gridder *containerPtr;
GridContainer *gridPtr;
Tk_Anchor old;
if (objc > 4) {
Tcl_WrongNumArgs(interp, 2, objv, "window ?anchor?");
return TCL_ERROR;
}
if (TkGetWindowFromObj(interp, tkwin, objv[2], &container) != TCL_OK) {
return TCL_ERROR;
}
containerPtr = GetGrid(container);
if (objc == 3) {
gridPtr = containerPtr->containerDataPtr;
Tcl_SetObjResult(interp, Tcl_NewStringObj(
Tk_NameOfAnchor(gridPtr?gridPtr->anchor:GRID_DEFAULT_ANCHOR),
-1));
return TCL_OK;
}
InitContainerData(containerPtr);
gridPtr = containerPtr->containerDataPtr;
old = gridPtr->anchor;
if (Tk_GetAnchorFromObj(interp, objv[3], &gridPtr->anchor) != TCL_OK) {
return TCL_ERROR;
}
/*
* Only request a relayout if the anchor changes.
*/
if (old != gridPtr->anchor) {
if (containerPtr->abortPtr != NULL) {
*containerPtr->abortPtr = 1;
}
if (!(containerPtr->flags & REQUESTED_RELAYOUT)) {
containerPtr->flags |= REQUESTED_RELAYOUT;
Tcl_DoWhenIdle(ArrangeGrid, containerPtr);
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GridBboxCommand --
*
* Implementation of the [grid bbox] subcommand.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Places bounding box information in the interp's result field.
*
*----------------------------------------------------------------------
*/
static int
GridBboxCommand(
Tk_Window tkwin, /* Main window of the application. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window container;
Gridder *containerPtr; /* container grid record */
GridContainer *gridPtr; /* pointer to grid data */
int row, column; /* origin for bounding box */
int row2, column2; /* end of bounding box */
int endX, endY; /* last column/row in the layout */
int x=0, y=0; /* starting pixels for this bounding box */
int width, height; /* size of the bounding box */
if (objc!=3 && objc != 5 && objc != 7) {
Tcl_WrongNumArgs(interp, 2, objv, "window ?column row ?column row??");
return TCL_ERROR;
}
if (TkGetWindowFromObj(interp, tkwin, objv[2], &container) != TCL_OK) {
return TCL_ERROR;
}
containerPtr = GetGrid(container);
if (objc >= 5) {
if (Tcl_GetIntFromObj(interp, objv[3], &column) != TCL_OK) {
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(interp, objv[4], &row) != TCL_OK) {
return TCL_ERROR;
}
column2 = column;
row2 = row;
}
if (objc == 7) {
if (Tcl_GetIntFromObj(interp, objv[5], &column2) != TCL_OK) {
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(interp, objv[6], &row2) != TCL_OK) {
return TCL_ERROR;
}
}
gridPtr = containerPtr->containerDataPtr;
if (gridPtr == NULL) {
Tcl_SetObjResult(interp, NewQuadObj(0, 0, 0, 0));
return TCL_OK;
}
SetGridSize(containerPtr);
endX = MAX(gridPtr->columnEnd, gridPtr->columnMax);
endY = MAX(gridPtr->rowEnd, gridPtr->rowMax);
if ((endX == 0) || (endY == 0)) {
Tcl_SetObjResult(interp, NewQuadObj(0, 0, 0, 0));
return TCL_OK;
}
if (objc == 3) {
row = 0;
column = 0;
row2 = endY;
column2 = endX;
}
if (column > column2) {
int temp = column;
column = column2;
column2 = temp;
}
if (row > row2) {
int temp = row;
row = row2;
row2 = temp;
}
if (column > 0 && column < endX) {
x = gridPtr->columnPtr[column-1].offset;
} else if (column > 0) {
x = gridPtr->columnPtr[endX-1].offset;
}
if (row > 0 && row < endY) {
y = gridPtr->rowPtr[row-1].offset;
} else if (row > 0) {
y = gridPtr->rowPtr[endY-1].offset;
}
if (column2 < 0) {
width = 0;
} else if (column2 >= endX) {
width = gridPtr->columnPtr[endX-1].offset - x;
} else {
width = gridPtr->columnPtr[column2].offset - x;
}
if (row2 < 0) {
height = 0;
} else if (row2 >= endY) {
height = gridPtr->rowPtr[endY-1].offset - y;
} else {
height = gridPtr->rowPtr[row2].offset - y;
}
Tcl_SetObjResult(interp, NewQuadObj(
x + gridPtr->startX, y + gridPtr->startY, width, height));
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GridForgetRemoveCommand --
*
* Implementation of the [grid forget]/[grid remove] subcommands. See the
* user documentation for details on what these do.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Removes a window from a grid layout.
*
*----------------------------------------------------------------------
*/
static int
GridForgetRemoveCommand(
Tk_Window tkwin, /* Main window of the application. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window content;
Gridder *contentPtr;
Tcl_Size i;
const char *string = Tcl_GetString(objv[1]);
char c = string[0];
for (i = 2; i < objc; i++) {
if (TkGetWindowFromObj(interp, tkwin, objv[i], &content) != TCL_OK) {
return TCL_ERROR;
}
contentPtr = GetGrid(content);
if (contentPtr->containerPtr != NULL) {
/*
* For "forget", reset all the settings to their defaults
*/
if (c == 'f') {
contentPtr->column = -1;
contentPtr->row = -1;
contentPtr->numCols = 1;
contentPtr->numRows = 1;
contentPtr->padX = 0;
contentPtr->padY = 0;
contentPtr->padLeft = 0;
contentPtr->padTop = 0;
contentPtr->iPadX = 0;
contentPtr->iPadY = 0;
if (contentPtr->in != NULL) {
Tcl_DecrRefCount(contentPtr->in);
contentPtr->in = NULL;
}
contentPtr->doubleBw = 2*Tk_Changes(tkwin)->border_width;
if (contentPtr->flags & REQUESTED_RELAYOUT) {
Tcl_CancelIdleCall(ArrangeGrid, contentPtr);
}
contentPtr->flags = 0;
contentPtr->sticky = 0;
} else {
/*
* When removing, store name of container to be able to
* restore it later, even if the container is recreated.
*/
if (contentPtr->in != NULL) {
Tcl_DecrRefCount(contentPtr->in);
contentPtr->in = NULL;
}
if (contentPtr->containerPtr != NULL) {
contentPtr->in = Tcl_NewStringObj(
Tk_PathName(contentPtr->containerPtr->tkwin), TCL_INDEX_NONE);
Tcl_IncrRefCount(contentPtr->in);
}
}
Tk_ManageGeometry(content, NULL, NULL);
if (contentPtr->containerPtr->tkwin != Tk_Parent(contentPtr->tkwin)) {
Tk_UnmaintainGeometry(contentPtr->tkwin,
contentPtr->containerPtr->tkwin);
}
Unlink(contentPtr);
Tk_UnmapWindow(contentPtr->tkwin);
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GridInfoCommand --
*
* Implementation of the [grid info] subcommand. See the user
* documentation for details on what it does.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Puts gridding information in the interpreter's result.
*
*----------------------------------------------------------------------
*/
static int
GridInfoCommand(
Tk_Window tkwin, /* Main window of the application. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Gridder *contentPtr;
Tk_Window content;
Tcl_Obj *infoObj;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "window");
return TCL_ERROR;
}
if (TkGetWindowFromObj(interp, tkwin, objv[2], &content) != TCL_OK) {
return TCL_ERROR;
}
contentPtr = GetGrid(content);
if (contentPtr->containerPtr == NULL) {
Tcl_ResetResult(interp);
return TCL_OK;
}
infoObj = Tcl_NewObj();
Tcl_DictObjPut(NULL, infoObj, Tcl_NewStringObj("-in", TCL_INDEX_NONE),
Tk_NewWindowObj(contentPtr->containerPtr->tkwin));
Tcl_DictObjPut(NULL, infoObj, Tcl_NewStringObj("-column", TCL_INDEX_NONE),
Tcl_NewWideIntObj(contentPtr->column));
Tcl_DictObjPut(NULL, infoObj, Tcl_NewStringObj("-row", TCL_INDEX_NONE),
Tcl_NewWideIntObj(contentPtr->row));
Tcl_DictObjPut(NULL, infoObj, Tcl_NewStringObj("-columnspan", TCL_INDEX_NONE),
Tcl_NewWideIntObj(contentPtr->numCols));
Tcl_DictObjPut(NULL, infoObj, Tcl_NewStringObj("-rowspan", TCL_INDEX_NONE),
Tcl_NewWideIntObj(contentPtr->numRows));
TkAppendPadAmount(infoObj, "-ipadx", contentPtr->iPadX/2, contentPtr->iPadX);
TkAppendPadAmount(infoObj, "-ipady", contentPtr->iPadY/2, contentPtr->iPadY);
TkAppendPadAmount(infoObj, "-padx", contentPtr->padLeft, contentPtr->padX);
TkAppendPadAmount(infoObj, "-pady", contentPtr->padTop, contentPtr->padY);
Tcl_DictObjPut(NULL, infoObj, Tcl_NewStringObj("-sticky", TCL_INDEX_NONE),
StickyToObj(contentPtr->sticky));
Tcl_SetObjResult(interp, infoObj);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GridLocationCommand --
*
* Implementation of the [grid location] subcommand. See the user
* documentation for details on what it does.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Puts location information in the interpreter's result field.
*
*----------------------------------------------------------------------
*/
static int
GridLocationCommand(
Tk_Window tkwin, /* Main window of the application. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window container;
Gridder *containerPtr; /* Container grid record. */
GridContainer *gridPtr; /* Pointer to grid data. */
SlotInfo *slotPtr;
int x, y; /* Offset in pixels, from edge of container. */
int i, j; /* Corresponding column and row indeces. */
int endX, endY; /* End of grid. */
if (objc != 5) {
Tcl_WrongNumArgs(interp, 2, objv, "window x y");
return TCL_ERROR;
}
if (TkGetWindowFromObj(interp, tkwin, objv[2], &container) != TCL_OK) {
return TCL_ERROR;
}
if (Tk_GetPixelsFromObj(interp, container, objv[3], &x) != TCL_OK) {
return TCL_ERROR;
}
if (Tk_GetPixelsFromObj(interp, container, objv[4], &y) != TCL_OK) {
return TCL_ERROR;
}
containerPtr = GetGrid(container);
if (containerPtr->containerDataPtr == NULL) {
Tcl_SetObjResult(interp, NewPairObj(-1, TCL_INDEX_NONE));
return TCL_OK;
}
gridPtr = containerPtr->containerDataPtr;
/*
* Update any pending requests. This is not always the steady state value,
* as more configure events could be in the pipeline, but its as close as
* its easy to get.
*/
while (containerPtr->flags & REQUESTED_RELAYOUT) {
Tcl_CancelIdleCall(ArrangeGrid, containerPtr);
ArrangeGrid(containerPtr);
}
SetGridSize(containerPtr);
endX = MAX(gridPtr->columnEnd, gridPtr->columnMax);
endY = MAX(gridPtr->rowEnd, gridPtr->rowMax);
slotPtr = containerPtr->containerDataPtr->columnPtr;
if (x < containerPtr->containerDataPtr->startX) {
i = -1;
} else {
x -= containerPtr->containerDataPtr->startX;
for (i = 0; slotPtr[i].offset < x && i < endX; i++) {
/* null body */
}
}
slotPtr = containerPtr->containerDataPtr->rowPtr;
if (y < containerPtr->containerDataPtr->startY) {
j = -1;
} else {
y -= containerPtr->containerDataPtr->startY;
for (j = 0; slotPtr[j].offset < y && j < endY; j++) {
/* null body */
}
}
Tcl_SetObjResult(interp, NewPairObj(i, j));
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GridPropagateCommand --
*
* Implementation of the [grid propagate] subcommand. See the user
* documentation for details on what it does.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* May alter geometry propagation for a widget.
*
*----------------------------------------------------------------------
*/
static int
GridPropagateCommand(
Tk_Window tkwin, /* Main window of the application. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window container;
Gridder *containerPtr;
int propagate, old;
if (objc > 4) {
Tcl_WrongNumArgs(interp, 2, objv, "window ?boolean?");
return TCL_ERROR;
}
if (TkGetWindowFromObj(interp, tkwin, objv[2], &container) != TCL_OK) {
return TCL_ERROR;
}
containerPtr = GetGrid(container);
if (objc == 3) {
Tcl_SetObjResult(interp,
Tcl_NewBooleanObj(!(containerPtr->flags & DONT_PROPAGATE)));
return TCL_OK;
}
if (Tcl_GetBooleanFromObj(interp, objv[3], &propagate) != TCL_OK) {
return TCL_ERROR;
}
/*
* Only request a relayout if the propagation bit changes.
*/
old = !(containerPtr->flags & DONT_PROPAGATE);
if (propagate != old) {
if (propagate) {
/*
* If we have content, we need to register as geometry container.
*/
if (containerPtr->contentPtr != NULL) {
if (TkSetGeometryContainer(interp, container, "grid") != TCL_OK) {
return TCL_ERROR;
}
containerPtr->flags |= ALLOCED_CONTAINER;
}
containerPtr->flags &= ~DONT_PROPAGATE;
} else {
if (containerPtr->flags & ALLOCED_CONTAINER) {
TkFreeGeometryContainer(container, "grid");
containerPtr->flags &= ~ALLOCED_CONTAINER;
}
containerPtr->flags |= DONT_PROPAGATE;
}
/*
* Re-arrange the container to allow new geometry information to
* propagate upwards to the container's container.
*/
if (containerPtr->abortPtr != NULL) {
*containerPtr->abortPtr = 1;
}
if (!(containerPtr->flags & REQUESTED_RELAYOUT)) {
containerPtr->flags |= REQUESTED_RELAYOUT;
Tcl_DoWhenIdle(ArrangeGrid, containerPtr);
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GridRowColumnConfigureCommand --
*
* Implementation of the [grid rowconfigure] and [grid columnconfigure]
* subcommands. See the user documentation for details on what these do.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Depends on arguments; see user documentation.
*
*----------------------------------------------------------------------
*/
static int
GridRowColumnConfigureCommand(
Tk_Window tkwin, /* Main window of the application. */
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Size objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window container, content;
Gridder *containerPtr, *contentPtr;
SlotInfo *slotPtr = NULL;
int slot; /* the column or row number */
int slotType; /* COLUMN or ROW */
int size; /* the configuration value */
Tcl_Size lObjc; /* Number of items in index list */
Tcl_Obj **lObjv; /* array of indices */
int ok; /* temporary TCL result code */
Tcl_Size i, j, first, last;
const char *string;
static const char *const optionStrings[] = {
"-minsize", "-pad", "-uniform", "-weight", NULL
};
enum options {
ROWCOL_MINSIZE, ROWCOL_PAD, ROWCOL_UNIFORM, ROWCOL_WEIGHT
};
int index;
Tcl_Obj *listCopy;
if (((objc % 2 != 0) && (objc > 6)) || (objc < 4)) {