changeset 61:08ffc44fc21a

More palette work.
author daveb
date Wed, 11 Aug 2010 15:07:58 +0930
parents e64baac3efb2
children 6c3993f4c3eb
files doodle/core/undo.d doodle/gtk/palette.d doodle/gtk/palette2.d doodle/main/prog/doodler.d doodle/tk/palette.d
diffstat 5 files changed, 32 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- a/doodle/core/undo.d	Tue Aug 10 22:55:54 2010 +0930
+++ b/doodle/core/undo.d	Wed Aug 11 15:07:58 2010 +0930
@@ -87,6 +87,10 @@
         _maxUndoLevel = maxUndoLevel;
     }
 
+    ~this() {
+        assert(_observers.length == 0);
+    }
+
     void addEdit(Edit edit) {
         _redoEdits.length = 0;
 
--- a/doodle/gtk/palette.d	Tue Aug 10 22:55:54 2010 +0930
+++ b/doodle/gtk/palette.d	Wed Aug 11 15:07:58 2010 +0930
@@ -15,7 +15,7 @@
     import std.stdio;
 }
 
-class Palette : Toolbar, IPalette {
+class Palette(T) : Toolbar, IPalette!T {
     this() {
         // INVALID, MENU, SMALL_TOOLBAR, LARGE_TOOLBAR,
         // BUTTON, DND, DIALOG
@@ -27,12 +27,12 @@
         setTooltips(true);
     }
 
-    override void configure(in Item[] items) {
-        _items = items.dup;
+    override void configure(Item[] items, Callback callback) {
+        _callback = callback;
 
         RadioToolButton group;
 
-        foreach(index, item; _items) {
+        foreach(index, item; items) {
             RadioToolButton button;
 
             if (index == 0) {
@@ -50,29 +50,33 @@
             button.setLabelWidget(label);
             button.setTooltipText(item.tooltipText);
 
-            int * i = new int;
-            *i = index;
-            button.objectGSetDataFull(_indexStr, cast(gpointer)i);
+            _buttons[item.t] = button;
+            button.objectGSetDataFull(_indexStr, cast(gpointer)item.t);
             button.addOnClicked(&onClicked);
 
             insert(button);
         }
     }
 
-    override void activate(in int index) {
-        warning("Not yet implemented");
+    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";
-        Item[] _items;
+
+        Callback _callback;
+        RadioToolButton[T] _buttons;
 
         void onClicked(ToolButton toolButton) {
             RadioToolButton button = cast(RadioToolButton)toolButton;
             if (button.getActive) {
-                int * i = cast(int *)button.getData(_indexStr);
-                _items[*i].callback(*i);
+                T t = cast(T)button.getData(_indexStr);
+                _callback(t);
             }
         }
     }
--- a/doodle/gtk/palette2.d	Tue Aug 10 22:55:54 2010 +0930
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,91 +0,0 @@
-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);
-            }
-        }
-    }
-}
--- a/doodle/main/prog/doodler.d	Tue Aug 10 22:55:54 2010 +0930
+++ b/doodle/main/prog/doodler.d	Wed Aug 11 15:07:58 2010 +0930
@@ -12,7 +12,7 @@
     import doodle.fig.tools;
 
     import doodle.gtk.canvas;
-    import doodle.gtk.palette2;
+    import doodle.gtk.palette;
 
     import gtk.Main;
     import gtk.MainWindow;
@@ -27,7 +27,7 @@
         auto window = new MainWindow("Doodle");
         auto vbox = new VBox(false, 0);
 
-        auto palette = new Palette2!Tool;
+        auto palette = new Palette!Tool;
         _palette = palette;
 
         vbox.packStart(palette, false, false, 0);
@@ -52,14 +52,14 @@
 
         vbox.packStart(canvas, true, true, 0);
 
-        Palette2!Tool.Item[] items = [
+        Palette!Tool.Item[] items = [
         { "select.svg",    "Select",    "Select and modify elements", new SelectTool },
         { "rectangle.svg", "Rectangle", "Create rectangle", new CreateRectangleTool(_diagram) },
         { "ellipse.svg",   "Ellipse",   "Create ellipse", new CreateRectangleTool(_diagram) },
         { "polyline.svg",  "Polyline",  "Create polyline", new CreateRectangleTool(_diagram) }
         ];
 
-        palette.configure(items, &paletteCallback);
+        palette.configure(items, &_toolStack.use);
         window.add(vbox);
         window.setDefaultSize(640, 580);
         window.showAll();
@@ -73,13 +73,8 @@
 
     private {
         IToolStack _toolStack;
-        Palette2!Tool _palette;     // FIXME refer to the interface?
+        IPalette!Tool _palette;
         IDiagram _diagram;
-
-        void paletteCallback(Tool tool) {
-            _toolStack.use(tool);
-        }
-
     }
 }
 
--- a/doodle/tk/palette.d	Tue Aug 10 22:55:54 2010 +0930
+++ b/doodle/tk/palette.d	Wed Aug 11 15:07:58 2010 +0930
@@ -2,14 +2,16 @@
 
 // XXX Not sure whether to use delegates or observer pattern...
 
-interface IPalette {
+interface IPalette(T) {
     struct Item {
         string iconPath;
         string labelText;
         string tooltipText;
-        void delegate(int) callback;
-    }
+        T t;
+    };
 
-    void configure(in Item[] items);
-    void activate(in int index);
+    alias void delegate(T) Callback;
+
+    void configure(Item[] items, Callback callback);
+    void activate(T t);
 }