Deal with the appalling mouse pointer colours. (Why doesn't GTK let
[u/mdw/putty] / unix / pterm.c
index 5cd187c..7f632d6 100644 (file)
@@ -32,6 +32,10 @@ struct gui_data {
     GdkCursor *rawcursor, *textcursor;
     GdkColor cols[NCOLOURS];
     GdkColormap *colmap;
+    wchar_t *pastein_data;
+    int pastein_data_len;
+    char *pasteout_data;
+    int pasteout_data_len;
     int font_width, font_height;
 };
 
@@ -266,11 +270,9 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
 
        /*
         * NYI:
-        *  - Shift-PgUp/PgDn for scrollbar
         *  - nethack mode
         *  - alt+numpad
         *  - Compose key (!!! requires Unicode faff before even trying)
-        *  - Shift-Ins for paste (need to deal with pasting first)
         */
 
        /*
@@ -286,6 +288,14 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
            return TRUE;
        }
 
+       /*
+        * Neither does Shift-Ins.
+        */
+       if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
+           request_paste();
+           return TRUE;
+       }
+
        /* ALT+things gives leading Escape. */
        output[0] = '\033';
        strncpy(output+1, event->string, 31);
@@ -616,6 +626,68 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
     return TRUE;
 }
 
+gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
+{
+    struct gui_data *inst = (struct gui_data *)data;
+    int shift, ctrl, alt, x, y, button, act;
+
+    shift = event->state & GDK_SHIFT_MASK;
+    ctrl = event->state & GDK_CONTROL_MASK;
+    alt = event->state & GDK_MOD1_MASK;
+    if (event->button == 1)
+       button = MBT_LEFT;
+    else if (event->button == 2)
+       button = MBT_MIDDLE;
+    else if (event->button == 3)
+       button = MBT_RIGHT;
+    else
+       return FALSE;                  /* don't even know what button! */
+
+    switch (event->type) {
+      case GDK_BUTTON_PRESS: act = MA_CLICK; break;
+      case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
+      case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
+      case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
+      default: return FALSE;          /* don't know this event type */
+    }
+
+    if (send_raw_mouse && !(cfg.mouse_override && shift) &&
+       act != MA_CLICK && act != MA_RELEASE)
+       return TRUE;                   /* we ignore these in raw mouse mode */
+
+    x = (event->x - cfg.window_border) / inst->font_width;
+    y = (event->y - cfg.window_border) / inst->font_height;
+
+    term_mouse(button, act, x, y, shift, ctrl, alt);
+
+    return TRUE;
+}
+
+gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
+{
+    struct gui_data *inst = (struct gui_data *)data;
+    int shift, ctrl, alt, x, y, button;
+
+    shift = event->state & GDK_SHIFT_MASK;
+    ctrl = event->state & GDK_CONTROL_MASK;
+    alt = event->state & GDK_MOD1_MASK;
+    if (event->state & GDK_BUTTON1_MASK)
+       button = MBT_LEFT;
+    else if (event->state & GDK_BUTTON2_MASK)
+       button = MBT_MIDDLE;
+    else if (event->state & GDK_BUTTON3_MASK)
+       button = MBT_RIGHT;
+    else
+       return FALSE;                  /* don't even know what button! */
+
+    x = (event->x - cfg.window_border) / inst->font_width;
+    y = (event->y - cfg.window_border) / inst->font_height;
+
+    term_mouse(button, MA_DRAG, x, y, shift, ctrl, alt);
+
+    return TRUE;
+}
+
 gint timer_func(gpointer data)
 {
     /* struct gui_data *inst = (struct gui_data *)data; */
@@ -692,15 +764,72 @@ void palette_reset(void)
 
 void write_clip(wchar_t * data, int len, int must_deselect)
 {
-    /* FIXME: currently ignored */
+    if (inst->pasteout_data)
+       sfree(inst->pasteout_data);
+    inst->pasteout_data = smalloc(len);
+    inst->pasteout_data_len = len;
+    wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
+            NULL, NULL);
+
+    if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
+                               GDK_CURRENT_TIME)) {
+       gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
+                                GDK_SELECTION_TYPE_STRING, 1);
+    }
+}
+
+void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
+                  guint info, guint time_stamp, gpointer data)
+{
+    gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
+                          inst->pasteout_data, inst->pasteout_data_len);
+}
+
+gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
+                    gpointer data)
+{
+    term_deselect();
+    if (inst->pasteout_data)
+       sfree(inst->pasteout_data);
+    inst->pasteout_data = NULL;
+    inst->pasteout_data_len = 0;
+    return TRUE;
+}
+
+void request_paste(void)
+{
+    /*
+     * In Unix, pasting is asynchronous: all we can do at the
+     * moment is to call gtk_selection_convert(), and when the data
+     * comes back _then_ we can call term_do_paste().
+     */
+    gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
+                         GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
+}
+
+void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
+                       gpointer data)
+{
+    if (seldata->length <= 0 ||
+       seldata->type != GDK_SELECTION_TYPE_STRING)
+       return;                        /* Nothing happens. */
+
+    if (inst->pastein_data)
+       sfree(inst->pastein_data);
+
+    inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
+    inst->pastein_data_len = seldata->length;
+    mb_to_wc(0, 0, seldata->data, seldata->length,
+            inst->pastein_data, inst->pastein_data_len);
+
+    term_do_paste();
 }
 
 void get_clip(wchar_t ** p, int *len)
 {
     if (p) {
-       /* FIXME: currently nonfunctional */
-       *p = NULL;
-       *len = 0;
+       *p = inst->pastein_data;
+       *len = inst->pastein_data_len;
     }
 }
 
