eda:gui:FieldDataBox; remove bad check for control visible on Init/Load/Save.
[sdk] / eda / libeda / src / gui.ec
index 67b36ab..e4486e6 100644 (file)
@@ -5,6 +5,7 @@ define shadowS = 4;
 
 default:
 extern int __ecereVMethodID_class_OnFree;
+extern int __ecereVMethodID_class_OnGetString;
 private:
 
 char * defaultNameField = "Name";
@@ -26,7 +27,8 @@ public class Group : Window
    tabCycle = true;
    //inactive = true; // TOFIX causes problems...
 
-   public Label title { this, font = { $"Arial", 10, bold = true }, position = { 16, 2 } };
+public:
+   Label title { this, font = { $"Arial", 10, bold = true }, position = { 16, 2 } };
 
    bool OnCreate()
    {
@@ -55,7 +57,6 @@ public class CheckBool : bool
 {
    Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
    {
-      if(this || !this) {     // FIXME
       Button button = dataBox.keepEditor ? (Button)obsolete : null;
       if(!button)
       {
@@ -63,12 +64,15 @@ public class CheckBool : bool
          {
             dataBox, borderStyle = 0, text = dataBox.text, anchor = { 0, 0, 0, 0 },
             // size = { 100, 22 };
-            modifyVirtualArea = false, isCheckbox = true;            
+            modifyVirtualArea = false, isCheckbox = true;
 
             bool DataBox::NotifyClicked(Button control, int x, int y, Modifiers mods)
             {
                bool checked = control.checked;
-               SetData(&checked, false);
+               if(readOnly)
+                  control.checked = !checked;
+               else
+                  SetData(&checked, false);
                return true;
             }
          };
@@ -76,9 +80,31 @@ public class CheckBool : bool
       button.checked = this;
       button.Create();
       return button;
+   }
+}
+
+String GetNameString(Row r, Field nameField)
+{
+   String s = null;
+   if(nameField.type != class(String) && nameField.type != class(char *))
+   {
+      char tempString[4096];
+      Class type = nameField.type;
+      int64 data = 0;
+      if(type.type == structClass)
+         data = (int64)new0 byte[type.structSize];
+      ((bool (*)())(void *)r.GetData)(r, nameField, type, (type.type == structClass) ? (void *)data : &data);
+      s = CopyString(((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, (void *)data, tempString, null, null));
+      ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, (void *)data);
+      if(type.type == structClass)
+      {
+         void * _data = (void *)data;
+         delete _data;
       }
-      return null;
    }
+   else
+      r.GetData(nameField, s);
+   return s;
 }
 
 public class TableDropBox : DropBox
@@ -86,14 +112,15 @@ public class TableDropBox : DropBox
    anchor = { left = 130, top = 180, right = shadowS + sgs * 2 };
    borderStyle = deep;
 
-   public uint filter;
-   public bool filtered;
-   public Field nameField;
-   public uint exclusion;
-   public Table table;
-   public Field filterField;
+public:
+   uint filter;
+   bool filtered;
+   Field nameField;
+   uint exclusion;
+   Table table;
+   Field filterField;
 
-   public property uint filter
+   property uint filter
    {
       set
       {
@@ -102,11 +129,11 @@ public class TableDropBox : DropBox
       }
       get { return filter; }
    }
-   public property Field nameField { set { nameField = value; } }
-   public property uint exclusion { set { exclusion = value; } }
-   public property Table table { set { table = value; if(!nameField && value) nameField = value.FindField(defaultNameField); } }
+   property Field nameField { set { nameField = value; } }
+   property uint exclusion { set { exclusion = value; } }
+   property Table table { set { table = value; if(!nameField && value) nameField = value.FindField(defaultNameField); } }
 
-   public virtual void Refill()
+   virtual void Refill()
    {
       Clear();
       if(table)
@@ -132,7 +159,7 @@ public class TableDropBox : DropBox
                      tbl.GenerateIndex(1, indexedFields, false);
 
                      r = Row { tbl };
-                     
+
                      for(r.Find(filterField, middle, nil, filter); !r.nil; r.Next()) //while(r.Next())
                      {
                         Id id;
@@ -143,8 +170,7 @@ public class TableDropBox : DropBox
                            break;
                         if(!exclusion || id != exclusion)
                         {
-                           String s;
-                           r.GetData(nameField, s);
+                           String s = GetNameString(r, nameField);
                            AddString(s).tag = id;
                            delete s;
                         }
@@ -161,8 +187,7 @@ public class TableDropBox : DropBox
                      r.GetData(fldId, id);
                      if(idList && idList.Includes(filter) && (!exclusion || !idList.Includes(exclusion)))
                      {
-                        String s;
-                        r.GetData(nameField, s);
+                        String s = GetNameString(r, nameField);
                         AddString(s).tag = id;
                         delete s;
                      }
@@ -178,8 +203,7 @@ public class TableDropBox : DropBox
                   r.GetData(fldId, id);
                   if(id != exclusion)
                   {
-                     String s;
-                     r.GetData(nameField, s);
+                     String s = GetNameString(r, nameField);
                      AddString(s).tag = id;
                      delete s;
                   }
@@ -190,9 +214,8 @@ public class TableDropBox : DropBox
                while(r.Next())
                {
                   Id id;
-                  String s;
+                  String s = GetNameString(r, nameField);
                   r.GetData(fldId, id);
-                  r.GetData(nameField, s);
                   AddString(s).tag = id;
                   delete s;
                }
@@ -203,7 +226,7 @@ public class TableDropBox : DropBox
       Sort(null, 1);
    }
 
-   public property Field filterField { set { filterField = value; } }
+   property Field filterField { set { filterField = value; } }
 
    bool OnKeyHit(Key key, unichar ch)
    {
@@ -214,7 +237,7 @@ public class TableDropBox : DropBox
       }
       else if((SmartKey)key == enter)
          parent.CycleChildren(true, false, false, true);
-      
+
       return DropBox::OnKeyHit(key, ch);
    }
 
@@ -230,13 +253,13 @@ public class TableDropBox : DropBox
       return true;
    }
 
-   public void EditNotifyCharsAdded()
+   void EditNotifyCharsAdded()
    {
       if(!editBox.NotifyUpdate || editBox.NotifyUpdate == EditBox::NotifyUpdate)
          editBox.NotifyUpdate = EditNotifyUpdate;
    }
 
-   public void EditNotifyUpdate(EditBox editBox)
+   void EditNotifyUpdate(EditBox editBox)
    {
       DataRow row;
       char * contents = editBox.contents;
@@ -271,24 +294,19 @@ public class DropDataBox : DataBox
    size.h = 24;
    keepEditor = true;
    borderStyle = deep;
-
-   Field nameField;
-   Field filterField;
-   uint exclusion;
-   uint filter;
-   bool filtered;
-   bool showNone;
    showNone = false; //true;
 
-   public property uint filter { set { filtered = true; filter = value; } get { return filter; } }
-   public property bool filtered { set { filtered = value; } }
-   public property uint exclusion { set { exclusion = value; } }
-   public property Field filterField { set { filterField = value; } }
-   public property Field nameField { set { nameField = value; } }
-   public virtual void TableDropBox::RefillFunction();
-   public property bool showNone { set { showNone = value; } }
+public:
+
+   property uint filter { set { filtered = true; filter = value; } get { return filter; } }
+   property bool filtered { set { filtered = value; } }
+   property uint exclusion { set { exclusion = value; } }
+   property Field filterField { set { filterField = value; } }
+   property Field nameField { set { nameField = value; } }
+   virtual void TableDropBox::RefillFunction();
+   property bool showNone { set { showNone = value; } }
 
-   public void Refill()
+   void Refill()
    {
       if(editor)
       {
@@ -315,10 +333,18 @@ public class DropDataBox : DataBox
          dropBox.filter = filter;
       else
          dropBox.filtered = false;
-      
+
       dropBox.exclusion = exclusion;
       dropBox.showNone = showNone;
    }
+
+private:
+   Field nameField;
+   Field filterField;
+   uint exclusion;
+   uint filter;
+   bool filtered;
+   bool showNone;
 }
 
 public class EditDropDataBox : DropDataBox
@@ -337,16 +363,13 @@ public class FieldDataBox : DataBox
    anchor = { left = 110, right = shadowS + sgs * 2 };
    borderStyle = deep;
 
-   Field field;
-   int64 dataHolder; // THERE SEEMS TO BE A BUG WHEN ACCESSING row ACROSS .so
-   Row row;
-
-   public property Row row
+public:
+   property Row row
    {
       set { row = value; }
       get { return row; }
    }
-   public property EditSection editor
+   property EditSection editor
    {
       set
       {
@@ -357,7 +380,7 @@ public class FieldDataBox : DataBox
       }
    };
 
-   public property Field field
+   property Field field
    {
       set
       {
@@ -366,7 +389,7 @@ public class FieldDataBox : DataBox
 
          if(dataHolder)
          {
-            type._vTbl[__ecereVMethodID_class_OnFree](type, dataHolder);
+            ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, (void *)dataHolder);
             if(type.type == structClass)
             {
                void * dataPtr = (void *)dataHolder;
@@ -402,30 +425,27 @@ public class FieldDataBox : DataBox
       }
    }
 
-   public void Clear()
+   void Clear()
    {
-      if(visible)
-      {
-         if(data)
-            SetData(null, false);
-
-         if(type && (type.type == noHeadClass || type.type == normalClass))
-         {
-            if(eClass_IsDerived(type, class(String)))
-               dataHolder = (int64)CopyString("");
-            else
-               dataHolder = (int64)eInstance_New(type);
-            data = (void *)&dataHolder;
-         }
+      if(data)
+         SetData(null, false);
 
-         if(created)
-            Refresh();
+      if(type && (type.type == noHeadClass || type.type == normalClass))
+      {
+         if(eClass_IsDerived(type, class(String)))
+            dataHolder = (int64)CopyString("");
+         else
+            dataHolder = (int64)eInstance_New(type);
+         data = (void *)&dataHolder;
       }
+
+      if(created)
+         Refresh();
    }
 
-   public void Load()
+   void Load()
    {
-      if(visible && field && row)
+      if(field && row)
       {
          SetData(null, false);
          master.modifiedDocument = false;
@@ -446,23 +466,23 @@ public class FieldDataBox : DataBox
       }
    }
 
-   public virtual void Save()
+   virtual void Save()
    {
       bool result;
-      if(visible && field && row)
+      if(field && row)
       {
          Class type = field.type;
          if(!DataBox::SaveData())
             Refresh();
 
-         ((bool (*)())(void *)Row::SetData)(row, field, type, 
+         ((bool (*)())(void *)Row::SetData)(row, field, type,
             (type.type == noHeadClass || type.type == normalClass) ? *(void **)data : data);
       }
    }
 
-   public void Init()
+   void Init()
    {
-      if(visible && created)
+      if(created)
          Refresh();
    }
 
@@ -491,17 +511,17 @@ public class FieldDataBox : DataBox
    {
       if((SmartKey)key == enter)
          parent.CycleChildren(true, false, false, true);
-      
+
       return DataBox::OnKeyHit(key, ch);
    }
 
-   public bool Window::NotifyChanged(bool closingDropDown)
+   bool Window::NotifyChanged(DataBox dataBox, bool closingDropDown)
    {
       modifiedDocument = true;
       return true;
    }
 
-   public bool Window::NotifyModified()
+   bool Window::NotifyModified()
    {
       modifiedDocument = true;
       return true;
@@ -514,7 +534,7 @@ public class FieldDataBox : DataBox
 
       if(dataHolder)
       {
-         type._vTbl[__ecereVMethodID_class_OnFree](type, dataHolder);
+         ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, (void *)dataHolder);
          if(type.type == structClass)
          {
             void * dataPtr = (void *)dataHolder;
@@ -523,6 +543,11 @@ public class FieldDataBox : DataBox
          dataHolder = 0;
       }
    }
+
+private:
+   Field field;
+   int64 dataHolder; // THERE SEEMS(ED?) TO BE A BUG WHEN ACCESSING row ACROSS .so
+   Row row;
 }
 
 public class FieldCheckButton : FieldDataBox
@@ -537,24 +562,18 @@ public class FieldDropDataBox : FieldDataBox
 {
    size.h = 24;
    keepEditor = true;
-
-   Field nameField;
-   Field filterField;
-   uint exclusion;
-   uint filter;
-   bool filtered;
-   bool showNone;
    showNone = true;
 
-   public property uint filter { set { filtered = true; filter = value; } get { return filter; } }
-   public property bool filtered { set { filtered = value; } }
-   public property uint exclusion { set { exclusion = value; } }
-   public property Field filterField { set { filterField = value; } }
-   public property Field nameField { set { nameField = value; } }
-   public virtual void TableDropBox::RefillFunction();
-   public property bool showNone { set { showNone = value; } }
-
-   public void Refill()
+public:
+   property uint filter { set { filtered = true; filter = value; } get { return filter; } }
+   property bool filtered { set { filtered = value; } }
+   property uint exclusion { set { exclusion = value; } }
+   property Field filterField { set { filterField = value; } }
+   property Field nameField { set { nameField = value; } }
+   virtual void TableDropBox::RefillFunction();
+   property bool showNone { set { showNone = value; } }
+
+   void Refill()
    {
       if(editor)
       {
@@ -578,12 +597,20 @@ public class FieldDropDataBox : FieldDataBox
       dropBox.exclusion = exclusion;
       dropBox.showNone = showNone;
    }
+
+private:
+   Field nameField;
+   Field filterField;
+   uint exclusion;
+   uint filter;
+   bool filtered;
+   bool showNone;
 }
 
 public class EditFieldDropDataBox : FieldDropDataBox
 {
    // showNone = false;
-
+public:
    void OnConfigure(TableDropBox dropBox)
    {
       FieldDropDataBox::OnConfigure(dropBox);
@@ -594,7 +621,7 @@ public class EditFieldDropDataBox : FieldDropDataBox
    void Save()
    {
       TableDropBox dropBox = (TableDropBox) editor;
-      
+
       if(!dropBox.currentRow && dropBox.contents[0])
       {
          Row row { dropBox.table };
@@ -609,7 +636,7 @@ public class EditFieldDropDataBox : FieldDropDataBox
       FieldDataBox::Save();
    }
 
-   public virtual bool OnAddTextEntry(Row row, TableDropBox dropBox, char * entry)
+   virtual bool OnAddTextEntry(Row row, TableDropBox dropBox, char * entry)
    {
       row.Add();
       row.SetData(dropBox.nameField, entry);
@@ -622,10 +649,8 @@ public class ListSection : Group
    text = $"List";
    size = { 710, 287 };
    anchor = { left = sgs, top = 32 + sgs * 3, bottom = 55 + sgs * 3 };
-
-   EditSection editor;
-
-   public property EditSection editor
+public:
+   property EditSection editor
    {
       set
       {
@@ -634,7 +659,7 @@ public class ListSection : Group
       }
    }
 
-   public property Table table
+   property Table table
    {
       set
       {
@@ -644,29 +669,28 @@ public class ListSection : Group
             if(table)
             {
                FieldIndex indexedFields[1];
-               
+
                if(!fldId) fldId = table.FindField(defaultIdField);
                if(!fldName) fldName = table.FindField(defaultNameField);
                if(!fldActive) fldActive = table.FindField(defaultActiveField);
-               
+
                indexedFields[0] = { fldId };
                table.Index(1, indexedFields);
-               
+
                editor.editRow.tbl = table;
-               
+
                RefillList();
             }
          }
       }
    }
-   Table table;
-   public Field fldId, fldName, fldActive;
+   Field fldId, fldName, fldActive;
 
-   public virtual DialogResult Window::NotifySaveConfirmation(ListSection listSection)
+   virtual DialogResult Window::NotifySaveConfirmation(ListSection listSection)
    {
       return MessageBox { master = this, type = yesNoCancel, text = $"List Editor", contents = $"You have modified this entry. Would you like to save it before proceeding?" }.Modal();
    }
-   
+
    bool OnClose(bool parentClosing)
    {
       if(editor && editor.modifiedDocument)
@@ -685,7 +709,7 @@ public class ListSection : Group
       return true;
    }
 
-   public void RefillList()
+   void RefillList()
    {
       list.Clear();
       //if(fldId && fldName)
@@ -698,7 +722,7 @@ public class ListSection : Group
       editor.modifiedDocument = false;
    }
 
-   public virtual void Window::NotifyRefillList(ListSection listSection, Row r)
+   virtual void Window::NotifyRefillList(ListSection listSection, Row r)
    {
       if(listSection.fldId && listSection.fldName)
       {
@@ -718,9 +742,9 @@ public class ListSection : Group
       }
    }
 
-   public virtual bool Window::NotifyNew(ListSection listSection, Row r);
+   virtual bool Window::NotifyNew(ListSection listSection, Row r);
 
-   public ButtonStyle btnNew
+   ButtonStyle btnNew
    {
       this, anchor = { right = shadowS + sgs * 2, top = 24 }, hotKey = altW, text = $"New";
 
@@ -729,9 +753,9 @@ public class ListSection : Group
          list.NotifySelect(this, list, null, 0);
          if(!editor.modifiedDocument)
          {
-            uint id; // = table.rowsCount + 1; // this is bad with deleted rows, won't work, how to have unique id? 
+            uint id; // = table.rowsCount + 1; // this is bad with deleted rows, won't work, how to have unique id?
             Row r { table };
-         
+
             if(r.Last())   // this will reuse ids in cases where the item(s) with the last id have been deleted
             {
                r.GetData(fldId, id);
@@ -739,7 +763,7 @@ public class ListSection : Group
             }
             else
                id = 1;
-         
+
             editor.EditClear();
             {
                bool active = true;
@@ -759,7 +783,7 @@ public class ListSection : Group
                   r.SetData(fldActive, active);
 
                if(NotifyNew(master, this, r))
-                  list.currentRow = list.AddString("$[New]");
+                  list.currentRow = list.AddString($"[New]");
                delete r;
             }
 
@@ -772,18 +796,18 @@ public class ListSection : Group
       }
    };
 
-   public virtual bool Window::NotifyDeleteConfirmation(ListSection listSection)
+   virtual bool Window::NotifyDeleteConfirmation(ListSection listSection)
    {
-      return MessageBox {  master = this, type = yesNo, text = $"List Editor", 
+      return MessageBox {  master = this, type = yesNo, text = $"List Editor",
                            contents =  $"You are about to delete an entry.\n"
                                         "Do you wish to continue?"
                   }.Modal() == yes;
    }
 
-   public virtual void Window::NotifyDeleting(ListSection listSection);
-   public virtual void Window::NotifyDeleted(ListSection listSection);
+   virtual void Window::NotifyDeleting(ListSection listSection);
+   virtual void Window::NotifyDeleted(ListSection listSection);
 
-   public ButtonStyle btnDelete
+   ButtonStyle btnDelete
    {
       this, anchor = { right = shadowS + sgs * 2, top = 24 }, hotKey = altD, text = $"Delete";
 
@@ -809,7 +833,7 @@ public class ListSection : Group
       }
    };
 
-   public bool FilterNotifyChanged(bool closeDropDown)
+   bool FilterNotifyChanged(DataBox dataBox, bool closeDropDown)
    {
       editor.EditClear();
       RefillList();
@@ -819,12 +843,11 @@ public class ListSection : Group
       return true;
    }
 
-   DataRow lastRow;
-   public ListBox list
+   ListBox list
    {
       this, anchor = { left = sgs * 2, top = 22 + 22 + sgs * 4, right = shadowS + sgs * 2, bottom = shadowS + sgs * 2 };
       alwaysHighLight = true;
-      
+
       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
       {
          bool result = true;
@@ -854,14 +877,14 @@ public class ListSection : Group
       }
    };
 
-   public virtual void Window::NotifySelectListRow(ListSection listSection, uint id);
-   
-   public void SelectListRow(DataRow row)
+   virtual void Window::NotifySelectListRow(ListSection listSection, uint64 id);
+
+   void SelectListRow(DataRow row)
    {
       // Time startTime = GetTime();
       if(row)
       {
-         uint id = row.tag;
+         uint64 id = row.tag;
          lastRow = row;
 
          if(list.currentRow != row)
@@ -876,14 +899,14 @@ public class ListSection : Group
       // Logf("SelectListRow took %f seconds\n", GetTime() - startTime);
    }
 
-   public void SelectFirst()
+   void SelectFirst()
    {
       if(list.firstRow)
          SelectListRow(list.firstRow);
       RefreshState();
    }
 
-   public void RefreshState()
+   void RefreshState()
    {
       if(editor)
       {
@@ -893,7 +916,7 @@ public class ListSection : Group
          editor.disabled = !(bool)list.firstRow;
       }
    }
-   
+
    void OnResize(int width, int height)
    {
       int x = width - btnDelete.size.w - 20;
@@ -911,6 +934,10 @@ public class ListSection : Group
       return Window::OnPostCreate();
    }
 
+private:
+   EditSection editor;
+   Table table;
+   DataRow lastRow;
 }
 
 public class EditSection : Group
@@ -945,26 +972,26 @@ public:
    DataRow listRow;
    OldList editBoxes { };
 
-   public Window editArea { this, borderStyle = deep, tabCycle = true, anchor = { left = 8, top = 54, right = 10, bottom = 10 }, hasVertScroll = true, dontHideScroll = true };
-   
-   public ButtonStyle btnSave
+   Window editArea { this, borderStyle = deep, tabCycle = true, anchor = { left = 8, top = 54, right = 10, bottom = 10 }, hasVertScroll = true, dontHideScroll = true };
+
+   ButtonStyle btnSave
    {
       this, anchor = { right = shadowS + sgs * 2, top = 24 }, hotKey = altV, text = $"Save";
 
       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
       {
-         EditSave(editRow);
+         EditSave();
          return true;
       }
    };
 
-   public ButtonStyle btnReload
+   ButtonStyle btnReload
    {
       this, anchor = { left = 10, top = 24 }, hotKey = altV, text = $"Revert";
 
       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
       {
-         EditLoad(editRow);
+         EditLoad();
          return true;
       }
    };
@@ -976,7 +1003,7 @@ public:
    }
 
    virtual void Window::NotifyInitFields(EditSection editSection);
-   
+
    void InitFields()
    {
       OldLink link;
@@ -990,7 +1017,6 @@ public:
 
    void EditNew()
    {
-
       modifiedDocument = false;
    }
 
@@ -998,7 +1024,7 @@ public:
    {
       edit.listRow.string = name;
    }
-   
+
    void EditSave()
    {
       bool stringName = !strcmp(list.fldName.type.dataTypeString, "char *");