cleaned all trailing white space from source files.
[sdk] / ecere / src / gui / controls / SelectorBar.ec
1 #ifdef BUILDING_ECERE_COM
2 namespace gui::controls;
3 import "Stacker"
4 #else
5 /*
6 #ifdef ECERE_STATIC
7 public import static "ecere"
8 #else
9 public import "ecere"
10 #endif
11 */
12 #endif
13
14 static void DrawStipple(Surface surface, Size clientSize)
15 {
16    int x1 = 0;
17    int y1 = 0;
18    int x2 = clientSize.w - 1;
19    int y2 = clientSize.h - 1;
20    if((x2 - x1) & 1) x2--;
21    if((y2 - y1) & 1) y2--;
22
23    surface.LineStipple(0x5555);
24    surface.Rectangle(x1, y1, x2, y2);
25    surface.LineStipple(0);
26 }
27
28 public class SelectorBar : Stacker
29 {
30    direction = horizontal;
31    background = formColor;
32    //tabCycle = true;
33    //isActiveClient = true;
34
35    clientSize = { h = 40 };
36
37 public:
38    /*Label label
39    {
40       this, anchor = { left = 8, top = 12 }, labeledWindow = this;
41
42       void OnRedraw(Surface surface)
43       {
44          Label::OnRedraw(surface);
45          if(labeledWindow.active)
46             DrawStipple(surface, clientSize);
47       }
48    };*/
49
50    void Clear()
51    {
52       DestroyChildren();
53    }
54
55    void Select(SelectorButton button)
56    {
57       button.checked = true;
58       if(button.created)
59          button.Activate();
60       button.NotifyClicked(button.master, button, 0, 0, 0);
61       MakeControlVisible(button);
62    }
63
64    void AddButton(SelectorButton button)
65    {
66       if(created)
67          button.Create();
68    }
69
70    void RemoveButton(SelectorButton button)
71    {
72       Iterator<Window> it { controls };
73       while(it.Next())
74       {
75          if(button == (SelectorButton)it.data)
76          {
77             IteratorPointer toRemove = it.pointer;
78             if(it.Next() || (it.Prev() && it.Prev()))
79             {
80                SelectorButton newSelection = (SelectorButton)it.data;
81                newSelection.checked = true;
82                newSelection.NotifyClicked(newSelection.master, newSelection, 0, 0, 0);
83             }
84             break;
85          }
86       }
87       button.Destroy(0);
88    }
89
90    SelectorButton FindButtonByID(int64 id)
91    {
92       SelectorButton button = null;
93       Iterator<Window> it { controls };
94       while(it.Next())
95       {
96          Window b = it.data;
97          if(eClass_IsDerived(b._class, class(SelectorButton)) && b.id == id)
98          {
99             button = (SelectorButton)b;
100             break;
101          }
102       }
103       return button;
104    }
105
106    bool OnCreate()
107    {
108       OnResize(clientSize.w, clientSize.h);
109       return true;
110    }
111
112    property SelectorButton selectedButton
113    {
114       get
115       {
116          SelectorButton button = null;
117          Iterator<Window> it { controls };
118          while(it.Next())
119          {
120             Window w = it.data;
121             if(eClass_IsDerived(w._class, class(SelectorButton)))
122             {
123                SelectorButton b = (SelectorButton)w;
124                if(b.checked)
125                {
126                   button = (SelectorButton)b;
127                   break;
128                }
129             }
130          }
131          return button;
132       }
133    }
134
135    ~SelectorBar()
136    {
137       Clear();
138    }
139 };
140
141 public class SelectorButton : Button
142 {
143    bevelOver = true, isRadio = true, bitmap = null, minClientSize = { 44, 22 }; isRemote = true;
144
145 public:
146    Window focusHolder;
147
148    watch(parent)
149    {
150       if(parent && eClass_IsDerived(parent._class, class(SelectorBar)))
151       {
152          SelectorBar parent = (SelectorBar)this.parent;
153          // Fix Cycling order
154          if(cycle)
155             parent.childrenCycle.Move(cycle, null);
156
157          /* Currently, it could be done this way outside libecere:
158          if(parent.controls.count)
159          {
160             parent.controls.lastIterator.data.Activate();
161             inactive = true;
162             inactive = false;
163          }
164          */
165          parent.AddButton(this);
166       }
167    };
168
169    watch(checked)
170    {
171       if(!checked)
172          font = null;
173       else if(parent && eClass_IsDerived(parent._class, class(SelectorBar)))
174       {
175          SelectorButton b;
176          SelectorBar selector = (SelectorBar)parent;
177          for(b = (SelectorButton)parent.firstChild; b; b = (SelectorButton)b.next)
178          {
179             if(b.nonClient) continue;
180             if(eClass_IsDerived(b._class, class(SelectorButton)) && b != this)
181                b.checked = false;
182          }
183          font = { font.faceName, font.size, bold = true };
184          // this should not be required: the font change should resize the control and Stacker should adapt automatically
185          // why does it not?
186          selector.OnResize(selector.clientSize.w, selector.clientSize.h);
187          selector.MakeControlVisible(this);
188       }
189    };
190
191    bool OnLeftButtonDown(int x, int y, Modifiers mods)
192    {
193       bool result;
194       Activate();
195       result = Button::OnLeftButtonDown(x, y, mods);
196       if(focusHolder && !checked)
197          focusHolder.Activate();
198       return result;
199    }
200 };
201
202 public class EditableSelectorButton : SelectorButton
203 {
204    EditBox editBox;
205    bool renameable;
206
207    bool OnKeyDown(Key key, unichar ch)
208    {
209       if(key == f2 && !editBox)
210       {
211          if(!checked)
212          {
213             checked = true;
214             NotifyClicked(master, this, 0, 0, 0);
215          }
216          OnLeftButtonDown(0, 0, 0);
217          return false;
218       }
219       return SelectorButton::OnKeyDown(key, ch);
220    }
221
222    bool OnLeftButtonDown(int x, int y, Modifiers mods)
223    {
224       Activate();
225       if(renameable && checked && !editBox)
226       {
227          editBox =
228          {
229             this, anchor = { 2, 2, 2, 2 }, /*opacity = 0, */borderStyle = 0, textHorzScroll = true;
230
231             bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
232             {
233                EditBox::OnActivate(active, previous, goOnWithActivation, direct);
234                if(!active && !destroyed)
235                {
236                   ((EditableSelectorButton)master).editBox = null;
237                   Destroy(0);
238                   //OnLeavingEdit();
239                   if(master && ((EditableSelectorButton)master).focusHolder)
240                      ((EditableSelectorButton)master).focusHolder.Activate();
241                   delete this;
242                }
243                return true;
244             }
245
246             bool NotifyModified(EditBox editBox)
247             {
248                char * oldName = CopyString(text);
249                char * newName = CopyString(editBox.contents);
250
251                if(OnRename(master, this, &oldName, &newName))
252                {
253                   SelectorBar selector = (SelectorBar)parent;
254                   text = newName;
255                   if(selector)
256                      selector.MakeControlVisible(this);
257                }
258
259                delete oldName;
260                delete newName;
261
262                //OnLeavingEdit();? //master.someControl.Activate();
263                if(focusHolder)
264                   focusHolder.Activate();
265
266                return true;
267             }
268
269             bool OnKeyDown(Key key, unichar ch)
270             {
271                if((SmartKey)key == enter || key == escape)
272                {
273                   if(key == escape)
274                   {
275                      EditableSelectorButton button = (EditableSelectorButton)master;
276                      if(button.editBox)
277                         button.editBox.SetModified(false);
278                   }
279                   Deactivate();
280                   return false;
281                }
282                else
283                   return EditBox::OnKeyDown(key, ch);
284             }
285          };
286          incref editBox;
287          editBox.contents = text;
288          editBox.Create();
289          editBox.SetModified(false);
290          editBox.SelectAll();
291       }
292       return Button::OnLeftButtonDown(x, y, mods);
293    }
294
295 public:
296    property bool renameable
297    {
298       set { renameable = value; }
299       get { return renameable; }
300    }
301    property EditBox editBox { get { return editBox; } }
302
303    virtual bool Window::OnRename(EditableSelectorButton button, char ** oldName, char ** newName);
304 }