summaryrefslogtreecommitdiff
path: root/lwlib/lwlib-Xolmb.c
blob: a49e8ab54057fa04c5b79d502ec7a945a183a25c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* An OLIT menubar widget, by Chuck Thompson <cthomp@cs.uiuc.edu>
   Copyright (C) 1993 Lucid, Inc.

This file is part of the Lucid Widget Library.

The Lucid Widget Library is free software; you can redistribute it and/or 
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

The Lucid Widget Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include <X11/IntrinsicP.h>
#include <X11/Intrinsic.h>
#include <X11/CompositeP.h>
#include <X11/Composite.h>
#include "lwlib-Xol-mbP.h"
#include "lwlib-Xol-mb.h"

#define HORIZ_SPACING	4
#define VERT_SPACING	4

static void		Initialize();
static void		Resize();
static void		ChangeManaged();
static Boolean		SetValues();
static XtGeometryResult	GeometryManager();
static XtGeometryResult	PreferredSize();
static void		do_layout();
static XtGeometryResult try_layout();

lwMenuBarClassRec lwMenubarClassRec = 
{
  {
    /* core_class members */
    
    (WidgetClass) &compositeClassRec,	/* superclass			*/
    "Menubar",				/* class_name			*/
    sizeof(lwMenuBarRec),		/* widget_size			*/
    NULL,				/* class_initialize		*/
    NULL,				/* class_part_initialize	*/
    FALSE,				/* class_inited			*/
    Initialize,				/* initialize			*/
    NULL,				/* initialize_hook		*/
    XtInheritRealize,			/* realize			*/
    NULL,				/* actions			*/
    0,					/* num_actions			*/
    NULL,				/* resources			*/
    0,					/* num_resources		*/
    NULLQUARK,				/* xrm_class			*/
    TRUE,				/* compress_motion		*/
    XtExposeCompressMaximal,		/* compress_exposure		*/
    TRUE,				/* compress_enterleave		*/
    FALSE,				/* visible_interest		*/
    NULL,				/* destroy			*/
    Resize,				/* resize			*/
    NULL,				/* expose			*/
    NULL,				/* set_values			*/
    NULL,				/* set_values_hook		*/
    XtInheritSetValuesAlmost,		/* set_values_almost		*/
    NULL,				/* get_values_hook		*/
    NULL,				/* accept_focus			*/
    XtVersion,				/* version			*/
    NULL,				/* callback_private		*/
    NULL,				/* tm_table			*/
    PreferredSize,			/* query_geometry		*/
    NULL,				/* display_accelerator		*/
    NULL,				/* extension			*/
  },
  {
    /* composite_class members */
    
    GeometryManager,			/* geometry_manager		*/
    ChangeManaged,			/* change_managed		*/
    XtInheritInsertChild,		/* insert_child			*/
    XtInheritDeleteChild,		/* delete_child			*/
    NULL,				/* extension			*/
  },
  {
    /* Menubar class members */
    
    0,					/* empty			*/
  }
};
WidgetClass lwMenubarWidgetClass = (WidgetClass) &lwMenubarClassRec;


static void Initialize (request, new)
     lwMenuBarWidget	request, new;
{
  if (request->core.width <= 0)
    new->core.width = 1;
  if (request->core.height <= 0)
    new->core.height = 23;
}

static void
Resize (w)
     lwMenuBarWidget	w;
{
  do_layout(w);
}

static void
do_layout (parent)
     lwMenuBarWidget parent;
{
  Widget	child;
  int		cnt;
  int		managed_children = 0;
  int		managed_width = 0;
  int		new_pos = 0;

  /*
   * Determine number of children which will fit on one line.
   * For now we ignore the rest, making sure they are unmanaged.
   */

  cnt = 0;
  while ((cnt < (int) parent->composite.num_children) &&
	 (managed_width < (int) parent->core.width))
    {
      child = parent->composite.children[cnt++];
      managed_children++;
      managed_width += child->core.width + child->core.border_width * 2 +
	HORIZ_SPACING;
    }
  
  if (managed_width > (int) parent->core.width)
    managed_children--;
  
  /*
   * Correct positioning of children.
   */

  cnt = 0;
  while (managed_children)
    {
      child = parent->composite.children[cnt++];

      if (!child->core.managed)
	XtManageChild (child);

      if ((child->core.x != new_pos) || (child->core.y != 0))
	XtMoveWidget (child, new_pos, 0);
      new_pos += child->core.width + child->core.border_width * 2 +
	HORIZ_SPACING;

      managed_children--;
    }
  
  /*
   * Make sure all remaining children are unmanaged.
   */

  while (cnt < parent->composite.num_children)
    {
      child = parent->composite.children[cnt];
      
      if (child->core.managed)
	XtUnmanageChild (child);

      if ((child->core.x != parent->core.width) || 
	  (child->core.y != parent->core.height))
	XtMoveWidget (child, parent->core.width, parent->core.height);

      cnt++;
    }
}


