diff options
author | Hiroshi Sumita <hsumita@chromium.org> | 2011-11-09 11:26:33 +0900 |
---|---|---|
committer | Hiroshi Sumita <hsumita@chromium.org> | 2011-11-09 11:26:33 +0900 |
commit | b002be5085bc626cc28c204666694841b730c721 (patch) | |
tree | 357c301ba9343b092ab4ac9d81d06a6127b02437 | |
parent | 32c13912899c5157109956df739e7ac0393026cb (diff) | |
download | pyzy-b002be5085bc626cc28c204666694841b730c721.tar.gz |
Refactoring APIs.
- Add many APIs to replace processKeyEvent.
- Remove pager related codes.
BUG=None
TEST=Run a test
Review URL: http://codereview.appspot.com/5314054
-rw-r--r-- | po/Makefile.in.in | 279 | ||||
-rw-r--r-- | src/PyZyBopomofoContext.cc | 143 | ||||
-rw-r--r-- | src/PyZyBopomofoContext.h | 43 | ||||
-rw-r--r-- | src/PyZyConfig.cc | 14 | ||||
-rw-r--r-- | src/PyZyConfig.h | 6 | ||||
-rw-r--r-- | src/PyZyDatabase.cc | 8 | ||||
-rw-r--r-- | src/PyZyDatabase.h | 1 | ||||
-rw-r--r-- | src/PyZyDoublePinyinContext.cc | 121 | ||||
-rw-r--r-- | src/PyZyDoublePinyinContext.h | 29 | ||||
-rw-r--r-- | src/PyZyFullPinyinContext.cc | 72 | ||||
-rw-r--r-- | src/PyZyFullPinyinContext.h | 33 | ||||
-rw-r--r-- | src/PyZyInputContext.h | 73 | ||||
-rw-r--r-- | src/PyZyPhoneticContext.cc | 207 | ||||
-rw-r--r-- | src/PyZyPhoneticContext.h | 51 | ||||
-rw-r--r-- | src/PyZyPhraseEditor.cc | 3 | ||||
-rw-r--r-- | src/PyZyPinyinContext.cc | 37 | ||||
-rw-r--r-- | src/PyZyPinyinContext.h | 7 | ||||
-rw-r--r-- | src/PyZyTest.cc | 297 |
18 files changed, 392 insertions, 1032 deletions
diff --git a/po/Makefile.in.in b/po/Makefile.in.in deleted file mode 100644 index eb60dd0..0000000 --- a/po/Makefile.in.in +++ /dev/null @@ -1,279 +0,0 @@ -# Makefile for program source directory in GNU NLS utilities package. -# Copyright (C) 1995, 1996, 1997 by Ulrich Drepper <drepper@gnu.ai.mit.edu> -# -# This file file be copied and used freely without restrictions. It can -# be used in projects which are not available under the GNU Public License -# but which still want to provide support for the GNU gettext functionality. -# Please note that the actual code is *not* freely available. -# -# - Modified by Owen Taylor <otaylor@redhat.com> to use GETTEXT_PACKAGE -# instead of PACKAGE and to look for po2tbl in ./ not in intl/ -# -# - Modified by jacob berkman <jacob@ximian.com> to install -# Makefile.in.in and po2tbl.sed.in for use with glib-gettextize - -GETTEXT_PACKAGE = @GETTEXT_PACKAGE@ -PACKAGE = @PACKAGE@ -VERSION = @VERSION@ - -SHELL = @SHELL@ -@SET_MAKE@ - -srcdir = @srcdir@ -top_srcdir = @top_srcdir@ -VPATH = @srcdir@ - -prefix = @prefix@ -exec_prefix = @exec_prefix@ -datarootdir = @datarootdir@ -datadir = @datadir@ -libdir = @libdir@ -localedir = $(libdir)/locale -gnulocaledir = $(datadir)/locale -gettextsrcdir = $(datadir)/glib-2.0/gettext/po -subdir = po - -INSTALL = @INSTALL@ -INSTALL_DATA = @INSTALL_DATA@ -MKINSTALLDIRS = $(top_srcdir)/@MKINSTALLDIRS@ - -CC = @CC@ -GENCAT = @GENCAT@ -GMSGFMT = @GMSGFMT@ -MSGFMT = @MSGFMT@ -MSGFMT_OPTS = @MSGFMT_OPTS@ -XGETTEXT = @XGETTEXT@ -MSGMERGE = msgmerge - -DEFS = @DEFS@ -CFLAGS = @CFLAGS@ -CPPFLAGS = @CPPFLAGS@ - -INCLUDES = -I.. -I$(top_srcdir)/intl - -COMPILE = $(CC) -c $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(XCFLAGS) - -SOURCES = -POFILES = @POFILES@ -GMOFILES = @GMOFILES@ -DISTFILES = LINGUAS ChangeLog Makefile.in.in POTFILES.in $(GETTEXT_PACKAGE).pot \ -$(POFILES) $(GMOFILES) $(SOURCES) - -POTFILES = \ - -CATALOGS = @CATALOGS@ -CATOBJEXT = @CATOBJEXT@ -INSTOBJEXT = @INSTOBJEXT@ - -.SUFFIXES: -.SUFFIXES: .c .o .po .pox .gmo .mo .msg .cat - -.c.o: - $(COMPILE) $< - -.po.pox: - $(MAKE) $(GETTEXT_PACKAGE).pot - $(MSGMERGE) $< $(srcdir)/$(GETTEXT_PACKAGE).pot -o $*.pox - -.po.mo: - $(MSGFMT) -o $@ $< - -.po.gmo: - file=$(srcdir)/`echo $* | sed 's,.*/,,'`.gmo \ - && rm -f $$file && $(GMSGFMT) $(MSGFMT_OPTS) -o $$file $< - -.po.cat: - sed -f ../intl/po2msg.sed < $< > $*.msg \ - && rm -f $@ && $(GENCAT) $@ $*.msg - - -all: all-@USE_NLS@ - -all-yes: $(CATALOGS) -all-no: - -$(srcdir)/$(GETTEXT_PACKAGE).pot: $(POTFILES) - $(XGETTEXT) --default-domain=$(GETTEXT_PACKAGE) --directory=$(top_srcdir) \ - --add-comments --keyword=_ --keyword=N_ \ - --keyword=C_:1c,2 \ - --keyword=NC_:1c,2 \ - --keyword=g_dcgettext:2 \ - --keyword=g_dngettext:2,3 \ - --keyword=g_dpgettext2:2c,3 \ - --flag=N_:1:pass-c-format \ - --flag=C_:2:pass-c-format \ - --flag=NC_:2:pass-c-format \ - --flag=g_dngettext:2:pass-c-format \ - --flag=g_strdup_printf:1:c-format \ - --flag=g_string_printf:2:c-format \ - --flag=g_string_append_printf:2:c-format \ - --flag=g_error_new:3:c-format \ - --flag=g_set_error:4:c-format \ - --flag=g_markup_printf_escaped:1:c-format \ - --flag=g_log:3:c-format \ - --flag=g_print:1:c-format \ - --flag=g_printerr:1:c-format \ - --flag=g_printf:1:c-format \ - --flag=g_fprintf:2:c-format \ - --flag=g_sprintf:2:c-format \ - --flag=g_snprintf:3:c-format \ - --flag=g_scanner_error:2:c-format \ - --flag=g_scanner_warn:2:c-format \ - --files-from=$(srcdir)/POTFILES.in \ - && test ! -f $(GETTEXT_PACKAGE).po \ - || ( rm -f $(srcdir)/$(GETTEXT_PACKAGE).pot \ - && mv $(GETTEXT_PACKAGE).po $(srcdir)/$(GETTEXT_PACKAGE).pot ) - -install: install-exec install-data -install-exec: -install-data: install-data-@USE_NLS@ -install-data-no: all -install-data-yes: all - if test -r "$(MKINSTALLDIRS)"; then \ - $(MKINSTALLDIRS) $(DESTDIR)$(datadir); \ - else \ - $(SHELL) $(top_srcdir)/mkinstalldirs $(DESTDIR)$(datadir); \ - fi - @catalogs='$(CATALOGS)'; \ - for cat in $$catalogs; do \ - cat=`basename $$cat`; \ - case "$$cat" in \ - *.gmo) destdir=$(gnulocaledir);; \ - *) destdir=$(localedir);; \ - esac; \ - lang=`echo $$cat | sed 's/\$(CATOBJEXT)$$//'`; \ - dir=$(DESTDIR)$$destdir/$$lang/LC_MESSAGES; \ - if test -r "$(MKINSTALLDIRS)"; then \ - $(MKINSTALLDIRS) $$dir; \ - else \ - $(SHELL) $(top_srcdir)/mkinstalldirs $$dir; \ - fi; \ - if test -r $$cat; then \ - $(INSTALL_DATA) $$cat $$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT); \ - echo "installing $$cat as $$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT)"; \ - else \ - $(INSTALL_DATA) $(srcdir)/$$cat $$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT); \ - echo "installing $(srcdir)/$$cat as" \ - "$$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT)"; \ - fi; \ - if test -r $$cat.m; then \ - $(INSTALL_DATA) $$cat.m $$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT).m; \ - echo "installing $$cat.m as $$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT).m"; \ - else \ - if test -r $(srcdir)/$$cat.m ; then \ - $(INSTALL_DATA) $(srcdir)/$$cat.m \ - $$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT).m; \ - echo "installing $(srcdir)/$$cat as" \ - "$$dir/$(GETTEXT_PACKAGE)$(INSTOBJEXT).m"; \ - else \ - true; \ - fi; \ - fi; \ - done - if test "$(PACKAGE)" = "glib"; then \ - if test -r "$(MKINSTALLDIRS)"; then \ - $(MKINSTALLDIRS) $(DESTDIR)$(gettextsrcdir); \ - else \ - $(SHELL) $(top_srcdir)/mkinstalldirs $(DESTDIR)$(gettextsrcdir); \ - fi; \ - $(INSTALL_DATA) $(srcdir)/Makefile.in.in \ - $(DESTDIR)$(gettextsrcdir)/Makefile.in.in; \ - else \ - : ; \ - fi - -# Define this as empty until I found a useful application. -installcheck: - -uninstall: - catalogs='$(CATALOGS)'; \ - for cat in $$catalogs; do \ - cat=`basename $$cat`; \ - lang=`echo $$cat | sed 's/\$(CATOBJEXT)$$//'`; \ - rm -f $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(GETTEXT_PACKAGE)$(INSTOBJEXT); \ - rm -f $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(GETTEXT_PACKAGE)$(INSTOBJEXT).m; \ - rm -f $(DESTDIR)$(gnulocaledir)/$$lang/LC_MESSAGES/$(GETTEXT_PACKAGE)$(INSTOBJEXT); \ - rm -f $(DESTDIR)$(gnulocaledir)/$$lang/LC_MESSAGES/$(GETTEXT_PACKAGE)$(INSTOBJEXT).m; \ - done - if test "$(PACKAGE)" = "glib"; then \ - rm -f $(DESTDIR)$(gettextsrcdir)/Makefile.in.in; \ - fi - -check: all - -dvi info tags TAGS ID: - -mostlyclean: - rm -f core core.* *.pox $(GETTEXT_PACKAGE).po *.old.po cat-id-tbl.tmp - rm -fr *.o - -clean: mostlyclean - -distclean: clean - rm -f Makefile Makefile.in POTFILES *.mo *.msg *.cat *.cat.m - -maintainer-clean: distclean - @echo "This command is intended for maintainers to use;" - @echo "it deletes files that may require special tools to rebuild." - rm -f $(GMOFILES) - -distdir = ../$(GETTEXT_PACKAGE)-$(VERSION)/$(subdir) -dist distdir: $(DISTFILES) - dists="$(DISTFILES)"; \ - for file in $$dists; do \ - ln $(srcdir)/$$file $(distdir) 2> /dev/null \ - || cp -p $(srcdir)/$$file $(distdir); \ - done - -update-po: Makefile - $(MAKE) $(GETTEXT_PACKAGE).pot - tmpdir=`pwd`; \ - cd $(srcdir); \ - catalogs='$(CATALOGS)'; \ - for cat in $$catalogs; do \ - cat=`basename $$cat`; \ - lang=`echo $$cat | sed 's/\$(CATOBJEXT)$$//'`; \ - echo "$$lang:"; \ - if $(MSGMERGE) $$lang.po $(GETTEXT_PACKAGE).pot -o $$tmpdir/$$lang.new.po; then \ - if cmp $$lang.po $$tmpdir/$$lang.new.po >/dev/null 2>&1; then \ - rm -f $$tmpdir/$$lang.new.po; \ - else \ - if mv -f $$tmpdir/$$lang.new.po $$lang.po; then \ - :; \ - else \ - echo "msgmerge for $$lang.po failed: cannot move $$tmpdir/$$lang.new.po to $$lang.po" 1>&2; \ - rm -f $$tmpdir/$$lang.new.po; \ - exit 1; \ - fi; \ - fi; \ - else \ - echo "msgmerge for $$cat failed!"; \ - rm -f $$tmpdir/$$lang.new.po; \ - fi; \ - done - -# POTFILES is created from POTFILES.in by stripping comments, empty lines -# and Intltool tags (enclosed in square brackets), and appending a full -# relative path to them -POTFILES: POTFILES.in - ( if test 'x$(srcdir)' != 'x.'; then \ - posrcprefix='$(top_srcdir)/'; \ - else \ - posrcprefix="../"; \ - fi; \ - rm -f $@-t $@ \ - && (sed -e '/^#/d' \ - -e "s/^\[.*\] +//" \ - -e '/^[ ]*$$/d' \ - -e "s@.*@ $$posrcprefix& \\\\@" < $(srcdir)/$@.in \ - | sed -e '$$s/\\$$//') > $@-t \ - && chmod a-w $@-t \ - && mv $@-t $@ ) - -Makefile: Makefile.in.in ../config.status POTFILES - cd .. \ - && $(SHELL) ./config.status $(subdir)/$@.in - -# Tell versions [3.59,3.63) of GNU make not to export all variables. -# Otherwise a system limit (for SysV at least) may be exceeded. -.NOEXPORT: diff --git a/src/PyZyBopomofoContext.cc b/src/PyZyBopomofoContext.cc index 5d03a08..589c28a 100644 --- a/src/PyZyBopomofoContext.cc +++ b/src/PyZyBopomofoContext.cc @@ -41,8 +41,7 @@ const static gchar * bopomofo_select_keys[] = { }; BopomofoContext::BopomofoContext (Config & config, PhoneticContext::Observer *observer) - : PhoneticContext (config, observer), - m_select_mode (FALSE) + : PhoneticContext (config, observer) { } @@ -50,19 +49,16 @@ BopomofoContext::~BopomofoContext (void) { } -void -BopomofoContext::reset () +bool +BopomofoContext::insert (char ch) { - m_select_mode = FALSE; - PhoneticContext::reset (); -} + if (keyvalToBopomofo (ch) == BOPOMOFO_ZERO) { + return false; + } -gboolean -BopomofoContext::insert (gint ch) -{ /* is full */ if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN)) - return TRUE; + return true; m_text.insert (m_cursor++, ch); @@ -83,14 +79,14 @@ BopomofoContext::insert (gint ch) updateAuxiliaryText (); } } - return TRUE; + return true; } -gboolean +bool BopomofoContext::removeCharBefore (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor --; m_text.erase (m_cursor, 1); @@ -98,27 +94,27 @@ BopomofoContext::removeCharBefore (void) updateSpecialPhrases (); updatePinyin (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::removeCharAfter (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_text.erase (m_cursor, 1); updatePreeditText (); updateAuxiliaryText (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::removeWordBefore (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; guint cursor; @@ -137,57 +133,57 @@ BopomofoContext::removeWordBefore (void) updateSpecialPhrases (); updatePhraseEditor (); update (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::removeWordAfter (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_text.erase (m_cursor, -1); updatePreeditText (); updateAuxiliaryText (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::moveCursorLeft (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor --; updateSpecialPhrases (); updatePinyin (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::moveCursorRight (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_cursor ++; updateSpecialPhrases (); updatePinyin (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::moveCursorLeftByWord (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; if (G_UNLIKELY (m_cursor > m_pinyin_len)) { m_cursor = m_pinyin_len; - return TRUE; + return true; } const Pinyin & p = *m_pinyin.back (); @@ -199,20 +195,20 @@ BopomofoContext::moveCursorLeftByWord (void) updatePhraseEditor (); update (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::moveCursorRightByWord (void) { return moveCursorToEnd (); } -gboolean +bool BopomofoContext::moveCursorToBegin (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor = 0; m_pinyin.clear (); @@ -222,62 +218,20 @@ BopomofoContext::moveCursorToBegin (void) updatePhraseEditor (); update (); - return TRUE; + return true; } -gboolean +bool BopomofoContext::moveCursorToEnd (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_cursor = m_text.length (); updateSpecialPhrases (); updatePinyin (); - return TRUE; -} - -bool -BopomofoContext::processKeyEvent (unsigned short key_event) -{ - const guint key_code = key_event & 0x00ff; - const guint vkey_code = key_event & 0xff00; - - if (vkey_code == 0 && keyvalToBopomofo (key_code) != BOPOMOFO_ZERO) { - m_select_mode = FALSE; - return insert (key_code); - } - - switch (vkey_code) { - case VKEY_BOPOMOFO_SELECT_MODE: - m_select_mode = TRUE; - return TRUE; - - case VKEY_CANDIDATE_SELECT: - case VKEY_CANDIDATE_FOCUS: - case VKEY_CANDIDATE_RESET: - case VKEY_PAGE_PREVIOUS: - case VKEY_PAGE_NEXT: - case VKEY_PAGE_BEGIN: - case VKEY_PAGE_END: - m_select_mode = TRUE; - break; - - case VKEY_CURSOR_RIGHT: - case VKEY_CURSOR_LEFT: - case VKEY_CURSOR_RIGHT_BY_WORD: - case VKEY_CURSOR_LEFT_BY_WORD: - case VKEY_CURSOR_TO_BEGIN: - case VKEY_CURSOR_TO_END: - case VKEY_DELETE_CHARACTER_AFTER: - case VKEY_DELETE_WORD_BEFORE: - case VKEY_DELETE_WORD_AFTER: - m_select_mode = FALSE; - break; - } - - return PhoneticContext::processKeyEvent (key_event); + return true; } void @@ -288,7 +242,7 @@ BopomofoContext::updatePinyin (void) m_pinyin_len = 0; } else { - bopomofo.clear(); + std::wstring bopomofo; for(String::iterator i = m_text.begin (); i != m_text.end (); ++i) { bopomofo += bopomofo_char[keyvalToBopomofo (*i)]; } @@ -353,14 +307,14 @@ BopomofoContext::updateAuxiliaryText (void) } void -BopomofoContext::commit () +BopomofoContext::commit (CommitType type) { if (G_UNLIKELY (m_buffer.empty ())) return; m_buffer.clear (); - if (m_select_mode) { + if (G_LIKELY (type == TYPE_CONVERTED)) { m_buffer << m_phrase_editor.selectedString (); const gchar *p; @@ -376,13 +330,20 @@ BopomofoContext::commit () while (*p != '\0') { m_buffer.appendUnichar ((gunichar)bopomofo_char[keyvalToBopomofo (*p++)]); } + + m_phrase_editor.commit (); } - else { - m_buffer << m_text; + else if (type == TYPE_PHONETIC) { + const gchar *p = m_text; + while (*p != '\0') { + m_buffer.appendUnichar ((gunichar)bopomofo_char[keyvalToBopomofo (*p++)]); + } + } else { + m_buffer = m_text; + m_phrase_editor.reset (); } - - m_phrase_editor.commit (); - reset (); + + resetContext (); update (); PhoneticContext::commitText (m_buffer); } diff --git a/src/PyZyBopomofoContext.h b/src/PyZyBopomofoContext.h index 2d0b61b..28f4841 100644 --- a/src/PyZyBopomofoContext.h +++ b/src/PyZyBopomofoContext.h @@ -33,36 +33,31 @@ class BopomofoContext : public PhoneticContext { public: BopomofoContext (Config & config, PhoneticContext::Observer *observer); - ~BopomofoContext (void); - bool processKeyEvent (unsigned short key_event); - void reset (); + virtual ~BopomofoContext (void); -protected: - std::wstring bopomofo; - gboolean m_select_mode; + /* API of InputContext */ + virtual bool insert (char ch); + virtual void commit (CommitType type); - gboolean processBopomofo (guint keyval, guint keycode, guint modifiers); + virtual bool removeCharBefore (void); + virtual bool removeCharAfter (void); + virtual bool removeWordBefore (void); + virtual bool removeWordAfter (void); - void updateAuxiliaryText (); - void updatePinyin (); - void updatePreeditText (); + virtual bool moveCursorLeft (void); + virtual bool moveCursorRight (void); + virtual bool moveCursorLeftByWord (void); + virtual bool moveCursorRightByWord (void); + virtual bool moveCursorToBegin (void); + virtual bool moveCursorToEnd (void); - void commit (); +protected: + virtual void updateAuxiliaryText (); + virtual void updatePinyin (); + virtual void updatePreeditText (); - gboolean insert (gint ch); + bool processBopomofo (guint keyval, guint keycode, guint modifiers); gint keyvalToBopomofo(gint ch); - - gboolean removeCharBefore (void); - gboolean removeCharAfter (void); - gboolean removeWordBefore (void); - gboolean removeWordAfter (void); - - gboolean moveCursorLeft (void); - gboolean moveCursorRight (void); - gboolean moveCursorLeftByWord (void); - gboolean moveCursorRightByWord (void); - gboolean moveCursorToBegin (void); - gboolean moveCursorToEnd (void); }; }; diff --git a/src/PyZyConfig.cc b/src/PyZyConfig.cc index 344c72a..85e9fc0 100644 --- a/src/PyZyConfig.cc +++ b/src/PyZyConfig.cc @@ -63,7 +63,6 @@ struct Config::ConfigImpl { unsigned int m_option_mask; unsigned int m_double_pinyin_schema; unsigned int m_bopomofo_keyboard_mapping; - unsigned int m_page_size; }; Config::Config () @@ -108,12 +107,6 @@ Config::bopomofoKeyboardMapping (void) const return m_impl->m_bopomofo_keyboard_mapping; } -unsigned int -Config::pageSize (void) const -{ - return m_impl->m_page_size; -} - void Config::setOption (unsigned int value) { @@ -145,17 +138,10 @@ Config::setBopomofoKeyboardMapping (unsigned int value) } void -Config::setPageSize (unsigned int value) -{ - m_impl->m_page_size = value; -} - -void Config::readDefaultValues () { m_impl->m_option = PINYIN_DEFAULT_OPTION; m_impl->m_option_mask = PINYIN_INCOMPLETE_PINYIN | PINYIN_CORRECT_ALL; - m_impl->m_page_size = 5; m_impl->m_double_pinyin_schema = 0; m_impl->m_mode_simp = TRUE; m_impl->m_special_phrases = TRUE; diff --git a/src/PyZyConfig.h b/src/PyZyConfig.h index 0a2c5fe..c6ba4f2 100644 --- a/src/PyZyConfig.h +++ b/src/PyZyConfig.h @@ -48,14 +48,12 @@ public: bool specialPhrases (void) const; bool modeSimp (void) const; unsigned int bopomofoKeyboardMapping (void) const; - unsigned int pageSize (void) const; void setOption (unsigned int value); void setDoublePinyinSchema (unsigned int value); void setSpecialPhrases (bool value); void setModeSimp (bool value); void setBopomofoKeyboardMapping (unsigned int value); - void setPageSize (unsigned int value); protected: struct ConfigImpl; @@ -67,10 +65,10 @@ class PinyinConfig : public Config { public: static void init (); static PinyinConfig & instance (void); + virtual void readDefaultValues (void); protected: PinyinConfig (); - virtual void readDefaultValues (void); private: struct PinyinConfigImpl; @@ -82,10 +80,10 @@ class BopomofoConfig : public Config { public: static void init (); static BopomofoConfig & instance (void); + virtual void readDefaultValues (void); protected: BopomofoConfig (); - virtual void readDefaultValues (void); private: struct BopomofoConfigImpl; diff --git a/src/PyZyDatabase.cc b/src/PyZyDatabase.cc index 2b7a5ea..0e730fe 100644 --- a/src/PyZyDatabase.cc +++ b/src/PyZyDatabase.cc @@ -714,14 +714,6 @@ Database::remove (const Phrase & phrase) } void -Database::init () -{ - if (m_instance.get () == NULL) { - m_instance.reset (new Database ("libpyzy")); - } -} - -void Database::init (const std::string & user_data_dir) { String cache_dir = g_get_user_cache_dir (); diff --git a/src/PyZyDatabase.h b/src/PyZyDatabase.h index 93d16b4..cf13dd0 100644 --- a/src/PyZyDatabase.h +++ b/src/PyZyDatabase.h @@ -63,7 +63,6 @@ protected: Database (const std::string & user_data_dir); public: - static void init (void); static void init (const std::string & data_dir); SQLStmtPtr query (const PinyinArray & pinyin, diff --git a/src/PyZyDoublePinyinContext.cc b/src/PyZyDoublePinyinContext.cc index 336ae67..fce7664 100644 --- a/src/PyZyDoublePinyinContext.cc +++ b/src/PyZyDoublePinyinContext.cc @@ -49,31 +49,35 @@ DoublePinyinContext::DoublePinyinContext (Config & config, PhoneticContext::Obse { } -gboolean -DoublePinyinContext::insert (gint ch) +DoublePinyinContext::~DoublePinyinContext () +{ +} + +bool +DoublePinyinContext::insert (char ch) { gint id; /* is full */ if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN)) - return TRUE; + return true; id = ID (ch); if (id == -1) { /* it is not availidate ch */ - return FALSE; + return false; } if (G_UNLIKELY (m_text.empty () && ID_TO_SHENG (id) == PINYIN_ID_VOID)) { - return FALSE; + return false; } m_text.insert (m_cursor++, ch); - if (m_cursor > m_pinyin_len + 2 || updatePinyin (FALSE) == FALSE) { + if (m_cursor > m_pinyin_len + 2 || updatePinyin (false) == false) { if (!IS_ALPHA (ch)) { m_text.erase (--m_cursor, 1); - return FALSE; + return false; } else { if (updateSpecialPhrases ()) { @@ -83,27 +87,27 @@ DoublePinyinContext::insert (gint ch) updatePreeditText (); updateAuxiliaryText (); } - return TRUE; + return true; } } else { updateSpecialPhrases (); updatePhraseEditor (); update (); - return TRUE; + return true; } } -gboolean +bool DoublePinyinContext::removeCharBefore (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor --; m_text.erase (m_cursor, 1); - if (updatePinyin (FALSE)) { + if (updatePinyin (false)) { updateSpecialPhrases (); updatePhraseEditor (); update (); @@ -117,14 +121,14 @@ DoublePinyinContext::removeCharBefore (void) updateAuxiliaryText (); } } - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::removeCharAfter (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_text.erase (m_cursor, 1); if (updateSpecialPhrases ()) { @@ -134,14 +138,14 @@ DoublePinyinContext::removeCharAfter (void) updatePreeditText (); updateAuxiliaryText (); } - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::removeWordBefore (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; if (G_UNLIKELY (m_cursor > m_pinyin_len)) { m_text.erase (m_pinyin_len, m_cursor - m_pinyin_len); @@ -164,14 +168,14 @@ DoublePinyinContext::removeWordBefore (void) update (); } - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::removeWordAfter (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_text.erase (m_cursor); if (updateSpecialPhrases ()) { @@ -181,14 +185,14 @@ DoublePinyinContext::removeWordAfter (void) updatePreeditText (); updateAuxiliaryText (); } - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::moveCursorLeft (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor --; @@ -202,7 +206,7 @@ DoublePinyinContext::moveCursorLeft (void) } } else { - if (updatePinyin (FALSE)) { + if (updatePinyin (false)) { updateSpecialPhrases (); updatePhraseEditor (); update (); @@ -218,17 +222,17 @@ DoublePinyinContext::moveCursorLeft (void) } } - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::moveCursorRight (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_cursor ++; - if (updatePinyin (FALSE)) { + if (updatePinyin (false)) { updateSpecialPhrases (); updatePhraseEditor (); update (); @@ -242,14 +246,14 @@ DoublePinyinContext::moveCursorRight (void) updateAuxiliaryText (); } } - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::moveCursorLeftByWord (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; if (G_UNLIKELY (m_cursor > m_pinyin_len)) { m_cursor = m_pinyin_len; @@ -269,20 +273,20 @@ DoublePinyinContext::moveCursorLeftByWord (void) update (); } - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::moveCursorRightByWord (void) { return moveCursorToEnd (); } -gboolean +bool DoublePinyinContext::moveCursorToBegin (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor = 0; m_pinyin.clear (); @@ -291,17 +295,17 @@ DoublePinyinContext::moveCursorToBegin (void) updatePhraseEditor (); update (); - return TRUE; + return true; } -gboolean +bool DoublePinyinContext::moveCursorToEnd (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_cursor = m_text.length (); - if (updatePinyin (FALSE)) { + if (updatePinyin (false)) { updateSpecialPhrases (); updatePhraseEditor (); update (); @@ -315,13 +319,7 @@ DoublePinyinContext::moveCursorToEnd (void) updateAuxiliaryText (); } } - return TRUE; -} - -void -DoublePinyinContext::reset (void) -{ - PinyinContext::reset (); + return true; } inline const Pinyin * @@ -385,20 +383,20 @@ DoublePinyinContext::isPinyin (gint i, gint j) } } -inline gboolean -DoublePinyinContext::updatePinyin (gboolean all) +inline bool +DoublePinyinContext::updatePinyin (bool all) { - gboolean retval = FALSE; + gboolean retval = false; if (all && (m_pinyin_len != 0 || !m_pinyin.empty ())) { m_pinyin.clear (); m_pinyin_len = 0; - retval = TRUE; + retval = true; } if (m_pinyin_len > m_cursor) { - retval = TRUE; + retval = true; while (m_pinyin_len > m_cursor) { m_pinyin_len = m_pinyin.back ().begin; m_pinyin.pop_back (); @@ -411,7 +409,7 @@ DoublePinyinContext::updatePinyin (gboolean all) if (m_pinyin_len < m_cursor) { guint len = m_pinyin_len; - if (m_pinyin.empty () == FALSE && + if (m_pinyin.empty () == false && m_pinyin.back ()->flags & PINYIN_INCOMPLETE_PINYIN) { const Pinyin *pinyin = isPinyin (ID (m_text[m_pinyin_len -1]),ID (m_text[m_pinyin_len])); if (pinyin) { @@ -443,26 +441,11 @@ DoublePinyinContext::updatePinyin (gboolean all) } if (len == m_pinyin_len) return retval; - return TRUE; + return true; } return retval; } -bool -DoublePinyinContext::processKeyEvent (unsigned short key_event) -{ - const guint key_code = key_event && 0xff; - - // handle ';' key - if (G_UNLIKELY (key_code == ';')) { - if (insert (key_code)) { - return TRUE; - } - } - - return PinyinContext::processKeyEvent (key_event); -} - }; // namespace PyZy diff --git a/src/PyZyDoublePinyinContext.h b/src/PyZyDoublePinyinContext.h index af5e941..5dcdb4c 100644 --- a/src/PyZyDoublePinyinContext.h +++ b/src/PyZyDoublePinyinContext.h @@ -32,27 +32,24 @@ class DoublePinyinContext : public PinyinContext { public: DoublePinyinContext (Config & config, PhoneticContext::Observer *observer); + virtual ~DoublePinyinContext (); - gboolean insert (gint ch); + virtual bool insert (char ch); - gboolean removeCharBefore (void); - gboolean removeCharAfter (void); - gboolean removeWordBefore (void); - gboolean removeWordAfter (void); + virtual bool removeCharBefore (void); + virtual bool removeCharAfter (void); + virtual bool removeWordBefore (void); + virtual bool removeWordAfter (void); - gboolean moveCursorLeft (void); - gboolean moveCursorRight (void); - gboolean moveCursorLeftByWord (void); - gboolean moveCursorRightByWord (void); - gboolean moveCursorToBegin (void); - gboolean moveCursorToEnd (void); - - /* override virtual functions */ - bool processKeyEvent (unsigned short key_event); - void reset (void); + virtual bool moveCursorLeft (void); + virtual bool moveCursorRight (void); + virtual bool moveCursorLeftByWord (void); + virtual bool moveCursorRightByWord (void); + virtual bool moveCursorToBegin (void); + virtual bool moveCursorToEnd (void); protected: - gboolean updatePinyin (gboolean all); + virtual bool updatePinyin (bool all); private: const Pinyin *isPinyin (gint i, gint j); diff --git a/src/PyZyFullPinyinContext.cc b/src/PyZyFullPinyinContext.cc index 7c31028..ee069ae 100644 --- a/src/PyZyFullPinyinContext.cc +++ b/src/PyZyFullPinyinContext.cc @@ -34,18 +34,12 @@ FullPinyinContext::~FullPinyinContext (void) { } -void -FullPinyinContext::reset (void) -{ - PinyinContext::reset (); -} - -gboolean -FullPinyinContext::insert (gint ch) +bool +FullPinyinContext::insert (char ch) { /* is full */ if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN)) - return TRUE; + return true; m_text.insert (m_cursor++, ch); @@ -66,14 +60,14 @@ FullPinyinContext::insert (gint ch) updateAuxiliaryText (); } } - return TRUE; + return true; } -gboolean +bool FullPinyinContext::removeCharBefore (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor --; m_text.erase (m_cursor, 1); @@ -81,27 +75,27 @@ FullPinyinContext::removeCharBefore (void) updateSpecialPhrases (); updatePinyin (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::removeCharAfter (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_text.erase (m_cursor, 1); updatePreeditText (); updateAuxiliaryText (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::removeWordBefore (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; guint cursor; @@ -120,56 +114,56 @@ FullPinyinContext::removeWordBefore (void) updateSpecialPhrases (); updatePhraseEditor (); update (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::removeWordAfter (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_text.erase (m_cursor, -1); updatePreeditText (); updateAuxiliaryText (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::moveCursorLeft (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor --; updateSpecialPhrases (); updatePinyin (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::moveCursorRight (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_cursor ++; updateSpecialPhrases (); updatePinyin (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::moveCursorLeftByWord (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; if (G_UNLIKELY (m_cursor > m_pinyin_len)) { m_cursor = m_pinyin_len; - return TRUE; + return true; } const Pinyin & p = *m_pinyin.back (); @@ -180,20 +174,20 @@ FullPinyinContext::moveCursorLeftByWord (void) updateSpecialPhrases (); updatePhraseEditor (); update (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::moveCursorRightByWord (void) { return moveCursorToEnd (); } -gboolean +bool FullPinyinContext::moveCursorToBegin (void) { if (G_UNLIKELY (m_cursor == 0)) - return FALSE; + return false; m_cursor = 0; m_pinyin.clear (); @@ -202,20 +196,20 @@ FullPinyinContext::moveCursorToBegin (void) updateSpecialPhrases (); updatePhraseEditor (); update (); - return TRUE; + return true; } -gboolean +bool FullPinyinContext::moveCursorToEnd (void) { if (G_UNLIKELY (m_cursor == m_text.length ())) - return FALSE; + return false; m_cursor = m_text.length (); updateSpecialPhrases (); updatePinyin (); - return TRUE; + return true; } void diff --git a/src/PyZyFullPinyinContext.h b/src/PyZyFullPinyinContext.h index c196f75..3739558 100644 --- a/src/PyZyFullPinyinContext.h +++ b/src/PyZyFullPinyinContext.h @@ -32,30 +32,25 @@ class FullPinyinContext : public PinyinContext { public: FullPinyinContext (Config & config, PhoneticContext::Observer *observer); - ~FullPinyinContext (void); + virtual ~FullPinyinContext (void); public: -#if 0 - virtual bool processKeyEvent (unsigned short key_event); -#endif - virtual void reset (void); + virtual bool insert (char ch); -protected: - gboolean insert (gint ch); - - gboolean removeCharBefore (void); - gboolean removeCharAfter (void); - gboolean removeWordBefore (void); - gboolean removeWordAfter (void); + virtual bool removeCharBefore (void); + virtual bool removeCharAfter (void); + virtual bool removeWordBefore (void); + virtual bool removeWordAfter (void); - gboolean moveCursorLeft (void); - gboolean moveCursorRight (void); - gboolean moveCursorLeftByWord (void); - gboolean moveCursorRightByWord (void); - gboolean moveCursorToBegin (void); - gboolean moveCursorToEnd (void); + virtual bool moveCursorLeft (void); + virtual bool moveCursorRight (void); + virtual bool moveCursorLeftByWord (void); + virtual bool moveCursorRightByWord (void); + virtual bool moveCursorToBegin (void); + virtual bool moveCursorToEnd (void); - void updatePinyin (void); +protected: + virtual void updatePinyin (void); }; diff --git a/src/PyZyInputContext.h b/src/PyZyInputContext.h index 557b5df..c26ee57 100644 --- a/src/PyZyInputContext.h +++ b/src/PyZyInputContext.h @@ -40,39 +40,6 @@ struct Candidate { CandidateType type; }; -// lower 8bits of VKeyCode should be zero. -enum VKeyCode { - VKEY_FIRST = 1 << 8, - - VKEY_COMMIT = (10 << 8), - VKEY_RESET = (11 << 8), - - VKEY_CURSOR_RIGHT = (20 << 8), - VKEY_CURSOR_LEFT = (21 << 8), - VKEY_CURSOR_RIGHT_BY_WORD = (22 << 8), - VKEY_CURSOR_LEFT_BY_WORD = (23 << 8), - VKEY_CURSOR_TO_BEGIN = (24 << 8), - VKEY_CURSOR_TO_END = (25 << 8), - - VKEY_CANDIDATE_SELECT = (30 << 8), - VKEY_CANDIDATE_FOCUS = (31 << 8), - VKEY_CANDIDATE_FOCUS_PREVIOUS = (32 << 8), - VKEY_CANDIDATE_FOCUS_NEXT = (33 << 8), - VKEY_CANDIDATE_RESET = (34 << 8), - - VKEY_PAGE_PREVIOUS = (40 << 8), - VKEY_PAGE_NEXT = (41 << 8), - VKEY_PAGE_BEGIN = (42 << 8), - VKEY_PAGE_END = (43 << 8), - - VKEY_DELETE_CHARACTER_BEFORE = (50 << 8), - VKEY_DELETE_CHARACTER_AFTER = (51 << 8), - VKEY_DELETE_WORD_BEFORE = (52 << 8), - VKEY_DELETE_WORD_AFTER = (53 << 8), - - VKEY_BOPOMOFO_SELECT_MODE = (60 << 8), -}; - class InputContext { public: virtual ~InputContext (void) { } @@ -93,6 +60,37 @@ public: BOPOMOFO, }; + enum CommitType { + TYPE_RAW, + TYPE_PHONETIC, + TYPE_CONVERTED + }; + + /* member functions */ + virtual bool insert (char ch) = 0; + virtual void commit (CommitType type = TYPE_CONVERTED) = 0; + virtual void reset (void) = 0; + + virtual bool moveCursorRight (void) = 0; + virtual bool moveCursorLeft (void) = 0; + virtual bool moveCursorRightByWord (void) = 0; + virtual bool moveCursorLeftByWord (void) = 0; + virtual bool moveCursorToBegin (void) = 0; + virtual bool moveCursorToEnd (void) = 0; + + virtual bool selectCandidate (unsigned int index) = 0; + virtual bool focusCandidatePrevious (void) = 0; + virtual bool focusCandidateNext (void) = 0; + virtual bool resetCandidate (unsigned int index) = 0; + virtual bool unselectCandidates () = 0; + + virtual bool removeCharBefore (void) = 0; + virtual bool removeCharAfter (void) = 0; + virtual bool removeWordBefore (void) = 0; + virtual bool removeWordAfter (void) = 0; + + virtual void bopomofoSelectMode () = 0; + /* static functions */ static void init (); static void init (const std::string & user_data_dir); @@ -101,20 +99,15 @@ public: Config & config, InputContext::Observer * observer); - virtual bool processKeyEvent (unsigned short key_event) = 0; - virtual void update (void) = 0; - virtual void commit (void) = 0; - virtual void reset (void) = 0; - + /* accessors */ + virtual std::string inputText () const = 0; virtual std::string selectedText (void) const = 0; virtual std::string conversionText (void) const = 0; virtual std::string restText (void) const = 0; virtual std::string auxiliaryText (void) const = 0; virtual std::vector<Candidate> candidates () const = 0; - virtual std::string inputText () const = 0; virtual unsigned int cursor () const = 0; virtual unsigned int focusedCandidate () const = 0; - virtual unsigned int page () const = 0; }; }; // namespace PyZy diff --git a/src/PyZyPhoneticContext.cc b/src/PyZyPhoneticContext.cc index ea5b061..13ba387 100644 --- a/src/PyZyPhoneticContext.cc +++ b/src/PyZyPhoneticContext.cc @@ -31,7 +31,7 @@ PhoneticContext::PhoneticContext (Config & config, PhoneticContext::Observer *ob m_phrase_editor (config), m_observer (observer) { - reset (); + resetContext (); } PhoneticContext::~PhoneticContext () @@ -39,124 +39,16 @@ PhoneticContext::~PhoneticContext () } bool -PhoneticContext::processKeyEvent (unsigned short key) -{ - const guint key_code = key & 0x00ff; - const guint vkey_code = key & 0xff00; - - if (vkey_code == 0) { - if ('a' <= key_code && key_code <= 'z') { - return insert (key_code); - } - } else { - const guint key_num = key_code - '1'; - - switch (vkey_code) { - case VKEY_COMMIT: - commit (); - return TRUE; - case VKEY_RESET: - reset (); - update (); - return TRUE; - - case VKEY_CURSOR_RIGHT: - if (m_phrase_editor.unselectCandidates ()) { - update (); - return TRUE; - } else { - return moveCursorRight(); - } - case VKEY_CURSOR_LEFT: - if (m_phrase_editor.unselectCandidates ()) { - update (); - return TRUE; - } else { - return moveCursorLeft(); - } - case VKEY_CURSOR_RIGHT_BY_WORD: - if (m_phrase_editor.unselectCandidates ()) { - update (); - return TRUE; - } else { - return moveCursorRightByWord (); - } - case VKEY_CURSOR_LEFT_BY_WORD: - if (m_phrase_editor.unselectCandidates ()) { - update (); - return TRUE; - } else { - return moveCursorLeftByWord (); - } - case VKEY_CURSOR_TO_BEGIN: - if (m_phrase_editor.unselectCandidates ()) { - update (); - return TRUE; - } else { - return moveCursorToBegin(); - } - case VKEY_CURSOR_TO_END: - if (m_phrase_editor.unselectCandidates ()) { - update (); - return TRUE; - } else { - return moveCursorToEnd(); - } - - case VKEY_CANDIDATE_SELECT: - return selectCandidateInPage (key_num); - case VKEY_CANDIDATE_FOCUS: - return focusCandidateInPage (key_num); - case VKEY_CANDIDATE_FOCUS_PREVIOUS: - return focusCandidatePrevious (); - case VKEY_CANDIDATE_FOCUS_NEXT: - return focusCandidateNext (); - case VKEY_CANDIDATE_RESET: - return resetCandidateInPage (key_num); - - case VKEY_PAGE_PREVIOUS: - selectPage (MAX(1, page ()) - 1); - return TRUE; - case VKEY_PAGE_NEXT: - selectPage (page () + 1); - return TRUE; - case VKEY_PAGE_BEGIN: - selectPage (0); - return TRUE; - case VKEY_PAGE_END: - { - const guint guint_max = (guint)-1; - selectPage (guint_max); - } - return TRUE; - - case VKEY_DELETE_CHARACTER_BEFORE: - return removeCharBefore(); - case VKEY_DELETE_CHARACTER_AFTER: - return removeCharAfter(); - case VKEY_DELETE_WORD_BEFORE: - return removeWordBefore(); - case VKEY_DELETE_WORD_AFTER: - return removeWordAfter(); - } - } - - g_warning ("Can't handle KeyEvent (keycode=%d, vkeycode=%d)\n", - key_code, vkey_code); - return FALSE; -} - -gboolean PhoneticContext::updateSpecialPhrases (void) { guint size = m_special_phrases.size (); m_special_phrases.clear (); if (!m_config.specialPhrases ()) - return FALSE; + return false; if (!m_selected_special_phrase.empty ()) - return FALSE; + return false; guint begin = m_phrase_editor.cursorInChar (); guint end = m_cursor; @@ -171,6 +63,13 @@ PhoneticContext::updateSpecialPhrases (void) } void +PhoneticContext::reset (void) +{ + resetContext (); + update (); +} + +void PhoneticContext::commitText (const std::string & commit_text) { m_observer->commitText (this, commit_text); @@ -222,75 +121,51 @@ PhoneticContext::updatePreeditText (void) } void -PhoneticContext::reset (void) +PhoneticContext::resetContext (void) { + m_cursor = 0; + m_focused_candidate = 0; m_pinyin.clear (); m_pinyin_len = 0; m_phrase_editor.reset (); m_special_phrases.clear (); m_selected_special_phrase.clear (); - - m_cursor = 0; - m_focused_candidate = 0; m_text.clear (); m_preedit_text.clear (); + m_candidates.clear (); + m_auxiliary_text.clear (); } -gboolean -PhoneticContext::focusCandidateInPage (guint i) -{ - return focusCandidate (page () * m_config.pageSize () + i); -} - -gboolean +bool PhoneticContext::focusCandidatePrevious () { if (G_UNLIKELY (m_focused_candidate == 0)) { - return FALSE; + return false; } return focusCandidate (m_focused_candidate - 1); } -gboolean +bool PhoneticContext::focusCandidateNext () { if (G_UNLIKELY (m_focused_candidate >= m_candidates.size ())) { - return FALSE; + return false; } return focusCandidate (m_focused_candidate + 1); } -gboolean -PhoneticContext::focusCandidate (guint i) +bool +PhoneticContext::focusCandidate (unsigned int i) { if (G_UNLIKELY (i >= m_candidates.size ())) { g_warning ("Too big index. Can't focus to selected candidate."); - return FALSE; + return false; } m_focused_candidate = i; update (); - return TRUE; -} - -void -PhoneticContext::selectPage (guint i) -{ - if (G_UNLIKELY (m_candidates.size () == 0)) { - m_focused_candidate = 0; - return; - } - - const guint size = m_config.pageSize (); - const guint max_page = (m_candidates.size () - 1) / size; - if (i > max_page) { - i = max_page; - } - - m_focused_candidate = MIN (m_candidates.size (), i * size + m_focused_candidate % size); - - update (); + return true; } void @@ -301,10 +176,10 @@ PhoneticContext::update () updateAuxiliaryText (); } -gboolean -PhoneticContext::selectCandidate (guint i) +bool +PhoneticContext::selectCandidate (unsigned int i) { - if (i >= m_config.pageSize ()) { + if (i >= m_candidates.size ()) { g_warning ("selectCandidate(%ud): Too big index!\n", i); } @@ -320,7 +195,7 @@ PhoneticContext::selectCandidate (guint i) update (); } - return TRUE; + return true; } i -= m_special_phrases.size (); @@ -334,23 +209,17 @@ PhoneticContext::selectCandidate (guint i) else { commit (); } - return TRUE; + return true; } - return FALSE; -} - -gboolean -PhoneticContext::selectCandidateInPage (guint i) -{ - return selectCandidate (page () * m_config.pageSize () + i); + return false; } -gboolean -PhoneticContext::resetCandidate (guint i) +bool +PhoneticContext::resetCandidate (unsigned int i) { if (i < m_special_phrases.size ()) { - return FALSE; + return false; } i -= m_special_phrases.size (); @@ -358,13 +227,17 @@ PhoneticContext::resetCandidate (guint i) update (); } - return TRUE; + return true; } -gboolean -PhoneticContext::resetCandidateInPage (guint i) +bool +PhoneticContext::unselectCandidates () { - return resetCandidate (page () * m_config.pageSize () + i); + if (!m_phrase_editor.unselectCandidates ()) { + return false; + } + update (); + return true; } }; // namespace PyZy diff --git a/src/PyZyPhoneticContext.h b/src/PyZyPhoneticContext.h index 25a1275..e1a62e6 100644 --- a/src/PyZyPhoneticContext.h +++ b/src/PyZyPhoneticContext.h @@ -50,41 +50,38 @@ public: PhoneticContext (Config & config, PhoneticContext::Observer *observer); virtual ~PhoneticContext (void); - virtual bool processKeyEvent (unsigned short key_event); - - virtual void update (void); - virtual void commit (void) = 0; + /* API of InputContext */ virtual void reset (void); + + bool selectCandidate (unsigned int i); + bool focusCandidate (unsigned int i); + bool focusCandidatePrevious (); + bool focusCandidateNext (); + bool resetCandidate (unsigned int i); + bool unselectCandidates (); /* inline functions */ + virtual void bopomofoSelectMode () { } + + /* Accessors of InputContext. */ + virtual std::string inputText () const { return m_text; } virtual std::string selectedText (void) const { return m_preedit_text.selected_text; } virtual std::string conversionText (void) const { return m_preedit_text.candidate_text; } virtual std::string restText (void) const { return m_preedit_text.rest_text; } virtual std::string auxiliaryText (void) const { return m_auxiliary_text; } virtual std::vector<Candidate> candidates () const { return m_candidates; } - virtual std::string inputText () const { return m_text; } - unsigned int cursor () const { return m_cursor; } - unsigned int focusedCandidate () const { return m_focused_candidate; } - unsigned int page () const { return m_focused_candidate / m_config.pageSize (); } + virtual unsigned int cursor () const { return m_cursor; } + virtual unsigned int focusedCandidate () const { return m_focused_candidate; } protected: - + virtual void resetContext (void); + virtual void update (void); virtual void commitText (const std::string & commit_text); virtual void updateLookupTable (void); virtual void updateAuxiliaryText (void); virtual void updatePreeditText (void); - virtual gboolean updateSpecialPhrases (void); - gboolean selectCandidate (guint i); - gboolean selectCandidateInPage (guint i); - gboolean resetCandidate (guint i); - gboolean resetCandidateInPage (guint i); - gboolean focusCandidate (guint i); - gboolean focusCandidateInPage (guint i); - gboolean focusCandidatePrevious (); - gboolean focusCandidateNext (); - - virtual void selectPage (guint i); + virtual bool updateSpecialPhrases (void); /* inline functions */ void updatePhraseEditor (void) @@ -111,20 +108,6 @@ protected: return (const gchar *)m_text + m_cursor; } - - /* pure virtual functions */ - virtual gboolean insert (gint ch) = 0; - virtual gboolean removeCharBefore (void) = 0; - virtual gboolean removeCharAfter (void) = 0; - virtual gboolean removeWordBefore (void) = 0; - virtual gboolean removeWordAfter (void) = 0; - virtual gboolean moveCursorLeft (void) = 0; - virtual gboolean moveCursorRight (void) = 0; - virtual gboolean moveCursorLeftByWord (void) = 0; - virtual gboolean moveCursorRightByWord (void) = 0; - virtual gboolean moveCursorToBegin (void) = 0; - virtual gboolean moveCursorToEnd (void) = 0; - /* variables */ Config &m_config; guint m_cursor; diff --git a/src/PyZyPhraseEditor.cc b/src/PyZyPhraseEditor.cc index 870dbb1..71ff215 100644 --- a/src/PyZyPhraseEditor.cc +++ b/src/PyZyPhraseEditor.cc @@ -70,8 +70,7 @@ void PhraseEditor::commit (void) { Database::instance ().commit (m_selected_phrases); - // TODO(hsumita): Should we use reset () ? - // reset (); + reset (); } gboolean diff --git a/src/PyZyPinyinContext.cc b/src/PyZyPinyinContext.cc index 5b07c39..dead5f2 100644 --- a/src/PyZyPinyinContext.cc +++ b/src/PyZyPinyinContext.cc @@ -34,33 +34,34 @@ PinyinContext::~PinyinContext () } void -PinyinContext::reset (void) -{ - PhoneticContext::reset (); -} - -void -PinyinContext::commit () +PinyinContext::commit (CommitType type) { if (G_UNLIKELY (m_buffer.empty ())) return; m_buffer.clear (); - m_buffer << m_phrase_editor.selectedString (); - const gchar *p; + if (G_LIKELY(type == TYPE_CONVERTED)) { + m_buffer << m_phrase_editor.selectedString (); - if (m_selected_special_phrase.empty ()) { - p = textAfterPinyin (m_buffer.utf8Length ()); - } - else { - m_buffer << m_selected_special_phrase; - p = textAfterCursor (); + const gchar *p; + + if (m_selected_special_phrase.empty ()) { + p = textAfterPinyin (m_buffer.utf8Length ()); + } + else { + m_buffer << m_selected_special_phrase; + p = textAfterCursor (); + } + m_buffer << p; + + m_phrase_editor.commit (); + } else { + m_buffer = m_text; + m_phrase_editor.reset (); } - m_buffer << p; - m_phrase_editor.commit (); - reset (); + resetContext (); update (); PhoneticContext::commitText (m_buffer); } diff --git a/src/PyZyPinyinContext.h b/src/PyZyPinyinContext.h index 35ba7df..701f253 100644 --- a/src/PyZyPinyinContext.h +++ b/src/PyZyPinyinContext.h @@ -30,12 +30,11 @@ class PinyinContext : public PhoneticContext { public: PinyinContext (Config & config, PhoneticContext::Observer *observer); virtual ~PinyinContext (void); - virtual void reset (void); - virtual void commit (void); + virtual void commit (CommitType type); protected: - void updateAuxiliaryText (void); - void updatePreeditText (void); + virtual void updateAuxiliaryText (void); + virtual void updatePreeditText (void); }; }; // namespace PyZy diff --git a/src/PyZyTest.cc b/src/PyZyTest.cc index 14557df..0c6d27f 100644 --- a/src/PyZyTest.cc +++ b/src/PyZyTest.cc @@ -45,7 +45,7 @@ int main(int argc, char **argv) { class PyZyTest : public testing::Test { protected: virtual void SetUp () { - PhoneticContext::init ("libpyzy-test"); + InputContext::init ("libpyzy-test"); PinyinConfig::init (); BopomofoConfig::init (); } @@ -95,7 +95,7 @@ private: void insertKeys (PhoneticContext &context, const string &keys) { for (guint i = 0; i < keys.size (); ++i) { - context.processKeyEvent (keys[i]); + context.insert (keys[i]); } } @@ -121,7 +121,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_RESET); + context.reset (); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -153,7 +153,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -185,7 +185,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -217,7 +217,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -249,7 +249,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CURSOR_LEFT); + context.moveCursorLeft (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -263,7 +263,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("AA制", context.selectedText ()); @@ -277,7 +277,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -309,7 +309,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_FOCUS | '2'); + context.focusCandidate (1); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -323,10 +323,10 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '4'); + context.selectCandidate (4); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); - EXPECT_EQ ("啊", context.selectedText ()); + EXPECT_EQ ("阿", context.selectedText ()); EXPECT_EQ ("啊之", context.conversionText ()); EXPECT_EQ ("", context.restText ()); EXPECT_EQ ("a zhi|", context.auxiliaryText ()); @@ -337,7 +337,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_CURSOR_LEFT); + context.unselectCandidates (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -351,7 +351,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_CURSOR_LEFT); + context.moveCursorLeft (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -365,7 +365,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent ('i'); + context.insert ('i'); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -379,7 +379,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_DELETE_CHARACTER_BEFORE); + context.removeCharBefore (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -393,7 +393,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CURSOR_RIGHT); + context.moveCursorRight (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -407,7 +407,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_DELETE_WORD_BEFORE); + context.removeWordBefore (); EXPECT_EQ (2, context.cursor ()); EXPECT_EQ ("aa", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -435,7 +435,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '2'); + context.selectCandidate (1); EXPECT_EQ (7, context.cursor ()); EXPECT_EQ ("aanihao", context.inputText ()); EXPECT_EQ ("啊啊", context.selectedText ()); @@ -449,7 +449,7 @@ TEST_F (PyZyTest, FullPinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -486,7 +486,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_RESET); + context.reset (); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -518,7 +518,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -550,7 +550,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -582,7 +582,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -614,7 +614,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CURSOR_LEFT); + context.moveCursorLeft (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -628,7 +628,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("AA制", context.selectedText ()); @@ -642,7 +642,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -674,7 +674,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_FOCUS | '2'); + context.focusCandidate (1); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -688,7 +688,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '4'); + context.selectCandidate (3); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("阿", context.selectedText ()); @@ -703,7 +703,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { // TODO(hsumita) fix. maybe we should get AA制 observer.clear (); - context.processKeyEvent (VKEY_CURSOR_LEFT); + context.unselectCandidates (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -717,7 +717,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_CURSOR_LEFT); + context.moveCursorLeft (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -731,7 +731,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent ('i'); + context.insert ('i'); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -745,7 +745,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_DELETE_CHARACTER_BEFORE); + context.removeCharBefore (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -759,7 +759,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CURSOR_RIGHT); + context.moveCursorRight (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -773,7 +773,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_DELETE_WORD_BEFORE); + context.removeWordBefore (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazh", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -801,7 +801,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '2'); + context.selectCandidate (1); EXPECT_EQ (8, context.cursor ()); EXPECT_EQ ("aazhnihk", context.inputText ()); EXPECT_EQ ("啊", context.selectedText ()); @@ -815,7 +815,7 @@ TEST_F (PyZyTest, DoublePinyinBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -852,7 +852,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_RESET); + context.reset (); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -884,7 +884,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_COMMIT); + context.commit (InputContext::TYPE_RAW); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -916,7 +916,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -948,7 +948,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -980,7 +980,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CURSOR_LEFT); + context.moveCursorLeft (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -994,7 +994,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '1'); + context.selectCandidate (0); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("妈妈好吃哦", context.selectedText ()); @@ -1008,7 +1008,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1040,7 +1040,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_FOCUS | '2'); + context.focusCandidate (1); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1054,7 +1054,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '4'); + context.selectCandidate (3); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("慢慢", context.selectedText ()); @@ -1069,7 +1069,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { // TODO(hsumita) maybe we should get AA制 observer.clear (); - context.processKeyEvent (VKEY_CURSOR_LEFT); + context.unselectCandidates (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1085,7 +1085,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { // TODO (hsumita): should we use a space as a separator of restText // TODO (hsumita): "ㄇㄇㄈㄘ ㄛ" is conversionText or restText ? observer.clear (); - context.processKeyEvent (VKEY_CURSOR_LEFT); + context.moveCursorLeft (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1099,7 +1099,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent ('i'); + context.insert ('i'); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhii", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1113,7 +1113,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_DELETE_CHARACTER_BEFORE); + context.removeCharBefore (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1127,7 +1127,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CURSOR_RIGHT); + context.moveCursorRight (); EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("aazhi", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1141,7 +1141,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_DELETE_WORD_BEFORE); + context.removeWordBefore (); EXPECT_EQ (4, context.cursor ()); EXPECT_EQ ("aazh", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1169,7 +1169,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_CANDIDATE_SELECT | '2'); + context.selectCandidate (1); EXPECT_EQ (8, context.cursor ()); EXPECT_EQ ("aazhnihk", context.inputText ()); EXPECT_EQ ("妈妈", context.selectedText ()); @@ -1183,7 +1183,7 @@ TEST_F (PyZyTest, BopomofoBasicTest) { EXPECT_EQ (0, observer.commitTextCount ()); observer.clear (); - context.processKeyEvent(VKEY_COMMIT); + context.commit (InputContext::TYPE_CONVERTED); EXPECT_EQ (0, context.cursor ()); EXPECT_EQ ("", context.inputText ()); EXPECT_EQ ("", context.selectedText ()); @@ -1198,189 +1198,80 @@ TEST_F (PyZyTest, BopomofoBasicTest) { } } -TEST_F(PyZyTest, PagingTest) { - { +TEST_F (PyZyTest, Commit) { + { // Pinyin commit CounterObserver observer; FullPinyinContext context(PinyinConfig::instance (), &observer); observer.clear (); insertKeys (context, "nihao"); - EXPECT_EQ (5, context.cursor ()); EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); EXPECT_EQ ("你好", context.conversionText ()); - EXPECT_EQ ("", context.restText ()); EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (5, observer.preeditTextCount ()); - EXPECT_EQ (5, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (0, context.focusedCandidate ()); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_FOCUS | '4'); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("尼", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (3, context.focusedCandidate ()); + context.commit (InputContext::TYPE_RAW); + EXPECT_EQ ("nihao", observer.commitedText ()); + context.reset (); observer.clear (); - context.processKeyEvent (VKEY_PAGE_PREVIOUS); - EXPECT_EQ (5, context.cursor ()); + insertKeys (context, "nihao"); EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("尼", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); + EXPECT_EQ ("你好", context.conversionText ()); EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (3, context.focusedCandidate ()); observer.clear (); - context.processKeyEvent (VKEY_PAGE_NEXT); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("腻", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (8, context.focusedCandidate ()); + context.commit (InputContext::TYPE_PHONETIC); + EXPECT_EQ ("nihao", observer.commitedText ()); + context.reset (); observer.clear (); - context.processKeyEvent (VKEY_PAGE_END); - EXPECT_EQ (5, context.cursor ()); + insertKeys (context, "nihao"); EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("匿", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); + EXPECT_EQ ("你好", context.conversionText ()); EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (12, context.focusedCandidate ()); + // To get "你好", we should call selectCandidate(). observer.clear (); - context.processKeyEvent (VKEY_PAGE_NEXT); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("匿", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (12, context.focusedCandidate ()); + context.commit (InputContext::TYPE_CONVERTED); + EXPECT_EQ ("nihao", observer.commitedText ()); + } - observer.clear (); - context.processKeyEvent (VKEY_PAGE_PREVIOUS); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("妮", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (7, context.focusedCandidate ()); + { // Bopomofo commit + CounterObserver observer; + BopomofoContext context(BopomofoConfig::instance (), &observer); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_FOCUS | '1'); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("泥", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (5, context.focusedCandidate ()); + insertKeys (context, "sucl"); + EXPECT_EQ ("sucl", context.inputText ()); + EXPECT_EQ ("你好", context.conversionText ()); + EXPECT_EQ ("ㄋㄧ,ㄏㄠ|", context.auxiliaryText ()); observer.clear (); - context.processKeyEvent (VKEY_PAGE_BEGIN); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("你好", context.conversionText ()); - EXPECT_EQ ("", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (0, context.focusedCandidate ()); + context.commit (InputContext::TYPE_RAW); + EXPECT_EQ ("sucl", observer.commitedText ()); + context.reset (); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_FOCUS_PREVIOUS); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); + insertKeys (context, "sucl"); + EXPECT_EQ ("sucl", context.inputText ()); EXPECT_EQ ("你好", context.conversionText ()); - EXPECT_EQ ("", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (0, observer.preeditTextCount ()); - EXPECT_EQ (0, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (0, context.focusedCandidate ()); + EXPECT_EQ ("ㄋㄧ,ㄏㄠ|", context.auxiliaryText ()); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_FOCUS_NEXT); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); - EXPECT_EQ ("你", context.conversionText ()); - EXPECT_EQ ("hao", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (1, context.focusedCandidate ()); + context.commit (InputContext::TYPE_PHONETIC); + EXPECT_EQ ("ㄋㄧㄏㄠ", observer.commitedText ()); + context.reset (); observer.clear (); - context.processKeyEvent (VKEY_CANDIDATE_FOCUS_PREVIOUS); - EXPECT_EQ (5, context.cursor ()); - EXPECT_EQ ("nihao", context.inputText ()); - EXPECT_EQ ("", context.selectedText ()); + insertKeys (context, "sucl"); + EXPECT_EQ ("sucl", context.inputText ()); EXPECT_EQ ("你好", context.conversionText ()); - EXPECT_EQ ("", context.restText ()); - EXPECT_EQ ("ni hao|", context.auxiliaryText ()); - EXPECT_LT (0, context.candidates ().size ()); - EXPECT_EQ ("", observer.commitedText ()); - EXPECT_EQ (1, observer.preeditTextCount ()); - EXPECT_EQ (1, observer.lookupTableCount ()); - EXPECT_EQ (0, observer.commitTextCount ()); - EXPECT_EQ (0, context.focusedCandidate ()); + EXPECT_EQ ("ㄋㄧ,ㄏㄠ|", context.auxiliaryText ()); + + // To get "你好", we should call selectCandidate(). + observer.clear (); + context.commit (InputContext::TYPE_CONVERTED); + EXPECT_EQ ("ㄋㄧㄏㄠ", observer.commitedText ()); } } |