libpayload: remove trailing whitespace and run dos2unix
[coreboot.git] / payloads / libpayload / curses / PDCurses-3.4 / x11 / ScrollBox.c
1 /*
2  * Copyright 1989 O'Reilly and Associates, Inc.
3
4      The X Consortium, and any party obtaining a copy of these files from
5      the X Consortium, directly or indirectly, is granted, free of charge, a
6      full and unrestricted irrevocable, world-wide, paid up, royalty-free,
7      nonexclusive right and license to deal in this software and
8      documentation files (the "Software"), including without limitation the
9      rights to use, copy, modify, merge, publish, distribute, sublicense,
10      and/or sell copies of the Software, and to permit persons who receive
11      copies from any such party to do so.  This license includes without
12      limitation a license to do the foregoing actions under any patents of
13      the party supplying this software to the X Consortium.
14
15      $Id: ScrollBox.c,v 1.15 2008/07/14 04:24:52 wmcbrine Exp $
16  */
17
18 /* ScrollBox.c - scrollBox composite widget */
19
20 #include <X11/IntrinsicP.h>
21 #include <X11/StringDefs.h>
22 #include <X11/Shell.h>
23
24 #include "x11/ScrollBoxP.h"
25
26 #include <stdio.h>
27
28 #define INITIAL_WIDTH 300
29 #define INITIAL_HEIGHT 300
30
31 /************************************************************************
32  *                                                                      *
33  * scrollBox Resources                                                  *
34  *                                                                      *
35  ************************************************************************/
36
37 static XtResource resources[] =
38 {
39     { XtNhSpace, XtCHSpace, XtRDimension, sizeof(Dimension),
40         XtOffset(ScrollBoxWidget, scrollBox.h_space),
41         XtRImmediate, (XtPointer)4 },
42     { XtNvSpace, XtCVSpace, XtRDimension, sizeof(Dimension),
43         XtOffset(ScrollBoxWidget, scrollBox.v_space),
44         XtRImmediate, (XtPointer)4 },
45     { XtNheightInc, XtCHeightInc, XtRDimension, sizeof(Dimension),
46         XtOffset(ScrollBoxWidget, scrollBox.increment_height),
47         XtRImmediate, (XtPointer)13 },
48     { XtNwidthInc, XtCWidthInc, XtRDimension, sizeof(Dimension),
49         XtOffset(ScrollBoxWidget, scrollBox.increment_width),
50         XtRImmediate, (XtPointer)7 },
51 };
52
53 /************************************************************************
54  *                                                                      *
55  * Full class record constant                                           *
56  *                                                                      *
57  ************************************************************************/
58
59 static void Initialize(Widget, Widget, ArgList, Cardinal *);
60 static void Resize(Widget);
61 static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
62 static void ChangeManaged(Widget);
63 static XtGeometryResult QueryGeometry(Widget, XtWidgetGeometry *,
64                                       XtWidgetGeometry *);
65 static XtGeometryResult GeometryManager(Widget, XtWidgetGeometry *,
66                                         XtWidgetGeometry *);
67 static void RefigureLocations(Widget);
68
69 ScrollBoxClassRec scrollBoxClassRec = {
70   {
71     /* core_class fields        */
72         /* superclass           */  (WidgetClass) &compositeClassRec,
73         /* class_name           */  "scrollBox",
74         /* widget_size          */  sizeof(ScrollBoxRec),
75         /* class_initialize     */  NULL,
76         /* class_part_init      */  NULL,
77         /* class_inited         */  FALSE,
78         /* initialize           */  Initialize,
79         /* initialize_hook      */  NULL,
80         /* realize              */  XtInheritRealize,
81         /* actions              */  NULL,
82         /* num_actions          */  0,
83         /* resources            */  resources,
84         /* num_resources        */  XtNumber(resources),
85         /* xrm_class            */  NULLQUARK,
86         /* compress_motion      */  TRUE,
87         /* compress_exposure    */  TRUE,
88         /* compress_enterleave  */  TRUE,
89         /* visible_interest     */  FALSE,
90         /* destroy              */  NULL,
91         /* resize               */  Resize,
92         /* expose               */  NULL,
93         /* set_values           */  SetValues,
94         /* set_values_hook      */  NULL,
95         /* set_values_almost    */  XtInheritSetValuesAlmost,
96         /* get_values_hook      */  NULL,
97         /* accept_focus         */  NULL,
98         /* version              */  XtVersion,
99         /* callback_private     */  NULL,
100         /* tm_table             */  NULL,
101         /* query_geometry       */  QueryGeometry,
102         /* display_accelerator  */  XtInheritDisplayAccelerator,
103         /* extension            */  NULL
104   },{
105     /* composite_class fields   */
106         /* geometry_manager     */  GeometryManager,
107         /* change_managed       */  ChangeManaged,
108         /* insert_child         */  XtInheritInsertChild,
109         /* delete_child         */  XtInheritDeleteChild,
110         /* extension            */  NULL
111   },{
112     /* scrollBox class fields   */
113         /* empty                */  0,
114   }
115 };
116
117 WidgetClass scrollBoxWidgetClass = (WidgetClass)&scrollBoxClassRec;
118
119
120 /************************************************************************
121  *                                                                      *
122  * Private Routines                                                     *
123  *                                                                      *
124  ************************************************************************/
125
126 /* Do a layout, either actually assigning positions, or just
127    calculating size. */
128
129 static void DoLayout(Widget w, Boolean doit)
130 {
131     ScrollBoxWidget sbw = (ScrollBoxWidget)w;
132     Widget wmain, vscroll, hscroll, child;
133     Dimension mw, mh;   /* main window */
134     Dimension vh;   /* vertical scrollbar length (height) */
135     Dimension hw;   /* horizontal scrollbar length (width) */
136     Position vx;
137     Position hy;
138     Cardinal i;
139
140     if (sbw->composite.num_children != 3)
141         XtAppError(XtWidgetToApplicationContext(w),
142             "ScrollBox: must manage exactly three widgets.");
143
144     for (i = 0; i < sbw->composite.num_children; i++)
145     {
146         child = sbw->composite.children[i];
147
148         if (!XtIsManaged(child))
149             XtAppError(XtWidgetToApplicationContext(w),
150                 "ScrollBox: all three widgets must be managed.");
151     }
152
153     /* Child one is the main window, two is the vertical scrollbar,
154        and three is the horizontal scrollbar. */
155
156     wmain = sbw->composite.children[0];
157     vscroll = sbw->composite.children[1];
158     hscroll = sbw->composite.children[2];
159
160     /* Size all three widgets so that space is fully utilized. */
161
162     mw = sbw->core.width - (2 * sbw->scrollBox.h_space) -
163         vscroll->core.width - (2 * vscroll->core.border_width) -
164         (2 * wmain->core.border_width);
165
166     mh = sbw->core.height - (2 * sbw->scrollBox.v_space) -
167         hscroll->core.height - (2 * hscroll->core.border_width) -
168         (2 * wmain->core.border_width);
169
170     /* Force the main window to be sized to the appropriate increment. */
171
172     mw = (mw / sbw->scrollBox.increment_width) *
173         sbw->scrollBox.increment_width;
174
175     mh = ((mh / sbw->scrollBox.increment_height) *
176         sbw->scrollBox.increment_height) +
177         sbw->scrollBox.increment_height;
178
179     vx = wmain->core.x + mw + sbw->scrollBox.h_space +
180         wmain->core.border_width + vscroll->core.border_width;
181
182     hy = wmain->core.y + mh + sbw->scrollBox.v_space +
183         wmain->core.border_width + hscroll->core.border_width;
184
185     vh = mh;   /* scrollbars are always same length as main window */
186     hw = mw;
187
188     if (doit)
189     {
190         XtResizeWidget(wmain, mw, mh, 1);
191
192         XtResizeWidget(vscroll, vscroll->core.width, vh, 1);
193         XtMoveWidget(vscroll, vx, vscroll->core.y);
194
195         XtResizeWidget(hscroll, hw, hscroll->core.height, 1);
196         XtMoveWidget(hscroll, hscroll->core.x, hy);
197     }
198 }
199
200 static XtGeometryResult GeometryManager(Widget w, XtWidgetGeometry *request,
201                                         XtWidgetGeometry *reply)
202 {
203     XtWidgetGeometry allowed;
204
205     if (request->request_mode & ~(XtCWQueryOnly | CWWidth | CWHeight))
206         return XtGeometryNo;
207
208     if (request->request_mode & CWWidth)
209         allowed.width = request->width;
210     else
211         allowed.width = w->core.width;
212
213     if (request->request_mode & CWHeight)
214         allowed.height = request->height;
215     else
216         allowed.height = w->core.height;
217
218     if (allowed.width == w->core.width && allowed.height == w->core.height)
219         return XtGeometryNo;
220
221     if (!(request->request_mode & XtCWQueryOnly))
222         RefigureLocations(w);
223
224     return XtGeometryYes;
225 }
226
227 static void RefigureLocations(Widget w)
228 {
229     DoLayout(w, False);
230 }
231
232 /* Calculate preferred size.  We can't just use the current sizes
233    of the children, because that calculation would always end up with
234    our current size.  Could query each child, and use that size to
235    recalculate a size for us, then if it ends up being larger than width
236    and height passed in, accept bounding box. However, we know our
237    children and they don't have any particular preferred geometry,
238    except the bigger the better. Therefore, if the parent suggested a
239    size, we'll take it. */
240
241 static XtGeometryResult QueryGeometry(Widget w, XtWidgetGeometry *request,
242                                       XtWidgetGeometry *reply_return)
243 {
244     XtGeometryResult result=XtGeometryNo;
245
246     request->request_mode &= CWWidth | CWHeight;
247
248     /* parent isn't going to change w or h, so nothing to re-compute */
249
250     if (request->request_mode == 0)
251         return XtGeometryYes;
252
253     /* if proposed size is large enough, accept it.  Otherwise, suggest
254        our arbitrary initial size. */
255
256     if (request->request_mode & CWHeight)
257     {
258         if (request->height < INITIAL_HEIGHT)
259         {
260             result = XtGeometryAlmost;
261             reply_return->height = INITIAL_HEIGHT;
262             reply_return->request_mode &= CWHeight;
263         }
264         else
265             result = XtGeometryYes;
266     }
267
268     if (request->request_mode & CWWidth)
269     {
270         if (request->width < INITIAL_WIDTH)
271         {
272             result = XtGeometryAlmost;
273             reply_return->width = INITIAL_WIDTH;
274             reply_return->request_mode &= CWWidth;
275         }
276         else
277             result = XtGeometryYes;
278     }
279
280     return result;
281 }
282
283 /* Actually layout the scrollBox  */
284
285 static void Resize(Widget w)
286 {
287     DoLayout(w, True);
288 }
289
290 static void ChangeManaged(Widget w)
291 {
292     DoLayout(w, True);
293 }
294
295 static void Initialize(Widget request, Widget new,
296                        ArgList args, Cardinal *num_args)
297 {
298     ScrollBoxWidget newsbw = (ScrollBoxWidget)new;
299
300     if (newsbw->core.width == 0)
301         newsbw->core.width = INITIAL_WIDTH;
302
303     if (newsbw->core.height == 0)
304         newsbw->core.height = INITIAL_HEIGHT;
305
306 }
307
308 static Boolean SetValues(Widget current, Widget request, Widget new,
309                          ArgList args, Cardinal *num_args)
310 {
311     ScrollBoxWidget sbwcurrent = (ScrollBoxWidget)current;
312     ScrollBoxWidget sbwnew = (ScrollBoxWidget)new;
313
314     /* need to relayout if h_space or v_space change */
315
316     if ((sbwnew->scrollBox.h_space != sbwcurrent->scrollBox.h_space) ||
317         (sbwnew->scrollBox.v_space != sbwcurrent->scrollBox.v_space))
318         DoLayout(new, True);
319
320     return False;
321 }