static XtGeometryResult
PreferredSize (w, request, preferred)
     lwMenuBarWidget	w;
     XtWidgetGeometry	*request, *preferred;
{
  Widget	child;
  int		cnt;

  /*
   * If no changes are being made to the width or height, just agree.
   */

  if (!(request->request_mode & CWWidth) &&
      !(request->request_mode & CWHeight))
    return (XtGeometryYes);
  
  /*
   * Right now assume everything goes in one row.  Calculate the
   * minimum required width and height.
   */

  preferred->width = 0;
  preferred->height = 0;
  
  for (cnt = 0; cnt < w->composite.num_children; cnt++)
    {
      child = w->composite.children[cnt];
      if (child->core.managed)
	{
	  preferred->width += child->core.width + child->core.border_width*2 +
	    HORIZ_SPACING;
	  if (preferred->height < (Dimension) (child->core.height +
	      child->core.border_width * 2))
	    preferred->height = child->core.height +
	      child->core.border_width * 2;
	}
    }
  
  preferred->request_mode = CWWidth | CWHeight;

  /*
   * Case:  both height and width requested
   */

  if ((request->request_mode & CWWidth) &&
      (request->request_mode & CWHeight))
    {
      /*
       * Ok if same or bigger.
       */

      if (preferred->width <= request->width &&
	  preferred->height <= request->height)
	{
	  preferred->width = request->width;
	  return (XtGeometryYes);
	}
      
      /*
       * If both dimensions are too small, say no.
       */

      else
	if (preferred->width > request->width &&
	    preferred->height > request->height)
	  return (XtGeometryNo);
      
      /*
       * Otherwise one must be right, so say almost.
       */

      else
	return (XtGeometryAlmost);
    }
  
  /*
   * If only one dimension is requested, either its OK or it isn't.
   */

  else
    {
      if (request->request_mode & CWWidth)
	{
	  if (preferred->width <= request->width)
	    {
	      preferred->width = request->width;
	      return (XtGeometryYes);
	    }
	  else
	    return (XtGeometryNo);
	}
      else if (request->request_mode & CWHeight)
	{
	  if (preferred->height <= request->height)
	    {
	      return (XtGeometryYes);
	    }
	  else
	    return (XtGeometryNo);
	}

      return (XtGeometryYes);
    }
}
  

static XtGeometryResult
GeometryManager (w, request, reply)
     Widget		w;
     XtWidgetGeometry	*request;
     XtWidgetGeometry	*reply;
{
  
  lwMenuBarWidget	parent = (lwMenuBarWidget) w->core.parent;
  
  /*
   * If the widget wants to move, just say no.
   */

  if ((request->request_mode & CWX && request->x != w->core.x) ||
   (request->request_mode & CWY && request->y != w->core.y))
    return (XtGeometryNo);
  
  /*
   * Since everything "fits" for now, grant all requests.
   */

  if (request->request_mode & CWWidth)
    w->core.width = request->width;
  if (request->request_mode & CWHeight)
    w->core.height = request->height;
  if (request->request_mode & CWBorderWidth)
    w->core.border_width = request->border_width;
  
  do_layout (parent);
  return (XtGeometryYes);
}


static XtGeometryResult
try_layout (parent)
     lwMenuBarWidget parent;
{
  Widget	child;
  int		cnt;
  int		managed_children = 0;
  int		managed_width = 0;
  int		new_pos = 0;

  /*
   * Determine number of children which will fit on one line.
   * For now we ignore the rest, making sure they are unmanaged.
   */

  cnt = 0;
  while ((cnt < (int) parent->composite.num_children) &&
	 (managed_width < (int) parent->core.width))
    {
      child = parent->composite.children[cnt++];
      if (child->core.managed)
	{
	  managed_children++;
	  managed_width += child->core.width + child->core.border_width * 2 +
	    HORIZ_SPACING;
	}
    }

  if (managed_width > (int) parent->core.width)
    return (XtGeometryNo);
  else
    return (XtGeometryYes);
}


     
static void
ChangeManaged (w)
     lwMenuBarWidget	w;
{
  XtGeometryResult	result;
  
  result = try_layout (w);

  if (result != XtGeometryYes)
    {
      XtUnmanageChild (w->composite.children[w->composite.num_children - 1]);
      XtMoveWidget (w->composite.children[w->composite.num_children-1],
		    w->core.width, w->core.height);
    }
  
  do_layout (w);
}