File: unikey_instance.cpp

package info (click to toggle)
scim-unikey 0.3.1%2Bdebian-3.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,396 kB
  • ctags: 1,262
  • sloc: sh: 10,650; cpp: 7,490; makefile: 169; sed: 16
file content (869 lines) | stat: -rw-r--r-- 26,033 bytes parent folder | download | duplicates (4)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/**
   Scim-Unikey Input Method

   Copyright (C) 2008-2009 Ubuntu-VN <http://www.ubuntu-vn.org>
   Author: Le Quoc Tuan <mr.lequoctuan@gmail.com>
   Home: http://scim-unikey.googlecode.com
   License: GNU LESSER GENERAL PUBLIC LICENSE v2.1
*/

#define Uses_SCIM_IMENGINE
#define Uses_SCIM_ICONV
#define Uses_SCIM_CONFIG_BASE
#define Uses_SCIM_CONFIG_PATH

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <libintl.h>
#define _(String) dgettext(PACKAGE_NAME,String)

#include <scim.h>
#include "scim_unikey_const.h"
#include "utils.h"
#include "im_engine.h"

#include <string.h>
#include "unikey.h"
#include "vnconv.h"

static unsigned char WordBreakSyms[] =
{
    ',', ';', ':', '.', '!', '?', ' ', '<', '>', '=', '+', '-', '*', '/', '_', '~',
    '`', '@', '#', '$', '%', '^', '&', '(', ')', '{', '}', '[', ']', '|', '\"', '\'', '\\',
};

static unsigned char WordAutoCommit[] =
{
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'b', 'c', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n',
    'p', 'q', 'r', 's', 't', 'v', 'x', 'z',
    'B', 'C', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N',
    'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z'
};
const String          Unikey_IMNames[]    = {"Telex", "Vni", "STelex", "STelex2"};
const UkInputMethod   Unikey_IM[]         = {UkTelex, UkVni, UkSimpleTelex, UkSimpleTelex2};
const unsigned int    NUM_INPUTMETHOD     = sizeof(Unikey_IM)/sizeof(Unikey_IM[0]);

const String          Unikey_OCNames[]    = {"Unicode",
                                             "TCVN3",
                                             "VNI Win",
                                             "VIQR",
                                             "CString",
                                             "NCR Decimal",
                                             "NCR Hex"};
const unsigned int    Unikey_OC[]         = {CONV_CHARSET_XUTF8,
                                             CONV_CHARSET_TCVN3,
                                             CONV_CHARSET_VNIWIN,
                                             CONV_CHARSET_VIQR,
                                             CONV_CHARSET_UNI_CSTRING,
                                             CONV_CHARSET_UNIREF,
                                             CONV_CHARSET_UNIREF_HEX};
const unsigned int    NUM_OUTPUTCHARSET   = sizeof(Unikey_OC)/sizeof(Unikey_OC[0]);

extern ConfigPointer __config;

/*************************************************
**************** Unikey Instance *****************
*************************************************/
UnikeyInstance::UnikeyInstance(UnikeyFactory *factory, const String& encoding, int id)
        :IMEngineInstanceBase(factory, encoding, id)
{
    static bool t, o;  //temp variable

    CreateDefaultUnikeyOptions(&m_ukopt);

    /* Read config
       if can't read config, set it to default value*/

    t = __config->read(SCIM_IMENGINE_UNIKEY_INPUTMETHOD, &m_im);
    if (!t) m_im = 0;

    t = __config->read(SCIM_IMENGINE_UNIKEY_OUTPUTCHARSET, &m_oc);
    if (!t) m_oc = 0;

    t = __config->read(SCIM_IMENGINE_UNIKEY_PROCESSWATWORDBEGIN, &o);
    m_process_w_AtBeginWord = t?o:SCIM_IMENGINE_UNIKEY_PROCESSWATWORDBEGIN_DEF;

// Unikey Options
    t = __config->read(SCIM_IMENGINE_UNIKEY_FREEMARKING, &o);
    m_ukopt.freeMarking = t?o:SCIM_IMENGINE_UNIKEY_FREEMARKING_DEF;

    t = __config->read(SCIM_IMENGINE_UNIKEY_MODERNSTYLE, &o);
    m_ukopt.modernStyle = t?o:SCIM_IMENGINE_UNIKEY_MODERNSTYLE_DEF;

    t = __config->read(SCIM_IMENGINE_UNIKEY_MACROENABLED, &o);
    m_ukopt.macroEnabled = t?o:SCIM_IMENGINE_UNIKEY_MACROENABLED_DEF;

    t = __config->read(SCIM_IMENGINE_UNIKEY_SPELLCHECKENABLED, &o);
    m_ukopt.spellCheckEnabled = t?o:SCIM_IMENGINE_UNIKEY_SPELLCHECKENABLED_DEF;

    t = __config->read(SCIM_IMENGINE_UNIKEY_AUTONONVNRESTORE, &o);
    m_ukopt.autoNonVnRestore = t?o:SCIM_IMENGINE_UNIKEY_AUTONONVNRESTORE_DEF;

    UnikeySetOptions(&m_ukopt);

    if (m_ukopt.macroEnabled)
    {
        UnikeyLoadMacroTable(getMacroFile());
    }
};

