added toolbar

Sat, 22 Mar 2014 10:37:12 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 22 Mar 2014 10:37:12 +0100
changeset 3
c1a75454b444
parent 2
eeb50c534497
child 4
39b9b86ec452

added toolbar

application/main.c file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/document.c file | annotate | diff | comparison | revisions
ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/button.h file | annotate | diff | comparison | revisions
ui/gtk/objs.mk 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/gtk/window.c file | annotate | diff | comparison | revisions
ui/motif/button.c file | annotate | diff | comparison | revisions
ui/motif/button.h file | annotate | diff | comparison | revisions
ui/motif/menu.c file | annotate | diff | comparison | revisions
ui/motif/objs.mk file | annotate | diff | comparison | revisions
ui/motif/stock.c file | annotate | diff | comparison | revisions
ui/motif/stock.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/window.c file | annotate | diff | comparison | revisions
ui/ui/stock.h file | annotate | diff | comparison | revisions
ui/ui/toolbar.h file | annotate | diff | comparison | revisions
ui/ui/ui.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/application/main.c	Sat Mar 22 10:37:12 2014 +0100
@@ -85,6 +85,15 @@
     ui_checkitem_nv("Check", "check1");
     ui_menuitem("Close", action_close, NULL);
     
+    ui_toolitem_st("new", UI_STOCK_NEW, action_new, NULL);
+    ui_toolitem_st("open", UI_STOCK_OPEN, action_open, NULL);
+    ui_toolitem_st("close", UI_STOCK_CLOSE, action_close, NULL);
+    
+    ui_toolbar_add_default("new");
+    ui_toolbar_add_default("open");
+    ui_toolbar_add_default("close");
+    
+    
     UiObject *window = ui_window("Mod0", NULL);
     TestWindowData *wdata = malloc(sizeof(TestWindowData));
     window->window = wdata;
--- a/ui/common/context.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/common/context.c	Sat Mar 22 10:37:12 2014 +0100
@@ -73,8 +73,6 @@
         } else {
             // register the current document/wdata map
             // if the document is closed, the var will be moved to this map
-            
-            // TODO: get current map
             var->from = from;
             
             return var;
--- a/ui/common/document.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/common/document.c	Sat Mar 22 10:37:12 2014 +0100
@@ -242,6 +242,7 @@
                 t->set(t, t->value);
             } else {
                 //t->value = t->get(t);
+                f->value = f->get(f);
                 //t->value = 0;
             }
             break;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/button.c	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,43 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "button.h"
