Newer
Older
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
/* vim:set et ts=4 sts=4:
*
* ibus-pinyin - The Chinese PinYin engine for IBus
*
* Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
*
* 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "PyZyPhoneticContext.h"
#include "PyZySimpTradConverter.h"
#include "PyZyDatabase.h"
#include "PyZyPhraseEditor.h"
namespace PyZy {
PhoneticContext::PhoneticContext (Config & config, PhoneticContext::Observer *observer)
: m_config (config),
m_phrase_editor (config),
m_observer (observer)
{
reset ();
}
PhoneticContext::~PhoneticContext ()
{
}
bool
PhoneticContext::processKeyEvent (unsigned short key)
{
const guint key_code = key & 0x00ff;
const guint vkey_code = key & 0xff00;
if (vkey_code == 0) {
if ('a' <= key_code && key_code <= 'z') {
return insert (key_code);
}
} else {
const guint key_num = key_code - '1';
switch (vkey_code) {
case VKEY_COMMIT:
commit ();
return TRUE;
case VKEY_RESET:
reset ();
update ();
return TRUE;
case VKEY_CURSOR_RIGHT:
if (m_phrase_editor.unselectCandidates ()) {
update ();
return TRUE;
} else {
return moveCursorRight();
}
case VKEY_CURSOR_LEFT:
if (m_phrase_editor.unselectCandidates ()) {
update ();
return TRUE;
} else {
return moveCursorLeft();
}
case VKEY_CURSOR_RIGHT_BY_WORD:
if (m_phrase_editor.unselectCandidates ()) {
update ();
return TRUE;
} else {
return moveCursorRightByWord ();
}
case VKEY_CURSOR_LEFT_BY_WORD:
if (m_phrase_editor.unselectCandidates ()) {
update ();
return TRUE;
} else {
return moveCursorLeftByWord ();
}
case VKEY_CURSOR_TO_BEGIN:
if (m_phrase_editor.unselectCandidates ()) {
update ();
return TRUE;
} else {
return moveCursorToBegin();
}
case VKEY_CURSOR_TO_END:
if (m_phrase_editor.unselectCandidates ()) {
update ();
return TRUE;
} else {
return moveCursorToEnd();
}
case VKEY_CANDIDATE_SELECT:
return selectCandidateInPage (key_num);
case VKEY_CANDIDATE_FOCUS:
return focusCandidateInPage (key_num);
case VKEY_CANDIDATE_RESET:
return resetCandidateInPage (key_num);
case VKEY_PAGE_PREVIOUS:
selectPage (MAX(1, page ()) - 1);
return TRUE;
case VKEY_PAGE_NEXT:
selectPage (page () + 1);
return TRUE;
case VKEY_PAGE_BEGIN:
selectPage (0);
return TRUE;
case VKEY_PAGE_END:
{
const guint guint_max = (guint)-1;
selectPage (guint_max);
}
return TRUE;
case VKEY_DELETE_CHARACTER_BEFORE:
return removeCharBefore();
case VKEY_DELETE_CHARACTER_AFTER:
return removeCharAfter();
case VKEY_DELETE_WORD_BEFORE:
return removeWordBefore();
case VKEY_DELETE_WORD_AFTER:
return removeWordAfter();
}
}
g_warning ("Can't handle KeyEvent (keycode=%d, vkeycode=%d)\n",
key_code, vkey_code);
return FALSE;
}
gboolean
PhoneticContext::updateSpecialPhrases (void)
{
guint size = m_special_phrases.size ();
m_special_phrases.clear ();
if (!m_config.specialPhrases ())
return FALSE;
if (!m_selected_special_phrase.empty ())
return FALSE;
guint begin = m_phrase_editor.cursorInChar ();
guint end = m_cursor;
if (begin < end) {
SpecialPhraseTable::instance ().lookup (
m_text.substr (begin, m_cursor - begin),
m_special_phrases);
}
return size != m_special_phrases.size () || size != 0;
}
void
PhoneticContext::updateLookupTable (void)
{
m_candidates.clear ();
for (gint i = 0; i < m_special_phrases.size (); ++i) {
Candidate candidate;
candidate.text = m_special_phrases[i];
candidate.type = SPECIAL_PHRASE;
m_candidates.push_back (candidate);
}
const PhraseArray & phrase_array = m_phrase_editor.candidates ();
for (gint i = 0; i < phrase_array.size (); ++i) {
CandidateType candidate_type;
if (i < m_special_phrases.size ()) {
candidate_type = SPECIAL_PHRASE;
} else if (m_phrase_editor.candidateIsUserPhrase (i - m_special_phrases.size ())) {
candidate_type = USER_PHRASE;
} else {
candidate_type = NORMAL_PHRASE;
}
Candidate candidate;
candidate.text = phrase_array[i].phrase;
candidate.type = candidate_type;
m_candidates.push_back (candidate);
}
m_observer->lookupTableChanged ();
}
void
PhoneticContext::reset (void)
{
m_pinyin.clear ();
m_pinyin_len = 0;
m_phrase_editor.reset ();
m_special_phrases.clear ();
m_selected_special_phrase.clear ();
m_cursor = 0;
m_focused_candidate = 0;
m_text.clear ();
m_preedit_text.clear ();
}
gboolean
PhoneticContext::focusCandidateInPage (guint i)
{
return focusCandidate (page () * m_config.pageSize () + i);
}
gboolean
PhoneticContext::focusCandidate (guint i)
{
if (G_UNLIKELY (i >= m_candidates.size ())) {
g_warning ("Too big index. Can't focus to selected candidate.");
return FALSE;
}
m_focused_candidate = i;
update ();
return TRUE;
}
void
PhoneticContext::selectPage (guint i)
{
if (G_UNLIKELY (m_candidates.size () == 0)) {
m_focused_candidate = 0;
return;
}
const guint size = m_config.pageSize ();
const guint max_page = (m_candidates.size () - 1) / size;
if (i > max_page) {
i = max_page;
}
m_focused_candidate = MIN (m_candidates.size (), i * size + m_focused_candidate % size);
update ();
}
void
PhoneticContext::update ()
{
updateLookupTable ();
updatePreeditText ();
updateAuxiliaryText ();
}
gboolean
PhoneticContext::selectCandidate (guint i)
{
if (i >= m_config.pageSize ()) {
g_warning ("selectCandidate(%ud): Too big index!\n", i);
}
if (i < m_special_phrases.size ()) {
// select a special phrase
m_selected_special_phrase = m_special_phrases[i];
m_focused_candidate = 0;
if (m_cursor == m_text.size ()) {
commit ();
}
else {
updateSpecialPhrases ();
update ();
}
return TRUE;
}
i -= m_special_phrases.size ();
if (m_phrase_editor.selectCandidate (i)) {
m_focused_candidate = 0;
if (m_phrase_editor.pinyinExistsAfterCursor () ||
*textAfterPinyin () != '\0') {
updateSpecialPhrases ();
update ();
}
else {
commit ();
}
return TRUE;
}
return FALSE;
}
gboolean
PhoneticContext::selectCandidateInPage (guint i)
{
return selectCandidate (page () * m_config.pageSize () + i);
}
gboolean
PhoneticContext::resetCandidate (guint i)
{
if (i < m_special_phrases.size ()) {
return FALSE;
}
i -= m_special_phrases.size ();
if (m_phrase_editor.resetCandidate (i)) {
update ();
}
return TRUE;
}
gboolean
PhoneticContext::resetCandidateInPage (guint i)
{
return resetCandidate (page () * m_config.pageSize () + i);
}
}; // namespace PyZy