Debian Codegraph @

pTk/mTk/generic/tkPack.c

raw symbols SCIP
1 /*
2 * tkPack.c --
3 *
4 * This file contains code to implement the "packer"
5 * geometry manager for Tk.
6 *
7 * Copyright (c) 1990-1994 The Regents of the University of California.
8 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkPack.c,v 1.16.2.1 2003/07/17 03:17:08 dgp Exp $
14 */
15
16 #include "tkPort.h"
17 #include "tkInt.h"
18
19 typedef enum {TOP, BOTTOM, LEFT, RIGHT} Side;
20 static CONST char *sideNames[] = {
21 "top", "bottom", "left", "right", (char *) NULL
22 };
23
24 /* For each window that the packer cares about (either because
25 * the window is managed by the packer or because the window
26 * has slaves that are managed by the packer), there is a
27 * structure of the following type:
28 */
29
30 typedef struct Packer {
31 Tk_Window tkwin; /* Tk token for window. NULL means that
32 * the window has been deleted, but the
33 * packet hasn't had a chance to clean up
34 * yet because the structure is still in
35 * use. */
36 struct Packer *masterPtr; /* Master window within which this window
37 * is packed (NULL means this window
38 * isn't managed by the packer). */
39 struct Packer *nextPtr; /* Next window packed within same
40 * parent. List is priority-ordered:
41 * first on list gets packed first. */
42 struct Packer *slavePtr; /* First in list of slaves packed
43 * inside this window (NULL means
44 * no packed slaves). */
45 Side side; /* Side of parent against which
46 * this window is packed. */
47 Tk_Anchor anchor; /* If frame allocated for window is larger
48 * than window needs, this indicates how
49 * where to position window in frame. */
50 int padX, padY; /* Total additional pixels to leave around the
51 * window. Some is of this space is on each
52 * side. This is space *outside* the window:
53 * we'll allocate extra space in frame but
54 * won't enlarge window). */
55 int padLeft, padTop; /* The part of padX or padY to use on the
56 * left or top of the widget, respectively.
57 * By default, this is half of padX or padY. */
58 int iPadX, iPadY; /* Total extra pixels to allocate inside the
59 * window (half of this amount will appear on
60 * each side). */
61 int doubleBw; /* Twice the window's last known border
62 * width. If this changes, the window
63 * must be repacked within its parent. */
64 int *abortPtr; /* If non-NULL, it means that there is a nested
65 * call to ArrangePacking already working on
66 * this window. *abortPtr may be set to 1 to
67 * abort that nested call. This happens, for
68 * example, if tkwin or any of its slaves
69 * is deleted. */
70 int flags; /* Miscellaneous flags; see below
71 * for definitions. */
72 } Packer;
73
74 /*
75 * Flag values for Packer structures:
76 *
77 * REQUESTED_REPACK: 1 means a Tcl_DoWhenIdle request
78 * has already been made to repack
79 * all the slaves of this window.
80 * FILLX: 1 means if frame allocated for window
81 * is wider than window needs, expand window
82 * to fill frame. 0 means don't make window
83 * any larger than needed.
84 * FILLY: Same as FILLX, except for height.
85 * EXPAND: 1 means this window's frame will absorb any
86 * extra space in the parent window.
87 * OLD_STYLE: 1 means this window is being managed with
88 * the old-style packer algorithms (before
89 * Tk version 3.3). The main difference is
90 * that padding and filling are done differently.
91 * DONT_PROPAGATE: 1 means don't set this window's requested
92 * size. 0 means if this window is a master
93 * then Tk will set its requested size to fit
94 * the needs of its slaves.
95 */
96
97 #define REQUESTED_REPACK 1
98 #define FILLX 2
99 #define FILLY 4
100 #define EXPAND 8
101 #define OLD_STYLE 16
102 #define DONT_PROPAGATE 32
103
104 /*
105 * The following structure is the official type record for the
106 * packer:
107 */
108
109 static void PackReqProc _ANSI_ARGS_((ClientData clientData,
110 Tk_Window tkwin));
111 static void PackLostSlaveProc _ANSI_ARGS_((ClientData clientData,
112 Tk_Window tkwin));
113
114 static Tk_GeomMgr packerType = {
115 "pack", /* name */
116 PackReqProc, /* requestProc */
117 PackLostSlaveProc, /* lostSlaveProc */
118 };
119
120 /*
121 * Forward declarations for procedures defined later in this file:
122 */
123
124 static void ArrangePacking _ANSI_ARGS_((ClientData clientData));
125 static int ConfigureSlaves _ANSI_ARGS_((Tcl_Interp *interp,
126 Tk_Window tkwin, int objc, Tcl_Obj *CONST objv[]));
127 static void DestroyPacker _ANSI_ARGS_((char *memPtr));
128 static Packer * GetPacker _ANSI_ARGS_((Tk_Window tkwin));
129 static int PackAfter _ANSI_ARGS_((Tcl_Interp *interp,
130 Packer *prevPtr, Packer *masterPtr, int objc,
131 Tcl_Obj *CONST objv[]));
132 static void PackReqProc _ANSI_ARGS_((ClientData clientData,
133 Tk_Window tkwin));
134 static void PackStructureProc _ANSI_ARGS_((ClientData clientData,
135 XEvent *eventPtr));
136 static void Unlink _ANSI_ARGS_((Packer *packPtr));
137 static int XExpansion _ANSI_ARGS_((Packer *slavePtr,
138 int cavityWidth));
139 static int YExpansion _ANSI_ARGS_((Packer *slavePtr,
140 int cavityHeight));
141
142 /*
143 *--------------------------------------------------------------
144 *
145 * TkPrintPadAmount --
146 *
147 * This procedure generates a text value that describes one
148 * of the -padx, -pady, -ipadx, or -ipady configuration options.
149 * The text value generated is appended to the interpreter
150 * result.
151 *
152 * Results:
153 * None.
154 *
155 * Side effects:
156 * None.
157 *
158 *--------------------------------------------------------------
159 */
160 void
161 TkPrintPadAmount(interp, switchName, halfSpace, allSpace)
162 Tcl_Interp *interp; /* The interpreter into which the result
163 * is written. */
164 char *switchName; /* One of "padx", "pady", "ipadx" or "ipady" */
165 int halfSpace; /* The left or top padding amount */
166 int allSpace; /* The total amount of padding */
167 {
168 char buffer[60 + 2*TCL_INTEGER_SPACE];
169 sprintf(buffer, "-%.10s", switchName);
170 Tcl_AppendElement(interp, buffer);
171 if (halfSpace*2 == allSpace) {
172 Tcl_IntResults(interp, 1, 1, halfSpace);
173 } else {
174 Tcl_Obj *list = Tcl_NewListObj(0,NULL);
175 Tcl_ListObjAppendElement(interp, list, Tcl_NewIntObj(halfSpace));
176 Tcl_ListObjAppendElement(interp, list, Tcl_NewIntObj(allSpace - halfSpace));
177 Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp), list);
178 }
179 }
180
181
182 /*
183 *--------------------------------------------------------------
184 *
185 * Tk_PackCmd --
186 *
187 * This procedure is invoked to process the "pack" Tcl command.
188 * See the user documentation for details on what it does.
189 *
190 * Results:
191 * A standard Tcl result.
192 *
193 * Side effects:
194 * See the user documentation.
195 *
196 *--------------------------------------------------------------
197 */
198
199 int
200 Tk_PackObjCmd(clientData, interp, objc, objv)
201 ClientData clientData; /* Main window associated with
202 * interpreter. */
203 Tcl_Interp *interp; /* Current interpreter. */
204 int objc; /* Number of arguments. */
205 Tcl_Obj *CONST objv[]; /* Argument objects. */
206 {
207 Tk_Window tkwin = (Tk_Window) clientData;
208 char *argv2;
209 static CONST char *optionStrings[] = {
210 /* after, append, before and unpack are deprecated */
211 "after", "append", "before", "unpack",
212 "configure", "forget", "info", "propagate", "slaves", (char *) NULL };
213 enum options {
214 PACK_AFTER, PACK_APPEND, PACK_BEFORE, PACK_UNPACK,
215 PACK_CONFIGURE, PACK_FORGET, PACK_INFO, PACK_PROPAGATE, PACK_SLAVES };
216 int index;
217
218 if (objc >= 2) {
219 char *string = Tcl_GetString(objv[1]);
220 if (string[0] == '.') {
221 return ConfigureSlaves(interp, tkwin, objc-1, objv+1);
222 }
223 }
224 if (objc < 3) {
225 Tcl_WrongNumArgs(interp, 1, objv, "option arg ?arg ...?");
226 return TCL_ERROR;
227 }
228
229 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
230 &index) != TCL_OK) {
231 /*
232 * Call it again without the deprecated ones to get a proper
233 * error message.
234 * This works well since there can't be any ambiguity between
235 * deprecated and new options.
236 */
237
238 Tcl_ResetResult(interp);
239 Tcl_GetIndexFromObj(interp, objv[1], &optionStrings[4], "option", 0,
240 &index);
241 return TCL_ERROR;
242 }
243
244 argv2 = Tcl_GetString(objv[2]);
245 if (index == PACK_AFTER) {
246 Packer *prevPtr;
247 Tk_Window tkwin2;
248
249 if (TkGetWindowFromObj(interp, tkwin, objv[2], &tkwin2) != TCL_OK) {
250 return TCL_ERROR;
251 }
252 prevPtr = GetPacker(tkwin2);
253 if (prevPtr->masterPtr == NULL) {
254 Tcl_AppendResult(interp, "window \"", argv2,
255 "\" isn't packed", (char *) NULL);
256 return TCL_ERROR;
257 }
258 return PackAfter(interp, prevPtr, prevPtr->masterPtr, objc-3, objv+3);
259 } else if (index == PACK_APPEND) {
260 Packer *masterPtr;
261 register Packer *prevPtr;
262 Tk_Window tkwin2;
263
264 if (TkGetWindowFromObj(interp, tkwin, objv[2], &tkwin2) != TCL_OK) {
265 return TCL_ERROR;
266 }
267 masterPtr = GetPacker(tkwin2);
268 prevPtr = masterPtr->slavePtr;
269 if (prevPtr != NULL) {
270 while (prevPtr->nextPtr != NULL) {
271 prevPtr = prevPtr->nextPtr;
272 }
273 }
274 return PackAfter(interp, prevPtr, masterPtr, objc-3, objv+3);
275 } else if (index == PACK_BEFORE) {
276 Packer *packPtr, *masterPtr;
277 register Packer *prevPtr;
278 Tk_Window tkwin2;
279
280 if (TkGetWindowFromObj(interp, tkwin, objv[2], &tkwin2) != TCL_OK) {
281 return TCL_ERROR;
282 }
283 packPtr = GetPacker(tkwin2);
284 if (packPtr->masterPtr == NULL) {
285 Tcl_AppendResult(interp, "window \"", argv2,
286 "\" isn't packed", (char *) NULL);
287 return TCL_ERROR;
288 }
289 masterPtr = packPtr->masterPtr;
290 prevPtr = masterPtr->slavePtr;
291 if (prevPtr == packPtr) {
292 prevPtr = NULL;
293 } else {
294 for ( ; ; prevPtr = prevPtr->nextPtr) {
295 if (prevPtr == NULL) {
296 panic("\"pack before\" couldn't find predecessor");
297 }
298 if (prevPtr->nextPtr == packPtr) {
299 break;
300 }
301 }
302 }
303 return PackAfter(interp, prevPtr, masterPtr, objc-3, objv+3);
304 } else if (index == PACK_CONFIGURE) {
305 if (argv2[0] != '.') {
306 Tcl_AppendResult(interp, "bad argument \"", argv2,
307 "\": must be name of window", (char *) NULL);
308 return TCL_ERROR;
309 }
310 return ConfigureSlaves(interp, tkwin, objc-2, objv+2);
311 } else if (index == PACK_FORGET) {
312 Tk_Window slave;
313 Packer *slavePtr;
314 int i;
315
316 for (i = 2; i < objc; i++) {
317 if (TkGetWindowFromObj(interp, tkwin, objv[i], &slave) != TCL_OK) {
318 continue;
319 }
320 slavePtr = GetPacker(slave);
321 if ((slavePtr != NULL) && (slavePtr->masterPtr != NULL)) {
322 Tk_ManageGeometry(slave, (Tk_GeomMgr *) NULL,
323 (ClientData) NULL);
324 if (slavePtr->masterPtr->tkwin != Tk_Parent(slavePtr->tkwin)) {
325 Tk_UnmaintainGeometry(slavePtr->tkwin,
326 slavePtr->masterPtr->tkwin);
327 }
328 Unlink(slavePtr);
329 Tk_UnmapWindow(slavePtr->tkwin);
330 }
331 }
332 } else if (index == PACK_INFO) {
333 register Packer *slavePtr;
334 Tk_Window slave;
335
336 if (objc != 3) {
337 Tcl_WrongNumArgs(interp, 2, objv, "window");
338 return TCL_ERROR;
339 }
340 if (TkGetWindowFromObj(interp, tkwin, objv[2], &slave) != TCL_OK) {
341 return TCL_ERROR;
342 }
343 slavePtr = GetPacker(slave);
344 if (slavePtr->masterPtr == NULL) {
345 Tcl_AppendResult(interp, "window \"", argv2,
346 "\" isn't packed", (char *) NULL);
347 return TCL_ERROR;
348 }
349 Tcl_AppendElement(interp, "-in");
350 Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),
351 LangWidgetObj(interp,slavePtr->masterPtr->tkwin));
352 Tcl_AppendElement(interp, "-anchor");
353 Tcl_AppendElement(interp, Tk_NameOfAnchor(slavePtr->anchor));
354 Tcl_AppendElement(interp, "-expand");
355 Tcl_IntResults(interp, 1, 1, (slavePtr->flags & EXPAND) != 0);
356 Tcl_AppendElement(interp, "-fill");
357 switch (slavePtr->flags & (FILLX|FILLY)) {
358 case 0:
359 Tcl_AppendElement(interp, "none");
360 break;
361 case FILLX:
362 Tcl_AppendElement(interp, "x");
363 break;
364 case FILLY:
365 Tcl_AppendElement(interp, "y");
366 break;
367 case FILLX|FILLY:
368 Tcl_AppendElement(interp, "both");
369 break;
370 }
371 TkPrintPadAmount(interp, "ipadx", slavePtr->iPadX/2, slavePtr->iPadX);
372 TkPrintPadAmount(interp, "ipady", slavePtr->iPadY/2, slavePtr->iPadY);
373 TkPrintPadAmount(interp, "padx", slavePtr->padLeft, slavePtr->padX);
374 TkPrintPadAmount(interp, "pady", slavePtr->padTop, slavePtr->padY);
375 Tcl_AppendElement(interp, "-side");
376 Tcl_AppendElement(interp, sideNames[slavePtr->side]);
377 } else if (index == PACK_PROPAGATE) {
378 Tk_Window master;
379 Packer *masterPtr;
380 int propagate;
381
382 if (objc > 4) {
383 Tcl_WrongNumArgs(interp, 2, objv, "window ?boolean?");
384 return TCL_ERROR;
385 }
386 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
387 return TCL_ERROR;
388 }
389 masterPtr = GetPacker(master);
390 if (objc == 3) {
391 Tcl_SetObjResult(interp,
392 Tcl_NewBooleanObj(!(masterPtr->flags & DONT_PROPAGATE)));
393 return TCL_OK;
394 }
395 if (Tcl_GetBooleanFromObj(interp, objv[3], &propagate) != TCL_OK) {
396 return TCL_ERROR;
397 }
398 if (propagate) {
399 masterPtr->flags &= ~DONT_PROPAGATE;
400
401 /*
402 * Repack the master to allow new geometry information to
403 * propagate upwards to the master's master.
404 */
405
406 if (masterPtr->abortPtr != NULL) {
407 *masterPtr->abortPtr = 1;
408 }
409 if (!(masterPtr->flags & REQUESTED_REPACK)) {
410 masterPtr->flags |= REQUESTED_REPACK;
411 Tcl_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
412 }
413 } else {
414 masterPtr->flags |= DONT_PROPAGATE;
415 }
416 } else if (index == PACK_SLAVES) {
417 Tk_Window master;
418 Packer *masterPtr, *slavePtr;
419
420 if (objc != 3) {
421 Tcl_WrongNumArgs(interp, 2, objv, "window");
422 return TCL_ERROR;
423 }
424 if (TkGetWindowFromObj(interp, tkwin, objv[2], &master) != TCL_OK) {
425 return TCL_ERROR;
426 }
427 masterPtr = GetPacker(master);
428 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
429 slavePtr = slavePtr->nextPtr) {
430 Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),
431 LangWidgetObj(interp,slavePtr->tkwin));
432 }
433 } else if (index == PACK_UNPACK) {
434 Tk_Window tkwin2;
435 Packer *packPtr;
436
437 if (objc != 3) {
438 Tcl_WrongNumArgs(interp, 2, objv, "window");
439 return TCL_ERROR;
440 }
441 if (TkGetWindowFromObj(interp, tkwin, objv[2], &tkwin2) != TCL_OK) {
442 return TCL_ERROR;
443 }
444 packPtr = GetPacker(tkwin2);
445 if ((packPtr != NULL) && (packPtr->masterPtr != NULL)) {
446 Tk_ManageGeometry(tkwin2, (Tk_GeomMgr *) NULL,
447 (ClientData) NULL);
448 if (packPtr->masterPtr->tkwin != Tk_Parent(packPtr->tkwin)) {
449 Tk_UnmaintainGeometry(packPtr->tkwin,
450 packPtr->masterPtr->tkwin);
451 }
452 Unlink(packPtr);
453 Tk_UnmapWindow(packPtr->tkwin);
454 }
455 }
456
457 return TCL_OK;
458 }
459
460 /*
461 *--------------------------------------------------------------
462 *
463 * PackReqProc --
464 *
465 * This procedure is invoked by Tk_GeometryRequest for
466 * windows managed by the packer.
467 *
468 * Results:
469 * None.
470 *
471 * Side effects:
472 * Arranges for tkwin, and all its managed siblings, to
473 * be re-packed at the next idle point.
474 *
475 *--------------------------------------------------------------
476 */
477
478 /* ARGSUSED */
479 static void
480 PackReqProc(clientData, tkwin)
481 ClientData clientData; /* Packer's information about
482 * window that got new preferred
483 * geometry. */
484 Tk_Window tkwin; /* Other Tk-related information
485 * about the window. */
486 {
487 register Packer *packPtr = (Packer *) clientData;
488
489 packPtr = packPtr->masterPtr;
490 if (!(packPtr->flags & REQUESTED_REPACK)) {
491 packPtr->flags |= REQUESTED_REPACK;
492 Tcl_DoWhenIdle(ArrangePacking, (ClientData) packPtr);
493 }
494 }
495
496 /*
497 *--------------------------------------------------------------
498 *
499 * PackLostSlaveProc --
500 *
501 * This procedure is invoked by Tk whenever some other geometry
502 * claims control over a slave that used to be managed by us.
503 *
504 * Results:
505 * None.
506 *
507 * Side effects:
508 * Forgets all packer-related information about the slave.
509 *
510 *--------------------------------------------------------------
511 */
512
513 /* ARGSUSED */
514 static void
515 PackLostSlaveProc(clientData, tkwin)
516 ClientData clientData; /* Packer structure for slave window that
517 * was stolen away. */
518 Tk_Window tkwin; /* Tk's handle for the slave window. */
519 {
520 register Packer *slavePtr = (Packer *) clientData;
521
522 if (slavePtr->masterPtr->tkwin != Tk_Parent(slavePtr->tkwin)) {
523 Tk_UnmaintainGeometry(slavePtr->tkwin, slavePtr->masterPtr->tkwin);
524 }
525 Unlink(slavePtr);
526 Tk_UnmapWindow(slavePtr->tkwin);
527 }
528
529 /*
530 *--------------------------------------------------------------
531 *
532 * ArrangePacking --
533 *
534 * This procedure is invoked (using the Tcl_DoWhenIdle
535 * mechanism) to re-layout a set of windows managed by
536 * the packer. It is invoked at idle time so that a
537 * series of packer requests can be merged into a single
538 * layout operation.
539 *
540 * Results:
541 * None.
542 *
543 * Side effects:
544 * The packed slaves of masterPtr may get resized or
545 * moved.
546 *
547 *--------------------------------------------------------------
548 */
549
550 static void
551 ArrangePacking(clientData)
552 ClientData clientData; /* Structure describing parent whose slaves
553 * are to be re-layed out. */
554 {
555 register Packer *masterPtr = (Packer *) clientData;
556 register Packer *slavePtr;
557 int cavityX, cavityY, cavityWidth, cavityHeight;
558 /* These variables keep track of the
559 * as-yet-unallocated space remaining in
560 * the middle of the parent window. */
561 int frameX, frameY, frameWidth, frameHeight;
562 /* These variables keep track of the frame
563 * allocated to the current window. */
564 int x, y, width, height; /* These variables are used to hold the
565 * actual geometry of the current window. */
566 int abort; /* May get set to non-zero to abort this
567 * repacking operation. */
568 int borderX, borderY;
569 int borderTop, borderBtm;
570 int borderLeft, borderRight;
571 int maxWidth, maxHeight, tmp;
572
573 masterPtr->flags &= ~REQUESTED_REPACK;
574
575 /*
576 * If the parent has no slaves anymore, then don't do anything
577 * at all: just leave the parent's size as-is.
578 */
579
580 if (masterPtr->slavePtr == NULL) {
581 return;
582 }
583
584 /*
585 * Abort any nested call to ArrangePacking for this window, since
586 * we'll do everything necessary here, and set up so this call
587 * can be aborted if necessary.
588 */
589
590 if (masterPtr->abortPtr != NULL) {
591 *masterPtr->abortPtr = 1;
592 }
593 masterPtr->abortPtr = &abort;
594 abort = 0;
595 Tcl_Preserve((ClientData) masterPtr);
596
597 /*
598 * Pass #1: scan all the slaves to figure out the total amount
599 * of space needed. Two separate width and height values are
600 * computed:
601 *
602 * width - Holds the sum of the widths (plus padding) of
603 * all the slaves seen so far that were packed LEFT
604 * or RIGHT.
605 * height - Holds the sum of the heights (plus padding) of
606 * all the slaves seen so far that were packed TOP
607 * or BOTTOM.
608 *
609 * maxWidth - Gradually builds up the width needed by the master
610 * to just barely satisfy all the slave's needs. For
611 * each slave, the code computes the width needed for
612 * all the slaves so far and updates maxWidth if the
613 * new value is greater.
614 * maxHeight - Same as maxWidth, except keeps height info.
615 */
616
617 width = maxWidth = Tk_InternalBorderLeft(masterPtr->tkwin) +
618 Tk_InternalBorderRight(masterPtr->tkwin);
619 height = maxHeight = Tk_InternalBorderTop(masterPtr->tkwin) +
620 Tk_InternalBorderBottom(masterPtr->tkwin);
621 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
622 slavePtr = slavePtr->nextPtr) {
623 if ((slavePtr->side == TOP) || (slavePtr->side == BOTTOM)) {
624 tmp = Tk_ReqWidth(slavePtr->tkwin) + slavePtr->doubleBw
625 + slavePtr->padX + slavePtr->iPadX + width;
626 if (tmp > maxWidth) {
627 maxWidth = tmp;
628 }
629 height += Tk_ReqHeight(slavePtr->tkwin) + slavePtr->doubleBw
630 + slavePtr->padY + slavePtr->iPadY;
631 } else {
632 tmp = Tk_ReqHeight(slavePtr->tkwin) + slavePtr->doubleBw
633 + slavePtr->padY + slavePtr->iPadY + height;
634 if (tmp > maxHeight) {
635 maxHeight = tmp;
636 }
637 width += Tk_ReqWidth(slavePtr->tkwin) + slavePtr->doubleBw
638 + slavePtr->padX + slavePtr->iPadX;
639 }
640 }
641 if (width > maxWidth) {
642 maxWidth = width;
643 }
644 if (height > maxHeight) {
645 maxHeight = height;
646 }
647
648 if (maxWidth < Tk_MinReqWidth(masterPtr->tkwin)) {
649 maxWidth = Tk_MinReqWidth(masterPtr->tkwin);
650 }
651 if (maxHeight < Tk_MinReqHeight(masterPtr->tkwin)) {
652 maxHeight = Tk_MinReqHeight(masterPtr->tkwin);
653 }
654
655 /*
656 * If the total amount of space needed in the parent window has
657 * changed, and if we're propagating geometry information, then
658 * notify the next geometry manager up and requeue ourselves to
659 * start again after the parent has had a chance to
660 * resize us.
661 */
662
663 if (((maxWidth != Tk_ReqWidth(masterPtr->tkwin))
664 || (maxHeight != Tk_ReqHeight(masterPtr->tkwin)))
665 && !(masterPtr->flags & DONT_PROPAGATE)) {
666 Tk_GeometryRequest(masterPtr->tkwin, maxWidth, maxHeight);
667 masterPtr->flags |= REQUESTED_REPACK;
668 Tcl_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
669 goto done;
670 }
671
672 /*
673 * Pass #2: scan the slaves a second time assigning
674 * new sizes. The "cavity" variables keep track of the
675 * unclaimed space in the cavity of the window; this
676 * shrinks inward as we allocate windows around the
677 * edges. The "frame" variables keep track of the space
678 * allocated to the current window and its frame. The
679 * current window is then placed somewhere inside the
680 * frame, depending on anchor.
681 */
682
683 cavityX = x = Tk_InternalBorderLeft(masterPtr->tkwin);
684 cavityY = y = Tk_InternalBorderTop(masterPtr->tkwin);
685 cavityWidth = Tk_Width(masterPtr->tkwin) -
686 Tk_InternalBorderLeft(masterPtr->tkwin) -
687 Tk_InternalBorderRight(masterPtr->tkwin);
688 cavityHeight = Tk_Height(masterPtr->tkwin) -
689 Tk_InternalBorderTop(masterPtr->tkwin) -
690 Tk_InternalBorderBottom(masterPtr->tkwin);
691 for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
692 slavePtr = slavePtr->nextPtr) {
693 if ((slavePtr->side == TOP) || (slavePtr->side == BOTTOM)) {
694 frameWidth = cavityWidth;
695 frameHeight = Tk_ReqHeight(slavePtr->tkwin) + slavePtr->doubleBw
696 + slavePtr->padY + slavePtr->iPadY;
697 if (slavePtr->flags & EXPAND) {
698 frameHeight += YExpansion(slavePtr, cavityHeight);
699 }
700 cavityHeight -= frameHeight;
701 if (cavityHeight < 0) {
702 frameHeight += cavityHeight;
703 cavityHeight = 0;
704 }
705 frameX = cavityX;
706 if (slavePtr->side == TOP) {
707 frameY = cavityY;
708 cavityY += frameHeight;
709 } else {
710 frameY = cavityY + cavityHeight;
711 }
712 } else {
713 frameHeight = cavityHeight;
714 frameWidth = Tk_ReqWidth(slavePtr->tkwin) + slavePtr->doubleBw
715 + slavePtr->padX + slavePtr->iPadX;
716 if (slavePtr->flags & EXPAND) {
717 frameWidth += XExpansion(slavePtr, cavityWidth);
718 }
719 cavityWidth -= frameWidth;
720 if (cavityWidth < 0) {
721 frameWidth += cavityWidth;
722 cavityWidth = 0;
723 }
724 frameY = cavityY;
725 if (slavePtr->side == LEFT) {
726 frameX = cavityX;
727 cavityX += frameWidth;
728 } else {
729 frameX = cavityX + cavityWidth;
730 }
731 }
732
733 /*
734 * Now that we've got the size of the frame for the window,
735 * compute the window's actual size and location using the
736 * fill, padding, and frame factors. The variables "borderX"
737 * and "borderY" are used to handle the differences between
738 * old-style packing and the new style (in old-style, iPadX
739 * and iPadY are always zero and padding is completely ignored
740 * except when computing frame size).
741 */
742
743 if (slavePtr->flags & OLD_STYLE) {
744 borderX = borderY = 0;
745 borderTop = borderBtm = 0;
746 borderLeft = borderRight = 0;
747 } else {
748 borderX = slavePtr->padX;
749 borderY = slavePtr->padY;
750 borderLeft = slavePtr->padLeft;
751 borderRight = borderX - borderLeft;
752 borderTop = slavePtr->padTop;
753 borderBtm = borderY - borderTop;
754 }
755 width = Tk_ReqWidth(slavePtr->tkwin) + slavePtr->doubleBw
756 + slavePtr->iPadX;
757 if ((slavePtr->flags & FILLX)
758 || (width > (frameWidth - borderX))) {
759 width = frameWidth - borderX;
760 }
761 height = Tk_ReqHeight(slavePtr->tkwin) + slavePtr->doubleBw
762 + slavePtr->iPadY;
763 if ((slavePtr->flags & FILLY)
764 || (height > (frameHeight - borderY))) {
765 height = frameHeight - borderY;
766 }
767 switch (slavePtr->anchor) {
768 case TK_ANCHOR_N:
769 x = frameX + (borderLeft + frameWidth - width - borderRight)/2;
770 y = frameY + borderTop;
771 break;
772 case TK_ANCHOR_NE:
773 x = frameX + frameWidth - width - borderRight;
774 y = frameY + borderTop;
775 break;
776 case TK_ANCHOR_E:
777 x = frameX + frameWidth - width - borderRight;
778 y = frameY + (borderTop + frameHeight - height - borderBtm)/2;
779 break;
780 case TK_ANCHOR_SE:
781 x = frameX + frameWidth - width - borderRight;
782 y = frameY + frameHeight - height - borderBtm;
783 break;
784 case TK_ANCHOR_S:
785 x = frameX + (borderLeft + frameWidth - width - borderRight)/2;
786 y = frameY + frameHeight - height - borderBtm;
787 break;
788 case TK_ANCHOR_SW:
789 x = frameX + borderLeft;
790 y = frameY + frameHeight - height - borderBtm;
791 break;
792 case TK_ANCHOR_W:
793 x = frameX + borderLeft;
794 y = frameY + (borderTop + frameHeight - height - borderBtm)/2;
795 break;
796 case TK_ANCHOR_NW:
797 x = frameX + borderLeft;
798 y = frameY + borderTop;
799 break;
800 case TK_ANCHOR_CENTER:
801 x = frameX + (borderLeft + frameWidth - width - borderRight)/2;
802 y = frameY + (borderTop + frameHeight - height - borderBtm)/2;
803 break;
804 default:
805 panic("bad frame factor in ArrangePacking");
806 }
807 width -= slavePtr->doubleBw;
808 height -= slavePtr->doubleBw;
809
810 /*
811 * The final step is to set the position, size, and mapped/unmapped
812 * state of the slave. If the slave is a child of the master, then
813 * do this here. Otherwise let Tk_MaintainGeometry do the work.
814 */
815
816 if (masterPtr->tkwin == Tk_Parent(slavePtr->tkwin)) {
817 if ((width <= 0) || (height <= 0)) {
818 Tk_UnmapWindow(slavePtr->tkwin);
819 } else {
820 if ((x != Tk_X(slavePtr->tkwin))
821 || (y != Tk_Y(slavePtr->tkwin))
822 || (width != Tk_Width(slavePtr->tkwin))
823 || (height != Tk_Height(slavePtr->tkwin))) {
824 Tk_MoveResizeWindow(slavePtr->tkwin, x, y, width, height);
825 }
826 if (abort) {
827 goto done;
828 }
829
830 /*
831 * Don't map the slave if the master isn't mapped: wait
832 * until the master gets mapped later.
833 */
834
835 if (Tk_IsMapped(masterPtr->tkwin)) {
836 Tk_MapWindow(slavePtr->tkwin);
837 }
838 }
839 } else {
840 if ((width <= 0) || (height <= 0)) {
841 Tk_UnmaintainGeometry(slavePtr->tkwin, masterPtr->tkwin);
842 Tk_UnmapWindow(slavePtr->tkwin);
843 } else {
844 Tk_MaintainGeometry(slavePtr->tkwin, masterPtr->tkwin,
845 x, y, width, height);
846 }
847 }
848
849 /*
850 * Changes to the window's structure could cause almost anything
851 * to happen, including deleting the parent or child. If this
852 * happens, we'll be told to abort.
853 */
854
855 if (abort) {
856 goto done;
857 }
858 }
859
860 done:
861 masterPtr->abortPtr = NULL;
862 Tcl_Release((ClientData) masterPtr);
863 }
864
865 /*
866 *----------------------------------------------------------------------
867 *
868 * XExpansion --
869 *
870 * Given a list of packed slaves, the first of which is packed
871 * on the left or right and is expandable, compute how much to
872 * expand the child.
873 *
874 * Results:
875 * The return value is the number of additional pixels to give to
876 * the child.
877 *
878 * Side effects:
879 * None.
880 *
881 *----------------------------------------------------------------------
882 */
883
884 static int
885 XExpansion(slavePtr, cavityWidth)
886 register Packer *slavePtr; /* First in list of remaining
887 * slaves. */
888 int cavityWidth; /* Horizontal space left for all
889 * remaining slaves. */
890 {
891 int numExpand, minExpand, curExpand;
892 int childWidth;
893
894 /*
895 * This procedure is tricky because windows packed top or bottom can
896 * be interspersed among expandable windows packed left or right.
897 * Scan through the list, keeping a running sum of the widths of
898 * all left and right windows (actually, count the cavity space not
899 * allocated) and a running count of all expandable left and right
900 * windows. At each top or bottom window, and at the end of the
901 * list, compute the expansion factor that seems reasonable at that
902 * point. Return the smallest factor seen at any of these points.
903 */
904
905 minExpand = cavityWidth;
906 numExpand = 0;
907 for ( ; slavePtr != NULL; slavePtr = slavePtr->nextPtr) {
908 childWidth = Tk_ReqWidth(slavePtr->tkwin) + slavePtr->doubleBw
909 + slavePtr->padX + slavePtr->iPadX;
910 if ((slavePtr->side == TOP) || (slavePtr->side == BOTTOM)) {
911 curExpand = (cavityWidth - childWidth)/numExpand;
912 if (curExpand < minExpand) {
913 minExpand = curExpand;
914 }
915 } else {
916 cavityWidth -= childWidth;
917 if (slavePtr->flags & EXPAND) {
918 numExpand++;
919 }
920 }
921 }
922 curExpand = cavityWidth/numExpand;
923 if (curExpand < minExpand) {
924 minExpand = curExpand;
925 }
926 return (minExpand < 0) ? 0 : minExpand;
927 }
928
929 /*
930 *----------------------------------------------------------------------
931 *
932 * YExpansion --
933 *
934 * Given a list of packed slaves, the first of which is packed
935 * on the top or bottom and is expandable, compute how much to
936 * expand the child.
937 *
938 * Results:
939 * The return value is the number of additional pixels to give to
940 * the child.
941 *
942 * Side effects:
943 * None.
944 *
945 *----------------------------------------------------------------------
946 */
947
948 static int
949 YExpansion(slavePtr, cavityHeight)
950 register Packer *slavePtr; /* First in list of remaining
951 * slaves. */
952 int cavityHeight; /* Vertical space left for all
953 * remaining slaves. */
954 {
955 int numExpand, minExpand, curExpand;
956 int childHeight;
957
958 /*
959 * See comments for XExpansion.
960 */
961
962 minExpand = cavityHeight;
963 numExpand = 0;
964 for ( ; slavePtr != NULL; slavePtr = slavePtr->nextPtr) {
965 childHeight = Tk_ReqHeight(slavePtr->tkwin) + slavePtr->doubleBw
966 + slavePtr->padY + slavePtr->iPadY;
967 if ((slavePtr->side == LEFT) || (slavePtr->side == RIGHT)) {
968 curExpand = (cavityHeight - childHeight)/numExpand;
969 if (curExpand < minExpand) {
970 minExpand = curExpand;
971 }
972 } else {
973 cavityHeight -= childHeight;
974 if (slavePtr->flags & EXPAND) {
975 numExpand++;
976 }
977 }
978 }
979 curExpand = cavityHeight/numExpand;
980 if (curExpand < minExpand) {
981 minExpand = curExpand;
982 }
983 return (minExpand < 0) ? 0 : minExpand;
984 }
985
986 /*
987 *--------------------------------------------------------------
988 *
989 * GetPacker --
990 *
991 * This internal procedure is used to locate a Packer
992 * structure for a given window, creating one if one
993 * doesn't exist already.
994 *
995 * Results:
996 * The return value is a pointer to the Packer structure
997 * corresponding to tkwin.
998 *
999 * Side effects:
1000 * A new packer structure may be created. If so, then
1001 * a callback is set up to clean things up when the
1002 * window is deleted.
1003 *
1004 *--------------------------------------------------------------
1005 */
1006
1007 static Packer *
1008 GetPacker(tkwin)
1009 Tk_Window tkwin; /* Token for window for which
1010 * packer structure is desired. */
1011 {
1012 register Packer *packPtr;
1013 Tcl_HashEntry *hPtr;
1014 int new;
1015 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
1016
1017 if (!dispPtr->packInit) {
1018 dispPtr->packInit = 1;
1019 Tcl_InitHashTable(&dispPtr->packerHashTable, TCL_ONE_WORD_KEYS);
1020 }
1021
1022 /*
1023 * See if there's already packer for this window. If not,
1024 * then create a new one.
1025 */
1026
1027 hPtr = Tcl_CreateHashEntry(&dispPtr->packerHashTable, (char *) tkwin,
1028 &new);
1029 if (!new) {
1030 return (Packer *) Tcl_GetHashValue(hPtr);
1031 }
1032 packPtr = (Packer *) ckalloc(sizeof(Packer));
1033 packPtr->tkwin = tkwin;
1034 packPtr->masterPtr = NULL;
1035 packPtr->nextPtr = NULL;
1036 packPtr->slavePtr = NULL;
1037 packPtr->side = TOP;
1038 packPtr->anchor = TK_ANCHOR_CENTER;
1039 packPtr->padX = packPtr->padY = 0;
1040 packPtr->padLeft = packPtr->padTop = 0;
1041 packPtr->iPadX = packPtr->iPadY = 0;
1042 packPtr->doubleBw = 2*Tk_Changes(tkwin)->border_width;
1043 packPtr->abortPtr = NULL;
1044 packPtr->flags = 0;
1045 Tcl_SetHashValue(hPtr, packPtr);
1046 Tk_CreateEventHandler(tkwin, StructureNotifyMask,
1047 PackStructureProc, (ClientData) packPtr);
1048 return packPtr;
1049 }
1050
1051 /*
1052 *--------------------------------------------------------------
1053 *
1054 * TkParsePadAmount --
1055 *
1056 * This procedure parses a padding specification and returns
1057 * the appropriate padding values. A padding specification can
1058 * be either a single pixel width, or a list of two pixel widths.
1059 * If a single pixel width, the amount specified is used for
1060 * padding on both sides. If two amounts are specified, then
1061 * they specify the left/right or top/bottom padding.
1062 *
1063 * Results:
1064 * A standard Tcl return value.
1065 *
1066 * Side effects:
1067 * An error message is written to the interpreter is something
1068 * is not right.
1069 *
1070 *--------------------------------------------------------------
1071 */
1072
1073 int
1074 TkParsePadAmount(interp, tkwin, specObj, halfPtr, allPtr)
1075 Tcl_Interp *interp; /* Interpreter for error reporting. */
1076 Tk_Window tkwin; /* A window. Needed by Tk_GetPixels() */
1077 Tcl_Obj *specObj; /* The argument to "-padx", "-pady", "-ipadx",
1078 * or "-ipady". The thing to be parsed. */
1079 int *halfPtr; /* Write the left/top part of padding here */
1080 int *allPtr; /* Write the total padding here */
1081 {
1082 char *secondPart; /* The second pixel amount of the list */
1083 char *separator = 0; /* Separator between 1st and 2nd pixel widths */
1084 int sepChar = 0; /* Character used as the separator */
1085 int firstInt, secondInt; /* The two components of the padding */
1086 char *padSpec = Tcl_GetString(specObj);
1087
1088 for (secondPart=padSpec;
1089 (*secondPart != '\0') && !isspace(UCHAR(*secondPart));
1090 secondPart++)
1091 { /* Do nothing */ }
1092 if (*secondPart != '\0') {
1093 separator = secondPart;
1094 sepChar = *secondPart;
1095 *secondPart = '\0';
1096 secondPart++;
1097 while ( isspace(UCHAR(*secondPart)) ) {
1098 secondPart++;
1099 }
1100 if (*secondPart == '\0'){
1101 secondPart = 0;
1102 *separator = sepChar;
1103 }
1104 } else {
1105 secondPart = 0;
1106 }
1107 if ((Tk_GetPixels(interp, tkwin, padSpec, &firstInt) != TCL_OK) ||
1108 (firstInt < 0)) {
1109 Tcl_ResetResult(interp);
1110 Tcl_AppendResult(interp, "bad pad value \"", padSpec,
1111 "\": must be positive screen distance", (char *) NULL);
1112 return TCL_ERROR;
1113 }
1114 if (secondPart) {
1115 if ((Tk_GetPixels(interp, tkwin, secondPart, &secondInt) != TCL_OK) ||
1116 (secondInt < 0)) {
1117 Tcl_ResetResult(interp);
1118 Tcl_AppendResult(interp, "bad 2nd pad value \"", secondPart,
1119 "\": must be positive screen distance", (char *) NULL);
1120 return TCL_ERROR;
1121 }
1122 *separator = sepChar;
1123 } else {
1124 secondInt = firstInt;
1125 }
1126 if (halfPtr != 0) *halfPtr = firstInt;
1127 *allPtr = firstInt + secondInt;
1128 return TCL_OK;
1129 }
1130
1131 /*
1132 *--------------------------------------------------------------
1133 *
1134 * PackAfter --
1135 *
1136 * This procedure does most of the real work of adding
1137 * one or more windows into the packing order for its parent.
1138 *
1139 * Results:
1140 * A standard Tcl return value.
1141 *
1142 * Side effects:
1143 * The geometry of the specified windows may change, both now and
1144 * again in the future.
1145 *
1146 *--------------------------------------------------------------
1147 */
1148
1149 static int
1150 PackAfter(interp, prevPtr, masterPtr, objc, objv)
1151 Tcl_Interp *interp; /* Interpreter for error reporting. */
1152 Packer *prevPtr; /* Pack windows in argv just after this
1153 * window; NULL means pack as first
1154 * child of masterPtr. */
1155 Packer *masterPtr; /* Master in which to pack windows. */
1156 int objc; /* Number of elements in objv. */
1157 Tcl_Obj *CONST objv[]; /* Array of lists, each containing 2
1158 * elements: window name and side
1159 * against which to pack. */
1160 {
1161 register Packer *packPtr;
1162 Tk_Window tkwin, ancestor, parent;
1163 int length;
1164 Tcl_Obj **options;
1165 int index, optionCount, c;
1166
1167 /*
1168 * Iterate over all of the window specifiers, each consisting of
1169 * two arguments. The first argument contains the window name and
1170 * the additional arguments contain options such as "top" or
1171 * "padx 20".
1172 */
1173
1174 for ( ; objc > 0; objc -= 2, objv += 2, prevPtr = packPtr) {
1175 if (objc < 2) {
1176 Tcl_AppendResult(interp, "wrong # args: window \"",
1177 Tcl_GetString(objv[0]), "\" should be followed by options",
1178 (char *) NULL);
1179 return TCL_ERROR;
1180 }
1181
1182 /*
1183 * Find the packer for the window to be packed, and make sure
1184 * that the window in which it will be packed is either its
1185 * or a descendant of its parent.
1186 */
1187
1188 if (TkGetWindowFromObj(interp, masterPtr->tkwin, objv[0], &tkwin)
1189 != TCL_OK) {
1190 return TCL_ERROR;
1191 }
1192
1193 parent = Tk_Parent(tkwin);
1194 for (ancestor = masterPtr->tkwin; ; ancestor = Tk_Parent(ancestor)) {
1195 if (ancestor == parent) {
1196 break;
1197 }
1198 if (((Tk_FakeWin *) (ancestor))->flags & TK_TOP_HIERARCHY) {
1199 badWindow:
1200 Tcl_AppendResult(interp, "can't pack ", Tcl_GetString(objv[0]),
1201 " inside ", Tk_PathName(masterPtr->tkwin),
1202 (char *) NULL);
1203 return TCL_ERROR;
1204 }
1205 }
1206 if (((Tk_FakeWin *) (tkwin))->flags & TK_TOP_HIERARCHY) {
1207 goto badWindow;
1208 }
1209 if (tkwin == masterPtr->tkwin) {
1210 goto badWindow;
1211 }
1212 packPtr = GetPacker(tkwin);
1213
1214 /*
1215 * Process options for this window.
1216 */
1217
1218 if (Tcl_ListObjGetElements(interp, objv[1], &optionCount, &options)
1219 != TCL_OK) {
1220 return TCL_ERROR;
1221 }
1222 packPtr->side = TOP;
1223 packPtr->anchor = TK_ANCHOR_CENTER;
1224 packPtr->padX = packPtr->padY = 0;
1225 packPtr->padLeft = packPtr->padTop = 0;
1226 packPtr->iPadX = packPtr->iPadY = 0;
1227 packPtr->flags &= ~(FILLX|FILLY|EXPAND);
1228 packPtr->flags |= OLD_STYLE;
1229 for (index = 0 ; index < optionCount; index++) {
1230 Tcl_Obj *curOptPtr = options[index];
1231 char *curOpt = Tcl_GetStringFromObj(curOptPtr, (int *) &length);
1232
1233 c = curOpt[0];
1234
1235 if ((c == 't')
1236 && (strncmp(curOpt, "top", (size_t) length)) == 0) {
1237 packPtr->side = TOP;
1238 } else if ((c == 'b')
1239 && (strncmp(curOpt, "bottom", (size_t) length)) == 0) {
1240 packPtr->side = BOTTOM;
1241 } else if ((c == 'l')
1242 && (strncmp(curOpt, "left", (size_t) length)) == 0) {
1243 packPtr->side = LEFT;
1244 } else if ((c == 'r')
1245 && (strncmp(curOpt, "right", (size_t) length)) == 0) {
1246 packPtr->side = RIGHT;
1247 } else if ((c == 'e')
1248 && (strncmp(curOpt, "expand", (size_t) length)) == 0) {
1249 packPtr->flags |= EXPAND;
1250 } else if ((c == 'f')
1251 && (strcmp(curOpt, "fill")) == 0) {
1252 packPtr->flags |= FILLX|FILLY;
1253 } else if ((length == 5) && (strcmp(curOpt, "fillx")) == 0) {
1254 packPtr->flags |= FILLX;
1255 } else if ((length == 5) && (strcmp(curOpt, "filly")) == 0) {
1256 packPtr->flags |= FILLY;
1257 } else if ((c == 'p') && (strcmp(curOpt, "padx")) == 0) {
1258 if (optionCount < (index+2)) {
1259 missingPad:
1260 Tcl_AppendResult(interp, "wrong # args: \"", curOpt,
1261 "\" option must be followed by screen distance",
1262 (char *) NULL);
1263 return TCL_ERROR;
1264 }
1265 if (TkParsePadAmount(interp, tkwin, options[index+1],
1266 &packPtr->padLeft, &packPtr->padX) != TCL_OK) {
1267 return TCL_ERROR;
1268 }
1269 packPtr->padX /= 2;
1270 packPtr->padLeft /= 2;
1271 packPtr->iPadX = 0;
1272 index++;
1273 } else if ((c == 'p') && (strcmp(curOpt, "pady")) == 0) {
1274 if (optionCount < (index+2)) {
1275 goto missingPad;
1276 }
1277 if (TkParsePadAmount(interp, tkwin, options[index+1],
1278 &packPtr->padTop, &packPtr->padY) != TCL_OK) {
1279 return TCL_ERROR;
1280 }
1281 packPtr->padY /= 2;
1282 packPtr->padTop /= 2;
1283 packPtr->iPadY = 0;
1284 index++;
1285 } else if ((c == 'f') && (length > 1)
1286 && (strncmp(curOpt, "frame", (size_t) length) == 0)) {
1287 if (optionCount < (index+2)) {
1288 Tcl_AppendResult(interp, "wrong # args: \"frame\" ",
1289 "option must be followed by anchor point",
1290 (char *) NULL);
1291 return TCL_ERROR;
1292 }
1293 if (Tk_GetAnchorFromObj(interp, options[index+1],
1294 &packPtr->anchor) != TCL_OK) {
1295 return TCL_ERROR;
1296 }
1297 index++;
1298 } else {
1299 Tcl_AppendResult(interp, "bad option \"", curOpt,
1300 "\": should be top, bottom, left, right, ",
1301 "expand, fill, fillx, filly, padx, pady, or frame",
1302 (char *) NULL);
1303 return TCL_ERROR;
1304 }
1305 }
1306
1307 if (packPtr != prevPtr) {
1308
1309 /*
1310 * Unpack this window if it's currently packed.
1311 */
1312
1313 if (packPtr->masterPtr != NULL) {
1314 if ((packPtr->masterPtr != masterPtr) &&
1315 (packPtr->masterPtr->tkwin
1316 != Tk_Parent(packPtr->tkwin))) {
1317 Tk_UnmaintainGeometry(packPtr->tkwin,
1318 packPtr->masterPtr->tkwin);
1319 }
1320 Unlink(packPtr);
1321 }
1322
1323 /*
1324 * Add the window in the correct place in its parent's
1325 * packing order, then make sure that the window is
1326 * managed by us.
1327 */
1328
1329 packPtr->masterPtr = masterPtr;
1330 if (prevPtr == NULL) {
1331 packPtr->nextPtr = masterPtr->slavePtr;
1332 masterPtr->slavePtr = packPtr;
1333 } else {
1334 packPtr->nextPtr = prevPtr->nextPtr;
1335 prevPtr->nextPtr = packPtr;
1336 }
1337 Tk_ManageGeometry(tkwin, &packerType, (ClientData) packPtr);
1338 }
1339 }
1340
1341 /*
1342 * Arrange for the parent to be re-packed at the first
1343 * idle moment.
1344 */
1345
1346 if (masterPtr->abortPtr != NULL) {
1347 *masterPtr->abortPtr = 1;
1348 }
1349 if (!(masterPtr->flags & REQUESTED_REPACK)) {
1350 masterPtr->flags |= REQUESTED_REPACK;
1351 Tcl_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
1352 }
1353 return TCL_OK;
1354 }
1355
1356 /*
1357 *----------------------------------------------------------------------
1358 *
1359 * Unlink --
1360 *
1361 * Remove a packer from its parent's list of slaves.
1362 *
1363 * Results:
1364 * None.
1365 *
1366 * Side effects:
1367 * The parent will be scheduled for repacking.
1368 *
1369 *----------------------------------------------------------------------
1370 */
1371
1372 static void
1373 Unlink(packPtr)
1374 register Packer *packPtr; /* Window to unlink. */
1375 {
1376 register Packer *masterPtr, *packPtr2;
1377
1378 masterPtr = packPtr->masterPtr;
1379 if (masterPtr == NULL) {
1380 return;
1381 }
1382 if (masterPtr->slavePtr == packPtr) {
1383 masterPtr->slavePtr = packPtr->nextPtr;
1384 } else {
1385 for (packPtr2 = masterPtr->slavePtr; ; packPtr2 = packPtr2->nextPtr) {
1386 if (packPtr2 == NULL) {
1387 panic("Unlink couldn't find previous window");
1388 }
1389 if (packPtr2->nextPtr == packPtr) {
1390 packPtr2->nextPtr = packPtr->nextPtr;
1391 break;
1392 }
1393 }
1394 }
1395 if (!(masterPtr->flags & REQUESTED_REPACK)) {
1396 masterPtr->flags |= REQUESTED_REPACK;
1397 Tcl_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
1398 }
1399 if (masterPtr->abortPtr != NULL) {
1400 *masterPtr->abortPtr = 1;
1401 }
1402
1403 packPtr->masterPtr = NULL;
1404 }
1405
1406 /*
1407 *----------------------------------------------------------------------
1408 *
1409 * DestroyPacker --
1410 *
1411 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
1412 * to clean up the internal structure of a packer at a safe time
1413 * (when no-one is using it anymore).
1414 *
1415 * Results:
1416 * None.
1417 *
1418 * Side effects:
1419 * Everything associated with the packer is freed up.
1420 *
1421 *----------------------------------------------------------------------
1422 */
1423
1424 static void
1425 DestroyPacker(memPtr)
1426 char *memPtr; /* Info about packed window that
1427 * is now dead. */
1428 {
1429 register Packer *packPtr = (Packer *) memPtr;
1430 ckfree((char *) packPtr);
1431 }
1432
1433 /*
1434 *----------------------------------------------------------------------
1435 *
1436 * PackStructureProc --
1437 *
1438 * This procedure is invoked by the Tk event dispatcher in response
1439 * to StructureNotify events.
1440 *
1441 * Results:
1442 * None.
1443 *
1444 * Side effects:
1445 * If a window was just deleted, clean up all its packer-related
1446 * information. If it was just resized, repack its slaves, if
1447 * any.
1448 *
1449 *----------------------------------------------------------------------
1450 */
1451
1452 static void
1453 PackStructureProc(clientData, eventPtr)
1454 ClientData clientData; /* Our information about window
1455 * referred to by eventPtr. */
1456 XEvent *eventPtr; /* Describes what just happened. */
1457 {
1458 register Packer *packPtr = (Packer *) clientData;
1459
1460 if (eventPtr->type == ConfigureNotify) {
1461 if ((packPtr->slavePtr != NULL)
1462 && !(packPtr->flags & REQUESTED_REPACK)) {
1463 packPtr->flags |= REQUESTED_REPACK;
1464 Tcl_DoWhenIdle(ArrangePacking, (ClientData) packPtr);
1465 }
1466 if (packPtr->doubleBw != 2*Tk_Changes(packPtr->tkwin)->border_width) {
1467 if ((packPtr->masterPtr != NULL)
1468 && !(packPtr->masterPtr->flags & REQUESTED_REPACK)) {
1469 packPtr->doubleBw = 2*Tk_Changes(packPtr->tkwin)->border_width;
1470 packPtr->masterPtr->flags |= REQUESTED_REPACK;
1471 Tcl_DoWhenIdle(ArrangePacking, (ClientData) packPtr->masterPtr);
1472 }
1473 }
1474 } else if (eventPtr->type == DestroyNotify) {
1475 register Packer *slavePtr, *nextPtr;
1476
1477 if (packPtr->masterPtr != NULL) {
1478 Unlink(packPtr);
1479 }
1480 for (slavePtr = packPtr->slavePtr; slavePtr != NULL;
1481 slavePtr = nextPtr) {
1482 Tk_ManageGeometry(slavePtr->tkwin, (Tk_GeomMgr *) NULL,
1483 (ClientData) NULL);
1484 Tk_UnmapWindow(slavePtr->tkwin);
1485 slavePtr->masterPtr = NULL;
1486 nextPtr = slavePtr->nextPtr;
1487 slavePtr->nextPtr = NULL;
1488 }
1489 if (packPtr->tkwin != NULL) {
1490 TkDisplay *dispPtr = ((TkWindow *) packPtr->tkwin)->dispPtr;
1491 Tcl_DeleteHashEntry(Tcl_FindHashEntry(&dispPtr->packerHashTable,
1492 (char *) packPtr->tkwin));
1493 }
1494 if (packPtr->flags & REQUESTED_REPACK) {
1495 Tcl_CancelIdleCall(ArrangePacking, (ClientData) packPtr);
1496 }
1497 packPtr->tkwin = NULL;
1498 Tcl_EventuallyFree((ClientData) packPtr, DestroyPacker);
1499 } else if (eventPtr->type == MapNotify) {
1500 /*
1501 * When a master gets mapped, must redo the geometry computation
1502 * so that all of its slaves get remapped.
1503 */
1504
1505 if ((packPtr->slavePtr != NULL)
1506 && !(packPtr->flags & REQUESTED_REPACK)) {
1507 packPtr->flags |= REQUESTED_REPACK;
1508 Tcl_DoWhenIdle(ArrangePacking, (ClientData) packPtr);
1509 }
1510 } else if (eventPtr->type == UnmapNotify) {
1511 register Packer *packPtr2;
1512
1513 /*
1514 * Unmap all of the slaves when the master gets unmapped,
1515 * so that they don't bother to keep redisplaying
1516 * themselves.
1517 */
1518 for (packPtr2 = packPtr->slavePtr; packPtr2 != NULL;
1519 packPtr2 = packPtr2->nextPtr) {
1520 Tk_UnmapWindow(packPtr2->tkwin);
1521 }
1522 }
1523 }
1524
1525 /*
1526 *----------------------------------------------------------------------
1527 *
1528 * ConfigureSlaves --
1529 *
1530 * This implements the guts of the "pack configure" command. Given
1531 * a list of slaves and configuration options, it arranges for the
1532 * packer to manage the slaves and sets the specified options.
1533 *
1534 * Results:
1535 * TCL_OK is returned if all went well. Otherwise, TCL_ERROR is
1536 * returned and the interp's result is set to contain an error message.
1537 *
1538 * Side effects:
1539 * Slave windows get taken over by the packer.
1540 *
1541 *----------------------------------------------------------------------
1542 */
1543
1544 static int
1545 ConfigureSlaves(interp, tkwin, objc, objv)
1546 Tcl_Interp *interp; /* Interpreter for error reporting. */
1547 Tk_Window tkwin; /* Any window in application containing
1548 * slaves. Used to look up slave names. */
1549 int objc; /* Number of elements in argv. */
1550 Tcl_Obj *CONST objv[]; /* Argument objects: contains one or more
1551 * window names followed by any number
1552 * of "option value" pairs. Caller must
1553 * make sure that there is at least one
1554 * window name. */
1555 {
1556 Packer *masterPtr, *slavePtr, *prevPtr, *otherPtr;
1557 Tk_Window other, slave, parent, ancestor;
1558 int i, j, numWindows, tmp, positionGiven;
1559 char *string;
1560 static CONST char *optionStrings[] = {
1561 "-after", "-anchor", "-before", "-expand", "-fill",
1562 "-in", "-ipadx", "-ipady", "-padx", "-pady", "-side", (char *) NULL };
1563 enum options {
1564 CONF_AFTER, CONF_ANCHOR, CONF_BEFORE, CONF_EXPAND, CONF_FILL,
1565 CONF_IN, CONF_IPADX, CONF_IPADY, CONF_PADX, CONF_PADY, CONF_SIDE };
1566 int index, side;
1567
1568 /*
1569 * Find out how many windows are specified.
1570 */
1571
1572 for (numWindows = 0; numWindows < objc; numWindows++) {
1573 string = Tcl_GetString(objv[numWindows]);
1574 if (string[0] != '.') {
1575 break;
1576 }
1577 }
1578
1579 /*
1580 * Iterate over all of the slave windows, parsing the configuration
1581 * options for each slave. It's a bit wasteful to re-parse the
1582 * options for each slave, but things get too messy if we try to
1583 * parse the arguments just once at the beginning. For example,
1584 * if a slave already is packed we want to just change a few
1585 * existing values without resetting everything. If there are
1586 * multiple windows, the -after, -before, and -in options only
1587 * get processed for the first window.
1588 */
1589
1590 masterPtr = NULL;
1591 prevPtr = NULL;
1592 positionGiven = 0;
1593 for (j = 0; j < numWindows; j++) {
1594 if (TkGetWindowFromObj(interp, tkwin, objv[j], &slave) != TCL_OK) {
1595 return TCL_ERROR;
1596 }
1597 if (Tk_TopWinHierarchy(slave)) {
1598 Tcl_AppendResult(interp, "can't pack \"", Tcl_GetString(objv[j]),
1599 "\": it's a top-level window", (char *) NULL);
1600 return TCL_ERROR;
1601 }
1602 slavePtr = GetPacker(slave);
1603 slavePtr->flags &= ~OLD_STYLE;
1604
1605 /*
1606 * If the slave isn't currently packed, reset all of its
1607 * configuration information to default values (there could
1608 * be old values left from a previous packing).
1609 */
1610
1611 if (slavePtr->masterPtr == NULL) {
1612 slavePtr->side = TOP;
1613 slavePtr->anchor = TK_ANCHOR_CENTER;
1614 slavePtr->padX = slavePtr->padY = 0;
1615 slavePtr->padLeft = slavePtr->padTop = 0;
1616 slavePtr->iPadX = slavePtr->iPadY = 0;
1617 slavePtr->flags &= ~(FILLX|FILLY|EXPAND);
1618 }
1619
1620 for (i = numWindows; i < objc; i+=2) {
1621 if ((i+2) > objc) {
1622 Tcl_AppendResult(interp, "extra option \"",
1623 Tcl_GetString(objv[i]),
1624 "\" (option with no value?)", (char *) NULL);
1625 return TCL_ERROR;
1626 }
1627 if (Tcl_GetIndexFromObj(interp, objv[i], optionStrings, "option",
1628 0, &index) != TCL_OK) {
1629 return TCL_ERROR;
1630 }
1631 if (index == CONF_AFTER) {
1632 if (j == 0) {
1633 if (TkGetWindowFromObj(interp, tkwin, objv[i+1], &other)
1634 != TCL_OK) {
1635 return TCL_ERROR;
1636 }
1637 prevPtr = GetPacker(other);
1638 if (prevPtr->masterPtr == NULL) {
1639 notPacked:
1640 Tcl_AppendResult(interp, "window \"",
1641 Tcl_GetString(objv[i+1]),
1642 "\" isn't packed", (char *) NULL);
1643 return TCL_ERROR;
1644 }
1645 masterPtr = prevPtr->masterPtr;
1646 positionGiven = 1;
1647 }
1648 } else if (index == CONF_ANCHOR) {
1649 if (Tk_GetAnchorFromObj(interp, objv[i+1], &slavePtr->anchor)
1650 != TCL_OK) {
1651 return TCL_ERROR;
1652 }
1653 } else if (index == CONF_BEFORE) {
1654 if (j == 0) {
1655 if (TkGetWindowFromObj(interp, tkwin, objv[i+1], &other)
1656 != TCL_OK) {
1657 return TCL_ERROR;
1658 }
1659 otherPtr = GetPacker(other);
1660 if (otherPtr->masterPtr == NULL) {
1661 goto notPacked;
1662 }
1663 masterPtr = otherPtr->masterPtr;
1664 prevPtr = masterPtr->slavePtr;
1665 if (prevPtr == otherPtr) {
1666 prevPtr = NULL;
1667 } else {
1668 while (prevPtr->nextPtr != otherPtr) {
1669 prevPtr = prevPtr->nextPtr;
1670 }
1671 }
1672 positionGiven = 1;
1673 }
1674 } else if (index == CONF_EXPAND) {
1675 if (Tcl_GetBooleanFromObj(interp, objv[i+1], &tmp) != TCL_OK) {
1676 return TCL_ERROR;
1677 }
1678 slavePtr->flags &= ~EXPAND;
1679 if (tmp) {
1680 slavePtr->flags |= EXPAND;
1681 }
1682 } else if (index == CONF_FILL) {
1683 string = Tcl_GetString(objv[i+1]);
1684 if (strcmp(string, "none") == 0) {
1685 slavePtr->flags &= ~(FILLX|FILLY);
1686 } else if (strcmp(string, "x") == 0) {
1687 slavePtr->flags = (slavePtr->flags & ~FILLY) | FILLX;
1688 } else if (strcmp(string, "y") == 0) {
1689 slavePtr->flags = (slavePtr->flags & ~FILLX) | FILLY;
1690 } else if (strcmp(string, "both") == 0) {
1691 slavePtr->flags |= FILLX|FILLY;
1692 } else {
1693 Tcl_AppendResult(interp, "bad fill style \"", string,
1694 "\": must be none, x, y, or both", (char *) NULL);
1695 return TCL_ERROR;
1696 }
1697 } else if (index == CONF_IN) {
1698 if (j == 0) {
1699 if (TkGetWindowFromObj(interp, tkwin, objv[i+1], &other)
1700 != TCL_OK) {
1701 return TCL_ERROR;
1702 }
1703 masterPtr = GetPacker(other);
1704 prevPtr = masterPtr->slavePtr;
1705 if (prevPtr != NULL) {
1706 while (prevPtr->nextPtr != NULL) {
1707 prevPtr = prevPtr->nextPtr;
1708 }
1709 }
1710 positionGiven = 1;
1711 }
1712 } else if (index == CONF_IPADX) {
1713 if ((Tk_GetPixelsFromObj(interp, slave, objv[i+1], &tmp)
1714 != TCL_OK)
1715 || (tmp < 0)) {
1716 Tcl_ResetResult(interp);
1717 Tcl_AppendResult(interp, "bad ipadx value \"",
1718 Tcl_GetString(objv[i+1]),
1719 "\": must be positive screen distance",
1720 (char *) NULL);
1721 return TCL_ERROR;
1722 }
1723 slavePtr->iPadX = tmp * 2;
1724 } else if (index == CONF_IPADY) {
1725 if ((Tk_GetPixelsFromObj(interp, slave, objv[i+1], &tmp)
1726 != TCL_OK)
1727 || (tmp < 0)) {
1728 Tcl_ResetResult(interp);
1729 Tcl_AppendResult(interp, "bad ipady value \"",
1730 Tcl_GetString(objv[i+1]),
1731 "\": must be positive screen distance",
1732 (char *) NULL);
1733 return TCL_ERROR;
1734 }
1735 slavePtr->iPadY = tmp * 2;
1736 } else if (index == CONF_PADX) {
1737 if (TkParsePadAmount(interp, slave, objv[i+1],
1738 &slavePtr->padLeft, &slavePtr->padX) != TCL_OK) {
1739 return TCL_ERROR;
1740 }
1741 } else if (index == CONF_PADY) {
1742 if (TkParsePadAmount(interp, slave, objv[i+1],
1743 &slavePtr->padTop, &slavePtr->padY) != TCL_OK) {
1744 return TCL_ERROR;
1745 }
1746 } else if (index == CONF_SIDE) {
1747 if (Tcl_GetIndexFromObj(interp, objv[i+1], sideNames, "side",
1748 TCL_EXACT, &side) != TCL_OK) {
1749 return TCL_ERROR;
1750 }
1751 slavePtr->side = (Side) side;
1752 }
1753 }
1754
1755 /*
1756 * If no position in a packing list was specified and the slave
1757 * is already packed, then leave it in its current location in
1758 * its current packing list.
1759 */
1760
1761 if (!positionGiven && (slavePtr->masterPtr != NULL)) {
1762 masterPtr = slavePtr->masterPtr;
1763 goto scheduleLayout;
1764 }
1765
1766 /*
1767 * If the slave is going to be put back after itself then
1768 * skip the whole operation, since it won't work anyway.
1769 */
1770
1771 if (prevPtr == slavePtr) {
1772 masterPtr = slavePtr->masterPtr;
1773 goto scheduleLayout;
1774 }
1775
1776 /*
1777 * If none of the "-in", "-before", or "-after" options has
1778 * been specified, arrange for the slave to go at the end of
1779 * the order for its parent.
1780 */
1781
1782 if (!positionGiven) {
1783 masterPtr = GetPacker(Tk_Parent(slave));
1784 prevPtr = masterPtr->slavePtr;
1785 if (prevPtr != NULL) {
1786 while (prevPtr->nextPtr != NULL) {
1787 prevPtr = prevPtr->nextPtr;
1788 }
1789 }
1790 }
1791
1792 /*
1793 * Make sure that the slave's parent is either the master or
1794 * an ancestor of the master, and that the master and slave
1795 * aren't the same.
1796 */
1797
1798 parent = Tk_Parent(slave);
1799 for (ancestor = masterPtr->tkwin; ; ancestor = Tk_Parent(ancestor)) {
1800 if (ancestor == parent) {
1801 break;
1802 }
1803 if (Tk_TopWinHierarchy(ancestor)) {
1804 Tcl_AppendResult(interp, "can't pack ", Tcl_GetString(objv[j]),
1805 " inside ", Tk_PathName(masterPtr->tkwin),
1806 (char *) NULL);
1807 return TCL_ERROR;
1808 }
1809 }
1810 if (slave == masterPtr->tkwin) {
1811 Tcl_AppendResult(interp, "can't pack ", Tcl_GetString(objv[j]),
1812 " inside itself", (char *) NULL);
1813 return TCL_ERROR;
1814 }
1815
1816 /*
1817 * Unpack the slave if it's currently packed, then position it
1818 * after prevPtr.
1819 */
1820
1821 if (slavePtr->masterPtr != NULL) {
1822 if ((slavePtr->masterPtr != masterPtr) &&
1823 (slavePtr->masterPtr->tkwin
1824 != Tk_Parent(slavePtr->tkwin))) {
1825 Tk_UnmaintainGeometry(slavePtr->tkwin,
1826 slavePtr->masterPtr->tkwin);
1827 }
1828 Unlink(slavePtr);
1829 }
1830 slavePtr->masterPtr = masterPtr;
1831 if (prevPtr == NULL) {
1832 slavePtr->nextPtr = masterPtr->slavePtr;
1833 masterPtr->slavePtr = slavePtr;
1834 } else {
1835 slavePtr->nextPtr = prevPtr->nextPtr;
1836 prevPtr->nextPtr = slavePtr;
1837 }
1838 Tk_ManageGeometry(slave, &packerType, (ClientData) slavePtr);
1839 prevPtr = slavePtr;
1840
1841 /*
1842 * Arrange for the parent to be re-packed at the first
1843 * idle moment.
1844 */
1845
1846 scheduleLayout:
1847 if (masterPtr->abortPtr != NULL) {
1848 *masterPtr->abortPtr = 1;
1849 }
1850 if (!(masterPtr->flags & REQUESTED_REPACK)) {
1851 masterPtr->flags |= REQUESTED_REPACK;
1852 Tcl_DoWhenIdle(ArrangePacking, (ClientData) masterPtr);
1853 }
1854 }
1855 return TCL_OK;
1856 }
1857
1858
1859