+
+
+
+void ui_button_clicked(GtkWidget *widget, UiEventData *event) {
+    UiEvent e;
+    e.obj = event->obj;
+    e.window = event->obj->window;
+    e.document = event->obj->document;
+    e.intval = 0;
+    event->callback(&e, event->user_data);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/button.h	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef BUTTON_H
+#define	BUTTON_H
+
+#include "../ui/toolkit.h"
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+// event wrapper
+void ui_button_clicked(GtkWidget *widget, UiEventData *event);
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* BUTTON_H */
+
--- a/ui/gtk/objs.mk	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/gtk/objs.mk	Sat Mar 22 10:37:12 2014 +0100
@@ -32,8 +32,9 @@
 # some objects are defined in config.mk
 GTKOBJ += toolkit.o
 GTKOBJ += window.o
-
 GTKOBJ += menu.o
+GTKOBJ += toolbar.o
+GTKOBJ += button.o
 
 TOOLKITOBJS = $(GTKOBJ:%=$(GTK_OBJPRE)%)
 TOOLKITSOURCE = $(GTKOBJ:%.o=gtk/%.c)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/toolbar.c	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,139 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "toolbar.h"
+#include "button.h"
+#include "../../ucx/mempool.h"
+#include "../common/context.h"
+
+static UcxMap *toolbar_items;
+static UcxList *defaults;
+
+void ui_toolbar_init() {
+    toolbar_items = ucx_map_new(16);
+}
+
+void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
+    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;
+    
+    ucx_map_cstr_put(toolbar_items, name, item);
+}
+
+void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *ud) {
+    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;
+    
+    ucx_map_cstr_put(toolbar_items, name, item);
+}
+
+void ui_toolbar_add_default(char *name) {
+    char *s = strdup(name);
+    defaults = ucx_list_append(defaults, s);
+}
+
+GtkWidget* ui_create_toolbar(UiObject *obj) {
+    if(!defaults) {
+        return NULL;
+    }
+    
+    GtkWidget *toolbar = gtk_toolbar_new();
+#ifdef UI_GTK3
+    gtk_style_context_add_class(
+            gtk_widget_get_style_context(toolbar),
+            GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
+#endif
+    
+    GtkToolbar *tb = GTK_TOOLBAR(toolbar);
+    UCX_FOREACH(elm, defaults) {
+        UiToolItemI *item = ucx_map_cstr_get(toolbar_items, elm->data);
+        if(item) {
+            item->add_to(tb, item, obj);
+        } else if(!strcmp(elm->data, "@separator")) {
+            gtk_toolbar_insert(tb, gtk_separator_tool_item_new(), -1);
+        } else {
+            fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data);
+        }
+    }
+    
+    return toolbar;
+}
+
+void add_toolitem_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj) {
+    GtkToolItem *button = gtk_tool_button_new(NULL, item->label);
+    gtk_tool_item_set_homogeneous(button, FALSE);
+    
+    if(item->callback) {
+        UiEventData *event = ucx_mempool_malloc(
+                obj->ctx->mempool,
+                sizeof(UiEventData));
+        event->obj = obj;
+        event->user_data = item->userdata;
+        event->callback = item->callback;
+        
+        g_signal_connect(
+                button,
+                "clicked",
+                G_CALLBACK(ui_button_clicked),
+                event);
+    }
+    
+    gtk_toolbar_insert(tb, button, -1);
+}
+
+void add_toolitem_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) {
+    GtkToolItem *button = gtk_tool_button_new_from_stock(item->stockid);
+    gtk_tool_item_set_homogeneous(button, FALSE);
+    
+    if(item->callback) {
+        UiEventData *event = ucx_mempool_malloc(
+                obj->ctx->mempool,
+                sizeof(UiEventData));
+        event->obj = obj;
+        event->user_data = item->userdata;
+        event->callback = item->callback;
+        
+        g_signal_connect(
+                button,
+                "clicked",
+                G_CALLBACK(ui_button_clicked),
+                event);
+    }
+    
+    gtk_toolbar_insert(tb, button, -1);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/toolbar.h	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,77 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TOOLBAR_H
+#define	TOOLBAR_H
+
+#include "../ui/toolbar.h"
+#include "../../ucx/map.h"
+#include "../../ucx/list.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiToolItemI    UiToolItemI;
+typedef struct UiToolItem     UiToolItem;
+typedef struct UiStToolItem   UiStToolItem;
+
+typedef void(*ui_toolbar_add_f)(GtkToolbar*, UiToolItemI*, UiObject*);
+
+struct UiToolItemI {
+    ui_toolbar_add_f  add_to;
+};
+
+struct UiToolItem {
+    UiToolItemI item;
+    char           *label;
+    ui_callback    callback;
+    void           *userdata;
+};
+
+struct UiStToolItem {
+    UiToolItemI    item;
+    char           *stockid;
+    ui_callback    callback;
+    void           *userdata;
+};
+
+void ui_toolbar_init();
+
+GtkWidget* ui_create_toolbar(UiObject *obj);
+
+void add_toolitem_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj);
+void add_toolitem_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj);
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* TOOLBAR_H */
+
--- a/ui/gtk/toolkit.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/gtk/toolkit.c	Sat Mar 22 10:37:12 2014 +0100
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 
 #include "toolkit.h"
+#include "toolbar.h"
 #include "../common/document.h"
 
 static char *application_name;
@@ -42,6 +43,7 @@
     application_name = appname;
     
     uic_docmgr_init();
+    ui_toolbar_init();
     
     // init custom types
     //ui_list_init();
--- a/ui/gtk/window.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/gtk/window.c	Sat Mar 22 10:37:12 2014 +0100
@@ -33,6 +33,7 @@
 #include "../common/context.h"
 
 #include "menu.h"
+#include "toolbar.h"
 
 static int nwindows = 0;
 
@@ -86,6 +87,12 @@
         gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0);
     }
     
+    // toolbar
+    GtkWidget *tb = ui_create_toolbar(obj);
+    if(tb) {
+        gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0);
+    }
+    
     nwindows++;
     return obj;
 }
