File: Tiled.c

package info (click to toggle)
xfaces 3.3-29
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 948 kB
  • ctags: 1,414
  • sloc: ansic: 7,487; makefile: 948; sh: 5
file content (616 lines) | stat: -rw-r--r-- 15,246 bytes parent folder | download | duplicates (7)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*                             -*- Mode: C++-C -*- 
 * 
 *		 Copyright 1994 Christopher B. Liebman
 *
 *     Permission to use, copy, modify, distribute, and sell this software
 *     and its documentation for any purpose is hereby granted without fee,
 *     provided that the above copyright notice appear in all copies and that
 *     both that copyright notice and this permission notice appear in
 *     supporting documentation, and that the name Christopher B. Liebman not
 *     be used in advertising or publicity pertaining to distribution of this
 *     software without specific, written prior permission.
 *
 *    THIS SOFTWARE IS PROVIDED `AS-IS'.  CHRISTOPHER B. LIEBMAN, DISCLAIMS
 *    ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
 *    LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL CHRISTOPHER
 *    B. LIEBMAN, BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING SPECIAL,
 *    INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA, OR
 *    PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
 *    WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF
 *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author          : Chris Liebman
 * Created On      : 
 * Last Modified By: Chris Liebman
 * Last Modified On: Sun Feb 13 16:58:19 1994
 * Update Count    : 8
 * Status          : Released
 * 
 * HISTORY
 * 13-Feb-1994		Chris Liebman	
 *    Last Modified: Sat Feb  5 20:43:46 1994 #6 (Chris Liebman)
 *    Allow this file to compile with a non-ansi compiler.
 * 
 * PURPOSE
 * 	Simple tiled layout widget
*/


#ifndef lint
static char RCSid[] = "$Id: Tiled.c,v 1.5 1994/03/07 20:15:55 liebman Exp $";
#endif  /* lint */

#include "TiledP.h"
#include "X11/Composite.h"
#include "X11/CompositeP.h"
#include <X11/Xmu/CharSet.h>

#define superclass	    ((CompositeWidgetClass)&compositeClassRec)

#ifdef __STDC__
#define P_(x)	x
#else
#define	P_(x)	(/* x */)
#endif

/*.
 ***************************************************************************

   Functional Prototypes. 

 ***************************************************************************
 */

static Boolean		SetValues P_((Widget 			gcurrent,
				      Widget			grequest,
				      Widget			gnew,
				      ArgList 			args,
				      Cardinal 			*num_args));

static Boolean		AcceptFocus P_((Widget			w,
					Time			*time));

static void		Layout P_((TiledWidget			tw));

static XtGeometryResult	GeometryHandler P_((Widget			w,
					    XtWidgetGeometry	*request,
					    XtWidgetGeometry	*result));

static void		ChangeManaged P_((Widget 		widget));

static void		InsertChild P_((Widget			new));

static void		DeleteChild P_((Widget			old));

static Boolean defTrue = True;

#define offset(field) 			XtOffsetOf(TiledRec, field)

static XtResource resources[] =
{
    {
	XtNvertSpacing, XtCHeight, XtRDimension, sizeof(Dimension),
	offset(tiled.vert_spacing), XtRImmediate, (XtPointer) 0
    },
    {
	XtNhorizSpacing, XtCHeight, XtRDimension, sizeof(Dimension),
	offset(tiled.horiz_spacing), XtRImmediate, (XtPointer) 0
    },
    {
	XtNinternalWidth, XtCWidth, XtRDimension, sizeof(Dimension),
	offset(tiled.internal_width), XtRImmediate, (XtPointer) 0
    },
    {
	XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
	offset(tiled.internal_height), XtRImmediate, (XtPointer) 0
    },
    {
	XtNtileWidth, XtCWidth, XtRDimension, sizeof(Dimension),
	offset(tiled.tile_width), XtRImmediate, (XtPointer) 64
    },
    {
	XtNtileHeight, XtCHeight, XtRDimension, sizeof(Dimension),
	offset(tiled.tile_height), XtRImmediate, (XtPointer) 64
    },
    {
	XtNsetWidth, XtCWidth, XtRDimension, sizeof(Dimension),
	offset(tiled.set_width), XtRImmediate, (XtPointer) 0
    },
    {
	XtNsetHeight, XtCHeight, XtRDimension, sizeof(Dimension),
	offset(tiled.set_height), XtRImmediate, (XtPointer) 0
    },
    {
	XtNminWidth, XtCWidth, XtRDimension, sizeof(Dimension),
	offset(tiled.min_width), XtRImmediate, (XtPointer) 0
    },
    {
	XtNminHeight, XtCHeight, XtRDimension, sizeof(Dimension),
	offset(tiled.min_height), XtRImmediate, (XtPointer) 0
    },
    {
	XtNmaxWidth, XtCWidth, XtRDimension, sizeof(Dimension),
	offset(tiled.max_width), XtRImmediate, (XtPointer) 0
    },
    {
	XtNmaxHeight, XtCHeight, XtRDimension, sizeof(Dimension),
	offset(tiled.max_height), XtRImmediate, (XtPointer) 0
    },
    {
	XtNlayout, XtCLayout, XtRBoolean, sizeof(Boolean),
	offset(tiled.layout), XtRBoolean, (caddr_t) &defTrue
    }
};

#undef offset

static void
NewSize(tw)
TiledWidget	tw;
{
    int		internal_width  = tw->tiled.internal_width;
    int		internal_height = tw->tiled.internal_height;
    int		vert_spacing	= tw->tiled.vert_spacing;
    int		horiz_spacing	= tw->tiled.horiz_spacing;
    int		tile_width	= tw->tiled.tile_width;
    int		tile_height	= tw->tiled.tile_height;
    int		min_width	= tw->tiled.min_width;
    int		min_height	= tw->tiled.min_height;
    int		max_width	= tw->tiled.max_width;
    int		max_height	= tw->tiled.max_height;
    int		width_in_tiles	= tw->tiled.set_width;
    int		height_in_tiles	= tw->tiled.set_height;
    int		tile_count	= tw->tiled.tile_count;
    Dimension	width;
    Dimension	height;
    
    /*
     *  If there is no set width then calulate the preferred width in tiles;
    */
    
    if (width_in_tiles == 0)
    {
	width_in_tiles = tile_count;
	
	if ((max_width  > 0) && (width_in_tiles > max_width))
	{
	    width_in_tiles = max_width;
	}
	else if ((min_width > 0) && (width_in_tiles < min_width))
	{
	    width_in_tiles = min_width;
	}
    }
    
    /*
     *  If there is no set height then calulate the preferred height in tiles;
    */
    
    if (height_in_tiles == 0)
    {
	/*
	 *  Calculate what we want based upon the width in tiles and the
	 * tile count.
	*/
	
	height_in_tiles = (tile_count / width_in_tiles);

	if ((height_in_tiles * width_in_tiles) < tile_count)
	{
	    height_in_tiles += 1;
	}
	
	if ((max_height  > 0) && (height_in_tiles > max_height))
	{
	    height_in_tiles = max_height;
	}
	else if ((min_height > 0) && (height_in_tiles < min_height))
	{
	    height_in_tiles = min_height;
	}
    }
    
    /*
     *  Compute width and height in pixels.
    */
    
    width  = (2 * internal_width +
	      width_in_tiles * tile_width +
	      (width_in_tiles - 1) * horiz_spacing);
    height = (2 * internal_height +
	      height_in_tiles * tile_height +
	      (height_in_tiles - 1) * vert_spacing);

    /*
     *  If this is already our size then we can stop now!
    */

    if ((tw->core.width  == width) &&
	(tw->core.height == height))
    {
	return;
    }
    
    /*
     * Now ask our parrent for this size.
    */
    
    switch (XtMakeResizeRequest((Widget)tw, width, height,
			&width, &height))
    {
      case XtGeometryYes:
	break;
	
      case XtGeometryNo:
	/*
	 *   Said no!  Keep the current size!
	*/
	
	width  = tw->core.width;
	height = tw->core.height;
	break;
	
      case XtGeometryAlmost:
	/*
	 *  We take whatever we can get!  (parent must accept as was returned
	 * "almost")
	*/
	
	(void)XtMakeResizeRequest((Widget)tw, width, height, &width, &height);
	break;
	
      default:
	/*
	 *  Don't understand result!
	*/
	
	width  = tw->core.width;
	height = tw->core.height;
	break;
    }
    
    /*
     * recompute tile size based upon new width and height.
     *
     * First remove internal_width.
    */
    
    width -= (2 * internal_width);
    
    /*
     * Stick in an extra horiz_spacing to compensate for the extra one in the
     * next calculation.
    */
    
    width += horiz_spacing;
    
    /*
     *   Compute the new width in tiles.
    */
    
    width_in_tiles = width / (tile_width + horiz_spacing);
    
    if (width_in_tiles < 1)
    {
	width_in_tiles = 1;
    }
    
    tw->tiled.width_in_tiles = width_in_tiles;

    /*
     * Remove internal height.
    */
    
    height -= (2 * internal_height);

    /*
     * Stick in an extra horiz_spacing to compensate for the extra one in the
     * next calculation.
    */
    
    height += vert_spacing;
    
    /*
     *   Compute the new height in tiles.
    */
    
    height_in_tiles = height / (tile_height + vert_spacing);
    
    if (height_in_tiles < 1)
    {
	height_in_tiles = 1;
    }
    
    tw->tiled.height_in_tiles = height_in_tiles;
}

static void
Layout(tw)
TiledWidget tw;
{
    int		i, pos;
    Position	x, y;
    Widget	child;
    int		internal_width  = tw->tiled.internal_width;
    int		internal_height = tw->tiled.internal_height;
    int		vert_spacing	= tw->tiled.vert_spacing;
    int		horiz_spacing	= tw->tiled.horiz_spacing;
    int		tile_width	= tw->tiled.tile_width;
    int		tile_height	= tw->tiled.tile_height;
    int		width_in_tiles  = tw->tiled.width_in_tiles;
    Dimension	width, height;
    
    /*
     * Compute the new positions.
    */
    
    for ( i = 0, pos = 0; i < tw->composite.num_children; i++ )
    {
	child = tw->composite.children[i];
	
	/*
	 * Ignore unmanaged children!
	*/
	
	if (!XtIsManaged( child ))
	{
	    continue;
	}
	
	/*
	 * Compute the position for the child.
	*/
	
	x = (pos % width_in_tiles) * tile_width
	    + (pos % width_in_tiles) * horiz_spacing + internal_width;
	
	y = (pos / width_in_tiles) * tile_height
	    + (pos / width_in_tiles) * vert_spacing + internal_height;
	
	x -= child->core.border_width;
	y -= child->core.border_width;

	/*
	 * We allow children to be smaller than the tile size but *not*
	 * bigger.
	*/

	height = child->core.height;
	if (height > tile_height)
	{
	    height = tile_height;
	}

	width = child->core.width;
	if (width > tile_width)
	{
	    width = tile_width;
	}
	
	XtConfigureWidget(child, x, y, width, height,
			  child->core.border_width);
	
	++pos;
    }
    
    return;
}

static Boolean
SetValues(gcurrent, grequest, gnew, args, num_args)
Widget 		gcurrent;
Widget		grequest;
Widget		gnew;
ArgList 	args;
Cardinal	*num_args;
{
    TiledWidget 	current = (TiledWidget) gcurrent;
    TiledWidget 	new = (TiledWidget) gnew;
    
    /*
     * If the tile size or internal width/height or itemSpacing changes then
     * call Layout().
    */
    
    if ((current->tiled.vert_spacing    != new->tiled.vert_spacing) ||
	(current->tiled.horiz_spacing    != new->tiled.horiz_spacing) ||
	(current->tiled.internal_width  != new->tiled.internal_width) ||
	(current->tiled.internal_height != new->tiled.internal_height) ||
	(current->tiled.tile_width      != new->tiled.tile_width) ||
	(current->tiled.tile_height     != new->tiled.tile_height) ||
	(current->tiled.set_width       != new->tiled.set_width) ||
	(current->tiled.set_height      != new->tiled.set_height) ||
	(current->tiled.min_width       != new->tiled.min_width) ||
	(current->tiled.min_height      != new->tiled.min_height) ||
	(current->tiled.max_width       != new->tiled.max_width) ||
	(current->tiled.max_height      != new->tiled.max_height))
    {
	if (new->tiled.layout)
	{
	    NewSize(new);
	    Layout(new);
	}
    }
    else if (!current->tiled.layout && new->tiled.layout)
    {
	NewSize(new);
	Layout(new);
    }
    
    return( FALSE );
}

static Boolean
AcceptFocus(w, time)
Widget	w;
Time	*time;
{
    return( True );
}


/*
 * The geometry handler.
*/
static XtGeometryResult
GeometryHandler(w, request, result)
Widget			w;		/* Child widget. */
XtWidgetGeometry	*request;
XtWidgetGeometry	*result;
{
    if (!(request->request_mode & XtCWQueryOnly))
    {
	if (request->request_mode & CWX)
	{
	    w->core.x = request->x;
	}
	if (request->request_mode & CWY)
	{
	    w->core.y = request->y;
	}
	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;
	}
    }
    
    return( XtGeometryYes );
}

static void
ChangeManaged(widget)
Widget	widget;
{
    TiledWidget		tw = (TiledWidget)widget;
    int			count;
    int			i;
    Widget		child;
    
    for ( i = 0, count = 0; i < tw->composite.num_children; i++ )
    {
	child = tw->composite.children[i];
	
	if (XtIsManaged( child ))
	{
	    ++count;
	}
    }
    
    tw->tiled.tile_count = count;
    
    if (tw->tiled.layout)
    {
	NewSize(tw);
	Layout(tw);
    }
    
    return;
}

static void
InsertChild(new)
Widget new;
{
    /*
     * Insert the child widget in the composite children list with the
     * superclass insert_child routine.
    */
    
    (*superclass->composite_class.insert_child)(new);
    return;
}

static void
DeleteChild(old)
Widget old;
{
    TiledWidget		tw;
    
    tw = (TiledWidget)XtParent( old );
    
    if (tw->tiled.layout)
    {
	NewSize(tw);
	Layout(tw);
    }
    
    /*
     * Delete the child widget in the composite children list with the
     * superclass delete_child routine.
    */
    
    (*superclass->composite_class.delete_child)(old);
    
    return;
}


/*.
 ***************************************************************************

   Full class record constant.

 ***************************************************************************
 */

TiledClassRec tiledClassRec =
{
    {
	/* core_class fields */	
	(WidgetClass)superclass,	    /* superclass               */
	"Tiled",			    /* class_name               */
	sizeof(TiledRec),		    /* widget_size              */
	NULL,				    /* class_initialize         */
	NULL,				    /* class_part_initialize    */
	FALSE,				    /* class_inited             */
	NULL,				    /* initialize               */
	NULL,				    /* initialize_hook          */
	XtInheritRealize,		    /* realize                  */
	NULL,				    /* actions                  */
	0,				    /* num_actions              */
	resources,			    /* resources                */
	XtNumber(resources),		    /* num_resources            */
	NULLQUARK,			    /* xrm_class                */
	TRUE,				    /* compress_motion          */
	TRUE,				    /* compress_exposure        */
	TRUE,				    /* compress_enterleave      */
	FALSE,				    /* visible_interest         */
	NULL,				    /* destroy                  */
	XtInheritResize,		    /* resize                   */
	NULL,				    /* expose                   */
	SetValues,			    /* set_values               */
	NULL,				    /* set_values_hook          */
	XtInheritSetValuesAlmost,	    /* set_values_almost        */
	NULL,				    /* get_values_hook          */
	AcceptFocus,			    /* accept_focus             */
	XtVersion,			    /* version                  */
	NULL,				    /* callback_private         */
	NULL,				    /* tm_table                 */
	XtInheritQueryGeometry,		    /* query_geometry           */
	XtInheritDisplayAccelerator,	    /* display_accelerator      */
	NULL				    /* extension                */
    },
    {
	/**** CompositePart *****/
	GeometryHandler,		    /* geometry_handler     */
	ChangeManaged,			    /* change_managed       */
	InsertChild,			    /* insert_child         */
	DeleteChild,			    /* delete_child         */
	NULL				    /* extension            */
    },
    {
	0,				    /* unused. */
    }
    
};

WidgetClass tiledWidgetClass = (WidgetClass)&tiledClassRec;