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