Debian Codegraph @

pTk/mTk/generic/tkGrid.c

raw symbols SCIP
1 /*
2 * tkGrid.c --
3 *
4 * Grid based geometry manager.
5 *
6 * Copyright (c) 1996-1997 by Sun Microsystems, Inc.
7 *
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tkGrid.c,v 1.25 2002/10/10 21:07:51 pspjuth Exp $
12 */
13
14 #include "tkInt.h"
15
16 /*
17 * Convenience Macros
18 */
19
20 #ifdef MAX
21 # undef MAX
22 #endif
23 #define MAX(x,y) ((x) > (y) ? (x) : (y))
24 #ifdef MIN
25 # undef MIN
26 #endif
27 #define MIN(x,y) ((x) > (y) ? (y) : (x))
28
29 #define COLUMN (1) /* working on column offsets */
30 #define ROW (2) /* working on row offsets */
31
32 #define CHECK_ONLY (1) /* check max slot constraint */
33 #define CHECK_SPACE (2) /* alloc more space, don't change max */
34
35 /*
36 * Pre-allocate enough row and column slots for "typical" sized tables
37 * this value should be chosen so by the time the extra malloc's are
38 * required, the layout calculations overwehlm them. [A "slot" contains
39 * information for either a row or column, depending upon the context.]
40 */
41
42 #define TYPICAL_SIZE 25 /* (arbitrary guess) */
43 #define PREALLOC 10 /* extra slots to allocate */
44
45 /*
46 * Pre-allocate room for uniform groups during layout.
47 */
48
49 #define UNIFORM_PREALLOC 10
50
51 /*
52 * Data structures are allocated dynamically to support arbitrary sized tables.
53 * However, the space is proportional to the highest numbered slot with
54 * some non-default property. This limit is used to head off mistakes and
55 * denial of service attacks by limiting the amount of storage required.
56 */
57
58 #define MAX_ELEMENT 10000
59
60 /*
61 * Special characters to support relative layouts.
62 */
63
64 #define REL_SKIP 'x' /* Skip this column. */
65 #define REL_HORIZ '-' /* Extend previous widget horizontally. */
66 #define REL_VERT '^' /* Extend widget from row above. */
67
68 /*
69 * Structure to hold information for grid masters. A slot is either
70 * a row or column.
71 */
72
73 typedef struct SlotInfo {
74 int minSize; /* The minimum size of this slot (in pixels).
75 * It is set via the rowconfigure or
76 * columnconfigure commands. */
77 int weight; /* The resize weight of this slot. (0) means
78 * this slot doesn't resize. Extra space in
79 * the layout is given distributed among slots
80 * inproportion to their weights. */
81 int pad; /* Extra padding, in pixels, required for
82 * this slot. This amount is "added" to the
83 * largest slave in the slot. */
84 Tk_Uid uniform; /* Value of -uniform option. It is used to
85 * group slots that should have the same
86 * size. */
87 int offset; /* This is a cached value used for
88 * introspection. It is the pixel
89 * offset of the right or bottom edge
90 * of this slot from the beginning of the
91 * layout. */
92 int temp; /* This is a temporary value used for
93 * calculating adjusted weights when
94 * shrinking the layout below its
95 * nominal size. */
96 } SlotInfo;
97
98 /*
99 * Structure to hold information during layout calculations. There
100 * is one of these for each slot, an array for each of the rows or columns.
101 */
102
103 typedef struct GridLayout {
104 struct Gridder *binNextPtr; /* The next slave window in this bin.
105 * Each bin contains a list of all
106 * slaves whose spans are >1 and whose
107 * right edges fall in this slot. */
108 int minSize; /* Minimum size needed for this slot,
109 * in pixels. This is the space required
110 * to hold any slaves contained entirely
111 * in this slot, adjusted for any slot
112 * constrants, such as size or padding. */
113 int pad; /* Padding needed for this slot */
114 int weight; /* Slot weight, controls resizing. */
115 Tk_Uid uniform; /* Value of -uniform option. It is used to
116 * group slots that should have the same
117 * size. */
118 int minOffset; /* The minimum offset, in pixels, from
119 * the beginning of the layout to the
120 * right/bottom edge of the slot calculated
121 * from top/left to bottom/right. */
122 int maxOffset; /* The maximum offset, in pixels, from
123 * the beginning of the layout to the
124 * right-or-bottom edge of the slot calculated
125 * from bottom-or-right to top-or-left. */
126 } GridLayout;
127
128 /*
129 * Keep one of these for each geometry master.
130 */
131
132 typedef struct {
133 SlotInfo *columnPtr; /* Pointer to array of column constraints. */
134 SlotInfo *rowPtr; /* Pointer to array of row constraints. */
135 int columnEnd; /* The last column occupied by any slave. */
136 int columnMax; /* The number of columns with constraints. */
137 int columnSpace; /* The number of slots currently allocated for
138 * column constraints. */
139 int rowEnd; /* The last row occupied by any slave. */
140 int rowMax; /* The number of rows with constraints. */
141 int rowSpace; /* The number of slots currently allocated
142 * for row constraints. */
143 int startX; /* Pixel offset of this layout within its
144 * parent. */
145 int startY; /* Pixel offset of this layout within its
146 * parent. */
147 } GridMaster;
148
149 /*
150 * For each window that the grid cares about (either because
151 * the window is managed by the grid or because the window
152 * has slaves that are managed by the grid), there is a
153 * structure of the following type:
154 */
155
156 typedef struct Gridder {
157 Tk_Window tkwin; /* Tk token for window. NULL means that
158 * the window has been deleted, but the
159 * gridder hasn't had a chance to clean up
160 * yet because the structure is still in
161 * use. */
162 struct Gridder *masterPtr; /* Master window within which this window
163 * is managed (NULL means this window
164 * isn't managed by the gridder). */
165 struct Gridder *nextPtr; /* Next window managed within same
166 * parent. List order doesn't matter. */
167 struct Gridder *slavePtr; /* First in list of slaves managed
168 * inside this window (NULL means
169 * no grid slaves). */
170 GridMaster *masterDataPtr; /* Additional data for geometry master. */
171 int column, row; /* Location in the grid (starting
172 * from zero). */
173 int numCols, numRows; /* Number of columns or rows this slave spans.
174 * Should be at least 1. */
175 int padX, padY; /* Total additional pixels to leave around the
176 * window. Some is of this space is on each
177 * side. This is space *outside* the window:
178 * we'll allocate extra space in frame but
179 * won't enlarge window). */
180 int padLeft, padTop; /* The part of padX or padY to use on the
181 * left or top of the widget, respectively.
182 * By default, this is half of padX or padY. */
183 int iPadX, iPadY; /* Total extra pixels to allocate inside the
184 * window (half this amount will appear on
185 * each side). */
186 int sticky; /* which sides of its cavity this window
187 * sticks to. See below for definitions */
188 int doubleBw; /* Twice the window's last known border
189 * width. If this changes, the window
190 * must be re-arranged within its parent. */
191 int *abortPtr; /* If non-NULL, it means that there is a nested
192 * call to ArrangeGrid already working on
193 * this window. *abortPtr may be set to 1 to
194 * abort that nested call. This happens, for
195 * example, if tkwin or any of its slaves
196 * is deleted. */
197 int flags; /* Miscellaneous flags; see below
198 * for definitions. */
199
200 /*
201 * These fields are used temporarily for layout calculations only.
202 */
203
204 struct Gridder *binNextPtr; /* Link to next span>1 slave in this bin. */
205 int size; /* Nominal size (width or height) in pixels
206 * of the slave. This includes the padding. */
207 } Gridder;
208
209 /* Flag values for "sticky"ness The 16 combinations subsume the packer's
210 * notion of anchor and fill.
211 *
212 * STICK_NORTH This window sticks to the top of its cavity.
213 * STICK_EAST This window sticks to the right edge of its cavity.
214 * STICK_SOUTH This window sticks to the bottom of its cavity.
215 * STICK_WEST This window sticks to the left edge of its cavity.
216 */
217
218 #define STICK_NORTH 1
219 #define STICK_EAST 2
220 #define STICK_SOUTH 4
221 #define STICK_WEST 8
222
223
224 /*
225 * Structure to gather information about uniform groups during layout.
226 */
227
228 typedef struct UniformGroup {
229 Tk_Uid group;
230 int minSize;
231 } UniformGroup;
232
233 /*
234 * Flag values for Grid structures:
235 *
236 * REQUESTED_RELAYOUT: 1 means a Tcl_DoWhenIdle request
237 * has already been made to re-arrange
238 * all the slaves of this window.
239 *
240 * DONT_PROPAGATE: 1 means don't set this window's requested
241 * size. 0 means if this window is a master
242 * then Tk will set its requested size to fit
243 * the needs of its slaves.
244 */
245
246 #define REQUESTED_RELAYOUT 1
247 #define DONT_PROPAGATE 2
248
249 /*
250 * Prototypes for procedures used only in this file:
251 */
252
253 static void AdjustForSticky _ANSI_ARGS_((Gridder *slavePtr, int *xPtr,
254 int *yPtr, int *widthPtr, int *heightPtr));
255 static int AdjustOffsets _ANSI_ARGS_((int width,
256 int elements, SlotInfo *slotPtr));
257 static void ArrangeGrid _ANSI_ARGS_((ClientData clientData));
258 static int CheckSlotData _ANSI_ARGS_((Gridder *masterPtr, int slot,
259 int slotType, int checkOnly));
260 static int ConfigureSlaves _ANSI_ARGS_((Tcl_Interp *interp,
261 Tk_Window tkwin, int objc, Tcl_Obj *CONST objv[]));
262 static void DestroyGrid _ANSI_ARGS_((char *memPtr));
263 static Gridder *GetGrid _ANSI_ARGS_((Tk_Window tkwin));
264 static int GridBboxCommand _ANSI_ARGS_((Tk_Window tkwin,
265 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
266 static int GridForgetRemoveCommand _ANSI_ARGS_((Tk_Window tkwin,
267 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
268 static int GridInfoCommand _ANSI_ARGS_((Tk_Window tkwin,
269 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
270 static int GridLocationCommand _ANSI_ARGS_((Tk_Window tkwin,
271 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
272 static int GridPropagateCommand _ANSI_ARGS_((Tk_Window tkwin,
273 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
274 static int GridRowColumnConfigureCommand _ANSI_ARGS_((Tk_Window tkwin,
275 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
276 static int GridSizeCommand _ANSI_ARGS_((Tk_Window tkwin,
277 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
278 static int GridSlavesCommand _ANSI_ARGS_((Tk_Window tkwin,
279 Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
280 static void GridStructureProc _ANSI_ARGS_((
281 ClientData clientData, XEvent *eventPtr));
282 static void GridLostSlaveProc _ANSI_ARGS_((ClientData clientData,
283 Tk_Window tkwin));
284 static void GridReqProc _ANSI_ARGS_((ClientData clientData,
285 Tk_Window tkwin));
286 static void InitMasterData _ANSI_ARGS_((Gridder *masterPtr));
287 static Tcl_Obj *NewPairObj _ANSI_ARGS_((Tcl_Interp*, int, int));
288 static Tcl_Obj *NewQuadObj _ANSI_ARGS_((Tcl_Interp*, int, int, int, int));
289 static int ResolveConstraints _ANSI_ARGS_((Gridder *gridPtr,
290 int rowOrColumn, int maxOffset));
291 static void SetGridSize _ANSI_ARGS_((Gridder *gridPtr));
292 static void StickyToString _ANSI_ARGS_((int flags, char *result));
293 static int StringToSticky _ANSI_ARGS_((char *string));
294 static void Unlink _ANSI_ARGS_((Gridder *gridPtr));
295
296 /*
297 * Prototypes for procedures contained in other files but not exported
298 * using tkIntDecls.h
299 */
300
301 void TkPrintPadAmount _ANSI_ARGS_((Tcl_Interp*, char*, int, int));
302 int TkParsePadAmount _ANSI_ARGS_((Tcl_Interp*, Tk_Window, Tcl_Obj*, int*, int*));
303
304 static Tk_GeomMgr gridMgrType = {
305 "grid", /* name */
306 GridReqProc, /* requestProc */
307 GridLostSlaveProc, /* lostSlaveProc */
308 };
309
310 /*
311 *--------------------------------------------------------------
312 *
313 * Tk_GridCmd --
314 *
315 * This procedure is invoked to process the "grid" Tcl command.
316 * See the user documentation for details on what it does.
317 *
318 * Results:
319 * A standard Tcl result.
320 *
321 * Side effects:
322 * See the user documentation.
323 *
324 *--------------------------------------------------------------
325 */
326
327 int
328 Tk_GridObjCmd(clientData, interp, objc, objv)
329 ClientData clientData; /* Main window associated with
330 * interpreter. */
331 Tcl_Interp *interp; /* Current interpreter. */
332 int objc; /* Number of arguments. */
333 Tcl_Obj *CONST objv[]; /* Argument objects. */
334 {
335 Tk_Window tkwin = (Tk_Window) clientData;
336 static CONST char *optionStrings[] = {
337 "bbox", "columnconfigure", "configure", "forget",
338 "info", "location", "propagate", "remove",
339 "rowconfigure", "size", "slaves", (char *) NULL };
340 enum options {
341 GRID_BBOX, GRID_COLUMNCONFIGURE, GRID_CONFIGURE, GRID_FORGET,
342 GRID_INFO, GRID_LOCATION, GRID_PROPAGATE, GRID_REMOVE,
343 GRID_ROWCONFIGURE, GRID_SIZE, GRID_SLAVES };
344 int index;
345
346
347 if (objc >= 2) {
348 char *argv1 = Tcl_GetString(objv[1]);
349 if ((argv1[0] == '.') || (argv1[0] == REL_SKIP) ||
350 (argv1[0] == REL_VERT)) {
351 return ConfigureSlaves(interp, tkwin, objc-1, objv+1);
352 }
353 }
354 if (objc < 3) {
355 Tcl_WrongNumArgs(interp, 1, objv, "option arg ?arg ...?");
356 return TCL_ERROR;
357 }
358
359 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
360 &index) != TCL_OK) {
361 return TCL_ERROR;
362 }
363
364 switch ((enum options) index) {
365 case GRID_BBOX:
366 return GridBboxCommand(tkwin, interp, objc, objv);
367 case GRID_CONFIGURE:
368 return ConfigureSlaves(interp, tkwin, objc-2, objv+2);
369 case GRID_FORGET:
370 case GRID_REMOVE:
371 return GridForgetRemoveCommand(tkwin, interp, objc, objv);
372 case GRID_INFO:
373 return GridInfoCommand(tkwin, interp, objc, objv);
374 case GRID_LOCATION:
375 return GridLocationCommand(tkwin, interp, objc, objv);
376 case GRID_PROPAGATE:
377 return GridPropagateCommand(tkwin, interp, objc, objv);
378 case GRID_SIZE:
379 return GridSizeCommand(tkwin, interp, objc, objv);
380 case GRID_SLAVES:
381 return GridSlavesCommand(tkwin, interp, objc, objv);
382
383 /*
384 * Sample argument combinations:
385 * grid columnconfigure <master> <index> -option
386 * grid columnconfigure <master> <index> -option value -option value
387 * grid rowconfigure <master> <index>
388 * grid rowconfigure <master> <index> -option
389 * grid rowconfigure <master> <index> -option value -option value.
390 */
391
392 case GRID_COLUMNCONFIGURE:
393 case GRID_ROWCONFIGURE:
394 return GridRowColumnConfigureCommand(tkwin, interp, objc, objv);
395 }
396
397 /* This should not happen */
398 Tcl_SetResult(interp, "Internal error in grid.", TCL_STATIC);
399 return TCL_ERROR;
400 }
401
402 /*
403 *----------------------------------------------------------------------
404 *
405 * GridBboxCommand --
406 *
407 * Implementation of the [grid bbox] subcommand.
408 *
409 * Results:
410 * Standard Tcl result.
411 *
412 * Side effects:
413 * Places bounding box information in the interp's result field.
414 *
415 *----------------------------------------------------------------------
416 */
417
418 static int
419 GridBboxCommand(tkwin, interp, objc, objv)
420 Tk_Window tkwin; /* Main window of the application. */
421 Tcl_Interp *interp; /* Current interpreter. */
422 int objc; /* Number of arguments. */
423 Tcl_Obj *CONST objv[]; /* Argument objects. */
424 {
425 Tk_Window master;
426 Gridder *masterPtr; /* master grid record */
427 GridMaster *gridPtr; /* pointer to grid data */
428 int row, column; /* origin for bounding box */
429 int row2, column2; /* end of bounding box */
430 int endX, endY; /* last column/row in the layout */
431 int x=0, y=0; /* starting pixels for this bounding box */
432 int width, height; /* size of the bounding box */
433
434 if (objc!=3 && objc != 5 && objc != 7) {
435 Tcl_WrongNumArgs(interp, 2, objv, "master ?column row ?column row??");
436 return TCL_ERROR;
437 }
438
439 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
440 return TCL_ERROR;
441 }
442 masterPtr = GetGrid(master);
443
444 if (objc >= 5) {
445 if (Tcl_GetIntFromObj(interp, objv[3], &column) != TCL_OK) {
446 return TCL_ERROR;
447 }
448 if (Tcl_GetIntFromObj(interp, objv[4], &row) != TCL_OK) {
449 return TCL_ERROR;
450 }
451 column2 = column;
452 row2 = row;
453 }
454
455 if (objc == 7) {
456 if (Tcl_GetIntFromObj(interp, objv[5], &column2) != TCL_OK) {
457 return TCL_ERROR;
458 }
459 if (Tcl_GetIntFromObj(interp, objv[6], &row2) != TCL_OK) {
460 return TCL_ERROR;
461 }
462 }
463
464 gridPtr = masterPtr->masterDataPtr;
465 if (gridPtr == NULL) {
466 Tcl_SetObjResult(interp, NewQuadObj(interp, 0, 0, 0, 0));
467 return TCL_OK;
468 }
469
470 SetGridSize(masterPtr);
471 endX = MAX(gridPtr->columnEnd, gridPtr->columnMax);
472 endY = MAX(gridPtr->rowEnd, gridPtr->rowMax);
473
474 if ((endX == 0) || (endY == 0)) {
475 Tcl_SetObjResult(interp, NewQuadObj(interp, 0, 0, 0, 0));
476 return TCL_OK;
477 }
478 if (objc == 3) {
479 row = column = 0;
480 row2 = endY;
481 column2 = endX;
482 }
483
484 if (column > column2) {
485 int temp = column;
486 column = column2, column2 = temp;
487 }
488 if (row > row2) {
489 int temp = row;
490 row = row2, row2 = temp;
491 }
492
493 if (column > 0 && column < endX) {
494 x = gridPtr->columnPtr[column-1].offset;
495 } else if (column > 0) {
496 x = gridPtr->columnPtr[endX-1].offset;
497 }
498
499 if (row > 0 && row < endY) {
500 y = gridPtr->rowPtr[row-1].offset;
501 } else if (row > 0) {
502 y = gridPtr->rowPtr[endY-1].offset;
503 }
504
505 if (column2 < 0) {
506 width = 0;
507 } else if (column2 >= endX) {
508 width = gridPtr->columnPtr[endX-1].offset - x;
509 } else {
510 width = gridPtr->columnPtr[column2].offset - x;
511 }
512
513 if (row2 < 0) {
514 height = 0;
515 } else if (row2 >= endY) {
516 height = gridPtr->rowPtr[endY-1].offset - y;
517 } else {
518 height = gridPtr->rowPtr[row2].offset - y;
519 }
520
521 Tcl_SetObjResult(interp, NewQuadObj(interp,
522 x + gridPtr->startX, y + gridPtr->startY, width, height));
523 return TCL_OK;
524 }
525
526 /*
527 *----------------------------------------------------------------------
528 *
529 * GridForgetRemoveCommand --
530 *
531 * Implementation of the [grid forget]/[grid remove] subcommands.
532 * See the user documentation for details on what these do.
533 *
534 * Results:
535 * Standard Tcl result.
536 *
537 * Side effects:
538 * Removes a window from a grid layout.
539 *
540 *----------------------------------------------------------------------
541 */
542
543 static int
544 GridForgetRemoveCommand(tkwin, interp, objc, objv)
545 Tk_Window tkwin; /* Main window of the application. */
546 Tcl_Interp *interp; /* Current interpreter. */
547 int objc; /* Number of arguments. */
548 Tcl_Obj *CONST objv[]; /* Argument objects. */
549 {
550 Tk_Window slave;
551 Gridder *slavePtr;
552 int i;
553 char *string = Tcl_GetString(objv[1]);
554 char c = string[0];
555
556 for (i = 2; i < objc; i++) {
557 if (TkGetWindowFromObj(interp, tkwin, objv[i], &slave) != TCL_OK) {
558 return TCL_ERROR;
559 }
560
561 slavePtr = GetGrid(slave);
562 if (slavePtr->masterPtr != NULL) {
563
564 /*
565 * For "forget", reset all the settings to their defaults
566 */
567
568 if (c == 'f') {
569 slavePtr->column = slavePtr->row = -1;
570 slavePtr->numCols = 1;
571 slavePtr->numRows = 1;
572 slavePtr->padX = slavePtr->padY = 0;
573 slavePtr->padLeft = slavePtr->padTop = 0;
574 slavePtr->iPadX = slavePtr->iPadY = 0;
575 slavePtr->doubleBw = 2*Tk_Changes(tkwin)->border_width;
576 if (slavePtr->flags & REQUESTED_RELAYOUT) {
577 Tcl_CancelIdleCall(ArrangeGrid, (ClientData) slavePtr);
578 }
579 slavePtr->flags = 0;
580 slavePtr->sticky = 0;
581 }
582 Tk_ManageGeometry(slave, (Tk_GeomMgr *) NULL,
583 (ClientData) NULL);
584 if (slavePtr->masterPtr->tkwin != Tk_Parent(slavePtr->tkwin)) {
585 Tk_UnmaintainGeometry(slavePtr->tkwin,
586 slavePtr->masterPtr->tkwin);
587 }
588 Unlink(slavePtr);
589 Tk_UnmapWindow(slavePtr->tkwin);
590 }
591 }
592 return TCL_OK;
593 }
594
595 /*
596 *----------------------------------------------------------------------
597 *
598 * GridInfoCommand --
599 *
600 * Implementation of the [grid info] subcommand. See the user
601 * documentation for details on what it does.
602 *
603 * Results:
604 * Standard Tcl result.
605 *
606 * Side effects:
607 * Puts gridding information in the interpreter's result.
608 *
609 *----------------------------------------------------------------------
610 */
611
612 static int
613 GridInfoCommand(tkwin, interp, objc, objv)
614 Tk_Window tkwin; /* Main window of the application. */
615 Tcl_Interp *interp; /* Current interpreter. */
616 int objc; /* Number of arguments. */
617 Tcl_Obj *CONST objv[]; /* Argument objects. */
618 {
619 register Gridder *slavePtr;
620 Tk_Window slave;
621 char buffer[64 + TCL_INTEGER_SPACE * 4];
622
623 if (objc != 3) {
624 Tcl_WrongNumArgs(interp, 2, objv, "window");
625 return TCL_ERROR;
626 }
627 if (TkGetWindowFromObj(interp, tkwin, objv[2], &slave) != TCL_OK) {
628 return TCL_ERROR;
629 }
630 slavePtr = GetGrid(slave);
631 if (slavePtr->masterPtr == NULL) {
632 Tcl_ResetResult(interp);
633 return TCL_OK;
634 }
635
636 Tcl_AppendElement(interp, "-in");
637 Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),
638 LangWidgetObj(interp, slavePtr->masterPtr->tkwin));
639 Tcl_AppendElement(interp, "-column");
640 Tcl_IntResults(interp, 1, 1, slavePtr->column);
641 Tcl_AppendElement(interp, "-row");
642 Tcl_IntResults(interp, 1, 1, slavePtr->row);
643 Tcl_AppendElement(interp, "-columnspan");
644 Tcl_IntResults(interp, 1, 1, slavePtr->numCols);
645 Tcl_AppendElement(interp, "-rowspan");
646 Tcl_IntResults(interp, 1, 1, slavePtr->numRows);
647
648 TkPrintPadAmount(interp, "ipadx", slavePtr->iPadX/2, slavePtr->iPadX);
649 TkPrintPadAmount(interp, "ipady", slavePtr->iPadY/2, slavePtr->iPadY);
650 TkPrintPadAmount(interp, "padx", slavePtr->padLeft, slavePtr->padX);
651 TkPrintPadAmount(interp, "pady", slavePtr->padTop, slavePtr->padY);
652 StickyToString(slavePtr->sticky,buffer);
653 Tcl_AppendElement(interp, "-sticky");
654 Tcl_AppendElement(interp,buffer);
655 return TCL_OK;
656 }
657
658 /*
659 *----------------------------------------------------------------------
660 *
661 * GridLocationCommand --
662 *
663 * Implementation of the [grid location] subcommand. See the user
664 * documentation for details on what it does.
665 *
666 * Results:
667 * Standard Tcl result.
668 *
669 * Side effects:
670 * Puts location information in the interpreter's result field.
671 *
672 *----------------------------------------------------------------------
673 */
674
675 static int
676 GridLocationCommand(tkwin, interp, objc, objv)
677 Tk_Window tkwin; /* Main window of the application. */
678 Tcl_Interp *interp; /* Current interpreter. */
679 int objc; /* Number of arguments. */
680 Tcl_Obj *CONST objv[]; /* Argument objects. */
681 {
682 Tk_Window master;
683 Gridder *masterPtr; /* master grid record */
684 GridMaster *gridPtr; /* pointer to grid data */
685 register SlotInfo *slotPtr;
686 int x, y; /* Offset in pixels, from edge of parent. */
687 int i, j; /* Corresponding column and row indeces. */
688 int endX, endY; /* end of grid */
689
690 if (objc != 5) {
691 Tcl_WrongNumArgs(interp, 2, objv, "master x y");
692 return TCL_ERROR;
693 }
694
695 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
696 return TCL_ERROR;
697 }
698
699 if (Tk_GetPixelsFromObj(interp, master, objv[3], &x) != TCL_OK) {
700 return TCL_ERROR;
701 }
702 if (Tk_GetPixelsFromObj(interp, master, objv[4], &y) != TCL_OK) {
703 return TCL_ERROR;
704 }
705
706 masterPtr = GetGrid(master);
707 if (masterPtr->masterDataPtr == NULL) {
708 Tcl_SetObjResult(interp, NewPairObj(interp, -1, -1));
709 return TCL_OK;
710 }
711 gridPtr = masterPtr->masterDataPtr;
712
713 /*
714 * Update any pending requests. This is not always the
715 * steady state value, as more configure events could be in
716 * the pipeline, but its as close as its easy to get.
717 */
718
719 while (masterPtr->flags & REQUESTED_RELAYOUT) {
720 Tcl_CancelIdleCall(ArrangeGrid, (ClientData) masterPtr);
721 ArrangeGrid ((ClientData) masterPtr);
722 }
723 SetGridSize(masterPtr);
724 endX = MAX(gridPtr->columnEnd, gridPtr->columnMax);
725 endY = MAX(gridPtr->rowEnd, gridPtr->rowMax);
726
727 slotPtr = masterPtr->masterDataPtr->columnPtr;
728 if (x < masterPtr->masterDataPtr->startX) {
729 i = -1;
730 } else {
731 x -= masterPtr->masterDataPtr->startX;
732 for (i = 0; slotPtr[i].offset < x && i < endX; i++) {
733 /* null body */
734 }
735 }
736
737 slotPtr = masterPtr->masterDataPtr->rowPtr;
738 if (y < masterPtr->masterDataPtr->startY) {
739 j = -1;
740 } else {
741 y -= masterPtr->masterDataPtr->startY;
742 for (j = 0; slotPtr[j].offset < y && j < endY; j++) {
743 /* null body */
744 }
745 }
746
747 Tcl_SetObjResult(interp, NewPairObj(interp, i, j));
748 return TCL_OK;
749 }
750
751 /*
752 *----------------------------------------------------------------------
753 *
754 * GridPropagateCommand --
755 *
756 * Implementation of the [grid propagate] subcommand. See the user
757 * documentation for details on what it does.
758 *
759 * Results:
760 * Standard Tcl result.
761 *
762 * Side effects:
763 * May alter geometry propagation for a widget.
764 *
765 *----------------------------------------------------------------------
766 */
767
768 static int
769 GridPropagateCommand(tkwin, interp, objc, objv)
770 Tk_Window tkwin; /* Main window of the application. */
771 Tcl_Interp *interp; /* Current interpreter. */
772 int objc; /* Number of arguments. */
773 Tcl_Obj *CONST objv[]; /* Argument objects. */
774 {
775 Tk_Window master;
776 Gridder *masterPtr;
777 int propagate, old;
778
779 if (objc > 4) {
780 Tcl_WrongNumArgs(interp, 2, objv, "window ?boolean?");
781 return TCL_ERROR;
782 }
783
784 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
785 return TCL_ERROR;
786 }
787 masterPtr = GetGrid(master);
788 if (objc == 3) {
789 Tcl_SetObjResult(interp,
790 Tcl_NewBooleanObj(!(masterPtr->flags & DONT_PROPAGATE)));
791 return TCL_OK;
792 }
793 if (Tcl_GetBooleanFromObj(interp, objv[3], &propagate) != TCL_OK) {
794 return TCL_ERROR;
795 }
796
797 /* Only request a relayout if the propagation bit changes */
798
799 old = !(masterPtr->flags & DONT_PROPAGATE);
800 if (propagate != old) {
801 if (propagate) {
802 masterPtr->flags &= ~DONT_PROPAGATE;
803 } else {
804 masterPtr->flags |= DONT_PROPAGATE;
805 }
806
807 /*
808 * Re-arrange the master to allow new geometry information to
809 * propagate upwards to the master's master.
810 */
811
812 if (masterPtr->abortPtr != NULL) {
813 *masterPtr->abortPtr = 1;
814 }
815 if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
816 masterPtr->flags |= REQUESTED_RELAYOUT;
817 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) masterPtr);
818 }
819 }
820 return TCL_OK;
821 }
822
823 /*
824 *----------------------------------------------------------------------
825 *
826 * GridRowColumnConfigureCommand --
827 *
828 * Implementation of the [grid rowconfigure] and [grid columnconfigure]
829 * subcommands. See the user documentation for details on what these
830 * do.
831 *
832 * Results:
833 * Standard Tcl result.
834 *
835 * Side effects:
836 * Depends on arguments; see user documentation.
837 *
838 *----------------------------------------------------------------------
839 */
840
841 static int
842 GridRowColumnConfigureCommand(tkwin, interp, objc, objv)
843 Tk_Window tkwin; /* Main window of the application. */
844 Tcl_Interp *interp; /* Current interpreter. */
845 int objc; /* Number of arguments. */
846 Tcl_Obj *CONST objv[]; /* Argument objects. */
847 {
848 Tk_Window master;
849 Gridder *masterPtr;
850 SlotInfo *slotPtr = NULL;
851 int slot; /* the column or row number */
852 int slotType; /* COLUMN or ROW */
853 int size; /* the configuration value */
854 int checkOnly; /* check the size only */
855 int lObjc; /* Number of items in index list */
856 Tcl_Obj **lObjv; /* array of indices */
857 int ok; /* temporary TCL result code */
858 int i, j;
859 char *string;
860 static CONST char *optionStrings[] = {
861 "-minsize", "-pad", "-uniform", "-weight", (char *) NULL };
862 enum options { ROWCOL_MINSIZE, ROWCOL_PAD, ROWCOL_UNIFORM, ROWCOL_WEIGHT };
863 int index;
864
865 if (((objc % 2 != 0) && (objc > 6)) || (objc < 4)) {
866 Tcl_WrongNumArgs(interp, 2, objv, "master index ?-option value...?");
867 return TCL_ERROR;
868 }
869
870 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
871 return TCL_ERROR;
872 }
873
874 if (Tcl_ListObjGetElements(interp, objv[3], &lObjc, &lObjv) != TCL_OK) {
875 return TCL_ERROR;
876 }
877
878 string = Tcl_GetString(objv[1]);
879 checkOnly = ((objc == 4) || (objc == 5));
880 masterPtr = GetGrid(master);
881 slotType = (*string == 'c') ? COLUMN : ROW;
882 if (checkOnly && lObjc > 1) {
883 Tcl_AppendResult(interp, Tcl_GetString(objv[3]),
884 " must be a single element.", (char *) NULL);
885 return TCL_ERROR;
886 }
887 for (j = 0; j < lObjc; j++) {
888 if (Tcl_GetIntFromObj(interp, lObjv[j], &slot) != TCL_OK) {
889 return TCL_ERROR;
890 }
891 ok = CheckSlotData(masterPtr, slot, slotType, checkOnly);
892 if ((ok != TCL_OK) && ((objc < 4) || (objc > 5))) {
893 Tcl_AppendResult(interp, Tcl_GetString(objv[0]), " ",
894 Tcl_GetString(objv[1]), ": \"", Tcl_GetString(lObjv[j]),
895 "\" is out of range", (char *) NULL);
896 return TCL_ERROR;
897 } else if (ok == TCL_OK) {
898 slotPtr = (slotType == COLUMN) ?
899 masterPtr->masterDataPtr->columnPtr :
900 masterPtr->masterDataPtr->rowPtr;
901 }
902
903 /*
904 * Return all of the options for this row or column. If the
905 * request is out of range, return all 0's.
906 */
907
908 if (objc == 4) {
909 int minsize = 0, pad = 0, weight = 0;
910 Tk_Uid uniform = NULL;
911 Tcl_Obj *res = Tcl_NewListObj(0, NULL);
912
913 if (ok == TCL_OK) {
914 minsize = slotPtr[slot].minSize;
915 pad = slotPtr[slot].pad;
916 weight = slotPtr[slot].weight;
917 uniform = slotPtr[slot].uniform;
918 }
919
920 Tcl_ListObjAppendElement(interp, res,
921 Tcl_NewStringObj("-minsize", -1));
922 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(minsize));
923 Tcl_ListObjAppendElement(interp, res,
924 Tcl_NewStringObj("-pad", -1));
925 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(pad));
926 Tcl_ListObjAppendElement(interp, res,
927 Tcl_NewStringObj("-uniform", -1));
928 Tcl_ListObjAppendElement(interp, res,
929 Tcl_NewStringObj(uniform == NULL ? "" : uniform, -1));
930 Tcl_ListObjAppendElement(interp, res,
931 Tcl_NewStringObj("-weight", -1));
932 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(weight));
933 Tcl_SetObjResult(interp, res);
934 return TCL_OK;
935 }
936
937 /*
938 * Loop through each option value pair, setting the values as
939 * required. If only one option is given, with no value, the
940 * current value is returned.
941 */
942
943 for (i = 4; i < objc; i += 2) {
944 if (Tcl_GetIndexFromObj(interp, objv[i], optionStrings, "option", 0,
945 &index) != TCL_OK) {
946 return TCL_ERROR;
947 }
948 if (index == ROWCOL_MINSIZE) {
949 if (objc == 5) {
950 Tcl_SetObjResult(interp, Tcl_NewIntObj(
951 (ok == TCL_OK) ? slotPtr[slot].minSize : 0));
952 } else if (Tk_GetPixelsFromObj(interp, master, objv[i+1], &size)
953 != TCL_OK) {
954 return TCL_ERROR;
955 } else {
956 slotPtr[slot].minSize = size;
957 }
958 }
959 else if (index == ROWCOL_WEIGHT) {
960 int wt;
961 if (objc == 5) {
962 Tcl_SetObjResult(interp, Tcl_NewIntObj(
963 (ok == TCL_OK) ? slotPtr[slot].weight : 0));
964 } else if (Tcl_GetIntFromObj(interp, objv[i+1], &wt)
965 != TCL_OK) {
966 return TCL_ERROR;
967 } else if (wt < 0) {
968 Tcl_AppendResult(interp, "invalid arg \"",
969 Tcl_GetString(objv[i]),
970 "\": should be non-negative", (char *) NULL);
971 return TCL_ERROR;
972 } else {
973 slotPtr[slot].weight = wt;
974 }
975 }
976 else if (index == ROWCOL_UNIFORM) {
977 if (objc == 5) {
978 Tk_Uid value;
979 value = (ok == TCL_OK) ? slotPtr[slot].uniform : "";
980 if (value == NULL) {
981 value = "";
982 }
983 Tcl_SetObjResult(interp, Tcl_NewStringObj(value, -1));
984 } else {
985 slotPtr[slot].uniform = Tk_GetUid(Tcl_GetString(objv[i+1]));
986 if (slotPtr[slot].uniform != NULL &&
987 slotPtr[slot].uniform[0] == 0) {
988 slotPtr[slot].uniform = NULL;
989 }
990 }
991 }
992 else if (index == ROWCOL_PAD) {
993 if (objc == 5) {
994 Tcl_SetObjResult(interp, Tcl_NewIntObj(
995 (ok == TCL_OK) ? slotPtr[slot].pad : 0));
996 } else if (Tk_GetPixelsFromObj(interp, master, objv[i+1], &size)
997 != TCL_OK) {
998 return TCL_ERROR;
999 } else if (size < 0) {
1000 Tcl_AppendResult(interp, "invalid arg \"",
1001 Tcl_GetString(objv[i]),
1002 "\": should be non-negative", (char *) NULL);
1003 return TCL_ERROR;
1004 } else {
1005 slotPtr[slot].pad = size;
1006 }
1007 }
1008 }
1009 }
1010
1011 /*
1012 * If we changed a property, re-arrange the table,
1013 * and check for constraint shrinkage.
1014 */
1015
1016 if (objc != 5) {
1017 if (slotType == ROW) {
1018 int last = masterPtr->masterDataPtr->rowMax - 1;
1019 while ((last >= 0) && (slotPtr[last].weight == 0)
1020 && (slotPtr[last].pad == 0)
1021 && (slotPtr[last].minSize == 0)
1022 && (slotPtr[last].uniform == NULL)) {
1023 last--;
1024 }
1025 masterPtr->masterDataPtr->rowMax = last+1;
1026 } else {
1027 int last = masterPtr->masterDataPtr->columnMax - 1;
1028 while ((last >= 0) && (slotPtr[last].weight == 0)
1029 && (slotPtr[last].pad == 0)
1030 && (slotPtr[last].minSize == 0)
1031 && (slotPtr[last].uniform == NULL)) {
1032 last--;
1033 }
1034 masterPtr->masterDataPtr->columnMax = last + 1;
1035 }
1036
1037 if (masterPtr->abortPtr != NULL) {
1038 *masterPtr->abortPtr = 1;
1039 }
1040 if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
1041 masterPtr->flags |= REQUESTED_RELAYOUT;
1042 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) masterPtr);
1043 }
1044 }
1045 return TCL_OK;
1046 }
1047
1048 /*
1049 *----------------------------------------------------------------------
1050 *
1051 * GridSizeCommand --
1052 *
1053 * Implementation of the [grid size] subcommand. See the user
1054 * documentation for details on what it does.
1055 *
1056 * Results:
1057 * Standard Tcl result.
1058 *
1059 * Side effects:
1060 * Puts grid size information in the interpreter's result.
1061 *
1062 *----------------------------------------------------------------------
1063 */
1064
1065 static int
1066 GridSizeCommand(tkwin, interp, objc, objv)
1067 Tk_Window tkwin; /* Main window of the application. */
1068 Tcl_Interp *interp; /* Current interpreter. */
1069 int objc; /* Number of arguments. */
1070 Tcl_Obj *CONST objv[]; /* Argument objects. */
1071 {
1072 Tk_Window master;
1073 Gridder *masterPtr;
1074 GridMaster *gridPtr; /* pointer to grid data */
1075
1076 if (objc != 3) {
1077 Tcl_WrongNumArgs(interp, 2, objv, "window");
1078 return TCL_ERROR;
1079 }
1080
1081 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
1082 return TCL_ERROR;
1083 }
1084 masterPtr = GetGrid(master);
1085
1086 if (masterPtr->masterDataPtr != NULL) {
1087 SetGridSize(masterPtr);
1088 gridPtr = masterPtr->masterDataPtr;
1089 Tcl_SetObjResult(interp, NewPairObj(interp,
1090 MAX(gridPtr->columnEnd, gridPtr->columnMax),
1091 MAX(gridPtr->rowEnd, gridPtr->rowMax)));
1092 } else {
1093 Tcl_SetObjResult(interp, NewPairObj(interp, 0, 0));
1094 }
1095 return TCL_OK;
1096 }
1097
1098 /*
1099 *----------------------------------------------------------------------
1100 *
1101 * GridSlavesCommand --
1102 *
1103 * Implementation of the [grid slaves] subcommand. See the user
1104 * documentation for details on what it does.
1105 *
1106 * Results:
1107 * Standard Tcl result.
1108 *
1109 * Side effects:
1110 * Places a list of slaves of the specified window in the
1111 * interpreter's result field.
1112 *
1113 *----------------------------------------------------------------------
1114 */
1115
1116 static int
1117 GridSlavesCommand(tkwin, interp, objc, objv)
1118 Tk_Window tkwin; /* Main window of the application. */
1119 Tcl_Interp *interp; /* Current interpreter. */
1120 int objc; /* Number of arguments. */
1121 Tcl_Obj *CONST objv[]; /* Argument objects. */
1122 {
1123 Tk_Window master;
1124 Gridder *masterPtr; /* master grid record */
1125 Gridder *slavePtr;
1126 int i, value;
1127 int row = -1, column = -1;
1128 static CONST char *optionStrings[] = {
1129 "-column", "-row", (char *) NULL };
1130 enum options { SLAVES_COLUMN, SLAVES_ROW };
1131 int index;
1132 Tcl_Obj *res;
1133
1134 if ((objc < 3) || ((objc % 2) == 0)) {
1135 Tcl_WrongNumArgs(interp, 2, objv, "window ?-option value...?");
1136 return TCL_ERROR;
1137 }
1138
1139 for (i = 3; i < objc; i += 2) {
1140 if (Tcl_GetIndexFromObj(interp, objv[i], optionStrings, "option", 0,
1141 &index) != TCL_OK) {
1142 return TCL_ERROR;
1143 }
1144 if (Tcl_GetIntFromObj(interp, objv[i+1], &value) != TCL_OK) {
1145 return TCL_ERROR;
1146 }
1147 if (value < 0) {
1148 Tcl_AppendResult(interp, Tcl_GetString(objv[i]),
1149 " is an invalid value: should NOT be < 0",
1150 (char *) NULL);
1151 return TCL_ERROR;
1152 }
1153 if (index == SLAVES_COLUMN) {
1154 column = value;
1155 } else {
1156 row = value;
1157 }
1158 }
1159
1160 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
1161 return TCL_ERROR;
1162 }
1163 masterPtr = GetGrid(master);
1164
1165 res = Tcl_NewListObj(0, NULL);
1166 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
1167 slavePtr = slavePtr->nextPtr) {
1168 if (column>=0 && (slavePtr->column > column
1169 || slavePtr->column+slavePtr->numCols-1 < column)) {
1170 continue;
1171 }
1172 if (row>=0 && (slavePtr->row > row ||
1173 slavePtr->row+slavePtr->numRows-1 < row)) {
1174 continue;
1175 }
1176 /*Tcl_ListObjAppendElement(interp, res,
1177 Tcl_NewStringObj(Tk_PathName(slavePtr->tkwin), -1));*/
1178 Tcl_ListObjAppendElement(interp, res,
1179 LangWidgetObj(interp,slavePtr->tkwin));
1180 }
1181 Tcl_SetObjResult(interp, res);
1182 return TCL_OK;
1183 }
1184
1185 /*
1186 *--------------------------------------------------------------
1187 *
1188 * GridReqProc --
1189 *
1190 * This procedure is invoked by Tk_GeometryRequest for
1191 * windows managed by the grid.
1192 *
1193 * Results:
1194 * None.
1195 *
1196 * Side effects:
1197 * Arranges for tkwin, and all its managed siblings, to
1198 * be re-arranged at the next idle point.
1199 *
1200 *--------------------------------------------------------------
1201 */
1202
1203 static void
1204 GridReqProc(clientData, tkwin)
1205 ClientData clientData; /* Grid's information about
1206 * window that got new preferred
1207 * geometry. */
1208 Tk_Window tkwin; /* Other Tk-related information
1209 * about the window. */
1210 {
1211 register Gridder *gridPtr = (Gridder *) clientData;
1212
1213 gridPtr = gridPtr->masterPtr;
1214 if (gridPtr && !(gridPtr->flags & REQUESTED_RELAYOUT)) {
1215 gridPtr->flags |= REQUESTED_RELAYOUT;
1216 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) gridPtr);
1217 }
1218 }
1219
1220 /*
1221 *--------------------------------------------------------------
1222 *
1223 * GridLostSlaveProc --
1224 *
1225 * This procedure is invoked by Tk whenever some other geometry
1226 * claims control over a slave that used to be managed by us.
1227 *
1228 * Results:
1229 * None.
1230 *
1231 * Side effects:
1232 * Forgets all grid-related information about the slave.
1233 *
1234 *--------------------------------------------------------------
1235 */
1236
1237 static void
1238 GridLostSlaveProc(clientData, tkwin)
1239 ClientData clientData; /* Grid structure for slave window that
1240 * was stolen away. */
1241 Tk_Window tkwin; /* Tk's handle for the slave window. */
1242 {
1243 register Gridder *slavePtr = (Gridder *) clientData;
1244
1245 if (slavePtr->masterPtr->tkwin != Tk_Parent(slavePtr->tkwin)) {
1246 Tk_UnmaintainGeometry(slavePtr->tkwin, slavePtr->masterPtr->tkwin);
1247 }
1248 Unlink(slavePtr);
1249 Tk_UnmapWindow(slavePtr->tkwin);
1250 }
1251
1252 /*
1253 *--------------------------------------------------------------
1254 *
1255 * AdjustOffsets --
1256 *
1257 * This procedure adjusts the size of the layout to fit in the
1258 * space provided. If it needs more space, the extra is added
1259 * according to the weights. If it needs less, the space is removed
1260 * according to the weights, but at no time does the size drop below
1261 * the minsize specified for that slot.
1262 *
1263 * Results:
1264 * The initial offset of the layout,
1265 * if all the weights are zero, else 0.
1266 *
1267 * Side effects:
1268 * The slot offsets are modified to shrink the layout.
1269 *
1270 *--------------------------------------------------------------
1271 */
1272
1273 static int
1274 AdjustOffsets(size, slots, slotPtr)
1275 int size; /* The total layout size (in pixels). */
1276 int slots; /* Number of slots. */
1277 register SlotInfo *slotPtr; /* Pointer to slot array. */
1278 {
1279 register int slot; /* Current slot. */
1280 int diff; /* Extra pixels needed to add to the layout. */
1281 int totalWeight = 0; /* Sum of the weights for all the slots. */
1282 int weight = 0; /* Sum of the weights so far. */
1283 int minSize = 0; /* Minimum possible layout size. */
1284 int newDiff; /* The most pixels that can be added on
1285 * the current pass. */
1286
1287 diff = size - slotPtr[slots-1].offset;
1288
1289 /*
1290 * The layout is already the correct size; all done.
1291 */
1292
1293 if (diff == 0) {
1294 return(0);
1295 }
1296
1297 /*
1298 * If all the weights are zero, center the layout in its parent if
1299 * there is extra space, else clip on the bottom/right.
1300 */
1301
1302 for (slot=0; slot < slots; slot++) {
1303 totalWeight += slotPtr[slot].weight;
1304 }
1305
1306 if (totalWeight == 0 ) {
1307 return(diff > 0 ? diff/2 : 0);
1308 }
1309
1310 /*
1311 * Add extra space according to the slot weights. This is done
1312 * cumulatively to prevent round-off error accumulation.
1313 */
1314
1315 if (diff > 0) {
1316 for (weight=slot=0; slot < slots; slot++) {
1317 weight += slotPtr[slot].weight;
1318 slotPtr[slot].offset += diff * weight / totalWeight;
1319 }
1320 return(0);
1321 }
1322
1323 /*
1324 * The layout must shrink below its requested size. Compute the
1325 * minimum possible size by looking at the slot minSizes.
1326 */
1327
1328 for (slot=0; slot < slots; slot++) {
1329 if (slotPtr[slot].weight > 0) {
1330 minSize += slotPtr[slot].minSize;
1331 } else if (slot > 0) {
1332 minSize += slotPtr[slot].offset - slotPtr[slot-1].offset;
1333 } else {
1334 minSize += slotPtr[slot].offset;
1335 }
1336 }
1337
1338 /*
1339 * If the requested size is less than the minimum required size,
1340 * set the slot sizes to their minimum values, then clip on the
1341 * bottom/right.
1342 */
1343
1344 if (size <= minSize) {
1345 int offset = 0;
1346 for (slot=0; slot < slots; slot++) {
1347 if (slotPtr[slot].weight > 0) {
1348 offset += slotPtr[slot].minSize;
1349 } else if (slot > 0) {
1350 offset += slotPtr[slot].offset - slotPtr[slot-1].offset;
1351 } else {
1352 offset += slotPtr[slot].offset;
1353 }
1354 slotPtr[slot].offset = offset;
1355 }
1356 return(0);
1357 }
1358
1359 /*
1360 * Remove space from slots according to their weights. The weights
1361 * get renormalized anytime a slot shrinks to its minimum size.
1362 */
1363
1364 while (diff < 0) {
1365
1366 /*
1367 * Find the total weight for the shrinkable slots.
1368 */
1369
1370 for (totalWeight=slot=0; slot < slots; slot++) {
1371 int current = (slot == 0) ? slotPtr[slot].offset :
1372 slotPtr[slot].offset - slotPtr[slot-1].offset;
1373 if (current > slotPtr[slot].minSize) {
1374 totalWeight += slotPtr[slot].weight;
1375 slotPtr[slot].temp = slotPtr[slot].weight;
1376 } else {
1377 slotPtr[slot].temp = 0;
1378 }
1379 }
1380 if (totalWeight == 0) {
1381 break;
1382 }
1383
1384 /*
1385 * Find the maximum amount of space we can distribute this pass.
1386 */
1387
1388 newDiff = diff;
1389 for (slot = 0; slot < slots; slot++) {
1390 int current; /* current size of this slot */
1391 int maxDiff; /* max diff that would cause
1392 * this slot to equal its minsize */
1393 if (slotPtr[slot].temp == 0) {
1394 continue;
1395 }
1396 current = (slot == 0) ? slotPtr[slot].offset :
1397 slotPtr[slot].offset - slotPtr[slot-1].offset;
1398 maxDiff = totalWeight * (slotPtr[slot].minSize - current)
1399 / slotPtr[slot].temp;
1400 if (maxDiff > newDiff) {
1401 newDiff = maxDiff;
1402 }
1403 }
1404
1405 /*
1406 * Now distribute the space.
1407 */
1408
1409 for (weight=slot=0; slot < slots; slot++) {
1410 weight += slotPtr[slot].temp;
1411 slotPtr[slot].offset += newDiff * weight / totalWeight;
1412 }
1413 diff -= newDiff;
1414 }
1415 return(0);
1416 }
1417
1418 /*
1419 *--------------------------------------------------------------
1420 *
1421 * AdjustForSticky --
1422 *
1423 * This procedure adjusts the size of a slave in its cavity based
1424 * on its "sticky" flags.
1425 *
1426 * Results:
1427 * The input x, y, width, and height are changed to represent the
1428 * desired coordinates of the slave.
1429 *
1430 * Side effects:
1431 * None.
1432 *
1433 *--------------------------------------------------------------
1434 */
1435
1436 static void
1437 AdjustForSticky(slavePtr, xPtr, yPtr, widthPtr, heightPtr)
1438 Gridder *slavePtr; /* Slave window to arrange in its cavity. */
1439 int *xPtr; /* Pixel location of the left edge of the cavity. */
1440 int *yPtr; /* Pixel location of the top edge of the cavity. */
1441 int *widthPtr; /* Width of the cavity (in pixels). */
1442 int *heightPtr; /* Height of the cavity (in pixels). */
1443 {
1444 int diffx=0; /* Cavity width - slave width. */
1445 int diffy=0; /* Cavity hight - slave height. */
1446 int sticky = slavePtr->sticky;
1447
1448 *xPtr += slavePtr->padLeft;
1449 *widthPtr -= slavePtr->padX;
1450 *yPtr += slavePtr->padTop;
1451 *heightPtr -= slavePtr->padY;
1452
1453 if (*widthPtr > (Tk_ReqWidth(slavePtr->tkwin) + slavePtr->iPadX)) {
1454 diffx = *widthPtr - (Tk_ReqWidth(slavePtr->tkwin) + slavePtr->iPadX);
1455 *widthPtr = Tk_ReqWidth(slavePtr->tkwin) + slavePtr->iPadX;
1456 }
1457
1458 if (*heightPtr > (Tk_ReqHeight(slavePtr->tkwin) + slavePtr->iPadY)) {
1459 diffy = *heightPtr - (Tk_ReqHeight(slavePtr->tkwin) + slavePtr->iPadY);
1460 *heightPtr = Tk_ReqHeight(slavePtr->tkwin) + slavePtr->iPadY;
1461 }
1462
1463 if (sticky&STICK_EAST && sticky&STICK_WEST) {
1464 *widthPtr += diffx;
1465 }
1466 if (sticky&STICK_NORTH && sticky&STICK_SOUTH) {
1467 *heightPtr += diffy;
1468 }
1469 if (!(sticky&STICK_WEST)) {
1470 *xPtr += (sticky&STICK_EAST) ? diffx : diffx/2;
1471 }
1472 if (!(sticky&STICK_NORTH)) {
1473 *yPtr += (sticky&STICK_SOUTH) ? diffy : diffy/2;
1474 }
1475 }
1476
1477 /*
1478 *--------------------------------------------------------------
1479 *
1480 * ArrangeGrid --
1481 *
1482 * This procedure is invoked (using the Tcl_DoWhenIdle
1483 * mechanism) to re-layout a set of windows managed by
1484 * the grid. It is invoked at idle time so that a
1485 * series of grid requests can be merged into a single
1486 * layout operation.
1487 *
1488 * Results:
1489 * None.
1490 *
1491 * Side effects:
1492 * The slaves of masterPtr may get resized or moved.
1493 *
1494 *--------------------------------------------------------------
1495 */
1496
1497 static void
1498 ArrangeGrid(clientData)
1499 ClientData clientData; /* Structure describing parent whose slaves
1500 * are to be re-layed out. */
1501 {
1502 register Gridder *masterPtr = (Gridder *) clientData;
1503 register Gridder *slavePtr;
1504 GridMaster *slotPtr = masterPtr->masterDataPtr;
1505 int abort;
1506 int width, height; /* requested size of layout, in pixels */
1507 int realWidth, realHeight; /* actual size layout should take-up */
1508
1509 masterPtr->flags &= ~REQUESTED_RELAYOUT;
1510
1511 /*
1512 * If the parent has no slaves anymore, then don't do anything
1513 * at all: just leave the parent's size as-is. Otherwise there is
1514 * no way to "relinquish" control over the parent so another geometry
1515 * manager can take over.
1516 */
1517
1518 if (masterPtr->slavePtr == NULL) {
1519 return;
1520 }
1521
1522 if (masterPtr->masterDataPtr == NULL) {
1523 return;
1524 }
1525
1526 /*
1527 * Abort any nested call to ArrangeGrid for this window, since
1528 * we'll do everything necessary here, and set up so this call
1529 * can be aborted if necessary.
1530 */
1531
1532 if (masterPtr->abortPtr != NULL) {
1533 *masterPtr->abortPtr = 1;
1534 }
1535 masterPtr->abortPtr = &abort;
1536 abort = 0;
1537 Tcl_Preserve((ClientData) masterPtr);
1538
1539 /*
1540 * Call the constraint engine to fill in the row and column offsets.
1541 */
1542
1543 SetGridSize(masterPtr);
1544 width = ResolveConstraints(masterPtr, COLUMN, 0);
1545 height = ResolveConstraints(masterPtr, ROW, 0);
1546 width += Tk_InternalBorderLeft(masterPtr->tkwin) +
1547 Tk_InternalBorderRight(masterPtr->tkwin);
1548 height += Tk_InternalBorderTop(masterPtr->tkwin) +
1549 Tk_InternalBorderBottom(masterPtr->tkwin);
1550
1551 if (width < Tk_MinReqWidth(masterPtr->tkwin)) {
1552 width = Tk_MinReqWidth(masterPtr->tkwin);
1553 }
1554 if (height < Tk_MinReqHeight(masterPtr->tkwin)) {
1555 height = Tk_MinReqHeight(masterPtr->tkwin);
1556 }
1557
1558 if (((width != Tk_ReqWidth(masterPtr->tkwin))
1559 || (height != Tk_ReqHeight(masterPtr->tkwin)))
1560 && !(masterPtr->flags & DONT_PROPAGATE)) {
1561 Tk_GeometryRequest(masterPtr->tkwin, width, height);
1562 if (width>1 && height>1) {
1563 masterPtr->flags |= REQUESTED_RELAYOUT;
1564 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) masterPtr);
1565 }
1566 masterPtr->abortPtr = NULL;
1567 Tcl_Release((ClientData) masterPtr);
1568 return;
1569 }
1570
1571 /*
1572 * If the currently requested layout size doesn't match the parent's
1573 * window size, then adjust the slot offsets according to the
1574 * weights. If all of the weights are zero, center the layout in
1575 * its parent. I haven't decided what to do if the parent is smaller
1576 * than the requested size.
1577 */
1578
1579 realWidth = Tk_Width(masterPtr->tkwin) -
1580 Tk_InternalBorderLeft(masterPtr->tkwin) -
1581 Tk_InternalBorderRight(masterPtr->tkwin);
1582 realHeight = Tk_Height(masterPtr->tkwin) -
1583 Tk_InternalBorderTop(masterPtr->tkwin) -
1584 Tk_InternalBorderBottom(masterPtr->tkwin);
1585 slotPtr->startX = AdjustOffsets(realWidth,
1586 MAX(slotPtr->columnEnd,slotPtr->columnMax), slotPtr->columnPtr);
1587 slotPtr->startY = AdjustOffsets(realHeight,
1588 MAX(slotPtr->rowEnd,slotPtr->rowMax), slotPtr->rowPtr);
1589 slotPtr->startX += Tk_InternalBorderLeft(masterPtr->tkwin);
1590 slotPtr->startY += Tk_InternalBorderTop(masterPtr->tkwin);
1591
1592 /*
1593 * Now adjust the actual size of the slave to its cavity by
1594 * computing the cavity size, and adjusting the widget according
1595 * to its stickyness.
1596 */
1597
1598 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL && !abort;
1599 slavePtr = slavePtr->nextPtr) {
1600 int x, y; /* top left coordinate */
1601 int width, height; /* slot or slave size */
1602 int col = slavePtr->column;
1603 int row = slavePtr->row;
1604
1605 x = (col>0) ? slotPtr->columnPtr[col-1].offset : 0;
1606 y = (row>0) ? slotPtr->rowPtr[row-1].offset : 0;
1607
1608 width = slotPtr->columnPtr[slavePtr->numCols+col-1].offset - x;
1609 height = slotPtr->rowPtr[slavePtr->numRows+row-1].offset - y;
1610
1611 x += slotPtr->startX;
1612 y += slotPtr->startY;
1613
1614 AdjustForSticky(slavePtr, &x, &y, &width, &height);
1615
1616 /*
1617 * Now put the window in the proper spot. (This was taken directly
1618 * from tkPack.c.) If the slave is a child of the master, then
1619 * do this here. Otherwise let Tk_MaintainGeometry do the work.
1620 */
1621
1622 if (masterPtr->tkwin == Tk_Parent(slavePtr->tkwin)) {
1623 if ((width <= 0) || (height <= 0)) {
1624 Tk_UnmapWindow(slavePtr->tkwin);
1625 } else {
1626 if ((x != Tk_X(slavePtr->tkwin))
1627 || (y != Tk_Y(slavePtr->tkwin))
1628 || (width != Tk_Width(slavePtr->tkwin))
1629 || (height != Tk_Height(slavePtr->tkwin))) {
1630 Tk_MoveResizeWindow(slavePtr->tkwin, x, y, width, height);
1631 }
1632 if (abort) {
1633 break;
1634 }
1635
1636 /*
1637 * Don't map the slave if the master isn't mapped: wait
1638 * until the master gets mapped later.
1639 */
1640
1641 if (Tk_IsMapped(masterPtr->tkwin)) {
1642 Tk_MapWindow(slavePtr->tkwin);
1643 }
1644 }
1645 } else {
1646 if ((width <= 0) || (height <= 0)) {
1647 Tk_UnmaintainGeometry(slavePtr->tkwin, masterPtr->tkwin);
1648 Tk_UnmapWindow(slavePtr->tkwin);
1649 } else {
1650 Tk_MaintainGeometry(slavePtr->tkwin, masterPtr->tkwin,
1651 x, y, width, height);
1652 }
1653 }
1654 }
1655
1656 masterPtr->abortPtr = NULL;
1657 Tcl_Release((ClientData) masterPtr);
1658 }
1659
1660 /*
1661 *--------------------------------------------------------------
1662 *
1663 * ResolveConstraints --
1664 *
1665 * Resolve all of the column and row boundaries. Most of
1666 * the calculations are identical for rows and columns, so this procedure
1667 * is called twice, once for rows, and again for columns.
1668 *
1669 * Results:
1670 * The offset (in pixels) from the left/top edge of this layout is
1671 * returned.
1672 *
1673 * Side effects:
1674 * The slot offsets are copied into the SlotInfo structure for the
1675 * geometry master.
1676 *
1677 *--------------------------------------------------------------
1678 */
1679
1680 static int
1681 ResolveConstraints(masterPtr, slotType, maxOffset)
1682 Gridder *masterPtr; /* The geometry master for this grid. */
1683 int slotType; /* Either ROW or COLUMN. */
1684 int maxOffset; /* The actual maximum size of this layout
1685 * in pixels, or 0 (not currently used). */
1686 {
1687 register SlotInfo *slotPtr; /* Pointer to row/col constraints. */
1688 register Gridder *slavePtr; /* List of slave windows in this grid. */
1689 int constraintCount; /* Count of rows or columns that have
1690 * constraints. */
1691 int slotCount; /* Last occupied row or column. */
1692 int gridCount; /* The larger of slotCount and constraintCount.
1693 */
1694 GridLayout *layoutPtr; /* Temporary layout structure. */
1695 int requiredSize; /* The natural size of the grid (pixels).
1696 * This is the minimum size needed to
1697 * accomodate all of the slaves at their
1698 * requested sizes. */
1699 int offset; /* The pixel offset of the right edge of the
1700 * current slot from the beginning of the
1701 * layout. */
1702 int slot; /* The current slot. */
1703 int start; /* The first slot of a contiguous set whose
1704 * constraints are not yet fully resolved. */
1705 int end; /* The Last slot of a contiguous set whose
1706 * constraints are not yet fully resolved. */
1707 UniformGroup uniformPre[UNIFORM_PREALLOC];
1708 /* Pre-allocated space for uniform groups. */
1709 UniformGroup *uniformGroupPtr;
1710 /* Uniform groups data. */
1711 int uniformGroups; /* Number of currently used uniform groups. */
1712 int uniformGroupsAlloced; /* Size of allocated space for uniform groups.
1713 */
1714 int weight, minSize;
1715
1716 /*
1717 * For typical sized tables, we'll use stack space for the layout data
1718 * to avoid the overhead of a malloc and free for every layout.
1719 */
1720
1721 GridLayout layoutData[TYPICAL_SIZE + 2];
1722
1723 if (slotType == COLUMN) {
1724 constraintCount = masterPtr->masterDataPtr->columnMax;
1725 slotCount = masterPtr->masterDataPtr->columnEnd;
1726 slotPtr = masterPtr->masterDataPtr->columnPtr;
1727 } else {
1728 constraintCount = masterPtr->masterDataPtr->rowMax;
1729 slotCount = masterPtr->masterDataPtr->rowEnd;
1730 slotPtr = masterPtr->masterDataPtr->rowPtr;
1731 }
1732
1733 /*
1734 * Make sure there is enough memory for the layout.
1735 */
1736
1737 gridCount = MAX(constraintCount,slotCount);
1738 if (gridCount >= TYPICAL_SIZE) {
1739 layoutPtr = (GridLayout *) ckalloc(sizeof(GridLayout) * (1+gridCount));
1740 } else {
1741 layoutPtr = layoutData;
1742 }
1743
1744 /*
1745 * Allocate an extra layout slot to represent the left/top edge of
1746 * the 0th slot to make it easier to calculate slot widths from
1747 * offsets without special case code.
1748 * Initialize the "dummy" slot to the left/top of the table.
1749 * This slot avoids special casing the first slot.
1750 */
1751
1752 layoutPtr->minOffset = 0;
1753 layoutPtr->maxOffset = 0;
1754 layoutPtr++;
1755
1756 /*
1757 * Step 1.
1758 * Copy the slot constraints into the layout structure,
1759 * and initialize the rest of the fields.
1760 */
1761
1762 for (slot=0; slot < constraintCount; slot++) {
1763 layoutPtr[slot].minSize = slotPtr[slot].minSize;
1764 layoutPtr[slot].weight = slotPtr[slot].weight;
1765 layoutPtr[slot].uniform = slotPtr[slot].uniform;
1766 layoutPtr[slot].pad = slotPtr[slot].pad;
1767 layoutPtr[slot].binNextPtr = NULL;
1768 }
1769 for(;slot<gridCount;slot++) {
1770 layoutPtr[slot].minSize = 0;
1771 layoutPtr[slot].weight = 0;
1772 layoutPtr[slot].uniform = NULL;
1773 layoutPtr[slot].pad = 0;
1774 layoutPtr[slot].binNextPtr = NULL;
1775 }
1776
1777 /*
1778 * Step 2.
1779 * Slaves with a span of 1 are used to determine the minimum size of
1780 * each slot. Slaves whose span is two or more slots don't
1781 * contribute to the minimum size of each slot directly, but can cause
1782 * slots to grow if their size exceeds the the sizes of the slots they
1783 * span.
1784 *
1785 * Bin all slaves whose spans are > 1 by their right edges. This
1786 * allows the computation on minimum and maximum possible layout
1787 * sizes at each slot boundary, without the need to re-sort the slaves.
1788 */
1789
1790 switch (slotType) {
1791 case COLUMN:
1792 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
1793 slavePtr = slavePtr->nextPtr) {
1794 int rightEdge = slavePtr->column + slavePtr->numCols - 1;
1795 slavePtr->size = Tk_ReqWidth(slavePtr->tkwin) +
1796 slavePtr->padX + slavePtr->iPadX + slavePtr->doubleBw;
1797 if (slavePtr->numCols > 1) {
1798 slavePtr->binNextPtr = layoutPtr[rightEdge].binNextPtr;
1799 layoutPtr[rightEdge].binNextPtr = slavePtr;
1800 } else {
1801 int size = slavePtr->size + layoutPtr[rightEdge].pad;
1802 if (size > layoutPtr[rightEdge].minSize) {
1803 layoutPtr[rightEdge].minSize = size;
1804 }
1805 }
1806 }
1807 break;
1808 case ROW:
1809 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
1810 slavePtr = slavePtr->nextPtr) {
1811 int rightEdge = slavePtr->row + slavePtr->numRows - 1;
1812 slavePtr->size = Tk_ReqHeight(slavePtr->tkwin) +
1813 slavePtr->padY + slavePtr->iPadY + slavePtr->doubleBw;
1814 if (slavePtr->numRows > 1) {
1815 slavePtr->binNextPtr = layoutPtr[rightEdge].binNextPtr;
1816 layoutPtr[rightEdge].binNextPtr = slavePtr;
1817 } else {
1818 int size = slavePtr->size + layoutPtr[rightEdge].pad;
1819 if (size > layoutPtr[rightEdge].minSize) {
1820 layoutPtr[rightEdge].minSize = size;
1821 }
1822 }
1823 }
1824 break;
1825 }
1826
1827 /*
1828 * Step 2b.
1829 * Consider demands on uniform sizes.
1830 */
1831
1832 uniformGroupPtr = uniformPre;
1833 uniformGroupsAlloced = UNIFORM_PREALLOC;
1834 uniformGroups = 0;
1835
1836 for (slot = 0; slot < gridCount; slot++) {
1837 if (layoutPtr[slot].uniform != NULL) {
1838 for (start = 0; start < uniformGroups; start++) {
1839 if (uniformGroupPtr[start].group == layoutPtr[slot].uniform) {
1840 break;
1841 }
1842 }
1843 if (start >= uniformGroups) {
1844 /*
1845 * Have not seen that group before, set up data for it.
1846 */
1847
1848 if (uniformGroups >= uniformGroupsAlloced) {
1849 /*
1850 * We need to allocate more space.
1851 */
1852
1853 size_t oldSize = uniformGroupsAlloced
1854 * sizeof(UniformGroup);
1855 size_t newSize = (uniformGroupsAlloced + UNIFORM_PREALLOC)
1856 * sizeof(UniformGroup);
1857 UniformGroup *new = (UniformGroup *) ckalloc(newSize);
1858 UniformGroup *old = uniformGroupPtr;
1859 memcpy((VOID *) new, (VOID *) old, oldSize);
1860 if (old != uniformPre) {
1861 ckfree((char *) old);
1862 }
1863 uniformGroupPtr = new;
1864 uniformGroupsAlloced += UNIFORM_PREALLOC;
1865 }
1866 uniformGroups++;
1867 uniformGroupPtr[start].group = layoutPtr[slot].uniform;
1868 uniformGroupPtr[start].minSize = 0;
1869 }
1870 weight = layoutPtr[slot].weight;
1871 weight = weight > 0 ? weight : 1;
1872 minSize = (layoutPtr[slot].minSize + weight - 1) / weight;
1873 if (minSize > uniformGroupPtr[start].minSize) {
1874 uniformGroupPtr[start].minSize = minSize;
1875 }
1876 }
1877 }
1878
1879 /*
1880 * Data has been gathered about uniform groups. Now relayout accordingly.
1881 */
1882
1883 if (uniformGroups > 0) {
1884 for (slot = 0; slot < gridCount; slot++) {
1885 if (layoutPtr[slot].uniform != NULL) {
1886 for (start = 0; start < uniformGroups; start++) {
1887 if (uniformGroupPtr[start].group ==
1888 layoutPtr[slot].uniform) {
1889 weight = layoutPtr[slot].weight;
1890 weight = weight > 0 ? weight : 1;
1891 layoutPtr[slot].minSize =
1892 uniformGroupPtr[start].minSize * weight;
1893 break;
1894 }
1895 }
1896 }
1897 }
1898 }
1899
1900 if (uniformGroupPtr != uniformPre) {
1901 ckfree((char *) uniformGroupPtr);
1902 }
1903
1904 /*
1905 * Step 3.
1906 * Determine the minimum slot offsets going from left to right
1907 * that would fit all of the slaves. This determines the minimum
1908 */
1909
1910 for (offset=slot=0; slot < gridCount; slot++) {
1911 layoutPtr[slot].minOffset = layoutPtr[slot].minSize + offset;
1912 for (slavePtr = layoutPtr[slot].binNextPtr; slavePtr != NULL;
1913 slavePtr = slavePtr->binNextPtr) {
1914 int span = (slotType == COLUMN) ? slavePtr->numCols : slavePtr->numRows;
1915 int required = slavePtr->size + layoutPtr[slot - span].minOffset;
1916 if (required > layoutPtr[slot].minOffset) {
1917 layoutPtr[slot].minOffset = required;
1918 }
1919 }
1920 offset = layoutPtr[slot].minOffset;
1921 }
1922
1923 /*
1924 * At this point, we know the minimum required size of the entire layout.
1925 * It might be prudent to stop here if our "master" will resize itself
1926 * to this size.
1927 */
1928
1929 requiredSize = offset;
1930 if (maxOffset > offset) {
1931 offset=maxOffset;
1932 }
1933
1934 /*
1935 * Step 4.
1936 * Determine the minimum slot offsets going from right to left,
1937 * bounding the pixel range of each slot boundary.
1938 * Pre-fill all of the right offsets with the actual size of the table;
1939 * they will be reduced as required.
1940 */
1941
1942 for (slot=0; slot < gridCount; slot++) {
1943 layoutPtr[slot].maxOffset = offset;
1944 }
1945 for (slot=gridCount-1; slot > 0;) {
1946 for (slavePtr = layoutPtr[slot].binNextPtr; slavePtr != NULL;
1947 slavePtr = slavePtr->binNextPtr) {
1948 int span = (slotType == COLUMN) ? slavePtr->numCols : slavePtr->numRows;
1949 int require = offset - slavePtr->size;
1950 int startSlot = slot - span;
1951 if (startSlot >=0 && require < layoutPtr[startSlot].maxOffset) {
1952 layoutPtr[startSlot].maxOffset = require;
1953 }
1954 }
1955 offset -= layoutPtr[slot].minSize;
1956 slot--;
1957 if (layoutPtr[slot].maxOffset < offset) {
1958 offset = layoutPtr[slot].maxOffset;
1959 } else {
1960 layoutPtr[slot].maxOffset = offset;
1961 }
1962 }
1963
1964 /*
1965 * Step 5.
1966 * At this point, each slot boundary has a range of values that
1967 * will satisfy the overall layout size.
1968 * Make repeated passes over the layout structure looking for
1969 * spans of slot boundaries where the minOffsets are less than
1970 * the maxOffsets, and adjust the offsets according to the slot
1971 * weights. At each pass, at least one slot boundary will have
1972 * its range of possible values fixed at a single value.
1973 */
1974
1975 for (start=0; start < gridCount;) {
1976 int totalWeight = 0; /* Sum of the weights for all of the
1977 * slots in this span. */
1978 int need = 0; /* The minimum space needed to layout
1979 * this span. */
1980 int have; /* The actual amount of space that will
1981 * be taken up by this span. */
1982 int weight; /* Cumulative weights of the columns in
1983 * this span. */
1984 int noWeights = 0; /* True if the span has no weights. */
1985
1986 /*
1987 * Find a span by identifying ranges of slots whose edges are
1988 * already constrained at fixed offsets, but whose internal
1989 * slot boundaries have a range of possible positions.
1990 */
1991
1992 if (layoutPtr[start].minOffset == layoutPtr[start].maxOffset) {
1993 start++;
1994 continue;
1995 }
1996
1997 for (end=start+1; end<gridCount; end++) {
1998 if (layoutPtr[end].minOffset == layoutPtr[end].maxOffset) {
1999 break;
2000 }
2001 }
2002
2003 /*
2004 * We found a span. Compute the total weight, minumum space required,
2005 * for this span, and the actual amount of space the span should
2006 * use.
2007 */
2008
2009 for (slot=start; slot<=end; slot++) {
2010 totalWeight += layoutPtr[slot].weight;
2011 need += layoutPtr[slot].minSize;
2012 }
2013 have = layoutPtr[end].maxOffset - layoutPtr[start-1].minOffset;
2014
2015 /*
2016 * If all the weights in the span are zero, then distribute the
2017 * extra space evenly.
2018 */
2019
2020 if (totalWeight == 0) {
2021 noWeights++;
2022 totalWeight = end - start + 1;
2023 }
2024
2025 /*
2026 * It might not be possible to give the span all of the space
2027 * available on this pass without violating the size constraints
2028 * of one or more of the internal slot boundaries.
2029 * Determine the maximum amount of space that when added to the
2030 * entire span, would cause a slot boundary to have its possible
2031 * range reduced to one value, and reduce the amount of extra
2032 * space allocated on this pass accordingly.
2033 *
2034 * The calculation is done cumulatively to avoid accumulating
2035 * roundoff errors.
2036 */
2037
2038 for (weight=0,slot=start; slot<end; slot++) {
2039 int diff = layoutPtr[slot].maxOffset - layoutPtr[slot].minOffset;
2040 weight += noWeights ? 1 : layoutPtr[slot].weight;
2041 if ((noWeights || layoutPtr[slot].weight>0) &&
2042 (diff*totalWeight/weight) < (have-need)) {
2043 have = diff * totalWeight / weight + need;
2044 }
2045 }
2046
2047 /*
2048 * Now distribute the extra space among the slots by
2049 * adjusting the minSizes and minOffsets.
2050 */
2051
2052 for (weight=0,slot=start; slot<end; slot++) {
2053 weight += noWeights ? 1 : layoutPtr[slot].weight;
2054 layoutPtr[slot].minOffset +=
2055 (int)((double) (have-need) * weight/totalWeight + 0.5);
2056 layoutPtr[slot].minSize = layoutPtr[slot].minOffset
2057 - layoutPtr[slot-1].minOffset;
2058 }
2059 layoutPtr[slot].minSize = layoutPtr[slot].minOffset
2060 - layoutPtr[slot-1].minOffset;
2061
2062 /*
2063 * Having pushed the top/left boundaries of the slots to
2064 * take up extra space, the bottom/right space is recalculated
2065 * to propagate the new space allocation.
2066 */
2067
2068 for (slot=end; slot > start; slot--) {
2069 layoutPtr[slot-1].maxOffset =
2070 layoutPtr[slot].maxOffset-layoutPtr[slot].minSize;
2071 }
2072 }
2073
2074
2075 /*
2076 * Step 6.
2077 * All of the space has been apportioned; copy the
2078 * layout information back into the master.
2079 */
2080
2081 for (slot=0; slot < gridCount; slot++) {
2082 slotPtr[slot].offset = layoutPtr[slot].minOffset;
2083 }
2084
2085 --layoutPtr;
2086 if (layoutPtr != layoutData) {
2087 ckfree((char *)layoutPtr);
2088 }
2089 return requiredSize;
2090 }
2091
2092 /*
2093 *--------------------------------------------------------------
2094 *
2095 * GetGrid --
2096 *
2097 * This internal procedure is used to locate a Grid
2098 * structure for a given window, creating one if one
2099 * doesn't exist already.
2100 *
2101 * Results:
2102 * The return value is a pointer to the Grid structure
2103 * corresponding to tkwin.
2104 *
2105 * Side effects:
2106 * A new grid structure may be created. If so, then
2107 * a callback is set up to clean things up when the
2108 * window is deleted.
2109 *
2110 *--------------------------------------------------------------
2111 */
2112
2113 static Gridder *
2114 GetGrid(tkwin)
2115 Tk_Window tkwin; /* Token for window for which
2116 * grid structure is desired. */
2117 {
2118 register Gridder *gridPtr;
2119 Tcl_HashEntry *hPtr;
2120 int new;
2121 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
2122
2123 if (!dispPtr->gridInit) {
2124 Tcl_InitHashTable(&dispPtr->gridHashTable, TCL_ONE_WORD_KEYS);
2125 dispPtr->gridInit = 1;
2126 }
2127
2128 /*
2129 * See if there's already grid for this window. If not,
2130 * then create a new one.
2131 */
2132
2133 hPtr = Tcl_CreateHashEntry(&dispPtr->gridHashTable, (char *) tkwin, &new);
2134 if (!new) {
2135 return (Gridder *) Tcl_GetHashValue(hPtr);
2136 }
2137 gridPtr = (Gridder *) ckalloc(sizeof(Gridder));
2138 gridPtr->tkwin = tkwin;
2139 gridPtr->masterPtr = NULL;
2140 gridPtr->masterDataPtr = NULL;
2141 gridPtr->nextPtr = NULL;
2142 gridPtr->slavePtr = NULL;
2143 gridPtr->binNextPtr = NULL;
2144
2145 gridPtr->column = gridPtr->row = -1;
2146 gridPtr->numCols = 1;
2147 gridPtr->numRows = 1;
2148
2149 gridPtr->padX = gridPtr->padY = 0;
2150 gridPtr->padLeft = gridPtr->padTop = 0;
2151 gridPtr->iPadX = gridPtr->iPadY = 0;
2152 gridPtr->doubleBw = 2*Tk_Changes(tkwin)->border_width;
2153 gridPtr->abortPtr = NULL;
2154 gridPtr->flags = 0;
2155 gridPtr->sticky = 0;
2156 gridPtr->size = 0;
2157 gridPtr->masterDataPtr = NULL;
2158 Tcl_SetHashValue(hPtr, gridPtr);
2159 Tk_CreateEventHandler(tkwin, StructureNotifyMask,
2160 GridStructureProc, (ClientData) gridPtr);
2161 return gridPtr;
2162 }
2163
2164 /*
2165 *--------------------------------------------------------------
2166 *
2167 * SetGridSize --
2168 *
2169 * This internal procedure sets the size of the grid occupied
2170 * by slaves.
2171 *
2172 * Results:
2173 * none
2174 *
2175 * Side effects:
2176 * The width and height arguments are filled in the master data structure.
2177 * Additional space is allocated for the constraints to accomodate
2178 * the offsets.
2179 *
2180 *--------------------------------------------------------------
2181 */
2182
2183 static void
2184 SetGridSize(masterPtr)
2185 Gridder *masterPtr; /* The geometry master for this grid. */
2186 {
2187 register Gridder *slavePtr; /* Current slave window. */
2188 int maxX = 0, maxY = 0;
2189
2190 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
2191 slavePtr = slavePtr->nextPtr) {
2192 maxX = MAX(maxX,slavePtr->numCols + slavePtr->column);
2193 maxY = MAX(maxY,slavePtr->numRows + slavePtr->row);
2194 }
2195 masterPtr->masterDataPtr->columnEnd = maxX;
2196 masterPtr->masterDataPtr->rowEnd = maxY;
2197 CheckSlotData(masterPtr, maxX, COLUMN, CHECK_SPACE);
2198 CheckSlotData(masterPtr, maxY, ROW, CHECK_SPACE);
2199 }
2200
2201 /*
2202 *--------------------------------------------------------------
2203 *
2204 * CheckSlotData --
2205 *
2206 * This internal procedure is used to manage the storage for
2207 * row and column (slot) constraints.
2208 *
2209 * Results:
2210 * TRUE if the index is OK, False otherwise.
2211 *
2212 * Side effects:
2213 * A new master grid structure may be created. If so, then
2214 * it is initialized. In addition, additional storage for
2215 * a row or column constraints may be allocated, and the constraint
2216 * maximums are adjusted.
2217 *
2218 *--------------------------------------------------------------
2219 */
2220
2221 static int
2222 CheckSlotData(masterPtr, slot, slotType, checkOnly)
2223 Gridder *masterPtr; /* the geometry master for this grid */
2224 int slot; /* which slot to look at */
2225 int slotType; /* ROW or COLUMN */
2226 int checkOnly; /* don't allocate new space if true */
2227 {
2228 int numSlot; /* number of slots already allocated (Space) */
2229 int end; /* last used constraint */
2230
2231 /*
2232 * If slot is out of bounds, return immediately.
2233 */
2234
2235 if (slot < 0 || slot >= MAX_ELEMENT) {
2236 return TCL_ERROR;
2237 }
2238
2239 if ((checkOnly == CHECK_ONLY) && (masterPtr->masterDataPtr == NULL)) {
2240 return TCL_ERROR;
2241 }
2242
2243 /*
2244 * If we need to allocate more space, allocate a little extra to avoid
2245 * repeated re-alloc's for large tables. We need enough space to
2246 * hold all of the offsets as well.
2247 */
2248
2249 InitMasterData(masterPtr);
2250 end = (slotType == ROW) ? masterPtr->masterDataPtr->rowMax :
2251 masterPtr->masterDataPtr->columnMax;
2252 if (checkOnly == CHECK_ONLY) {
2253 return (end < slot) ? TCL_ERROR : TCL_OK;
2254 } else {
2255 numSlot = (slotType == ROW) ? masterPtr->masterDataPtr->rowSpace
2256 : masterPtr->masterDataPtr->columnSpace;
2257 if (slot >= numSlot) {
2258 int newNumSlot = slot + PREALLOC ;
2259 size_t oldSize = numSlot * sizeof(SlotInfo) ;
2260 size_t newSize = newNumSlot * sizeof(SlotInfo) ;
2261 SlotInfo *new = (SlotInfo *) ckalloc(newSize);
2262 SlotInfo *old = (slotType == ROW) ?
2263 masterPtr->masterDataPtr->rowPtr :
2264 masterPtr->masterDataPtr->columnPtr;
2265 memcpy((VOID *) new, (VOID *) old, oldSize );
2266 memset((VOID *) (new+numSlot), 0, newSize - oldSize );
2267 ckfree((char *) old);
2268 if (slotType == ROW) {
2269 masterPtr->masterDataPtr->rowPtr = new ;
2270 masterPtr->masterDataPtr->rowSpace = newNumSlot ;
2271 } else {
2272 masterPtr->masterDataPtr->columnPtr = new;
2273 masterPtr->masterDataPtr->columnSpace = newNumSlot ;
2274 }
2275 }
2276 if (slot >= end && checkOnly != CHECK_SPACE) {
2277 if (slotType == ROW) {
2278 masterPtr->masterDataPtr->rowMax = slot+1;
2279 } else {
2280 masterPtr->masterDataPtr->columnMax = slot+1;
2281 }
2282 }
2283 return TCL_OK;
2284 }
2285 }
2286
2287 /*
2288 *--------------------------------------------------------------
2289 *
2290 * InitMasterData --
2291 *
2292 * This internal procedure is used to allocate and initialize
2293 * the data for a geometry master, if the data
2294 * doesn't exist already.
2295 *
2296 * Results:
2297 * none
2298 *
2299 * Side effects:
2300 * A new master grid structure may be created. If so, then
2301 * it is initialized.
2302 *
2303 *--------------------------------------------------------------
2304 */
2305
2306 static void
2307 InitMasterData(masterPtr)
2308 Gridder *masterPtr;
2309 {
2310 size_t size;
2311 if (masterPtr->masterDataPtr == NULL) {
2312 GridMaster *gridPtr = masterPtr->masterDataPtr =
2313 (GridMaster *) ckalloc(sizeof(GridMaster));
2314 size = sizeof(SlotInfo) * TYPICAL_SIZE;
2315
2316 gridPtr->columnEnd = 0;
2317 gridPtr->columnMax = 0;
2318 gridPtr->columnPtr = (SlotInfo *) ckalloc(size);
2319 gridPtr->columnSpace = TYPICAL_SIZE;
2320 gridPtr->rowEnd = 0;
2321 gridPtr->rowMax = 0;
2322 gridPtr->rowPtr = (SlotInfo *) ckalloc(size);
2323 gridPtr->rowSpace = TYPICAL_SIZE;
2324 gridPtr->startX = 0;
2325 gridPtr->startY = 0;
2326
2327 memset((VOID *) gridPtr->columnPtr, 0, size);
2328 memset((VOID *) gridPtr->rowPtr, 0, size);
2329 }
2330 }
2331
2332 /*
2333 *----------------------------------------------------------------------
2334 *
2335 * Unlink --
2336 *
2337 * Remove a grid from its parent's list of slaves.
2338 *
2339 * Results:
2340 * None.
2341 *
2342 * Side effects:
2343 * The parent will be scheduled for re-arranging, and the size of the
2344 * grid will be adjusted accordingly
2345 *
2346 *----------------------------------------------------------------------
2347 */
2348
2349 static void
2350 Unlink(slavePtr)
2351 register Gridder *slavePtr; /* Window to unlink. */
2352 {
2353 register Gridder *masterPtr, *slavePtr2;
2354
2355 masterPtr = slavePtr->masterPtr;
2356 if (masterPtr == NULL) {
2357 return;
2358 }
2359
2360 if (masterPtr->slavePtr == slavePtr) {
2361 masterPtr->slavePtr = slavePtr->nextPtr;
2362 } else {
2363 for (slavePtr2 = masterPtr->slavePtr; ; slavePtr2 = slavePtr2->nextPtr) {
2364 if (slavePtr2 == NULL) {
2365 panic("Unlink couldn't find previous window");
2366 }
2367 if (slavePtr2->nextPtr == slavePtr) {
2368 slavePtr2->nextPtr = slavePtr->nextPtr;
2369 break;
2370 }
2371 }
2372 }
2373 if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
2374 masterPtr->flags |= REQUESTED_RELAYOUT;
2375 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) masterPtr);
2376 }
2377 if (masterPtr->abortPtr != NULL) {
2378 *masterPtr->abortPtr = 1;
2379 }
2380
2381 SetGridSize(slavePtr->masterPtr);
2382 slavePtr->masterPtr = NULL;
2383 }
2384
2385 /*
2386 *----------------------------------------------------------------------
2387 *
2388 * DestroyGrid --
2389 *
2390 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
2391 * to clean up the internal structure of a grid at a safe time
2392 * (when no-one is using it anymore). Cleaning up the grid involves
2393 * freeing the main structure for all windows. and the master structure
2394 * for geometry managers.
2395 *
2396 * Results:
2397 * None.
2398 *
2399 * Side effects:
2400 * Everything associated with the grid is freed up.
2401 *
2402 *----------------------------------------------------------------------
2403 */
2404
2405 static void
2406 DestroyGrid(memPtr)
2407 char *memPtr; /* Info about window that is now dead. */
2408 {
2409 register Gridder *gridPtr = (Gridder *) memPtr;
2410
2411 if (gridPtr->masterDataPtr != NULL) {
2412 if (gridPtr->masterDataPtr->rowPtr != NULL) {
2413 ckfree((char *) gridPtr->masterDataPtr -> rowPtr);
2414 }
2415 if (gridPtr->masterDataPtr->columnPtr != NULL) {
2416 ckfree((char *) gridPtr->masterDataPtr -> columnPtr);
2417 }
2418 ckfree((char *) gridPtr->masterDataPtr);
2419 }
2420 ckfree((char *) gridPtr);
2421 }
2422
2423 /*
2424 *----------------------------------------------------------------------
2425 *
2426 * GridStructureProc --
2427 *
2428 * This procedure is invoked by the Tk event dispatcher in response
2429 * to StructureNotify events.
2430 *
2431 * Results:
2432 * None.
2433 *
2434 * Side effects:
2435 * If a window was just deleted, clean up all its grid-related
2436 * information. If it was just resized, re-configure its slaves, if
2437 * any.
2438 *
2439 *----------------------------------------------------------------------
2440 */
2441
2442 static void
2443 GridStructureProc(clientData, eventPtr)
2444 ClientData clientData; /* Our information about window
2445 * referred to by eventPtr. */
2446 XEvent *eventPtr; /* Describes what just happened. */
2447 {
2448 register Gridder *gridPtr = (Gridder *) clientData;
2449 TkDisplay *dispPtr = ((TkWindow *) gridPtr->tkwin)->dispPtr;
2450
2451 if (eventPtr->type == ConfigureNotify) {
2452 if (!(gridPtr->flags & REQUESTED_RELAYOUT)) {
2453 gridPtr->flags |= REQUESTED_RELAYOUT;
2454 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) gridPtr);
2455 }
2456 if (gridPtr->doubleBw != 2*Tk_Changes(gridPtr->tkwin)->border_width) {
2457 if ((gridPtr->masterPtr != NULL) &&
2458 !(gridPtr->masterPtr->flags & REQUESTED_RELAYOUT)) {
2459 gridPtr->doubleBw = 2*Tk_Changes(gridPtr->tkwin)->border_width;
2460 gridPtr->masterPtr->flags |= REQUESTED_RELAYOUT;
2461 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) gridPtr->masterPtr);
2462 }
2463 }
2464 } else if (eventPtr->type == DestroyNotify) {
2465 register Gridder *gridPtr2, *nextPtr;
2466
2467 if (gridPtr->masterPtr != NULL) {
2468 Unlink(gridPtr);
2469 }
2470 for (gridPtr2 = gridPtr->slavePtr; gridPtr2 != NULL;
2471 gridPtr2 = nextPtr) {
2472 Tk_UnmapWindow(gridPtr2->tkwin);
2473 gridPtr2->masterPtr = NULL;
2474 nextPtr = gridPtr2->nextPtr;
2475 gridPtr2->nextPtr = NULL;
2476 }
2477 Tcl_DeleteHashEntry(Tcl_FindHashEntry(&dispPtr->gridHashTable,
2478 (char *) gridPtr->tkwin));
2479 if (gridPtr->flags & REQUESTED_RELAYOUT) {
2480 Tcl_CancelIdleCall(ArrangeGrid, (ClientData) gridPtr);
2481 }
2482 gridPtr->tkwin = NULL;
2483 Tcl_EventuallyFree((ClientData) gridPtr, DestroyGrid);
2484 } else if (eventPtr->type == MapNotify) {
2485 if (!(gridPtr->flags & REQUESTED_RELAYOUT)) {
2486 gridPtr->flags |= REQUESTED_RELAYOUT;
2487 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) gridPtr);
2488 }
2489 } else if (eventPtr->type == UnmapNotify) {
2490 register Gridder *gridPtr2;
2491
2492 for (gridPtr2 = gridPtr->slavePtr; gridPtr2 != NULL;
2493 gridPtr2 = gridPtr2->nextPtr) {
2494 Tk_UnmapWindow(gridPtr2->tkwin);
2495 }
2496 }
2497 }
2498
2499 /*
2500 *----------------------------------------------------------------------
2501 *
2502 * ConfigureSlaves --
2503 *
2504 * This implements the guts of the "grid configure" command. Given
2505 * a list of slaves and configuration options, it arranges for the
2506 * grid to manage the slaves and sets the specified options.
2507 * arguments consist of windows or window shortcuts followed by
2508 * "-option value" pairs.
2509 *
2510 * Results:
2511 * TCL_OK is returned if all went well. Otherwise, TCL_ERROR is
2512 * returned and the interp's result is set to contain an error message.
2513 *
2514 * Side effects:
2515 * Slave windows get taken over by the grid.
2516 *
2517 *----------------------------------------------------------------------
2518 */
2519
2520 static int
2521 ConfigureSlaves(interp, tkwin, objc, objv)
2522 Tcl_Interp *interp; /* Interpreter for error reporting. */
2523 Tk_Window tkwin; /* Any window in application containing
2524 * slaves. Used to look up slave names. */
2525 int objc; /* Number of elements in argv. */
2526 Tcl_Obj *CONST objv[]; /* Argument objects: contains one or more
2527 * window names followed by any number
2528 * of "option value" pairs. Caller must
2529 * make sure that there is at least one
2530 * window name. */
2531 {
2532 Gridder *masterPtr;
2533 Gridder *slavePtr;
2534 Tk_Window other, slave, parent, ancestor;
2535 int i, j, tmp;
2536 int length;
2537 int numWindows;
2538 int width;
2539 int defaultColumn = 0; /* default column number */
2540 int defaultColumnSpan = 1; /* default number of columns */
2541 char *lastWindow; /* use this window to base current
2542 * Row/col on */
2543 int numSkip; /* number of 'x' found */
2544 static CONST char *optionStrings[] = {
2545 "-column", "-columnspan", "-in", "-ipadx", "-ipady",
2546 "-padx", "-pady", "-row", "-rowspan", "-sticky",
2547 (char *) NULL };
2548 enum options {
2549 CONF_COLUMN, CONF_COLUMNSPAN, CONF_IN, CONF_IPADX, CONF_IPADY,
2550 CONF_PADX, CONF_PADY, CONF_ROW, CONF_ROWSPAN, CONF_STICKY };
2551 int index;
2552 char *string;
2553 char firstChar, prevChar;
2554
2555 /*
2556 * Count the number of windows, or window short-cuts.
2557 */
2558
2559 firstChar = 0;
2560 for (numWindows = i = 0; i < objc; i++) {
2561 prevChar = firstChar;
2562 string = Tcl_GetStringFromObj(objv[i], (int *) &length);
2563 firstChar = string[0];
2564
2565 if (firstChar == '.') {
2566 numWindows++;
2567 continue;
2568 }
2569 if (length > 1 && i == 0) {
2570 Tcl_AppendResult(interp, "bad argument \"", string,
2571 "\": must be name of window", (char *) NULL);
2572 return TCL_ERROR;
2573 }
2574 if (length > 1 && firstChar == '-') {
2575 break;
2576 }
2577 if (length > 1) {
2578 Tcl_AppendResult(interp, "unexpected parameter, \"",
2579 string, "\", in configure list. ",
2580 "Should be window name or option", (char *) NULL);
2581 return TCL_ERROR;
2582 }
2583
2584 if ((firstChar == REL_HORIZ) && ((numWindows == 0) ||
2585 (prevChar == REL_SKIP) || (prevChar == REL_VERT))) {
2586 Tcl_AppendResult(interp,
2587 "Must specify window before shortcut '-'.",
2588 (char *) NULL);
2589 return TCL_ERROR;
2590 }
2591
2592 if ((firstChar == REL_VERT) || (firstChar == REL_SKIP)
2593 || (firstChar == REL_HORIZ)) {
2594 continue;
2595 }
2596
2597 Tcl_AppendResult(interp, "invalid window shortcut, \"",
2598 string, "\" should be '-', 'x', or '^'", (char *) NULL);
2599 return TCL_ERROR;
2600 }
2601 numWindows = i;
2602
2603 if ((objc - numWindows) & 1) {
2604 Tcl_AppendResult(interp, "extra option or",
2605 " option with no value", (char *) NULL);
2606 return TCL_ERROR;
2607 }
2608
2609 /*
2610 * Iterate over all of the slave windows and short-cuts, parsing
2611 * options for each slave. It's a bit wasteful to re-parse the
2612 * options for each slave, but things get too messy if we try to
2613 * parse the arguments just once at the beginning. For example,
2614 * if a slave already is managed we want to just change a few
2615 * existing values without resetting everything. If there are
2616 * multiple windows, the -in option only gets processed for the
2617 * first window.
2618 */
2619
2620 masterPtr = NULL;
2621 for (j = 0; j < numWindows; j++) {
2622 string = Tcl_GetString(objv[j]);
2623 firstChar = string[0];
2624
2625 /*
2626 * '^' and 'x' cause us to skip a column. '-' is processed
2627 * as part of its preceeding slave.
2628 */
2629
2630 if ((firstChar == REL_VERT) || (firstChar == REL_SKIP)) {
2631 defaultColumn++;
2632 continue;
2633 }
2634 if (firstChar == REL_HORIZ) {
2635 continue;
2636 }
2637
2638 for (defaultColumnSpan = 1; j + defaultColumnSpan < numWindows;
2639 defaultColumnSpan++) {
2640 char *string = Tcl_GetString(objv[j + defaultColumnSpan]);
2641 if (*string != REL_HORIZ) {
2642 break;
2643 }
2644 }
2645
2646 if (TkGetWindowFromObj(interp, tkwin, objv[j], &slave) != TCL_OK) {
2647 return TCL_ERROR;
2648 }
2649
2650 if (Tk_TopWinHierarchy(slave)) {
2651 Tcl_AppendResult(interp, "can't manage \"", Tcl_GetString(objv[j]),
2652 "\": it's a top-level window", (char *) NULL);
2653 return TCL_ERROR;
2654 }
2655 slavePtr = GetGrid(slave);
2656
2657 /*
2658 * The following statement is taken from tkPack.c:
2659 *
2660 * "If the slave isn't currently managed, reset all of its
2661 * configuration information to default values (there could
2662 * be old values left from a previous packer)."
2663 *
2664 * I [D.S.] disagree with this statement. If a slave is disabled (using
2665 * "forget") and then re-enabled, I submit that 90% of the time the
2666 * programmer will want it to retain its old configuration information.
2667 * If the programmer doesn't want this behavior, then the
2668 * defaults can be reestablished by hand, without having to worry
2669 * about keeping track of the old state.
2670 */
2671
2672 for (i = numWindows; i < objc; i += 2) {
2673 if (Tcl_GetIndexFromObj(interp, objv[i], optionStrings, "option", 0,
2674 &index) != TCL_OK) {
2675 return TCL_ERROR;
2676 }
2677 if (index == CONF_COLUMN) {
2678 if (Tcl_GetIntFromObj(interp, objv[i+1], &tmp) != TCL_OK ||
2679 tmp < 0) {
2680 Tcl_ResetResult(interp);
2681 Tcl_AppendResult(interp, "bad column value \"",
2682 Tcl_GetString(objv[i+1]),
2683 "\": must be a non-negative integer", (char *)NULL);
2684 return TCL_ERROR;
2685 }
2686 slavePtr->column = tmp;
2687 } else if (index == CONF_COLUMNSPAN) {
2688 if (Tcl_GetIntFromObj(interp, objv[i+1], &tmp) != TCL_OK ||
2689 tmp <= 0) {
2690 Tcl_ResetResult(interp);
2691 Tcl_AppendResult(interp, "bad columnspan value \"",
2692 Tcl_GetString(objv[i+1]),
2693 "\": must be a positive integer", (char *)NULL);
2694 return TCL_ERROR;
2695 }
2696 slavePtr->numCols = tmp;
2697 } else if (index == CONF_IN) {
2698 if (TkGetWindowFromObj(interp, tkwin, objv[i+1], &other) !=
2699 TCL_OK) {
2700 return TCL_ERROR;
2701 }
2702 if (other == slave) {
2703 Tcl_SetResult(interp, "Window can't be managed in itself",
2704 TCL_STATIC);
2705 return TCL_ERROR;
2706 }
2707 masterPtr = GetGrid(other);
2708 InitMasterData(masterPtr);
2709 } else if (index == CONF_IPADX) {
2710 if ((Tk_GetPixelsFromObj(interp, slave, objv[i+1], &tmp)
2711 != TCL_OK)
2712 || (tmp < 0)) {
2713 Tcl_ResetResult(interp);
2714 Tcl_AppendResult(interp, "bad ipadx value \"",
2715 Tcl_GetString(objv[i+1]),
2716 "\": must be positive screen distance",
2717 (char *) NULL);
2718 return TCL_ERROR;
2719 }
2720 slavePtr->iPadX = tmp*2;
2721 } else if (index == CONF_IPADY) {
2722 if ((Tk_GetPixelsFromObj(interp, slave, objv[i+1], &tmp)
2723 != TCL_OK)
2724 || (tmp < 0)) {
2725 Tcl_ResetResult(interp);
2726 Tcl_AppendResult(interp, "bad ipady value \"",
2727 Tcl_GetString(objv[i+1]),
2728 "\": must be positive screen distance",
2729 (char *) NULL);
2730 return TCL_ERROR;
2731 }
2732 slavePtr->iPadY = tmp*2;
2733 } else if (index == CONF_PADX) {
2734 if (TkParsePadAmount(interp, tkwin, objv[i+1],
2735 &slavePtr->padLeft, &slavePtr->padX) != TCL_OK) {
2736 return TCL_ERROR;
2737 }
2738 } else if (index == CONF_PADY) {
2739 if (TkParsePadAmount(interp, tkwin, objv[i+1],
2740 &slavePtr->padTop, &slavePtr->padY) != TCL_OK) {
2741 return TCL_ERROR;
2742 }
2743 } else if (index == CONF_ROW) {
2744 if (Tcl_GetIntFromObj(interp, objv[i+1], &tmp) != TCL_OK
2745 || tmp < 0) {
2746 Tcl_ResetResult(interp);
2747 Tcl_AppendResult(interp, "bad grid value \"",
2748 Tcl_GetString(objv[i+1]),
2749 "\": must be a non-negative integer", (char *)NULL);
2750 return TCL_ERROR;
2751 }
2752 slavePtr->row = tmp;
2753 } else if (index == CONF_ROWSPAN) {
2754 if ((Tcl_GetIntFromObj(interp, objv[i+1], &tmp) != TCL_OK)
2755 || tmp <= 0) {
2756 Tcl_ResetResult(interp);
2757 Tcl_AppendResult(interp, "bad rowspan value \"",
2758 Tcl_GetString(objv[i+1]),
2759 "\": must be a positive integer", (char *)NULL);
2760 return TCL_ERROR;
2761 }
2762 slavePtr->numRows = tmp;
2763 } else if (index == CONF_STICKY) {
2764 int sticky = StringToSticky(Tcl_GetString(objv[i+1]));
2765 if (sticky == -1) {
2766 Tcl_AppendResult(interp, "bad stickyness value \"",
2767 Tcl_GetString(objv[i+1]),
2768 "\": must be a string containing n, e, s, and/or w",
2769 (char *)NULL);
2770 return TCL_ERROR;
2771 }
2772 slavePtr->sticky = sticky;
2773 }
2774 }
2775
2776 /*
2777 * Make sure we have a geometry master. We look at:
2778 * 1) the -in flag
2779 * 2) the geometry master of the first slave (if specified)
2780 * 3) the parent of the first slave.
2781 */
2782
2783 if (masterPtr == NULL) {
2784 masterPtr = slavePtr->masterPtr;
2785 }
2786 parent = Tk_Parent(slave);
2787 if (masterPtr == NULL) {
2788 masterPtr = GetGrid(parent);
2789 InitMasterData(masterPtr);
2790 }
2791
2792 if (slavePtr->masterPtr != NULL && slavePtr->masterPtr != masterPtr) {
2793 Unlink(slavePtr);
2794 slavePtr->masterPtr = NULL;
2795 }
2796
2797 if (slavePtr->masterPtr == NULL) {
2798 Gridder *tempPtr = masterPtr->slavePtr;
2799 slavePtr->masterPtr = masterPtr;
2800 masterPtr->slavePtr = slavePtr;
2801 slavePtr->nextPtr = tempPtr;
2802 }
2803
2804 /*
2805 * Make sure that the slave's parent is either the master or
2806 * an ancestor of the master, and that the master and slave
2807 * aren't the same.
2808 */
2809
2810 for (ancestor = masterPtr->tkwin; ; ancestor = Tk_Parent(ancestor)) {
2811 if (ancestor == parent) {
2812 break;
2813 }
2814 if (Tk_TopWinHierarchy(ancestor)) {
2815 Tcl_AppendResult(interp, "can't put ", Tcl_GetString(objv[j]),
2816 " inside ", Tk_PathName(masterPtr->tkwin),
2817 (char *) NULL);
2818 Unlink(slavePtr);
2819 return TCL_ERROR;
2820 }
2821 }
2822
2823 /*
2824 * Try to make sure our master isn't managed by us.
2825 */
2826
2827 if (masterPtr->masterPtr == slavePtr) {
2828 Tcl_AppendResult(interp, "can't put ", Tcl_GetString(objv[j]),
2829 " inside ", Tk_PathName(masterPtr->tkwin),
2830 ", would cause management loop.",
2831 (char *) NULL);
2832 Unlink(slavePtr);
2833 return TCL_ERROR;
2834 }
2835
2836 Tk_ManageGeometry(slave, &gridMgrType, (ClientData) slavePtr);
2837
2838 /*
2839 * Assign default position information.
2840 */
2841
2842 if (slavePtr->column == -1) {
2843 slavePtr->column = defaultColumn;
2844 }
2845 slavePtr->numCols += defaultColumnSpan - 1;
2846 if (slavePtr->row == -1) {
2847 if (masterPtr->masterDataPtr == NULL) {
2848 slavePtr->row = 0;
2849 } else {
2850 slavePtr->row = masterPtr->masterDataPtr->rowEnd;
2851 }
2852 }
2853 defaultColumn += slavePtr->numCols;
2854 defaultColumnSpan = 1;
2855
2856 /*
2857 * Arrange for the parent to be re-arranged at the first
2858 * idle moment.
2859 */
2860
2861 if (masterPtr->abortPtr != NULL) {
2862 *masterPtr->abortPtr = 1;
2863 }
2864 if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
2865 masterPtr->flags |= REQUESTED_RELAYOUT;
2866 Tcl_DoWhenIdle(ArrangeGrid, (ClientData) masterPtr);
2867 }
2868 }
2869
2870 /* Now look for all the "^"'s. */
2871
2872 lastWindow = NULL;
2873 numSkip = 0;
2874 for (j = 0; j < numWindows; j++) {
2875 struct Gridder *otherPtr;
2876 int match; /* found a match for the ^ */
2877 int lastRow, lastColumn; /* implied end of table */
2878
2879 string = Tcl_GetString(objv[j]);
2880 firstChar = string[0];
2881
2882 if (firstChar == '.') {
2883 lastWindow = string;
2884 numSkip = 0;
2885 }
2886 if (firstChar == REL_SKIP) {
2887 numSkip++;
2888 }
2889 if (firstChar != REL_VERT) {
2890 continue;
2891 }
2892
2893 if (masterPtr == NULL) {
2894 Tcl_AppendResult(interp, "can't use '^', cant find master",
2895 (char *) NULL);
2896 return TCL_ERROR;
2897 }
2898
2899 /* Count the number of consecutive ^'s starting from this position */
2900 for (width = 1; width + j < numWindows; width++) {
2901 char *string = Tcl_GetString(objv[j+width]);
2902 if (*string != REL_VERT) break;
2903 }
2904
2905 /*
2906 * Find the implied grid location of the ^
2907 */
2908
2909 if (lastWindow == NULL) {
2910 if (masterPtr->masterDataPtr != NULL) {
2911 SetGridSize(masterPtr);
2912 lastRow = masterPtr->masterDataPtr->rowEnd - 2;
2913 } else {
2914 lastRow = 0;
2915 }
2916 lastColumn = 0;
2917 } else {
2918 other = Tk_NameToWindow(interp, lastWindow, tkwin);
2919 otherPtr = GetGrid(other);
2920 lastRow = otherPtr->row + otherPtr->numRows - 2;
2921 lastColumn = otherPtr->column + otherPtr->numCols;
2922 }
2923
2924 lastColumn += numSkip;
2925
2926 for (match=0, slavePtr = masterPtr->slavePtr; slavePtr != NULL;
2927 slavePtr = slavePtr->nextPtr) {
2928
2929 if (slavePtr->column == lastColumn
2930 && slavePtr->row + slavePtr->numRows - 1 == lastRow) {
2931 if (slavePtr->numCols <= width) {
2932 slavePtr->numRows++;
2933 match++;
2934 j += slavePtr->numCols - 1;
2935 lastWindow = Tk_PathName(slavePtr->tkwin);
2936 numSkip = 0;
2937 break;
2938 }
2939 }
2940 }
2941 if (!match) {
2942 Tcl_AppendResult(interp, "can't find slave to extend with \"^\".",
2943 (char *) NULL);
2944 return TCL_ERROR;
2945 }
2946 }
2947
2948 if (masterPtr == NULL) {
2949 Tcl_AppendResult(interp, "can't determine master window",
2950 (char *) NULL);
2951 return TCL_ERROR;
2952 }
2953 SetGridSize(masterPtr);
2954 return TCL_OK;
2955 }
2956
2957 /*
2958 *----------------------------------------------------------------------
2959 *
2960 * StickyToString
2961 *
2962 * Converts the internal boolean combination of "sticky" bits onto
2963 * a TCL list element containing zero or mor of n, s, e, or w.
2964 *
2965 * Results:
2966 * A string is placed into the "result" pointer.
2967 *
2968 * Side effects:
2969 * none.
2970 *
2971 *----------------------------------------------------------------------
2972 */
2973
2974 static void
2975 StickyToString(flags, result)
2976 int flags; /* the sticky flags */
2977 char *result; /* where to put the result */
2978 {
2979 int count = 0;
2980 if (flags&STICK_NORTH) {
2981 result[count++] = 'n';
2982 }
2983 if (flags&STICK_EAST) {
2984 result[count++] = 'e';
2985 }
2986 if (flags&STICK_SOUTH) {
2987 result[count++] = 's';
2988 }
2989 if (flags&STICK_WEST) {
2990 result[count++] = 'w';
2991 }
2992 #ifdef _LANG
2993 result[count] = '\0';
2994 #else
2995 if (count) {
2996 result[count] = '\0';
2997 } else {
2998 sprintf(result,"{}");
2999 }
3000 #endif
3001 }
3002
3003 /*
3004 *----------------------------------------------------------------------
3005 *
3006 * StringToSticky --
3007 *
3008 * Converts an ascii string representing a widgets stickyness
3009 * into the boolean result.
3010 *
3011 * Results:
3012 * The boolean combination of the "sticky" bits is retuned. If an
3013 * error occurs, such as an invalid character, -1 is returned instead.
3014 *
3015 * Side effects:
3016 * none
3017 *
3018 *----------------------------------------------------------------------
3019 */
3020
3021 static int
3022 StringToSticky(string)
3023 char *string;
3024 {
3025 int sticky = 0;
3026 char c;
3027
3028 while ((c = *string++) != '\0') {
3029 switch (c) {
3030 case 'n': case 'N': sticky |= STICK_NORTH; break;
3031 case 'e': case 'E': sticky |= STICK_EAST; break;
3032 case 's': case 'S': sticky |= STICK_SOUTH; break;
3033 case 'w': case 'W': sticky |= STICK_WEST; break;
3034 case ' ': case ',': case '\t': case '\r': case '\n': break;
3035 default: return -1;
3036 }
3037 }
3038 return sticky;
3039 }
3040
3041 /*
3042 *----------------------------------------------------------------------
3043 *
3044 * NewPairObj --
3045 *
3046 * Creates a new list object and fills it with two integer objects.
3047 *
3048 * Results:
3049 * The newly created list object is returned.
3050 *
3051 * Side effects:
3052 * None.
3053 *
3054 *----------------------------------------------------------------------
3055 */
3056
3057 static Tcl_Obj *
3058 NewPairObj(interp, val1, val2)
3059 Tcl_Interp *interp; /* Current interpreter. */
3060 int val1, val2;
3061 {
3062 Tcl_Obj *res = Tcl_NewListObj(0, NULL);
3063 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(val1));
3064 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(val2));
3065 return res;
3066 }
3067
3068 /*
3069 *----------------------------------------------------------------------
3070 *
3071 * NewQuadObj --
3072 *
3073 * Creates a new list object and fills it with four integer objects.
3074 *
3075 * Results:
3076 * The newly created list object is returned.
3077 *
3078 * Side effects:
3079 * None.
3080 *
3081 *----------------------------------------------------------------------
3082 */
3083
3084 static Tcl_Obj *
3085 NewQuadObj(interp, val1, val2, val3, val4)
3086 Tcl_Interp *interp; /* Current interpreter. */
3087 int val1, val2, val3, val4;
3088 {
3089 Tcl_Obj *res = Tcl_NewListObj(0, NULL);
3090 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(val1));
3091 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(val2));
3092 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(val3));
3093 Tcl_ListObjAppendElement(interp, res, Tcl_NewIntObj(val4));
3094 return res;
3095 }
3096