ide/Debugger/Project: Fixed wrong active configs being used for debugging
[sdk] / ide / src / project / ProjectView.ec
1 import "ide"
2
3 import "FileSystemIterator"
4
5 class ImportFolderFSI : NormalFileSystemIterator
6 {
7    ProjectView projectView;
8    Array<ProjectNode> stack { };
9
10    bool OnFolder(char * folderPath)
11    {
12       char name[MAX_LOCATION];
13       ProjectNode parentNode = stack.lastIterator.data;
14       ProjectNode folder;
15       GetLastDirectory(folderPath, name);
16       folder = parentNode.FindSpecial(name, false, true, true);
17       if(!folder)
18          folder = projectView.NewFolder(parentNode, name, false);
19       stack.Add(folder);
20       return true;
21    }
22
23    void OutFolder(char * folderPath, bool isRoot)
24    {
25       stack.lastIterator.Remove(); //stack.Remove();
26    }
27
28    bool OnFile(char * filePath)
29    {
30       ProjectNode parentNode = stack.lastIterator.data;
31       projectView.AddFile(parentNode, filePath, parentNode.isInResources, false);
32       return true;
33    }
34 }
35
36 static Array<FileFilter> fileFilters
37 { [
38    { $"eC/C/C++ Files (*.ec, *.eh, *.c, *.cpp, *.cc, *.cxx, *.h, *.hpp, *.hh, *.hxx)", "ec, eh, c, cpp, cc, cxx, h, hpp, hh, hxx" },
39    { $"eC/C/C++ Source Files (*.ec, *.c, *.cpp, *.cc, *.cxx)", "ec, eh, c, cpp, cc, cxx" },
40    { $"Header Files for eC/C/C++ (*.eh, *.h, *.hpp, *.hh, *.hxx)", "eh, h, hpp, hh, hxx" },
41    { $"All files", null }
42 ] };
43
44 static Array<FileFilter> resourceFilters
45 { [
46    { $"Image Files (*.jpg, *.jpeg, *.bmp, *.pcx, *.png,*.gif)", "jpg, jpeg, bmp, pcx, png, gif" },
47    { $"3D Studio Model Files (*.3ds)", "3ds" },
48    { $"All files", null }
49 ] };
50
51 static Array<FileType> fileTypes
52 { [
53    { $"Based on extension", null },
54    { $"Text",               "txt" },
55    { $"Image",              "jpg" },
56    { $"3D Studio Model",    "3ds" }
57 ] };
58
59 static Array<FileFilter> projectFilters
60 { [
61    { $"Project Files (*.epj)", ProjectExtension },
62    { $"Workspace Files (*.ews)", WorkspaceExtension }
63 ] };
64
65 static Array<FileType> projectTypes
66 { [
67    { $"Ecere IDE Project", ProjectExtension },
68    { $"Ecere IDE Workspace", WorkspaceExtension }
69 ] };
70
71 static char * iconNames[] = 
72 {
73    "<:ecere>mimeTypes/file.png",                   /*genFile*/
74    "<:ecere>mimeTypes/textEcereWorkspace.png",     /*ewsFile*/
75    "<:ecere>mimeTypes/textEcereProject.png",       /*epjFile*/
76    "<:ecere>places/folder.png",                    /*folder*/
77    "<:ecere>status/folderOpen.png",                /*openFolder*/
78    "<:ecere>mimeTypes/textEcereSource.png",        /*ecFile*/
79    "<:ecere>mimeTypes/textEcereHeader.png",        /*ehFile*/
80    "<:ecere>mimeTypes/textCSource.png",            /*cFile*/
81    "<:ecere>mimeTypes/textCHeader.png",            /*hFile*/
82    "<:ecere>mimeTypes/textC++Source.png",          /*cppFile*/
83    "<:ecere>mimeTypes/textC++Header.png",          /*hppFile*/
84    "<:ecere>mimeTypes/text.png",                   /*textFile*/
85    "<:ecere>mimeTypes/textHyperTextMarkup.png",    /*webFile*/
86    "<:ecere>mimeTypes/image.png",                  /*pictureFile*/
87    "<:ecere>status/audioVolumeHigh.png",           /*soundFile*/
88    "<:ecere>mimeTypes/package.png",                /*archiveFile*/
89    "<:ecere>mimeTypes/packageSoftware.png",        /*packageFile*/
90    "<:ecere>mimeTypes/packageOpticalDisc.png",     /*opticalMediaImageFile*/
91    "<:ecere>mimeTypes/file.png"                    /*mFile*/ //TODO: create icon for mfile
92 };
93
94 enum PrepareMakefileMethod { normal, force, forceExists };
95
96 enum BuildType { build, rebuild, relink, run, start, restart };
97 enum BuildState
98 {
99    none, buildingMainProject, buildingSecondaryProject, compilingFile;
100
101    property bool { get { return this != none; } }
102    //property bool actualBuild { get { return this == buildingMainProject || this == buildingSecondaryProject;  } }
103 };
104
105 class ProjectView : Window
106 {
107    isDocument = true;
108    //hasMinimize = true;
109    hasClose = true;
110    borderStyle = sizable;
111    hasHorzScroll = true;
112    hasVertScroll = true;
113    background = white;
114    size = { 300 };
115    anchor = Anchor { left = 0, top = 0, bottom = 0 };
116    menu = Menu { };
117    
118    //hasMinimize = true;
119    saveDialog = projectFileDialog;
120    
121    DataRow resourceRow;
122    BuildState buildInProgress;
123    BitmapResource icons[NodeIcons];
124    Project project;
125    Workspace workspace;
126    property Workspace workspace
127    {
128       set
129       {
130          if(workspace)
131          {
132             for(prj : workspace.projects)
133             {
134                DeleteNode(prj.topNode);
135             }
136             workspace.projects.Free();
137             ide.debugger.CleanUp();
138          }
139          workspace = value;
140          if(workspace)
141          {
142             fileDialog.currentDirectory = workspace.workspaceDir;
143             resourceFileDialog.currentDirectory = workspace.workspaceDir;
144             for(prj : workspace.projects)
145                AddNode(prj.topNode, null);
146             ide.statusBar.text = $"Generating Makefile & Dependencies...";
147             app.UpdateDisplay();
148             for(prj : workspace.projects)
149                prj.ModifiedAllConfigs(true, false, false, false);
150             ide.statusBar.text = $"Initializing Debugger"; app.UpdateDisplay();
151             ide.statusBar.text = null;
152             app.UpdateDisplay();
153          }
154       }
155       get { return workspace; }
156    }
157    
158    bool drawingInProjectSettingsDialog;
159    bool drawingInProjectSettingsDialogHeader;
160    ProjectSettings projectSettingsDialog;
161
162    bool stopBuild;
163
164    ProjectView()
165    {
166       NodeIcons c;
167       for(c = 0; c < NodeIcons::enumSize; c++)
168       {
169          icons[c] = BitmapResource { iconNames[c], alphaBlend = true };
170          AddResource(icons[c]);
171       }
172       fileList.AddField(DataField { dataType = class(ProjectNode), freeData = false, userData = this });
173    }
174
175    ~ProjectView()
176    {
177       DebugStop();
178       ide.DestroyTemporaryProjectDir();
179       if(project)
180       {
181          workspace.Free();
182          delete workspace;
183       }
184    }
185
186    ListBox fileList
187    {
188       multiSelect = true, fullRowSelect = false, hasVertScroll = true, hasHorzScroll = true;
189       borderStyle = deep, parent = this, collapseControl = true, treeBranches = true;
190       anchor = Anchor { left = 0, right = 0, top = 0 , bottom = 0 };
191
192       background = projectViewBackground;
193       foreground = projectViewText;
194       selectionColor = selectionColor, selectionText = selectionText;
195       stippleColor = skyBlue;
196
197       bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
198       {
199          if(!active) Update(null);
200          return ListBox::OnActivate(active, previous, goOnWithActivation, direct);
201       }
202       
203       bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
204       {
205          // Prevent the double click from reactivating the project view (returns false if we opened something)
206          return !OpenSelectedNodes();
207       }
208
209       bool NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods)
210       {
211          DataRow row = listBox.currentRow;
212          if(row)
213          {
214             ProjectNode node = (ProjectNode)row.tag;
215             if(node.type == NodeTypes::project || node.type == resources || node.type == file || node.type == folder)
216             {
217                bool buildMenuUnavailable = buildInProgress;
218                PopupMenu popupMenu;
219                Menu popupContent { };
220                
221                if(node.type == NodeTypes::project)
222                {
223                   //if(node == ((Project)workspace.projects.first).topNode)
224                   {
225                      MenuItem { popupContent, $"Build", b, NotifySelect = ProjectBuild }.disabled = buildMenuUnavailable;
226                      MenuItem { popupContent, $"Relink", l, NotifySelect = ProjectLink }.disabled = buildMenuUnavailable;
227                      MenuItem { popupContent, $"Rebuild", r, NotifySelect = ProjectRebuild }.disabled = buildMenuUnavailable;
228                      MenuItem { popupContent, $"Clean", c, NotifySelect = ProjectClean }.disabled = buildMenuUnavailable;
229                      MenuItem { popupContent, $"Regenerate Makefile", m, NotifySelect = ProjectRegenerate }.disabled = buildMenuUnavailable;
230                      MenuDivider { popupContent };
231                   }
232                   MenuItem { popupContent, $"New File...", l, Key { l, ctrl = true }, NotifySelect = ProjectNewFile };
233                   MenuItem { popupContent, $"New Folder...", n, Key { f, ctrl = true }, NotifySelect = ProjectNewFolder };
234                   MenuItem { popupContent, $"Import Folder...", i, NotifySelect = ProjectImportFolder };
235                   MenuItem { popupContent, $"Add Files to Project...", f, NotifySelect = ProjectAddFiles };
236                   MenuDivider { popupContent };
237                   MenuItem { popupContent, $"Add New Form...", o, NotifySelect = ProjectAddNewForm };
238                   // MenuItem { popupContent, "Add New Behavior Graph...", g, NotifySelect = ProjectAddNewGraph };
239                   MenuDivider { popupContent };
240                   if(node != ((Project)workspace.projects.first).topNode)
241                   {
242                      MenuItem { popupContent, $"Remove project from workspace", r, NotifySelect = ProjectRemove }.disabled = buildMenuUnavailable;
243                      MenuDivider { popupContent };
244                   }
245                   MenuItem { popupContent, $"Active Configuration...", s, Key { f5, alt = true } , NotifySelect = MenuConfig };
246                   MenuItem { popupContent, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
247                   MenuDivider { popupContent };
248                   MenuItem { popupContent, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
249                   MenuDivider { popupContent };
250                   MenuItem { popupContent, $"Save", v, Key { s, ctrl = true }, NotifySelect = ProjectSave }.disabled = !node.modified;
251                   MenuDivider { popupContent };
252                   MenuItem { popupContent, $"Properties...", p, Key { enter, alt = true }, NotifySelect = FileProperties };
253                }
254                else if(node.type == resources)
255                {
256                   MenuItem { popupContent, $"New File...", l, Key { l, ctrl = true }, NotifySelect = ProjectNewFile };
257                   MenuItem { popupContent, $"New Folder...", n, Key { f, ctrl = true }, NotifySelect = ProjectNewFolder };
258                   MenuItem { popupContent, $"Add Resources to Project...", f, NotifySelect = ResourcesAddFiles };
259                   MenuItem { popupContent, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
260                   MenuDivider { popupContent };
261                   MenuItem { popupContent, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
262                   MenuItem { popupContent, $"Properties...", p, Key { enter, alt = true }, NotifySelect = FileProperties };
263                }
264                else if(node.type == file)
265                {
266                   MenuItem { popupContent, $"Open", o, NotifySelect = FileOpenFile };
267                   MenuItem { popupContent, $"Compile", c, Key { f7, ctrl = true}, NotifySelect = FileCompile }.disabled = buildMenuUnavailable;
268                   MenuDivider { popupContent };
269                   MenuItem { popupContent, $"Remove", r, NotifySelect = FileRemoveFile };
270                   MenuDivider { popupContent };
271                   MenuItem { popupContent, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
272                   MenuDivider { popupContent };
273                   MenuItem { popupContent, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
274                   MenuItem { popupContent, $"Properties..", p, Key { enter, alt = true }, NotifySelect = FileProperties };
275                }
276                else if(node.type == folder)
277                {
278                   bool isInResources = node.isInResources;
279
280                   MenuItem { popupContent, $"New File...", l, Key { l, ctrl = true }, NotifySelect = ProjectNewFile };
281                   MenuItem { popupContent, $"New Folder...", n, Key { f, ctrl = true }, NotifySelect = ProjectNewFolder };
282                   MenuItem { popupContent, $"Import Folder...", i, NotifySelect = ProjectImportFolder };
283                   if(isInResources)
284                   {
285                      MenuItem { popupContent, $"Add Resources to Folder...", f, NotifySelect = ResourcesAddFiles };
286                   }
287                   else
288                   {
289                      MenuItem { popupContent, $"Add Files to Folder...", f, NotifySelect = ProjectAddFiles };
290                   }
291                   if(!isInResources)
292                   {
293                      MenuDivider { popupContent };
294                      MenuItem { popupContent, $"Add New Form...", o, NotifySelect = ProjectAddNewForm };
295                      MenuItem { popupContent, $"Add New Behavior Graph...", g, NotifySelect = ProjectAddNewGraph };
296                   }
297                   MenuDivider { popupContent };
298                   MenuItem { popupContent, $"Remove", r, NotifySelect = FileRemoveFile };
299                   MenuDivider { popupContent };
300                   MenuItem { popupContent, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
301                   MenuDivider { popupContent };
302                   MenuItem { popupContent, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
303                   MenuItem { popupContent, $"Properties...", p, Key { enter, alt = true }, NotifySelect = FileProperties };
304                }
305
306                popupMenu = 
307                {
308                   master = this, menu = popupContent;
309                   position = {
310                      x + clientStart.x + absPosition.x - app.desktop.position.x, 
311                      y + clientStart.y + absPosition.y - app.desktop.position.y };
312                };
313                popupMenu.Create();
314             }
315          }
316          return true;
317       }
318
319       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
320       {
321          if(row)
322          {
323             ProjectNode node = (ProjectNode)row.tag;
324             switch(key)
325             {
326                case altEnter: case Key { keyPadEnter, alt = true }:
327                {
328                   NodeProperties { parent = parent, master = this, 
329                      position = { position.x + 100, position.y + 100 }, node = node }.Create();
330                   return false;
331                }
332                case enter: case keyPadEnter:
333                {
334                   ProjectNode resNode;
335                   for(resNode = node.parent; resNode; resNode = resNode.parent)
336                      if(resNode.type == resources)
337                         break;
338                   if(node.type == project || (node.type == folder && !resNode))
339                   {
340                      AddFiles(false);
341                      return false;
342                   }
343                   else if(node.type == resources || node.type == folder)
344                   {
345                      AddFiles(true);
346                      return false;
347                   }
348                   break;
349                }
350                case ctrlI:
351                {
352                   if(node.type == project || node.type == folder || node.type == resources)
353                   {
354                      ImportFolder(node);
355                      return false;
356                   }
357                   break;
358                }
359                case ctrlF:
360                {
361                   if(node.type == project || node.type == folder || node.type == resources)
362                   {
363                      NewFolder(node, null, true);
364                      return false;
365                   }
366                   break;
367                }
368                case ctrlF7:
369                {
370                   if(node.type == file)
371                   {
372                      FileCompile(null, (Modifiers)key);
373                      return false;
374                   }
375                   break;
376                }
377                case Key { space, shift = true }:
378                case space:
379                {
380                   if(node.type == NodeTypes::project)
381                   {
382                      Project prj = null;
383                      for(p : workspace.projects)
384                      {
385                         if(p.topNode == node)
386                         {
387                            prj = p;
388                            break;
389                         }
390                      }
391                      prj.RotateActiveConfig(!key.shift);
392                      if(prj == project)
393                         ide.AdjustMenus();
394                      return false;
395                   }
396                   break;
397                }
398             }
399          }
400          switch(key)
401          {
402             case enter: case keyPadEnter:  OpenSelectedNodes();   break;
403             case del:                      RemoveSelectedNodes(); break;
404             case escape:                      
405             {
406                Window activeClient = ide.activeClient;
407                if(activeClient)
408                   activeClient.Activate();
409                else
410                   ide.RepositionWindows(true);
411                break;
412             }
413          }
414          return true;
415       }
416
417       bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
418       {
419          ProjectNode node = (ProjectNode)row.tag;
420          if(node.type == folder)
421             node.icon = collapsed ? folder : openFolder;
422          return true;
423       }
424    };
425
426    FileDialog importFileDialog { autoCreate = false, type = selectDir, text = $"Import Folder" };
427    FileDialog projectFileDialog
428    {
429       autoCreate = false, filters = projectFilters.array, sizeFilters = projectFilters.count * sizeof(FileFilter);
430       types = projectTypes.array, sizeTypes = projectTypes.count * sizeof(FileType);
431    };
432    FileDialog fileDialog
433    {
434       autoCreate = false, mayNotExist = true, filters = fileFilters.array, sizeFilters = fileFilters.count * sizeof(FileFilter);
435       types = fileTypes.array, sizeTypes = fileTypes.count * sizeof(FileType);
436    };
437    FileDialog resourceFileDialog
438    {
439       autoCreate = false, mayNotExist = true, filters = resourceFilters.array, sizeFilters = resourceFilters.count * sizeof(FileFilter);
440       types = fileTypes.array, sizeTypes = fileTypes.count * sizeof(FileType);
441    };
442
443    Menu fileMenu { menu, $"File", f };
444    MenuItem { fileMenu, $"Save", s, Key { s, ctrl = true }, NotifySelect = MenuFileSave };
445    // MenuItem { fileMenu, "Save As...", a, NotifySelect = MenuFileSaveAs };
446
447    bool OnClose(bool parentClosing)
448    {
449       if(!parentClosing && visible)
450       {
451          visible = false;
452          return false;
453       }
454       if(buildInProgress)
455          return false;
456       return true;
457    }
458
459    void OnDestroy(void)
460    {
461       //if(ide.findInFilesDialog && ide.findInFilesDialog.created && ide.findInFilesDialog.mode != directory)
462       //   ide.findInFilesDialog.SearchStop();
463
464       ide.outputView.buildBox.Clear();
465       ide.outputView.debugBox.Clear();
466       //ide.outputView.findBox.Clear();
467       ide.callStackView.Clear();
468       ide.watchesView.Clear();
469       ide.threadsView.Clear();
470       ide.breakpointsView.Clear();
471       ide.outputView.ShowClearSelectTab(find); // why this? 
472    }
473
474    bool OnSaveFile(char * fileName)
475    {
476       for(prj : ide.workspace.projects)
477       {
478          if(prj.topNode.modified && prj.Save(prj.filePath))
479          {
480             // ProjectUpdateMakefileForAllConfigs(prj, true, true);
481             prj.topNode.modified = false;
482          }
483       }
484       modifiedDocument = false;
485       Update(null);
486       return true;
487    }
488
489    bool IsModuleInProject(char * filePath)
490    {
491       char moduleName[MAX_FILENAME]; //, modulePath[MAX_LOCATION];
492       GetLastDirectory(filePath, moduleName);
493       return project.topNode.Find(moduleName, false) != null;
494    }
495
496    bool GetRelativePath(char * filePath, char * relativePath)
497    {
498       return project.GetRelativePath(filePath, relativePath);
499    }
500
501    ProjectNode GetNodeFromWindow(Window document, Project project)
502    {
503       if(document.fileName)
504       {
505          char winFileName[MAX_LOCATION];
506          char * documentFileName = GetSlashPathBuffer(winFileName, document.fileName);
507          for(p : ide.workspace.projects)
508          {
509             Project prj = project ? project : p;
510             ProjectNode node;
511             char moduleName[MAX_FILENAME], modulePath[MAX_LOCATION];
512             GetLastDirectory(document.fileName, moduleName);
513
514             if((node = prj.topNode.Find(moduleName, false)))
515             {
516                strcpy(modulePath, prj.topNode.path);
517                PathCatSlash(modulePath, node.path);
518                PathCatSlash(modulePath, node.name);
519                if(!fstrcmp(documentFileName, modulePath))
520                {
521                   return node;
522                }
523             }
524             if(project) break;
525          }
526       }
527       return null;
528    }
529
530    //                          ((( UTILITY FUNCTIONS )))
531    //
532    //  ************************************************************************
533    //  *** These methods below are part of a sequence of events, and as     ***
534    //  *** such they must be passed the current compiler and project config ***
535    //  ************************************************************************
536    bool DisplayCompiler(CompilerConfig compiler, bool cleanLog)
537    {
538       ShowOutputBuildLog(cleanLog);
539       ide.outputView.buildBox.Logf($"%s Compiler\n", compiler ? compiler.name : $"{problem with compiler selection}");
540    }
541
542    bool ProjectPrepareForToolchain(Project project, PrepareMakefileMethod method, bool cleanLog, bool displayCompiler,
543       CompilerConfig compiler, ProjectConfig config)
544    {
545       bool isReady = true;
546       char message[MAX_F_STRING];
547       LogBox logBox = ide.outputView.buildBox;
548
549       ShowOutputBuildLog(cleanLog);
550
551       if(displayCompiler)
552          DisplayCompiler(compiler, false);
553
554       ProjectPrepareMakefile(project, method, false, false, compiler, config);
555       return true;
556    }
557
558    bool ProjectPrepareMakefile(Project project, PrepareMakefileMethod method, bool cleanLog, bool displayCompiler,
559       CompilerConfig compiler, ProjectConfig config)
560    {
561       char makefilePath[MAX_LOCATION];
562       char makefileName[MAX_LOCATION];
563       bool exists;
564       LogBox logBox = ide.outputView.buildBox;
565       
566       ShowOutputBuildLog(cleanLog);
567
568       if(displayCompiler)
569          DisplayCompiler(compiler, false);
570
571       strcpy(makefilePath, project.topNode.path);
572       project.CatMakeFileName(makefileName, compiler, config);
573       PathCatSlash(makefilePath, makefileName);
574
575       exists = FileExists(makefilePath);
576       if((method == normal && (!exists || config.makingModified/*|| project.topNode.modified*/)) ||
577             (method == forceExists && exists) || 
578             method == force) // || config.makingModified || makefileDirty
579       {
580          char * reason;
581          char * action;
582          ide.statusBar.text = $"Generating Makefile & Dependencies..."; // Dependencies?
583          app.UpdateDisplay();
584          
585          if((method == normal && !exists) || (method == force && !exists))
586             action = $"Generating ";
587          else if(method == force)
588             action = $"Regenerating ";
589          else if(method == normal || method == forceExists)
590             action = $"Updating ";
591          else
592             action = "";
593          if(!exists)
594             reason = $"Makefile doesn't exist. ";
595          else if(project.topNode.modified)
596             reason = $"Project has been modified. ";
597          else
598             reason = "";
599
600          //logBox.Logf("%s\n", makefileName);
601          logBox.Logf($"%s - %s%smakefile for %s config...\n", makefileName, reason, action, GetConfigName(config));
602          project.GenerateMakefile(null, false, null, compiler, config);
603
604          ide.statusBar.text = null;
605          app.UpdateDisplay();
606          return true;
607       }
608       return false;
609    }
610    
611    bool BuildInterrim(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config)
612    {
613       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
614       {
615          ide.outputView.buildBox.Logf($"Building project %s using the %s configuration...\n", prj.name, GetConfigName(config));
616          return Build(prj, buildType, compiler, config);
617       }
618       return false;
619    }
620
621    bool Build(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config)
622    {
623       bool result = true;
624       Window document;
625
626       stopBuild = false;
627       for(document = master.firstChild; document; document = document.next)
628       {
629          if(document.modifiedDocument)
630          {
631             ProjectNode node = GetNodeFromWindow(document, prj);
632             if(node && !document.MenuFileSave(null, 0))
633             {
634                result = false;
635                break;
636             }
637          }
638       }
639       if(result)
640       {
641          DirExpression targetDir = prj.GetTargetDir(compiler, config);
642
643          // TOFIX: DebugStop is being abused and backfiring on us.
644          //        It's supposed to be the 'Debug/Stop' item, not unloading executable or anything else
645
646          //        configIsInDebugSession seems to be used for two OPPOSITE things:
647          //        If we're debugging another config, we need to unload the executable!
648          //        In building, we want to stop if we're debugging the 'same' executable
649          if(buildType != run) ///* && prj == project*/ && prj.configIsInDebugSession)
650          {
651             if(buildType == start || buildType == restart)
652             {
653                if(ide.debugger && ide.debugger.isPrepared)
654                {
655                   DebugStop();
656                }
657             }
658             else
659             {
660                if(ide.project == prj && ide.debugger && ide.debugger.prjConfig == config && ide.debugger.isPrepared)
661                {
662                   DebugStop();
663                }
664             }
665          }
666          
667          // TODO: Disabled until problems fixed... is it fixed?
668          if(buildType == rebuild || (config && config.compilingModified))
669             prj.Clean(compiler, config);
670          else
671          {
672             if(buildType == relink || (config && config.linkingModified))
673             {
674                char target[MAX_LOCATION];
675
676                strcpy(target, prj.topNode.path);
677                PathCat(target, targetDir.dir);
678                prj.CatTargetFileName(target, compiler, config);
679                if(FileExists(target))
680                   DeleteFile(target);
681             }
682             if(config && config.symbolGenModified)
683             {
684                DirExpression objDir = prj.GetObjDir(compiler, config);
685                char fileName[MAX_LOCATION];
686                char moduleName[MAX_FILENAME];
687                strcpy(fileName, prj.topNode.path);
688                PathCatSlash(fileName, objDir.dir);
689                strcpy(moduleName, prj.moduleName);
690                strcat(moduleName, ".main.ec");
691                PathCatSlash(fileName, moduleName);
692                if(FileExists(fileName))
693                   DeleteFile(fileName);
694                ChangeExtension(fileName, "c", fileName);
695                if(FileExists(fileName))
696                   DeleteFile(fileName);
697                ChangeExtension(fileName, "o", fileName);
698                if(FileExists(fileName))
699                   DeleteFile(fileName);
700
701                delete objDir;
702             }
703          }
704          buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
705          ide.AdjustBuildMenus();
706          ide.AdjustDebugMenus();
707
708          result = prj.Build(buildType == run, null, compiler, config);
709
710          if(config)
711          {
712             config.compilingModified = false;
713             if(!ide.ShouldStopBuild())
714                config.linkingModified = false;
715
716             config.symbolGenModified = false;
717          }
718          buildInProgress = none;
719          ide.AdjustBuildMenus();
720          ide.AdjustDebugMenus();
721
722          ide.workspace.modified = true;
723
724          delete targetDir;
725       }
726       return result;
727    }
728
729    //                          ((( USER ACTIONS )))
730    //
731    //  ************************************************************************
732    //  *** Methods below should atomically start a process, and as such     ***
733    //  *** they can query compiler and config directly from ide and project ***
734    //  *** but ONLY ONCE!!!                                                 ***
735    //  ************************************************************************
736
737    bool ProjectBuild(MenuItem selection, Modifiers mods)
738    {
739       Project prj = project;
740       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
741       ProjectConfig config;
742       if(selection || !ide.activeClient || activeClient == this)
743       {
744          DataRow row = fileList.currentRow;
745          ProjectNode node = row ? (ProjectNode)row.tag : null;
746          if(node) prj = node.project;
747       }
748       // Added this code here until dependencies work:
749       else //if(ide.activeClient)
750       {
751          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
752          if(node)
753             prj = node.project;
754       }
755       config = prj.config;
756       if(/*prj != project || */!prj.GetConfigIsInDebugSession(config) || !ide.DontTerminateDebugSession($"Project Build"))
757       {
758          BuildInterrim(prj, build, compiler, config);
759       }
760       delete compiler;
761       return true;
762    }
763
764    bool ProjectLink(MenuItem selection, Modifiers mods)
765    {
766       Project prj = project;
767       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
768       ProjectConfig config;
769       if(selection || !ide.activeClient || activeClient == this)
770       {
771          DataRow row = fileList.currentRow;
772          ProjectNode node = row ? (ProjectNode)row.tag : null;
773          if(node) prj = node.project;
774       }
775       // Added this code here until dependencies work:
776       else //if(ide.activeClient)
777       {
778          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
779          if(node)
780             prj = node.project;
781       }
782       config = prj.config;
783       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
784       {
785          ide.outputView.buildBox.Logf($"Relinking project %s using the %s configuration...\n", prj.name, GetConfigName(config));
786          if(config)
787             config.linkingModified = true;
788          Build(prj, relink, compiler, config);
789       }
790       delete compiler;
791       return true;
792    }
793
794    bool ProjectRebuild(MenuItem selection, Modifiers mods)
795    {
796       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
797       Project prj = GetSelectedProject((bool)selection);
798       ProjectConfig config = prj.config;
799       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
800       {
801          ide.outputView.buildBox.Logf($"Rebuilding project %s using the %s configuration...\n", prj.name, GetConfigName(config));
802          /*if(config)
803          {
804             config.compilingModified = true;
805             config.makingModified = true;
806          }*/ // -- should this still be used depite the new solution of BuildType?
807          Build(prj, rebuild, compiler, config);
808       }
809       delete compiler;
810       return true;
811    }
812
813    bool ProjectClean(MenuItem selection, Modifiers mods)
814    {
815       Project prj = GetSelectedProject((bool)selection);
816       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
817       ProjectConfig config = prj.config;
818       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
819       {
820          ide.outputView.buildBox.Logf($"Cleaning project %s using the %s configuration...\n", prj.name, GetConfigName(config));
821          
822          buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
823          ide.AdjustBuildMenus();
824
825          prj.Clean(compiler, config);
826          buildInProgress = none;
827          ide.AdjustBuildMenus();
828       }
829       delete compiler;
830       return true;
831    }
832
833    bool ProjectRegenerate(MenuItem selection, Modifiers mods)
834    {
835       Project prj = project;
836       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
837       if(selection || !ide.activeClient || activeClient == this)
838       {
839          DataRow row = fileList.currentRow;
840          ProjectNode node = row ? (ProjectNode)row.tag : null;
841          if(node)
842             prj = node.project;
843       }
844       // Added this code here until dependencies work:
845       else //if(ide.activeClient)
846       {
847          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
848          if(node)
849             prj = node.project;
850       }
851
852       ProjectPrepareMakefile(prj, force, true, true, compiler, prj.config);
853       delete compiler;
854       return true;
855    }
856
857    void Compile(ProjectNode node)
858    {
859       char fileName[MAX_LOCATION];
860       char extension[MAX_EXTENSION];
861       Window document;
862       Project prj = node.project;
863       ProjectConfig config = prj.config;
864       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
865       DirExpression objDir = prj.GetObjDir(compiler, config);
866
867       strcpy(fileName, prj.topNode.path);
868       PathCatSlash(fileName, objDir.dir);
869       PathCatSlash(fileName, node.name);
870       StripExtension(fileName);
871       strcat(fileName, ".o");
872       if(FileExists(fileName))
873          DeleteFile(fileName);
874
875       GetExtension(node.name, extension);
876       if(!strcmp(extension, "ec"))
877       {
878          // Delete generated C file
879          strcpy(fileName, prj.topNode.path);
880          PathCat(fileName, objDir.dir);
881          PathCat(fileName, node.name);
882          StripExtension(fileName);
883          strcat(fileName, ".c");
884          if(FileExists(fileName))
885             DeleteFile(fileName);
886
887          // Delete symbol file
888          strcpy(fileName, prj.topNode.path);
889          PathCat(fileName, node.path);
890          PathCat(fileName, node.name);
891          StripExtension(fileName);
892          strcat(fileName, ".sym");
893          if(FileExists(fileName))
894             DeleteFile(fileName);
895       }
896
897       stopBuild = false;
898
899       // Check if we have to save
900       strcpy(fileName, prj.topNode.path);
901       PathCatSlash(fileName, node.path);
902       PathCatSlash(fileName, node.name);
903       for(document = ide.firstChild; document; document = document.next)
904       {
905          if(document.modifiedDocument)
906          {
907             char documentFileName[MAX_LOCATION];
908             if(!fstrcmp(GetSlashPathBuffer(documentFileName, document.fileName), fileName))
909                if(!document.MenuFileSave(null, 0))
910                   return;
911          }
912       }
913
914       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
915       {
916          if(!node.GetIsExcluded(config))
917          {
918             buildInProgress = compilingFile;
919             ide.AdjustBuildMenus();
920
921             //ide.outputView.ShowClearSelectTab(build);
922             // this stuff doesn't even appear
923             //ide.outputView.buildBox.Logf($"%s Compiler\n", compiler.name);
924             if(config)
925                ide.outputView.buildBox.Logf($"Compiling single file %s in project %s using the %s configuration...\n", node.name, prj.name, config.name);
926             else
927                ide.outputView.buildBox.Logf($"Compiling single file %s in project %s...\n", node.name, prj.name);
928
929             prj.Compile(node, compiler, config);
930             buildInProgress = none;
931             ide.AdjustBuildMenus();
932          }
933          else
934             ide.outputView.buildBox.Logf($"File %s is excluded from current build configuration.\n", node.name);
935       }
936       delete objDir;
937       delete compiler;
938    }
939
940    bool ProjectNewFile(MenuItem selection, Modifiers mods)
941    {
942       DataRow row = fileList.currentRow;
943       if(row)
944       {
945          char fileName[1024];
946          char filePath[MAX_LOCATION];
947          ProjectNode parentNode = (ProjectNode)row.tag;
948          ProjectNode n, fileNode;
949          parentNode.GetFileSysMatchingPath(filePath);
950          MakePathRelative(filePath, parentNode.project.topNode.path, filePath);
951          for(n = parentNode; n && n != parentNode.project.resNode; n = n.parent);
952          sprintf(fileName, $"Untitled %d", documentID);
953          fileNode = AddFile(parentNode, fileName, (bool)n, true);
954          fileNode.path = CopyUnixPath(filePath);
955          if(fileNode)
956          {
957             NodeProperties nodeProperties
958             {
959                parent, this;
960                position = { position.x + 100, position.y + 100 };
961                mode = newFile;
962                node = fileNode;
963             };
964             nodeProperties.Create(); // not modal?
965          }
966       }
967       return true;
968    }
969
970    bool ProjectNewFolder(MenuItem selection, Modifiers mods)
971    {
972       DataRow row = fileList.currentRow;
973       if(row)
974       {
975          ProjectNode parentNode = (ProjectNode)row.tag;
976          NewFolder(parentNode, null, true);
977       }
978       return true;
979    }
980
981    bool ResourcesAddFiles(MenuItem selection, Modifiers mods)
982    {
983       AddFiles(true);
984       return true;
985    }
986
987    bool ProjectAddFiles(MenuItem selection, Modifiers mods)
988    {
989       AddFiles(false);
990       return true;
991    }
992
993    bool ProjectImportFolder(MenuItem selection, Modifiers mods)
994    {
995       DataRow row = fileList.currentRow;
996       if(row)
997       {
998          ProjectNode toNode = (ProjectNode)row.tag;
999          ImportFolder(toNode);
1000       }
1001       return true;
1002    }
1003
1004    bool ProjectAddNewForm(MenuItem selection, Modifiers mods)
1005    {
1006       CodeEditor codeEditor = CreateNew("Form", "form", "Window", null);
1007       codeEditor.EnsureUpToDate();
1008       return true;
1009    }
1010
1011    bool ProjectAddNewGraph(MenuItem selection, Modifiers mods)
1012    {
1013       CodeEditor codeEditor = CreateNew("Graph", "graph", "Block", null);
1014       if(codeEditor)
1015          codeEditor.EnsureUpToDate();
1016       return true;
1017    }
1018
1019    bool ProjectRemove(MenuItem selection, Modifiers mods)
1020    {
1021       DataRow row = fileList.currentRow;
1022       if(row)
1023       {
1024          ProjectNode node = (ProjectNode)row.tag;
1025          if(node.type == project)
1026             RemoveSelectedNodes();
1027       }
1028       return true;
1029    }
1030
1031    bool ProjectUpdateMakefileForAllConfigs(Project project, bool cleanLog, bool displayCompiler)
1032    {
1033       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1034       ShowOutputBuildLog(cleanLog);
1035
1036       if(displayCompiler)
1037          DisplayCompiler(compiler, false);
1038       
1039       for(config : project.configurations)
1040       {
1041          ProjectPrepareMakefile(project, forceExists, false, false,
1042             compiler, config);
1043       }
1044
1045       ide.Update(null);
1046       delete compiler;
1047    }
1048
1049    bool MenuConfig(MenuItem selection, Modifiers mods)
1050    {
1051       if(ProjectActiveConfig { parent = parent.parent, master = parent, project = project }.Modal() == ok)
1052          ide.AdjustMenus();
1053       return true;
1054    }
1055
1056    bool MenuCompiler(MenuItem selection, Modifiers mods)
1057    {
1058       ActiveCompilerDialog compilerDialog
1059       {
1060          parent = parent.parent, master = parent;
1061          ideSettings = ideSettings, workspaceActiveCompiler = ide.workspace.compiler;
1062       };
1063       incref compilerDialog;
1064       if(compilerDialog.Modal() == ok && strcmp(compilerDialog.workspaceActiveCompiler, ide.workspace.compiler))
1065       {
1066          ide.workspace.compiler = compilerDialog.workspaceActiveCompiler;
1067          for(prj : ide.workspace.projects)
1068             ide.projectView.ProjectUpdateMakefileForAllConfigs(prj, true, true);
1069       }
1070       delete compilerDialog;
1071       return true;
1072    }
1073
1074    bool MenuSettings(MenuItem selection, Modifiers mods)
1075    {
1076       ProjectNode node = GetSelectedNode(true);
1077       Project prj = node ? node.project : project;
1078       projectSettingsDialog = ProjectSettings { parent = parent.parent, master = parent, project = prj, projectNode = node };
1079       projectSettingsDialog.Modal();
1080
1081       Update(null);
1082       ide.AdjustMenus();
1083       return true;
1084    }
1085
1086    bool FileProperties(MenuItem selection, Modifiers mods)
1087    {
1088       DataRow row = fileList.currentRow;
1089       if(row)
1090       {
1091          ProjectNode node = (ProjectNode)row.tag;
1092          NodeProperties { parent = parent, master = this, node = node, 
1093                position = { position.x + 100, position.y + 100 } }.Create();
1094       }
1095       return true;
1096    }
1097
1098    bool FileOpenFile(MenuItem selection, Modifiers mods)
1099    {
1100       OpenSelectedNodes();
1101       return true;
1102    }
1103
1104    bool FileRemoveFile(MenuItem selection, Modifiers mods)
1105    {
1106       RemoveSelectedNodes();
1107       return true;
1108    }
1109
1110    bool FileCompile(MenuItem selection, Modifiers mods)
1111    {
1112       DataRow row = fileList.currentRow;
1113       if(row)
1114       {
1115          ProjectNode node = (ProjectNode)row.tag;
1116          Compile(node);
1117       }
1118       return true;
1119    }
1120
1121    Project GetSelectedProject(bool useSelection)
1122    {
1123       Project prj = project;
1124       if(useSelection)
1125       {
1126          DataRow row = fileList.currentRow;
1127          ProjectNode node = row ? (ProjectNode)row.tag : null;
1128          if(node)
1129             prj = node.project;
1130       }
1131       return prj;
1132    }
1133    
1134    ProjectNode GetSelectedNode(bool useSelection)
1135    {
1136       ProjectNode node = null;
1137       if(useSelection)
1138       {
1139          DataRow row = fileList.currentRow;
1140          if(row)
1141             node = (ProjectNode)row.tag;
1142       }
1143       return node;
1144    }
1145
1146    bool MenuBrowseFolder(MenuItem selection, Modifiers mods)
1147    {
1148       char folder[MAX_LOCATION];
1149       Project prj;
1150       ProjectNode node = GetSelectedNode(true);
1151       if(!node)
1152          node = project.topNode;
1153       prj = node.project;
1154
1155       strcpy(folder, prj.topNode.path);
1156       if(node != prj.topNode)
1157          PathCatSlash(folder, node.path);
1158       ShellOpen(folder);
1159    }
1160
1161    bool Run(MenuItem selection, Modifiers mods)
1162    {
1163       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1164       ProjectConfig config = project.config;
1165       String args = new char[maxPathLen];
1166       args[0] = '\0';
1167       if(ide.workspace.commandLineArgs)
1168          //ide.debugger.GetCommandLineArgs(args);
1169          strcpy(args, ide.workspace.commandLineArgs);
1170       if(ide.debugger.isActive)
1171          project.Run(args, compiler, config);
1172       /*else if(config.targetType == sharedLibrary || config.targetType == staticLibrary)
1173          MessageBox { master = ide, type = ok, text = "Run", contents = "Shared and static libraries cannot be run like executables." }.Modal();*/
1174       else if(BuildInterrim(project, run, compiler, config))
1175          project.Run(args, compiler, config);
1176       delete args;
1177       delete compiler;
1178       return true;
1179    }
1180
1181    bool DebugStart()
1182    {
1183       bool result = false;
1184       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1185       ProjectConfig config = project.config;
1186       TargetTypes targetType = project.GetTargetType(config);
1187       if(targetType == sharedLibrary || targetType == staticLibrary)
1188          MessageBox { master = ide, type = ok, text = $"Run", contents = $"Shared and static libraries cannot be run like executables." }.Modal();
1189       else if(project.GetCompress(config))
1190          MessageBox { master = ide, text = $"Starting Debug", contents = $"Debugging compressed applications is not supported\n" }.Modal();
1191       else if(project.GetDebug(config) ||
1192          MessageBox { master = ide, type = okCancel, text = $"Starting Debug", contents = $"Attempting to debug non-debug configuration\nProceed anyways?" }.Modal() == ok)
1193       {
1194          if(/*!IsProjectModified() ||*/ BuildInterrim(project, start, compiler, config))
1195          {
1196             if(compiler.type.isVC)
1197             {
1198                //bool result = false;
1199                char oldwd[MAX_LOCATION];
1200                PathBackup pathBackup { };
1201                char command[MAX_LOCATION];
1202
1203                ide.SetPath(false, compiler, config);
1204                
1205                GetWorkingDir(oldwd, sizeof(oldwd));
1206                ChangeWorkingDir(project.topNode.path);
1207
1208                sprintf(command, "%s /useenv %s.sln /projectconfig \"%s|Win32\" /command \"%s\"" , "devenv", project.name, config.name, "Debug.Start");
1209                //ide.outputView.buildBox.Logf("command: %s\n", command);
1210                Execute(command);
1211                ChangeWorkingDir(oldwd);
1212
1213                delete pathBackup;
1214             }
1215             else
1216             {
1217                ide.debugger.Start(compiler, config);
1218                result = true;
1219             }
1220          }
1221       }
1222       delete compiler;
1223       return result;      
1224    }
1225
1226    void GoToError(const char * line)
1227    {
1228       char * colon;
1229       
1230       while(isspace(*line)) line++;
1231       colon = strstr(line, ":");
1232
1233       {
1234          int lineNumber = 0;
1235          int col = 1;
1236          bool lookForLineNumber = true;
1237
1238          // deal with linking error
1239          if(colon && colon[1] == ' ')
1240          {
1241             colon = strstr(colon + 1, ":");
1242             if(colon && !colon[1])
1243             {
1244                colon = strstr(line, ":");
1245                lookForLineNumber = false;
1246             }
1247             else if(colon && !isdigit(colon[1]))
1248             {
1249                line = colon + 1;
1250                colon = strstr(line, ":");
1251             }
1252          }
1253          // Don't be mistaken by the drive letter colon
1254          if(colon && (colon[1] == '/' || colon[1] == '\\'))
1255             colon = strstr(colon + 1, ":");
1256          if(colon && lookForLineNumber)
1257          {
1258             char * comma;
1259             // MSVS Errors
1260             char * par = RSearchString(line, "(", colon - line, true, false);
1261             if(par && strstr(par, ")"))
1262                colon = par;
1263             else if((colon+1)[0] == ' ')
1264             {
1265                // NOTE: This is the same thing as saying 'colon = line'
1266                for( ; colon != line; colon--)
1267                   /*if(*colon == '(')
1268                      break*/;
1269             }
1270             lineNumber = atoi(colon + 1);
1271             /*
1272             comma = strchr(colon, ',');
1273             if(comma)
1274                col = atoi(comma+1);
1275             */
1276             comma = strchr(colon+1, ':');
1277             if(comma)
1278                col = atoi(comma+1);
1279          }
1280          
1281          {
1282             char moduleName[MAX_LOCATION], filePath[MAX_LOCATION];
1283             char * bracket;
1284             if(colon)
1285             {
1286                // Cut module name
1287                strncpy(moduleName, line, colon - line);
1288                moduleName[colon - line] = '\0';
1289             }
1290             else
1291                strcpy(moduleName, line);
1292
1293             // Remove stuff in brackets
1294             /*
1295             bracket = strstr(moduleName, "(");
1296             if(bracket) *bracket = '\0';
1297             */
1298             MakeSlashPath(moduleName);
1299
1300             if(!colon)
1301             {
1302                // Check if it's one of our modules
1303                ProjectNode node = project.topNode.Find(moduleName, false);
1304                if(node)
1305                {
1306                   strcpy(moduleName, node.path);
1307                   PathCatSlash(moduleName, node.name);
1308                }
1309                else
1310                   moduleName[0] = '\0';
1311             }
1312             if(moduleName[0])
1313             {
1314                CodeEditor codeEditor;
1315                strcpy(filePath, project.topNode.path);
1316                PathCatSlash(filePath, moduleName);
1317       
1318                codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal);
1319                if(!codeEditor)
1320                {
1321                   char name[MAX_LOCATION];
1322                   // TOFIX: Improve on this, don't use only filename, make a function
1323                   if(ide && ide.workspace)
1324                   {
1325                      for(prj : ide.workspace.projects)
1326                      {
1327                         if(prj.topNode.FindWithPath(moduleName, false))
1328                         {
1329                            strcpy(filePath, prj.topNode.path);
1330                            PathCatSlash(filePath, moduleName);
1331                            codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal);
1332                            if(codeEditor)
1333                               break;
1334                         }
1335                      }
1336                   }
1337                }
1338                if(codeEditor && lineNumber)
1339                {
1340                   EditBox editBox = codeEditor.editBox;
1341                   editBox.GoToLineNum(lineNumber - 1);
1342                   editBox.GoToPosition(editBox.line, lineNumber - 1, col ? (col - 1) : 0);
1343                }
1344             }
1345          }
1346       }
1347    }
1348
1349    bool OpenNode(ProjectNode node)
1350    {
1351       char filePath[MAX_LOCATION];
1352       node.GetFullFilePath(filePath);
1353       return ide.OpenFile(filePath, normal, true/*false Why was it opening hidden?*/, null, something, normal) ? true : false;
1354    }
1355
1356    void AddNode(ProjectNode node, DataRow addTo)
1357    {
1358       DataRow row = addTo ? addTo.AddRow() : fileList.AddRow();
1359
1360       row.tag = (int)node;
1361       node.row = row;
1362
1363       if(node.type == resources)
1364          resourceRow = row;
1365
1366       row.SetData(null, node);
1367
1368       if(node.files && node.files.first && node.parent && 
1369             !(!node.parent.parent && 
1370                (!strcmpi(node.name, "notes") || !strcmpi(node.name, "sources") || 
1371                   !strcmpi(node.name, "src") || !strcmpi(node.name, "tools"))))
1372          row.collapsed = true;
1373       else if(node.type == folder)
1374          node.icon = openFolder;
1375
1376       if(node.files)
1377       {
1378          for(child : node.files)
1379             AddNode(child, row);
1380       }
1381    }
1382
1383    void DeleteNode(ProjectNode projectNode)
1384    {
1385       if(projectNode.files)
1386       {
1387          ProjectNode child;
1388          while(child = projectNode.files.first)
1389             DeleteNode(child);
1390       }
1391       fileList.DeleteRow(projectNode.row);
1392       projectNode.Delete();
1393    }
1394
1395    bool ProjectSave(MenuItem selection, Modifiers mods)
1396    {
1397       DataRow row = fileList.currentRow;
1398       ProjectNode node = row ? (ProjectNode)row.tag : null;
1399       Project prj = node ? node.project : null;
1400       if(prj)
1401       {
1402          if(prj.Save(prj.filePath))
1403          {
1404             // ProjectUpdateMakefileForAllConfigs(prj, true, true);
1405             prj.topNode.modified = false;
1406             prj = null;
1407             for(p : ide.workspace.projects)
1408             {
1409                if(p.topNode.modified)
1410                { 
1411                   prj = p;
1412                   break;
1413                }
1414             }
1415             if(!prj)
1416                modifiedDocument = false;
1417             Update(null);
1418          }
1419       }
1420       return true;
1421    }
1422
1423    bool ShowOutputBuildLog(bool cleanLog)
1424    {
1425       OutputView output = ide.outputView;
1426       if(cleanLog)
1427          output.ShowClearSelectTab(build);
1428       else
1429       {
1430          output.SelectTab(build);
1431          output.Show();
1432       }
1433    }
1434
1435    bool DebugRestart()
1436    {
1437       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1438       ProjectConfig config = project.config;
1439
1440       bool result = false;
1441       if(/*!IsProjectModified() ||*/ BuildInterrim(project, restart, compiler, config))
1442       {
1443          // For Restart, compiler and config will only be used if for
1444          // whatever reason (if at all possible) the Debugger is in a
1445          // 'terminated' or 'none' state
1446          ide.debugger.Restart(compiler, config);
1447          result = true;
1448       }
1449
1450       delete compiler;
1451       return result;
1452    }
1453
1454    bool DebugResume()
1455    {
1456       ide.debugger.Resume();
1457       return true;
1458    }
1459
1460    bool DebugBreak()
1461    {
1462       ide.debugger.Break();
1463       return true;
1464    }
1465
1466    bool DebugStop()
1467    {
1468       ide.debugger.Stop();
1469       return true;
1470    }
1471
1472    bool DebugStepInto()
1473    {
1474       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1475       ProjectConfig config = project.config;
1476
1477       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config)))
1478          ide.debugger.StepInto(compiler, config);
1479       delete compiler;
1480       return true;
1481    }
1482
1483    bool DebugStepOver(bool skip)
1484    {
1485       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1486       ProjectConfig config = project.config;
1487
1488       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config)))
1489          ide.debugger.StepOver(compiler, config, skip);
1490
1491       delete compiler;
1492       return true;
1493    }
1494
1495    bool DebugStepOut(bool skip)
1496    {
1497       ide.debugger.StepOut(skip);
1498       return true;
1499    }
1500
1501    void ImportFolder(ProjectNode toNode)
1502    {
1503       if(toNode)
1504       {
1505          //bool isFolder = toNode.type == folder;
1506          //bool isRes = toNode.isInResources;
1507          
1508          FileDialog fileDialog = importFileDialog;
1509          fileDialog.master = parent;
1510          if(fileDialog.Modal() == ok)
1511          {
1512             ImportFolderFSI fsi { projectView = this };
1513             fsi.stack.Add(toNode);
1514             fsi.Iterate(fileDialog.filePath);
1515             delete fsi;
1516          }
1517       }
1518    }
1519
1520    ProjectNode NewFolder(ProjectNode parentNode, char * name, bool showProperties)
1521    {
1522       if(parentNode)
1523       {
1524          ProjectNode folderNode;
1525          Project prj = parentNode.project;
1526          int c;
1527          ProjectNode after = null;
1528          for(node : parentNode.files)
1529          {
1530             if(node.type != folder)
1531                break;
1532             after = node;
1533          }
1534          
1535          if(name && name[0])
1536             folderNode = parentNode.Add(prj, name, after, folder, folder, true);
1537          else
1538          {
1539             for(c = 0; c < 100; c++)
1540             {
1541                char string[16];
1542                sprintf(string, c ? "New Folder (%d)" : "New Folder", c);
1543                if((folderNode = parentNode.Add(prj, string, after, folder, folder, true)))
1544                   break;
1545             }
1546          }
1547
1548          if(folderNode)
1549          {
1550             NodeProperties nodeProperties;
1551             if(!showProperties)
1552             {
1553                modifiedDocument = true;
1554                prj.topNode.modified = true;
1555             }
1556             Update(null);
1557             folderNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
1558             folderNode.row.tag = (int)folderNode;
1559                
1560             folderNode.row.SetData(null, folderNode);
1561             fileList.currentRow = folderNode.row;
1562             
1563             if(showProperties)
1564             {
1565                nodeProperties = NodeProperties
1566                {
1567                   parent, this, mode = newFolder, node = folderNode;
1568                   position = { position.x + 100, position.y + 100 };
1569                };
1570                nodeProperties.Create();   // Modal?
1571             }
1572             return folderNode;
1573          }
1574       }
1575       return null;
1576    }
1577
1578    void AddFiles(bool resources)
1579    {
1580       FileDialog fileDialog = (!resources) ? this.fileDialog : resourceFileDialog;
1581       fileDialog.type = multiOpen;
1582       fileDialog.text = !resources ? $"Add Files to Project" : $"Add Resources to Project";
1583       fileDialog.master = parent;
1584
1585       if(fileDialog.Modal() == ok)
1586       {
1587          int c;
1588          DataRow row = fileList.currentRow;
1589          ProjectNode parentNode = (ProjectNode)row.tag;
1590          bool addFailed = false;
1591          int numSelections = fileDialog.numSelections;
1592          char ** multiFilePaths = fileDialog.multiFilePaths;
1593
1594          Array<String> nameConflictFiles { };
1595
1596          for(c = 0; c < numSelections; c++)
1597          {
1598             char * filePath = multiFilePaths[c];
1599             FileAttribs exists = FileExists(filePath);
1600             bool addThisFile = true;
1601
1602             if(exists.isDirectory)
1603                addThisFile = false;
1604             else if(!exists)
1605             {
1606                if(MessageBox { master = ide, type = yesNo, text = filePath, 
1607                      contents = $"File doesn't exist. Create?" }.Modal() == yes)
1608                {
1609                   File f = FileOpen(filePath, write);
1610                   if(f)
1611                   {
1612                      addThisFile = true;
1613                      delete f;
1614                   }
1615                   else
1616                   {
1617                      MessageBox { master = ide, type = ok, text = filePath, 
1618                            contents = $"Couldn't create file."}.Modal();
1619                      addThisFile = false;
1620                   }
1621                }
1622             }
1623
1624             if(addThisFile)
1625             {
1626                /*addFailed = */if(!AddFile(parentNode, filePath, resources, false))//;
1627                {
1628                   nameConflictFiles.Add(CopyString(filePath));
1629                   addFailed = true;
1630                }
1631             }
1632          }
1633          if(addFailed)
1634          {
1635             int len = 0;
1636             char * part1 = $"The following file";
1637             char * opt1 = $" was ";
1638             char * opt2 = $"s were ";
1639             char * part2 = $"not added because of identical file name conflict within the project.\n\n";
1640             char * message;
1641             len += strlen(part1);
1642             len += strlen(part2);
1643             len += nameConflictFiles.count > 1 ? strlen(opt2) : strlen(opt1);
1644             for(s : nameConflictFiles)
1645                len += strlen(s) + 1;
1646             message = new char[len + 1];
1647             strcpy(message, part1);
1648             strcat(message, nameConflictFiles.count > 1 ? opt2 : opt1);
1649             strcat(message, part2);
1650             for(s : nameConflictFiles)
1651             {
1652                strcat(message, s);
1653                strcat(message, "\n");
1654             }
1655             MessageBox { master = ide, type = ok, text = $"Name Conflict", 
1656                   contents = message }.Modal();
1657             delete message;
1658          }
1659          nameConflictFiles.Free();
1660          delete nameConflictFiles;
1661       }
1662    }
1663
1664    ProjectNode AddFile(ProjectNode parentNode, char * filePath, bool resources, bool isTemporary)
1665    {
1666       ProjectNode result = null;
1667       ProjectNode after = null;
1668       for(node : parentNode.files)
1669       {
1670          if(node.type != folder && node.type != file && node.type)
1671             break;
1672          after = node;
1673       }
1674
1675       result = parentNode.Add(parentNode.project, filePath, after, file, NodeIcons::SelectFileIcon(filePath), !resources);
1676
1677       if(result)
1678       {
1679          if(!isTemporary)
1680          {
1681             modifiedDocument = true;
1682             parentNode.project.topNode.modified = true;
1683             parentNode.project.ModifiedAllConfigs(true, false, true, true);
1684          }
1685          Update(null);
1686          result.row = parentNode.row.AddRowAfter(after ? after.row : null);
1687          result.row.tag = (int)result;
1688          result.row.SetData(null, result);
1689       }
1690       return result;
1691    }
1692
1693    CodeEditor CreateNew(char * upper, char * lower, char * base, char * className)
1694    {
1695       CodeEditor codeEditor = null;
1696       ProjectNode projectNode;
1697       ProjectNode after = null;
1698       DataRow row = fileList.currentRow;
1699       ProjectNode parentNode;
1700       int c;
1701
1702       if(!row) row = project.topNode.row;
1703
1704       parentNode = (ProjectNode)row.tag;
1705
1706       for(node : parentNode.files)
1707       {
1708          if(node.type != folder && node.type != file && node.type)
1709             break;
1710          after = node;
1711       }
1712       for(c = 1; c < 100; c++)
1713       {
1714          char string[16];
1715          sprintf(string, c ? "%s%d.ec" : "%s.ec", lower, c);
1716          if((projectNode = parentNode.Add(project, string, after, file, genFile, true)))
1717             break;
1718       }
1719       if(projectNode)
1720       {
1721          char name[256];
1722          char filePath[MAX_LOCATION];
1723
1724          modifiedDocument = true;
1725          project.topNode.modified = true;
1726          Update(null);
1727          project.ModifiedAllConfigs(true, false, false, true);
1728          projectNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
1729          projectNode.row.tag =(int)projectNode;
1730             
1731          projectNode.row.SetData(null, projectNode);
1732          fileList.currentRow = projectNode.row;
1733
1734          strcpy(filePath, project.topNode.path);
1735          PathCat(filePath, projectNode.path);
1736          PathCat(filePath, projectNode.name);
1737
1738          codeEditor = (CodeEditor)ide.FindWindow(filePath);
1739          if(!codeEditor)
1740          {
1741             Class baseClass = eSystem_FindClass(__thisModule, base);
1742             subclass(ClassDesignerBase) designerClass = eClass_GetDesigner(baseClass);
1743             if(designerClass)
1744             {
1745                codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, false, null, whatever, normal);
1746                strcpy(name, projectNode.name);
1747                sprintf(name, "%s%d", upper, c);
1748                if(className)
1749                   strcpy(className, name);
1750
1751                designerClass.CreateNew(codeEditor.editBox, codeEditor.clientSize, name, base);
1752
1753                //codeEditor.modifiedDocument = false;
1754                //codeEditor.designer.modifiedDocument = false;
1755
1756                //Code_EnsureUpToDate(codeEditor);
1757             }
1758          }
1759          else // TODO: fix no symbols generated when ommiting {} for following else
1760          {
1761             codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, false, null, whatever, normal);
1762          }
1763          if(codeEditor)
1764          {
1765             codeEditor.ViewDesigner();
1766             codeEditor.codeModified = true;
1767          }
1768       }
1769       visible = false;
1770       return codeEditor;   
1771    }
1772
1773    // Returns true if we opened something
1774    bool OpenSelectedNodes()
1775    {
1776       bool result = false;
1777       OldList selection;
1778       OldLink item;
1779
1780       fileList.GetMultiSelection(selection);
1781       for(item = selection.first; item; item = item.next)
1782       {
1783          DataRow row = item.data;
1784          ProjectNode node = (ProjectNode)row.tag;
1785          if(node.type == file)
1786          {
1787             OpenNode(node);
1788             result = true;
1789             break;
1790          }
1791       }
1792       selection.Free(null);
1793       return result;
1794    }
1795
1796    void RemoveSelectedNodes()
1797    {
1798       OldList selection;
1799       OldLink item, next;
1800       
1801       fileList.GetMultiSelection(selection);
1802
1803       // Remove children of parents we're deleting
1804       for(item = selection.first; item; item = next)
1805       {
1806          OldLink i;
1807          DataRow row = item.data;
1808          ProjectNode n, node = (ProjectNode)row.tag;
1809          bool remove = false;
1810
1811          next = item.next;
1812          for(i = selection.first; i && !remove; i = i.next)
1813          {
1814             ProjectNode iNode = (ProjectNode)((DataRow)i.data).tag;
1815             for(n = node.parent; n; n = n.parent)
1816             {
1817                if(iNode == n)
1818                {
1819                   remove = true;
1820                   break;
1821                }
1822             }
1823          }
1824          if(remove)
1825             selection.Delete(item);
1826       }
1827
1828       for(item = selection.first; item; item = item.next)
1829       {
1830          DataRow row = item.data;
1831          ProjectNode node = (ProjectNode)row.tag;
1832          ProjectNode resNode;
1833          for(resNode = node.parent; resNode; resNode = resNode.parent)
1834             if(resNode.type == resources)
1835                break;
1836          if(node.type == file)
1837          {
1838             Project prj = node.project;
1839             DeleteNode(node);
1840             modifiedDocument = true;
1841             prj.topNode.modified = true;
1842             Update(null);
1843             prj.ModifiedAllConfigs(true, false, true, true);
1844          }
1845          else if(node.type == folder)
1846          {
1847             char message[1024];
1848             sprintf(message, $"Are you sure you want to remove the folder \"%s\"\n"
1849                   "and all of its contents from the project?", node.name);
1850             if(MessageBox { master = ide, type = yesNo, text = $"Delete Folder", contents = message }.Modal() == yes)
1851             {
1852                Project prj = node.project;
1853                if(node.containsFile)
1854                   prj.ModifiedAllConfigs(true, false, true, true);
1855                DeleteNode(node);
1856                modifiedDocument = true;
1857                prj.topNode.modified = true;
1858             }
1859          }
1860          else if(node.type == project && node != project.topNode && !buildInProgress)
1861          {
1862             char message[1024];
1863             Project prj = null;
1864             for(p : workspace.projects)
1865             {
1866                if(p.topNode == node)
1867                {
1868                   prj = p;
1869                   break;
1870                }
1871             }
1872             sprintf(message, $"Are you sure you want to remove the \"%s\" project\n" "from this workspace?", node.name);
1873             if(MessageBox { master = ide, type = yesNo, text = $"Remove Project", contents = message }.Modal() == yes)
1874             {
1875                // THIS GOES FIRST!
1876                DeleteNode(node);
1877                if(prj)
1878                   workspace.RemoveProject(prj);
1879                //modifiedDocument = true; // when project view is a workspace view
1880             }
1881          }
1882       }
1883       selection.Free(null);
1884    }
1885 }