1 /* Tabs Widget for XEmacs.
2 Copyright (C) 1999 Edward A. Falk
4 This file is part of XEmacs.
6 XEmacs 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 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: Tabs.c 1.27 */
24 * Tabs.c - Index Tabs composite widget
26 * Author: Edward A. Falk
27 * falk@falconer.vip.best.com
32 * Overall layout of this widget is as follows:
34 * ________ ,---------. _________
35 * | label || Label || Label | \ tabs
36 * |________|| ||_________| /
37 * |+----------------------------+| \
39 * || child widget window || > frame
40 * |+----------------------------+| |
41 * +------------------------------+ /
43 * The height of the tabs includes the shadow width, top and bottom
44 * margins, and the height of the text.
46 * The height of the frame includes the top and bottom shadow width and the
47 * size of the child widget window.
49 * The tabs overlap the frame and each other vertically by the shadow
50 * width, so that when the topmost tab is drawn, it obliterates part of
55 * TODO: min child height = tab height
62 #include <X11/IntrinsicP.h>
63 #include <X11/StringDefs.h>
64 #include "../src/xmu.h"
70 #define INDENT 3 /* tabs indented from edge by this much */
71 #define SPACING 0 /* distance between tabs */
72 #define SHADWID 1 /* default shadow width */
73 #define TABDELTA 2 /* top tab grows this many pixels */
74 #define TABLDELTA 2 /* top tab label offset this many pixels */
77 /****************************************************************
81 ****************************************************************/
83 static char defaultTranslations[] = "\
85 <FocusIn>: highlight() \n\
86 <FocusOut>: unhighlight() \n\
87 <Key>Page_Up: page(up) \n\
88 <Key>KP_Page_Up: page(up) \n\
89 <Key>Prior: page(up) \n\
90 <Key>KP_Prior: page(up) \n\
91 <Key>Page_Down: page(down) \n\
92 <Key>KP_Page_Down: page(down) \n\
93 <Key>Next: page(down) \n\
94 <Key>KP_Next: page(down) \n\
95 <Key>Home: page(home) \n\
96 <Key>KP_Home: page(home) \n\
97 <Key>End: page(end) \n\
98 <Key>KP_End: page(end) \n\
99 <Key>Up: highlight(up) \n\
100 <Key>KP_Up: highlight(up) \n\
101 <Key>Down: highlight(down) \n\
102 <Key>KP_Down: highlight(down) \n\
103 <Key> : page(select) \n\
106 static char accelTable[] = " #augment\n\
107 <Key>Page_Up: page(up) \n\
108 <Key>KP_Page_Up: page(up) \n\
109 <Key>Prior: page(up) \n\
110 <Key>KP_Prior: page(up) \n\
111 <Key>Page_Down: page(down) \n\
112 <Key>KP_Page_Down: page(down) \n\
113 <Key>Next: page(down) \n\
114 <Key>KP_Next: page(down) \n\
115 <Key>Home: page(home) \n\
116 <Key>KP_Home: page(home) \n\
117 <Key>End: page(end) \n\
118 <Key>KP_End: page(end) \n\
119 <Key>Up: highlight(up) \n\
120 <Key>KP_Up: highlight(up) \n\
121 <Key>Down: highlight(down) \n\
122 <Key>KP_Down: highlight(down) \n\
123 <Key> : page(select) \n\
125 static XtAccelerators defaultAccelerators ;
127 #define offset(field) XtOffsetOf(TabsRec, tabs.field)
128 static XtResource resources[] = {
130 {XtNselectInsensitive, XtCSelectInsensitive, XtRBoolean, sizeof(Boolean),
131 offset(selectInsensitive), XtRImmediate, (XtPointer) True},
132 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
133 offset(font), XtRString, (XtPointer) XtDefaultFont},
134 {XtNinternalWidth, XtCWidth, XtRDimension, sizeof(Dimension),
135 offset(internalWidth), XtRImmediate, (XtPointer)4 },
136 {XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
137 offset(internalHeight), XtRImmediate, (XtPointer)4 },
138 {XtNborderWidth, XtCBorderWidth, XtRDimension, sizeof(Dimension),
139 XtOffsetOf(RectObjRec,rectangle.border_width), XtRImmediate, (XtPointer)0},
140 {XtNtopWidget, XtCTopWidget, XtRWidget, sizeof(Widget),
141 offset(topWidget), XtRImmediate, NULL},
142 {XtNcallback, XtCCallback, XtRCallback, sizeof(XtPointer),
143 offset(callbacks), XtRCallback, NULL},
144 {XtNpopdownCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
145 offset(popdownCallbacks), XtRCallback, NULL},
146 {XtNbeNiceToColormap, XtCBeNiceToColormap, XtRBoolean, sizeof(Boolean),
147 offset(be_nice_to_cmap), XtRImmediate, (XtPointer) True},
148 {XtNtopShadowContrast, XtCTopShadowContrast, XtRInt, sizeof(int),
149 offset(top_shadow_contrast), XtRImmediate, (XtPointer) 20},
150 {XtNbottomShadowContrast, XtCBottomShadowContrast, XtRInt, sizeof(int),
151 offset(bot_shadow_contrast), XtRImmediate, (XtPointer) 40},
152 {XtNinsensitiveContrast, XtCInsensitiveContrast, XtRInt, sizeof(int),
153 offset(insensitive_contrast), XtRImmediate, (XtPointer) 33},
154 {XtNaccelerators, XtCAccelerators, XtRAcceleratorTable,sizeof(XtTranslations),
155 XtOffsetOf(TabsRec,core.accelerators), XtRString, accelTable},
161 /* constraint resources */
163 #define offset(field) XtOffsetOf(TabsConstraintsRec, tabs.field)
164 static XtResource tabsConstraintResources[] = {
165 {XtNtabLabel, XtCLabel, XtRString, sizeof(String),
166 offset(label), XtRString, NULL},
167 {XtNtabLeftBitmap, XtCLeftBitmap, XtRBitmap, sizeof(Pixmap),
168 offset(left_bitmap), XtRImmediate, None},
169 {XtNtabForeground, XtCForeground, XtRPixel, sizeof(Pixel),
170 offset(foreground), XtRString, (XtPointer) XtDefaultForeground},
171 {XtNresizable, XtCResizable, XtRBoolean, sizeof(Boolean),
172 offset(resizable), XtRImmediate, (XtPointer) True},
179 #if !NeedFunctionPrototypes
181 /* FORWARD REFERENCES: */
183 /* member functions */
185 static void TabsClassInit();
186 static void TabsInit();
187 static void TabsResize();
188 static void TabsExpose();
189 static void TabsDestroy();
190 static void TabsRealize();
191 static Boolean TabsSetValues();
192 static Boolean TabsAcceptFocus();
193 static XtGeometryResult TabsQueryGeometry();
194 static XtGeometryResult TabsGeometryManager();
195 static void TabsChangeManaged();
196 static void TabsConstraintInitialize() ;
197 static Boolean TabsConstraintSetValues() ;
201 static void TabsSelect() ;
202 static void TabsPage() ;
203 static void TabsHighlight() ;
204 static void TabsUnhighlight() ;
206 /* internal privates */
208 static void TabsAllocGCs() ; /* get rendering GCs */
209 static void TabsFreeGCs() ; /* return rendering GCs */
210 static void DrawTabs() ; /* draw all tabs */
211 static void DrawTab() ; /* draw one index tab */
212 static void DrawFrame() ; /* draw frame around contents */
213 static void DrawTrim() ; /* draw trim around a tab */
214 static void DrawBorder() ; /* draw border */
215 static void DrawHighlight() ; /* draw highlight */
216 static void UndrawTab() ; /* undraw interior of a tab */
217 static void TabWidth() ; /* recompute tab size */
218 static void GetPreferredSizes() ; /* query all children for their sizes */
219 static void MaxChild() ; /* find max preferred child size */
220 static int PreferredSize() ; /* compute preferred size */
221 static int PreferredSize2() ; /* compute preferred size */
222 static int PreferredSize3() ; /* compute preferred size */
223 static void MakeSizeRequest() ; /* try to change size */
224 static void getBitmapInfo() ;
225 static int TabLayout() ; /* lay out tabs */
226 static void TabsShuffleRows() ; /* bring current tab to bottom row */
228 static void TabsAllocFgGC() ;
229 static void TabsAllocGreyGC() ;
233 static void TabsClassInit(void) ;
234 static void TabsInit( Widget req, Widget new, ArgList, Cardinal *nargs) ;
235 static void TabsConstraintInitialize(Widget, Widget, ArgList, Cardinal *) ;
236 static void TabsRealize(Widget, Mask *, XSetWindowAttributes *) ;
237 static void TabsDestroy( Widget w) ;
238 static void TabsResize( Widget w) ;
239 static void TabsExpose( Widget w, XEvent *event, Region region) ;
240 static Boolean TabsSetValues(Widget, Widget, Widget, ArgList, Cardinal *) ;
241 static Boolean TabsAcceptFocus(Widget, Time *);
242 static Boolean TabsConstraintSetValues(Widget, Widget, Widget,
243 ArgList, Cardinal *) ;
244 static XtGeometryResult TabsQueryGeometry(Widget,
245 XtWidgetGeometry *, XtWidgetGeometry *) ;
246 static XtGeometryResult TabsGeometryManager(Widget,
247 XtWidgetGeometry *, XtWidgetGeometry *) ;
248 static void TabsChangeManaged( Widget w) ;
250 static void TabsSelect(Widget, XEvent *, String *, Cardinal *) ;
251 static void TabsPage(Widget, XEvent *, String *, Cardinal *) ;
252 static void TabsHighlight(Widget, XEvent *, String *, Cardinal *) ;
253 static void TabsUnhighlight(Widget, XEvent *, String *, Cardinal *) ;
255 static void DrawTabs( TabsWidget tw, Bool labels) ;
256 static void DrawTab( TabsWidget tw, Widget child, Bool labels) ;
257 static void DrawFrame( TabsWidget tw) ;
258 static void DrawTrim( TabsWidget, int x, int y,
259 int wid, int hgt, Bool bottom, Bool undraw) ;
260 static void DrawBorder( TabsWidget tw, Widget child, Bool undraw) ;
261 static void DrawHighlight( TabsWidget tw, Widget child, Bool undraw) ;
262 static void UndrawTab( TabsWidget tw, Widget child) ;
264 static void TabWidth( Widget w) ;
265 static int TabLayout( TabsWidget, int wid, int hgt, Dimension *r_hgt,
267 static void GetPreferredSizes(TabsWidget) ;
268 static void MaxChild(TabsWidget, Widget except, Dimension, Dimension) ;
269 static void TabsShuffleRows( TabsWidget tw) ;
270 static int PreferredSize( TabsWidget,
271 Dimension *reply_width, Dimension *reply_height,
272 Dimension *reply_cw, Dimension *reply_ch) ;
273 static int PreferredSize2( TabsWidget, int cw, int ch,
274 Dimension *rw, Dimension *rh) ;
275 static int PreferredSize3( TabsWidget, int wid, int hgt,
276 Dimension *rw, Dimension *rh) ;
277 static void MakeSizeRequest(TabsWidget) ;
279 static void TabsAllocGCs(TabsWidget) ;
280 static void TabsFreeGCs(TabsWidget) ;
281 static void getBitmapInfo( TabsWidget tw, TabsConstraints tab) ;
282 static void TabsAllocFgGC( TabsWidget tw) ;
283 static void TabsAllocGreyGC( TabsWidget tw) ;
287 #define AddRect(i,xx,yy,w,h) \
288 do{rects[(i)].x=(xx); rects[i].y=(yy); \
289 rects[i].width=(w); rects[i].height=(h);}while(0)
291 static XtActionsRec actionsList[] =
293 {"select", TabsSelect},
295 {"highlight", TabsHighlight},
296 {"unhighlight", TabsUnhighlight},
300 /****************************************************************
302 * Full class record constant
304 ****************************************************************/
307 #define SuperClass (&constraintClassRec)
309 #define SuperClass (&xmManagerClassRec)
312 TabsClassRec tabsClassRec = {
314 /* core_class fields */
315 /* superclass */ (WidgetClass) SuperClass,
316 /* class_name */ "Tabs",
317 /* widget_size */ sizeof(TabsRec),
318 /* class_initialize */ TabsClassInit,
319 /* class_part_init */ NULL, /* TODO? */
320 /* class_inited */ FALSE,
321 /* initialize */ TabsInit,
322 /* initialize_hook */ NULL,
323 /* realize */ TabsRealize,
324 /* actions */ actionsList,
325 /* num_actions */ XtNumber(actionsList),
326 /* resources */ resources,
327 /* num_resources */ XtNumber(resources),
328 /* xrm_class */ NULLQUARK,
329 /* compress_motion */ TRUE,
330 #if XtSpecificationRelease < 6
331 /* compress_exposure */ XtExposeCompressMaximal,
333 /* compress_exposure */ XtExposeCompressMaximal|XtExposeNoRegion,
335 /* compress_enterleave*/ TRUE,
336 /* visible_interest */ TRUE,
337 /* destroy */ TabsDestroy,
338 /* resize */ TabsResize,
339 /* expose */ TabsExpose,
340 /* set_values */ TabsSetValues,
341 /* set_values_hook */ NULL,
342 /* set_values_almost */ XtInheritSetValuesAlmost,
343 /* get_values_hook */ NULL,
344 /* accept_focus */ TabsAcceptFocus,
345 /* version */ XtVersion,
346 /* callback_private */ NULL,
347 /* tm_table */ defaultTranslations,
348 /* query_geometry */ TabsQueryGeometry,
349 /* display_accelerator*/ XtInheritDisplayAccelerator,
353 /* composite_class fields */
354 /* geometry_manager */ TabsGeometryManager,
355 /* change_managed */ TabsChangeManaged,
356 /* insert_child */ XtInheritInsertChild, /* TODO? */
357 /* delete_child */ XtInheritDeleteChild, /* TODO? */
361 /* constraint_class fields */
362 /* subresources */ tabsConstraintResources,
363 /* subresource_count */ XtNumber(tabsConstraintResources),
364 /* constraint_size */ sizeof(TabsConstraintsRec),
365 /* initialize */ TabsConstraintInitialize,
367 /* set_values */ TabsConstraintSetValues,
368 /* extension */ NULL,
371 /* Manager Class fields */
373 /* translations */ NULL,
374 /* syn_resources */ NULL,
375 /* num_syn_resources */ 0,
376 /* syn_constraint_resources */ NULL,
377 /* num_syn_constraint_resources */ 0,
378 /* parent_process */ XmInheritParentProcess,
383 /* Tabs class fields */
384 /* extension */ NULL,
388 WidgetClass tabsWidgetClass = (WidgetClass)&tabsClassRec;
395 if(!(e)) fprintf(stderr,"yak! %s at %s:%d\n",#e,__FILE__,__LINE__)
398 if(!(e)) fprintf(stderr,"yak! e at %s:%d\n",__FILE__,__LINE__)
407 /****************************************************************
411 ****************************************************************/
416 defaultAccelerators = XtParseAcceleratorTable(accelTable) ;
417 /* TODO: register converter for labels? */
422 /* Init a newly created tabs widget. Compute height of tabs
423 * and optionally compute size of widget. */
428 TabsInit(Widget request, Widget new, ArgList args, Cardinal *num_args)
430 TabsWidget newTw = (TabsWidget)new;
432 newTw->tabs.numRows = 0 ;
433 newTw->tabs.displayChildren = 0;
435 GetPreferredSizes(newTw) ;
437 /* height is easy, it's the same for all tabs:
438 * TODO: font height + height of tallest bitmap.
440 newTw->tabs.tab_height = 2 * newTw->tabs.internalHeight + SHADWID ;
442 if( newTw->tabs.font != NULL )
443 newTw->tabs.tab_height += newTw->tabs.font->max_bounds.ascent +
444 newTw->tabs.font->max_bounds.descent ;
446 /* GC allocation is deferred until XtRealize() */
448 /* if size not explicitly set, set it to our preferred size now. */
450 if( request->core.width == 0 || request->core.height == 0 )
453 PreferredSize(newTw, &w, &h, NULL,NULL) ;
454 if( request->core.width == 0 ) new->core.width = w ;
455 if( request->core.height == 0 ) new->core.height = h ;
456 XtClass(new)->core_class.resize(new) ;
459 /* defer GC allocation, etc., until Realize() time. */
460 newTw->tabs.foregroundGC =
461 newTw->tabs.backgroundGC =
464 newTw->tabs.botGC = None ;
466 newTw->tabs.grey50 = None ;
468 newTw->tabs.needs_layout = False ;
470 newTw->tabs.hilight = NULL ;
473 newTw->manager.navigation_type = XmTAB_GROUP ;
474 newTw->manager.traversal_on = True ;
479 /* Init the constraint part of a new tab child. Compute the
484 TabsConstraintInitialize(Widget request, Widget new,
485 ArgList args, Cardinal *num_args)
487 TabsConstraints tab = (TabsConstraints) new->core.constraints ;
488 tab->tabs.greyAlloc = False ; /* defer allocation of pixel */
490 getBitmapInfo((TabsWidget)XtParent(new), tab) ;
496 /* Called when tabs widget first realized. Create the window
497 * and allocate the GCs
501 TabsRealize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
503 TabsWidget tw = (TabsWidget) w;
505 attributes->bit_gravity = NorthWestGravity;
506 *valueMask |= CWBitGravity;
508 SuperClass->core_class.realize(w, valueMask, attributes);
516 TabsDestroy(Widget w)
518 TabsFreeGCs((TabsWidget)w) ;
522 /* Parent has resized us. This will require that the tabs be
529 TabsWidget tw = (TabsWidget) w;
531 int num_children = tw->composite.num_children ;
533 TabsConstraints tab ;
536 /* Our size has now been dictated by the parent. Lay out the
537 * tabs, lay out the frame, lay out the children. Remember
538 * that the tabs overlap each other and the frame by shadowWidth.
539 * Also, the top tab is larger than the others, so if there's only
540 * one row, the widget must be made taller to accommodate this.
542 * Once the tabs are laid out, if there is more than one
543 * row, we may need to shuffle the rows to bring the top tab
547 tw->tabs.needs_layout = False ;
549 if( num_children > 0 && tw->composite.children != NULL )
551 /* Loop through the tabs and assign rows & x positions */
552 (void) TabLayout(tw, tw->core.width, tw->core.height, NULL, False) ;
553 num_children = tw->tabs.displayChildren;
555 /* assign a top widget, bring it to bottom row. */
556 TabsShuffleRows(tw) ;
558 /* now assign child positions & sizes. Positions are all the
559 * same: just inside the frame. Sizes are also all the same.
562 tw->tabs.child_width = cw = tw->core.width - 2 * SHADWID ;
563 tw->tabs.child_height = ch =
564 tw->core.height - tw->tabs.tab_total - 2 * SHADWID ;
567 for(i=0, childP=tw->composite.children;
570 if( XtIsManaged(*childP) )
572 tab = (TabsConstraints) (*childP)->core.constraints ;
573 bw = (*childP)->core.border_width ;
574 XtConfigureWidget(*childP, SHADWID,tw->tabs.tab_total+SHADWID,
575 cw-bw*2,ch-bw*2, bw) ;
577 if( XtIsRealized(w) ) {
578 XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
579 /* should not be necessary to explicitly repaint after a
580 * resize, but XEmacs folks tell me it is.
582 XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
589 /* Redraw entire Tabs widget */
593 TabsExpose(Widget w, XEvent *event, Region region)
595 TabsWidget tw = (TabsWidget) w;
597 if( tw->tabs.needs_layout )
598 XtClass(w)->core_class.resize(w) ;
604 /* Called when any Tabs widget resources are changed. */
608 TabsSetValues(Widget current, Widget request, Widget new,
609 ArgList args, Cardinal *num_args)
611 TabsWidget curtw = (TabsWidget) current ;
612 TabsWidget tw = (TabsWidget) new ;
613 Boolean needRedraw = False ;
618 if( tw->tabs.font != curtw->tabs.font ||
619 tw->tabs.internalWidth != curtw->tabs.internalWidth ||
620 tw->tabs.internalHeight != curtw->tabs.internalHeight )
622 tw->tabs.tab_height = 2 * tw->tabs.internalHeight + SHADWID ;
624 if( tw->tabs.font != NULL )
625 tw->tabs.tab_height += tw->tabs.font->max_bounds.ascent +
626 tw->tabs.font->max_bounds.descent ;
628 /* Tab size has changed. Resize all tabs and request a new size */
629 for(i=0, childP=tw->composite.children;
630 i < tw->composite.num_children;
632 if( XtIsManaged(*childP) )
634 PreferredSize(tw, &tw->core.width, &tw->core.height, NULL,NULL) ;
636 tw->tabs.needs_layout = True ;
639 /* TODO: if any color changes, need to recompute GCs and redraw */
641 if( tw->core.background_pixel != curtw->core.background_pixel ||
642 tw->core.background_pixmap != curtw->core.background_pixmap ||
643 tw->tabs.font != curtw->tabs.font )
644 if( XtIsRealized(new) )
651 if( tw->core.sensitive != curtw->core.sensitive )
654 /* If top widget changes, need to change stacking order, redraw tabs.
655 * Window system will handle the redraws.
658 if( tw->tabs.topWidget != curtw->tabs.topWidget )
660 if( XtIsRealized(tw->tabs.topWidget) )
662 Widget w = tw->tabs.topWidget ;
663 TabsConstraints tab = (TabsConstraints) w->core.constraints ;
665 XRaiseWindow(XtDisplay(w), XtWindow(w)) ;
667 XtVaSetValues(curtw->tabs.topWidget, XmNtraversalOn, False, 0) ;
668 XtVaSetValues(w, XmNtraversalOn, True, 0) ;
671 if( tab->tabs.row != tw->tabs.numRows-1 )
672 TabsShuffleRows(tw) ;
677 tw->tabs.needs_layout = True ;
684 /* Called when any child constraint resources change. */
688 TabsConstraintSetValues(Widget current, Widget request, Widget new,
689 ArgList args, Cardinal *num_args)
691 TabsWidget tw = (TabsWidget) XtParent(new) ;
692 TabsConstraints ctab = (TabsConstraints) current->core.constraints ;
693 TabsConstraints tab = (TabsConstraints) new->core.constraints ;
696 /* if label changes, need to re-layout the entire widget */
697 /* if foreground changes, need to redraw tab label */
699 /* TODO: only need resize of new bitmap has different dimensions
703 if( tab->tabs.label != ctab->tabs.label || /* Tab size has changed. */
704 tab->tabs.left_bitmap != ctab->tabs.left_bitmap )
707 tw->tabs.needs_layout = True ;
709 if( tab->tabs.left_bitmap != ctab->tabs.left_bitmap )
710 getBitmapInfo(tw, tab) ;
712 /* If there are no subclass ConstraintSetValues procedures remaining
713 * to be invoked, and if the preferred size has changed, ask
716 if( XtClass((Widget)tw) == tabsWidgetClass )
717 MakeSizeRequest(tw) ;
721 /* The child widget itself never needs a redisplay, but the parent
725 if( XtIsRealized(new) )
727 if( tw->tabs.needs_layout ) {
728 XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
729 XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
732 else if( tab->tabs.foreground != ctab->tabs.foreground )
733 DrawTab(tw, new, True) ;
741 TabsAcceptFocus(Widget w, Time *t)
743 if( !w->core.being_destroyed && XtIsRealized(w) &&
744 XtIsSensitive(w) && XtIsManaged(w) && w->core.visible )
747 for(p = XtParent(w); !XtIsShell(p); p = XtParent(p)) ;
748 XtSetKeyboardFocus(p,w) ;
758 * Return preferred size. Happily accept anything >= our preferred size.
759 * (TODO: is that the right thing to do? Should we always return "almost"
760 * if offerred more than we need?)
763 static XtGeometryResult
764 TabsQueryGeometry(Widget w,
765 XtWidgetGeometry *intended, XtWidgetGeometry *preferred)
767 register TabsWidget tw = (TabsWidget)w ;
768 XtGeometryMask mode = intended->request_mode ;
770 preferred->request_mode = CWWidth | CWHeight ;
771 PreferredSize(tw, &preferred->width, &preferred->height, NULL,NULL) ;
773 if( (!(mode & CWWidth) || intended->width == w->core.width) &&
774 (!(mode & CWHeight) || intended->height == w->core.height) )
775 return XtGeometryNo ;
777 if( (!(mode & CWWidth) || intended->width >= preferred->width) &&
778 (!(mode & CWHeight) || intended->height >= preferred->height) )
779 return XtGeometryYes;
781 return XtGeometryAlmost;
787 * Geometry Manager; called when a child wants to be resized.
790 static XtGeometryResult
791 TabsGeometryManager(Widget w, XtWidgetGeometry *req, XtWidgetGeometry *reply)
793 TabsWidget tw = (TabsWidget) XtParent(w);
794 Dimension s = SHADWID ;
795 TabsConstraints tab = (TabsConstraints)w->core.constraints;
796 XtGeometryResult result ;
799 /* Position request always denied */
801 if( ((req->request_mode & CWX) && req->x != w->core.x) ||
802 ((req->request_mode & CWY) && req->y != w->core.y) ||
803 !tab->tabs.resizable )
804 return XtGeometryNo ;
806 /* Make all three fields in the request valid */
807 if( !(req->request_mode & CWWidth) )
808 req->width = w->core.width;
809 if( !(req->request_mode & CWHeight) )
810 req->height = w->core.height;
811 if( !(req->request_mode & CWBorderWidth) )
812 req->border_width = w->core.border_width;
814 if( req->width == w->core.width &&
815 req->height == w->core.height &&
816 req->border_width == w->core.border_width )
817 return XtGeometryNo ;
819 rw = req->width + 2 * req->border_width ;
820 rh = req->height + 2 * req->border_width ;
822 /* find out how big the children want to be now */
823 MaxChild(tw, w, rw, rh) ;
826 /* Size changes must see if the new size can be accommodated.
827 * The Tabs widget keeps all of its children the same
828 * size. A request to shrink will be accepted only if the
829 * new size is still big enough for all other children. A
830 * request to shrink that is not big enough for all children
831 * returns an "almost" response with the new proposed size
832 * or a "no" response if unable to shrink at all.
834 * A request to grow will be accepted only if the Tabs parent can
835 * grow to accommodate.
838 * We could get fancy here and re-arrange the tabs if it is
839 * necessary to compromise with the parent, but we'll save that
843 if (req->request_mode & (CWWidth | CWHeight | CWBorderWidth))
845 Dimension cw,ch ; /* children's preferred size */
846 Dimension aw,ah ; /* available size we can give child */
847 Dimension th ; /* space used by tabs */
848 Dimension wid,hgt ; /* Tabs widget size */
850 cw = tw->tabs.max_cw ;
851 ch = tw->tabs.max_ch ;
853 /* find out what *my* resulting preferred size would be */
855 PreferredSize2(tw, cw, ch, &wid, &hgt) ;
857 /* Would my size change? If so, ask to be resized. */
859 if( wid != tw->core.width || hgt != tw->core.height )
861 Dimension oldWid = tw->core.width, oldHgt = tw->core.height ;
862 XtWidgetGeometry myrequest, myreply ;
864 myrequest.width = wid ;
865 myrequest.height = hgt ;
866 myrequest.request_mode = CWWidth | CWHeight ;
868 /* If child is only querying, or if we're going to have to
869 * offer the child a compromise, then make this a query only.
872 if( (req->request_mode & XtCWQueryOnly) || rw < cw || rh < ch )
873 myrequest.request_mode |= XtCWQueryOnly ;
875 result = XtMakeGeometryRequest((Widget)tw, &myrequest, &myreply) ;
877 /* !$@# Athena Box widget changes the core size even if QueryOnly
878 * is set. I'm convinced this is a bug. At any rate, to work
879 * around the bug, we need to restore the core size after every
880 * query geometry request. This is only partly effective,
881 * as there may be other boxes further up the tree.
883 if( myrequest.request_mode & XtCWQueryOnly ) {
884 tw->core.width = oldWid ;
885 tw->core.height = oldHgt ;
888 /* based on the parent's response, determine what the
889 * resulting Tabs widget size would be.
895 tw->tabs.needs_layout = True ;
899 wid = tw->core.width ;
900 hgt = tw->core.height ;
903 case XtGeometryAlmost:
904 wid = myreply.width ;
905 hgt = myreply.height ;
906 tw->tabs.needs_layout = True ;
911 /* Within the constraints imposed by the parent, what is
912 * the max size we can give the child?
914 (void) TabLayout(tw, wid, hgt, &th, True) ;
916 ah = hgt - th - 2*s ;
918 /* OK, make our decision. If requested size is >= max sibling
919 * preferred size, AND requested size <= available size, then
920 * we accept. Otherwise, we offer a compromise.
923 if( rw == aw && rh == ah )
925 /* Acceptable. If this wasn't a query, change *all* children
928 if( req->request_mode & XtCWQueryOnly )
929 return XtGeometryYes ;
932 Widget *childP = tw->composite.children ;
934 w->core.border_width = req->border_width ;
935 for(i=tw->tabs.displayChildren; --i >= 0; ++childP)
936 if( XtIsManaged(*childP) )
938 bw = (*childP)->core.border_width ;
939 XtConfigureWidget(*childP, s,tw->tabs.tab_total+s,
940 rw-2*bw, rh-2*bw, bw) ;
943 /* TODO: under what conditions will we need to redraw? */
944 XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
945 XtClass(tw)->core_class.expose((Widget)tw,NULL,NULL) ;
947 return XtGeometryDone ;
951 /* Cannot grant child's request. Describe what we *can* do
952 * and return counter-offer.
954 reply->width = aw - 2 * req->border_width ;
955 reply->height = ah - 2 * req->border_width ;
956 reply->border_width = req->border_width ;
957 reply->request_mode = CWWidth | CWHeight | CWBorderWidth ;
958 return XtGeometryAlmost ;
961 return XtGeometryYes ;
967 /* The number of children we manage has changed; recompute
972 TabsChangeManaged(Widget w)
974 TabsWidget tw = (TabsWidget)w ;
975 Widget *childP = tw->composite.children ;
978 if( tw->tabs.topWidget != NULL &&
979 ( !XtIsManaged(tw->tabs.topWidget) ||
980 tw->tabs.topWidget->core.being_destroyed ) )
981 tw->tabs.topWidget = NULL ;
983 GetPreferredSizes(tw) ;
984 MakeSizeRequest(tw) ;
986 XtClass(w)->core_class.resize(w) ;
987 if( XtIsRealized(w) )
989 Display *dpy = XtDisplay(w) ;
990 XClearWindow(dpy, XtWindow(w)) ;
991 XtClass(w)->core_class.expose(w,NULL,NULL) ;
993 /* make sure the top widget stays on top. This requires
994 * making sure that all new children are realized first.
996 if( tw->tabs.topWidget != NULL && XtIsRealized(tw->tabs.topWidget) )
998 for(i=tw->tabs.displayChildren; --i >= 0; ++childP)
999 if( !XtIsRealized(*childP) )
1000 XtRealizeWidget(*childP) ;
1002 XRaiseWindow(dpy, XtWindow(tw->tabs.topWidget)) ;
1007 /* Only top widget may receive input */
1009 for(childP = tw->composite.children, i=tw->composite.num_children;
1013 XtVaSetValues(*childP, XmNtraversalOn, False, 0) ;
1016 if( tw->tabs.topWidget != NULL )
1017 XtVaSetValues(tw->tabs.topWidget, XmNtraversalOn, True, 0) ;
1024 /****************************************************************
1028 ****************************************************************/
1031 /* User clicks on a tab, figure out which one it was. */
1035 TabsSelect(Widget w, XEvent *event, String *params, Cardinal *num_params)
1037 TabsWidget tw = (TabsWidget) w ;
1040 Dimension h = tw->tabs.tab_height ;
1044 XmProcessTraversal (w, XmTRAVERSE_CURRENT) ;
1047 /* TODO: is there an Xmu function or something to do this instead? */
1048 switch( event->type ) {
1051 x = event->xbutton.x ; y = event->xbutton.y ; break ;
1054 x = event->xkey.x ; y = event->xkey.y ; break ;
1059 /* TODO: determine which tab was clicked, if any. Set that
1060 * widget to be top of stacking order with XawTabsSetTop().
1062 for(i=0, childP=tw->composite.children;
1063 i < tw->tabs.displayChildren;
1065 if( XtIsManaged(*childP) )
1067 TabsConstraints tab = (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 )
1071 if( *childP != tw->tabs.topWidget &&
1072 (XtIsSensitive(*childP) || tw->tabs.selectInsensitive) )
1073 XawTabsSetTop(*childP, True) ;
1080 /* User hits a key */
1083 TabsPage(Widget w, XEvent *event, String *params, Cardinal *num_params)
1085 TabsWidget tw = (TabsWidget) w ;
1086 Widget newtop = NULL;
1089 int nc = tw->tabs.displayChildren ;
1094 if( *num_params < 1 ) {
1095 XtAppWarning(XtWidgetToApplicationContext(w),
1096 "Tabs: page() action called with no arguments") ;
1100 if( tw->tabs.topWidget == NULL )
1101 tw->tabs.topWidget = tw->composite.children[0] ;
1103 for(idx=0, childP=tw->composite.children; idx < nc; ++idx, ++childP )
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) ;
1144 /* User hits up/down key */
1147 TabsHighlight(Widget w, XEvent *event, String *params, Cardinal *num_params)
1149 TabsWidget tw = (TabsWidget) w ;
1150 Widget newhl = NULL;
1153 int nc = tw->tabs.displayChildren ;
1158 if( *num_params < 1 )
1160 if( tw->tabs.hilight != NULL )
1161 DrawHighlight(tw, tw->tabs.hilight, False) ;
1165 if( tw->tabs.hilight == NULL )
1166 newhl = tw->composite.children[0] ;
1170 /* find index of currently highlit child */
1171 for(idx=0, childP=tw->composite.children; idx < nc; ++idx, ++childP )
1172 if( tw->tabs.hilight == *childP )
1175 switch( params[0][0] ) {
1180 newhl = tw->composite.children[idx] ;
1183 case 'd': /* down */
1187 newhl = tw->composite.children[idx] ;
1192 newhl = tw->composite.children[0] ;
1197 newhl = tw->composite.children[nc-1] ;
1201 newhl = tw->tabs.hilight ;
1206 XawTabsSetHighlight(w, newhl) ;
1212 TabsUnhighlight(Widget w, XEvent *event, String *params, Cardinal *num_params)
1214 TabsWidget tw = (TabsWidget) w ;
1215 int nc = tw->composite.num_children ;
1220 if( tw->tabs.hilight != NULL )
1221 DrawHighlight(tw, tw->tabs.hilight, True) ;
1228 /****************************************************************
1232 ****************************************************************/
1235 /* Set the top tab, optionally call all callbacks. */
1237 XawTabsSetTop(Widget w, Bool callCallbacks)
1239 TabsWidget tw = (TabsWidget)w->core.parent ;
1240 TabsConstraints tab ;
1241 Widget oldtop = tw->tabs.topWidget ;
1243 if( !XtIsSubclass(w->core.parent, tabsWidgetClass) )
1246 sprintf(line, "XawTabsSetTop: widget \"%.64s\" is not the child of a tabs widget.", XtName(w)) ;
1247 XtAppWarning(XtWidgetToApplicationContext(w), line) ;
1252 XtCallCallbackList(w, tw->tabs.popdownCallbacks,
1253 (XtPointer)tw->tabs.topWidget) ;
1255 if( !XtIsRealized(w) ) {
1256 tw->tabs.topWidget = w ;
1257 tw->tabs.needs_layout = True ;
1261 XRaiseWindow(XtDisplay(w), XtWindow(w)) ;
1263 XtVaSetValues(oldtop, XmNtraversalOn, False, 0) ;
1264 XtVaSetValues(w, XmNtraversalOn, True, 0) ;
1267 tab = (TabsConstraints) w->core.constraints ;
1268 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) ;
1286 tw->tabs.topWidget = w ;
1287 TabsShuffleRows(tw) ;
1288 XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
1289 XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
1292 XawTabsSetHighlight((Widget)tw, w) ;
1295 XtCallCallbackList(w, tw->tabs.callbacks, (XtPointer)w) ;
1299 /* Set the top tab, optionally call all callbacks. */
1301 XawTabsSetHighlight(Widget t, Widget w)
1303 TabsWidget tw = (TabsWidget)t ;
1305 if( !XtIsSubclass(t, tabsWidgetClass) )
1308 if( XtIsRealized(t) && w != tw->tabs.hilight )
1310 if( tw->tabs.hilight != NULL )
1311 DrawHighlight(tw, tw->tabs.hilight, True) ;
1313 DrawHighlight(tw, w, False) ;
1316 tw->tabs.hilight = w ;
1322 /****************************************************************
1324 * Private Procedures
1326 ****************************************************************/
1330 TabsAllocGCs(TabsWidget tw)
1333 TabsAllocGreyGC(tw) ;
1334 tw->tabs.backgroundGC = AllocBackgroundGC((Widget)tw, None) ;
1335 tw->tabs.topGC = AllocTopShadowGC((Widget)tw,
1336 tw->tabs.top_shadow_contrast, tw->tabs.be_nice_to_cmap) ;
1337 tw->tabs.botGC = AllocBotShadowGC((Widget)tw,
1338 tw->tabs.bot_shadow_contrast, tw->tabs.be_nice_to_cmap) ;
1343 TabsFreeGCs(TabsWidget tw)
1345 Widget w = (Widget) tw;
1347 XtReleaseGC(w, tw->tabs.foregroundGC) ;
1348 XtReleaseGC(w, tw->tabs.greyGC) ;
1349 XtReleaseGC(w, tw->tabs.backgroundGC) ;
1350 XtReleaseGC(w, tw->tabs.topGC) ;
1351 XtReleaseGC(w, tw->tabs.botGC) ;
1353 XmuReleaseStippledPixmap(XtScreen(w), tw->tabs.grey50) ;
1361 /* Redraw entire Tabs widget */
1364 DrawTabs(TabsWidget tw, Bool labels)
1368 Dimension s = SHADWID ;
1369 Dimension th = tw->tabs.tab_height ;
1371 TabsConstraints tab ;
1373 if( !XtIsRealized((Widget)tw))
1376 /* draw tabs and frames by row except for the top tab, which
1377 * is drawn last. (This is inefficiently written, but should not
1378 * be too slow as long as there are not a lot of rows.)
1381 y = tw->tabs.numRows == 1 ? TABDELTA : 0 ;
1382 for(i=0; i<tw->tabs.numRows; ++i, y += th)
1384 for( j=tw->tabs.displayChildren, childP=tw->composite.children;
1385 --j >= 0; ++childP )
1386 if( XtIsManaged(*childP) )
1388 tab = (TabsConstraints)(*childP)->core.constraints;
1389 if( tab->tabs.row == i && *childP != tw->tabs.topWidget )
1390 DrawTab(tw, *childP, labels) ;
1392 if( i != tw->tabs.numRows -1 )
1393 DrawTrim(tw, 0,y+th, tw->core.width, th+s, False,False) ;
1398 /* and now the top tab */
1399 if( tw->tabs.topWidget != NULL )
1400 DrawTab(tw, tw->tabs.topWidget, labels) ;
1405 /* Draw one tab. Corners are rounded very slightly. */
1408 DrawTab(TabsWidget tw, Widget child, Bool labels)
1413 if( !XtIsRealized((Widget)tw))
1416 DrawBorder(tw, child, False) ;
1420 TabsConstraints tab = (TabsConstraints)child->core.constraints;
1421 Display *dpy = XtDisplay((Widget)tw) ;
1422 Window win = XtWindow((Widget)tw) ;
1423 String lbl = tab->tabs.label != NULL ?
1424 tab->tabs.label : XtName(child) ;
1426 if( XtIsSensitive(child) )
1428 gc = tw->tabs.foregroundGC ;
1429 XSetForeground(dpy, gc, tab->tabs.foreground) ;
1433 /* grey pixel allocation deferred until now */
1434 if( !tab->tabs.greyAlloc )
1436 if( tw->tabs.be_nice_to_cmap || tw->core.depth == 1 )
1437 tab->tabs.grey = tab->tabs.foreground ;
1439 tab->tabs.grey = AllocGreyPixel((Widget)tw,
1440 tab->tabs.foreground,
1441 tw->core.background_pixel,
1442 tw->tabs.insensitive_contrast ) ;
1443 tab->tabs.greyAlloc = True ;
1445 gc = tw->tabs.greyGC ;
1446 XSetForeground(dpy, gc, tab->tabs.grey) ;
1451 if( child == tw->tabs.topWidget )
1454 if( tab->tabs.left_bitmap != None && tab->tabs.lbm_width > 0 )
1456 if( tab->tabs.lbm_depth == 1 )
1457 XCopyPlane(dpy, tab->tabs.left_bitmap, win,gc,
1458 0,0, tab->tabs.lbm_width, tab->tabs.lbm_height,
1459 x+tab->tabs.lbm_x, y+tab->tabs.lbm_y, 1L) ;
1461 XCopyArea(dpy, tab->tabs.left_bitmap, win,gc,
1462 0,0, tab->tabs.lbm_width, tab->tabs.lbm_height,
1463 x+tab->tabs.lbm_x, y+tab->tabs.lbm_y) ;
1466 if( lbl != NULL && tw->tabs.font != NULL )
1467 XDrawString(dpy,win,gc,
1468 x+tab->tabs.l_x, y+tab->tabs.l_y,
1469 lbl, (int)strlen(lbl)) ;
1472 if( child == tw->tabs.hilight )
1473 DrawHighlight(tw, child, False) ;
1477 /* draw frame all the way around the child windows. */
1480 DrawFrame(TabsWidget tw)
1482 GC topgc = tw->tabs.topGC ;
1483 GC botgc = tw->tabs.botGC ;
1484 Dimension s = SHADWID ;
1485 Dimension ch = tw->tabs.child_height ;
1486 Draw3dBox((Widget)tw, 0,tw->tabs.tab_total,
1487 tw->core.width, ch+2*s, s, topgc, botgc) ;
1491 /* draw trim around a tab or underneath a row of tabs */
1494 DrawTrim(TabsWidget tw, /* widget */
1495 int x, /* upper-left corner */
1497 int wid, /* total size */
1499 Bool bottom, /* draw bottom? */
1500 Bool undraw) /* undraw all */
1502 Display *dpy = XtDisplay((Widget)tw) ;
1503 Window win = XtWindow((Widget)tw) ;
1504 GC bggc = tw->tabs.backgroundGC ;
1505 GC topgc = undraw ? bggc : tw->tabs.topGC ;
1506 GC botgc = undraw ? bggc : tw->tabs.botGC ;
1508 XDrawLine(dpy,win,bggc, x,y+hgt-1, x+wid-1,y+hgt-1) ; /* bottom */
1509 XDrawLine(dpy,win,topgc, x,y+2, x,y+hgt-2) ; /* left */
1510 XDrawPoint(dpy,win,topgc, x+1,y+1) ; /* corner */
1511 XDrawLine(dpy,win,topgc, x+2,y, x+wid-3,y) ; /* top */
1512 XDrawLine(dpy,win,botgc, x+wid-2,y+1, x+wid-2,y+hgt-2) ; /* right */
1513 XDrawLine(dpy,win,botgc, x+wid-1,y+2, x+wid-1,y+hgt-2) ; /* right */
1517 /* Draw one tab border. */
1520 DrawBorder(TabsWidget tw, Widget child, Bool undraw)
1522 TabsConstraints tab = (TabsConstraints)child->core.constraints;
1523 Position x = tab->tabs.x ;
1524 Position y = tab->tabs.y ;
1525 Dimension twid = tab->tabs.width ;
1526 Dimension thgt = tw->tabs.tab_height ;
1528 /* top tab requires a little special attention; it overlaps
1529 * neighboring tabs slightly, so the background must be cleared
1530 * in the region of the overlap to partially erase those neighbors.
1531 * TODO: is this worth doing with regions instead?
1533 if( child == tw->tabs.topWidget )
1535 Display *dpy = XtDisplay((Widget)tw) ;
1536 Window win = XtWindow((Widget)tw) ;
1537 GC bggc = tw->tabs.backgroundGC ;
1538 XRectangle rects[3] ;
1541 twid += TABDELTA*2 ;
1543 AddRect(0, x,y+1,twid,TABDELTA) ;
1544 AddRect(1, x+1,y,TABDELTA,thgt) ;
1545 AddRect(2, x+twid-TABDELTA-1,y,TABDELTA,thgt) ;
1546 XFillRectangles(dpy,win,bggc, rects, 3) ;
1549 DrawTrim(tw, x,y,twid,thgt+1, child == tw->tabs.topWidget, undraw) ;
1553 /* Draw highlight around tab that has focus */
1556 DrawHighlight(TabsWidget tw, Widget child, Bool undraw)
1558 TabsConstraints tab = (TabsConstraints)child->core.constraints;
1559 Display *dpy = XtDisplay((Widget)tw) ;
1560 Window win = XtWindow((Widget)tw) ;
1562 Position x = tab->tabs.x ;
1563 Position y = tab->tabs.y ;
1564 Dimension wid = tab->tabs.width ;
1565 Dimension hgt = tw->tabs.tab_height ;
1568 /* top tab does not have a highlight */
1570 if( child == tw->tabs.topWidget )
1574 gc = tw->tabs.backgroundGC ;
1576 else if( XtIsSensitive(child) )
1578 gc = tw->tabs.foregroundGC ;
1579 XSetForeground(dpy, gc, tab->tabs.foreground) ;
1583 gc = tw->tabs.greyGC ;
1584 XSetForeground(dpy, gc, tab->tabs.grey) ;
1587 points[0].x = x+1 ; points[0].y = y+hgt-1 ;
1588 points[1].x = x+1 ; points[1].y = y+2 ;
1589 points[2].x = x+2 ; points[2].y = y+1 ;
1590 points[3].x = x+wid-4 ; points[3].y = y+1 ;
1591 points[4].x = x+wid-3 ; points[4].y = y+2 ;
1592 points[5].x = x+wid-3 ; points[5].y = y+hgt-1 ;
1594 XDrawLines(dpy,win,gc, points,6, CoordModeOrigin) ;
1598 /* Undraw one tab interior */
1601 UndrawTab(TabsWidget tw, Widget child)
1603 TabsConstraints tab = (TabsConstraints)child->core.constraints;
1604 Position x = tab->tabs.x ;
1605 Position y = tab->tabs.y ;
1606 Dimension twid = tab->tabs.width ;
1607 Dimension thgt = tw->tabs.tab_height ;
1608 Display *dpy = XtDisplay((Widget)tw) ;
1609 Window win = XtWindow((Widget)tw) ;
1610 GC bggc = tw->tabs.backgroundGC ;
1612 XFillRectangle(dpy,win,bggc, x,y, twid,thgt) ;
1619 /* GEOMETRY UTILITIES */
1623 * MaxChild(): ask all children (except possibly one) their
1624 * preferred sizes, set max_cw, max_ch accordingly.
1626 * GetPreferredSizes(): ask all children their preferred sizes,
1627 * set max_cw, max_ch accordingly.
1629 * PreferredSize(): given max_cw, max_ch, return tabs widget
1630 * preferred size. Iterate with other widths in order to get
1631 * a reasonable aspect ratio.
1633 * PreferredSize2(): Given child dimensions, return Tabs
1634 * widget dimensions.
1636 * PreferredSize3(): Same, except given child dimensions plus
1641 /* Compute the width of one child's tab. Positions will be computed
1644 * height: font height + vertical_space*2 + shadowWid*2
1645 * width: string width + horizontal_space*2 + shadowWid*2
1647 * All tabs are the same height, so that is computed elsewhere.
1653 TabsConstraints tab = (TabsConstraints) w->core.constraints ;
1654 TabsWidget tw = (TabsWidget)XtParent(w) ;
1655 String lbl = tab->tabs.label != NULL ?
1656 tab->tabs.label : XtName(w) ;
1657 XFontStruct *font = tw->tabs.font ;
1658 int iw = tw->tabs.internalWidth ;
1660 tab->tabs.width = iw + SHADWID*2 ;
1661 tab->tabs.l_x = tab->tabs.lbm_x = SHADWID + iw ;
1663 if( tab->tabs.left_bitmap != None )
1665 tab->tabs.width += tab->tabs.lbm_width + iw ;
1666 tab->tabs.l_x += tab->tabs.lbm_width + iw ;
1667 tab->tabs.lbm_y = (tw->tabs.tab_height - tab->tabs.lbm_height)/2 ;
1670 if( lbl != NULL && font != NULL )
1672 tab->tabs.width += XTextWidth( font, lbl, (int)strlen(lbl) ) + iw ;
1673 tab->tabs.l_y = (tw->tabs.tab_height +
1674 tw->tabs.font->max_bounds.ascent -
1675 tw->tabs.font->max_bounds.descent)/2 ;
1681 /* Lay out tabs to fit in given width. Compute x,y position and
1682 * row number for each tab. Return number of rows and total height
1683 * required by all tabs. If there is only one row, add TABDELTA
1684 * height to the total. Rows are assigned bottom to top.
1686 * Tabs are indented from the edges by INDENT.
1688 * TODO: if they require more than two rows and the total height:width
1689 * ratio is more than 2:1, then try something else.
1693 TabLayout(TabsWidget tw, int wid, int hgt, Dimension *reply_height, Bool query_only)
1696 int num_children = tw->composite.num_children ;
1700 TabsConstraints tab ;
1703 tw->tabs.displayChildren = 0;
1705 /* Algorithm: loop through children, assign X positions. If a tab
1706 * would extend beyond the right edge, start a new row. After all
1707 * rows are assigned, make a second pass and assign Y positions.
1710 if( num_children > 0 )
1712 /* Loop through the tabs and see how much space they need. */
1718 for(i=num_children, childP=tw->composite.children; --i >= 0; ++childP)
1719 if( XtIsManaged(*childP) )
1721 tab = (TabsConstraints) (*childP)->core.constraints ;
1722 w = tab->tabs.width ;
1723 if( x + w > wid ) { /* new row */
1724 if (y + tw->tabs.tab_height > hgt)
1728 y += tw->tabs.tab_height ;
1733 tab->tabs.row = row ;
1737 tw->tabs.displayChildren++;
1739 /* If there was only one row, increse the height by TABDELTA */
1744 for(i=num_children, childP=tw->composite.children;
1745 --i >= 0 ; ++childP)
1746 if( XtIsManaged(*childP) )
1748 tab = (TabsConstraints) (*childP)->core.constraints ;
1752 y += tw->tabs.tab_height ;
1758 tw->tabs.tab_total = y ;
1759 tw->tabs.numRows = row ;
1762 if( reply_height != NULL )
1770 /* Find max preferred child size. Returned sizes include child
1775 GetPreferredSizes(TabsWidget tw)
1777 MaxChild(tw, NULL, 0,0) ;
1782 /* Find max preferred child size. Returned sizes include child
1783 * border widths. If except is non-null, don't ask that one.
1787 MaxChild(TabsWidget tw, Widget except, Dimension cw, Dimension ch)
1790 Widget *childP = tw->composite.children ;
1791 XtWidgetGeometry preferred ;
1793 for(i=tw->composite.num_children; --i >=0; ++childP)
1794 if( XtIsManaged(*childP) && *childP != except )
1796 (void) XtQueryGeometry(*childP, NULL, &preferred) ;
1797 cw = Max(cw, preferred.width + preferred.border_width * 2 ) ;
1798 ch = Max(ch, preferred.height + preferred.border_width * 2 ) ;
1801 tw->tabs.max_cw = cw ;
1802 tw->tabs.max_ch = ch ;
1807 /* rotate row numbers to bring current widget to bottom row,
1808 * compute y positions for all tabs
1812 TabsShuffleRows(TabsWidget tw)
1814 TabsConstraints tab ;
1818 Dimension th = tw->tabs.tab_height ;
1822 /* There must be a top widget. If not, assign one. */
1823 if( tw->tabs.topWidget == NULL && tw->composite.children != NULL )
1824 for(i=tw->composite.num_children, childP=tw->composite.children;
1827 if( XtIsManaged(*childP) ) {
1828 tw->tabs.topWidget = *childP ;
1832 if( tw->tabs.topWidget != NULL )
1834 nrows = tw->tabs.numRows ;
1835 assert( nrows > 0 ) ;
1839 tab = (TabsConstraints) tw->tabs.topWidget->core.constraints ;
1840 assert( tab != NULL ) ;
1842 /* how far to move top row */
1843 move = nrows - tab->tabs.row ;
1844 bottom = tw->tabs.tab_total - th ;
1846 for(i=tw->tabs.displayChildren, childP=tw->composite.children;
1849 if( XtIsManaged(*childP) )
1851 tab = (TabsConstraints) (*childP)->core.constraints ;
1852 tab->tabs.row = (tab->tabs.row + move) % nrows ;
1853 tab->tabs.y = bottom - tab->tabs.row * th ;
1860 /* Find preferred size. Ask children, find size of largest,
1861 * add room for tabs & return. This can get a little involved,
1862 * as we don't want to have too many rows of tabs; we may widen
1863 * the widget to reduce # of rows.
1865 * This function requires that max_cw, max_ch already be set.
1871 Dimension *reply_width, /* total widget size */
1872 Dimension *reply_height,
1873 Dimension *reply_cw, /* child widget size */
1874 Dimension *reply_ch)
1876 Dimension cw,ch ; /* child width, height */
1878 Dimension rwid,rhgt ;
1881 wid = cw = tw->tabs.max_cw ;
1882 hgt = ch = tw->tabs.max_ch ;
1884 nrow = PreferredSize2(tw, wid,hgt, &rwid, &rhgt) ;
1886 /* Check for absurd results (more than 2 rows, high aspect
1887 * ratio). Try wider size if needed.
1888 * TODO: make sure this terminates.
1891 if( nrow > 2 && rhgt > rwid )
1896 /* step 1: start doubling size until it's too big */
1899 wid = Max(wid*2, wid+20) ;
1900 nrow = PreferredSize2(tw, wid,hgt, &rwid,&rhgt) ;
1901 } while( nrow > 2 && rhgt > rwid ) ;
1904 /* step 2: use Newton's method to find ideal size. Stop within
1907 while( --maxloop > 0 && w1 > w0 + 8 )
1910 nrow = PreferredSize2(tw, wid,hgt, &rwid,&rhgt) ;
1911 if( nrow > 2 && rhgt > rwid )
1919 *reply_width = rwid ;
1920 *reply_height = rhgt ;
1921 if( reply_cw != NULL ) *reply_cw = cw ;
1922 if( reply_ch != NULL ) *reply_ch = ch ;
1927 /* Find preferred size, given size of children. */
1932 int cw, /* child width, height */
1934 Dimension *reply_width, /* total widget size */
1935 Dimension *reply_height)
1937 Dimension s = SHADWID ;
1939 /* make room for shadow frame */
1943 return PreferredSize3(tw, cw, ch, reply_width, reply_height) ;
1947 /* Find preferred size, given size of children+shadow. */
1952 int wid, /* child width, height */
1954 Dimension *reply_width, /* total widget size */
1955 Dimension *reply_height)
1957 Dimension th ; /* space used by tabs */
1960 if( tw->composite.num_children > 0 )
1961 nrows = TabLayout(tw, wid, hgt, &th, True) ;
1967 *reply_width = Max(wid, MIN_WID) ;
1968 *reply_height = Max(th+hgt, MIN_HGT) ;
1975 MakeSizeRequest(TabsWidget tw)
1977 Widget w = (Widget)tw ;
1978 XtWidgetGeometry request, reply ;
1979 XtGeometryResult result ;
1982 request.request_mode = CWWidth | CWHeight ;
1983 PreferredSize(tw, &request.width, &request.height, &cw, &ch) ;
1985 if( request.width == tw->core.width &&
1986 request.height == tw->core.height )
1989 result = XtMakeGeometryRequest(w, &request, &reply) ;
1991 if( result == XtGeometryAlmost )
1993 /* Bugger. Didn't get what we want, but were offered a
1994 * compromise. If the width was too small, recompute
1995 * based on the too-small width and try again.
1996 * If the height was too small, make a wild-ass guess
1997 * at a wider width and try again.
2000 if( reply.width < request.width && reply.height >= request.height )
2002 Dimension s = SHADWID ;
2004 PreferredSize3(tw, reply.width,ch, &request.width, &request.height);
2005 result = XtMakeGeometryRequest(w, &request, &reply) ;
2006 if( result == XtGeometryAlmost )
2007 (void) XtMakeGeometryRequest(w, &reply, NULL) ;
2011 (void) XtMakeGeometryRequest(w, &reply, NULL) ;
2017 getBitmapInfo(TabsWidget tw, TabsConstraints tab)
2023 if( tab->tabs.left_bitmap == None ||
2024 !XGetGeometry(XtDisplay(tw), tab->tabs.left_bitmap, &root, &x, &y,
2025 &tab->tabs.lbm_width, &tab->tabs.lbm_height,
2026 &bw, &tab->tabs.lbm_depth) )
2027 tab->tabs.lbm_width = tab->tabs.lbm_height = 0 ;
2033 /* Code copied & modified from Gcs.c. This version has dynamic
2038 TabsAllocFgGC(TabsWidget tw)
2040 Widget w = (Widget) tw;
2043 values.background = tw->core.background_pixel ;
2044 values.font = tw->tabs.font->fid ;
2045 values.line_style = LineOnOffDash ;
2046 values.line_style = LineSolid ;
2048 tw->tabs.foregroundGC =
2049 XtAllocateGC(w, w->core.depth,
2050 GCBackground|GCFont|GCLineStyle, &values,
2052 GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2053 GCDashList|GCArcMode) ;
2057 TabsAllocGreyGC(TabsWidget tw)
2059 Widget w = (Widget) tw;
2062 values.background = tw->core.background_pixel ;
2063 values.font = tw->tabs.font->fid ;
2065 if( tw->tabs.be_nice_to_cmap || w->core.depth == 1)
2067 values.fill_style = FillStippled ;
2069 values.stipple = XmuCreateStippledPixmap(XtScreen(w), 1L, 0L, 1) ;
2072 XtAllocateGC(w, w->core.depth,
2073 GCBackground|GCFont|GCStipple|GCFillStyle, &values,
2075 GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2076 GCDashList|GCArcMode) ;
2082 XtAllocateGC(w, w->core.depth,
2085 GCBackground|GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2086 GCDashList|GCArcMode) ;