~ubuntu-branches/debian/squeeze/ibus-hangul/squeeze

« back to all changes in this revision

Viewing changes to src/engine.c

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing
  • Date: 2010-01-03 12:46:27 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100103124627-myjyr6sjtomh148o
Tags: 1.2.0.20100102-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#include <ibus.h>
7
7
#include <hangul.h>
8
8
#include <string.h>
 
9
#include <ctype.h>
9
10
 
10
11
#include "i18n.h"
11
12
#include "engine.h"
27
28
 
28
29
    IBusLookupTable *table;
29
30
 
30
 
    IBusProperty    *hangul_mode_prop;
31
31
    IBusProperty    *prop_hanja_mode;
32
32
    IBusPropList    *prop_list;
33
33
};
36
36
        IBusEngineClass parent;
37
37
};
38
38
 
 
39
struct KeyEvent {
 
40
    guint keyval;
 
41
    guint modifiers;
 
42
};
 
43
 
39
44
/* functions prototype */
40
45
static void     ibus_hangul_engine_class_init   (IBusHangulEngineClass  *klass);
41
46
static void     ibus_hangul_engine_init             (IBusHangulEngine           *hangul);
98
103
                                             gboolean flag);
99
104
static gboolean lookup_table_is_visible     (IBusLookupTable *table);
100
105
 
 
106
static void     key_event_list_set          (GArray* list, const char* str);
 
107
static gboolean key_event_list_match        (GArray* list,
 
108
                                             guint keyval,
 
109
                                             guint modifiers);
 
110
 
101
111
static IBusEngineClass *parent_class = NULL;
102
112
static HanjaTable *hanja_table = NULL;
103
113
static HanjaTable *symbol_table = NULL;
104
114
static IBusConfig *config = NULL;
105
 
static GString    *hangul_keyboard;
 
115
static GString    *hangul_keyboard = NULL;
 
116
static GArray     *hanja_keys = NULL;
 
117
static int lookup_table_orientation = 0;
106
118
 
107
119
GType
108
120
ibus_hangul_engine_get_type (void)
149
161
    if (res) {
150
162
        const gchar* str = g_value_get_string (&value);
151
163
        g_string_assign (hangul_keyboard, str);
 
164
        g_value_unset(&value);
 
165
    }
 
166
 
 
167
    hanja_keys = g_array_sized_new(FALSE, TRUE, sizeof(struct KeyEvent), 4);
 
168
    res = ibus_config_get_value (config, "engine/Hangul",
 
169
                                         "HanjaKeys", &value);
 
170
    if (res) {
 
171
        const gchar* str = g_value_get_string (&value);
 
172
        key_event_list_set(hanja_keys, str);
 
173
        g_value_unset(&value);
 
174
    } else {
 
175
        struct KeyEvent ev;
 
176
 
 
177
        ev.keyval = IBUS_Hangul_Hanja;
 
178
        ev.modifiers = 0;
 
179
        g_array_append_val(hanja_keys, ev);
 
180
 
 
181
        ev.keyval = IBUS_F9;
 
182
        ev.modifiers = 0;
 
183
        g_array_append_val(hanja_keys, ev);
152
184
    }
153
185
}
154
186
 
237
269
    g_object_unref (label);
238
270
    g_object_unref (tooltip);
239
271
    ibus_prop_list_append (hangul->prop_list, prop);
 
272
    g_object_unref (prop);
240
273
 
241
274
    hangul->table = ibus_lookup_table_new (9, 0, TRUE, FALSE);
242
275
 
