(moduledir): Use ${libdir} instead of ${exec_prefix}/lib.
[m17n/m17n-lib.git] / example / medit.c
index 6195380..fe6fad2 100644 (file)
@@ -1,5 +1,5 @@
 /* medit.c -- simple multilingual editor.              -*- coding: euc-jp; -*-
-   Copyright (C) 2003, 2004, 2005
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H15PRO112
 
@@ -17,7 +17,7 @@
 
    You should have received a copy of the GNU Lesser General Public
    License along with the m17n library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    02111-1307, USA.  */
 
 /***en
@@ -49,7 +49,7 @@
 
     This program is to demonstrate how to use the m17n GUI API.
     Although m17n-edit directly uses the GUI API, the API is mainly
-    for toolkit libraries or to implement XOM (X Outout Method), not
+    for toolkit libraries or to implement XOM (X Output Method), not
     for direct use from application programs.
 */
 /***ja
@@ -81,7 +81,7 @@
 
     ¤³¤Î¥×¥í¥°¥é¥à¤Ï m17n GUI API ¤Î»È¤¤Êý¤ò¼¨¤¹¤â¤Î¤Ç¤¢¤ë¡£m17n-edit
     ¤ÏľÀÜ GUI API ¤ò»È¤Ã¤Æ¤¤¤ë¤¬¡¢¤³¤Î API ¤Ï¼ç¤Ë¥Ä¡¼¥ë¥­¥Ã¥È¥é¥¤¥Ö¥é
-    ¥ê¤äXOM (X Outout Method) ¤Î¼ÂÁõÍѤǤ¢¤ê¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥×¥í¥°¥é
+    ¥ê¤äXOM (X Output Method) ¤Î¼ÂÁõÍѤǤ¢¤ê¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥×¥í¥°¥é
     ¥à¤«¤é¤ÎľÀܤÎÍøÍѤò°Õ¿Þ¤·¤Æ¤¤¤Ê¤¤¡£
 */
 
 #include <X11/Xaw/SmeLine.h>
 #include <X11/Xaw/MenuButton.h>
 
-#define VERSION "1.2.0"
-
 /* Global variables.  */
 
 char *filename;
@@ -201,7 +199,9 @@ InputMethodInfo *input_method_table;
 
 int num_input_methods;
 int current_input_method = -1; /* i.e. none */
+int unicode_input_method = -1;
 int auto_input_method = 0;
+int saved_input_method = -3;
 MInputContext *current_input_context;
 
 struct FaceRec
@@ -353,12 +353,8 @@ update_scroll_bar (int from, int to)
 {
   float top = (float) from / nchars;
   float shown = (float) (to - from) / nchars;
-  XtArgVal *l_top = (XtArgVal *) &top;
-  XtArgVal *l_shown = (XtArgVal *) &shown;
 
-  XtSetArg (arg[0], XtNtopOfThumb, *l_top);
-  XtSetArg (arg[1], XtNshown, *l_shown);
-  XtSetValues (SbarWidget, arg, 2);
+  XawScrollbarSetThumb (SbarWidget, top, shown);
 }
 
 
