added groups for toolbar items and copy & paste (GTK, Motif)

Sat, 05 Apr 2014 18:41:30 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 05 Apr 2014 18:41:30 +0200
changeset 27
77b09bb52ca0
parent 26
a137277f9173
child 28
794a5c91c479

added groups for toolbar items and copy & paste (GTK, Motif)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/text.h file | annotate | diff | comparison | revisions
ui/gtk/toolbar.c file | annotate | diff | comparison | revisions
ui/gtk/toolbar.h file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/motif/text.c file | annotate | diff | comparison | revisions
ui/motif/text.h file | annotate | diff | comparison | revisions
ui/motif/toolbar.c file | annotate | diff | comparison | revisions
ui/motif/toolbar.h file | annotate | diff | comparison | revisions
ui/motif/toolkit.c file | annotate | diff | comparison | revisions
ui/motif/toolkit.h file | annotate | diff | comparison | revisions
ui/motif/window.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/application/main.c	Sat Apr 05 18:41:30 2014 +0200
@@ -170,7 +170,7 @@
     
     ui_toolitem_st("new", UI_STOCK_NEW, action_new, NULL);
     ui_toolitem_st("open", UI_STOCK_GO_BACK, action_open, NULL);
-    ui_toolitem_st("save", UI_STOCK_GO_FORWARD, action_save, NULL);
+    ui_toolitem_stgr("save", UI_STOCK_GO_FORWARD, action_save, NULL, 1, -1);
     ui_toolitem_st("close", UI_STOCK_CLOSE, action_close, NULL);
     ui_toolitem_st("undo", UI_STOCK_UNDO, action_undo, NULL);
     ui_toolitem_st("redo", UI_STOCK_REDO, action_redo, NULL);
--- a/ui/gtk/text.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/gtk/text.c	Sat Apr 05 18:41:30 2014 +0200
@@ -101,6 +101,8 @@
         value->set = ui_textarea_set;
         value->getsubstr = ui_textarea_getsubstr;
         value->insert = ui_textarea_insert;
+        value->position = ui_textarea_position;
+        value->selection = ui_textarea_selection;
         value->value = NULL;
         GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area));
         value->obj = buf;
@@ -175,6 +177,21 @@
     gtk_text_buffer_insert(text->obj, &offset, str, -1);
 }
 
+int ui_textarea_position(UiText *text) {
+    GtkTextIter begin;
+    GtkTextIter end;
+    gtk_text_buffer_get_selection_bounds(text->obj, &begin, &end);
+    return gtk_text_iter_get_offset(&begin);
+}
+
+void ui_textarea_selection(UiText *text, int *begin, int *end) {
+    GtkTextIter b;
+    GtkTextIter e;
+    gtk_text_buffer_get_selection_bounds(text->obj, &b, &e);
+    *begin = gtk_text_iter_get_offset(&b);
+    *end = gtk_text_iter_get_offset(&e);
+}
+
 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) {
     gtk_widget_grab_focus(widget);
 }
--- a/ui/gtk/text.h	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/gtk/text.h	Sat Apr 05 18:41:30 2014 +0200
@@ -63,6 +63,8 @@
 void ui_textarea_set(UiText *text, char *str);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
 void ui_textarea_insert(UiText *text, int pos, char *str);
+int ui_textarea_position(UiText *text);
+void ui_textarea_selection(UiText *text, int *begin, int *end);
 
 void ui_textarea_realize_event(GtkWidget *widget, gpointer data);
 void ui_textbuf_insert(
--- a/ui/gtk/toolbar.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/gtk/toolbar.c	Sat Apr 05 18:41:30 2014 +0200
@@ -52,12 +52,25 @@
     ucx_map_cstr_put(toolbar_items, name, item);
 }
 
-void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *ud) {
+void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) {
+    ui_toolitem_stgr(name, stockid, f, userdata, -1);
+}
+
+void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) {
     UiStToolItem *item = malloc(sizeof(UiStToolItem));
     item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget;
     item->stockid = stockid;
     item->callback = f;
-    item->userdata = ud;
+    item->userdata = userdata;
+    
+    // add groups
+    va_list ap;
+    va_start(ap, userdata);
+    int group;
+    while((group = va_arg(ap, int)) != -1) {
+        item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group);
+    }
+    va_end(ap);
     
     ucx_map_cstr_put(toolbar_items, name, item);
 }
@@ -114,6 +127,10 @@
     }
     
     gtk_toolbar_insert(tb, button, -1);
