diff --git a/scim-wnn/src/Makefile.am b/scim-wnn/src/Makefile.am index 150de1a..41688df 100644 --- a/scim-wnn/src/Makefile.am +++ b/scim-wnn/src/Makefile.am @@ -25,12 +25,13 @@ -DSCIM_WNN_ICON_FILE=\"@SCIM_ICONDIR@/scim-wnn.png\" noinst_HEADERS = scim_wnn_imengine.h wnnproto.h romkan.h romkan_table.h \ - wnnconversion.h scim_wnn_def.h + wnnconversion.h scim_wnn_def.h preeditor.h moduledir = @SCIM_MODULEDIR@/IMEngine module_LTLIBRARIES = wnn.la -wnn_la_SOURCES = scim_wnn_imengine.cpp romkan.cpp wnnconversion.cpp +wnn_la_SOURCES = scim_wnn_imengine.cpp romkan.cpp wnnconversion.cpp \ + preeditor.cpp wnn_la_CFLAGS = @SCIM_CFLAGS@ @SCIM_DEBUG_FLAGS@ wnn_la_CXXFLAGS = @SCIM_CFLAGS@ @SCIM_DEBUG_FLAGS@ diff --git a/scim-wnn/src/preeditor.cpp b/scim-wnn/src/preeditor.cpp new file mode 100644 index 0000000..88dfa37 --- /dev/null +++ b/scim-wnn/src/preeditor.cpp @@ -0,0 +1,191 @@ +/*************************************************************************** + * Copyright (C) 2004 by TAM(Teppei Tamra) * + * tam-t@par.odn.ne.jp * + * * + * 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 of the License, 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include "preeditor.h" + +PreEditor::PreEditor() +{ + iconvert.set_encoding ("EUC-JP"); +} + + +PreEditor::~PreEditor() +{ +} + +WideString PreEditor::text = WideString(); +int PreEditor::pos = 0; +IConvert PreEditor::iconvert; + +/*! + \fn PreEditor::getPos() + */ +int PreEditor::getPos() +{ + return(pos); +} + + +/*! + \fn PreEditor::getTextLength() + */ +int PreEditor::getTextLength() +{ + return(text.length()); +} + + +/*! + \fn PreEditor::setPos(int p) + */ +void PreEditor::setPos(int p) +{ + if (p < 0) p = 0; + else if (p > getTextLength()) p = getTextLength(); + pos = p; +} + + +/*! + \fn PreEditor::clear() + */ +void PreEditor::clear() +{ + text.clear(); +} + + +/*! + \fn PreEditor::insert(char k) + */ +WideString PreEditor::insert(char k) +{ + if (k == 0) return(text); + String s; + s = k; + text = text.substr(0,pos) + utf8_mbstowcs(s) + text.substr(pos); + pos ++; + return(getText()); +} + + + + +/*! + \fn PreEditor::reset() + */ +void PreEditor::reset() +{ + clear(); + pos = 0; +} + + +/*! + \fn PreEditor::getText(bool hosei) + */ +WideString PreEditor::getText(bool hosei) +{ + return(text); +} + + +/*! + \fn PreEditor::backspace() + */ +void PreEditor::backspace() +{ + if (getPos() == 0) return; + text = text.substr(0,pos - 1) + text.substr(pos); + setPos(pos - 1); +} + + + +/*! + \fn PreEditor::del() + */ +void PreEditor::del() +{ + if (getPos() == getTextLength()) return; + text = text.substr(0,pos) + text.substr(pos + 1); +} + + +/*! + \fn PreEditor::convHiraKata(WideString &t) + */ +void PreEditor::convHiraKata(WideString &t) +{ + WideString start_c,end_c,conv_c; + iconvert.convert(start_c,String("��")); + iconvert.convert(end_c,String("��")); + iconvert.convert(conv_c,String("��")); + for(unsigned int i = 0;i < t.size();i ++) { + if ((t[i] >= start_c[0]) && (t[i] <= end_c[0])) + t[i] = t[i] - start_c[0] + conv_c[0]; + } + return; +} + + +/*! + \fn PreEditor::convKataHira(WideString &t) + */ +void PreEditor::convKataHira(WideString &t) +{ + WideString start_c,end_c,conv_c; + iconvert.convert(start_c,String("��")); + iconvert.convert(end_c,String("��")); + iconvert.convert(conv_c,String("��")); + for(unsigned int i = 0;i < t.size();i ++) { + if ((t[i] >= start_c[0]) && (t[i] <= end_c[0])) + t[i] = t[i] - start_c[0] + conv_c[0]; + } + return; + +} + + +/*! + \fn PreEditor::hiraKata() + */ +void PreEditor::hiraKata() +{ + convHiraKata(text); +} + + +/*! + \fn PreEditor::kataHira() + */ +void PreEditor::kataHira() +{ + convKataHira(text); +} + + +/*! + \fn PreEditor::keyEventHook(const KeyEvent &key) + */ +bool PreEditor::keyEventHook(const KeyEvent &key) +{ + return(false); +} + diff --git a/scim-wnn/src/preeditor.h b/scim-wnn/src/preeditor.h new file mode 100644 index 0000000..7b7de62 --- /dev/null +++ b/scim-wnn/src/preeditor.h @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright (C) 2004 by TAM(Teppei Tamra) * + * tam-t@par.odn.ne.jp * + * * + * 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 of the License, 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifndef PREEDITOR_H +#define PREEDITOR_H + +#define Uses_SCIM_ICONV +#include +#include +#include + +using namespace std; +using namespace scim; + +/** +@author TAM(Teppei Tamra) +*/ +class PreEditor{ +public: + PreEditor(); + + ~PreEditor(); + virtual void clear(); + virtual WideString insert(char k); + int getPos(); + virtual void setPos(int p); + int getTextLength(); + virtual void reset(); + WideString getText(bool hosei = false); + virtual void backspace(); + virtual void del(); + virtual void hiraKata(); + virtual void kataHira(); + virtual bool keyEventHook(const KeyEvent &key); + static void convHiraKata(WideString &t); + static void convKataHira(WideString &t); + +protected: + static WideString text; + static int pos; + static IConvert iconvert; + +}; + +#endif diff --git a/scim-wnn/src/romkan.cpp b/scim-wnn/src/romkan.cpp index e2aaa69..8b3aa7f 100644 --- a/scim-wnn/src/romkan.cpp +++ b/scim-wnn/src/romkan.cpp @@ -32,23 +32,6 @@ } -/*! - \fn Romkan::getPos() - */ -int Romkan::getPos() -{ - return(pos); -} - - -/*! - \fn Romkan::getTextLength() - */ -int Romkan::getTextLength() -{ - return(text.length()); -} - /*! \fn Romkan::setPos(int p) @@ -196,40 +179,6 @@ } -/*! - \fn Romkan::convHiraKata(WideString &t) - */ -void Romkan::convHiraKata(WideString &t) -{ - WideString start_c,end_c,conv_c; - iconvert.convert(start_c,String("��")); - iconvert.convert(end_c,String("��")); - iconvert.convert(conv_c,String("��")); - for(unsigned int i = 0;i < t.size();i ++) { - if ((t[i] >= start_c[0]) && (t[i] <= end_c[0])) - t[i] = t[i] - start_c[0] + conv_c[0]; - } - return; -} - - -/*! - \fn Romkan::convKataHira(WideString &t) - */ -void Romkan::convKataHira(WideString &t) -{ - WideString start_c,end_c,conv_c; - iconvert.convert(start_c,String("��")); - iconvert.convert(end_c,String("��")); - iconvert.convert(conv_c,String("��")); - for(unsigned int i = 0;i < t.size();i ++) { - if ((t[i] >= start_c[0]) && (t[i] <= end_c[0])) - t[i] = t[i] - start_c[0] + conv_c[0]; - } - return; - -} - /*! \fn Romkan::hiraKata() diff --git a/scim-wnn/src/romkan.h b/scim-wnn/src/romkan.h index ca3d54e..ba970c0 100644 --- a/scim-wnn/src/romkan.h +++ b/scim-wnn/src/romkan.h @@ -25,6 +25,7 @@ #include #include #include +#include "preeditor.h" using namespace std; using namespace scim; @@ -32,32 +33,27 @@ /** @author TAM(Teppei Tamra) */ -class Romkan{ +class Romkan : public PreEditor { public: Romkan(); ~Romkan(); - void clear(); - WideString insert(char k); - WideString eval(); - int getPos(); - void setPos(int p); - int getTextLength(); - void reset(); - WideString getText(bool hosei = false); - void backspace(); - void del(); - void convHiraKata(WideString &t); - void convKataHira(WideString &t); - void hiraKata(); - void kataHira(); - bool keyEventHook(const KeyEvent &key); + virtual void clear(); + virtual WideString insert(char k); + virtual void setPos(int p); + virtual void reset(); + virtual WideString getText(bool hosei = false); + virtual void backspace(); + virtual void del(); + virtual void hiraKata(); + virtual void kataHira(); + virtual bool keyEventHook(const KeyEvent &key); protected: - WideString text; + WideString eval(); + +protected: String buf; - int pos; - IConvert iconvert; }; #endif diff --git a/scim-wnn/src/scim_wnn_imengine.cpp b/scim-wnn/src/scim_wnn_imengine.cpp index 7a6a3a0..548b31a 100644 --- a/scim-wnn/src/scim_wnn_imengine.cpp +++ b/scim-wnn/src/scim_wnn_imengine.cpp @@ -182,11 +182,13 @@ { m_iconv.set_encoding ("EUC-JP"); m_connected = false; + m_preeditor = new Romkan; init(); } WnnInstance::~ WnnInstance() { + delete m_preeditor; } @@ -276,68 +278,68 @@ */ bool WnnInstance::process_preedit_key_event(const KeyEvent &key) { - if (m_rk.keyEventHook(key)) { + if (m_preeditor->keyEventHook(key)) { show_preedit_string(); - update_preedit_string(m_rk.getText()); - update_preedit_caret(m_rk.getPos()); + update_preedit_string(m_preeditor->getText()); + update_preedit_caret(m_preeditor->getPos()); return(true); } else - if (m_rk.getTextLength()) { + if (m_preeditor->getTextLength()) { if (k_conversion_start.comp(key)) { - startConversion(m_rk.getText(true)); + startConversion(m_preeditor->getText(true)); alp_count ++; return(true); } else if (k_commit.comp(key)) { - commit_string(m_rk.getText(true)); - m_rk.reset(); + commit_string(m_preeditor->getText(true)); + m_preeditor->reset(); hide_preedit_string(); return(true); } else if (k_forward.comp(key) || k_backward.comp(key)) { - k_backward.comp(key) ? m_rk.setPos(m_rk.getPos() - 1) : m_rk.setPos(m_rk.getPos() + 1); - update_preedit_string(m_rk.getText()); - update_preedit_caret(m_rk.getPos()); + k_backward.comp(key) ? m_preeditor->setPos(m_preeditor->getPos() - 1) : m_preeditor->setPos(m_preeditor->getPos() + 1); + update_preedit_string(m_preeditor->getText()); + update_preedit_caret(m_preeditor->getPos()); return(true); } else if (k_backspace.comp(key)) { - m_rk.backspace(); - update_preedit_string(m_rk.getText()); - update_preedit_caret(m_rk.getPos()); + m_preeditor->backspace(); + update_preedit_string(m_preeditor->getText()); + update_preedit_caret(m_preeditor->getPos()); return(true); } else if (k_delete.comp(key)) { - m_rk.del(); - update_preedit_string(m_rk.getText()); - update_preedit_caret(m_rk.getPos()); + m_preeditor->del(); + update_preedit_string(m_preeditor->getText()); + update_preedit_caret(m_preeditor->getPos()); return(true); } else if (k_cancel.comp(key)) { - m_rk.reset(); + m_preeditor->reset(); hide_preedit_string(); return(true); } else if (k_convert_hiragana.comp(key)) { - m_rk.kataHira(); - update_preedit_string(m_rk.getText()); - update_preedit_caret(m_rk.getPos()); + m_preeditor->kataHira(); + update_preedit_string(m_preeditor->getText()); + update_preedit_caret(m_preeditor->getPos()); return(true); } else if (k_convert_katakana.comp(key)) { - m_rk.hiraKata(); - update_preedit_string(m_rk.getText()); - update_preedit_caret(m_rk.getPos()); + m_preeditor->hiraKata(); + update_preedit_string(m_preeditor->getText()); + update_preedit_caret(m_preeditor->getPos()); return(true); } } if (isprint(key.code)) { if (key.mask & (SCIM_KEY_AltMask | SCIM_KEY_ControlMask)) return(false); // �Хåե������λ��ι�Ƭ���ڡ�����ľ�����ϡ� - if ((key.code == SCIM_KEY_space) && (!m_rk.getTextLength())) return(false); + if ((key.code == SCIM_KEY_space) && (!m_preeditor->getTextLength())) return(false); show_preedit_string(); SCIM_DEBUG_IMENGINE(1) << key.get_key_string() << "\n"; - update_preedit_string(m_rk.insert(key.get_ascii_code())); - update_preedit_caret(m_rk.getPos()); + update_preedit_string(m_preeditor->insert(key.get_ascii_code())); + update_preedit_caret(m_preeditor->getPos()); return(true); } return(false); @@ -370,7 +372,7 @@ if (k_commit.comp(key)) { commit_string(wnn.getText()); wnn.updateFrequency(); - m_rk.reset(); + m_preeditor->reset(); wnn.reset(); m_conversion = false; hide_preedit_string(); @@ -383,8 +385,8 @@ if (k_cancel.comp(key) || k_backspace.comp(key)) { wnn.reset(); m_conversion = false; - update_preedit_string(m_rk.getText()); - update_preedit_caret(m_rk.getPos()); + update_preedit_string(m_preeditor->getText()); + update_preedit_caret(m_preeditor->getPos()); hide_lookup_table(); hide_aux_string(); m_lookup = false; @@ -441,7 +443,7 @@ } else if (k_convert_hiragana.comp(key) || k_convert_katakana.comp(key)) { WideString res = m_convList.Yomi; - k_convert_hiragana.comp(key) ? m_rk.convKataHira(res) : m_rk.convHiraKata(res); + k_convert_hiragana.comp(key) ? m_preeditor->convKataHira(res) : m_preeditor->convHiraKata(res); for(unsigned int i = 0;i < m_convList.count;i ++) { if (res == m_convList.kouho[i]) { m_convList.pos = i; @@ -485,7 +487,7 @@ commit_string(wnn.getText()); wnn.updateFrequency(); - m_rk.reset(); + m_preeditor->reset(); wnn.reset(); m_conversion = false; hide_preedit_string(); @@ -503,7 +505,7 @@ void WnnInstance::move_preedit_caret (unsigned int pos) { - //if (!m_conversion) m_rk.setPos(pos); + //if (!m_conversion) m_preeditor->setPos(pos); //update_preedit_caret(pos); } @@ -565,7 +567,7 @@ hide_aux_string(); hide_preedit_string (); wnn.reset(); - m_rk.reset(); + m_preeditor->reset(); } void WnnInstance::focus_in () @@ -577,7 +579,7 @@ if (m_conversion) { commit_string(wnn.getText()); wnn.updateFrequency(); - m_rk.reset(); + m_preeditor->reset(); wnn.reset(); m_conversion = false; hide_preedit_string(); @@ -585,9 +587,9 @@ hide_aux_string(); m_lookup = false; alp_count = 0; - } else if (m_rk.getTextLength()) { - commit_string(m_rk.getText(true)); - m_rk.reset(); + } else if (m_preeditor->getTextLength()) { + commit_string(m_preeditor->getText(true)); + m_preeditor->reset(); hide_preedit_string(); } } diff --git a/scim-wnn/src/scim_wnn_imengine.h b/scim-wnn/src/scim_wnn_imengine.h index d323287..aa175ba 100644 --- a/scim-wnn/src/scim_wnn_imengine.h +++ b/scim-wnn/src/scim_wnn_imengine.h @@ -80,7 +80,7 @@ protected: CommonLookupTable m_lookup_table; - Romkan m_rk; + PreEditor *m_preeditor; WnnConversion wnn; bool m_conversion; bool m_lookup;