home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / x / Viewport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-24  |  31.5 KB  |  1,057 lines

  1. /* $XConsortium: Viewport.c,v 1.68 91/07/24 18:56:11 converse Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  28.  
  29.  
  30. #include <X11/IntrinsicP.h>
  31. #include <X11/StringDefs.h>
  32.  
  33. #include <X11/Xaw/XawInit.h>
  34. #include <X11/Xmu/Misc.h>
  35. #include <X11/Xaw/Scrollbar.h>
  36. #include <X11/Xaw/ViewportP.h>
  37.  
  38. static void ScrollUpDownProc(), ThumbProc();
  39. static Boolean GetGeometry();
  40.  
  41. static void ComputeWithForceBars();
  42.  
  43. #define offset(field) XtOffsetOf(ViewportRec, viewport.field)
  44. static XtResource resources[] = {
  45.     {XtNforceBars, XtCBoolean, XtRBoolean, sizeof(Boolean),
  46.      offset(forcebars), XtRImmediate, (XtPointer)False},
  47.     {XtNallowHoriz, XtCBoolean, XtRBoolean, sizeof(Boolean),
  48.      offset(allowhoriz), XtRImmediate, (XtPointer)False},
  49.     {XtNallowVert, XtCBoolean, XtRBoolean, sizeof(Boolean),
  50.      offset(allowvert), XtRImmediate, (XtPointer)False},
  51.     {XtNuseBottom, XtCBoolean, XtRBoolean, sizeof(Boolean),
  52.      offset(usebottom), XtRImmediate, (XtPointer)False},
  53.     {XtNuseRight, XtCBoolean, XtRBoolean, sizeof(Boolean),
  54.      offset(useright), XtRImmediate, (XtPointer)False},
  55.     {XtNreportCallback, XtCReportCallback, XtRCallback, sizeof(XtPointer),
  56.      offset(report_callbacks), XtRImmediate, (XtPointer) NULL},
  57. };
  58. #undef offset
  59.  
  60. static void Initialize(), ConstraintInitialize(),
  61.     Realize(), Resize(), ChangeManaged();
  62. static Boolean SetValues(), Layout();
  63. static XtGeometryResult GeometryManager(), PreferredGeometry();
  64.  
  65. #define superclass    (&formClassRec)
  66. ViewportClassRec viewportClassRec = {
  67.   { /* core_class fields */
  68.     /* superclass      */    (WidgetClass) superclass,
  69.     /* class_name      */    "Viewport",
  70.     /* widget_size      */    sizeof(ViewportRec),
  71.     /* class_initialize      */    XawInitializeWidgetSet,
  72.     /* class_part_init    */    NULL,
  73.     /* class_inited      */    FALSE,
  74.     /* initialize      */    Initialize,
  75.     /* initialize_hook    */    NULL,
  76.     /* realize          */    Realize,
  77.     /* actions          */    NULL,
  78.     /* num_actions      */    0,
  79.     /* resources      */    resources,
  80.     /* num_resources      */    XtNumber(resources),
  81.     /* xrm_class      */    NULLQUARK,
  82.     /* compress_motion      */    TRUE,
  83.     /* compress_exposure  */    TRUE,
  84.     /* compress_enterleave*/    TRUE,
  85.     /* visible_interest      */    FALSE,
  86.     /* destroy          */    NULL,
  87.     /* resize          */    Resize,
  88.     /* expose          */    XtInheritExpose,
  89.     /* set_values      */    SetValues,
  90.     /* set_values_hook    */    NULL,
  91.     /* set_values_almost  */    XtInheritSetValuesAlmost,
  92.     /* get_values_hook    */    NULL,
  93.     /* accept_focus      */    NULL,
  94.     /* version            */    XtVersion,
  95.     /* callback_private      */    NULL,
  96.     /* tm_table          */    NULL,
  97.     /* query_geometry     */    PreferredGeometry,
  98.     /* display_accelerator*/    XtInheritDisplayAccelerator,
  99.     /* extension          */    NULL
  100.   },
  101.   { /* composite_class fields */
  102.     /* geometry_manager      */    GeometryManager,
  103.     /* change_managed      */    ChangeManaged,
  104.     /* insert_child      */    XtInheritInsertChild,
  105.     /* delete_child      */    XtInheritDeleteChild,
  106.     /* extension          */    NULL
  107.   },
  108.   { /* constraint_class fields */
  109.     /* subresourses      */    NULL,
  110.     /* subresource_count  */    0,
  111.     /* constraint_size      */    sizeof(ViewportConstraintsRec),
  112.     /* initialize      */    ConstraintInitialize,
  113.     /* destroy          */    NULL,
  114.     /* set_values      */    NULL,
  115.     /* extension          */    NULL
  116.   },
  117.   { /* form_class fields */
  118.     /* layout          */    Layout
  119.   },
  120.   { /* viewport_class fields */
  121.     /* empty          */    0
  122.   }
  123. };
  124.  
  125.  
  126. WidgetClass viewportWidgetClass = (WidgetClass)&viewportClassRec;
  127.  
  128. static Widget CreateScrollbar(w, horizontal)
  129.     ViewportWidget w;
  130.     Boolean horizontal;
  131. {
  132.     Widget clip = w->viewport.clip;
  133.     ViewportConstraints constraints =
  134.     (ViewportConstraints)clip->core.constraints;
  135.     static Arg barArgs[] = {
  136.     {XtNorientation, NULL},
  137.     {XtNlength, NULL},
  138.     {XtNleft, NULL},
  139.     {XtNright, NULL},
  140.     {XtNtop, NULL},
  141.     {XtNbottom, NULL},
  142.     {XtNmappedWhenManaged, False},
  143.     };
  144.     Widget bar;
  145.  
  146.     XtSetArg(barArgs[0], XtNorientation,
  147.           horizontal ? XtorientHorizontal : XtorientVertical );
  148.     XtSetArg(barArgs[1], XtNlength,
  149.          horizontal ? clip->core.width : clip->core.height);
  150.     XtSetArg(barArgs[2], XtNleft,
  151.          (!horizontal && w->viewport.useright) ? XtChainRight : XtChainLeft);
  152.     XtSetArg(barArgs[3], XtNright,
  153.          (!horizontal && !w->viewport.useright) ? XtChainLeft : XtChainRight);
  154.     XtSetArg(barArgs[4], XtNtop,
  155.          (horizontal && w->viewport.usebottom) ? XtChainBottom: XtChainTop);
  156.     XtSetArg(barArgs[5], XtNbottom,
  157.          (horizontal && !w->viewport.usebottom) ? XtChainTop: XtChainBottom);
  158.  
  159.     bar = XtCreateWidget( (horizontal ? "horizontal" : "vertical"),
  160.               scrollbarWidgetClass, (Widget)w,
  161.               barArgs, XtNumber(barArgs) );
  162.     XtAddCallback( bar, XtNscrollProc, ScrollUpDownProc, (XtPointer)w );
  163.     XtAddCallback( bar, XtNjumpProc, ThumbProc, (XtPointer)w );
  164.  
  165.     if (horizontal) {
  166.     w->viewport.horiz_bar = bar;
  167.     constraints->form.vert_base = bar;
  168.     }
  169.     else {
  170.     w->viewport.vert_bar = bar;
  171.     constraints->form.horiz_base = bar;
  172.     }
  173.  
  174.     XtManageChild( bar );
  175.  
  176.     return bar;
  177. }
  178.  
  179. /* ARGSUSED */
  180. static void Initialize(request, new)
  181.     Widget request, new;
  182. {
  183.     ViewportWidget w = (ViewportWidget)new;
  184.     static Arg clip_args[8];
  185.     Cardinal num_args;
  186.     Widget h_bar, v_bar;
  187.     Dimension clip_height, clip_width;
  188.  
  189.     w->form.default_spacing = 0;  /* Reset the default spacing to 0 pixels. */
  190.  
  191.  
  192. /* 
  193.  * Initialize all widget pointers to NULL.
  194.  */
  195.  
  196.     w->viewport.child = (Widget) NULL;
  197.     w->viewport.horiz_bar = w->viewport.vert_bar = (Widget)NULL;
  198.  
  199. /* 
  200.  * Create Clip Widget.
  201.  */
  202.  
  203.     num_args = 0;
  204.     XtSetArg(clip_args[num_args], XtNbackgroundPixmap, None); num_args++;
  205.     XtSetArg(clip_args[num_args], XtNborderWidth, 0); num_args++;
  206.     XtSetArg(clip_args[num_args], XtNleft, XtChainLeft); num_args++;
  207.     XtSetArg(clip_args[num_args], XtNright, XtChainRight); num_args++;
  208.     XtSetArg(clip_args[num_args], XtNtop, XtChainTop); num_args++;
  209.     XtSetArg(clip_args[num_args], XtNbottom, XtChainBottom); num_args++;
  210.     XtSetArg(clip_args[num_args], XtNwidth, w->core.width); num_args++;
  211.     XtSetArg(clip_args[num_args], XtNheight, w->core.height); num_args++;
  212.  
  213.     w->viewport.clip = XtCreateManagedWidget("clip", widgetClass, new,
  214.                          clip_args, num_args);
  215.  
  216.     if (!w->viewport.forcebars) 
  217.         return;         /* If we are not forcing the bars then we are done. */
  218.  
  219.     if (w->viewport.allowhoriz) 
  220.       (void) CreateScrollbar(w, True);
  221.     if (w->viewport.allowvert) 
  222.       (void) CreateScrollbar(w, False);
  223.  
  224.     h_bar = w->viewport.horiz_bar;
  225.     v_bar = w->viewport.vert_bar;
  226.  
  227. /*
  228.  * Set the clip widget to the correct height.
  229.  */
  230.  
  231.     clip_width = w->core.width;
  232.     clip_height = w->core.height;
  233.  
  234.     if ( (h_bar != NULL) &&
  235.      ((int)w->core.width >
  236.       (int)(h_bar->core.width + h_bar->core.border_width)) )
  237.         clip_width -= h_bar->core.width + h_bar->core.border_width;
  238.     
  239.     if ( (v_bar != NULL) &&
  240.      ((int)w->core.height >
  241.       (int)(v_bar->core.height + v_bar->core.border_width)) )
  242.         clip_height -= v_bar->core.height + v_bar->core.border_width;
  243.  
  244.     num_args = 0;
  245.     XtSetArg(clip_args[num_args], XtNwidth, clip_width); num_args++;
  246.     XtSetArg(clip_args[num_args], XtNheight, clip_height); num_args++;
  247.     XtSetValues(w->viewport.clip, clip_args, num_args);
  248. }
  249.  
  250. /* ARGSUSED */
  251. static void ConstraintInitialize(request, new)
  252.     Widget request, new;
  253. {
  254.     ((ViewportConstraints)new->core.constraints)->viewport.reparented = False;
  255. }
  256.  
  257. static void Realize(widget, value_mask, attributes)
  258.     Widget widget;
  259.     XtValueMask *value_mask;
  260.     XSetWindowAttributes *attributes;
  261. {
  262.     ViewportWidget w = (ViewportWidget)widget;
  263.     register Widget child = w->viewport.child;
  264.     register Widget clip = w->viewport.clip;
  265.  
  266.     *value_mask |= CWBitGravity;
  267.     attributes->bit_gravity = NorthWestGravity;
  268.     (*superclass->core_class.realize)(widget, value_mask, attributes);
  269.  
  270.     (*w->core.widget_class->core_class.resize)(widget);    /* turn on bars */
  271.  
  272.     if (child != (Widget)NULL) {
  273.     XtMoveWidget( child, (Position)0, (Position)0 );
  274.     XtRealizeWidget( clip );
  275.     XtRealizeWidget( child );
  276.     XReparentWindow( XtDisplay(w), XtWindow(child), XtWindow(clip),
  277.              (Position)0, (Position)0 );
  278.     XtMapWidget( child );
  279.     }
  280. }
  281.  
  282. /* ARGSUSED */
  283. static Boolean SetValues(current, request, new)
  284.     Widget current, request, new;
  285. {
  286.     ViewportWidget w = (ViewportWidget)new;
  287.     ViewportWidget cw = (ViewportWidget)current;
  288.  
  289.     if ( (w->viewport.forcebars != cw->viewport.forcebars) ||
  290.      (w->viewport.allowvert != cw->viewport.allowvert) ||
  291.      (w->viewport.allowhoriz != cw->viewport.allowhoriz) ||
  292.      (w->viewport.useright != cw->viewport.useright) ||
  293.      (w->viewport.usebottom != cw->viewport.usebottom) ) 
  294.     {
  295.     (*w->core.widget_class->core_class.resize)(new); /* Recompute layout.*/
  296.     }
  297.  
  298.     return False;
  299. }
  300.  
  301.  
  302. static void ChangeManaged(widget)
  303.     Widget widget;
  304. {
  305.     ViewportWidget w = (ViewportWidget)widget;
  306.     register int num_children = w->composite.num_children;
  307.     register Widget child, *childP;
  308.     register int i;
  309.  
  310.     child = (Widget)NULL;
  311.     for (childP=w->composite.children, i=0; i < num_children; childP++, i++) {
  312.     if (XtIsManaged(*childP)
  313.         && *childP != w->viewport.clip
  314.         && *childP != w->viewport.horiz_bar
  315.         && *childP != w->viewport.vert_bar)
  316.     {
  317.         child = *childP;
  318.         break;
  319.     }
  320.     }
  321.  
  322.     if (child != w->viewport.child) {
  323.     w->viewport.child = child;
  324.     if (child != (Widget)NULL) {
  325.         XtResizeWidget( child, child->core.width,
  326.                 child->core.height, (Dimension)0 );
  327.         if (XtIsRealized(widget)) {
  328.         ViewportConstraints constraints =
  329.             (ViewportConstraints)child->core.constraints;
  330.         if (!XtIsRealized(child)) {
  331.             Window window = XtWindow(w);
  332.             XtMoveWidget( child, (Position)0, (Position)0 );
  333. #ifdef notdef
  334.             /* this is dirty, but it saves the following code: */
  335.             XtRealizeWidget( child );
  336.             XReparentWindow( XtDisplay(w), XtWindow(child),
  337.                      XtWindow(w->viewport.clip),
  338.                      (Position)0, (Position)0 );
  339.             if (child->core.mapped_when_managed)
  340.             XtMapWidget( child );
  341. #else 
  342.             w->core.window = XtWindow(w->viewport.clip);
  343.             XtRealizeWidget( child );
  344.             w->core.window = window;
  345. #endif /* notdef */
  346.             constraints->viewport.reparented = True;
  347.         }
  348.         else if (!constraints->viewport.reparented) {
  349.             XReparentWindow( XtDisplay(w), XtWindow(child),
  350.                      XtWindow(w->viewport.clip),
  351.                      (Position)0, (Position)0 );
  352.             constraints->viewport.reparented = True;
  353.             if (child->core.mapped_when_managed)
  354.             XtMapWidget( child );
  355.         }
  356.         }
  357.         GetGeometry( widget, child->core.width, child->core.height );
  358.         (*((ViewportWidgetClass)w->core.widget_class)->form_class.layout)
  359.         ( (FormWidget)w, w->core.width, w->core.height );
  360.         /* %%% do we need to hide this child from Form?  */
  361.     }
  362.     }
  363.  
  364. #ifdef notdef
  365.     (*superclass->composite_class.change_managed)( widget );
  366. #endif
  367. }
  368.  
  369.  
  370. static void SetBar(w, top, length, total)
  371.     Widget w;
  372.     Position top;
  373.     Dimension length, total;
  374. {
  375.     XawScrollbarSetThumb(w, (float)top/(float)total,
  376.              (float)length/(float)total);
  377. }
  378.  
  379. static void RedrawThumbs(w)
  380.   ViewportWidget w;
  381. {
  382.     register Widget child = w->viewport.child;
  383.     register Widget clip = w->viewport.clip;
  384.  
  385.     if (w->viewport.horiz_bar != (Widget)NULL)
  386.     SetBar( w->viewport.horiz_bar, -(child->core.x),
  387.             clip->core.width, child->core.width );
  388.  
  389.     if (w->viewport.vert_bar != (Widget)NULL)
  390.     SetBar( w->viewport.vert_bar, -(child->core.y),
  391.             clip->core.height, child->core.height );
  392. }
  393.  
  394.  
  395.  
  396. static void SendReport (w, changed)
  397.     ViewportWidget w;
  398.     unsigned int changed;
  399. {
  400.     XawPannerReport rep;
  401.  
  402.     if (w->viewport.report_callbacks) {
  403.     register Widget child = w->viewport.child;
  404.     register Widget clip = w->viewport.clip;
  405.  
  406.     rep.changed = changed;
  407.     rep.slider_x = -child->core.x;    /* child is canvas */
  408.     rep.slider_y = -child->core.y;    /* clip is slider */
  409.     rep.slider_width = clip->core.width;
  410.     rep.slider_height = clip->core.height;
  411.     rep.canvas_width = child->core.width;
  412.     rep.canvas_height = child->core.height;
  413.     XtCallCallbackList ((Widget) w, w->viewport.report_callbacks,
  414.                 (XtPointer) &rep);
  415.     }
  416. }
  417.  
  418.  
  419. static void MoveChild(w, x, y)
  420.     ViewportWidget w;
  421.     Position x, y;
  422. {
  423.     register Widget child = w->viewport.child;
  424.     register Widget clip = w->viewport.clip;
  425.  
  426.     /* make sure we never move past right/bottom borders */
  427.     if (-x + (int)clip->core.width > (int)child->core.width)
  428.     x = -(child->core.width - clip->core.width);
  429.  
  430.     if (-y + (int)clip->core.height > (int)child->core.height)
  431.     y = -(child->core.height - clip->core.height);
  432.  
  433.     /* make sure we never move past left/top borders */
  434.     if (x >= 0) x = 0;
  435.     if (y >= 0) y = 0;
  436.  
  437.     XtMoveWidget(child, x, y);
  438.     SendReport (w, (XawPRSliderX | XawPRSliderY));
  439.  
  440.     RedrawThumbs(w);
  441. }
  442.  
  443.  
  444. static void ComputeLayout(widget, query, destroy_scrollbars)
  445.     Widget widget;        /* Viewport */
  446.     Boolean query;        /* query child's preferred geom? */
  447.     Boolean destroy_scrollbars;    /* destroy un-needed scrollbars? */
  448. {
  449.     ViewportWidget w = (ViewportWidget)widget;
  450.     register Widget child = w->viewport.child;
  451.     register Widget clip = w->viewport.clip;
  452.     ViewportConstraints constraints
  453.     = (ViewportConstraints)clip->core.constraints;
  454.     Boolean needshoriz, needsvert;
  455.     int clip_width, clip_height;
  456.     XtWidgetGeometry intended;
  457.  
  458.     if (child == (Widget) NULL) return;
  459.  
  460.     clip_width = w->core.width;
  461.     clip_height = w->core.height;
  462.     intended.request_mode = CWBorderWidth;
  463.     intended.border_width = 0;
  464.  
  465.     if (w->viewport.forcebars) {
  466.         needsvert = w->viewport.allowvert;
  467.         needshoriz = w->viewport.allowhoriz;
  468.         ComputeWithForceBars(widget, query, &intended, 
  469.                  &clip_width, &clip_height);
  470.     }
  471.     else {
  472.         Dimension prev_width, prev_height;
  473.     XtGeometryMask prev_mode;
  474.     XtWidgetGeometry preferred;
  475.  
  476.     needshoriz = needsvert = False;
  477.  
  478.     /*
  479.      * intended.{width,height} caches the eventual child dimensions,
  480.      * but we don't set the mode bits until after we decide that the
  481.      * child's preferences are not acceptable.
  482.      */
  483.  
  484.     if (!w->viewport.allowhoriz) 
  485.         intended.request_mode |= CWWidth;
  486.  
  487.     if ((int)child->core.width < clip_width) 
  488.         intended.width = clip_width;
  489.     else
  490.         intended.width = child->core.width;
  491.  
  492.     if ((int)child->core.height < clip_height) 
  493.         intended.height = clip_height;
  494.     else
  495.         intended.height = child->core.height;
  496.  
  497.     if (!w->viewport.allowvert) 
  498.         intended.request_mode |= CWHeight;
  499.  
  500.     if (!query) {
  501.         preferred.width = child->core.width;
  502.         preferred.height = child->core.height;
  503.     }
  504.     do { /* while intended != prev  */
  505.  
  506.         if (query) {
  507.             (void) XtQueryGeometry( child, &intended, &preferred );
  508.         if ( !(preferred.request_mode & CWWidth) )
  509.             preferred.width = intended.width;
  510.         if ( !(preferred.request_mode & CWHeight) )
  511.             preferred.height = intended.height;
  512.         }
  513.         prev_width = intended.width;
  514.         prev_height = intended.height;
  515.         prev_mode = intended.request_mode;
  516.         /*
  517.          * note that having once decided to turn on either bar
  518.          * we'll not change our mind until we're next resized,
  519.          * thus avoiding potential oscillations.
  520.          */
  521. #define CheckHoriz()                              \
  522.         if ( w->viewport.allowhoriz &&                  \
  523.          (int)preferred.width > clip_width) {               \
  524.             if (!needshoriz) {                      \
  525.             Widget bar;                              \
  526.             needshoriz = True;                          \
  527.             if ((bar = w->viewport.horiz_bar) == (Widget)NULL)    \
  528.                 bar = CreateScrollbar(w, True);                  \
  529.             clip_height -= bar->core.height +                  \
  530.                        bar->core.border_width;          \
  531.             if (clip_height < 1) clip_height = 1;          \
  532.         }                              \
  533.             intended.width = preferred.width;              \
  534.         }
  535. /*enddef*/
  536.         CheckHoriz();
  537.         if (w->viewport.allowvert && (int)preferred.height > clip_height) {
  538.             if (!needsvert) {
  539.             Widget bar;
  540.             needsvert = True;
  541.             if ((bar = w->viewport.vert_bar) == (Widget)NULL)
  542.                 bar = CreateScrollbar(w, False);
  543.             clip_width -= bar->core.width + bar->core.border_width;
  544.             if (clip_width < 1) clip_width = 1;
  545.             CheckHoriz();
  546.         }
  547.         intended.height = preferred.height;
  548.         }
  549.         if ( !w->viewport.allowhoriz ||
  550.          (int)preferred.width < clip_width) {
  551.             intended.width = clip_width;
  552.         intended.request_mode |= CWWidth;
  553.         }
  554.         if ( !w->viewport.allowvert ||
  555.          (int)preferred.height < clip_height) {
  556.             intended.height = clip_height;
  557.         intended.request_mode |= CWHeight;
  558.         }
  559.     } while (intended.request_mode != prev_mode
  560.          || (intended.request_mode & CWWidth
  561.              && intended.width != prev_width)
  562.          || (intended.request_mode & CWHeight
  563.              && intended.height != prev_height));
  564.     }
  565.  
  566.     if (XtIsRealized(clip))
  567.     XRaiseWindow( XtDisplay(clip), XtWindow(clip) );
  568.  
  569.     XtMoveWidget( clip,
  570.           needsvert ? (w->viewport.useright ? 0 :
  571.                    w->viewport.vert_bar->core.width +
  572.                    w->viewport.vert_bar->core.border_width) : 0,
  573.           needshoriz ? (w->viewport.usebottom ? 0 :
  574.                 w->viewport.horiz_bar->core.height +
  575.                     w->viewport.horiz_bar->core.border_width) : 0);
  576.     XtResizeWidget( clip, (Dimension)clip_width,
  577.             (Dimension)clip_height, (Dimension)0 );
  578.     
  579.     if (w->viewport.horiz_bar != (Widget)NULL) {
  580.     register Widget bar = w->viewport.horiz_bar;
  581.     if (!needshoriz) {
  582.         constraints->form.vert_base = (Widget)NULL;
  583.         if (destroy_scrollbars) {
  584.         XtDestroyWidget( bar );
  585.         w->viewport.horiz_bar = (Widget)NULL;
  586.         }
  587.     }
  588.     else {
  589.         register int bw = bar->core.border_width;
  590.         XtResizeWidget( bar, clip_width, bar->core.height, bw );
  591.         XtMoveWidget( bar,
  592.               ((needsvert && !w->viewport.useright)
  593.                ? w->viewport.vert_bar->core.width
  594.                : -bw),
  595.               (w->viewport.usebottom
  596.                 ? w->core.height - bar->core.height - bw
  597.                 : -bw) );
  598.         XtSetMappedWhenManaged( bar, True );
  599.     }
  600.     }
  601.  
  602.     if (w->viewport.vert_bar != (Widget)NULL) {
  603.     register Widget bar = w->viewport.vert_bar;
  604.     if (!needsvert) {
  605.         constraints->form.horiz_base = (Widget)NULL;
  606.         if (destroy_scrollbars) {
  607.         XtDestroyWidget( bar );
  608.         w->viewport.vert_bar = (Widget)NULL;
  609.         }
  610.     }
  611.     else {
  612.         register int bw = bar->core.border_width;
  613.         XtResizeWidget( bar, bar->core.width, clip_height, bw );
  614.         XtMoveWidget( bar,
  615.               (w->viewport.useright
  616.                ? w->core.width - bar->core.width - bw 
  617.                : -bw),
  618.               ((needshoriz && !w->viewport.usebottom)
  619.                 ? w->viewport.horiz_bar->core.height
  620.                 : -bw) );
  621.         XtSetMappedWhenManaged( bar, True );
  622.     }
  623.     }
  624.  
  625.     if (child != (Widget)NULL) {
  626.     XtResizeWidget( child, (Dimension)intended.width,
  627.                 (Dimension)intended.height, (Dimension)0 );
  628.     MoveChild(w,
  629.           needshoriz ? child->core.x : 0,
  630.           needsvert ? child->core.y : 0);
  631.     }
  632.  
  633.     SendReport (w, XawPRAll);
  634. }
  635.  
  636. /*      Function Name: ComputeWithForceBars
  637.  *      Description: Computes the layout give forcebars is set.
  638.  *      Arguments: widget - the viewport widget.
  639.  *                 query - whether or not to query the child.
  640.  *                 intended - the cache of the childs height is
  641.  *                            stored here ( USED AND RETURNED ).
  642.  *                 clip_width, clip_height - size of clip window.
  643.  *                                           (USED AND RETURNED ).
  644.  *      Returns: none.
  645.  */
  646.  
  647. static void
  648. ComputeWithForceBars(widget, query, intended, clip_width, clip_height)
  649. Widget widget;
  650. Boolean query;
  651. XtWidgetGeometry * intended;
  652. int *clip_width, *clip_height;
  653. {
  654.     ViewportWidget w = (ViewportWidget)widget;
  655.     register Widget child = w->viewport.child;
  656.     XtWidgetGeometry preferred;
  657.  
  658. /*
  659.  * If forcebars then needs = allows = has.
  660.  * Thus if needsvert is set it MUST have a scrollbar.
  661.  */
  662.  
  663.     if (w->viewport.allowvert) {
  664.     if (w->viewport.vert_bar == NULL) 
  665.         w->viewport.vert_bar = CreateScrollbar(w, False);
  666.  
  667.     *clip_width -= w->viewport.vert_bar->core.width +
  668.                w->viewport.vert_bar->core.border_width;
  669.     }
  670.  
  671.     if (w->viewport.allowhoriz) {
  672.     if (w->viewport.horiz_bar == NULL) 
  673.         w->viewport.horiz_bar = CreateScrollbar(w, True);
  674.  
  675.         *clip_height -= w->viewport.horiz_bar->core.height +
  676.                w->viewport.horiz_bar->core.border_width;
  677.     }
  678.  
  679.     AssignMax( *clip_width, 1 );
  680.     AssignMax( *clip_height, 1 );
  681.  
  682.     if (!w->viewport.allowvert) {
  683.         intended->height = *clip_height;
  684.         intended->request_mode = CWHeight;
  685.     }
  686.     if (!w->viewport.allowhoriz) {
  687.         intended->width = *clip_width;
  688.         intended->request_mode = CWWidth;
  689.     }
  690.  
  691.     if ( query ) {
  692.         if ( (w->viewport.allowvert || w->viewport.allowhoriz) ) { 
  693.         XtQueryGeometry( child, intended, &preferred );
  694.       
  695.         if ( !(intended->request_mode & CWWidth) )
  696.             if ( preferred.request_mode & CWWidth )
  697.             intended->width = preferred.width;
  698.         else
  699.             intended->width = child->core.width;
  700.  
  701.         if ( !(intended->request_mode & CWHeight) )
  702.             if ( preferred.request_mode & CWHeight )
  703.             intended->height = preferred.height;
  704.         else
  705.             intended->height = child->core.height;
  706.     }
  707.     }
  708.     else {
  709.         if (w->viewport.allowvert)
  710.         intended->height = child->core.height;
  711.     if (w->viewport.allowhoriz)
  712.         intended->width = child->core.width;
  713.     }
  714.  
  715.     if (*clip_width > (int)intended->width)
  716.     intended->width = *clip_width;
  717.     if (*clip_height > (int)intended->height)
  718.     intended->height = *clip_height;
  719. }
  720.  
  721. static void Resize(widget)
  722.     Widget widget;
  723. {
  724.     ComputeLayout( widget, /*query=*/True, /*destroy=*/True );
  725. }
  726.  
  727.  
  728. /* ARGSUSED */
  729. static Boolean Layout(w, width, height)
  730.     FormWidget w;
  731.     Dimension width, height;
  732. {
  733.     ComputeLayout( (Widget)w, /*query=*/True, /*destroy=*/True );
  734.     w->form.preferred_width = w->core.width;
  735.     w->form.preferred_height = w->core.height;
  736.     return False;
  737. }
  738.  
  739.  
  740. static void ScrollUpDownProc(widget, closure, call_data)
  741.     Widget widget;
  742.     XtPointer closure;
  743.     XtPointer call_data;
  744. {
  745.     ViewportWidget w = (ViewportWidget)closure;
  746.     register Widget child = w->viewport.child;
  747.     int pix = (int)call_data;
  748.     Position x, y;
  749.  
  750.     if (child == NULL) return;    /* no child to scroll. */
  751.  
  752.     x = child->core.x - ((widget == w->viewport.horiz_bar) ? pix : 0);
  753.     y = child->core.y - ((widget == w->viewport.vert_bar) ? pix : 0);
  754.     MoveChild(w, x, y);
  755. }
  756.  
  757.  
  758. /* ARGSUSED */
  759. static void ThumbProc(widget, closure, percent)
  760.     Widget widget;
  761.     XtPointer closure;
  762.     float *percent;
  763. {
  764.     ViewportWidget w = (ViewportWidget)closure;
  765.     register Widget child = w->viewport.child;
  766.     Position x, y;
  767.  
  768.     if (child == NULL) return;    /* no child to scroll. */
  769.  
  770.     if (widget == w->viewport.horiz_bar)
  771. #ifdef macII                /* bug in the macII A/UX 1.0 cc */
  772.     x = (int)(-*percent * child->core.width);
  773. #else /* else not macII */
  774.     x = -(int)(*percent * child->core.width);
  775. #endif /* macII */
  776.     else
  777.     x = child->core.x;
  778.  
  779.     if (widget == w->viewport.vert_bar)
  780. #ifdef macII                /* bug in the macII A/UX 1.0 cc */
  781.     y = (int)(-*percent * child->core.height);
  782. #else /* else not macII */
  783.     y = -(int)(*percent * child->core.height);
  784. #endif /* macII */
  785.     else
  786.     y = child->core.y;
  787.  
  788.     MoveChild(w, x, y);
  789. }
  790.  
  791. static XtGeometryResult
  792. TestSmaller(w, request, reply_return)
  793.      ViewportWidget w; XtWidgetGeometry *request, *reply_return;
  794. {
  795.   if (request->width < w->core.width || request->height < w->core.height)
  796.     return XtMakeGeometryRequest((Widget)w, request, reply_return);
  797.   else
  798.     return XtGeometryYes;  
  799. }
  800.  
  801. static XtGeometryResult
  802. GeometryRequestPlusScrollbar(w, horizontal, request, reply_return)
  803.      Boolean horizontal;
  804.      ViewportWidget w; 
  805.      XtWidgetGeometry *request, *reply_return;
  806. {
  807.   Widget sb;
  808.   XtWidgetGeometry plusScrollbars;
  809.   plusScrollbars = *request;
  810.   if ((sb = w->viewport.horiz_bar) == (Widget)NULL)
  811.     sb = CreateScrollbar( w, horizontal);
  812.   request->width += sb->core.width;
  813.   request->height += sb->core.height;
  814.   XtDestroyWidget(sb);
  815.   return XtMakeGeometryRequest((Widget) w, &plusScrollbars, reply_return);
  816.  }
  817.  
  818. #define WidthChange() (request->width != w->core.width)
  819. #define HeightChange() (request->height != w->core.height)
  820.  
  821. static XtGeometryResult 
  822. QueryGeometry(w, request, reply_return)
  823.      ViewportWidget w; XtWidgetGeometry *request, *reply_return;
  824. {    
  825.   if (w->viewport.allowhoriz && w->viewport.allowvert) 
  826.     return TestSmaller(w, request, reply_return);
  827.  
  828.   else if (w->viewport.allowhoriz && !w->viewport.allowvert) {
  829.     if (WidthChange() && !HeightChange())
  830.       return TestSmaller(w, request, reply_return);
  831.     else if (!WidthChange() && HeightChange())
  832.       return XtMakeGeometryRequest((Widget) w, request, reply_return);
  833.     else if (WidthChange() && HeightChange()) /* hard part */
  834.       return GeometryRequestPlusScrollbar(w, True, request, reply_return);
  835.     else /* !WidthChange() && !HeightChange() */
  836.       return XtGeometryYes;
  837.   }
  838.   else if (!w->viewport.allowhoriz && w->viewport.allowvert) {
  839.     if (!WidthChange() && HeightChange())
  840.       return TestSmaller(w, request, reply_return);
  841.     else if (WidthChange() && !HeightChange())
  842.       return XtMakeGeometryRequest((Widget)w, request, reply_return);
  843.     else if (WidthChange() && HeightChange()) /* hard part */
  844.       return GeometryRequestPlusScrollbar(w, False, request, reply_return);
  845.     else /* !WidthChange() && !HeightChange() */
  846.       return XtGeometryYes;
  847.   }      
  848.   else /* (!w->viewport.allowhoriz && !w->viewport.allowvert) */
  849.     return XtMakeGeometryRequest((Widget) w, request, reply_return);
  850. }
  851.  
  852. #undef WidthChange
  853. #undef HeightChange
  854.  
  855. static XtGeometryResult GeometryManager(child, request, reply)
  856.     Widget child;
  857.     XtWidgetGeometry *request, *reply;
  858. {
  859.     ViewportWidget w = (ViewportWidget)child->core.parent;
  860.     Boolean rWidth = (Boolean)(request->request_mode & CWWidth);
  861.     Boolean rHeight = (Boolean)(request->request_mode & CWHeight);
  862.     XtWidgetGeometry allowed;
  863.     XtGeometryResult result;
  864.     Boolean reconfigured;
  865.     Boolean child_changed_size;
  866.     Dimension height_remaining;
  867.  
  868.     if (request->request_mode & XtCWQueryOnly)
  869.       return QueryGeometry(w, request, reply);
  870.  
  871.     if (child != w->viewport.child
  872.         || request->request_mode & ~(CWWidth | CWHeight
  873.                      | CWBorderWidth)
  874.     || ((request->request_mode & CWBorderWidth)
  875.         && request->border_width > 0))
  876.     return XtGeometryNo;
  877.  
  878.     allowed = *request;
  879.  
  880.     reconfigured = GetGeometry( (Widget)w,
  881.                     (rWidth ? request->width : w->core.width),
  882.                     (rHeight ? request->height : w->core.height)
  883.                   );
  884.  
  885.     child_changed_size = ((rWidth && child->core.width != request->width) ||
  886.               (rHeight && child->core.height != request->height));
  887.  
  888.     height_remaining = w->core.height;
  889.     if (rWidth && w->core.width != request->width) {
  890.     if (w->viewport.allowhoriz && request->width > w->core.width) {
  891.         /* horizontal scrollbar will be needed so possibly reduce height */
  892.         Widget bar; 
  893.         if ((bar = w->viewport.horiz_bar) == (Widget)NULL)
  894.         bar = CreateScrollbar( w, True );
  895.         height_remaining -= bar->core.height + bar->core.border_width;
  896.         reconfigured = True;
  897.     }
  898.     else {
  899.         allowed.width = w->core.width;
  900.     }
  901.     }
  902.     if (rHeight && height_remaining != request->height) {
  903.     if (w->viewport.allowvert && request->height > height_remaining) {
  904.         /* vertical scrollbar will be needed, so possibly reduce width */
  905.         if (!w->viewport.allowhoriz || request->width < w->core.width) {
  906.         Widget bar;
  907.         if ((bar = w->viewport.vert_bar) == (Widget)NULL)
  908.             bar = CreateScrollbar( w, False );
  909.         if (!rWidth) {
  910.             allowed.width = w->core.width;
  911.             allowed.request_mode |= CWWidth;
  912.         }
  913.         if ( (int)allowed.width >
  914.              (int)(bar->core.width + bar->core.border_width) )
  915.             allowed.width -= bar->core.width + bar->core.border_width;
  916.         else
  917.             allowed.width = 1;
  918.         reconfigured = True;
  919.         }
  920.     }
  921.     else {
  922.         allowed.height = height_remaining;
  923.     }
  924.     }
  925.  
  926.     if (allowed.width != request->width || allowed.height != request->height) {
  927.     *reply = allowed;
  928.     result = XtGeometryAlmost;
  929.     }
  930.     else {
  931.     if (rWidth)  child->core.width = request->width;
  932.     if (rHeight) child->core.height = request->height;
  933.     result = XtGeometryYes;
  934.     }
  935.  
  936.     if (reconfigured || child_changed_size)
  937.     ComputeLayout( (Widget)w,
  938.                /*query=*/ False,
  939.                /*destroy=*/ (result == XtGeometryYes) ? True : False );
  940.  
  941.     return result;
  942.   }
  943.  
  944.  
  945. static Boolean GetGeometry(w, width, height)
  946.     Widget w;
  947.     Dimension width, height;
  948. {
  949.     XtWidgetGeometry geometry, return_geom;
  950.     XtGeometryResult result;
  951.  
  952.     if (width == w->core.width && height == w->core.height)
  953.     return False;
  954.  
  955.     geometry.request_mode = CWWidth | CWHeight;
  956.     geometry.width = width;
  957.     geometry.height = height;
  958.  
  959.     if (XtIsRealized(w)) {
  960.     if (((ViewportWidget)w)->viewport.allowhoriz && width > w->core.width)
  961.         geometry.width = w->core.width;
  962.     if (((ViewportWidget)w)->viewport.allowvert && height > w->core.height)
  963.         geometry.height = w->core.height;
  964.     } else {
  965.     /* This is the Realize call; we'll inherit a w&h iff none currently */
  966.     if (w->core.width != 0) {
  967.         if (w->core.height != 0) return False;
  968.         geometry.width = w->core.width;
  969.     }
  970.     if (w->core.height != 0) geometry.height = w->core.height;
  971.     }
  972.  
  973.     result = XtMakeGeometryRequest(w, &geometry, &return_geom);
  974.     if (result == XtGeometryAlmost)
  975.     result = XtMakeGeometryRequest(w, &return_geom, NULL);
  976.  
  977.     return (result == XtGeometryYes);
  978. }
  979.  
  980. static XtGeometryResult PreferredGeometry(w, constraints, reply)
  981.     Widget w;
  982.     XtWidgetGeometry *constraints, *reply;
  983. {
  984.     if (((ViewportWidget)w)->viewport.child != NULL)
  985.     return XtQueryGeometry( ((ViewportWidget)w)->viewport.child,
  986.                    constraints, reply );
  987.     else
  988.     return XtGeometryYes;
  989. }
  990.  
  991.  
  992. void
  993. #if NeedFunctionPrototypes
  994. XawViewportSetLocation (Widget gw,
  995. #if NeedWidePrototypes
  996.             double xoff, double yoff)
  997. #else
  998.             float xoff, float yoff)
  999. #endif
  1000. #else
  1001. XawViewportSetLocation (gw, xoff, yoff)
  1002.     Widget gw;
  1003.     float  xoff,yoff;
  1004. #endif
  1005. {
  1006.     ViewportWidget w = (ViewportWidget) gw;
  1007.     register Widget child = w->viewport.child;
  1008.     Position x, y;
  1009.  
  1010.     if (xoff > 1.0)            /* scroll to right */
  1011.        x = child->core.width;
  1012.     else if (xoff < 0.0)        /* if the offset is < 0.0 nothing */ 
  1013.        x = child->core.x;
  1014.     else
  1015.        x = (Position) (((float) child->core.width) * xoff);
  1016.  
  1017.     if (yoff > 1.0) 
  1018.        y = child->core.height;
  1019.     else if (yoff < 0.0)
  1020.        y = child->core.y;
  1021.     else
  1022.        y = (Position) (((float) child->core.height) * yoff);
  1023.  
  1024.     MoveChild (w, -x, -y);
  1025. }
  1026.  
  1027. void
  1028. #if NeedFunctionPrototypes
  1029. XawViewportSetCoordinates (Widget gw,
  1030. #if NeedWidePrototypes
  1031.                int x, int y)
  1032. #else
  1033.                Position x, Position y)
  1034. #endif
  1035. #else
  1036. XawViewportSetCoordinates (gw, x, y)
  1037.     Widget gw;
  1038.     Position x, y;
  1039. #endif
  1040. {
  1041.     ViewportWidget w = (ViewportWidget) gw;
  1042.     Widget child = w->viewport.child;
  1043.  
  1044.     if (x > (int)child->core.width) 
  1045.       x = child->core.width;
  1046.     else if (x < 0)
  1047.       x = child->core.x;
  1048.  
  1049.     if (y > (int)child->core.height)
  1050.       y = child->core.height;
  1051.     else if (y < 0)
  1052.       y = child->core.y;
  1053.  
  1054.     MoveChild (w, -x, -y);
  1055. }
  1056.  
  1057.