ide/ProjectView: Opening binary documents using shell from project view 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       if(!projectView.AddFile(parentNode, filePath, parentNode.isInResources, false))
32       {
33          char * msg = PrintString($"This file can't be imported due to a conflict.\n\n", filePath,
34                "\n\nThis occurs with identical file paths and with conflicting file names.\n");
35          MessageBox { master = ide, type = ok, text = "Import File Conflict", contents = msg }.Modal();
36          delete msg;
37       }
38       return true;
39    }
40 }
41
42 static Array<FileFilter> fileFilters
43 { [
44    { $"eC/C/C++ Files (*.ec, *.eh, *.c, *.cpp, *.cc, *.cxx, *.h, *.hpp, *.hh, *.hxx)", "ec, eh, c, cpp, cc, cxx, h, hpp, hh, hxx" },
45    { $"eC/C/C++ Source Files (*.ec, *.c, *.cpp, *.cc, *.cxx)", "ec, eh, c, cpp, cc, cxx" },
46    { $"Header Files for eC/C/C++ (*.eh, *.h, *.hpp, *.hh, *.hxx)", "eh, h, hpp, hh, hxx" },
47    { $"All files", null }
48 ] };
49
50 static Array<FileFilter> resourceFilters
51 { [
52    { $"Image Files (*.jpg, *.jpeg, *.bmp, *.pcx, *.png, *.gif)", "jpg, jpeg, bmp, pcx, png, gif" },
53    { $"3D Studio Model Files (*.3ds)", "3ds" },
54    { $"Translations (*.mo)", "mo" },
55    { $"All files", null }
56 ] };
57
58 static Array<FileFilter> projectFilters
59 { [
60    { $"Project Files (*.epj)", ProjectExtension },
61    { $"Workspace Files (*.ews)", WorkspaceExtension }
62 ] };
63
64 static Array<FileType> projectTypes
65 { [
66    { $"Ecere IDE Project", ProjectExtension },
67    { $"Ecere IDE Workspace", WorkspaceExtension }
68 ] };
69
70 static char * iconNames[] =
71 {
72    "<:ecere>mimeTypes/file.png",                   /*genFile*/
73    "<:ecere>mimeTypes/textEcereWorkspace.png",     /*ewsFile*/
74    "<:ecere>mimeTypes/textEcereProject.png",       /*epjFile*/
75    "<:ecere>places/folder.png",                    /*folder*/
76    "<:ecere>status/folderOpen.png",                /*openFolder*/
77    "<:ecere>mimeTypes/textEcereSource.png",        /*ecFile*/
78    "<:ecere>mimeTypes/textEcereHeader.png",        /*ehFile*/
79    "<:ecere>mimeTypes/textCSource.png",            /*sFile*/ // TODO: change sFile icon to differentiate from cFile icon
80    "<:ecere>mimeTypes/textCSource.png",            /*cFile*/
81    "<:ecere>mimeTypes/textCHeader.png",            /*hFile*/
82    "<:ecere>mimeTypes/textC++Source.png",          /*cppFile*/
83    "<:ecere>mimeTypes/textC++Header.png",          /*hppFile*/
84    "<:ecere>mimeTypes/text.png",                   /*textFile*/
85    "<:ecere>mimeTypes/textHyperTextMarkup.png",    /*webFile*/
86    "<:ecere>mimeTypes/image.png",                  /*pictureFile*/
87    "<:ecere>status/audioVolumeHigh.png",           /*soundFile*/
88    "<:ecere>mimeTypes/package.png",                /*archiveFile*/
89    "<:ecere>mimeTypes/packageSoftware.png",        /*packageFile*/
90    "<:ecere>mimeTypes/packageOpticalDisc.png",     /*opticalMediaImageFile*/
91    "<:ecere>mimeTypes/file.png",                   /*mFile*/ //TODO: create icon for .m file
92    "<:ecere>mimeTypes/file.png"                    /*mmFile*/ //TODO: create icon for .mm file
93 };
94
95 enum PrepareMakefileMethod { normal, force, forceExists };
96
97 enum CleanType { clean, realClean, cleanTarget };
98 enum BuildType { build, rebuild, relink, run, start, restart, clean, install };
99 enum BuildState
100 {
101    none, buildingMainProject, buildingSecondaryProject, compilingFile;
102
103    property bool { get { return this != none; } }
104    //property bool actualBuild { get { return this == buildingMainProject || this == buildingSecondaryProject;  } }
105 };
106
107 class ProjectView : Window
108 {
109    isDocument = true;
110    //hasMinimize = true;
111    hasClose = true;
112    borderStyle = sizable;
113    hasHorzScroll = true;
114    hasVertScroll = true;
115    background = white;
116    size = { 300 };
117    anchor = Anchor { left = 0, top = 0, bottom = 0 };
118    menu = Menu { };
119
120    //hasMinimize = true;
121    saveDialog = projectFileDialog;
122
123    DataRow resourceRow;
124    BuildState buildInProgress;
125    BitmapResource icons[NodeIcons];
126    Project project;
127    Workspace workspace;
128    property Workspace workspace
129    {
130       set
131       {
132          if(workspace)
133          {
134             for(prj : workspace.projects)
135             {
136                DeleteNode(prj.topNode);
137             }
138             workspace.projects.Free();
139             ide.debugger.CleanUp();
140          }
141          workspace = value;
142          if(workspace)
143          {
144             fileDialog.currentDirectory = workspace.workspaceDir;
145             resourceFileDialog.currentDirectory = workspace.workspaceDir;
146             for(prj : workspace.projects)
147                AddNode(prj.topNode, null);
148             ide.statusBar.text = $"Generating Makefile & Dependencies...";
149             app.UpdateDisplay();
150             for(prj : workspace.projects)
151                prj.ModifiedAllConfigs(true, false, false, false);
152             ide.statusBar.text = $"Initializing Debugger"; app.UpdateDisplay();
153             ide.statusBar.text = null;
154             app.UpdateDisplay();
155          }
156       }
157       get { return workspace; }
158    }
159
160    bool drawingInProjectSettingsDialog;
161    bool drawingInProjectSettingsDialogHeader;
162    ProjectSettings projectSettingsDialog;
163
164    bool stopBuild;
165    PopupMenu popupMenu;
166
167    ProjectView()
168    {
169       NodeIcons c;
170       for(c = 0; c < NodeIcons::enumSize; c++)
171       {
172          icons[c] = BitmapResource { iconNames[c], alphaBlend = true };
173          AddResource(icons[c]);
174       }
175       fileList.AddField(DataField { dataType = class(ProjectNode), freeData = false, userData = this });
176    }
177
178    ~ProjectView()
179    {
180       DebugStop();
181       ide.DestroyTemporaryProjectDir();
182       if(project)
183       {
184          workspace.Free();
185          delete workspace;
186       }
187    }
188
189    ListBox fileList
190    {
191       multiSelect = true, fullRowSelect = false, hasVertScroll = true, hasHorzScroll = true;
192       borderStyle = deep, parent = this, collapseControl = true, treeBranches = true;
193       anchor = Anchor { left = 0, right = 0, top = 0 , bottom = 0 };
194
195       background = projectViewBackground;
196       foreground = projectViewText;
197       selectionColor = selectionColor, selectionText = selectionText;
198       stippleColor = skyBlue;
199
200       bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
201       {
202          if(!active) Update(null);
203          return ListBox::OnActivate(active, previous, goOnWithActivation, direct);
204       }
205
206       bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
207       {
208          // Prevent the double click from reactivating the project view (returns false if we opened something)
209          return !OpenSelectedNodes(mods.ctrl && mods.shift);
210       }
211
212       bool NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods)
213       {
214          DataRow row = listBox.currentRow;
215          if(row)
216          {
217             bool showDebuggingMenuItems = mods.ctrl && mods.shift;
218             bool showInstallMenuItem = mods.ctrl && mods.shift;
219             ProjectNode node = (ProjectNode)row.tag;
220 #ifdef IDE_SHOW_INSTALL_MENU_BUTTON
221             showInstallMenuItem = true;
222 #endif
223             if(node.type == NodeTypes::project || node.type == resources || node.type == file || node.type == folder)
224             {
225                bool na = buildInProgress; // N/A - buildMenuUnavailable
226                Menu pop { };
227
228                if(node.type == NodeTypes::project)
229                {
230                   MenuItem mi;
231                                                                                                                                              mi = ide.projectBuildItem;
232                   MenuItem { pop, $"Build"              , b, f7     , NotifySelect = ProjectBuild      , bitmap = mi.bitmap }.disabled = na; mi = ide.projectLinkItem;
233                   MenuItem { pop, $"Relink"             , l         , NotifySelect = ProjectLink       , bitmap = mi.bitmap }.disabled = na; mi = ide.projectRebuildItem;
234                   MenuItem { pop, $"Rebuild"            , r, shiftF7, NotifySelect = ProjectRebuild    , bitmap = mi.bitmap }.disabled = na; mi = ide.projectCleanTargetItem;
235                   MenuItem { pop, $"Clean Target"       , g         , NotifySelect = ProjectCleanTarget, bitmap = mi.bitmap }.disabled = na; mi = ide.projectCleanItem;
236                   MenuItem { pop, $"Clean"              , c         , NotifySelect = ProjectClean      , bitmap = mi.bitmap }.disabled = na; mi = ide.projectRealCleanItem;
237                   MenuItem { pop, $"Real Clean"                     , NotifySelect = ProjectRealClean  , bitmap = mi.bitmap }.disabled = na; mi = ide.projectRegenerateItem;
238                   MenuItem { pop, $"Regenerate Makefile", m         , NotifySelect = ProjectRegenerate , bitmap = mi.bitmap }.disabled = na;
239                   if(showInstallMenuItem)
240                   {
241                      mi = ide.projectInstallItem;
242                      MenuItem { pop, $"Install"            , t         , NotifySelect = ProjectInstall    , bitmap = mi.bitmap }.disabled = na;
243                   }
244                   if(showDebuggingMenuItems && node.ContainsFilesWithExtension("ec", node.project.config))
245                   {
246                      MenuDivider { pop };
247                      MenuItem { pop, $"Debug Generate Symbols", l, NotifySelect = FileDebugGenerateSymbols }.disabled = na;
248                      MenuItem { pop, $"Debug Precompile", l, NotifySelect = FileDebugPrecompile }.disabled = na;
249                      MenuItem { pop, $"Debug Compile", l, NotifySelect = FileDebugCompile }.disabled = na;
250                   }
251                   MenuDivider { pop };
252                   MenuItem { pop, $"New File...", l, Key { l, ctrl = true }, NotifySelect = ProjectNewFile };
253                   MenuItem { pop, $"New Folder...", n, Key { f, ctrl = true }, NotifySelect = ProjectNewFolder };
254                   MenuItem { pop, $"Import Folder...", i, NotifySelect = ProjectImportFolder };
255                   MenuItem { pop, $"Add Files to Project...", f, NotifySelect = ProjectAddFiles };
256                   MenuDivider { pop };
257                   MenuItem { pop, $"Add New Form...", o, NotifySelect = ProjectAddNewForm };
258                   // MenuItem { pop, "Add New Behavior Graph...", g, NotifySelect = ProjectAddNewGraph };
259                   MenuDivider { pop };
260                   if(node != ((Project)workspace.projects.first).topNode)
261                   {
262                      MenuItem { pop, $"Remove project from workspace", r, NotifySelect = ProjectRemove }.disabled = na;
263                      MenuDivider { pop };
264                   }
265                   MenuItem { pop, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
266                   MenuDivider { pop };
267                   MenuItem { pop, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
268                   MenuDivider { pop };
269                   MenuItem { pop, $"Save", v, Key { s, ctrl = true }, NotifySelect = ProjectSave }.disabled = !node.modified;
270                   MenuDivider { pop };
271                   MenuItem { pop, $"Properties...", p, Key { enter, alt = true }, NotifySelect = FileProperties };
272                }
273                else if(node.type == resources)
274                {
275                   MenuItem { pop, $"New File...", l, Key { l, ctrl = true }, NotifySelect = ProjectNewFile };
276                   MenuItem { pop, $"New Folder...", n, Key { f, ctrl = true }, NotifySelect = ProjectNewFolder };
277                   MenuItem { pop, $"Import Folder...", i, NotifySelect = ProjectImportFolder };
278                   MenuItem { pop, $"Add Resources to Project...", f, NotifySelect = ResourcesAddFiles };
279                   MenuItem { pop, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
280                   MenuDivider { pop };
281                   MenuItem { pop, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
282                   MenuItem { pop, $"Properties...", p, Key { enter, alt = true }, NotifySelect = FileProperties };
283                }
284                else if(node.type == file)
285                {
286                   MenuItem { pop, $"Open", o, NotifySelect = FileOpenFile };
287                   MenuDivider { pop };
288                   MenuItem { pop, $"Clean", l, NotifySelect = FileClean, bitmap = ide.projectCleanItem.bitmap }.disabled = na;
289                   MenuItem { pop, $"Compile", c, Key { f7, ctrl = true}, NotifySelect = FileCompile, bitmap = ide.projectBuildItem.bitmap }.disabled = na;
290                   if(showDebuggingMenuItems)
291                   {
292                      char extension[MAX_EXTENSION];
293                      GetExtension(node.name, extension);
294                      if(!strcmpi(extension, "ec"))
295                      {
296                         MenuDivider { pop };
297                         MenuItem { pop, $"Debug Precompile", l, NotifySelect = FileDebugPrecompile }.disabled = na;
298                         MenuItem { pop, $"Debug Compile", l, NotifySelect = FileDebugCompile }.disabled = na;
299                      }
300                   }
301                   MenuDivider { pop };
302                   MenuItem { pop, $"Remove", r, NotifySelect = FileRemoveFile };
303                   MenuDivider { pop };
304                   MenuItem { pop, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
305                   MenuDivider { pop };
306                   MenuItem { pop, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
307                   MenuItem { pop, $"Properties..", p, Key { enter, alt = true }, NotifySelect = FileProperties };
308                }
309                else if(node.type == folder)
310                {
311                   bool isInResources = node.isInResources;
312
313                   MenuItem { pop, $"New File...", l, Key { l, ctrl = true }, NotifySelect = ProjectNewFile };
314                   MenuItem { pop, $"New Folder...", n, Key { f, ctrl = true }, NotifySelect = ProjectNewFolder };
315                   MenuItem { pop, $"Import Folder...", i, NotifySelect = ProjectImportFolder };
316                   if(isInResources)
317                   {
318                      MenuItem { pop, $"Add Resources to Folder...", f, NotifySelect = ResourcesAddFiles };
319                   }
320                   else
321                   {
322                      MenuItem { pop, $"Add Files to Folder...", f, NotifySelect = ProjectAddFiles };
323                   }
324                   if(!isInResources)
325                   {
326                      MenuDivider { pop };
327                      MenuItem { pop, $"Add New Form...", o, NotifySelect = ProjectAddNewForm };
328                      // MenuItem { pop, $"Add New Behavior Graph...", g, NotifySelect = ProjectAddNewGraph };
329                   }
330                   MenuDivider { pop };
331                   MenuItem { pop, $"Clean", l, NotifySelect = FileClean, bitmap = ide.projectCleanItem.bitmap }.disabled = na;
332                   MenuItem { pop, $"Compile", c, Key { f7, ctrl = true}, NotifySelect = FileCompile, bitmap = ide.projectBuildItem.bitmap }.disabled = na;
333                   MenuDivider { pop };
334                   MenuItem { pop, $"Remove", r, NotifySelect = FileRemoveFile };
335                   MenuDivider { pop };
336                   MenuItem { pop, $"Browse Folder", w, NotifySelect = MenuBrowseFolder };
337                   MenuDivider { pop };
338                   MenuItem { pop, $"Settings...", s, Key { f7, alt = true } , NotifySelect = MenuSettings };
339                   MenuItem { pop, $"Properties...", p, Key { enter, alt = true }, NotifySelect = FileProperties };
340                }
341
342                popupMenu =
343                {
344                   master = this, menu = pop;
345                   position = {
346                      x + clientStart.x + absPosition.x - app.desktop.position.x,
347                      y + clientStart.y + absPosition.y - app.desktop.position.y };
348
349                   void NotifyDestroyed(Window window, DialogResult result)
350                   {
351                      popupMenu = null;
352                   }
353                };
354                popupMenu.Create();
355                ide.AdjustPopupBuildMenus();
356             }
357          }
358          return true;
359       }
360
361       bool NotifyKeyHit(ListBox listBox, DataRow row, Key key, unichar ch)
362       {
363          if(key == altUp || key == altDown)
364          {
365             SelectNextProject(key == altUp);
366             return false;
367          }
368          return true;
369       }
370
371       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
372       {
373          if(row)
374          {
375             ProjectNode node = (ProjectNode)row.tag;
376             switch(key)
377             {
378                case altEnter: case Key { keyPadEnter, alt = true }:
379                {
380                   NodeProperties { parent = parent, master = this,
381                      position = { position.x + 100, position.y + 100 }, node = node }.Create();
382                   return false;
383                }
384                case enter: case keyPadEnter:
385                {
386                   ProjectNode resNode;
387                   for(resNode = node.parent; resNode; resNode = resNode.parent)
388                      if(resNode.type == resources)
389                         break;
390                   if(node.type == project || (node.type == folder && !resNode))
391                   {
392                      AddFiles(false);
393                      return false;
394                   }
395                   else if(node.type == resources || node.type == folder)
396                   {
397                      AddFiles(true);
398                      return false;
399                   }
400                   break;
401                }
402                case ctrlI:
403                {
404                   if(node.type == project || node.type == folder || node.type == resources)
405                   {
406                      ImportFolder(node);
407                      return false;
408                   }
409                   break;
410                }
411                case ctrlF:
412                {
413                   if(node.type == project || node.type == folder || node.type == resources)
414                   {
415                      NewFolder(node, null, true);
416                      return false;
417                   }
418                   break;
419                }
420                case ctrlF7:
421                {
422                   if(node.type == file)
423                   {
424                      FileCompile(null, (Modifiers)key);
425                      return false;
426                   }
427                   break;
428                }
429                case Key { space, false, true }:
430                case Key { space, true, true }:
431                case Key { space, true }:
432                case space:
433                {
434                   if(node.type == NodeTypes::project)
435                   {
436                      Project prj = null;
437                      for(p : workspace.projects)
438                      {
439                         if(p.topNode == node)
440                         {
441                            prj = p;
442                            break;
443                         }
444                      }
445                      prj.RotateActiveConfig(!key.shift, key.ctrl);
446                      if(prj == project)
447                         ide.AdjustMenus();
448                      return false;
449                   }
450                   break;
451                }
452             }
453          }
454          switch(key)
455          {
456             case Key { enter, true, true }:        OpenSelectedNodes(true);   break;
457             case Key { keyPadEnter, true, true }:  OpenSelectedNodes(true);   break;
458             case enter: case keyPadEnter:          OpenSelectedNodes(false);  break;
459             case del:                              RemoveSelectedNodes();     break;
460             case escape:
461             {
462                Window activeClient = ide.activeClient;
463                if(activeClient)
464                   activeClient.Activate();
465                else
466                   ide.RepositionWindows(true);
467                break;
468             }
469          }
470          return true;
471       }
472
473       bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
474       {
475          ProjectNode node = (ProjectNode)row.tag;
476          if(node.type == folder)
477             node.icon = collapsed ? folder : openFolder;
478          return true;
479       }
480    };
481
482    FileDialog importFileDialog { autoCreate = false, type = selectDir, text = $"Import Folder" };
483    FileDialog projectFileDialog
484    {
485       autoCreate = false, filters = projectFilters.array, sizeFilters = projectFilters.count * sizeof(FileFilter);
486       types = projectTypes.array, sizeTypes = projectTypes.count * sizeof(FileType);
487    };
488    FileDialog fileDialog
489    {
490       autoCreate = false, mayNotExist = true, filters = fileFilters.array, sizeFilters = fileFilters.count * sizeof(FileFilter);
491    };
492    FileDialog resourceFileDialog
493    {
494       autoCreate = false, mayNotExist = true, filters = resourceFilters.array, sizeFilters = resourceFilters.count * sizeof(FileFilter);
495    };
496
497    Menu fileMenu { menu, $"File", f };
498    MenuItem { fileMenu, $"Save", s, Key { s, ctrl = true }, NotifySelect = MenuFileSave };
499    // MenuItem { fileMenu, "Save As...", a, NotifySelect = MenuFileSaveAs };
500
501    bool OnClose(bool parentClosing)
502    {
503       if(!parentClosing && visible)
504       {
505          visible = false;
506          return false;
507       }
508       if(buildInProgress)
509          return false;
510
511       if(modifiedDocument)
512       {
513          DialogResult dialogRes;
514          char msg[2048];
515          bool first = true;
516          strcpy(msg, $"You have modified projects.\nSave changes to ");
517          for(p : ide.workspace.projects)
518          {
519             if(p.topNode.modified)
520             {
521                if(!first) strcat(msg, ", ");
522                strcat(msg, p.name);
523                first = false;
524             }
525          }
526          strcat(msg, "?");
527
528          dialogRes = MessageBox { master = master, type = yesNoCancel, text = parent.caption ? parent.caption : rootWindow.caption, contents = msg }.Modal();
529
530          if(dialogRes == yes)
531          {
532             // TOFIX: Precomp error if brackets are taken out
533             return (DialogResult)MenuFileSave(null, 0) != cancel;
534          }
535          else if(dialogRes == cancel)
536             return false;
537          modifiedDocument = false;
538       }
539       return true;
540    }
541
542    void OnDestroy(void)
543    {
544       //if(ide.findInFilesDialog && ide.findInFilesDialog.created && ide.findInFilesDialog.mode != directory)
545       //   ide.findInFilesDialog.SearchStop();
546
547       ide.outputView.buildBox.Clear();
548       ide.outputView.debugBox.Clear();
549       //ide.outputView.findBox.Clear();
550       ide.callStackView.Clear();
551       ide.watchesView.Clear();
552       ide.threadsView.Clear();
553       ide.breakpointsView.Clear();
554       ide.outputView.ShowClearSelectTab(find); // why this?
555    }
556
557    bool OnSaveFile(char * fileName)
558    {
559       for(prj : ide.workspace.projects)
560       {
561          if(prj.topNode.modified)
562          {
563             prj.StopMonitoring();
564             if(prj.Save(prj.filePath))
565                prj.topNode.modified = false;
566             prj.StartMonitoring();
567          }
568       }
569       modifiedDocument = false;
570       Update(null);
571       return true;
572    }
573
574    bool IsModuleInProject(char * filePath)
575    {
576       char moduleName[MAX_FILENAME]; //, modulePath[MAX_LOCATION];
577       GetLastDirectory(filePath, moduleName);
578       return project.topNode.Find(moduleName, false) != null;
579    }
580
581    ProjectNode GetNodeForCompilationFromWindow(Window document, bool nonExcludedFirst, bool * isExcluded, bool * isCObject)
582    {
583       ProjectNode node = null;
584       if(nonExcludedFirst)
585          node = GetNodeFromWindow(document, null, false, false, isExcluded);
586       if(!node)
587          node = GetNodeFromWindow(document, null, true, false, isExcluded);
588       if(!node && nonExcludedFirst)
589       {
590          node = GetNodeFromWindow(document, null, false, true, isExcluded);
591          if(isCObject && node) *isCObject = true;
592       }
593       if(!node)
594       {
595          node = GetNodeFromWindow(document, null, true, true, isExcluded);
596          if(isCObject && node) *isCObject = true;
597       }
598       return node;
599    }
600
601    ProjectNode GetNodeFromWindow(Window document, Project project, bool allNodes, bool isCObject, bool * isNodeExcluded)
602    {
603       ProjectNode node = null;
604       if(document.fileName)
605       {
606          bool excluded;
607          char winFileName[MAX_LOCATION];
608          char * documentFileName = GetSlashPathBuffer(winFileName, document.fileName);
609          Project prj;
610          ProjectNode n;
611          if(isCObject)
612          {
613             char name[MAX_FILENAME];
614             GetLastDirectory(documentFileName, name);
615             ChangeExtension(name, "ec", name);
616             for(p : ide.workspace.projects)
617             {
618                prj = project ? project : p;
619                if((n = prj.topNode.Find(name, false)))
620                {
621                   if(allNodes || !(excluded = n.GetIsExcluded(prj.config)))
622                   {
623                      node = n;
624                      break;
625                   }
626                }
627                if(project) break;
628             }
629          }
630          else
631          {
632             for(p : ide.workspace.projects)
633             {
634                Project pr = p;
635                prj = project ? project : p;
636                if((n = prj.topNode.FindByFullPath(documentFileName, false)))
637                {
638                   if(allNodes || !(excluded = n.GetIsExcluded(prj.config)))
639                   {
640                      node = n;
641                      break;
642                   }
643                }
644                if(project) break;
645             }
646          }
647          if(node && isNodeExcluded)
648             *isNodeExcluded = excluded;
649       }
650       return node;
651    }
652
653    //                          ((( UTILITY FUNCTIONS )))
654    //
655    //  ************************************************************************
656    //  *** These methods below are part of a sequence of events, and as     ***
657    //  *** such they must be passed the current compiler and project config ***
658    //  ************************************************************************
659    bool DisplayCompiler(CompilerConfig compiler, bool cleanLog)
660    {
661       ide.outputView.buildBox.Logf($"%s Compiler\n", compiler ? compiler.name : $"{problem with compiler selection}");
662       return true;
663    }
664
665    bool ProjectPrepareForToolchain(Project project, PrepareMakefileMethod method, bool cleanLog, bool displayCompiler,
666       CompilerConfig compiler, ProjectConfig config)
667    {
668       bool isReady = true;
669       char message[MAX_F_STRING];
670       LogBox logBox = ide.outputView.buildBox;
671
672       ShowOutputBuildLog(cleanLog);
673
674       if(displayCompiler)
675          DisplayCompiler(compiler, false);
676
677       ProjectPrepareCompiler(project, compiler, false);
678       ProjectPrepareMakefile(project, method, compiler, config);
679       return true;
680    }
681
682    bool ProjectPrepareCompiler(Project project, CompilerConfig compiler, bool silent)
683    {
684       if((!project.GenerateCrossPlatformMk(app.includeFile) ||
685             !project.GenerateCompilerCf(compiler, project.topNode.ContainsFilesWithExtension("ec", project.config))) && !silent)
686          ide.outputView.buildBox.Logf($"Error generating compiler configuration (Is the project/config directory writable?)\n");
687       return true;
688    }
689
690    // Note: Compiler is only passed in to for VisualStudio support
691    bool ProjectPrepareMakefile(Project project, PrepareMakefileMethod method, CompilerConfig compiler, ProjectConfig config)
692    {
693       if(compiler.type.isVC)
694       {
695          // I'm guessing we'll want to support generating VS files on Linux as well...
696          ide.statusBar.text = $"Generating Visual Studio Solution...";
697          app.UpdateDisplay();
698          //GenerateVSSolutionFile(project, compiler);
699          ide.statusBar.text = $"Generating Visual Studio Project...";
700          app.UpdateDisplay();
701          //GenerateVCProjectFile(project, compiler, bitDepth);
702          ide.statusBar.text = null;
703          app.UpdateDisplay();
704          return true;
705       }
706       else
707       {
708          char makefilePath[MAX_LOCATION];
709          char makefileName[MAX_LOCATION];
710          bool exists;
711          LogBox logBox = ide.outputView.buildBox;
712
713          strcpy(makefilePath, project.topNode.path);
714          project.CatMakeFileName(makefileName, config);
715          PathCatSlash(makefilePath, makefileName);
716
717          exists = FileExists(makefilePath);
718          if(method == force ||
719            (method == forceExists && exists) ||
720            (method == normal && (!exists || (config && config.makingModified))))
721          {
722             char * reason;
723             char * action;
724             ide.statusBar.text = $"Generating Makefile & Dependencies..."; // Dependencies?
725             app.UpdateDisplay();
726
727             if((method == normal && !exists) || (method == force && !exists))
728                action = $"Generating ";
729             else if(method == force)
730                action = $"Regenerating ";
731             else if(method == normal || method == forceExists)
732                action = $"Updating ";
733             else
734                action = "";
735             if(!exists)
736                reason = $"Makefile doesn't exist. ";
737             else if(project.topNode.modified)
738                reason = $"Project has been modified. ";
739             else
740                reason = "";
741
742             //logBox.Logf("%s\n", makefileName);
743             logBox.Logf($"%s - %s%smakefile for %s config...\n", makefileName, reason, action, GetConfigName(config));
744
745             if(!project.GenerateMakefile(null, false, null, config))
746                ide.outputView.buildBox.Logf($"Error generating makefile (Is the project directory writable?)\n");
747
748             ide.statusBar.text = null;
749             app.UpdateDisplay();
750             return true;
751          }
752       }
753       return false;
754    }
755
756    bool BuildInterrim(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config, int bitDepth, bool justPrint)
757    {
758       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
759       {
760          ide.outputView.buildBox.Logf($"Building project %s using the %s configuration...\n", prj.name, GetConfigName(config));
761          return Build(prj, buildType, compiler, config, bitDepth, justPrint);
762       }
763       return false;
764    }
765
766    bool DebugStopForMake(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config)
767    {
768       bool result = false;
769       // TOFIX: DebugStop is being abused and backfiring on us.
770       //        It's supposed to be the 'Debug/Stop' item, not unloading executable or anything else
771
772       //        configIsInDebugSession seems to be used for two OPPOSITE things:
773       //        If we're debugging another config, we need to unload the executable!
774       //        In building, we want to stop if we're debugging the 'same' executable
775       if(buildType != run) ///* && prj == project*/ && prj.configIsInDebugSession)
776       {
777          if(buildType == start || buildType == restart)
778          {
779             if(ide.debugger && ide.debugger.isPrepared)
780                result = DebugStop();
781          }
782          else
783          {
784             if(ide.project == prj && ide.debugger && ide.debugger.prjConfig == config && ide.debugger.isPrepared)
785                result = DebugStop();
786          }
787       }
788       app.ProcessInput(true);
789       ShowOutputBuildLog(false);
790       return result;
791    }
792
793    bool Build(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config, int bitDepth, bool justPrint)
794    {
795       bool result = true;
796       Window document;
797
798       stopBuild = false;
799       for(document = master.firstChild; document; document = document.next)
800       {
801          if(document.modifiedDocument)
802          {
803             ProjectNode node = GetNodeFromWindow(document, prj, true, false, null);
804             if(node && !document.MenuFileSave(null, 0))
805             {
806                result = false;
807                break;
808             }
809          }
810       }
811       if(result)
812       {
813          DirExpression targetDir = prj.GetTargetDir(compiler, config, bitDepth);
814
815          DebugStopForMake(prj, buildType, compiler, config);
816
817          // TODO: Disabled until problems fixed... is it fixed?
818          if(buildType == rebuild || (config && config.compilingModified))
819             prj.Clean(compiler, config, bitDepth, clean, justPrint);
820          else
821          {
822             if(buildType == relink || (config && config.linkingModified))
823                prj.Clean(compiler, config, bitDepth, cleanTarget, false);
824             if(config && config.symbolGenModified)
825             {
826                DirExpression objDir = prj.GetObjDir(compiler, config, bitDepth);
827                char fileName[MAX_LOCATION];
828                char moduleName[MAX_FILENAME];
829                strcpy(fileName, prj.topNode.path);
830                PathCatSlash(fileName, objDir.dir);
831                ReplaceSpaces(moduleName, prj.moduleName);
832                strcat(moduleName, ".main.ec");
833                PathCatSlash(fileName, moduleName);
834                if(FileExists(fileName))
835                   DeleteFile(fileName);
836                ChangeExtension(fileName, "c", fileName);
837                if(FileExists(fileName))
838                   DeleteFile(fileName);
839                ChangeExtension(fileName, "o", fileName);
840                if(FileExists(fileName))
841                   DeleteFile(fileName);
842
843                delete objDir;
844             }
845          }
846          buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
847          ide.AdjustBuildMenus();
848          ide.AdjustDebugMenus();
849
850          result = prj.Build(buildType, null, compiler, config, bitDepth, justPrint, normal);
851
852          if(config)
853          {
854             config.compilingModified = false;
855             if(!stopBuild)
856                config.linkingModified = false;
857
858             config.symbolGenModified = false;
859          }
860          buildInProgress = none;
861          ide.AdjustBuildMenus();
862          ide.AdjustDebugMenus();
863
864          ide.workspace.modified = true;
865
866          delete targetDir;
867       }
868       return result;
869    }
870
871    //                          ((( USER ACTIONS )))
872    //
873    //  ************************************************************************
874    //  *** Methods below should atomically start a process, and as such     ***
875    //  *** they can query compiler and config directly from ide and project ***
876    //  *** but ONLY ONCE!!!                                                 ***
877    //  ************************************************************************
878
879    bool ProjectBuild(MenuItem selection, Modifiers mods)
880    {
881       if(buildInProgress == none)
882       {
883          Project prj = project;
884          CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
885          int bitDepth = ide.workspace.bitDepth;
886          ProjectConfig config;
887          if(selection || !ide.activeClient)
888          {
889             DataRow row = fileList.currentRow;
890             ProjectNode node = row ? (ProjectNode)row.tag : null;
891             if(node) prj = node.project;
892          }
893          else
894          {
895             ProjectNode node = GetNodeForCompilationFromWindow(ide.activeClient, true, null, null);
896             if(node)
897                prj = node.project;
898          }
899          config = prj.config;
900          if(/*prj != project || */!prj.GetConfigIsInDebugSession(config) || !ide.DontTerminateDebugSession($"Project Build"))
901          {
902             BuildInterrim(prj, build, compiler, config, bitDepth, mods.ctrl && mods.shift);
903          }
904          delete compiler;
905       }
906       else
907          stopBuild = true;
908       return true;
909    }
910
911    bool ProjectInstall(MenuItem selection, Modifiers mods)
912    {
913       Project prj = project;
914       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
915       int bitDepth = ide.workspace.bitDepth;
916       ProjectConfig config;
917       if(selection || !ide.activeClient)
918       {
919          DataRow row = fileList.currentRow;
920          ProjectNode node = row ? (ProjectNode)row.tag : null;
921          if(node) prj = node.project;
922       }
923       else
924       {
925          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, true, false, null);
926          if(node)
927             prj = node.project;
928       }
929       config = prj.config;
930       if(!prj.GetConfigIsInDebugSession(config) ||
931             (!ide.DontTerminateDebugSession($"Project Install") && DebugStopForMake(prj, relink, compiler, config)))
932       {
933          BuildInterrim(prj, build, compiler, config, bitDepth, mods.ctrl && mods.shift);
934          if(ProjectPrepareForToolchain(prj, normal, false, false, compiler, config))
935          {
936             ide.outputView.buildBox.Logf($"\nInstalling project %s using the %s configuration...\n", prj.name, GetConfigName(config));
937             Build(prj, install, compiler, config, bitDepth, mods.ctrl && mods.shift);
938          }
939       }
940       delete compiler;
941       return true;
942    }
943
944    bool ProjectLink(MenuItem selection, Modifiers mods)
945    {
946       Project prj = project;
947       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
948       int bitDepth = ide.workspace.bitDepth;
949       ProjectConfig config;
950       if(selection || !ide.activeClient)
951       {
952          DataRow row = fileList.currentRow;
953          ProjectNode node = row ? (ProjectNode)row.tag : null;
954          if(node) prj = node.project;
955       }
956       else
957       {
958          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, true, false, null);
959          if(node)
960             prj = node.project;
961       }
962       config = prj.config;
963       if(!prj.GetConfigIsInDebugSession(config) ||
964             (!ide.DontTerminateDebugSession($"Project Link") && DebugStopForMake(prj, relink, compiler, config)))
965       {
966          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
967          {
968             ide.outputView.buildBox.Logf($"Relinking project %s using the %s configuration...\n", prj.name, GetConfigName(config));
969             if(config)
970                config.linkingModified = true;
971             Build(prj, relink, compiler, config, bitDepth, mods.ctrl && mods.shift);
972          }
973       }
974       delete compiler;
975       return true;
976    }
977
978    bool ProjectRebuild(MenuItem selection, Modifiers mods)
979    {
980       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
981       int bitDepth = ide.workspace.bitDepth;
982       Project prj = project;
983       ProjectConfig config;
984       if(selection || !ide.activeClient)
985       {
986          DataRow row = fileList.currentRow;
987          ProjectNode node = row ? (ProjectNode)row.tag : null;
988          if(node) prj = node.project;
989       }
990       else
991       {
992          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, true, false, null);
993          if(node)
994             prj = node.project;
995       }
996       config = prj.config;
997       if(!prj.GetConfigIsInDebugSession(config) ||
998             (!ide.DontTerminateDebugSession($"Project Rebuild") && DebugStopForMake(prj, rebuild, compiler, config)))
999       {
1000          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
1001          {
1002             ide.outputView.buildBox.Logf($"Rebuilding project %s using the %s configuration...\n", prj.name, GetConfigName(config));
1003             /*if(config)
1004             {
1005                config.compilingModified = true;
1006                config.makingModified = true;
1007             }*/ // -- should this still be used depite the new solution of BuildType?
1008             Build(prj, rebuild, compiler, config, bitDepth, mods.ctrl && mods.shift);
1009          }
1010       }
1011       delete compiler;
1012       return true;
1013    }
1014
1015    bool ProjectCleanTarget(MenuItem selection, Modifiers mods)
1016    {
1017       CleanProject($"Project Clean Target", $"Cleaning project %s target using the %s configuration...\n", selection, cleanTarget, mods.ctrl && mods.shift);
1018       return true;
1019    }
1020
1021    bool ProjectClean(MenuItem selection, Modifiers mods)
1022    {
1023       CleanProject($"Project Clean", $"Cleaning project %s using the %s configuration...\n", selection, clean, mods.ctrl && mods.shift);
1024       return true;
1025    }
1026
1027    bool ProjectRealClean(MenuItem selection, Modifiers mods)
1028    {
1029       CleanProject($"Project Real Clean", $"Removing intermediate objects directory for project %s using the %s configuration...\n", selection, realClean, mods.ctrl && mods.shift);
1030       return true;
1031    }
1032
1033    void CleanProject(char * terminateDebugSessionMessage, char * cleaningMessageLogFormat, MenuItem selection, CleanType cleanType, bool justPrint)
1034    {
1035       Project prj = project;
1036       Array<Project> projects { };
1037       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1038       int bitDepth = ide.workspace.bitDepth;
1039       if(selection)
1040       {
1041          OldLink item;
1042          OldList selectedRows;
1043          List<ProjectNode> nodes { };
1044          fileList.GetMultiSelection(selectedRows);
1045          for(item = selectedRows.first; item; item = item.next)
1046          {
1047             OldLink i;
1048             DataRow row = item.data;
1049             ProjectNode node = (ProjectNode)row.tag;
1050             if(node.type == project)
1051                projects.Add(node.project);
1052          }
1053          selectedRows.Free(null);
1054       }
1055       if(selection || !ide.activeClient)
1056       {
1057          DataRow row = fileList.currentRow;
1058          ProjectNode node = row ? (ProjectNode)row.tag : null;
1059          if(node) prj = node.project;
1060          if(projects.count == 0)
1061             projects.Add(prj);
1062       }
1063       else
1064       {
1065          // TODO: a file can belong to more than one project, ask which one to clean
1066          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, true, false, null);
1067          if(node) prj = node.project;
1068          if(projects.count == 0)
1069             projects.Add(prj);
1070       }
1071       for(prj : projects)
1072       {
1073          ProjectConfig config = prj.config;
1074          if(!prj.GetConfigIsInDebugSession(config) ||
1075                (!ide.DontTerminateDebugSession(terminateDebugSessionMessage) && DebugStopForMake(prj, clean, compiler, config)))
1076          {
1077             if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
1078             {
1079                ide.outputView.buildBox.Logf(cleaningMessageLogFormat, prj.name, GetConfigName(config));
1080
1081                buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
1082                ide.AdjustBuildMenus();
1083                ide.AdjustDebugMenus();
1084
1085                prj.Clean(compiler, config, bitDepth, cleanType, justPrint);
1086                buildInProgress = none;
1087                ide.AdjustBuildMenus();
1088                ide.AdjustDebugMenus();
1089             }
1090          }
1091       }
1092       delete compiler;
1093       delete projects;
1094    }
1095
1096    bool ProjectRegenerate(MenuItem selection, Modifiers mods)
1097    {
1098       Project prj = project;
1099       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1100       ShowOutputBuildLog(true);
1101       if(selection || !ide.activeClient)
1102       {
1103          DataRow row = fileList.currentRow;
1104          ProjectNode node = row ? (ProjectNode)row.tag : null;
1105          if(node)
1106             prj = node.project;
1107       }
1108       else
1109       {
1110          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, true, false, null);
1111          if(node)
1112             prj = node.project;
1113       }
1114
1115       DisplayCompiler(compiler, false);
1116       ProjectPrepareCompiler(project, compiler, false);
1117       ProjectPrepareMakefile(prj, force, compiler, prj.config);
1118       delete compiler;
1119       return true;
1120    }
1121
1122    bool Compile(Project project, List<ProjectNode> nodes, bool justPrint, SingleFileCompileMode mode)
1123    {
1124       bool result = true;
1125       char fileName[MAX_LOCATION];
1126       Window document;
1127       ProjectConfig config = project.config;
1128
1129       stopBuild = false;
1130
1131       for(document = ide.firstChild; document; document = document.next)
1132       {
1133          if(document.modifiedDocument)
1134          {
1135             ProjectNode n = GetNodeFromWindow(document, project, true, mode == cObject ? true : false, null);
1136             for(node : nodes)
1137             {
1138                if(n && n.IsInNode(node) && !document.MenuFileSave(null, 0))
1139                {
1140                   ide.outputView.buildBox.Logf($"Unable to save %s file.\n", node.name);
1141                   result = false;
1142                   break;
1143                }
1144             }
1145          }
1146       }
1147
1148       if(result)
1149       {
1150          CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1151          int bitDepth = ide.workspace.bitDepth;
1152          result = false;
1153          if(ProjectPrepareForToolchain(project, normal, true, true, compiler, config))
1154          {
1155             if(config)
1156                ide.outputView.buildBox.Logf($"%s specific file(s) in project %s using the %s configuration...\n",
1157                      (mode == normal || mode == cObject) ? $"Compiling" : $"Debug compiling", project.name, config.name);
1158             else
1159                ide.outputView.buildBox.Logf($"%s specific file(s) in project %s...\n",
1160                      (mode == normal || mode == cObject) ? $"Compiling" : $"Debug compiling", project.name);
1161
1162             buildInProgress = compilingFile;
1163             ide.AdjustBuildMenus();
1164             result = project.Compile(nodes, compiler, config, bitDepth, justPrint, mode);
1165             buildInProgress = none;
1166             ide.AdjustBuildMenus();
1167          }
1168          delete compiler;
1169       }
1170       return result;
1171    }
1172
1173    bool Clean(Project project, List<ProjectNode> nodes, bool justPrint)
1174    {
1175       bool result = true;
1176       char fileName[MAX_LOCATION];
1177       Window document;
1178       ProjectConfig config = project.config;
1179
1180       stopBuild = false;
1181
1182       for(document = ide.firstChild; document; document = document.next)
1183       {
1184          if(document.modifiedDocument)
1185          {
1186             ProjectNode n = GetNodeFromWindow(document, project, true, false, null);
1187             for(node : nodes)
1188             {
1189                if(n && n.IsInNode(node) && !document.MenuFileSave(null, 0))
1190                {
1191                   ide.outputView.buildBox.Logf($"Unable to save %s file.\n", node.name);
1192                   result = false;
1193                   break;
1194                }
1195             }
1196          }
1197       }
1198
1199       if(result)
1200       {
1201          CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1202          int bitDepth = ide.workspace.bitDepth;
1203          result = false;
1204          if(ProjectPrepareForToolchain(project, normal, true, true, compiler, config))
1205          {
1206             Map<String, NameCollisionInfo> namesInfo { };
1207             project.topNode.GenMakefileGetNameCollisionInfo(namesInfo, config);
1208             for(node : nodes)
1209             {
1210                if(node.GetIsExcluded(config))
1211                   ide.outputView.buildBox.Logf($"File %s is excluded from current build configuration.\n", node.name);
1212                else
1213                {
1214                   if(config)
1215                      ide.outputView.buildBox.Logf($"Deleting intermediate objects for %s %s in project %s using the %s configuration...\n",
1216                            node.type == file ? $"single file" : $"folder", node.name, project.name, config.name);
1217                   else
1218                      ide.outputView.buildBox.Logf($"Deleting intermediate objects for %s %s in project %s...\n",
1219                            node.type == file ? $"single file" : $"folder", node.name, project.name);
1220
1221                   node.DeleteIntermediateFiles(compiler, config, bitDepth, namesInfo, false);
1222                   result = true;
1223                }
1224             }
1225             namesInfo.Free();
1226             delete namesInfo;
1227          }
1228          delete compiler;
1229       }
1230       return result;
1231    }
1232
1233    bool ProjectNewFile(MenuItem selection, Modifiers mods)
1234    {
1235       DataRow row = fileList.currentRow;
1236       if(row)
1237       {
1238          char fileName[1024];
1239          char filePath[MAX_LOCATION];
1240          ProjectNode parentNode = (ProjectNode)row.tag;
1241          ProjectNode n, fileNode;
1242          parentNode.GetFileSysMatchingPath(filePath);
1243          MakePathRelative(filePath, parentNode.project.topNode.path, filePath);
1244          for(n = parentNode; n && n != parentNode.project.resNode; n = n.parent);
1245          sprintf(fileName, $"Untitled %d", documentID);
1246          fileNode = AddFile(parentNode, fileName, n != null, true);
1247          fileNode.path = CopyUnixPath(filePath);
1248          if(fileNode)
1249          {
1250             NodeProperties nodeProperties
1251             {
1252                parent, this;
1253                position = { position.x + 100, position.y + 100 };
1254                mode = newFile;
1255                node = fileNode;
1256             };
1257             nodeProperties.Create(); // not modal?
1258          }
1259       }
1260       return true;
1261    }
1262
1263    bool ProjectNewFolder(MenuItem selection, Modifiers mods)
1264    {
1265       DataRow row = fileList.currentRow;
1266       if(row)
1267       {
1268          ProjectNode parentNode = (ProjectNode)row.tag;
1269          NewFolder(parentNode, null, true);
1270       }
1271       return true;
1272    }
1273
1274    bool ResourcesAddFiles(MenuItem selection, Modifiers mods)
1275    {
1276       AddFiles(true);
1277       return true;
1278    }
1279
1280    bool ProjectAddFiles(MenuItem selection, Modifiers mods)
1281    {
1282       AddFiles(false);
1283       return true;
1284    }
1285
1286    bool ProjectImportFolder(MenuItem selection, Modifiers mods)
1287    {
1288       DataRow row = fileList.currentRow;
1289       if(row)
1290       {
1291          ProjectNode toNode = (ProjectNode)row.tag;
1292          ImportFolder(toNode);
1293       }
1294       return true;
1295    }
1296
1297    bool ProjectAddNewForm(MenuItem selection, Modifiers mods)
1298    {
1299       CodeEditor codeEditor = CreateNew("Form", "form", "Window", null);
1300       codeEditor.EnsureUpToDate();
1301       ide.RepositionWindows(false);
1302       return true;
1303    }
1304
1305    bool ProjectAddNewGraph(MenuItem selection, Modifiers mods)
1306    {
1307       CodeEditor codeEditor = CreateNew("Graph", "graph", "Block", null);
1308       if(codeEditor)
1309          codeEditor.EnsureUpToDate();
1310       return true;
1311    }
1312
1313    bool ProjectRemove(MenuItem selection, Modifiers mods)
1314    {
1315       DataRow row = fileList.currentRow;
1316       if(row)
1317       {
1318          ProjectNode node = (ProjectNode)row.tag;
1319          if(node.type == project)
1320             RemoveSelectedNodes();
1321       }
1322       return true;
1323    }
1324
1325    bool ProjectUpdateMakefileForAllConfigs(Project project)
1326    {
1327       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1328
1329       // This call really does not belong here:
1330       ide.UpdateToolBarActiveConfigs(false);
1331       for(config : project.configurations)
1332          ProjectPrepareMakefile(project, forceExists, compiler, config);
1333
1334       ide.Update(null);
1335       delete compiler;
1336       return true;
1337    }
1338
1339    bool MenuSettings(MenuItem selection, Modifiers mods)
1340    {
1341       ProjectNode node = GetSelectedNode(true);
1342       Project prj = node ? node.project : project;
1343       projectSettingsDialog = ProjectSettings { master = parent, project = prj, projectNode = node };
1344       incref projectSettingsDialog;
1345       projectSettingsDialog.Modal();
1346       delete projectSettingsDialog;
1347       ide.UpdateToolBarActiveConfigs(false);
1348       Update(null);
1349       ide.AdjustMenus();
1350       return true;
1351    }
1352
1353    bool FileProperties(MenuItem selection, Modifiers mods)
1354    {
1355       DataRow row = fileList.currentRow;
1356       if(row)
1357       {
1358          ProjectNode node = (ProjectNode)row.tag;
1359          NodeProperties { parent = parent, master = this, node = node,
1360                position = { position.x + 100, position.y + 100 } }.Create();
1361       }
1362       return true;
1363    }
1364
1365    bool FileOpenFile(MenuItem selection, Modifiers mods)
1366    {
1367       OpenSelectedNodes(mods.ctrl && mods.shift);
1368       return true;
1369    }
1370
1371    bool FileRemoveFile(MenuItem selection, Modifiers mods)
1372    {
1373       RemoveSelectedNodes();
1374       return true;
1375    }
1376
1377    bool FileCompile(MenuItem selection, Modifiers mods)
1378    {
1379       OldLink item;
1380       OldList selectedRows;
1381       Project project = null;
1382       List<ProjectNode> nodes { };
1383       fileList.GetMultiSelection(selectedRows);
1384       for(item = selectedRows.first; item; item = item.next)
1385       {
1386          OldLink i;
1387          DataRow row = item.data;
1388          ProjectNode node = (ProjectNode)row.tag;
1389          if(!project)
1390             project = node.project;
1391          else if(node.project != project)
1392          {
1393             project = null;
1394             break;
1395          }
1396          nodes.Add(node);
1397       }
1398       selectedRows.Free(null);
1399       if(project)
1400          Compile(project, nodes, mods.ctrl && mods.shift, normal);
1401       else
1402          ide.outputView.buildBox.Logf($"Please select files from a single project.\n");
1403       delete nodes;
1404       return true;
1405    }
1406
1407    bool FileClean(MenuItem selection, Modifiers mods)
1408    {
1409       OldLink item;
1410       OldList selectedRows;
1411       Project project = null;
1412       List<ProjectNode> nodes { };
1413       fileList.GetMultiSelection(selectedRows);
1414       for(item = selectedRows.first; item; item = item.next)
1415       {
1416          OldLink i;
1417          DataRow row = item.data;
1418          ProjectNode node = (ProjectNode)row.tag;
1419          if(!project)
1420             project = node.project;
1421          else if(node.project != project)
1422          {
1423             project = null;
1424             break;
1425          }
1426          nodes.Add(node);
1427       }
1428       selectedRows.Free(null);
1429       if(project)
1430          Clean(project, nodes, mods.ctrl && mods.shift);
1431       else
1432          ide.outputView.buildBox.Logf($"Please select files from a single project.\n");
1433       delete nodes;
1434       return true;
1435    }
1436
1437    bool FileDebugPrecompile(MenuItem selection, Modifiers mods)
1438    {
1439       DataRow row = fileList.currentRow;
1440       ProjectNode node = row ? (ProjectNode)row.tag : null;
1441       if(node)
1442       {
1443          List<ProjectNode> nodes { };
1444          nodes.Add(node);
1445          if(node.type == project)
1446             ProjectBuild(selection, mods);
1447          ide.Update(null);
1448          if(!stopBuild)
1449             Compile(node.project, nodes, mods.ctrl && mods.shift, debugPrecompile);
1450          delete nodes;
1451       }
1452       return true;
1453    }
1454
1455    bool FileDebugCompile(MenuItem selection, Modifiers mods)
1456    {
1457       DataRow row = fileList.currentRow;
1458       ProjectNode node = row ? (ProjectNode)row.tag : null;
1459       if(node)
1460       {
1461          List<ProjectNode> nodes { };
1462          nodes.Add(node);
1463          if(node.type == project)
1464             ProjectBuild(selection, mods);
1465          else
1466             Compile(node.project, nodes, mods.ctrl && mods.shift, normal);
1467          if(!stopBuild)
1468             Compile(node.project, nodes, mods.ctrl && mods.shift, debugCompile);
1469          delete nodes;
1470       }
1471       return true;
1472    }
1473
1474    bool FileDebugGenerateSymbols(MenuItem selection, Modifiers mods)
1475    {
1476       DataRow row = fileList.currentRow;
1477       ProjectNode node = row ? (ProjectNode)row.tag : null;
1478       if(node)
1479       {
1480          List<ProjectNode> nodes { };
1481          nodes.Add(node);
1482          if(node.type == project)
1483             ProjectBuild(selection, mods);
1484          else
1485             Compile(node.project, nodes, mods.ctrl && mods.shift, normal);
1486          if(!stopBuild)
1487             Compile(node.project, nodes, mods.ctrl && mods.shift, debugGenerateSymbols);
1488          delete nodes;
1489       }
1490       return true;
1491    }
1492
1493    Project GetSelectedProject(bool useSelection)
1494    {
1495       Project prj = project;
1496       if(useSelection)
1497       {
1498          DataRow row = fileList.currentRow;
1499          ProjectNode node = row ? (ProjectNode)row.tag : null;
1500          if(node)
1501             prj = node.project;
1502       }
1503       return prj;
1504    }
1505
1506    void SelectNextProject(bool backwards)
1507    {
1508       DataRow row = fileList.currentRow;
1509       DataRow currentRow = row;
1510       ProjectNode node = (ProjectNode)row.tag;
1511       if(node.type != project)
1512          row = node.project.topNode.row;
1513       else if(backwards)
1514          row = row.previous ? row.previous : fileList.lastRow;
1515       if(!backwards)
1516          row = row.next ? row.next : fileList.firstRow;
1517       if(row && row != currentRow)
1518       {
1519          fileList.SelectRow(row);
1520          fileList.currentRow = row;
1521       }
1522    }
1523
1524    ProjectNode GetSelectedNode(bool useSelection)
1525    {
1526       ProjectNode node = null;
1527       if(useSelection)
1528       {
1529          DataRow row = fileList.currentRow;
1530          if(row)
1531             node = (ProjectNode)row.tag;
1532       }
1533       return node;
1534    }
1535
1536    bool MenuBrowseFolder(MenuItem selection, Modifiers mods)
1537    {
1538       char folder[MAX_LOCATION];
1539       Project prj;
1540       ProjectNode node = GetSelectedNode(true);
1541       if(!node)
1542          node = project.topNode;
1543       prj = node.project;
1544
1545       strcpy(folder, prj.topNode.path);
1546       if(node != prj.topNode)
1547          PathCatSlash(folder, node.path);
1548       ShellOpen(folder);
1549       return true;
1550    }
1551
1552    bool Run(MenuItem selection, Modifiers mods)
1553    {
1554       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1555       ProjectConfig config = project.config;
1556       int bitDepth = ide.workspace.bitDepth;
1557       String args = new char[maxPathLen];
1558       args[0] = '\0';
1559       if(ide.workspace.commandLineArgs)
1560          //ide.debugger.GetCommandLineArgs(args);
1561          strcpy(args, ide.workspace.commandLineArgs);
1562       if(ide.debugger.isActive)
1563          project.Run(args, compiler, config, bitDepth);
1564       /*else if(config.targetType == sharedLibrary || config.targetType == staticLibrary)
1565          MessageBox { master = ide, type = ok, text = "Run", contents = "Shared and static libraries cannot be run like executables." }.Modal();*/
1566       else if(BuildInterrim(project, run, compiler, config, bitDepth, false))
1567          project.Run(args, compiler, config, bitDepth);
1568       delete args;
1569       delete compiler;
1570       return true;
1571    }
1572
1573    bool DebugStart()
1574    {
1575       bool result = false;
1576       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1577       ProjectConfig config = project.config;
1578       int bitDepth = ide.workspace.bitDepth;
1579       bool useValgrind = ide.workspace.useValgrind;
1580       TargetTypes targetType = project.GetTargetType(config);
1581       if(targetType == sharedLibrary || targetType == staticLibrary)
1582          MessageBox { master = ide, type = ok, text = $"Run", contents = $"Shared and static libraries cannot be run like executables." }.Modal();
1583       else if(project.GetCompress(config))
1584          MessageBox { master = ide, text = $"Starting Debug", contents = $"Debugging compressed applications is not supported\n" }.Modal();
1585       else if(project.GetDebug(config) ||
1586          MessageBox { master = ide, type = okCancel, text = $"Starting Debug", contents = $"Attempting to debug non-debug configuration\nProceed anyways?" }.Modal() == ok)
1587       {
1588          if(/*!IsProjectModified() ||*/ BuildInterrim(project, start, compiler, config, bitDepth, false))
1589          {
1590             if(compiler.type.isVC)
1591             {
1592                //bool result = false;
1593                char oldwd[MAX_LOCATION];
1594                PathBackup pathBackup { };
1595                char command[MAX_LOCATION];
1596
1597                ide.SetPath(false, compiler, config, bitDepth);
1598
1599                GetWorkingDir(oldwd, sizeof(oldwd));
1600                ChangeWorkingDir(project.topNode.path);
1601
1602                sprintf(command, "%s /useenv %s.sln /projectconfig \"%s|Win32\" /command \"%s\"" , "devenv", project.name, config.name, "Debug.Start");
1603                Execute(command);
1604                ChangeWorkingDir(oldwd);
1605
1606                delete pathBackup;
1607             }
1608             else
1609             {
1610                ide.debugger.Start(compiler, config, bitDepth, useValgrind);
1611                result = true;
1612             }
1613          }
1614       }
1615       delete compiler;
1616       return result;
1617    }
1618
1619    void GoToError(const char * line, const bool noParsing)
1620    {
1621       char * colon;
1622
1623       while(isspace(*line)) line++;
1624       colon = strstr(line, ":");
1625
1626       {
1627          int lineNumber = 0;
1628          int col = 1;
1629          bool lookForLineNumber = true;
1630
1631          // deal with linking error
1632          if(colon && colon[1] == ' ')
1633          {
1634             colon = strstr(colon + 1, ":");
1635             if(colon && !colon[1])
1636             {
1637                colon = strstr(line, ":");
1638                lookForLineNumber = false;
1639             }
1640             else if(colon && !isdigit(colon[1]))
1641             {
1642                line = colon + 1;
1643                colon = strstr(line, ":");
1644             }
1645          }
1646          // Don't be mistaken by the drive letter colon
1647          if(colon && (colon[1] == '/' || colon[1] == '\\'))
1648             colon = strstr(colon + 1, ":");
1649          if(colon && lookForLineNumber)
1650          {
1651             char * comma;
1652 #if 0 // MSVS Errors -- todo fix this later
1653             char * par = RSearchString(line, "(", colon - line, true, false);
1654             if(par && strstr(par, ")"))
1655                colon = par;
1656             else if((colon+1)[0] == ' ')
1657             {
1658                // NOTE: This is the same thing as saying 'colon = line'
1659                for( ; colon != line; colon--)
1660                   /*if(*colon == '(')
1661                      break*/;
1662             }
1663 #endif
1664             lineNumber = atoi(colon + 1);
1665             /*
1666             comma = strchr(colon, ',');
1667             if(comma)
1668                col = atoi(comma+1);
1669             */
1670             comma = strchr(colon+1, ':');
1671             if(comma)
1672                col = atoi(comma+1);
1673          }
1674
1675          {
1676             char moduleName[MAX_LOCATION], filePath[MAX_LOCATION] = "";
1677             char ext[MAX_EXTENSION] = "";
1678             char * bracket;
1679             ProjectNode node = null;
1680             if(colon)
1681             {
1682                char * inFileIncludedFrom = strstr(line, stringInFileIncludedFrom);
1683                char * from = strstr(line, "from ");
1684                char * start = inFileIncludedFrom ? inFileIncludedFrom + strlen(stringInFileIncludedFrom) : from ? from + strlen("from ") : line;
1685                int len;
1686                if(colon < start)
1687                   start = line;
1688                len = Min((int)(colon - start), MAX_LOCATION-1);
1689                // Cut module name
1690                strncpy(moduleName, start, len);
1691                moduleName[len] = '\0';
1692             }
1693             else
1694                strcpy(moduleName, line);
1695
1696             if(!colon)
1697             {
1698                char * msg;
1699                if((msg = strstr(moduleName, " - ")))
1700                {
1701                   bool found = false;
1702                   msg[0] = '\0';
1703                   for(prj : ide.workspace.projects)
1704                   {
1705                      strcpy(filePath, prj.topNode.path);
1706                      PathCatSlash(filePath, moduleName);
1707                      if(FileExists(filePath).isFile)
1708                      {
1709                         found = true;
1710                         break;
1711                      }
1712                   }
1713                   if(!found)
1714                   {
1715                      msg[0] = ' ';
1716                      filePath[0] = '\0';
1717                   }
1718                }
1719                else if((msg = strstr(moduleName, "...")) && (colon = strchr(moduleName, ' ')) && (++colon)[0])
1720                {
1721                   bool found = false;
1722                   msg[0] = '\0';
1723                   for(prj : ide.workspace.projects)
1724                   {
1725                      if((node = prj.resNode.Find(colon, true)))
1726                      {
1727                         strcpy(filePath, prj.topNode.path);
1728                         PathCatSlash(filePath, node.path);
1729                         PathCatSlash(filePath, node.name);
1730
1731                         found = true;
1732                         break;
1733                      }
1734                   }
1735                   if(!found)
1736                   {
1737                      msg[0] = '.';
1738                      filePath[0] = '\0';
1739                   }
1740                }
1741                colon = null;
1742             }
1743             // Remove stuff in brackets
1744             /*
1745             bracket = strstr(moduleName, "(");
1746             if(bracket) *bracket = '\0';
1747             */
1748             MakeSlashPath(moduleName);
1749             GetExtension(moduleName, ext);
1750
1751             if(!colon && !filePath[0])
1752             {
1753                // Check if it's one of our modules
1754                node = project.topNode.Find(moduleName, false);
1755                if(node)
1756                {
1757                   strcpy(filePath, node.path);
1758                   PathCatSlash(filePath, node.name);
1759                }
1760                else
1761                {
1762                   char ext[MAX_EXTENSION];
1763                   GetExtension(moduleName, ext);
1764                   {
1765                      DotMain dotMain = DotMain::FromFileName(moduleName);
1766                      IntermediateFileType type = IntermediateFileType::FromExtension(ext);
1767                      ProjectConfig config = null;
1768                      if(type)
1769                      {
1770                         for(prj : ide.workspace.projects; prj.lastBuildConfigName)
1771                         {
1772                            if((config = prj.GetConfig(prj.lastBuildConfigName)))
1773                               node = prj.FindNodeByObjectFileName(moduleName, type, dotMain, config);
1774                            if(node)
1775                               break;
1776                         }
1777                      }
1778                      if(node)
1779                      {
1780                         char name[MAX_FILENAME];
1781                         Project project = node.project;
1782                         CompilerConfig compiler = ideSettings.GetCompilerConfig(project.lastBuildCompilerName);
1783                         if(compiler)
1784                         {
1785                            int bitDepth = ide.workspace.bitDepth;
1786                            DirExpression objDir = project.GetObjDir(compiler, config, bitDepth);
1787                            strcpy(filePath, project.topNode.path);
1788                            PathCatSlash(filePath, objDir.dir);
1789                            node.GetObjectFileName(name, project.configsNameCollisions[config ? config.name : ""], type, dotMain);
1790                            PathCatSlash(filePath, name);
1791                            delete objDir;
1792                         }
1793                         delete compiler;
1794                      }
1795                   }
1796                }
1797                if(!node)
1798                   filePath[0] = '\0';
1799             }
1800             if(!strcmp(ext, "a") || !strcmp(ext, "o") || !strcmp(ext, "lib") ||
1801                   !strcmp(ext, "dll") || !strcmp(ext, "exe") || !strcmp(ext, "mo"))
1802                moduleName[0] = 0;    // Avoid opening binary files
1803             if(moduleName[0])
1804             {
1805                CodeEditor codeEditor;
1806                if(!filePath[0])
1807                {
1808                   strcpy(filePath, project.topNode.path);
1809                   PathCatSlash(filePath, moduleName);
1810                }
1811
1812                codeEditor = (CodeEditor)ide.OpenFile(filePath, false, true, null, no, normal, noParsing);
1813                if(!codeEditor && !strcmp(ext, "c"))
1814                {
1815                   char ecName[MAX_LOCATION];
1816                   ChangeExtension(filePath, "ec", ecName);
1817                   codeEditor = (CodeEditor)ide.OpenFile(ecName, false, true, null, no, normal, noParsing);
1818                }
1819                if(!codeEditor)
1820                {
1821                   char path[MAX_LOCATION];
1822                   // TOFIX: Improve on this, don't use only filename, make a function
1823                   if(ide && ide.workspace)
1824                   {
1825                      for(prj : ide.workspace.projects)
1826                      {
1827                         ProjectNode node;
1828                         MakePathRelative(filePath, prj.topNode.path, path);
1829
1830                         if((node = prj.topNode.FindWithPath(path, false)))
1831                         {
1832                            strcpy(filePath, prj.topNode.path);
1833                            PathCatSlash(filePath, node.path);
1834                            PathCatSlash(filePath, node.name);
1835                            codeEditor = (CodeEditor)ide.OpenFile(filePath, false, true, null, no, normal, noParsing);
1836                            if(codeEditor)
1837                               break;
1838                         }
1839                      }
1840                      if(!codeEditor && (strchr(moduleName, '/') || strchr(moduleName, '\\')))
1841                      {
1842                         for(prj : ide.workspace.projects)
1843                         {
1844                            ProjectNode node;
1845                            if((node = prj.topNode.FindWithPath(moduleName, false)))
1846                            {
1847                               strcpy(filePath, prj.topNode.path);
1848                               PathCatSlash(filePath, node.path);
1849                               PathCatSlash(filePath, node.name);
1850                               codeEditor = (CodeEditor)ide.OpenFile(filePath, false, true, null, no, normal, noParsing);
1851                               if(codeEditor)
1852                                  break;
1853                            }
1854                         }
1855                      }
1856                      if(!codeEditor)
1857                      {
1858                         GetLastDirectory(moduleName, moduleName);
1859                         for(prj : ide.workspace.projects)
1860                         {
1861                            ProjectNode node;
1862                            if((node = prj.topNode.Find(moduleName, false)))
1863                            {
1864                               strcpy(filePath, prj.topNode.path);
1865                               PathCatSlash(filePath, node.path);
1866                               PathCatSlash(filePath, node.name);
1867                               codeEditor = (CodeEditor)ide.OpenFile(filePath, false, true, null, no, normal, noParsing);
1868                               if(codeEditor)
1869                                  break;
1870                            }
1871                         }
1872                      }
1873                   }
1874                }
1875                if(codeEditor && lineNumber)
1876                {
1877                   EditBox editBox = codeEditor.editBox;
1878                   if(editBox.GoToLineNum(lineNumber - 1))
1879                      editBox.GoToPosition(editBox.line, lineNumber - 1, col ? (col - 1) : 0);
1880                }
1881             }
1882          }
1883       }
1884    }
1885
1886    bool OpenNode(ProjectNode node, bool noParsing)
1887    {
1888       char filePath[MAX_LOCATION];
1889       char ext[MAX_EXTENSION];
1890       node.GetFullFilePath(filePath);
1891       GetExtension(filePath, ext);
1892       if(binaryDocExt.Find(ext))
1893       {
1894          ShellOpen(filePath);
1895          return true;
1896       }
1897       else
1898          return ide.OpenFile(filePath, false, true/*false Why was it opening hidden?*/, null, something, normal, noParsing) ? true : false;
1899    }
1900
1901    void AddNode(ProjectNode node, DataRow addTo)
1902    {
1903       DataRow row = addTo ? addTo.AddRow() : fileList.AddRow();
1904
1905       row.tag = (int64)node;
1906       node.row = row;
1907
1908       if(node.type == resources)
1909          resourceRow = row;
1910
1911       row.SetData(null, node);
1912
1913       if(node.files && node.files.first && node.parent &&
1914             !(!node.parent.parent &&
1915                (!strcmpi(node.name, "notes") || !strcmpi(node.name, "sources") ||
1916                   !strcmpi(node.name, "src") || !strcmpi(node.name, "tools"))))
1917          row.collapsed = true;
1918       else if(node.type == folder)
1919          node.icon = openFolder;
1920
1921       if(node.files)
1922       {
1923          for(child : node.files)
1924             AddNode(child, row);
1925       }
1926    }
1927
1928    void DeleteNode(ProjectNode projectNode)
1929    {
1930       if(projectNode.files)
1931       {
1932          ProjectNode child;
1933          while(child = projectNode.files.first)
1934             DeleteNode(child);
1935       }
1936       fileList.DeleteRow(projectNode.row);
1937       projectNode.Delete();
1938    }
1939
1940    bool ProjectSave(MenuItem selection, Modifiers mods)
1941    {
1942       DataRow row = fileList.currentRow;
1943       ProjectNode node = row ? (ProjectNode)row.tag : null;
1944       Project prj = node ? node.project : null;
1945       if(prj)
1946       {
1947          prj.StopMonitoring();
1948          if(prj.Save(prj.filePath))
1949          {
1950             Project modPrj = null;
1951             prj.topNode.modified = false;
1952             for(p : ide.workspace.projects)
1953             {
1954                if(p.topNode.modified)
1955                {
1956                   modPrj = p;
1957                   break;
1958                }
1959             }
1960             if(!modPrj)
1961                modifiedDocument = false;
1962             Update(null);
1963          }
1964          prj.StartMonitoring();
1965       }
1966       return true;
1967    }
1968
1969    bool ShowOutputBuildLog(bool cleanLog)
1970    {
1971       OutputView output = ide.outputView;
1972       if(cleanLog)
1973          output.ShowClearSelectTab(build);
1974       else
1975       {
1976          output.SelectTab(build);
1977          output.Show();
1978       }
1979       return true;
1980    }
1981
1982    bool DebugRestart()
1983    {
1984       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1985       ProjectConfig config = project.config;
1986       int bitDepth = ide.workspace.bitDepth;
1987       bool useValgrind = ide.workspace.useValgrind;
1988
1989       bool result = false;
1990       if(/*!IsProjectModified() ||*/ BuildInterrim(project, restart, compiler, config, bitDepth, false))
1991       {
1992          // For Restart, compiler and config will only be used if for
1993          // whatever reason (if at all possible) the Debugger is in a
1994          // 'terminated' or 'none' state
1995          ide.debugger.Restart(compiler, config, bitDepth, useValgrind);
1996          result = true;
1997       }
1998
1999       delete compiler;
2000       return result;
2001    }
2002
2003    bool DebugResume()
2004    {
2005       ide.debugger.Resume();
2006       return true;
2007    }
2008
2009    bool DebugBreak()
2010    {
2011       ide.debugger.Break();
2012       return true;
2013    }
2014
2015    bool DebugStop()
2016    {
2017       ide.debugger.Stop();
2018       return true;
2019    }
2020
2021    bool DebugStepInto()
2022    {
2023       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
2024       ProjectConfig config = project.config;
2025       int bitDepth = ide.workspace.bitDepth;
2026       bool useValgrind = ide.workspace.useValgrind;
2027
2028       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config, bitDepth, false)))
2029          ide.debugger.StepInto(compiler, config, bitDepth, useValgrind);
2030       delete compiler;
2031       return true;
2032    }
2033
2034    bool DebugStepOver(bool skip)
2035    {
2036       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
2037       ProjectConfig config = project.config;
2038       int bitDepth = ide.workspace.bitDepth;
2039       bool useValgrind = ide.workspace.useValgrind;
2040
2041       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config, bitDepth, false)))
2042          ide.debugger.StepOver(compiler, config, bitDepth, useValgrind, skip);
2043
2044       delete compiler;
2045       return true;
2046    }
2047
2048    bool DebugStepUntil(bool skip)
2049    {
2050       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
2051       ProjectConfig config = project.config;
2052       int bitDepth = ide.workspace.bitDepth;
2053       bool useValgrind = ide.workspace.useValgrind;
2054
2055       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config, bitDepth, false)))
2056          ide.debugger.StepUntil(compiler, config, bitDepth, useValgrind, skip);
2057
2058       delete compiler;
2059       return true;
2060    }
2061
2062    bool DebugStepOut(bool skip)
2063    {
2064       ide.debugger.StepOut(skip);
2065       return true;
2066    }
2067
2068    void ImportFolder(ProjectNode toNode)
2069    {
2070       if(toNode)
2071       {
2072          char path[MAX_LOCATION];
2073          char currentDir[MAX_LOCATION];
2074          ProjectNode node = toNode;
2075          FileDialog fileDialog = importFileDialog;
2076          fileDialog.master = parent;
2077          while(node)
2078          {
2079             node.GetFullFilePath(path);
2080             while(path[0])
2081             {
2082                StripLastDirectory(path, path);
2083                if(FileExists(path).isDirectory) break;
2084             }
2085             if(path[0] || node == toNode.project.topNode)
2086                node = null;
2087             else
2088                node = toNode.project.topNode;
2089          }
2090          MakeSystemPath(path);
2091          StripLastDirectory(path, currentDir);
2092          fileDialog.currentDirectory = currentDir[0] ? currentDir : path;
2093          fileDialog.filePath = path;
2094          if(fileDialog.Modal() == ok)
2095          {
2096             ImportFolderFSI fsi { projectView = this };
2097             fsi.stack.Add(toNode);
2098             fsi.Iterate(fileDialog.filePath);
2099             delete fsi;
2100          }
2101       }
2102    }
2103
2104    ProjectNode NewFolder(ProjectNode parentNode, char * name, bool showProperties)
2105    {
2106       if(parentNode)
2107       {
2108          ProjectNode folderNode;
2109          Project prj = parentNode.project;
2110          int c;
2111          ProjectNode after = null;
2112          for(node : parentNode.files)
2113          {
2114             if(node.type != folder)
2115                break;
2116             after = node;
2117          }
2118
2119          if(name && name[0])
2120             folderNode = parentNode.Add(prj, name, after, folder, folder, true);
2121          else
2122          {
2123             for(c = 0; c < 100; c++)
2124             {
2125                char string[16];
2126                sprintf(string, c ? "New Folder (%d)" : "New Folder", c);
2127                if((folderNode = parentNode.Add(prj, string, after, folder, folder, true)))
2128                   break;
2129             }
2130          }
2131
2132          if(folderNode)
2133          {
2134             NodeProperties nodeProperties;
2135             if(!showProperties)
2136             {
2137                modifiedDocument = true;
2138                prj.topNode.modified = true;
2139             }
2140             Update(null);
2141             folderNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
2142             folderNode.row.tag = (int64)folderNode;
2143
2144             folderNode.row.SetData(null, folderNode);
2145             fileList.currentRow = folderNode.row;
2146
2147             if(showProperties)
2148             {
2149                nodeProperties = NodeProperties
2150                {
2151                   parent, this, mode = newFolder, node = folderNode;
2152                   position = { position.x + 100, position.y + 100 };
2153                };
2154                nodeProperties.Create();   // Modal?
2155             }
2156             return folderNode;
2157          }
2158       }
2159       return null;
2160    }
2161
2162    void AddFiles(bool resources)
2163    {
2164       FileDialog fileDialog = (!resources) ? this.fileDialog : resourceFileDialog;
2165       fileDialog.type = multiOpen;
2166       fileDialog.text = !resources ? $"Add Files to Project" : $"Add Resources to Project";
2167       fileDialog.master = parent;
2168
2169       if(fileDialog.Modal() == ok)
2170       {
2171          int c;
2172          DataRow row = fileList.currentRow;
2173          ProjectNode parentNode = (ProjectNode)row.tag;
2174          bool addFailed = false;
2175          int numSelections = fileDialog.numSelections;
2176          char ** multiFilePaths = fileDialog.multiFilePaths;
2177
2178          Array<String> nameConflictFiles { };
2179
2180          for(c = 0; c < numSelections; c++)
2181          {
2182             char * filePath = multiFilePaths[c];
2183             FileAttribs exists = FileExists(filePath);
2184             bool addThisFile = true;
2185
2186             if(exists.isDirectory)
2187                addThisFile = false;
2188             else if(!exists)
2189             {
2190                if(MessageBox { master = ide, type = yesNo, text = filePath,
2191                      contents = $"File doesn't exist. Create?" }.Modal() == yes)
2192                {
2193                   File f = FileOpen(filePath, write);
2194                   if(f)
2195                   {
2196                      addThisFile = true;
2197                      delete f;
2198                   }
2199                   else
2200                   {
2201                      MessageBox { master = ide, type = ok, text = filePath,
2202                            contents = $"Couldn't create file."}.Modal();
2203                      addThisFile = false;
2204                   }
2205                }
2206             }
2207
2208             if(addThisFile)
2209             {
2210                /*addFailed = */if(!AddFile(parentNode, filePath, resources, false))//;
2211                {
2212                   nameConflictFiles.Add(CopyString(filePath));
2213                   addFailed = true;
2214                }
2215             }
2216          }
2217          if(addFailed)
2218          {
2219             int len = 0;
2220             char * part1 = $"The following file";
2221             char * opt1 = $" was ";
2222             char * opt2 = $"s were ";
2223             char * part2 = $"not added because of identical file name conflict within the project.\n\n";
2224             char * message;
2225             len += strlen(part1);
2226             len += strlen(part2);
2227             len += nameConflictFiles.count > 1 ? strlen(opt2) : strlen(opt1);
2228             for(s : nameConflictFiles)
2229                len += strlen(s) + 1;
2230             message = new char[len + 1];
2231             strcpy(message, part1);
2232             strcat(message, nameConflictFiles.count > 1 ? opt2 : opt1);
2233             strcat(message, part2);
2234             for(s : nameConflictFiles)
2235             {
2236                strcat(message, s);
2237                strcat(message, "\n");
2238             }
2239             MessageBox { master = ide, type = ok, text = $"Name Conflict",
2240                   contents = message }.Modal();
2241             delete message;
2242          }
2243          nameConflictFiles.Free();
2244          delete nameConflictFiles;
2245       }
2246    }
2247
2248    ProjectNode AddFile(ProjectNode parentNode, char * filePath, bool resources, bool isTemporary)
2249    {
2250       ProjectNode result = null;
2251       ProjectNode after = null;
2252       for(node : parentNode.files)
2253       {
2254          if(node.type != folder && node.type != file && node.type)
2255             break;
2256          after = node;
2257       }
2258
2259       result = parentNode.Add(parentNode.project, filePath, after, file, NodeIcons::SelectFileIcon(filePath), !resources);
2260
2261       if(result)
2262       {
2263          if(!isTemporary)
2264          {
2265             modifiedDocument = true;
2266             parentNode.project.topNode.modified = true;
2267             parentNode.project.ModifiedAllConfigs(true, false, true, true);
2268          }
2269          Update(null);
2270          result.row = parentNode.row.AddRowAfter(after ? after.row : null);
2271          result.row.tag = (int64)result;
2272          result.row.SetData(null, result);
2273       }
2274       return result;
2275    }
2276
2277    CodeEditor CreateNew(char * upper, char * lower, char * base, char * className)
2278    {
2279       CodeEditor codeEditor = null;
2280       ProjectNode projectNode;
2281       ProjectNode after = null;
2282       DataRow row = fileList.currentRow;
2283       ProjectNode parentNode;
2284       int c;
2285
2286       if(!row) row = project.topNode.row;
2287
2288       parentNode = (ProjectNode)row.tag;
2289
2290       for(node : parentNode.files)
2291       {
2292          if(node.type != folder && node.type != file && node.type)
2293             break;
2294          after = node;
2295       }
2296       for(c = 1; c < 100; c++)
2297       {
2298          char string[16];
2299          sprintf(string, c ? "%s%d.ec" : "%s.ec", lower, c);
2300          if((projectNode = parentNode.Add(project, string, after, file, genFile, true)))
2301             break;
2302       }
2303       if(projectNode)
2304       {
2305          char name[256];
2306          char filePath[MAX_LOCATION];
2307
2308          modifiedDocument = true;
2309          project.topNode.modified = true;
2310          Update(null);
2311          project.ModifiedAllConfigs(true, false, false, true);
2312          projectNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
2313          projectNode.row.tag =(int64)projectNode;
2314
2315          projectNode.row.SetData(null, projectNode);
2316          fileList.currentRow = projectNode.row;
2317
2318          strcpy(filePath, project.topNode.path);
2319          PathCat(filePath, projectNode.path);
2320          PathCat(filePath, projectNode.name);
2321
2322          codeEditor = (CodeEditor)ide.FindWindow(filePath);
2323          if(!codeEditor)
2324          {
2325             Class baseClass = eSystem_FindClass(__thisModule, base);
2326             subclass(ClassDesignerBase) designerClass = eClass_GetDesigner(baseClass);
2327             if(designerClass)
2328             {
2329                codeEditor = (CodeEditor)ide.OpenFile(filePath, false, false, null, whatever, normal, false);
2330                strcpy(name, projectNode.name);
2331                sprintf(name, "%s%d", upper, c);
2332                if(className)
2333                   strcpy(className, name);
2334
2335                designerClass.CreateNew(codeEditor.editBox, codeEditor.clientSize, name, base);
2336
2337                //codeEditor.modifiedDocument = false;
2338                //codeEditor.designer.modifiedDocument = false;
2339
2340                //Code_EnsureUpToDate(codeEditor);
2341             }
2342          }
2343          else // TODO: fix no symbols generated when ommiting {} for following else
2344          {
2345             codeEditor = (CodeEditor)ide.OpenFile(filePath, false, false, null, whatever, normal, false);
2346          }
2347          ide.sheet.visible = true;
2348          ide.sheet.Activate();
2349          if(codeEditor)
2350          {
2351             codeEditor.ViewDesigner();
2352             codeEditor.codeModified = true;
2353          }
2354       }
2355       //visible = false;
2356       return codeEditor;
2357    }
2358
2359    // Returns true if we opened something
2360    bool OpenSelectedNodes(bool noParsing)
2361    {
2362       bool result = false;
2363       OldList selection;
2364       OldLink item;
2365
2366       fileList.GetMultiSelection(selection);
2367       for(item = selection.first; item; item = item.next)
2368       {
2369          DataRow row = item.data;
2370          ProjectNode node = (ProjectNode)row.tag;
2371          if(node.type == file)
2372          {
2373             OpenNode(node, noParsing);
2374             result = true;
2375          }
2376       }
2377       selection.Free(null);
2378       ide.RepositionWindows(false);
2379       return result;
2380    }
2381
2382    void RemoveSelectedNodes()
2383    {
2384       OldList selection;
2385       OldLink item, next;
2386
2387       fileList.GetMultiSelection(selection);
2388
2389       // Remove children of parents we're deleting
2390       for(item = selection.first; item; item = next)
2391       {
2392          OldLink i;
2393          DataRow row = item.data;
2394          ProjectNode n, node = (ProjectNode)row.tag;
2395          bool remove = false;
2396
2397          next = item.next;
2398          for(i = selection.first; i && !remove; i = i.next)
2399          {
2400             ProjectNode iNode = (ProjectNode)((DataRow)i.data).tag;
2401             for(n = node.parent; n; n = n.parent)
2402             {
2403                if(iNode == n)
2404                {
2405                   remove = true;
2406                   break;
2407                }
2408             }
2409          }
2410          if(remove)
2411             selection.Delete(item);
2412       }
2413
2414       for(item = selection.first; item; item = item.next)
2415       {
2416          DataRow row = item.data;
2417          ProjectNode node = (ProjectNode)row.tag;
2418          ProjectNode resNode;
2419          for(resNode = node.parent; resNode; resNode = resNode.parent)
2420             if(resNode.type == resources)
2421                break;
2422          if(node.type == file)
2423          {
2424             Project prj = node.project;
2425             DeleteNode(node);
2426             modifiedDocument = true;
2427             prj.topNode.modified = true;
2428             Update(null);
2429             prj.ModifiedAllConfigs(true, false, true, true);
2430          }
2431          else if(node.type == folder)
2432          {
2433             char message[1024];
2434             sprintf(message, $"Are you sure you want to remove the folder \"%s\"\n"
2435                   "and all of its contents from the project?", node.name);
2436             if(MessageBox { master = ide, type = yesNo, text = $"Delete Folder", contents = message }.Modal() == yes)
2437             {
2438                Project prj = node.project;
2439                if(node.containsFile)
2440                   prj.ModifiedAllConfigs(true, false, true, true);
2441                DeleteNode(node);
2442                modifiedDocument = true;
2443                prj.topNode.modified = true;
2444             }
2445          }
2446          else if(node.type == project && node != project.topNode && !buildInProgress)
2447          {
2448             char message[1024];
2449             Project prj = null;
2450             for(p : workspace.projects)
2451             {
2452                if(p.topNode == node)
2453                {
2454                   prj = p;
2455                   break;
2456                }
2457             }
2458             sprintf(message, $"Are you sure you want to remove the \"%s\" project\n" "from this workspace?", node.name);
2459             if(MessageBox { master = ide, type = yesNo, text = $"Remove Project", contents = message }.Modal() == yes)
2460             {
2461                // THIS GOES FIRST!
2462                DeleteNode(node);
2463                if(prj)
2464                   workspace.RemoveProject(prj);
2465                //modifiedDocument = true; // when project view is a workspace view
2466             }
2467          }
2468       }
2469       selection.Free(null);
2470    }
2471 }