fixes compile
[platform/upstream/ibus-libpinyin.git] / src / PYPFullPinyinEditor.cc
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
4  *
5  * Copyright (c) 2011 Peng Wu <alexepico@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #include "PYPFullPinyinEditor.h"
23 #include "PYConfig.h"
24 #include "PYLibPinyin.h"
25
26 using namespace PY;
27
28 LibPinyinFullPinyinEditor::LibPinyinFullPinyinEditor
29 (PinyinProperties & props, Config & config)
30     : LibPinyinPinyinEditor (props, config)
31 {
32     m_instance = LibPinyinBackEnd::instance ().allocPinyinInstance ();
33 }
34
35 LibPinyinFullPinyinEditor::~LibPinyinFullPinyinEditor (void)
36 {
37     LibPinyinBackEnd::instance ().freePinyinInstance (m_instance);
38     m_instance = NULL;
39 }
40
41 void
42 LibPinyinFullPinyinEditor::reset (void)
43 {
44     LibPinyinPinyinEditor::reset ();
45 }
46
47 gboolean
48 LibPinyinFullPinyinEditor::insert (gint ch)
49 {
50     /* is full */
51     if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN))
52         return TRUE;
53
54     m_text.insert (m_cursor++, ch);
55
56     updatePinyin ();
57     update ();
58     return TRUE;
59 }
60
61
62 gboolean
63 LibPinyinFullPinyinEditor::processKeyEvent (guint keyval,
64                                             guint keycode,
65                                             guint modifiers)
66 {
67     return LibPinyinPinyinEditor::processKeyEvent (keyval, keycode, modifiers);
68 }
69
70 void
71 LibPinyinFullPinyinEditor::updatePinyin (void)
72 {
73     if (G_UNLIKELY (m_text.empty ())) {
74         m_pinyin_len = 0;
75         /* TODO: check whether to replace "" with NULL. */
76         pinyin_parse_more_full_pinyins (m_instance, "");
77         pinyin_guess_sentence (m_instance);
78         return;
79     }
80
81     m_pinyin_len =
82         pinyin_parse_more_full_pinyins (m_instance, m_text.c_str ());
83     pinyin_guess_sentence (m_instance);
84 }
85
86 void
87 LibPinyinFullPinyinEditor::updateAuxiliaryText ()
88 {
89     if (G_UNLIKELY (m_text.empty ())) {
90         hideAuxiliaryText ();
91         return;
92     }
93
94     m_buffer.clear ();
95
96     guint len = 0;
97     pinyin_get_n_pinyin (m_instance, &len);
98
99     for (guint i = 0; i < len; ++i) {
100         PinyinKey *key = NULL;
101         pinyin_get_pinyin_key (m_instance, i, &key);
102
103         PinyinKeyPos *pos = NULL;
104         pinyin_get_pinyin_key_rest (m_instance, i, &pos);
105
106         guint16 cursor = 0, end = 0;
107         pinyin_get_pinyin_key_rest_positions (m_instance, pos, &cursor, &end);
108
109         gchar * str = NULL;
110         if (G_UNLIKELY (cursor == m_cursor)) { /* at word boundary. */
111             pinyin_get_pinyin_string (m_instance, key, &str);
112             m_buffer << '|' << str;
113             g_free (str);
114         } else if (G_LIKELY (cursor < m_cursor &&
115                              m_cursor < end)) { /* in word */
116             guint16 length = 0;
117             pinyin_get_pinyin_key_rest_length (m_instance, pos, &length);
118
119             /* raw text */
120             String raw = m_text.substr (cursor, length);
121             guint offset = m_cursor - cursor;
122             m_buffer << ' ' << raw.substr (0, offset)
123                      << '|' << raw.substr (offset);
124         } else { /* other words */
125             pinyin_get_pinyin_string (m_instance, key, &str);
126             m_buffer << ' ' << str;
127             g_free (str);
128         }
129     }
130
131     if (m_cursor == m_pinyin_len)
132         m_buffer << '|';
133
134     /* append rest text */
135     const gchar * p = m_text.c_str() + m_pinyin_len;
136     m_buffer << p;
137
138     StaticText aux_text (m_buffer);
139     Editor::updateAuxiliaryText (aux_text, TRUE);
140 }
141
142 void
143 LibPinyinFullPinyinEditor::update (void)
144 {
145     guint lookup_cursor = getLookupCursor ();
146     pinyin_guess_full_pinyin_candidates (m_instance, lookup_cursor);
147
148     updateLookupTable ();
149     updatePreeditText ();
150     updateAuxiliaryText ();
151 }