ide, epj2make; added a way to specify compiler flags for each compiler configuration...
[sdk] / ide / src / ProjectSettings.ec
1 import "ide"
2 import "WorkspaceSettings"
3 import "ProjectTabSettings"
4 import "StringsBox"
5 // import "SelectorBar"
6
7 static ProjectConfig config;
8 static Platform platform;
9 static ProjectNode currentNode;
10 static Project project;
11
12 static String MakeString(char * s, int len)
13 {
14    String string = new char[len+1];
15    memcpy(string, s, len);
16    string[len] = 0;
17    return string;
18 }
19
20 class StringListBox : EditBox
21 {
22    textHorzScroll = true;
23
24    property Array<String> strings
25    {
26       set
27       {
28          contents = "";
29          if(value)
30          {
31             bool first = true;
32             for(item : value)
33             {
34                if(!first)
35                   AddS(" ");
36                AddS(item);
37                first = false;
38             }
39          }
40       }
41       get
42       {
43          Array<String> array { };
44          int c, start = 0;
45          char * contents = property::contents;
46          char ch;
47          bool quoted = false;
48
49          for(c = 0; (ch = contents[c]); c++)
50          {
51             if(ch == ' ' && !quoted)
52             {
53                if(c - start)
54                   array.Add(MakeString(contents + start, c - start));
55                start = c + 1;
56             }
57             else if(ch == '\"')
58             {
59                if(quoted)
60                   quoted = false;
61                else
62                   quoted = true;
63             }
64          }
65          if(c - start)
66             array.Add(MakeString(contents + start, c - start));
67          return array;
68       }
69    }
70 }
71
72 define dialogTitle = $"Project Settings";
73 static Color unfocusedSelectorColor { 70, 96, 166 };
74 class ProjectSettings : Window
75 {
76    text = dialogTitle;
77    background = formColor;
78    borderStyle = sizable;
79    minClientSize = { 650, 520 };
80    hasClose = true;
81    tabCycle = true;
82    size = { 650, 520 };
83
84    property Project project
85    {
86       set
87       {
88          ::project = value;
89          projectTab.project = value;
90          buildTab.Init();
91
92          buildTab.SelectNode(project.topNode, false);
93
94          if(project && project.topNode && project.topNode.name && project.topNode.name[0])
95             UpdateDialogTitle();
96       }
97       get { return ::project; }
98    };
99
100    property ProjectNode projectNode
101    {
102       set { buildTab.SelectNode(value, false); }
103       get { return currentNode; }
104    }
105
106    void UpdateDialogTitle()
107    {
108       //char * s = PrintString("Project Settings - ", project.topNode.fileName);
109       //text = s;
110       char * projectName = new char[strlen(project.topNode.name) + 1];
111       char * nodeName = currentNode && currentNode != project.topNode ? currentNode.name : "";
112       char * config = buildTab.selectedConfigName;
113       char * platform = buildTab.selectedPlatformName;
114       char * label = new char[strlen(dialogTitle) + 3 + strlen(project.topNode.name) + 3 + 
115                               strlen(nodeName) + 2 + strlen(config) + 1 + strlen(platform) + 1 + 1];
116       strcpy(label, dialogTitle);
117       strcat(label, " - ");
118       strcpy(projectName, project.topNode.name);
119       StripExtension(projectName);
120       strcat(label, projectName);
121       if(currentNode && currentNode.type != project)
122       {
123          strcat(label, " - ");
124          strcat(label, nodeName);
125       }
126       if(strlen(config) || strlen(platform))
127       {
128          strcat(label, " (");
129          if(strlen(config))
130             strcat(label, config);
131          if(strlen(config) && strlen(platform))
132             strcat(label, "/");
133          if(strlen(platform))
134             strcat(label, platform);
135          strcat(label, ")");
136       }
137       text = label;
138       delete projectName;
139       delete label;
140    }
141
142    ~ProjectSettings()
143    {
144       currentNode = null;
145    }
146
147    TabControl prjTabControl
148    {
149       this, background = formColor, anchor = { left = 8, top = 4, right = 8, bottom = 38 };
150    };
151    ProjectTab projectTab { this, tabControl = prjTabControl };
152    BuildTab buildTab { this, tabControl = prjTabControl };
153    WorkspaceTab workspaceTab { this, tabControl = prjTabControl };
154
155    Button cancel
156    {
157       this, size = { 80, 22 };
158       anchor = { right = 8, bottom = 8 };
159       text = $"Cancel", hotKey = escape, id = DialogResult::cancel;
160
161       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
162       {
163          if(prjTabControl.curTab.modifiedDocument)
164          {
165             DialogResult diagRes = MessageBox
166             {
167                type = okCancel, master = ide,
168                text = $"Lose Changes?",
169                contents = $"Are you sure you wish to discard changes made to the build options?"
170             }.Modal();
171             if(diagRes == ok)
172             {
173                if(prjTabControl.curTab == buildTab)
174                {
175                   buildTab.RevertChanges();
176                   buildTab.modifiedDocument = false;
177                }
178                if(prjTabControl.curTab == workspaceTab)
179                {
180                   workspaceTab.modifiedDocument = false;
181                }
182                if(prjTabControl.curTab == projectTab)
183                {
184                   projectTab.modifiedDocument = false;
185                }
186                Destroy(DialogResult::cancel);
187             }
188          }
189          else
190             Destroy(DialogResult::cancel);
191          return true;
192       }
193    };
194    Button ok
195    {
196       this, size = { 80, 22 };
197       anchor = { right = 96, bottom = 8 };
198       text = $"OK", isDefault = true;
199
200       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
201       {
202          if(prjTabControl.curTab == buildTab && buildTab.modifiedDocument)
203          {
204             buildTab.modifiedDocument = false;
205
206             project.topNode.modified = true;
207             project.MarkChanges(buildTab.backupNode);
208             ide.projectView.modifiedDocument = true;
209             ide.projectView.Update(null);
210          }
211          if(prjTabControl.curTab == workspaceTab && workspaceTab.modifiedDocument)
212          {
213             workspaceTab.SaveChanges();
214             workspaceTab.modifiedDocument = false;
215          }
216          if(prjTabControl.curTab == projectTab && projectTab.modifiedDocument)
217          {
218             projectTab.SaveChanges();
219             projectTab.modifiedDocument = false;
220          }
221          Destroy(DialogResult::ok);
222          return true;
223       }
224    };
225
226    bool OnPostCreate()
227    {
228       UpdateDialogTitle();
229       prjTabControl.curTab = buildTab;
230       return true;
231    }
232 }
233
234 #define OPTION(x) ((uint)(&((ProjectOptions)0).x))
235
236 // TOFIX: USING T INSTEAD OF Z HERE CAUSED US SOME CONFLICTS WITH T IN Array TEMPLATES
237
238 // the BlendFileConfigPlatformProjectOptions function and the GenericOptionTools class
239 // contain code that is closely matched to the following code
240 // output changing modification should be mirrored in both implementations
241 class OptionBox<class Z> : CommonControl
242 {
243    bool mergeValues, configReplaces;
244    void * chainKeyDown;
245
246    autoCreate = false;
247
248    ~OptionBox()
249    {
250       delete editor;
251    }
252    property Window editor
253    {
254       set
255       {
256          editor = value;
257          incref editor;
258          editor.OnRightButtonDown = OptionBox_OnRightButtonDown;
259          chainKeyDown = (void *)editor.OnKeyDown;
260          editor.OnKeyDown = OptionBox_OnKeyDown;
261       }
262    }
263
264    property bool visible { set { editor.visible = value; } get { return editor.visible; } }
265    property Window parent { set { editor.parent = value; Window::parent = value; master = value; editor.id = (int64)this; } }
266    property Point position { set { editor.position = value; } }
267    property Size size { set { editor.size = value; } }
268    property Anchor anchor { set { editor.anchor = value; } }
269    property Key hotKey { set { editor.hotKey = value; } }
270    property char * text { set { editor.text = value; Window::text = value; } }
271
272    uint option;
273
274    Window editor;
275    Menu clearMenu { };
276    MenuItem clearItem
277    {
278       clearMenu, $"Clear";
279
280       bool NotifySelect(MenuItem selection, Modifiers mods)
281       {
282          OptionBox ob = (OptionBox)id;
283          if(eClass_IsDerived(ob._class, class(CheckBoxForEnumOptionBox)))
284          {
285             Window slave;
286             for(slave = ob.master.firstSlave; slave; slave = slave.nextSlave)
287             {
288                if(eClass_IsDerived(slave._class, class(CheckBoxForEnumOptionBox)) &&
289                   ((OptionBox)slave).option == ob.option)
290                      ((OptionBox)slave).Unset();
291             }
292          }
293          else
294             ob.Unset();
295          return true;
296       }
297    };
298    
299    bool Window::OptionBox_OnRightButtonDown(int x, int y, Modifiers mods)
300    {
301       OptionBox ob = (OptionBox)id;
302       GuiApplication app = ((GuiApplication)__thisModule.application);
303       Activate();
304       PopupMenu { null, this, menu = ob.clearMenu,
305          position = { absPosition.x + clientStart.x + x - app.desktop.position.x, absPosition.y + clientStart.y + y - app.desktop.position.y } }.Create();
306       return true;
307    }
308
309    bool Window::OptionBox_OnKeyDown(Key key, unichar ch)
310    {
311       OptionBox ob = (OptionBox)id;
312       if(key == Key { del, ctrl = true } || key == Key { keyPadDelete, ctrl = true })
313       {
314          ob.Unset();
315          return false;
316       }
317       return (((bool(*)(Window, Key, unichar)) ob.chainKeyDown)(this, key, ch);
318    }
319
320    // code: 0 = not set anywhere, 1 = overridden here, 2 = inherited
321    void SetAttribs(int code)
322    {
323       Window c;
324       Label label = null;
325
326       for(c = Window::parent.firstChild; c; c = c.next)
327       {
328          if(eClass_IsDerived(c._class, class(Label)))
329          {
330             label = (Label)c;
331             if(label.labeledWindow == this)
332                break;
333          }
334       }
335       
336       if(!c)
337       {
338          label = null;
339          for(c = editor.firstChild; c; c = c.next)
340          {
341             if(eClass_IsDerived(c._class, class(Label)))
342             {
343                label = (Label)c;
344                break;
345             }
346          }
347       }
348       // control.foreground = foreground;
349
350       if(code == 0 || code == 1)
351       {
352          editor.font = { editor.font.faceName, editor.font.size, bold = (code == 1) };
353          editor.background = white;
354       }
355       else if(code == 2)
356       {
357          Color foreground = 0x0F3F66;
358          int r = foreground.r, g = foreground.g, b = foreground.b;
359          Color src = white;
360          double alpha = 0.1;
361
362          editor.font = { editor.font.faceName, editor.font.size };
363
364          r = (int)(alpha * r + src.r * (1 - alpha));
365          g = (int)(alpha * g + src.g * (1 - alpha));
366          b = (int)(alpha * b + src.b * (1 - alpha));
367
368          r = Max(0,Min(255,r));
369          g = Max(0,Min(255,g));
370          b = Max(0,Min(255,b));
371
372          editor.background = Color { (byte) r, (byte) g, (byte) b };
373
374       }
375       if(label)
376       {
377          label.font = { editor.font.faceName, editor.font.size, bold = (code == 1) };
378          //label.foreground = foreground;
379       }
380    }
381
382    virtual void FinalizeLoading();
383
384    virtual void LoadOption(ProjectOptions options);
385    virtual void RetrieveOption(ProjectOptions options, bool isCfgOrPlt);
386    virtual void UnsetOption(ProjectOptions options)
387    {
388       Z value = (Z)0;
389       *(Z*)((byte *)options + option) = value;
390    }
391
392    virtual bool OptionSet(ProjectOptions options)
393    {
394       // TOFIX: If you get a crash here, it might be because JSON.ec must be after ProjectConfig.ec in the project files
395       //        JSON.ec must also be before ProjectSettings.ec in the project files
396       if(*(Z*)((byte *)options + option))
397          return true;
398       return false;
399    }
400    // BUG: OptionCheck = OptionSet; // Overrides derived classes OptionCheck ?
401
402    virtual bool OptionCheck(ProjectOptions options)
403    {
404       return OptionSet(options);
405    }
406
407    void MarkBuildTabModified()
408    {
409       BuildTab buildTab = (BuildTab)master;
410       while(buildTab && buildTab._class != class(BuildTab))
411          buildTab = (BuildTab)buildTab.master;
412       if(buildTab) buildTab.modifiedDocument = true;
413    }
414    
415    void Unset()
416    {
417       char * platformName = platform ? platform.OnGetString(0,0,0) : null;
418       MarkBuildTabModified();
419
420       if(config)
421       {
422          ProjectConfig c = null;
423          if(currentNode.configurations)
424          {
425             for(i : currentNode.configurations; !strcmpi(i.name, config.name)) { c = i; break; }
426             if(c)
427             {
428                if(platform)
429                {
430                   PlatformOptions p = null;
431                   if(c.platforms)
432                   {
433                      for(i : c.platforms; !strcmpi(i.name, platformName)) { p = i; break; }
434                      if(p)
435                      {
436                         if(p.options && OptionSet(p.options))
437                            UnsetOption(p.options);
438                         if(p.options && p.options.isEmpty)
439                            delete p.options;
440                         if(!p.options)
441                         {
442                            Iterator<PlatformOptions> it { c.platforms };
443                            if(it.Find(p))
444                            {
445                               it.Remove();
446                               delete p;
447                            }
448                         }
449                      }
450                      if(!c.platforms.count)
451                         c.platforms = null;
452                   }
453                   Load();
454                   return;
455                }
456                if(c.options && OptionSet(c.options))
457                   UnsetOption(c.options);
458                if(c.options && c.options.isEmpty)
459                   delete c.options;
460
461                // DON'T DELETE PROJECT CONFIGS HERE!
462                if(!c.options && currentNode != project.topNode)
463                {
464                   Iterator<ProjectConfig> it { currentNode.configurations };
465                   if(it.Find(c))
466                   {
467                      it.Remove();
468                      delete c;
469                   }
470                }
471             }
472             if(!currentNode.configurations.count)
473                currentNode.configurations = null;
474          }
475          Load();
476          return;
477       }
478       if(platform)
479       {
480          PlatformOptions p = null;
481          if(currentNode.platforms)
482          {
483             for(i : currentNode.platforms; !strcmpi(i.name, platformName)) { p = i; break; }
484             if(p)
485             {
486                if(p.options && OptionSet(p.options))
487                   UnsetOption(p.options);
488                if(p.options && p.options.isEmpty)
489                   delete p.options;
490                if(!p.options)
491                {
492                   Iterator<PlatformOptions> it { currentNode.platforms };
493                   if(it.Find(p))
494                   {
495                      it.Remove(p);
496                      delete p;
497                   }
498                }
499             }
500             if(!currentNode.platforms.count)
501                currentNode.platforms = null;
502          }
503          Load();
504          return;
505       }
506
507       if(currentNode.options && OptionSet(currentNode.options))
508          UnsetOption(currentNode.options);
509       if(currentNode.options && currentNode.options.isEmpty)
510       {
511          // delete currentNode.options;
512          // Property will free:
513          currentNode.options = null;
514       }
515
516       Load();
517    }
518
519    void FigureOutInherited()
520    {
521       ProjectNode node;
522       char * platformName = platform ? platform.OnGetString(0,0,0) : null;
523       bool skipped = false;
524       for(node = currentNode; node; node = node.parent)
525       {
526          bool configXplatformSet = false;
527          if(config && node.configurations)
528          {
529             for(c : node.configurations; !strcmpi(c.name, config.name))
530             {
531                if(platform && c.platforms)
532                {
533                   for(p : c.platforms; !strcmpi(p.name, platformName))
534                   {
535                      if(p.options && OptionSet(p.options))
536                      {
537                         if(skipped)
538                            LoadOption(p.options);
539                      }
540                      configXplatformSet = true;
541                      skipped = true;
542                      break;
543                   }
544                }               
545
546                if(skipped && c.options && OptionSet(c.options))
547                {
548                   LoadOption(c.options);
549                   if(configReplaces) return;
550                }
551                skipped = true;
552                break;
553             }
554          }
555          if((!configXplatformSet || !configReplaces) && platform && node.platforms)
556          {
557             for(p : node.platforms; !strcmpi(p.name, platformName))
558             {
559                if(skipped && p.options && OptionSet(p.options))
560                   LoadOption(p.options);
561                skipped = true;
562                break;
563             }
564          }
565          if(skipped && node.options && OptionSet(node.options))
566             LoadOption(node.options);
567          else if(skipped && !node.parent)
568             LoadOption(null);
569          skipped = true;
570       }
571    }
572
573    void Retrieve()
574    {
575       char * platformName = platform ? platform.OnGetString(0,0,0) : null;
576       MarkBuildTabModified();
577       if(config)
578       {
579          ProjectConfig c = null;
580          if(!currentNode.configurations) currentNode.configurations = { };
581          for(i : currentNode.configurations; !strcmpi(i.name, config.name)) { c = i; break; }
582          if(!c)
583             currentNode.configurations.Add(c = ProjectConfig { name = CopyString(config.name) });
584          if(platform)
585          {
586             PlatformOptions p = null;
587             if(!c.platforms) c.platforms = { };
588
589             for(i : c.platforms; !strcmpi(i.name, platformName)) { p = i; break; }
590             if(!p)
591                c.platforms.Add(p = PlatformOptions { CopyString(platformName) });
592
593             if(!p.options) p.options = { };
594             RetrieveOption(p.options, true);
595             if(!mergeValues) SetAttribs(1);
596             return;
597          }
598          if(!c.options) c.options = { };
599          RetrieveOption(c.options, true);
600          if(!mergeValues) SetAttribs(1);
601          return;
602       }
603       if(platform)
604       {
605          PlatformOptions p = null;
606          if(!currentNode.platforms) currentNode.platforms = { };
607          for(i : currentNode.platforms; !strcmpi(i.name, platformName)) { p = i; break; }
608          if(!p)
609             currentNode.platforms.Add(p = PlatformOptions { CopyString(platformName) });
610
611          if(!p.options) p.options = { };
612          RetrieveOption(p.options, true);
613          if(!mergeValues) SetAttribs(1);
614          return;
615       }
616
617       if(!currentNode.options) currentNode.options = { };
618       RetrieveOption(currentNode.options, false);
619       if(!mergeValues) SetAttribs((currentNode.parent || OptionCheck(currentNode.options)) ? 1 : 0);
620    }
621
622    void Load()
623    {
624       ProjectNode node;
625       char * platformName = platform ? platform.OnGetString(0,0,0) : null;
626       bool setAttribs = false;
627       for(node = currentNode; node; node = node.parent)
628       {
629          bool configXplatformSet = false;
630          ProjectConfig nodeConfig = null;
631          if(config && node.configurations)
632          {
633             for(c : node.configurations; !strcmpi(c.name, config.name))
634             {
635                if(platform && c.platforms)
636                {
637                   for(p : c.platforms; !strcmpi(p.name, platformName))
638                   {
639                      if(p.options && (mergeValues ? OptionCheck(p.options) : OptionSet(p.options)))
640                      {
641                         LoadOption(p.options);
642                         if(!setAttribs) { setAttribs = true; SetAttribs((node == currentNode) ? 1 : 2); }
643                         if(!mergeValues) { FinalizeLoading(); return; }
644                         configXplatformSet = true;
645                      }
646                      break;
647                   }
648                }               
649
650                nodeConfig = c;
651                break;
652             }
653          }
654          if(platform && node.platforms && (!configXplatformSet || !configReplaces))
655          {
656             for(p : node.platforms; !strcmpi(p.name, platformName))
657             {
658                if(p.options && (mergeValues ? OptionCheck(p.options) : OptionSet(p.options)))
659                {
660                   LoadOption(p.options);
661                   if(!setAttribs) { setAttribs = true; SetAttribs((node == currentNode && !config) ? 1 : 2); }
662                   if(!mergeValues) { FinalizeLoading(); return; }
663                }
664                break;
665             }
666          }
667
668          if(nodeConfig && nodeConfig.options && ((mergeValues && !configReplaces) ? OptionCheck(nodeConfig.options) : OptionSet(nodeConfig.options)))
669          {
670             LoadOption(nodeConfig.options);
671             if(!setAttribs) { setAttribs = true; SetAttribs((node == currentNode && !platform) ? 1 : 2); }
672             if(!mergeValues || configReplaces) { FinalizeLoading(); return; }
673          }
674
675          if(node.options && (mergeValues ? OptionCheck(node.options) : OptionSet(node.options)))
676          {
677             LoadOption(node.options);
678             if(!node.parent && !OptionCheck(node.options))
679             {
680                if(!setAttribs) { setAttribs = true; SetAttribs(0); }
681             }
682             else
683             {
684                if(!setAttribs) { setAttribs = true; SetAttribs((node == currentNode && !config && !platform) ? 1 : 2); }
685             }
686             if(!mergeValues) { FinalizeLoading(); return; }
687          }
688          else if(!node.parent)
689          {
690             LoadOption(null);
691             if(!setAttribs) { setAttribs = true; SetAttribs(0); }
692             if(!mergeValues) { FinalizeLoading(); return; }
693          }
694       }
695       FinalizeLoading();
696    }
697 }
698
699 class StringOptionBox : OptionBox<String>
700 {
701    editor = EditBox
702    {
703       bool NotifyModified(EditBox editBox)
704       {
705          ((OptionBox)editBox.id).Retrieve();
706          return true;
707       }
708
709       textHorzScroll = true;
710    };
711
712    void RetrieveOption(ProjectOptions options, bool isCfgOrPlt)
713    {
714       String * string = (String*)((byte *)options + option);
715       if(*string) delete *string;
716       *string = CopyString(((EditBox)editor).contents);
717    }
718
719    void LoadOption(ProjectOptions options)
720    {
721       ((EditBox)editor).contents = options ? *(String*)((byte *)options + option) : "";
722       ((EditBox)editor).Deselect();
723    }
724
725    bool OptionCheck(ProjectOptions options)
726    {
727       String string = *(String*)((byte *)options + option);
728       return string && string[0];
729    }
730
731    void UnsetOption(ProjectOptions options)
732    {
733       delete *(String*)((byte *)options + option);
734    }
735 }
736
737 class PathOptionBox : OptionBox<String>
738 {
739    bool Window::EditBoxORB(int x, int y, Modifiers mods)
740    {
741       Window parent = this.parent;
742       x += clientStart.x + position.x;
743       y += clientStart.y + position.y;
744       return ((OptionBox)this).OptionBox_OnRightButtonDown(parent, x, y, mods);
745    }
746
747    editor = PathBox
748    {
749       typeExpected = directory, browseDialog = { };
750       editBox.OnRightButtonDown = (void *)EditBoxORB;
751
752       bool NotifyModified(PathBox pathBox)
753       {
754          ((OptionBox)pathBox.id).Retrieve();
755          return true;
756       }
757    };
758
759    void RetrieveOption(ProjectOptions options, bool isCfgOrPlt)
760    {
761       String * string = (String*)((byte *)options + option);
762       String slashPath = ((PathBox)editor).slashPath;
763       if(*string) delete *string;
764       *string = CopyString(slashPath);//(slashPath && slashPath[0]) ? CopyString(slashPath) : null;
765    }
766
767    void LoadOption(ProjectOptions options)
768    {
769       ((PathBox)editor).path = options ? *(String*)((byte *)options + option) : "";
770       ((PathBox)editor).Deselect();
771    }
772
773    bool OptionCheck(ProjectOptions options)
774    {
775       String string = *(String*)((byte *)options + option);
776       return string && string[0];
777    }
778
779    void UnsetOption(ProjectOptions options)
780    {
781       delete *(String*)((byte *)options + option);
782    }
783 }
784
785 class MultiStringOptionBox : OptionBox<Array<String>>
786 {
787    bool caseSensitive;
788
789    mergeValues = true;
790    caseSensitive = true;
791
792    virtual Array<String> GetStrings();
793    virtual void SetStrings(Array<String> value);
794
795    Array<String> tempStrings;
796
797    void RetrieveOption(ProjectOptions options, bool isCfgOrPlt)
798    {
799       Array<String> newStrings = GetStrings();
800       Array<String> * strings = (Array<String>*)((byte *)options + option);
801       if(*strings) { strings->Free(); delete *strings; }
802
803       if(mergeValues)
804       {
805          Iterator<String> it { newStrings };
806
807          FigureOutInherited();
808
809          if(tempStrings)
810          {
811             Array<String> ts = tempStrings;
812             while(it.Next())
813             {
814                String s = it.data;
815                bool found = false;
816                for(i : tempStrings; !(caseSensitive ? strcmp : strcmpi)(i, s)) { found = true; break; }
817                if(found && (!configReplaces || platform))   // ADDED || platform here...
818                {
819                   delete s;
820                   it.Remove();
821                }
822             }
823          }
824          delete tempStrings;
825       }
826
827       if(!mergeValues || (configReplaces && isCfgOrPlt && !platform))
828          *strings = newStrings;
829       else
830       {
831          *strings = (newStrings && newStrings.count) ? newStrings : null;
832          if(newStrings && !newStrings.count) delete newStrings;
833       }
834
835       Load();
836    }
837
838    void LoadOption(ProjectOptions options)
839    {
840       if(mergeValues)
841       {
842          Array<String> strings = options ? *((Array<String>*)((byte *)options + option) : null;
843          if(strings)
844          {
845             if(!tempStrings)
846                tempStrings = { };
847             for(s : strings)
848             {
849                bool found = false;
850                for(i : tempStrings; !(caseSensitive ? strcmp : strcmpi)(i, s)) { found = true; break; }
851                if(!found) tempStrings.Add(s);
852             }
853          }
854       }         
855       else
856       {
857          SetStrings(options ? *(Array<String>*)((byte *)options + option) : null);
858       }
859    }
860
861    void FinalizeLoading()
862    {
863       if(mergeValues)
864       {
865          SetStrings(tempStrings);
866          delete tempStrings;
867       }
868    }
869
870    bool OptionSet(ProjectOptions options)
871    {
872       Array<String> strings = *(Array<String>*)((byte *)options + option);
873       if(mergeValues && !configReplaces)
874       {
875          return strings && strings.count;
876       }
877       else
878          return strings != null;
879    }
880
881    bool OptionCheck(ProjectOptions options)
882    {
883       Array<String> strings = *(Array<String>*)((byte *)options + option);
884       return strings && strings.count;
885    }
886
887    void UnsetOption(ProjectOptions options)
888    {
889       Array<String> * strings = (Array<String>*)((byte *)options + option);
890       if(*strings) { strings->Free(); delete *strings; }
891    }
892 }
893
894 class StringArrayOptionBox : MultiStringOptionBox
895 {
896    editor = StringListBox
897    {
898       bool NotifyModified(EditBox editBox)
899       {
900          ((OptionBox)editBox.id).Retrieve();
901          return true;
902       }
903    };
904
905    // NO VIRTUAL PROPERTIES YET...
906    Array<String> GetStrings() { return ((StringListBox)editor).strings; }
907    void SetStrings(Array<String> value) { ((StringListBox)editor).strings = value; }
908 }
909
910 class StringsArrayOptionBox : MultiStringOptionBox
911 {
912    editor = StringsBox
913    {
914       bool OnCreate()
915       {
916          project = ::project;
917          return true;
918       }
919
920       bool NotifyModified(StringsBox stringsBox)
921       {
922          ((OptionBox)stringsBox.id).Retrieve();
923          return true;
924       }
925    };
926
927    Array<String> GetStrings() { return ((StringsBox)editor).strings; }
928    void SetStrings(Array<String> value) { ((StringsBox)editor).strings = value; }
929 }
930
931 class DirsArrayOptionBox : MultiStringOptionBox
932 {
933    editor = DirectoriesBox
934    {
935       bool NotifyModified(DirectoriesBox dirsBox)
936       {
937          ((OptionBox)dirsBox.id).Retrieve();
938          return true;
939       }
940
941       bool OnChangedDir(char * * directory)
942       {
943          char fixedDirectory[MAX_LOCATION] = "";
944          if(PathCat(fixedDirectory, *directory))
945          {
946             char cwdBackup[MAX_LOCATION];
947             if(project)
948             {
949                GetWorkingDir(cwdBackup, sizeof(cwdBackup));
950                ChangeWorkingDir(project.topNode.path);
951             }
952             FileFixCase(fixedDirectory);
953             if(project)
954                ChangeWorkingDir(cwdBackup);
955             delete *directory;
956             *directory = CopyString(fixedDirectory);
957             return true;
958          }
959          return false;
960       }
961
962       bool OnPrepareBrowseDir(char * * directory)
963       {
964          char dir[MAX_LOCATION];
965          if(project)
966          {
967             GetSystemPathBuffer(dir, project.topNode.path);
968             if(*directory)
969                PathCat(dir, *directory);
970          }
971          else if(*directory)
972             strcpy(dir, *directory);
973          else
974             dir[0] = '\0';
975          
976          delete *directory;
977          *directory = CopyString(dir);
978
979             // GCC 4.4 bug:  -----  path becomes *directory
980             //strcpy(dir, path ? path : "");
981          return true;
982       }
983
984       bool OnBrowsedDir(char * * directory)
985       {
986          if(project)
987          {
988             char path[MAX_LOCATION];
989             MakePathRelative(*directory, project.topNode.path, path);
990             delete *directory;
991             *directory = CopyString(path);
992          }
993          return true;
994       }
995    };
996
997    Array<String> GetStrings() { return ((DirectoriesBox)editor).strings; }
998    void SetStrings(Array<String> value) { ((DirectoriesBox)editor).strings = value; }
999 }
1000
1001 class BoolOptionBox : OptionBox<SetBool>
1002 {
1003    editor = Button
1004    {
1005       isCheckbox = true;
1006
1007       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
1008       {
1009          ((OptionBox)button.id).Retrieve();
1010          return true;
1011       }
1012    };
1013
1014    bool OptionCheck(ProjectOptions options)
1015    {
1016       return *(SetBool*)((byte *)options + option) == true;
1017    }
1018
1019    void RetrieveOption(ProjectOptions options, bool isCfgOrPlt)
1020    {
1021       bool checked = ((Button)editor).checked;
1022       *(SetBool*)((byte *)options + option) = checked ? true : 
1023          ((currentNode.parent || isCfgOrPlt) ? false : unset);
1024    }
1025
1026    void LoadOption(ProjectOptions options)
1027    {
1028       ((Button)editor).checked = options && (*(SetBool*)((byte *)options + option) == true);
1029    }
1030 }
1031
1032 class CheckBoxForEnumOptionBox : OptionBox
1033 {
1034    editor = Button
1035    {
1036       isCheckbox = true;
1037
1038       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
1039       {
1040          ((OptionBox)button.id).Retrieve();
1041          {
1042             Window slave;
1043             for(slave = master.firstSlave; slave; slave = slave.nextSlave)
1044             {
1045                if(eClass_IsDerived(slave._class, class(CheckBoxForEnumOptionBox)) &&
1046                      slave != (Window)button.id &&
1047                      ((OptionBox)slave).option == ((OptionBox)button.id).option)
1048                   ((OptionBox)slave).Load();
1049             }
1050          }
1051          return true;
1052       }
1053    };
1054
1055    Z enumValue;
1056    void LoadOption(ProjectOptions options)
1057    {
1058       Z value = options ? *(Z*)((byte *)options + option) : (Z)0;
1059       ((Button)editor).checked = value == enumValue;
1060    }
1061
1062    void RetrieveOption(ProjectOptions options, bool isCfgOrPlt)
1063    {
1064       Button checkBox = (Button)editor;
1065       if(checkBox.checked)
1066          *(Z*)((byte *)options + option) = enumValue;
1067    }
1068 }
1069
1070 class BuildBitDepthOptionBox : CheckBoxForEnumOptionBox<BuildBitDepth> { }
1071
1072 class DropOptionBox : OptionBox
1073 {
1074    editor = DropBox
1075    {
1076       bool NotifySelect(DropBox dropBox, DataRow row, Modifiers mods)
1077       {
1078          ((OptionBox)dropBox.id).Retrieve();
1079          return true;
1080       }
1081    };   
1082
1083    void LoadOption(ProjectOptions options)
1084    {
1085       DropBox dropBox = (DropBox)editor;
1086       Z value = options ? *(Z*)((byte *)options + option) : (Z)0;
1087       dropBox.currentRow = value ? dropBox.FindRow((int64)value) : dropBox.firstRow;
1088    }
1089
1090    void RetrieveOption(ProjectOptions options, bool isCfgOrPlt)
1091    {
1092       DropBox dropBox = (DropBox)editor;
1093       DataRow row = dropBox.currentRow;
1094       Z value = (Z)(row ? row.tag : 0);
1095       *(Z*)((byte *)options + option) = value;
1096    }
1097 }
1098
1099 class TargetTypeDB : DropOptionBox<TargetTypes>
1100 {
1101    TargetTypeDB()
1102    {
1103       DataRow row;
1104
1105       row = ((DropBox)editor).AddRow();
1106       row.tag = TargetTypes::executable;
1107       row.SetData(null, $"Executable");
1108
1109       row = ((DropBox)editor).AddRow();
1110       row.tag = TargetTypes::sharedLibrary;
1111       row.SetData(null, $"Shared Library");
1112
1113       row = ((DropBox)editor).AddRow();
1114       row.tag = TargetTypes::staticLibrary;
1115       row.SetData(null, $"Static Library");
1116    }
1117
1118    bool OptionCheck(ProjectOptions options)
1119    {
1120       TargetTypes value = *(TargetTypes*)((byte *)options + option);
1121       return value && value != executable;
1122    }
1123 }
1124
1125 class OptimizationDB : DropOptionBox<OptimizationStrategy>
1126 {
1127    OptimizationDB()
1128    {
1129       DataRow row;
1130       row = ((DropBox)editor).AddRow();
1131       row.tag = OptimizationStrategy::none;
1132       row.SetData(null, $"None");
1133
1134       row = ((DropBox)editor).AddRow();
1135       row.tag = OptimizationStrategy::speed;
1136       row.SetData(null, $"For Speed (-O2)");
1137
1138       row = ((DropBox)editor).AddRow();
1139       row.tag = OptimizationStrategy::size;
1140       row.SetData(null, $"For Size (-Os)");
1141    }
1142
1143    bool OptionCheck(ProjectOptions options)
1144    {
1145       OptimizationStrategy value = *(OptimizationStrategy*)((byte *)options + option);
1146       return value && value != none;
1147    }
1148 }
1149
1150 class WarningsDB : DropOptionBox<WarningsOption>
1151 {
1152    WarningsDB()
1153    {
1154       DataRow row;
1155       row = ((DropBox)editor).AddRow();
1156       row.tag = WarningsOption::normal;
1157       row.SetData(null, $"Normal");
1158
1159       row = ((DropBox)editor).AddRow();
1160       row.tag = WarningsOption::none;
1161       row.SetData(null, $"None");
1162
1163       row = ((DropBox)editor).AddRow();
1164       row.tag = WarningsOption::all;
1165       row.SetData(null, $"All");
1166    }
1167
1168    bool OptionCheck(ProjectOptions options)
1169    {
1170       WarningsOption value = *(WarningsOption*)((byte *)options + option);
1171       return value && value != none;
1172    }
1173 }
1174
1175 void DrawStipple(Surface surface, Size clientSize)
1176 {
1177    int x1 = 0;
1178    int y1 = 0;
1179    int x2 = clientSize.w - 1;
1180    int y2 = clientSize.h - 1;
1181    if((x2 - x1) & 1) x2--;
1182    if((y2 - y1) & 1) y2--;
1183
1184    surface.LineStipple(0x5555);
1185    surface.Rectangle(x1, y1, x2, y2);
1186    surface.LineStipple(0);            
1187 }
1188
1189 class BuildTab : Tab
1190 {
1191    text = $"Build";
1192    background = formColor;
1193    tabCycle = true;
1194
1195    ProjectNode backupNode;
1196    String activeConfigName;
1197
1198    ProjectNode lastSelectedNode;
1199
1200    property char * selectedConfigName
1201    {
1202       get
1203       {
1204          if(created)
1205          {
1206             SelectorButton button = (SelectorButton)configSelector.selectedButton;
1207             if(button && button.id)
1208             {
1209                ProjectConfig config = (ProjectConfig)button.id;
1210                return config.name;
1211             }
1212          }
1213          return "";
1214       }
1215    }
1216
1217    property char * selectedPlatformName
1218    {
1219       get
1220       {
1221          if(created)
1222          {
1223             SelectorButton button = (SelectorButton)platformSelector.selectedButton;
1224             if(button && button.id)
1225             {
1226                Platform platform = (Platform)button.id;
1227                char * platformName = platform ? platform.OnGetString(0,0,0) : null; // all these platformName are leaking, no? 
1228                return platformName;
1229             }
1230          }
1231          return "";
1232       }
1233    }
1234
1235    Label labelConfigurations
1236    {
1237       this, anchor = { left = 8, top = 14 }, labeledWindow = configSelector;
1238
1239       void OnRedraw(Surface surface)
1240       {
1241          Label::OnRedraw(surface);
1242          if(labeledWindow.active) DrawStipple(surface, clientSize);
1243       }
1244    };
1245    SelectorBar configSelector
1246    {
1247       this, text = $"Configurations: ", anchor = { left = 98, top = 8, right = 54 }; size = { 0, 26 };
1248       opacity = 0;
1249       direction = horizontal, scrollable = true;
1250
1251       bool OnKeyDown(Key key, unichar ch)
1252       {
1253          if(key == insert)
1254          {
1255             ((BuildTab)parent).createConfig.NotifyClicked(parent, ((BuildTab)parent).createConfig, 0, 0, 0);
1256             return false;
1257          }
1258          else if(key == del)
1259          {
1260             ((BuildTab)parent).deleteConfig.NotifyClicked(parent, ((BuildTab)parent).deleteConfig, 0, 0, 0);
1261             return false;
1262          }
1263          return SelectorBar::OnKeyDown(key, ch);
1264       }
1265       
1266       bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
1267       {
1268          ((BuildTab)master).labelConfigurations.Update(null);
1269          return true;
1270       }
1271    };
1272
1273    Button createConfig
1274    {
1275       parent = this, bevelOver = true, inactive = true;
1276       size = { 22, 22 };
1277       anchor = { top = 10, right = 31 };
1278       hotKey = altC, bitmap = BitmapResource { fileName = ":actions/docNew.png", alphaBlend = true };
1279
1280       bool NotifyClicked(Button b, int x, int y, Modifiers mods)
1281       {
1282          char tmp[MAX_F_STRING];
1283          ProjectConfig config;
1284          EditableSelectorButton button;
1285
1286          FindUniqueConfigName("NewConfig", false, tmp);
1287
1288          config =
1289          {
1290             makingModified = true;
1291             compilingModified = true;
1292             linkingModified = true;
1293             name = CopyString(tmp);
1294             options =
1295             {
1296                // objectsDirectory = /*CopyString(*/defaultObjDirExpression/*)*/;
1297             };
1298          };
1299          if(!project.topNode.configurations) project.topNode.configurations = { };
1300          project.topNode.configurations.Add(config);
1301          /*
1302          targetType = project.config.options.targetType;
1303          config.options.
1304          config.options.targetFileName = project.moduleName;
1305          config.options.targetDir.dir = "";
1306          config.options.objectsDirectory = defaultObjDirExpression);
1307          config.options.debug = true;
1308          config.options.optimization = none;
1309          config.options.warnings = all;
1310          */         
1311
1312          button =
1313          {
1314             configSelector, renameable = true, master = this, text = config.name, id = (int64)config;
1315             NotifyClicked = ConfigClicked, OnRename = ConfigOnRename;
1316          };
1317
1318          configSelector.Select(button);
1319          modifiedDocument = true;
1320          return true;
1321       }
1322    };
1323    /*Button duplicateConfig
1324    {
1325       parent = this, bevelOver = true, inactive = true;
1326       size = { 22, 22 };
1327       anchor = { top = 10, right = 31 };
1328       hotKey = altU, bitmap = BitmapResource { fileName = ":actions/editCopy.png", alphaBlend = true };
1329    };*/
1330    Button deleteConfig
1331    {
1332       parent = this, bevelOver = true, inactive = true;
1333       size = { 22, 22 };
1334       anchor = { top = 10, right = 8 };
1335       hotKey = altD, bitmap = BitmapResource { fileName = ":actions/delete2.png", alphaBlend = true };
1336
1337       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
1338       {
1339          if(config)
1340          {
1341             String title = PrintString($"Delete ", config.name, $" Configuration");
1342             String msg = PrintString($"Are you sure you wish to delete the ", config.name, $" configuration?");
1343             if(MessageBox { type = okCancel, text = title, contents = msg }.Modal() == ok)
1344             {
1345                Iterator<Window> it { configSelector.controls };
1346                ProjectConfig configToDelete = config;
1347                /*
1348                while(it.Next())
1349                {
1350                   SelectorButton button = (SelectorButton)it.data;
1351                   if((ProjectConfig)button.id == config)
1352                   {
1353                      button.visible = false;
1354                      button.Destroy(0);
1355
1356                      if(it.Prev())
1357                      {
1358                         button = (SelectorButton)it.data;
1359                         config = (ProjectConfig)button.id;
1360                         configSelector.Select(button);
1361                      }
1362                      break;
1363                   }
1364                }
1365                */
1366                SelectorButton button = configSelector.FindButtonByID((int64)configToDelete);
1367                if(button)
1368                   configSelector.RemoveButton(button);
1369
1370                project.topNode.DeleteConfig(configToDelete);
1371
1372                modifiedDocument = true;
1373             }
1374             delete title;
1375             delete msg;
1376          }
1377          return true;
1378       }
1379    };
1380    
1381    Label labelPlatforms
1382    {
1383       this, anchor = { left = 8, top = 44 }, labeledWindow = platformSelector;
1384
1385       void OnRedraw(Surface surface)
1386       {
1387          Label::OnRedraw(surface);
1388          if(labeledWindow.active) DrawStipple(surface, clientSize);
1389       }
1390    };
1391    SelectorBar platformSelector
1392    {
1393       this, text = $"Platforms: ", anchor = { left = 64, top = 38, right = 54 }; size = { 0, 26 };
1394       opacity = 0;
1395       direction = horizontal, scrollable = true;
1396
1397       bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
1398       {
1399          ((BuildTab)master).labelPlatforms.Update(null);
1400          return true;
1401       }
1402    };
1403
1404    TabControl buildTabControl
1405    {
1406       this, background = formColor, anchor = { left = 8, top = 64, right = 8, bottom = 8 };
1407       curTab = compilerTab;
1408    };
1409    CompilerTab compilerTab { this, tabControl = buildTabControl };
1410    LinkerTab linkerTab { this, tabControl = buildTabControl };
1411    BuilderTab builderTab { this, tabControl = buildTabControl };
1412    Label rightClick
1413    {
1414       this, font = { font.faceName, font.size, italic = true }, stayOnTop = true,
1415       text = $"(Right click or press Ctrl-Del to revert an option to inherited value)", anchor = { top = 72, right = 16 }
1416    };
1417
1418    void FindUniqueConfigName(char * baseName, bool startWithNumber, char * output)
1419    {
1420       int num = 0;
1421       char tmp[MAX_F_STRING];
1422       if(startWithNumber)
1423          sprintf(tmp, "%s%d", baseName, num);
1424       else
1425          strcpy(tmp, baseName);
1426       while(true)
1427       {
1428          ProjectConfig config = null;
1429          for(c : project.topNode.configurations)
1430          {     // TOFIX: Error when omitting these brackets, c not found
1431             if(c.name && !strcmp(c.name, tmp))
1432             {
1433                config = c;
1434                break;
1435             }
1436          }
1437          if(config)
1438          {
1439             num++;
1440             sprintf(tmp, "%s%d", baseName, num);
1441          }
1442          else
1443             break;
1444       }
1445       strcpy(output, tmp);
1446    }
1447
1448    bool PlatformClicked(Button clickedButton, int x, int y, Modifiers mods)
1449    {
1450       if(!eClass_IsDerived(clickedButton._class, class(EditableSelectorButton)) || !((EditableSelectorButton)clickedButton).editBox)
1451       {
1452          platform = (Platform)clickedButton.id;
1453
1454          // Load Settings Into Dialog
1455          compilerTab.LoadSettings();
1456          linkerTab.LoadSettings();
1457          builderTab.LoadSettings();
1458
1459          if(!mods)
1460             buildTabControl.Activate();
1461
1462          if(compilerTab.rightPaneHeader.visible)
1463             compilerTab.rightPaneHeader.Update(null);
1464          ((ProjectSettings)master).UpdateDialogTitle();
1465       }
1466       return true;
1467    }
1468
1469    ~BuildTab()
1470    {
1471       platformSelector.DestroyChildren();
1472       configSelector.DestroyChildren();
1473
1474       delete activeConfigName;
1475    }
1476
1477    bool ConfigOnRename(EditableSelectorButton button, char * * oldName, char * * newName)
1478    {
1479       int c, d = 0;
1480       char ch;
1481
1482       for(c = 0; (ch = (*newName)[c]); c++)
1483       {
1484          if(ch == '_' || isalpha(ch) || (isdigit(ch) && d))
1485             (*newName)[d++] = ch;
1486       }
1487       (*newName)[d] = 0;
1488
1489       {
1490          bool found = false;
1491          for(c : project.topNode.configurations; c != config)
1492          {
1493             if(!strcmpi(c.name, *newName))
1494             {
1495                found = true;
1496                break;
1497             }
1498          }
1499          if(found || !(*newName)[0])
1500          {
1501             char tmp[MAX_F_STRING];
1502             char * tmpName = config.name;
1503             config.name = null;
1504             FindUniqueConfigName("NewConfig", false, tmp);
1505             config.name = tmpName;
1506             delete *newName;
1507             *newName = CopyString(tmp);
1508          }
1509       }
1510
1511       if(activeConfigName && !strcmp(activeConfigName, *oldName))
1512       {
1513          delete activeConfigName;
1514          activeConfigName = CopyString(*newName);
1515       }
1516
1517       project.topNode.RenameConfig(config.name, *newName);
1518       
1519       modifiedDocument = true;
1520       return true;
1521    }
1522
1523    bool ConfigClicked(Button clickedButton, int x, int y, Modifiers mods)
1524    {
1525       if(!eClass_IsDerived(clickedButton._class, class(EditableSelectorButton)) || !((EditableSelectorButton)clickedButton).editBox)
1526       {
1527          config = (ProjectConfig)clickedButton.id;
1528
1529          // Load Settings Into Dialog
1530          compilerTab.LoadSettings();
1531          linkerTab.LoadSettings();
1532          builderTab.LoadSettings();
1533
1534          deleteConfig.disabled = (clickedButton._class == class(SelectorButton));
1535
1536          if(!mods)
1537             buildTabControl.Activate();
1538
1539          compilerTab.fileList.Update(null);
1540          if(compilerTab.rightPaneHeader.visible)
1541             compilerTab.rightPaneHeader.Update(null);
1542          ((ProjectSettings)master).UpdateDialogTitle();
1543       }
1544       return true;
1545    }
1546
1547    void SelectNode(ProjectNode node, bool ignoreAsLastSelection)
1548    {
1549       if(node != currentNode)
1550       {
1551          Window ac = compilerTab.rightPane.activeChild;
1552          bool prevNodeRes = currentNode ? currentNode.isInResources : false;
1553          bool newNodeRes;
1554
1555          if(!node) node = project.topNode;
1556
1557          newNodeRes = node.isInResources;
1558          
1559          currentNode = node;
1560          if(!ignoreAsLastSelection)
1561             lastSelectedNode = node;
1562
1563          ((ProjectSettings)master).UpdateDialogTitle();
1564          if(node.type == project)
1565          {
1566             compilerTab.rightPaneHeader.visible = false;
1567          }
1568          else
1569          {
1570             compilerTab.rightPaneHeader.id = (int64)node;
1571             compilerTab.rightPaneHeader.Update(null);
1572             compilerTab.rightPaneHeader.visible = true;
1573          }
1574
1575          {
1576             DataRow row = compilerTab.fileList.FindSubRow((int64)currentNode);
1577             if(row)
1578             {
1579                compilerTab.fileList.currentRow = row;
1580                while((row = row.parent))
1581                   row.collapsed = false;
1582             }
1583          }
1584
1585          if(prevNodeRes != newNodeRes)
1586          {
1587             compilerTab.labelObjDir.visible = !newNodeRes;
1588             compilerTab.objDir.visible = !newNodeRes;
1589             compilerTab.excludeFromBuild.visible = !newNodeRes;
1590             compilerTab.labelPreprocessorDefs.visible = !newNodeRes;
1591             compilerTab.preprocessorDefs.visible = !newNodeRes;
1592             compilerTab.labelDefaultNameSpace.visible = !newNodeRes;
1593             compilerTab.defaultNameSpace.visible = !newNodeRes;
1594             compilerTab.strictNameSpaces.visible = !newNodeRes;
1595             compilerTab.memoryGuard.visible = !newNodeRes;
1596             compilerTab.noLineNumbers.visible = !newNodeRes;
1597             compilerTab.debug.visible = !newNodeRes;
1598             compilerTab.labelWarnings.visible = !newNodeRes;
1599             compilerTab.warnings.visible = !newNodeRes;
1600             compilerTab.profiling.visible = !newNodeRes;
1601             compilerTab.labelOptimization.visible = !newNodeRes;
1602             compilerTab.optimization.visible = !newNodeRes;
1603             compilerTab.fastMath.visible = !newNodeRes;
1604             compilerTab.labelIncludeDirs.visible = !newNodeRes;
1605             compilerTab.includeDirs.visible = !newNodeRes;
1606          }
1607          
1608          if(node == project.topNode)
1609          {
1610             compilerTab.objDir.visible = true;
1611             compilerTab.labelObjDir.visible = true;
1612
1613             compilerTab.excludeFromBuild.visible = false;
1614          }
1615          else
1616          {
1617             compilerTab.objDir.visible = false;
1618             compilerTab.labelObjDir.visible = false;
1619
1620             compilerTab.excludeFromBuild.visible = (node != project.resNode);
1621          }
1622
1623          // Load Settings Into Dialog
1624          compilerTab.LoadSettings();
1625          linkerTab.LoadSettings();
1626          builderTab.LoadSettings();
1627
1628          if(ac)
1629          {
1630             if(!ac.visible)
1631             {
1632                if(ac == compilerTab.excludeFromBuild.editor)
1633                   ac = compilerTab.objDir.editor;
1634                else if(compilerTab.excludeFromBuild.editor.visible)
1635                   ac = compilerTab.excludeFromBuild.editor;
1636             }
1637             ac.MakeActive();
1638          }
1639       }
1640    }
1641
1642    void CreateConfigButtons()
1643    {
1644       SelectorButton commonButton;
1645
1646       // Create Config Buttons
1647       commonButton = SelectorButton
1648       {
1649          configSelector, master = this, text = $"Common", id = (int64)null; font = { font.faceName, font.size, true };
1650          checked = true;
1651          NotifyClicked = ConfigClicked;
1652       };
1653       
1654       config = null;
1655
1656       if(project.topNode.configurations)
1657       {
1658          for(c : project.topNode.configurations)
1659          {
1660             EditableSelectorButton button
1661             {
1662                configSelector, master = this, renameable = true, text = c.name, id = (int64)c;
1663                NotifyClicked = ConfigClicked, OnRename = ConfigOnRename;
1664             };
1665          }
1666       }
1667    }
1668    
1669    void Init()
1670    {
1671       Platform p;
1672       SelectorButton button;
1673
1674       activeConfigName = project.config ? CopyString(project.config.name) : null;
1675       
1676       compilerTab.AddNode(project.topNode, null);
1677
1678       CreateConfigButtons();
1679
1680       platformButton = button =
1681       {
1682          platformSelector, master = this, text = $"Common", id = 0;  font = { font.faceName, font.size, true };
1683          NotifyClicked = PlatformClicked; checked = true;
1684       };
1685
1686       platform = 0;
1687
1688       for(p = (Platform)1; p < Platform::enumSize; p++)
1689       {
1690          SelectorButton button
1691          {
1692             platformSelector, master = this, text = p.OnGetString(0,0,0), id = (int64)p; 
1693             NotifyClicked = PlatformClicked;
1694          };
1695       }
1696    }
1697    SelectorButton platformButton;
1698
1699    bool OnPostCreate()
1700    {
1701       // Backup Current Settings
1702       backupNode = project.topNode.Backup();
1703
1704       buildTabControl.Activate();
1705
1706       {
1707          Iterator<Window> it { configSelector.controls };
1708          while(it.Next())
1709          {
1710             SelectorButton configButton = (SelectorButton)it.data;
1711             ProjectConfig buttonConfig = (ProjectConfig)configButton.id;
1712             if(buttonConfig == project.config)
1713             {
1714                configButton.Activate();
1715                configButton.checked = true;
1716                ConfigClicked(configButton, 0, 0, (Modifiers)null);
1717                break;
1718             }
1719          }
1720       }
1721       if(platformButton)
1722       {
1723          platformButton.MakeActive();
1724          platformButton = null;
1725       }
1726       return true;
1727    }
1728
1729    void OnDestroy()
1730    {
1731       delete backupNode;
1732
1733       lastSelectedNode = null;
1734
1735       project.config = null;
1736
1737       /* // THIS IS NOW AUTOMATED WITH A project CHECK IN ProjectNode
1738       project.configurations = project.topNode.configurations;
1739       project.platforms = project.topNode.platforms;
1740       project.options = project.topNode.options;
1741       */
1742
1743       if(project.topNode.configurations)
1744       {
1745          for(c : project.topNode.configurations)
1746          {
1747             if(!strcmpi(c.name, activeConfigName))
1748             {
1749                project.config = c;
1750                break;
1751             }
1752          }
1753       }
1754       if(!project.config)
1755       {
1756          List<ProjectConfig> configs = project.topNode.configurations;
1757          if(configs && configs.count)
1758             project.config = configs[0];
1759       }
1760
1761       ide.UpdateToolBarActiveConfigs(false);
1762    }
1763
1764    void RevertChanges()
1765    {
1766       String configName = config ? CopyString(config.name) : null;
1767
1768       // Revert to saved project options
1769       project.topNode.Revert(backupNode);
1770
1771       configSelector.DestroyChildren();
1772       CreateConfigButtons();
1773
1774       // Reselect Configuration
1775       if(configName)
1776       {
1777          Iterator<Window> it { configSelector.controls };
1778          while(it.Next())
1779          {
1780             Button button = (Button)it.data;
1781             ProjectConfig c = (ProjectConfig)button.id;
1782             if(c && !strcmp(c.name, configName))
1783             {
1784                config = c;
1785                button.Activate();
1786                button.checked = true;
1787                ConfigClicked(button, 0,0, 0);
1788                break;
1789             }
1790          }
1791       }
1792
1793       SelectNode(project.topNode, false);
1794
1795       delete configName;
1796    }
1797
1798    bool OnClose(bool parentClosing)
1799    {
1800       if(modifiedDocument)
1801       {
1802          DialogResult diagRes = MessageBox
1803          {
1804             type = yesNoCancel, master = ide,
1805             text = $"Save changes to project settings?",
1806             contents = $"Would you like to save changes made to the build options?"
1807          }.Modal();
1808          if(diagRes == no)
1809             RevertChanges();
1810          if(diagRes == cancel)
1811             return false;
1812          if(diagRes == yes)
1813          {
1814             project.MarkChanges(backupNode);
1815             project.topNode.modified = true;
1816             ide.projectView.modifiedDocument = true;
1817             ide.UpdateToolBarActiveConfigs(false);
1818             ide.projectView.Update(null);
1819          }
1820          modifiedDocument = false;
1821       }
1822       return true;
1823    }
1824 }
1825
1826 class CompilerTab : Tab
1827 {
1828    background = formColor;
1829    text = $"Compiler";
1830
1831    Window leftPane { this, size = { 180 }, anchor = { left = 0, top = 0, bottom = 0 }, background = formColor };
1832
1833    Label labelFileList { leftPane, this, position = { 8, 8 }, labeledWindow = fileList };
1834    ListBox fileList
1835    {
1836       leftPane, this, borderStyle = deep, hasVertScroll = true, hasHorzScroll = true;
1837       // THIS WOULD BE EVEN MORE FUN: multiSelect = true,
1838       fullRowSelect = false, collapseControl = true, treeBranches = true;
1839       alwaysHighLight = true;
1840       selectionColor = unfocusedSelectorColor;
1841       size = { 180 };
1842       anchor = Anchor { left = 8, top = 24, right = 4, bottom = 8 };
1843       text = $"Files";
1844
1845       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
1846       {
1847          BuildTab buildTab = (BuildTab)master;
1848          ProjectNode node = (ProjectNode)row.tag;
1849          buildTab.SelectNode(node, false);
1850          return true;
1851       }
1852
1853       void OnRedraw(Surface surface)
1854       {
1855          ide.projectView.drawingInProjectSettingsDialog = true;
1856          ListBox::OnRedraw(surface);
1857          ide.projectView.drawingInProjectSettingsDialog = false;
1858       }
1859
1860       bool NotifyActivate(Window window, bool active, Window previous)
1861       {
1862          if(active)
1863          {
1864             //subclass(Skin) skinClass = (subclass(Skin))eSystem_FindClass(app, app.skin);
1865             fileList.selectionColor = Color { 10, 36, 106 }; //skinClass.selectionColor; // darkBlue;
1866          }
1867          else if(fileList.currentRow)
1868          {
1869             DataRow currentRow = fileList.currentRow;
1870             //int headerSize = ((fileList.style.header) ? fileList.rowHeight : 0);
1871             int height = fileList.clientSize.h + 1;// - fileList.headerSize;
1872             fileList.selectionColor = unfocusedSelectorColor;
1873             if(currentRow && currentRow.index * fileList.rowHeight > fileList.scroll.y + height - fileList.rowHeight)
1874                fileList.SetScrollPosition(fileList.scroll.x, currentRow.index * fileList.rowHeight - height + fileList.rowHeight);
1875             else if(!currentRow || currentRow.index * fileList.rowHeight < fileList.scroll.y)
1876                fileList.SetScrollPosition(fileList.scroll.x, currentRow ? currentRow.index * fileList.rowHeight : 0);
1877
1878          }
1879
1880          return true;
1881       }
1882    };
1883
1884    Window rightPane 
1885    {
1886       this, anchor = { left = 196, top = 0, right = 0, bottom = 0 }, background = formColor, tabCycle = true;
1887    };
1888
1889    Window rightPaneHeader
1890    {
1891       rightPane, this, size = { h = 21 }, anchor = { left = 0, top = 0, right = 0 }, background = Color { 70, 96, 166 };//0x0F3F66;
1892       foreground = white; visible = false;
1893
1894       void OnRedraw(Surface surface)
1895       {
1896          if(id)
1897          {
1898             ide.projectView.drawingInProjectSettingsDialogHeader = true;
1899             ((void (*)(void *, void *, void *, int, int, int, void *, uint, uint))(void *)class(ProjectNode)._vTbl[__ecereVMethodID_class_OnDisplay])(class(ProjectNode),
1900                (void *)id, surface, 8, 2, clientSize.w, ide.projectView, Alignment::left, DataDisplayFlags { selected = true });
1901             ide.projectView.drawingInProjectSettingsDialogHeader = false;
1902          }
1903       }
1904    };
1905
1906    PaneSplitter splitter
1907    {
1908       this, leftPane = leftPane, rightPane = rightPane, split = 188
1909    };
1910
1911    Label labelObjDir { rightPane, this, position = { 8, 8 }, labeledWindow = objDir };
1912    PathOptionBox objDir
1913    {
1914       rightPane, this, size = { 250, 22 }, anchor = { left = 8, top = 24, right = 8 };
1915       text = $"Intermediate Objects Directory", hotKey = altJ, option = OPTION(objectsDirectory);
1916    };
1917
1918    BoolOptionBox excludeFromBuild
1919    {
1920       rightPane, this, position = { 8, 28 },
1921       text = $"Exclude from Build", visible = false, option = OPTION(excludeFromBuild);
1922    };
1923
1924    Label labelPreprocessorDefs { rightPane, this, position = { 8, 50 }, labeledWindow = preprocessorDefs };
1925    StringArrayOptionBox preprocessorDefs
1926    {
1927       rightPane, this, size = { 290, 22 }, anchor = { left = 8, top = 66, right = 8 };
1928       text = $"Preprocessor Definitions", hotKey = altD, option = OPTION(preprocessorDefinitions);
1929    };
1930
1931    Label labelDefaultNameSpace { rightPane, this, position = { 8, 92 }, labeledWindow = defaultNameSpace };
1932    StringOptionBox defaultNameSpace
1933    {
1934       rightPane, this, size = { 160, 22 }, position = { 8, 108 };
1935       text = $"Default Name Space", option = OPTION(defaultNameSpace);
1936    };
1937    BoolOptionBox strictNameSpaces
1938    {
1939       rightPane, this, position = { 172, 112 },
1940       text = $"Strict Name Spaces", option = OPTION(strictNameSpaces);
1941    };
1942
1943    BoolOptionBox fastMath
1944    {
1945       rightPane, this, position = { 316, 112 },
1946       text = $"Fast Math", option = OPTION(fastMath);
1947    };
1948
1949    BoolOptionBox memoryGuard
1950    {
1951       rightPane, this, position = { 8, 154 };
1952       text = $"MemoryGuard", hotKey = altM, option = OPTION(memoryGuard);
1953    };
1954
1955    Label labelWarnings { rightPane, position = { 116, 138 }, labeledWindow = warnings };
1956    WarningsDB warnings
1957    {
1958       rightPane, this, position = { 116, 154 };
1959       text = $"Warnings", hotKey = altW, option = OPTION(warnings);
1960    };
1961
1962    Label labelOptimization { rightPane, position = { 220, 138 }, labeledWindow = optimization };
1963    OptimizationDB optimization
1964    {
1965       rightPane, this, position = { 220, 154 }, size = { 120, 22 };
1966       text = $"Optimization", hotKey = altO, option = OPTION(optimization);
1967    };
1968
1969    BoolOptionBox debug
1970    {
1971       rightPane, this, position = { 8, 188 };
1972       text = $"Debuggable", hotKey = altG, option = OPTION(debug);
1973    };
1974
1975    BoolOptionBox profiling
1976    {
1977       rightPane, this, position = { 116, 188 };
1978       text = $"Profiling Data", hotKey = altP, option = OPTION(profile);
1979    };
1980
1981    BoolOptionBox noLineNumbers
1982    {
1983       rightPane, this, position = { 220, 188 };
1984       text = $"No Line Numbers", hotKey = altN, option = OPTION(noLineNumbers);
1985    };
1986
1987    Label labelCompilerOptions { rightPane, this, position = { 8, 208 }, labeledWindow = compilerOptions };
1988    StringArrayOptionBox compilerOptions
1989    {
1990       rightPane, this, size = { 290, 22 }, anchor = { left = 8, top = 224, right = 8 };
1991       text = $"Compiler Options", hotKey = altO, option = OPTION(compilerOptions);
1992       configReplaces = true;
1993    };
1994
1995    Label labelIncludeDirs { includeDirs.editor, labeledWindow = includeDirs, position = { 0, 6 }; };
1996    DirsArrayOptionBox includeDirs
1997    {
1998       rightPane, this, size = { 290, 22 }, anchor = { left = 8, top = 250, right = 8, bottom = 8 };
1999       text = $"Additional Include Directories", hotKey = altI, option = OPTION(includeDirs);
2000    };
2001
2002    CompilerTab()
2003    {
2004       fileList.AddField(DataField { dataType = class(ProjectNode), freeData = false,
2005          userData = null /* Now set in the ProjectNode directly to know we're in ProjectSettings Dialog -- ide.projectView*/ });
2006    }
2007
2008    bool OnCreate()
2009    {
2010       BuildTab buildTab = (BuildTab)master;
2011       buildTab.SelectNode(buildTab.lastSelectedNode, true);
2012       return true;
2013    }
2014
2015    void AddNode(ProjectNode node, DataRow addTo)
2016    {
2017       DataRow row = addTo ? addTo.AddRow() : fileList.AddRow();
2018
2019       row.tag = (int64)node;
2020
2021       row.SetData(null, node);
2022
2023       if(node.files && node.files.first && node.parent && 
2024             !(!node.parent.parent && 
2025                (!strcmpi(node.name, "notes") || !strcmpi(node.name, "sources") || 
2026                   !strcmpi(node.name, "src") || !strcmpi(node.name, "tools"))))
2027          row.collapsed = true;
2028       else if(node.type == folder)
2029          node.icon = openFolder;
2030
2031       if(node.files)
2032       {
2033          for(child : node.files)
2034             AddNode(child, row);
2035       }
2036    }
2037
2038    void LoadSettings()
2039    {
2040       OptionBox ob;
2041       for(ob = (OptionBox)firstSlave; ob; ob = (OptionBox)ob.nextSlave)
2042          if(eClass_IsDerived(ob._class, class(OptionBox)))
2043             ob.Load();
2044
2045       if(activeChild && activeChild.active)
2046       {
2047          Window control = activeChild;
2048          control.Deactivate();         
2049          control.Activate();
2050       }
2051    }
2052
2053    bool OnPostCreate()
2054    {
2055       objDir.editor.Activate();
2056       return true;
2057    }
2058 }
2059
2060 class LinkerTab : Tab
2061 {
2062    background = formColor;
2063    text = $"Linker";
2064
2065    Label labelTargetName { this, position = { 8, 8 }, labeledWindow = targetName };
2066    StringOptionBox targetName
2067    {
2068       this, position = { 8, 24 }, size = { 200, 22 };
2069       text = $"Target Name", hotKey = altN, option = OPTION(targetFileName);
2070    };
2071    
2072    Label labelTargetType { this, position = { 216, 8 }, labeledWindow = targetType };
2073    TargetTypeDB targetType
2074    {
2075       this, position = { 216, 24 }, size = { 120, 22 };
2076       text = $"Target Type", hotKey = altT, option = OPTION(targetType);
2077    };
2078    
2079    Label labelTargetDirectory { this, position = { 344, 8 }, labeledWindow = targetDirectory };
2080    PathOptionBox targetDirectory
2081    {
2082       this, size = { 270, 22 }, anchor = { left = 344, top = 24, right = 8 };
2083       hotKey = altR, text = $"Target Directory", option = OPTION(targetDirectory);
2084    };
2085
2086    Label labelLibraries { this, position = { 8, 50 }, labeledWindow = libraries };
2087    StringArrayOptionBox libraries
2088    {
2089       this, size = { 290, 22 }, anchor = { left = 8, top = 66, right = 8 };
2090       text = $"Additional Libraries", hotKey = altL, option = OPTION(libraries);
2091       configReplaces = true;
2092    };
2093
2094    Label labelLinkerOptions { this, position = { 8, 92 }, labeledWindow = linkerOptions };
2095    StringArrayOptionBox linkerOptions
2096    {
2097       this, size = { 290, 22 }, anchor = { left = 8, top = 108, right = 8 };
2098       text = $"Linker Options", hotKey = altO, option = OPTION(linkerOptions);
2099       configReplaces = true;
2100    };
2101
2102    BoolOptionBox console
2103    {
2104       this, position = { 8, 138 };
2105       text = $"Console Application", hotKey = altC, option = OPTION(console);
2106    };
2107
2108    BoolOptionBox compress
2109    {
2110       this, position = { 8, 162 };
2111       text = $"Compress", hotKey = altW, option = OPTION(compress);
2112    };
2113
2114    Label labelLibraryDirs { libraryDirs.editor, labeledWindow = libraryDirs, position = { 0, 6 }; };
2115    DirsArrayOptionBox libraryDirs
2116    {
2117       this, size = { 290, 22 }, anchor = { left = 8, top = 182, right = 8, bottom = 8 };
2118       text = $"Additional Library Directories", hotKey = altY, option = OPTION(libraryDirs);
2119    };
2120
2121    bool OnCreate()
2122    {
2123       ((BuildTab)master).SelectNode(project.topNode, true);
2124       return true;
2125    }
2126
2127    void LoadSettings()
2128    {
2129       OptionBox ob;
2130       for(ob = (OptionBox)firstSlave; ob; ob = (OptionBox)ob.nextSlave)
2131          if(eClass_IsDerived(ob._class, class(OptionBox)))
2132             ob.Load();
2133       compress.disabled = (config && config.options && config.options.debug == true) || project.topNode.options.debug == true;
2134
2135       if(activeChild && activeChild.active)
2136       {
2137          Window control = activeChild;
2138          control.Deactivate();         
2139          control.Activate();
2140       }
2141    }
2142 }
2143
2144 class BuilderTab : Tab
2145 {
2146    background = formColor;
2147    text = $"Builder";
2148
2149    Label labelPrebuildCommands { prebuildCommands.editor, labeledWindow = prebuildCommands, position = { 0, 6 }; };
2150    StringsArrayOptionBox prebuildCommands
2151    {
2152       this, size = { 290, 100 }, anchor = { left = 8, top = 8, right = 8, bottom = 0.5 };
2153       text = $"Pre-build Commands", hotKey = altE, option = OPTION(prebuildCommands);
2154    };
2155
2156    Label labelPostbuildCommands { postbuildCommands.editor, labeledWindow = postbuildCommands, position = { 0, 6 }; };
2157    StringsArrayOptionBox postbuildCommands
2158    {
2159       this, size = { 290 }, anchor = { left = 8, top = 0.5, right = 8, bottom = 8 };
2160       text = $"Post-build Commands", hotKey = altT, option = OPTION(postbuildCommands);
2161    };
2162
2163    void LoadSettings()
2164    {
2165       bool disabled = strlen(((BuildTab)master).selectedPlatformName) > 0;
2166       OptionBox ob;
2167       for(ob = (OptionBox)firstSlave; ob; ob = (OptionBox)ob.nextSlave)
2168          if(eClass_IsDerived(ob._class, class(OptionBox)))
2169             ob.Load();
2170
2171       if(activeChild && activeChild.active)
2172       {
2173          Window control = activeChild;
2174          control.Deactivate();         
2175          control.Activate();
2176       }
2177    }
2178 }