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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
###
### This file is part of
###
### TurboLinux ZWinPro
###
### Copyright (C) 1999-2000 TurboLinux, Inc.
### All Rights Reserved
### Distributed under the terms of the GNU General Public License (GPL)
###
###
### Authors: TurboLinux Chinese Development Team:
### Justin Yu <justiny@turbolinux.com.cn>
### Sean Chen <seanc@turbolinux.com.cn>
### Daniel Fang <danf@turbolinux.com.cn>
###
*/
/* Handwriting recognition engine implemented by Justin Yu
*/
#include "all.h"
#define XINC (x0 += KEY_WIDTH)
#define YINC (y0 += KEY_HEIGHT)
#define F_STRING 0
#define F_CONTROL 1
extern int ForwardEvent();
static int in_hwkeys(int x0, int y0);
static void set_hwkey(int n, char c, int f, char *label, char *blabel,
int x, int y, int w, int h);
void HZinitServerHandwritingPad(int x, int y, int w, int h)
{
int x0, y0;
Cursor pencil_cursor = XCreateFontCursor(display, XC_pencil);
XDefineCursor(display, window5, pencil_cursor);
XDefineCursor(display, window6, pencil_cursor);
//server part
HZServer.hzHWwin.x = x;
HZServer.hzHWwin.y = y;
HZServer.hzHWwin.w = w;
HZServer.hzHWwin.h = h;
//for handwriting
flag_checked = True; //need not check
flag_side = HZSERVER_HW_LEFT;
flag_sent = True;
//set default timeout
hw_timeout = HZSERVER_HW_TIMEOUT;
//no choice list
hw_activekeys = 0;
//char keys
x0 = HZSERVER_HW_XOFF;
y0 = HZSERVER_HW_YOFF - 36;
set_hwkey(0, '\b', F_STRING, "ɾ", "R", x0, y0, KEY_WIDTH, KEY_HEIGHT);XINC;
set_hwkey(1, ' ', F_STRING, "", "", x0, y0, KEY_WIDTH, KEY_HEIGHT);XINC;
set_hwkey(2, '\n', F_STRING, "", "^", x0, y0, KEY_WIDTH, KEY_HEIGHT);XINC;
set_hwkey(3, '\0', F_STRING, "", "A", x0, y0, KEY_WIDTH, KEY_HEIGHT);XINC;
set_hwkey(4, '\0', F_STRING, "", "C", x0, y0, KEY_WIDTH, KEY_HEIGHT);XINC;
set_hwkey(5, '\0', F_STRING, "", "B", x0, y0, KEY_WIDTH, KEY_HEIGHT);
XINC; XINC;
//control keys
set_hwkey(6, '\0', F_CONTROL,"", "C", x0, y0, KEY_WIDTH, KEY_HEIGHT);XINC;
set_hwkey(7, '\0', F_CONTROL,"", "", x0, y0, KEY_WIDTH, KEY_HEIGHT);XINC;
//choice keys
x0 = (HZSERVER_HW_XOFF + HZSERVER_HW_CHILD_WIDTH) * 2;
y0 = HZSERVER_HW_YOFF + 30;
set_hwkey(8, '\0', F_STRING, "", "@", x0, y0, KEY_WIDTH, KEY_HEIGHT);YINC;
set_hwkey(9, '\0', F_STRING, "", "@", x0, y0, KEY_WIDTH, KEY_HEIGHT);YINC;
set_hwkey(10,'\0', F_STRING, "", "@", x0, y0, KEY_WIDTH, KEY_HEIGHT);YINC;
set_hwkey(11,'\0', F_STRING, "", "@", x0, y0, KEY_WIDTH, KEY_HEIGHT);YINC;
set_hwkey(12,'\0', F_STRING, "", "@", x0, y0, KEY_WIDTH, KEY_HEIGHT);YINC;
}
void HZhwDrawWindow(void)
{
int i;
Display *dpy = HZServer.display;
Window win = HZServer.window4;
GC dimgc = HZServer.dimGC;
GC lightgc = HZServer.lightGC;
GC bargc = HZServer.barGC;
//faked title bar
XFillRectangle(dpy, win, bargc, HZSERVER_HW_XOFF, 5,
HZSERVER_HW_WIDTH - HZSERVER_HW_XOFF * 2, 24);
if(HZServer.encoding == HZSERVER_ENCODING_GB ||
HZServer.encoding == HZSERVER_ENCODING_GBK ||
HZServer.encoding == HZSERVER_ENCODING_GB18030)
MyXmbDrawString(dpy, win, fontset_gb, lightgc, 180, 20,
"дʶ", strlen("дʶ"));
else
MyXmbDrawString(dpy, win, fontset_big5, lightgc, 180, 20,
"gѧOO", strlen("gѧOO"));
//draw buttons
for(i=0; i<N_HWKEYS - 5 + hw_activekeys; i++){
draw_button(dpy, win, dimgc, lightgc,
hwkeys[i].x, hwkeys[i].y,
hwkeys[i].x + hwkeys[i].w - 1,
hwkeys[i].y + hwkeys[i].h - 1, True);
if(HZServer.encoding == HZSERVER_ENCODING_GB ||
HZServer.encoding == HZSERVER_ENCODING_GBK ||
HZServer.encoding == HZSERVER_ENCODING_GB18030)
MyDrawString(win, HZServer.normalGC, hwkeys[i].x + 6,
hwkeys[i].y + 20, hwkeys[i].label, 2);
else
MyDrawString(win, HZServer.normalGC, hwkeys[i].x + 6,
hwkeys[i].y + 20, hwkeys[i].blabel, 2);
}
}
void HZhwClearPadLeft(void)
{
Display *dpy = HZServer.display;
Window win = HZServer.window5;
GC dimgc = HZServer.dimGC;
//set background of drawing area
XFillRectangle(dpy, win, dimgc, 0, 0,
HZSERVER_HW_CHILD_WIDTH, HZSERVER_HW_CHILD_HEIGHT);
}
void HZhwClearPadRight(void)
{
Display *dpy = HZServer.display;
Window win = HZServer.window6;
GC dimgc = HZServer.dimGC;
//set background of drawing area
XFillRectangle(dpy, win, dimgc, 0, 0,
HZSERVER_HW_CHILD_WIDTH, HZSERVER_HW_CHILD_HEIGHT);
}
void HZprocHWWindow(int x0, int y0)
{
int rtn;
Display *dpy = HZServer.display;
Window win = HZServer.window4;
GC dimgc= HZServer.dimGC;
GC lightgc= HZServer.lightGC;
rtn = in_hwkeys(x0, y0);
if(rtn != -1){
//send string
draw_button(dpy, win, dimgc, lightgc,
hwkeys[rtn].x, hwkeys[rtn].y,
hwkeys[rtn].x + hwkeys[rtn].w - 1,
hwkeys[rtn].y + hwkeys[rtn].h - 1, False);
wait_button_release();
//check the pointer ?
draw_button(dpy, win, dimgc, lightgc,
hwkeys[rtn].x, hwkeys[rtn].y,
hwkeys[rtn].x + hwkeys[rtn].w - 1,
hwkeys[rtn].y + hwkeys[rtn].h - 1, True);
if(hwkeys[rtn].f == F_CONTROL) {
if(rtn == 6){
if(hw_timeout<5000)hw_timeout += 100;
}else if(rtn == 7){
if(hw_timeout>500)hw_timeout -= 100;
}
} else if(rtn == 0){ //delete char
ForwardEvent(XK_BackSpace, 0, 0, 0);
} else if(hwkeys[rtn].c){
char buf[2];
buf[0] = hwkeys[rtn].c;
buf[1] = '\0';
ForwardString(buf, 1);
} else {
HZhwClearPadLeft();
HZhwClearPadRight();
if(HZServer.encoding == HZSERVER_ENCODING_BIG5)
ForwardString(hwkeys[rtn].blabel, 2);
else
ForwardString(hwkeys[rtn].label, 2);
if(rtn >= 8) flag_sent = True;
}
} else {
HZprocMoveWindow();
}
}
void HZprocHWChildWindow(Window win, int x0, int y0)
{
Display *dpy = HZServer.display;
GC lightgc= HZServer.lightGC;
XEvent event;
//see if drawing window changed
if((win == window5 && flag_side == HZSERVER_HW_RIGHT) ||
(win == window6 && flag_side == HZSERVER_HW_LEFT)){
//lookup the other side's recognition
if(!flag_checked){
HZhwToEngineNewLine();
HZhwRecognize();
HZhwDrawPanel();
HZhwDrawWindow();
flag_checked = True;
}
//if side changed, submit string even if
//not timeout.
if(!flag_sent && hw_activekeys > 0){
if(HZServer.encoding == HZSERVER_ENCODING_BIG5)
ForwardString(hwkeys[8].blabel, 2);
else
ForwardString(hwkeys[8].label, 2);
flag_sent = True;
hw_activekeys = 0;
}
if(win == window6) {
HZhwClearPadLeft();
flag_side = HZSERVER_HW_RIGHT;
} else if(win == window5) {
HZhwClearPadRight();
flag_side = HZSERVER_HW_LEFT;
}
} else { //the same side
//if timeout but string not sent,
//send the first hanzi as default.
if(flag_checked > 0 && !flag_sent) {
if(HZServer.encoding == HZSERVER_ENCODING_BIG5)
ForwardString(hwkeys[8].blabel, 2);
else
ForwardString(hwkeys[8].label, 2);
flag_sent = True;
hw_activekeys = 0;
HZhwDrawPanel();
HZhwDrawWindow();
HZhwClearPadLeft();
HZhwClearPadRight();
}
}
//XFlush(display);
//Here I confine the pointer to the handwriting pad window
//Note: use this function only will cause hang of some XIM clients
XGrabPointer(dpy, win, True,
ButtonMotionMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync,
win, None, CurrentTime);
//so I grab the server at the same time......strange!!!
XGrabServer(dpy);
//enter loop
while(1){
int x, y;
//send current position
HZhwToEnginePoint(x0, y0);
XNextEvent(dpy, &event);
switch(event.type){
case ButtonRelease:
//release server
XUngrabServer(dpy);
XUngrabPointer(dpy, CurrentTime);
//send current position
HZhwToEnginePoint(event.xbutton.x,
event.xbutton.y);
//send newline to end the stroke
HZhwToEngineNewLine();
//record the time for timeout checking
record_time();
//init
flag_sent = False;
flag_checked = False;
return ;
case MotionNotify:
x = event.xbutton.x;
y = event.xbutton.y;
XDrawLine(dpy, win, lightgc, x0, y0, x, y);
HZhwToEnginePoint(x, y);
x0 = x;
y0 = y;
break;
default:
break;
}
}
}
static int in_hwkeys(int x0, int y0)
{
int i;
//get
for(i=0; i<N_HWKEYS - 5 + hw_activekeys; i++){
if(in_box(x0, y0, hwkeys[i].x, hwkeys[i].y,
hwkeys[i].x + hwkeys[i].w, hwkeys[i].y + hwkeys[i].h)){
return i;
}
}
return -1;
}
static void set_hwkey(int n, char c, int f, char *label, char *blabel,
int x, int y, int w, int h)
{
hwkeys[n].c = c;
hwkeys[n].f = f;
strncpy(hwkeys[n].label, label, 3);
strncpy(hwkeys[n].blabel, blabel, 3);
hwkeys[n].x = x;
hwkeys[n].y = y;
hwkeys[n].w = w;
hwkeys[n].h = h;
}
void HZhwDrawPanel(void)
{
Display *dpy = HZServer.display;
Window win = HZServer.window4;
GC dimgc = HZServer.dimGC;
GC lightgc = HZServer.lightGC;
GC panelgc = HZServer.panelGC;
int width = HZServer.hzHWwin.w;
int height = HZServer.hzHWwin.h;
XFillRectangle(dpy, win, panelgc, 0, 0, width, height);
/* draw 3D border */
XDrawLine(dpy, win, lightgc, 0, 0, width-1, 0);
XDrawLine(dpy, win, lightgc, 0, 1, width-1, 1);
XDrawLine(dpy, win, lightgc, 0, 0, 0, height-1);
XDrawLine(dpy, win, lightgc, 1, 0, 1, height-1);
XDrawLine(dpy, win, dimgc, 0, height-1, width-1, height-1);
XDrawLine(dpy, win, dimgc, 0, height-2, width-1, height-2);
XDrawLine(dpy, win, dimgc, width-1, 0, width-1, height-1);
XDrawLine(dpy, win, dimgc, width-2, 0, width-2, height-1);
}
void HZhwFlush()
{
HZhwDrawPanel();
HZhwDrawWindow();
HZhwClearPadLeft();
HZhwClearPadRight();
}
void HZprocHW()
{
if(hwmode == True){
XUnmapWindow(display, window6);
XUnmapWindow(display, window5);
XUnmapWindow(display, window4);
hwmode = False;
} else {
XUnmapWindow(display, window1);
XUnmapWindow(display, window2);
XUnmapWindow(display, window3);
hwmode = True;
XMapRaised(display, window4);
XMapRaised(display, window5);
XMapRaised(display, window6);
HZhwFlush();
}
}
|