1 /* Tabs Widget for SXEmacs.
2 Copyright (C) 1999 Edward A. Falk
4 This file is part of SXEmacs.
6 SXEmacs is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation, either version 3 of the License, or (at your
9 option) any later version.
11 SXEmacs is distributed in the hope that it will be
12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Synched up with: Tabs.c 1.27.
21 #### This file contains essential XEmacs related fixes to the original
22 verison of the Tabs widget. Be VERY careful about syncing if you ever
23 update to a more recent version. In general this is probably now a
27 * Tabs.c - Index Tabs composite widget
29 * Author: Edward A. Falk
30 * falk@falconer.vip.best.com
35 * Overall layout of this widget is as follows:
37 * ________ ,---------. _________
38 * | label || Label || Label | \ tabs
39 * |________|| ||_________| /
40 * |+----------------------------+| \
42 * || child widget window || > frame
43 * |+----------------------------+| |
44 * +------------------------------+ /
46 * The height of the tabs includes the shadow width, top and bottom
47 * margins, and the height of the text.
49 * The height of the frame includes the top and bottom shadow width and the
50 * size of the child widget window.
52 * The tabs overlap the frame and each other vertically by the shadow
53 * width, so that when the topmost tab is drawn, it obliterates part of
58 * TODO: min child height = tab height
65 #include <X11/IntrinsicP.h>
66 #include <X11/StringDefs.h>
68 #include "lwlib-internal.h"
69 #include "ui/X11/xmu.h"
75 #define INDENT 3 /* tabs indented from edge by this much */
76 #define SPACING 0 /* distance between tabs */
77 #define SHADWID 1 /* default shadow width */
78 #define TABDELTA 2 /* top tab grows this many pixels */
79 #define TABLDELTA 2 /* top tab label offset this many pixels */
81 /****************************************************************
85 ****************************************************************/
87 static char defaultTranslations[] = "\
89 <FocusIn>: highlight() \n\
90 <FocusOut>: unhighlight() \n\
91 <Key>Page_Up: page(up) \n\
92 <Key>KP_Page_Up: page(up) \n\
93 <Key>Prior: page(up) \n\
94 <Key>KP_Prior: page(up) \n\
95 <Key>Page_Down: page(down) \n\
96 <Key>KP_Page_Down: page(down) \n\
97 <Key>Next: page(down) \n\
98 <Key>KP_Next: page(down) \n\
99 <Key>Home: page(home) \n\
100 <Key>KP_Home: page(home) \n\
101 <Key>End: page(end) \n\
102 <Key>KP_End: page(end) \n\
103 <Key>Up: highlight(up) \n\
104 <Key>KP_Up: highlight(up) \n\
105 <Key>Down: highlight(down) \n\
106 <Key>KP_Down: highlight(down) \n\
107 <Key> : page(select) \n\
110 static char accelTable[] = " #augment\n\
111 <Key>Page_Up: page(up) \n\
112 <Key>KP_Page_Up: page(up) \n\
113 <Key>Prior: page(up) \n\
114 <Key>KP_Prior: page(up) \n\
115 <Key>Page_Down: page(down) \n\
116 <Key>KP_Page_Down: page(down) \n\
117 <Key>Next: page(down) \n\
118 <Key>KP_Next: page(down) \n\
119 <Key>Home: page(home) \n\
120 <Key>KP_Home: page(home) \n\
121 <Key>End: page(end) \n\
122 <Key>KP_End: page(end) \n\
123 <Key>Up: highlight(up) \n\
124 <Key>KP_Up: highlight(up) \n\
125 <Key>Down: highlight(down) \n\
126 <Key>KP_Down: highlight(down) \n\
127 <Key> : page(select) \n\
129 static XtAccelerators defaultAccelerators; /* #### Never used */
131 #define offset(field) XtOffsetOf(TabsRec, tabs.field)
132 static XtResource resources[] = {
134 {XtNselectInsensitive, XtCSelectInsensitive, XtRBoolean,
136 offset(selectInsensitive), XtRImmediate, (XtPointer) True}
138 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
139 offset(font), XtRString, (XtPointer) XtDefaultFont}
141 {XtNinternalWidth, XtCWidth, XtRDimension, sizeof(Dimension),
142 offset(internalWidth), XtRImmediate, (XtPointer) 4}
144 {XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
145 offset(internalHeight), XtRImmediate, (XtPointer) 4}
147 {XtNborderWidth, XtCBorderWidth, XtRDimension, sizeof(Dimension),
148 XtOffsetOf(RectObjRec, rectangle.border_width), XtRImmediate,
151 {XtNtopWidget, XtCTopWidget, XtRWidget, sizeof(Widget),
152 offset(topWidget), XtRImmediate, NULL}
154 {XtNhighlightWidget, XtCHighlightWidget, XtRWidget, sizeof(Widget),
155 offset(hilight), XtRImmediate, NULL}
157 {XtNcallback, XtCCallback, XtRCallback, sizeof(XtPointer),
158 offset(callbacks), XtRCallback, NULL}
160 {XtNpopdownCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
161 offset(popdownCallbacks), XtRCallback, NULL}
163 {XtNbeNiceToColormap, XtCBeNiceToColormap, XtRBoolean, sizeof(Boolean),
164 offset(be_nice_to_cmap), XtRImmediate, (XtPointer) True}
166 {XtNtopShadowContrast, XtCTopShadowContrast, XtRInt, sizeof(int),
167 offset(top_shadow_contrast), XtRImmediate, (XtPointer) 20},
168 {XtNbottomShadowContrast, XtCBottomShadowContrast, XtRInt, sizeof(int),
169 offset(bot_shadow_contrast), XtRImmediate, (XtPointer) 40},
170 {XtNinsensitiveContrast, XtCInsensitiveContrast, XtRInt, sizeof(int),
171 offset(insensitive_contrast), XtRImmediate, (XtPointer) 33},
172 {XtNaccelerators, XtCAccelerators, XtRAcceleratorTable,
173 sizeof(XtTranslations),
174 XtOffsetOf(TabsRec, core.accelerators), XtRString, accelTable}
179 /* constraint resources */
181 #define offset(field) XtOffsetOf(TabsConstraintsRec, tabs.field)
182 static XtResource tabsConstraintResources[] = {
183 {XtNtabLabel, XtCLabel, XtRString, sizeof(String),
184 offset(label), XtRString, NULL}
186 {XtNtabLeftBitmap, XtCLeftBitmap, XtRBitmap, sizeof(Pixmap),
187 offset(left_bitmap), XtRImmediate, None}
189 {XtNtabForeground, XtCForeground, XtRPixel, sizeof(Pixel),
190 offset(foreground), XtRString, (XtPointer) XtDefaultForeground}
192 {XtNresizable, XtCResizable, XtRBoolean, sizeof(Boolean),
193 offset(resizable), XtRImmediate, (XtPointer) True}
198 #if !NeedFunctionPrototypes
200 /* FORWARD REFERENCES: */
202 /* member functions */
204 static void TabsClassInit();
205 static void TabsInit();
206 static void TabsResize();
207 static void TabsExpose();
208 static void TabsDestroy();
209 static void TabsRealize();
210 static Boolean TabsSetValues();
211 static Boolean TabsAcceptFocus();
212 static XtGeometryResult TabsQueryGeometry();
213 static XtGeometryResult TabsGeometryManager();
214 static void TabsChangeManaged();
215 static void TabsConstraintInitialize();
216 static Boolean TabsConstraintSetValues();
220 static void TabsSelect();
221 static void TabsPage();
222 static void TabsHighlight();
223 static void TabsUnhighlight();
225 /* internal privates */
227 static void TabsAllocGCs(); /* get rendering GCs */
228 static void TabsFreeGCs(); /* return rendering GCs */
229 static void DrawTabs(); /* draw all tabs */
230 static void DrawTab(); /* draw one index tab */
231 static void DrawFrame(); /* draw frame around contents */
232 static void DrawTrim(); /* draw trim around a tab */
233 static void DrawBorder(); /* draw border */
234 static void DrawHighlight(); /* draw highlight */
235 static void UndrawTab(); /* undraw interior of a tab */
236 static void TabWidth(); /* recompute tab size */
237 static void GetPreferredSizes(); /* query all children for their sizes */
238 static void MaxChild(); /* find max preferred child size */
239 static int PreferredSize(); /* compute preferred size */
240 static int PreferredSize2(); /* compute preferred size */
241 static int PreferredSize3(); /* compute preferred size */
242 static void MakeSizeRequest(); /* try to change size */
243 static void getBitmapInfo();
244 static int TabLayout(); /* lay out tabs */
245 static void TabsShuffleRows(); /* bring current tab to bottom row */
247 static void TabsAllocFgGC();
248 static void TabsAllocGreyGC();
252 static void TabsClassInit(void);
253 static void TabsInit(Widget req, Widget new, ArgList, Cardinal * nargs);
254 static void TabsConstraintInitialize(Widget, Widget, ArgList, Cardinal *);
255 static void TabsRealize(Widget, Mask *, XSetWindowAttributes *);
256 static void TabsDestroy(Widget w);
257 static void TabsResize(Widget w);
258 static void TabsExpose(Widget w, XEvent * event, Region region);
259 static Boolean TabsSetValues(Widget, Widget, Widget, ArgList, Cardinal *);
260 static Boolean TabsAcceptFocus(Widget, Time *);
261 static Boolean TabsConstraintSetValues(Widget, Widget, Widget,
262 ArgList, Cardinal *);
263 static XtGeometryResult TabsQueryGeometry(Widget,
266 static XtGeometryResult TabsGeometryManager(Widget, XtWidgetGeometry *,
268 static void TabsChangeManaged(Widget w);
270 static void TabsSelect(Widget, XEvent *, String *, Cardinal *);
271 static void TabsPage(Widget, XEvent *, String *, Cardinal *);
272 static void TabsHighlight(Widget, XEvent *, String *, Cardinal *);
273 static void TabsUnhighlight(Widget, XEvent *, String *, Cardinal *);
275 static void DrawTabs(TabsWidget tw, Bool labels);
276 static void DrawTab(TabsWidget tw, Widget child, Bool labels);
277 static void DrawFrame(TabsWidget tw);
278 static void DrawTrim(TabsWidget, int x, int y,
279 int wid, int hgt, Bool bottom, Bool undraw);
280 static void DrawBorder(TabsWidget tw, Widget child, Bool undraw);
281 static void DrawHighlight(TabsWidget tw, Widget child, Bool undraw);
282 static void UndrawTab(TabsWidget tw, Widget child);
284 static void TabWidth(Widget w);
285 static int TabLayout(TabsWidget, Dimension wid, Dimension hgt,
286 Dimension * r_hgt, Bool query_only);
287 static void GetPreferredSizes(TabsWidget);
288 static void MaxChild(TabsWidget, Widget except, Dimension, Dimension);
289 static void TabsShuffleRows(TabsWidget tw);
290 static int PreferredSize(TabsWidget,
291 Dimension * reply_width, Dimension * reply_height,
292 Dimension * reply_cw, Dimension * reply_ch);
293 static int PreferredSize2(TabsWidget, Dimension cw, Dimension ch,
294 Dimension * rw, Dimension * rh);
295 static int PreferredSize3(TabsWidget, Dimension wid, Dimension hgt,
296 Dimension * rw, Dimension * rh);
297 static void MakeSizeRequest(TabsWidget);
299 static void TabsAllocGCs(TabsWidget);
300 static void TabsFreeGCs(TabsWidget);
301 static void getBitmapInfo(TabsWidget tw, TabsConstraints tab);
302 static void TabsAllocFgGC(TabsWidget tw);
303 static void TabsAllocGreyGC(TabsWidget tw);
307 #define AddRect(i,xx,yy,w,h) \
308 do{rects[(i)].x=(xx); rects[i].y=(yy); \
309 rects[i].width=(w); rects[i].height=(h);}while(0)
311 static XtActionsRec actionsList[] = {
312 {"select", TabsSelect},
314 {"highlight", TabsHighlight},
315 {"unhighlight", TabsUnhighlight},
318 /****************************************************************
320 * Full class record constant
322 ****************************************************************/
325 #define SuperClass (&constraintClassRec)
327 #define SuperClass (&xmManagerClassRec)
330 TabsClassRec tabsClassRec = {
332 /* core_class fields */
333 /* superclass */ (WidgetClass) SuperClass,
334 /* class_name */ "Tabs",
335 /* widget_size */ sizeof(TabsRec),
336 /* class_initialize */ TabsClassInit,
337 /* class_part_init */ NULL,
339 /* class_inited */ FALSE,
340 /* initialize */ TabsInit,
341 /* initialize_hook */ NULL,
342 /* realize */ TabsRealize,
343 /* actions */ actionsList,
344 /* num_actions */ XtNumber(actionsList),
345 /* resources */ resources,
346 /* num_resources */ XtNumber(resources),
347 /* xrm_class */ NULLQUARK,
348 /* compress_motion */ TRUE,
349 #if XtSpecificationRelease < 6
350 /* compress_exposure */ XtExposeCompressMaximal,
352 /* compress_exposure */ XtExposeCompressMaximal | XtExposeNoRegion,
354 /* compress_enterleave */ TRUE,
355 /* visible_interest */ TRUE,
356 /* destroy */ TabsDestroy,
357 /* resize */ TabsResize,
358 /* expose */ TabsExpose,
359 /* set_values */ TabsSetValues,
360 /* set_values_hook */ NULL,
361 /* set_values_almost */ XtInheritSetValuesAlmost,
362 /* get_values_hook */ NULL,
363 /* accept_focus */ TabsAcceptFocus,
364 /* version */ XtVersion,
365 /* callback_private */ NULL,
366 /* tm_table */ defaultTranslations,
367 /* query_geometry */ TabsQueryGeometry,
368 /* display_accelerator */ XtInheritDisplayAccelerator,
373 /* composite_class fields */
374 /* geometry_manager */ TabsGeometryManager,
375 /* change_managed */ TabsChangeManaged,
376 /* insert_child */ XtInheritInsertChild,
378 /* delete_child */ XtInheritDeleteChild,
384 /* constraint_class fields */
385 /* subresources */ tabsConstraintResources,
386 /* subresource_count */ XtNumber(tabsConstraintResources),
387 /* constraint_size */ sizeof(TabsConstraintsRec),
388 /* initialize */ TabsConstraintInitialize,
390 /* set_values */ TabsConstraintSetValues,
391 /* extension */ NULL,
395 /* Manager Class fields */
397 /* translations */ NULL,
398 /* syn_resources */ NULL,
399 /* num_syn_resources */ 0,
400 /* syn_constraint_resources */ NULL,
401 /* num_syn_constraint_resources */ 0,
402 /* parent_process */ XmInheritParentProcess,
408 /* Tabs class fields */
409 /* extension */ NULL,
413 WidgetClass tabsWidgetClass = (WidgetClass) & tabsClassRec;
415 #define TabsNumChildren(tw) (((TabsWidget)tw)->composite.num_children)
416 #define TabVisible(tab) \
417 (XtIsManaged(tab) && \
418 ((TabsConstraints)((tab)->core.constraints))->tabs.visible)
420 /****************************************************************
424 ****************************************************************/
426 static void TabsClassInit(void)
428 defaultAccelerators = XtParseAcceleratorTable(accelTable);
429 /* TODO: register converter for labels? */
432 /* Init a newly created tabs widget. Compute height of tabs
433 * and optionally compute size of widget. */
438 TabsInit(Widget request, Widget new, ArgList args, Cardinal * num_args)
440 TabsWidget newTw = (TabsWidget) new;
442 newTw->tabs.numRows = 0;
443 newTw->tabs.realRows = 0;
445 GetPreferredSizes(newTw);
447 /* height is easy, it's the same for all tabs:
448 * TODO: font height + height of tallest bitmap.
450 newTw->tabs.tab_height = 2 * newTw->tabs.internalHeight + SHADWID;
452 if (newTw->tabs.font != NULL)
453 newTw->tabs.tab_height += newTw->tabs.font->max_bounds.ascent +
454 newTw->tabs.font->max_bounds.descent;
456 /* GC allocation is deferred until XtRealize() */
458 /* if size not explicitly set, set it to our preferred size now. */
460 if (request->core.width == 0 || request->core.height == 0) {
462 PreferredSize(newTw, &w, &h, NULL, NULL);
463 if (request->core.width == 0)
465 if (request->core.height == 0)
466 new->core.height = h;
467 XtClass(new)->core_class.resize(new);
470 /* defer GC allocation, etc., until Realize() time. */
471 newTw->tabs.foregroundGC =
472 newTw->tabs.backgroundGC =
473 newTw->tabs.greyGC = newTw->tabs.topGC = newTw->tabs.botGC = None;
475 newTw->tabs.grey50 = None;
477 newTw->tabs.needs_layout = False;
479 newTw->tabs.hilight = NULL;
482 newTw->manager.navigation_type = XmTAB_GROUP;
483 newTw->manager.traversal_on = True;
487 /* Init the constraint part of a new tab child. Compute the
492 TabsConstraintInitialize(Widget request, Widget new,
493 ArgList args, Cardinal * num_args)
495 TabsConstraints tab = (TabsConstraints) new->core.constraints;
496 tab->tabs.greyAlloc = False; /* defer allocation of pixel */
497 tab->tabs.visible = False;
499 getBitmapInfo((TabsWidget) XtParent(new), tab);
503 /* Called when tabs widget first realized. Create the window
504 * and allocate the GCs
508 TabsRealize(Widget w, Mask * valueMask, XSetWindowAttributes * attributes)
510 TabsWidget tw = (TabsWidget) w;
512 attributes->bit_gravity = NorthWestGravity;
513 *valueMask |= CWBitGravity;
515 SuperClass->core_class.realize(w, valueMask, attributes);
520 static void TabsDestroy(Widget w)
522 TabsFreeGCs((TabsWidget) w);
525 /* Parent has resized us. This will require that the tabs be
529 static void TabsResize(Widget w)
531 TabsWidget tw = (TabsWidget) w;
533 int num_children = tw->composite.num_children;
535 TabsConstraints tab; /* #### unused */
536 Dimension cw, ch, bw;
538 /* Our size has now been dictated by the parent. Lay out the
539 * tabs, lay out the frame, lay out the children. Remember
540 * that the tabs overlap each other and the frame by shadowWidth.
541 * Also, the top tab is larger than the others, so if there's only
542 * one row, the widget must be made taller to accommodate this.
544 * Once the tabs are laid out, if there is more than one
545 * row, we may need to shuffle the rows to bring the top tab
549 tw->tabs.needs_layout = False;
551 if (num_children > 0 && tw->composite.children != NULL) {
552 /* Loop through the tabs and assign rows & x positions */
553 (void)TabLayout(tw, tw->core.width, tw->core.height, NULL,
555 num_children = TabsNumChildren(tw);
557 /* assign a top widget, bring it to bottom row. */
560 /* now assign child positions & sizes. Positions are all the
561 * same: just inside the frame. Sizes are also all the same.
564 tw->tabs.child_width = cw = tw->core.width - 2 * SHADWID;
565 tw->tabs.child_height = ch =
566 tw->core.height < (tw->tabs.tab_total + 2 * SHADWID) ? 0 :
567 tw->core.height - tw->tabs.tab_total - 2 * SHADWID;
569 for (i = 0, childP = tw->composite.children;
570 i < num_children; ++i, ++childP)
571 if (XtIsManaged(*childP)) {
573 (TabsConstraints) (*childP)->core.
575 bw = (*childP)->core.border_width;
576 /* Don't do anything if we can't see any of the child. */
577 if (ch >= bw * 2 && ch > 0 && cw >= bw * 2
579 XtConfigureWidget(*childP, SHADWID,
581 SHADWID, cw - bw * 2,
584 if (XtIsRealized(w)) {
585 XClearWindow(XtDisplay((Widget) tw),
586 XtWindow((Widget) tw));
587 /* should not be necessary to explicitly repaint after a
588 * resize, but XEmacs folks tell me it is.
590 XtClass(tw)->core_class.expose((Widget) tw, NULL, None);
595 /* Redraw entire Tabs widget */
598 static void TabsExpose(Widget w, XEvent * event, Region region)
600 TabsWidget tw = (TabsWidget) w;
602 if (tw->tabs.needs_layout)
603 XtClass(w)->core_class.resize(w);
608 /* Called when any Tabs widget resources are changed. */
612 TabsSetValues(Widget current, Widget request, Widget new,
613 ArgList args, Cardinal * num_args)
615 TabsWidget curtw = (TabsWidget) current;
616 TabsWidget tw = (TabsWidget) new;
617 Boolean needRedraw = False;
621 if (tw->tabs.font != curtw->tabs.font ||
622 tw->tabs.internalWidth != curtw->tabs.internalWidth ||
623 tw->tabs.internalHeight != curtw->tabs.internalHeight) {
624 tw->tabs.tab_height = 2 * tw->tabs.internalHeight + SHADWID;
626 if (tw->tabs.font != NULL)
627 tw->tabs.tab_height +=
628 tw->tabs.font->max_bounds.ascent +
629 tw->tabs.font->max_bounds.descent;
631 /* Tab size has changed. Resize all tabs and request a new size */
632 for (i = 0, childP = tw->composite.children;
633 i < tw->composite.num_children; ++i, ++childP)
634 if (XtIsManaged(*childP))
636 PreferredSize(tw, &tw->core.width, &tw->core.height, NULL,
639 tw->tabs.needs_layout = True;
642 /* TODO: if any color changes, need to recompute GCs and redraw */
644 if (tw->core.background_pixel != curtw->core.background_pixel ||
645 tw->core.background_pixmap != curtw->core.background_pixmap ||
646 tw->tabs.font != curtw->tabs.font)
647 if (XtIsRealized(new)) {
653 if (tw->core.sensitive != curtw->core.sensitive)
656 /* Highlit widget changed */
657 if (tw->tabs.hilight != curtw->tabs.hilight) {
661 /* If top widget changes, need to change stacking order, redraw tabs.
662 * Window system will handle the redraws.
665 if (tw->tabs.topWidget != curtw->tabs.topWidget) {
666 if (XtIsRealized(tw->tabs.topWidget)) {
667 Widget w = tw->tabs.topWidget;
668 TabsConstraints tab =
669 (TabsConstraints) w->core.constraints;
671 XRaiseWindow(XtDisplay(w), XtWindow(w));
673 XtVaSetValues(curtw->tabs.topWidget, XmNtraversalOn,
675 XtVaSetValues(w, XmNtraversalOn, True, 0);
678 if ((int)tab->tabs.row != (int)tw->tabs.numRows - 1) {
683 tw->tabs.needs_layout = True;
689 /* Called when any child constraint resources change. */
693 TabsConstraintSetValues(Widget current, Widget request, Widget new,
694 ArgList args, Cardinal * num_args)
696 TabsWidget tw = (TabsWidget) XtParent(new);
697 TabsConstraints ctab = (TabsConstraints) current->core.constraints;
698 TabsConstraints tab = (TabsConstraints) new->core.constraints;
700 /* if label changes, need to re-layout the entire widget */
701 /* if foreground changes, need to redraw tab label */
703 /* TODO: only need resize of new bitmap has different dimensions
707 if (tab->tabs.label != ctab->tabs.label || /* Tab size has changed. */
708 tab->tabs.left_bitmap != ctab->tabs.left_bitmap) {
710 tw->tabs.needs_layout = True;
712 if (tab->tabs.left_bitmap != ctab->tabs.left_bitmap)
713 getBitmapInfo(tw, tab);
715 /* If there are no subclass ConstraintSetValues procedures remaining
716 * to be invoked, and if the preferred size has changed, ask
719 if (XtClass((Widget) tw) == tabsWidgetClass)
723 /* The child widget itself never needs a redisplay, but the parent
727 if (XtIsRealized(new)) {
728 if (tw->tabs.needs_layout) {
729 XClearWindow(XtDisplay((Widget) tw),
730 XtWindow((Widget) tw));
731 XtClass(tw)->core_class.expose((Widget) tw, NULL, None);
734 else if (tab->tabs.foreground != ctab->tabs.foreground)
735 DrawTab(tw, new, True);
741 static Boolean TabsAcceptFocus(Widget w, Time * t)
743 if (!w->core.being_destroyed && XtIsRealized(w) &&
744 XtIsSensitive(w) && XtIsManaged(w) && w->core.visible) {
746 for (p = XtParent(w); !XtIsShell(p); p = XtParent(p)) ;
747 XtSetKeyboardFocus(p, w);
754 * Return preferred size. Happily accept anything >= our preferred size.
755 * (TODO: is that the right thing to do? Should we always return "almost"
756 * if offered more than we need?)
759 static XtGeometryResult
760 TabsQueryGeometry(Widget w,
761 XtWidgetGeometry * intended, XtWidgetGeometry * preferred)
763 register TabsWidget tw = (TabsWidget) w;
764 XtGeometryMask mode = intended->request_mode;
766 preferred->request_mode = CWWidth | CWHeight;
767 PreferredSize(tw, &preferred->width, &preferred->height, NULL, NULL);
769 if ((!(mode & CWWidth) || intended->width == w->core.width) &&
770 (!(mode & CWHeight) || intended->height == w->core.height))
773 if ((!(mode & CWWidth) || intended->width >= preferred->width) &&
774 (!(mode & CWHeight) || intended->height >= preferred->height))
775 return XtGeometryYes;
777 return XtGeometryAlmost;
781 * Geometry Manager; called when a child wants to be resized.
784 static XtGeometryResult
785 TabsGeometryManager(Widget w, XtWidgetGeometry * req, XtWidgetGeometry * reply)
787 TabsWidget tw = (TabsWidget) XtParent(w);
788 Dimension s = SHADWID;
789 TabsConstraints tab = (TabsConstraints) w->core.constraints;
790 XtGeometryResult result;
793 /* Position request always denied */
795 if (((req->request_mode & CWX) && req->x != w->core.x) ||
796 ((req->request_mode & CWY) && req->y != w->core.y) ||
797 !tab->tabs.resizable)
800 /* Make all three fields in the request valid */
801 if (!(req->request_mode & CWWidth))
802 req->width = w->core.width;
803 if (!(req->request_mode & CWHeight))
804 req->height = w->core.height;
805 if (!(req->request_mode & CWBorderWidth))
806 req->border_width = w->core.border_width;
808 if (req->width == w->core.width &&
809 req->height == w->core.height &&
810 req->border_width == w->core.border_width)
813 rw = req->width + 2 * req->border_width;
814 rh = req->height + 2 * req->border_width;
816 /* find out how big the children want to be now */
817 MaxChild(tw, w, rw, rh);
819 /* Size changes must see if the new size can be accommodated.
820 * The Tabs widget keeps all of its children the same
821 * size. A request to shrink will be accepted only if the
822 * new size is still big enough for all other children. A
823 * request to shrink that is not big enough for all children
824 * returns an "almost" response with the new proposed size
825 * or a "no" response if unable to shrink at all.
827 * A request to grow will be accepted only if the Tabs parent can
828 * grow to accommodate.
831 * We could get fancy here and re-arrange the tabs if it is
832 * necessary to compromise with the parent, but we'll save that
836 if (req->request_mode & (CWWidth | CWHeight | CWBorderWidth)) {
837 Dimension cw, ch; /* children's preferred size */
838 Dimension aw, ah; /* available size we can give child */
839 Dimension th; /* space used by tabs */
840 Dimension wid, hgt; /* Tabs widget size */
842 cw = tw->tabs.max_cw;
843 ch = tw->tabs.max_ch;
845 /* find out what *my* resulting preferred size would be */
847 PreferredSize2(tw, cw, ch, &wid, &hgt);
849 /* Would my size change? If so, ask to be resized. */
851 if (wid != tw->core.width || hgt != tw->core.height) {
852 Dimension oldWid = tw->core.width, oldHgt =
854 XtWidgetGeometry myrequest, myreply;
856 myrequest.width = wid;
857 myrequest.height = hgt;
858 myrequest.request_mode = CWWidth | CWHeight;
860 assert(wid > 0 && hgt > 0);
861 /* If child is only querying, or if we're going to have to
862 * offer the child a compromise, then make this a query only.
865 if ((req->request_mode & XtCWQueryOnly) || rw < cw
867 myrequest.request_mode |= XtCWQueryOnly;
870 XtMakeGeometryRequest((Widget) tw, &myrequest,
873 /* !$@# Athena Box widget changes the core size even if QueryOnly
874 * is set. I'm convinced this is a bug. At any rate, to work
875 * around the bug, we need to restore the core size after every
876 * query geometry request. This is only partly effective,
877 * as there may be other boxes further up the tree.
879 if (myrequest.request_mode & XtCWQueryOnly) {
880 tw->core.width = oldWid;
881 tw->core.height = oldHgt;
884 /* based on the parent's response, determine what the
885 * resulting Tabs widget size would be.
891 tw->tabs.needs_layout = True;
895 wid = tw->core.width;
896 hgt = tw->core.height;
899 case XtGeometryAlmost:
901 hgt = myreply.height;
902 tw->tabs.needs_layout = True;
909 /* Within the constraints imposed by the parent, what is
910 * the max size we can give the child?
912 (void)TabLayout(tw, wid, hgt, &th, True);
914 ah = hgt - th - 2 * s;
916 /* OK, make our decision. If requested size is >= max sibling
917 * preferred size, AND requested size <= available size, then
918 * we accept. Otherwise, we offer a compromise.
921 if (rw == aw && rh == ah) {
922 /* Acceptable. If this wasn't a query, change *all* children
925 if (req->request_mode & XtCWQueryOnly)
926 return XtGeometryYes;
928 Widget *childP = tw->composite.children;
930 w->core.border_width = req->border_width;
931 for (i = TabsNumChildren(tw); --i >= 0;
933 if (TabVisible(*childP)) {
934 bw = (*childP)->core.
936 XtConfigureWidget(*childP, s,
944 /* TODO: under what conditions will we need to redraw? */
945 XClearWindow(XtDisplay((Widget) tw),
946 XtWindow((Widget) tw));
947 XtClass(tw)->core_class.expose((Widget) tw,
950 return XtGeometryDone;
954 /* Cannot grant child's request. Describe what we *can* do
955 * and return counter-offer.
957 reply->width = aw - 2 * req->border_width;
958 reply->height = ah - 2 * req->border_width;
959 reply->border_width = req->border_width;
960 reply->request_mode = CWWidth | CWHeight | CWBorderWidth;
961 return XtGeometryAlmost;
964 return XtGeometryYes;
967 /* The number of children we manage has changed; recompute
971 static void TabsChangeManaged(Widget w)
973 TabsWidget tw = (TabsWidget) w;
974 Widget *childP = tw->composite.children;
977 if (tw->tabs.topWidget != NULL &&
978 (!XtIsManaged(tw->tabs.topWidget) ||
979 tw->tabs.topWidget->core.being_destroyed))
980 tw->tabs.topWidget = NULL;
982 /* Check whether the highlight tab is still valid. */
983 if (tw->tabs.hilight != NULL &&
984 (!XtIsManaged(tw->tabs.hilight) ||
985 tw->tabs.hilight->core.being_destroyed))
986 tw->tabs.hilight = NULL;
988 GetPreferredSizes(tw);
991 XtClass(w)->core_class.resize(w);
992 if (XtIsRealized(w)) {
993 Display *dpy = XtDisplay(w);
994 XClearWindow(dpy, XtWindow(w));
995 XtClass(w)->core_class.expose(w, NULL, NULL);
997 /* make sure the top widget stays on top. This requires
998 * making sure that all new children are realized first.
1000 if (tw->tabs.topWidget != NULL
1001 && XtIsRealized(tw->tabs.topWidget)) {
1002 for (i = TabsNumChildren(tw); --i >= 0; ++childP)
1003 if (!XtIsRealized(*childP))
1004 XtRealizeWidget(*childP);
1006 XRaiseWindow(dpy, XtWindow(tw->tabs.topWidget));
1010 /* Only top widget may receive input */
1012 for (childP = tw->composite.children, i = tw->composite.num_children;
1013 --i >= 0; ++childP) {
1014 XtVaSetValues(*childP, XmNtraversalOn, False, 0);
1017 if (tw->tabs.topWidget != NULL)
1018 XtVaSetValues(tw->tabs.topWidget, XmNtraversalOn, True, 0);
1022 /****************************************************************
1026 ****************************************************************/
1028 /* User clicks on a tab, figure out which one it was. */
1032 TabsSelect(Widget w, XEvent * event, String * params, Cardinal * num_params)
1034 TabsWidget tw = (TabsWidget) w;
1037 Dimension h = tw->tabs.tab_height;
1041 XmProcessTraversal(w, XmTRAVERSE_CURRENT);
1044 /* TODO: is there an Xmu function or something to do this instead? */
1045 switch (event->type) {
1048 x = event->xbutton.x;
1049 y = event->xbutton.y;
1060 /* TODO: determine which tab was clicked, if any. Set that
1061 * widget to be top of stacking order with XawTabsSetTop().
1063 for (i = 0, childP = tw->composite.children;
1064 i < TabsNumChildren(tw); ++i, ++childP)
1065 if (TabVisible(*childP)) {
1066 TabsConstraints tab =
1067 (TabsConstraints) (*childP)->core.constraints;
1068 if (x > tab->tabs.x && x < tab->tabs.x + tab->tabs.width
1069 && y > tab->tabs.y && y < tab->tabs.y + h) {
1070 if (*childP != tw->tabs.topWidget
1071 && (XtIsSensitive(*childP)
1072 || tw->tabs.selectInsensitive))
1073 XawTabsSetTop(*childP, True);
1079 /* User hits a key */
1082 TabsPage(Widget w, XEvent * event, String * params, Cardinal * num_params)
1084 TabsWidget tw = (TabsWidget) w;
1085 Widget newtop = NULL;
1088 int nc = TabsNumChildren(tw);
1093 if (*num_params < 1) {
1094 XtAppWarning(XtWidgetToApplicationContext(w),
1095 "Tabs: page() action called with no arguments");
1099 if (tw->tabs.topWidget == NULL)
1100 tw->tabs.topWidget = tw->composite.children[0];
1102 for (idx = 0, childP = tw->composite.children; idx < nc;
1104 if (tw->tabs.topWidget == *childP)
1107 switch (params[0][0]) {
1112 newtop = tw->composite.children[idx];
1115 case 'd': /* down */
1119 newtop = tw->composite.children[idx];
1125 newtop = tw->composite.children[0];
1130 newtop = tw->composite.children[nc - 1];
1133 case 's': /* selected */
1135 if ((newtop = tw->tabs.hilight) == NULL)
1140 XawTabsSetTop(newtop, True);
1143 /* User hits up/down key */
1146 TabsHighlight(Widget w, XEvent * event, String * params, Cardinal * num_params)
1148 TabsWidget tw = (TabsWidget) w;
1149 Widget newhl = NULL;
1152 int nc = TabsNumChildren(tw);
1157 if (*num_params < 1) {
1158 if (tw->tabs.hilight != NULL)
1159 DrawHighlight(tw, tw->tabs.hilight, False);
1163 if (tw->tabs.hilight == NULL)
1164 newhl = tw->composite.children[0];
1167 /* find index of currently highlit child */
1168 for (idx = 0, childP = tw->composite.children; idx < nc;
1170 if (tw->tabs.hilight == *childP)
1173 switch (params[0][0]) {
1178 newhl = tw->composite.children[idx];
1181 case 'd': /* down */
1185 newhl = tw->composite.children[idx];
1190 newhl = tw->composite.children[0];
1195 newhl = tw->composite.children[nc - 1];
1199 newhl = tw->tabs.hilight;
1204 XawTabsSetHighlight(w, newhl);
1208 TabsUnhighlight(Widget w, XEvent * event, String * params,
1209 Cardinal * num_params)
1211 TabsWidget tw = (TabsWidget) w;
1212 int nc = tw->composite.num_children;
1217 if (tw->tabs.hilight != NULL)
1218 DrawHighlight(tw, tw->tabs.hilight, True);
1221 /****************************************************************
1225 ****************************************************************/
1227 /* Set the top tab, optionally call all callbacks. */
1228 void XawTabsSetTop(Widget w, Bool callCallbacks)
1230 TabsWidget tw = (TabsWidget) w->core.parent;
1231 TabsConstraints tab;
1232 Widget oldtop = tw->tabs.topWidget;
1234 if (!XtIsSubclass(w->core.parent, tabsWidgetClass)) {
1237 "XawTabsSetTop: widget \"%.64s\" is not the child of a tabs widget.",
1239 XtAppWarning(XtWidgetToApplicationContext(w), line);
1244 XtCallCallbackList(w, tw->tabs.popdownCallbacks,
1245 (XtPointer) tw->tabs.topWidget);
1247 if (!XtIsRealized(w)) {
1248 tw->tabs.topWidget = w;
1249 tw->tabs.needs_layout = True;
1250 tw->tabs.hilight = NULL; /* The highlight tab might disappear. */
1254 XRaiseWindow(XtDisplay(w), XtWindow(w));
1256 XtVaSetValues(oldtop, XmNtraversalOn, False, 0);
1257 XtVaSetValues(w, XmNtraversalOn, True, 0);
1260 tab = (TabsConstraints) w->core.constraints;
1262 /* Unhighlight before we start messing with the stacking order. */
1263 if (tw->tabs.hilight != NULL) {
1264 DrawHighlight(tw, tw->tabs.hilight, True);
1265 tw->tabs.hilight = NULL;
1268 if (tab->tabs.row == 0) {
1269 /* Easy case; undraw current top, undraw new top, assign new
1270 * top, redraw all borders.
1271 * We *could* just erase and execute a full redraw, but I like to
1272 * reduce screen flicker.
1274 UndrawTab(tw, oldtop); /* undraw old */
1275 DrawBorder(tw, oldtop, True);
1276 UndrawTab(tw, w); /* undraw new */
1277 DrawBorder(tw, w, True);
1278 tw->tabs.topWidget = w;
1279 DrawTab(tw, oldtop, True); /* redraw old */
1280 DrawTab(tw, w, True); /* redraw new */
1281 DrawTabs(tw, False);
1283 tw->tabs.topWidget = w;
1284 TabsShuffleRows(tw);
1285 XClearWindow(XtDisplay((Widget) tw), XtWindow((Widget) tw));
1286 XtClass(tw)->core_class.expose((Widget) tw, NULL, None);
1289 XawTabsSetHighlight((Widget) tw, w);
1292 XtCallCallbackList(w, tw->tabs.callbacks, (XtPointer) w);
1295 /* Set the top tab, optionally call all callbacks. */
1296 void XawTabsSetHighlight(Widget t, Widget w)
1298 TabsWidget tw = (TabsWidget) t;
1300 if (!XtIsSubclass(t, tabsWidgetClass))
1303 if (XtIsRealized(t) && w != tw->tabs.hilight) {
1305 DrawHighlight(tw, w, False);
1308 tw->tabs.hilight = w;
1311 /****************************************************************
1313 * Private Procedures
1315 ****************************************************************/
1317 static void TabsAllocGCs(TabsWidget tw)
1320 TabsAllocGreyGC(tw);
1321 tw->tabs.backgroundGC = AllocBackgroundGC((Widget) tw, None);
1322 tw->tabs.topGC = AllocTopShadowGC((Widget) tw,
1323 tw->tabs.top_shadow_contrast,
1324 tw->tabs.be_nice_to_cmap);
1326 AllocBotShadowGC((Widget) tw, tw->tabs.bot_shadow_contrast,
1327 tw->tabs.be_nice_to_cmap);
1330 static void TabsFreeGCs(TabsWidget tw)
1332 Widget w = (Widget) tw;
1334 XtReleaseGC(w, tw->tabs.foregroundGC);
1335 XtReleaseGC(w, tw->tabs.greyGC);
1336 XtReleaseGC(w, tw->tabs.backgroundGC);
1337 XtReleaseGC(w, tw->tabs.topGC);
1338 XtReleaseGC(w, tw->tabs.botGC);
1340 XmuReleaseStippledPixmap(XtScreen(w), tw->tabs.grey50);
1344 /* Redraw entire Tabs widget */
1346 static void DrawTabs(TabsWidget tw, Bool labels)
1351 Dimension s = SHADWID;
1352 Dimension th = tw->tabs.tab_height;
1354 TabsConstraints tab;
1356 if (!XtIsRealized((Widget) tw))
1359 /* draw tabs and frames by row except for the top tab, which
1360 * is drawn last. (This is inefficiently written, but should not
1361 * be too slow as long as there are not a lot of rows.)
1364 y = tw->tabs.numRows == 1 ? TABDELTA : 0;
1365 for (i = 0; i < tw->tabs.numRows; ++i, y += th) {
1366 for (j = TabsNumChildren(tw), childP = tw->composite.children;
1368 if (TabVisible(*childP)) {
1370 (TabsConstraints) (*childP)->core.
1372 if (tab->tabs.row == (int)i
1373 && *childP != tw->tabs.topWidget)
1374 DrawTab(tw, *childP, labels);
1376 if (i != tw->tabs.numRows - 1)
1377 DrawTrim(tw, 0, y + th, tw->core.width, th + s, False,
1383 /* and now the top tab */
1384 if (tw->tabs.topWidget != NULL)
1385 DrawTab(tw, tw->tabs.topWidget, labels);
1388 /* Draw one tab. Corners are rounded very slightly. */
1390 static void DrawTab(TabsWidget tw, Widget child, Bool labels)
1395 if (!XtIsRealized((Widget) tw))
1398 DrawBorder(tw, child, False);
1401 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1402 Display *dpy = XtDisplay((Widget) tw);
1403 Window win = XtWindow((Widget) tw);
1404 String lbl = tab->tabs.label != NULL ?
1405 tab->tabs.label : XtName(child);
1407 if (XtIsSensitive(child)) {
1408 gc = tw->tabs.foregroundGC;
1409 XSetForeground(dpy, gc, tab->tabs.foreground);
1411 /* grey pixel allocation deferred until now */
1412 if (!tab->tabs.greyAlloc) {
1413 if (tw->tabs.be_nice_to_cmap
1414 || tw->core.depth == 1)
1415 tab->tabs.grey = tab->tabs.foreground;
1418 AllocGreyPixel((Widget) tw,
1419 tab->tabs.foreground,
1423 insensitive_contrast);
1424 tab->tabs.greyAlloc = True;
1426 gc = tw->tabs.greyGC;
1427 XSetForeground(dpy, gc, tab->tabs.grey);
1432 if (child == tw->tabs.topWidget)
1435 if (tab->tabs.left_bitmap != None && tab->tabs.lbm_width > 0) {
1436 if (tab->tabs.lbm_depth == 1)
1437 XCopyPlane(dpy, tab->tabs.left_bitmap, win, gc,
1438 0, 0, tab->tabs.lbm_width,
1439 tab->tabs.lbm_height,
1440 x + tab->tabs.lbm_x,
1441 y + tab->tabs.lbm_y, 1L);
1443 XCopyArea(dpy, tab->tabs.left_bitmap, win, gc,
1444 0, 0, tab->tabs.lbm_width,
1445 tab->tabs.lbm_height,
1446 x + tab->tabs.lbm_x,
1447 y + tab->tabs.lbm_y);
1450 if (lbl != NULL && tw->tabs.font != NULL)
1451 XDrawString(dpy, win, gc,
1452 x + tab->tabs.l_x, y + tab->tabs.l_y,
1453 lbl, (int)strlen(lbl));
1456 if (child == tw->tabs.hilight)
1457 DrawHighlight(tw, child, False);
1460 /* draw frame all the way around the child windows. */
1462 static void DrawFrame(TabsWidget tw)
1464 GC topgc = tw->tabs.topGC;
1465 GC botgc = tw->tabs.botGC;
1466 Dimension s = SHADWID;
1467 Dimension ch = tw->tabs.child_height;
1469 Draw3dBox((Widget) tw, 0, tw->tabs.tab_total,
1470 tw->core.width, ch + 2 * s, s, topgc, botgc);
1472 Widget w = tw->tabs.topWidget;
1474 TabsConstraints tab =
1475 (TabsConstraints) w->core.constraints;
1476 Draw3dBox((Widget) tw, 0, tw->core.height - 2 * s,
1477 tab->tabs.x, 2 * s, s, topgc, botgc);
1478 Draw3dBox((Widget) tw, tab->tabs.x + tab->tabs.width,
1479 tw->core.height - 2 * s,
1480 tw->core.width - tab->tabs.x -
1481 tab->tabs.width, 2 * s, s, topgc, botgc);
1483 Draw3dBox((Widget) tw, 0, tw->core.height - 2 * s,
1484 tw->core.width, 2 * s, s, topgc, botgc);
1488 /* draw trim around a tab or underneath a row of tabs */
1490 static void DrawTrim(TabsWidget tw, /* widget */
1491 int x, /* upper-left corner */
1492 int y, int wid, /* total size */
1493 int hgt, Bool bottom, /* draw bottom? */
1496 Display *dpy = XtDisplay((Widget) tw);
1497 Window win = XtWindow((Widget) tw);
1498 GC bggc = tw->tabs.backgroundGC;
1499 GC topgc = undraw ? bggc : tw->tabs.topGC;
1500 GC botgc = undraw ? bggc : tw->tabs.botGC;
1502 XDrawLine(dpy, win, bggc, x, y + hgt - 1, x + wid - 1, y + hgt - 1); /* bottom */
1503 XDrawLine(dpy, win, topgc, x, y + 2, x, y + hgt - 2); /* left */
1504 XDrawPoint(dpy, win, topgc, x + 1, y + 1); /* corner */
1505 XDrawLine(dpy, win, topgc, x + 2, y, x + wid - 3, y); /* top */
1506 XDrawLine(dpy, win, botgc, x + wid - 2, y + 1, x + wid - 2, y + hgt - 2); /* right */
1507 XDrawLine(dpy, win, botgc, x + wid - 1, y + 2, x + wid - 1, y + hgt - 2); /* right */
1510 /* Draw one tab border. */
1512 static void DrawBorder(TabsWidget tw, Widget child, Bool undraw)
1514 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1515 Position x = tab->tabs.x;
1516 Position y = tab->tabs.y;
1517 Dimension twid = tab->tabs.width;
1518 Dimension thgt = tw->tabs.tab_height;
1520 /* top tab requires a little special attention; it overlaps
1521 * neighboring tabs slightly, so the background must be cleared
1522 * in the region of the overlap to partially erase those neighbors.
1523 * TODO: is this worth doing with regions instead?
1525 if (child == tw->tabs.topWidget) {
1526 Display *dpy = XtDisplay((Widget) tw);
1527 Window win = XtWindow((Widget) tw);
1528 GC bggc = tw->tabs.backgroundGC;
1529 XRectangle rects[3];
1532 twid += TABDELTA * 2;
1534 AddRect(0, x, y + 1, twid, TABDELTA);
1535 AddRect(1, x + 1, y, TABDELTA, thgt);
1536 AddRect(2, x + twid - TABDELTA - 1, y, TABDELTA, thgt);
1537 XFillRectangles(dpy, win, bggc, rects, 3);
1540 DrawTrim(tw, x, y, twid, thgt + 1, child == tw->tabs.topWidget, undraw);
1543 /* Draw highlight around tab that has focus */
1545 static void DrawHighlight(TabsWidget tw, Widget child, Bool undraw)
1547 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1548 Display *dpy = XtDisplay((Widget) tw);
1549 Window win = XtWindow((Widget) tw);
1551 Position x = tab->tabs.x;
1552 Position y = tab->tabs.y;
1553 Dimension wid = tab->tabs.width;
1554 Dimension hgt = tw->tabs.tab_height;
1557 /* top tab does not have a highlight */
1559 if (child == tw->tabs.topWidget)
1563 gc = tw->tabs.backgroundGC;
1565 else if (XtIsSensitive(child)) {
1566 gc = tw->tabs.foregroundGC;
1567 XSetForeground(dpy, gc, tab->tabs.foreground);
1569 gc = tw->tabs.greyGC;
1570 XSetForeground(dpy, gc, tab->tabs.grey);
1573 points[0].x = x + 1;
1574 points[0].y = y + hgt - 1;
1575 points[1].x = x + 1;
1576 points[1].y = y + 2;
1577 points[2].x = x + 2;
1578 points[2].y = y + 1;
1579 points[3].x = x + wid - 4;
1580 points[3].y = y + 1;
1581 points[4].x = x + wid - 3;
1582 points[4].y = y + 2;
1583 points[5].x = x + wid - 3;
1584 points[5].y = y + hgt - 1;
1586 XDrawLines(dpy, win, gc, points, 6, CoordModeOrigin);
1589 /* Undraw one tab interior */
1591 static void UndrawTab(TabsWidget tw, Widget child)
1593 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1594 Position x = tab->tabs.x;
1595 Position y = tab->tabs.y;
1596 Dimension twid = tab->tabs.width;
1597 Dimension thgt = tw->tabs.tab_height;
1598 Display *dpy = XtDisplay((Widget) tw);
1599 Window win = XtWindow((Widget) tw);
1600 GC bggc = tw->tabs.backgroundGC;
1602 XFillRectangle(dpy, win, bggc, x, y, twid, thgt);
1605 /* GEOMETRY UTILITIES */
1609 * MaxChild(): ask all children (except possibly one) their
1610 * preferred sizes, set max_cw, max_ch accordingly.
1612 * GetPreferredSizes(): ask all children their preferred sizes,
1613 * set max_cw, max_ch accordingly.
1615 * PreferredSize(): given max_cw, max_ch, return tabs widget
1616 * preferred size. Iterate with other widths in order to get
1617 * a reasonable aspect ratio.
1619 * PreferredSize2(): Given child dimensions, return Tabs
1620 * widget dimensions.
1622 * PreferredSize3(): Same, except given child dimensions plus
1626 /* Compute the width of one child's tab. Positions will be computed
1629 * height: font height + vertical_space*2 + shadowWid*2
1630 * width: string width + horizontal_space*2 + shadowWid*2
1632 * All tabs are the same height, so that is computed elsewhere.
1635 static void TabWidth(Widget w)
1637 TabsConstraints tab = (TabsConstraints) w->core.constraints;
1638 TabsWidget tw = (TabsWidget) XtParent(w);
1639 String lbl = tab->tabs.label != NULL ? tab->tabs.label : XtName(w);
1640 XFontStruct *font = tw->tabs.font;
1641 int iw = tw->tabs.internalWidth;
1643 tab->tabs.width = iw + SHADWID * 2;
1644 tab->tabs.l_x = tab->tabs.lbm_x = SHADWID + iw;
1646 if (tab->tabs.left_bitmap != None) {
1647 tab->tabs.width += tab->tabs.lbm_width + iw;
1648 tab->tabs.l_x += tab->tabs.lbm_width + iw;
1650 (tw->tabs.tab_height - tab->tabs.lbm_height) / 2;
1653 if (lbl != NULL && font != NULL) {
1654 tab->tabs.width += XTextWidth(font, lbl, (int)strlen(lbl)) + iw;
1655 tab->tabs.l_y = (tw->tabs.tab_height +
1656 tw->tabs.font->max_bounds.ascent -
1657 tw->tabs.font->max_bounds.descent) / 2;
1661 /* Lay out tabs to fit in given width. Compute x,y position and
1662 * row number for each tab. Return number of rows and total height
1663 * required by all tabs. If there is only one row, add TABDELTA
1664 * height to the total. Rows are assigned bottom to top.
1666 * Tabs are indented from the edges by INDENT.
1668 * TODO: if they require more than two rows and the total height:width
1669 * ratio is more than 2:1, then try something else.
1673 TabLayout(TabsWidget tw,
1675 Dimension hgt, Dimension * reply_height, Bool query_only)
1677 int i, row, done = 0, display_rows = 0;
1678 int num_children = tw->composite.num_children;
1682 TabsConstraints tab;
1684 /* Algorithm: loop through children, assign X positions. If a tab
1685 * would extend beyond the right edge, start a new row. After all
1686 * rows are assigned, make a second pass and assign Y positions.
1689 if (num_children > 0) {
1690 /* Loop through the tabs and see how much space they need. */
1696 for (i = num_children, childP = tw->composite.children;
1698 if (XtIsManaged(*childP)) {
1700 (TabsConstraints) (*childP)->core.
1702 w = tab->tabs.width;
1704 if (x + w > wid) { /* new row */
1705 if (y + tw->tabs.tab_height > hgt
1712 y += tw->tabs.tab_height;
1717 tab->tabs.row = row;
1720 if (!query_only && !done)
1721 tab->tabs.visible = 1;
1724 /* If there was only one row, increase the height by TABDELTA */
1725 if (++display_rows == 1) {
1729 for (i = num_children, childP =
1730 tw->composite.children; --i >= 0; ++childP)
1731 if (XtIsManaged(*childP)) {
1734 (*childP)->core.constraints;
1738 y += tw->tabs.tab_height;
1740 display_rows = row = y = 0;
1743 tw->tabs.tab_total = y;
1744 tw->tabs.numRows = display_rows;
1745 tw->tabs.realRows = row;
1748 if (reply_height != NULL)
1751 return display_rows;
1754 /* Find max preferred child size. Returned sizes include child
1758 static void GetPreferredSizes(TabsWidget tw)
1760 MaxChild(tw, NULL, 0, 0);
1763 /* Find max preferred child size. Returned sizes include child
1764 * border widths. If except is non-null, don't ask that one.
1767 static void MaxChild(TabsWidget tw, Widget except, Dimension cw, Dimension ch)
1770 Widget *childP = tw->composite.children;
1771 XtWidgetGeometry preferred;
1773 for (i = tw->composite.num_children; --i >= 0; ++childP)
1774 if (TabVisible(*childP) /*XtIsManaged(*childP) */ &&*childP !=
1776 (void)XtQueryGeometry(*childP, NULL, &preferred);
1778 preferred.width + preferred.border_width * 2);
1780 preferred.height + preferred.border_width * 2);
1783 tw->tabs.max_cw = cw;
1784 tw->tabs.max_ch = ch;
1787 /* rotate row numbers to bring current widget to bottom row,
1788 * compute y positions for all tabs
1791 static void TabsShuffleRows(TabsWidget tw)
1793 TabsConstraints tab;
1795 int real_rows, display_rows;
1797 Dimension th = tw->tabs.tab_height;
1801 /* There must be a top widget. If not, assign one. */
1802 if (tw->tabs.topWidget == NULL && tw->composite.children != NULL)
1803 for (i = TabsNumChildren(tw), childP = tw->composite.children;
1805 if (XtIsManaged(*childP)) {
1806 tw->tabs.topWidget = *childP;
1810 if (tw->tabs.topWidget != NULL) {
1811 display_rows = tw->tabs.numRows;
1812 real_rows = tw->tabs.realRows;
1813 assert(display_rows <= real_rows);
1815 if (real_rows > 1) {
1817 (TabsConstraints) tw->tabs.topWidget->core.
1819 assert(tab != NULL);
1821 /* How far to move top row. The selected tab must be on
1822 the bottom row of the *visible* rows. */
1823 move = (real_rows + 1 - display_rows) - tab->tabs.row;
1825 move = real_rows - move;
1826 bottom = tw->tabs.tab_total - th;
1828 for (i = tw->composite.num_children, childP =
1829 tw->composite.children; --i >= 0; ++childP)
1830 if (XtIsManaged(*childP)) {
1832 (TabsConstraints) (*childP)->core.
1835 (tab->tabs.row + move) % real_rows;
1837 bottom - tab->tabs.row * th;
1839 (tab->tabs.row < display_rows);
1845 /* Find preferred size. Ask children, find size of largest,
1846 * add room for tabs & return. This can get a little involved,
1847 * as we don't want to have too many rows of tabs; we may widen
1848 * the widget to reduce # of rows.
1850 * This function requires that max_cw, max_ch already be set.
1852 static int PreferredSize(TabsWidget tw, Dimension * reply_width, /* total widget size */
1853 Dimension * reply_height, Dimension * reply_cw, /* child widget size */
1854 Dimension * reply_ch)
1856 Dimension cw, ch; /* child width, height */
1858 Dimension rwid, rhgt;
1861 wid = cw = tw->tabs.max_cw;
1862 hgt = ch = tw->tabs.max_ch;
1864 nrow = PreferredSize2(tw, wid, hgt, &rwid, &rhgt);
1866 /* Check for absurd results (more than 2 rows, high aspect
1867 * ratio). Try wider size if needed.
1868 * TODO: make sure this terminates.
1871 if (nrow > 2 && rhgt > rwid) {
1875 /* step 1: start doubling size until it's too big */
1878 wid = Max(wid * 2, wid + 20);
1879 nrow = PreferredSize2(tw, wid, hgt, &rwid, &rhgt);
1880 } while (nrow > 2 && rhgt > rwid);
1883 /* step 2: use Newton's method to find ideal size. Stop within
1886 while (--maxloop > 0 && w1 > w0 + 8) {
1887 wid = (w0 + w1) / 2;
1888 nrow = PreferredSize2(tw, wid, hgt, &rwid, &rhgt);
1889 if (nrow > 2 && rhgt > rwid)
1897 *reply_width = rwid;
1898 *reply_height = rhgt;
1899 if (reply_cw != NULL)
1901 if (reply_ch != NULL)
1906 /* Find preferred size, given size of children. */
1908 static int PreferredSize2(TabsWidget tw, Dimension cw, /* child width, height */
1909 Dimension ch, Dimension * reply_width, /* total widget size */
1910 Dimension * reply_height)
1912 Dimension s = SHADWID;
1915 /* make room for shadow frame */
1919 ret = PreferredSize3(tw, cw, ch, reply_width, reply_height);
1921 assert(*reply_width > 0 && *reply_height > 0);
1925 /* Find preferred size, given size of children+shadow. */
1927 static int PreferredSize3(TabsWidget tw, Dimension wid, /* child width, height */
1928 Dimension hgt, Dimension * reply_width, /* total widget size */
1929 Dimension * reply_height)
1931 Dimension th; /* space used by tabs */
1934 if (tw->composite.num_children > 0)
1935 nrows = TabLayout(tw, wid, hgt, &th, True);
1941 *reply_width = Max(wid, MIN_WID);
1942 *reply_height = Max(th + hgt, MIN_HGT);
1947 static void MakeSizeRequest(TabsWidget tw)
1949 Widget w = (Widget) tw;
1950 XtWidgetGeometry request, reply;
1951 XtGeometryResult result;
1954 request.request_mode = CWWidth | CWHeight;
1955 PreferredSize(tw, &request.width, &request.height, &cw, &ch);
1957 if (request.width == tw->core.width &&
1958 request.height == tw->core.height)
1961 result = XtMakeGeometryRequest(w, &request, &reply);
1963 if (result == XtGeometryAlmost) {
1964 /* Bugger. Didn't get what we want, but were offered a
1965 * compromise. If the width was too small, recompute
1966 * based on the too-small width and try again.
1967 * If the height was too small, make a wild-ass guess
1968 * at a wider width and try again.
1971 if (reply.width < request.width
1972 && reply.height >= request.height) {
1973 Dimension s = SHADWID;
1975 PreferredSize3(tw, reply.width, ch, &request.width,
1977 result = XtMakeGeometryRequest(w, &request, &reply);
1978 if (result == XtGeometryAlmost)
1979 (void)XtMakeGeometryRequest(w, &reply, NULL);
1983 (void)XtMakeGeometryRequest(w, &reply, NULL);
1987 static void getBitmapInfo(TabsWidget tw, TabsConstraints tab)
1993 if (tab->tabs.left_bitmap == None ||
1994 !XGetGeometry(XtDisplay(tw), tab->tabs.left_bitmap, &root, &x, &y,
1995 &tab->tabs.lbm_width, &tab->tabs.lbm_height,
1996 &bw, &tab->tabs.lbm_depth))
1997 tab->tabs.lbm_width = tab->tabs.lbm_height = 0;
2000 /* Code copied & modified from Gcs.c. This version has dynamic
2004 static void TabsAllocFgGC(TabsWidget tw)
2006 Widget w = (Widget) tw;
2009 values.background = tw->core.background_pixel;
2010 values.font = tw->tabs.font->fid;
2011 values.line_style = LineOnOffDash;
2012 values.line_style = LineSolid;
2014 tw->tabs.foregroundGC =
2015 XtAllocateGC(w, w->core.depth,
2016 GCBackground | GCFont | GCLineStyle, &values,
2018 GCSubwindowMode | GCGraphicsExposures | GCDashOffset |
2019 GCDashList | GCArcMode);
2022 static void TabsAllocGreyGC(TabsWidget tw)
2024 Widget w = (Widget) tw;
2027 values.background = tw->core.background_pixel;
2028 values.font = tw->tabs.font->fid;
2030 if (tw->tabs.be_nice_to_cmap || w->core.depth == 1) {
2031 values.fill_style = FillStippled;
2034 XmuCreateStippledPixmap(XtScreen(w), 1L, 0L, 1);
2037 XtAllocateGC(w, w->core.depth,
2038 GCBackground | GCFont | GCStipple |
2039 GCFillStyle, &values, GCForeground,
2040 GCSubwindowMode | GCGraphicsExposures |
2041 GCDashOffset | GCDashList | GCArcMode);
2046 XtAllocateGC(w, w->core.depth,
2049 GCBackground | GCSubwindowMode |
2050 GCGraphicsExposures | GCDashOffset | GCDashList