1 /* vim:set et ts=4 sts=4:
3 * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "PYPFullPinyinEditor.h"
24 #include "PYLibPinyin.h"
28 LibPinyinFullPinyinEditor::LibPinyinFullPinyinEditor
29 (PinyinProperties & props, Config & config)
30 : LibPinyinPinyinEditor (props, config)
32 m_instance = LibPinyinBackEnd::instance ().allocPinyinInstance ();
35 LibPinyinFullPinyinEditor::~LibPinyinFullPinyinEditor (void)
37 LibPinyinBackEnd::instance ().freePinyinInstance (m_instance);
42 LibPinyinFullPinyinEditor::reset (void)
44 LibPinyinPinyinEditor::reset ();
48 LibPinyinFullPinyinEditor::insert (gint ch)
51 if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN))
54 m_text.insert (m_cursor++, ch);
63 LibPinyinFullPinyinEditor::processKeyEvent (guint keyval,
67 return LibPinyinPinyinEditor::processKeyEvent (keyval, keycode, modifiers);
71 LibPinyinFullPinyinEditor::updatePinyin (void)
73 if (G_UNLIKELY (m_text.empty ())) {
75 /* TODO: check whether to replace "" with NULL. */
76 pinyin_parse_more_full_pinyins (m_instance, "");
77 pinyin_guess_sentence (m_instance);
82 pinyin_parse_more_full_pinyins (m_instance, m_text.c_str ());
83 pinyin_guess_sentence (m_instance);
87 LibPinyinFullPinyinEditor::updateAuxiliaryText ()
89 if (G_UNLIKELY (m_text.empty ())) {
97 pinyin_get_n_pinyin (m_instance, &len);
99 for (guint i = 0; i < len; ++i) {
100 PinyinKey *key = NULL;
101 pinyin_get_pinyin_key (m_instance, i, &key);
103 PinyinKeyPos *pos = NULL;
104 pinyin_get_pinyin_key_rest (m_instance, i, &pos);
106 guint16 cursor = 0, end = 0;
107 pinyin_get_pinyin_key_rest_positions (m_instance, pos, &cursor, &end);
110 if (G_UNLIKELY (cursor == m_cursor)) { /* at word boundary. */
111 pinyin_get_pinyin_string (m_instance, key, &str);
112 m_buffer << '|' << str;
114 } else if (G_LIKELY (cursor < m_cursor &&
115 m_cursor < end)) { /* in word */
117 pinyin_get_pinyin_key_rest_length (m_instance, pos, &length);
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;
131 if (m_cursor == m_pinyin_len)
134 /* append rest text */
135 const gchar * p = m_text.c_str() + m_pinyin_len;
138 StaticText aux_text (m_buffer);
139 Editor::updateAuxiliaryText (aux_text, TRUE);
143 LibPinyinFullPinyinEditor::update (void)
145 guint lookup_cursor = getLookupCursor ();
146 pinyin_guess_full_pinyin_candidates (m_instance, lookup_cursor);
148 updateLookupTable ();
149 updatePreeditText ();
150 updateAuxiliaryText ();