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