Add -fpermissive to allow compilation of old C++ on modern GCC
[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 librararies 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()
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()
94 {
95     if (fid != font_id) {
96         FalCloseFont(fid);
97         fid = 0;
98     }
99 }
100
101 static void
102 DrawCode(w, low_code)
103 Widget w;
104 int low_code;
105 {
106     int hi_code;
107     char *f;
108     XImage image;
109     GC gc = XDefaultGC(display, 0);
110
111     hi_code = (code1 & 0xff) << 8;
112     image.width = font_w;
113     image.height = font_h;
114     image.xoffset = 0;
115     image.format = XYBitmap;
116     image.byte_order = LSBFirst;
117     image.bitmap_unit = 8;
118     image.bitmap_bit_order = MSBFirst;
119     image.bitmap_pad = 8;
120     image.depth = 1;
121     image.bytes_per_line = (font_w - 1) / 8 + 1;
122     XSetForeground(display, gc, fg);
123     XSetBackground(display, gc, bg);
124
125     if (open_font) {
126         f = (char *)FalReadFont(fid, hi_code | low_code, font_w, font_h);
127         if(f == (char *)FAL_ERROR)
128             return;
129         if( EXISTS_FLAG == 0 ) {
130             memcpy(ptn, f, len);
131             image.data = (char *)ptn;
132         } else {
133             image.data = (char *)clr;
134         }
135         XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
136     } else {
137         if (ptnGet(hi_code | low_code, ptn) == 0) {
138             image.data = (char *)ptn;
139         } else {
140             image.data = (char *)clr;
141         }
142         XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
143     }
144 }
145
146 static void
147 SetPixmap(start_code)
148 {
149     int hi_code, low_code;
150     char *f;
151     XImage image;
152     GC gc = XDefaultGC(display, 0);
153
154     hi_code = (start_code & 0xff) << 8;
155     image.width = font_w;
156     image.height = font_h;
157     image.xoffset = 0;
158     image.format = XYBitmap;
159     image.byte_order = LSBFirst;
160     image.bitmap_unit = 8;
161     image.bitmap_bit_order = MSBFirst;
162     image.bitmap_pad = 8;
163     image.depth = 1;
164     image.bytes_per_line = (font_w - 1) / 8 + 1;
165     XSetForeground(display, gc, fg);
166     XSetBackground(display, gc, bg);
167
168     if (open_font) {
169         for (low_code=0; low_code < NUM; low_code++) {
170             f = (char *)FalReadFont(fid, hi_code | low_code, font_w, font_h);
171             if(f == (char *)FAL_ERROR)
172                 return;
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(w, start_code, data)
195 Widget w;
196 int start_code;
197 XmListCallbackStruct *data;
198 {
199     code1 = start_code + data->item_position -1;
200     SetPixmap(code1);
201 }
202
203 static void
204 Code2Call(w, code2, data)
205 Widget w;
206 int code2;
207 XmPushButtonCallbackStruct *data;
208 {
209     char asc[16];
210     sprintf(asc, "%2.2X%2.2X", code1, code2);
211     XtVaSetValues(text, XmNvalue, asc, NULL);
212     DrawCode(w, code2);
213 }
214
215 static void
216 PrevPage(w, scroll)
217 Widget w, scroll;
218 {
219     int *list;
220     int num;
221     if (XmListGetSelectedPos(scroll, &list, &num)) {
222         if(list[0] > 1) {
223             XmListSelectPos(scroll, list[0] - 1, True);
224             XmListSetPos(scroll, list[0] - 1);
225         }
226     }
227 }
228
229 static void
230 NextPage(w, scroll)
231 Widget w, scroll;
232 {
233     int *list;
234     int num, item;
235     XtVaGetValues(scroll, XmNitemCount, &item, NULL);
236     if (XmListGetSelectedPos(scroll, &list, &num)) {
237         if(list[0] < item) {
238             XmListSelectPos(scroll, list[0] + 1, True);
239             XmListSetBottomPos(scroll, list[0] + 1);
240         }
241     }
242 }
243
244 static void
245 Cancel()
246 {
247     XtUnmapWidget(XtParent(code_w));
248 }
249
250 static void
251 Apply()
252 {
253     char *asc;
254     XtVaGetValues(text, XmNvalue, &asc, NULL);
255     XtVaSetValues(set_widget, XmNvalue, asc, NULL);
256     XtFree(asc);
257     Cancel();
258 }
259
260 static void
261 CreateItem(item, item_count, start_code)
262 XmString *item;
263 int *item_count;
264 int *start_code;
265 {
266     int count;
267     int start, end;
268     char str[5];
269     FalFontinfo         finfo;
270     extern int begin_code, last_code;
271
272     if (open_font) {
273         if (FalQueryFont(fid, &finfo) == FAL_ERROR) {
274             *item_count = 0;
275             CloseFont();
276             return;
277         }
278         font_w = finfo.width;
279         font_h = finfo.height;
280         start = (finfo.top & 0xff00) >> 8;
281         end = (finfo.bottom & 0xff00) >> 8;
282     } else {
283         font_w = edg.width;
284         font_h = edg.height;
285         start = (begin_code & 0xff00) >> 8;
286         end = (last_code & 0xff00) >> 8;
287     }
288     if (CodePoint) {
289         if (start < 0x80)
290             start = 80;
291         if (end < 0x80)
292             start = 80;
293     } else {
294         if (start > 0x7f)
295             start = 0x7f;
296         if (end > 0x7f)
297             end = 0x7f;
298     }
299     *start_code = start;
300     for (count=0;start <= end; start++, count++) {
301         sprintf(str, "%X", start);
302         item[count] = XmStringCreateLocalized(str);
303     }
304     *item_count = count;
305 }
306
307 static Widget
308 CreateCodeWindow(w)
309 Widget w;
310 {
311     Widget top, base1, base2, base3, base4;
312     Widget frame, scroll, label, sep, form;
313     Widget push1, push2, push3, push4;
314     Arg args[16];
315     int n, i;
316     XmString item[128];
317     XmString xcs;
318     int item_count, start_code, add_c;
319     char add[3];
320
321     n = 0;
322     XtSetArg(args[n], XmNmwmFunctions, MWM_FUNC_ALL | MWM_FUNC_CLOSE |
323                                         MWM_FUNC_RESIZE); n++;
324     XtSetArg(args[n], XmNmwmDecorations, MWM_DECOR_ALL); n++;
325     top = XmCreateFormDialog(w, "code", args, n);
326     
327     base1 = XtVaCreateManagedWidget("base1", xmRowColumnWidgetClass, top,
328                                         XmNorientation, XmVERTICAL, NULL);
329
330     frame = XtVaCreateManagedWidget("frame", xmFrameWidgetClass, base1,
331                                         NULL);
332
333     base2 = XtVaCreateManagedWidget("base2", xmRowColumnWidgetClass, frame,
334                                         XmNorientation, XmHORIZONTAL, NULL);
335
336     CreateItem(item, &item_count, &start_code);
337     code1 = start_code;
338     n = 0 ;
339     XtSetArg(args[n], XmNitems, item); n++;
340     XtSetArg(args[n], XmNitemCount, item_count); n++;
341     XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmSTATIC); n++;
342     scroll = XmCreateScrolledList(base2, "scroll", args, n);
343     XtManageChild(scroll);
344     XtAddCallback(scroll, XmNbrowseSelectionCallback,
345                         (XtCallbackProc)Code1Call, (XtPointer)start_code);
346     XtAddCallback(scroll, XmNdefaultActionCallback,
347                         (XtCallbackProc)Code1Call, (XtPointer)start_code);
348     XmListSelectPos(scroll, 1, False);
349
350     base3 = XtVaCreateManagedWidget("base3", xmRowColumnWidgetClass, base2,
351                                         XmNorientation, XmHORIZONTAL,
352                                         XmNpacking, XmPACK_COLUMN,
353                                         XmNradioAlwaysOne, True,
354                                         XmNradioBehavior , True,
355                                         XmNmarginHeight, 0,
356                                         XmNmarginWidth, 0,
357                                         XmNspacing, 0,
358                                         XmNnumColumns, 9, NULL);
359
360
361     len = (font_w / 8 + 1) * font_h;
362     ptn = (unsigned char *) malloc(len);
363     clr = (unsigned char *) malloc(len);
364     memset(clr, 0, len);
365     XtVaGetValues(w, XmNforeground, &fg, XmNbackground, &bg, NULL);
366     no_pix = XCreatePixmapFromBitmapData(display, BaseW, (char *)clr,
367                 font_w, font_h, fg, bg, DefaultDepth(display, screen));
368
369     XtVaCreateManagedWidget("", xmLabelWidgetClass, base3, NULL);
370     for (i=0; i < 16; i++) {
371         sprintf(add, "%2.1X", i);
372         XtVaCreateManagedWidget(add, xmLabelWidgetClass, base3, NULL);
373     }
374     if (CodePoint)
375         add_c = 8;
376     else
377         add_c = 0;
378     for (i=0; i < NUM; i++) {
379         if ((i % 16) == 0) {
380             sprintf(add, "%2.1X", add_c++);
381             XtVaCreateManagedWidget(add, xmLabelWidgetClass, base3, NULL);
382         }
383         toggle[i] = XtVaCreateWidget("toggle",
384                                         xmPushButtonWidgetClass, base3,
385                                         XmNwidth, font_w,
386                                         XmNheight, font_h,
387                                         XmNlabelType, XmPIXMAP,
388                                         XmNlabelPixmap, no_pix, NULL);
389         XtAddCallback(toggle[i], XmNactivateCallback,
390                                 (XtCallbackProc)Code2Call, (XtPointer)i);
391         XtAddCallback(toggle[i], XmNarmCallback,
392                                 (XtCallbackProc)DrawCode, (XtPointer)i);
393         XtAddCallback(toggle[i], XmNdisarmCallback,
394                                 (XtCallbackProc)DrawCode, (XtPointer)i);
395         XtAddEventHandler(toggle[i], ExposureMask, False,
396                                 (XtEventHandler)DrawCode, (XtPointer)i);
397     }
398     XtManageChildren(toggle, NUM);
399
400     base4 = XtVaCreateManagedWidget("base4", xmFormWidgetClass, base1, NULL);
401
402     xcs = XmStringCreateLocalized(resource.l_code);
403     label = XtVaCreateManagedWidget("code", xmLabelWidgetClass, base4,
404                                         XmNlabelString, xcs,
405                                         XmNleftAttachment, XmATTACH_POSITION,
406                                         XmNleftPosition, 30, NULL);
407
408     text = XtVaCreateManagedWidget("text", xmTextWidgetClass, base4,
409                                         XmNeditable, False,
410                                         XmNcursorPositionVisible, False,
411                                         XmNcolumns, 4,
412                                         XmNleftAttachment, XmATTACH_WIDGET,
413                                         XmNleftWidget, label, NULL);
414
415     sep = XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, base1,
416                                         XmNorientation, XmHORIZONTAL, NULL);
417
418     form = XtVaCreateManagedWidget("sep", xmFormWidgetClass, base1, NULL);
419
420     xcs = XmStringCreateLocalized(resource.previous);
421     push1 = XtVaCreateManagedWidget("PreviousPage", xmPushButtonWidgetClass, form,
422                                         XmNleftAttachment, XmATTACH_POSITION,
423                                         XmNlabelString, xcs,
424                                         XmNleftPosition, 10, NULL);
425     XtAddCallback(push1, XmNactivateCallback,
426                                 (XtCallbackProc)PrevPage, (XtPointer)scroll);
427     XmStringFree(xcs);
428
429     xcs = XmStringCreateLocalized(resource.next);
430     push2 = XtVaCreateManagedWidget("NextPage", xmPushButtonWidgetClass, form,
431                                         XmNleftAttachment, XmATTACH_POSITION,
432                                         XmNlabelString, xcs,
433                                         XmNleftPosition, 35, NULL);
434     XtAddCallback(push2, XmNactivateCallback,
435                                 (XtCallbackProc)NextPage, (XtPointer)scroll);
436     XmStringFree(xcs);
437
438     xcs = XmStringCreateLocalized(resource.apply);
439     push3 = XtVaCreateManagedWidget("Apply", xmPushButtonWidgetClass, form,
440                                         XmNleftAttachment, XmATTACH_POSITION,
441                                         XmNlabelString, xcs,
442                                         XmNleftPosition, 60, NULL);
443     XtAddCallback(push3, XmNactivateCallback, (XtCallbackProc)Apply, NULL);
444     XmStringFree(xcs);
445
446     xcs = XmStringCreateLocalized(resource.l_cancel);
447     push4 = XtVaCreateManagedWidget("Cancel", xmPushButtonWidgetClass, form,
448                                         XmNleftAttachment, XmATTACH_POSITION,
449                                         XmNlabelString, xcs,
450                                         XmNleftPosition, 80, NULL);
451     XtAddCallback(push4, XmNactivateCallback, (XtCallbackProc)Cancel, NULL);
452     XmStringFree(xcs);
453
454     return(top);
455 }
456
457 void
458 CodeWindow(widget, font_name, load_font)
459     Widget      widget;
460     char *      font_name;
461     Boolean     load_font;
462 {
463     static Boolean      old_load_font;
464     extern int  CodePoint;
465
466     set_widget = widget;
467
468     BaseW = XtWindow(widget);
469     display = XtDisplay(widget);
470     screen  = DefaultScreen(display);
471     if (FontName == NULL || strcmp(font_name, FontName) ||
472                                 load_font != old_load_font) {
473         old_load_font = load_font;
474         if (code_w) {
475             XtDestroyWidget(code_w);
476             code_w = 0;
477             free(ptn);
478             free(clr);
479             if (fid != 0) {
480                 CloseFont();
481             }
482         }
483         if (FontName)
484             free(FontName);
485         FontName = (char *)malloc(strlen(font_name)+1);
486         strcpy(FontName, font_name);
487
488         if (load_font) {
489             OpenFont();
490             if (fid == 0)
491                 return;
492             open_font = True;
493         } else {
494             open_font = False;
495         }
496
497         code_w = CreateCodeWindow(widget);
498         XtManageChild(code_w);
499     } else {
500         XtMapWidget(XtParent(code_w));
501     }
502 }