--- a/ui/motif/button.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/motif/button.c	Sat Mar 22 10:37:12 2014 +0100
@@ -58,3 +58,12 @@
     e.intval = tb->set;
     event->callback(&e, event->user_data); 
 }
+
+void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d) {
+    UiEvent e;
+    e.obj = event->obj;
+    e.window = event->obj->window;
+    e.document = event->obj->document;
+    e.intval = 0;
+    event->callback(&e, event->user_data);
+}
--- a/ui/motif/button.h	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/motif/button.h	Sat Mar 22 10:37:12 2014 +0100
@@ -44,6 +44,7 @@
         Widget widget,
         UiEventData *data,
         XmToggleButtonCallbackStruct *e);
+void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d);
 
 #ifdef	__cplusplus
 }
--- a/ui/motif/menu.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/motif/menu.c	Sat Mar 22 10:37:12 2014 +0100
@@ -204,7 +204,11 @@
         event->obj = obj;
         event->user_data = mi->userdata;
         event->callback = mi->callback;
-        XtAddCallback(mitem, XmNactivateCallback, ui_menu_event_wrapper, event);
+        XtAddCallback(
+                mitem,
+                XmNactivateCallback,
+                (XtCallbackProc)ui_push_button_callback,
+                event);
     }
     
     return 1;
--- a/ui/motif/objs.mk	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/motif/objs.mk	Sat Mar 22 10:37:12 2014 +0100
@@ -30,9 +30,10 @@
 MOTIF_OBJPRE = $(OBJ_DIR)$(MOTIF_SRC_DIR)
 
 MOTIFOBJ = toolkit.o
+MOTIFOBJ += stock.o
 MOTIFOBJ += window.o
-
 MOTIFOBJ += menu.o
+MOTIFOBJ += toolbar.o
 MOTIFOBJ += button.o
 
 TOOLKITOBJS = $(MOTIFOBJ:%=$(MOTIF_OBJPRE)%)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/stock.c	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,57 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "stock.h"
