~ubuntu-branches/ubuntu/utopic/ibus-chewing/utopic

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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
requires 2.0.0

%alltop{
/*
 * Copyright © 2009,2010  Red Hat, Inc. All rights reserved.
 * Copyright © 2009,2010  Ding-Yi Chen <dchen at redhat.com>
 *
 * This file is part of the ibus-chewing Project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdlib.h>
#include <libintl.h>
#include <ibus.h>
#include <chewing.h>
#include <string.h>
#include <stdio.h>
#define GETTEXT_PACKAGE "gtk20"
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <X11/extensions/XTest.h>
#include <X11/Xlib.h>
#include <ctype.h>
#include "ibus-chewing-util.h"
%}

enum CHEWING_KBTYPE{
    INVALID=-1,
    DEFAULT,
    HSU,
    IBM,
    GIN_YIEH,
    ETEN,
    ETEN26,
    DVORAK,
    DVORAK_HSU,
    DACHEN_CP26,
    HAN_YU
} Chewing:KbType;

enum CHEWING_MODIFIER_SYNC{
    DISABLE,
    FROM_KEYBOARD,
    FROM_IM
} Chewing:Modifier:Sync;

enum CHEWING_INPUT_STYLE{
    IN_APPLICATION,
    IN_CANDIDATE,
} Chewing:Input:Style;

/**
 * ChewingInputMode:
 * @BYPASS: Bypass the cursor movement keys.
 * @EDITING: Forming Chinese character (in pre-edit buffer).
 * @SELECTING: Start Selecting candidate. Immediately after character is formed.
 * @SELECTING_CONTINUE: Still selecting candidate,.
 * @SELECTION_DONE: A candidate is manually selected.
 *
 * Chewing input (key handling) mode.
 */
enum CHEWING_INPUT_MODE{
    BYPASS,
    EDITING,
    SELECTING,
    SELECTING_CONTINUE,
    SELECTION_DONE
} Chewing:Input:Mode;

enum CHEWING_OUTPUT_CHARSET{
    AUTO,
    BIG5,
    UTF8
} Chewing:Output:Charset;

/**
 * ChewingFlag:
 * @PLAIN_ZHUYIN: Plain chewing mode (no AI).
 * @FORCE_LOWERCASE_ENGLISH: Force Lowercase even when CapsLock is on.
 * @NUMPAD_ALWAYS_NUMBER: Key from num pad should always produce number.
 * @EASY_SYMBOL_INPUT: Symbols can be inputed by pressing Shift->[key]
 *
 * Chewing flags stores configuration switches.
 * The flags are only changed when initializing or preferences are changed.
 */
enum CHEWING_FLAG{
    PLAIN_ZHUYIN=		0x1,
    FORCE_LOWERCASE_ENGLISH=	0x2,
    NUMPAD_ALWAYS_NUMBER=	0x4,
    EASY_SYMBOL_INPUT=		0x8,
} Chewing:Flag;

/**
 * EngineStatus:
 * @INITIALIZED:	Engine is initialized.
 * @ENABLED:            Engine is enabled.
 * @FOCUS_IN:		Engine gets focus.
 * @SHOW_CANDIDATE:     Whether the candidate selection should be shown. Important for Plain Zhuyin.
 * @NEED_COMMIT:        There's Something to commit.
 * @FORCE_COMMIT:       The engine should commit
 * @IS_PASSWORD:        Current input is password.
 *
 * Engine status show the current states of engine,
 * Thus this will change quite often.
 *
 * libChewing does not have direct method to clean commit buffer.
 * Thus NEED_COMMIT is needed to prevent unnecessary commit.
 */
enum ENGINE_STATUS{
    INITIALIZED=		0x1,
    ENABLED=			0x2,
    FOCUS_IN=			0x4,
    SHOW_CANDIDATE=		0x8,
    NEED_COMMIT=		0x10,
    FORCE_COMMIT=		0x20,
    IS_PASSWORD=		0x40,
} Engine:Status;

%h{
#include "maker-dialog.h"
#define GCONF_KEY_PREFIX "/desktop/ibus/engine/Chewing/"
/* For easy symbol input work around */
#define EASY_SYMBOL_INPUT_WORK_AROUND

extern PropertySpec propSpecs[];
extern const gchar *page_labels[];
extern const gchar *button_labels[];
extern GtkResponseType button_responses[];
#define ZHUYIN_BUFFER_SIZE 12

#define ibus_chewing_engine_set_status_flag(cengine,f) cengine->_priv->statusFlags |= f
#define ibus_chewing_engine_clear_status_flag(cengine,f) cengine->_priv->statusFlags &= ~f

#define G_DEBUG_MSG(level, msg, args...) if (level<=ibus_chewing_verbose) g_debug(msg, ##args)

%}

%privateheader{
void ibus_chewing_engine_handle_Default(IBusChewingEngine *self, guint keyval, gboolean shiftPressed);
#define self_handle_Default ibus_chewing_engine_handle_Default

gboolean ibus_chewing_engine_process_key_event(IBusEngine *engine, guint keysym,  guint keycode, guint modifiers);
void ibus_chewing_engine_candidate_clicked(IBusEngine *engine, guint index, guint button, guint state);
void ibus_chewing_engine_property_activate(IBusEngine *engine, const gchar  *prop_name, guint  prop_state);

%}

%{
extern gboolean ibus_chewing_verbose;
//extern IBusConfig *iConfig;

#define IBUS_CHEWING_MAIN
#include "IBusChewingEngine-def.c"
%}

class IBus:Chewing:Engine from IBus:Engine{
    public ChewingContext *context=NULL
	destroywith chewing_delete;

    public Chewing:Modifier:Sync syncCapsLock_local=CHEWING_MODIFIER_SYNC_FROM_KEYBOARD;

    public GtkWidget *setting_dialog={
	GTK_WIDGET(maker_dialog_new_full(_("Setting"),3,page_labels,1,button_labels,button_responses))};

    protected gint selKeys[MAX_SELKEY];

    protected ChewingInputMode inputMode=CHEWING_INPUT_MODE_BYPASS;

    /* Controlling flags */
    protected ChewingFlag chewingFlags=0;

    /* Indicate the string is commited to avoid update() set NEED_COMMIT status
       again when focus in event is triiger, because the last key is Return key
       and libchewing is still Commit state. */
    //public guint hasCommit=0;

    private guint statusFlags=0;

    /* For easy symbol input work around */
    private ChewingInputStyle inputStyle;

    protected IBusLookupTable *table=NULL
	destroywith ibus_lookup_table_clear;

    public IBusProperty    *chieng_prop={
        g_object_ref_sink (
            ibus_property_new("chewing_chieng_prop",PROP_TYPE_NORMAL,
		SELF_GET_CLASS(self)->chieng_prop_label_chi,
		NULL, NULL, TRUE, TRUE,
		PROP_STATE_UNCHECKED, NULL))
        }
        destroywith g_object_unref;

    public IBusProperty    *alnumSize_prop={
        g_object_ref_sink (
            ibus_property_new("chewing_alnumSize_prop",PROP_TYPE_NORMAL,
	       	SELF_GET_CLASS(self)->alnumSize_prop_label_half,
	      	NULL, NULL, TRUE, TRUE,
		PROP_STATE_UNCHECKED, NULL))
        }
        destroywith g_object_unref;

    public IBusProperty    *settings_prop={
        g_object_ref_sink (
	    ibus_property_new("chewing_settings_prop",PROP_TYPE_TOGGLE,
	       	SELF_GET_CLASS(self)->settings_prop_label,
		quote_me(PRJ_DATA_DIR) "/icons/settings.png", NULL, TRUE, TRUE,
		PROP_STATE_UNCHECKED, NULL))
        }
        destroywith g_object_unref;

    public IBusPropList   *prop_list={ g_object_ref_sink (ibus_prop_list_new()) }
        destroywith  g_object_unref;

    public IBusConfig *config=NULL;

    protected IBusKeymap *keymap_us={ibus_keymap_get("us")};

    private Display *pDisplay = {XOpenDisplay( NULL )}
          destroywith XCloseDisplay;

    private guint key_last=0;
    private gchar zhuyin_latest[ZHUYIN_BUFFER_SIZE];

    classwide IBusText *chieng_prop_label_chi={g_object_ref_sink(ibus_text_new_from_static_string(_("Chi")))};
    classwide IBusText *chieng_prop_label_eng={g_object_ref_sink(ibus_text_new_from_static_string(_("Eng")))};
    classwide IBusText *alnumSize_prop_label_full={g_object_ref_sink(ibus_text_new_from_static_string(_("Full")))};
    classwide IBusText *alnumSize_prop_label_half={g_object_ref_sink(ibus_text_new_from_static_string(_("Half")))};

    classwide IBusText *settings_prop_label={g_object_ref_sink(ibus_text_new_from_static_string(_("Settings")))};
    classwide IBusText *emptyText={g_object_ref_sink(ibus_text_new_from_static_string(""))};

    init (self) {
          /* initialize the object here */
	G_DEBUG_MSG(1,"[I1] init() %sinitialized",(self->_priv->statusFlags & ENGINE_STATUS_INITIALIZED)? "": "un" );
	if (!(self->_priv->statusFlags & ENGINE_STATUS_INITIALIZED)){
	    maker_dialog_set_verbose_level(MAKER_DIALOG(self->setting_dialog),ibus_chewing_verbose);
	    gchar buf[100];
	    g_snprintf(buf,100,"%s/.chewing",getenv("HOME"));

	    G_DEBUG_MSG(6,"[I6] init() CHEWING_DATA_DIR=%s", CHEWING_DATA_DIR);
	    chewing_Init(CHEWING_DATA_DIR, buf);

	    self->context=chewing_new();
	    chewing_set_ChiEngMode(self->context,CHINESE_MODE);
	    self->inputMode=CHEWING_INPUT_MODE_SELECTION_DONE;

	    /* init properties */
	    ibus_prop_list_append(self->prop_list,self->chieng_prop);
	    ibus_prop_list_append(self->prop_list,self->alnumSize_prop);
	    ibus_prop_list_append(self->prop_list,self->settings_prop);

	    ibus_chewing_engine_set_status_flag(self,ENGINE_STATUS_INITIALIZED);
	}
    }

    class_init(klass){
	ibus_engine_class->property_activate= ibus_chewing_engine_property_activate;
	ibus_engine_class->process_key_event = ibus_chewing_engine_process_key_event;
	ibus_engine_class->candidate_clicked = ibus_chewing_engine_candidate_clicked;
    }

    private  void load_setting(self){
#define BUFFER_SIZE_LOCAL 200
	G_DEBUG_MSG(3,"[I3] load_setting()");
#if IBUS_COMPAT_VERSION >= 10399
	GError *error=NULL;
	GDBusConnection *connection=ibus_service_get_connection(IBUS_SERVICE(self));
	self->config=g_object_ref_sink(ibus_config_new (connection, NULL, &error));
	g_assert(error==NULL);
#else
	GList  *connections_list=ibus_service_get_connections(IBUS_SERVICE(self));
	g_assert(connections_list);
	g_assert(connections_list->data);
	IBusConnection *iConnection=(IBusConnection *) connections_list->data;
	self->config=g_object_ref_sink(ibus_config_new(iConnection));
#endif

	int i;
	gchar buf[BUFFER_SIZE_LOCAL];

	for (i=0; propSpecs[i].valueType!=G_TYPE_INVALID;i++){
	    GValue gValue={0};
	    G_DEBUG_MSG(4,"[I4]  load_setting(), i=%d",i);
	    if (ibus_chewing_config_get_value(self->config, "engine/Chewing", propSpecs[i].key, &gValue)){
		switch(propSpecs[i].valueType){
		    case G_TYPE_BOOLEAN:
			buf[0]=(g_value_get_boolean(&gValue))? '1' : '0';
			buf[1]='\0';
			break;
		    case G_TYPE_UINT:
			g_snprintf(buf,BUFFER_SIZE_LOCAL,"%u",
			    g_value_get_uint(&gValue));
    			break;
		    case G_TYPE_INT:
			g_snprintf(buf,BUFFER_SIZE_LOCAL,"%d",
			    g_value_get_int(&gValue));
			break;
		    case G_TYPE_STRING:
			g_strlcpy(buf,
	    		    g_value_get_string(&gValue),
			    BUFFER_SIZE_LOCAL);
			break;
		    default:
			break;

		}
	    }else{
		g_warning("[WW] %s /desktop/ibus/engine/Chewing/%s, %s %s",
			_("Warning: cannot load configure key"),
			propSpecs[i].key,
			(propSpecs[i].defaultValue)? _(" Use default value:") : _(" No default value, skipped.") ,
			(propSpecs[i].defaultValue)? propSpecs[i].defaultValue : ""
			);
		if (propSpecs[i].defaultValue){
		    g_strlcpy(buf,propSpecs[i].defaultValue, BUFFER_SIZE_LOCAL);
		}else{
		    continue;
		}
	    }
	    /* Add property to dialog */
	    maker_dialog_add_property(MAKER_DIALOG(self->setting_dialog),&propSpecs[i],buf,self);
	    maker_dialog_apply_widget_value(MAKER_DIALOG(self->setting_dialog),propSpecs[i].key);
	    if (G_IS_VALUE(&gValue)){
		g_value_unset(&gValue);
	    }
	}

	for (i=0; page_labels[i]!=NULL;i++){
	    maker_dialog_align_labels(MAKER_DIALOG(self->setting_dialog),page_labels[i],1.0f, 0.5f);
	}
	gtk_widget_show_all(GTK_WIDGET(MAKER_DIALOG(self->setting_dialog)->dialog_notebook));
#undef BUFFER_SIZE_LOCAL
    }

    protected void set_lookup_table_label(self,const gchar *labels){
	int i,len=strlen(labels);
	g_array_set_size(self->table->labels,0);
	for(i=0;i<len;i++){
	    IBusText *text=g_object_ref_sink(ibus_text_new_from_unichar((gunichar) labels[i]));
	    ibus_lookup_table_append_label (self->table,text);
	}
    }

    private gchar *make_preedit_string(self, glong *zhuyin_item_written_ptr){
	gchar *buff_str=chewing_buffer_String(self->context);
	G_DEBUG_MSG(4, "[I4] make_preedit_string(): chewing_buffer_String=%s ",
		buff_str);

	int chiSymbolCursor = chewing_cursor_Current(self->context);
	int zhuyin_count;
        int zhuyin_tone=-1;

	gchar *zhuyin_str=chewing_zuin_String(self->context,&zhuyin_count);
	*zhuyin_item_written_ptr= (glong) zhuyin_count;
	G_DEBUG_MSG(5, "[I5]  make_preedit_string(): chewing_zuin_String=%s count=%d inputMode=%d",
		zhuyin_str,zhuyin_count,self->inputMode);

	switch(self->inputMode){
	    case CHEWING_INPUT_MODE_EDITING:
		g_strlcpy(self->_priv->zhuyin_latest,zhuyin_str,ZHUYIN_BUFFER_SIZE);
		zhuyin_tone=0;
		break;
	    case CHEWING_INPUT_MODE_SELECTING:
		zhuyin_tone=get_tone(chewing_get_KBType(self->context),self->_priv->key_last);
		add_tone(self->_priv->zhuyin_latest,zhuyin_tone);
		if (zhuyin_tone>0 ){
		    if (self->chewingFlags & CHEWING_FLAG_PLAIN_ZHUYIN){
			/* Open candidate selection window */
                        if (chewing_get_spaceAsSelection(self->context)){
                            chewing_handle_Space(self->context);
                        }else{
                            chewing_handle_Down(self->context);
                        }
		    }
		}
		break;
	    case CHEWING_INPUT_MODE_SELECTING_CONTINUE:
		/* Do Nothing, as the action has been taken by
		 * either chewing_handle_Space, chewing_handle_Left,
		 * or chewing_handle_Right
		 */
		break;
	    case CHEWING_INPUT_MODE_SELECTION_DONE:
		self->_priv->zhuyin_latest[0]='\0';
		zhuyin_tone=-1;
		if (zhuyin_count)
		    self->inputMode=CHEWING_INPUT_MODE_EDITING;
		break;
	    default:
		/* BYPASS */
		break;

	}

	gsize len=strlen(zhuyin_str)+strlen(buff_str);
	gchar *preeditBuf=g_new(gchar, len+1);
        preeditBuf[0]='\0';
	if (chiSymbolCursor>0){
	    g_utf8_strncpy(preeditBuf, buff_str, chiSymbolCursor);
	}
	gsize zhuyin_start=strlen(preeditBuf);
	/* Inserting zhuyinBuf */
	if (zhuyin_count>0){
	    g_strlcat(preeditBuf,zhuyin_str,len+1);
	}
	/* Append rest chiSymbolBuf */
	g_strlcat(preeditBuf,buff_str+zhuyin_start, len+1);

	g_free(buff_str);
	g_free(zhuyin_str);
	G_DEBUG_MSG(4, "[I4]  make_preedit_string(): return preedit=%s zhuyin_latest=%s zhuyin_tone=%d inputMode=%d",
		preeditBuf, self->_priv->zhuyin_latest,zhuyin_tone,self->inputMode);

	return preeditBuf;
    }

    private void show_lookup_table(self, gboolean isShow){
	ibus_engine_update_lookup_table(IBUS_ENGINE(self),self->table,isShow);
	if (isShow){
	    ibus_chewing_engine_set_status_flag(self,ENGINE_STATUS_SHOW_CANDIDATE);
	    ibus_engine_show_lookup_table(IBUS_ENGINE(self));
	}else{
	    ibus_engine_hide_lookup_table(IBUS_ENGINE(self));
	    ibus_chewing_engine_clear_status_flag(self,ENGINE_STATUS_SHOW_CANDIDATE);
	}
    }

    private void update_lookup_table(self){
	ibus_lookup_table_clear(self->table);
	int i=0;
	char *candidate=NULL;
	IBusText *iText=NULL;

	G_DEBUG_MSG(4,"[I4] update_lookup_table() CurrentPage=%d", chewing_cand_CurrentPage(self->context));
	if (self->inputMode==CHEWING_INPUT_MODE_SELECTING){
	    int choicePerPage=chewing_cand_ChoicePerPage(self->context);
	    chewing_cand_Enumerate(self->context);
	    for(i=0;i<choicePerPage;i++){
		if (chewing_cand_hasNext(self->context)){
		    candidate=chewing_cand_String(self->context);
		    iText=g_object_ref_sink(ibus_text_new_from_string (candidate));
		    ibus_lookup_table_append_candidate(self->table,iText);
		    g_free(candidate);
		    g_object_unref (iText);
		}else{
		    break;
		}
	    }
	    self_show_lookup_table(self, TRUE);
	}else{
	    self_show_lookup_table(self, FALSE);
	}
    }

    /* Apply IBusText attributes */
    private IBusText *decorate_preedit(self, gchar *preeditBuf, gint *chiSymbolCursor, gint zhuyin_item_written){
	IBusText *iText=g_object_ref_sink(ibus_text_new_from_string(preeditBuf));
	*chiSymbolCursor = chewing_cursor_Current(self->context);
	G_DEBUG_MSG(4,"[I4] decorate_preedit() chiSymbolCursor=%d preeditBuf=%s statusFlags=%x",
		*chiSymbolCursor,preeditBuf, self->_priv->statusFlags);
	g_free(preeditBuf);

	gint cursorRight=0;
	gint charLen=(gint) g_utf8_strlen(iText->text, -1);

	cursorRight=*chiSymbolCursor + zhuyin_item_written;

	G_DEBUG_MSG(5,"[I5]  decorate_preedit() charLen=%d cursorRight=%d", charLen, cursorRight);

	IntervalType it;
	chewing_interval_Enumerate(self->context);

	it.from=0;
	it.to=charLen;
	int phase=1;
	int last=0;
	int i;
	if(chewing_interval_hasNext(self->context))
		chewing_interval_Get(self->context,&it);

	for(i=0;i<charLen;++i){
		if(
		    (i==it.from) ||
		    (i==it.to-1) ||
		    (*chiSymbolCursor==cursorRight && i==*chiSymbolCursor) ||
		    (*chiSymbolCursor!=cursorRight && i==cursorRight-1)){
			if((*chiSymbolCursor==cursorRight && i==*chiSymbolCursor) || (*chiSymbolCursor!=cursorRight && i==cursorRight-1)){
				if(last < i+1){
					ibus_text_append_attribute (iText, IBUS_ATTR_TYPE_UNDERLINE, IBUS_ATTR_UNDERLINE_DOUBLE,
					    last, i+1);
					phase*=-1;
					ibus_text_append_attribute (iText, IBUS_ATTR_TYPE_FOREGROUND, 0x00ffffff,
					    last, i+1);
					ibus_text_append_attribute (iText, IBUS_ATTR_TYPE_BACKGROUND, 0x00000000,
					    last, i+1);
				}
				last=i+1;
			}else if(i==it.to-1){
				if(last < i+1){
					ibus_text_append_attribute (iText, IBUS_ATTR_TYPE_UNDERLINE, (phase>0)?IBUS_ATTR_UNDERLINE_SINGLE:IBUS_ATTR_UNDERLINE_LOW,
					    last, i+1);
					phase*=-1;
				}
				last=i+1;
				if(chewing_interval_hasNext(self->context)){
					chewing_interval_Get(self->context,&it);
					if(it.from>=*chiSymbolCursor){
						it.from+=zhuyin_item_written;
						it.to+=zhuyin_item_written;
					}
				}
			}else{
				if(last < i){
					ibus_text_append_attribute (iText, IBUS_ATTR_TYPE_UNDERLINE, (phase>0)?IBUS_ATTR_UNDERLINE_SINGLE:IBUS_ATTR_UNDERLINE_LOW,
					    last, i);
					phase*=-1;
				}
				last=i;
			}
		}
	}

	return iText;
    }

    private void update_aux_string(self, IBusText *preeditIText){
	G_DEBUG_MSG(4,"[I4] update_aux_string() statusFlags=%x", self->_priv->statusFlags);
	IBusText *iText=NULL;
	switch (self->_priv->inputStyle){
	    case CHEWING_INPUT_STYLE_IN_APPLICATION:
		if (chewing_aux_Length(self->context)>0){
		    gchar *aux_string=chewing_aux_String(self->context);
		    iText=g_object_ref_sink(ibus_text_new_from_string (aux_string));
		    ibus_engine_update_auxiliary_text(IBUS_ENGINE(self),iText,TRUE);
		    g_object_unref (iText);
		    ibus_engine_show_auxiliary_text(IBUS_ENGINE(self));
		    g_free(aux_string);
		}else{
		    ibus_engine_hide_auxiliary_text(IBUS_ENGINE(self));
		}
		break;
	    case CHEWING_INPUT_STYLE_IN_CANDIDATE:
		if (self->inputMode==CHEWING_INPUT_MODE_BYPASS){
		    ibus_engine_hide_auxiliary_text(IBUS_ENGINE(self));
		}else{
		    ibus_engine_update_auxiliary_text(IBUS_ENGINE(self),preeditIText,TRUE);
		    ibus_engine_show_auxiliary_text(IBUS_ENGINE(self));
		}
		break;
	}
    }

    private void update_preedit(self, IBusText *preeditIText,gint chiSymbolCursor){
	G_DEBUG_MSG(5,"[I5] update_preedit() text=%s statusFlags=%x", preeditIText->text, self->_priv->statusFlags);
	int mode;
	if(g_utf8_strlen(preeditIText->text, -1)>0)
		mode=IBUS_ENGINE_PREEDIT_COMMIT;
	else
		mode=IBUS_ENGINE_PREEDIT_CLEAR;

	ibus_engine_update_preedit_text (IBUS_ENGINE(self),SELF_GET_CLASS(self)->emptyText, 0, TRUE);
	switch (self->_priv->inputStyle){
	    case CHEWING_INPUT_STYLE_IN_APPLICATION:
		ibus_engine_update_preedit_text_with_mode (IBUS_ENGINE(self),preeditIText, chiSymbolCursor, TRUE, mode);
		break;
	    case CHEWING_INPUT_STYLE_IN_CANDIDATE:
		ibus_engine_update_preedit_text_with_mode (IBUS_ENGINE(self),SELF_GET_CLASS(self)->emptyText, 0, TRUE, mode);
		break;
	}
	G_DEBUG_MSG(5,"[I5]  update_preedit(): return");
    }

    protected gboolean update(self){
	G_DEBUG_MSG(3,"[I3] update() statusFlags=%x", self->_priv->statusFlags);
	self_determine_input_mode(self);
	gint chiSymbolCursor;
	glong zhuyin_item_written;
	self_check_commit(self);
	gchar *preeditBuf=self_make_preedit_string(self, &zhuyin_item_written);
	IBusText *iText=self_decorate_preedit(self, preeditBuf, &chiSymbolCursor, zhuyin_item_written);
	if (g_object_is_floating(iText)){
	    g_object_ref_sink(iText);
	}

	self_update_aux_string(self, iText);
	self_update_preedit(self, iText, chiSymbolCursor);
	g_object_unref(iText);
	G_DEBUG_MSG(4,"[I4]  update() inputMode=%d nPhoneSeq=%d statusFlags=%x",
		self->inputMode,
		chewing_get_phoneSeqLen(self->context),
		self->_priv->statusFlags);
	self_update_lookup_table(self);
	gboolean ret=TRUE;

        if (chewing_keystroke_CheckIgnore(self->context)){
	    ret=FALSE;
	}
	G_DEBUG_MSG(4,"[I4]  update() return %s",(ret)? "TRUE": "FALSE");
	return ret;
    }

    protected void refresh_property(self,const gchar  *prop_name){
	G_DEBUG_MSG(5,"[I5] refresh_property(%s)",prop_name);
	if (strcmp(prop_name,"chewing_chieng_prop")==0){
	    if (chewing_get_ChiEngMode(self->context)){
		/* Chinese  */
		ibus_property_set_label(self->chieng_prop,SELF_GET_CLASS(self)->chieng_prop_label_chi);
	    }else{
		/* English */
		ibus_property_set_label(self->chieng_prop,SELF_GET_CLASS(self)->chieng_prop_label_eng);
	    }
	    ibus_engine_update_property(IBUS_ENGINE(self),self->chieng_prop);
	}else if (strcmp(prop_name,"chewing_alnumSize_prop")==0){
	    if (chewing_get_ShapeMode(self->context)){
		/* Full-Sized Shape */
		ibus_property_set_label(self->alnumSize_prop,SELF_GET_CLASS(self)->alnumSize_prop_label_full);
	    }else{
		/* Half-Sized Shape */
		ibus_property_set_label(self->alnumSize_prop,SELF_GET_CLASS(self)->alnumSize_prop_label_half);
	    }
	    ibus_engine_update_property(IBUS_ENGINE(self),self->alnumSize_prop);
	}
    }

    protected gboolean is_password(self){
	G_DEBUG_MSG(5, "[I5] is_password = %d", self->chewingFlags & ENGINE_STATUS_IS_PASSWORD);
	return !!(self->_priv->statusFlags & ENGINE_STATUS_IS_PASSWORD);
    }

    /**
     * refresh_property_list:
     * @self: this instances.
     *
     * Refresh the property list (language bar).
     */
    public void refresh_property_list(self){
	self_refresh_property(self,"chewing_chieng_prop");

	self_refresh_property(self,"chewing_alnumSize_prop");

	self_refresh_property(self,"chewing_settings_prop");
	if (self->_priv->statusFlags & (ENGINE_STATUS_ENABLED | ENGINE_STATUS_FOCUS_IN)){
	    ibus_engine_register_properties(IBUS_ENGINE(self),self->prop_list);
	    IBUS_ENGINE_GET_CLASS(self)->property_show(IBUS_ENGINE(self),"chewing_chieng_prop");
	    IBUS_ENGINE_GET_CLASS(self)->property_show(IBUS_ENGINE(self),"chewing_alnumSize_prop");
	    IBUS_ENGINE_GET_CLASS(self)->property_show(IBUS_ENGINE(self),"chewing_settings_prop");
	}
    }

    /**
     * hide_property_list:
     * @self: this instances.
     *
     * Hide the property list (language bar).
     */
    public void hide_property_list(self){
	IBUS_ENGINE_GET_CLASS(self)->property_hide(IBUS_ENGINE(self),"chewing_chieng_prop");
	IBUS_ENGINE_GET_CLASS(self)->property_hide(IBUS_ENGINE(self),"chewing_alnumSize_prop");
	IBUS_ENGINE_GET_CLASS(self)->property_hide(IBUS_ENGINE(self),"chewing_settings_prop");
    }

    /**
     * save_config:
     * @self: this instances.
     * @key_suffix: key to be set.
     * @returns: TRUE if successful, FALSE otherwise.
     *
     * Save the property value to disk.
     */
    public gboolean save_config(self, const gchar *key_suffix){
	G_DEBUG_MSG(1,"[I1] save_config(%s,-)",key_suffix);
	GValue gValue={0};
	maker_dialog_get_widget_value(MAKER_DIALOG(self->setting_dialog),key_suffix,&gValue);
	return ibus_chewing_config_set_value (self->config, "engine/Chewing", key_suffix,&gValue);
    }

    /**
     * save_config_all:
     * @self: this instances.
     * @key_suffix: key to be set.
     * @returns: TRUE if all successful, FALSE otherwise.
     *
     * Save alll property values to disk.
     */
    public gboolean save_config_all(self){
	int i;
	gboolean success=TRUE;
	for(i=0;propSpecs[i].valueType!=G_TYPE_INVALID;i++){
	    if (!self_save_config(self,propSpecs[i].key)){
		success=FALSE;
	    }
	}
	return success;
    }

    protected gboolean is_selectKey(self, guint keyval){
	gint j;
	for (j=0;j< MAX_SELKEY;j++){
	    if (self->selKeys[j]==keyval){
		return TRUE;
	    }
	}
	return FALSE;
    }

    protected void set_selKeys_string(self,const gchar* selKeys_str){
	int j;
	int len_min= MIN(strlen(selKeys_str), MAX_SELKEY);
	for (j=0;j< len_min;j++){
	    self->selKeys[j]=(gint) selKeys_str[j];
	}
	chewing_set_selKey(self->context, self->selKeys,len_min);
    }

    private IBusProperty* get_iBusProperty(self, const gchar *prop_name){
	if (strcmp(prop_name,"chewing_chieng_prop")==0){
	    return self->chieng_prop;
	}else if (strcmp(prop_name,"chewing_alnumSize_prop")==0){
	    return self->alnumSize_prop;
	}else if (strcmp(prop_name,"chewing_settings_prop")==0){
	    return self->settings_prop;
	}
	G_DEBUG_MSG(2,"[I2] get_iBusProperty(%s): NULL is returned",prop_name);
	return NULL;
    }

    /*
     * determine_input_mode:
     *
     * Determine input mode.
     */
    private void determine_input_mode(self){
	gint zhuyin_count=-1;
	gchar *str_ptr=chewing_zuin_String(self->context,&zhuyin_count);
	G_DEBUG_MSG(3, "[I3] determine_input_mode(): chewing_zuin_String=%s count=%d inputMode=%d keyLast=%s",
		str_ptr,zhuyin_count,self->inputMode, keyName_get(self->_priv->key_last));

	int zhuyin_tone=-1;
	gint totalChoice=chewing_cand_TotalChoice(self->context);
	G_DEBUG_MSG(5, "[I5]  determine_input_mode(): TotalChoice=%d", totalChoice);
//	if (chewing_commit_Check(self->context)
//		&& (self->_priv->key_last==IBUS_Return || self->_priv->key_last==IBUS_KP_Enter)){
//		if(!self->hasCommit)
//		  ibus_chewing_engine_set_status_flag(self,ENGINE_STATUS_NEED_COMMIT);
//	}
	if (totalChoice >0){
	    /* Candidate selection mode ensure. */
	    self->inputMode=CHEWING_INPUT_MODE_SELECTING;
	}else if (zhuyin_count>0){
	    /* Incomplete Character*/
	    self->inputMode=CHEWING_INPUT_MODE_EDITING;
	}else{
	    /* Character/Phrase is completed. */
	    switch(self->inputMode){
		case CHEWING_INPUT_MODE_BYPASS:
		    if (chewing_buffer_Len(self->context)>0){
			/* Has punctuation mark in buffer */
			self->inputMode=CHEWING_INPUT_MODE_SELECTING;
		    }
		    break;
		case CHEWING_INPUT_MODE_EDITING:
		    zhuyin_tone=get_tone(chewing_get_KBType(self->context),self->_priv->key_last);
		    if (zhuyin_tone>0 ){
			if (self->chewingFlags & CHEWING_FLAG_PLAIN_ZHUYIN){
			    /* For plain zhuyin, Enter SELECTING mode */
			    self->inputMode=CHEWING_INPUT_MODE_SELECTING;
			}else{
			    /* For normal zhuyin, Enter SELECTION_DONE mode */
			    self->inputMode=CHEWING_INPUT_MODE_SELECTION_DONE;
			}
		    }
		    break;
		case CHEWING_INPUT_MODE_SELECTING:
		    /* ESC to cancel input mode */
		    if (self->chewingFlags & CHEWING_FLAG_PLAIN_ZHUYIN &&
			chewing_get_escCleanAllBuf(self->context) &&
			self->_priv->key_last == IBUS_Escape) {

			/* Close candidate window */
			self->inputMode=CHEWING_INPUT_MODE_SELECTION_DONE;
			chewing_handle_Esc(self->context);
			ibus_chewing_engine_clear_status_flag(self, ENGINE_STATUS_SHOW_CANDIDATE);
			break;
		    }

		    /* Switch to another page */
		    if (self_is_selectKey(self, self->_priv->key_last)){
			self->inputMode=CHEWING_INPUT_MODE_SELECTION_DONE;
		    }else{
			switch(self->_priv->key_last){
			    case ' ':
				/* Space to next page */
				if (!chewing_get_spaceAsSelection(self->context)){
				    break;
				}
			    case IBUS_Left:
			    case IBUS_KP_Left:
			    case IBUS_Right:
			    case IBUS_KP_Right:
				self->inputMode=CHEWING_INPUT_MODE_SELECTING_CONTINUE;
				break;
			    default:
				break;
			}
		    }
		    break;
		case CHEWING_INPUT_MODE_SELECTING_CONTINUE:
		    break;
		case CHEWING_INPUT_MODE_SELECTION_DONE:
		    /* One of selection keys is pressed */
		    break;
	    }
	}

	if (totalChoice + chewing_buffer_Len(self->context) + zhuyin_count==0){
	    /* If preedit buffer is empty, then enter BYPASS mode.*/
	    /* So cursor keys can be used freely. */
	    self->inputMode=CHEWING_INPUT_MODE_BYPASS;
	    self->_priv->zhuyin_latest[0]='\0';
	}
	g_free(str_ptr);
	G_DEBUG_MSG(2, "[I2]  determine_input_mode() return: zhuyin_latest=%s zhuyin_tone=%d inputMode=%d",
		self->_priv->zhuyin_latest,zhuyin_tone,self->inputMode);
    }

    /*
     * Clean chewing buffer
     */
    protected void clean_chewing_buffer(self){
	/* Save the orig Esc clean buffer state */
	gint origState=chewing_get_escCleanAllBuf(self->context);
	chewing_set_escCleanAllBuf(self->context, TRUE);
	chewing_handle_Esc(self->context);
	chewing_set_escCleanAllBuf(self->context, origState);
    }

    /* commit string */
    protected gboolean commit(self){
	gchar *commit_string=chewing_commit_String(self->context);
	G_DEBUG_MSG(2,"[I2] commit() commit:%s", commit_string);
	IBusText *iText=g_object_ref_sink(ibus_text_new_from_string(commit_string));
	ibus_engine_commit_text(IBUS_ENGINE(self),iText);
	//self->hasCommit = 1;
	g_object_unref(iText);
	chewing_free(commit_string);

	if ((self->_priv->statusFlags & ENGINE_STATUS_FORCE_COMMIT)){
	    /* Force commit implies chewing buffer need to be cleaned. */
	    G_DEBUG_MSG(3,"[I3]  _commit() clean chewing buffer for force commit.");
	    self_clean_chewing_buffer(self);
	    ibus_chewing_engine_clear_status_flag(self, ENGINE_STATUS_FORCE_COMMIT);
	}
	return TRUE;
    }

    /**
     * check_commit:
     *
     * Invoke commit() if NEED_COMMIT and commit flag is TRUE.
     */
    protected gboolean check_commit(self){
	gint isCommit=chewing_commit_Check(self->context);
	gint committed=FALSE;
	G_DEBUG_MSG(2,"[I2] check_commit() %s statusFlags=%x", (isCommit) ? "TRUE": "FALSE", self->_priv->statusFlags);
	if (isCommit && (self->_priv->statusFlags & ENGINE_STATUS_NEED_COMMIT)){
	    self_commit(self);
	    committed =TRUE;
	}
	if (!chewing_buffer_Check(self->context)){
	    /* Buffer is empty, safe to clear the ENGINE_STATUS_NEED_COMMIT */
	    ibus_chewing_engine_clear_status_flag(self, ENGINE_STATUS_NEED_COMMIT);
	}
	return committed;
    }

    /**
     * force_commit:
     *
     * Simulate key combos to commit current preedit.
     * This is triggered by:
     * + Press text selection key combos, such as Shift-(ArrowKeys)
     *
     *  Note: From IBus 1.3, focus-out event is already been handled by
     *  ibus_engine_update_preedit_text_with_mode()
     *  2.
     *
     */
    protected void force_commit(self){
	G_DEBUG_MSG(2,"[I2] force_commit() buffer=%d, commit=%d statusFlags=%x",
	    chewing_buffer_Check(self->context), chewing_commit_Check(self->context),
	    self->_priv->statusFlags);
	/* Close candidate window */
	if (self->_priv->statusFlags & ENGINE_STATUS_SHOW_CANDIDATE){
	    chewing_handle_Esc(self->context);
	    ibus_chewing_engine_clear_status_flag(self, ENGINE_STATUS_SHOW_CANDIDATE);
	}
	/* Remove the incomplete zhuyin symbol */
	if (self->inputMode==CHEWING_INPUT_MODE_EDITING)
	    chewing_handle_Esc(self->context);
	if (chewing_buffer_Check(self->context)){
	    chewing_handle_Enter(self->context);
	    G_DEBUG_MSG(3,"[I3]  force_commit() buffer is not empty.");
//	    if (self->_priv->inputStyle==CHEWING_INPUT_STYLE_IN_CANDIDATE){
//		/* But if in IN_CANDIDATE mode, still need to simulate enter.
//		 */
//		if (!chewing_commit_Check(self->context)){
//		    chewing_handle_Enter(self->context);
//		}
//	    }
	    ibus_chewing_engine_set_status_flag(self, ENGINE_STATUS_FORCE_COMMIT);
//	    self_update(self);
	}
//	if (chewing_commit_Check(self->context)){
//	    G_DEBUG_MSG(3,
//		    "[I3]  force_commit() has something to commit. buffer=%d, commit=%d",
//		    chewing_buffer_Check(self->context),chewing_commit_Check(self->context));
//	    self_update(self);
//	}
    }


    /*============================================
     * Overridden Parent (IBusEngine) methods
     */
    override (IBus:Engine) void
    reset(IBus:Engine *engine){
	G_DEBUG_MSG(1,"[I1] reset");
	Self *self=SELF(engine);

	/* Always clean buffer */
	self_clean_chewing_buffer(self);
	ibus_chewing_engine_clear_status_flag(self,
		ENGINE_STATUS_FORCE_COMMIT | ENGINE_STATUS_NEED_COMMIT);

	ibus_lookup_table_clear(self->table);
	/* Save KBType type, becaue chewing_Reset() will reset it to default.
	 */
	int kbType=chewing_get_KBType(self->context);
	chewing_Reset(self->context);
	chewing_set_KBType(self->context,kbType);
	ibus_engine_hide_auxiliary_text(engine);
	ibus_engine_hide_lookup_table(engine);
	ibus_engine_update_preedit_text(engine, SELF_GET_CLASS(self)->emptyText, 0, FALSE );
    }

    override (IBus:Engine) void
    page_up(IBus:Engine *engine){
	Self *self=SELF(engine);
	if (self_is_password(self)) return;
	chewing_handle_PageUp(self->context);
	self_update(self);
    }


    override (IBus:Engine) void
    page_down(IBus:Engine *engine){
	Self *self=SELF(engine);
	if (self_is_password(self)) return;
	chewing_handle_PageDown(self->context);
	self_update(self);
    }

    override (IBus:Engine) void
    cursor_up(IBus:Engine *engine){
	Self *self=SELF(engine);
	if (self_is_password(self)) return;
	chewing_handle_Up(self->context);
	self_update(self);
    }

    override (IBus:Engine) void
    cursor_down(IBus:Engine *engine){
	Self *self=SELF(engine);
	if (self_is_password(self)) return;
	chewing_handle_Down(self->context);
	self_update(self);
    }

    override (IBus:Engine) void
    enable(IBus:Engine  *engine){
	G_DEBUG_MSG(2,"[I2] enable()");
        Self *self=SELF(engine);

        if (!self->config){
	    self_load_setting(self);
        }
	ibus_chewing_engine_set_status_flag(self, ENGINE_STATUS_ENABLED);
	self_refresh_property_list(self);
	self->inputMode=CHEWING_INPUT_MODE_SELECTION_DONE;
    }

    override (IBus:Engine) void
    disable(IBus:Engine  *engine){
	G_DEBUG_MSG(2,"[I2] disable()");
	Self *self=SELF(engine);
	ibus_chewing_engine_clear_status_flag(self,ENGINE_STATUS_ENABLED);
	self_hide_property_list(self);
	self->inputMode=CHEWING_INPUT_MODE_BYPASS;
    }

    override (IBus:Engine) void
    focus_in(IBus:Engine *engine){
	Self *self=SELF(engine);
	if (!self->config){
	    self_load_setting(self);
	}
	ibus_chewing_engine_set_status_flag(self, ENGINE_STATUS_FOCUS_IN);
	self_refresh_property_list(self);

	/* Sync Caps_Lock and ChiEngMode */
	guint modifiers=keyModifier_get(self->_priv->pDisplay);
	gint caps_is_lock=(modifiers & IBUS_LOCK_MASK)!=0;
	G_DEBUG_MSG(2,"[I2] focus_in(): statusFlags=%d ChiEng=%d IBUS_LOCK=%d",
		self->_priv->statusFlags,
		chewing_get_ChiEngMode(self->context),caps_is_lock);
	if (chewing_get_ChiEngMode(self->context)==caps_is_lock){
	    /* Caps_lock and ChiEngMode does not agree each other */
	    switch(self->syncCapsLock_local){
		case CHEWING_MODIFIER_SYNC_FROM_KEYBOARD:
		    chewing_set_ChiEngMode(self->context,!caps_is_lock);
		    break;
		case CHEWING_MODIFIER_SYNC_FROM_IM:
		    /* fake event won't go through process_key_event */
		    key_send_fake_event(IBUS_Caps_Lock,self->_priv->pDisplay);
		    break;
		default:
		    g_warning("Caps_lock and ChiEngMode does not agree each other!");
		    break;

	    }
	}
	self_refresh_property_list(self);

	/* Shouldn't have anything to commit when Focus-in */
	self_clean_chewing_buffer(self);
	ibus_chewing_engine_clear_status_flag(self,
		ENGINE_STATUS_FORCE_COMMIT | ENGINE_STATUS_NEED_COMMIT);

//	self_update(self);
	G_DEBUG_MSG(4,"[I4] focus_in(): return");
    }

    override (IBus:Engine) void
    focus_out(IBus:Engine *engine){
	Self *self=SELF(engine);
	G_DEBUG_MSG(2,"[I2] focus_out(): statusFlags=%x",self->_priv->statusFlags);
	ibus_chewing_engine_clear_status_flag(self, ENGINE_STATUS_FOCUS_IN);

	/*
	 * force commit is no longer needed,
	 * as ibus_engine_update_preedit_text_with_mode now send to focus out with
	 * IBUS_ENGINE_PREEDIT_COMMIT
	 */
//	self_force_commit(self);
	G_DEBUG_MSG(5,"[I5]  focus_out(): return");
    }

    override (IBus:Engine) void
    set_capabilities(IBus:Engine *engine, guint caps){
	Self *self=SELF(engine);
	G_DEBUG_MSG(2,"[I2] set_capabilities(%x): statusFlags=%x",caps, self->_priv->statusFlags);
    }

    override (IBus:Engine) void
    property_activate(IBus:Engine *engine, const gchar  *prop_name, guint  prop_state){
	G_DEBUG_MSG(3,"[I3] property_activate(-, %s, %u)", prop_name, prop_state);
	Self *self=SELF(engine);
	gboolean needRefresh=TRUE;
	if (strcmp(prop_name,"chewing_chieng_prop")==0){
	    /* Toggle Chinese <-> English */
	    chewing_set_ChiEngMode(self->context, !chewing_get_ChiEngMode(self->context));
	}else if (strcmp(prop_name,"chewing_alnumSize_prop")==0){
	    /* Toggle Full <-> Half */
	    chewing_set_ShapeMode(self->context, !chewing_get_ShapeMode(self->context));
	}else if (strcmp(prop_name,"chewing_settings_prop")==0){
#if IBUS_CHECK_VERSION(1, 4, 0)
      if (ibus_property_get_state(self->settings_prop)==PROP_STATE_UNCHECKED)
#else
	    if (self->settings_prop->state==PROP_STATE_UNCHECKED)
#endif
      {

		gtk_widget_show_all(self->setting_dialog);
		if (gtk_dialog_run(GTK_DIALOG(self->setting_dialog))==GTK_RESPONSE_OK){
		    self_save_config_all(self);
		}
		gtk_widget_hide(self->setting_dialog);
#if IBUS_CHECK_VERSION(1, 4, 0)
    ibus_property_set_state(self->settings_prop,PROP_STATE_UNCHECKED);
#else
		self->settings_prop->state=PROP_STATE_UNCHECKED;
#endif
	    }
	}else{
	    G_DEBUG_MSG(3,"[I3]  property_activate(-, %s, %u) not recognized",prop_name, prop_state);
	    needRefresh=FALSE;
	}
	if (needRefresh)
	    self_refresh_property(self,prop_name);
    }

    override (IBus:Engine) void
    property_show(IBus:Engine *engine, const gchar  *prop_name){
	G_DEBUG_MSG(5,"[I5] property_show(-, %s)",prop_name);
	Self *self=SELF(engine);
	IBusProperty *prop=self_get_iBusProperty(self, prop_name);
	ibus_property_set_visible(prop,TRUE);
	ibus_engine_update_property(engine,prop);
    }

    override (IBus:Engine) void
    property_hide(IBus:Engine *engine, const gchar  *prop_name){
	G_DEBUG_MSG(5,"[I5] property_hide(-, %s)",prop_name);
	Self *self=SELF(engine);
	IBusProperty *prop=self_get_iBusProperty(self, prop_name);
	ibus_property_set_visible(prop,FALSE);
	ibus_engine_update_property(engine,prop);
    }

    override (IBus:Engine) void
    set_content_type(IBus:Engine *engine, guint purpose, guint hints){
	G_DEBUG_MSG(5,"[I5] set_content_type(%d, %d)", purpose, hints);

	Self *self=SELF(engine);
	if (purpose == IBUS_INPUT_PURPOSE_PASSWORD ||
	    purpose == IBUS_INPUT_PURPOSE_PIN) {
	    ibus_chewing_engine_set_status_flag(self, ENGINE_STATUS_IS_PASSWORD);
	} else {
	    ibus_chewing_engine_clear_status_flag(self, ENGINE_STATUS_IS_PASSWORD);
	}
    }
}

%{
#include "IBusChewingEngine-input-events.c"
%}