sdk/ecere/ide: Improved color customization support; Set up a decent default distinct...
[sdk] / ide / src / designer / Sheet.ec
1 import "ide"
2
3 import "CodeObject"
4
5 static void UnusedFunction()
6 {
7    int a;
8    Module b;
9    a.OnGetString(0,0,0);
10    a.OnFree();
11    a.OnCopy(null);
12    a.OnCompare(null);
13    a.OnSaveEdit(null,0);
14    a.OnEdit(null,null,0,0,0,0,0);
15    a.OnDisplay(null,0,0,0,0,0,0);
16    b.OnLoad();
17 }
18
19 extern int __ecereVMethodID_class_OnEdit;
20 extern int __ecereVMethodID_class_OnDisplay;
21 extern int __ecereVMethodID_class_OnGetString;
22 extern int __ecereVMethodID_class_OnFree;
23 extern int __ecereVMethodID_class_OnCompare;
24 extern int __ecereVMethodID_class_OnCopy;
25 extern int __ecereVMethodID_class_OnSaveEdit;
26
27 //#define SHOW_METHODS
28
29
30 // *** THESE METHODS SHOULD BE IMPROVED UPON AND USED TO SET PROPERTIES FROM NOW ON ***
31 //     (Other locations where this will be useful: JSON (DataValue version?), CodeEditor, ...)
32 // This takes in a value according to the any_object rules
33
34 void SetPropValue(Property prop, void * object, any_object value)
35 {
36    Class type = prop.dataTypeClass;
37    if(!type)
38       type = prop.dataTypeClass = eSystem_FindClass(prop._class.module, prop.dataTypeString);
39
40    if(type.type == normalClass || type.type == noHeadClass || type.type == structClass)
41    {
42       prop.Set(object, value);
43    }
44    // TOFIX: How to swiftly handle classes with base data type?
45    else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
46    {
47       ((void (*)(void *, double))(void *)prop.Set)(object, *(double *)value);
48    }
49    else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
50    {
51       ((void (*)(void *, float))(void *)prop.Set)(object, *(float *)value);
52    }
53    else if(type.typeSize == sizeof(int64))// || !strcmp(type.dataTypeString, "int64") || !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
54    {
55       ((void (*)(void *, int64))(void *)prop.Set)(object, *(int64 *)value);
56    }
57    else if(type.typeSize == sizeof(int))// || !strcmp(type.dataTypeString, "int") || !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
58    {
59       ((void (*)(void *, int))(void *)prop.Set)(object, *(int *)value);
60    }
61    else if(type.typeSize == sizeof(short int)) // || !strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||  !strcmp(type.dataTypeString, "int16"))
62    {
63       ((void (*)(void *, short))(void *)prop.Set)(object, *(short *)value);
64    }
65    else if(type.typeSize == sizeof(byte))// || !strcmp(type.dataTypeString, "char") || !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
66    {
67       ((void (*)(void *, byte))(void *)prop.Set)(object, *(byte *)value);
68    }
69    else
70    {
71       ((void (*)(void *, void *))(void *)prop.Set)(object, value);
72    }
73 }
74
75 any_object GetPropValue(Property prop, Instance object)
76 {
77    if(object)
78    {
79       Class type = prop.dataTypeClass;
80       if(!type)
81       {
82          type = prop.dataTypeClass = eSystem_FindClass(prop._class.module, prop.dataTypeString);
83       }
84
85       if(type.type == normalClass || type.type == noHeadClass || type.type == structClass)
86       {
87          return ((void*(*)())(void *)prop.Get)(object);
88       }
89       // TOFIX: How to swiftly handle classes with base data type?
90       else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
91       {
92          double d = ((double(*)())(void *)prop.Get)(object);
93          return d;
94       }
95       else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
96       {
97          float f =((float(*)())(void *)prop.Get)(object);
98          return f;
99       }
100       else if(type.typeSize == sizeof(int64))// || !strcmp(type.dataTypeString, "int64") || !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
101       {
102          return ((int64(*)())(void *)prop.Get)(object);
103       }
104       else if(type.typeSize == sizeof(int))// || !strcmp(type.dataTypeString, "int") || !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
105       {
106          return ((int(*)())(void *)prop.Get)(object);
107       }
108       else if(type.typeSize == sizeof(short int)) // || !strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||  !strcmp(type.dataTypeString, "int16"))
109       {
110          return ((short(*)())(void *)prop.Get)(object);
111       }
112       else if(type.typeSize == sizeof(byte))// || !strcmp(type.dataTypeString, "char") || !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
113       {
114          return ((byte(*)())(void *)prop.Get)(object);
115       }
116       else
117       {
118          return prop.Get(object);
119       }
120    }
121    else
122       return 0;
123 }
124
125 void CopyProperty(Property prop, Instance dest, Instance src)
126 {
127    Class type = prop.dataTypeClass;
128    if(!type)
129       type = prop.dataTypeClass = eSystem_FindClass(prop._class.module, prop.dataTypeString);
130
131    if(type.type == structClass)
132    {
133       void * propData = new0 byte[type.structSize];
134       prop.Get(src, propData);
135       prop.Set(dest, propData);
136       delete propData;
137    }
138    else if(type.type == normalClass || type.type == noHeadClass)
139    {
140       // TOCHECK: Why was there a return here?
141       /*return */prop.Set(dest, ((void*(*)())(void *)prop.Get)(src));
142    }
143    // TOFIX: How to swiftly handle classes with base data type?
144    else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
145    {
146       ((void (*)(void *, double))(void *)prop.Set)(dest, ((double(*)())(void *)prop.Get)(src));
147    }
148    else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
149    {
150       ((void (*)(void *, float))(void *)prop.Set)(dest, ((float(*)())(void *)prop.Get)(src));
151    }
152    else if(type.typeSize == sizeof(int64))// || !strcmp(type.dataTypeString, "int64") || !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
153    {
154       ((void (*)(void *, int64))(void *)prop.Set)(dest, ((int64(*)())(void *)prop.Get)(src));
155    }
156    else if(type.typeSize == sizeof(int))// || !strcmp(type.dataTypeString, "int") || !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
157    {
158       ((void (*)(void *, int))(void *)prop.Set)(dest, ((int(*)())(void *)prop.Get)(src));
159    }
160    else if(type.typeSize == sizeof(short int)) // || !strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||  !strcmp(type.dataTypeString, "int16"))
161    {
162       ((void (*)(void *, short))(void *)prop.Set)(dest, ((short(*)())(void *)prop.Get)(src));
163    }
164    else if(type.typeSize == sizeof(byte))// || !strcmp(type.dataTypeString, "char") || !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
165    {
166       ((void (*)(void *, byte))(void *)prop.Set)(dest, ((byte(*)())(void *)prop.Get)(src));
167    }
168    else
169    {
170       prop.Set(dest, prop.Get(src));
171    }
172 }
173
174 void GetProperty(Property prop, Instance object, DataValue value)
175 {
176    if(object)
177    {
178       Class type = prop.dataTypeClass;
179       if(!type)
180       {
181          type = prop.dataTypeClass = eSystem_FindClass(prop._class.module, prop.dataTypeString);
182 #ifdef _DEBUG
183          if(prop._class.module.application == __thisModule &&
184             prop.dataTypeClass.module.application == ((Designer)GetActiveDesigner()).codeEditor.privateModule)
185             printf("Warning");
186 #endif
187       }
188
189       if(type.type == normalClass || type.type == noHeadClass || type.type == structClass)
190       {
191          value.p = ((void*(*)())(void *)prop.Get)(object);
192       }
193       // TOFIX: How to swiftly handle classes with base data type?
194       else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
195       {
196          value.d = ((double(*)())(void *)prop.Get)(object);
197       }
198       else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
199       {
200          value.f = ((float(*)())(void *)prop.Get)(object);
201       }
202       else if(type.typeSize == sizeof(int64))// || !strcmp(type.dataTypeString, "int64") || !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
203       {
204          value.i64 = ((int64(*)())(void *)prop.Get)(object);
205       }
206       else if(type.typeSize == sizeof(int))// || !strcmp(type.dataTypeString, "int") || !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
207       {
208          value.i = ((int(*)())(void *)prop.Get)(object);
209       }
210       else if(type.typeSize == sizeof(short int)) // || !strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||  !strcmp(type.dataTypeString, "int16"))
211       {
212          value.s = ((short(*)())(void *)prop.Get)(object);
213       }
214       else if(type.typeSize == sizeof(byte))// || !strcmp(type.dataTypeString, "char") || !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
215       {
216          value.uc = ((byte(*)())(void *)prop.Get)(object);
217       }
218       else
219       {
220          value.i = prop.Get(object);
221       }
222    }
223    else
224       value.i64 = 0;
225 }
226
227 void SetProperty(Property prop, Instance object, DataValue value)
228 {
229    if(object)
230    {
231       Class type = prop.dataTypeClass;
232       if(!type)
233          type = prop.dataTypeClass = eSystem_FindClass(prop._class.module, prop.dataTypeString);
234
235       if(type.type == normalClass || type.type == noHeadClass || type.type == structClass)
236       {
237          prop.Set(object, value);
238       }
239       // TOFIX: How to swiftly handle classes with base data type?
240       else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
241       {
242          ((void (*)(void *, double))(void *)prop.Set)(object, value.d);
243       }
244       else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
245       {
246          ((void (*)(void *, float))(void *)prop.Set)(object, value.f);
247       }
248       else if(type.typeSize == sizeof(int64))// || !strcmp(type.dataTypeString, "int64") || !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
249       {
250          ((void (*)(void *, int64))(void *)prop.Set)(object, value.i64);
251       }
252       else if(type.typeSize == sizeof(int))// || !strcmp(type.dataTypeString, "int") || !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
253       {
254          ((void (*)(void *, int))(void *)prop.Set)(object, value.i);
255       }
256       else if(type.typeSize == sizeof(short int)) // || !strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||  !strcmp(type.dataTypeString, "int16"))
257       {
258          ((void (*)(void *, short))(void *)prop.Set)(object, value.s);
259       }
260       else if(type.typeSize == sizeof(byte))// || !strcmp(type.dataTypeString, "char") || !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
261       {
262          ((void (*)(void *, byte))(void *)prop.Set)(object, value.uc);
263       }
264       else
265       {
266          ((void (*)(void *, int))(void *)prop.Set)(object, value.i);
267       }
268    }
269 }
270
271 class Sheet : Window
272 {
273    text = "Sheet";
274    borderStyle = sizable;
275    hasClose = true;
276    //tabCycle = true;
277    size.w = 300;
278    anchor = { left = 0, top = 0, bottom = 0 };
279    background = activeBorder;
280
281    Sheet()
282    {
283       dropBox.AddField(dropField);
284       properties.AddField(propertyName);
285       properties.AddField(propertyValue);
286       methods.AddField(methodName);
287    }
288
289    ~Sheet()
290    {
291       categories.Free(null);
292    }
293
294    DropBox dropBox
295    {
296       this,
297       anchor = { left = 0, top = 0, right = 0 };
298
299       bool NotifySelect(DropBox control, DataRow row, Modifiers keyFlags)
300       {
301          ObjectInfo selected = (ObjectInfo)(row ? row.tag : null);
302          ToolBox toolBox = ((IDE)parent).toolBox;
303
304          if(codeEditor && selected)
305             codeEditor.SelectObject(selected);
306
307          // TODO: Get containing class of object
308          toolBox.selectedClass = selected ? selected.oClass : null;
309
310          object = selected ? selected.instance : null;
311
312          methods.Clear();
313          ListProperties(true);
314
315          {
316             DataRow row;
317             row = methods.currentRow;
318             if(row)
319                strcpy(selectedMethod, ((CodeObject)row.GetData(methodName)).name);
320          }
321
322
323          if(selected && selected.instance && codeEditor)
324          {
325             Class _class;
326             int c = 0;
327             int rowHeight = methods.rowHeight;
328
329             propertyValue.userData = (void *)selected.instance;
330
331             // Fill up the methods
332             {
333                CodeObjectType type;
334                {
335                   for(_class = selected.instance._class; _class && _class.type != systemClass; _class = _class.base)
336                   {
337                      int id;
338                      for(id = _class.base ? _class.base.vTblSize : 0; id<_class.vTblSize; id++)
339                      {
340                         Method method;
341                         for(method = (Method)_class.methods.first; method; method = (Method)((BTNode)method).next)
342                         {
343                            if(method.type == virtualMethod && method.vid == id)
344                            {
345                               if(!method.dataType)
346                                  method.dataType = ProcessTypeString(method.dataTypeString, false);
347
348                               type = method.dataType.thisClass ? typeEvent : typeMethod;
349                               {
350                                  DataRow row = methods.AddRow();
351                                  CodeObject codeObject
352                                  {
353                                     eventsUp = (selected.oClass == selected) ? false : true;
354                                     type = type;
355                                     method = method;
356                                     name = method.name;
357                                     overriden = codeEditor.FindMethod(method.name, &codeObject.function, null);
358                                  };
359                                  if(!codeObject.overriden || codeObject.overriden == 2)
360                                     codeEditor.FindCompatibleMethods(method, codeObject.compatible);
361
362                                  row.SetData(methodName, codeObject);
363
364                                  if(!strcmp(method.name, selectedMethod))
365                                     methods.currentRow = row;
366                               }
367                            }
368                         }
369                      }
370                   }
371                }
372             }
373             methods.Sort(methodName, 1);
374             {
375                DataRow row;
376                for(row = methods.firstRow; row; row = row.next)
377                {
378                   CodeObject codeObject = row.GetData(methodName);
379                   CreateButtons(codeObject, row.index * rowHeight, rowHeight, row);
380                }
381             }
382          }
383          return true;
384       }
385    };
386    DataField dropField { dataType = class(CodeObject) };
387
388    Button propBtn
389    {
390       this, inactive = true, text = "Properties", bevelOver = true, isRadio = true;
391       size.h = 20;
392       anchor = { left = 0, bottom = 3, right = 0.5 };
393       bitmap = null;
394
395       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
396       {
397          text = "Properties";
398          button.font = { "Tahoma", 8.25f, bold = true };
399          methBtn.font = null;
400
401          methods.visible = false;
402          methBtn.Activate();      // Ensure proper cycling (until tab order?)
403          properties.visible = true;
404
405          alphabetical.disabled = false;
406          categorized.disabled = false;
407
408          properties.Activate();
409
410          // ((IDE)master).SheetSelected(Properties);
411          return true;
412       }
413    };
414
415    Button methBtn
416    {
417       this, inactive = true, bevelOver = true;
418       text = "Methods";
419       isRadio = true;
420       bitmap = null;
421       size.h = 20;
422       anchor = { bottom = 3, left = 0.5, right = 0 };
423
424       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
425       {
426          text = "Methods";
427          button.font = { "Tahoma", 8.25f, bold = true };
428          propBtn.font = null;
429
430          properties.visible = false;
431          methBtn.Activate();      // Ensure proper cycling (until tab order?)
432          methods.visible = true;
433
434          alphabetical.disabled = true;
435          categorized.disabled = true;
436
437          methods.Activate();
438                        
439          // ((IDE)master).SheetSelected(Methods);
440          return true;
441       }
442    };
443
444    // Menu
445    menu = Menu { };
446    MenuPlacement editMenu { menu, text = "Edit" };
447    Menu viewMenu { menu, text = "View" };
448
449    // Property Sheet
450    ListBox properties
451    {
452       this, anchor = { left = 0, right = 0, top = 50, bottom = 25 };
453       hasVertScroll = true, alwaysEdit = true, collapseControl = true, resizable = true;
454       background = viewsBackground;
455       foreground = viewsText;
456
457       bool NotifySelect(ListBox control, DataRow row, Modifiers keyFlags)
458       {
459          /*
460          if(row)
461          {
462             strcpy(selectedProp, (char *)row.GetData(propertyName));
463             selectedScroll = properties.scroll.y;
464             selectedScroll -= row.index * properties.rowHeight;
465          }
466          else
467             selectedProp[0] = 0;
468          if(row)
469          {
470             prop = ((PropertyInfo)row.GetData(propertyValue)).prop;
471          }
472          else
473             prop = null;
474          */
475          return true;
476       }
477    };
478
479    DataField propertyName { dataType = class(char *), width = 130 };
480    DataField propertyValue { dataType = class(PropertyInfo), width = 0, editable = true };
481
482    // Method Sheet
483    ListBox methods
484    {
485       this, anchor = { left = 0, right = 0, top = 50, bottom = 25 };
486       hasVertScroll = true;
487       background = viewsBackground;
488       foreground = viewsText;
489       // alwaysEdit = true;
490       // resizable = true;
491
492       bool NotifyDoubleClick(ListBox control, int x, int y, Modifiers mods)
493       {
494          CodeObject object = control.GetData(methodName);
495          if(object)
496             codeEditor.AddMethod(object.method);   
497          return false;
498       }
499
500       bool NotifyRightClick(ListBox control, int x, int y, Modifiers mods)
501       {
502          CodeObject object = control.GetData(methodName);
503          Menu menu { };
504          PopupMenu popupMenu;
505          MenuItem item;
506          if(object.overriden == 0)
507          {
508             MenuItem { menu, "Override", o, enter, bold = true, NotifySelect = OverrideMethodSelected };
509             if(object.compatible.count)
510             {
511                Menu attachMenu { menu, "Attach", a };
512                OldLink compatible;
513                for(compatible = object.compatible.first; compatible; compatible = compatible.next)
514                {
515                   ClassFunction function = compatible.data;
516                   MenuItem { attachMenu, function.declarator.symbol.string, id = (int)function, NotifySelect = AttachMethodSelected };
517                }
518             }
519          }
520          else if(object.overriden == 1)
521          {
522             MenuItem { menu, "Go to", g, enter, bold = true, NotifySelect = GotoMethodSelected };
523             MenuItem { menu, "Detach", d, d, NotifySelect = DetachMethodSelected };
524             MenuItem { menu, "Delete", del, del, NotifySelect = DeleteMethodSelected };
525          }
526          else if(object.overriden == 2)
527          {
528             MenuItem { menu, "Go to", g, enter, bold = true, NotifySelect = GotoMethodSelected };
529             MenuItem { menu, "Detach", d, d, NotifySelect = DetachMethodSelected };
530             if(object.compatible.count > 1)
531             {
532                Menu attachMenu { menu, "Reattach", r };
533                OldLink compatible;
534                for(compatible = object.compatible.first; compatible; compatible = compatible.next)
535                {
536                   ClassFunction function = compatible.data;
537                   if(function != object.function)
538                   {
539                      MenuItem { attachMenu, function.declarator.symbol.string, id = (int)function, NotifySelect = ReattachMethodSelected };
540                   }
541                }
542             }
543          }
544
545          attachMethod = object.method;
546          popupMenu = PopupMenu { menu = menu, master = this, position =
547             {
548                x + control.absPosition.x - app.desktop.absPosition.x,
549                y + control.absPosition.y - app.desktop.absPosition.y
550             } };
551          popupMenu.Create();
552          // popupMenu.Capture();
553          return false;
554       }
555
556       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
557       {
558          if(row)
559          {
560             CodeObject object = row.GetData(methodName);
561             switch((SmartKey)key)
562             {
563                case enter:
564                   if(!object.overriden)
565                      listBox.NotifyDoubleClick(this, listBox, 0,0, 0);
566                   else
567                      codeEditor.GoToMethod(object.method.name);
568                   break;
569                case del:
570                   if(object.deleteBtn)
571                      object.deleteBtn.NotifyClicked(this, object.deleteBtn, 0,0,0);
572                   else if(object.detachBtn)
573                      object.detachBtn.NotifyClicked(this, object.detachBtn, 0,0,0);
574                   break;
575                case a:
576                   if(object.attachBtn)
577                      object.attachBtn.NotifyPushed(this, object.attachBtn, 0,0,0);
578                   break;
579                case d:
580                   if(object.detachBtn)
581                      object.detachBtn.NotifyClicked(this, object.detachBtn, 0,0,0);
582                   break;
583             }
584          }
585          return true;
586       }
587    };
588
589    DataField methodName { dataType = class(CodeObject) };
590
591 #ifdef SHOW_METHODS
592    methBtn.font = { "Tahoma", 8.25, bold = true };
593    methBtn.checked = true;
594    properties.visible = false;
595    text = "Methods";
596 #else
597    propBtn.font = { "Tahoma", 8.25f, bold = true };
598    propBtn.checked = true;
599    methods.visible = false;
600    text = "Properties";
601 #endif
602
603    Button alphabetical
604    {
605       this, bevelOver = true, inactive = true, position = { 25, 25 }, size = { 24, 24 };
606       bitmap = { "<:ecere>elements/orderAscending.png" };
607       // isRadio = true;
608
609       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
610       {
611          if(!alphabetical.checked)
612          {
613             alphabetical.checked = true;
614             categorized.checked = false;
615
616             ListProperties(true);
617          }
618          return true;
619       }
620    };
621
622    Button categorized
623    {
624       this, bevelOver = true, checked = true, inactive = true, position = { 0, 25 }, size = { 24, 24 };
625       bitmap = { "<:ecere>elements/orderCategorized.png" };
626       // isRadio = true;
627       
628       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
629       {
630          if(!categorized.checked)
631          {
632             categorized.checked = true;
633             alphabetical.checked = false;
634
635             ListProperties(true);
636          }
637          return true;
638       }
639    };
640
641    property CodeEditor codeEditor
642    {
643       set
644       {
645          if(codeEditor != value)
646          {
647             codeEditor = value;
648             // Refill it up
649             dropBox.Clear();
650             dropField.userData = codeEditor;
651             methodName.userData = codeEditor;
652             
653             if(codeEditor)
654                codeEditor.EnumerateObjects(this);
655           }
656       }
657       get
658       {
659          return codeEditor;
660       }
661    }
662
663    property SheetType sheetSelected
664    {
665       set
666       {
667          if(methBtn.checked != (value == SheetType::methods))
668             ToggleSheet();
669       }
670       get
671       {
672          return methBtn.checked ? methods : properties;
673       }
674    }
675
676    bool OnClose(bool parentClosing)
677    {
678       if(!parentClosing)
679       {
680          visible = false;
681          return false;
682       }
683       return true;
684    }
685
686    bool OnKeyDown(Key key, unichar ch)
687    {
688       if(key == shiftTab)
689          dropBox.Activate();
690       else if(key == tab)
691          ToggleSheet();
692       else if(key == escape)
693       {
694          Window activeClient = ide.activeClient;
695          if(activeClient) 
696             activeClient.Activate();
697          else 
698             ide.RepositionWindows(true);
699       }
700       return true;
701    }
702
703    bool OnKeyHit(Key key, unichar ch)
704    {
705       return properties.OnKeyHit(key, ch);
706    }
707
708    bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
709    {
710       if(active && codeEditor)
711          codeEditor.EnsureUpToDate();
712       return true;
713    }
714
715    void ListProperties(bool clear)
716    {
717       DataRow row = dropBox.currentRow;
718       ObjectInfo selected = row ? (ObjectInfo)row.tag : null;
719       
720       //int scroll = 0;
721       bool categorized = this.categorized.checked;
722       bool currentRow = false;
723       char selectedProp[1024];
724
725       if(clear)
726       {
727          Category cat;
728          DataRow row = properties.currentRow;
729          if(row)
730          {
731             DataRow propRow = row;
732             char * propName;
733             while(propRow && propRow.parent && !propRow.parent.isHeader)
734                propRow = row.parent;
735             
736             propName = propRow.GetData(propertyName);
737             strcpy(this.selectedProp, propName);
738             selectedScroll = properties.scroll.y;
739             selectedScroll -= propRow.index * properties.rowHeight;
740          }
741          currentRow = this.selectedProp[0] ? true : false;
742
743          for(cat = categories.first; cat; cat = cat.next)
744          {
745             cat.collapsed = cat.row.collapsed;
746             cat.row = null;
747          }
748
749          // Preserve selected property (PropertySheetSelect will null it)
750          strcpy(selectedProp, this.selectedProp);
751          properties.Clear();
752          strcpy(this.selectedProp, selectedProp);
753       }
754       if(selected && selected.instance && codeEditor)
755       {
756          Instance test = eInstance_New(selected.instance._class);
757          Class _class = null;
758          incref test;
759
760          // Put it in the same desktop window...
761          if(selected.classDefinition)
762             codeEditor.designer.PrepareTestObject(test);
763
764          // Fill up the properties
765          while(_class != selected.instance._class)
766          {
767             BitMember bitMember = null;
768             Class lastClass = _class;
769             Property propIt;
770
771             for(_class = selected.instance._class; _class.base != lastClass && _class.base.type != systemClass && _class.inheritanceAccess != privateAccess; _class = _class.base);
772
773             for(propIt = _class.membersAndProperties.first; propIt; propIt = propIt.next)
774             {
775                if(propIt.isProperty)
776                {
777                   Property prop = eClass_FindProperty(selected.instance._class, propIt.name, GetPrivateModule());
778                   
779                   if(prop && prop.Set && prop.Get && prop.compiled)
780                   {
781                      bool disabled = Code_IsPropertyDisabled(selected, prop.name);
782                      bool bold;
783                      Class dataType = prop.dataTypeClass;
784                      if(!dataType)
785                         dataType = prop.dataTypeClass = eSystem_FindClass(codeEditor.privateModule, prop.dataTypeString);
786
787                      if(!strcmp(_class.name, "DesignerBase"))
788                         disabled = true;
789                      bold = !disabled && Code_IsPropertyModified(test, selected, prop);                           
790
791                      if(dataType)
792                      {
793                         DataRow row;
794                         PropertyInfo info { prop, disabled, bold ? codeEditor.boldFont : codeEditor.normalFont };
795                         char * name = prop.category ? prop.category : "Misc";
796                         Category category = categories.FindName(name, false);
797
798                         // Hide properties like this for now..
799                         if(name && !strcmp(name, "Private")) 
800                            continue;
801
802                         if(!category)
803                         {
804                            category = Category { name = name };
805                            categories.AddName(category);
806                         }
807                         if(!category.row && categorized)
808                         {
809                            PropertyInfo catInfo { null, false, null, name };
810                            category.row = properties.AddRow();
811                            category.row.SetData(propertyName, name );
812                            category.row.SetData(propertyValue, catInfo);
813                            category.row.isHeader = true;
814                            category.row.collapsed = category.collapsed;
815                         }
816
817                         if(clear)
818                         {
819                            row = categorized ? category.row.FindRow((int)prop) : properties.FindRow((int)prop);
820                            if(!row)
821                               row = categorized ? category.row.AddRow() : properties.AddRow();
822                            row.tag = (int)prop;
823                         }
824                         else
825                            row = categorized ? category.row.FindRow((int)prop) : properties.FindRow((int)prop);
826
827                         row.SetData(propertyName, prop.name);
828                         row.SetData(propertyValue, info);
829
830                         if(clear && !strcmp(prop.name, this.selectedProp))
831                            properties.currentRow = row;
832
833                         if(!dataType.noExpansion && (dataType.type == structClass || dataType.type == normalClass || dataType.type == noHeadClass || dataType.type == bitClass))
834                         {
835                            DataMember member;
836                            
837                            if(clear)
838                               row.collapsed = true;
839
840                            for(member = dataType.membersAndProperties.first; member; member = member.next)
841                            {
842                               if(member.isProperty)
843                               {
844                                  Property subProp = (Property)member;
845                                  if(!subProp.conversion && subProp.Get && subProp.Set)
846                                  {
847                                     DataRow subRow;
848                                     PropertyInfo info { prop, disabled, bold ? codeEditor.boldFont : codeEditor.normalFont, null, null, subProp };
849
850                                     if(clear)
851                                     {
852                                        subRow = row.AddRow();
853                                        subRow.tag = (int)subProp;
854                                     }
855                                     else
856                                        subRow = row.FindRow((int)subProp);
857                                     
858                                     subRow.SetData(propertyName, subProp.name);
859                                     subRow.SetData(propertyValue, info);
860                                  }
861                               }
862                               else if(member.name)
863                               {
864                                  DataRow subRow;
865                                  PropertyInfo info { prop, disabled, bold ? codeEditor.boldFont : codeEditor.normalFont, null, member, null };
866                                  if(clear)
867                                  {
868                                     subRow = row.AddRow();
869                                     subRow.tag = (int)member;
870                                  }
871                                  else
872                                     subRow = row.FindRow((int)member);
873
874                                  subRow.SetData(propertyName, member.name);
875                                  subRow.SetData(propertyValue, info);
876                               }
877                               else
878                               {
879                                  DataMember subMember;
880                                  for(subMember = member.members.first; subMember; subMember = subMember.next)
881                                  {
882                                     DataRow subRow;
883                                     PropertyInfo info { prop, disabled, bold ? codeEditor.boldFont : codeEditor.normalFont, null, subMember, null, member.offset };
884                                     if(clear)
885                                     {
886                                        subRow = row.AddRow();
887                                        subRow.tag = (int)subMember;
888                                     }
889                                     else
890                                        subRow = row.FindRow((int)subMember);
891
892                                     subRow.SetData(propertyName, subMember.name);
893                                     subRow.SetData(propertyValue, info);
894                                  }
895                               }
896                            }
897                         }
898                      }
899                   }
900                }
901             }
902          }
903          delete test;
904          // Sort alphabetically for now...
905          if(clear)
906          {
907             // properties.Sort(null, 1);
908             properties.Sort(propertyValue, 1);
909             if(!properties.currentRow)
910             {
911                bool found = false;
912                
913                for(_class = selected.instance._class; _class; _class = _class.base)
914                {
915                   Property prop;
916                   for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
917                   {
918                      if(prop.isProperty && prop.Set && prop.Get && prop.compiled)
919                      {
920                         if(_class.defaultProperty && !strcmp(prop.name, _class.defaultProperty))
921                         {
922                            DataRow row;
923                            char * name = prop.category ? prop.category : "Misc";
924                            Category category = categories.FindName(name, false);
925                            row = category ? (categorized ? category.row.FindRow((int)prop) : properties.FindRow((int)prop)) : null;
926                            properties.currentRow = row;
927                            found = true;
928                            break;                                                                                                                              
929                         }
930                      }
931                   }
932                   if(found) break;
933                }
934                if(!found)
935                   properties.currentRow = properties.firstRow;
936             }
937
938             if(currentRow)
939             {
940                DataRow row = properties.currentRow;
941                properties.scroll.y = selectedScroll + row.index * properties.rowHeight;
942             }
943          }
944       }
945    }
946
947    void AddObject(ObjectInfo object, char * name, CodeObjectType type, bool select)
948    {
949       DataRow after = null;
950       DataRow row;
951       CodeObject codeObject;
952       char * bitmap = null;
953       bool foundClass = false;
954
955       for(row = dropBox.firstRow; row; row = row.next)
956       {
957          CodeObject codeObject = row.GetData(null);
958          if(codeObject.object.oClass == object.oClass)
959             foundClass = true;
960          else if(foundClass)
961             break;
962          after = row;
963       }
964
965       row = (DataRow)dropBox.AddRowAfter(after);
966       
967       row.tag = (int)object;
968
969       codeObject = 
970       {
971          object = object;
972          name = name;
973          type = type;
974          indent = (type == typeClass) ? 0 : 1;
975       };
976
977       if(type != typeClass)
978          bitmap = (char *)eClass_GetProperty(object.instance._class, "icon");
979       if(bitmap)
980       {
981          codeObject.bitmap = { bitmap };
982          AddResource(codeObject.bitmap);
983       }
984       
985       row.SetData(null, codeObject);
986
987       if(select)
988       {
989          this.object = object ? object.instance : null;
990          propertyValue.userData = object ? (void *)object.instance : null;
991          dropBox.SelectRow(row);
992       }
993    }
994
995    void DeleteObject(ObjectInfo object)
996    {
997       DataRow row = dropBox.FindRow((int)object);
998       if(row)
999       {
1000          CodeObject codeObject = row.GetData(null);
1001      
1002          if(codeObject.bitmap)
1003             RemoveResource(codeObject.bitmap);
1004          dropBox.DeleteRow(row);
1005       }
1006    }
1007
1008    void SelectObject(ObjectInfo object)
1009    {
1010       if(this)
1011       {
1012          DataRow row = dropBox.FindRow((int)object);
1013          this.object = object ? object.instance : null;
1014          propertyValue.userData = object ? (void *)object.instance : null;
1015          dropBox.SelectRow(row);
1016       }
1017    }
1018
1019    void RenameObject(ObjectInfo object, char * name)
1020    {
1021       DataRow row = dropBox.FindRow((int)object);
1022       CodeObject codeObject = row.GetData(null);
1023       // Isn't this useless? Shouldn't it be after?
1024       codeObject.name = name;
1025       // row.SetData(null, codeObject);  // Is this necessary?
1026    }
1027
1028    void DataBox::EditSetData(void * setValue, bool closingDropDown)
1029    {
1030       ((Sheet)master.master).SetData(setValue, this);
1031    }
1032
1033    void SetData(void * setValue, DataBox dataBox)
1034    {
1035       //PropertyInfo propertyPtr = row.GetData(null);
1036       PropertyInfo propertyPtr = properties.GetData(null);
1037       Property prop = propertyPtr ? propertyPtr.prop : null;
1038       Instance object = this.object;
1039       if(prop)
1040       {   
1041          Class dataType = prop.dataTypeClass;
1042          if(!dataType)
1043             dataType = prop.dataTypeClass = eSystem_FindClass(codeEditor.privateModule, prop.dataTypeString);
1044          if(propertyPtr.subMember)
1045          {
1046             DataMember member = propertyPtr.subMember;
1047             Class subDataType = member.dataTypeClass;
1048             if(!member.dataTypeClass)
1049                subDataType = member.dataTypeClass = eSystem_FindClass(codeEditor.privateModule, member.dataTypeString);
1050             if(subDataType)
1051             {
1052                void * data = null;
1053                if(!subDataType.dataType)
1054                   subDataType.dataType = ProcessTypeString(subDataType.dataTypeString, false);
1055
1056                if(dataType.type == structClass)
1057                {
1058                   data = new0 byte[dataType.structSize];
1059                   prop.Get(object, data);
1060                   // CopyBytes((byte *)data + member.offset + propertyPtr.extraOffset, &setValue, subDataType.size);
1061                   CopyBytes((byte *)data + member.offset + propertyPtr.extraOffset, &setValue, subDataType.dataType.size);
1062                   prop.Set(object, data);
1063                }
1064                else if(dataType.type == normalClass || dataType.type == noHeadClass)
1065                {
1066                }
1067                else
1068                {
1069                   if(dataType.type == bitClass)
1070                   {
1071                      BitMember bitMember = (BitMember) member;
1072                      if(subDataType)
1073                      {
1074                         DataValue value = { 0 };
1075                         value.ui = prop.Get(object);
1076                         value.ui &= ~ (uint)bitMember.mask;
1077                         value.ui |= (uint)setValue << bitMember.pos;
1078                         prop.Set(object, value.ui);
1079                      }
1080                   }
1081                   else
1082                   {
1083                      data = dataType.typeSize ? new0 byte[dataType.typeSize] : null;
1084                      prop.Get(object, data);
1085                      // CopyBytes((byte *)data + member.offset + propertyPtr.extraOffset, &setValue, subDataType.typeSize);
1086                      CopyBytes((byte *)data + member.offset + propertyPtr.extraOffset, &setValue, subDataType.dataType.size);
1087                      // TODO: Support non 32 bit datatypes here
1088                      prop.Set(object, data);
1089                   }
1090                }
1091
1092                if(data) dataType._vTbl[__ecereVMethodID_class_OnFree](dataType,&data);
1093                delete data;
1094             }
1095          }
1096          else if(propertyPtr.subProperty)
1097          {
1098             Property subProperty = propertyPtr.subProperty;
1099             Class subDataType = subProperty.dataTypeClass;
1100             if(!subDataType)
1101                subDataType = subProperty.dataTypeClass = eSystem_FindClass(codeEditor.privateModule, subProperty.dataTypeString);
1102             if(subDataType)
1103             {
1104                void * data = null;
1105
1106                if(dataType.type == structClass)
1107                {
1108                   data = new0 byte[dataType.structSize];
1109                   prop.Get(object, data);
1110                   subProperty.Set(data, (uint)setValue);
1111                   prop.Set(object, data);
1112                }
1113                else if(dataType.type == normalClass || dataType.type == noHeadClass)
1114                {
1115                   Instance current = (Instance)prop.Get(object);
1116                   Instance propObject = eInstance_New(dataType);
1117                   CopyInstanceData(dataType, propObject, current);
1118                   subProperty.Set(propObject, setValue);
1119                   prop.Set(object, propObject);
1120                }
1121                else
1122                {
1123                   data = dataType.typeSize ? new0 byte[dataType.typeSize] : null;
1124                   prop.Get(object, data);
1125                   subProperty.Set(data, setValue);
1126                   // TODO: Support not 32 bit data types here
1127                   prop.Set(object, data);
1128                }
1129
1130                if(data) dataType._vTbl[__ecereVMethodID_class_OnFree](dataType,&data);
1131                delete data;
1132             }
1133          }
1134          else
1135          {
1136             SetPropValue(prop, object, setValue);
1137          }      
1138          Code_FixProperty(propertyPtr.prop, object);
1139
1140          properties.Update(null);
1141          dropBox.Update(null);
1142          codeEditor.designer.Update(null);
1143          codeEditor.Update(null);   // patch for redraw bug if on top
1144
1145          ListProperties(false);
1146
1147          dataBox.editor.font = { propertyPtr.font.faceName, propertyPtr.font.size, propertyPtr.font.bold };
1148          codeEditor.ModifyCode();
1149       }
1150    }
1151
1152    bool SaveEdit(PropertyInfo propertyPtr, Instance object)
1153    {
1154       codeEditor.designer.Update(null);
1155       codeEditor.Update(null);   // patch for redraw bug if on top
1156       properties.Update(null);
1157       dropBox.Update(null);
1158
1159       Code_FixProperty(propertyPtr.prop, object);
1160       ListProperties(false);
1161
1162       codeEditor.ModifyCode();
1163       return true;
1164    }
1165
1166    void ToggleSheet()
1167    {
1168       if(!propBtn.checked)
1169       {
1170          propBtn.checked = true;
1171          propBtn.NotifyClicked(this, propBtn, 0,0,0);
1172       }
1173       else
1174       {
1175          methBtn.checked = true;
1176          methBtn.NotifyClicked(this, methBtn, 0,0,0);
1177       }
1178    }
1179
1180    bool AttachMethodSelected(MenuItem selection, Modifiers mods)
1181    {
1182       ClassFunction function = (ClassFunction)selection.id;
1183       codeEditor.AttachMethod(attachMethod, function);
1184       return true;
1185    }
1186
1187    bool ReattachMethodSelected(MenuItem selection, Modifiers mods)
1188    {
1189       ClassFunction function = (ClassFunction)selection.id;
1190       CodeObject object = methods.GetData(methodName);
1191       codeEditor.ReAttachMethod(attachMethod, function);
1192       return true;
1193    }
1194
1195    bool OverrideMethodSelected(MenuItem selection, Modifiers mods)
1196    {
1197       ClassFunction function = (ClassFunction)selection.id;
1198       CodeObject object = methods.GetData(methodName);
1199       if(object)
1200          codeEditor.AddMethod(object.method);   
1201       return true;
1202    }
1203
1204    bool GotoMethodSelected(MenuItem selection, Modifiers mods)
1205    {
1206       ClassFunction function = (ClassFunction)selection.id;
1207       CodeObject object = methods.GetData(methodName);
1208       if(object)
1209          codeEditor.GoToMethod(object.method.name);
1210       return true;
1211    }
1212
1213    bool DetachMethodSelected(MenuItem selection, Modifiers mods)
1214    {
1215       ClassFunction function = (ClassFunction)selection.id;
1216       CodeObject object = methods.GetData(methodName);
1217       if(object)
1218          codeEditor.DetachMethod(object.method, object.function, object.overriden);
1219       return true;
1220    }
1221
1222    bool DeleteMethodSelected(MenuItem selection, Modifiers mods)
1223    {
1224       ClassFunction function = (ClassFunction)selection.id;
1225       CodeObject object = methods.GetData(methodName);
1226       if(object)
1227          object.deleteBtn.NotifyClicked(this, object.deleteBtn, 0,0,0);
1228       return true;
1229    }
1230
1231    bool AddMethodClicked(Button button, int x, int y, Modifiers mods)
1232    {
1233       DataRow row = (DataRow)button.id;
1234       CodeObject object = row.GetData(methodName);
1235       codeEditor.AddMethod(object.method);   
1236       return true;
1237    }
1238
1239    void CreateButtons(CodeObject codeObject, int y, int h, DataRow row)
1240    {
1241       BitmapResource bitmap;
1242       
1243       if(codeObject.overriden)
1244       {
1245          if(codeObject.overriden == 1)
1246          {
1247             codeObject.deleteBtn = Button
1248             {
1249                methods, master = this,
1250                inactive = true,
1251                bitmap = { ":actions/delete.png", alphaBlend = true },
1252                anchor = { right = 16, top = y },
1253                size = { 16, h },
1254                id = (int)row;
1255
1256                bool NotifyClicked(Button button, int x, int y, Modifiers mods)
1257                {
1258                   CodeObject codeObject = ((DataRow)button.id).GetData(null);
1259                   bool confirmation = !Code_IsFunctionEmpty(codeObject.function, codeObject.method, codeEditor.selected);
1260
1261                   if(confirmation)
1262                   {
1263                      char title[1024];
1264                      sprintf(title, "Delete %s", codeObject.name);
1265                      if(MessageBox
1266                         {
1267                            master = parent, type = okCancel, text = title, 
1268                            contents = "Method still contains code. Are you sure you want to delete it?"
1269                         }.Modal() == ok)
1270                         confirmation = false;
1271                   }
1272
1273                   if(!confirmation && codeObject.function.attached.count)
1274                   {
1275                      char title[1024];
1276                      sprintf(title, "Delete %s", codeObject.name);
1277                      confirmation = true;
1278                      if(MessageBox
1279                         {
1280                            master = parent, type = okCancel, text = title,
1281                            contents = "Other methods are still attached to this method. Are you sure you want to delete it?"
1282                         }.Modal() == ok)
1283                         confirmation = false;
1284                   }
1285
1286                   if(!confirmation)
1287                   {
1288                      codeEditor.DeleteMethod(codeObject.function);
1289                   }
1290                   return false;
1291                }
1292             };
1293             incref codeObject.deleteBtn;
1294             codeObject.deleteBtn.Create();
1295          }
1296
1297          if(codeObject.overriden == 2 || !codeObject.function.attached.count)
1298          {
1299             codeObject.detachBtn = Button 
1300             {
1301                methods,
1302                master = methods.master,
1303                inactive = true,
1304                bitmap = { ":actions/detach.png" },
1305                anchor = { right = 0, top = y },
1306                size = { 16, h },
1307                id = (int)row;
1308
1309                bool NotifyClicked(Button button, int x, int y, Modifiers mods)
1310                {
1311                   DataRow row = (DataRow)button.id;
1312                   CodeObject object = row.GetData(methodName);
1313
1314                   codeEditor.DetachMethod(object.method, object.function, object.overriden);
1315                   return true;
1316                }
1317             };
1318             incref codeObject.detachBtn;
1319             codeObject.detachBtn.Create();
1320          }
1321       }
1322       else
1323       {
1324          if(codeObject.compatible.count)
1325          {
1326             codeObject.attachBtn = Button
1327             {
1328                parent = methods, master = methods.master,
1329                inactive = true,
1330                bitmap = { ":actions/attach.png" },
1331                anchor = { right = 0, top = y },
1332                size = { 16, h },
1333                id = (int)row;
1334
1335                bool NotifyPushed(Button button, int x, int y, Modifiers mods)
1336                {
1337                   // Create menu
1338                   DataRow row = (DataRow)button.id;
1339                   CodeObject object = row.GetData(methodName);
1340                   OldLink compatible;
1341                   PopupMenu popupMenu;
1342
1343                   Menu menu { };
1344                   
1345                   for(compatible = object.compatible.first; compatible; compatible = compatible.next)
1346                   {
1347                      ClassFunction function = compatible.data;
1348                      MenuItem { menu, function.declarator.symbol.string, id = (int)function, NotifySelect = AttachMethodSelected };
1349                   }
1350                   attachMethod = object.method;
1351
1352                   popupMenu = PopupMenu
1353                   { 
1354                      master = this, menu = menu, 
1355                      position =
1356                      {
1357                         button.absPosition.x - app.desktop.position.x,
1358                         button.absPosition.y - app.desktop.position.y + button.size.h
1359                      };
1360                   };
1361                   popupMenu.Create();
1362                   button.ReleaseCapture();
1363                   popupMenu.Capture();
1364                   return false;
1365                }
1366             };
1367             incref codeObject.attachBtn;
1368             codeObject.attachBtn.Create();
1369          }
1370       }
1371    }
1372    Instance object;
1373    Method attachMethod;
1374    char selectedMethod[1024];
1375    CodeEditor codeEditor;
1376    OldList categories;
1377    char selectedProp[1024];
1378    int selectedScroll;
1379 }
1380
1381 static int String_OnCompare(char ** string1, char ** string2)
1382 {
1383    int result = 0;
1384    if(*string1 && *string2)
1385       result = strcmpi(*string1, *string2);
1386    else if(!*string1 && *string2)
1387       result = 1;
1388    else if(*string1 && !*string2)
1389       result = -1;
1390    return result;
1391 }
1392
1393 static void CopyInstanceData(Class dataType, Instance propObject, Instance current)
1394 {
1395    Class _class;
1396    for(_class = dataType; _class && _class.type != systemClass; _class = _class.base)
1397    {
1398       DataMember member;
1399       for(member = _class.membersAndProperties.first; member; member = member.next)
1400       {               
1401          Class memberType = member.dataTypeClass;
1402          if(!memberType)
1403             memberType = member.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, member.dataTypeString);
1404          if(member.isProperty)
1405          {
1406             Property subProp = (Property) member;
1407             if(subProp.Get && subProp.Set)
1408                CopyProperty(subProp, propObject, current);
1409          }
1410          else
1411          {
1412             if(memberType)
1413                // TOCHECK: I have serious doubts this works in many cases.
1414                memberType._vTbl[__ecereVMethodID_class_OnCopy](memberType, (byte *)propObject + member.offset, (byte *)current + member.offset);
1415             else if(member.memberOffset)
1416                memcpy((byte *)propObject + member.offset, (byte *)current + member.offset, member.memberOffset);
1417          }
1418       }
1419    }
1420 }
1421
1422 class PropertyInfo : struct
1423 {
1424 public:
1425    Property prop;
1426    bool disabled;
1427    FontResource font;
1428    char * categoryName;
1429    DataMember subMember;
1430    Property subProperty;
1431    uint extraOffset;
1432
1433    void OnDisplay(Surface surface, int x, int y, int width, Instance object, Alignment alignment, DataDisplayFlags displayFlags)
1434    {
1435       Property prop = this.prop;
1436
1437       surface.TextFont(font.font);
1438       if(disabled)
1439       {  
1440          surface.SetBackground(Color { 170, 170, 170 });
1441          surface.Area(0,0, x+width-1, y+100);
1442       }
1443       else if(prop && prop.dataTypeString)
1444       {
1445          Class dataType = prop.dataTypeClass;
1446          Module module = ((Designer)GetActiveDesigner()).codeEditor.privateModule;
1447          if(!dataType)
1448             dataType = prop.dataTypeClass = eSystem_FindClass(module, prop.dataTypeString);
1449          
1450          if(dataType && prop.Get)
1451          {
1452             void * dataPtr, * data = null, * subData = null;
1453             DataValue valueData, valueSubData;
1454             uint64 bitValue;
1455             
1456             // Get main prop
1457             if(dataType.type == structClass)
1458             {
1459                data = new0 byte[dataType.structSize];
1460                prop.Get(object, data);
1461                dataPtr = data;
1462             }
1463             else
1464             {
1465                GetProperty(prop, object, &valueData);
1466                if(dataType.type == normalClass)
1467                   dataPtr = valueData.p;
1468                else
1469                   dataPtr = &valueData;
1470             }
1471             
1472             // Get sub prop
1473             if(this.subMember)
1474             {
1475                DataMember member = this.subMember;
1476                Class subDataType = member.dataTypeClass;
1477                if(!subDataType)
1478                   subDataType = member.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, member.dataTypeString);
1479                if(subDataType)
1480                {
1481                   if(dataType.type == bitClass)
1482                   {
1483                      BitMember bitMember = (BitMember)member;
1484                      bitValue = (valueData.i & bitMember.mask) >> bitMember.pos;
1485                      dataPtr = &bitValue;
1486                   }
1487                   else
1488                      dataPtr = (byte *)dataPtr + member.offset + this.extraOffset;
1489                }
1490                dataType = subDataType;
1491             }
1492             else if(this.subProperty)
1493             {
1494                Property subProperty = this.subProperty;
1495                Class subDataType = subProperty.dataTypeClass;
1496                if(!subDataType)
1497                   subDataType = subProperty.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, subProperty.dataTypeString);
1498                if(!subProperty.Get) subDataType = null;
1499                if(subDataType)
1500                {
1501                   if(subDataType.type == structClass)
1502                   {
1503                      subData = new0 byte[subDataType.structSize];
1504                      subProperty.Get(dataPtr, subData);
1505                      dataPtr = subData;
1506                   }
1507                   else
1508                   {
1509                      GetProperty(subProperty, dataPtr, &valueSubData);
1510                      if(subDataType.type == normalClass)
1511                         dataPtr = valueSubData.p;
1512                      else
1513                         dataPtr = &valueSubData;
1514                   }
1515                }
1516                dataType = subDataType;
1517             }
1518
1519             if(dataType)
1520                dataType._vTbl[__ecereVMethodID_class_OnDisplay](dataType, dataPtr, surface, x, y, width, null, alignment, displayFlags);
1521
1522             delete data;
1523             delete subData;
1524          }
1525       }
1526    }
1527
1528    Window OnEdit(DataBox dataBox, Window obsolete, int x, int y, int w, int h, void * unused)
1529    {
1530       EditBox editData = null;
1531       Property prop = this.prop;
1532       
1533       dataBox.SetData = Sheet::EditSetData;
1534       if(prop && prop.dataTypeString && !this.disabled)
1535       {
1536          Sheet propertyWindow = (Sheet)dataBox.master.master;
1537          Instance object = propertyWindow.object;
1538          Class dataType = prop.dataTypeClass;
1539          if(!dataType)
1540             dataType = prop.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, prop.dataTypeString);
1541
1542          if(dataType && prop.Get)
1543          {
1544             void * dataPtr, * data = null, * subData = null;
1545             DataValue valueData, valueSubData;
1546             uint64 bitValue;
1547             
1548             // Get main prop
1549             if(dataType.type == structClass)
1550             {
1551                data = new0 byte[dataType.structSize];
1552                prop.Get(object, data);
1553                dataPtr = data;
1554             }
1555             else
1556             {
1557                GetProperty(prop, object, &valueData);
1558                if(dataType.type == normalClass)
1559                   dataPtr = valueData.p;
1560                else
1561                   dataPtr = &valueData;
1562             }
1563             
1564             // Get sub prop
1565             if(this.subMember)
1566             {
1567                DataMember member = this.subMember;
1568                Class subDataType = member.dataTypeClass;
1569                if(!subDataType)
1570                   subDataType = member.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, member.dataTypeString);
1571                if(subDataType)
1572                {
1573                   if(dataType.type == bitClass)
1574                   {
1575                      BitMember bitMember = (BitMember)member;
1576                      bitValue = (valueData.i & bitMember.mask) >> bitMember.pos;
1577                      dataPtr = &bitValue;
1578                   }
1579                   else
1580                      dataPtr = (byte *)dataPtr + member.offset + this.extraOffset;
1581                }
1582                dataType = subDataType;
1583             }
1584             else if(this.subProperty)
1585             {
1586                Property subProperty = this.subProperty;
1587                Class subDataType = subProperty.dataTypeClass;
1588                if(!subDataType)
1589                   subDataType = subProperty.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, subProperty.dataTypeString);
1590                if(!subProperty.Get) subDataType = null;
1591                if(subDataType)
1592                {
1593                   if(subDataType.type == structClass)
1594                   {
1595                      subData = new0 byte[subDataType.structSize];
1596                      subProperty.Get(dataPtr, subData);
1597                      dataPtr = subData;
1598                   }
1599                   else
1600                   {
1601                      GetProperty(subProperty, dataPtr, &valueSubData);
1602                      if(subDataType.type == normalClass)
1603                         dataPtr = valueSubData.p;
1604                      else
1605                         dataPtr = &valueSubData;
1606                   }
1607                }
1608                dataType = subDataType;
1609             }
1610
1611             if(dataType)
1612                editData = (void *)dataType._vTbl[__ecereVMethodID_class_OnEdit](dataType, dataPtr, dataBox, obsolete,  x, y, w, h, object /*unused*/);
1613
1614             delete data;
1615             delete subData;
1616
1617             editData.font = { font.faceName, font.size, font.bold };
1618          }
1619       }
1620       return editData;
1621    }
1622
1623    int OnCompare(PropertyInfo data2)
1624    {
1625       char * category1 = prop ? prop.category : categoryName;
1626       char * category2 = data2.prop ? data2.prop.category : data2.categoryName;
1627       int result;
1628
1629       if(!category1) category1 = "Misc";
1630       if(!category2) category2 = "Misc";
1631       
1632       if(!prop)
1633       {
1634          // result = String::OnCompare((String)category1, (String)category2);
1635          result = String_OnCompare(&category1, &category2);
1636       }
1637       else
1638       //if(!result)
1639       {
1640          if(subMember && !data2.subMember)
1641          {
1642             result = 1;
1643          }
1644          else if(!subMember && data2.subMember)
1645          {
1646             result = 1;
1647          }
1648          else if(subMember && data2.subMember)
1649          {
1650             if(subMember.id < data2.subMember.id)
1651                result = -1;
1652             else if(subMember.id > data2.subMember.id)
1653                result = 1;
1654             else
1655                result = 0;
1656          }
1657          else if(subProperty && !data2.subProperty)
1658          {
1659             result = 1;
1660          }
1661          else if(!subProperty && data2.subProperty)
1662          {
1663             result = 1;
1664          }
1665          else if(subProperty && data2.subProperty)
1666          {
1667             if(subProperty.id < data2.subProperty.id)
1668                result = -1;
1669             else if(subProperty.id > data2.subProperty.id)
1670                result = 1;
1671             else
1672                result = 0;
1673          }
1674          else if(prop && !data2.prop)
1675             result = 1;
1676          else if(!prop && data2.prop)
1677             result = -1;
1678          else
1679             // result = ((String)prop.name).OnCompare(data2.prop.name);
1680             // result = String::OnCompare((String)prop.name, (String)data2.prop.name);
1681             result = String_OnCompare(&prop.name, &data2.prop.name);
1682       }
1683       return result;
1684    }
1685
1686    bool OnSaveEdit(Window editControl, void * unusedData)
1687    {
1688       Property prop = this.prop;
1689       if(prop)
1690       {
1691          Sheet sheet = (Sheet)editControl.master.master.master;
1692          Instance object = sheet.object;
1693          Class mainDataType = prop.dataTypeClass;
1694          Class dataType;
1695          bool result = false;
1696          void * dataPtr, * data = null, * subData = null;
1697          void * propObject = null;
1698          DataValue valueData = { 0 }, valueSubData = { 0 };
1699          uint bitValue;
1700
1701          if(!mainDataType)
1702             mainDataType = prop.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, prop.dataTypeString);
1703          dataType = mainDataType;
1704          
1705          // Prepare main prop
1706          if(dataType.type == structClass)
1707          {
1708             data = new0 byte[dataType.structSize];
1709             if(this.subMember || this.subProperty)
1710                prop.Get(object, data);
1711             dataPtr = data;
1712             propObject = data;
1713          }
1714          else if(dataType.type == normalClass || dataType.type == noHeadClass)
1715          {
1716             dataPtr = &valueData;
1717             
1718             if(this.subMember || this.subProperty)
1719             {
1720                Class _class;
1721                Instance current = (Instance)prop.Get(object);
1722                propObject = valueData.p = eInstance_New(dataType);
1723                CopyInstanceData(dataType, propObject, current);
1724             }
1725          }
1726          else
1727          {
1728             
1729             if(this.subMember || this.subProperty)
1730                GetProperty(prop, object, &valueData);
1731             
1732             dataPtr = &valueData;
1733             propObject = &valueData;
1734          }
1735          
1736          // Prepare sub prop
1737          if(this.subMember)
1738          {
1739             DataMember member = this.subMember;
1740             Class subDataType = member.dataTypeClass;
1741             if(!subDataType)
1742                subDataType = member.dataTypeClass = eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, member.dataTypeString);
1743             if(subDataType)
1744             {
1745                if(dataType.type == bitClass)
1746                   dataPtr = &bitValue;
1747                else
1748                   dataPtr = (byte *)dataPtr + member.offset + this.extraOffset;
1749             }
1750             dataType = subDataType;
1751          }
1752          else if(this.subProperty)
1753          {
1754             Property subProperty = this.subProperty;
1755             Class subDataType = subProperty.dataTypeClass;
1756             
1757             if(!subDataType)
1758                subDataType = subProperty.dataTypeClass = 
1759                   eSystem_FindClass(((Designer)GetActiveDesigner()).codeEditor.privateModule, subProperty.dataTypeString);
1760             if(!subProperty.Get) subDataType = null;
1761             if(subDataType)
1762             {
1763                if(subDataType.type == structClass)
1764                {
1765                   subData = new0 byte[subDataType.structSize];
1766                   dataPtr = subData;
1767                }
1768                else
1769                   dataPtr = &valueSubData;
1770             }
1771             dataType = subDataType;
1772          }
1773
1774          if(dataType)
1775          {
1776             if(dataType._vTbl[__ecereVMethodID_class_OnSaveEdit](dataType, dataPtr, editControl, null))
1777             {
1778                if(mainDataType.type == bitClass && this.subMember)
1779                {
1780                   BitMember bitMember = (BitMember)this.subMember;
1781                   valueData.ui &= ~ (uint)bitMember.mask;
1782                   valueData.ui |= bitValue << bitMember.pos;
1783                }
1784                if(this.subProperty)
1785                {
1786                   if(dataType.type == structClass)
1787                      this.subProperty.Set(propObject, subData);
1788                   else if(dataType.type == unitClass || dataType.type == enumClass || dataType.type == bitClass)
1789                   {
1790                      if(!strcmp(dataType.dataTypeString, "float"))
1791                         ((void(*)(void *,float))(void *)this.subProperty.Set)(propObject, valueSubData.f);
1792                      else if(!strcmp(dataType.dataTypeString, "double"))
1793                         ((void(*)(void *,double))(void *)this.subProperty.Set)(propObject, valueSubData.d);
1794                      else if(!strcmp(dataType.dataTypeString, "byte"))
1795                         ((void(*)(void *,byte))(void *)this.subProperty.Set)(propObject, valueSubData.uc);
1796                      else if(!strcmp(dataType.dataTypeString, "uint16"))
1797                         ((void(*)(void *,uint16))(void *)this.subProperty.Set)(propObject, valueSubData.us);
1798                      else 
1799                         this.subProperty.Set(propObject, valueSubData.ui);
1800                   }
1801                   else
1802                      this.subProperty.Set(propObject, valueSubData.ui);
1803                }
1804                if(mainDataType.type == structClass)
1805                   prop.Set(object, data);
1806                else if(mainDataType.type == unitClass || mainDataType.type == enumClass || mainDataType.type == bitClass)
1807                {
1808                   if(!strcmp(mainDataType.dataTypeString, "float"))
1809                      ((void(*)(void *,float))(void *)prop.Set)(object, valueData.f);
1810                   else if(!strcmp(mainDataType.dataTypeString, "double"))
1811                      ((void(*)(void *,double))(void *)prop.Set)(object, valueData.d);
1812                   else if(!strcmp(mainDataType.dataTypeString, "byte"))
1813                      ((void(*)(void *,byte))(void *)prop.Set)(object, valueData.uc);
1814                   else if(!strcmp(mainDataType.dataTypeString, "uint16"))
1815                      ((void(*)(void *,uint16))(void *)prop.Set)(object, valueData.us);
1816                   else
1817                      prop.Set(object, valueData.ui);
1818                }
1819                else
1820                   prop.Set(object, valueData.ui);
1821
1822                result = true;
1823             }
1824             if(data == dataPtr)     dataType._vTbl[__ecereVMethodID_class_OnFree](dataType, &data);
1825             if(subData == dataPtr)  dataType._vTbl[__ecereVMethodID_class_OnFree](dataType, &subData);
1826          }
1827          delete data;
1828          delete subData;
1829
1830          if(result)
1831             return sheet.SaveEdit(this, object);
1832       }
1833       return false;
1834    }
1835 };
1836
1837 class Category : struct
1838 {
1839    Category prev, next;
1840    char * name;
1841    DataRow row;
1842    bool collapsed;
1843 };