Build System: Fixed linking issues with SQLiteCipher occurring when OPENSSL_CONF...
[sdk] / ecere / src / gui / controls / DataBox.ec
1 namespace gui::controls;
2
3 import "Window"
4
5 default:
6 extern int __ecereVMethodID_class_OnGetString;
7 extern int __ecereVMethodID_class_OnGetDataFromString;
8 extern int __ecereVMethodID_class_OnCompare;
9 extern int __ecereVMethodID_class_OnSerialize;
10 extern int __ecereVMethodID_class_OnUnserialize;
11 extern int __ecereVMethodID_class_OnFree;
12 extern int __ecereVMethodID_class_OnEdit;
13 extern int __ecereVMethodID_class_OnCopy;
14 extern int __ecereVMethodID_class_OnDisplay;
15 extern int __ecereVMethodID_class_OnSaveEdit;
16 private:
17
18 public class DataBox : CommonControl
19 {
20 public:
21    Class type;
22    void * data;
23    void * fieldData;
24    Window editor;
25    bool readOnly;
26    bool keepEditor;
27    bool autoSize;
28    bool needUpdate;
29    String stringValue;
30    needUpdate = true;
31
32    ~DataBox()
33    {
34       delete stringValue;
35    }
36
37    virtual void SetData(any_object newData, bool closingDropDown)
38    {
39       //type._vTbl[__ecereVMethodID_class_OnCopy](type, data, newData);
40       needUpdate = true;
41       if(type)
42       {
43          if(type.type == normalClass || type.type == noHeadClass)
44          {
45             if(((void **)data)[0])
46                type._vTbl[__ecereVMethodID_class_OnFree](type, ((void **)data)[0]);
47             ((void **)data)[0] = newData;
48          }
49          else
50          {
51             // Free old data first
52             type._vTbl[__ecereVMethodID_class_OnFree](type, data);
53             type._vTbl[__ecereVMethodID_class_OnCopy](type, data, newData);
54          }
55       }
56       if(created)
57          NotifyChanged(master, closingDropDown);
58       //editor.Activate();
59    }
60
61    bool SaveData()      // TODO: Clear this up, along with Saving DataBox
62    {
63       if(editor && type._vTbl[__ecereVMethodID_class_OnSaveEdit](type, data, editor, null))
64       {
65          Refresh();
66          NotifyChanged(master, false);
67          // Refresh();
68          return true;
69       }
70       return false;
71    }
72
73    void Modified()
74    {
75       NotifyModified(master);
76    }
77
78    void Refresh()
79    {
80       needUpdate = true;
81       if(created)
82       {
83          if(!keepEditor)
84          {
85             editor.Destroy(0);
86             editor = null;
87          }
88          OnPostCreate();
89       }
90    }
91
92    virtual bool Window::NotifyModified();
93    virtual bool Window::NotifyChanged(bool closingDropDown);
94    virtual void OnConfigure(Window editor);
95
96 private:
97
98    bool OnPostCreate()
99    {
100       if(type && !readOnly && (type.type == normalClass || type.type == noHeadClass || data))
101       {
102          // IMPORTANT FIX: If keepEditor is true, we were passing editor rather than the editor's current master
103          editor = (Window)type._vTbl[__ecereVMethodID_class_OnEdit](type, 
104             (type.type == normalClass || type.type == noHeadClass) ? (data ? (*(void **)data) : null) : data, 
105             this, (keepEditor && editor) ? editor.master : this, 0, 0, clientSize.w, clientSize.h, fieldData);// null);
106          if(editor)
107          {
108             // editor.anchor = { 0, 0, 0, 0 };
109             editor.background = background;
110             editor.foreground = foreground;
111             editor.opacity = opacity;
112          }
113          else
114          {
115             // TODO: Should returning 0 from OnPostCreate cancel creation?
116             Destroy(0);
117             return false;
118          }
119       }
120       return true;
121    }
122
123    void OnRedraw(Surface surface)
124    {
125       if(type)
126       {
127          char tempString[1024];
128          if(needUpdate)
129          {
130             String s;
131             if(type.type == noHeadClass || type.type == normalClass)
132                s = (String)type._vTbl[__ecereVMethodID_class_OnGetString](type, *(void **)this.data, tempString, fieldData, null);
133             else
134                s = (String)type._vTbl[__ecereVMethodID_class_OnGetString](type, this.data, tempString, fieldData, null);
135             delete stringValue;
136             stringValue = CopyString(s);
137             needUpdate = false;
138          }
139          type._vTbl[__ecereVMethodID_class_OnDisplay](class(String), stringValue, surface, 3, 1, clientSize.w, fieldData, type.defaultAlignment, 0);
140       }
141    }
142
143    void OnDestroy()
144    {
145       editor = null;
146    }
147
148    void OnPosition(int x, int y, int width, int height)
149    {
150       if(editor)
151          editor.OnPosition(editor.position.x, editor.position.y, editor.size.w, editor.size.h);
152    }
153
154    bool OnKeyHit(Key key, unichar ch)
155    {
156       return editor ? editor.OnKeyHit(key, ch) : true;
157    }
158
159    bool OnKeyDown(Key key, unichar ch)
160    {
161       if((SmartKey)key == enter)
162       {
163          SaveData();
164          return false;
165       }
166       else if((SmartKey)key == escape)
167       {
168          Refresh();
169          return true;
170       }
171       return editor ? editor.OnKeyDown(key, ch) : true;
172    }
173 };
174
175 public class SavingDataBox : DataBox
176 {
177    class_property(icon) = "<:ecere>controls/dataBox.png";
178    borderStyle = deep;
179    bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
180    {
181       if(!active && editor && !editor.modalSlave)
182       {
183          if(!SaveData())
184             Refresh();
185       }
186       return true;
187    }
188
189    bool OnKeyDown(Key key, unichar ch)
190    {
191       if((SmartKey)key == enter)
192       {
193          if(!SaveData())
194             // Force Refresh on Enter if SaveData didn't do it
195             Refresh();
196          return true;
197       }
198       return DataBox::OnKeyDown(key, ch);
199    }
200 }