XEmacs 21.2.26 "Millenium".
[chise/xemacs-chise.git.1] / lwlib / xlwtabs.c
1  /* Tabs Widget for XEmacs.
2     Copyright (C) 1999 Edward A. Falk
3  
4  This file is part of XEmacs.
5  
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
9  later version.
10  
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
14  for more details.
15  
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.  */
20  
21  /* Synched up with: Tabs.c 1.27 */
22  
23  /*
24  * Tabs.c - Index Tabs composite widget
25  *
26  * Author: Edward A. Falk
27  *         falk@falconer.vip.best.com
28  *
29  * Date: July 29, 1997
30  *
31  *
32  * Overall layout of this widget is as follows:
33  *
34  *   ________ ,---------. _________
35  *  |  label ||  Label  ||  Label  |  \ tabs
36  *  |________||         ||_________|  /
37  *  |+----------------------------+|  \
38  *  ||                            ||  |
39  *  ||  child widget window       ||   > frame
40  *  |+----------------------------+|  |
41  *  +------------------------------+  /
42  *
43  * The height of the tabs includes the shadow width, top and bottom
44  * margins, and the height of the text.
45  *
46  * The height of the frame includes the top and bottom shadow width and the
47  * size of the child widget window.
48  *
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
51  * the frame.
52  */
53
54 /*
55  * TODO: min child height = tab height
56  */
57
58 #include        <config.h>
59 #include        <stdio.h>
60
61 #include        <X11/Xlib.h>
62 #include        <X11/IntrinsicP.h>
63 #include        <X11/StringDefs.h>
64 #include        "../src/xmu.h"
65 #include        "xlwtabsP.h"
66 #include        "xlwgcs.h"
67
68 #define MIN_WID         10
69 #define MIN_HGT         10
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 */
75
76
77 /****************************************************************
78  *
79  * IndexTabs Resources
80  *
81  ****************************************************************/
82
83 static  char    defaultTranslations[] = "\
84         <BtnUp>:                select()        \n\
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\
104          " ;
105
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\
124          " ;
125 static  XtAccelerators  defaultAccelerators ;
126
127 #define offset(field)   XtOffsetOf(TabsRec, tabs.field)
128 static XtResource resources[] = {
129
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},
156 };
157 #undef  offset
158
159
160
161         /* constraint resources */
162
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},
173 } ;
174 #undef  offset
175
176
177
178
179 #if     !NeedFunctionPrototypes
180
181         /* FORWARD REFERENCES: */
182
183         /* member functions */
184
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() ;
198
199         /* action procs */
200
201 static  void    TabsSelect() ;
202 static  void    TabsPage() ;
203 static  void    TabsHighlight() ;
204 static  void    TabsUnhighlight() ;
205
206         /* internal privates */
207
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 */
227
228 static  void    TabsAllocFgGC() ;
229 static  void    TabsAllocGreyGC() ;
230
231 #else
232
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) ;
249
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 *) ;
254
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) ;
263
264 static  void    TabWidth( Widget w) ;
265 static  int     TabLayout( TabsWidget, int wid, int hgt, Dimension *r_hgt,
266                         Bool query_only) ;
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) ;
278
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) ;
284
285 #endif
286
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)
290
291 static  XtActionsRec    actionsList[] =
292   {
293     {"select",  TabsSelect},
294     {"page",    TabsPage},
295     {"highlight", TabsHighlight},
296     {"unhighlight", TabsUnhighlight},
297   } ;
298
299
300 /****************************************************************
301 *
302 * Full class record constant
303 *
304 ****************************************************************/
305
306 #ifndef NEED_MOTIF
307 #define SuperClass      (&constraintClassRec)
308 #else
309 #define SuperClass      (&xmManagerClassRec)
310 #endif
311
312 TabsClassRec tabsClassRec = {
313   {
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,
332 #else
333     /* compress_exposure  */    XtExposeCompressMaximal|XtExposeNoRegion,
334 #endif
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,
350     /* extension          */    NULL
351   },
352   {
353 /* composite_class fields */
354     /* geometry_manager   */    TabsGeometryManager,
355     /* change_managed     */    TabsChangeManaged,
356     /* insert_child       */    XtInheritInsertChild,   /* TODO? */
357     /* delete_child       */    XtInheritDeleteChild,   /* TODO? */
358     /* extension          */    NULL
359   },
360   {
361 /* constraint_class fields */
362     /* subresources       */    tabsConstraintResources,
363     /* subresource_count  */    XtNumber(tabsConstraintResources),
364     /* constraint_size    */    sizeof(TabsConstraintsRec),
365     /* initialize         */    TabsConstraintInitialize,
366     /* destroy            */    NULL,
367     /* set_values         */    TabsConstraintSetValues,
368     /* extension          */    NULL,
369   },
370 #ifdef  NEED_MOTIF
371 /* Manager Class fields */
372   {
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,
379     /* extension                */      NULL
380   },
381 #endif
382   {
383 /* Tabs class fields */
384     /* extension          */    NULL,
385   }
386 };
387
388 WidgetClass tabsWidgetClass = (WidgetClass)&tabsClassRec;
389
390
391
392 #ifdef  DEBUG
393 #ifdef  __STDC__
394 #define assert(e) \
395           if(!(e)) fprintf(stderr,"yak! %s at %s:%d\n",#e,__FILE__,__LINE__)
396 #else
397 #define assert(e) \
398           if(!(e)) fprintf(stderr,"yak! e at %s:%d\n",__FILE__,__LINE__)
399 #endif
400 #else
401 #define assert(e)
402 #endif
403
404
405 \f
406
407 /****************************************************************
408  *
409  * Member Procedures
410  *
411  ****************************************************************/
412
413 static void
414 TabsClassInit(void)
415 {
416         defaultAccelerators = XtParseAcceleratorTable(accelTable) ;
417         /* TODO: register converter for labels? */
418 }
419
420
421
422         /* Init a newly created tabs widget.  Compute height of tabs
423          * and optionally compute size of widget. */
424
425 /* ARGSUSED */
426
427 static void
428 TabsInit(Widget request, Widget new, ArgList args, Cardinal *num_args)
429 {
430     TabsWidget newTw = (TabsWidget)new;
431
432     newTw->tabs.numRows = 0 ;
433     newTw->tabs.displayChildren = 0;
434
435     GetPreferredSizes(newTw) ;
436
437     /* height is easy, it's the same for all tabs:
438      *  TODO: font height + height of tallest bitmap.
439      */
440     newTw->tabs.tab_height = 2 * newTw->tabs.internalHeight + SHADWID ;
441
442     if( newTw->tabs.font != NULL )
443       newTw->tabs.tab_height += newTw->tabs.font->max_bounds.ascent +
444                                 newTw->tabs.font->max_bounds.descent ;
445
446     /* GC allocation is deferred until XtRealize() */
447
448     /* if size not explicitly set, set it to our preferred size now. */
449
450     if( request->core.width == 0 || request->core.height == 0 )
451     {
452       Dimension w,h ;
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) ;
457     }
458
459     /* defer GC allocation, etc., until Realize() time. */
460     newTw->tabs.foregroundGC =
461     newTw->tabs.backgroundGC =
462     newTw->tabs.greyGC =
463     newTw->tabs.topGC =
464     newTw->tabs.botGC = None ;
465
466     newTw->tabs.grey50 = None ;
467
468     newTw->tabs.needs_layout = False ;
469
470     newTw->tabs.hilight = NULL ;
471
472 #ifdef  NEED_MOTIF
473     newTw->manager.navigation_type = XmTAB_GROUP ;
474     newTw->manager.traversal_on = True ;
475 #endif
476 }
477
478
479         /* Init the constraint part of a new tab child.  Compute the
480          * size of the tab.
481          */
482 /* ARGSUSED */
483 static  void
484 TabsConstraintInitialize(Widget request, Widget new,
485         ArgList args, Cardinal *num_args)
486 {
487         TabsConstraints tab = (TabsConstraints) new->core.constraints ;
488         tab->tabs.greyAlloc = False ;   /* defer allocation of pixel */
489
490         getBitmapInfo((TabsWidget)XtParent(new), tab) ;
491         TabWidth(new) ;
492 }
493
494
495
496         /* Called when tabs widget first realized.  Create the window
497          * and allocate the GCs
498          */
499
500 static  void
501 TabsRealize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
502 {
503         TabsWidget tw = (TabsWidget) w;
504
505         attributes->bit_gravity = NorthWestGravity;
506         *valueMask |= CWBitGravity;
507
508         SuperClass->core_class.realize(w, valueMask, attributes);
509
510         TabsAllocGCs(tw) ;
511 }
512
513
514
515 static  void
516 TabsDestroy(Widget w)
517 {
518         TabsFreeGCs((TabsWidget)w) ;
519 }
520
521
522         /* Parent has resized us.  This will require that the tabs be
523          * laid out again.
524          */
525
526 static void
527 TabsResize(Widget w)
528 {
529         TabsWidget      tw = (TabsWidget) w;
530         int             i ;
531         int             num_children = tw->composite.num_children ;
532         Widget          *childP ;
533         TabsConstraints tab ;
534         Dimension       cw,ch,bw ;
535
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.
541          *
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
544          * to the bottom row.
545          */
546
547         tw->tabs.needs_layout = False ;
548
549         if( num_children > 0 && tw->composite.children != NULL )
550         {
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;
554
555           /* assign a top widget, bring it to bottom row. */
556           TabsShuffleRows(tw) ;
557
558           /* now assign child positions & sizes.  Positions are all the
559            * same: just inside the frame.  Sizes are also all the same.
560            */
561
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 ;
565
566
567           for(i=0, childP=tw->composite.children;
568                 i < num_children;
569                 ++i, ++childP)
570             if( XtIsManaged(*childP) )
571             {
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) ;
576             }
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.
581              */
582             XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
583           }
584         }
585 } /* Resize */
586
587
588
589         /* Redraw entire Tabs widget */
590
591 /* ARGSUSED */
592 static  void
593 TabsExpose(Widget w, XEvent *event, Region region)
594 {
595         TabsWidget      tw = (TabsWidget) w;
596
597         if( tw->tabs.needs_layout )
598           XtClass(w)->core_class.resize(w) ;
599
600         DrawTabs(tw, True) ;
601 }
602
603
604         /* Called when any Tabs widget resources are changed. */
605
606 /* ARGSUSED */
607 static  Boolean
608 TabsSetValues(Widget current, Widget request, Widget new,
609         ArgList args, Cardinal *num_args)
610 {
611         TabsWidget curtw = (TabsWidget) current ;
612         TabsWidget tw = (TabsWidget) new ;
613         Boolean needRedraw = False ;
614         Widget  *childP ;
615         int     i ;
616
617
618         if( tw->tabs.font != curtw->tabs.font  ||
619             tw->tabs.internalWidth != curtw->tabs.internalWidth ||
620             tw->tabs.internalHeight != curtw->tabs.internalHeight )
621         {
622           tw->tabs.tab_height = 2 * tw->tabs.internalHeight + SHADWID ;
623
624           if( tw->tabs.font != NULL )
625             tw->tabs.tab_height += tw->tabs.font->max_bounds.ascent +
626                                    tw->tabs.font->max_bounds.descent ;
627
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;
631                 ++i, ++childP)
632             if( XtIsManaged(*childP) )
633               TabWidth(*childP) ;
634           PreferredSize(tw, &tw->core.width, &tw->core.height, NULL,NULL) ;
635           needRedraw = True ;
636           tw->tabs.needs_layout = True ;
637         }
638
639         /* TODO: if any color changes, need to recompute GCs and redraw */
640
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) )
645           {
646             TabsFreeGCs(tw) ;
647             TabsAllocGCs(tw) ;
648             needRedraw = True ;
649           }
650
651         if( tw->core.sensitive != curtw->core.sensitive )
652           needRedraw = True ;
653
654         /* If top widget changes, need to change stacking order, redraw tabs.
655          * Window system will handle the redraws.
656          */
657
658         if( tw->tabs.topWidget != curtw->tabs.topWidget ) 
659         {
660           if( XtIsRealized(tw->tabs.topWidget) )
661           {
662             Widget              w = tw->tabs.topWidget ;
663             TabsConstraints     tab = (TabsConstraints) w->core.constraints ;
664
665             XRaiseWindow(XtDisplay(w), XtWindow(w)) ;
666 #ifdef  NEED_MOTIF
667             XtVaSetValues(curtw->tabs.topWidget, XmNtraversalOn, False, 0) ;
668             XtVaSetValues(w, XmNtraversalOn, True, 0) ;
669 #endif
670
671             if( tab->tabs.row != tw->tabs.numRows-1 )
672               TabsShuffleRows(tw) ;
673
674             needRedraw = True ;
675           }
676           else
677             tw->tabs.needs_layout = True ;
678         }
679
680         return needRedraw ;
681 }
682
683
684         /* Called when any child constraint resources change. */
685
686 /* ARGSUSED */
687 static  Boolean
688 TabsConstraintSetValues(Widget current, Widget request, Widget new,
689         ArgList args, Cardinal *num_args)
690 {
691         TabsWidget tw = (TabsWidget) XtParent(new) ;
692         TabsConstraints ctab = (TabsConstraints) current->core.constraints ;
693         TabsConstraints tab = (TabsConstraints) new->core.constraints ;
694
695
696         /* if label changes, need to re-layout the entire widget */
697         /* if foreground changes, need to redraw tab label */
698
699         /* TODO: only need resize of new bitmap has different dimensions
700          * from old bitmap.
701          */
702
703         if( tab->tabs.label != ctab->tabs.label ||  /* Tab size has changed. */
704             tab->tabs.left_bitmap != ctab->tabs.left_bitmap )
705         {
706           TabWidth(new) ;
707           tw->tabs.needs_layout = True ;
708
709           if( tab->tabs.left_bitmap != ctab->tabs.left_bitmap )
710             getBitmapInfo(tw, tab) ;
711
712           /* If there are no subclass ConstraintSetValues procedures remaining
713            * to be invoked, and if the preferred size has changed, ask
714            * for a resize.
715            */
716           if( XtClass((Widget)tw) == tabsWidgetClass )
717             MakeSizeRequest(tw) ;
718         }
719
720
721         /* The child widget itself never needs a redisplay, but the parent
722          * Tabs widget might.
723          */
724
725         if( XtIsRealized(new) )
726         {
727           if( tw->tabs.needs_layout ) {
728             XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
729             XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
730           }
731
732           else if( tab->tabs.foreground != ctab->tabs.foreground )
733             DrawTab(tw, new, True) ;
734         }
735
736         return False ;
737 }
738
739
740 static  Boolean
741 TabsAcceptFocus(Widget w, Time *t)
742 {
743         if( !w->core.being_destroyed && XtIsRealized(w) &&
744             XtIsSensitive(w) && XtIsManaged(w) && w->core.visible )
745         {
746           Widget p ;
747           for(p = XtParent(w); !XtIsShell(p); p = XtParent(p)) ;
748           XtSetKeyboardFocus(p,w) ;
749           return True ;
750         }
751         else
752           return False ;
753 }
754
755
756
757 /*
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?)
761  */
762
763 static XtGeometryResult
764 TabsQueryGeometry(Widget w,
765         XtWidgetGeometry *intended, XtWidgetGeometry *preferred)
766 {
767     register TabsWidget tw = (TabsWidget)w ;
768     XtGeometryMask mode = intended->request_mode ;
769
770     preferred->request_mode = CWWidth | CWHeight ;
771     PreferredSize(tw, &preferred->width, &preferred->height, NULL,NULL) ;
772
773     if( (!(mode & CWWidth) || intended->width == w->core.width)  &&
774         (!(mode & CWHeight) || intended->height == w->core.height) )
775       return XtGeometryNo ;
776
777     if( (!(mode & CWWidth) || intended->width >= preferred->width)  &&
778         (!(mode & CWHeight) || intended->height >= preferred->height) )
779       return XtGeometryYes;
780
781     return XtGeometryAlmost;
782 }
783
784
785
786 /*
787  * Geometry Manager; called when a child wants to be resized.
788  */
789
790 static XtGeometryResult
791 TabsGeometryManager(Widget w, XtWidgetGeometry *req, XtWidgetGeometry *reply)
792 {
793         TabsWidget              tw = (TabsWidget) XtParent(w);
794         Dimension               s = SHADWID ;
795         TabsConstraints         tab = (TabsConstraints)w->core.constraints;
796         XtGeometryResult        result ;
797         Dimension               rw, rh ;
798
799         /* Position request always denied */
800
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 ;
805
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;
813
814         if( req->width == w->core.width &&
815             req->height == w->core.height &&
816             req->border_width == w->core.border_width )
817           return XtGeometryNo ;
818
819         rw = req->width + 2 * req->border_width ;
820         rh = req->height + 2 * req->border_width ;
821
822         /* find out how big the children want to be now */
823         MaxChild(tw, w, rw, rh) ;
824
825
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.
833          *
834          * A request to grow will be accepted only if the Tabs parent can
835          * grow to accommodate.
836          *
837          * TODO:
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
840          * for another day.
841          */
842
843         if (req->request_mode & (CWWidth | CWHeight | CWBorderWidth))
844         {
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 */
849
850           cw = tw->tabs.max_cw ;
851           ch = tw->tabs.max_ch ;
852
853           /* find out what *my* resulting preferred size would be */
854
855           PreferredSize2(tw, cw, ch, &wid, &hgt) ;
856
857           /* Would my size change?  If so, ask to be resized. */
858
859           if( wid != tw->core.width || hgt != tw->core.height )
860           {
861             Dimension   oldWid = tw->core.width, oldHgt = tw->core.height ;
862             XtWidgetGeometry    myrequest, myreply ;
863
864             myrequest.width = wid ;
865             myrequest.height = hgt ;
866             myrequest.request_mode = CWWidth | CWHeight ;
867
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.
870              */
871
872             if( (req->request_mode & XtCWQueryOnly)  || rw < cw || rh < ch )
873               myrequest.request_mode |= XtCWQueryOnly ;
874
875             result = XtMakeGeometryRequest((Widget)tw, &myrequest, &myreply) ;
876
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.
882              */
883             if( myrequest.request_mode & XtCWQueryOnly ) {
884               tw->core.width = oldWid ;
885               tw->core.height = oldHgt ;
886             }
887
888             /* based on the parent's response, determine what the
889              * resulting Tabs widget size would be.
890              */
891
892             switch( result ) {
893               case XtGeometryYes:
894               case XtGeometryDone:
895                 tw->tabs.needs_layout = True ;
896                 break ;
897
898               case XtGeometryNo:
899                 wid = tw->core.width ;
900                 hgt = tw->core.height ;
901                 break ;
902
903               case XtGeometryAlmost:
904                 wid = myreply.width ;
905                 hgt = myreply.height ;
906                 tw->tabs.needs_layout = True ;
907                 break ;
908             }
909           }
910
911           /* Within the constraints imposed by the parent, what is
912            * the max size we can give the child?
913            */
914           (void) TabLayout(tw, wid, hgt, &th, True) ;
915           aw = wid - 2*s ;
916           ah = hgt - th - 2*s ;
917
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.
921            */
922
923           if( rw == aw && rh == ah )
924           {
925             /* Acceptable.  If this wasn't a query, change *all* children
926              * to this size.
927              */
928             if( req->request_mode & XtCWQueryOnly )
929               return XtGeometryYes ;
930             else
931             {
932               Widget    *childP = tw->composite.children ;
933               int       i,bw ;
934               w->core.border_width = req->border_width ;
935               for(i=tw->tabs.displayChildren; --i >= 0; ++childP)
936                 if( XtIsManaged(*childP) )
937                 {
938                   bw = (*childP)->core.border_width ;
939                   XtConfigureWidget(*childP, s,tw->tabs.tab_total+s,
940                             rw-2*bw, rh-2*bw, bw) ;
941                 }
942 #ifdef  COMMENT
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) ;
946 #endif  /* COMMENT */
947               return XtGeometryDone ;
948             }
949           }
950
951           /* Cannot grant child's request.  Describe what we *can* do
952            * and return counter-offer.
953            */
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 ;
959         }
960
961         return XtGeometryYes ;
962 }
963
964
965
966
967         /* The number of children we manage has changed; recompute
968          * size from scratch.
969          */
970
971 static  void
972 TabsChangeManaged(Widget w)
973 {
974     TabsWidget  tw = (TabsWidget)w ;
975     Widget      *childP = tw->composite.children ;
976     int         i ;
977
978     if( tw->tabs.topWidget != NULL &&
979         ( !XtIsManaged(tw->tabs.topWidget) ||
980           tw->tabs.topWidget->core.being_destroyed ) )
981       tw->tabs.topWidget = NULL ;
982
983     GetPreferredSizes(tw) ;
984     MakeSizeRequest(tw) ;
985
986     XtClass(w)->core_class.resize(w) ;
987     if( XtIsRealized(w) )
988     {
989       Display *dpy = XtDisplay(w) ;
990       XClearWindow(dpy, XtWindow(w)) ;
991       XtClass(w)->core_class.expose(w,NULL,NULL) ;
992
993       /* make sure the top widget stays on top.  This requires
994        * making sure that all new children are realized first.
995        */
996       if( tw->tabs.topWidget != NULL && XtIsRealized(tw->tabs.topWidget) )
997       {
998         for(i=tw->tabs.displayChildren; --i >= 0; ++childP)
999           if( !XtIsRealized(*childP) )
1000             XtRealizeWidget(*childP) ;
1001
1002         XRaiseWindow(dpy, XtWindow(tw->tabs.topWidget)) ;
1003       }
1004     }
1005
1006 #ifdef  NEED_MOTIF
1007     /* Only top widget may receive input */
1008
1009     for(childP = tw->composite.children, i=tw->composite.num_children;
1010         --i >= 0;
1011         ++childP)
1012     {
1013       XtVaSetValues(*childP, XmNtraversalOn, False, 0) ;
1014     }
1015
1016     if( tw->tabs.topWidget != NULL )
1017       XtVaSetValues(tw->tabs.topWidget, XmNtraversalOn, True, 0) ;
1018 #endif
1019 }
1020
1021
1022 \f
1023
1024 /****************************************************************
1025  *
1026  * Action Procedures
1027  *
1028  ****************************************************************/
1029
1030
1031         /* User clicks on a tab, figure out which one it was. */
1032
1033 /* ARGSUSED */
1034 static  void
1035 TabsSelect(Widget w, XEvent *event, String *params, Cardinal *num_params)
1036 {
1037         TabsWidget      tw = (TabsWidget) w ;
1038         Widget  *childP ;
1039         Position x,y ;
1040         Dimension h = tw->tabs.tab_height ;
1041         int     i ;
1042
1043 #ifdef  NEED_MOTIF
1044         XmProcessTraversal (w, XmTRAVERSE_CURRENT) ;
1045 #endif
1046
1047         /* TODO: is there an Xmu function or something to do this instead? */
1048         switch( event->type ) {
1049           case ButtonPress:
1050           case ButtonRelease:
1051             x = event->xbutton.x ; y = event->xbutton.y ; break ;
1052           case KeyPress:
1053           case KeyRelease:
1054             x = event->xkey.x ; y = event->xkey.y ; break ;
1055           default:
1056             return ;
1057         }
1058
1059         /* TODO: determine which tab was clicked, if any.  Set that
1060          * widget to be top of stacking order with XawTabsSetTop().
1061          */
1062         for(i=0, childP=tw->composite.children;
1063               i < tw->tabs.displayChildren;
1064               ++i, ++childP)
1065           if( XtIsManaged(*childP) )
1066           {
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 )
1070             {
1071               if( *childP != tw->tabs.topWidget &&
1072                   (XtIsSensitive(*childP) || tw->tabs.selectInsensitive) )
1073                 XawTabsSetTop(*childP, True) ;
1074               break ;
1075             }
1076           }
1077 }
1078
1079
1080         /* User hits a key */
1081
1082 static  void
1083 TabsPage(Widget w, XEvent *event, String *params, Cardinal *num_params)
1084 {
1085         TabsWidget      tw = (TabsWidget) w ;
1086         Widget          newtop = NULL;
1087         Widget          *childP ;
1088         int             idx ;
1089         int             nc = tw->tabs.displayChildren ;
1090
1091         if( nc <= 0 )
1092           return ;
1093
1094         if( *num_params < 1 ) {
1095           XtAppWarning(XtWidgetToApplicationContext(w),
1096             "Tabs: page() action called with no arguments") ;
1097           return ;
1098         }
1099
1100         if( tw->tabs.topWidget == NULL )
1101           tw->tabs.topWidget = tw->composite.children[0] ;
1102
1103         for(idx=0, childP=tw->composite.children; idx < nc; ++idx, ++childP )
1104           if( tw->tabs.topWidget == *childP )
1105             break ;
1106
1107         switch( params[0][0] ) {
1108           case 'u':             /* up */
1109           case 'U':
1110             if( --idx < 0 )
1111               idx = nc-1 ;
1112             newtop = tw->composite.children[idx] ;
1113             break ;
1114
1115           case 'd':             /* down */
1116           case 'D':
1117             if( ++idx >= nc )
1118               idx = 0 ;
1119             newtop = tw->composite.children[idx] ;
1120             break ;
1121
1122           case 'h':
1123           case 'H':
1124           default:
1125               newtop = tw->composite.children[0] ;
1126               break ;
1127
1128           case 'e':
1129           case 'E':
1130               newtop = tw->composite.children[nc-1] ;
1131               break ;
1132
1133           case 's':             /* selected */
1134           case 'S':
1135               if( (newtop = tw->tabs.hilight) == NULL )
1136                 return ;
1137               break ;
1138         }
1139
1140         XawTabsSetTop(newtop, True) ;
1141 }
1142
1143
1144         /* User hits up/down key */
1145
1146 static  void
1147 TabsHighlight(Widget w, XEvent *event, String *params, Cardinal *num_params)
1148 {
1149         TabsWidget      tw = (TabsWidget) w ;
1150         Widget          newhl = NULL;
1151         Widget          *childP ;
1152         int             idx ;
1153         int             nc = tw->tabs.displayChildren ;
1154
1155         if( nc <= 0 )
1156           return ;
1157
1158         if( *num_params < 1 )
1159         {
1160           if( tw->tabs.hilight != NULL )
1161             DrawHighlight(tw, tw->tabs.hilight, False) ;
1162           return ;
1163         }
1164
1165         if( tw->tabs.hilight == NULL )
1166           newhl = tw->composite.children[0] ;
1167
1168         else
1169         {
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 )
1173               break ;
1174
1175           switch( params[0][0] ) {
1176             case 'u':           /* up */
1177             case 'U':
1178               if( --idx < 0 )
1179                 idx = nc-1 ;
1180               newhl = tw->composite.children[idx] ;
1181               break ;
1182
1183             case 'd':           /* down */
1184             case 'D':
1185               if( ++idx >= nc )
1186                 idx = 0 ;
1187               newhl = tw->composite.children[idx] ;
1188               break ;
1189
1190             case 'h':
1191             case 'H':
1192                 newhl = tw->composite.children[0] ;
1193                 break ;
1194
1195             case 'e':
1196             case 'E':
1197                 newhl = tw->composite.children[nc-1] ;
1198                 break ;
1199
1200             default:
1201                 newhl = tw->tabs.hilight ;
1202                 break ;
1203           }
1204         }
1205
1206         XawTabsSetHighlight(w, newhl) ;
1207 }
1208
1209
1210
1211 static  void
1212 TabsUnhighlight(Widget w, XEvent *event, String *params, Cardinal *num_params)
1213 {
1214         TabsWidget      tw = (TabsWidget) w ;
1215         int             nc = tw->composite.num_children ;
1216
1217         if( nc <= 0 )
1218           return ;
1219
1220         if( tw->tabs.hilight != NULL )
1221           DrawHighlight(tw, tw->tabs.hilight, True) ;
1222 }
1223
1224
1225
1226 \f
1227
1228 /****************************************************************
1229  *
1230  * Public Procedures
1231  *
1232  ****************************************************************/
1233
1234
1235         /* Set the top tab, optionally call all callbacks. */
1236 void
1237 XawTabsSetTop(Widget w, Bool callCallbacks)
1238 {
1239         TabsWidget      tw = (TabsWidget)w->core.parent ;
1240         TabsConstraints tab ;
1241         Widget          oldtop = tw->tabs.topWidget ;
1242
1243         if( !XtIsSubclass(w->core.parent, tabsWidgetClass) )
1244         {
1245           char line[256] ;
1246           sprintf(line, "XawTabsSetTop: widget \"%.64s\" is not the child of a tabs widget.", XtName(w)) ;
1247           XtAppWarning(XtWidgetToApplicationContext(w), line) ;
1248           return ;
1249         }
1250
1251         if( callCallbacks )
1252           XtCallCallbackList(w, tw->tabs.popdownCallbacks,
1253                 (XtPointer)tw->tabs.topWidget) ;
1254
1255         if( !XtIsRealized(w) ) {
1256           tw->tabs.topWidget = w ;
1257           tw->tabs.needs_layout = True ;
1258           return ;
1259         }
1260
1261         XRaiseWindow(XtDisplay(w), XtWindow(w)) ;
1262 #ifdef  NEED_MOTIF
1263         XtVaSetValues(oldtop, XmNtraversalOn, False, 0) ;
1264         XtVaSetValues(w, XmNtraversalOn, True, 0) ;
1265 #endif
1266
1267         tab = (TabsConstraints) w->core.constraints ;
1268         if( tab->tabs.row == 0 )
1269         {
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.
1274            */
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) ;
1283         }
1284         else
1285         {
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) ;
1290         }
1291
1292         XawTabsSetHighlight((Widget)tw, w) ;
1293
1294         if( callCallbacks )
1295           XtCallCallbackList(w, tw->tabs.callbacks, (XtPointer)w) ;
1296 }
1297
1298
1299         /* Set the top tab, optionally call all callbacks. */
1300 void
1301 XawTabsSetHighlight(Widget t, Widget w)
1302 {
1303         TabsWidget      tw = (TabsWidget)t ;
1304
1305         if( !XtIsSubclass(t, tabsWidgetClass) )
1306           return ;
1307
1308         if( XtIsRealized(t) && w != tw->tabs.hilight )
1309         {
1310           if( tw->tabs.hilight != NULL )
1311             DrawHighlight(tw, tw->tabs.hilight, True) ;
1312           if( w != NULL )
1313             DrawHighlight(tw, w, False) ;
1314         }
1315
1316         tw->tabs.hilight = w ;
1317 }
1318
1319
1320 \f
1321
1322 /****************************************************************
1323  *
1324  * Private Procedures
1325  *
1326  ****************************************************************/
1327
1328
1329 static  void
1330 TabsAllocGCs(TabsWidget tw)
1331 {
1332         TabsAllocFgGC(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) ;
1339 }
1340
1341
1342 static  void
1343 TabsFreeGCs(TabsWidget tw)
1344 {
1345         Widget w = (Widget) tw;
1346
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) ;
1352 #ifdef HAVE_XMU
1353         XmuReleaseStippledPixmap(XtScreen(w), tw->tabs.grey50) ;
1354 #endif
1355 }
1356
1357
1358
1359
1360
1361         /* Redraw entire Tabs widget */
1362
1363 static  void
1364 DrawTabs(TabsWidget tw, Bool labels)
1365 {
1366         Widget          *childP ;
1367         int             i,j ;
1368         Dimension       s = SHADWID ;
1369         Dimension       th = tw->tabs.tab_height ;
1370         Position        y ;
1371         TabsConstraints tab ;
1372
1373         if( !XtIsRealized((Widget)tw))
1374           return ;
1375  
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.)
1379          */
1380
1381         y = tw->tabs.numRows == 1 ? TABDELTA : 0 ;
1382         for(i=0; i<tw->tabs.numRows; ++i, y += th)
1383         {
1384           for( j=tw->tabs.displayChildren, childP=tw->composite.children;
1385               --j >= 0; ++childP )
1386             if( XtIsManaged(*childP) )
1387             {
1388               tab = (TabsConstraints)(*childP)->core.constraints;
1389               if( tab->tabs.row == i && *childP != tw->tabs.topWidget )
1390                 DrawTab(tw, *childP, labels) ;
1391             }
1392           if( i != tw->tabs.numRows -1 )
1393             DrawTrim(tw, 0,y+th, tw->core.width, th+s, False,False) ;
1394         }
1395
1396         DrawFrame(tw) ;
1397
1398         /* and now the top tab */
1399         if( tw->tabs.topWidget != NULL )
1400           DrawTab(tw, tw->tabs.topWidget, labels) ;
1401 }
1402
1403
1404
1405 /* Draw one tab.  Corners are rounded very slightly. */
1406
1407 static  void
1408 DrawTab(TabsWidget tw, Widget child, Bool labels)
1409 {
1410         GC      gc ;
1411         int     x,y ;
1412
1413         if( !XtIsRealized((Widget)tw))
1414           return ;
1415
1416         DrawBorder(tw, child, False) ;
1417
1418         if( labels )
1419         {
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) ;
1425
1426           if( XtIsSensitive(child) )
1427           {
1428             gc = tw->tabs.foregroundGC ;
1429             XSetForeground(dpy, gc, tab->tabs.foreground) ;
1430           }
1431           else
1432           {
1433             /* grey pixel allocation deferred until now */
1434             if( !tab->tabs.greyAlloc )
1435             {
1436               if( tw->tabs.be_nice_to_cmap || tw->core.depth == 1 )
1437                 tab->tabs.grey = tab->tabs.foreground ;
1438               else
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 ;
1444             }
1445             gc = tw->tabs.greyGC ;
1446             XSetForeground(dpy, gc, tab->tabs.grey) ;
1447           }
1448
1449           x = tab->tabs.x ;
1450           y = tab->tabs.y ;
1451           if( child == tw->tabs.topWidget )
1452             y -= TABLDELTA ;
1453
1454           if( tab->tabs.left_bitmap != None && tab->tabs.lbm_width > 0 )
1455           {
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) ;
1460             else
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) ;
1464           }
1465
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)) ;
1470         }
1471
1472         if( child == tw->tabs.hilight )
1473           DrawHighlight(tw, child, False) ;
1474 }
1475
1476
1477         /* draw frame all the way around the child windows. */
1478
1479 static  void
1480 DrawFrame(TabsWidget tw)
1481 {
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) ;
1488 }
1489
1490
1491         /* draw trim around a tab or underneath a row of tabs */
1492
1493 static  void
1494 DrawTrim(TabsWidget tw,         /* widget */
1495         int     x,              /* upper-left corner */
1496         int     y,
1497         int     wid,            /* total size */
1498         int     hgt,
1499         Bool    bottom,         /* draw bottom? */
1500         Bool    undraw)         /* undraw all */
1501 {
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 ;
1507         if( bottom )
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 */
1514 }
1515
1516
1517 /* Draw one tab border. */
1518
1519 static  void
1520 DrawBorder(TabsWidget tw, Widget child, Bool undraw)
1521 {
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 ;
1527
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?
1532          */
1533         if( child == tw->tabs.topWidget )
1534         {
1535           Display       *dpy = XtDisplay((Widget)tw) ;
1536           Window        win = XtWindow((Widget)tw) ;
1537           GC            bggc = tw->tabs.backgroundGC ;
1538           XRectangle    rects[3] ;
1539           x -= TABDELTA ;
1540           y -= TABDELTA ;
1541           twid += TABDELTA*2 ;
1542           thgt += TABDELTA ;
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) ;
1547         }
1548
1549         DrawTrim(tw, x,y,twid,thgt+1, child == tw->tabs.topWidget, undraw) ;
1550 }
1551
1552
1553 /* Draw highlight around tab that has focus */
1554
1555 static  void
1556 DrawHighlight(TabsWidget tw, Widget child, Bool undraw)
1557 {
1558         TabsConstraints tab = (TabsConstraints)child->core.constraints;
1559         Display         *dpy = XtDisplay((Widget)tw) ;
1560         Window          win = XtWindow((Widget)tw) ;
1561         GC              gc ;
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 ;
1566         XPoint          points[6] ;
1567
1568         /* top tab does not have a highlight */
1569
1570         if( child == tw->tabs.topWidget )
1571           return ;
1572
1573         if( undraw )
1574           gc = tw->tabs.backgroundGC ;
1575
1576         else if( XtIsSensitive(child) )
1577         {
1578           gc = tw->tabs.foregroundGC ;
1579           XSetForeground(dpy, gc, tab->tabs.foreground) ;
1580         }
1581         else
1582         {
1583           gc = tw->tabs.greyGC ;
1584           XSetForeground(dpy, gc, tab->tabs.grey) ;
1585         }
1586
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 ;
1593
1594         XDrawLines(dpy,win,gc, points,6, CoordModeOrigin) ;
1595 }
1596
1597
1598 /* Undraw one tab interior */
1599
1600 static  void
1601 UndrawTab(TabsWidget tw, Widget child)
1602 {
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 ;
1611
1612         XFillRectangle(dpy,win,bggc, x,y, twid,thgt) ;
1613 }
1614
1615
1616
1617
1618 \f
1619         /* GEOMETRY UTILITIES */
1620
1621         /* Overview:
1622          *
1623          *  MaxChild(): ask all children (except possibly one) their
1624          *  preferred sizes, set max_cw, max_ch accordingly.
1625          *
1626          *  GetPreferredSizes(): ask all children their preferred sizes,
1627          *  set max_cw, max_ch accordingly.
1628          *
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.
1632          *
1633          *  PreferredSize2(): Given child dimensions, return Tabs
1634          *  widget dimensions.
1635          *
1636          *  PreferredSize3(): Same, except given child dimensions plus
1637          *  shadow.
1638          */
1639
1640
1641         /* Compute the width of one child's tab.  Positions will be computed
1642          * elsewhere.
1643          *
1644          *      height: font height + vertical_space*2 + shadowWid*2
1645          *      width:  string width + horizontal_space*2 + shadowWid*2
1646          *
1647          * All tabs are the same height, so that is computed elsewhere.
1648          */
1649
1650 static  void
1651 TabWidth(Widget w)
1652 {
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 ;
1659
1660         tab->tabs.width = iw + SHADWID*2 ;
1661         tab->tabs.l_x = tab->tabs.lbm_x = SHADWID + iw ;
1662
1663         if( tab->tabs.left_bitmap != None )
1664         {
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 ;
1668         }
1669
1670         if( lbl != NULL && font != NULL )
1671         {
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 ;
1676         }
1677 }
1678
1679
1680
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.
1685          *
1686          * Tabs are indented from the edges by INDENT.
1687          *
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.
1690          */
1691
1692 static  int
1693 TabLayout(TabsWidget tw, int wid, int hgt, Dimension *reply_height, Bool query_only)
1694 {
1695         int             i, row ;
1696         int             num_children = tw->composite.num_children ;
1697         Widget          *childP ;
1698         Dimension       w ;
1699         Position        x,y ;
1700         TabsConstraints tab ;
1701
1702         if (!query_only)
1703           tw->tabs.displayChildren = 0;
1704
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.
1708          */
1709
1710         if( num_children > 0 )
1711         {
1712           /* Loop through the tabs and see how much space they need. */
1713
1714           row = 0 ;
1715           x = INDENT ;
1716           y = 0 ;
1717           wid -= INDENT ;
1718           for(i=num_children, childP=tw->composite.children; --i >= 0; ++childP)
1719             if( XtIsManaged(*childP) )
1720             {
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)
1725                   break;
1726                 ++row ;
1727                 x = INDENT ;
1728                 y += tw->tabs.tab_height ;
1729               }
1730               if( !query_only ) {
1731                 tab->tabs.x = x ;
1732                 tab->tabs.y = y ;
1733                 tab->tabs.row = row ;
1734               }
1735               x += w + SPACING ;
1736               if (!query_only)
1737                 tw->tabs.displayChildren++;
1738             }
1739           /* If there was only one row, increse the height by TABDELTA */
1740           if( ++row == 1 )
1741           {
1742             y = TABDELTA ;
1743             if( !query_only )
1744               for(i=num_children, childP=tw->composite.children;
1745                     --i >= 0 ; ++childP)
1746                 if( XtIsManaged(*childP) )
1747                 {
1748                   tab = (TabsConstraints) (*childP)->core.constraints ;
1749                   tab->tabs.y = y ;
1750                 }
1751           }
1752           y += tw->tabs.tab_height ;
1753         }
1754         else
1755           row = y = 0 ;
1756
1757         if( !query_only ) {
1758           tw->tabs.tab_total = y ;
1759           tw->tabs.numRows = row ;
1760         }
1761
1762         if( reply_height != NULL )
1763           *reply_height = y ;
1764
1765         return row ;
1766 }
1767
1768
1769
1770         /* Find max preferred child size.  Returned sizes include child
1771          * border widths.
1772          */
1773
1774 static  void
1775 GetPreferredSizes(TabsWidget tw)
1776 {
1777         MaxChild(tw, NULL, 0,0) ;
1778 }
1779
1780
1781
1782         /* Find max preferred child size.  Returned sizes include child
1783          * border widths.  If except is non-null, don't ask that one.
1784          */
1785
1786 static  void
1787 MaxChild(TabsWidget tw, Widget except, Dimension cw, Dimension ch)
1788 {
1789         int                     i ;
1790         Widget                  *childP = tw->composite.children ;
1791         XtWidgetGeometry        preferred ;
1792
1793         for(i=tw->composite.num_children; --i >=0; ++childP)
1794           if( XtIsManaged(*childP)  &&  *childP != except )
1795           {
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 ) ;
1799           }
1800
1801         tw->tabs.max_cw = cw ;
1802         tw->tabs.max_ch = ch ;
1803 }
1804
1805
1806
1807         /* rotate row numbers to bring current widget to bottom row,
1808          * compute y positions for all tabs
1809          */
1810
1811 static  void
1812 TabsShuffleRows(TabsWidget tw)
1813 {
1814         TabsConstraints tab ;
1815         int             move ;
1816         int             nrows ;
1817         Widget          *childP ;
1818         Dimension       th = tw->tabs.tab_height ;
1819         Position        bottom ;
1820         int             i ;
1821
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;
1825               --i >= 0;
1826               ++childP)
1827             if( XtIsManaged(*childP) ) {
1828               tw->tabs.topWidget = *childP ;
1829               break ;
1830             }
1831
1832         if( tw->tabs.topWidget != NULL )
1833         {
1834           nrows = tw->tabs.numRows ;
1835           assert( nrows > 0 ) ;
1836
1837           if( nrows > 1 )
1838           {
1839             tab = (TabsConstraints) tw->tabs.topWidget->core.constraints ;
1840             assert( tab != NULL ) ;
1841
1842             /* how far to move top row */
1843             move = nrows - tab->tabs.row ;
1844             bottom = tw->tabs.tab_total - th ;
1845
1846             for(i=tw->tabs.displayChildren, childP=tw->composite.children;
1847                   --i >= 0;
1848                   ++childP)
1849               if( XtIsManaged(*childP) )
1850               {
1851                 tab = (TabsConstraints) (*childP)->core.constraints ;
1852                 tab->tabs.row = (tab->tabs.row + move) % nrows ;
1853                 tab->tabs.y = bottom - tab->tabs.row * th ;
1854               }
1855           }
1856         }
1857 }
1858
1859
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.
1864          *
1865          * This function requires that max_cw, max_ch already be set.
1866          */
1867
1868 static  int
1869 PreferredSize(
1870         TabsWidget      tw,
1871         Dimension       *reply_width,           /* total widget size */
1872         Dimension       *reply_height,
1873         Dimension       *reply_cw,              /* child widget size */
1874         Dimension       *reply_ch)
1875 {
1876         Dimension       cw,ch ;         /* child width, height */
1877         Dimension       wid,hgt ;
1878         Dimension       rwid,rhgt ;
1879         int             nrow ;
1880
1881         wid = cw = tw->tabs.max_cw ;
1882         hgt = ch = tw->tabs.max_ch ;
1883
1884         nrow = PreferredSize2(tw, wid,hgt, &rwid, &rhgt) ;
1885
1886         /* Check for absurd results (more than 2 rows, high aspect
1887          * ratio).  Try wider size if needed.
1888          * TODO: make sure this terminates.
1889          */
1890
1891         if( nrow > 2 && rhgt > rwid )
1892         {
1893           Dimension w0, w1 ;
1894           int maxloop = 20 ;
1895
1896           /* step 1: start doubling size until it's too big */
1897           do {
1898             w0 = wid ;
1899             wid = Max(wid*2, wid+20) ;
1900             nrow = PreferredSize2(tw, wid,hgt, &rwid,&rhgt) ;
1901           } while( nrow > 2 && rhgt > rwid ) ;
1902           w1 = wid ;
1903
1904           /* step 2: use Newton's method to find ideal size.  Stop within
1905            * 8 pixels.
1906            */
1907           while( --maxloop > 0 && w1 > w0 + 8 )
1908           {
1909             wid = (w0+w1)/2 ;
1910             nrow = PreferredSize2(tw, wid,hgt, &rwid,&rhgt) ;
1911             if( nrow > 2 && rhgt > rwid )
1912               w0 = wid ;
1913             else
1914               w1 = wid ;
1915           }
1916           wid = w1 ;
1917         }
1918
1919         *reply_width = rwid ;
1920         *reply_height = rhgt ;
1921         if( reply_cw != NULL ) *reply_cw = cw ;
1922         if( reply_ch != NULL ) *reply_ch = ch ;
1923         return nrow ;
1924 }
1925
1926
1927         /* Find preferred size, given size of children. */
1928
1929 static  int
1930 PreferredSize2(
1931         TabsWidget      tw,
1932         int             cw,             /* child width, height */
1933         int             ch,
1934         Dimension       *reply_width,   /* total widget size */
1935         Dimension       *reply_height)
1936 {
1937         Dimension       s = SHADWID ;
1938
1939         /* make room for shadow frame */
1940         cw += s*2 ;
1941         ch += s*2 ;
1942
1943         return PreferredSize3(tw, cw, ch, reply_width, reply_height) ;
1944 }
1945
1946
1947         /* Find preferred size, given size of children+shadow. */
1948
1949 static  int
1950 PreferredSize3(
1951         TabsWidget      tw,
1952         int             wid,            /* child width, height */
1953         int             hgt,
1954         Dimension       *reply_width,   /* total widget size */
1955         Dimension       *reply_height)
1956 {
1957         Dimension       th ;            /* space used by tabs */
1958         int             nrows ;
1959
1960         if( tw->composite.num_children > 0 )
1961           nrows = TabLayout(tw, wid, hgt, &th, True) ;
1962         else {
1963           th = 0 ;
1964           nrows = 0 ;
1965         }
1966
1967         *reply_width = Max(wid, MIN_WID) ;
1968         *reply_height = Max(th+hgt, MIN_HGT) ;
1969
1970         return nrows ;
1971 }
1972
1973
1974 static  void
1975 MakeSizeRequest(TabsWidget tw)
1976 {
1977         Widget                  w = (Widget)tw ;
1978         XtWidgetGeometry        request, reply ;
1979         XtGeometryResult        result ;
1980         Dimension               cw,ch ;
1981
1982         request.request_mode = CWWidth | CWHeight ;
1983         PreferredSize(tw, &request.width, &request.height, &cw, &ch) ;
1984
1985         if( request.width == tw->core.width &&
1986             request.height == tw->core.height )
1987           return ;
1988
1989         result = XtMakeGeometryRequest(w, &request, &reply) ;
1990
1991         if( result == XtGeometryAlmost )
1992         {
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.
1998            */
1999
2000           if( reply.width < request.width && reply.height >= request.height )
2001           {
2002             Dimension s = SHADWID ;
2003             ch += s*2 ;
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) ;
2008           }
2009
2010           else
2011             (void) XtMakeGeometryRequest(w, &reply, NULL) ;
2012         }
2013 }
2014
2015
2016 static  void
2017 getBitmapInfo(TabsWidget tw, TabsConstraints tab)
2018 {
2019         Window root ;
2020         int     x,y ;
2021         unsigned int bw ;
2022
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 ;
2028 }
2029
2030 \f
2031
2032
2033         /* Code copied & modified from Gcs.c.  This version has dynamic
2034          * foreground.
2035          */
2036
2037 static  void
2038 TabsAllocFgGC(TabsWidget tw)
2039 {
2040         Widget          w = (Widget) tw;
2041         XGCValues       values ;
2042
2043         values.background = tw->core.background_pixel ;
2044         values.font = tw->tabs.font->fid ;
2045         values.line_style = LineOnOffDash ;
2046         values.line_style = LineSolid ;
2047
2048         tw->tabs.foregroundGC =
2049           XtAllocateGC(w, w->core.depth,
2050             GCBackground|GCFont|GCLineStyle, &values,
2051             GCForeground,
2052             GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2053                 GCDashList|GCArcMode) ;
2054 }
2055
2056 static  void
2057 TabsAllocGreyGC(TabsWidget tw)
2058 {
2059         Widget          w = (Widget) tw;
2060         XGCValues       values ;
2061
2062         values.background = tw->core.background_pixel ;
2063         values.font = tw->tabs.font->fid ;
2064 #ifdef HAVE_XMU
2065         if( tw->tabs.be_nice_to_cmap || w->core.depth == 1)
2066         {
2067           values.fill_style = FillStippled ;
2068           tw->tabs.grey50 =
2069           values.stipple = XmuCreateStippledPixmap(XtScreen(w), 1L, 0L, 1) ;
2070
2071           tw->tabs.greyGC =
2072             XtAllocateGC(w, w->core.depth,
2073               GCBackground|GCFont|GCStipple|GCFillStyle, &values,
2074               GCForeground,
2075               GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2076                   GCDashList|GCArcMode) ;
2077         }
2078         else
2079 #endif
2080         {
2081           tw->tabs.greyGC =
2082             XtAllocateGC(w, w->core.depth,
2083               GCFont, &values,
2084               GCForeground,
2085               GCBackground|GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2086                   GCDashList|GCArcMode) ;
2087         }
2088 }