Make generic memo-event handler, and connect history callback
[xboard.git] / xoptions.c
index 26e7d02..f209698 100644 (file)
@@ -50,6 +50,7 @@ extern char *getenv();
 #include <cairo/cairo.h>
 #include <cairo/cairo-xlib.h>
 #include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>  
 
 #include "common.h"
 #include "backend.h"
@@ -534,13 +535,77 @@ char *translationTable[] = { // beware: order is essential!
    filterTranslations, gameListTranslations, memoTranslations
 };
 
+static gboolean
+ICSKeyEvent(GtkWidget *widget, GdkEventKey *event)
+{
+    switch(event->keyval) {
+      case GDK_Return: IcsKey(0); return TRUE;
+      case GDK_Up:     IcsKey(1); return TRUE;
+      case GDK_Down:  IcsKey(-1); return TRUE;
+      default: return FALSE;
+    }
+}
+
+static gboolean
+MemoEvent(GtkWidget *widget, GdkEvent *event, gpointer gdata)
+{   // handle mouse clicks on text widgets that need it
+    int w, h;
+    int button=10, f=1;
+    Option *opt, *memo = (Option *) gdata;
+    MemoCallback *userHandler = (MemoCallback *) memo->choice;
+    GdkEventButton *bevent = (GdkEventButton *) event;
+    GdkEventMotion *mevent = (GdkEventMotion *) event;
+    GtkTextIter start, end;
+    String val = NULL;
+    gboolean res;
+    gint index, x, y;
+
+    switch(event->type) { // figure out what's up
+       case GDK_MOTION_NOTIFY:
+           f = 0;
+           w = mevent->x; h = mevent->y;
+           break;
+       case GDK_BUTTON_RELEASE:
+           f = -1; // release indicated by negative button numbers
+           w = bevent->x; h = bevent->y;
+           break;
+       case GDK_BUTTON_PRESS:
+           w = bevent->x; h = bevent->y;
+           button = bevent->button;
+// GTK_TODO: is this really the most efficient way to get the character at the mouse cursor???
+           gtk_text_view_window_to_buffer_coords(widget, GTK_TEXT_WINDOW_WIDGET, w, h, &x, &y);
+           gtk_text_view_get_iter_at_location(widget, &start, x, y);
+           gtk_text_buffer_place_cursor(memo->handle, &start);
+           /* get cursor position into index */
+           g_object_get(memo->handle, "cursor-position", &index, NULL);
+           /* get text from textbuffer */
+           gtk_text_buffer_get_start_iter (memo->handle, &start);
+           gtk_text_buffer_get_end_iter (memo->handle, &end);
+           val = gtk_text_buffer_get_text (memo->handle, &start, &end, FALSE); 
+           break;
+       default:
+           return FALSE; // should not happen
+    }
+    button *= f;
+    // hand click parameters as well as text & location to user
+    res = (userHandler) (memo, button, w, h, val, index);
+    if(val) g_free(val);
+    return res;
+}
+
 void
 AddHandler (Option *opt, int nr)
 {
-#ifdef TODO_GTK
     switch(nr) {
-      case 
+      case 0: 
+      case 1: 
+      case 2: break;
+      case 3: g_signal_connect(opt->handle, "key-press-event", G_CALLBACK (ICSKeyEvent), NULL); break; // Input Box
+      case 4: 
+      case 5: 
+      case 6: break;
     }
+#ifdef TODO_GTK
     XtOverrideTranslations(opt->handle, XtParseTranslationTable(translationTable[nr]));
 #endif
 }
@@ -614,10 +679,11 @@ PopDown (DialogClass n)
     
     gtk_widget_hide(shells[n]);
     shellUp[n]--; // count rather than clear
+
     if(n == 0 || n >= PromoDlg) {
         gtk_widget_destroy(shells[n]);
         shells[n] = NULL;
-    }    
+    }
 
     if(marked[n]) {
        MarkMenuItem(marked[n], False);
@@ -633,26 +699,37 @@ PopDown (DialogClass n)
     return 1;
 }
 
-gboolean GenericPopDown(w, event, gdata)
+/* GTK callback used when OK/cancel clicked in genericpopup for non-modal dialog */
+gboolean GenericPopDown(w, resptype, gdata)
      GtkWidget *w;
