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

test.c

Go to the documentation of this file.
00001 /* "$Id: $"
00002  *
00003  * Author: Jean-Marc Lienher ( http://oksid.ch )
00004  * Copyright 2000-2003 by O'ksi'D.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00019  * USA.
00020  *
00021  * Please report all bugs and problems on the following page:
00022  *
00023  *     http://www.fltk.org/str.php
00024  */
00025 
00026 /*
00027  *   UTF-8 X test program (It contains MINIMAL code to support XIM !!!)
00028  *
00029  ****************
00030  * To test it do:
00031  ****************
00032 
00033 kinput2 -canna
00034 XMODIFIERS="@im=kinput2"; export XMODIFIERS
00035 LANG=ja_JP; export LANG
00036 ./test
00037 
00038         to open a conversion window press "Shift space"
00039         type some keys.
00040         press space.
00041         select glyph with arrows keys.
00042         press return.
00043         press return.
00044         press "Shift space" to close the window
00045 
00046 LANG=ar_AE; export LANG
00047 LANG=he_IL; export LANG
00048 export LANG=ja_JP; export XMODIFIERS="@im=kinput2"
00049 export LANG=ja_JP; export XMODIFIERS="@im=nicolatter"
00050 export LANG=ja_JP; export XMODIFIERS="@im=jmode"
00051 export LANG=ja_JP; export XMODIFIERS="@im=htt"
00052 export LANG=ko_KR; export XMODIFIERS="@im=Ami"
00053 export LANG=zh_TW; export XMODIFIERS="@im=xcin-zh_TW"
00054 export LANG=zh_TW; export XMODIFIERS="@im=xcin-zh_CN"
00055 export LANG=C; export XMODIFIERS="@im=interxim"
00056 **********************************************************/
00057 
00058 #include <stdlib.h>
00059 #include <string.h>
00060 #include <stdio.h>
00061 #include <wchar.h>
00062 #include "Xutf8.h"
00063 #include <X11/Xlocale.h>
00064 #include <X11/X.h>
00065 #include <X11/Xlib.h>
00066 #include <X11/Xutil.h>
00067 #include <X11/Intrinsic.h>
00068 #include <X11/Xmd.h>
00069 
00070 char *jp_txt = "é  UTF-8 e\xCC\x82=\xC3\xAA"
00071                 "  \357\274\270\357\274\254\357\274\246\357\274"
00072                 "\244\345\220\215\343\201\247\346\214\207    \345\256\232"
00073                 "\343\201\231\343\202\213";
00074 
00075 char *rtl_txt = "->e\xCC\x82=\xC3\xAA";
00076 
00077 XIM xim_im = NULL;
00078 XIC xim_ic = NULL;
00079 static XIMStyles* xim_styles = NULL;
00080 XUtf8FontStruct *fontset;
00081 GC gc;
00082 int x = 2;
00083 int y = 40;
00084 
00085 int main(int argc, char**argv) {
00086   char **missing_charset_list;
00087   int missing_charset_count;
00088   XGCValues xgcv;
00089   unsigned long mask;
00090   Display* dpy;
00091   int scr;
00092   Window w, root;
00093   XSetWindowAttributes set_attr;
00094   int i;
00095   XIMStyle *style;
00096   static char buf[128];
00097   KeySym keysym = 0;
00098   Status status;
00099   XWMHints wm_hints;
00100   XClassHint class_hints;
00101   XIMStyle input_style = 0;
00102   char **font_name_list;
00103   char *def_string;
00104   XFontStruct **font_struct_list;
00105   char **font_encoding_list;
00106   int nb_font;
00107   int len = 0;
00108   int no_xim = 0;
00109 
00110   printf ("A -> %c \n", XUtf8Tolower('A'));
00111   if (!setlocale(LC_ALL, ""))
00112     puts("locale not supported by C library, locale unchanged");
00113 
00114   if (!XSetLocaleModifiers(""))
00115     puts("X locale modifiers not supported, using default");
00116   
00117   dpy = XOpenDisplay(0);
00118   if (!dpy) { puts("cannot open display.\n"); exit(-1); }
00119   scr = DefaultScreen(dpy);
00120   root = RootWindow(dpy, scr);
00121   set_attr.event_mask = KeyPressMask|FocusChangeMask;
00122   set_attr.background_pixel = WhitePixel(dpy, DefaultScreen(dpy));
00123   set_attr.border_pixel = BlackPixel(dpy, DefaultScreen(dpy));
00124   w = XCreateWindow(dpy, root, 10,10,200,100,0, 
00125                     DefaultDepth(dpy, DefaultScreen(dpy)),
00126                     InputOutput, DefaultVisual(dpy, DefaultScreen(dpy)),
00127                     CWEventMask | CWBackPixel | CWBorderPixel, &set_attr);
00128   if (!w) {
00129     puts("cannot creat window.\n");
00130     exit(-1);
00131   }
00132 
00133   class_hints.res_name = "test";
00134   class_hints.res_class = "Test";
00135   wm_hints.input = True;
00136   wm_hints.flags = InputHint;
00137 
00138   XmbSetWMProperties(dpy, w, "test", "test", NULL, 0,
00139                      NULL, &wm_hints, &class_hints);
00140 
00141   XMapWindow(dpy, w);
00142   xim_im = XOpenIM(dpy, NULL, "test", "Test");
00143   if (!xim_im) { 
00144     puts("cannot Open Input Manager: Try default.\n"); 
00145     XSetLocaleModifiers("@im=");
00146     xim_im = XOpenIM(dpy, NULL, "test", "Test");
00147     if (!xim_im) {
00148       puts("Failed exiting.\n");
00149       exit(-1);
00150     }
00151   }
00152   XGetIMValues (xim_im, XNQueryInputStyle, &xim_styles, NULL, NULL);
00153   for (i = 0, style = xim_styles->supported_styles;
00154        i < xim_styles->count_styles; i++, style++) {
00155     if (i == 0 && *style == (XIMStatusNone|XIMPreeditNone)) {
00156       printf("this is not a XIM server !!!\n");
00157       no_xim = 1;
00158     }
00159     printf("input style : 0x%X\n", *style);
00160   }
00161 
00162   xim_ic = XCreateIC(xim_im,
00163                      XNInputStyle, 
00164                      (XIMPreeditNothing | XIMStatusNothing),
00165                      XNClientWindow, w,
00166                      XNFocusWindow, w,
00167                      NULL);
00168   if (!xim_ic) {
00169     puts("cannot create Input Context.\n");
00170     exit(-1);
00171   }
00172   XFree(xim_styles);
00173   XSetICFocus(xim_ic);
00174 
00175   /***************************************************************
00176    *  I don't recommend to use a font base name list similar 
00177    *  to the following one in a real application ;-) 
00178    *  You should use an iso8859-1 font, plus a single font for 
00179    *  your language.
00180    ***************************************************************/
00181   fontset = XCreateUtf8FontStruct(dpy, 
00182           "-*-*-*-*-*-*-*-*-*-*-*-*-iso8858-3," /* not valid */
00183           "-*-*-medium-r-*-*-*-*-*-*-*-*-iso8859-1,"
00184           "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-6,"
00185           "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-8,"
00186           "-*-*-*-*-*-*-*-*-*-*-*-*-ksc5601.1987-0,"
00187           "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific," 
00188           "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-2,"
00189           "-*-*-*-*-*-*-*-*-*-*-*-*-koi8-1,"
00190           "-*-*-*-*-*-*-*-*-*-*-*-*-jisx0208.1983-0,"
00191           "-*-*-*-*-*-*-*-*-*-*-*-*-jisx0212.1990-0,"
00192           "-*-*-*-*-*-*-*-*-*-*-*-*-big5-0,"
00193           "-*-*-*-*-*-*-*-*-*-*-*-*-jisx0201.1976-0,"
00194           "-*-unifont-*-*-*-*-*-*-*-*-*-*-iso10646-1[0x300 0x400_0x500],"
00195           "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"); 
00196 
00197   /* THIS PART IS NOT REQUIERED */
00198   nb_font = fontset->nb_font;
00199 
00200   while (nb_font > 0) {
00201     nb_font--;
00202     if (fontset->fonts[nb_font]) {
00203       printf("encoding=\"\" fid=%d \n  %s\n", 
00204       //     fontset->encodings[nb_font],
00205              fontset->fonts[nb_font]->fid,
00206              fontset->font_name_list[nb_font]);
00207     }
00208   }
00209   /* END OF NOT REQUIERED PART*/
00210 
00211   mask = (GCForeground | GCBackground);
00212   xgcv.foreground = BlackPixel(dpy, DefaultScreen(dpy));
00213   xgcv.background = WhitePixel(dpy, DefaultScreen(dpy));
00214 
00215   gc = XCreateGC(dpy, w, mask, &xgcv);
00216   if (!gc) {
00217     puts("cannot create Graphic Context.\n");
00218     exit(-1);
00219   }
00220 
00221   /***************************************************************/
00222   while (1) {
00223     int filtered;
00224     static XEvent xevent;
00225     static XVaNestedList list1 = 0;
00226     int r;
00227 
00228     XNextEvent(dpy, &xevent);
00229     if (xevent.type == KeyPress) {
00230       XKeyEvent *e = (XKeyEvent*) &xevent;
00231       printf ("0x%X %d\n", e->state, e->keycode);
00232     }
00233     if (xevent.type == DestroyNotify) {
00234       /* XIM server has crashed */
00235       no_xim = 1;
00236       XSetLocaleModifiers("@im=");
00237       xim_im = XOpenIM(dpy, NULL, "test", "Test");
00238       if (xim_im) {
00239         xim_ic = XCreateIC(xim_im,
00240                            XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
00241                            XNClientWindow, w,
00242                            XNFocusWindow, w,
00243                            NULL);
00244       } else {
00245         xim_ic = NULL;
00246       }
00247       if (!xim_ic) {
00248         puts("Crash recovery failed. exiting.\n");
00249         exit(-1);
00250       }
00251     }
00252     if (xevent.type != DestroyNotify) {
00253       filtered = XFilterEvent(&xevent, 0);
00254     }
00255     if (xevent.type == FocusOut && xim_ic) XUnsetICFocus(xim_ic);
00256     if (xevent.type == FocusIn && xim_ic) XSetICFocus(xim_ic);
00257 
00258     if (xevent.type == KeyPress && !filtered) {
00259       len = XUtf8LookupString(xim_ic, &xevent.xkey, 
00260                               buf, 127, &keysym, &status);
00261 
00262       if (len == 1 && buf[0] == '\b') {
00263         x -= XUtf8TextWidth(fontset, buf, len);
00264         XUtf8DrawImageString(dpy, w, fontset, gc, 
00265                              x, y, buf, len);
00266       } else if (len == 1 && buf[0] == '\r') {
00267         y += fontset->ascent + fontset->descent;
00268         x = 0;
00269         XCloseIM(xim_im);
00270       } else {
00271         XUtf8DrawImageString(dpy, w, fontset, gc, x, y, buf, len);
00272         x += XUtf8TextWidth(fontset, buf, len);
00273       }
00274 
00275       XUtf8DrawString(dpy, w, fontset, gc, 0, 20, jp_txt, strlen(jp_txt));
00276       XUtf8DrawString(dpy, w, fontset, gc, 50, 90, rtl_txt, strlen(rtl_txt));
00277       XUtf8DrawRtlString(dpy, w, fontset, gc, 50, 90, rtl_txt, strlen(rtl_txt));
00278       buf[len] = 0;
00279       printf("'%s' %d %x\n", buf, keysym, keysym);
00280       buf[0] = 0;
00281     }
00282     if (filtered) {
00283       printf("Dead key\n");
00284     }
00285   }
00286   XFreeUtf8FontStruct(dpy, fontset);
00287   return 0;
00288 }
00289 
00290 /*
00291  * End of "$Id$".
00292  */