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