@@ -786,7 +915,6 @@ void do_text(Context ctx, int x, int y, char *text, int len,
      *  - cursor shapes other than block
      *  - VT100 line drawing stuff; code pages in general!
      *  - shadow bolding
-     *  - underline
      */
 
     nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
@@ -823,7 +951,8 @@ void do_text(Context ctx, int x, int y, char *text, int len,
            uheight = inst->font_height - 1;
        gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
                      y*inst->font_height + uheight + cfg.window_border,
-                     (x+len)*inst->font_width-1, y*inst->font_height+uheight);
+                     (x+len)*inst->font_width-1+cfg.window_border,
+                     y*inst->font_height + uheight + cfg.window_border);
     }
 
     gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
@@ -864,6 +993,89 @@ void do_cursor(Context ctx, int x, int y, char *text, int len,
     }
 }
 
+GdkCursor *make_mouse_ptr(int cursor_val)
+{
+    /*
+     * Truly hideous hack: GTK doesn't allow us to set the mouse
+     * cursor foreground and background colours unless we've _also_
+     * created our own cursor from bitmaps. Therefore, I need to
+     * load the `cursor' font and draw glyphs from it on to
+     * pixmaps, in order to construct my cursors with the fg and bg
+     * I want. This is a gross hack, but it's more self-contained
+     * than linking in Xlib to find the X window handle to
+     * inst->area and calling XRecolorCursor, and it's more
+     * futureproof than hard-coding the shapes as bitmap arrays.
+     */
+    static GdkFont *cursor_font = NULL;
+    GdkPixmap *source, *mask;
+    GdkGC *gc;
+    GdkColor cfg = { 0, 65535, 65535, 65535 };
+    GdkColor cbg = { 0, 0, 0, 0 };
+    GdkColor dfg = { 1, 65535, 65535, 65535 };
+    GdkColor dbg = { 0, 0, 0, 0 };
+    GdkCursor *ret;
+    gchar text[2];
+    gint lb, rb, wid, asc, desc, w, h, x, y;
+
+    if (cursor_val < 0) {
+       gdk_font_unref(cursor_font);
+       return NULL;
+    }
+
+    if (!cursor_font)
+       cursor_font = gdk_font_load("cursor");
+
+    /*
+     * Get the text extent of the cursor in question. We use the
+     * mask character for this, because it's typically slightly
+     * bigger than the main character.
+     */
+    text[1] = '\0';
+    text[0] = (char)cursor_val + 1;
+    gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
+    w = rb-lb; h = asc+desc; x = -lb; y = asc;
+
+    source = gdk_pixmap_new(NULL, w, h, 1);
+    mask = gdk_pixmap_new(NULL, w, h, 1);
+
+    /*
+     * Draw the mask character on the mask pixmap.
+     */
+    text[1] = '\0';
+    text[0] = (char)cursor_val + 1;
+    gc = gdk_gc_new(mask);
+    gdk_gc_set_foreground(gc, &dbg);
+    gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
+    gdk_gc_set_foreground(gc, &dfg);
+    gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
+    gdk_gc_unref(gc);
+
+    /*
+     * Draw the main character on the source pixmap.
+     */
+    text[1] = '\0';
+    text[0] = (char)cursor_val;
+    gc = gdk_gc_new(source);
+    gdk_gc_set_foreground(gc, &dbg);
+    gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
+    gdk_gc_set_foreground(gc, &dfg);
+    gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
+    gdk_gc_unref(gc);
+
+    /*
+     * Create the cursor.
+     */
+    ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
+
+    /*
+     * Clean up.
+     */
+    gdk_pixmap_unref(source);
+    gdk_pixmap_unref(mask);
+
+    return ret;
+}
+
 void modalfatalbox(char *p, ...)
 {
     va_list ap;
@@ -906,7 +1118,6 @@ int main(int argc, char **argv)
     gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
 
     gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(inst->hbox));
