1 /* vim:set et ts=4 sts=4:
3 * ibus-pinyin - The Chinese PinYin engine for IBus
5 * Copyright (c) 2011 Peng Wu <alexepico@gmail.com>
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)
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "PYPPinyinEditor.h"
24 #include "PYPinyinProperties.h"
25 #include "PYSimpTradConverter.h"
26 #include "PYHalfFullConverter.h"
27 #include "PYLibPinyin.h"
31 /* init static members*/
32 LibPinyinPinyinEditor::LibPinyinPinyinEditor (PinyinProperties & props,
34 : LibPinyinPhoneticEditor (props, config)
43 LibPinyinPinyinEditor::processPinyin (guint keyval, guint keycode,
46 if (G_UNLIKELY (cmshm_filter (modifiers) != 0))
47 return m_text ? TRUE : FALSE;
49 return insert (keyval);
56 LibPinyinPinyinEditor::processNumber (guint keyval, guint keycode,
69 case IBUS_1 ... IBUS_9:
72 case IBUS_KP_1 ... IBUS_KP_9:
73 i = keyval - IBUS_KP_1;
76 g_return_val_if_reached (FALSE);
80 selectCandidateInPage (i);
87 LibPinyinPinyinEditor::processPunct (guint keyval, guint keycode,
93 if (cmshm_filter (modifiers) != 0)
98 return insert (keyval);
100 if (m_config.commaPeriodPage ()) {
106 if (m_config.minusEqualPage ()) {
112 if (m_config.commaPeriodPage ()) {
118 if (m_config.minusEqualPage ()) {
126 if (m_config.autoCommit ()) {
127 if (m_lookup_table.size ()) {
128 /* TODO: check here. */
129 selectCandidate (m_lookup_table.cursorPos ());
140 LibPinyinPinyinEditor::processFunctionKey (guint keyval, guint keycode,
147 modifiers = cmshm_filter (modifiers);
149 if (modifiers != 0 && modifiers != IBUS_CONTROL_MASK)
152 /* process some cursor control keys */
153 if (modifiers == 0) { /* no modifiers. */
156 if (!m_config.shiftSelectCandidate ())
158 selectCandidateInPage (1);
162 if (!m_config.shiftSelectCandidate ())
164 selectCandidateInPage (2);
169 return LibPinyinPhoneticEditor::processFunctionKey (keyval, keycode,
174 LibPinyinPinyinEditor::processKeyEvent (guint keyval, guint keycode,
177 modifiers &= (IBUS_SHIFT_MASK |
187 case IBUS_a ... IBUS_z:
188 return processPinyin (keyval, keycode, modifiers);
189 case IBUS_0 ... IBUS_9:
190 case IBUS_KP_0 ... IBUS_KP_9:
191 return processNumber (keyval, keycode, modifiers);
192 case IBUS_exclam ... IBUS_slash:
193 case IBUS_colon ... IBUS_at:
194 case IBUS_bracketleft ... IBUS_quoteleft:
195 case IBUS_braceleft ... IBUS_asciitilde:
196 return processPunct (keyval, keycode, modifiers);
198 return processSpace (keyval, keycode, modifiers);
200 return processFunctionKey (keyval, keycode, modifiers);
205 LibPinyinPinyinEditor::commit ()
207 if (G_UNLIKELY (m_text.empty ()))
212 /* sentence candidate */
214 pinyin_get_sentence(m_instance, &tmp);
216 if (m_props.modeSimp ()) {
219 SimpTradConverter::simpToTrad (tmp, m_buffer);
224 /* text after pinyin */
225 const gchar *p = m_text.c_str() + m_pinyin_len;
226 if (G_UNLIKELY (m_props.modeFull ())) {
228 m_buffer.appendUnichar (HalfFullConverter::toFull (*p++));
234 pinyin_train(m_instance);
235 LibPinyinBackEnd::instance ().modified ();
236 LibPinyinPhoneticEditor::commit ((const gchar *)m_buffer);
241 LibPinyinPinyinEditor::updatePreeditText ()
243 /* preedit text = guessed sentence + un-parsed pinyin text */
244 if (G_UNLIKELY (m_text.empty ())) {
251 pinyin_get_sentence(m_instance, &tmp);
253 if (m_props.modeSimp ()) {
256 SimpTradConverter::simpToTrad (tmp, m_buffer);
262 /* append rest text */
263 const gchar *p = m_text.c_str () + m_pinyin_len;
266 StaticText preedit_text (m_buffer);
268 preedit_text.appendAttribute (IBUS_ATTR_TYPE_UNDERLINE, IBUS_ATTR_UNDERLINE_SINGLE, 0, -1);
270 guint pinyin_cursor = getPinyinCursor ();
271 Editor::updatePreeditText (preedit_text, pinyin_cursor, TRUE);
275 LibPinyinPinyinEditor::updateAuxiliaryText ()
277 if (G_UNLIKELY (m_text.empty ())) {
278 hideAuxiliaryText ();
284 /* Note: cursor handling is defered to full/double pinyin editors. */
285 // guint pinyin_cursor = getPinyinCursor ();
286 PinyinKeyVector & pinyin_keys = m_instance->m_pinyin_keys;
287 for (guint i = 0; i < pinyin_keys->len; ++i) {
290 PinyinKey *key = &g_array_index (pinyin_keys, PinyinKey, i);
291 m_buffer << key->get_pinyin_string ();
294 /* append rest text */
295 const gchar *p = m_text.c_str() + m_pinyin_len;
298 StaticText aux_text (m_buffer);
299 Editor::updateAuxiliaryText (aux_text, TRUE);
303 LibPinyinPinyinEditor::updateLookupTable ()
305 m_lookup_table.setPageSize (m_config.pageSize ());
306 m_lookup_table.setOrientation (m_config.orientation ());
307 LibPinyinPhoneticEditor::updateLookupTable ();