File: ICLabel.c

package info (click to toggle)
kinput2 3.0-19
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,688 kB
  • ctags: 4,649
  • sloc: ansic: 45,508; makefile: 100; sh: 35
file content (365 lines) | stat: -rw-r--r-- 10,171 bytes parent folder | download | duplicates (10)
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
#ifndef lint
static char *rcsid = "$Id: ICLabel.c,v 1.11 1991/09/23 04:01:20 ishisone Rel $";
#endif
/*
 * Copyright (c) 1990  Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, 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 of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 */

#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/Converters.h>
#if XtSpecificationRelease > 4
#include <X11/Xfuncs.h>
#endif
#include "ICLabelP.h"
#include "ConvDisp.h"

static XtResource resources[] = {
#define offset(field) XtOffset(ICLabelWidget, iclabel.field)
    { XtNhorizontalSpacing, XtCSpacing, XtRDimension, sizeof(Dimension),
	offset(hspace), XtRString, "1" },
    { XtNverticalSpacing, XtCSpacing, XtRDimension, sizeof(Dimension),
	offset(vspace), XtRString, "1" },
    { XtNlabel, XtCLabel, XtRPointer, sizeof(ICString*),
	offset(label), XtRImmediate, NULL },
    { XtNjustify, XtCJustify, XtRJustify, sizeof(XtJustify),
	offset(justify), XtRImmediate, (XtPointer)XtJustifyCenter },
    { XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
	offset(cursor), XtRImmediate, (XtPointer)None },
#undef offset
};

static void ClassInitialize();
static void Initialize(), Destroy();
static void Realize();
static void Redisplay();
static Boolean SetValues();
static XtGeometryResult QueryGeometry();
static void InsertChild();

static void computeSize();
static ICString *copyString();
static void freeString();

static CompositeClassExtensionRec CompositeExtension = {
    /* next_extension		*/	NULL,
    /* record_type		*/	NULLQUARK,
    /* version			*/	XtCompositeExtensionVersion,
    /* record_size		*/	sizeof(CompositeClassExtensionRec),
    /* accept_objects		*/	True,
};

ICLabelClassRec icLabelClassRec = {
  { /* core fields */
    /* superclass		*/	(WidgetClass) &compositeClassRec,
    /* class_name		*/	"ICLabel",
    /* widget_size		*/	sizeof(ICLabelRec),
    /* class_initialize		*/	ClassInitialize,
    /* class_part_initialize	*/	NULL,
    /* class_inited		*/	FALSE,
    /* initialize		*/	Initialize,
    /* initialize_hook		*/	NULL,
    /* realize			*/	Realize,
    /* actions			*/	NULL,
    /* num_actions		*/	0,
    /* resources		*/	resources,
    /* num_resources		*/	XtNumber(resources),
    /* xrm_class		*/	NULLQUARK,
    /* compress_motion		*/	TRUE,
    /* compress_exposure	*/	TRUE,
    /* compress_enterleave	*/	TRUE,
    /* visible_interest		*/	FALSE,
    /* destroy			*/	Destroy,
    /* resize			*/	NULL,
    /* expose			*/	Redisplay,
    /* set_values		*/	SetValues,
    /* set_values_hook		*/	NULL,
    /* set_values_almost	*/	XtInheritSetValuesAlmost,
    /* get_values_hook		*/	NULL,
    /* accept_focus		*/	NULL,
    /* version			*/	XtVersion,
    /* callback_private		*/	NULL,
    /* tm_table			*/	NULL,
    /* query_geometry		*/	QueryGeometry,
    /* display_accelerator	*/	XtInheritDisplayAccelerator,
    /* extension		*/	NULL
  },
  { /* composite fields */
    /* geometry_manager		*/	NULL,
    /* change_managed		*/	NULL,
    /* insert_child		*/	InsertChild,
    /* delete_child		*/	XtInheritDeleteChild,
    /* extension		*/	(XtPointer)&CompositeExtension,
  },
  { /* iclabel fields */
    /* empty			*/	0
  }
};