-//    gtk_container_add(GTK_CONTAINER(window), inst->sbar);
 
     {
        GdkGeometry geom;
@@ -921,10 +1132,6 @@ int main(int argc, char **argv)
        gtk_window_set_geometry_hints(GTK_WINDOW(window), inst->area, &geom,
                                      GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
                                      GDK_HINT_RESIZE_INC);
-       // FIXME: base_width and base_height don't seem to work properly.
-       // Wonder if this is because base of _zero_ is deprecated, and we
-       // need a nonzero base size? Will it help if I put in the scrollbar?
-       // FIXME also: the scrollbar is not working, find out why.
     }
 
     gtk_signal_connect(GTK_OBJECT(window), "destroy",
@@ -941,20 +1148,35 @@ int main(int argc, char **argv)
                       GTK_SIGNAL_FUNC(configure_area), inst);
     gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
                       GTK_SIGNAL_FUNC(expose_area), inst);
+    gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
+                      GTK_SIGNAL_FUNC(button_event), inst);
+    gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
+                      GTK_SIGNAL_FUNC(button_event), inst);
+    gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
+                      GTK_SIGNAL_FUNC(motion_event), inst);
+    gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
+                      GTK_SIGNAL_FUNC(selection_received), inst);
+    gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
+                      GTK_SIGNAL_FUNC(selection_get), inst);
+    gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
+                      GTK_SIGNAL_FUNC(selection_clear), inst);
     gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
                       GTK_SIGNAL_FUNC(scrollbar_moved), inst);
     gtk_timeout_add(20, timer_func, inst);
     gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
     gtk_widget_add_events(GTK_WIDGET(inst->area),
-                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
+                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
+                         GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
+                         GDK_BUTTON_MOTION_MASK);
 
     gtk_widget_show(inst->area);
     gtk_widget_show(inst->sbar);
     gtk_widget_show(GTK_WIDGET(inst->hbox));
     gtk_widget_show(window);
 
-    inst->textcursor = gdk_cursor_new(GDK_XTERM);
-    inst->rawcursor = gdk_cursor_new(GDK_ARROW);
+    inst->textcursor = make_mouse_ptr(GDK_XTERM);
+    inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
+    make_mouse_ptr(-1);                       /* clean up cursor font */
     gdk_window_set_cursor(inst->area->window, inst->textcursor);
 
     term_init();