+#include "../../ucx/map.h"
+
+static UcxMap *stock_items;
+
+void ui_stock_init() {
+    stock_items = ucx_map_new(64);
+    
+    ui_add_stock_item(UI_STOCK_NEW, "New", NULL);
+    ui_add_stock_item(UI_STOCK_OPEN, "Open", NULL);
+    ui_add_stock_item(UI_STOCK_SAVE, "Save", NULL);
+    ui_add_stock_item(UI_STOCK_SAVE_AS, "Save as ...", NULL);
+    ui_add_stock_item(UI_STOCK_CLOSE, "Close", NULL);
+}
+
+void ui_add_stock_item(char *id, char *label, void *icon) {
+    UiStockItem *i = malloc(sizeof(UiStockItem));
+    i->label = label;
+    // TODO: icon
+    
+    ucx_map_cstr_put(stock_items, id, i);
+}
+
+UiStockItem* ui_get_stock_item(char *id) {
+    return ucx_map_cstr_get(stock_items, id);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/stock.h	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,54 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef STOCK_H
+#define	STOCK_H
+
+#include "../ui/stock.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiStockItem {
+    char *label;
+    // TODO: icon
+} UiStockItem;
+    
+void ui_stock_init();
+
+void ui_add_stock_item(char *id, char *label, void *icon);
+
+UiStockItem* ui_get_stock_item(char *id);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* STOCK_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/toolbar.c	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,157 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "toolbar.h"
+#include "button.h"
+#include "stock.h"
+#include "../../ucx/mempool.h"
+#include "../common/context.h"
+
+static UcxMap *toolbar_items;
+static UcxList *defaults;
+
+void ui_toolbar_init() {
+    toolbar_items = ucx_map_new(16);
+}
+
+void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
+    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;
+    
+    ucx_map_cstr_put(toolbar_items, name, item);
+}
+
+void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *ud) {
+    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;
+    
+    ucx_map_cstr_put(toolbar_items, name, item);
+}
+
+void ui_toolbar_add_default(char *name) {
+    char *s = strdup(name);
+    defaults = ucx_list_append(defaults, s);
+}
+
+void ui_create_toolbar(UiObject *obj, Widget parent) {
+    if(!defaults) {
+        return;
+    }
+    
+    Arg args[8];
+    XtSetArg(args[0], XmNshadowType, XmSHADOW_ETCHED_OUT);
+    XtSetArg(args[1], XmNshadowThickness, 1);
+    XtSetArg(args[2], XmNtopAttachment, XmATTACH_FORM);
+    XtSetArg(args[3], XmNleftAttachment, XmATTACH_FORM);
+    XtSetArg(args[4], XmNrightAttachment, XmATTACH_FORM);
+    Widget frame = XmCreateFrame(parent, "toolbar_frame", args, 5);
+    
+    XtSetArg(args[0], XmNorientation, XmHORIZONTAL);
+    XtSetArg(args[1], XmNpacking, XmPACK_TIGHT);
+    Widget toolbar = XmCreateRowColumn (frame, "toolbar", args, 2);
+    
+    UCX_FOREACH(elm, defaults) {
+        UiToolItemI *item = ucx_map_cstr_get(toolbar_items, elm->data);
+        if(item) {
+            item->add_to(toolbar, item, obj);
+        } else if(!strcmp(elm->data, "@separator")) {
+            
+        } else {
+            fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data);
+        }
+    }
+    
+    XtManageChild(toolbar);
+    XtManageChild(frame);
+}
+
+void add_toolitem_widget(Widget parent, UiToolItem *item, UiObject *obj) {
+    Arg args[4];
+    
+    XmString label = XmStringCreateLocalized(item->label);
+    XtSetArg(args[0], XmNlabelString, label);
+    XtSetArg(args[1], XmNshadowThickness, 1);
+    Widget button = XmCreatePushButton(parent, "toolbar_button", args, 2);
+    
+    XmStringFree(label);
+    
+    if(item->callback) {
+        UiEventData *event = ucx_mempool_malloc(
+                obj->ctx->mempool,
+                sizeof(UiEventData));
+        event->obj = obj;
+        event->user_data = item->userdata;
+        event->callback = item->callback;
+        XtAddCallback(
+                button,
+                XmNactivateCallback,
+                (XtCallbackProc)ui_push_button_callback,
+                event);
+    }
+    
+    XtManageChild(button);
+}
+
+void add_toolitem_st_widget(Widget parent, UiStToolItem *item, UiObject *obj) {
+    Arg args[4];
+    
+    UiStockItem *stock_item = ui_get_stock_item(item->stockid);
+    
+    XmString label = XmStringCreateLocalized(stock_item->label);
+    XtSetArg(args[0], XmNlabelString, label);
+    XtSetArg(args[1], XmNshadowThickness, 1);
+    Widget button = XmCreatePushButton(parent, "toolbar_button", args, 2);
+    
+    XmStringFree(label);
+    
+    if(item->callback) {
+        UiEventData *event = ucx_mempool_malloc(
+                obj->ctx->mempool,
+                sizeof(UiEventData));
+        event->obj = obj;
+        event->user_data = item->userdata;
+        event->callback = item->callback;
+        XtAddCallback(
+                button,
+                XmNactivateCallback,
+                (XtCallbackProc)ui_push_button_callback,
+                event);
+    }
+    
+    XtManageChild(button);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/toolbar.h	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,76 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TOOLBAR_H
+#define	TOOLBAR_H
+
+#include "../ui/toolbar.h"
+#include "../../ucx/map.h"
+#include "../../ucx/list.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiToolItemI    UiToolItemI;
+typedef struct UiToolItem     UiToolItem;
+typedef struct UiStToolItem   UiStToolItem;
+
+typedef void(*ui_toolbar_add_f)(Widget, UiToolItemI*, UiObject*);
+
+struct UiToolItemI {
+    ui_toolbar_add_f  add_to;
+};
+
+struct UiToolItem {
+    UiToolItemI item;
+    char           *label;
+    ui_callback    callback;
+    void           *userdata;
+};
+
+struct UiStToolItem {
+    UiToolItemI    item;
+    char           *stockid;
+    ui_callback    callback;
+    void           *userdata;
+};
+
+void ui_toolbar_init();
+
+void ui_create_toolbar(UiObject *obj, Widget parent);
+
+void add_toolitem_widget(Widget tb, UiToolItem *item, UiObject *obj);
+void add_toolitem_st_widget(Widget tb, UiStToolItem *item, UiObject *obj);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* TOOLBAR_H */
+
--- a/ui/motif/toolkit.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/motif/toolkit.c	Sat Mar 22 10:37:12 2014 +0100
@@ -30,6 +30,8 @@
 #include <stdlib.h>
 
 #include "toolkit.h"
+#include "toolbar.h"
+#include "stock.h"
 #include "../common/document.h"
 
 static XtAppContext app;
@@ -56,6 +58,8 @@
     display =  XtOpenDisplay(app, NULL, appname, appname, NULL, 0, &argc, argv);
     
     uic_docmgr_init();
+    ui_toolbar_init();
+    ui_stock_init();
 }
 
 Display* ui_get_display() {
--- a/ui/motif/window.c	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/motif/window.c	Sat Mar 22 10:37:12 2014 +0100
@@ -31,6 +31,7 @@
 
 #include "toolkit.h"
 #include "menu.h"
+#include "toolbar.h"
 #include "../ui/window.h"
 #include "../common/context.h"
 
@@ -89,6 +90,18 @@
             NULL);
     obj->widget = window;
     ui_create_menubar(obj);
