diff options
author | Peng Wu <alexepico@gmail.com> | 2011-09-29 14:59:44 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2011-12-22 12:23:13 +0800 |
commit | 6438ef7044c5d6d5e6e187dfd7d5c57deb601e8c (patch) | |
tree | 543dd9a3668f73f7d8ced7af5f7939c8a0c455ac | |
parent | 08242f69f3740431d0fadc352fbcad7535827eb1 (diff) | |
download | ibus-libpinyin-6438ef7044c5d6d5e6e187dfd7d5c57deb601e8c.tar.gz ibus-libpinyin-6438ef7044c5d6d5e6e187dfd7d5c57deb601e8c.tar.xz ibus-libpinyin-6438ef7044c5d6d5e6e187dfd7d5c57deb601e8c.zip |
add PYPBopomofoEngine.cc
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/PYPBopomofoEngine.cc | 254 |
2 files changed, 255 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index e3c8026..b2a2670 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -71,6 +71,7 @@ ibus_engine_pinyin_c_sources = \ PYPDoublePinyinEditor.cc \ PYPBopomofoEditor.cc \ PYPPinyinEngine.cc \ + PYPBopomofoEngine.cc \ $(NULL) ibus_engine_pinyin_h_sources = \ PYBopomofo.h \ diff --git a/src/PYPBopomofoEngine.cc b/src/PYPBopomofoEngine.cc new file mode 100644 index 0000000..e48e9af --- /dev/null +++ b/src/PYPBopomofoEngine.cc @@ -0,0 +1,254 @@ +/* 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> + * Copyright (c) 2010 BYVoid <byvoid1@gmail.com> + * Copyright (c) 2011 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#include "PYPBopomofoEngine.h" +#include <string> +#include "PYPunctEditor.h" +#include "PYPBopomofoEditor.h" +#include "PYFallbackEditor.h" +#include "PYConfig.h" + +using namespace PY; + +/* constructor */ +LibPinyinBopomofoEngine::LibPinyinBopomofoEngine (IBusEngine *engine) + : Engine (engine), + m_props (BopomofoConfig::instance ()), + m_prev_pressed_key (IBUS_VoidSymbol), + m_input_mode (MODE_INIT), + m_fallback_editor (new FallbackEditor (m_props, BopomofoConfig::instance())) +{ + gint i; + + /* create editors */ + m_editors[MODE_INIT].reset (new LibPinyinBopomofoEditor (m_props, BopomofoConfig::instance ())); + m_editors[MODE_PUNCT].reset (new PunctEditor (m_props, BopomofoConfig::instance ())); + + m_props.signalUpdateProperty ().connect + (std::bind (&LibPinyinBopomofoEngine::updateProperty, this, _1)); + + for (i = MODE_INIT; i < MODE_LAST; i++) { + connectEditorSignals (m_editors[i]); + } + + connectEditorSignals (m_fallback_editor); +} + +/* destructor */ +LibPinyinBopomofoEngine::~LibPinyinBopomofoEngine (void) +{ +} + +gboolean +LibPinyinBopomofoEngine::processKeyEvent (guint keyval, guint keycode, guint modifiers) +{ + gboolean retval = FALSE; + + /* check Shift + Release hotkey, + * and then ignore other Release key event */ + if (modifiers & IBUS_RELEASE_MASK) { + /* press and release keyval are same, + * and no other key event between the press and release key event */ + if (m_prev_pressed_key == keyval) { + if (keyval == IBUS_Shift_L || keyval == IBUS_Shift_R) { + if (!m_editors[MODE_INIT]->text ().empty ()) + m_editors[MODE_INIT]->reset (); + m_props.toggleModeChinese (); + return TRUE; + } + } + + if (m_input_mode == MODE_INIT && + m_editors[MODE_INIT]->text ().empty ()) { + /* If it is init mode, and no any previous input text, + * we will let client applications to handle release key event */ + return FALSE; + } else { + return TRUE; + } + } + + /* Toggle simp/trad Chinese Mode when hotkey Ctrl + Shift + F pressed */ + if (keyval == IBUS_F && scmshm_test (modifiers, (IBUS_SHIFT_MASK | IBUS_CONTROL_MASK))) { + m_props.toggleModeSimp(); + m_prev_pressed_key = IBUS_F; + return TRUE; + } + + if (m_props.modeChinese ()) { + if (G_UNLIKELY (m_input_mode == MODE_INIT && + m_editors[MODE_INIT]->text ().empty () && + cmshm_filter (modifiers) == 0 && + keyval == IBUS_grave)){ + /* if BopomofoEditor is empty and get a grave key, + * switch current editor to PunctEditor */ + m_input_mode = MODE_PUNCT; + } + + retval = m_editors[m_input_mode]->processKeyEvent (keyval, keycode, modifiers); + if (G_UNLIKELY (retval && + m_input_mode != MODE_INIT && + m_editors[m_input_mode]->text ().empty ())) + m_input_mode = MODE_INIT; + } + + if (G_UNLIKELY (!retval)) + retval = m_fallback_editor->processKeyEvent (keyval, keycode, modifiers); + + /* store ignored key event by editors */ + m_prev_pressed_key = retval ? IBUS_VoidSymbol : keyval; + + return retval; +} + +void +LibPinyinBopomofoEngine::focusIn (void) +{ + registerProperties (m_props.properties ()); +} + +void +LibPinyinBopomofoEngine::focusOut (void) +{ + reset (); +} + +void +LibPinyinBopomofoEngine::reset (void) +{ + m_prev_pressed_key = IBUS_VoidSymbol; + m_input_mode = MODE_INIT; + for (gint i = 0; i < MODE_LAST; i++) { + m_editors[i]->reset (); + } + m_fallback_editor->reset (); +} + +void +LibPinyinBopomofoEngine::enable (void) +{ + m_props.reset (); +} + +void +LibPinyinBopomofoEngine::disable (void) +{ +} + +void +LibPinyinBopomofoEngine::pageUp (void) +{ + m_editors[m_input_mode]->pageUp (); +} + +void +LibPinyinBopomofoEngine::pageDown (void) +{ + m_editors[m_input_mode]->pageDown (); +} + +void +LibPinyinBopomofoEngine::cursorUp (void) +{ + m_editors[m_input_mode]->cursorUp (); +} + +void +LibPinyinBopomofoEngine::cursorDown (void) +{ + m_editors[m_input_mode]->cursorDown (); +} + +inline void +LibPinyinBopomofoEngine::showSetupDialog (void) +{ + g_assert (FALSE); +} + +gboolean +LibPinyinBopomofoEngine::propertyActivate (const gchar *prop_name, + guint prop_state) +{ + const static std::string setup ("setup"); + if (m_props.propertyActivate (prop_name, prop_state)) { + return TRUE; + } + else if (setup == prop_name) { + showSetupDialog (); + return TRUE; + } + return FALSE; +} + +void +LibPinyinBopomofoEngine::candidateClicked (guint index, + guint button, + guint state) +{ + m_editors[m_input_mode]->candidateClicked (index, button, state); +} + +void +LibPinyinBopomofoEngine::commitText (Text & text) +{ + Engine::commitText (text); + if (m_input_mode != MODE_INIT) + m_input_mode = MODE_INIT; +#if 0 + if (text.text ()) + static_cast<FallbackEditor*> (m_fallback_editor.get ())->setPrevCommittedChar (*text.text ()); + else + static_cast<FallbackEditor*> (m_fallback_editor.get ())->setPrevCommittedChar (0); +#endif +} + +void +LibPinyinBopomofoEngine::connectEditorSignals (EditorPtr editor) +{ + editor->signalCommitText ().connect ( + std::bind (&LibPinyinBopomofoEngine::commitText, this, _1)); + + editor->signalUpdatePreeditText ().connect ( + std::bind (&LibPinyinBopomofoEngine::updatePreeditText, this, _1, _2, _3)); + editor->signalShowPreeditText ().connect ( + std::bind (&LibPinyinBopomofoEngine::showPreeditText, this)); + editor->signalHidePreeditText ().connect ( + std::bind (&LibPinyinBopomofoEngine::hidePreeditText, this)); + + editor->signalUpdateAuxiliaryText ().connect ( + std::bind (&LibPinyinBopomofoEngine::updateAuxiliaryText, this, _1, _2)); + editor->signalShowAuxiliaryText ().connect ( + std::bind (&LibPinyinBopomofoEngine::showAuxiliaryText, this)); + editor->signalHideAuxiliaryText ().connect ( + std::bind (&LibPinyinBopomofoEngine::hideAuxiliaryText, this)); + + editor->signalUpdateLookupTable ().connect ( + std::bind (&LibPinyinBopomofoEngine::updateLookupTable, this, _1, _2)); + editor->signalUpdateLookupTableFast ().connect ( + std::bind (&LibPinyinBopomofoEngine::updateLookupTableFast, this, _1, _2)); + editor->signalShowLookupTable ().connect ( + std::bind (&LibPinyinBopomofoEngine::showLookupTable, this)); + editor->signalHideLookupTable ().connect ( + std::bind (&LibPinyinBopomofoEngine::hideLookupTable, this)); +} + + |