diff doodle/gtk/palette2.d @ 60:e64baac3efb2

Attempt at a templatised palette
author David Bryant <bagnose@gmail.com>
date Tue, 10 Aug 2010 22:55:54 +0930
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doodle/gtk/palette2.d	Tue Aug 10 22:55:54 2010 +0930
@@ -0,0 +1,91 @@
+module doodle.gtk.palette2;
+
+public {
+    import gtk.Toolbar;
+}
+
+private {
+    import doodle.core.logging;
+    import gtk.ToolButton;
+    import gtk.RadioToolButton;
+    import gtk.Image;
+    import gtk.Label;
+    import glib.ListSG;
+    import std.stdio;
+}
+
+class Palette2(T) : Toolbar {
+    struct Item {
+        string iconPath;
+        string labelText;
+        string tooltipText;
+        T t;
+    };
+
+    alias void delegate(T) Callback;
+
+    this() {
+        // INVALID, MENU, SMALL_TOOLBAR, LARGE_TOOLBAR,
+        // BUTTON, DND, DIALOG
+        setIconSize(GtkIconSize.LARGE_TOOLBAR);
+	// ICONS, TEXT, BOTH, BOTH_HORIZ
+        setStyle(GtkToolbarStyle.ICONS);
+	// HORIZONTAL, VERTICAL
+        setOrientation(GtkOrientation.HORIZONTAL);
+        setTooltips(true);
+    }
+
+    void configure(Item[] items, Callback callback) {
+        _callback = callback;
+
+        RadioToolButton group;
+
+        foreach(index, item; items) {
+            RadioToolButton button;
+
+            if (index == 0) {
+                ListSG list;
+                button = new RadioToolButton(list);
+                group = button;
+            }
+            else {
+                button = new RadioToolButton(group);
+            }
+
+            auto image = new Image(_iconBase ~ "/" ~ item.iconPath);
+            auto label = new Label(item.tooltipText);
+            button.setIconWidget(image);
+            button.setLabelWidget(label);
+            button.setTooltipText(item.tooltipText);
+
+            _buttons[item.t] = button;
+            button.objectGSetDataFull(_indexStr, cast(gpointer)item.t);
+            button.addOnClicked(&onClicked);
+
+            insert(button);
+        }
+    }
+
+    void activate(T t) {
+        RadioToolButton button = _buttons[t];
+        if (!button.getActive) {
+            button.setActive(true);
+        }
+    }
+
+    private {
+        immutable _iconBase = "/home/daveb/source/d/doodle/doodle/gtk/data";
+        immutable _indexStr = "index";
+
+        Callback _callback;
+        RadioToolButton[T] _buttons;
+
+        void onClicked(ToolButton toolButton) {
+            RadioToolButton button = cast(RadioToolButton)toolButton;
+            if (button.getActive) {
+                T t = cast(T)button.getData(_indexStr);
+                _callback(t);
+            }
+        }
+    }
+}