Mercurial > kinput2.yaz
comparison lib/ICLabel.c @ 0:92745d501b9a
initial import from kinput2-v3.1
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 08 Mar 2010 04:44:30 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:92745d501b9a |
---|---|
1 #ifndef lint | |
2 static char *rcsid = "$Id: ICLabel.c,v 1.11 1991/09/23 04:01:20 ishisone Rel $"; | |
3 #endif | |
4 /* | |
5 * Copyright (c) 1990 Software Research Associates, Inc. | |
6 * | |
7 * Permission to use, copy, modify, and distribute this software and its | |
8 * documentation for any purpose and without fee is hereby granted, provided | |
9 * that the above copyright notice appear in all copies and that both that | |
10 * copyright notice and this permission notice appear in supporting | |
11 * documentation, and that the name of Software Research Associates not be | |
12 * used in advertising or publicity pertaining to distribution of the | |
13 * software without specific, written prior permission. Software Research | |
14 * Associates makes no representations about the suitability of this software | |
15 * for any purpose. It is provided "as is" without express or implied | |
16 * warranty. | |
17 * | |
18 * Author: Makoto Ishisone, Software Research Associates, Inc., Japan | |
19 */ | |
20 | |
21 #include <X11/IntrinsicP.h> | |
22 #include <X11/StringDefs.h> | |
23 #include <X11/Xmu/Converters.h> | |
24 #if XtSpecificationRelease > 4 | |
25 #include <X11/Xfuncs.h> | |
26 #endif | |
27 #include "ICLabelP.h" | |
28 #include "ConvDisp.h" | |
29 | |
30 static XtResource resources[] = { | |
31 #define offset(field) XtOffset(ICLabelWidget, iclabel.field) | |
32 { XtNhorizontalSpacing, XtCSpacing, XtRDimension, sizeof(Dimension), | |
33 offset(hspace), XtRString, "1" }, | |
34 { XtNverticalSpacing, XtCSpacing, XtRDimension, sizeof(Dimension), | |
35 offset(vspace), XtRString, "1" }, | |
36 { XtNlabel, XtCLabel, XtRPointer, sizeof(ICString*), | |
37 offset(label), XtRImmediate, NULL }, | |
38 { XtNjustify, XtCJustify, XtRJustify, sizeof(XtJustify), | |
39 offset(justify), XtRImmediate, (XtPointer)XtJustifyCenter }, | |
40 { XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor), | |
41 offset(cursor), XtRImmediate, (XtPointer)None }, | |
42 #undef offset | |
43 }; | |
44 | |
45 static void ClassInitialize(); | |
46 static void Initialize(), Destroy(); | |
47 static void Realize(); | |
48 static void Redisplay(); | |
49 static Boolean SetValues(); | |
50 static XtGeometryResult QueryGeometry(); | |
51 static void InsertChild(); | |
52 | |
53 static void computeSize(); | |
54 static ICString *copyString(); | |
55 static void freeString(); | |
56 | |
57 static CompositeClassExtensionRec CompositeExtension = { | |
58 /* next_extension */ NULL, | |
59 /* record_type */ NULLQUARK, | |
60 /* version */ XtCompositeExtensionVersion, | |
61 /* record_size */ sizeof(CompositeClassExtensionRec), | |
62 /* accept_objects */ True, | |
63 }; | |
64 | |
65 ICLabelClassRec icLabelClassRec = { | |
66 { /* core fields */ | |
67 /* superclass */ (WidgetClass) &compositeClassRec, | |
68 /* class_name */ "ICLabel", | |
69 /* widget_size */ sizeof(ICLabelRec), | |
70 /* class_initialize */ ClassInitialize, | |
71 /* class_part_initialize */ NULL, | |
72 /* class_inited */ FALSE, | |
73 /* initialize */ Initialize, | |
74 /* initialize_hook */ NULL, | |
75 /* realize */ Realize, | |
76 /* actions */ NULL, | |
77 /* num_actions */ 0, | |
78 /* resources */ resources, | |
79 /* num_resources */ XtNumber(resources), | |
80 /* xrm_class */ NULLQUARK, | |
81 /* compress_motion */ TRUE, | |
82 /* compress_exposure */ TRUE, | |
83 /* compress_enterleave */ TRUE, | |
84 /* visible_interest */ FALSE, | |
85 /* destroy */ Destroy, | |
86 /* resize */ NULL, | |
87 /* expose */ Redisplay, | |
88 /* set_values */ SetValues, | |
89 /* set_values_hook */ NULL, | |
90 /* set_values_almost */ XtInheritSetValuesAlmost, | |
91 /* get_values_hook */ NULL, | |
92 /* accept_focus */ NULL, | |
93 /* version */ XtVersion, | |
94 /* callback_private */ NULL, | |
95 /* tm_table */ NULL, | |
96 /* query_geometry */ QueryGeometry, | |
97 /* display_accelerator */ XtInheritDisplayAccelerator, | |
98 /* extension */ NULL | |
99 }, | |
100 { /* composite fields */ | |
101 /* geometry_manager */ NULL, | |
102 /* change_managed */ NULL, | |
103 /* insert_child */ InsertChild, | |
104 /* delete_child */ XtInheritDeleteChild, | |
105 /* extension */ (XtPointer)&CompositeExtension, | |
106 }, | |
107 { /* iclabel fields */ | |
108 /* empty */ 0 | |
109 } | |
110 }; | |
111 | |
112 WidgetClass icLabelWidgetClass = (WidgetClass)&icLabelClassRec; | |
113 | |
114 static void | |
115 ClassInitialize() | |
116 { | |
117 XtAddConverter(XtRString, XtRJustify, XmuCvtStringToJustify, NULL, 0); | |
118 } | |
119 | |
120 /* ARGSUSED */ | |
121 static void | |
122 Initialize(req, new, args, num_args) | |
123 Widget req; | |
124 Widget new; | |
125 ArgList args; | |
126 Cardinal *num_args; | |
127 { | |
128 ICLabelWidget iclw = (ICLabelWidget)new; | |
129 | |
130 if (iclw->iclabel.label != NULL) { | |
131 iclw->iclabel.label = copyString(iclw->iclabel.label); | |
132 } | |
133 iclw->iclabel.width = 0; | |
134 } | |
135 | |
136 static void | |
137 Destroy(w) | |
138 Widget w; | |
139 { | |
140 ICLabelWidget iclw = (ICLabelWidget)w; | |
141 | |
142 if (iclw->iclabel.label != NULL) freeString(iclw->iclabel.label); | |
143 } | |
144 | |
145 static void | |
146 Realize(w, maskp, attr) | |
147 Widget w; | |
148 XtValueMask *maskp; | |
149 XSetWindowAttributes *attr; | |
150 { | |
151 ICLabelWidget iclw = (ICLabelWidget)w; | |
152 | |
153 if (iclw->iclabel.cursor != None) { | |
154 *maskp |= CWCursor; | |
155 attr->cursor = iclw->iclabel.cursor; | |
156 } | |
157 (*icLabelWidgetClass->core_class.superclass->core_class.realize)(w, maskp, attr); | |
158 } | |
159 | |
160 /* ARGSUSED */ | |
161 static void | |
162 Redisplay(w, ev, region) | |
163 Widget w; | |
164 XEvent *ev; | |
165 Region region; | |
166 { | |
167 ICLabelWidget iclw = (ICLabelWidget)w; | |
168 Widget dispobj = iclw->iclabel.displayobj; | |
169 int x, y; | |
170 | |
171 if (iclw->iclabel.label == NULL || iclw->iclabel.displayobj == NULL) return; | |
172 | |
173 switch (iclw->iclabel.justify) { | |
174 case XtJustifyLeft: | |
175 x = iclw->iclabel.hspace; | |
176 break; | |
177 case XtJustifyRight: | |
178 x = iclw->core.width - (iclw->iclabel.vspace + iclw->iclabel.width); | |
179 break; | |
180 case XtJustifyCenter: | |
181 default: | |
182 x = (iclw->core.width - iclw->iclabel.width) / 2; | |
183 break; | |
184 } | |
185 y = (iclw->core.height - iclw->iclabel.fontheight) / 2; | |
186 | |
187 CDDrawString(dispobj, w, iclw->iclabel.label, 0, -1, x, y); | |
188 } | |
189 | |
190 /* ARGSUSED */ | |
191 static Boolean | |
192 SetValues(cur, req, wid, args, num_args) | |
193 Widget cur; | |
194 Widget req; | |
195 Widget wid; | |
196 ArgList args; | |
197 Cardinal *num_args; | |
198 { | |
199 ICLabelWidget old = (ICLabelWidget)cur; | |
200 ICLabelWidget new = (ICLabelWidget)wid; | |
201 Boolean widthspecified = False; | |
202 Boolean heightspecified = False; | |
203 Boolean redisplay = False; | |
204 Boolean label_changed = False; | |
205 int i; | |
206 | |
207 for (i = 0; i < *num_args; i++) { | |
208 if (!strcmp(args->name, XtNwidth)) widthspecified = True; | |
209 else if (!strcmp(args->name, XtNheight)) heightspecified = True; | |
210 } | |
211 | |
212 if (new->iclabel.label != old->iclabel.label) { | |
213 /* compute maximum pixel width of the list items */ | |
214 if (old->iclabel.label != NULL) freeString(old->iclabel.label); | |
215 if (new->iclabel.label != NULL) { | |
216 new->iclabel.label = copyString(new->iclabel.label); | |
217 if (new->iclabel.displayobj != NULL) { | |
218 new->iclabel.width = CDStringWidth(new->iclabel.displayobj, | |
219 new->iclabel.label, 0, -1); | |
220 } | |
221 } else { | |
222 new->iclabel.width = 0; | |
223 } | |
224 label_changed = True; | |
225 redisplay = True; | |
226 } | |
227 | |
228 if (new->iclabel.hspace != old->iclabel.hspace || | |
229 new->iclabel.vspace != old->iclabel.vspace || | |
230 label_changed) { | |
231 computeSize(new, widthspecified, heightspecified); | |
232 redisplay = True; | |
233 } else if (new->iclabel.justify != old->iclabel.justify) { | |
234 redisplay = True; | |
235 } | |
236 | |
237 if (new->iclabel.cursor != old->iclabel.cursor && XtIsRealized(wid)) { | |
238 XDefineCursor(XtDisplay(wid), XtWindow(wid), new->iclabel.cursor); | |
239 } | |
240 | |
241 return redisplay; | |
242 } | |
243 | |
244 static XtGeometryResult | |
245 QueryGeometry(w, req, ret) | |
246 Widget w; | |
247 XtWidgetGeometry *req; | |
248 XtWidgetGeometry *ret; | |
249 { | |
250 ICLabelWidget iclw = (ICLabelWidget)w; | |
251 | |
252 ret->request_mode = CWWidth | CWHeight; | |
253 | |
254 ret->width = iclw->iclabel.width + iclw->iclabel.hspace * 2; | |
255 ret->height = iclw->iclabel.fontheight + iclw->iclabel.vspace * 2; | |
256 | |
257 if ((!(req->request_mode & CWWidth) || ret->width == req->width) && | |
258 (!(req->request_mode & CWHeight) || ret->height == req->height)) { | |
259 return XtGeometryYes; | |
260 } else if (ret->width == iclw->core.width && | |
261 ret->height == iclw->core.height) { | |
262 return XtGeometryNo; | |
263 } | |
264 return XtGeometryAlmost; | |
265 } | |
266 | |
267 static void | |
268 InsertChild(w) | |
269 Widget w; | |
270 { | |
271 ICLabelWidget iclw = (ICLabelWidget)XtParent(w); | |
272 CompositeWidgetClass super = (CompositeWidgetClass)XtClass(iclw)->core_class.superclass; | |
273 String params[1]; | |
274 Cardinal num_params; | |
275 | |
276 if (!XtIsSubclass(w, convDisplayObjectClass)) { | |
277 params[0] = XtClass(iclw)->core_class.class_name; | |
278 num_params = 1; | |
279 XtAppErrorMsg(XtWidgetToApplicationContext(w), | |
280 "childError", "class", "WidgetError", | |
281 "%s: child must be subclass of ConvDisplayObject", | |
282 params, &num_params); | |
283 } | |
284 if (iclw->composite.num_children != 0) { | |
285 params[0] = XtClass(iclw)->core_class.class_name; | |
286 num_params = 1; | |
287 XtAppErrorMsg(XtWidgetToApplicationContext(w), | |
288 "childError", "number", "WidgetError", | |
289 "%s: can only take one child", | |
290 params, &num_params); | |
291 } | |
292 | |
293 (*super->composite_class.insert_child)(w); | |
294 | |
295 iclw->iclabel.displayobj = w; | |
296 iclw->iclabel.fontheight = CDLineHeight(w, (Position *)NULL); | |
297 | |
298 if (iclw->iclabel.label != NULL) { | |
299 iclw->iclabel.width = CDStringWidth(iclw->iclabel.displayobj, | |
300 iclw->iclabel.label, 0, -1); | |
301 } | |
302 computeSize(iclw, iclw->core.width != 0, iclw->core.height != 0); | |
303 } | |
304 | |
305 static void | |
306 computeSize(iclw, fixwidth, fixheight) | |
307 ICLabelWidget iclw; | |
308 Boolean fixwidth; | |
309 Boolean fixheight; | |
310 { | |
311 if (!fixwidth) { | |
312 iclw->core.width = iclw->iclabel.width + iclw->iclabel.hspace * 2; | |
313 } | |
314 if (!fixheight) { | |
315 iclw->core.height = iclw->iclabel.fontheight + iclw->iclabel.vspace * 2; | |
316 } | |
317 } | |
318 | |
319 static ICString * | |
320 copyString(from) | |
321 ICString *from; | |
322 { | |
323 ICString *to = XtNew(ICString); | |
324 | |
325 to->nbytes = from->nbytes; | |
326 to->nchars = from->nchars; | |
327 to->attr = from->attr; | |
328 to->data = XtMalloc(from->nbytes); | |
329 (void)bcopy(from->data, to->data, from->nbytes); | |
330 return to; | |
331 } | |
332 | |
333 static void | |
334 freeString(s) | |
335 ICString *s; | |
336 { | |
337 XtFree(s->data); | |
338 XtFree((char *)s); | |
339 } | |
340 | |
341 | |
342 /* | |
343 * public function | |
344 */ | |
345 | |
346 void | |
347 ICLRecomputeSize(w) | |
348 Widget w; | |
349 { | |
350 ICLabelWidget iclw = (ICLabelWidget)w; | |
351 Dimension width, height; | |
352 | |
353 if (iclw->iclabel.displayobj != NULL) { | |
354 iclw->iclabel.fontheight = CDLineHeight(iclw->iclabel.displayobj, | |
355 (Position *)NULL); | |
356 if (iclw->iclabel.label != NULL) { | |
357 iclw->iclabel.width = CDStringWidth(iclw->iclabel.displayobj, | |
358 iclw->iclabel.label, 0, -1); | |
359 } | |
360 width = iclw->iclabel.width + iclw->iclabel.hspace * 2; | |
361 height = iclw->iclabel.fontheight + iclw->iclabel.vspace * 2; | |
362 XtMakeResizeRequest(w, width, height, | |
363 (Dimension *)NULL, (Dimension *)NULL); | |
364 } /* else do nothing */ | |
365 } |