262
295
static void
263
296
ibus_hangul_engine_destroy (IBusHangulEngine *hangul)
264
297
{
 
298
    if (hangul->prop_hanja_mode) {
 
299
        g_object_unref (hangul->prop_hanja_mode);
 
300
        hangul->prop_hanja_mode = NULL;
 
301
    }
 
302
 
265
303
    if (hangul->prop_list) {
266
304
        g_object_unref (hangul->prop_list);
267
305
        hangul->prop_list = NULL;
268
306
    }
269
307
 
270
 
    if (hangul->hangul_mode_prop) {
271
 
        g_object_unref (hangul->hangul_mode_prop);
272
 
        hangul->hangul_mode_prop = NULL;
273
 
    }
274
 
 
275
308
    if (hangul->table) {
276
309
        g_object_unref (hangul->table);
277
310
        hangul->table = NULL;
502
535
            ibus_hangul_engine_hide_lookup_table (hangul);
503
536
        }
504
537
        return TRUE;
505
 
    } else if (keyval == IBUS_Left) {
506
 
        ibus_lookup_table_cursor_up (hangul->table);
507
 
        ibus_hangul_engine_update_lookup_table_ui (hangul);
508
 
        return TRUE;
509
 
    } else if (keyval == IBUS_Right) {
510
 
        ibus_lookup_table_cursor_down (hangul->table);
511
 
        ibus_hangul_engine_update_lookup_table_ui (hangul);
512
 
        return TRUE;
513
 
    } else if (keyval == IBUS_Up) {
514
 
        ibus_lookup_table_page_up (hangul->table);
515
 
        ibus_hangul_engine_update_lookup_table_ui (hangul);
516
 
        return TRUE;
517
 
    } else if (keyval == IBUS_Down) {
518
 
        ibus_lookup_table_page_down (hangul->table);
519
 
        ibus_hangul_engine_update_lookup_table_ui (hangul);
520
 
        return TRUE;
521
538
    } else if (keyval == IBUS_Page_Up) {
522
 
        ibus_lookup_table_page_up (hangul->table);
523
 
        ibus_hangul_engine_update_lookup_table_ui (hangul);
 
539
        ibus_lookup_table_page_up (hangul->table);
 
540
        ibus_hangul_engine_update_lookup_table_ui (hangul);
524
541
        return TRUE;
525
542
    } else if (keyval == IBUS_Page_Down) {
526
 
        ibus_lookup_table_page_down (hangul->table);
527
 
        ibus_hangul_engine_update_lookup_table_ui (hangul);
 
543
        ibus_lookup_table_page_down (hangul->table);
 
544
        ibus_hangul_engine_update_lookup_table_ui (hangul);
528
545
        return TRUE;
 
546
    } else {
 
547
        if (lookup_table_orientation == 0) {
 
548
            // horizontal
 
549
            if (keyval == IBUS_Left) {
 
550
                ibus_lookup_table_cursor_up (hangul->table);
 
551
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
552
                return TRUE;
 
553
            } else if (keyval == IBUS_Right) {
 
554
                ibus_lookup_table_cursor_down (hangul->table);
 
555
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
556
                return TRUE;
 
557
            } else if (keyval == IBUS_Up) {
 
558
                ibus_lookup_table_page_up (hangul->table);
 
559
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
560
                return TRUE;
 
561
            } else if (keyval == IBUS_Down) {
 
562
                ibus_lookup_table_page_down (hangul->table);
 
563
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
564
                return TRUE;
 
565
            }
 
566
        } else {
 
567
            // vertical
 
568
            if (keyval == IBUS_Left) {
 
569
                ibus_lookup_table_page_up (hangul->table);
 
570
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
571
                return TRUE;
 
572
            } else if (keyval == IBUS_Right) {
 
573
                ibus_lookup_table_page_down (hangul->table);
 
574
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
575
                return TRUE;
 
576
            } else if (keyval == IBUS_Up) {
 
577
                ibus_lookup_table_cursor_up (hangul->table);
 
578
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
579
                return TRUE;
 
580
            } else if (keyval == IBUS_Down) {
 
581
                ibus_lookup_table_cursor_down (hangul->table);
 
582
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
583
                return TRUE;
 
584
            }
 
585
        }
529
586
    }
530
587
 
531
588
    if (!hangul->hanja_mode) {
532
 
        if (keyval == IBUS_h) {
533
 
            ibus_lookup_table_cursor_up (hangul->table);
534
 
            ibus_hangul_engine_update_lookup_table_ui (hangul);
535
 
            return TRUE;
536
 
        } else if (keyval == IBUS_l) {
537
 
            ibus_lookup_table_cursor_down (hangul->table);
538
 
            ibus_hangul_engine_update_lookup_table_ui (hangul);
539
 
            return TRUE;
540
 
        } else if (keyval == IBUS_k) {
541
 
            ibus_lookup_table_page_up (hangul->table);
542
 
            ibus_hangul_engine_update_lookup_table_ui (hangul);
543
 
            return TRUE;
544
 
        } else if (keyval == IBUS_j) {
545
 
            ibus_lookup_table_page_down (hangul->table);
546
 
            ibus_hangul_engine_update_lookup_table_ui (hangul);
547
 
            return TRUE;
 
589
        if (lookup_table_orientation == 0) {
 
590
            // horizontal
 
591
            if (keyval == IBUS_h) {
 
592
                ibus_lookup_table_cursor_up (hangul->table);
 
593
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
594
                return TRUE;
 
595
            } else if (keyval == IBUS_l) {
 
596
                ibus_lookup_table_cursor_down (hangul->table);
 
597
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
598
                return TRUE;
 
599
            } else if (keyval == IBUS_k) {
 
600
                ibus_lookup_table_page_up (hangul->table);
 
601
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
602
                return TRUE;
 
603
            } else if (keyval == IBUS_j) {
 
604
                ibus_lookup_table_page_down (hangul->table);
 
605
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
606
                return TRUE;
 
607
            }
 
608
        } else {
 
609
            // vertical
 
610
            if (keyval == IBUS_h) {
 
611
                ibus_lookup_table_page_up (hangul->table);
 
612
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
613
                return TRUE;
 
614
            } else if (keyval == IBUS_l) {
 
615
                ibus_lookup_table_page_down (hangul->table);
 
616
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
617
                return TRUE;
 
618
            } else if (keyval == IBUS_k) {
 
619
                ibus_lookup_table_cursor_up (hangul->table);
 
620
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
621
                return TRUE;
 
622
            } else if (keyval == IBUS_j) {
 
623
                ibus_lookup_table_cursor_down (hangul->table);
 
624
                ibus_hangul_engine_update_lookup_table_ui (hangul);
 
625
                return TRUE;
 
626
            }
548
627
        }
549
628
    }
550
629
 
573
652
    if (keyval == IBUS_Shift_L || keyval == IBUS_Shift_R)
574
653
        return FALSE;
575
654
 
576
 
    if (keyval == IBUS_F9 || keyval == IBUS_Hangul_Hanja) {
 
655
    if (key_event_list_match(hanja_keys, keyval, modifiers)) {
577
656
        if (hangul->hanja_list == NULL) {
578
657
            ibus_hangul_engine_update_lookup_table (hangul);
579
658
        } else {
599
678
    if (keyval == IBUS_BackSpace) {
600
679
        retval = hangul_ic_backspace (hangul->context);
601
680
    } else {
 
681
        // ignore capslock
 
682
        if (modifiers & IBUS_LOCK_MASK) {
 
683
            if (keyval >= 'A' && keyval <= 'z') {
 
684
                if (isupper(keyval))
 
685
                    keyval = tolower(keyval);
 
686
                else
 
687
                    keyval = toupper(keyval);
 
688
            }
 
689
        }
602
690
        retval = hangul_ic_process (hangul->context, keyval);
603
691
    }
604
692
 
648
736
    const gunichar *str;
649
737
    IBusText *text;
650
738
 
 
739
    ibus_hangul_engine_hide_lookup_table (hangul);
 
740
 
651
741
    str = hangul_ic_flush (hangul->context);
652
742
 
653
 
    if (str == NULL || str[0] == 0)
 
743
    ustring_append_ucs4 (hangul->preedit, str, -1);
 
744
 
 
745
    if (ustring_length (hangul->preedit) == 0)
654
746
        return;
655
747
 
 
748
    str = ustring_begin (hangul->preedit);
656
749
    text = ibus_text_new_from_ucs4 (str);
657
750
 
658
751
    ibus_engine_hide_preedit_text ((IBusEngine *) hangul);
659
752
    ibus_engine_commit_text ((IBusEngine *) hangul, text);
660
753
 
661
754
    g_object_unref (text);
 
755
 
 
756
    ustring_clear(hangul->preedit);
662
757
}
663
758
 
664
759
static void
791
886
        }
792
887
 
793
888
        ibus_engine_update_property (engine, hangul->prop_hanja_mode);
 
889
        ibus_hangul_engine_flush (hangul);
794
890
    }
795
891
}
796
892
 
