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)) {
1236 int n = snprintf(line, sizeof(line),
1237 "XawTabsSetTop: widget \"%.64s\" is not the child of a tabs widget.",
1239 assert(n >= 0 && (size_t)n < sizeof(line));
1240 XtAppWarning(XtWidgetToApplicationContext(w), line);
1245 XtCallCallbackList(w, tw->tabs.popdownCallbacks,
1246 (XtPointer) tw->tabs.topWidget);
1248 if (!XtIsRealized(w)) {
1249 tw->tabs.topWidget = w;
1250 tw->tabs.needs_layout = True;
1251 tw->tabs.hilight = NULL; /* The highlight tab might disappear. */
1255 XRaiseWindow(XtDisplay(w), XtWindow(w));
1257 XtVaSetValues(oldtop, XmNtraversalOn, False, 0);
1258 XtVaSetValues(w, XmNtraversalOn, True, 0);
1261 tab = (TabsConstraints) w->core.constraints;
1263 /* Unhighlight before we start messing with the stacking order. */
1264 if (tw->tabs.hilight != NULL) {
1265 DrawHighlight(tw, tw->tabs.hilight, True);
1266 tw->tabs.hilight = NULL;
1269 if (tab->tabs.row == 0) {
1270 /* Easy case; undraw current top, undraw new top, assign new
1271 * top, redraw all borders.
1272 * We *could* just erase and execute a full redraw, but I like to
1273 * reduce screen flicker.
1275 UndrawTab(tw, oldtop); /* undraw old */
1276 DrawBorder(tw, oldtop, True);
1277 UndrawTab(tw, w); /* undraw new */
1278 DrawBorder(tw, w, True);
1279 tw->tabs.topWidget = w;
1280 DrawTab(tw, oldtop, True); /* redraw old */
1281 DrawTab(tw, w, True); /* redraw new */
1282 DrawTabs(tw, False);
1284 tw->tabs.topWidget = w;
1285 TabsShuffleRows(tw);
1286 XClearWindow(XtDisplay((Widget) tw), XtWindow((Widget) tw));
1287 XtClass(tw)->core_class.expose((Widget) tw, NULL, None);
1290 XawTabsSetHighlight((Widget) tw, w);
1293 XtCallCallbackList(w, tw->tabs.callbacks, (XtPointer) w);
1296 /* Set the top tab, optionally call all callbacks. */
1297 void XawTabsSetHighlight(Widget t, Widget w)
1299 TabsWidget tw = (TabsWidget) t;
1301 if (!XtIsSubclass(t, tabsWidgetClass))
1304 if (XtIsRealized(t) && w != tw->tabs.hilight) {
1306 DrawHighlight(tw, w, False);
1309 tw->tabs.hilight = w;
1312 /****************************************************************
1314 * Private Procedures
1316 ****************************************************************/
1318 static void TabsAllocGCs(TabsWidget tw)
1321 TabsAllocGreyGC(tw);
1322 tw->tabs.backgroundGC = AllocBackgroundGC((Widget) tw, None);
1323 tw->tabs.topGC = AllocTopShadowGC((Widget) tw,
1324 tw->tabs.top_shadow_contrast,
1325 tw->tabs.be_nice_to_cmap);
1327 AllocBotShadowGC((Widget) tw, tw->tabs.bot_shadow_contrast,
1328 tw->tabs.be_nice_to_cmap);
1331 static void TabsFreeGCs(TabsWidget tw)
1333 Widget w = (Widget) tw;
1335 XtReleaseGC(w, tw->tabs.foregroundGC);
1336 XtReleaseGC(w, tw->tabs.greyGC);
1337 XtReleaseGC(w, tw->tabs.backgroundGC);
1338 XtReleaseGC(w, tw->tabs.topGC);
1339 XtReleaseGC(w, tw->tabs.botGC);
1341 XmuReleaseStippledPixmap(XtScreen(w), tw->tabs.grey50);
1345 /* Redraw entire Tabs widget */
1347 static void DrawTabs(TabsWidget tw, Bool labels)
1352 Dimension s = SHADWID;
1353 Dimension th = tw->tabs.tab_height;
1355 TabsConstraints tab;
1357 if (!XtIsRealized((Widget) tw))
1360 /* draw tabs and frames by row except for the top tab, which
1361 * is drawn last. (This is inefficiently written, but should not
1362 * be too slow as long as there are not a lot of rows.)
1365 y = tw->tabs.numRows == 1 ? TABDELTA : 0;
1366 for (i = 0; i < tw->tabs.numRows; ++i, y += th) {
1367 for (j = TabsNumChildren(tw), childP = tw->composite.children;
1369 if (TabVisible(*childP)) {
1371 (TabsConstraints) (*childP)->core.
1373 if (tab->tabs.row == (int)i
1374 && *childP != tw->tabs.topWidget)
1375 DrawTab(tw, *childP, labels);
1377 if (i != tw->tabs.numRows - 1)
1378 DrawTrim(tw, 0, y + th, tw->core.width, th + s, False,
1384 /* and now the top tab */
1385 if (tw->tabs.topWidget != NULL)
1386 DrawTab(tw, tw->tabs.topWidget, labels);
1389 /* Draw one tab. Corners are rounded very slightly. */
1391 static void DrawTab(TabsWidget tw, Widget child, Bool labels)
1396 if (!XtIsRealized((Widget) tw))
1399 DrawBorder(tw, child, False);
1402 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1403 Display *dpy = XtDisplay((Widget) tw);
1404 Window win = XtWindow((Widget) tw);
1405 String lbl = tab->tabs.label != NULL ?
1406 tab->tabs.label : XtName(child);
1408 if (XtIsSensitive(child)) {
1409 gc = tw->tabs.foregroundGC;
1410 XSetForeground(dpy, gc, tab->tabs.foreground);
1412 /* grey pixel allocation deferred until now */
1413 if (!tab->tabs.greyAlloc) {
1414 if (tw->tabs.be_nice_to_cmap
1415 || tw->core.depth == 1)
1416 tab->tabs.grey = tab->tabs.foreground;
1419 AllocGreyPixel((Widget) tw,
1420 tab->tabs.foreground,
1424 insensitive_contrast);
1425 tab->tabs.greyAlloc = True;
1427 gc = tw->tabs.greyGC;
1428 XSetForeground(dpy, gc, tab->tabs.grey);
1433 if (child == tw->tabs.topWidget)
1436 if (tab->tabs.left_bitmap != None && tab->tabs.lbm_width > 0) {
1437 if (tab->tabs.lbm_depth == 1)
1438 XCopyPlane(dpy, tab->tabs.left_bitmap, win, gc,
1439 0, 0, tab->tabs.lbm_width,
1440 tab->tabs.lbm_height,
1441 x + tab->tabs.lbm_x,
1442 y + tab->tabs.lbm_y, 1L);
1444 XCopyArea(dpy, tab->tabs.left_bitmap, win, gc,
1445 0, 0, tab->tabs.lbm_width,
1446 tab->tabs.lbm_height,
1447 x + tab->tabs.lbm_x,
1448 y + tab->tabs.lbm_y);
1451 if (lbl != NULL && tw->tabs.font != NULL)
1452 XDrawString(dpy, win, gc,
1453 x + tab->tabs.l_x, y + tab->tabs.l_y,
1454 lbl, (int)strlen(lbl));
1457 if (child == tw->tabs.hilight)
1458 DrawHighlight(tw, child, False);
1461 /* draw frame all the way around the child windows. */
1463 static void DrawFrame(TabsWidget tw)
1465 GC topgc = tw->tabs.topGC;
1466 GC botgc = tw->tabs.botGC;
1467 Dimension s = SHADWID;
1468 Dimension ch = tw->tabs.child_height;
1470 Draw3dBox((Widget) tw, 0, tw->tabs.tab_total,
1471 tw->core.width, ch + 2 * s, s, topgc, botgc);
1473 Widget w = tw->tabs.topWidget;
1475 TabsConstraints tab =
1476 (TabsConstraints) w->core.constraints;
1477 Draw3dBox((Widget) tw, 0, tw->core.height - 2 * s,
1478 tab->tabs.x, 2 * s, s, topgc, botgc);
1479 Draw3dBox((Widget) tw, tab->tabs.x + tab->tabs.width,
1480 tw->core.height - 2 * s,
1481 tw->core.width - tab->tabs.x -
1482 tab->tabs.width, 2 * s, s, topgc, botgc);
1484 Draw3dBox((Widget) tw, 0, tw->core.height - 2 * s,
1485 tw->core.width, 2 * s, s, topgc, botgc);
1489 /* draw trim around a tab or underneath a row of tabs */
1491 static void DrawTrim(TabsWidget tw, /* widget */
1492 int x, /* upper-left corner */
1493 int y, int wid, /* total size */
1494 int hgt, Bool bottom, /* draw bottom? */
1497 Display *dpy = XtDisplay((Widget) tw);
1498 Window win = XtWindow((Widget) tw);
1499 GC bggc = tw->tabs.backgroundGC;
1500 GC topgc = undraw ? bggc : tw->tabs.topGC;
1501 GC botgc = undraw ? bggc : tw->tabs.botGC;
1503 XDrawLine(dpy, win, bggc, x, y + hgt - 1, x + wid - 1, y + hgt - 1); /* bottom */
1504 XDrawLine(dpy, win, topgc, x, y + 2, x, y + hgt - 2); /* left */
1505 XDrawPoint(dpy, win, topgc, x + 1, y + 1); /* corner */
1506 XDrawLine(dpy, win, topgc, x + 2, y, x + wid - 3, y); /* top */
1507 XDrawLine(dpy, win, botgc, x + wid - 2, y + 1, x + wid - 2, y + hgt - 2); /* right */
1508 XDrawLine(dpy, win, botgc, x + wid - 1, y + 2, x + wid - 1, y + hgt - 2); /* right */
1511 /* Draw one tab border. */
1513 static void DrawBorder(TabsWidget tw, Widget child, Bool undraw)
1515 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1516 Position x = tab->tabs.x;
1517 Position y = tab->tabs.y;
1518 Dimension twid = tab->tabs.width;
1519 Dimension thgt = tw->tabs.tab_height;
1521 /* top tab requires a little special attention; it overlaps
1522 * neighboring tabs slightly, so the background must be cleared
1523 * in the region of the overlap to partially erase those neighbors.
1524 * TODO: is this worth doing with regions instead?
1526 if (child == tw->tabs.topWidget) {
1527 Display *dpy = XtDisplay((Widget) tw);
1528 Window win = XtWindow((Widget) tw);
1529 GC bggc = tw->tabs.backgroundGC;
1530 XRectangle rects[3];
1533 twid += TABDELTA * 2;
1535 AddRect(0, x, y + 1, twid, TABDELTA);
1536 AddRect(1, x + 1, y, TABDELTA, thgt);
1537 AddRect(2, x + twid - TABDELTA - 1, y, TABDELTA, thgt);
1538 XFillRectangles(dpy, win, bggc, rects, 3);
1541 DrawTrim(tw, x, y, twid, thgt + 1, child == tw->tabs.topWidget, undraw);
1544 /* Draw highlight around tab that has focus */
1546 static void DrawHighlight(TabsWidget tw, Widget child, Bool undraw)
1548 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1549 Display *dpy = XtDisplay((Widget) tw);
1550 Window win = XtWindow((Widget) tw);
1552 Position x = tab->tabs.x;
1553 Position y = tab->tabs.y;
1554 Dimension wid = tab->tabs.width;
1555 Dimension hgt = tw->tabs.tab_height;
1558 /* top tab does not have a highlight */
1560 if (child == tw->tabs.topWidget)
1564 gc = tw->tabs.backgroundGC;
1566 else if (XtIsSensitive(child)) {
1567 gc = tw->tabs.foregroundGC;
1568 XSetForeground(dpy, gc, tab->tabs.foreground);
1570 gc = tw->tabs.greyGC;
1571 XSetForeground(dpy, gc, tab->tabs.grey);
1574 points[0].x = x + 1;
1575 points[0].y = y + hgt - 1;
1576 points[1].x = x + 1;
1577 points[1].y = y + 2;
1578 points[2].x = x + 2;
1579 points[2].y = y + 1;
1580 points[3].x = x + wid - 4;
1581 points[3].y = y + 1;
1582 points[4].x = x + wid - 3;
1583 points[4].y = y + 2;
1584 points[5].x = x + wid - 3;
1585 points[5].y = y + hgt - 1;
1587 XDrawLines(dpy, win, gc, points, 6, CoordModeOrigin);
1590 /* Undraw one tab interior */
1592 static void UndrawTab(TabsWidget tw, Widget child)
1594 TabsConstraints tab = (TabsConstraints) child->core.constraints;
1595 Position x = tab->tabs.x;
1596 Position y = tab->tabs.y;
1597 Dimension twid = tab->tabs.width;
1598 Dimension thgt = tw->tabs.tab_height;
1599 Display *dpy = XtDisplay((Widget) tw);
1600 Window win = XtWindow((Widget) tw);
1601 GC bggc = tw->tabs.backgroundGC;
1603 XFillRectangle(dpy, win, bggc, x, y, twid, thgt);
1606 /* GEOMETRY UTILITIES */
1610 * MaxChild(): ask all children (except possibly one) their
1611 * preferred sizes, set max_cw, max_ch accordingly.
1613 * GetPreferredSizes(): ask all children their preferred sizes,
1614 * set max_cw, max_ch accordingly.
1616 * PreferredSize(): given max_cw, max_ch, return tabs widget
1617 * preferred size. Iterate with other widths in order to get
1618 * a reasonable aspect ratio.
1620 * PreferredSize2(): Given child dimensions, return Tabs
1621 * widget dimensions.
1623 * PreferredSize3(): Same, except given child dimensions plus
1627 /* Compute the width of one child's tab. Positions will be computed
1630 * height: font height + vertical_space*2 + shadowWid*2
1631 * width: string width + horizontal_space*2 + shadowWid*2
1633 * All tabs are the same height, so that is computed elsewhere.
1636 static void TabWidth(Widget w)
1638 TabsConstraints tab = (TabsConstraints) w->core.constraints;
1639 TabsWidget tw = (TabsWidget) XtParent(w);
1640 String lbl = tab->tabs.label != NULL ? tab->tabs.label : XtName(w);
1641 XFontStruct *font = tw->tabs.font;
1642 int iw = tw->tabs.internalWidth;
1644 tab->tabs.width = iw + SHADWID * 2;
1645 tab->tabs.l_x = tab->tabs.lbm_x = SHADWID + iw;
1647 if (tab->tabs.left_bitmap != None) {
1648 tab->tabs.width += tab->tabs.lbm_width + iw;
1649 tab->tabs.l_x += tab->tabs.lbm_width + iw;
1651 (tw->tabs.tab_height - tab->tabs.lbm_height) / 2;
1654 if (lbl != NULL && font != NULL) {
1655 tab->tabs.width += XTextWidth(font, lbl, (int)strlen(lbl)) + iw;
1656 tab->tabs.l_y = (tw->tabs.tab_height +
1657 tw->tabs.font->max_bounds.ascent -
1658 tw->tabs.font->max_bounds.descent) / 2;
1662 /* Lay out tabs to fit in given width. Compute x,y position and
1663 * row number for each tab. Return number of rows and total height
1664 * required by all tabs. If there is only one row, add TABDELTA
1665 * height to the total. Rows are assigned bottom to top.
1667 * Tabs are indented from the edges by INDENT.
1669 * TODO: if they require more than two rows and the total height:width
1670 * ratio is more than 2:1, then try something else.
1674 TabLayout(TabsWidget tw,
1676 Dimension hgt, Dimension * reply_height, Bool query_only)
1678 int i, row, done = 0, display_rows = 0;
1679 int num_children = tw->composite.num_children;
1683 TabsConstraints tab;
1685 /* Algorithm: loop through children, assign X positions. If a tab
1686 * would extend beyond the right edge, start a new row. After all
1687 * rows are assigned, make a second pass and assign Y positions.
1690 if (num_children > 0) {
1691 /* Loop through the tabs and see how much space they need. */
1697 for (i = num_children, childP = tw->composite.children;
1699 if (XtIsManaged(*childP)) {
1701 (TabsConstraints) (*childP)->core.
1703 w = tab->tabs.width;
1705 if (x + w > wid) { /* new row */
1706 if (y + tw->tabs.tab_height > hgt
1713 y += tw->tabs.tab_height;
1718 tab->tabs.row = row;
1721 if (!query_only && !done)
1722 tab->tabs.visible = 1;
1725 /* If there was only one row, increase the height by TABDELTA */
1726 if (++display_rows == 1) {
1730 for (i = num_children, childP =
1731 tw->composite.children; --i >= 0; ++childP)
1732 if (XtIsManaged(*childP)) {
1735 (*childP)->core.constraints;
1739 y += tw->tabs.tab_height;
1741 display_rows = row = y = 0;
1744 tw->tabs.tab_total = y;
1745 tw->tabs.numRows = display_rows;
1746 tw->tabs.realRows = row;
1749 if (reply_height != NULL)
1752 return display_rows;
1755 /* Find max preferred child size. Returned sizes include child
1759 static void GetPreferredSizes(TabsWidget tw)
1761 MaxChild(tw, NULL, 0, 0);
1764 /* Find max preferred child size. Returned sizes include child
1765 * border widths. If except is non-null, don't ask that one.
1768 static void MaxChild(TabsWidget tw, Widget except, Dimension cw, Dimension ch)
1771 Widget *childP = tw->composite.children;
1772 XtWidgetGeometry preferred;
1774 for (i = tw->composite.num_children; --i >= 0; ++childP)
1775 if (TabVisible(*childP) /*XtIsManaged(*childP) */ &&*childP !=
1777 (void)XtQueryGeometry(*childP, NULL, &preferred);
1779 preferred.width + preferred.border_width * 2);
1781 preferred.height + preferred.border_width * 2);
1784 tw->tabs.max_cw = cw;
1785 tw->tabs.max_ch = ch;
1788 /* rotate row numbers to bring current widget to bottom row,
1789 * compute y positions for all tabs
1792 static void TabsShuffleRows(TabsWidget tw)
1794 TabsConstraints tab;
1796 int real_rows, display_rows;
1798 Dimension th = tw->tabs.tab_height;
1802 /* There must be a top widget. If not, assign one. */
1803 if (tw->tabs.topWidget == NULL && tw->composite.children != NULL)
1804 for (i = TabsNumChildren(tw), childP = tw->composite.children;
1806 if (XtIsManaged(*childP)) {
1807 tw->tabs.topWidget = *childP;
1811 if (tw->tabs.topWidget != NULL) {
1812 display_rows = tw->tabs.numRows;
1813 real_rows = tw->tabs.realRows;
1814 assert(display_rows <= real_rows);
1816 if (real_rows > 1) {
1818 (TabsConstraints) tw->tabs.topWidget->core.
1820 assert(tab != NULL);
1822 /* How far to move top row. The selected tab must be on
1823 the bottom row of the *visible* rows. */
1824 move = (real_rows + 1 - display_rows) - tab->tabs.row;
1826 move = real_rows - move;
1827 bottom = tw->tabs.tab_total - th;
1829 for (i = tw->composite.num_children, childP =
1830 tw->composite.children; --i >= 0; ++childP)
1831 if (XtIsManaged(*childP)) {
1833 (TabsConstraints) (*childP)->core.
1836 (tab->tabs.row + move) % real_rows;
1838 bottom - tab->tabs.row * th;
1840 (tab->tabs.row < display_rows);
1846 /* Find preferred size. Ask children, find size of largest,
1847 * add room for tabs & return. This can get a little involved,
1848 * as we don't want to have too many rows of tabs; we may widen
1849 * the widget to reduce # of rows.
1851 * This function requires that max_cw, max_ch already be set.
1853 static int PreferredSize(TabsWidget tw, Dimension * reply_width, /* total widget size */
1854 Dimension * reply_height, Dimension * reply_cw, /* child widget size */
1855 Dimension * reply_ch)
1857 Dimension cw, ch; /* child width, height */
1859 Dimension rwid, rhgt;
1862 wid = cw = tw->tabs.max_cw;
1863 hgt = ch = tw->tabs.max_ch;
1865 nrow = PreferredSize2(tw, wid, hgt, &rwid, &rhgt);
1867 /* Check for absurd results (more than 2 rows, high aspect
1868 * ratio). Try wider size if needed.
1869 * TODO: make sure this terminates.
1872 if (nrow > 2 && rhgt > rwid) {
1876 /* step 1: start doubling size until it's too big */
1879 wid = Max(wid * 2, wid + 20);
1880 nrow = PreferredSize2(tw, wid, hgt, &rwid, &rhgt);
1881 } while (nrow > 2 && rhgt > rwid);
1884 /* step 2: use Newton's method to find ideal size. Stop within
1887 while (--maxloop > 0 && w1 > w0 + 8) {
1888 wid = (w0 + w1) / 2;
1889 nrow = PreferredSize2(tw, wid, hgt, &rwid, &rhgt);
1890 if (nrow > 2 && rhgt > rwid)
1898 *reply_width = rwid;
1899 *reply_height = rhgt;
1900 if (reply_cw != NULL)
1902 if (reply_ch != NULL)
1907 /* Find preferred size, given size of children. */
1909 static int PreferredSize2(TabsWidget tw, Dimension cw, /* child width, height */
1910 Dimension ch, Dimension * reply_width, /* total widget size */
1911 Dimension * reply_height)
1913 Dimension s = SHADWID;
1916 /* make room for shadow frame */
1920 ret = PreferredSize3(tw, cw, ch, reply_width, reply_height);
1922 assert(*reply_width > 0 && *reply_height > 0);
1926 /* Find preferred size, given size of children+shadow. */
1928 static int PreferredSize3(TabsWidget tw, Dimension wid, /* child width, height */
1929 Dimension hgt, Dimension * reply_width, /* total widget size */
1930 Dimension * reply_height)
1932 Dimension th; /* space used by tabs */
1935 if (tw->composite.num_children > 0)
1936 nrows = TabLayout(tw, wid, hgt, &th, True);
1942 *reply_width = Max(wid, MIN_WID);
1943 *reply_height = Max(th + hgt, MIN_HGT);
1948 static void MakeSizeRequest(TabsWidget tw)
1950 Widget w = (Widget) tw;
1951 XtWidgetGeometry request, reply;
1952 XtGeometryResult result;
1955 request.request_mode = CWWidth | CWHeight;
1956 PreferredSize(tw, &request.width, &request.height, &cw, &ch);
1958 if (request.width == tw->core.width &&
1959 request.height == tw->core.height)
1962 result = XtMakeGeometryRequest(w, &request, &reply);
1964 if (result == XtGeometryAlmost) {
1965 /* Bugger. Didn't get what we want, but were offered a
1966 * compromise. If the width was too small, recompute
1967 * based on the too-small width and try again.
1968 * If the height was too small, make a wild-ass guess
1969 * at a wider width and try again.
1972 if (reply.width < request.width
1973 && reply.height >= request.height) {
1974 Dimension s = SHADWID;
1976 PreferredSize3(tw, reply.width, ch, &request.width,
1978 result = XtMakeGeometryRequest(w, &request, &reply);
1979 if (result == XtGeometryAlmost)
1980 (void)XtMakeGeometryRequest(w, &reply, NULL);
1984 (void)XtMakeGeometryRequest(w, &reply, NULL);
1988 static void getBitmapInfo(TabsWidget tw, TabsConstraints tab)
1994 if (tab->tabs.left_bitmap == None ||
1995 !XGetGeometry(XtDisplay(tw), tab->tabs.left_bitmap, &root, &x, &y,
1996 &tab->tabs.lbm_width, &tab->tabs.lbm_height,
1997 &bw, &tab->tabs.lbm_depth))
1998 tab->tabs.lbm_width = tab->tabs.lbm_height = 0;
2001 /* Code copied & modified from Gcs.c. This version has dynamic
2005 static void TabsAllocFgGC(TabsWidget tw)
2007 Widget w = (Widget) tw;
2010 values.background = tw->core.background_pixel;
2011 values.font = tw->tabs.font->fid;
2012 values.line_style = LineOnOffDash;
2013 values.line_style = LineSolid;
2015 tw->tabs.foregroundGC =
2016 XtAllocateGC(w, w->core.depth,
2017 GCBackground | GCFont | GCLineStyle, &values,
2019 GCSubwindowMode | GCGraphicsExposures | GCDashOffset |
2020 GCDashList | GCArcMode);
2023 static void TabsAllocGreyGC(TabsWidget tw)
2025 Widget w = (Widget) tw;
2028 values.background = tw->core.background_pixel;
2029 values.font = tw->tabs.font->fid;
2031 if (tw->tabs.be_nice_to_cmap || w->core.depth == 1) {
2032 values.fill_style = FillStippled;
2035 XmuCreateStippledPixmap(XtScreen(w), 1L, 0L, 1);
2038 XtAllocateGC(w, w->core.depth,
2039 GCBackground | GCFont | GCStipple |
2040 GCFillStyle, &values, GCForeground,
2041 GCSubwindowMode | GCGraphicsExposures |
2042 GCDashOffset | GCDashList | GCArcMode);
2047 XtAllocateGC(w, w->core.depth,
2050 GCBackground | GCSubwindowMode |
2051 GCGraphicsExposures | GCDashOffset | GCDashList