f6d52ba3b60ee9ea2923f605a1478d52138833c3
[sdk] / eda / libeda / src / gui / controls / FieldBox.ec
1 import "TableEditor"
2
3 default:
4 extern int __ecereVMethodID_class_OnFree;
5 private:
6
7 public class FieldBox : DataBox
8 {
9    class_no_expansion;
10    size = { 100, 22 };
11    borderStyle = deep;
12
13    Field field;
14    int64 dataHolder; // THERE SEEMS TO BE A BUG WHEN ACCESSING row ACROSS .so
15    TableEditor editor;
16
17    property Row row
18    {
19       get
20       {
21          Row result = null;
22          if(field && editor)
23          {
24             if(editor.table == field.table)
25                result = editor.editRow;
26             else if(editor.lookups)
27             {
28                Lookup lookup = editor.lookups[field.table]; // Map<Table, Lookup> limits to single lookup per table
29                if(lookup.valueField && lookup.findField && lookup.row && !lookup.row.nil)
30                   result = lookup.row;
31             }
32          }
33          return result;
34       }
35    }
36    
37    // DataBox has a member called editor as well?
38    // would like to rename TableEditor to TableControl anyway
39    public property TableEditor editor
40    {
41       set
42       {
43          if(value != editor)
44          {
45             if(editor)
46                editor.RemoveFieldBox(this);
47             editor = value;
48             if(value)
49                value.AddFieldBox(this);
50          }
51       }
52    }
53
54    watch(parent)
55    {
56       if(eClass_IsDerived(parent._class, class(TableEditor)))
57          property::editor = (TableEditor)parent;
58    };
59
60    watch(master)
61    {
62       if(eClass_IsDerived(master._class, class(TableEditor)))
63          property::editor = (TableEditor)master;
64    };
65
66    public property Field field
67    {
68       set
69       {
70          Class dataType;
71          if(field) type = null;
72
73          if(dataHolder)
74          {
75             ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, (void *)dataHolder);
76             if(type.type == structClass)
77             {
78                void * dataPtr = (void *)dataHolder;
79                delete dataPtr;
80             }
81             dataHolder = 0;
82          }
83
84          field = value;
85          dataType = value ? value.type : null;
86          if(!text || !text[0])
87             text = field ? field.name : null;
88
89          if(dataType && dataType.type == structClass)
90          {
91             dataHolder = (int64)new0 byte[dataType.structSize];
92             data = (void *)dataHolder;
93          }
94          else if(dataType && (dataType.type == noHeadClass || dataType.type == normalClass))
95          {
96             if(eClass_IsDerived(dataType, class(String)))
97                dataHolder = (int64)CopyString("");
98             else
99                dataHolder = (int64)eInstance_New(dataType);
100             data = (void *)&dataHolder;
101          }
102          else
103          {
104             dataHolder = 0;
105             data = &dataHolder;
106          }
107          if(!type) type = dataType;
108       }
109    }
110
111    void Clear()
112    {
113       if(visible)
114       {
115          if(data)
116             SetData(null, false);
117
118          if(type && (type.type == noHeadClass || type.type == normalClass))
119          {
120             if(eClass_IsDerived(type, class(String)))
121                dataHolder = (int64)CopyString("");
122             else
123                dataHolder = (int64)eInstance_New(type);
124             data = (void *)&dataHolder;
125          }
126
127          if(created)
128             Refresh();
129       }
130    }
131
132    void Load()
133    {
134       Row row = this.row;
135       if(visible && row)
136       {
137          //Id test = row.sysID;
138          SetData(null, false);
139
140          ((bool (*)())(void *)Row::GetData)(row, field, field.type, data);
141
142          if(!dataHolder && type && (type.type == noHeadClass || type.type == normalClass))
143          {
144             if(eClass_IsDerived(type, class(String)))
145                dataHolder = (int64)CopyString("");
146             else
147                dataHolder = (int64)eInstance_New(type);
148             data = (void *)&dataHolder;
149          }
150
151          // if(created)
152          Refresh();
153       }
154    }
155
156    virtual void Save()
157    {
158       bool result;
159       Row row = this.row;
160       if(visible && row)
161       {
162          Class type = field.type;
163          if(!DataBox::SaveData())
164             Refresh();
165
166          ((bool (*)())(void *)Row::SetData)(row, field, type, 
167             (type.type == noHeadClass || type.type == normalClass) ? *(void **)data : data);
168       
169          modifiedDocument = false;
170       }
171    }
172
173    void Init()
174    {
175       if(visible && created)
176          Refresh();
177    }
178
179    bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
180    {
181       if(!active)
182       {
183          if(modifiedDocument && !DataBox::SaveData())
184             Refresh();
185       }
186       return true;
187    }
188
189    bool OnKeyDown(Key key, unichar ch)
190    {
191       if((SmartKey)key == enter)
192       {
193          DataBox::OnKeyDown(key, ch);
194          return true;
195       }
196       else
197          return DataBox::OnKeyDown(key, ch);
198    }
199
200    bool OnKeyHit(Key key, unichar ch)
201    {
202       if((SmartKey)key == enter)
203          parent.CycleChildren(true, false, false, true);
204       
205       return DataBox::OnKeyHit(key, ch);
206    }
207
208    bool Window::NotifyChanged(bool closingDropDown)
209    {
210       // This gets called on the master and is unaware of the change being user input or internally set
211       modifiedDocument = true;
212       return true;
213    }
214
215    bool Window::NotifyModified()
216    {
217       // This gets called on the master and is unaware of the change being user input or internally set
218       modifiedDocument = true;
219       return true;
220    }
221
222    ~FieldBox()
223    {
224       if(data)
225          SetData(null, false);
226
227       if(dataHolder)
228       {
229          ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, (void *)dataHolder);
230          if(type.type == structClass)
231          {
232             void * dataPtr = (void *)dataHolder;
233             delete dataPtr;
234          }
235          dataHolder = 0;
236       }
237    }
238 }