WidgetClass icLabelWidgetClass = (WidgetClass)&icLabelClassRec;

static void
ClassInitialize()
{
    XtAddConverter(XtRString, XtRJustify, XmuCvtStringToJustify, NULL, 0);
}

/* ARGSUSED */
static void
Initialize(req, new, args, num_args)
Widget req;
Widget new;
ArgList args;
Cardinal *num_args;
{
    ICLabelWidget iclw = (ICLabelWidget)new;

    if (iclw->iclabel.label != NULL) {
	iclw->iclabel.label = copyString(iclw->iclabel.label);
    }
    iclw->iclabel.width = 0;
}

static void
Destroy(w)
Widget w;
{
    ICLabelWidget iclw = (ICLabelWidget)w;

    if (iclw->iclabel.label != NULL) freeString(iclw->iclabel.label);
}

static void
Realize(w, maskp, attr)
Widget w;
XtValueMask *maskp;
XSetWindowAttributes *attr;
{
    ICLabelWidget iclw = (ICLabelWidget)w;

    if (iclw->iclabel.cursor != None) {
	*maskp |= CWCursor;
	attr->cursor = iclw->iclabel.cursor;
    }
    (*icLabelWidgetClass->core_class.superclass->core_class.realize)(w, maskp, attr);
}

/* ARGSUSED */
static void
Redisplay(w, ev, region)
Widget w;
XEvent *ev;
Region region;
{
    ICLabelWidget iclw = (ICLabelWidget)w;
    Widget dispobj = iclw->iclabel.displayobj;
    int x, y;

    if (iclw->iclabel.label == NULL || iclw->iclabel.displayobj == NULL) return;

    switch (iclw->iclabel.justify) {
    case XtJustifyLeft:
	x = iclw->iclabel.hspace;
	break;
    case XtJustifyRight:
	x = iclw->core.width - (iclw->iclabel.vspace + iclw->iclabel.width);
	break;
    case XtJustifyCenter:
    default:
	x = (iclw->core.width - iclw->iclabel.width) / 2;
	break;
    }
    y = (iclw->core.height - iclw->iclabel.fontheight) / 2;

    CDDrawString(dispobj, w, iclw->iclabel.label, 0, -1, x, y);
}

/* ARGSUSED */
static Boolean
SetValues(cur, req, wid, args, num_args)
Widget cur;
Widget req;
Widget wid;
ArgList args;
Cardinal *num_args;
{
    ICLabelWidget old = (ICLabelWidget)cur;
    ICLabelWidget new = (ICLabelWidget)wid;
    Boolean widthspecified = False;
    Boolean heightspecified = False;
    Boolean redisplay = False;
    Boolean label_changed = False;
    int i;

    for (i = 0; i < *num_args; i++) {
	if (!strcmp(args->name, XtNwidth)) widthspecified = True;
	else if (!strcmp(args->name, XtNheight)) heightspecified = True;
    }

    if (new->iclabel.label != old->iclabel.label) {
	/* compute maximum pixel width of the list items */
	if (old->iclabel.label != NULL) freeString(old->iclabel.label);
	if (new->iclabel.label != NULL) {
	    new->iclabel.label = copyString(new->iclabel.label);
	    if (new->iclabel.displayobj != NULL) {
		new->iclabel.width = CDStringWidth(new->iclabel.displayobj,
						   new->iclabel.label, 0, -1);
	    }
	} else {
	    new->iclabel.width = 0;
	}
	label_changed = True;
	redisplay = True;
    }

    if (new->iclabel.hspace != old->iclabel.hspace ||
	new->iclabel.vspace != old->iclabel.vspace ||
	label_changed) {
	computeSize(new, widthspecified, heightspecified);
	redisplay = True;
    } else if (new->iclabel.justify != old->iclabel.justify) {
	redisplay = True;
    }

    if (new->iclabel.cursor != old->iclabel.cursor && XtIsRealized(wid)) {
	XDefineCursor(XtDisplay(wid), XtWindow(wid), new->iclabel.cursor);
    }

    return redisplay;
}

