1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xlocale.h>
#define DEFAULT_FONT \
"8x16,\
-tlc-song-medium-r-normal--16-*-*-*-*-*-gbk-0"
#define F_SIZE 16
#define COL 30
#define ROW 10
#define W_WIDTH (F_SIZE * COL)
#define W_HEIGHT (F_SIZE * ROW)
main(int argc, char *argv[])
{
Display *display;
int screen;
GC gc;
Window window;
XWMHints hints;
Atom protocols;
XFontSet fs;
char **missing_list;
int missing_count;
char *def_string;
XFontSetExtents *fs_ext;
int dec;
XIM im;
XIC ic;
XEvent event;
unsigned long mask, fevent;
int len = 127;
unsigned char string[128], s_tab[ROW][127];
KeySym keysym;
int row = 0, col = 0;
int i, count = 0;
Status status;
XModifierKeymap *xmkm;
display = XOpenDisplay("");
screen = DefaultScreen(display);
gc = DefaultGC(display, screen);
/*
xmkm = XGetModifierMapping(display);
for( i = 0; i < 8; i++ )
printf("%d: %d %d\n", i, xmkm->modifiermap[2 * i],
xmkm->modifiermap[2 * i + 1] );
*/
if (setlocale(LC_CTYPE, "") == NULL) {
fprintf(stderr, "Error : setlocale() !\n");
exit(0);
}
if (! XSupportsLocale() ) {
fprintf(stderr, "Error : XSupportsLocale() !\n");
exit(0);
}
if( (fs = XCreateFontSet(display, DEFAULT_FONT, &missing_list,
&missing_count, &def_string)) == NULL) {
fprintf(stderr, "Error : XCreateFontSet() !\n");
exit(0);
}
fs_ext = XExtentsOfFontSet(fs);
dec = fs_ext->max_logical_extent.height + fs_ext->max_logical_extent.y;
window = XCreateSimpleWindow(display, RootWindow(display, screen),
0, 0, W_WIDTH, W_HEIGHT + dec, 2,
BlackPixel(display, screen),
WhitePixel(display, screen));
XStoreName(display, window, "XIM Demo - Root Window");
hints.flags = InputHint;
hints.input = True;
XSetWMHints(display, window, &hints);
protocols = XInternAtom(display, "WM_DELETE_WINDOW", True);
XSetWMProtocols(display, window, &protocols, 1);
XMapRaised(display, window);
XFlush(display);
if( (im = XOpenIM(display, NULL, NULL, NULL)) == NULL ) {
printf("Error : XOpenIM !\n");
exit(0);
}
if( (ic = XCreateIC(im, XNInputStyle,
XIMPreeditNothing | XIMStatusNothing,
XNClientWindow, window, NULL)) == NULL )
{
printf("Error : XCreateIC() ! \n");
XCloseIM(im);
exit(0);
}
XGetICValues(ic, XNFilterEvents, &fevent, NULL);
mask = ExposureMask | KeyPressMask | FocusChangeMask;
XSelectInput(display, window, mask | fevent);
for(i = 0; i < ROW; i++)
s_tab[i][0] = 0;
for(;;) {
XNextEvent(display, &event);
if (XFilterEvent(&event, None) == True)
continue;
switch(event.type)
{
case FocusIn:
XSetICFocus(ic);
break;
case FocusOut:
XUnsetICFocus(ic);
break;
case Expose:
for ( i=0; i < ROW; i++)
XmbDrawString(display, window, fs, gc, 0,
F_SIZE * (i +1), s_tab[i], strlen(s_tab[i]));
break;
case KeyPress:
count = XmbLookupString(ic, (XKeyPressedEvent *) &event,
string, len, &keysym, &status);
string[count] = 0;
if (status == XLookupBoth && keysym == XK_Return)
{
row = (++row) % ROW;
col = 0;
s_tab[row][0] = 0;
XClearArea(display, window, 0, F_SIZE * row + dec,
W_WIDTH, F_SIZE, False);
}
else if (status = XLookupChars || status == XLookupBoth)
{
XmbDrawString(display, window, fs, gc,
F_SIZE / 2 * col, F_SIZE * (row + 1),
string, count);
for (i = 0; i < count && col < len && string[i]; i++, col++)
s_tab[row][col] = string[i];
s_tab[row][col] = 0;
}
break;
case ClientMessage:
if (event.xclient.data.l[0] == protocols)
{
XDestroyIC(ic);
XCloseIM(im);
XDestroyWindow(display, window);
XCloseDisplay(display);
exit(0);
}
break;
defaults:
break;
}
}
}
|