cleaned all trailing white space from source files.
[sdk] / ecere / src / gui / controls / StatusBar.ec
1 namespace gui::controls;
2
3 import "Window"
4
5 #include <stdarg.h>
6
7 #define MIN_INFO_WIDTH  300
8
9 public class StatusBar : Window
10 {
11    class_property(icon) = "<:ecere>controls/statusBar.png";
12
13    background = formColor;
14    nonClient = true;
15    inactive = true;
16    anchor = Anchor { left = 0, right = 0, bottom = 0 };
17
18 public:
19    void AddField(StatusField field)
20    {
21       if(this && field)
22       {
23          field._statusBar = this;
24          incref field;
25          width += field.width + (guiApp.textMode ? 0 : 6);
26          fields.Add(field);
27          Update(null);
28       }
29    }
30
31    void RemoveField(StatusField field)
32    {
33       if(this && field)
34       {
35          field._statusBar = null;
36          width -= field.width + (guiApp.textMode ? 0 : 6);
37          fields.Remove(field);
38          delete field;
39
40          Update(null);
41       }
42    }
43
44    void Clear()
45    {
46       if(this)
47       {
48          while(fields.first)
49             RemoveField(fields.first);
50       }
51    }
52
53 private:
54    StatusBar()
55    {
56       fields.offset = (uint)&((StatusField)0).prev;
57    }
58
59    ~StatusBar()
60    {
61       Clear();
62    }
63
64    bool OnLoadGraphics()
65    {
66       StatusField field;
67       width = 0;
68       for(field = fields.first; field; field = field.next)
69          width += field.width + (guiApp.textMode ? 0 : 6);
70       return true;
71    }
72
73    bool OnResizing(int * w, int * h)
74    {
75       *h = Max(*h, statusBarHeight);
76       if(!*w) *w = 80;
77       return true;
78    }
79
80    void OnRedraw(Surface surface)
81    {
82       StatusField field;
83       int position = Max(clientSize.w, MIN_INFO_WIDTH + width)-1-2;
84       Box clip { 2, 2, MIN_INFO_WIDTH, statusBarHeight-1 };
85       Window parent = this.parent;
86
87       if(!guiApp.textMode)
88          surface.ThinBevel(false, -1,0, clientSize.w+2, clientSize.h+1);
89
90       for(field = fields.first; field; field = field.next)
91       {
92          int x = position - field.width;
93
94          surface.SetBackground(white);
95          if(!guiApp.textMode)
96          {
97             surface.ThinBevel(true, x - 2, 2, field.width + 4, statusBarHeight-2);
98             surface.SetForeground(field.colorSet ? field.color : foreground);
99          }
100          else
101          {
102             if(parent.borderStyle == sizable && parent.state == normal)
103                surface.SetForeground(field.colorSet ? (field.color == black ? white : field.color) : (foreground == black ? white : foreground));
104             else
105                surface.SetForeground(field.colorSet ? field.color : foreground);
106          }
107          if(field.text)
108          {
109             Box clip { x, 2, x + field.width - 1, statusBarHeight-1 };
110             int tw;
111             surface.Clip(clip);
112             surface.TextExtent(field.text, strlen(field.text), &tw, null);
113             surface.WriteTextf(x + (field.width - tw) / 2, 2, field.text);
114             surface.Clip(null);
115          }
116          position -= field.width + (guiApp.textMode ? 0 : 6);
117       }
118
119       // TESTING THIS HERE!
120       clip.right = position;
121       surface.Clip(clip);
122       if(guiApp.textMode && parent.borderStyle == sizable && parent.state == normal)
123          surface.SetForeground(white);
124       else
125          surface.SetForeground(foreground);
126
127       surface.WriteTextf(2, 2, text);
128       surface.Clip(null);
129    }
130
131    watch(caption)
132    {
133       text = property::text;
134       Update(null);
135    };
136
137    char * text;
138    OldList fields;
139    int width;
140 };
141
142 public class StatusField
143 {
144 public:
145
146    property StatusBar statusBar
147    {
148       set
149       {
150          if(this)
151          {
152             if(_statusBar)
153                _statusBar.RemoveField(this);
154             if(value)
155                value.AddField(this);
156             _statusBar = value;
157          }
158       }
159    }
160    property Color color
161    {
162       set
163       {
164          if(this)
165          {
166             color = value;
167             colorSet = true;
168             if(_statusBar)
169                _statusBar.Update(null);
170          }
171       }
172    }
173
174    property char * text
175    {
176       set
177       {
178          if(this)
179          {
180             if(value)
181             {
182                int len = strlen(value);
183                text = renew text char[len + 1];
184                CopyBytes(text, value, len + 1);
185             }
186             else
187                delete text;
188             if(_statusBar)
189                _statusBar.Update(null);
190          }
191       }
192    }
193
194    property int width
195    {
196       set
197       {
198          if(this)
199          {
200             if(_statusBar)
201             {
202                _statusBar.width += value - width;
203                _statusBar.Update(null);
204             }
205             width = value;
206          }
207       }
208    }
209
210    void SetTextf(char * format, ...)
211    {
212       if(this)
213       {
214          delete text;
215          if(format)
216          {
217             char tempText[MAX_F_STRING];
218             va_list args;
219             va_start(args, format);
220             vsnprintf(tempText, sizeof(tempText), format, args);
221             tempText[sizeof(tempText)-1] = 0;
222             va_end(args);
223             text = CopyString(tempText);
224          }
225          if(_statusBar)
226             _statusBar.Update(null);
227       }
228    }
229
230 private:
231    ~StatusField()
232    {
233       delete text;
234    }
235
236    StatusField prev, next;
237    char * text;
238    int width;
239    Color color;
240    StatusBar _statusBar;
241    bool colorSet;
242 };