@@ -509,6 +505,7 @@ update_cursor (int pos, int full)
 {
   MDrawMetric rect;
 
+  control.cursor_pos = pos;
   if (full)
     {
       /* CUR is inaccurate.  We can trust only TOP.  */
@@ -865,8 +862,8 @@ show_cursor (XtPointer client_data)
       else
        {
          XtSetArg (arg[0], XtNborderWidth, 1);
-         XtSetArg (arg[1], XtNlabel,
-                   msymbol_name (msymbol_get (sym, Mlanguage)));
+         sym = mlanguage_name (sym);
+         XtSetArg (arg[1], XtNlabel, msymbol_name (sym));
          XtSetValues (CurLangWidget, arg, 2);
        }
       XtSetValues (CurLangWidget, arg, 2);
@@ -1282,7 +1279,7 @@ ButtonProc (Widget w, XEvent *event, String *str, Cardinal *num)
     {
       MText *produced = mtext ();
 
-          minput_lookup (current_input_context, Mnil, NULL, produced);
+      minput_lookup (current_input_context, Mnil, NULL, produced);
       if (mtext_len (produced) > 0)
        {
          insert_chars (produced);
@@ -1551,6 +1548,7 @@ JumpProc (Widget w, XtPointer client_data, XtPointer persent_ptr)
   update_cursor (pos1, 1);
 }
 
+static void InputMethodProc (Widget, XtPointer, XtPointer);
 
 static void
 KeyProc (Widget w, XEvent *event, String *str, Cardinal *num)
@@ -1590,6 +1588,12 @@ KeyProc (Widget w, XEvent *event, String *str, Cardinal *num)
     ret = XLookupString (key_event, buf, sizeof (buf), &keysym, NULL);
   m17n_object_unref (produced);
 
+  if (saved_input_method > -3)
+    {
+      InputMethodProc (w, (XtPointer) saved_input_method, NULL);
+      saved_input_method = -3;
+    }
+
   switch (keysym)
     {
     case XK_Delete:
@@ -1758,6 +1762,14 @@ KeyProc (Widget w, XEvent *event, String *str, Cardinal *num)
              redraw (0, win_height, 1, 1);
              return;
            }
+         else if (buf[0] == '='
+                  && (event->xkey.state & ControlMask)
+                  && unicode_input_method >= 0)
+           {
+             saved_input_method = current_input_method;
+             InputMethodProc (w, (XtPointer) unicode_input_method, NULL);
+             minput_filter (current_input_context, msymbol ("C-u"), NULL);
+           }
          else
            {
              MText *temp = mtext ();
@@ -1929,7 +1941,7 @@ FilterProc (Widget w, XtPointer client_data, XtPointer call_data)
   handle = dlopen (filter_module, RTLD_NOW);
   if (! handle)
     return;
-  *(void **) (&func) = dlsym (handle, "filter");
+  func = (void (*) (MText *, int, int)) dlsym (handle, "filter");
   if (func)
     (*func) (mt, mtext_property_start (selection),
             mtext_property_end (selection));
@@ -2185,7 +2197,7 @@ surrounding_text_handler (MInputContext *ic, MSymbol command)
     {
       int len = (int) mplist_value (ic->plist);
       int pos;
-      MText *surround = NULL;
+      MText *surround;
 
       if (len < 0)
        {
@@ -2201,11 +2213,10 @@ surrounding_text_handler (MInputContext *ic, MSymbol command)
            pos = nchars;
          surround = mtext_duplicate (mt, cursor.from, pos);
        }
-      if (surround)
-       {
-         mplist_set (ic->plist, Mtext, surround);
-         m17n_object_unref (surround);
-       }
+      else
+       surround = mtext ();
+      mplist_set (ic->plist, Mtext, surround);
+      m17n_object_unref (surround);
     }
   else if (command == Minput_delete_surrounding_text)
     {
@@ -2247,8 +2258,8 @@ compare_input_method (const void *elt1, const void *elt2)
     return 1;
   if (im2->language == Mt)
     return -1;
-  lang1 = msymbol_get (im1->language, Mlanguage);
-  lang2 = msymbol_get (im2->language, Mlanguage);
+  lang1 = mlanguage_name (im1->language);
+  lang2 = mlanguage_name (im2->language);
   return strcmp (msymbol_name (lang1), msymbol_name (lang2));
 }
 
@@ -2258,6 +2269,7 @@ setup_input_methods (int with_xim, char *initial_input_method)
   MPlist *plist = mdatabase_list (msymbol ("input-method"), Mnil, Mnil, Mnil);
   MPlist *pl;
   int i;
+  MSymbol Municode = msymbol ("unicode");
 
   num_input_methods = plist ? mplist_length (plist) : 0;
   if (with_xim)
@@ -2291,16 +2303,23 @@ setup_input_methods (int with_xim, char *initial_input_method)
 
   qsort (input_method_table, num_input_methods, sizeof input_method_table[0],
         compare_input_method);
-  mplist_put (minput_driver->callback_list, Minput_status_start,
-             (void *) input_status);
-  mplist_put (minput_driver->callback_list, Minput_status_draw,
-             (void *) input_status);
-  mplist_put (minput_driver->callback_list, Minput_status_done,
-             (void *) input_status);
-  mplist_put (minput_driver->callback_list, Minput_get_surrounding_text,
-             (void *) surrounding_text_handler);
-  mplist_put (minput_driver->callback_list, Minput_delete_surrounding_text,
-             (void *) surrounding_text_handler);
+  for (i = 0; i < num_input_methods; i++)
+    if (input_method_table[i].language == Mt
+       && input_method_table[i].name == Municode)
+      {
+       unicode_input_method = i;
+       break;
+      }
+  mplist_put_func (minput_driver->callback_list, Minput_status_start,
+                  M17N_FUNC (input_status));
+  mplist_put_func (minput_driver->callback_list, Minput_status_draw,
+                  M17N_FUNC (input_status));
+  mplist_put_func (minput_driver->callback_list, Minput_status_done,
+                  M17N_FUNC (input_status));
+  mplist_put_func (minput_driver->callback_list, Minput_get_surrounding_text,
+                  M17N_FUNC (surrounding_text_handler));
+  mplist_put_func (minput_driver->callback_list, Minput_delete_surrounding_text,
+                  M17N_FUNC (surrounding_text_handler));
 
   current_input_context = NULL;
   current_input_method = -1;
@@ -2506,7 +2525,8 @@ create_menu_button (Widget top, Widget parent, Widget left, char *button_name,
   XtSetArg (arg[3], XtNhighlightThickness, 1);
   XtSetArg (arg[4], XtNleft, XawChainLeft);
   XtSetArg (arg[5], XtNright, XawChainLeft);
-  i = 6;
+  XtSetArg (arg[6], XtNinternational, True);
+  i = 7;
   if (left)
     XtSetArg (arg[i], XtNfromHoriz, left), i++;
   button = XtCreateManagedWidget (button_name, menuButtonWidgetClass, parent,
@@ -2652,8 +2672,8 @@ main (int argc, char **argv)
        help_exit (argv[0], 0);
       else if (! strcmp (argv[i], "--version"))
        {
-         printf ("m17n-edit (m17n library) %s\n", VERSION);
-         printf ("Copyright (C) 2003 AIST, JAPAN\n");
+         printf ("m17n-edit (m17n library) %s\n", M17NLIB_VERSION_NAME);
+         printf ("Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 AIST, JAPAN\n");
          exit (0);
        }
       else if (! strcmp (argv[i], "--geometry"))
@@ -2963,7 +2983,7 @@ main (int argc, char **argv)
 
        if (im->language != Mnil && im->language != Mt)
          {
-           MSymbol sym = msymbol_get (im->language, Mlanguage);
+           MSymbol sym = mlanguage_name (im->language);
            if (sym == Mnil)
              name1 = msymbol_name (im->language);
            else
@@ -3090,7 +3110,7 @@ main (int argc, char **argv)
            MSymbol fullname;
 
            if (sym != Mnil
-               && ((fullname = msymbol_get (sym, Mlanguage)) != Mnil))
+               && ((fullname = mlanguage_name (sym)) != Mnil))
              {
                char *name = msymbol_name (fullname);
                char c = name[0];