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