ide; added support for -n / --just-print / --dry-run / --recon option to make. hold...
[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       if(!project.GenerateCrossPlatformMk() || !project.GenerateCompilerCf(compiler))
590          ide.outputView.buildBox.Logf($"Error generating compiler configuration (Is the project/config directory writable?)\n");
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             if(!project.GenerateMakefile(null, false, null, config))
650                ide.outputView.buildBox.Logf($"Error generating makefile (Is the project directory writable?)\n");
651
652             ide.statusBar.text = null;
653             app.UpdateDisplay();
654             return true;
655          }
656       }
657       return false;
658    }
659    
660    bool BuildInterrim(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config, bool justPrint)
661    {
662       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
663       {
664          ide.outputView.buildBox.Logf($"Building project %s using the %s configuration...\n", prj.name, GetConfigName(config));
665          return Build(prj, buildType, compiler, config, justPrint);
666       }
667       return false;
668    }
669
670    bool DebugStopForMake(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config)
671    {
672       bool result = false;
673       // TOFIX: DebugStop is being abused and backfiring on us.
674       //        It's supposed to be the 'Debug/Stop' item, not unloading executable or anything else
675
676       //        configIsInDebugSession seems to be used for two OPPOSITE things:
677       //        If we're debugging another config, we need to unload the executable!
678       //        In building, we want to stop if we're debugging the 'same' executable
679       if(buildType != run) ///* && prj == project*/ && prj.configIsInDebugSession)
680       {
681          if(buildType == start || buildType == restart)
682          {
683             if(ide.debugger && ide.debugger.isPrepared)
684                result = DebugStop();
685          }
686          else
687          {
688             if(ide.project == prj && ide.debugger && ide.debugger.prjConfig == config && ide.debugger.isPrepared)
689                result = DebugStop();
690          }
691       }
692       return result;
693    }
694
695    bool Build(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config, bool justPrint)
696    {
697       bool result = true;
698       Window document;
699
700       stopBuild = false;
701       for(document = master.firstChild; document; document = document.next)
702       {
703          if(document.modifiedDocument)
704          {
705             ProjectNode node = GetNodeFromWindow(document, prj);
706             if(node && !document.MenuFileSave(null, 0))
707             {
708                result = false;
709                break;
710             }
711          }
712       }
713       if(result)
714       {
715          DirExpression targetDir = prj.GetTargetDir(compiler, config);
716
717          DebugStopForMake(prj, buildType, compiler, config);
718
719          // TODO: Disabled until problems fixed... is it fixed?
720          if(buildType == rebuild || (config && config.compilingModified))
721             prj.Clean(compiler, config, false, justPrint);
722          else
723          {
724             if(buildType == relink || (config && config.linkingModified))
725             {
726                char target[MAX_LOCATION];
727
728                strcpy(target, prj.topNode.path);
729                PathCat(target, targetDir.dir);
730                prj.CatTargetFileName(target, compiler, config);
731                if(FileExists(target))
732                   DeleteFile(target);
733             }
734             if(config && config.symbolGenModified)
735             {
736                DirExpression objDir = prj.GetObjDir(compiler, config);
737                char fileName[MAX_LOCATION];
738                char moduleName[MAX_FILENAME];
739                strcpy(fileName, prj.topNode.path);
740                PathCatSlash(fileName, objDir.dir);
741                strcpy(moduleName, prj.moduleName);
742                strcat(moduleName, ".main.ec");
743                PathCatSlash(fileName, moduleName);
744                if(FileExists(fileName))
745                   DeleteFile(fileName);
746                ChangeExtension(fileName, "c", fileName);
747                if(FileExists(fileName))
748                   DeleteFile(fileName);
749                ChangeExtension(fileName, "o", fileName);
750                if(FileExists(fileName))
751                   DeleteFile(fileName);
752
753                delete objDir;
754             }
755          }
756          buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
757          ide.AdjustBuildMenus();
758          ide.AdjustDebugMenus();
759
760          result = prj.Build(buildType == run, null, compiler, config, justPrint);
761
762          if(config)
763          {
764             config.compilingModified = false;
765             if(!ide.ShouldStopBuild())
766                config.linkingModified = false;
767
768             config.symbolGenModified = false;
769          }
770          buildInProgress = none;
771          ide.AdjustBuildMenus();
772          ide.AdjustDebugMenus();
773
774          ide.workspace.modified = true;
775
776          delete targetDir;
777       }
778       return result;
779    }
780
781    //                          ((( USER ACTIONS )))
782    //
783    //  ************************************************************************
784    //  *** Methods below should atomically start a process, and as such     ***
785    //  *** they can query compiler and config directly from ide and project ***
786    //  *** but ONLY ONCE!!!                                                 ***
787    //  ************************************************************************
788
789    bool ProjectBuild(MenuItem selection, Modifiers mods)
790    {
791       Project prj = project;
792       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
793       ProjectConfig config;
794       if(selection || !ide.activeClient)
795       {
796          DataRow row = fileList.currentRow;
797          ProjectNode node = row ? (ProjectNode)row.tag : null;
798          if(node) prj = node.project;
799       }
800       else
801       {
802          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
803          if(node)
804             prj = node.project;
805       }
806       config = prj.config;
807       if(/*prj != project || */!prj.GetConfigIsInDebugSession(config) || !ide.DontTerminateDebugSession($"Project Build"))
808       {
809          BuildInterrim(prj, build, compiler, config, mods.ctrl && mods.shift);
810       }
811       delete compiler;
812       return true;
813    }
814
815    bool ProjectLink(MenuItem selection, Modifiers mods)
816    {
817       Project prj = project;
818       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
819       ProjectConfig config;
820       if(selection || !ide.activeClient)
821       {
822          DataRow row = fileList.currentRow;
823          ProjectNode node = row ? (ProjectNode)row.tag : null;
824          if(node) prj = node.project;
825       }
826       else
827       {
828          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
829          if(node)
830             prj = node.project;
831       }
832       config = prj.config;
833       if(!prj.GetConfigIsInDebugSession(config) ||
834             (!ide.DontTerminateDebugSession($"Project Link") && DebugStopForMake(prj, relink, compiler, config)))
835       {
836          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
837          {
838             ide.outputView.buildBox.Logf($"Relinking project %s using the %s configuration...\n", prj.name, GetConfigName(config));
839             if(config)
840                config.linkingModified = true;
841             Build(prj, relink, compiler, config, mods.ctrl && mods.shift);
842          }
843       }
844       delete compiler;
845       return true;
846    }
847
848    bool ProjectRebuild(MenuItem selection, Modifiers mods)
849    {
850       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
851       Project prj = project;
852       ProjectConfig config;
853       if(selection || !ide.activeClient)
854       {
855          DataRow row = fileList.currentRow;
856          ProjectNode node = row ? (ProjectNode)row.tag : null;
857          if(node) prj = node.project;
858       }
859       else
860       {
861          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
862          if(node)
863             prj = node.project;
864       }
865       config = prj.config;
866       if(!prj.GetConfigIsInDebugSession(config) ||
867             (!ide.DontTerminateDebugSession($"Project Rebuild") && DebugStopForMake(prj, rebuild, compiler, config)))
868       {
869          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
870          {
871             ide.outputView.buildBox.Logf($"Rebuilding project %s using the %s configuration...\n", prj.name, GetConfigName(config));
872             /*if(config)
873             {
874                config.compilingModified = true;
875                config.makingModified = true;
876             }*/ // -- should this still be used depite the new solution of BuildType?
877             Build(prj, rebuild, compiler, config, mods.ctrl && mods.shift);
878          }
879       }
880       delete compiler;
881       return true;
882    }
883
884    bool ProjectClean(MenuItem selection, Modifiers mods)
885    {
886       Project prj = project;
887       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
888       ProjectConfig config;
889       if(selection || !ide.activeClient)
890       {
891          DataRow row = fileList.currentRow;
892          ProjectNode node = row ? (ProjectNode)row.tag : null;
893          if(node) prj = node.project;
894       }
895       else
896       {
897          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
898          if(node)
899             prj = node.project;
900       }
901       config = prj.config;
902       if(!prj.GetConfigIsInDebugSession(config) ||
903             (!ide.DontTerminateDebugSession($"Project Clean") && DebugStopForMake(prj, clean, compiler, config)))
904       {
905          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
906          {
907             ide.outputView.buildBox.Logf($"Cleaning project %s using the %s configuration...\n", prj.name, GetConfigName(config));
908
909             buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
910             ide.AdjustBuildMenus();
911             ide.AdjustDebugMenus();
912
913             prj.Clean(compiler, config, false, mods.ctrl && mods.shift);
914             buildInProgress = none;
915             ide.AdjustBuildMenus();
916             ide.AdjustDebugMenus();
917          }
918       }
919       delete compiler;
920       return true;
921    }
922
923    bool ProjectRealClean(MenuItem selection, Modifiers mods)
924    {
925       Project prj = project;
926       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
927       ProjectConfig config;
928       if(selection || !ide.activeClient)
929       {
930          DataRow row = fileList.currentRow;
931          ProjectNode node = row ? (ProjectNode)row.tag : null;
932          if(node) prj = node.project;
933       }
934       else
935       {
936          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
937          if(node)
938             prj = node.project;
939       }
940       config = prj.config;
941       if(!prj.GetConfigIsInDebugSession(config) ||
942             (!ide.DontTerminateDebugSession($"Project Real Clean") && DebugStopForMake(prj, clean, compiler, config)))
943       {
944          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
945          {
946             ide.outputView.buildBox.Logf($"Removing intermediate objects directory for project %s using the %s configuration...\n", prj.name, GetConfigName(config));
947
948             buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
949             ide.AdjustBuildMenus();
950             ide.AdjustDebugMenus();
951
952             prj.Clean(compiler, config, true, mods.ctrl && mods.shift);
953             buildInProgress = none;
954             ide.AdjustBuildMenus();
955             ide.AdjustDebugMenus();
956          }
957       }
958       delete compiler;
959       return true;
960    }
961
962    bool ProjectRegenerate(MenuItem selection, Modifiers mods)
963    {
964       Project prj = project;
965       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
966       ShowOutputBuildLog(true);
967       if(selection || !ide.activeClient)
968       {
969          DataRow row = fileList.currentRow;
970          ProjectNode node = row ? (ProjectNode)row.tag : null;
971          if(node)
972             prj = node.project;
973       }
974       else
975       {
976          ProjectNode node = GetNodeFromWindow(ide.activeClient, null);
977          if(node)
978             prj = node.project;
979       }
980
981       DisplayCompiler(compiler, false);
982       ProjectPrepareCompiler(project, compiler);
983       ProjectPrepareMakefile(prj, force, compiler, prj.config);
984       delete compiler;
985       return true;
986    }
987
988    bool Compile(ProjectNode node, bool justPrint)
989    {
990       bool result = false;
991       char fileName[MAX_LOCATION];
992       Window document;
993       Project prj = node.project;
994       ProjectConfig config = prj.config;
995       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
996
997       stopBuild = false;
998
999       // Check if we have to save
1000       strcpy(fileName, prj.topNode.path);
1001       PathCatSlash(fileName, node.path);
1002       PathCatSlash(fileName, node.name);
1003       for(document = ide.firstChild; document; document = document.next)
1004       {
1005          if(document.modifiedDocument)
1006          {
1007             char documentFileName[MAX_LOCATION];
1008             if(!fstrcmp(GetSlashPathBuffer(documentFileName, document.fileName), fileName))
1009                if(!document.MenuFileSave(null, 0))
1010                   return result;
1011          }
1012       }
1013
1014       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
1015       {
1016          if(!node.GetIsExcluded(config))
1017          {
1018             // Delete intermedaite files
1019             {
1020                char extension[MAX_EXTENSION];
1021                DirExpression objDir = prj.GetObjDir(compiler, config);
1022
1023                strcpy(fileName, prj.topNode.path);
1024                PathCatSlash(fileName, objDir.dir);
1025                PathCatSlash(fileName, node.name);
1026                StripExtension(fileName);
1027                strcat(fileName, ".o");
1028                if(FileExists(fileName))
1029                   DeleteFile(fileName);
1030
1031                GetExtension(node.name, extension);
1032                if(!strcmp(extension, "ec"))
1033                {
1034                   // Delete generated C file
1035                   strcpy(fileName, prj.topNode.path);
1036                   PathCat(fileName, objDir.dir);
1037                   PathCat(fileName, node.name);
1038                   StripExtension(fileName);
1039                   strcat(fileName, ".c");
1040                   if(FileExists(fileName))
1041                      DeleteFile(fileName);
1042
1043                   // Delete symbol file
1044                   strcpy(fileName, prj.topNode.path);
1045                   PathCat(fileName, node.path);
1046                   PathCat(fileName, node.name);
1047                   StripExtension(fileName);
1048                   strcat(fileName, ".sym");
1049                   if(FileExists(fileName))
1050                      DeleteFile(fileName);
1051                }
1052
1053                delete objDir;
1054             }
1055             buildInProgress = compilingFile;
1056             ide.AdjustBuildMenus();
1057
1058             //ide.outputView.ShowClearSelectTab(build);
1059             // this stuff doesn't even appear
1060             //ide.outputView.buildBox.Logf($"%s Compiler\n", compiler.name);
1061             if(config)
1062                ide.outputView.buildBox.Logf($"Compiling single file %s in project %s using the %s configuration...\n", node.name, prj.name, config.name);
1063             else
1064                ide.outputView.buildBox.Logf($"Compiling single file %s in project %s...\n", node.name, prj.name);
1065
1066             prj.Compile(node, compiler, config, justPrint);
1067             buildInProgress = none;
1068             ide.AdjustBuildMenus();
1069
1070             result = true;
1071          }
1072       }
1073       delete compiler;
1074       return result;
1075    }
1076
1077    bool ProjectNewFile(MenuItem selection, Modifiers mods)
1078    {
1079       DataRow row = fileList.currentRow;
1080       if(row)
1081       {
1082          char fileName[1024];
1083          char filePath[MAX_LOCATION];
1084          ProjectNode parentNode = (ProjectNode)row.tag;
1085          ProjectNode n, fileNode;
1086          parentNode.GetFileSysMatchingPath(filePath);
1087          MakePathRelative(filePath, parentNode.project.topNode.path, filePath);
1088          for(n = parentNode; n && n != parentNode.project.resNode; n = n.parent);
1089          sprintf(fileName, $"Untitled %d", documentID);
1090          fileNode = AddFile(parentNode, fileName, (bool)n, true);
1091          fileNode.path = CopyUnixPath(filePath);
1092          if(fileNode)
1093          {
1094             NodeProperties nodeProperties
1095             {
1096                parent, this;
1097                position = { position.x + 100, position.y + 100 };
1098                mode = newFile;
1099                node = fileNode;
1100             };
1101             nodeProperties.Create(); // not modal?
1102          }
1103       }
1104       return true;
1105    }
1106
1107    bool ProjectNewFolder(MenuItem selection, Modifiers mods)
1108    {
1109       DataRow row = fileList.currentRow;
1110       if(row)
1111       {
1112          ProjectNode parentNode = (ProjectNode)row.tag;
1113          NewFolder(parentNode, null, true);
1114       }
1115       return true;
1116    }
1117
1118    bool ResourcesAddFiles(MenuItem selection, Modifiers mods)
1119    {
1120       AddFiles(true);
1121       return true;
1122    }
1123
1124    bool ProjectAddFiles(MenuItem selection, Modifiers mods)
1125    {
1126       AddFiles(false);
1127       return true;
1128    }
1129
1130    bool ProjectImportFolder(MenuItem selection, Modifiers mods)
1131    {
1132       DataRow row = fileList.currentRow;
1133       if(row)
1134       {
1135          ProjectNode toNode = (ProjectNode)row.tag;
1136          ImportFolder(toNode);
1137       }
1138       return true;
1139    }
1140
1141    bool ProjectAddNewForm(MenuItem selection, Modifiers mods)
1142    {
1143       CodeEditor codeEditor = CreateNew("Form", "form", "Window", null);
1144       codeEditor.EnsureUpToDate();
1145       return true;
1146    }
1147
1148    bool ProjectAddNewGraph(MenuItem selection, Modifiers mods)
1149    {
1150       CodeEditor codeEditor = CreateNew("Graph", "graph", "Block", null);
1151       if(codeEditor)
1152          codeEditor.EnsureUpToDate();
1153       return true;
1154    }
1155
1156    bool ProjectRemove(MenuItem selection, Modifiers mods)
1157    {
1158       DataRow row = fileList.currentRow;
1159       if(row)
1160       {
1161          ProjectNode node = (ProjectNode)row.tag;
1162          if(node.type == project)
1163             RemoveSelectedNodes();
1164       }
1165       return true;
1166    }
1167
1168    bool ProjectUpdateMakefileForAllConfigs(Project project)
1169    {
1170       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1171
1172       for(config : project.configurations)
1173          ProjectPrepareMakefile(project, forceExists, compiler, config);
1174
1175       ide.Update(null);
1176       delete compiler;
1177       return true;
1178    }
1179
1180    bool MenuConfig(MenuItem selection, Modifiers mods)
1181    {
1182       if(ProjectActiveConfig { master = parent, project = project }.Modal() == ok)
1183          ide.AdjustMenus();
1184       return true;
1185    }
1186
1187    bool MenuCompiler(MenuItem selection, Modifiers mods)
1188    {
1189       ActiveCompilerDialog compilerDialog
1190       {
1191          master = parent;
1192          ideSettings = ideSettings, workspaceActiveCompiler = ide.workspace.compiler;
1193       };
1194       incref compilerDialog;
1195       if(compilerDialog.Modal() == ok && strcmp(compilerDialog.workspaceActiveCompiler, ide.workspace.compiler))
1196       {
1197          CompilerConfig compiler = ideSettings.GetCompilerConfig(compilerDialog.workspaceActiveCompiler);
1198          ide.workspace.compiler = compilerDialog.workspaceActiveCompiler;
1199          ide.projectView.ShowOutputBuildLog(true);
1200          ide.projectView.DisplayCompiler(compiler, false);
1201
1202          for(prj : ide.workspace.projects)
1203             ide.projectView.ProjectPrepareCompiler(prj, compiler);
1204          delete compiler;
1205       }
1206       delete compilerDialog;
1207       return true;
1208    }
1209
1210    bool MenuSettings(MenuItem selection, Modifiers mods)
1211    {
1212       ProjectNode node = GetSelectedNode(true);
1213       Project prj = node ? node.project : project;
1214       projectSettingsDialog = ProjectSettings { master = parent, project = prj, projectNode = node };
1215       projectSettingsDialog.Modal();
1216
1217       Update(null);
1218       ide.AdjustMenus();
1219       return true;
1220    }
1221
1222    bool FileProperties(MenuItem selection, Modifiers mods)
1223    {
1224       DataRow row = fileList.currentRow;
1225       if(row)
1226       {
1227          ProjectNode node = (ProjectNode)row.tag;
1228          NodeProperties { parent = parent, master = this, node = node, 
1229                position = { position.x + 100, position.y + 100 } }.Create();
1230       }
1231       return true;
1232    }
1233
1234    bool FileOpenFile(MenuItem selection, Modifiers mods)
1235    {
1236       OpenSelectedNodes();
1237       return true;
1238    }
1239
1240    bool FileRemoveFile(MenuItem selection, Modifiers mods)
1241    {
1242       RemoveSelectedNodes();
1243       return true;
1244    }
1245
1246    bool FileCompile(MenuItem selection, Modifiers mods)
1247    {
1248       DataRow row = fileList.currentRow;
1249       if(row)
1250       {
1251          ProjectNode node = (ProjectNode)row.tag;
1252          if(!Compile(node, mods.ctrl && mods.shift))
1253             ide.outputView.buildBox.Logf($"File %s is excluded from current build configuration.\n", node.name);
1254       }
1255       return true;
1256    }
1257
1258    Project GetSelectedProject(bool useSelection)
1259    {
1260       Project prj = project;
1261       if(useSelection)
1262       {
1263          DataRow row = fileList.currentRow;
1264          ProjectNode node = row ? (ProjectNode)row.tag : null;
1265          if(node)
1266             prj = node.project;
1267       }
1268       return prj;
1269    }
1270    
1271    void SelectNextProject(bool backwards)
1272    {
1273       DataRow row = fileList.currentRow;
1274       DataRow currentRow = row;
1275       ProjectNode node = (ProjectNode)row.tag;
1276       if(node.type != project)
1277          row = node.project.topNode.row;
1278       else if(backwards)
1279          row = row.previous ? row.previous : fileList.lastRow;
1280       if(!backwards)
1281          row = row.next ? row.next : fileList.firstRow;
1282       if(row && row != currentRow)
1283       {
1284          fileList.SelectRow(row);
1285          fileList.currentRow = row;
1286       }
1287    }
1288
1289    ProjectNode GetSelectedNode(bool useSelection)
1290    {
1291       ProjectNode node = null;
1292       if(useSelection)
1293       {
1294          DataRow row = fileList.currentRow;
1295          if(row)
1296             node = (ProjectNode)row.tag;
1297       }
1298       return node;
1299    }
1300
1301    bool MenuBrowseFolder(MenuItem selection, Modifiers mods)
1302    {
1303       char folder[MAX_LOCATION];
1304       Project prj;
1305       ProjectNode node = GetSelectedNode(true);
1306       if(!node)
1307          node = project.topNode;
1308       prj = node.project;
1309
1310       strcpy(folder, prj.topNode.path);
1311       if(node != prj.topNode)
1312          PathCatSlash(folder, node.path);
1313       ShellOpen(folder);
1314       return true;
1315    }
1316
1317    bool Run(MenuItem selection, Modifiers mods)
1318    {
1319       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1320       ProjectConfig config = project.config;
1321       String args = new char[maxPathLen];
1322       args[0] = '\0';
1323       if(ide.workspace.commandLineArgs)
1324          //ide.debugger.GetCommandLineArgs(args);
1325          strcpy(args, ide.workspace.commandLineArgs);
1326       if(ide.debugger.isActive)
1327          project.Run(args, compiler, config);
1328       /*else if(config.targetType == sharedLibrary || config.targetType == staticLibrary)
1329          MessageBox { master = ide, type = ok, text = "Run", contents = "Shared and static libraries cannot be run like executables." }.Modal();*/
1330       else if(BuildInterrim(project, run, compiler, config, false))
1331          project.Run(args, compiler, config);
1332       delete args;
1333       delete compiler;
1334       return true;
1335    }
1336
1337    bool DebugStart()
1338    {
1339       bool result = false;
1340       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1341       ProjectConfig config = project.config;
1342       TargetTypes targetType = project.GetTargetType(config);
1343       if(targetType == sharedLibrary || targetType == staticLibrary)
1344          MessageBox { master = ide, type = ok, text = $"Run", contents = $"Shared and static libraries cannot be run like executables." }.Modal();
1345       else if(project.GetCompress(config))
1346          MessageBox { master = ide, text = $"Starting Debug", contents = $"Debugging compressed applications is not supported\n" }.Modal();
1347       else if(project.GetDebug(config) ||
1348          MessageBox { master = ide, type = okCancel, text = $"Starting Debug", contents = $"Attempting to debug non-debug configuration\nProceed anyways?" }.Modal() == ok)
1349       {
1350          if(/*!IsProjectModified() ||*/ BuildInterrim(project, start, compiler, config, false))
1351          {
1352             if(compiler.type.isVC)
1353             {
1354                //bool result = false;
1355                char oldwd[MAX_LOCATION];
1356                PathBackup pathBackup { };
1357                char command[MAX_LOCATION];
1358
1359                ide.SetPath(false, compiler, config);
1360                
1361                GetWorkingDir(oldwd, sizeof(oldwd));
1362                ChangeWorkingDir(project.topNode.path);
1363
1364                sprintf(command, "%s /useenv %s.sln /projectconfig \"%s|Win32\" /command \"%s\"" , "devenv", project.name, config.name, "Debug.Start");
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, false))
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, false)))
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, false)))
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 }