Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / programs / dtudcfonted / code.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* code.c 1.37 - Fujitsu source for CDEnext    96/10/30 13:13:47      */
24 /* $XConsortium: code.c /main/6 1996/11/08 01:57:21 cde-fuj $ */
25 /*
26  *  (c) Copyright 1995 FUJITSU LIMITED
27  *  This is source code modified by FUJITSU LIMITED under the Joint
28  *  Development Agreement for the CDEnext PST.
29  *  This is unpublished proprietary source code of FUJITSU LIMITED
30  */
31 #include        <stdio.h>
32 #include        <string.h>
33 #include        <X11/keysym.h>
34 #include        <Xm/Xm.h>
35 #include        <Xm/Label.h>
36 #include        <Xm/List.h>
37 #include        <Xm/RowColumn.h>
38 #include        <Xm/SeparatoG.h>
39 #include        <Xm/Text.h>
40 #include        <Xm/PushB.h>
41 #include        <Xm/Frame.h>
42 #include        <Xm/Form.h>
43 #include        <Xm/MwmUtil.h>
44 #include        <X11/Intrinsic.h>
45 #include        "falfont.h"
46 #include        "xoakufont.h"
47
48 #define NUM 0x80
49
50 static Display *display=NULL;
51 static int     screen;
52 static Boolean open_font;
53 static Widget toggle[NUM];
54 static Widget text;
55 static Window BaseW;
56 static Widget set_widget;
57 static Widget code_w=0;
58 static int font_w, font_h;
59 static unsigned long fg, bg;
60 static unsigned char *ptn, *clr;
61 static Pixmap no_pix;
62 static int len, code1;
63 static char *FontName=NULL;
64 static FalFontID        fid;
65
66 extern FalFontID font_id;
67 extern int CodePoint;
68 extern Resource resource;
69
70 static void
71 OpenFont(void)
72 {
73     int                 mask;
74     FalFontData     font_data;
75     static FalFontDataList      *copylist;
76     extern FalFontData  fullFontData;
77
78     if(strcmp(FontName, fullFontData.xlfdname) == 0) {
79         fid = font_id;
80         return;
81     }
82     mask =  FAL_FONT_MASK_XLFDNAME | FAL_FONT_MASK_DEFINED |
83                                         FAL_FONT_MASK_UNDEFINED;
84     font_data.xlfdname = FontName;
85     if (! CodePoint) {
86         mask |= FAL_FONT_MASK_GLYPH_INDEX;
87     }
88
89     fid = FalOpenSysFont(&font_data, mask, &copylist);
90 }
91
92 static void
93 CloseFont(void)
94 {
95     if (fid != font_id) {
96         FalCloseFont(fid);
97         fid = 0;
98     }
99 }
100
101 static void
102 DrawCode(Widget w, int low_code)
103 {
104     int hi_code;
105     char *f = NULL;
106     XImage image;
107     GC gc = XDefaultGC(display, 0);
108
109     hi_code = (code1 & 0xff) << 8;
110     image.width = font_w;
111     image.height = font_h;
112     image.xoffset = 0;
113     image.format = XYBitmap;
114     image.byte_order = LSBFirst;
115     image.bitmap_unit = 8;
116     image.bitmap_bit_order = MSBFirst;
117     image.bitmap_pad = 8;
118     image.depth = 1;
119     image.bytes_per_line = (font_w - 1) / 8 + 1;
120     XSetForeground(display, gc, fg);
121     XSetBackground(display, gc, bg);
122
123     if (open_font) {
124         f = (char *)FalReadFont(fid, hi_code | low_code, font_w, font_h);
125         if(f == NULL || f == (char *)FAL_ERROR) {
126             return;
127         }
128         if( EXISTS_FLAG == 0 ) {
129             memcpy(ptn, f, len);
130             image.data = (char *)ptn;
131         } else {
132             image.data = (char *)clr;
133         }
134         XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
135     } else {
136         if (ptnGet(hi_code | low_code, ptn) == 0) {
137             image.data = (char *)ptn;
138         } else {
139             image.data = (char *)clr;
140         }
141         XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
142     }
143 }
144
145 static void
146 SetPixmap(start_code)
147 {
148     int hi_code, low_code;
149     char *f = NULL;
150     XImage image;
151     GC gc = XDefaultGC(display, 0);
152
153     hi_code = (start_code & 0xff) << 8;
154     image.width = font_w;
155     image.height = font_h;
156     image.xoffset = 0;
157     image.format = XYBitmap;
158     image.byte_order = LSBFirst;
159     image.bitmap_unit = 8;
160     image.bitmap_bit_order = MSBFirst;
161     image.bitmap_pad = 8;
162     image.depth = 1;
163     image.bytes_per_line = (font_w - 1) / 8 + 1;
164     XSetForeground(display, gc, fg);
165     XSetBackground(display, gc, bg);
166
167     if (open_font) {
168         for (low_code=0; low_code < NUM; low_code++) {
169             f = (char *)FalReadFont(fid, hi_code | low_code, font_w, font_h);
170             if(f == NULL || f == (char *)FAL_ERROR) {
171                 return;
172             }
173             if( EXISTS_FLAG == 0 ) {
174                 memcpy(ptn, f, len);
175                 image.data = (char *)ptn;
176             } else {
177                 image.data = (char *)clr;
178             }
179             XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
180         }
181     } else {
182         for (low_code=0; low_code < NUM; low_code++) {
183             if (ptnGet(hi_code | low_code, ptn) == 0) {
184                 image.data = (char *)ptn;
185             } else {
186                 image.data = (char *)clr;
187             }
188             XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
189         }
190     }
191 }
192
193 static void
194 Code1Call(Widget w, int start_code, XmListCallbackStruct *data)
195 {
196     code1 = start_code + data->item_position -1;
197     SetPixmap(code1);
198 }
199
200 static void
201 Code2Call(Widget w, int code2, XmPushButtonCallbackStruct *data)
202 {
203     char asc[16];
204     sprintf(asc, "%2.2X%2.2X", code1, code2);
205     XtVaSetValues(text, XmNvalue, asc, NULL);
206     DrawCode(w, code2);
207 }
208
209 static void
210 PrevPage(Widget w, Widget scroll)
211 {
212     int *list;
213     int num;
214     if (XmListGetSelectedPos(scroll, &list, &num)) {
215         if(list[0] > 1) {
216             XmListSelectPos(scroll, list[0] - 1, True);
217             XmListSetPos(scroll, list[0] - 1);
218         }
219     }
220 }
221
222 static void
223 NextPage(Widget w, Widget scroll)
224 {
225     int *list;
226     int num, item;
227     XtVaGetValues(scroll, XmNitemCount, &item, NULL);
228     if (XmListGetSelectedPos(scroll, &list, &num)) {
229         if(list[0] < item) {
230             XmListSelectPos(scroll, list[0] + 1, True);
231             XmListSetBottomPos(scroll, list[0] + 1);
232         }
233     }
234 }
235
236 static void
237 Cancel(void)
238 {
239     XtUnmapWidget(XtParent(code_w));
240 }
241
242 static void
243 Apply(void)
244 {
245     char *asc;
246     XtVaGetValues(text, XmNvalue, &asc, NULL);
247     XtVaSetValues(set_widget, XmNvalue, asc, NULL);
248     XtFree(asc);
249     Cancel();
250 }
251
252 static void
253 CreateItem(XmString *item, int *item_count, int *start_code)
254 {
255     int count;
256     int start, end;
257     char str[5];
258     FalFontinfo         finfo;
259     extern int begin_code, last_code;
260
261     if (open_font) {
262         if (FalQueryFont(fid, &finfo) == FAL_ERROR) {
263             *item_count = 0;
264             CloseFont();
265             return;
266         }
267         font_w = finfo.width;
268         font_h = finfo.height;
269         start = (finfo.top & 0xff00) >> 8;
270         end = (finfo.bottom & 0xff00) >> 8;
271     } else {
272         font_w = edg.width;
273         font_h = edg.height;
274         start = (begin_code & 0xff00) >> 8;
275         end = (last_code & 0xff00) >> 8;
276     }
277     if (CodePoint) {
278         if (start < 0x80)
279             start = 80;
280         if (end < 0x80)
281             start = 80;
282     } else {
283         if (start > 0x7f)
284             start = 0x7f;
285         if (end > 0x7f)
286             end = 0x7f;
287     }
288     *start_code = start;
289     for (count=0;start <= end; start++, count++) {
290         sprintf(str, "%X", start);
291         item[count] = XmStringCreateLocalized(str);
292     }
293     *item_count = count;
294 }
295
296 static Widget
297 CreateCodeWindow(Widget w)
298 {
299     Widget top, base1, base2, base3, base4;
300     Widget frame, scroll, label, sep, form;
301     Widget push1, push2, push3, push4;
302     Arg args[16];
303     int n, i;
304     XmString item[128];
305     XmString xcs;
306     int item_count, start_code, add_c;
307     char add[9];
308
309     n = 0;
310     XtSetArg(args[n], XmNmwmFunctions, MWM_FUNC_ALL | MWM_FUNC_CLOSE |
311                                         MWM_FUNC_RESIZE); n++;
312     XtSetArg(args[n], XmNmwmDecorations, MWM_DECOR_ALL); n++;
313     top = XmCreateFormDialog(w, "code", args, n);
314
315     base1 = XtVaCreateManagedWidget("base1", xmRowColumnWidgetClass, top,
316                                         XmNorientation, XmVERTICAL, NULL);
317
318     frame = XtVaCreateManagedWidget("frame", xmFrameWidgetClass, base1,
319                                         NULL);
320
321     base2 = XtVaCreateManagedWidget("base2", xmRowColumnWidgetClass, frame,
322                                         XmNorientation, XmHORIZONTAL, NULL);
323
324     CreateItem(item, &item_count, &start_code);
325     code1 = start_code;
326     n = 0 ;
327     XtSetArg(args[n], XmNitems, item); n++;
328     XtSetArg(args[n], XmNitemCount, item_count); n++;
329     XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmSTATIC); n++;
330     scroll = XmCreateScrolledList(base2, "scroll", args, n);
331     XtManageChild(scroll);
332     XtAddCallback(scroll, XmNbrowseSelectionCallback,
333                         (XtCallbackProc)Code1Call, (XtPointer) (intptr_t) start_code);
334     XtAddCallback(scroll, XmNdefaultActionCallback,
335                         (XtCallbackProc)Code1Call, (XtPointer) (intptr_t) start_code);
336     XmListSelectPos(scroll, 1, False);
337
338     base3 = XtVaCreateManagedWidget("base3", xmRowColumnWidgetClass, base2,
339                                         XmNorientation, XmHORIZONTAL,
340                                         XmNpacking, XmPACK_COLUMN,
341                                         XmNradioAlwaysOne, True,
342                                         XmNradioBehavior , True,
343                                         XmNmarginHeight, 0,
344                                         XmNmarginWidth, 0,
345                                         XmNspacing, 0,
346                                         XmNnumColumns, 9, NULL);
347
348
349     len = (font_w / 8 + 1) * font_h;
350     ptn = (unsigned char *) malloc(len);
351     clr = (unsigned char *) malloc(len);
352     memset(clr, 0, len);
353     XtVaGetValues(w, XmNforeground, &fg, XmNbackground, &bg, NULL);
354     no_pix = XCreatePixmapFromBitmapData(display, BaseW, (char *)clr,
355                 font_w, font_h, fg, bg, DefaultDepth(display, screen));
356
357     XtVaCreateManagedWidget("", xmLabelWidgetClass, base3, NULL);
358     for (i=0; i < 16; i++) {
359         sprintf(add, "%2.1X", i);
360         XtVaCreateManagedWidget(add, xmLabelWidgetClass, base3, NULL);
361     }
362     if (CodePoint)
363         add_c = 8;
364     else
365         add_c = 0;
366     for (i=0; i < NUM; i++) {
367         if ((i % 16) == 0) {
368             snprintf(add, sizeof(add), "%2.1X", add_c++);
369             XtVaCreateManagedWidget(add, xmLabelWidgetClass, base3, NULL);
370         }
371         toggle[i] = XtVaCreateWidget("toggle",
372                                         xmPushButtonWidgetClass, base3,
373                                         XmNwidth, font_w,
374                                         XmNheight, font_h,
375                                         XmNlabelType, XmPIXMAP,
376                                         XmNlabelPixmap, no_pix, NULL);
377         XtAddCallback(toggle[i], XmNactivateCallback,
378                                 (XtCallbackProc)Code2Call, (XtPointer) (intptr_t) i);
379         XtAddCallback(toggle[i], XmNarmCallback,
380                                 (XtCallbackProc)DrawCode, (XtPointer) (intptr_t) i);
381         XtAddCallback(toggle[i], XmNdisarmCallback,
382                                 (XtCallbackProc)DrawCode, (XtPointer) (intptr_t) i);
383         XtAddEventHandler(toggle[i], ExposureMask, False,
384                                 (XtEventHandler)DrawCode, (XtPointer) (intptr_t) i);
385     }
386     XtManageChildren(toggle, NUM);
387
388     base4 = XtVaCreateManagedWidget("base4", xmFormWidgetClass, base1, NULL);
389
390     xcs = XmStringCreateLocalized(resource.l_code);
391     label = XtVaCreateManagedWidget("code", xmLabelWidgetClass, base4,
392                                         XmNlabelString, xcs,
393                                         XmNleftAttachment, XmATTACH_POSITION,
394                                         XmNleftPosition, 30, NULL);
395
396     text = XtVaCreateManagedWidget("text", xmTextWidgetClass, base4,
397                                         XmNeditable, False,
398                                         XmNcursorPositionVisible, False,
399                                         XmNcolumns, 4,
400                                         XmNleftAttachment, XmATTACH_WIDGET,
401                                         XmNleftWidget, label, NULL);
402
403     sep = XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, base1,
404                                         XmNorientation, XmHORIZONTAL, NULL);
405
406     form = XtVaCreateManagedWidget("sep", xmFormWidgetClass, base1, NULL);
407
408     xcs = XmStringCreateLocalized(resource.previous);
409     push1 = XtVaCreateManagedWidget("PreviousPage", xmPushButtonWidgetClass, form,
410                                         XmNleftAttachment, XmATTACH_POSITION,
411                                         XmNlabelString, xcs,
412                                         XmNleftPosition, 10, NULL);
413     XtAddCallback(push1, XmNactivateCallback,
414                                 (XtCallbackProc)PrevPage, (XtPointer)scroll);
415     XmStringFree(xcs);
416
417     xcs = XmStringCreateLocalized(resource.next);
418     push2 = XtVaCreateManagedWidget("NextPage", xmPushButtonWidgetClass, form,
419                                         XmNleftAttachment, XmATTACH_POSITION,
420                                         XmNlabelString, xcs,
421                                         XmNleftPosition, 35, NULL);
422     XtAddCallback(push2, XmNactivateCallback,
423                                 (XtCallbackProc)NextPage, (XtPointer)scroll);
424     XmStringFree(xcs);
425
426     xcs = XmStringCreateLocalized(resource.apply);
427     push3 = XtVaCreateManagedWidget("Apply", xmPushButtonWidgetClass, form,
428                                         XmNleftAttachment, XmATTACH_POSITION,
429                                         XmNlabelString, xcs,
430                                         XmNleftPosition, 60, NULL);
431     XtAddCallback(push3, XmNactivateCallback, (XtCallbackProc)Apply, NULL);
432     XmStringFree(xcs);
433
434     xcs = XmStringCreateLocalized(resource.l_cancel);
435     push4 = XtVaCreateManagedWidget("Cancel", xmPushButtonWidgetClass, form,
436                                         XmNleftAttachment, XmATTACH_POSITION,
437                                         XmNlabelString, xcs,
438                                         XmNleftPosition, 80, NULL);
439     XtAddCallback(push4, XmNactivateCallback, (XtCallbackProc)Cancel, NULL);
440     XmStringFree(xcs);
441
442     return(top);
443 }
444
445 void
446 CodeWindow(Widget widget, char *font_name, Boolean load_font)
447 {
448     static Boolean      old_load_font;
449     extern int  CodePoint;
450
451     set_widget = widget;
452
453     BaseW = XtWindow(widget);
454     display = XtDisplay(widget);
455     screen  = DefaultScreen(display);
456     if (FontName == NULL || strcmp(font_name, FontName) ||
457                                 load_font != old_load_font) {
458         old_load_font = load_font;
459         if (code_w) {
460             XtDestroyWidget(code_w);
461             code_w = 0;
462             free(ptn);
463             free(clr);
464             if (fid != 0) {
465                 CloseFont();
466             }
467         }
468         if (FontName)
469             free(FontName);
470         FontName = (char *)malloc(strlen(font_name)+1);
471         strcpy(FontName, font_name);
472
473         if (load_font) {
474             OpenFont();
475             if (fid == 0)
476                 return;
477             open_font = True;
478         } else {
479             open_font = False;
480         }
481
482         code_w = CreateCodeWindow(widget);
483         XtManageChild(code_w);
484     } else {
485         XtMapWidget(XtParent(code_w));
486     }
487 }