fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

test.c
Go to the documentation of this file.
1 /* "$Id: $"
2  *
3  * Author: Jean-Marc Lienher ( http://oksid.ch )
4  * Copyright 2000-2003 by O'ksi'D.
5  *
6  * This library is free software. Distribution and use rights are outlined in
7  * the file "COPYING" which should have been included with this file. If this
8  * file is missing or damaged, see the license at:
9  *
10  * http://www.fltk.org/COPYING.php
11  *
12  * Please report all bugs and problems on the following page:
13  *
14  * http://www.fltk.org/str.php
15  */
16 
17 /*
18  * UTF-8 X test program (It contains MINIMAL code to support XIM !!!)
19  *
20  ****************
21  * To test it do:
22  ****************
23 
24 kinput2 -canna
25 XMODIFIERS="@im=kinput2"; export XMODIFIERS
26 LANG=ja_JP; export LANG
27 ./test
28 
29  to open a conversion window press "Shift space"
30  type some keys.
31  press space.
32  select glyph with arrows keys.
33  press return.
34  press return.
35  press "Shift space" to close the window
36 
37 LANG=ar_AE; export LANG
38 LANG=he_IL; export LANG
39 export LANG=ja_JP; export XMODIFIERS="@im=kinput2"
40 export LANG=ja_JP; export XMODIFIERS="@im=nicolatter"
41 export LANG=ja_JP; export XMODIFIERS="@im=jmode"
42 export LANG=ja_JP; export XMODIFIERS="@im=htt"
43 export LANG=ko_KR; export XMODIFIERS="@im=Ami"
44 export LANG=zh_TW; export XMODIFIERS="@im=xcin-zh_TW"
45 export LANG=zh_TW; export XMODIFIERS="@im=xcin-zh_CN"
46 export LANG=C; export XMODIFIERS="@im=interxim"
47 **********************************************************/
48 
49 #include <stdlib.h>
50 #include <string.h>
51 #include <stdio.h>
52 #include <wchar.h>
53 #include "Xutf8.h"
54 #include <X11/Xlocale.h>
55 #include <X11/X.h>
56 #include <X11/Xlib.h>
57 #include <X11/Xutil.h>
58 #include <X11/Intrinsic.h>
59 #include <X11/Xmd.h>
60 
61 char *jp_txt = " UTF-8 e\xCC\x82=\xC3\xAA"
62  " \357\274\270\357\274\254\357\274\246\357\274"
63  "\244\345\220\215\343\201\247\346\214\207 \345\256\232"
64  "\343\201\231\343\202\213";
65 
66 char *rtl_txt = "->e\xCC\x82=\xC3\xAA";
67 
68 XIM xim_im = NULL;
69 XIC xim_ic = NULL;
70 static XIMStyles* xim_styles = NULL;
72 GC gc;
73 int x = 2;
74 int y = 40;
75 
76 int main(int argc, char**argv) {
77  char **missing_charset_list;
78  int missing_charset_count;
79  XGCValues xgcv;
80  unsigned long mask;
81  Display* dpy;
82  int scr;
83  Window w, root;
84  XSetWindowAttributes set_attr;
85  int i;
86  XIMStyle *style;
87  static char buf[128];
88  KeySym keysym = 0;
89  Status status;
90  XWMHints wm_hints;
91  XClassHint class_hints;
92  XIMStyle input_style = 0;
93  char **font_name_list;
94  char *def_string;
95  XFontStruct **font_struct_list;
96  char **font_encoding_list;
97  int nb_font;
98  int len = 0;
99  int no_xim = 0;
100 
101  printf ("A -> %c \n", XUtf8Tolower('A'));
102  if (!setlocale(LC_ALL, ""))
103  puts("locale not supported by C library, locale unchanged");
104 
105  if (!XSetLocaleModifiers(""))
106  puts("X locale modifiers not supported, using default");
107 
108  dpy = XOpenDisplay(0);
109  if (!dpy) { puts("cannot open display.\n"); exit(-1); }
110  scr = DefaultScreen(dpy);
111  root = RootWindow(dpy, scr);
112  set_attr.event_mask = KeyPressMask|FocusChangeMask;
113  set_attr.background_pixel = WhitePixel(dpy, DefaultScreen(dpy));
114  set_attr.border_pixel = BlackPixel(dpy, DefaultScreen(dpy));
115  w = XCreateWindow(dpy, root, 10,10,200,100,0,
116  DefaultDepth(dpy, DefaultScreen(dpy)),
117  InputOutput, DefaultVisual(dpy, DefaultScreen(dpy)),
118  CWEventMask | CWBackPixel | CWBorderPixel, &set_attr);
119  if (!w) {
120  puts("cannot creat window.\n");
121  exit(-1);
122  }
123 
124  class_hints.res_name = "test";
125  class_hints.res_class = "Test";
126  wm_hints.input = True;
127  wm_hints.flags = InputHint;
128 
129  XmbSetWMProperties(dpy, w, "test", "test", NULL, 0,
130  NULL, &wm_hints, &class_hints);
131 
132  XMapWindow(dpy, w);
133  xim_im = XOpenIM(dpy, NULL, "test", "Test");
134  if (!xim_im) {
135  puts("cannot Open Input Manager: Try default.\n");
136  XSetLocaleModifiers("@im=");
137  xim_im = XOpenIM(dpy, NULL, "test", "Test");
138  if (!xim_im) {
139  puts("Failed exiting.\n");
140  exit(-1);
141  }
142  }
143  XGetIMValues (xim_im, XNQueryInputStyle, &xim_styles, NULL, NULL);
144  for (i = 0, style = xim_styles->supported_styles;
145  i < xim_styles->count_styles; i++, style++) {
146  if (i == 0 && *style == (XIMStatusNone|XIMPreeditNone)) {
147  printf("this is not a XIM server !!!\n");
148  no_xim = 1;
149  }
150  printf("input style : 0x%X\n", *style);
151  }
152 
153  xim_ic = XCreateIC(xim_im,
154  XNInputStyle,
155  (XIMPreeditNothing | XIMStatusNothing),
156  XNClientWindow, w,
157  XNFocusWindow, w,
158  NULL);
159  if (!xim_ic) {
160  puts("cannot create Input Context.\n");
161  exit(-1);
162  }
163  XFree(xim_styles);
164  XSetICFocus(xim_ic);
165 
166  /***************************************************************
167  * I don't recommend to use a font base name list similar
168  * to the following one in a real application ;-)
169  * You should use an iso8859-1 font, plus a single font for
170  * your language.
171  ***************************************************************/
173  "-*-*-*-*-*-*-*-*-*-*-*-*-iso8858-3," /* not valid */
174  "-*-*-medium-r-*-*-*-*-*-*-*-*-iso8859-1,"
175  "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-6,"
176  "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-8,"
177  "-*-*-*-*-*-*-*-*-*-*-*-*-ksc5601.1987-0,"
178  "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific,"
179  "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-2,"
180  "-*-*-*-*-*-*-*-*-*-*-*-*-koi8-1,"
181  "-*-*-*-*-*-*-*-*-*-*-*-*-jisx0208.1983-0,"
182  "-*-*-*-*-*-*-*-*-*-*-*-*-jisx0212.1990-0,"
183  "-*-*-*-*-*-*-*-*-*-*-*-*-big5-0,"
184  "-*-*-*-*-*-*-*-*-*-*-*-*-jisx0201.1976-0,"
185  "-*-unifont-*-*-*-*-*-*-*-*-*-*-iso10646-1[0x300 0x400_0x500],"
186  "-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
187 
188  /* THIS PART IS NOT REQUIERED */
189  nb_font = fontset->nb_font;
190 
191  while (nb_font > 0) {
192  nb_font--;
193  if (fontset->fonts[nb_font]) {
194  printf("encoding=\"\" fid=%d \n %s\n",
195  /* fontset->encodings[nb_font], */
196  fontset->fonts[nb_font]->fid,
197  fontset->font_name_list[nb_font]);
198  }
199  }
200  /* END OF NOT REQUIERED PART*/
201 
202  mask = (GCForeground | GCBackground);
203  xgcv.foreground = BlackPixel(dpy, DefaultScreen(dpy));
204  xgcv.background = WhitePixel(dpy, DefaultScreen(dpy));
205 
206  gc = XCreateGC(dpy, w, mask, &xgcv);
207  if (!gc) {
208  puts("cannot create Graphic Context.\n");
209  exit(-1);
210  }
211 
212  /***************************************************************/
213  while (1) {
214  int filtered;
215  static XEvent xevent;
216  static XVaNestedList list1 = 0;
217  int r;
218 
219  XNextEvent(dpy, &xevent);
220  if (xevent.type == KeyPress) {
221  XKeyEvent *e = (XKeyEvent*) &xevent;
222  printf ("0x%X %d\n", e->state, e->keycode);
223  }
224  if (xevent.type == DestroyNotify) {
225  /* XIM server has crashed */
226  no_xim = 1;
227  XSetLocaleModifiers("@im=");
228  xim_im = XOpenIM(dpy, NULL, "test", "Test");
229  if (xim_im) {
230  xim_ic = XCreateIC(xim_im,
231  XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
232  XNClientWindow, w,
233  XNFocusWindow, w,
234  NULL);
235  } else {
236  xim_ic = NULL;
237  }
238  if (!xim_ic) {
239  puts("Crash recovery failed. exiting.\n");
240  exit(-1);
241  }
242  }
243  if (xevent.type != DestroyNotify) {
244  filtered = XFilterEvent(&xevent, 0);
245  }
246  if (xevent.type == FocusOut && xim_ic) XUnsetICFocus(xim_ic);
247  if (xevent.type == FocusIn && xim_ic) XSetICFocus(xim_ic);
248 
249  if (xevent.type == KeyPress && !filtered) {
250  len = XUtf8LookupString(xim_ic, &xevent.xkey,
251  buf, 127, &keysym, &status);
252 
253  if (len == 1 && buf[0] == '\b') {
254  x -= XUtf8TextWidth(fontset, buf, len);
255  XUtf8DrawImageString(dpy, w, fontset, gc,
256  x, y, buf, len);
257  } else if (len == 1 && buf[0] == '\r') {
258  y += fontset->ascent + fontset->descent;
259  x = 0;
260  XCloseIM(xim_im);
261  } else {
262  XUtf8DrawImageString(dpy, w, fontset, gc, x, y, buf, len);
263  x += XUtf8TextWidth(fontset, buf, len);
264  }
265 
266  XUtf8DrawString(dpy, w, fontset, gc, 0, 20, jp_txt, strlen(jp_txt));
267  XUtf8DrawString(dpy, w, fontset, gc, 50, 90, rtl_txt, strlen(rtl_txt));
268  XUtf8DrawRtlString(dpy, w, fontset, gc, 50, 90, rtl_txt, strlen(rtl_txt));
269  buf[len] = 0;
270  printf("'%s' %d %x\n", buf, keysym, keysym);
271  buf[0] = 0;
272  }
273  if (filtered) {
274  printf("Dead key\n");
275  }
276  }
278  return 0;
279 }
280 
281 /*
282  * End of "$Id$".
283  */
XFreeUtf8FontStruct
void XFreeUtf8FontStruct(Display *dpy, XUtf8FontStruct *font_set)
Definition: utf8Wrap.c:1005
buf
static char * buf
Definition: fl_encoding_mac_roman.cxx:76
xim_ic
XIC xim_ic
Definition: test.c:69
xim_styles
static XIMStyles * xim_styles
Definition: test.c:70
xim_im
XIM xim_im
Definition: test.c:68
XUtf8FontStruct::nb_font
int nb_font
Definition: Xutf8.h:31
NULL
#define NULL
Definition: forms.H:34
XUtf8DrawString
void XUtf8DrawString(Display *display, Drawable d, XUtf8FontStruct *font_set, GC gc, int x, int y, const char *string, int num_bytes)
Definition: utf8Wrap.c:445
fontset
XUtf8FontStruct * fontset
Definition: test.c:71
XUtf8FontStruct::descent
int descent
Definition: Xutf8.h:37
XUtf8Tolower
int XUtf8Tolower(int ucs)
Definition: case.c:30
main
int main(int argc, char **argv)
Definition: test.c:76
jp_txt
char * jp_txt
Definition: test.c:61
gc
GC gc
Definition: test.c:72
XUtf8FontStruct::ascent
int ascent
Definition: Xutf8.h:36
rtl_txt
char * rtl_txt
Definition: test.c:66
XUtf8FontStruct
Definition: Xutf8.h:30
x
int x
Definition: test.c:73
y
int y
Definition: test.c:74
XCreateUtf8FontStruct
XUtf8FontStruct * XCreateUtf8FontStruct(Display *dpy, const char *base_font_name_list)
Definition: utf8Wrap.c:295
XUtf8FontStruct::fonts
XFontStruct ** fonts
Definition: Xutf8.h:34
XUtf8DrawRtlString
void XUtf8DrawRtlString(Display *display, Drawable d, XUtf8FontStruct *font_set, GC gc, int x, int y, const char *string, int num_bytes)
Definition: utf8Wrap.c:324
XUtf8FontStruct::font_name_list
char ** font_name_list
Definition: Xutf8.h:32
XUtf8TextWidth
int XUtf8TextWidth(XUtf8FontStruct *font_set, const char *string, int num_bytes)
Definition: utf8Wrap.c:705
Window
class FLWindow * Window
Definition: mac.H:32
XUtf8DrawImageString
void XUtf8DrawImageString(Display *display, Drawable d, XUtf8FontStruct *font_set, GC gc, int x, int y, const char *string, int num_bytes)
Definition: utf8Wrap.c:952
XUtf8LookupString
int XUtf8LookupString(XIC ic, XKeyPressedEvent *event, char *buffer_return, int bytes_buffer, KeySym *keysym, Status *status_return)
Definition: utf8Input.c:412
Xutf8.h