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