(A-IWDSU+271C4): New abstract node.
[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  #### This file contains essential XEmacs related fixes to the original
24  verison of the Tabs widget. Be VERY careful about syncing if you ever
25  update to a more recent version. In general this is probably now a
26  bad idea. */
27
28  /*
29  * Tabs.c - Index Tabs composite widget
30  *
31  * Author: Edward A. Falk
32  *         falk@falconer.vip.best.com
33  *
34  * Date: July 29, 1997
35  *
36  *
37  * Overall layout of this widget is as follows:
38  *
39  *   ________ ,---------. _________
40  *  |  label ||  Label  ||  Label  |  \ tabs
41  *  |________||         ||_________|  /
42  *  |+----------------------------+|  \
43  *  ||                            ||  |
44  *  ||  child widget window       ||   > frame
45  *  |+----------------------------+|  |
46  *  +------------------------------+  /
47  *
48  * The height of the tabs includes the shadow width, top and bottom
49  * margins, and the height of the text.
50  *
51  * The height of the frame includes the top and bottom shadow width and the
52  * size of the child widget window.
53  *
54  * The tabs overlap the frame and each other vertically by the shadow
55  * width, so that when the topmost tab is drawn, it obliterates part of
56  * the frame.
57  */
58
59 /*
60  * TODO: min child height = tab height
61  */
62
63 #include        <config.h>
64 #include        <stdio.h>
65
66 #include        <X11/Xlib.h>
67 #include        <X11/IntrinsicP.h>
68 #include        <X11/StringDefs.h>
69
70 #include        "lwlib-internal.h"
71 #include        "../src/xmu.h"
72 #include        "xlwtabsP.h"
73 #include        "xlwgcs.h"
74
75 #define MIN_WID         10
76 #define MIN_HGT         10
77 #define INDENT          3       /* tabs indented from edge by this much */
78 #define SPACING         0       /* distance between tabs */
79 #define SHADWID         1       /* default shadow width */
80 #define TABDELTA        2       /* top tab grows this many pixels */
81 #define TABLDELTA       2       /* top tab label offset this many pixels */
82
83
84 /****************************************************************
85  *
86  * IndexTabs Resources
87  *
88  ****************************************************************/
89
90 static  char    defaultTranslations[] = "\
91         <BtnUp>:                select()        \n\
92         <FocusIn>:              highlight()     \n\
93         <FocusOut>:             unhighlight()   \n\
94         <Key>Page_Up:           page(up)        \n\
95         <Key>KP_Page_Up:        page(up)        \n\
96         <Key>Prior:             page(up)        \n\
97         <Key>KP_Prior:          page(up)        \n\
98         <Key>Page_Down:         page(down)      \n\
99         <Key>KP_Page_Down:      page(down)      \n\
100         <Key>Next:              page(down)      \n\
101         <Key>KP_Next:           page(down)      \n\
102         <Key>Home:              page(home)      \n\
103         <Key>KP_Home:           page(home)      \n\
104         <Key>End:               page(end)       \n\
105         <Key>KP_End:            page(end)       \n\
106         <Key>Up:                highlight(up)   \n\
107         <Key>KP_Up:             highlight(up)   \n\
108         <Key>Down:              highlight(down) \n\
109         <Key>KP_Down:           highlight(down) \n\
110         <Key> :                 page(select)    \n\
111          " ;
112
113 static  char    accelTable[] = "        #augment\n\
114         <Key>Page_Up:           page(up)        \n\
115         <Key>KP_Page_Up:        page(up)        \n\
116         <Key>Prior:             page(up)        \n\
117         <Key>KP_Prior:          page(up)        \n\
118         <Key>Page_Down:         page(down)      \n\
119         <Key>KP_Page_Down:      page(down)      \n\
120         <Key>Next:              page(down)      \n\
121         <Key>KP_Next:           page(down)      \n\
122         <Key>Home:              page(home)      \n\
123         <Key>KP_Home:           page(home)      \n\
124         <Key>End:               page(end)       \n\
125         <Key>KP_End:            page(end)       \n\
126         <Key>Up:                highlight(up)   \n\
127         <Key>KP_Up:             highlight(up)   \n\
128         <Key>Down:              highlight(down) \n\
129         <Key>KP_Down:           highlight(down) \n\
130         <Key> :                 page(select)    \n\
131          " ;
132 static  XtAccelerators  defaultAccelerators ; /* #### Never used */
133
134 #define offset(field)   XtOffsetOf(TabsRec, tabs.field)
135 static XtResource resources[] = {
136
137   {XtNselectInsensitive, XtCSelectInsensitive, XtRBoolean, sizeof(Boolean),
138         offset(selectInsensitive), XtRImmediate, (XtPointer) True},
139   {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
140         offset(font), XtRString, (XtPointer) XtDefaultFont},
141   {XtNinternalWidth, XtCWidth, XtRDimension, sizeof(Dimension),
142         offset(internalWidth), XtRImmediate, (XtPointer)4 },
143   {XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
144         offset(internalHeight), XtRImmediate, (XtPointer)4 },
145   {XtNborderWidth, XtCBorderWidth, XtRDimension, sizeof(Dimension),
146         XtOffsetOf(RectObjRec,rectangle.border_width), XtRImmediate, (XtPointer)0},
147   {XtNtopWidget, XtCTopWidget, XtRWidget, sizeof(Widget),
148         offset(topWidget), XtRImmediate, NULL},
149   {XtNhighlightWidget, XtCHighlightWidget, XtRWidget, sizeof(Widget),
150         offset(hilight), XtRImmediate, NULL},
151   {XtNcallback, XtCCallback, XtRCallback, sizeof(XtPointer),
152         offset(callbacks), XtRCallback, NULL},
153   {XtNpopdownCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
154         offset(popdownCallbacks), XtRCallback, NULL},
155   {XtNbeNiceToColormap, XtCBeNiceToColormap, XtRBoolean, sizeof(Boolean),
156         offset(be_nice_to_cmap), XtRImmediate, (XtPointer) True},
157   {XtNtopShadowContrast, XtCTopShadowContrast, XtRInt, sizeof(int),
158         offset(top_shadow_contrast), XtRImmediate, (XtPointer) 20},
159   {XtNbottomShadowContrast, XtCBottomShadowContrast, XtRInt, sizeof(int),
160         offset(bot_shadow_contrast), XtRImmediate, (XtPointer) 40},
161   {XtNinsensitiveContrast, XtCInsensitiveContrast, XtRInt, sizeof(int),
162         offset(insensitive_contrast), XtRImmediate, (XtPointer) 33},
163   {XtNaccelerators, XtCAccelerators, XtRAcceleratorTable,sizeof(XtTranslations),
164         XtOffsetOf(TabsRec,core.accelerators), XtRString, accelTable},
165 };
166 #undef  offset
167
168
169
170         /* constraint resources */
171
172 #define offset(field)   XtOffsetOf(TabsConstraintsRec, tabs.field)
173 static XtResource tabsConstraintResources[] = {
174   {XtNtabLabel, XtCLabel, XtRString, sizeof(String),
175         offset(label), XtRString, NULL},
176   {XtNtabLeftBitmap, XtCLeftBitmap, XtRBitmap, sizeof(Pixmap),
177         offset(left_bitmap), XtRImmediate, None},
178   {XtNtabForeground, XtCForeground, XtRPixel, sizeof(Pixel),
179         offset(foreground), XtRString, (XtPointer) XtDefaultForeground},
180   {XtNresizable, XtCResizable, XtRBoolean, sizeof(Boolean),
181         offset(resizable), XtRImmediate, (XtPointer) True},
182 } ;
183 #undef  offset
184
185
186
187
188 #if     !NeedFunctionPrototypes
189
190         /* FORWARD REFERENCES: */
191
192         /* member functions */
193
194 static  void    TabsClassInit();
195 static  void    TabsInit();
196 static  void    TabsResize();
197 static  void    TabsExpose();
198 static  void    TabsDestroy();
199 static  void    TabsRealize();
200 static  Boolean TabsSetValues();
201 static  Boolean TabsAcceptFocus();
202 static  XtGeometryResult        TabsQueryGeometry();
203 static  XtGeometryResult        TabsGeometryManager();
204 static  void    TabsChangeManaged();
205 static  void    TabsConstraintInitialize() ;
206 static  Boolean TabsConstraintSetValues() ;
207
208         /* action procs */
209
210 static  void    TabsSelect() ;
211 static  void    TabsPage() ;
212 static  void    TabsHighlight() ;
213 static  void    TabsUnhighlight() ;
214
215         /* internal privates */
216
217 static  void    TabsAllocGCs() ;        /* get rendering GCs */
218 static  void    TabsFreeGCs() ;         /* return rendering GCs */
219 static  void    DrawTabs() ;            /* draw all tabs */
220 static  void    DrawTab() ;             /* draw one index tab */
221 static  void    DrawFrame() ;           /* draw frame around contents */
222 static  void    DrawTrim() ;            /* draw trim around a tab */
223 static  void    DrawBorder() ;          /* draw border */
224 static  void    DrawHighlight() ;       /* draw highlight */
225 static  void    UndrawTab() ;           /* undraw interior of a tab */
226 static  void    TabWidth() ;            /* recompute tab size */
227 static  void    GetPreferredSizes() ;   /* query all children for their sizes */
228 static  void    MaxChild() ;            /* find max preferred child size */
229 static  int     PreferredSize() ;       /* compute preferred size */
230 static  int     PreferredSize2() ;      /* compute preferred size */
231 static  int     PreferredSize3() ;      /* compute preferred size */
232 static  void    MakeSizeRequest() ;     /* try to change size */
233 static  void    getBitmapInfo() ;
234 static  int     TabLayout() ;           /* lay out tabs */
235 static  void    TabsShuffleRows() ;     /* bring current tab to bottom row */
236
237 static  void    TabsAllocFgGC() ;
238 static  void    TabsAllocGreyGC() ;
239
240 #else
241
242 static  void    TabsClassInit(void) ;
243 static  void    TabsInit( Widget req, Widget new, ArgList, Cardinal *nargs) ;
244 static  void    TabsConstraintInitialize(Widget, Widget, ArgList, Cardinal *) ;
245 static  void    TabsRealize(Widget, Mask *, XSetWindowAttributes *) ;
246 static  void    TabsDestroy( Widget w) ;
247 static  void    TabsResize( Widget w) ;
248 static  void    TabsExpose( Widget w, XEvent *event, Region region) ;
249 static  Boolean TabsSetValues(Widget, Widget, Widget, ArgList, Cardinal *) ;
250 static  Boolean TabsAcceptFocus(Widget, Time *);
251 static  Boolean TabsConstraintSetValues(Widget, Widget, Widget,
252                         ArgList, Cardinal *) ;
253 static  XtGeometryResult TabsQueryGeometry(Widget,
254                                 XtWidgetGeometry *, XtWidgetGeometry *) ;
255 static  XtGeometryResult TabsGeometryManager(Widget,
256                                 XtWidgetGeometry *, XtWidgetGeometry *) ;
257 static  void    TabsChangeManaged( Widget w) ;
258
259 static  void    TabsSelect(Widget, XEvent *, String *, Cardinal *) ;
260 static  void    TabsPage(Widget, XEvent *, String *, Cardinal *) ;
261 static  void    TabsHighlight(Widget, XEvent *, String *, Cardinal *) ;
262 static  void    TabsUnhighlight(Widget, XEvent *, String *, Cardinal *) ;
263
264 static  void    DrawTabs( TabsWidget tw, Bool labels) ;
265 static  void    DrawTab( TabsWidget tw, Widget child, Bool labels) ;
266 static  void    DrawFrame( TabsWidget tw) ;
267 static  void    DrawTrim( TabsWidget, int x, int y,
268                   int wid, int hgt, Bool bottom, Bool undraw) ;
269 static  void    DrawBorder( TabsWidget tw, Widget child, Bool undraw) ;
270 static  void    DrawHighlight( TabsWidget tw, Widget child, Bool undraw) ;
271 static  void    UndrawTab( TabsWidget tw, Widget child) ;
272
273 static  void    TabWidth( Widget w) ;
274 static  int     TabLayout( TabsWidget, Dimension wid, Dimension hgt, Dimension *r_hgt,
275                         Bool query_only) ;
276 static  void    GetPreferredSizes(TabsWidget) ;
277 static  void    MaxChild(TabsWidget, Widget except, Dimension, Dimension) ;
278 static  void    TabsShuffleRows( TabsWidget tw) ;
279 static  int     PreferredSize( TabsWidget,
280                         Dimension *reply_width, Dimension *reply_height,
281                         Dimension *reply_cw, Dimension *reply_ch) ;
282 static  int     PreferredSize2( TabsWidget, Dimension cw, Dimension ch,
283                         Dimension *rw, Dimension *rh) ;
284 static  int     PreferredSize3( TabsWidget, Dimension wid, Dimension hgt,
285                         Dimension *rw, Dimension *rh) ;
286 static  void    MakeSizeRequest(TabsWidget) ;
287
288 static  void    TabsAllocGCs(TabsWidget) ;
289 static  void    TabsFreeGCs(TabsWidget) ;
290 static  void    getBitmapInfo( TabsWidget tw, TabsConstraints tab) ;
291 static  void    TabsAllocFgGC( TabsWidget tw) ;
292 static  void    TabsAllocGreyGC( TabsWidget tw) ;
293
294 #endif
295
296 #define AddRect(i,xx,yy,w,h)    \
297   do{rects[(i)].x=(xx); rects[i].y=(yy);        \
298      rects[i].width=(w); rects[i].height=(h);}while(0)
299
300 static  XtActionsRec    actionsList[] =
301   {
302     {"select",  TabsSelect},
303     {"page",    TabsPage},
304     {"highlight", TabsHighlight},
305     {"unhighlight", TabsUnhighlight},
306   } ;
307
308
309 /****************************************************************
310 *
311 * Full class record constant
312 *
313 ****************************************************************/
314
315 #ifndef NEED_MOTIF
316 #define SuperClass      (&constraintClassRec)
317 #else
318 #define SuperClass      (&xmManagerClassRec)
319 #endif
320
321 TabsClassRec tabsClassRec = {
322   {
323 /* core_class fields      */
324     /* superclass         */    (WidgetClass) SuperClass,
325     /* class_name         */    "Tabs",
326     /* widget_size        */    sizeof(TabsRec),
327     /* class_initialize   */    TabsClassInit,
328     /* class_part_init    */    NULL,                   /* TODO? */
329     /* class_inited       */    FALSE,
330     /* initialize         */    TabsInit,
331     /* initialize_hook    */    NULL,
332     /* realize            */    TabsRealize,
333     /* actions            */    actionsList,
334     /* num_actions        */    XtNumber(actionsList),
335     /* resources          */    resources,
336     /* num_resources      */    XtNumber(resources),
337     /* xrm_class          */    NULLQUARK,
338     /* compress_motion    */    TRUE,
339 #if XtSpecificationRelease < 6
340     /* compress_exposure  */    XtExposeCompressMaximal,
341 #else
342     /* compress_exposure  */    XtExposeCompressMaximal|XtExposeNoRegion,
343 #endif
344     /* compress_enterleave*/    TRUE,
345     /* visible_interest   */    TRUE,
346     /* destroy            */    TabsDestroy,
347     /* resize             */    TabsResize,
348     /* expose             */    TabsExpose,
349     /* set_values         */    TabsSetValues,
350     /* set_values_hook    */    NULL,
351     /* set_values_almost  */    XtInheritSetValuesAlmost,
352     /* get_values_hook    */    NULL,
353     /* accept_focus       */    TabsAcceptFocus,
354     /* version            */    XtVersion,
355     /* callback_private   */    NULL,
356     /* tm_table           */    defaultTranslations,
357     /* query_geometry     */    TabsQueryGeometry,
358     /* display_accelerator*/    XtInheritDisplayAccelerator,
359     /* extension          */    NULL
360   },
361   {
362 /* composite_class fields */
363     /* geometry_manager   */    TabsGeometryManager,
364     /* change_managed     */    TabsChangeManaged,
365     /* insert_child       */    XtInheritInsertChild,   /* TODO? */
366     /* delete_child       */    XtInheritDeleteChild,   /* TODO? */
367     /* extension          */    NULL
368   },
369   {
370 /* constraint_class fields */
371     /* subresources       */    tabsConstraintResources,
372     /* subresource_count  */    XtNumber(tabsConstraintResources),
373     /* constraint_size    */    sizeof(TabsConstraintsRec),
374     /* initialize         */    TabsConstraintInitialize,
375     /* destroy            */    NULL,
376     /* set_values         */    TabsConstraintSetValues,
377     /* extension          */    NULL,
378   },
379 #ifdef  NEED_MOTIF
380 /* Manager Class fields */
381   {
382     /* translations             */      NULL,
383     /* syn_resources            */      NULL,
384     /* num_syn_resources        */      0,
385     /* syn_constraint_resources */      NULL,
386     /* num_syn_constraint_resources */  0,
387     /* parent_process           */      XmInheritParentProcess,
388     /* extension                */      NULL
389   },
390 #endif
391   {
392 /* Tabs class fields */
393     /* extension          */    NULL,
394   }
395 };
396
397 WidgetClass tabsWidgetClass = (WidgetClass)&tabsClassRec;
398
399 #define TabsNumChildren(tw) (((TabsWidget)tw)->composite.num_children)
400 #define TabVisible(tab) \
401         (XtIsManaged(tab) && \
402          ((TabsConstraints)((tab)->core.constraints))->tabs.visible)
403 \f
404
405 /****************************************************************
406  *
407  * Member Procedures
408  *
409  ****************************************************************/
410
411 static void
412 TabsClassInit(void)
413 {
414         defaultAccelerators = XtParseAcceleratorTable(accelTable) ;
415         /* TODO: register converter for labels? */
416 }
417
418
419
420         /* Init a newly created tabs widget.  Compute height of tabs
421          * and optionally compute size of widget. */
422
423 /* ARGSUSED */
424
425 static void
426 TabsInit(Widget request, Widget new, ArgList args, Cardinal *num_args)
427 {
428     TabsWidget newTw = (TabsWidget)new;
429
430     newTw->tabs.numRows = 0 ;
431     newTw->tabs.realRows = 0;
432
433     GetPreferredSizes(newTw) ;
434
435     /* height is easy, it's the same for all tabs:
436      *  TODO: font height + height of tallest bitmap.
437      */
438     newTw->tabs.tab_height = 2 * newTw->tabs.internalHeight + SHADWID ;
439
440     if( newTw->tabs.font != NULL )
441       newTw->tabs.tab_height += newTw->tabs.font->max_bounds.ascent +
442                                 newTw->tabs.font->max_bounds.descent ;
443
444     /* GC allocation is deferred until XtRealize() */
445
446     /* if size not explicitly set, set it to our preferred size now. */
447
448     if( request->core.width == 0 || request->core.height == 0 )
449     {
450       Dimension w,h ;
451       PreferredSize(newTw, &w, &h, NULL,NULL) ;
452       if( request->core.width == 0 ) new->core.width = w ;
453       if( request->core.height == 0 ) new->core.height = h ;
454       XtClass(new)->core_class.resize(new) ;
455     }
456
457     /* defer GC allocation, etc., until Realize() time. */
458     newTw->tabs.foregroundGC =
459     newTw->tabs.backgroundGC =
460     newTw->tabs.greyGC =
461     newTw->tabs.topGC =
462     newTw->tabs.botGC = None ;
463
464     newTw->tabs.grey50 = None ;
465
466     newTw->tabs.needs_layout = False ;
467
468     newTw->tabs.hilight = NULL ;
469
470 #ifdef  NEED_MOTIF
471     newTw->manager.navigation_type = XmTAB_GROUP ;
472     newTw->manager.traversal_on = True ;
473 #endif
474 }
475
476
477         /* Init the constraint part of a new tab child.  Compute the
478          * size of the tab.
479          */
480 /* ARGSUSED */
481 static  void
482 TabsConstraintInitialize(Widget request, Widget new,
483         ArgList args, Cardinal *num_args)
484 {
485         TabsConstraints tab = (TabsConstraints) new->core.constraints ;
486         tab->tabs.greyAlloc = False ;   /* defer allocation of pixel */
487         tab->tabs.visible = False ;
488
489         getBitmapInfo((TabsWidget)XtParent(new), tab) ;
490         TabWidth(new) ;
491 }
492
493
494
495         /* Called when tabs widget first realized.  Create the window
496          * and allocate the GCs
497          */
498
499 static  void
500 TabsRealize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
501 {
502         TabsWidget tw = (TabsWidget) w;
503
504         attributes->bit_gravity = NorthWestGravity;
505         *valueMask |= CWBitGravity;
506
507         SuperClass->core_class.realize(w, valueMask, attributes);
508
509         TabsAllocGCs(tw) ;
510 }
511
512
513
514 static  void
515 TabsDestroy(Widget w)
516 {
517         TabsFreeGCs((TabsWidget)w) ;
518 }
519
520
521         /* Parent has resized us.  This will require that the tabs be
522          * laid out again.
523          */
524
525 static void
526 TabsResize(Widget w)
527 {
528         TabsWidget      tw = (TabsWidget) w;
529         int             i ;
530         int             num_children = tw->composite.num_children ;
531         Widget          *childP ;
532         TabsConstraints tab ; /* #### unused */
533         Dimension       cw,ch,bw ;
534
535         /* Our size has now been dictated by the parent.  Lay out the
536          * tabs, lay out the frame, lay out the children.  Remember
537          * that the tabs overlap each other and the frame by shadowWidth.
538          * Also, the top tab is larger than the others, so if there's only
539          * one row, the widget must be made taller to accommodate this.
540          *
541          * Once the tabs are laid out, if there is more than one
542          * row, we may need to shuffle the rows to bring the top tab
543          * to the bottom row.
544          */
545
546         tw->tabs.needs_layout = False ;
547
548         if( num_children > 0 && tw->composite.children != NULL )
549         {
550           /* Loop through the tabs and assign rows & x positions */
551           (void) TabLayout(tw, tw->core.width, tw->core.height, NULL, False) ;
552           num_children = TabsNumChildren (tw);
553
554           /* assign a top widget, bring it to bottom row. */
555           TabsShuffleRows(tw) ;
556
557           /* now assign child positions & sizes.  Positions are all the
558            * same: just inside the frame.  Sizes are also all the same.
559            */
560
561           tw->tabs.child_width = cw = tw->core.width - 2 * SHADWID ;
562           tw->tabs.child_height = ch =
563             tw->core.height < (tw->tabs.tab_total + 2 * SHADWID) ? 0 :
564             tw->core.height - tw->tabs.tab_total - 2 * SHADWID ;
565
566           for(i=0, childP=tw->composite.children;
567                 i < num_children;
568                 ++i, ++childP)
569             if( XtIsManaged(*childP) )
570             {
571               tab = (TabsConstraints) (*childP)->core.constraints ;
572               bw = (*childP)->core.border_width ;
573               /* Don't do anything if we can't see any of the child. */
574               if (ch >= bw*2 && ch > 0 && cw >= bw*2 && cw > 0)
575                 XtConfigureWidget(*childP, SHADWID,tw->tabs.tab_total+SHADWID,
576                                   cw-bw*2,ch-bw*2, bw) ;
577             }
578           if( XtIsRealized(w) ) {
579             XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
580             /* should not be necessary to explicitly repaint after a
581              * resize, but XEmacs folks tell me it is.
582              */
583             XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
584           }
585         }
586 } /* Resize */
587
588
589
590         /* Redraw entire Tabs widget */
591
592 /* ARGSUSED */
593 static  void
594 TabsExpose(Widget w, XEvent *event, Region region)
595 {
596         TabsWidget      tw = (TabsWidget) w;
597
598         if( tw->tabs.needs_layout )
599           XtClass(w)->core_class.resize(w) ;
600
601         DrawTabs(tw, True) ;
602 }
603
604
605         /* Called when any Tabs widget resources are changed. */
606
607 /* ARGSUSED */
608 static  Boolean
609 TabsSetValues(Widget current, Widget request, Widget new,
610         ArgList args, Cardinal *num_args)
611 {
612         TabsWidget curtw = (TabsWidget) current ;
613         TabsWidget tw = (TabsWidget) new ;
614         Boolean needRedraw = False ;
615         Widget  *childP ;
616         int     i ;
617
618
619         if( tw->tabs.font != curtw->tabs.font  ||
620             tw->tabs.internalWidth != curtw->tabs.internalWidth ||
621             tw->tabs.internalHeight != curtw->tabs.internalHeight )
622         {
623           tw->tabs.tab_height = 2 * tw->tabs.internalHeight + SHADWID ;
624
625           if( tw->tabs.font != NULL )
626             tw->tabs.tab_height += tw->tabs.font->max_bounds.ascent +
627                                    tw->tabs.font->max_bounds.descent ;
628
629           /* Tab size has changed.  Resize all tabs and request a new size */
630           for(i=0, childP=tw->composite.children;
631                 i < tw->composite.num_children;
632                 ++i, ++childP)
633             if( XtIsManaged(*childP) )
634               TabWidth(*childP) ;
635           PreferredSize(tw, &tw->core.width, &tw->core.height, NULL,NULL) ;
636           needRedraw = True ;
637           tw->tabs.needs_layout = True ;
638         }
639
640         /* TODO: if any color changes, need to recompute GCs and redraw */
641
642         if( tw->core.background_pixel != curtw->core.background_pixel ||
643             tw->core.background_pixmap != curtw->core.background_pixmap ||
644             tw->tabs.font != curtw->tabs.font )
645           if( XtIsRealized(new) )
646           {
647             TabsFreeGCs(tw) ;
648             TabsAllocGCs(tw) ;
649             needRedraw = True ;
650           }
651
652         if( tw->core.sensitive != curtw->core.sensitive )
653           needRedraw = True ;
654
655         /* Highlit widget changed */
656         if ( tw->tabs.hilight != curtw->tabs.hilight )
657         {
658                 needRedraw = True ;
659         }
660
661         /* If top widget changes, need to change stacking order, redraw tabs.
662          * Window system will handle the redraws.
663          */
664
665         if( tw->tabs.topWidget != curtw->tabs.topWidget )
666         {
667           if( XtIsRealized(tw->tabs.topWidget) )
668           {
669             Widget              w = tw->tabs.topWidget ;
670             TabsConstraints     tab = (TabsConstraints) w->core.constraints ;
671
672             XRaiseWindow(XtDisplay(w), XtWindow(w)) ;
673 #ifdef  NEED_MOTIF
674             XtVaSetValues(curtw->tabs.topWidget, XmNtraversalOn, False, 0) ;
675             XtVaSetValues(w, XmNtraversalOn, True, 0) ;
676 #endif
677
678             if( tab->tabs.row != tw->tabs.numRows-1 )
679               TabsShuffleRows(tw) ;
680
681                 
682             needRedraw = True ;
683           }
684           else
685             tw->tabs.needs_layout = True ;
686         }
687
688         return needRedraw ;
689 }
690
691
692         /* Called when any child constraint resources change. */
693
694 /* ARGSUSED */
695 static  Boolean
696 TabsConstraintSetValues(Widget current, Widget request, Widget new,
697         ArgList args, Cardinal *num_args)
698 {
699         TabsWidget tw = (TabsWidget) XtParent(new) ;
700         TabsConstraints ctab = (TabsConstraints) current->core.constraints ;
701         TabsConstraints tab = (TabsConstraints) new->core.constraints ;
702
703
704         /* if label changes, need to re-layout the entire widget */
705         /* if foreground changes, need to redraw tab label */
706
707         /* TODO: only need resize of new bitmap has different dimensions
708          * from old bitmap.
709          */
710
711         if( tab->tabs.label != ctab->tabs.label ||  /* Tab size has changed. */
712             tab->tabs.left_bitmap != ctab->tabs.left_bitmap )
713         {
714           TabWidth(new) ;
715           tw->tabs.needs_layout = True ;
716
717           if( tab->tabs.left_bitmap != ctab->tabs.left_bitmap )
718             getBitmapInfo(tw, tab) ;
719
720           /* If there are no subclass ConstraintSetValues procedures remaining
721            * to be invoked, and if the preferred size has changed, ask
722            * for a resize.
723            */
724           if( XtClass((Widget)tw) == tabsWidgetClass )
725             MakeSizeRequest(tw) ;
726         }
727
728
729         /* The child widget itself never needs a redisplay, but the parent
730          * Tabs widget might.
731          */
732
733         if( XtIsRealized(new) )
734         {
735           if( tw->tabs.needs_layout ) {
736             XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
737             XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
738           }
739
740           else if( tab->tabs.foreground != ctab->tabs.foreground )
741             DrawTab(tw, new, True) ;
742         }
743
744         return False ;
745 }
746
747
748 static  Boolean
749 TabsAcceptFocus(Widget w, Time *t)
750 {
751         if( !w->core.being_destroyed && XtIsRealized(w) &&
752             XtIsSensitive(w) && XtIsManaged(w) && w->core.visible )
753         {
754           Widget p ;
755           for(p = XtParent(w); !XtIsShell(p); p = XtParent(p)) ;
756           XtSetKeyboardFocus(p,w) ;
757           return True ;
758         }
759         else
760           return False ;
761 }
762
763
764
765 /*
766  * Return preferred size.  Happily accept anything >= our preferred size.
767  * (TODO: is that the right thing to do?  Should we always return "almost"
768  * if offered more than we need?)
769  */
770
771 static XtGeometryResult
772 TabsQueryGeometry(Widget w,
773         XtWidgetGeometry *intended, XtWidgetGeometry *preferred)
774 {
775     register TabsWidget tw = (TabsWidget)w ;
776     XtGeometryMask mode = intended->request_mode ;
777
778     preferred->request_mode = CWWidth | CWHeight ;
779     PreferredSize(tw, &preferred->width, &preferred->height, NULL,NULL) ;
780
781     if( (!(mode & CWWidth) || intended->width == w->core.width)  &&
782         (!(mode & CWHeight) || intended->height == w->core.height) )
783       return XtGeometryNo ;
784
785     if( (!(mode & CWWidth) || intended->width >= preferred->width)  &&
786         (!(mode & CWHeight) || intended->height >= preferred->height) )
787       return XtGeometryYes;
788
789     return XtGeometryAlmost;
790 }
791
792
793
794 /*
795  * Geometry Manager; called when a child wants to be resized.
796  */
797
798 static XtGeometryResult
799 TabsGeometryManager(Widget w, XtWidgetGeometry *req, XtWidgetGeometry *reply)
800 {
801         TabsWidget              tw = (TabsWidget) XtParent(w);
802         Dimension               s = SHADWID ;
803         TabsConstraints         tab = (TabsConstraints)w->core.constraints;
804         XtGeometryResult        result ;
805         Dimension               rw, rh ;
806
807         /* Position request always denied */
808
809         if( ((req->request_mode & CWX) && req->x != w->core.x) ||
810             ((req->request_mode & CWY) && req->y != w->core.y) ||
811             !tab->tabs.resizable )
812           return XtGeometryNo ;
813
814         /* Make all three fields in the request valid */
815         if( !(req->request_mode & CWWidth) )
816             req->width = w->core.width;
817         if( !(req->request_mode & CWHeight) )
818             req->height = w->core.height;
819         if( !(req->request_mode & CWBorderWidth) )
820             req->border_width = w->core.border_width;
821
822         if( req->width == w->core.width &&
823             req->height == w->core.height &&
824             req->border_width == w->core.border_width )
825           return XtGeometryNo ;
826
827         rw = req->width + 2 * req->border_width ;
828         rh = req->height + 2 * req->border_width ;
829
830         /* find out how big the children want to be now */
831         MaxChild(tw, w, rw, rh) ;
832
833
834         /* Size changes must see if the new size can be accommodated.
835          * The Tabs widget keeps all of its children the same
836          * size.  A request to shrink will be accepted only if the
837          * new size is still big enough for all other children.  A
838          * request to shrink that is not big enough for all children
839          * returns an "almost" response with the new proposed size
840          * or a "no" response if unable to shrink at all.
841          *
842          * A request to grow will be accepted only if the Tabs parent can
843          * grow to accommodate.
844          *
845          * TODO:
846          * We could get fancy here and re-arrange the tabs if it is
847          * necessary to compromise with the parent, but we'll save that
848          * for another day.
849          */
850
851         if (req->request_mode & (CWWidth | CWHeight | CWBorderWidth))
852         {
853           Dimension     cw,ch ;         /* children's preferred size */
854           Dimension     aw,ah ;         /* available size we can give child */
855           Dimension     th ;            /* space used by tabs */
856           Dimension     wid,hgt ;       /* Tabs widget size */
857
858           cw = tw->tabs.max_cw ;
859           ch = tw->tabs.max_ch ;
860
861           /* find out what *my* resulting preferred size would be */
862
863           PreferredSize2(tw, cw, ch, &wid, &hgt) ;
864
865           /* Would my size change?  If so, ask to be resized. */
866
867           if( wid != tw->core.width || hgt != tw->core.height )
868           {
869             Dimension   oldWid = tw->core.width, oldHgt = tw->core.height ;
870             XtWidgetGeometry    myrequest, myreply ;
871
872             myrequest.width = wid ;
873             myrequest.height = hgt ;
874             myrequest.request_mode = CWWidth | CWHeight ;
875             
876             assert (wid > 0 && hgt > 0);
877             /* If child is only querying, or if we're going to have to
878              * offer the child a compromise, then make this a query only.
879              */
880
881             if( (req->request_mode & XtCWQueryOnly)  || rw < cw || rh < ch )
882               myrequest.request_mode |= XtCWQueryOnly ;
883
884             result = XtMakeGeometryRequest((Widget)tw, &myrequest, &myreply) ;
885
886             /* !$@# Athena Box widget changes the core size even if QueryOnly
887              * is set.  I'm convinced this is a bug.  At any rate, to work
888              * around the bug, we need to restore the core size after every
889              * query geometry request.  This is only partly effective,
890              * as there may be other boxes further up the tree.
891              */
892             if( myrequest.request_mode & XtCWQueryOnly ) {
893               tw->core.width = oldWid ;
894               tw->core.height = oldHgt ;
895             }
896
897             /* based on the parent's response, determine what the
898              * resulting Tabs widget size would be.
899              */
900
901             switch( result ) {
902               case XtGeometryYes:
903               case XtGeometryDone:
904                 tw->tabs.needs_layout = True ;
905                 break ;
906
907               case XtGeometryNo:
908                 wid = tw->core.width ;
909                 hgt = tw->core.height ;
910                 break ;
911
912               case XtGeometryAlmost:
913                 wid = myreply.width ;
914                 hgt = myreply.height ;
915                 tw->tabs.needs_layout = True ;
916                 break ;
917             }
918           }
919
920           /* Within the constraints imposed by the parent, what is
921            * the max size we can give the child?
922            */
923           (void) TabLayout(tw, wid, hgt, &th, True) ;
924           aw = wid - 2*s ;
925           ah = hgt - th - 2*s ;
926
927           /* OK, make our decision.  If requested size is >= max sibling
928            * preferred size, AND requested size <= available size, then
929            * we accept.  Otherwise, we offer a compromise.
930            */
931
932           if( rw == aw && rh == ah )
933           {
934             /* Acceptable.  If this wasn't a query, change *all* children
935              * to this size.
936              */
937             if( req->request_mode & XtCWQueryOnly )
938               return XtGeometryYes ;
939             else
940             {
941               Widget    *childP = tw->composite.children ;
942               int       i,bw ;
943               w->core.border_width = req->border_width ;
944               for(i=TabsNumChildren (tw); --i >= 0; ++childP)
945                 if( TabVisible(*childP) )
946                 {
947                   bw = (*childP)->core.border_width ;
948                   XtConfigureWidget(*childP, s,tw->tabs.tab_total+s,
949                             rw-2*bw, rh-2*bw, bw) ;
950                 }
951 #ifdef  COMMENT
952               /* TODO: under what conditions will we need to redraw? */
953               XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
954               XtClass(tw)->core_class.expose((Widget)tw,NULL,NULL) ;
955 #endif  /* COMMENT */
956               return XtGeometryDone ;
957             }
958           }
959
960           /* Cannot grant child's request.  Describe what we *can* do
961            * and return counter-offer.
962            */
963           reply->width  = aw - 2 * req->border_width ;
964           reply->height = ah - 2 * req->border_width ;
965           reply->border_width = req->border_width ;
966           reply->request_mode = CWWidth | CWHeight | CWBorderWidth ;
967           return XtGeometryAlmost ;
968         }
969
970         return XtGeometryYes ;
971 }
972
973
974
975
976         /* The number of children we manage has changed; recompute
977          * size from scratch.
978          */
979
980 static  void
981 TabsChangeManaged(Widget w)
982 {
983     TabsWidget  tw = (TabsWidget)w ;
984     Widget      *childP = tw->composite.children ;
985     int         i ;
986
987     if( tw->tabs.topWidget != NULL &&
988         ( !XtIsManaged(tw->tabs.topWidget) ||
989           tw->tabs.topWidget->core.being_destroyed ) )
990       tw->tabs.topWidget = NULL ;
991
992     /* Check whether the highlight tab is still valid. */
993     if( tw->tabs.hilight != NULL &&
994         ( !XtIsManaged(tw->tabs.hilight) ||
995           tw->tabs.hilight->core.being_destroyed ) )
996       tw->tabs.hilight = NULL ;
997
998     GetPreferredSizes(tw) ;
999     MakeSizeRequest(tw) ;
1000
1001     XtClass(w)->core_class.resize(w) ;
1002     if( XtIsRealized(w) )
1003     {
1004       Display *dpy = XtDisplay(w) ;
1005       XClearWindow(dpy, XtWindow(w)) ;
1006       XtClass(w)->core_class.expose(w,NULL,NULL) ;
1007
1008       /* make sure the top widget stays on top.  This requires
1009        * making sure that all new children are realized first.
1010        */
1011       if( tw->tabs.topWidget != NULL && XtIsRealized(tw->tabs.topWidget) )
1012       {
1013         for(i=TabsNumChildren (tw); --i >= 0; ++childP)
1014           if( !XtIsRealized(*childP) )
1015             XtRealizeWidget(*childP) ;
1016
1017         XRaiseWindow(dpy, XtWindow(tw->tabs.topWidget)) ;
1018       }
1019     }
1020
1021 #ifdef  NEED_MOTIF
1022     /* Only top widget may receive input */
1023
1024     for(childP = tw->composite.children, i=tw->composite.num_children;
1025         --i >= 0;
1026         ++childP)
1027     {
1028       XtVaSetValues(*childP, XmNtraversalOn, False, 0) ;
1029     }
1030
1031     if( tw->tabs.topWidget != NULL )
1032       XtVaSetValues(tw->tabs.topWidget, XmNtraversalOn, True, 0) ;
1033 #endif
1034 }
1035
1036
1037 \f
1038
1039 /****************************************************************
1040  *
1041  * Action Procedures
1042  *
1043  ****************************************************************/
1044
1045
1046         /* User clicks on a tab, figure out which one it was. */
1047
1048 /* ARGSUSED */
1049 static  void
1050 TabsSelect(Widget w, XEvent *event, String *params, Cardinal *num_params)
1051 {
1052         TabsWidget      tw = (TabsWidget) w ;
1053         Widget  *childP ;
1054         Position x,y ;
1055         Dimension h = tw->tabs.tab_height ;
1056         int     i ;
1057
1058 #ifdef  NEED_MOTIF
1059         XmProcessTraversal (w, XmTRAVERSE_CURRENT) ;
1060 #endif
1061
1062         /* TODO: is there an Xmu function or something to do this instead? */
1063         switch( event->type ) {
1064           case ButtonPress:
1065           case ButtonRelease:
1066             x = event->xbutton.x ; y = event->xbutton.y ; break ;
1067           case KeyPress:
1068           case KeyRelease:
1069             x = event->xkey.x ; y = event->xkey.y ; break ;
1070           default:
1071             return ;
1072         }
1073
1074         /* TODO: determine which tab was clicked, if any.  Set that
1075          * widget to be top of stacking order with XawTabsSetTop().
1076          */
1077         for(i=0, childP=tw->composite.children;
1078               i < TabsNumChildren (tw);
1079               ++i, ++childP)
1080           if( TabVisible(*childP) )
1081           {
1082             TabsConstraints tab = (TabsConstraints)(*childP)->core.constraints;
1083             if( x > tab->tabs.x  &&  x < tab->tabs.x + tab->tabs.width  &&
1084                 y > tab->tabs.y  &&  y < tab->tabs.y + h )
1085             {
1086               if( *childP != tw->tabs.topWidget &&
1087                   (XtIsSensitive(*childP) || tw->tabs.selectInsensitive) )
1088                 XawTabsSetTop(*childP, True) ;
1089               break ;
1090             }
1091           }
1092 }
1093
1094
1095         /* User hits a key */
1096
1097 static  void
1098 TabsPage(Widget w, XEvent *event, String *params, Cardinal *num_params)
1099 {
1100         TabsWidget      tw = (TabsWidget) w ;
1101         Widget          newtop = NULL;
1102         Widget          *childP ;
1103         int             idx ;
1104         int             nc = TabsNumChildren (tw) ;
1105
1106         if( nc <= 0 )
1107           return ;
1108
1109         if( *num_params < 1 ) {
1110           XtAppWarning(XtWidgetToApplicationContext(w),
1111             "Tabs: page() action called with no arguments") ;
1112           return ;
1113         }
1114
1115         if( tw->tabs.topWidget == NULL )
1116           tw->tabs.topWidget = tw->composite.children[0] ;
1117
1118         for(idx=0, childP=tw->composite.children; idx < nc; ++idx, ++childP )
1119           if( tw->tabs.topWidget == *childP )
1120             break ;
1121
1122         switch( params[0][0] ) {
1123           case 'u':             /* up */
1124           case 'U':
1125             if( --idx < 0 )
1126               idx = nc-1 ;
1127             newtop = tw->composite.children[idx] ;
1128             break ;
1129
1130           case 'd':             /* down */
1131           case 'D':
1132             if( ++idx >= nc )
1133               idx = 0 ;
1134             newtop = tw->composite.children[idx] ;
1135             break ;
1136
1137           case 'h':
1138           case 'H':
1139           default:
1140               newtop = tw->composite.children[0] ;
1141               break ;
1142
1143           case 'e':
1144           case 'E':
1145               newtop = tw->composite.children[nc-1] ;
1146               break ;
1147
1148           case 's':             /* selected */
1149           case 'S':
1150               if( (newtop = tw->tabs.hilight) == NULL )
1151                 return ;
1152               break ;
1153         }
1154
1155         XawTabsSetTop(newtop, True) ;
1156 }
1157
1158
1159         /* User hits up/down key */
1160
1161 static  void
1162 TabsHighlight(Widget w, XEvent *event, String *params, Cardinal *num_params)
1163 {
1164         TabsWidget      tw = (TabsWidget) w ;
1165         Widget          newhl = NULL;
1166         Widget          *childP ;
1167         int             idx ;
1168         int             nc = TabsNumChildren (tw) ;
1169
1170         if( nc <= 0 )
1171           return ;
1172
1173         if( *num_params < 1 )
1174         {
1175           if( tw->tabs.hilight != NULL )
1176             DrawHighlight(tw, tw->tabs.hilight, False) ;
1177           return ;
1178         }
1179
1180         if( tw->tabs.hilight == NULL )
1181           newhl = tw->composite.children[0] ;
1182
1183         else
1184         {
1185           /* find index of currently highlit child */
1186           for(idx=0, childP=tw->composite.children; idx < nc; ++idx, ++childP )
1187             if( tw->tabs.hilight == *childP )
1188               break ;
1189
1190           switch( params[0][0] ) {
1191             case 'u':           /* up */
1192             case 'U':
1193               if( --idx < 0 )
1194                 idx = nc-1 ;
1195               newhl = tw->composite.children[idx] ;
1196               break ;
1197
1198             case 'd':           /* down */
1199             case 'D':
1200               if( ++idx >= nc )
1201                 idx = 0 ;
1202               newhl = tw->composite.children[idx] ;
1203               break ;
1204
1205             case 'h':
1206             case 'H':
1207                 newhl = tw->composite.children[0] ;
1208                 break ;
1209
1210             case 'e':
1211             case 'E':
1212                 newhl = tw->composite.children[nc-1] ;
1213                 break ;
1214
1215             default:
1216                 newhl = tw->tabs.hilight ;
1217                 break ;
1218           }
1219         }
1220
1221         XawTabsSetHighlight(w, newhl) ;
1222 }
1223
1224
1225
1226 static  void
1227 TabsUnhighlight(Widget w, XEvent *event, String *params, Cardinal *num_params)
1228 {
1229         TabsWidget      tw = (TabsWidget) w ;
1230         int             nc = tw->composite.num_children ;
1231
1232         if( nc <= 0 )
1233           return ;
1234
1235         if( tw->tabs.hilight != NULL )
1236           DrawHighlight(tw, tw->tabs.hilight, True) ;
1237 }
1238
1239
1240
1241 \f
1242
1243 /****************************************************************
1244  *
1245  * Public Procedures
1246  *
1247  ****************************************************************/
1248
1249
1250         /* Set the top tab, optionally call all callbacks. */
1251 void
1252 XawTabsSetTop(Widget w, Bool callCallbacks)
1253 {
1254         TabsWidget      tw = (TabsWidget)w->core.parent ;
1255         TabsConstraints tab ;
1256         Widget          oldtop = tw->tabs.topWidget ;
1257
1258         if( !XtIsSubclass(w->core.parent, tabsWidgetClass) )
1259         {
1260           char line[256] ;
1261           sprintf(line, "XawTabsSetTop: widget \"%.64s\" is not the child of a tabs widget.", XtName(w)) ;
1262           XtAppWarning(XtWidgetToApplicationContext(w), line) ;
1263           return ;
1264         }
1265
1266         if( callCallbacks )
1267           XtCallCallbackList(w, tw->tabs.popdownCallbacks,
1268                 (XtPointer)tw->tabs.topWidget) ;
1269
1270         if( !XtIsRealized(w) ) {
1271           tw->tabs.topWidget = w ;
1272           tw->tabs.needs_layout = True ;
1273           tw->tabs.hilight = NULL; /* The highlight tab might disappear. */
1274           return ;
1275         }
1276
1277         XRaiseWindow(XtDisplay(w), XtWindow(w)) ;
1278 #ifdef  NEED_MOTIF
1279         XtVaSetValues(oldtop, XmNtraversalOn, False, 0) ;
1280         XtVaSetValues(w, XmNtraversalOn, True, 0) ;
1281 #endif
1282
1283         tab = (TabsConstraints) w->core.constraints ;
1284
1285         /* Unhighlight before we start messing with the stacking order. */
1286         if( tw->tabs.hilight != NULL )
1287           {
1288             DrawHighlight(tw, tw->tabs.hilight, True) ;
1289             tw->tabs.hilight = NULL;
1290           }
1291
1292         if( tab->tabs.row == 0 )
1293         {
1294           /* Easy case; undraw current top, undraw new top, assign new
1295            * top, redraw all borders.
1296            * We *could* just erase and execute a full redraw, but I like to
1297            * reduce screen flicker.
1298            */
1299           UndrawTab(tw, oldtop) ;               /* undraw old */
1300           DrawBorder(tw, oldtop, True) ;
1301           UndrawTab(tw, w) ;                    /* undraw new */
1302           DrawBorder(tw, w, True) ;
1303           tw->tabs.topWidget = w ;
1304           DrawTab(tw, oldtop, True) ;           /* redraw old */
1305           DrawTab(tw, w, True) ;                /* redraw new */
1306           DrawTabs(tw, False) ;
1307         }
1308         else
1309         {
1310           tw->tabs.topWidget = w ;
1311           TabsShuffleRows(tw) ;
1312           XClearWindow(XtDisplay((Widget)tw), XtWindow((Widget)tw)) ;
1313           XtClass(tw)->core_class.expose((Widget)tw,NULL,None) ;
1314         }
1315
1316         XawTabsSetHighlight((Widget)tw, w) ;
1317
1318         if( callCallbacks )
1319           XtCallCallbackList(w, tw->tabs.callbacks, (XtPointer)w) ;
1320 }
1321
1322
1323         /* Set the top tab, optionally call all callbacks. */
1324 void
1325 XawTabsSetHighlight(Widget t, Widget w)
1326 {
1327         TabsWidget      tw = (TabsWidget)t ;
1328
1329         if( !XtIsSubclass(t, tabsWidgetClass) )
1330           return ;
1331
1332         if( XtIsRealized(t) && w != tw->tabs.hilight )
1333         {
1334           if( w != NULL )
1335             DrawHighlight(tw, w, False) ;
1336         }
1337
1338         tw->tabs.hilight = w ;
1339 }
1340
1341
1342 \f
1343
1344 /****************************************************************
1345  *
1346  * Private Procedures
1347  *
1348  ****************************************************************/
1349
1350
1351 static  void
1352 TabsAllocGCs(TabsWidget tw)
1353 {
1354         TabsAllocFgGC(tw) ;
1355         TabsAllocGreyGC(tw) ;
1356         tw->tabs.backgroundGC = AllocBackgroundGC((Widget)tw, None) ;
1357         tw->tabs.topGC = AllocTopShadowGC((Widget)tw,
1358                 tw->tabs.top_shadow_contrast, tw->tabs.be_nice_to_cmap) ;
1359         tw->tabs.botGC = AllocBotShadowGC((Widget)tw,
1360                 tw->tabs.bot_shadow_contrast, tw->tabs.be_nice_to_cmap) ;
1361 }
1362
1363
1364 static  void
1365 TabsFreeGCs(TabsWidget tw)
1366 {
1367         Widget w = (Widget) tw;
1368
1369         XtReleaseGC(w, tw->tabs.foregroundGC) ;
1370         XtReleaseGC(w, tw->tabs.greyGC) ;
1371         XtReleaseGC(w, tw->tabs.backgroundGC) ;
1372         XtReleaseGC(w, tw->tabs.topGC) ;
1373         XtReleaseGC(w, tw->tabs.botGC) ;
1374 #ifdef HAVE_XMU
1375         XmuReleaseStippledPixmap(XtScreen(w), tw->tabs.grey50) ;
1376 #endif
1377 }
1378
1379
1380
1381
1382
1383         /* Redraw entire Tabs widget */
1384
1385 static  void
1386 DrawTabs(TabsWidget tw, Bool labels)
1387 {
1388         Widget          *childP ;
1389         int             i,j ;
1390         Dimension       s = SHADWID ;
1391         Dimension       th = tw->tabs.tab_height ;
1392         Position        y ;
1393         TabsConstraints tab ;
1394
1395         if( !XtIsRealized((Widget)tw))
1396           return ;
1397
1398         /* draw tabs and frames by row except for the top tab, which
1399          * is drawn last.  (This is inefficiently written, but should not
1400          * be too slow as long as there are not a lot of rows.)
1401          */
1402
1403         y = tw->tabs.numRows == 1 ? TABDELTA : 0 ;
1404         for(i=0; i<tw->tabs.numRows; ++i, y += th)
1405         {
1406           for( j=TabsNumChildren (tw), childP=tw->composite.children;
1407               --j >= 0; ++childP )
1408             if( TabVisible(*childP) )
1409             {
1410               tab = (TabsConstraints)(*childP)->core.constraints;
1411               if( tab->tabs.row == i && *childP != tw->tabs.topWidget )
1412                 DrawTab(tw, *childP, labels) ;
1413             }
1414           if( i != tw->tabs.numRows -1 )
1415             DrawTrim(tw, 0,y+th, tw->core.width, th+s, False,False) ;
1416         }
1417
1418         DrawFrame(tw) ;
1419
1420         /* and now the top tab */
1421         if( tw->tabs.topWidget != NULL )
1422           DrawTab(tw, tw->tabs.topWidget, labels) ;
1423 }
1424
1425
1426
1427 /* Draw one tab.  Corners are rounded very slightly. */
1428
1429 static  void
1430 DrawTab(TabsWidget tw, Widget child, Bool labels)
1431 {
1432         GC      gc ;
1433         int     x,y ;
1434
1435         if( !XtIsRealized((Widget)tw))
1436           return ;
1437
1438         DrawBorder(tw, child, False) ;
1439
1440         if( labels )
1441         {
1442           TabsConstraints tab = (TabsConstraints)child->core.constraints;
1443           Display       *dpy = XtDisplay((Widget)tw) ;
1444           Window        win = XtWindow((Widget)tw) ;
1445           String        lbl = tab->tabs.label != NULL ?
1446                               tab->tabs.label : XtName(child) ;
1447
1448           if( XtIsSensitive(child) )
1449           {
1450             gc = tw->tabs.foregroundGC ;
1451             XSetForeground(dpy, gc, tab->tabs.foreground) ;
1452           }
1453           else
1454           {
1455             /* grey pixel allocation deferred until now */
1456             if( !tab->tabs.greyAlloc )
1457             {
1458               if( tw->tabs.be_nice_to_cmap || tw->core.depth == 1 )
1459                 tab->tabs.grey = tab->tabs.foreground ;
1460               else
1461                 tab->tabs.grey = AllocGreyPixel((Widget)tw,
1462                                         tab->tabs.foreground,
1463                                         tw->core.background_pixel,
1464                                         tw->tabs.insensitive_contrast ) ;
1465               tab->tabs.greyAlloc = True ;
1466             }
1467             gc = tw->tabs.greyGC ;
1468             XSetForeground(dpy, gc, tab->tabs.grey) ;
1469           }
1470
1471           x = tab->tabs.x ;
1472           y = tab->tabs.y ;
1473           if( child == tw->tabs.topWidget )
1474             y -= TABLDELTA ;
1475
1476           if( tab->tabs.left_bitmap != None && tab->tabs.lbm_width > 0 )
1477           {
1478             if( tab->tabs.lbm_depth == 1 )
1479               XCopyPlane(dpy, tab->tabs.left_bitmap, win,gc,
1480                 0,0, tab->tabs.lbm_width, tab->tabs.lbm_height,
1481                 x+tab->tabs.lbm_x, y+tab->tabs.lbm_y, 1L) ;
1482             else
1483               XCopyArea(dpy, tab->tabs.left_bitmap, win,gc,
1484                 0,0, tab->tabs.lbm_width, tab->tabs.lbm_height,
1485                 x+tab->tabs.lbm_x, y+tab->tabs.lbm_y) ;
1486           }
1487
1488           if( lbl != NULL && tw->tabs.font != NULL )
1489             XDrawString(dpy,win,gc,
1490               x+tab->tabs.l_x, y+tab->tabs.l_y,
1491               lbl, (int)strlen(lbl)) ;
1492         }
1493
1494         if( child == tw->tabs.hilight )
1495           DrawHighlight(tw, child, False) ;
1496 }
1497
1498
1499         /* draw frame all the way around the child windows. */
1500
1501 static  void
1502 DrawFrame(TabsWidget tw)
1503 {
1504         GC              topgc = tw->tabs.topGC ;
1505         GC              botgc = tw->tabs.botGC ;
1506         Dimension       s = SHADWID ;
1507         Dimension       ch = tw->tabs.child_height ;
1508         if (ch > 0)
1509           Draw3dBox((Widget)tw, 0,tw->tabs.tab_total,
1510                     tw->core.width, ch+2*s, s, topgc, botgc) ;
1511         else
1512           {
1513             Widget              w = tw->tabs.topWidget ;
1514             if (w != NULL)
1515               {
1516                 TabsConstraints tab = (TabsConstraints) w->core.constraints ;
1517                 Draw3dBox((Widget)tw, 0,tw->core.height - 2*s,
1518                           tab->tabs.x, 2*s, s, topgc, botgc);
1519                 Draw3dBox((Widget)tw, tab->tabs.x + tab->tabs.width, 
1520                           tw->core.height - 2*s,
1521                           tw->core.width - tab->tabs.x - tab->tabs.width, 2*s, s, 
1522                           topgc, botgc);
1523               }
1524             else
1525               Draw3dBox((Widget)tw, 0,tw->core.height - 2*s,
1526                         tw->core.width, 2*s, s, topgc, botgc) ;
1527           }
1528 }
1529
1530
1531         /* draw trim around a tab or underneath a row of tabs */
1532
1533 static  void
1534 DrawTrim(TabsWidget tw,         /* widget */
1535         int     x,              /* upper-left corner */
1536         int     y,
1537         int     wid,            /* total size */
1538         int     hgt,
1539         Bool    bottom,         /* draw bottom? */
1540         Bool    undraw)         /* undraw all */
1541 {
1542         Display         *dpy = XtDisplay((Widget)tw) ;
1543         Window          win = XtWindow((Widget)tw) ;
1544         GC              bggc = tw->tabs.backgroundGC ;
1545         GC              topgc = undraw ? bggc : tw->tabs.topGC ;
1546         GC              botgc = undraw ? bggc : tw->tabs.botGC ;
1547         if( bottom )
1548           XDrawLine(dpy,win,bggc, x,y+hgt-1, x+wid-1,y+hgt-1) ; /* bottom */
1549         XDrawLine(dpy,win,topgc, x,y+2, x,y+hgt-2) ;            /* left */
1550         XDrawPoint(dpy,win,topgc, x+1,y+1) ;                    /* corner */
1551         XDrawLine(dpy,win,topgc, x+2,y, x+wid-3,y) ;            /* top */
1552         XDrawLine(dpy,win,botgc, x+wid-2,y+1, x+wid-2,y+hgt-2) ; /* right */
1553         XDrawLine(dpy,win,botgc, x+wid-1,y+2, x+wid-1,y+hgt-2) ; /* right */
1554 }
1555
1556
1557 /* Draw one tab border. */
1558
1559 static  void
1560 DrawBorder(TabsWidget tw, Widget child, Bool undraw)
1561 {
1562         TabsConstraints tab = (TabsConstraints)child->core.constraints;
1563         Position        x = tab->tabs.x ;
1564         Position        y = tab->tabs.y ;
1565         Dimension       twid = tab->tabs.width ;
1566         Dimension       thgt = tw->tabs.tab_height ;
1567
1568         /* top tab requires a little special attention; it overlaps
1569          * neighboring tabs slightly, so the background must be cleared
1570          * in the region of the overlap to partially erase those neighbors.
1571          * TODO: is this worth doing with regions instead?
1572          */
1573         if( child == tw->tabs.topWidget )
1574         {
1575           Display       *dpy = XtDisplay((Widget)tw) ;
1576           Window        win = XtWindow((Widget)tw) ;
1577           GC            bggc = tw->tabs.backgroundGC ;
1578           XRectangle    rects[3] ;
1579           x -= TABDELTA ;
1580           y -= TABDELTA ;
1581           twid += TABDELTA*2 ;
1582           thgt += TABDELTA ;
1583           AddRect(0, x,y+1,twid,TABDELTA) ;
1584           AddRect(1, x+1,y,TABDELTA,thgt) ;
1585           AddRect(2, x+twid-TABDELTA-1,y,TABDELTA,thgt) ;
1586           XFillRectangles(dpy,win,bggc, rects, 3) ;
1587         }
1588
1589         DrawTrim(tw, x,y,twid,thgt+1, child == tw->tabs.topWidget, undraw) ;
1590 }
1591
1592
1593 /* Draw highlight around tab that has focus */
1594
1595 static  void
1596 DrawHighlight(TabsWidget tw, Widget child, Bool undraw)
1597 {
1598         TabsConstraints tab = (TabsConstraints)child->core.constraints;
1599         Display         *dpy = XtDisplay((Widget)tw) ;
1600         Window          win = XtWindow((Widget)tw) ;
1601         GC              gc ;
1602         Position        x = tab->tabs.x ;
1603         Position        y = tab->tabs.y ;
1604         Dimension       wid = tab->tabs.width ;
1605         Dimension       hgt = tw->tabs.tab_height ;
1606         XPoint          points[6] ;
1607
1608         /* top tab does not have a highlight */
1609
1610         if( child == tw->tabs.topWidget )
1611           return ;
1612
1613         if( undraw )
1614           gc = tw->tabs.backgroundGC ;
1615
1616         else if( XtIsSensitive(child) )
1617         {
1618           gc = tw->tabs.foregroundGC ;
1619           XSetForeground(dpy, gc, tab->tabs.foreground) ;
1620         }
1621         else
1622         {
1623           gc = tw->tabs.greyGC ;
1624           XSetForeground(dpy, gc, tab->tabs.grey) ;
1625         }
1626
1627         points[0].x = x+1 ; points[0].y = y+hgt-1 ;
1628         points[1].x = x+1 ; points[1].y = y+2 ;
1629         points[2].x = x+2 ; points[2].y = y+1 ;
1630         points[3].x = x+wid-4 ; points[3].y = y+1 ;
1631         points[4].x = x+wid-3 ; points[4].y = y+2 ;
1632         points[5].x = x+wid-3 ; points[5].y = y+hgt-1 ;
1633
1634         XDrawLines(dpy,win,gc, points,6, CoordModeOrigin) ;
1635 }
1636
1637
1638 /* Undraw one tab interior */
1639
1640 static  void
1641 UndrawTab(TabsWidget tw, Widget child)
1642 {
1643         TabsConstraints tab = (TabsConstraints)child->core.constraints;
1644         Position        x = tab->tabs.x ;
1645         Position        y = tab->tabs.y ;
1646         Dimension       twid = tab->tabs.width ;
1647         Dimension       thgt = tw->tabs.tab_height ;
1648         Display         *dpy = XtDisplay((Widget)tw) ;
1649         Window          win = XtWindow((Widget)tw) ;
1650         GC              bggc = tw->tabs.backgroundGC ;
1651
1652         XFillRectangle(dpy,win,bggc, x,y, twid,thgt) ;
1653 }
1654
1655
1656
1657
1658 \f
1659         /* GEOMETRY UTILITIES */
1660
1661         /* Overview:
1662          *
1663          *  MaxChild(): ask all children (except possibly one) their
1664          *  preferred sizes, set max_cw, max_ch accordingly.
1665          *
1666          *  GetPreferredSizes(): ask all children their preferred sizes,
1667          *  set max_cw, max_ch accordingly.
1668          *
1669          *  PreferredSize(): given max_cw, max_ch, return tabs widget
1670          *  preferred size.  Iterate with other widths in order to get
1671          *  a reasonable aspect ratio.
1672          *
1673          *  PreferredSize2(): Given child dimensions, return Tabs
1674          *  widget dimensions.
1675          *
1676          *  PreferredSize3(): Same, except given child dimensions plus
1677          *  shadow.
1678          */
1679
1680
1681         /* Compute the width of one child's tab.  Positions will be computed
1682          * elsewhere.
1683          *
1684          *      height: font height + vertical_space*2 + shadowWid*2
1685          *      width:  string width + horizontal_space*2 + shadowWid*2
1686          *
1687          * All tabs are the same height, so that is computed elsewhere.
1688          */
1689
1690 static  void
1691 TabWidth(Widget w)
1692 {
1693         TabsConstraints tab = (TabsConstraints) w->core.constraints ;
1694         TabsWidget      tw = (TabsWidget)XtParent(w) ;
1695         String          lbl = tab->tabs.label != NULL ?
1696                                 tab->tabs.label : XtName(w) ;
1697         XFontStruct     *font = tw->tabs.font ;
1698         int             iw = tw->tabs.internalWidth ;
1699
1700         tab->tabs.width = iw + SHADWID*2 ;
1701         tab->tabs.l_x = tab->tabs.lbm_x = SHADWID + iw ;
1702
1703         if( tab->tabs.left_bitmap != None )
1704         {
1705           tab->tabs.width += tab->tabs.lbm_width + iw ;
1706           tab->tabs.l_x += tab->tabs.lbm_width + iw ;
1707           tab->tabs.lbm_y = (tw->tabs.tab_height - tab->tabs.lbm_height)/2 ;
1708         }
1709
1710         if( lbl != NULL && font != NULL )
1711         {
1712           tab->tabs.width += XTextWidth( font, lbl, (int)strlen(lbl) ) + iw ;
1713           tab->tabs.l_y = (tw->tabs.tab_height +
1714                  tw->tabs.font->max_bounds.ascent -
1715                  tw->tabs.font->max_bounds.descent)/2 ;
1716         }
1717 }
1718
1719
1720
1721         /* Lay out tabs to fit in given width.  Compute x,y position and
1722          * row number for each tab.  Return number of rows and total height
1723          * required by all tabs.  If there is only one row, add TABDELTA
1724          * height to the total.  Rows are assigned bottom to top.
1725          *
1726          * Tabs are indented from the edges by INDENT.
1727          *
1728          * TODO: if they require more than two rows and the total height:width
1729          * ratio is more than 2:1, then try something else.
1730          */
1731
1732 static  int
1733 TabLayout(TabsWidget tw, 
1734           Dimension wid, 
1735           Dimension hgt, 
1736           Dimension *reply_height, Bool query_only)
1737 {
1738         int             i, row, done = 0, display_rows = 0 ;
1739         int             num_children = tw->composite.num_children ;
1740         Widget          *childP ;
1741         Dimension       w ;
1742         Position        x,y ;
1743         TabsConstraints tab ;
1744
1745         /* Algorithm: loop through children, assign X positions.  If a tab
1746          * would extend beyond the right edge, start a new row.  After all
1747          * rows are assigned, make a second pass and assign Y positions.
1748          */
1749
1750         if( num_children > 0 )
1751         {
1752           /* Loop through the tabs and see how much space they need. */
1753
1754           row = 0 ;
1755           x = INDENT ;
1756           y = 0 ;
1757           wid -= INDENT ;
1758           for(i=num_children, childP=tw->composite.children; --i >= 0; ++childP)
1759             if( XtIsManaged(*childP) )
1760             {
1761               tab = (TabsConstraints) (*childP)->core.constraints ;
1762               w = tab->tabs.width ;
1763
1764               if( x + w > wid ) {                       /* new row */
1765                 if (y + tw->tabs.tab_height > hgt && !done)
1766                   {
1767                     display_rows = row;
1768                     done = 1;
1769                   }
1770                 ++row;
1771                 x = INDENT ;
1772                 y += tw->tabs.tab_height ;
1773               }
1774               if( !query_only ) {
1775                 tab->tabs.x = x ;
1776                 tab->tabs.y = y ;
1777                 tab->tabs.row = row ;
1778               }
1779               x += w + SPACING ;
1780               if (!query_only && !done)
1781                 tab->tabs.visible = 1;
1782
1783             }
1784           /* If there was only one row, increase the height by TABDELTA */
1785           if( ++display_rows == 1 )
1786           {
1787             row++;
1788             y = TABDELTA ;
1789             if( !query_only )
1790               for(i=num_children, childP=tw->composite.children;
1791                     --i >= 0 ; ++childP)
1792                 if( XtIsManaged(*childP) )
1793                 {
1794                   tab = (TabsConstraints) (*childP)->core.constraints ;
1795                   tab->tabs.y = y ;
1796                 }
1797           }
1798           y += tw->tabs.tab_height ;
1799         }
1800         else
1801           display_rows = row = y = 0 ;
1802
1803         if( !query_only ) {
1804           tw->tabs.tab_total = y ;
1805           tw->tabs.numRows = display_rows ;
1806           tw->tabs.realRows = row;
1807         }
1808
1809         if( reply_height != NULL )
1810           *reply_height = y ;
1811
1812         return display_rows ;
1813 }
1814
1815
1816
1817         /* Find max preferred child size.  Returned sizes include child
1818          * border widths.
1819          */
1820
1821 static  void
1822 GetPreferredSizes(TabsWidget tw)
1823 {
1824         MaxChild(tw, NULL, 0,0) ;
1825 }
1826
1827
1828
1829         /* Find max preferred child size.  Returned sizes include child
1830          * border widths.  If except is non-null, don't ask that one.
1831          */
1832
1833 static  void
1834 MaxChild(TabsWidget tw, Widget except, Dimension cw, Dimension ch)
1835 {
1836         int                     i ;
1837         Widget                  *childP = tw->composite.children ;
1838         XtWidgetGeometry        preferred ;
1839
1840         for(i=tw->composite.num_children; --i >=0; ++childP)
1841           if( TabVisible (*childP) /*XtIsManaged(*childP)*/  &&  *childP != except )
1842           {
1843             (void) XtQueryGeometry(*childP, NULL, &preferred) ;
1844             cw = Max(cw, preferred.width + preferred.border_width * 2 ) ;
1845             ch = Max(ch, preferred.height + preferred.border_width * 2 ) ;
1846           }
1847
1848         tw->tabs.max_cw = cw ;
1849         tw->tabs.max_ch = ch ;
1850 }
1851
1852
1853
1854         /* rotate row numbers to bring current widget to bottom row,
1855          * compute y positions for all tabs
1856          */
1857
1858 static  void
1859 TabsShuffleRows(TabsWidget tw)
1860 {
1861         TabsConstraints tab ;
1862         int             move ;
1863         int             real_rows, display_rows ;
1864         Widget          *childP ;
1865         Dimension       th = tw->tabs.tab_height ;
1866         Position        bottom ;
1867         int             i ;
1868
1869         /* There must be a top widget.  If not, assign one. */
1870         if( tw->tabs.topWidget == NULL && tw->composite.children != NULL )
1871           for(i=TabsNumChildren (tw), childP=tw->composite.children;
1872               --i >= 0;
1873               ++childP)
1874             if( XtIsManaged(*childP) ) {
1875               tw->tabs.topWidget = *childP ;
1876               break ;
1877             }
1878
1879         if( tw->tabs.topWidget != NULL )
1880         {
1881           display_rows = tw->tabs.numRows ;
1882           real_rows = tw->tabs.realRows ;
1883           assert( display_rows <= real_rows ) ;
1884
1885           if( real_rows > 1 )
1886           {
1887             tab = (TabsConstraints) tw->tabs.topWidget->core.constraints ;
1888             assert( tab != NULL ) ;
1889
1890             /* How far to move top row. The selected tab must be on
1891                the bottom row of the *visible* rows. */
1892             move = (real_rows + 1 - display_rows) - tab->tabs.row ;
1893             if (move < 0) 
1894               move = real_rows - move;
1895             bottom = tw->tabs.tab_total - th ;
1896
1897             for(i=tw->composite.num_children, childP=tw->composite.children;
1898                   --i >= 0;
1899                   ++childP)
1900               if( XtIsManaged(*childP) )
1901               {
1902                 tab = (TabsConstraints) (*childP)->core.constraints ;
1903                 tab->tabs.row = (tab->tabs.row + move) % real_rows ;
1904                 tab->tabs.y = bottom - tab->tabs.row * th ;
1905                 tab->tabs.visible = (tab->tabs.row < display_rows);
1906               }
1907           }
1908         }
1909 }
1910
1911
1912         /* Find preferred size.  Ask children, find size of largest,
1913          * add room for tabs & return.  This can get a little involved,
1914          * as we don't want to have too many rows of tabs; we may widen
1915          * the widget to reduce # of rows.
1916          *
1917          * This function requires that max_cw, max_ch already be set.
1918          */
1919 static  int
1920 PreferredSize(
1921         TabsWidget      tw,
1922         Dimension       *reply_width,           /* total widget size */
1923         Dimension       *reply_height,
1924         Dimension       *reply_cw,              /* child widget size */
1925         Dimension       *reply_ch)
1926 {
1927         Dimension       cw,ch ;         /* child width, height */
1928         Dimension       wid,hgt ;
1929         Dimension       rwid,rhgt ;
1930         int             nrow ;
1931
1932         wid = cw = tw->tabs.max_cw ;
1933         hgt = ch = tw->tabs.max_ch ;
1934
1935         nrow = PreferredSize2(tw, wid,hgt, &rwid, &rhgt) ;
1936
1937         /* Check for absurd results (more than 2 rows, high aspect
1938          * ratio).  Try wider size if needed.
1939          * TODO: make sure this terminates.
1940          */
1941
1942         if( nrow > 2 && rhgt > rwid )
1943         {
1944           Dimension w0, w1 ;
1945           int maxloop = 20 ;
1946
1947           /* step 1: start doubling size until it's too big */
1948           do {
1949             w0 = wid ;
1950             wid = Max(wid*2, wid+20) ;
1951             nrow = PreferredSize2(tw, wid,hgt, &rwid,&rhgt) ;
1952           } while( nrow > 2 && rhgt > rwid ) ;
1953           w1 = wid ;
1954
1955           /* step 2: use Newton's method to find ideal size.  Stop within
1956            * 8 pixels.
1957            */
1958           while( --maxloop > 0 && w1 > w0 + 8 )
1959           {
1960             wid = (w0+w1)/2 ;
1961             nrow = PreferredSize2(tw, wid,hgt, &rwid,&rhgt) ;
1962             if( nrow > 2 && rhgt > rwid )
1963               w0 = wid ;
1964             else
1965               w1 = wid ;
1966           }
1967           wid = w1 ;
1968         }
1969
1970         *reply_width = rwid ;
1971         *reply_height = rhgt ;
1972         if( reply_cw != NULL ) *reply_cw = cw ;
1973         if( reply_ch != NULL ) *reply_ch = ch ;
1974         return nrow ;
1975 }
1976
1977
1978         /* Find preferred size, given size of children. */
1979
1980 static  int
1981 PreferredSize2(
1982         TabsWidget      tw,
1983         Dimension       cw,             /* child width, height */
1984         Dimension       ch,
1985         Dimension       *reply_width,   /* total widget size */
1986         Dimension       *reply_height)
1987 {
1988         Dimension       s = SHADWID ;
1989         int ret;
1990
1991         /* make room for shadow frame */
1992         cw += s*2 ;
1993         ch += s*2 ;
1994
1995         ret = PreferredSize3(tw, cw, ch, reply_width, reply_height) ;
1996
1997         assert (*reply_width > 0 && *reply_height > 0);
1998         return ret;
1999 }
2000
2001
2002         /* Find preferred size, given size of children+shadow. */
2003
2004 static  int
2005 PreferredSize3(
2006         TabsWidget      tw,
2007         Dimension       wid,            /* child width, height */
2008         Dimension       hgt,
2009         Dimension       *reply_width,   /* total widget size */
2010         Dimension       *reply_height)
2011 {
2012         Dimension       th ;            /* space used by tabs */
2013         int             nrows ;
2014
2015         if( tw->composite.num_children > 0 )
2016           nrows = TabLayout(tw, wid, hgt, &th, True) ;
2017         else {
2018           th = 0 ;
2019           nrows = 0 ;
2020         }
2021
2022         *reply_width = Max(wid, MIN_WID) ;
2023         *reply_height = Max(th+hgt, MIN_HGT) ;
2024
2025         return nrows ;
2026 }
2027
2028
2029 static  void
2030 MakeSizeRequest(TabsWidget tw)
2031 {
2032         Widget                  w = (Widget)tw ;
2033         XtWidgetGeometry        request, reply ;
2034         XtGeometryResult        result ;
2035         Dimension               cw,ch ;
2036
2037         request.request_mode = CWWidth | CWHeight ;
2038         PreferredSize(tw, &request.width, &request.height, &cw, &ch) ;
2039
2040         if( request.width == tw->core.width &&
2041             request.height == tw->core.height )
2042           return ;
2043
2044         result = XtMakeGeometryRequest(w, &request, &reply) ;
2045
2046         if( result == XtGeometryAlmost )
2047         {
2048           /* Bugger.  Didn't get what we want, but were offered a
2049            * compromise.  If the width was too small, recompute
2050            * based on the too-small width and try again.
2051            * If the height was too small, make a wild-ass guess
2052            * at a wider width and try again.
2053            */
2054
2055           if( reply.width < request.width && reply.height >= request.height )
2056           {
2057             Dimension s = SHADWID ;
2058             ch += s*2 ;
2059             PreferredSize3(tw, reply.width,ch, &request.width, &request.height);
2060             result = XtMakeGeometryRequest(w, &request, &reply) ;
2061             if( result == XtGeometryAlmost )
2062               (void) XtMakeGeometryRequest(w, &reply, NULL) ;
2063           }
2064
2065           else
2066             (void) XtMakeGeometryRequest(w, &reply, NULL) ;
2067         }
2068 }
2069
2070
2071 static  void
2072 getBitmapInfo(TabsWidget tw, TabsConstraints tab)
2073 {
2074         Window root ;
2075         int     x,y ;
2076         unsigned int bw ;
2077
2078         if( tab->tabs.left_bitmap == None  ||
2079             !XGetGeometry(XtDisplay(tw), tab->tabs.left_bitmap, &root, &x, &y,
2080                 &tab->tabs.lbm_width, &tab->tabs.lbm_height,
2081                 &bw, &tab->tabs.lbm_depth) )
2082           tab->tabs.lbm_width = tab->tabs.lbm_height = 0 ;
2083 }
2084
2085 \f
2086
2087
2088         /* Code copied & modified from Gcs.c.  This version has dynamic
2089          * foreground.
2090          */
2091
2092 static  void
2093 TabsAllocFgGC(TabsWidget tw)
2094 {
2095         Widget          w = (Widget) tw;
2096         XGCValues       values ;
2097
2098         values.background = tw->core.background_pixel ;
2099         values.font = tw->tabs.font->fid ;
2100         values.line_style = LineOnOffDash ;
2101         values.line_style = LineSolid ;
2102
2103         tw->tabs.foregroundGC =
2104           XtAllocateGC(w, w->core.depth,
2105             GCBackground|GCFont|GCLineStyle, &values,
2106             GCForeground,
2107             GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2108                 GCDashList|GCArcMode) ;
2109 }
2110
2111 static  void
2112 TabsAllocGreyGC(TabsWidget tw)
2113 {
2114         Widget          w = (Widget) tw;
2115         XGCValues       values ;
2116
2117         values.background = tw->core.background_pixel ;
2118         values.font = tw->tabs.font->fid ;
2119 #ifdef HAVE_XMU
2120         if( tw->tabs.be_nice_to_cmap || w->core.depth == 1)
2121         {
2122           values.fill_style = FillStippled ;
2123           tw->tabs.grey50 =
2124           values.stipple = XmuCreateStippledPixmap(XtScreen(w), 1L, 0L, 1) ;
2125
2126           tw->tabs.greyGC =
2127             XtAllocateGC(w, w->core.depth,
2128               GCBackground|GCFont|GCStipple|GCFillStyle, &values,
2129               GCForeground,
2130               GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2131                   GCDashList|GCArcMode) ;
2132         }
2133         else
2134 #endif
2135         {
2136           tw->tabs.greyGC =
2137             XtAllocateGC(w, w->core.depth,
2138               GCFont, &values,
2139               GCForeground,
2140               GCBackground|GCSubwindowMode|GCGraphicsExposures|GCDashOffset|
2141                   GCDashList|GCArcMode) ;
2142         }
2143 }