+    
+    // toolbar
+    Widget form = XtVaCreateManagedWidget(
+            "window_form",
+            xmFormWidgetClass,
+            window,
+            NULL);
+    
+    ui_create_toolbar(obj, form);
+    
+    
+    XtManageChild(form);
       
     obj->widget = toplevel;
     nwindows++;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/ui/stock.h	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,84 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UI_STOCK_H
+#define	UI_STOCK_H
+
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+// motif stock ids
+#ifdef UI_MOTIF
+    
+#define UI_STOCK_NEW                    "uiNew"
+#define UI_STOCK_OPEN                   "uiOpen"
+#define UI_STOCK_SAVE                   "uiSave"
+#define UI_STOCK_SAVE_AS                "uiSaveAs"
+#define UI_STOCK_REVERT_TO_SAVED        "uiRevertToSaved"
+#define UI_STOCK_UNDO                   "uiUndo"
+#define UI_STOCK_REDO                   "uiRedo"
+#define UI_STOCK_GO_BACK                "uiGoBack"
+#define UI_STOCK_GO_FORWARD             "uiGoForward"
+#define UI_STOCK_ADD                    "uiAdd"
+#define UI_STOCK_CLOSE                  "uiClose"
+    
+#endif
+    
+#if UI_GTK2 || UI_GTK3
+    
+#define UI_STOCK_NEW                    GTK_STOCK_NEW
+#define UI_STOCK_OPEN                   GTK_STOCK_OPEN
+#define UI_STOCK_SAVE                   GTK_STOCK_SAVE
+#define UI_STOCK_SAVE_AS                GTK_STOCK_SAVE_AS
+#define UI_STOCK_REVERT_TO_SAVED        GTK_STOCK_REVERT_TO_SAVED
+#define UI_STOCK_UNDO                   GTK_STOCK_UNDO
+#define UI_STOCK_REDO                   GTK_STOCK_REDO
+#define UI_STOCK_GO_BACK                GTK_STOCK_GO_BACK
+#define UI_STOCK_GO_FORWARD             GTK_STOCK_GO_FORWARD
+#define UI_STOCK_ADD                    GTK_STOCK_ADD
+#define UI_STOCK_CLOSE                  GTK_STOCK_CLOSE
+
+#define UI_STOCK_UNDO                   GTK_STOCK_UNDO
+#define UI_STOCK_REDO                   GTK_STOCK_REDO
+#define UI_STOCK_CUT                    GTK_STOCK_CUT
+#define UI_STOCK_COPY                   GTK_STOCK_COPY
+#define UI_STOCK_PASTE                  GTK_STOCK_PASTE
+#define UI_STOCK_DELETE                 GTK_STOCK_DELETE
+    
+#endif
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* UI_STOCK_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/ui/toolbar.h	Sat Mar 22 10:37:12 2014 +0100
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UI_TOOLBAR_H
+#define	UI_TOOLBAR_H
+
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+void ui_toolitem(char *name, char *label, ui_callback f, void *udata);
+void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata);
+
+void ui_toolbar_add_default(char *name);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* UI_TOOLBAR_H */
+
--- a/ui/ui/ui.h	Fri Mar 21 13:20:53 2014 +0100
+++ b/ui/ui/ui.h	Sat Mar 22 10:37:12 2014 +0100
@@ -31,7 +31,9 @@
 
 #include "toolkit.h"
 #include "menu.h"
+#include "toolbar.h"
 #include "window.h"
+#include "stock.h"
 
 #endif	/* UI_H */
 

mercurial