*** empty log message ***
[m17n/m17n-lib-cs.git] / MInputMethod.cs
index e75f5bc..a6c8552 100644 (file)
@@ -34,15 +34,12 @@ namespace M17N.Input
     private static MSymbol Mvariable = "variable";
     private static MSymbol Mcommand = "command";
     private static MSymbol Mmodule = "module";
-    private static MSymbol Mmodule_list = "module-list";
     private static MSymbol Mtitle = "title";
     private static MSymbol Minclude = "include";
     private static MSymbol Mmacro = "macro";
-    private static MSymbol Mmacro_list = "macro-list";
     private static MSymbol Mmap = "map";
     private static MSymbol Mmap_list = "map-list";
     private static MSymbol Mstate = "state";
-    private static MSymbol Mstate_list = "state-list";
     internal static MSymbol Mcandidates = "candidates";
     private static MSymbol Minsert = "insert";
     private static MSymbol Mdelete = "delete";
@@ -70,28 +67,6 @@ namespace M17N.Input
 
     internal static MInputMethod im_global = null;
 
-    // Sub classes
-    private class Exception : System.Exception
-    {
-      bool error;
-
-      public Exception (string msg) : base (msg)
-       {
-         error = true;
-       }
-
-      public Exception (string fmt, params object[] args)
-       : base (String.Format (fmt, args))
-       {
-         error = true;
-       }
-
-      public Exception (string msg, bool error) : base (msg)
-       {
-         this.error = error;
-       }
-    }
-
     [FlagsAttribute]
     private enum LoadStatus
     {
@@ -857,11 +832,7 @@ namespace M17N.Input
       List<MDatabase> list = MDatabase.List (tag);
       M17n.DebugPrint ("Found {0} input methods\n", list.Count);
       foreach (MDatabase mdb in list)
-       {
-         im_table[mdb.tag] = new MInputMethod (mdb.tag);
-       }
-      tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil, "global");
-      im_global = im_table[tag];
+       im_table[mdb.tag] = new MInputMethod (mdb.tag);
     }
 
     // Constructor
@@ -1335,6 +1306,19 @@ namespace M17N.Input
        }
     }
 
+    private Xex.Variable get_global_var (Xex.Symbol name)
+    {
+      if (im_global == null || this != im_global)
+       {
+         tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
+                                  "global");
+         im_global = im_table[tag];
+         if (! im_global.Open ())
+           throw new Exception ("Failed to load global"); 
+       }
+      return im_global.domain.GetVar (name, false);
+    }
+
     private void parse_variables (MPlist plist)
     {
       var_names = new Xex.Symbol[plist.Count];
@@ -1349,8 +1333,7 @@ namespace M17N.Input
          var_names[i] = name;
          p = p.next;
          string desc = (string) parse_description (p);
-         Xex.Variable vari
-           = im_global != null ? im_global.domain.GetVar (name, false) : null;
+         Xex.Variable vari = get_global_var (name);
          if (vari != null)
            domain.Defvar (vari);
          if (desc != null)
@@ -1377,8 +1360,7 @@ namespace M17N.Input
       for (int i = 0; i < node_list.Count; i++)
        {
          Xex.Symbol name = node_list[i].Attributes[0].Value;
-         Xex.Variable vari = im_global.domain.GetVar (name, false);
-
+         Xex.Variable vari = get_global_var (name);
          if (vari != null)
            domain.Defvar (vari);
          else
@@ -1870,17 +1852,13 @@ namespace M17N.Input
     private MText produced;
     private bool active;
     private MText status;
-    private bool status_changed;
     internal MText preedit;
-    private bool preedit_changed;
     internal int cursor_pos;
-    private bool cursor_pos_changed;
     private Candidates candidates;
     private MPlist candidate_group;
     private int candidate_index;
     private int candidate_from, candidate_to;
     private bool candidate_show;
-    private bool candidate_changed;
 
     private Stack<MInputMethod.State> states;
     internal MInputMethod.KeySeq keys;
@@ -1897,6 +1875,25 @@ namespace M17N.Input
 
     internal Xex.Domain domain;
 
+    [FlagsAttribute]
+    public enum ChangedStatus
+    {
+      None =           0x00,
+      StateTitle =     0x01,
+      Preedit   =      0x02,
+      CursorPos =      0x04,
+      CandidateList =  0x08,
+      CandidateIndex = 0x10,
+      CandidateShow =  0x20,
+    }
+
+    private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
+                                                | ChangedStatus.CandidateIndex
+                                                | ChangedStatus.CandidateShow);
+    private ChangedStatus changed;
+
+    public ChangedStatus Changed { get { return changed; } }
+
     public MInputContext (MInputMethod im)
     {
       this.im = im;
@@ -1949,8 +1946,7 @@ namespace M17N.Input
        preedit_replace (cursor_pos, cursor_pos, arg.Intval);
       else
        preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
-      preedit_changed = true;
-      cursor_pos_changed = true;
+      changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
     }
 
     private class Candidates
@@ -2070,9 +2066,8 @@ namespace M17N.Input
       {
        ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
        ic.candidates = null;
-       ic.preedit_changed = true;
-       ic.cursor_pos_changed = true;
-       ic.candidate_changed = true;
+       ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
+                      | CandidateAll);
       }
 
       // Fill the array "group" by candidates stating from INDEX.
@@ -2225,9 +2220,8 @@ namespace M17N.Input
       preedit.PushProp (candidate_from, candidate_to,
                        MInputMethod.Mcandidates, this);
       cursor_pos = candidate_from;
-      preedit_changed = true;
-      cursor_pos_changed = true;
-      candidate_changed = true;
+      changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
+                 | CandidateAll);
     }
 
     internal void insert_candidates (Xex.Term arg)
@@ -2368,20 +2362,19 @@ namespace M17N.Input
        preedit_replace (pos, cursor_pos, null);
       else
        preedit_replace (cursor_pos, pos, null);
-      preedit_changed = true;
-      cursor_pos_changed = true;
+      changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
     }
 
     internal void show ()
     {
       candidate_show = true;
-      candidate_changed = true;
+      changed |= ChangedStatus.CandidateShow;
     }
 
     internal void hide ()
     {
       candidate_show = false;
-      candidate_changed = true;
+      changed |= ChangedStatus.CandidateShow;
     }
 
     internal void move (int pos)
@@ -2393,7 +2386,7 @@ namespace M17N.Input
       if (pos != cursor_pos)
        {
          cursor_pos = pos;
-         preedit_changed = true;
+         changed |= ChangedStatus.Preedit;
        }
     }
 
@@ -2454,7 +2447,7 @@ namespace M17N.Input
     {
       produced.Cat (preedit);
       preedit.Del ();
-      preedit_changed = true;
+      changed |= ChangedStatus.Preedit;
     }
 
     internal void shift (MSymbol sym)
@@ -2493,7 +2486,7 @@ namespace M17N.Input
              status = state.title;
              if (status == null)
                status = im.title;
-             status_changed = true;
+             changed |= ChangedStatus.StateTitle;
              Xex on_entry
                = (Xex) state.branches.Get (MSymbol.t);
              if (on_entry != null)
@@ -2532,9 +2525,12 @@ namespace M17N.Input
 
     internal void HandleKey ()
     {
-      MInputMethod.State state = states.Peek ();
+    }
 
-      
+    public bool Toggle ()
+    {
+      active = ! active;
+      return active;
     }
   }
 }