+    
+    if(item->groups) {
+        uic_add_group_widget(obj->ctx, button, item->groups);
+    }
 }
 
 void add_toolitem_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) {
@@ -136,4 +153,8 @@
     }
     
     gtk_toolbar_insert(tb, button, -1);
+    
+    if(item->groups) {
+        uic_add_group_widget(obj->ctx, button, item->groups);
+    }
 }
--- a/ui/gtk/toolbar.h	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/gtk/toolbar.h	Sat Apr 05 18:41:30 2014 +0200
@@ -52,6 +52,7 @@
     char           *label;
     ui_callback    callback;
     void           *userdata;
+    UcxList        *groups;
 };
 
 struct UiStToolItem {
@@ -59,6 +60,7 @@
     char           *stockid;
     ui_callback    callback;
     void           *userdata;
+    UcxList        *groups;
 };
 
 void ui_toolbar_init();
--- a/ui/gtk/toolkit.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/gtk/toolkit.c	Sat Apr 05 18:41:30 2014 +0200
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "toolkit.h"
 #include "toolbar.h"
@@ -78,6 +79,22 @@
     gtk_widget_set_sensitive(widget, enabled);
 }
 
+void ui_clipboard_set(char *str) {
+    GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
+    gtk_clipboard_set_text(cb, str, strlen(str));
+}
+
+char* ui_clipboard_get() {
+    GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
+    char *str = gtk_clipboard_wait_for_text(cb);
+    if(str) {
+        char *copy = strdup(str);
+        g_free(str);
+        return copy;
+    } else {
+        return NULL;
+    }
+}
 
 void ui_destroy_userdata(GtkWidget *object, void *userdata) {
     free(userdata);
--- a/ui/motif/text.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/motif/text.c	Sat Apr 05 18:41:30 2014 +0200
@@ -63,6 +63,8 @@
         value->get = ui_textarea_get;
         value->getsubstr = ui_textarea_getsubstr;
         value->insert = ui_textarea_insert;
+        value->position = ui_textarea_position;
+        value->selection = ui_textarea_selection;
         value->value = NULL;
         value->obj = text_area;
         
@@ -116,6 +118,17 @@
     XmTextInsert(text->obj, pos, str);
 }
 
+int ui_textarea_position(UiText *text) {
+    long begin;
+    long end;
+    XmTextGetSelectionPosition(text->obj, &begin, &end);
+    return begin;
+}
+
+void ui_textarea_selection(UiText *text, int *begin, int *end) {
+    XmTextGetSelectionPosition(text->obj, (long*)begin, (long*)end);
+}
+
 UiUndoMgr* ui_create_undomgr() {
     UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
     mgr->begin = NULL;
--- a/ui/motif/text.h	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/motif/text.h	Sat Apr 05 18:41:30 2014 +0200
@@ -63,6 +63,8 @@
 void ui_textarea_set(UiText *text, char *str);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
 void ui_textarea_insert(UiText *text, int pos, char *str);
+int ui_textarea_position(UiText *text);
+void ui_textarea_selection(UiText *text, int *begin, int *end);
 
 UiUndoMgr* ui_create_undomgr();
 void ui_text_selection_callback(
--- a/ui/motif/toolbar.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/motif/toolbar.c	Sat Apr 05 18:41:30 2014 +0200
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 
 #include "toolbar.h"
 #include "button.h"
@@ -43,22 +44,37 @@
     toolbar_items = ucx_map_new(16);
 }
 
-void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
+void ui_toolitem(char *name, char *label, ui_callback f, void *userdata) {
     UiToolItem *item = malloc(sizeof(UiToolItem));
     item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget;
     item->label = label;
     item->callback = f;
-    item->userdata = udata;
+    item->userdata = userdata;
+    item->groups = NULL;
     
     ucx_map_cstr_put(toolbar_items, name, item);
 }
 
-void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *ud) {
+void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) {
+    ui_toolitem_stgr(name, stockid, f, userdata, -1);
+}
+
+void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) {
     UiStToolItem *item = malloc(sizeof(UiStToolItem));
     item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget;
     item->stockid = stockid;
     item->callback = f;
-    item->userdata = ud;
+    item->userdata = userdata;
+    item->groups = NULL;
+    
+    // add groups
+    va_list ap;
+    va_start(ap, userdata);
+    int group;
+    while((group = va_arg(ap, int)) != -1) {
+        item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group);
+    }
+    va_end(ap);
     
     ucx_map_cstr_put(toolbar_items, name, item);
 }
