code.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. #define NUM 0x80
  48. static Display *display=NULL;
  49. static int screen;
  50. static Boolean open_font;
  51. static Widget toggle[NUM];
  52. static Widget text;
  53. static Window BaseW;
  54. static Widget set_widget;
  55. static Widget code_w=0;
  56. static int font_w, font_h;
  57. static unsigned long fg, bg;
  58. static unsigned char *ptn, *clr;
  59. static Pixmap no_pix;
  60. static int len, code1;
  61. static char *FontName=NULL;
  62. static FalFontID fid;
  63. extern FalFontID font_id;
  64. extern int CodePoint;
  65. extern Resource resource;
  66. static void
  67. OpenFont(void)
  68. {
  69. int mask;
  70. FalFontData font_data;
  71. static FalFontDataList *copylist;
  72. extern FalFontData fullFontData;
  73. if(strcmp(FontName, fullFontData.xlfdname) == 0) {
  74. fid = font_id;
  75. return;
  76. }
  77. mask = FAL_FONT_MASK_XLFDNAME | FAL_FONT_MASK_DEFINED |
  78. FAL_FONT_MASK_UNDEFINED;
  79. font_data.xlfdname = FontName;
  80. if (! CodePoint) {
  81. mask |= FAL_FONT_MASK_GLYPH_INDEX;
  82. }
  83. fid = FalOpenSysFont(&font_data, mask, &copylist);
  84. }
  85. static void
  86. CloseFont(void)
  87. {
  88. if (fid != font_id) {
  89. FalCloseFont(fid);
  90. fid = 0;
  91. }
  92. }
  93. static void
  94. DrawCode(Widget w, int low_code)
  95. {
  96. int hi_code;
  97. char *f = NULL;
  98. XImage image;
  99. GC gc = XDefaultGC(display, 0);
  100. hi_code = (code1 & 0xff) << 8;
  101. image.width = font_w;
  102. image.height = font_h;
  103. image.xoffset = 0;
  104. image.format = XYBitmap;
  105. image.byte_order = LSBFirst;
  106. image.bitmap_unit = 8;
  107. image.bitmap_bit_order = MSBFirst;
  108. image.bitmap_pad = 8;
  109. image.depth = 1;
  110. image.bytes_per_line = (font_w - 1) / 8 + 1;
  111. XSetForeground(display, gc, fg);
  112. XSetBackground(display, gc, bg);
  113. if (open_font) {
  114. f = (char *)FalReadFont(fid, hi_code | low_code, font_w, font_h);
  115. if(f == NULL || f == (char *)FAL_ERROR) {
  116. return;
  117. }
  118. if( EXISTS_FLAG == 0 ) {
  119. memcpy(ptn, f, len);
  120. image.data = (char *)ptn;
  121. } else {
  122. image.data = (char *)clr;
  123. }
  124. XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
  125. } else {
  126. if (ptnGet(hi_code | low_code, ptn) == 0) {
  127. image.data = (char *)ptn;
  128. } else {
  129. image.data = (char *)clr;
  130. }
  131. XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
  132. }
  133. }
  134. static void
  135. SetPixmap(start_code)
  136. {
  137. int hi_code, low_code;
  138. char *f = NULL;
  139. XImage image;
  140. GC gc = XDefaultGC(display, 0);
  141. hi_code = (start_code & 0xff) << 8;
  142. image.width = font_w;
  143. image.height = font_h;
  144. image.xoffset = 0;
  145. image.format = XYBitmap;
  146. image.byte_order = LSBFirst;
  147. image.bitmap_unit = 8;
  148. image.bitmap_bit_order = MSBFirst;
  149. image.bitmap_pad = 8;
  150. image.depth = 1;
  151. image.bytes_per_line = (font_w - 1) / 8 + 1;
  152. XSetForeground(display, gc, fg);
  153. XSetBackground(display, gc, bg);
  154. if (open_font) {
  155. for (low_code=0; low_code < NUM; low_code++) {
  156. f = (char *)FalReadFont(fid, hi_code | low_code, font_w, font_h);
  157. if(f == NULL || f == (char *)FAL_ERROR) {
  158. return;
  159. }
  160. if( EXISTS_FLAG == 0 ) {
  161. memcpy(ptn, f, len);
  162. image.data = (char *)ptn;
  163. } else {
  164. image.data = (char *)clr;
  165. }
  166. XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
  167. }
  168. } else {
  169. for (low_code=0; low_code < NUM; low_code++) {
  170. if (ptnGet(hi_code | low_code, ptn) == 0) {
  171. image.data = (char *)ptn;
  172. } else {
  173. image.data = (char *)clr;
  174. }
  175. XPutImage(display, XtWindow(toggle[low_code]), gc, &image, 0, 0, 6, 6, font_w, font_h);
  176. }
  177. }
  178. }
  179. static void
  180. Code1Call(Widget w, int start_code, XmListCallbackStruct *data)
  181. {
  182. code1 = start_code + data->item_position -1;
  183. SetPixmap(code1);
  184. }
  185. static void
  186. Code2Call(Widget w, int code2, XmPushButtonCallbackStruct *data)
  187. {
  188. char asc[16];
  189. sprintf(asc, "%2.2X%2.2X", code1, code2);
  190. XtVaSetValues(text, XmNvalue, asc, NULL);
  191. DrawCode(w, code2);
  192. }
  193. static void
  194. PrevPage(Widget w, Widget scroll)
  195. {
  196. int *list;
  197. int num;
  198. if (XmListGetSelectedPos(scroll, &list, &num)) {
  199. if(list[0] > 1) {
  200. XmListSelectPos(scroll, list[0] - 1, True);
  201. XmListSetPos(scroll, list[0] - 1);
  202. }
  203. }
  204. }
  205. static void
  206. NextPage(Widget w, Widget scroll)
  207. {
  208. int *list;
  209. int num, item;
  210. XtVaGetValues(scroll, XmNitemCount, &item, NULL);
  211. if (XmListGetSelectedPos(scroll, &list, &num)) {
  212. if(list[0] < item) {
  213. XmListSelectPos(scroll, list[0] + 1, True);
  214. XmListSetBottomPos(scroll, list[0] + 1);
  215. }
  216. }
  217. }
  218. static void
  219. Cancel(void)
  220. {
  221. XtUnmapWidget(XtParent(code_w));
  222. }
  223. static void
  224. Apply(void)
  225. {
  226. char *asc;
  227. XtVaGetValues(text, XmNvalue, &asc, NULL);
  228. XtVaSetValues(set_widget, XmNvalue, asc, NULL);
  229. XtFree(asc);
  230. Cancel();
  231. }
  232. static void
  233. CreateItem(XmString *item, int *item_count, int *start_code)
  234. {
  235. int count;
  236. int start, end;
  237. char str[5];
  238. FalFontinfo finfo;
  239. extern int begin_code, last_code;
  240. if (open_font) {
  241. if (FalQueryFont(fid, &finfo) == FAL_ERROR) {
  242. *item_count = 0;
  243. CloseFont();
  244. return;
  245. }
  246. font_w = finfo.width;
  247. font_h = finfo.height;
  248. start = (finfo.top & 0xff00) >> 8;
  249. end = (finfo.bottom & 0xff00) >> 8;
  250. } else {
  251. font_w = edg.width;
  252. font_h = edg.height;
  253. start = (begin_code & 0xff00) >> 8;
  254. end = (last_code & 0xff00) >> 8;
  255. }
  256. if (CodePoint) {
  257. if (start < 0x80)
  258. start = 80;
  259. if (end < 0x80)
  260. start = 80;
  261. } else {
  262. if (start > 0x7f)
  263. start = 0x7f;
  264. if (end > 0x7f)
  265. end = 0x7f;
  266. }
  267. *start_code = start;
  268. for (count=0;start <= end; start++, count++) {
  269. sprintf(str, "%X", start);
  270. item[count] = XmStringCreateLocalized(str);
  271. }
  272. *item_count = count;
  273. }
  274. static Widget
  275. CreateCodeWindow(Widget w)
  276. {
  277. Widget top, base1, base2, base3, base4;
  278. Widget frame, scroll, label, sep, form;
  279. Widget push1, push2, push3, push4;
  280. Arg args[16];
  281. int n, i;
  282. XmString item[128];
  283. XmString xcs;
  284. int item_count, start_code, add_c;
  285. char add[9];
  286. n = 0;
  287. XtSetArg(args[n], XmNmwmFunctions, MWM_FUNC_ALL | MWM_FUNC_CLOSE |
  288. MWM_FUNC_RESIZE); n++;
  289. XtSetArg(args[n], XmNmwmDecorations, MWM_DECOR_ALL); n++;
  290. top = XmCreateFormDialog(w, "code", args, n);
  291. base1 = XtVaCreateManagedWidget("base1", xmRowColumnWidgetClass, top,
  292. XmNorientation, XmVERTICAL, NULL);
  293. frame = XtVaCreateManagedWidget("frame", xmFrameWidgetClass, base1,
  294. NULL);
  295. base2 = XtVaCreateManagedWidget("base2", xmRowColumnWidgetClass, frame,
  296. XmNorientation, XmHORIZONTAL, NULL);
  297. CreateItem(item, &item_count, &start_code);
  298. code1 = start_code;
  299. n = 0 ;
  300. XtSetArg(args[n], XmNitems, item); n++;
  301. XtSetArg(args[n], XmNitemCount, item_count); n++;
  302. XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmSTATIC); n++;
  303. scroll = XmCreateScrolledList(base2, "scroll", args, n);
  304. XtManageChild(scroll);
  305. XtAddCallback(scroll, XmNbrowseSelectionCallback,
  306. (XtCallbackProc)Code1Call, (XtPointer) (intptr_t) start_code);
  307. XtAddCallback(scroll, XmNdefaultActionCallback,
  308. (XtCallbackProc)Code1Call, (XtPointer) (intptr_t) start_code);
  309. XmListSelectPos(scroll, 1, False);
  310. base3 = XtVaCreateManagedWidget("base3", xmRowColumnWidgetClass, base2,
  311. XmNorientation, XmHORIZONTAL,
  312. XmNpacking, XmPACK_COLUMN,
  313. XmNradioAlwaysOne, True,
  314. XmNradioBehavior , True,
  315. XmNmarginHeight, 0,
  316. XmNmarginWidth, 0,
  317. XmNspacing, 0,
  318. XmNnumColumns, 9, NULL);
  319. len = (font_w / 8 + 1) * font_h;
  320. ptn = (unsigned char *) malloc(len);
  321. clr = (unsigned char *) malloc(len);
  322. memset(clr, 0, len);
  323. XtVaGetValues(w, XmNforeground, &fg, XmNbackground, &bg, NULL);
  324. no_pix = XCreatePixmapFromBitmapData(display, BaseW, (char *)clr,
  325. font_w, font_h, fg, bg, DefaultDepth(display, screen));
  326. XtVaCreateManagedWidget("", xmLabelWidgetClass, base3, NULL);
  327. for (i=0; i < 16; i++) {
  328. sprintf(add, "%2.1X", i);
  329. XtVaCreateManagedWidget(add, xmLabelWidgetClass, base3, NULL);
  330. }
  331. if (CodePoint)
  332. add_c = 8;
  333. else
  334. add_c = 0;
  335. for (i=0; i < NUM; i++) {
  336. if ((i % 16) == 0) {
  337. snprintf(add, sizeof(add), "%2.1X", add_c++);
  338. XtVaCreateManagedWidget(add, xmLabelWidgetClass, base3, NULL);
  339. }
  340. toggle[i] = XtVaCreateWidget("toggle",
  341. xmPushButtonWidgetClass, base3,
  342. XmNwidth, font_w,
  343. XmNheight, font_h,
  344. XmNlabelType, XmPIXMAP,
  345. XmNlabelPixmap, no_pix, NULL);
  346. XtAddCallback(toggle[i], XmNactivateCallback,
  347. (XtCallbackProc)Code2Call, (XtPointer) (intptr_t) i);
  348. XtAddCallback(toggle[i], XmNarmCallback,
  349. (XtCallbackProc)DrawCode, (XtPointer) (intptr_t) i);
  350. XtAddCallback(toggle[i], XmNdisarmCallback,
  351. (XtCallbackProc)DrawCode, (XtPointer) (intptr_t) i);
  352. XtAddEventHandler(toggle[i], ExposureMask, False,
  353. (XtEventHandler)DrawCode, (XtPointer) (intptr_t) i);
  354. }
  355. XtManageChildren(toggle, NUM);
  356. base4 = XtVaCreateManagedWidget("base4", xmFormWidgetClass, base1, NULL);
  357. xcs = XmStringCreateLocalized(resource.l_code);
  358. label = XtVaCreateManagedWidget("code", xmLabelWidgetClass, base4,
  359. XmNlabelString, xcs,
  360. XmNleftAttachment, XmATTACH_POSITION,
  361. XmNleftPosition, 30, NULL);
  362. text = XtVaCreateManagedWidget("text", xmTextWidgetClass, base4,
  363. XmNeditable, False,
  364. XmNcursorPositionVisible, False,
  365. XmNcolumns, 4,
  366. XmNleftAttachment, XmATTACH_WIDGET,
  367. XmNleftWidget, label, NULL);
  368. sep = XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, base1,
  369. XmNorientation, XmHORIZONTAL, NULL);
  370. form = XtVaCreateManagedWidget("sep", xmFormWidgetClass, base1, NULL);
  371. xcs = XmStringCreateLocalized(resource.previous);
  372. push1 = XtVaCreateManagedWidget("PreviousPage", xmPushButtonWidgetClass, form,
  373. XmNleftAttachment, XmATTACH_POSITION,
  374. XmNlabelString, xcs,
  375. XmNleftPosition, 10, NULL);
  376. XtAddCallback(push1, XmNactivateCallback,
  377. (XtCallbackProc)PrevPage, (XtPointer)scroll);
  378. XmStringFree(xcs);
  379. xcs = XmStringCreateLocalized(resource.next);
  380. push2 = XtVaCreateManagedWidget("NextPage", xmPushButtonWidgetClass, form,
  381. XmNleftAttachment, XmATTACH_POSITION,
  382. XmNlabelString, xcs,
  383. XmNleftPosition, 35, NULL);
  384. XtAddCallback(push2, XmNactivateCallback,
  385. (XtCallbackProc)NextPage, (XtPointer)scroll);
  386. XmStringFree(xcs);
  387. xcs = XmStringCreateLocalized(resource.apply);
  388. push3 = XtVaCreateManagedWidget("Apply", xmPushButtonWidgetClass, form,
  389. XmNleftAttachment, XmATTACH_POSITION,
  390. XmNlabelString, xcs,
  391. XmNleftPosition, 60, NULL);
  392. XtAddCallback(push3, XmNactivateCallback, (XtCallbackProc)Apply, NULL);
  393. XmStringFree(xcs);
  394. xcs = XmStringCreateLocalized(resource.l_cancel);
  395. push4 = XtVaCreateManagedWidget("Cancel", xmPushButtonWidgetClass, form,
  396. XmNleftAttachment, XmATTACH_POSITION,
  397. XmNlabelString, xcs,
  398. XmNleftPosition, 80, NULL);
  399. XtAddCallback(push4, XmNactivateCallback, (XtCallbackProc)Cancel, NULL);
  400. XmStringFree(xcs);
  401. return(top);
  402. }
  403. void
  404. CodeWindow(Widget widget, char *font_name, Boolean load_font)
  405. {
  406. static Boolean old_load_font;
  407. extern int CodePoint;
  408. set_widget = widget;
  409. BaseW = XtWindow(widget);
  410. display = XtDisplay(widget);
  411. screen = DefaultScreen(display);
  412. if (FontName == NULL || strcmp(font_name, FontName) ||
  413. load_font != old_load_font) {
  414. old_load_font = load_font;
  415. if (code_w) {
  416. XtDestroyWidget(code_w);
  417. code_w = 0;
  418. free(ptn);
  419. free(clr);
  420. if (fid != 0) {
  421. CloseFont();
  422. }
  423. }
  424. if (FontName)
  425. free(FontName);
  426. FontName = (char *)malloc(strlen(font_name)+1);
  427. strcpy(FontName, font_name);
  428. if (load_font) {
  429. OpenFont();
  430. if (fid == 0)
  431. return;
  432. open_font = True;
  433. } else {
  434. open_font = False;
  435. }
  436. code_w = CreateCodeWindow(widget);
  437. XtManageChild(code_w);
  438. } else {
  439. XtMapWidget(XtParent(code_w));
  440. }
  441. }