-     GdkEvent  *event;
-     gpointer  gdata; 
+     GtkResponseType  resptype;
+     gpointer  gdata;
 {
-    int dlg = (intptr_t) gdata; /* dialog number dlgnr */
-    
+    DialogClass dlg = (intptr_t) gdata; /* dialog number dlgnr */
+    GtkWidget *sh = shells[dlg];
+
+    currentOption = dialogOptions[dlg];
+
 #ifdef TODO_GTK
 // I guess BrowserDlg will be abandoned, as GTK has a better browser of its own
-    if(shellUp[BrowserDlg] && dlg != BrowserDlg || dialogError) return; // prevent closing dialog when it has an open file-browse daughter
+    if(shellUp[BrowserDlg] && dlg != BrowserDlg || dialogError) return True; // prevent closing dialog when it has an open file-browse daughter
 #else
-    if(browserUp || dialogError && dlg != FatalDlg) return True; // prevent closing dialog when it has an open file-browse daughter
+    if(browserUp || dialogError && dlg != FatalDlg) return True; // prevent closing dialog when it has an open file-browse or error-popup daughter
 #endif
-    GtkWidget *sh = shells[dlg];
-printf("popdown %d\n", dlg);
     shells[dlg] = w; // make sure we pop down the right one in case of multiple instances
-    PopDown(dlg);
+
+    /* OK pressed */    
+    if (resptype == GTK_RESPONSE_ACCEPT) {
+        if (GenericReadout(currentOption, -1)) PopDown(dlg);
+        return TRUE;
+    } else
+    /* cancel pressed */
+    {
+       if(dlg == BoardWindow) ExitEvent(0);
+       PopDown(dlg);
+    }
     shells[dlg] = sh; // restore
-    if(dlg == BoardWindow) ExitEvent(0);
-    return True; /* don't propagate to default handler */
+    return TRUE;
 }
 
 int AppendText(Option *opt, char *s)
@@ -777,27 +854,6 @@ GraphExpose (Option *opt, int x, int y, int w, int h)
  *) &e, (gpointer) opt); // fake expose event
 }
 
-/* GTK callback used when OK/cancel clicked in genericpopup for non-modal dialog */
-void GenericPopUpCallback(w, resptype, gdata)
-     GtkWidget *w;
-     GtkResponseType  resptype;
-     gpointer  gdata;
-{
-    int data = (intptr_t) gdata; /* dialog number dlgnr */
-    DialogClass dlg;
-
-    currentOption = dialogOptions[dlg=data>>16]; data &= 0xFFFF;
-
-    /* OK pressed */    
-    if (resptype == GTK_RESPONSE_ACCEPT) {
-        if (GenericReadout(currentOption, -1)) PopDown(data);
-        return;
-    }
-
-    /* cancel pressed */
-    PopDown(dlg);    
-}
-
 void GenericCallback(GtkWidget *widget, gpointer gdata)
 {
     const gchar *name;
@@ -815,15 +871,13 @@ void GenericCallback(GtkWidget *widget, gpointer gdata)
     sh = shells[dlg]; // make following line a no-op, as we haven't found out what the real shell is yet (breaks multiple popups of same type!)
 #endif
     oldSh = shells[dlg]; shells[dlg] = sh; // bow to reality
-    
-#ifdef TODO_GTK
+
     if (data == 30000) { // cancel
         PopDown(dlg); 
     } else
     if (data == 30001) { // save buttons imply OK
         if(GenericReadout(currentOption, -1)) PopDown(dlg); // calls OK-proc after full readout, but no popdown if it returns false
     } else
-#endif
 
     if(currentCps) {
         name = gtk_button_get_label (GTK_BUTTON(widget));         
@@ -1054,7 +1108,7 @@ GenericPopUp (Option *option, char *title, DialogClass dlgNr, DialogClass parent
 
     int i, j, arraysize, left, top, height=999, width=1, boxStart;    
     char def[MSG_SIZ], *msg, engineDlg = (currentCps != NULL && dlgNr != BrowserDlg);
-    
+
     if(dlgNr < PromoDlg && shellUp[dlgNr]) return 0; // already up
 
     if(dlgNr && dlgNr < PromoDlg && shells[dlgNr]) { // reusable, and used before (but popped down)
@@ -1177,6 +1231,11 @@ GenericPopUp (Option *option, char *title, DialogClass dlgNr, DialogClass parent
                 else
                     gtk_text_buffer_set_text (textbuffer, "", -1); 
                 option[i].handle = (void*)textbuffer;
+               if(option[i].choice) { // textviews can request a handler for mouse events in the choice field
+                   g_signal_connect(textview, "button-press-event", G_CALLBACK (MemoEvent), (gpointer) &option[i] );
+                   g_signal_connect(textview, "button-release-event", G_CALLBACK (MemoEvent), (gpointer) &option[i] );
+                   g_signal_connect(textview, "motion-notify-event", G_CALLBACK (MemoEvent), (gpointer) &option[i] );
+               }
                 break; 
             }
 
@@ -1407,8 +1466,8 @@ GenericPopUp (Option *option, char *title, DialogClass dlgNr, DialogClass parent
     }
 
     g_signal_connect (dialog, "response",
-                      G_CALLBACK (GenericPopUpCallback),
-                      (gpointer)(intptr_t) (dlgNr<<16 | i));
+                      G_CALLBACK (GenericPopDown),
+                      (gpointer)(intptr_t) dlgNr);
     g_signal_connect (dialog, "delete-event",
                       G_CALLBACK (GenericPopDown),
                       (gpointer)(intptr_t) dlgNr);