begin to write stroke editor
authorPeng Wu <alexepico@gmail.com>
Tue, 24 Jul 2012 07:31:27 +0000 (15:31 +0800)
committerPeng Wu <alexepico@gmail.com>
Tue, 24 Jul 2012 07:31:27 +0000 (15:31 +0800)
src/PYEnglishEditor.cc
src/PYStrokeEditor.cc

index 5b49f63..77407f2 100644 (file)
@@ -419,13 +419,12 @@ EnglishEditor::processKeyEvent (guint keyval, guint keycode, guint modifiers)
         g_return_val_if_fail ('v' == keyval, FALSE);
         m_text = "v";
         m_cursor ++;
-    }
-    else {
+    } else {
         g_return_val_if_fail ('v' == m_text[0], FALSE);
         if ((keyval >= 'a' && keyval <= 'z') ||
             (keyval >= 'A' && keyval <= 'Z')) {
             m_text.insert (m_cursor, keyval);
-            m_cursor++;
+            m_cursor ++;
         }
     }
 
@@ -524,7 +523,8 @@ EnglishEditor::processLabelKey (guint keyval)
 }
 
 gboolean
-EnglishEditor::processEnter (guint keyval){
+EnglishEditor::processEnter (guint keyval)
+{
     if (keyval != IBUS_Return)
         return FALSE;
 
index 694fd64..69ad88a 100644 (file)
@@ -134,6 +134,194 @@ private:
     String m_sql;
 };
 
+StrokeEditor::StrokeEditor (PinyinProperties &props, Config &config)
+    : Editor (props, config)
+{
+    m_stroke_database = new StrokeDatabase;
+
+    gboolean result = m_stroke_database->openDatabase
+        (".." G_DIR_SEPARATOR_S "data" G_DIR_SEPARATOR_S "strokes.db") ||
+        m_stroke_database->openDatabase
+        (PKGDATADIR G_DIR_SEPARATOR_S "db" G_DIR_SEPARATOR_S "strokes.db");
+
+    if (!result)
+        g_warning ("can't open strokes database.\n");
+}
+
+StrokeEditor::~StrokeEditor ()
+{
+    delete m_stroke_database;
+    m_stroke_database = NULL;
+}
+
+gboolean
+StrokeEditor::processKeyEvent (guint keyval, guint keycode, guint modifiers)
+{
+    //IBUS_SHIFT_MASK is removed.
+    modifiers &= (IBUS_CONTROL_MASK |
+                  IBUS_MOD1_MASK |
+                  IBUS_SUPER_MASK |
+                  IBUS_HYPER_MASK |
+                  IBUS_META_MASK |
+                  IBUS_LOCK_MASK);
+    if (modifiers)
+        return FALSE;
+
+    //handle backspace/delete here.
+    if (processEditKey (keyval))
+        return TRUE;
+
+    //handle page/cursor up/down here.
+    if (processPageKey (keyval))
+        return TRUE;
+
+    //handle label key select here.
+    if (processLabelKey (keyval))
+        return TRUE;
+
+    if (processSpace (keyval))
+        return TRUE;
+
+    if (processEnter (keyval))
+        return TRUE;
+
+    m_cursor = std::min (m_cursor, (guint)m_text.length ());
+
+    /* Remember the input string. */
+    if (m_cursor == 0) {
+        g_return_val_if_fail ('u' == keyval, FALSE);
+        m_text = "u";
+        m_cursor ++;
+    } else {
+        g_return_val_if_fail ('u' == m_text[0], FALSE);
+        if (keyval >= 'a' && keyval <= 'z') {
+            /* only lower case characters here */
+            m_text.insert (m_cursor, keyval);
+            m_cursor ++;
+        }
+    }
+
+    /* Deal other staff with updateStateFromInput (). */
+    updateStateFromInput ();
+    update ();
+    return TRUE;
+}
+
+gboolean
+StrokeEditor::processEditKey (guint keyval)
+{
+    switch (keyval) {
+    case IBUS_Delete:
+    case IBUS_KP_Delete:
+        removeCharAfter ();
+        updateStateFromInput ();
+        update ();
+        return TRUE;
+    case IBUS_BackSpace:
+        removeCharBefore ();
+        updateStateFromInput ();
+        update ();
+        return TRUE;
+    }
+    return FALSE;
+}
+
+gboolean
+StrokeEditor::processPageKey (guint keyval)
+{
+    switch (keyval) {
+    case IBUS_comma:
+        if (m_config.commaPeriodPage ()) {
+            pageUp ();
+            return TRUE;
+        }
+        break;
+    case IBUS_minus:
+        if (m_config.minusEqualPage ()) {
+            pageUp ();
+            return TRUE;
+        }
+        break;
+    case IBUS_period:
+        if (m_config.commaPeriodPage ()) {
+            pageDown ();
+            return TRUE;
+        }
+        break;
+    case IBUS_equal:
+        if (m_config.minusEqualPage ()) {
+            pageDown ();
+            return TRUE;
+        }
+        break;
+
+    case IBUS_Up:
+    case IBUS_KP_Up:
+        cursorUp ();
+        return TRUE;
+
+    case IBUS_Down:
+    case IBUS_KP_Down:
+        cursorDown ();
+        return TRUE;
+
+    case IBUS_Page_Up:
+    case IBUS_KP_Page_Up:
+        pageUp ();
+        return TRUE;
+
+    case IBUS_Page_Down:
+    case IBUS_KP_Page_Down:
+        pageDown ();
+        return TRUE;
+
+    case IBUS_Escape:
+        reset ();
+        return TRUE;
+    }
+    return FALSE;
+}
+
+gboolean
+StrokeEditor::processLabelKey (guint keyval)
+{
+    switch (keyval) {
+    case '1' ... '9':
+        return selectCandidateInPage (keyval - '1');
+        break;
+    case '0':
+        return selectCandidateInPage (9);
+        break;
+    }
+    return FALSE;
+}
+
+gboolean
+StrokeEditor::processEnter (guint keyval)
+{
+    if (keyval != IBUS_Return)
+        return FALSE;
+
+    if (m_text.length () == 0)
+        return FALSE;
+
+    String preedit = m_text.substr (1);
+    Text text (preedit);
+    commitText (text);
+    reset ();
+    return TRUE;
+}
+
+gboolean
+StrokeEditor::processSpace (guint keyval)
+{
+    if (!(keyval == IBUS_space || keyval == IBUS_KP_Space))
+        return FALSE;
+
+    guint cursor_pos = m_lookup_table.cursorPos ();
+    return selectCandidate (cursor_pos);
+}
+
 #if 0
 
 /* using static initializor to test stroke database here. */