static XtGeometryResult
QueryGeometry(w, req, ret)
Widget w;
XtWidgetGeometry *req;
XtWidgetGeometry *ret;
{
    ICLabelWidget iclw = (ICLabelWidget)w;

    ret->request_mode = CWWidth | CWHeight;

    ret->width = iclw->iclabel.width + iclw->iclabel.hspace * 2;
    ret->height = iclw->iclabel.fontheight + iclw->iclabel.vspace * 2;

    if ((!(req->request_mode & CWWidth) || ret->width == req->width) &&
	(!(req->request_mode & CWHeight) || ret->height == req->height)) {
	return XtGeometryYes;
    } else if (ret->width == iclw->core.width &&
	       ret->height == iclw->core.height) {
	return XtGeometryNo;
    }
    return XtGeometryAlmost;
}

static void
InsertChild(w)
Widget w;
{
    ICLabelWidget iclw = (ICLabelWidget)XtParent(w);
    CompositeWidgetClass super = (CompositeWidgetClass)XtClass(iclw)->core_class.superclass;
    String params[1];
    Cardinal num_params;

    if (!XtIsSubclass(w, convDisplayObjectClass)) {
	params[0] = XtClass(iclw)->core_class.class_name;
	num_params = 1;
	XtAppErrorMsg(XtWidgetToApplicationContext(w),
		      "childError", "class", "WidgetError",
		      "%s: child must be subclass of ConvDisplayObject",
		      params, &num_params);
    }
    if (iclw->composite.num_children != 0) {
	params[0] = XtClass(iclw)->core_class.class_name;
	num_params = 1;
	XtAppErrorMsg(XtWidgetToApplicationContext(w),
		      "childError", "number", "WidgetError",
		      "%s: can only take one child",
		      params, &num_params);
    }

    (*super->composite_class.insert_child)(w);

    iclw->iclabel.displayobj = w;
    iclw->iclabel.fontheight = CDLineHeight(w, (Position *)NULL);

    if (iclw->iclabel.label != NULL) {
	iclw->iclabel.width = CDStringWidth(iclw->iclabel.displayobj,
					    iclw->iclabel.label, 0, -1);
    }
    computeSize(iclw, iclw->core.width != 0, iclw->core.height != 0);
}

static void
computeSize(iclw, fixwidth, fixheight)
ICLabelWidget iclw;
Boolean fixwidth;
Boolean fixheight;
{
    if (!fixwidth) {
	iclw->core.width = iclw->iclabel.width + iclw->iclabel.hspace * 2;
    }
    if (!fixheight) {
	iclw->core.height = iclw->iclabel.fontheight + iclw->iclabel.vspace * 2;
    }
}

static ICString *
copyString(from)
ICString *from;
{
    ICString *to = XtNew(ICString);

    to->nbytes = from->nbytes;
    to->nchars = from->nchars;
    to->attr = from->attr;
    to->data = XtMalloc(from->nbytes);
    (void)bcopy(from->data, to->data, from->nbytes);
    return to;
}

static void
freeString(s)
ICString *s;
{
    XtFree(s->data);
    XtFree((char *)s);
}


/*
 * public function
 */

void
ICLRecomputeSize(w)
Widget w;
{
    ICLabelWidget iclw = (ICLabelWidget)w;
    Dimension width, height;

    if (iclw->iclabel.displayobj != NULL) {
	iclw->iclabel.fontheight = CDLineHeight(iclw->iclabel.displayobj,
						(Position *)NULL);
	if (iclw->iclabel.label != NULL) {
	    iclw->iclabel.width = CDStringWidth(iclw->iclabel.displayobj,
						iclw->iclabel.label, 0, -1);
	}
	width = iclw->iclabel.width + iclw->iclabel.hspace * 2;
	height = iclw->iclabel.fontheight + iclw->iclabel.vspace * 2;
	XtMakeResizeRequest(w, width, height,
			    (Dimension *)NULL, (Dimension *)NULL);
    } /* else do nothing */
}