808
904
            const gchar *str = g_value_get_string (value);
809
905
            g_string_assign (hangul_keyboard, str);
810
906
            hangul_ic_select_keyboard (hangul->context, hangul_keyboard->str);
811
 
        }
 
907
        } else if (strcmp(name, "HanjaKeys") == 0) {
 
908
            const gchar* str = g_value_get_string (value);
 
909
            key_event_list_set(hanja_keys, str);
 
910
        }
 
911
    } else if (strcmp(section, "panel") == 0) {
 
912
        if (strcmp(name, "lookup_table_orientation") == 0) {
 
913
            lookup_table_orientation = g_value_get_int (value);
 
914
        }
812
915
    }
813
916
}
814
917
 
824
927
    gpointer res = g_object_get_data (G_OBJECT(table), "visible");
825
928
    return GPOINTER_TO_UINT(res);
826
929
}
 
930
 
 
931
static void
 
932
key_event_list_set (GArray* list, const char* str)
 
933
{
 
934
    gchar** items = g_strsplit(str, ",", 0);
 
935
 
 
936
    g_array_set_size(list, 0);
 
937
 
 
938
    if (items != NULL) {
 
939
        int i;
 
940
        for (i = 0; items[i] != NULL; ++i) {
 
941
            guint keyval = 0;
 
942
            guint modifiers = 0;
 
943
            gboolean res;
 
944
            res = ibus_key_event_from_string(items[i], &keyval, &modifiers);
 
945
            if (res) {
 
946
                struct KeyEvent ev = { keyval, modifiers };
 
947
                g_array_append_val(list, ev);
 
948
            }
 
949
        }
 
950
        g_strfreev(items);
 
951
    }
 
952
}
 
953
 
 
954
static gboolean
 
955
key_event_list_match(GArray* list, guint keyval, guint modifiers)
 
956
{
 
957
    guint i;
 
958
    guint mask;
 
959
 
 
960
    /* ignore capslock and numlock */
 
961
    mask = IBUS_SHIFT_MASK |
 
962
           IBUS_CONTROL_MASK |
 
963
           IBUS_MOD1_MASK |
 
964
           IBUS_MOD3_MASK |
 
965
           IBUS_MOD4_MASK |
 
966
           IBUS_MOD5_MASK;
 
967
 
 
968
    modifiers &= mask;
 
969
    for (i = 0; i < list->len; ++i) {
 
970
        struct KeyEvent* ev = &g_array_index(list, struct KeyEvent, i);
 
971
        if (ev->keyval == keyval && ev->modifiers == modifiers) {
 
972
            return TRUE;
 
973
        }
 
974
    }
 
975
 
 
976
    return FALSE;
 
977
}