Mercurial > kinput2.yaz
comparison lib/Ruby.c @ 16:598fcbe482b5
imported patch 19_kinput2-v3.1-ruby.patch
| author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
|---|---|
| date | Mon, 08 Mar 2010 20:38:17 +0900 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 15:89750191b165 | 16:598fcbe482b5 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 1990 Software Research Associates, Inc. | |
| 3 * | |
| 4 * Permission to use, copy, modify, and distribute this software and its | |
| 5 * documentation for any purpose and without fee is hereby granted, provided | |
| 6 * that the above copyright notice appear in all copies and that both that | |
| 7 * copyright notice and this permission notice appear in supporting | |
| 8 * documentation, and that the name of Software Research Associates not be | |
| 9 * used in advertising or publicity pertaining to distribution of the | |
| 10 * software without specific, written prior permission. Software Research | |
| 11 * Associates makes no representations about the suitability of this software | |
| 12 * for any purpose. It is provided "as is" without express or implied | |
| 13 * warranty. | |
| 14 * | |
| 15 * Author: Makoto Ishisone, Software Research Associates, Inc., Japan | |
| 16 */ | |
| 17 | |
| 18 /* Copyright 1991 NEC Corporation, Tokyo, Japan. | |
| 19 * | |
| 20 * Permission to use, copy, modify, and distribute this software and its | |
| 21 * documentation for any purpose and without fee is hereby granted, | |
| 22 * provided that the above copyright notice appear in all copies and that | |
| 23 * both that copyright notice and this permission notice appear in | |
| 24 * supporting documentation, and that the name of NEC Corporation | |
| 25 * not be used in advertising or publicity pertaining to distribution | |
| 26 * of the software without specific, written prior permission. NEC | |
| 27 * Corporation makes no representations about the suitability of this | |
| 28 * software for any purpose. It is provided "as is" without express | |
| 29 * or implied warranty. | |
| 30 * | |
| 31 * NEC CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
| 32 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN | |
| 33 * NO EVENT SHALL NEC CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
| 34 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF | |
| 35 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | |
| 36 * OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
| 37 * PERFORMANCE OF THIS SOFTWARE. | |
| 38 * | |
| 39 * Author: Akira Kon, NEC Corporation. (kon@d1.bs2.mt.nec.co.jp) | |
| 40 */ | |
| 41 | |
| 42 /* Copyright 2003 Hiroyuki Komatsu <komatsu@taiyaki.org> (Ruby.c) */ | |
| 43 | |
| 44 /* 直さなければならないところ | |
| 45 | |
| 46 ・Destroy が呼ばれないので CloseUIContext できない。 | |
| 47 ・モード領域の大きさ。(これは他のファイルだろうな) | |
| 48 | |
| 49 */ | |
| 50 | |
| 51 #ifndef lint | |
| 52 static char *rcsid = "$Id: Ruby.c,v 1.2 2003/06/10 02:11:23 komatsu Exp $"; | |
| 53 #endif | |
| 54 | |
| 55 #include <X11/IntrinsicP.h> | |
| 56 #include <X11/StringDefs.h> | |
| 57 #include <X11/Xmu/Atoms.h> | |
| 58 #define XK_KATAKANA | |
| 59 #include <X11/keysym.h> | |
| 60 #if XtSpecificationRelease > 4 | |
| 61 #include <X11/Xfuncs.h> | |
| 62 #endif | |
| 63 | |
| 64 #include "RubyP.h" | |
| 65 #include "DebugPrint.h" | |
| 66 | |
| 67 #define wchar_t wchar | |
| 68 | |
| 69 #include "ruby-c.h" | |
| 70 | |
| 71 static XtResource resources[] = { | |
| 72 #define offset(field) XtOffset(RubyObject, ruby.field) | |
| 73 { XtNrubyhost, XtCRubyhost, XtRString, sizeof(String), | |
| 74 offset(rubyhost), XtRString, NULL }, | |
| 75 { XtNrubyfile, XtCRubyfile, XtRString, sizeof(String), | |
| 76 offset(rubyfile), XtRString, NULL }, | |
| 77 { XtNsendReturnByString, XtCSendReturnByString, | |
| 78 XtRBoolean, sizeof(Boolean), | |
| 79 offset(sendReturnByString), XtRBoolean, False }, | |
| 80 #undef offset | |
| 81 }; | |
| 82 | |
| 83 static void ClassInitialize(); | |
| 84 static void Initialize(), Destroy(); | |
| 85 static Boolean SetValues(); | |
| 86 static int InputEvent(); | |
| 87 static ICString *GetMode(); | |
| 88 static int CursorPos(); | |
| 89 static int NumSegments(); | |
| 90 static ICString *GetSegment(); | |
| 91 static int CompareSegment(); | |
| 92 static ICString *GetItemList(); | |
| 93 static int SelectItem(); | |
| 94 static int ConvertedString(); | |
| 95 static int ClearConversion(); | |
| 96 static int GetTriggerKeys(); | |
| 97 static int PreeditString(); | |
| 98 static int StatusString(); | |
| 99 | |
| 100 static void convend(); | |
| 101 | |
| 102 #if 1 /* KC_SETLISTCALLBACK */ | |
| 103 static void openSelection(); | |
| 104 #define SELECTION_SET 0 /* SelectionStart をしても良いと言う情報を設定する */ | |
| 105 #define SELECTION_DO 1 /* 実際に SelectionStart を開始する */ | |
| 106 #else /* !KC_SETLISTCALLBACK */ | |
| 107 #define openSelection(x, y, z) | |
| 108 #endif /* !KC_SETLISTCALLBACK */ | |
| 109 | |
| 110 static ICString *GetAuxSegments(); | |
| 111 | |
| 112 RubyClassRec rubyClassRec = { | |
| 113 { /* object fields */ | |
| 114 /* superclass */ (WidgetClass) &inputConvClassRec, | |
| 115 /* class_name */ "Ruby", | |
| 116 /* widget_size */ sizeof(RubyRec), | |
| 117 /* class_initialize */ ClassInitialize, | |
| 118 /* class_part_initialize */ NULL, | |
| 119 /* class_inited */ FALSE, | |
| 120 /* initialize */ Initialize, | |
| 121 /* initialize_hook */ NULL, | |
| 122 /* obj1 */ NULL, | |
| 123 /* obj2 */ NULL, | |
| 124 /* obj3 */ 0, | |
| 125 /* resources */ resources, | |
| 126 /* num_resources */ XtNumber(resources), | |
| 127 /* xrm_class */ NULLQUARK, | |
| 128 /* obj4 */ FALSE, | |
| 129 /* obj5 */ FALSE, | |
| 130 /* obj6 */ FALSE, | |
| 131 /* obj7 */ FALSE, | |
| 132 /* destroy */ Destroy, | |
| 133 /* obj8 */ NULL, | |
| 134 /* obj9 */ NULL, | |
| 135 /* set_values */ SetValues, | |
| 136 /* set_values_hook */ NULL, | |
| 137 /* obj10 */ NULL, | |
| 138 /* get_values_hook */ NULL, | |
| 139 /* obj11 */ NULL, | |
| 140 /* version */ XtVersion, | |
| 141 /* callback_private */ NULL, | |
| 142 /* obj12 */ NULL, | |
| 143 /* obj13 */ NULL, | |
| 144 /* obj14 */ NULL, | |
| 145 /* extension */ NULL | |
| 146 }, | |
| 147 { /* inputConv fields */ | |
| 148 /* InputEvent */ InputEvent, | |
| 149 /* GetMode */ GetMode, | |
| 150 /* CursorPos */ CursorPos, | |
| 151 /* NumSegments */ NumSegments, | |
| 152 /* GetSegment */ GetSegment, | |
| 153 /* CompareSegment */ CompareSegment, | |
| 154 /* GetItemList */ GetItemList, | |
| 155 /* SelectItem */ SelectItem, | |
| 156 /* GetConvertedString */ ConvertedString, | |
| 157 /* ClearConversion */ ClearConversion, | |
| 158 /* GetAuxSegments */ GetAuxSegments, | |
| 159 /* SupportMultipleObjects */ True, | |
| 160 /* GetTriggerKeys */ GetTriggerKeys, | |
| 161 /* num_trigger_keys */ 0, | |
| 162 /* trigger_keys */ NULL, | |
| 163 /* GetPreeditString */ PreeditString, | |
| 164 /* GetStatusString */ StatusString, | |
| 165 /* NoMoreObjects */ False, | |
| 166 }, | |
| 167 { /* ruby fields */ | |
| 168 /* foo */ 0, | |
| 169 } | |
| 170 }; | |
| 171 | |
| 172 WidgetClass rubyObjectClass = (WidgetClass)&rubyClassRec; | |
| 173 | |
| 174 static void fix(); | |
| 175 | |
| 176 static ICString *SymbolList; | |
| 177 static int NumSymbols; | |
| 178 | |
| 179 static void addObject(); | |
| 180 static void deleteObject(); | |
| 181 | |
| 182 static Display *displaybell = (Display *)0; | |
| 183 | |
| 184 static void rubyEUCtoICString(); | |
| 185 static void rubyEUCArraytoICStringArray(); | |
| 186 static void rubySelectionEvent(); | |
| 187 static void rubySelectionDisplay(); | |
| 188 static void rubyDialogDisplay(); | |
| 189 static void rubyDisplayPreEdit(); | |
| 190 | |
| 191 typedef struct { | |
| 192 ICString *array; | |
| 193 int length; | |
| 194 } ICStringArray; | |
| 195 | |
| 196 static ICString *ruby_tmp_seg; | |
| 197 static ICStringArray *ruby_cand_list; | |
| 198 | |
| 199 static void | |
| 200 ClassInitialize() | |
| 201 { | |
| 202 ruby_init(); | |
| 203 | |
| 204 ruby_tmp_seg = XtMalloc(sizeof(ICString)); | |
| 205 ruby_tmp_seg->nchars = 0; | |
| 206 ruby_tmp_seg->nbytes = 0; | |
| 207 ruby_cand_list = XtMalloc(sizeof(ICStringArray)); | |
| 208 ruby_cand_list->length = 0; | |
| 209 | |
| 210 ruby_eval("$LOAD_PATH.concat(eval(`ruby -e 'p $LOAD_PATH'`))"); | |
| 211 ruby_eval("$LOAD_PATH.uniq!"); | |
| 212 | |
| 213 if (ruby_eval("ENV['KINPUT2_RUBY']") != Qnil) { | |
| 214 ruby_eval("require ENV['KINPUT2_RUBY']"); | |
| 215 } else { | |
| 216 ruby_eval("require 'kinput2_default'"); | |
| 217 } | |
| 218 if (ruby_eval("global_variables.member?('$kanjiConv')") == Qfalse) { | |
| 219 fprintf(stderr, "Abort: KanjiConv was not initialized.\n"); | |
| 220 exit (-1); | |
| 221 } | |
| 222 ruby_eval("kanjiConv = $kanjiConv"); | |
| 223 TRACE(("RubyObjectClass initialized\n")); | |
| 224 } | |
| 225 | |
| 226 static int | |
| 227 InputEvent(w, event) | |
| 228 Widget w; | |
| 229 XEvent *event; | |
| 230 { | |
| 231 RubyObject obj = (RubyObject)w; | |
| 232 char key_buf[200]; | |
| 233 KeySym keysym; | |
| 234 int len, nbytes, functionalChar; | |
| 235 | |
| 236 /* KeyPress以外は捨てる */ | |
| 237 if (event->type != KeyPress /*&& event->type != KeyRelease*/) return 0; | |
| 238 | |
| 239 /* 取りあえず文字に直してしまう */ | |
| 240 key_buf[0] = '\0'; | |
| 241 key_buf[1] = '\0'; | |
| 242 key_buf[2] = '\0'; | |
| 243 nbytes = XLookupString(event, key_buf, 20, &keysym, NULL); | |
| 244 | |
| 245 if (keysym == XK_space && (event->xkey.state & ShiftMask)) { | |
| 246 convend(obj); | |
| 247 return 0; | |
| 248 } | |
| 249 | |
| 250 /* ベルを鳴らすディスプレイの設定 */ | |
| 251 displaybell = XtDisplayOfObject((Widget)obj); | |
| 252 | |
| 253 /* かな漢字変換する */ | |
| 254 { | |
| 255 VALUE pass; | |
| 256 printf ("key_buf: `%d'\n", key_buf[0]); | |
| 257 printf ("strlen(key_buf): `%d'\n", strlen(key_buf)); | |
| 258 printf ("nbytes: `%d'\n", nbytes); | |
| 259 pass = ruby_evalf ("kanjiConv.inputEvent(%d, %d, %d)", | |
| 260 key_buf[0], (int)keysym, (int)(event->xkey.state)); | |
| 261 printf ("Input: done\n"); | |
| 262 | |
| 263 rubyDisplayPreEdit(obj); | |
| 264 | |
| 265 return NUM2INT(pass); /* 1: pass the key / 0: trap the key */ | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 | |
| 270 static void | |
| 271 rubyDialogDisplay(obj) | |
| 272 RubyObject obj; | |
| 273 { | |
| 274 ICAuxControlArg arg; | |
| 275 | |
| 276 /* I'm not sure about Aux. */ | |
| 277 if (ruby_eval("kanjiConv.dialog.visible") == Qfalse) { | |
| 278 if (ruby_eval("kanjiConv.dialog.call_open") == Qtrue && | |
| 279 ruby_eval ("kanjiConv.dialog.text.length > 0") == Qtrue) { | |
| 280 printf ("Dialog: open\n"); | |
| 281 arg.command = ICAuxStart; | |
| 282 XtCallCallbackList((Widget)obj, obj->inputConv.auxcallback, | |
| 283 (XtPointer)&arg); | |
| 284 ruby_eval("kanjiConv.dialog.open_end"); | |
| 285 } | |
| 286 } else { /* value_dialog_visible == true */ | |
| 287 if (ruby_eval("kanjiConv.dialog.call_close") == Qtrue) { | |
| 288 printf ("Dialog: close\n"); | |
| 289 arg.command = ICAuxEnd; | |
| 290 XtCallCallbackList((Widget)obj, obj->inputConv.auxcallback, | |
| 291 (XtPointer)&arg); | |
| 292 ruby_eval("kanjiConv.dialog.close_end"); | |
| 293 printf ("Dialog: close...done\n"); | |
| 294 } else { | |
| 295 arg.command = ICAuxChange; | |
| 296 XtCallCallbackList((Widget)obj, obj->inputConv.auxcallback, | |
| 297 (XtPointer)&arg); | |
| 298 } | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 static void | |
| 303 rubySelectionEvent(obj) | |
| 304 RubyObject obj; | |
| 305 { | |
| 306 /*** Right ***/ | |
| 307 if (ruby_eval("kanjiConv.selection.call_right") == Qtrue) { | |
| 308 moveSelection(obj, ICMoveRight); | |
| 309 ruby_eval("kanjiConv.selection.right_end"); | |
| 310 } | |
| 311 | |
| 312 /*** Left ***/ | |
| 313 if (ruby_eval("kanjiConv.selection.call_left") == Qtrue) { | |
| 314 moveSelection(obj, ICMoveLeft); | |
| 315 ruby_eval("kanjiConv.selection.left_end"); | |
| 316 } | |
| 317 | |
| 318 /*** Down ***/ | |
| 319 if (ruby_eval("kanjiConv.selection.call_down") == Qtrue) { | |
| 320 moveSelection(obj, ICMoveDown); | |
| 321 ruby_eval("kanjiConv.selection.down_end"); | |
| 322 } | |
| 323 | |
| 324 /*** Up ***/ | |
| 325 if (ruby_eval("kanjiConv.selection.call_up") == Qtrue) { | |
| 326 moveSelection(obj, ICMoveUp); | |
| 327 ruby_eval("kanjiConv.selection.up_end"); | |
| 328 } | |
| 329 | |
| 330 /*** Beginning Line ***/ | |
| 331 if (ruby_eval("kanjiConv.selection.call_line_beginning") == Qtrue) { | |
| 332 moveSelection(obj, ICMoveLeftMost); | |
| 333 ruby_eval("kanjiConv.selection.line_beginning_end"); | |
| 334 } | |
| 335 | |
| 336 /*** End Line ***/ | |
| 337 if (ruby_eval("kanjiConv.selection.call_line_end") == Qtrue) { | |
| 338 moveSelection(obj, ICMoveRightMost); | |
| 339 ruby_eval("kanjiConv.selection.line_end_end"); | |
| 340 } | |
| 341 | |
| 342 /*** Get Index ***/ | |
| 343 { | |
| 344 ICSelectionControlArg arg; | |
| 345 arg.command = ICSelectionGet; | |
| 346 arg.u.current_item = -1; | |
| 347 | |
| 348 XtCallCallbackList((Widget)obj, obj->inputConv.selectioncallback, | |
| 349 (XtPointer)&arg); | |
| 350 | |
| 351 if (arg.u.current_item >= 0) { | |
| 352 ruby_evalf ("kanjiConv.set_cand_index (%d)", arg.u.current_item); | |
| 353 } | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 static void | |
| 358 moveSelection(obj, dir) | |
| 359 RubyObject obj; | |
| 360 int dir; | |
| 361 { | |
| 362 ICSelectionControlArg arg; | |
| 363 | |
| 364 arg.command = ICSelectionMove; | |
| 365 arg.u.dir = dir; | |
| 366 XtCallCallbackList((Widget)obj, obj->inputConv.selectioncallback, | |
| 367 (XtPointer)&arg); | |
| 368 } | |
| 369 | |
| 370 static void | |
| 371 rubySelectionDisplay(obj) | |
| 372 RubyObject obj; | |
| 373 { | |
| 374 ICSelectionControlArg arg; | |
| 375 | |
| 376 if (ruby_eval("kanjiConv.selection.call_open") == Qtrue) { | |
| 377 printf ("Selection: open\n"); | |
| 378 arg.command = ICSelectionStart; | |
| 379 arg.u.selection_kind = ICSelectionCandidates; | |
| 380 XtCallCallbackList((Widget)obj, obj->inputConv.selectioncallback, | |
| 381 (XtPointer)&arg); | |
| 382 | |
| 383 /* set current item */ | |
| 384 arg.command = ICSelectionSet; | |
| 385 arg.u.current_item = 0; /* INDEX of ITEM */ | |
| 386 XtCallCallbackList((Widget)obj, obj->inputConv.selectioncallback, | |
| 387 (XtPointer)&arg); | |
| 388 | |
| 389 ruby_eval("kanjiConv.selection.open_end"); | |
| 390 } else if (ruby_eval("kanjiConv.selection.call_close") == Qtrue) { | |
| 391 printf ("Selection: close\n"); | |
| 392 arg.command = ICSelectionEnd; | |
| 393 arg.u.current_item = -1; | |
| 394 XtCallCallbackList((Widget)obj, obj->inputConv.selectioncallback, | |
| 395 (XtPointer)&arg); | |
| 396 ruby_eval("kanjiConv.selection.close_end"); | |
| 397 } | |
| 398 } | |
| 399 | |
| 400 | |
| 401 static void | |
| 402 rubyDisplayPreEdit(obj) | |
| 403 RubyObject obj; | |
| 404 { | |
| 405 Widget w = (Widget)obj; | |
| 406 | |
| 407 if (ruby_eval("kanjiConv.call_fix") == Qtrue) { | |
| 408 fix(obj); | |
| 409 } | |
| 410 | |
| 411 /* 入力モードをチェックする */ | |
| 412 /* #if 0 */ | |
| 413 /* if (ks->info & KanjiModeInfo) { */ | |
| 414 /* printf("modechangecallback\n"); */ | |
| 415 XtCallCallbackList(w, obj->inputConv.modechangecallback, | |
| 416 (XtPointer)NULL); | |
| 417 /* } */ | |
| 418 /* #endif */ | |
| 419 | |
| 420 rubyDialogDisplay(obj); | |
| 421 rubySelectionEvent(obj); | |
| 422 rubySelectionDisplay(obj); | |
| 423 | |
| 424 XtCallCallbackList(w, obj->inputConv.textchangecallback, | |
| 425 (XtPointer)NULL); | |
| 426 } | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 static ICString * | |
| 432 GetMode(w) | |
| 433 Widget w; | |
| 434 { | |
| 435 rubyEUCtoICString (ruby_eval ("kanjiConv.modeline"), ruby_tmp_seg); | |
| 436 ruby_tmp_seg->attr = ICAttrNormalString; | |
| 437 return ruby_tmp_seg; | |
| 438 } | |
| 439 | |
| 440 static int | |
| 441 CursorPos(w, nsegp, ncharp) | |
| 442 Widget w; | |
| 443 Cardinal *nsegp; | |
| 444 Cardinal *ncharp; | |
| 445 { | |
| 446 /* | |
| 447 return value : =0 corsor ON / =1 OFF | |
| 448 nseg: >0 segment index / =0 no segment | |
| 449 nchar: number of chars; 'テs' == 2 | |
| 450 */ | |
| 451 *ncharp = NUM2INT(ruby_eval("kanjiConv.input.cursor")); | |
| 452 *nsegp = 0; | |
| 453 | |
| 454 /* printf ("CursorPos: nseg=%d, nchar=%d\n", (int)*nsegp, (int)*ncharp); */ | |
| 455 | |
| 456 return (*ncharp == 0) ? 1: 0; | |
| 457 } | |
| 458 | |
| 459 static int | |
| 460 NumSegments(w) | |
| 461 Widget w; | |
| 462 { | |
| 463 int num; | |
| 464 printf ("NumSegments\n"); | |
| 465 num = NUM2INT(ruby_eval("kanjiConv.segment_length")); | |
| 466 /* printf ("NumSegments: num=%d\n", num); */ | |
| 467 return num; | |
| 468 } | |
| 469 | |
| 470 static ICString * | |
| 471 GetSegment(w, n) | |
| 472 Widget w; | |
| 473 Cardinal n; | |
| 474 { | |
| 475 RubyObject obj = (RubyObject)w; | |
| 476 static ICString seg; | |
| 477 | |
| 478 printf("GetSegment\n"); | |
| 479 | |
| 480 rubyEUCtoICString (ruby_evalf ("kanjiConv.segment_word(%d)", n), | |
| 481 ruby_tmp_seg); | |
| 482 if (ruby_evalf("kanjiConv.segment_status(%d) == :highlight", n) == Qtrue) { | |
| 483 ruby_tmp_seg->attr = ICAttrConverted | ICAttrCurrentSegment; | |
| 484 } else { | |
| 485 ruby_tmp_seg->attr = ICAttrNotConverted; | |
| 486 } | |
| 487 /* ICAttrConverted | ICAttrCurrentSegment : Inversion. */ | |
| 488 /* |= ICAttrChanged : Non-Underline. */ | |
| 489 return ruby_tmp_seg; | |
| 490 } | |
| 491 | |
| 492 /* ARGSUSED */ | |
| 493 static int | |
| 494 CompareSegment(w, seg1, seg2, n) | |
| 495 Widget w; | |
| 496 ICString *seg1; | |
| 497 ICString *seg2; | |
| 498 Cardinal *n; | |
| 499 { | |
| 500 wchar *p, *q; | |
| 501 int len, nsame; | |
| 502 int result = 0; | |
| 503 | |
| 504 printf("CompareSegment\n"); | |
| 505 | |
| 506 if (seg1->attr != seg2->attr) result |= ICAttrChanged; | |
| 507 | |
| 508 len = seg1->nchars > seg2->nchars ? seg2->nchars : seg1->nchars; | |
| 509 nsame = 0; | |
| 510 p = (wchar *)seg1->data; | |
| 511 q = (wchar *)seg2->data; | |
| 512 while (nsame < len && *p++ == *q++) nsame++; | |
| 513 | |
| 514 if (nsame != len || len != seg1->nchars || len != seg2->nchars) | |
| 515 result |= ICStringChanged; | |
| 516 | |
| 517 if (n) *n = nsame; | |
| 518 | |
| 519 return result; | |
| 520 } | |
| 521 | |
| 522 static ICString * | |
| 523 GetItemList(w, n) | |
| 524 Widget w; | |
| 525 Cardinal *n; | |
| 526 { | |
| 527 RubyObject obj = (RubyObject)w; | |
| 528 | |
| 529 printf("GetItemList\n"); | |
| 530 rubyEUCArraytoICStringArray(ruby_eval("kanjiConv.cand_list"), | |
| 531 ruby_cand_list); | |
| 532 *n = ruby_cand_list->length; | |
| 533 return ruby_cand_list->array; | |
| 534 } | |
| 535 | |
| 536 static int | |
| 537 SelectItem(w, n) | |
| 538 Widget w; | |
| 539 int n; | |
| 540 { | |
| 541 | |
| 542 RubyObject obj = (RubyObject)w; | |
| 543 printf ("selectItem\n"); | |
| 544 ruby_evalf("kanjiConv.selection_fix (%d)", n); | |
| 545 /* FIXME: Create new fix function in the feature. */ | |
| 546 fix (obj); | |
| 547 XtCallCallbackList(w, obj->inputConv.textchangecallback, | |
| 548 (XtPointer)NULL); | |
| 549 return 0; | |
| 550 } | |
| 551 | |
| 552 static int | |
| 553 ConvertedString(w, encoding, format, length, string) | |
| 554 Widget w; | |
| 555 Atom *encoding; | |
| 556 int *format; | |
| 557 int *length; | |
| 558 XtPointer *string; | |
| 559 { | |
| 560 RubyObject obj = (RubyObject)w; | |
| 561 wchar *wbuf, *wp; | |
| 562 int len, wlen; | |
| 563 extern int convJWStoCT(); | |
| 564 | |
| 565 /* メモリの解放は自動的に行われる. str を直接書き換えてはいけない. */ | |
| 566 unsigned char *str = STR2CSTR(ruby_eval("kanjiConv.value_fixed")); | |
| 567 | |
| 568 printf("ConvertedString\n"); | |
| 569 | |
| 570 wbuf = XtMalloc((strlen(str) + 1) * sizeof(wchar)); | |
| 571 euc2wcs(str, strlen(str), wbuf); | |
| 572 wlen = wstrlen(wbuf); | |
| 573 | |
| 574 /* | |
| 575 * Ruby オブジェクトは COMPOUND_TEXT エンコーディングしかサポートしない | |
| 576 * COMPOUND_TEXT に変換する | |
| 577 */ | |
| 578 *encoding = XA_COMPOUND_TEXT(XtDisplayOfObject((Widget)obj)); | |
| 579 *format = 8; | |
| 580 | |
| 581 /* COMPOUND_TEXT は \r が送れないので \n に変換しておく */ | |
| 582 for (wp = wbuf; *wp != 0; wp++) { | |
| 583 if (*wp == '\r') *wp = '\n'; | |
| 584 } | |
| 585 | |
| 586 *length = len = convJWStoCT(wbuf, (unsigned char *)NULL, 0); | |
| 587 *string = XtMalloc(len + 1); | |
| 588 (void)convJWStoCT(wbuf, (unsigned char *)*string, 0); | |
| 589 | |
| 590 XtFree(wbuf); | |
| 591 | |
| 592 return 0; | |
| 593 } | |
| 594 | |
| 595 static int | |
| 596 ClearConversion(w) | |
| 597 Widget w; | |
| 598 { | |
| 599 ruby_eval ("kanjiConv.clear"); | |
| 600 return 0; | |
| 601 } | |
| 602 | |
| 603 static ICString * | |
| 604 GetAuxSegments(w, n, ns, nc) | |
| 605 Widget w; | |
| 606 Cardinal *n, *ns, *nc; | |
| 607 { | |
| 608 /* I'm not sure about this function's purpose. */ | |
| 609 /* n => ngseg ; number of segments (1 <= n <= 3). */ | |
| 610 /* ns => nseg ; index of reversed segment (0 or 1). */ | |
| 611 /* nc => nchar ; length of current segment??? */ | |
| 612 printf ("GetAuxSegments\n"); | |
| 613 | |
| 614 if ((ruby_eval ("kanjiConv.dialog.visible") == Qtrue || | |
| 615 ruby_eval ("kanjiConv.dialog.call_open") == Qtrue) && | |
| 616 ruby_eval ("kanjiConv.dialog.text.length > 0") == Qtrue) { | |
| 617 if (n) { | |
| 618 *n = 1; | |
| 619 } | |
| 620 | |
| 621 if (ns) { | |
| 622 *ns = 0; | |
| 623 } | |
| 624 | |
| 625 rubyEUCtoICString (ruby_eval ("kanjiConv.dialog.text"), | |
| 626 ruby_tmp_seg); | |
| 627 ruby_tmp_seg->attr = ICAttrConverted; | |
| 628 | |
| 629 if (nc) { | |
| 630 *nc = ruby_tmp_seg->nchars; | |
| 631 } | |
| 632 | |
| 633 return ruby_tmp_seg; | |
| 634 } else { | |
| 635 if (n) { | |
| 636 *n = 0; | |
| 637 } | |
| 638 if (ns) { | |
| 639 *ns = 0; | |
| 640 } | |
| 641 if (nc) { | |
| 642 *nc = 0; | |
| 643 } | |
| 644 return NULL; | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 /* ARGSUSED */ | |
| 649 static void | |
| 650 Initialize(req, new, args, num_args) | |
| 651 Widget req; | |
| 652 Widget new; | |
| 653 ArgList args; | |
| 654 Cardinal *num_args; | |
| 655 { | |
| 656 RubyObject obj = (RubyObject)new; | |
| 657 | |
| 658 obj->ruby.selectionending = False; | |
| 659 obj->ruby.textchanged = False; | |
| 660 obj->ruby.symbollist = SymbolList; | |
| 661 obj->ruby.numsymbols = NumSymbols; | |
| 662 obj->ruby.cursymbol = 0; | |
| 663 obj->ruby.candlist = NULL; | |
| 664 obj->ruby.candlistsize = 0; | |
| 665 obj->ruby.numcand = 0; | |
| 666 obj->ruby.lastTextLengthIsZero = False; | |
| 667 | |
| 668 addObject(obj); | |
| 669 } | |
| 670 | |
| 671 static int | |
| 672 bell() | |
| 673 { | |
| 674 if (displaybell) { | |
| 675 XBell(displaybell, 0); | |
| 676 } | |
| 677 return 0; | |
| 678 } | |
| 679 | |
| 680 static int nRubyContexts = 0; | |
| 681 | |
| 682 static void | |
| 683 Destroy(w) | |
| 684 Widget w; | |
| 685 { | |
| 686 RubyObject obj = (RubyObject)w; | |
| 687 wchar buf[512]; | |
| 688 int i; | |
| 689 | |
| 690 if (obj->ruby.candlist) { | |
| 691 for (i = 0 ; i < obj->ruby.candlistsize ; i++) { | |
| 692 if ((obj->ruby.candlist + i)->data) { | |
| 693 XtFree((obj->ruby.candlist + i)->data); | |
| 694 } | |
| 695 } | |
| 696 | |
| 697 XtFree((char *)obj->ruby.candlist); | |
| 698 } | |
| 699 | |
| 700 /* Ruby 用終了処理をここに書く */ | |
| 701 | |
| 702 deleteObject(obj); | |
| 703 } | |
| 704 | |
| 705 static Boolean | |
| 706 SetValues(cur, req, wid, args, num_args) | |
| 707 Widget cur; | |
| 708 Widget req; | |
| 709 Widget wid; | |
| 710 ArgList args; | |
| 711 Cardinal *num_args; | |
| 712 /* ARGSUSED */ | |
| 713 { | |
| 714 RubyObject old = (RubyObject)cur; | |
| 715 RubyObject new = (RubyObject)wid; | |
| 716 | |
| 717 return False; | |
| 718 } | |
| 719 | |
| 720 static void | |
| 721 fix(obj) | |
| 722 RubyObject obj; | |
| 723 { | |
| 724 /* 確定の処理 */ | |
| 725 XtCallCallbackList((Widget)obj, obj->inputConv.fixcallback, | |
| 726 (XtPointer)NULL); | |
| 727 ruby_eval("kanjiConv.fix_end"); | |
| 728 } | |
| 729 | |
| 730 static void | |
| 731 convend(obj) | |
| 732 RubyObject obj; | |
| 733 { | |
| 734 XtCallCallbackList((Widget)obj, obj->inputConv.endcallback, | |
| 735 (XtPointer)NULL); | |
| 736 ruby_eval("kanjiConv.reset"); | |
| 737 } | |
| 738 | |
| 739 /* | |
| 740 * keeping list of objects | |
| 741 */ | |
| 742 typedef struct _oblist_ { | |
| 743 RubyObject obj; | |
| 744 struct _oblist_ *next; | |
| 745 } ObjRec; | |
| 746 | |
| 747 static ObjRec *ObjList = NULL; | |
| 748 | |
| 749 static void | |
| 750 addObject(obj) | |
| 751 RubyObject obj; | |
| 752 { | |
| 753 ObjRec *objp = XtNew(ObjRec); | |
| 754 | |
| 755 objp->obj = obj; | |
| 756 objp->next = ObjList; | |
| 757 ObjList = objp; | |
| 758 } | |
| 759 | |
| 760 static void | |
| 761 deleteObject(obj) | |
| 762 RubyObject obj; | |
| 763 { | |
| 764 ObjRec *objp, *objp0; | |
| 765 | |
| 766 for (objp0 = NULL, objp = ObjList; | |
| 767 objp != NULL; | |
| 768 objp0 = objp, objp = objp->next) { | |
| 769 if (objp->obj == obj) { | |
| 770 if (objp0 == NULL) { | |
| 771 ObjList = objp->next; | |
| 772 } else { | |
| 773 objp0->next = objp->next; | |
| 774 } | |
| 775 XtFree((char *)objp); | |
| 776 return; | |
| 777 } | |
| 778 } | |
| 779 } | |
| 780 | |
| 781 /* ARGSUSED */ | |
| 782 static int | |
| 783 GetTriggerKeys(w, keys_return) | |
| 784 Widget w; | |
| 785 ICTriggerKey **keys_return; | |
| 786 { | |
| 787 /* 何をする関数なのか不明 (komatsu) */ | |
| 788 | |
| 789 *keys_return = NULL; | |
| 790 return 0; | |
| 791 } | |
| 792 | |
| 793 /* ARGSUSED */ | |
| 794 | |
| 795 | |
| 796 /* | |
| 797 * int ICGetPreeditString(Widget object, int segn, int offset, | |
| 798 * Atom *encoding, int *format, | |
| 799 * int *length, XtPointer *string) | |
| 800 * 変換途中の segn 番目の セグメントの offset 文字からのテキストを | |
| 801 * string に返す | |
| 802 * encoding には、テキストのエンコーディングを指定しておく | |
| 803 * ただしこれは単なるリクエストであって、変換オブジェクトは | |
| 804 * 別のエンコーディングで返してもよい | |
| 805 * encoding には実際のエンコーディングが返される | |
| 806 * 変換オブジェクトは少なくとも COMPOUND_TEXT エンコーディングは | |
| 807 * サポートしなくてはならない | |
| 808 * format には 8/16/32 のいずれか、length は string のエレメント数が | |
| 809 * それぞれ返される | |
| 810 * テキストの領域は malloc されているのでこの関数を呼んだ側で | |
| 811 * free しなければならない | |
| 812 * 変換テキストがない時やエラーの場合には -1、そうでなければ 0 が | |
| 813 * 関数の値として返される | |
| 814 */ | |
| 815 /* まだ, 未検討 (komatsu) */ | |
| 816 static int | |
| 817 PreeditString(w, segn, offset, encoding, format, length, string) | |
| 818 Widget w; | |
| 819 int segn; | |
| 820 int offset; | |
| 821 Atom *encoding; | |
| 822 int *format; | |
| 823 int *length; | |
| 824 XtPointer *string; | |
| 825 { | |
| 826 RubyObject obj = (RubyObject)w; | |
| 827 | |
| 828 return -1; | |
| 829 } | |
| 830 | |
| 831 /* ARGSUSED */ | |
| 832 static int | |
| 833 StatusString(w, encoding, format, length, string, nchars) | |
| 834 Widget w; | |
| 835 Atom *encoding; | |
| 836 int *format; | |
| 837 int *length; | |
| 838 XtPointer *string; | |
| 839 int *nchars; | |
| 840 { | |
| 841 ICString *seg; | |
| 842 wchar *wbuf, *wp; | |
| 843 int len, wlen; | |
| 844 extern int convJWStoCT(); | |
| 845 | |
| 846 printf("StatusString\n"); | |
| 847 | |
| 848 seg = GetMode(w); | |
| 849 if (seg == NULL) { | |
| 850 *length = *nchars = 0; | |
| 851 return -1; | |
| 852 } | |
| 853 | |
| 854 wlen = seg->nchars; | |
| 855 if (wlen <= 0) { | |
| 856 *length = *nchars = 0; | |
| 857 return -1; | |
| 858 } | |
| 859 | |
| 860 /* | |
| 861 * data に入っている変換テキストは null ターミネートされていないかも | |
| 862 * しれないので、まずコピーして null ターミネートする | |
| 863 */ | |
| 864 wbuf = (wchar *)XtMalloc((wlen + 1) * sizeof(wchar)); | |
| 865 (void)bcopy(seg->data, (char *)wbuf, sizeof(wchar) * wlen); | |
| 866 wbuf[wlen] = 0; | |
| 867 | |
| 868 /* | |
| 869 * Ruby オブジェクトは COMPOUND_TEXT エンコーディングしかサポートしない | |
| 870 * COMPOUND_TEXT に変換する | |
| 871 */ | |
| 872 *encoding = XA_COMPOUND_TEXT(XtDisplayOfObject(w)); | |
| 873 *format = 8; | |
| 874 | |
| 875 /* COMPOUND_TEXT は \r が送れないので \n に変換しておく */ | |
| 876 for (wp = wbuf; *wp != 0; wp++) { | |
| 877 if (*wp == '\r') *wp = '\n'; | |
| 878 } | |
| 879 | |
| 880 *length = len = convJWStoCT(wbuf, (unsigned char *)NULL, 0); | |
| 881 *string = XtMalloc(len + 1); | |
| 882 (void)convJWStoCT(wbuf, (unsigned char *)*string, 0); | |
| 883 *nchars = seg->nchars; | |
| 884 | |
| 885 /* wbuf を free しておく */ | |
| 886 XtFree((char *)wbuf); | |
| 887 | |
| 888 return 0; | |
| 889 } | |
| 890 | |
| 891 /*** | |
| 892 ic_str->nbytes が 0 の場合 ic_str->data は自動的に確保 (XtMalloc) される. | |
| 893 0 以外の場合 ic_str->data は (XtRealloc) される. | |
| 894 */ | |
| 895 static void | |
| 896 rubyEUCtoICString(ruby_str, ic_str) | |
| 897 VALUE ruby_str; | |
| 898 ICString *ic_str; | |
| 899 { | |
| 900 unsigned char *euc_str; | |
| 901 int euc_len; | |
| 902 wchar *wc_str; | |
| 903 | |
| 904 /* printf("String_Len: %d\n", RSTRING(ruby_str)->len); */ | |
| 905 | |
| 906 euc_str = STR2CSTR(ruby_str); | |
| 907 euc_len = strlen(euc_str); | |
| 908 | |
| 909 if (ic_str->nbytes == 0) { | |
| 910 wc_str = (wchar *)XtMalloc((euc_len + 1) * sizeof(wchar)); | |
| 911 } else { | |
| 912 wc_str = (wchar *)XtRealloc(ic_str->data, (euc_len + 1) * sizeof(wchar)); | |
| 913 } | |
| 914 euc2wcs(euc_str, euc_len, wc_str); | |
| 915 | |
| 916 ic_str->data = (char *)wc_str; | |
| 917 ic_str->nchars = wstrlen(wc_str); | |
| 918 ic_str->nbytes = (euc_len + 1) * sizeof(wchar); | |
| 919 ic_str->attr = ICAttrNotConverted; | |
| 920 } | |
| 921 | |
| 922 static void | |
| 923 rubyEUCArraytoICStringArray(ruby_str_ary, ic_str_ary) | |
| 924 VALUE ruby_str_ary; | |
| 925 ICStringArray *ic_str_ary; | |
| 926 { | |
| 927 VALUE ruby_str; | |
| 928 int i, array_length; | |
| 929 ICString ic_str; | |
| 930 | |
| 931 array_length = RARRAY(ruby_str_ary)->len; | |
| 932 if ((ic_str_ary->length) < array_length) { | |
| 933 ic_str_ary->array = (ICString *)XtRealloc((char *)ic_str_ary->array, | |
| 934 array_length * sizeof(ICString)); | |
| 935 for (i = ic_str_ary->length; i < array_length; i++) { | |
| 936 /* ic_str_ary->array[i] = (ICString *)XtMalloc(1 * sizeof(ICString)); */ | |
| 937 ic_str_ary->array[i].nbytes = 0; | |
| 938 } | |
| 939 } | |
| 940 ic_str_ary->length = array_length; | |
| 941 | |
| 942 /* printf("array_length: %d\n", array_length); */ | |
| 943 | |
| 944 for (i = 0; i < array_length; i++) { | |
| 945 rubyEUCtoICString(rb_ary_entry(ruby_str_ary, i), &(ic_str_ary->array[i])); | |
| 946 } | |
| 947 } |
