1 /*
2  * $Id: callback.c,v 1.2 2001/06/14 18:16:10 ura Exp $
3  */
4 
5 /*
6  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7  * This file is part of FreeWnn.
8  *
9  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
10  *
11  * Author: OMRON SOFTWARE Co., Ltd. <freewnn@rd.kyoto.omronsoft.co.jp>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with GNU Emacs; see the file COPYING.  If not, write to the
25  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  * Commentary:
28  *
29  * Change log:
30  *
31  * Last modified date: 8,Feb.1999
32  *
33  * Code:
34  *
35  */
36 
37 /*
38  * X11R5 Input Method Test Program
39  * callback.c v 1.0   Tue Apr 23 13:33:07 JST 1991
40  */
41 
42 /*
43  *      Author: Takashi Inoue    OMRON Corporation
44  *                               takashi@ari.ncl.omron.co.jp
45  */
46 
47 #include <stdio.h>
48 
49 #include "exvalue.h"
50 #include "func.h"
51 
52 void
geometry(ic,client_data,call_data)53 geometry (ic, client_data, call_data)
54      XIC ic;
55      XPointer client_data;
56 #ifdef SYSV
57      caddr_t call_data;
58 #else
59      void *call_data;
60 #endif
61 {
62   prstatus ("Calling function of \"GeometryCallback()\"...");
63   cls (prdisp);
64   trnpebuf (call_data, "Geometry");
65   wlogcb (ic, client_data, call_data, "Geometry");
66   prstatus ("done.");
67 }
68 
69 int
pe_start(ic,client_data,call_data)70 pe_start (ic, client_data, call_data)
71      XIC ic;
72      XPointer client_data;
73 #ifdef SYSV
74      caddr_t call_data;
75 #else
76      void *call_data;
77 #endif
78 {
79   int bufsize;
80 
81   prstatus ("Calling function of \"PreeditStartCallback()\"...");
82   cls (prdisp);
83   bufsize = trnpebuf (call_data, "Start");
84   wlogcb (ic, client_data, call_data, "PreeditStart");
85   prstatus ("done.");
86   return (bufsize);
87 }
88 
89 void
pe_done(ic,client_data,call_data)90 pe_done (ic, client_data, call_data)
91      XIC ic;
92      XPointer client_data;
93 #ifdef SYSV
94      caddr_t call_data;
95 #else
96      void *call_data;
97 #endif
98 {
99   prstatus ("Calling function of \"PreeditDoneCallback()\"...");
100   cls (prdisp);
101   trnpebuf (call_data, "Done");
102   wlogcb (ic, client_data, call_data, "PreeditDone");
103   prstatus ("done.");
104 }
105 
106 void
pe_draw(ic,client_data,call_data)107 pe_draw (ic, client_data, call_data)
108      XIC ic;
109      XPointer client_data;
110      XIMPreeditDrawCallbackStruct *call_data;
111 {
112   prstatus ("Calling function of \"PreeditDrawCallback()\"...");
113   cls (prdisp);
114   trnpebuf (call_data, "Draw");
115   wlogcb (ic, client_data, call_data, "PreeditDraw");
116   prstatus ("done.");
117 }
118 
119 void
pe_caret(ic,client_data,call_data)120 pe_caret (ic, client_data, call_data)
121      XIC ic;
122      XPointer client_data;
123      XIMPreeditCaretCallbackStruct *call_data;
124 {
125   char *direction;
126   char *style;
127 
128   prstatus ("Calling function of \"PreeditCaretCallback()\"...");
129   cls (prdisp);
130   prprint ("Valuables of \"PreeditCaretCallback()\"\n\n");
131   chgcaretval (call_data, &direction, &style);
132   prcaret (call_data->position, direction, style);
133   wlogcb (ic, client_data, call_data, "PreeditCaret");
134   prstatus ("done.");
135 }
136 
137 void
st_start(ic,client_data,call_data)138 st_start (ic, client_data, call_data)
139      XIC ic;
140      XPointer client_data;
141 #ifdef SYSV
142      caddr_t call_data;
143 #else
144      void *call_data;
145 #endif
146 {
147   prstatus ("Calling function of \"StatusStartCallback()\"...");
148   cls (prdisp);
149   wlogcb (ic, client_data, call_data, "StatusStart");
150   prstatus ("done.");
151 }
152 
153 void
st_done(ic,client_data,call_data)154 st_done (ic, client_data, call_data)
155      XIC ic;
156      XPointer client_data;
157 #ifdef SYSV
158      caddr_t call_data;
159 #else
160      void *call_data;
161 #endif
162 {
163   prstatus ("Calling function of \"StatusDoneCallback()\"...");
164   cls (prdisp);
165   wlogcb (ic, client_data, call_data, "StatusDone");
166   prstatus ("done.");
167 }
168 
169 void
st_draw(ic,client_data,call_data)170 st_draw (ic, client_data, call_data)
171      XIC ic;
172      XPointer client_data;
173      XIMStatusDrawCallbackStruct *call_data;
174 {
175   prstatus ("Calling function of \"StatusDrawCallback()\"...");
176   cls (prdisp);
177   if (call_data->type == XIMTextType)
178     {
179       if (call_data->data.text->encoding_is_wchar == True)
180         {
181           (XwcDrawImageString (dpy, window1.win, fontset[0], gc, WIN_ITV * 2 + NW_WIDTH, CALCY (W1_HEIGHT, 0), call_data->data.text->string.wide_char, call_data->data.text->length));
182         }
183       else
184         {
185           (XmbDrawImageString (dpy, window1.win, fontset[0], gc, WIN_ITV * 2 + NW_WIDTH, CALCY (W1_HEIGHT, 0), call_data->data.text->string.multi_byte, call_data->data.text->length));
186         }
187     }
188   wlogcb (ic, client_data, call_data, "StatusDraw");
189   prstatus ("done.");
190 }
191 
192 void
wlogcb(ic,client_data,call_data,cat)193 wlogcb (ic, client_data, call_data, cat)
194      XIC ic;
195      XPointer client_data;
196 #ifdef SYSV
197      caddr_t call_data;
198 #else
199      void *call_data;
200 #endif
201      char *cat;
202 {
203   XIMPreeditDrawCallbackStruct *pedcd;
204   XIMPreeditCaretCallbackStruct *peccd;
205   XIMStatusDrawCallbackStruct *stdcd;
206 
207   char *direction;
208   char *style;
209 
210   fprintf (icfp, "\nFunction \"%sCallback\" has just called.\n", cat);
211   fprintf (icfp, "1. ic          : 0x%-18X\n", ic);
212   fprintf (icfp, "2. client_data : 0x%-18X\n", client_data);
213   if ((!strcmp (cat, "Geometry")) || (!strcmp (cat, "PreeditStart")) || (!strcmp (cat, "PreeditDone")) || (!strcmp (cat, "StatusStart")) || (!strcmp (cat, "StatusDone")))
214     {
215       fprintf (icfp, "3. call_data     : 0x%-18X\n", call_data);
216     }
217   else if (!strcmp (cat, "PreeditDraw"))
218     {
219       pedcd = (XIMPreeditDrawCallbackStruct *) call_data;
220       fprintf (icfp, "3. call_data\n");
221       fprintf (icfp, "    1. caret         : %-20d\n", pedcd->caret);
222       fprintf (icfp, "    2. chg_first     : %-20d\n", pedcd->chg_first);
223       fprintf (icfp, "    3. chg_length    : %-20d\n", pedcd->chg_length);
224       fprintf (icfp, "    4. text\n");
225       wlogtext (pedcd->text);
226     }
227   else if (!strcmp (cat, "PreeditCaret"))
228     {
229       peccd = (XIMPreeditCaretCallbackStruct *) call_data;
230       chgcaretval (peccd, &direction, &style);
231       fprintf (icfp, "3. call_data\n");
232       fprintf (icfp, "    1. position      : %-20d\n", peccd->position);
233       fprintf (icfp, "    2. direction     : %-20s\n", direction);
234       fprintf (icfp, "    3. style         : %-20s\n", style);
235     }
236   else if (!strcmp (cat, "StatusDraw"))
237     {
238       stdcd = (XIMStatusDrawCallbackStruct *) call_data;
239       fprintf (icfp, "3. call_data\n");
240       fprintf (icfp, "    1. type          : %-20s\n", ((stdcd->type == XIMTextType) ? "XIMTextType" : "XIMBitmapType"));
241       if (stdcd->type == XIMTextType)
242         {
243           fprintf (icfp, "    2. text\n");
244           wlogtext (stdcd->data.text);
245         }
246       else
247         {
248           fprintf (icfp, "    2. bitmap        : 0x%-18x\n", stdcd->data.bitmap);
249         }
250     }
251 }
252 
253 void
wlogtext(text)254 wlogtext (text)
255      XIMText *text;
256 {
257   int cnt;                      /* CouNTer */
258 
259   fprintf (icfp, "        1. length        : %-20d\n", text->length);
260   fprintf (icfp, "        2. feedback      : 0x%-18x\n", *text->feedback);
261   fprintf (icfp, "        3. encording_is_wchar : %d (%s)\n", text->encoding_is_wchar, ((text->encoding_is_wchar == True) ? "True" : "False"));
262   if (text->length == 0)
263     {
264       fprintf (icfp, "        4. character code [%s]   : None\n", ((text->encoding_is_wchar == True) ? "wc" : "mb"));
265     }
266   else
267     {
268       for (cnt = 0; cnt < text->length; cnt++)
269         {
270           fprintf (icfp, "        4. character code [%s]   : 0x%-18X\n",
271                    ((text->encoding_is_wchar == True) ? "wc" : "mb"), ((text->encoding_is_wchar == True) ? text->string.wide_char[cnt] : text->string.multi_byte[cnt]));
272         }
273     }
274   fprintf (icfp, "\n");
275 }
276 
277 void
chgcaretval(call_data,direction,style)278 chgcaretval (call_data, direction, style)
279      XIMPreeditCaretCallbackStruct *call_data;
280      char **direction;
281      char **style;
282 {
283   switch (call_data->direction)
284     {
285     case XIMForwardChar:
286       *direction = "XIMForwardChar";
287       break;
288     case XIMBackwardChar:
289       *direction = "XIMBackwardChar";
290       break;
291     case XIMForwardWord:
292       *direction = "XIMForwardWord";
293       break;
294     case XIMBackwardWord:
295       *direction = "XIMBackwardWord";
296       break;
297     case XIMCaretUp:
298       *direction = "XIMCaretUp";
299       break;
300     case XIMCaretDown:
301       *direction = "XIMCaretDown";
302       break;
303     case XIMNextLine:
304       *direction = "XIMNextLine";
305       break;
306     case XIMPreviousLine:
307       *direction = "XIMPreviousLine";
308       break;
309     case XIMLineStart:
310       *direction = "XIMLineStart";
311       break;
312     case XIMLineEnd:
313       *direction = "XIMLineEend";
314       break;
315     case XIMAbsolutePosition:
316       *direction = "XIMAbsolutePosition";
317       break;
318     case XIMDontChange:
319       *direction = "XIMDontChange";
320       break;
321     }
322   switch (call_data->style)
323     {
324     case XIMIsInvisible:
325       *style = "XIMIsInvisible";
326       break;
327     case XIMIsPrimary:
328       *style = "XIMIsPrimary";
329       break;
330     case XIMIsSecondary:
331       *style = "XIMIsSecondary";
332       break;
333     }
334 }
335 
336 int
trnpebuf(call_data,cat)337 trnpebuf (call_data, cat)
338 #ifdef SYSV
339      caddr_t call_data;
340 #else
341      void *call_data;
342 #endif
343      char *cat;
344 {
345 
346   static int cnt;               /* CouNTer */
347   static int cntwk;             /* CouNTer for WorK buffer */
348   static wchar_t buf_pe[MAX_BUF];       /* BUFfer for PreEdit */
349   static wchar_t tmp_tx[MAX_BUF];       /* TeMPorary TeXt */
350   static wchar_t buf_wk[MAX_BUF];       /* BUFfer for WorK */
351   XIMPreeditDrawCallbackStruct *pedcd;
352   /* PreEditDrawCallbackData */
353 
354   if ((!strcmp (cat, "Start")) || (!strcmp (cat, "Done")))
355     {
356       bzero (buf_pe, (sizeof (wchar_t)) * MAX_BUF);
357       bzero (tmp_tx, (sizeof (wchar_t)) * MAX_BUF);
358       bzero (buf_wk, (sizeof (wchar_t)) * MAX_BUF);
359       cnt = 0;
360       if (!strcmp (cat, "Start"))
361         {
362           return (MAX_BUF);
363         }
364     }
365   else if (!strcmp (cat, "Draw"))
366     {
367       pedcd = (XIMPreeditDrawCallbackStruct *) call_data;
368       for (cnt = 0; buf_pe[cnt] != 0; cnt++)
369         ;
370       cntwk = cnt - ((pedcd->chg_first) + (pedcd->chg_length));
371 /*      bzero(tmp_tx, (sizeof(wchar_t)) * MAX_BUF);
372         bzero(buf_wk, (sizeof(wchar_t)) * MAX_BUF); */
373       bcopy (&buf_pe[(pedcd->chg_first) + (pedcd->chg_length)], buf_wk, sizeof (wchar_t) * cntwk);
374       bzero (&buf_pe[pedcd->chg_first], sizeof (wchar_t) * (cnt - pedcd->chg_first));
375       cnt = pedcd->chg_first;
376       if (pedcd->text->length > 0)
377         {
378           if (pedcd->text->encoding_is_wchar == False)
379             {
380               /* transaction of translation */
381             }
382           else
383             {
384               bcopy (pedcd->text->string.wide_char, tmp_tx, sizeof (wchar_t) * (pedcd->text->length));
385             }
386           bcopy (tmp_tx, &buf_pe[pedcd->chg_first], sizeof (wchar_t) * (pedcd->text->length));
387           cnt += pedcd->text->length;
388         }
389       bcopy (buf_wk, &buf_pe[cnt], sizeof (wchar_t) * cntwk);
390       cnt += cntwk;
391     }
392   XClearArea (dpy, window1.win, 0, 0, 0, 80, True);
393   XFlush (dpy);
394   if (cnt > 0)
395     {
396       XwcDrawImageString (dpy, window1.win, fontset[0], gc, 0, 20, buf_pe, cnt);
397     }
398   XFlush (dpy);
399   return (0);
400 }
401