Debian Codegraph @

demos/unsupported/dainput/dainput_dlg.c

raw symbols SCIP
1 /* $XConsortium: dainput_dlg.c /main/5 1995/07/15 20:47:28 drk $ */
2 /*
3 * Motif
4 *
5 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
6 *
7 * These libraries and programs are free software; you can
8 * redistribute them and/or modify them under the terms of the GNU
9 * Lesser General Public License as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * These libraries and programs are distributed in the hope that
14 * they will be useful, but WITHOUT ANY WARRANTY; without even the
15 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU Lesser General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with these librararies and programs; if not, write
21 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
22 * Floor, Boston, MA 02110-1301 USA
23 *
24 */
25 /*
26 * HISTORY
27 */
28 #include <Xm/XmP.h> /* for XmeGetDefaultRenderTable */
29 #include <Xm/XmIm.h>
30 #include <stdlib.h>
31 #include "dainput.h"
32
33 typedef struct _DaTextDataRec {
34 char * text; /* Contains the (multibyte) text */
35 long text_length; /* Current number of bytes displayed */
36 long alloced_length; /* Allocated nuber of bytes */
37 XFontStruct * font; /* Font for displaying text */
38 XFontSet font_set; /* Font set for displaying text - takes
39 precedence over font */
40 GC gc; /* GC for drawing text */
41 Dimension baseline; /* Baseline for displayed text */
42 Dimension lineheight; /* line height for font or font set */
43 Dimension cursor_pos; /* X position for insertion cursor */
44 } DaTextDataRec, *DaTextData;
45
46 /****************************************************************
47 * GetGC:
48 * Get a gc for drawing text and lines in the drawing area.
49 ****************************************************************/
50 static void
51 GetGC(Widget widget,
52 DaTextData text_data)
53 {
54 Pixel foreground;
55 XtGCMask value_mask = GCForeground|GCLineWidth;
56 XGCValues values;
57
58 XtVaGetValues(widget,
59 XmNforeground, &foreground,
60 NULL);
61 values.foreground = foreground;
62 values.line_width = 1;
63
64 if (text_data->font) {
65 values.font = text_data->font->fid;
66 value_mask |= GCFont;
67 }
68
69 text_data->gc = XtGetGC(widget, value_mask, &values);
70 }
71
72
73 /****************************************************************
74 * GetFont:
75 * Get a font for drawing text. Gets the default text render
76 * table, and extract the font or fontset from the rendition
77 * tagged XmFONTLIST_DEFAULT_TAG. Use the first font set or font
78 * found if there is no such rendition. Also fills in baseline
79 * and lineheight values.
80 ****************************************************************/
81 static void
82 GetFont(Widget widget,
83 DaTextData text_data)
84 {
85 XmRenderTable rt;
86 XmFontContext context;
87 XmFontListEntry next_entry;
88 XmFontType type_return = XmFONT_IS_FONT;
89 XtPointer tmp_font;
90 Boolean have_font_struct = False;
91 Boolean have_font_set = False;
92 char* font_tag = NULL;
93
94 rt = XmeGetDefaultRenderTable(widget, XmTEXT_FONTLIST);
95
96 if (!XmFontListInitFontContext(&context, rt))
97 return;
98
99 do {
100 next_entry = XmFontListNextEntry(context);
101 if (next_entry) {
102 tmp_font = XmFontListEntryGetFont(next_entry, &type_return);
103 font_tag = XmFontListEntryGetTag(next_entry);
104 if (strcmp(XmFONTLIST_DEFAULT_TAG, font_tag) == 0) {
105 /* If there is a real font in this font entry, we're done */
106 if (type_return == XmFONT_IS_FONTSET) {
107 text_data->font_set = (XFontSet)tmp_font;
108 text_data->font = NULL;
109 break;
110 } else if (type_return == XmFONT_IS_FONT) {
111 text_data->font = (XFontStruct *)tmp_font;
112 text_data->font_set = NULL;
113 break;
114 }
115 } else {
116 /* save the first one in case no font is found
117 * in default entry. Font sets prevail over fonts */
118 if (type_return == XmFONT_IS_FONTSET) {
119 if (!have_font_set) {
120 text_data->font_set = (XFontSet)tmp_font;
121 have_font_set = True;
122 }
123 } else if (type_return == XmFONT_IS_FONT) {
124 if (!have_font_struct && !have_font_set) {
125 text_data->font = (XFontStruct*)tmp_font;
126 have_font_struct = True;
127 }
128 }
129 }
130 if (font_tag) XtFree(font_tag);
131 }
132 } while (next_entry != NULL);
133
134 XmFontListFreeFontContext(context);
135
136 if (text_data->font_set) {
137 XFontSetExtents *fs_extents;
138 fs_extents = XExtentsOfFontSet(text_data->font_set);
139 text_data->baseline = -fs_extents->max_logical_extent.y;
140 text_data->lineheight = fs_extents->max_logical_extent.height;
141 } else if (text_data->font) {
142 text_data->baseline = text_data->font->max_bounds.ascent;
143 text_data->lineheight = (text_data->font->max_bounds.ascent +
144 text_data->font->max_bounds.descent);
145 }
146 }
147
148 /****************************************************************
149 * DaExit:
150 * Exit application.
151 ****************************************************************/
152 /*CCB*/
153 void
154 DaExit(Widget widget,
155 XtPointer client_data,
156 XtPointer calldata)
157 {
158 exit(0);
159 }
160
161 /****************************************************************
162 * DaOverView:
163 * Show help information.
164 ****************************************************************/
165 /*CCB*/
166 void
167 DaOverView(Widget widget,
168 XtPointer client_data,
169 XtPointer calldata)
170 {
171 /* Not yet implemented */
172 }
173
174 /****************************************************************
175 * DaRedrawText:
176 * Draw a line one pixel below the text baseline.
177 * Draw the text.
178 * Draw a cursor marking where the next character typed will be
179 * drawn.
180 ****************************************************************/
181 /*CCB*/
182 void
183 DaRedrawText(Widget widget,
184 XtPointer client_data,
185 XtPointer calldata)
186 {
187 DaTextData text_data = NULL;
188 Dimension margin_width, margin_height, width;
189 XPoint points[3];
190
191 XtVaGetValues(widget,
192 XmNuserData, &text_data,
193 XmNmarginWidth, &margin_width,
194 XmNmarginHeight, &margin_height,
195 XmNwidth, &width,
196 NULL);
197
198 if (text_data == NULL) {
199 /* First time through - allocate the extra data we need */
200 text_data = (DaTextData)XtCalloc(1, sizeof(DaTextDataRec));
201 text_data->alloced_length = 20;
202 text_data->text = XtMalloc(text_data->alloced_length);
203 GetFont(widget, text_data);
204 GetGC(widget, text_data);
205 text_data->cursor_pos = margin_width;
206 XtVaSetValues(widget, XmNuserData, text_data, NULL);
207 }
208
209 /* Draw baseline */
210 XDrawLine(XtDisplay(widget), XtWindow(widget),
211 text_data->gc,
212 margin_width, text_data->baseline + margin_height + 1,
213 width - margin_width, text_data->baseline + margin_height + 1);
214
215 /* If there is text, draw it */
216 if (text_data->text_length > 0) {
217 if (text_data->font_set) {
218 XmbDrawString(XtDisplay(widget), XtWindow(widget),
219 text_data->font_set, text_data->gc,
220 margin_width, text_data->baseline + margin_height,
221 text_data->text, text_data->text_length);
222 } else if (text_data->font) {
223 XDrawString(XtDisplay(widget), XtWindow(widget),
224 text_data->gc,
225 margin_width, text_data->baseline + margin_height,
226 text_data->text, text_data->text_length);
227 }
228 }
229
230 /* Draw the cursor */
231 points[0].x = text_data->cursor_pos - 3;
232 points[0].y = text_data->baseline + margin_height + 3;
233 points[1].x = 3;
234 points[1].y = -3;
235 points[2].x = 4;
236 points[2].y = 4;
237 XDrawLines(XtDisplay(widget), XtWindow(widget),
238 text_data->gc, points, 3, CoordModePrevious);
239 (points[0].y)--;
240 XDrawLines(XtDisplay(widget), XtWindow(widget),
241 text_data->gc, points, 3, CoordModePrevious);
242 }
243
244
245 /****************************************************************
246 * DaInputText:
247 * If we received a keypress event, check whether any character(s)
248 * was typed. If so, add it to the text, forward the cursor
249 * position and redraw.
250 ****************************************************************/
251 /*CCB*/
252 void
253 DaInputText(Widget widget,
254 XtPointer client_data,
255 XtPointer calldata)
256 {
257 DaTextData text_data = NULL;
258 XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *)calldata;
259 XPoint spot;
260 Dimension margin_width, margin_height;
261
262 if (cbs->event && cbs->event->type == KeyPress) {
263 char insert_string[100];
264 int insert_length;
265 Status status_return;
266
267 XtVaGetValues(widget,
268 XmNuserData, &text_data,
269 XmNmarginWidth, &margin_width,
270 XmNmarginHeight, &margin_height,
271 NULL);
272
273 /* Get the characters typed, if any */
274 insert_length = XmImMbLookupString(widget, (XKeyEvent *) cbs->event,
275 insert_string, 100,
276 NULL, &status_return);
277
278 /* if we got any text, append it to the rest */
279 if (insert_length > 0 &&
280 (status_return == XLookupChars || status_return == XLookupBoth)) {
281 if (text_data->text_length + insert_length >
282 text_data->alloced_length) {
283 text_data->alloced_length += insert_length + 20;
284 text_data->text = XtRealloc(text_data->text,
285 text_data->alloced_length);
286 }
287 strncpy(&(text_data->text[text_data->text_length]),
288 insert_string, insert_length);
289
290 text_data->text_length += insert_length;
291
292 /* compute new cursor position */
293 if (text_data->text_length > 0) {
294 if (text_data->font_set) {
295 text_data->cursor_pos =
296 margin_width + XmbTextEscapement(text_data->font_set,
297 text_data->text,
298 text_data->text_length);
299 } else if (text_data->font) {
300 text_data->cursor_pos =
301 margin_width + XTextWidth(text_data->font,
302 text_data->text,
303 text_data->text_length);
304 }
305 }
306
307 /* Tell input method about new cursor position */
308 spot.x = text_data->cursor_pos;
309 spot.y = text_data->baseline + margin_height;
310
311 XmImVaSetValues(widget,
312 XmNspotLocation, &spot,
313 NULL);
314 }
315 /* redraw the text. This isn't very efficient, but it does the job... */
316 XClearWindow(XtDisplay(widget), XtWindow(widget));
317 DaRedrawText(widget, NULL, NULL);
318 }
319 }
320
321 /****************************************************************
322 * DaResizedText:
323 * Widget was resized. Redisplay the window.
324 ****************************************************************/
325 /*CCB*/
326 void
327 DaResizedText(Widget widget,
328 XtPointer client_data,
329 XtPointer calldata)
330 {
331 /* redraw the text. This isn't very efficient or smart,
332 but it does the job... */
333 XClearWindow(XtDisplay(widget), XtWindow(widget));
334 DaRedrawText(widget, NULL, NULL);
335 }
336
337 /****************************************************************
338 * DaFocusHandler:
339 * Keyboard focus movement event handler. Inform the input
340 * method when we lose or gain keyboard focus.
341 ****************************************************************/
342 void
343 DaFocusHandler(Widget widget,
344 XtPointer client_data,
345 XEvent *event,
346 Boolean *cont)
347 {
348 DaTextData text_data = NULL;
349 XPoint spot;
350 XmRenderTable rt;
351 Dimension margin_height;
352
353 if (!event)
354 return;
355
356 XtVaGetValues(widget,
357 XmNuserData, &text_data,
358 XmNmarginHeight, &margin_height,
359 NULL);
360
361 switch (event->type)
362 {
363 case FocusIn:
364 case EnterNotify:
365 /* Tell input method about our cursor position, line spacing
366 * and font.
367 */
368 if (text_data) {
369 spot.x = text_data->cursor_pos;
370 spot.y = text_data->baseline + margin_height;
371 } else {
372 spot.x = spot.y = 10;
373 }
374 rt = XmeGetDefaultRenderTable(widget, XmTEXT_FONTLIST);
375
376 XmImVaSetFocusValues(widget,
377 XmNspotLocation, &spot,
378 XmNrenderTable, rt, /* since DrawingArea does
379 not have a resource */
380 XmNlineSpace, (text_data ?
381 text_data->lineheight :
382 10),
383 NULL);
384 break;
385 case FocusOut:
386 case LeaveNotify:
387 XmImUnsetFocus(widget);
388 break;
389 }
390 }
391