UnikeyInstance::~UnikeyInstance()
{
};

void UnikeyInstance::reset()
{
    UnikeyResetBuf();

    m_lastkey_with_shift = false;
}

void UnikeyInstance::focus_in()
{
    UnikeySetInputMethod(Unikey_IM[m_im]);
    UnikeySetOutputCharset(Unikey_OC[m_oc]);
    UnikeySetOptions(&m_ukopt);
}

PropertyList UnikeyInstance::CreatePropertyList()
{
    PropertyList props;
    uint i;

    Property prop("", "", "", "");

// input method
    prop.set_key("/Unikey/InputMethod");
    prop.set_label(Unikey_IMNames[m_im]);
    prop.set_icon("");
    prop.set_tip(_("Choose input method"));
    props.push_back(prop);

    for (i=0; i<NUM_INPUTMETHOD; i++)
    {
        prop.set_key(String("/Unikey/InputMethod/") + Unikey_IMNames[i] + String((uint)m_im==i?"-Checked":""));
        prop.set_label(Unikey_IMNames[i]);
        prop.set_icon((uint)m_im==i?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
        prop.set_tip("");
        props.push_back(prop);
    }

// output charset

    prop.set_key("/Unikey/OutputCharset");
    prop.set_label(Unikey_OCNames[m_oc]);
    prop.set_icon("");
    prop.set_tip(_("Choose output charset"));
    props.push_back(prop);

    for (i=0; i<NUM_OUTPUTCHARSET; i++)
    {
        prop.set_key(String("/Unikey/OutputCharset/") + Unikey_OCNames[i] + String((uint)m_oc==i?"-Checked":""));
        prop.set_label(Unikey_OCNames[i]);
        prop.set_icon((uint)m_oc==i?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
        prop.set_tip("");
        props.push_back(prop);
    }

// unikey options
    // menu
    prop.set_key("/Unikey/Options");
    prop.set_label(_("Options"));
    prop.set_icon(SCIM_ICONDIR SCIM_UNIKEY_ICON_CONFIGURE);
    prop.set_tip(_("Configure Unikey here"));
    props.push_back(prop);

    // spellcheck
    prop.set_key(m_ukopt.spellCheckEnabled?
                 "/Unikey/Options/SpellCheck/Disable":"/Unikey/Options/SpellCheck/Enable");
    prop.set_label(_("Enable spell check"));
    prop.set_icon(m_ukopt.spellCheckEnabled?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
    prop.set_tip(_("If enable, you can decrease mistake when typing"));
    props.push_back(prop);
    
    // auto restore keystroke
    prop.set_key(m_ukopt.autoNonVnRestore?
                 "/Unikey/Options/AutoRestoreKeys/Disable":"/Unikey/Options/AutoRestoreKeys/Enable");
    prop.set_label(_("Auto restore keys with invalid words"));
    prop.set_icon(m_ukopt.autoNonVnRestore?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
    prop.set_tip(_("When typing a word not in Vietnamese,\n"
                   "it will auto restore keystroke into original"));
    props.push_back(prop);

    // modern style
    prop.set_key(m_ukopt.modernStyle?
                 "/Unikey/Options/ModernStyle/Disable":"/Unikey/Options/ModernStyle/Enable");
    prop.set_label(_("Use oà, uý (instead of òa, úy)"));
    prop.set_icon(m_ukopt.modernStyle?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
    prop.set_tip("");
    props.push_back(prop);

    // freeMarking
    prop.set_key(m_ukopt.freeMarking?
                 "/Unikey/Options/FreeMarking/Disable":"/Unikey/Options/FreeMarking/Enable");
    prop.set_label(_("Allow type with more freedom"));
    prop.set_icon(m_ukopt.freeMarking?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
    prop.set_tip("");
    props.push_back(prop);

    // macro
    prop.set_key(m_ukopt.macroEnabled?
                 "/Unikey/Options/EnabledMacro/Disable":"/Unikey/Options/EnabledMacro/Enable");
    prop.set_label(_("Enable Macro"));
    prop.set_icon(m_ukopt.macroEnabled?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
    prop.set_tip("");
    props.push_back(prop);

    // process w at begin
    prop.set_key(m_process_w_AtBeginWord?
                 "/Unikey/Options/ProcessWAtBegin/Disable":"/Unikey/Options/ProcessWAtBegin/Enable");
    prop.set_label(_("Process W at word begin"));
    prop.set_icon(m_process_w_AtBeginWord?SCIM_ICONDIR SCIM_UNIKEY_ICON_CHECK:"");
    prop.set_tip(_("If enable, type W at begin\n"
                   "of word will change to Ư."));
    props.push_back(prop);

    // config gui
    prop.set_key("/Unikey/Options/RunSetup");
    prop.set_label(_("Launch Unikey Setup"));
    prop.set_icon(SCIM_ICONDIR SCIM_UNIKEY_ICON_MAIN);
    prop.set_tip("");
    props.push_back(prop);

    return props;
}

void UnikeyInstance::trigger_property(const String &property)
{
    bool change = false;
    uint i;

// input method
    if (!property.compare(0, strlen("/Unikey/InputMethod/"), "/Unikey/InputMethod/"))
    {
        for (i=0; i<NUM_INPUTMETHOD; i++)
            if (!property.compare(strlen("/Unikey/InputMethod/"),
                                  property.length() - strlen("/Unikey/InputMethod/"),
                                  Unikey_IMNames[i]))
            {
                m_im = i;
                __config->write(SCIM_IMENGINE_UNIKEY_INPUTMETHOD, m_im);
                change = true;
                break;
            }
    }

// output charset
    else if (!property.compare(0, strlen("/Unikey/OutputCharset/"), "/Unikey/OutputCharset/"))
    {
        for (i=0; i<NUM_OUTPUTCHARSET; i++)
            if (!property.compare(strlen("/Unikey/OutputCharset/"),
                                  property.length() - strlen("/Unikey/OutputCharset/"),
                                  Unikey_OCNames[i]))
            {
                m_oc = i;
                __config->write(SCIM_IMENGINE_UNIKEY_OUTPUTCHARSET, m_oc);
                change = true;
                break;
            }
    }

// spellcheck
    else if (property == "/Unikey/Options/SpellCheck/Enable")
    {
        m_ukopt.spellCheckEnabled = true;
        __config->write(SCIM_IMENGINE_UNIKEY_SPELLCHECKENABLED, true);
        change = true;
    }
    else if (property == "/Unikey/Options/SpellCheck/Disable")
    {
        m_ukopt.spellCheckEnabled = false;
        __config->write(SCIM_IMENGINE_UNIKEY_SPELLCHECKENABLED, false);
        change = true;
    }

// auto restore keystroke
    else if (property == "/Unikey/Options/AutoRestoreKeys/Enable")
    {
        m_ukopt.autoNonVnRestore = true;
        __config->write(SCIM_IMENGINE_UNIKEY_AUTONONVNRESTORE, true);
        change = true;
    }
    else if (property == "/Unikey/Options/AutoRestoreKeys/Disable")
    {
        m_ukopt.autoNonVnRestore = false;
        __config->write(SCIM_IMENGINE_UNIKEY_AUTONONVNRESTORE, false);
        change = true;
    }

// modern style
    else if (property == "/Unikey/Options/ModernStyle/Enable")
    {
        m_ukopt.modernStyle = true;
        __config->write(SCIM_IMENGINE_UNIKEY_MODERNSTYLE, true);
        change = true;
    }
    else if (property == "/Unikey/Options/ModernStyle/Disable")
    {
        m_ukopt.modernStyle = false;
        __config->write(SCIM_IMENGINE_UNIKEY_MODERNSTYLE, false);
        change = true;
    }

// free Marking
    else if (property == "/Unikey/Options/FreeMarking/Enable")
    {
        m_ukopt.freeMarking = true;
        __config->write(SCIM_IMENGINE_UNIKEY_FREEMARKING, true);
        change = true;
    }
    else if (property == "/Unikey/Options/FreeMarking/Disable")
    {
        m_ukopt.freeMarking = false;
        __config->write(SCIM_IMENGINE_UNIKEY_FREEMARKING, false);
        change = true;
    }
// macro 
    else if (property == "/Unikey/Options/EnabledMacro/Enable")
    {
        m_ukopt.macroEnabled = true;
        UnikeyLoadMacroTable(getMacroFile());
        __config->write(SCIM_IMENGINE_UNIKEY_MACROENABLED, true);
        change = true;
    }
    else if (property == "/Unikey/Options/EnabledMacro/Disable")
    {
        m_ukopt.macroEnabled = false;
        __config->write(SCIM_IMENGINE_UNIKEY_MACROENABLED, false);
        change = true;
    }

// process w at begin
    else if (property == "/Unikey/Options/ProcessWAtBegin/Enable")
    {
        m_process_w_AtBeginWord = true;
        __config->write(SCIM_IMENGINE_UNIKEY_PROCESSWATWORDBEGIN, true);
        change = true;
    }
    else if (property == "/Unikey/Options/ProcessWAtBegin/Disable")
    {
        m_process_w_AtBeginWord = false;
        __config->write(SCIM_IMENGINE_UNIKEY_PROCESSWATWORDBEGIN, false);
        change = true;
    }

// run setup
    else if (property == "/Unikey/Options/RunSetup")
    {
        system(LIBEXECDIR "/scim-setup-unikey &");
    }

    if (change)
    {
        __config->flush();
        focus_out();
        focus_in();
    }
}

/*********************************************************
**************** Unikey Instance Preedit *****************
*********************************************************/

UnikeyInstancePreedit::UnikeyInstancePreedit(UnikeyFactory *factory, const String &encoding, int id)
    :UnikeyInstance(factory, encoding, id),
     m_auto_commit(false)
{
    reset();
}

UnikeyInstancePreedit::~UnikeyInstancePreedit()
{
}

void UnikeyInstancePreedit::focus_in()
{
    UnikeyInstance::focus_in();
    register_properties(CreatePropertyList());
}

void UnikeyInstancePreedit::focus_out()
{
    reset();
}

void UnikeyInstancePreedit::reset()
{
    UnikeyInstance::reset();

    if (m_preeditstring.length())
    {
        commit_string(m_preeditstring);
        hide_preedit_string();
        m_preeditstring.clear();
    }
    m_auto_commit = false;
}

void UnikeyInstancePreedit::unikey_update_preedit_string(const WideString s, const bool visible)
{
    AttributeList attlist;
    Attribute att;

    // underline preedit string
    att = Attribute(0, s.length(), SCIM_ATTR_DECORATE, SCIM_ATTR_DECORATE_UNDERLINE);
    attlist.push_back(att);

    if (m_ukopt.spellCheckEnabled==1 && UnikeyLastWordIsNonVn())
    {
        // red preedit string
        att = Attribute(0, s.length(), SCIM_ATTR_FOREGROUND, 0xff0000);
        attlist.push_back(att);
    }

    update_preedit_string(s, attlist);
    update_preedit_caret(s.length());

    if (visible == true)
    {
        show_preedit_string();
    }
    else
    {
        hide_preedit_string();
    }
}

bool UnikeyInstancePreedit::process_key_event(const KeyEvent& key)
{
    bool tmp;

    tmp = unikey_process_key_event(key);

    if ((key.code >= SCIM_KEY_space && key.code <= SCIM_KEY_asciitilde)
        || (key.code >= SCIM_KEY_KP_Multiply && key.code <= SCIM_KEY_KP_9))
    {
        m_lastkey_with_shift = key.is_shift_down();
    }
    else
    {
        m_lastkey_with_shift = false;
    }

    return tmp;
}

bool UnikeyInstancePreedit::unikey_process_key_event(const KeyEvent& key)
{
    static uint i;

    if (key.code == SCIM_KEY_Tab
        || key.mask & SCIM_KEY_ControlMask
        || key.mask & SCIM_KEY_AltMask)
    {
        if (m_preeditstring.length())
        {
            commit_string(m_preeditstring);
            hide_preedit_string();
            m_preeditstring.clear();
        }
        reset();
        return false;
    }

    if (key.is_key_release())
        return true;

    if (key.code == SCIM_KEY_Tab
             || key.code == SCIM_KEY_Return
             || key.code == SCIM_KEY_Delete
             || key.code == SCIM_KEY_KP_Enter
             || (key.code >= SCIM_KEY_Home && key.code <= SCIM_KEY_Insert)
             || (key.code >= SCIM_KEY_KP_Home && key.code <= SCIM_KEY_KP_Delete))
    {
        if (m_preeditstring.length())
        {
            commit_string(m_preeditstring);
            hide_preedit_string();
            m_preeditstring.clear();
        }

        reset();
        return false;
    }

    else if (key.code >= SCIM_KEY_Shift_L && key.code <= SCIM_KEY_Hyper_R)
        return false;

    else if (key.code == SCIM_KEY_BackSpace)
    {
        UnikeyBackspacePress();     // process Backspace

        if (UnikeyBackspaces==0 || m_preeditstring.empty())    // if Unikey tell no Backspace
        {
            reset();
            return false;  // Return Backspace to client
        }
        else
        {
            static int len;

            len = m_preeditstring.length();
            if (len <= UnikeyBackspaces)
            {
                m_preeditstring.clear();
                hide_preedit_string();
                m_auto_commit = true;
            }
            else
            {
                m_preeditstring.erase(len - UnikeyBackspaces, UnikeyBackspaces);
                unikey_update_preedit_string(m_preeditstring, true);
            }

            // append key that need change tone pos after press backspace
            if (UnikeyBufChars)
            {
                if (Unikey_OC[m_oc] == CONV_CHARSET_XUTF8)
                    m_preeditstring.append(utf8_mbstowcs((const char*)UnikeyBuf, UnikeyBufChars));
                else
                {
                    static unsigned char buf[1024];
                    int bufSize=sizeof(buf)/sizeof(buf[0]);

                    latinToUtf(buf, UnikeyBuf, UnikeyBufChars, &bufSize);
                    m_preeditstring.append(utf8_mbstowcs((const char*)buf, sizeof(buf)/sizeof(buf[0])-bufSize));
                }

                m_auto_commit = false;
                unikey_update_preedit_string(m_preeditstring, true);
            }
        }

        return true;
    }

    else if (key.code >= SCIM_KEY_space && key.code <= SCIM_KEY_asciitilde)
    {
        UnikeySetCapsState(key.mask & SCIM_KEY_ShiftMask, key.mask & SCIM_KEY_CapsLockMask);

        // auto commit char: commit char that never change later in preedit string
        // if macro enabled, then not auto commit. Because macro may change any word
        if (m_ukopt.macroEnabled==0 && (UnikeyAtWordBeginning() || m_auto_commit))
        {
            for (i = 0; i < sizeof(WordAutoCommit); i++)
                if (key.code == WordAutoCommit[i])
                {
                    UnikeyPutChar(key.code);
                    m_auto_commit = true;
                    forward_key_event(key); // forward keyevent instead of return false
                    return true;
                }
        } // end auto commit char

        if ((Unikey_IM[m_im] == UkTelex || Unikey_IM[m_im] == UkSimpleTelex2)
                 && m_process_w_AtBeginWord == false
                 && UnikeyAtWordBeginning()
                 && (key.code == SCIM_KEY_w || key.code == SCIM_KEY_W))
        {
            UnikeyPutChar(key.code);
            if (m_ukopt.macroEnabled==0)
            {
                forward_key_event(key);
            }
            else
            {
                m_preeditstring.push_back(key.code);
                unikey_update_preedit_string(m_preeditstring, true);
            }

            m_auto_commit = true;
            
            return true;
        }

        m_auto_commit = false;

        if (m_lastkey_with_shift == false
            && key.mask & SCIM_KEY_ShiftMask
            && key.code == SCIM_KEY_space
            && !UnikeyAtWordBeginning())
        {
            UnikeyRestoreKeyStrokes();
        }

        else
        {
            UnikeyFilter(key.code);
        }

        if (UnikeyBackspaces > 0)
        {
            static int len;

            len = m_preeditstring.length();

            if (len <= UnikeyBackspaces)
                m_preeditstring.clear();
            else
                m_preeditstring.erase(len - UnikeyBackspaces, UnikeyBackspaces);
        }

        if (UnikeyBufChars > 0)
        {
            if (Unikey_OC[m_oc] == CONV_CHARSET_XUTF8)
                m_preeditstring.append(utf8_mbstowcs((const char*)UnikeyBuf, UnikeyBufChars));
            else
            {
                static unsigned char buf[1024];
                int bufSize=sizeof(buf)/sizeof(buf[0]);

                latinToUtf(buf, UnikeyBuf, UnikeyBufChars, &bufSize);
                m_preeditstring.append(utf8_mbstowcs((const char*)buf, sizeof(buf)/sizeof(buf[0])-bufSize));
            }
        }
        else
        {    
            m_preeditstring.push_back(key.get_unicode_code());
        }

        if (m_preeditstring.length())
        {
            for (i=0; i < sizeof(WordBreakSyms); i++)
            {
                if (WordBreakSyms[i] == m_preeditstring[m_preeditstring.length()-1] && key.code == WordBreakSyms[i])
                {
                    commit_string(m_preeditstring);
                    hide_preedit_string();
                    m_preeditstring.clear();
                    reset();
                    return true;
                }
            }
        }

        unikey_update_preedit_string(m_preeditstring, true);
        return true;
    }

    // else (key non process)
    reset();
    return false;
}

PropertyList UnikeyInstancePreedit::CreatePropertyList()
{
    return UnikeyInstance::CreatePropertyList();
}

void UnikeyInstancePreedit::trigger_property(const String &property)
{
    UnikeyInstance::trigger_property(property);
}


/*********************************************************
**************** Unikey Instance Classic *****************
*********************************************************/

UnikeyInstanceClassic::UnikeyInstanceClassic(UnikeyFactory *factory, const String &encoding, int id)
    :UnikeyInstance(factory, encoding, id)
{
    reset();
}

UnikeyInstanceClassic::~UnikeyInstanceClassic()
{
}

void UnikeyInstanceClassic::focus_in()
{
    UnikeyInstance::focus_in();
    register_properties(CreatePropertyList());
}

void UnikeyInstanceClassic::focus_out()
{
    reset();
}

void UnikeyInstanceClassic::reset()
{
    UnikeyInstance::reset();
}

void UnikeyInstanceClassic::unikey_send_backspace(int nBackspace)
{
    static WideString ws;
    static int n;

    //  if surrounding text was provided, use it instead of send backspace
    if (get_surrounding_text(ws, n, nBackspace, 0))
    {
        // for type in Auto Complete in OpenOffice
        // Hope this not rise bugs in others application
        // not use SCIM_KEY_NullKey, because GTK application crash when GTK_IM_MODULE=scim

        forward_key_event(SCIM_KEY_VoidSymbol);

        delete_surrounding_text(-ws.length(), ws.length());
    }
    else
    {
        for (int i=0; i < nBackspace; i++)
            forward_key_event(SCIM_KEY_BackSpace);
    }
}

void UnikeyInstanceClassic::unikey_commit_key_event(const KeyEvent& key)
{
    static WideString s;

    s.clear();
    s.push_back(key.code);
    commit_string(s);
}

bool UnikeyInstanceClassic::process_key_event(const KeyEvent& key)
{
    bool tmp;

    tmp = unikey_process_key_event(key);

    if ((key.code >= SCIM_KEY_space && key.code <= SCIM_KEY_asciitilde)
        || (key.code >= SCIM_KEY_KP_Multiply && key.code <= SCIM_KEY_KP_9))
    {
        m_lastkey_with_shift = key.is_shift_down();
    }
    else
    {
        m_lastkey_with_shift = false;
    }

    return tmp;
}

bool UnikeyInstanceClassic::unikey_process_key_event(const KeyEvent& key)
{
    if (key.is_key_release())
    {
        return true;
    }

    if (key.is_control_down() || key.mask & SCIM_KEY_AltMask)
    {
        reset();
        return false;
    }

    if (key.code >= SCIM_KEY_Shift_L && key.code <= SCIM_KEY_Hyper_R)
    {
        return false;
    }

    if (key.code == SCIM_KEY_BackSpace)
    {
        UnikeyBackspacePress();     // xu ly phim backspace
        if (UnikeyBackspaces==0)    // neu ukengine bao khong can xoa ky tu nao,
            return false;  // thi tra lai backspace cho chuong trinh khach
        else
        {
            unikey_send_backspace(UnikeyBackspaces);

            // append key that need change tone pos after press backspace
            if (UnikeyBufChars)
            {
                if (Unikey_OC[m_oc] == CONV_CHARSET_XUTF8)
                    commit_string(utf8_mbstowcs((const char*)UnikeyBuf, UnikeyBufChars));  // send the solved string to client
                else
                {
                    static unsigned char buf[1024];
                    int bufSize=sizeof(buf)/sizeof(buf[0]);

                    latinToUtf(buf, UnikeyBuf, UnikeyBufChars, &bufSize);
                    commit_string(utf8_mbstowcs((const char*)buf, sizeof(buf)/sizeof(buf[0]) - bufSize));
                }
            }

            return true;
        }
    }

    if (key.code >= SCIM_KEY_space && key.code <= SCIM_KEY_asciitilde)
    {
        UnikeySetCapsState(key.mask & SCIM_KEY_ShiftMask,
                           key.mask & SCIM_KEY_CapsLockMask);

        // shift + space to restore keystroke
        if (m_lastkey_with_shift == false
            && key.mask & SCIM_KEY_ShiftMask
            && key.code == SCIM_KEY_space
            && !UnikeyAtWordBeginning())
        {
            UnikeyRestoreKeyStrokes();
            if (UnikeyBackspaces == 0)
            {
                UnikeyPutChar(key.code);
            }
        }

        else if ((Unikey_IM[m_im] == UkTelex || Unikey_IM[m_im] == UkSimpleTelex2)
                 && m_process_w_AtBeginWord == false
                 && UnikeyAtWordBeginning()
                 && (key.code == SCIM_KEY_w || key.code == SCIM_KEY_W))
        {
            UnikeyPutChar(key.code);
        }

        else
        {
            UnikeyFilter(key.code);
        }

        if (UnikeyBackspaces)
        {
            unikey_send_backspace(UnikeyBackspaces);
        }

        if (UnikeyBufChars)
        {
            if (Unikey_OC[m_oc] == CONV_CHARSET_XUTF8)
            {
                commit_string(utf8_mbstowcs((const char*)UnikeyBuf, UnikeyBufChars));  // send the solved string to client
            }
            else
            {
                static unsigned char buf[1024];
                int bufSize=sizeof(buf)/sizeof(buf[0]);

                latinToUtf(buf, UnikeyBuf, UnikeyBufChars, &bufSize);
                commit_string(utf8_mbstowcs((const char*)buf, sizeof(buf)/sizeof(buf[0]) - bufSize));
            }
        }
        else
        {
            unikey_commit_key_event(key);
            return true;
        }

        return true;
    }

    // else
    reset();
    return false;
}

PropertyList UnikeyInstanceClassic::CreatePropertyList()
{
    return UnikeyInstance::CreatePropertyList();
}

void UnikeyInstanceClassic::trigger_property(const String &property)
{
    UnikeyInstance::trigger_property(property);
}