comparison mde/gui/widget/Widget.d @ 91:4d5d53e4f881

Shared alignment for dynamic content lists - finally implemented! Lots of smaller changes too. Some debugging improvements. When multiple .mtt files are read for merging, files with invalid headers are ignored and no error is thrown so long as at least one file os valid.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 16 Oct 2008 17:43:48 +0100
parents ea58f277f487
children 085f2ca31914
comparison
equal deleted inserted replaced
90:b525ff28774b 91:4d5d53e4f881
23 module mde.gui.widget.Widget; 23 module mde.gui.widget.Widget;
24 24
25 public import mde.gui.widget.Ifaces; 25 public import mde.gui.widget.Ifaces;
26 import mde.gui.exception; 26 import mde.gui.exception;
27 27
28 28 debug {
29 /************************************************************************************************* 29 import tango.util.log.Log : Log, Logger;
30 * Widgets may use WDCheck as a utility to check what data holds. Its use is encouraged, so that 30 private Logger logger;
31 * the checks can easily be updated should WidgetData be changed. 31 static this () {
32 * 32 logger = Log.getLogger ("mde.gui.widget.Widget");
33 * Params: 33 }
34 * data = the WidgetData to check lengths of
35 * n_ints = number of integers wanted
36 * n_strings= number of strings (default 0 since not all widgets use strings)
37 *************************************************************************************************/
38 void WDCheck (WidgetData data, size_t n_ints, size_t n_strings = 0) {
39 if (data.ints.length != n_ints ||
40 data.strings.length != n_strings)
41 throw new WidgetDataException;
42 } 34 }
43 35
44 36
45 /************************************************************************************************* 37 /*************************************************************************************************
46 * An abstract base widget class. 38 * An abstract base widget class.
51 *************************************************************************************************/ 43 *************************************************************************************************/
52 abstract class Widget : IChildWidget 44 abstract class Widget : IChildWidget
53 { 45 {
54 //BEGIN Load and save 46 //BEGIN Load and save
55 // Base this() for child Widgets. 47 // Base this() for child Widgets.
56 this (IWidgetManager mgr, WidgetData data) { 48 this (IWidgetManager mgr, widgetID, WidgetData) {
57 this.mgr = mgr; 49 this.mgr = mgr;
58 } 50 }
59 51
60 // Don't save any data: fine for many widgets. 52 // Don't save any data: fine for many widgets.
61 bool saveChanges (widgetID) { 53 bool saveChanges (widgetID) {
78 } 70 }
79 wdim minHeight () { 71 wdim minHeight () {
80 return mh; 72 return mh;
81 } 73 }
82 74
75 wdim width () {
76 return w;
77 }
78 wdim height() {
79 return h;
80 }
81
83 deprecated void getCurrentSize (out wdim cw, out wdim ch) { 82 deprecated void getCurrentSize (out wdim cw, out wdim ch) {
84 cw = w; 83 cw = w;
85 ch = h; 84 ch = h;
86 } 85 }
87 86
88 /* Set size: minimal size is (mw,mh). Note that both resizable and fixed widgets should allow 87 /* Set size: minimal size is (mw,mh). Note that both resizable and fixed widgets should allow
89 * enlarging, so in both cases this is a correct implementation. */ 88 * enlarging, so in both cases this is a correct implementation. */
90 void setWidth (wdim nw, int) { 89 void setWidth (wdim nw, int) {
90 debug if (nw < mw) logger.warn ("Widget width set below minimal size");
91 w = (nw >= mw ? nw : mw); 91 w = (nw >= mw ? nw : mw);
92 } 92 }
93 void setHeight (wdim nh, int) { 93 void setHeight (wdim nh, int) {
94 debug if (nh < mh) logger.warn ("Widget height set below minimal size");
94 h = (nh >= mh ? nh : mh); 95 h = (nh >= mh ? nh : mh);
95 } 96 }
96 97
97 void setPosition (wdim nx, wdim ny) { 98 void setPosition (wdim nx, wdim ny) {
98 x = nx; 99 x = nx;
101 //END Size and position 102 //END Size and position
102 103
103 //BEGIN Events 104 //BEGIN Events
104 /* This method is only called when the location is over this widget; hence for all widgets 105 /* This method is only called when the location is over this widget; hence for all widgets
105 * without children this method is valid. */ 106 * without children this method is valid. */
106 IChildWidget getWidget (wdim,wdim) { 107 IChildWidget getWidget (wdim cx, wdim cy) {
108 debug assert (cx >= x && cx < x + w && cy >= y && cy < y + h, "getWidget: not on widget (code error)");
107 return this; 109 return this;
108 } 110 }
109 111
110 /* Dummy event method (suitable for all widgets which don't respond to events). */ 112 /* Dummy event method (suitable for all widgets which don't respond to events). */
111 void clickEvent (wdabs cx, wdabs cy, ubyte b, bool state) {} 113 void clickEvent (wdabs cx, wdabs cy, ubyte b, bool state) {}
115 void draw () { 117 void draw () {
116 mgr.renderer.drawWidgetBack (x,y, w,h); 118 mgr.renderer.drawWidgetBack (x,y, w,h);
117 } 119 }
118 120
119 protected: 121 protected:
122 /**********************************************************************************************
123 * Widgets may use WDCheck as a utility to check what data holds. Its use is encouraged, so
124 * that the checks can easily be updated should WidgetData be changed.
125 *
126 * Params:
127 * data = the WidgetData to check lengths of
128 * n_ints = number of integers wanted
129 * n_strings= number of strings (default 0 since not all widgets use strings)
130 *********************************************************************************************/
131 void WDCheck (WidgetData data, size_t n_ints, size_t n_strings = 0) {
132 if (data.ints.length != n_ints ||
133 data.strings.length != n_strings)
134 throw new WidgetDataException (this);
135 }
136
120 IWidgetManager mgr; // the enclosing window 137 IWidgetManager mgr; // the enclosing window
121 wdim x, y; // position 138 wdim x, y; // position
122 wdim w, h; // size 139 wdim w, h; // size
123 wdim mw = 0, mh = 0; // minimal or fixed size, depending on whether the widget is 140 wdim mw = 0, mh = 0; // minimal or fixed size, depending on whether the widget is
124 // resizible; both types of widgets should actually be expandable. 141 // resizible; both types of widgets should actually be expandable.
130 /** Constructor for a fixed-size [blank] widget. 147 /** Constructor for a fixed-size [blank] widget.
131 * 148 *
132 * Widget uses the initialisation data: 149 * Widget uses the initialisation data:
133 * [widgetID, w, h] 150 * [widgetID, w, h]
134 * where w, h is the fixed size. */ 151 * where w, h is the fixed size. */
135 this (IWidgetManager mgr, WidgetData data) { 152 this (IWidgetManager mgr, widgetID id, WidgetData data) {
136 super (mgr, data); 153 super (mgr, id, data);
137 mw = cast(wdim) data.ints[1]; 154 mw = cast(wdim) data.ints[1];
138 mh = cast(wdim) data.ints[2]; 155 mh = cast(wdim) data.ints[2];
139 w = mw; 156 w = mw;
140 h = mh; 157 h = mh;
141 } 158 }
143 160
144 /** A base for resizable widgets. */ 161 /** A base for resizable widgets. */
145 class SizableWidget : Widget { 162 class SizableWidget : Widget {
146 // Check data.length is at least 1 before calling! 163 // Check data.length is at least 1 before calling!
147 /// Constructor for a completely resizable [blank] widget. 164 /// Constructor for a completely resizable [blank] widget.
148 this (IWidgetManager mgr, WidgetData data) { 165 this (IWidgetManager mgr, widgetID id, WidgetData data) {
149 super (mgr, data); 166 super (mgr, id, data);
150 } 167 }
151 168
152 bool isWSizable () { return true; } 169 bool isWSizable () { return true; }
153 bool isHSizable () { return true; } 170 bool isHSizable () { return true; }
154 } 171 }