annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
1 module doodle.gtk.palette2;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
2
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
3 public {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
4 import gtk.Toolbar;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
5 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
6
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
7 private {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
8 import doodle.core.logging;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
9 import gtk.ToolButton;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
10 import gtk.RadioToolButton;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
11 import gtk.Image;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
12 import gtk.Label;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
13 import glib.ListSG;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
14 import std.stdio;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
15 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
16
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
17 class Palette2(T) : Toolbar {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
18 struct Item {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
19 string iconPath;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
20 string labelText;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
21 string tooltipText;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
22 T t;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
23 };
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
24
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
25 alias void delegate(T) Callback;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
26
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
27 this() {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
28 // INVALID, MENU, SMALL_TOOLBAR, LARGE_TOOLBAR,
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
29 // BUTTON, DND, DIALOG
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
30 setIconSize(GtkIconSize.LARGE_TOOLBAR);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
31 // ICONS, TEXT, BOTH, BOTH_HORIZ
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
32 setStyle(GtkToolbarStyle.ICONS);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
33 // HORIZONTAL, VERTICAL
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
34 setOrientation(GtkOrientation.HORIZONTAL);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
35 setTooltips(true);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
36 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
37
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
38 void configure(Item[] items, Callback callback) {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
39 _callback = callback;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
40
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
41 RadioToolButton group;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
42
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
43 foreach(index, item; items) {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
44 RadioToolButton button;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
45
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
46 if (index == 0) {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
47 ListSG list;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
48 button = new RadioToolButton(list);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
49 group = button;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
50 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
51 else {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
52 button = new RadioToolButton(group);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
53 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
54
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
55 auto image = new Image(_iconBase ~ "/" ~ item.iconPath);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
56 auto label = new Label(item.tooltipText);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
57 button.setIconWidget(image);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
58 button.setLabelWidget(label);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
59 button.setTooltipText(item.tooltipText);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
60
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
61 _buttons[item.t] = button;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
62 button.objectGSetDataFull(_indexStr, cast(gpointer)item.t);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
63 button.addOnClicked(&onClicked);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
64
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
65 insert(button);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
66 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
67 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
68
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
69 void activate(T t) {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
70 RadioToolButton button = _buttons[t];
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
71 if (!button.getActive) {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
72 button.setActive(true);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
73 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
74 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
75
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
76 private {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
77 immutable _iconBase = "/home/daveb/source/d/doodle/doodle/gtk/data";
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
78 immutable _indexStr = "index";
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
79
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
80 Callback _callback;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
81 RadioToolButton[T] _buttons;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
82
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
83 void onClicked(ToolButton toolButton) {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
84 RadioToolButton button = cast(RadioToolButton)toolButton;
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
85 if (button.getActive) {
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
86 T t = cast(T)button.getData(_indexStr);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
87 _callback(t);
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
88 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
89 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
90 }
e64baac3efb2 Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
diff changeset
91 }