summaryrefslogtreecommitdiffstats
path: root/src/ZYZPinyinEditor.cc
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2014-06-10 18:42:48 +0800
committerPeng Wu <alexepico@gmail.com>2014-06-10 18:42:48 +0800
commit902899415e83b1b90a47d60c042b9414d51e094d (patch)
treecde74feb58d5aedbe2438ef9a434dde6545ae9f6 /src/ZYZPinyinEditor.cc
parent3d1e39c6d32470c4914aace87104d238bc37a1ce (diff)
downloadibus-libzhuyin-902899415e83b1b90a47d60c042b9414d51e094d.tar.gz
ibus-libzhuyin-902899415e83b1b90a47d60c042b9414d51e094d.tar.xz
ibus-libzhuyin-902899415e83b1b90a47d60c042b9414d51e094d.zip
write ZYZPinyinEditor.cc
Diffstat (limited to 'src/ZYZPinyinEditor.cc')
-rw-r--r--src/ZYZPinyinEditor.cc216
1 files changed, 216 insertions, 0 deletions
diff --git a/src/ZYZPinyinEditor.cc b/src/ZYZPinyinEditor.cc
new file mode 100644
index 0000000..a7d9c61
--- /dev/null
+++ b/src/ZYZPinyinEditor.cc
@@ -0,0 +1,216 @@
+/* vim:set et ts=4 sts=4:
+ *
+ * ibus-libzhuyin - New Zhuyin engine based on libzhuyin for IBus
+ *
+ * Copyright (c) 2014 Peng Wu <alexepico@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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "ZYZPinyinEditor.h"
+#include <assert.h>
+#include "ZYConfig.h"
+#include "ZYLibZhuyin.h"
+#include "ZYZhuyinProperties.h"
+#include "ZYTradSimpConverter.h"
+#include "ZYEnhancedText.h"
+
+
+using namespace ZY;
+
+PinyinEditor::PinyinEditor (ZhuyinProperties & props, Config & config)
+ : PhoneticEditor (props, config)
+{
+ m_instance = LibZhuyinBackEnd::instance ().allocZhuyinInstance ();
+}
+
+PinyinEditor::~PinyinEditor (void)
+{
+ LibZhuyinBackEnd::instance ().freeZhuyinInstance (m_instance);
+ m_instance = NULL;
+}
+
+void
+PinyinEditor::commit (void)
+{
+ if (G_UNLIKELY (m_preedit_text.empty ()))
+ return;
+
+ for (size_t i = 0; i < m_instances.size (); ++i) {
+ zhuyin_train (m_instances[i]);
+ }
+ LibZhuyinBackEnd::instance ().modified ();
+ PhoneticEditor::commit (m_preedit_text.c_str ());
+ reset ();
+}
+
+void
+PinyinEditor::reset (void)
+{
+ PhoneticEditor::reset ();
+}
+
+void
+PinyinEditor::updateZhuyin (void)
+{
+ const String & enhanced_text = m_text;
+
+ resizeInstances ();
+
+ size_t index = 0;
+ size_t start_pos = 0, end_pos = 0;
+
+ while (end_pos != enhanced_text.size ()) {
+ section_t type = probe_section_quick (enhanced_text, start_pos);
+
+ if (PHONETIC_SECTION == type) {
+ String section;
+ get_phonetic_section (enhanced_text, start_pos, end_pos, section);
+
+ zhuyin_instance_t * instance = m_instances[index];
+ zhuyin_parse_more_full_pinyins (instance, section.c_str ());
+ zhuyin_guess_sentence (instance);
+
+ ++index;
+ }
+
+ if (SYMBOL_SECTION == type) {
+ String type, lookup, choice;
+ get_symbol_section (enhanced_text, start_pos, end_pos,
+ type, lookup, choice);
+
+ }
+
+ start_pos = end_pos;
+ }
+
+ return;
+}
+
+void
+PinyinEditor::updateAuxiliaryText (void)
+{
+ /* libchewing doesn't use the auxiliary text, always hide. */
+ return;
+}
+
+void
+PinyinEditor::updatePreeditText (void)
+{
+ if (G_UNLIKELY (m_text.empty ())) {
+ hidePreeditText ();
+ return;
+ }
+
+ const String & enhanced_text = m_text;
+ m_preedit_text.clear ();
+
+ size_t index = 0;
+ size_t start_pos = 0, end_pos = 0;
+
+ while (end_pos != enhanced_text.size ()) {
+ section_t type = probe_section_quick (enhanced_text, start_pos);
+
+ if (PHONETIC_SECTION == type) {
+ String section;
+ get_phonetic_section (enhanced_text, start_pos, end_pos, section);
+ zhuyin_instance_t * instance = m_instances[index];
+
+ char * sentence = NULL;
+ zhuyin_get_sentence (instance, &sentence);
+ m_preedit_text += sentence;
+ g_free (sentence);
+
+ size_t len = zhuyin_get_parsed_input_length (instance);
+ m_preedit_text += section.substr (len);
+
+ ++index;
+ }
+
+ if (SYMBOL_SECTION == type) {
+ String type, lookup, choice;
+ get_symbol_section (enhanced_text, start_pos, end_pos,
+ type, lookup, choice);
+ m_preedit_text += choice;
+ }
+
+ start_pos = end_pos;
+ }
+
+ if (m_props.modeTrad ()) {
+ m_buffer = m_preedit_text;
+ } else {
+ TradSimpConverter::tradToSimp (m_preedit_text.c_str (), m_buffer);
+ }
+
+ StaticText preedit_text (m_buffer);
+ /* underline */
+ preedit_text.appendAttribute (IBUS_ATTR_TYPE_UNDERLINE, IBUS_ATTR_UNDERLINE_SINGLE, 0, -1);
+
+ /* calcuate the cursor position. */
+ size_t cursor = getZhuyinCursor ();
+ Editor::updatePreeditText (preedit_text, cursor, TRUE);
+ return;
+}
+
+gboolean
+PinyinEditor::insert (gint ch)
+{
+ if (('a' <= ch && ch <= 'z')||
+ ('1'<=ch && ch <= '5')) {
+ insert_phonetic (m_text, m_cursor++, ch);
+
+ updateZhuyin ();
+ update ();
+ return TRUE;
+ }
+
+ /* TODO:: handle symbols here. */
+
+ return FALSE;
+}
+
+gboolean
+PinyinEditor::processKeyEvent (guint keyval, guint keycode, guint modifiers)
+{
+ modifiers &= (IBUS_SHIFT_MASK |
+ IBUS_CONTROL_MASK |
+ IBUS_MOD1_MASK |
+ IBUS_SUPER_MASK |
+ IBUS_HYPER_MASK |
+ IBUS_META_MASK |
+ IBUS_LOCK_MASK);
+
+ if (STATE_INPUT == m_input_state) {
+ if (insert (keyval))
+ return TRUE;
+
+ if (processEnter (keyval, keycode, modifiers))
+ return TRUE;
+
+ if (processFunctionKey (keyval, keycode, modifiers))
+ return TRUE;
+ }
+
+ if (STATE_CANDIDATE_SHOWN == m_input_state ||
+ STATE_BUILTIN_SYMBOL_SHOWN == m_input_state /* ||
+ STATE_USER_SYMBOL_LIST_ALL == m_input_state ||
+ STATE_USER_SYMBOL_SHOWN == m_input_state */) {
+ if (processCandidateKey (keyval, keycode, modifiers))
+ return TRUE;
+ }
+
+ return FALSE;
+}