From cde4b45e75e2cf7bcf2b22ab8056322b1ad04cba Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Wed, 2 Jul 2014 17:12:40 +0800 Subject: write method prepareCandidates --- src/ZYSymbols.h | 2 + src/ZYZBopomofoSymbolSection.cc | 3 +- src/ZYZBuiltinSymbolSection.cc | 2 +- src/ZYZPhoneticEditor.cc | 119 +++++++++++++++++++++++++++++++++++++++- src/ZYZPhoneticEditor.h | 1 + 5 files changed, 123 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ZYSymbols.h b/src/ZYSymbols.h index a6421b9..47ea6d2 100644 --- a/src/ZYSymbols.h +++ b/src/ZYSymbols.h @@ -49,6 +49,8 @@ bool half_english_to_full_english (const char key, String & english); #define BUILTIN_SYMBOL_TYPE "builtin" +#define BOPOMOFO_SYMBOL_TYPE "bopomofo" + }; diff --git a/src/ZYZBopomofoSymbolSection.cc b/src/ZYZBopomofoSymbolSection.cc index c8e5716..7ccf5f6 100644 --- a/src/ZYZBopomofoSymbolSection.cc +++ b/src/ZYZBopomofoSymbolSection.cc @@ -22,6 +22,7 @@ #include "ZYZBopomofoSymbolSection.h" #include #include +#include "ZYSymbols.h" namespace ZY { @@ -29,7 +30,7 @@ BopomofoSymbolSection::BopomofoSymbolSection (PhoneticEditor & editor, ZhuyinProperties & props) : SymbolSection (editor, props) { - m_type = "bopomofo"; + m_type = BOPOMOFO_SYMBOL_TYPE; } BopomofoSymbolSection::~BopomofoSymbolSection () diff --git a/src/ZYZBuiltinSymbolSection.cc b/src/ZYZBuiltinSymbolSection.cc index fb4956b..275739c 100644 --- a/src/ZYZBuiltinSymbolSection.cc +++ b/src/ZYZBuiltinSymbolSection.cc @@ -29,7 +29,7 @@ BuiltinSymbolSection::BuiltinSymbolSection (PhoneticEditor & editor, ZhuyinProperties & props) : SymbolSection (editor, props) { - m_type = "builtin"; + m_type = BUILTIN_SYMBOL_TYPE; } BuiltinSymbolSection::~BuiltinSymbolSection () diff --git a/src/ZYZPhoneticEditor.cc b/src/ZYZPhoneticEditor.cc index b670614..5e2a7df 100644 --- a/src/ZYZPhoneticEditor.cc +++ b/src/ZYZPhoneticEditor.cc @@ -169,8 +169,8 @@ PhoneticEditor::processShowCandidateKey (guint keyval, guint keycode, switch (keyval) { case IBUS_Down: case IBUS_KP_Down: - assert (FALSE); /* check phonetic or symbol section here */ + prepareCandidates (); m_input_state = STATE_CANDIDATE_SHOWN; break; @@ -499,7 +499,8 @@ PhoneticEditor::resizeInstances (void) } } -guint PhoneticEditor::getZhuyinCursor (void) +guint +PhoneticEditor::getZhuyinCursor (void) { /* decrement the cursor variable to calculate the zhuyin cursor. */ guint cursor = m_cursor; @@ -638,4 +639,118 @@ PhoneticEditor::insertEnglish (gint ch) return FALSE; } +gboolean +PhoneticEditor::prepareCandidates (void) +{ + /* decrement the cursor variable to calculate the zhuyin cursor. */ + guint cursor = m_cursor; + + const String & enhanced_text = m_text; + + size_t index = 0; + size_t start_pos = 0, end_pos = 0; + + while (end_pos != enhanced_text.size ()) { + if (0 == cursor) + break; + + start_pos = end_pos; + 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); + + size_t section_len = end_pos - start_pos; + + if (cursor < section_len) + break; + + cursor -= section_len; + ++index; + } + + if (SYMBOL_SECTION == type) { + String type, lookup, choice; + get_symbol_section (enhanced_text, start_pos, end_pos, + type, lookup, choice); + --cursor; + } + } + + /* deal with candidates */ + if (m_cursor != 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); + size_t section_len = end_pos - start_pos; + + zhuyin_instance_t * instance = m_instances[index]; + size_t parsed_len = zhuyin_get_parsed_input_length (instance); + + assert (cursor < section_len); + assert (parsed_len <= section_len); + + String lookup; + if (cursor >= parsed_len) { + lookup = section[cursor]; + m_input_state = STATE_BOPOMOFO_SYMBOL_SHOWN; + m_symbol_sections[m_input_state]->initCandidates + (m_instance, lookup); + + update (); + return TRUE; + } else { + guint len = 0; + zhuyin_get_n_zhuyin (instance, &len); + + guint inner_cursor = len; + + guint16 prev_end = 0, cur_end; + for (size_t i = 0; i < len; ++i) { + ChewingKeyRest *pos = NULL; + zhuyin_get_zhuyin_key_rest (instance, i, &pos); + zhuyin_get_zhuyin_key_rest_positions + (instance, pos, NULL, &cur_end); + + if (prev_end < cursor && cursor < cur_end) + inner_cursor = i; + + prev_end = cur_end; + } + + assert (inner_cursor >= 0); + m_input_state = STATE_CANDIDATE_SHOWN; + m_phonetic_section->initCandidates (instance, inner_cursor); + + update (); + return TRUE; + } + } + + if (SYMBOL_SECTION == type) { + String type, lookup, choice; + get_symbol_section (enhanced_text, start_pos, end_pos, + type, lookup, choice); + + if (BUILTIN_SYMBOL_TYPE == type) { + m_input_state = STATE_BUILTIN_SYMBOL_SHOWN; + m_symbol_sections[m_input_state]->initCandidates + (m_instance, lookup); + } else if (BOPOMOFO_SYMBOL_TYPE == type) { + m_input_state = STATE_BOPOMOFO_SYMBOL_SHOWN; + m_symbol_sections[m_input_state]->initCandidates + (m_instance, lookup); + } else + assert (FALSE); + + update (); + return TRUE; + } + } +} + + }; diff --git a/src/ZYZPhoneticEditor.h b/src/ZYZPhoneticEditor.h index 6652ac6..7ea3a58 100644 --- a/src/ZYZPhoneticEditor.h +++ b/src/ZYZPhoneticEditor.h @@ -68,6 +68,7 @@ public: virtual gboolean insertEnglish (gint ch); protected: + gboolean prepareCandidates (void); gboolean selectCandidate (guint i); gboolean selectCandidateInPage (guint i); -- cgit