comparison mde/gui/widget/createWidget.d @ 80:ea58f277f487

Gui reorganization and changes; partial implementation of floating widgets. Moved contents of mde/gui/WidgetData.d elsewhere; new gui/WidgetDataSet.d and gui/types.d modules. Changes to widget/createWidget.d Partially implemented FloatingAreaWidget to provide an area for floating "window" widgets. New DebugWidget and some uses of it (e.g. bad widget data). Decoupled OptionChanges from Options.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 07 Aug 2008 11:25:27 +0100
parents 3dfd934100f7
children 56c0ddd90193
comparison
equal deleted inserted replaced
79:61ea26abe4dd 80:ea58f277f487
21 21
22 // Widgets to create: 22 // Widgets to create:
23 import mde.gui.widget.layout; 23 import mde.gui.widget.layout;
24 import mde.gui.widget.miscWidgets; 24 import mde.gui.widget.miscWidgets;
25 import mde.gui.widget.TextWidget; 25 import mde.gui.widget.TextWidget;
26 import mde.gui.widget.Floating;
27 import tango.util.log.Log : Log, Logger;
26 28
27 /** Create a widget of type data[0] (see enum WIDGET_TYPES) under manager mgr, with initialisation 29 private Logger logger;
28 * data [1..$]. */ 30 static this () {
29 IChildWidget createWidget (IWidgetManager mgr, WidgetData data) 31 logger = Log.getLogger ("mde.gui.widget.createWidget");
32 }
33
34 /** Create a widget.
35 *
36 * Usually called by the widget manager's makeWidget function.
37 *
38 * Widget created of type data.ints[0] (see enum WIDGET_TYPES), with one of the following CTORs:
39 * ---
40 * this (IWidgetManager mgr, WidgetData data);
41 * // Called if (data.ints[0] & WIDGET_TYPES.TAKES_PARENT):
42 * this (IWidgetManager mgr, WidgetData data, IParentWidget parent);
43 * ---
44 *************************************************************************************************/
45 IChildWidget createWidget (IWidgetManager mgr, WidgetData data, IParentWidget parent)
30 in { 46 in {
31 assert (mgr !is null, "createWidget: mgr is null"); 47 assert (mgr !is null, "createWidget: mgr is null");
32 } body { 48 } body {
33 if (data.ints.length < 1) throw new WidgetDataException ("No widget data"); 49 if (data.ints.length < 1) {
50 logger.error ("No int data; creating a debug widget");
51 data.ints = [WIDGET_TYPE.Debug];
52 }
34 int type = data.ints[0]; // type is first element of data 53 int type = data.ints[0]; // type is first element of data
35 54
55 //pragma (msg, binarySearch ("type", WIDGETS));
36 mixin (binarySearch ("type", WIDGETS)); // creates widget by type as new *Widget (mgr, data); 56 mixin (binarySearch ("type", WIDGETS)); // creates widget by type as new *Widget (mgr, data);
37 throw new WidgetDataException ("Bad widget type"); 57
58 // Not returned a new widget...
59 logger.error ("Bad widget type: {}; creating a debug widget instead.",type);
60 return new DebugWidget (mgr, data);
38 } 61 }
39 62
40 /+ for converting to a char[] name (unused) 63 /+ for converting to a char[] name (unused)
41 static this() { 64 static this() {
42 WIDGET_NAMES = [ 65 WIDGET_NAMES = [
48 }+/ 71 }+/
49 72
50 private: 73 private:
51 /// Widget types. 74 /// Widget types.
52 enum WIDGET_TYPE : int { 75 enum WIDGET_TYPE : int {
53 WSIZABLE = 0x1000, // horizontally resizable 76 TAKES_PARENT = 0x4000, // CTOR takes reference to its parent
54 HSIZABLE = 0x2000, // vertically resizable 77 PARENT = 0x8000, // widget can have children
55 INTERACTIBLE = 0x4000, // any specific interaction
56 LAYOUT = 0x8000, // is a layout widget (i.e. has sub-widgets)?
57 78
58 // Use widget names rather than usual capitals convention 79 // Use widget names rather than usual capitals convention
59 Unnamed = 0x0, // Only for use by widgets not created with createWidget 80 Unnamed = 0x0, // Only for use by widgets not created with createWidget
60 81
61 // blank: 0x1 82 // blank: 0x1
62 FixedBlank = 0x1, 83 FixedBlank = 0x1,
63 SizableBlank = WSIZABLE | HSIZABLE | 0x1, 84 SizableBlank = 0x2,
85 Debug = 0xF,
64 86
65 // buttons: 0x10 87 // buttons: 0x10
66 Button = INTERACTIBLE | 0x10, 88 Button = 0x10,
67 89
68 // content: 0x20 90 // content: 0x20
69 Text = 0x21, 91 Text = 0x21,
70 Int = 0x22, 92 Int = 0x22,
71 93
72 GridLayout = LAYOUT | WSIZABLE | HSIZABLE | 0x4, 94 GridLayout = PARENT | 0x100,
73 TrialContentLayout = LAYOUT | WSIZABLE | HSIZABLE | 0x5 95 TrialContentLayout = PARENT | 0x110,
96
97 FloatingArea = PARENT | 0x200,
74 } 98 }
75 99
76 //const char[][int] WIDGET_NAMES; 100 //const char[][int] WIDGET_NAMES;
77 101
78 // Only used for binarySearch algorithm generation; must be ordered by numerical values. 102 // Only used for binarySearch algorithm generation; must be ordered by numerical values.
79 const char[][] WIDGETS = [ 103 const char[][] WIDGETS = [
80 "FixedBlank", 104 "FixedBlank",
105 "SizableBlank",
106 "Debug",
107 "Button",
81 "Text", 108 "Text",
82 "Int", 109 "Int",
83 "SizableBlank",
84 "Button",
85 "GridLayout", 110 "GridLayout",
86 "TrialContentLayout"]; 111 "TrialContentLayout",
112 "FloatingArea"];
87 113
88 /* Generates a binary search algorithm. */ 114 /* Generates a binary search algorithm. */
89 char[] binarySearch (char[] var, char[][] consts) { 115 char[] binarySearch (char[] var, char[][] consts) {
90 if (consts.length > 3) { 116 if (consts.length > 3) {
91 return "if (" ~ var ~ " <= WIDGET_TYPE." ~ consts[$/2 - 1] ~ ") {\n" ~ 117 return "if (" ~ var ~ " <= WIDGET_TYPE." ~ consts[$/2 - 1] ~ ") {\n" ~
94 binarySearch (var, consts[$/2 .. $]) ~ 120 binarySearch (var, consts[$/2 .. $]) ~
95 "}\n"; 121 "}\n";
96 } else { 122 } else {
97 char[] ret; 123 char[] ret;
98 foreach (c; consts) { 124 foreach (c; consts) {
99 ret ~= "if (" ~ var ~ " == WIDGET_TYPE." ~ c ~ ") {\n" ~ 125 ret ~= "if (" ~ var ~ " == WIDGET_TYPE." ~ c ~ ") {\n" ~
100 "return new " ~ c ~ "Widget (mgr, data);\n" ~ 126 " debug logger.trace (\"Creating new "~c~"Widget.\");\n" ~
127 " static if (WIDGET_TYPE."~c~" & WIDGET_TYPE.TAKES_PARENT)\n" ~
128 " return new " ~ c ~ "Widget (mgr, data, parent);\n" ~
129 " else\n" ~
130 " return new " ~ c ~ "Widget (mgr, data);\n" ~
101 "} else "; 131 "} else ";
102 } 132 }
103 ret = ret[0..$-6] ~ '\n'; // remove last else 133 ret = ret[0..$-6] ~ '\n'; // remove last else
104 return ret; 134 return ret;
105 } 135 }