@@ -128,6 +144,10 @@
     }
     
     XtManageChild(button);
+    
+    if(item->groups) {
+        uic_add_group_widget(obj->ctx, button, item->groups);
+    }
 }
 
 void add_toolitem_st_widget(Widget parent, UiStToolItem *item, UiObject *obj) {
@@ -157,4 +177,8 @@
     }
     
     XtManageChild(button);
+    
+    if(item->groups) {
+        uic_add_group_widget(obj->ctx, button, item->groups);
+    }
 }
--- a/ui/motif/toolbar.h	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/motif/toolbar.h	Sat Apr 05 18:41:30 2014 +0200
@@ -52,6 +52,7 @@
     char           *label;
     ui_callback    callback;
     void           *userdata;
+    UcxList        *groups;
 };
 
 struct UiStToolItem {
@@ -59,6 +60,7 @@
     char           *stockid;
     ui_callback    callback;
     void           *userdata;
+    UcxList        *groups;
 };
 
 void ui_toolbar_init();
--- a/ui/motif/toolkit.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/motif/toolkit.c	Sat Apr 05 18:41:30 2014 +0200
@@ -34,9 +34,11 @@
 #include "stock.h"
 #include "../common/document.h"
 #include "../common/properties.h"
+#include "../../ucx/buffer.h"
 
 static XtAppContext app;
 static Display *display;
+static Widget active_window;
 static char *application_name;
 
 static ui_callback appclose_fnc;
@@ -50,7 +52,7 @@
 };
 
 void ui_init(char *appname, int argc, char **argv) { 
-    char *name = application_name ? application_name : "application";
+    application_name = appname;
     
     XtToolkitInitialize();
     XtSetLanguageProc(NULL, NULL, NULL);
@@ -65,6 +67,10 @@
     uic_load_app_properties();
 }
 
+char* ui_appname() {
+    return application_name;
+}
+
 Display* ui_get_display() {
     return display;
 }
@@ -90,3 +96,70 @@
 void ui_set_enabled(UIWIDGET widget, int enabled) {
     XtSetSensitive(widget, enabled);
 }
+
+void ui_clipboard_set(char *str) {
+    printf("copy: {%s}\n", str);
+    int length = strlen(str) + 1;
+    
+    Display *dp = XtDisplayOfObject(active_window);
+    Window window = XtWindowOfObject(active_window);
+    
+    XmString label = XmStringCreateLocalized("toolkit_clipboard");
+    long id = 0;
+    
+    while(XmClipboardStartCopy(
+            dp,
+            window,
+            label,
+            CurrentTime,
+            NULL,
+            NULL,
+            &id) == ClipboardLocked);
+    XmStringFree(label);
+    
+    while(XmClipboardCopy(
+            dp,
+            window,
+            id,
+            "STRING",
+            str, 
+            length,
+            1,
+            NULL) == ClipboardLocked);
+    
+    while(XmClipboardEndCopy(dp, window, id) == ClipboardLocked);
+}
+
+char* ui_clipboard_get() {
+    Display *dp = XtDisplayOfObject(active_window);
+    Window window = XtWindowOfObject(active_window);
+    
+    long id;
+    size_t size = 128;
+    char *buf = malloc(size);
+    
+    int r;
+    for(;;) {
+        r = XmClipboardRetrieve(dp, window, "STRING", buf, size, NULL, &id);
+        if(r == ClipboardSuccess) {
+            break;
+        } else if(r == ClipboardTruncate) {
+            size *= 2;
+            buf = realloc(buf, size);
+        } else if(r == ClipboardNoData) {
+            free(buf);
+            buf = NULL;
+            break;
+        }
+    }
+    
+    return buf;
+}
+
+void ui_set_active_window(Widget w) {
+    active_window = w;
+}
+
+Widget ui_get_active_window() {
+    return active_window;
+}
--- a/ui/motif/toolkit.h	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/motif/toolkit.h	Sat Apr 05 18:41:30 2014 +0200
@@ -46,6 +46,9 @@
     int         value;
 } UiEventData;
 
+void ui_set_active_window(Widget w);
+Widget ui_get_active_window();
+
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/motif/window.c	Sat Apr 05 15:53:41 2014 +0200
+++ b/ui/motif/window.c	Sat Apr 05 18:41:30 2014 +0200
@@ -80,6 +80,9 @@
             window_close_handler,
             obj);
     
+    // TODO: use callback
+    ui_set_active_window(toplevel);
+    
     // menu
     Widget window = XtVaCreateManagedWidget(
             title,

mercurial