13390d54fe7bf50e1d138584e8eeecf6573ebec0
[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"))
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, shift = true }:
430                case space:
431                {
432                   if(node.type == NodeTypes::project)
433                   {
434                      Project prj = null;
435                      for(p : workspace.projects)
436                      {
437                         if(p.topNode == node)
438                         {
439                            prj = p;
440                            break;
441                         }
442                      }
443                      prj.RotateActiveConfig(!key.shift);
444                      if(prj == project)
445                         ide.AdjustMenus();
446                      return false;
447                   }
448                   break;
449                }
450             }
451          }
452          switch(key)
453          {
454             case Key { enter, true, true }:        OpenSelectedNodes(true);   break;
455             case Key { keyPadEnter, true, true }:  OpenSelectedNodes(true);   break;
456             case enter: case keyPadEnter:          OpenSelectedNodes(false);  break;
457             case del:                              RemoveSelectedNodes();     break;
458             case escape:                      
459             {
460                Window activeClient = ide.activeClient;
461                if(activeClient)
462                   activeClient.Activate();
463                else
464                   ide.RepositionWindows(true);
465                break;
466             }
467          }
468          return true;
469       }
470
471       bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
472       {
473          ProjectNode node = (ProjectNode)row.tag;
474          if(node.type == folder)
475             node.icon = collapsed ? folder : openFolder;
476          return true;
477       }
478    };
479
480    FileDialog importFileDialog { autoCreate = false, type = selectDir, text = $"Import Folder" };
481    FileDialog projectFileDialog
482    {
483       autoCreate = false, filters = projectFilters.array, sizeFilters = projectFilters.count * sizeof(FileFilter);
484       types = projectTypes.array, sizeTypes = projectTypes.count * sizeof(FileType);
485    };
486    FileDialog fileDialog
487    {
488       autoCreate = false, mayNotExist = true, filters = fileFilters.array, sizeFilters = fileFilters.count * sizeof(FileFilter);
489    };
490    FileDialog resourceFileDialog
491    {
492       autoCreate = false, mayNotExist = true, filters = resourceFilters.array, sizeFilters = resourceFilters.count * sizeof(FileFilter);
493    };
494
495    Menu fileMenu { menu, $"File", f };
496    MenuItem { fileMenu, $"Save", s, Key { s, ctrl = true }, NotifySelect = MenuFileSave };
497    // MenuItem { fileMenu, "Save As...", a, NotifySelect = MenuFileSaveAs };
498
499    bool OnClose(bool parentClosing)
500    {
501       if(!parentClosing && visible)
502       {
503          visible = false;
504          return false;
505       }
506       if(buildInProgress)
507          return false;
508
509       if(modifiedDocument)
510       {
511          DialogResult dialogRes;
512          char msg[2048];
513          bool first = true;
514          strcpy(msg, $"You have modified projects.\nSave changes to ");
515          for(p : ide.workspace.projects)
516          {
517             if(p.topNode.modified)
518             {
519                if(!first) strcat(msg, ", ");
520                strcat(msg, p.name);
521                first = false;
522             }
523          }
524          strcat(msg, "?");
525
526          dialogRes = MessageBox { master = master, type = yesNoCancel, text = parent.caption ? parent.caption : rootWindow.caption, contents = msg }.Modal();
527
528          if(dialogRes == yes)
529          {
530             // TOFIX: Precomp error if brackets are taken out
531             return (DialogResult)MenuFileSave(null, 0) != cancel;
532          }
533          else if(dialogRes == cancel)
534             return false;
535          modifiedDocument = false;
536       }
537       return true;
538    }
539
540    void OnDestroy(void)
541    {
542       //if(ide.findInFilesDialog && ide.findInFilesDialog.created && ide.findInFilesDialog.mode != directory)
543       //   ide.findInFilesDialog.SearchStop();
544
545       ide.outputView.buildBox.Clear();
546       ide.outputView.debugBox.Clear();
547       //ide.outputView.findBox.Clear();
548       ide.callStackView.Clear();
549       ide.watchesView.Clear();
550       ide.threadsView.Clear();
551       ide.breakpointsView.Clear();
552       ide.outputView.ShowClearSelectTab(find); // why this? 
553    }
554
555    bool OnSaveFile(char * fileName)
556    {
557       for(prj : ide.workspace.projects)
558       {
559          if(prj.topNode.modified)
560          {
561             prj.StopMonitoring();
562             if(prj.Save(prj.filePath))
563                prj.topNode.modified = false;
564             prj.StartMonitoring();
565          }
566       }
567       modifiedDocument = false;
568       Update(null);
569       return true;
570    }
571
572    bool IsModuleInProject(char * filePath)
573    {
574       char moduleName[MAX_FILENAME]; //, modulePath[MAX_LOCATION];
575       GetLastDirectory(filePath, moduleName);
576       return project.topNode.Find(moduleName, false) != null;
577    }
578
579    ProjectNode GetNodeFromWindow(Window document, Project project, bool isCObject)
580    {
581       if(document.fileName)
582       {
583          char winFileName[MAX_LOCATION];
584          char * documentFileName = GetSlashPathBuffer(winFileName, document.fileName);
585          ProjectNode node;
586          Project prj;
587          if(isCObject)
588          {
589             char name[MAX_FILENAME];
590             GetLastDirectory(documentFileName, name);
591             ChangeExtension(name, "ec", name);
592             for(p : ide.workspace.projects)
593             {
594                prj = project ? project : p;
595                if((node = prj.topNode.Find(name, false)))
596                   return node;
597                if(project) break;
598             }
599          }
600          else
601          {
602             for(p : ide.workspace.projects)
603             {
604                prj = project ? project : p;
605                if((node = prj.topNode.FindByFullPath(documentFileName, false)))
606                   return node;
607                if(project) break;
608             }
609          }
610       }
611       return null;
612    }
613
614    //                          ((( UTILITY FUNCTIONS )))
615    //
616    //  ************************************************************************
617    //  *** These methods below are part of a sequence of events, and as     ***
618    //  *** such they must be passed the current compiler and project config ***
619    //  ************************************************************************
620    bool DisplayCompiler(CompilerConfig compiler, bool cleanLog)
621    {
622       ide.outputView.buildBox.Logf($"%s Compiler\n", compiler ? compiler.name : $"{problem with compiler selection}");
623       return true;
624    }
625
626    bool ProjectPrepareForToolchain(Project project, PrepareMakefileMethod method, bool cleanLog, bool displayCompiler,
627       CompilerConfig compiler, ProjectConfig config)
628    {
629       bool isReady = true;
630       char message[MAX_F_STRING];
631       LogBox logBox = ide.outputView.buildBox;
632
633       ShowOutputBuildLog(cleanLog);
634
635       if(displayCompiler)
636          DisplayCompiler(compiler, false);
637
638       ProjectPrepareCompiler(project, compiler, false);
639       ProjectPrepareMakefile(project, method, compiler, config);
640       return true;
641    }
642
643    bool ProjectPrepareCompiler(Project project, CompilerConfig compiler, bool silent)
644    {
645       if((!project.GenerateCrossPlatformMk(app.includeFile) || !project.GenerateCompilerCf(compiler)) && !silent)
646          ide.outputView.buildBox.Logf($"Error generating compiler configuration (Is the project/config directory writable?)\n");
647       return true;
648    }
649
650    // Note: Compiler is only passed in to for VisualStudio support
651    bool ProjectPrepareMakefile(Project project, PrepareMakefileMethod method, CompilerConfig compiler, ProjectConfig config)
652    {
653       if(compiler.type.isVC)
654       {
655          // I'm guessing we'll want to support generating VS files on Linux as well...
656          ide.statusBar.text = $"Generating Visual Studio Solution...";
657          app.UpdateDisplay();
658          //GenerateVSSolutionFile(project, compiler);
659          ide.statusBar.text = $"Generating Visual Studio Project...";
660          app.UpdateDisplay();
661          //GenerateVCProjectFile(project, compiler, bitDepth);
662          ide.statusBar.text = null;
663          app.UpdateDisplay();
664          return true;
665       }
666       else
667       {
668          char makefilePath[MAX_LOCATION];
669          char makefileName[MAX_LOCATION];
670          bool exists;
671          LogBox logBox = ide.outputView.buildBox;
672          
673          strcpy(makefilePath, project.topNode.path);
674          project.CatMakeFileName(makefileName, config);
675          PathCatSlash(makefilePath, makefileName);
676
677          exists = FileExists(makefilePath);
678          if(method == force ||
679            (method == forceExists && exists) ||
680            (method == normal && (!exists || (config && config.makingModified))))
681          {
682             char * reason;
683             char * action;
684             ide.statusBar.text = $"Generating Makefile & Dependencies..."; // Dependencies?
685             app.UpdateDisplay();
686
687             if((method == normal && !exists) || (method == force && !exists))
688                action = $"Generating ";
689             else if(method == force)
690                action = $"Regenerating ";
691             else if(method == normal || method == forceExists)
692                action = $"Updating ";
693             else
694                action = "";
695             if(!exists)
696                reason = $"Makefile doesn't exist. ";
697             else if(project.topNode.modified)
698                reason = $"Project has been modified. ";
699             else
700                reason = "";
701
702             //logBox.Logf("%s\n", makefileName);
703             logBox.Logf($"%s - %s%smakefile for %s config...\n", makefileName, reason, action, GetConfigName(config));
704
705             if(!project.GenerateMakefile(null, false, null, config))
706                ide.outputView.buildBox.Logf($"Error generating makefile (Is the project directory writable?)\n");
707
708             ide.statusBar.text = null;
709             app.UpdateDisplay();
710             return true;
711          }
712       }
713       return false;
714    }
715    
716    bool BuildInterrim(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config, int bitDepth, bool justPrint)
717    {
718       if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
719       {
720          ide.outputView.buildBox.Logf($"Building project %s using the %s configuration...\n", prj.name, GetConfigName(config));
721          return Build(prj, buildType, compiler, config, bitDepth, justPrint);
722       }
723       return false;
724    }
725
726    bool DebugStopForMake(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config)
727    {
728       bool result = false;
729       // TOFIX: DebugStop is being abused and backfiring on us.
730       //        It's supposed to be the 'Debug/Stop' item, not unloading executable or anything else
731
732       //        configIsInDebugSession seems to be used for two OPPOSITE things:
733       //        If we're debugging another config, we need to unload the executable!
734       //        In building, we want to stop if we're debugging the 'same' executable
735       if(buildType != run) ///* && prj == project*/ && prj.configIsInDebugSession)
736       {
737          if(buildType == start || buildType == restart)
738          {
739             if(ide.debugger && ide.debugger.isPrepared)
740                result = DebugStop();
741          }
742          else
743          {
744             if(ide.project == prj && ide.debugger && ide.debugger.prjConfig == config && ide.debugger.isPrepared)
745                result = DebugStop();
746          }
747       }
748       return result;
749    }
750
751    bool Build(Project prj, BuildType buildType, CompilerConfig compiler, ProjectConfig config, int bitDepth, bool justPrint)
752    {
753       bool result = true;
754       Window document;
755
756       stopBuild = false;
757       for(document = master.firstChild; document; document = document.next)
758       {
759          if(document.modifiedDocument)
760          {
761             ProjectNode node = GetNodeFromWindow(document, prj, false);
762             if(node && !document.MenuFileSave(null, 0))
763             {
764                result = false;
765                break;
766             }
767          }
768       }
769       if(result)
770       {
771          DirExpression targetDir = prj.GetTargetDir(compiler, config, bitDepth);
772
773          DebugStopForMake(prj, buildType, compiler, config);
774
775          // TODO: Disabled until problems fixed... is it fixed?
776          if(buildType == rebuild || (config && config.compilingModified))
777             prj.Clean(compiler, config, bitDepth, clean, justPrint);
778          else
779          {
780             if(buildType == relink || (config && config.linkingModified))
781                prj.Clean(compiler, config, bitDepth, cleanTarget, false);
782             if(config && config.symbolGenModified)
783             {
784                DirExpression objDir = prj.GetObjDir(compiler, config, bitDepth);
785                char fileName[MAX_LOCATION];
786                char moduleName[MAX_FILENAME];
787                strcpy(fileName, prj.topNode.path);
788                PathCatSlash(fileName, objDir.dir);
789                ReplaceSpaces(moduleName, prj.moduleName);
790                strcat(moduleName, ".main.ec");
791                PathCatSlash(fileName, moduleName);
792                if(FileExists(fileName))
793                   DeleteFile(fileName);
794                ChangeExtension(fileName, "c", fileName);
795                if(FileExists(fileName))
796                   DeleteFile(fileName);
797                ChangeExtension(fileName, "o", fileName);
798                if(FileExists(fileName))
799                   DeleteFile(fileName);
800
801                delete objDir;
802             }
803          }
804          buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
805          ide.AdjustBuildMenus();
806          ide.AdjustDebugMenus();
807
808          result = prj.Build(buildType, null, compiler, config, bitDepth, justPrint, normal);
809
810          if(config)
811          {
812             config.compilingModified = false;
813             if(!stopBuild)
814                config.linkingModified = false;
815
816             config.symbolGenModified = false;
817          }
818          buildInProgress = none;
819          ide.AdjustBuildMenus();
820          ide.AdjustDebugMenus();
821
822          ide.workspace.modified = true;
823
824          delete targetDir;
825       }
826       return result;
827    }
828
829    //                          ((( USER ACTIONS )))
830    //
831    //  ************************************************************************
832    //  *** Methods below should atomically start a process, and as such     ***
833    //  *** they can query compiler and config directly from ide and project ***
834    //  *** but ONLY ONCE!!!                                                 ***
835    //  ************************************************************************
836
837    bool ProjectBuild(MenuItem selection, Modifiers mods)
838    {
839       if(buildInProgress == none)
840       {
841          Project prj = project;
842          CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
843          int bitDepth = ide.workspace.bitDepth;
844          ProjectConfig config;
845          if(selection || !ide.activeClient)
846          {
847             DataRow row = fileList.currentRow;
848             ProjectNode node = row ? (ProjectNode)row.tag : null;
849             if(node) prj = node.project;
850          }
851          else
852          {
853             ProjectNode node = GetNodeFromWindow(ide.activeClient, null, false);
854             if(node)
855                prj = node.project;
856          }
857          config = prj.config;
858          if(/*prj != project || */!prj.GetConfigIsInDebugSession(config) || !ide.DontTerminateDebugSession($"Project Build"))
859          {
860             BuildInterrim(prj, build, compiler, config, bitDepth, mods.ctrl && mods.shift);
861          }
862          delete compiler;
863       }
864       else
865          stopBuild = true;
866       return true;
867    }
868
869    bool ProjectInstall(MenuItem selection, Modifiers mods)
870    {
871       Project prj = project;
872       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
873       int bitDepth = ide.workspace.bitDepth;
874       ProjectConfig config;
875       if(selection || !ide.activeClient)
876       {
877          DataRow row = fileList.currentRow;
878          ProjectNode node = row ? (ProjectNode)row.tag : null;
879          if(node) prj = node.project;
880       }
881       else
882       {
883          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, false);
884          if(node)
885             prj = node.project;
886       }
887       config = prj.config;
888       if(!prj.GetConfigIsInDebugSession(config) ||
889             (!ide.DontTerminateDebugSession($"Project Install") && DebugStopForMake(prj, relink, compiler, config)))
890       {
891          BuildInterrim(prj, build, compiler, config, bitDepth, mods.ctrl && mods.shift);
892          if(ProjectPrepareForToolchain(prj, normal, false, false, compiler, config))
893          {
894             ide.outputView.buildBox.Logf($"\nInstalling project %s using the %s configuration...\n", prj.name, GetConfigName(config));
895             Build(prj, install, compiler, config, bitDepth, mods.ctrl && mods.shift);
896          }
897       }
898       delete compiler;
899       return true;
900    }
901
902    bool ProjectLink(MenuItem selection, Modifiers mods)
903    {
904       Project prj = project;
905       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
906       int bitDepth = ide.workspace.bitDepth;
907       ProjectConfig config;
908       if(selection || !ide.activeClient)
909       {
910          DataRow row = fileList.currentRow;
911          ProjectNode node = row ? (ProjectNode)row.tag : null;
912          if(node) prj = node.project;
913       }
914       else
915       {
916          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, false);
917          if(node)
918             prj = node.project;
919       }
920       config = prj.config;
921       if(!prj.GetConfigIsInDebugSession(config) ||
922             (!ide.DontTerminateDebugSession($"Project Link") && DebugStopForMake(prj, relink, compiler, config)))
923       {
924          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
925          {
926             ide.outputView.buildBox.Logf($"Relinking project %s using the %s configuration...\n", prj.name, GetConfigName(config));
927             if(config)
928                config.linkingModified = true;
929             Build(prj, relink, compiler, config, bitDepth, mods.ctrl && mods.shift);
930          }
931       }
932       delete compiler;
933       return true;
934    }
935
936    bool ProjectRebuild(MenuItem selection, Modifiers mods)
937    {
938       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
939       int bitDepth = ide.workspace.bitDepth;
940       Project prj = project;
941       ProjectConfig config;
942       if(selection || !ide.activeClient)
943       {
944          DataRow row = fileList.currentRow;
945          ProjectNode node = row ? (ProjectNode)row.tag : null;
946          if(node) prj = node.project;
947       }
948       else
949       {
950          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, false);
951          if(node)
952             prj = node.project;
953       }
954       config = prj.config;
955       if(!prj.GetConfigIsInDebugSession(config) ||
956             (!ide.DontTerminateDebugSession($"Project Rebuild") && DebugStopForMake(prj, rebuild, compiler, config)))
957       {
958          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
959          {
960             ide.outputView.buildBox.Logf($"Rebuilding project %s using the %s configuration...\n", prj.name, GetConfigName(config));
961             /*if(config)
962             {
963                config.compilingModified = true;
964                config.makingModified = true;
965             }*/ // -- should this still be used depite the new solution of BuildType?
966             Build(prj, rebuild, compiler, config, bitDepth, mods.ctrl && mods.shift);
967          }
968       }
969       delete compiler;
970       return true;
971    }
972
973    bool ProjectCleanTarget(MenuItem selection, Modifiers mods)
974    {
975       CleanProject($"Project Clean Target", $"Cleaning project %s target using the %s configuration...\n", selection, cleanTarget, mods.ctrl && mods.shift);
976       return true;
977    }
978
979    bool ProjectClean(MenuItem selection, Modifiers mods)
980    {
981       CleanProject($"Project Clean", $"Cleaning project %s using the %s configuration...\n", selection, clean, mods.ctrl && mods.shift);
982       return true;
983    }
984
985    bool ProjectRealClean(MenuItem selection, Modifiers mods)
986    {
987       CleanProject($"Project Real Clean", $"Removing intermediate objects directory for project %s using the %s configuration...\n", selection, realClean, mods.ctrl && mods.shift);
988       return true;
989    }
990
991    void CleanProject(char * terminateDebugSessionMessage, char * cleaningMessageLogFormat, MenuItem selection, CleanType cleanType, bool justPrint)
992    {
993       Project prj = project;
994       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
995       ProjectConfig config;
996       int bitDepth = ide.workspace.bitDepth;
997       if(selection || !ide.activeClient)
998       {
999          DataRow row = fileList.currentRow;
1000          ProjectNode node = row ? (ProjectNode)row.tag : null;
1001          if(node) prj = node.project;
1002       }
1003       else
1004       {
1005          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, false);
1006          if(node) prj = node.project;
1007       }
1008       config = prj.config;
1009       if(!prj.GetConfigIsInDebugSession(config) ||
1010             (!ide.DontTerminateDebugSession(terminateDebugSessionMessage) && DebugStopForMake(prj, clean, compiler, config)))
1011       {
1012          if(ProjectPrepareForToolchain(prj, normal, true, true, compiler, config))
1013          {
1014             ide.outputView.buildBox.Logf(cleaningMessageLogFormat, prj.name, GetConfigName(config));
1015
1016             buildInProgress = prj == project ? buildingMainProject : buildingSecondaryProject;
1017             ide.AdjustBuildMenus();
1018             ide.AdjustDebugMenus();
1019
1020             prj.Clean(compiler, config, bitDepth, cleanType, justPrint);
1021             buildInProgress = none;
1022             ide.AdjustBuildMenus();
1023             ide.AdjustDebugMenus();
1024          }
1025       }
1026       delete compiler;
1027    }
1028
1029    bool ProjectRegenerate(MenuItem selection, Modifiers mods)
1030    {
1031       Project prj = project;
1032       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1033       ShowOutputBuildLog(true);
1034       if(selection || !ide.activeClient)
1035       {
1036          DataRow row = fileList.currentRow;
1037          ProjectNode node = row ? (ProjectNode)row.tag : null;
1038          if(node)
1039             prj = node.project;
1040       }
1041       else
1042       {
1043          ProjectNode node = GetNodeFromWindow(ide.activeClient, null, false);
1044          if(node)
1045             prj = node.project;
1046       }
1047
1048       DisplayCompiler(compiler, false);
1049       ProjectPrepareCompiler(project, compiler, false);
1050       ProjectPrepareMakefile(prj, force, compiler, prj.config);
1051       delete compiler;
1052       return true;
1053    }
1054
1055    bool Compile(Project project, List<ProjectNode> nodes, bool justPrint, SingleFileCompileMode mode)
1056    {
1057       bool result = true;
1058       char fileName[MAX_LOCATION];
1059       Window document;
1060       ProjectConfig config = project.config;
1061
1062       stopBuild = false;
1063
1064       for(document = ide.firstChild; document; document = document.next)
1065       {
1066          if(document.modifiedDocument)
1067          {
1068             ProjectNode n = GetNodeFromWindow(document, project, mode == cObject ? true : false);
1069             for(node : nodes)
1070             {
1071                if(n && n.IsInNode(node) && !document.MenuFileSave(null, 0))
1072                {
1073                   ide.outputView.buildBox.Logf($"Unable to save %s file.\n", node.name);
1074                   result = false;
1075                   break;
1076                }
1077             }
1078          }
1079       }
1080
1081       if(result)
1082       {
1083          CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1084          int bitDepth = ide.workspace.bitDepth;
1085          result = false;
1086          if(ProjectPrepareForToolchain(project, normal, true, true, compiler, config))
1087          {
1088             if(config)
1089                ide.outputView.buildBox.Logf($"%s specific file(s) in project %s using the %s configuration...\n",
1090                      (mode == normal || mode == cObject) ? $"Compiling" : $"Debug compiling", project.name, config.name);
1091             else
1092                ide.outputView.buildBox.Logf($"%s specific file(s) in project %s...\n",
1093                      (mode == normal || mode == cObject) ? $"Compiling" : $"Debug compiling", project.name);
1094
1095             buildInProgress = compilingFile;
1096             ide.AdjustBuildMenus();
1097             result = project.Compile(nodes, compiler, config, bitDepth, justPrint, mode);
1098             buildInProgress = none;
1099             ide.AdjustBuildMenus();
1100          }
1101          delete compiler;
1102       }
1103       return result;
1104    }
1105
1106    bool Clean(Project project, List<ProjectNode> nodes, bool justPrint)
1107    {
1108       bool result = true;
1109       char fileName[MAX_LOCATION];
1110       Window document;
1111       ProjectConfig config = project.config;
1112
1113       stopBuild = false;
1114
1115       for(document = ide.firstChild; document; document = document.next)
1116       {
1117          if(document.modifiedDocument)
1118          {
1119             ProjectNode n = GetNodeFromWindow(document, project, false);
1120             for(node : nodes)
1121             {
1122                if(n && n.IsInNode(node) && !document.MenuFileSave(null, 0))
1123                {
1124                   ide.outputView.buildBox.Logf($"Unable to save %s file.\n", node.name);
1125                   result = false;
1126                   break;
1127                }
1128             }
1129          }
1130       }
1131
1132       if(result)
1133       {
1134          CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1135          int bitDepth = ide.workspace.bitDepth;
1136          result = false;
1137          if(ProjectPrepareForToolchain(project, normal, true, true, compiler, config))
1138          {
1139             Map<String, NameCollisionInfo> namesInfo { };
1140             project.topNode.GenMakefileGetNameCollisionInfo(namesInfo, config);
1141             for(node : nodes)
1142             {
1143                if(node.GetIsExcluded(config))
1144                   ide.outputView.buildBox.Logf($"File %s is excluded from current build configuration.\n", node.name);
1145                else
1146                {
1147                   if(config)
1148                      ide.outputView.buildBox.Logf($"Deleting intermediate objects for %s %s in project %s using the %s configuration...\n",
1149                            node.type == file ? $"single file" : $"folder", node.name, project.name, config.name);
1150                   else
1151                      ide.outputView.buildBox.Logf($"Deleting intermediate objects for %s %s in project %s...\n",
1152                            node.type == file ? $"single file" : $"folder", node.name, project.name);
1153
1154                   node.DeleteIntermediateFiles(compiler, config, bitDepth, namesInfo, false);
1155                   result = true;
1156                }
1157             }
1158             namesInfo.Free();
1159             delete namesInfo;
1160          }
1161          delete compiler;
1162       }
1163       return result;
1164    }
1165
1166    bool ProjectNewFile(MenuItem selection, Modifiers mods)
1167    {
1168       DataRow row = fileList.currentRow;
1169       if(row)
1170       {
1171          char fileName[1024];
1172          char filePath[MAX_LOCATION];
1173          ProjectNode parentNode = (ProjectNode)row.tag;
1174          ProjectNode n, fileNode;
1175          parentNode.GetFileSysMatchingPath(filePath);
1176          MakePathRelative(filePath, parentNode.project.topNode.path, filePath);
1177          for(n = parentNode; n && n != parentNode.project.resNode; n = n.parent);
1178          sprintf(fileName, $"Untitled %d", documentID);
1179          fileNode = AddFile(parentNode, fileName, (bool)n, true);
1180          fileNode.path = CopyUnixPath(filePath);
1181          if(fileNode)
1182          {
1183             NodeProperties nodeProperties
1184             {
1185                parent, this;
1186                position = { position.x + 100, position.y + 100 };
1187                mode = newFile;
1188                node = fileNode;
1189             };
1190             nodeProperties.Create(); // not modal?
1191          }
1192       }
1193       return true;
1194    }
1195
1196    bool ProjectNewFolder(MenuItem selection, Modifiers mods)
1197    {
1198       DataRow row = fileList.currentRow;
1199       if(row)
1200       {
1201          ProjectNode parentNode = (ProjectNode)row.tag;
1202          NewFolder(parentNode, null, true);
1203       }
1204       return true;
1205    }
1206
1207    bool ResourcesAddFiles(MenuItem selection, Modifiers mods)
1208    {
1209       AddFiles(true);
1210       return true;
1211    }
1212
1213    bool ProjectAddFiles(MenuItem selection, Modifiers mods)
1214    {
1215       AddFiles(false);
1216       return true;
1217    }
1218
1219    bool ProjectImportFolder(MenuItem selection, Modifiers mods)
1220    {
1221       DataRow row = fileList.currentRow;
1222       if(row)
1223       {
1224          ProjectNode toNode = (ProjectNode)row.tag;
1225          ImportFolder(toNode);
1226       }
1227       return true;
1228    }
1229
1230    bool ProjectAddNewForm(MenuItem selection, Modifiers mods)
1231    {
1232       CodeEditor codeEditor = CreateNew("Form", "form", "Window", null);
1233       codeEditor.EnsureUpToDate();
1234       return true;
1235    }
1236
1237    bool ProjectAddNewGraph(MenuItem selection, Modifiers mods)
1238    {
1239       CodeEditor codeEditor = CreateNew("Graph", "graph", "Block", null);
1240       if(codeEditor)
1241          codeEditor.EnsureUpToDate();
1242       return true;
1243    }
1244
1245    bool ProjectRemove(MenuItem selection, Modifiers mods)
1246    {
1247       DataRow row = fileList.currentRow;
1248       if(row)
1249       {
1250          ProjectNode node = (ProjectNode)row.tag;
1251          if(node.type == project)
1252             RemoveSelectedNodes();
1253       }
1254       return true;
1255    }
1256
1257    bool ProjectUpdateMakefileForAllConfigs(Project project)
1258    {
1259       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1260
1261       // This call really does not belong here:
1262       ide.UpdateToolBarActiveConfigs(false);
1263       for(config : project.configurations)
1264          ProjectPrepareMakefile(project, forceExists, compiler, config);
1265
1266       ide.Update(null);
1267       delete compiler;
1268       return true;
1269    }
1270
1271    bool MenuSettings(MenuItem selection, Modifiers mods)
1272    {
1273       ProjectNode node = GetSelectedNode(true);
1274       Project prj = node ? node.project : project;
1275       projectSettingsDialog = ProjectSettings { master = parent, project = prj, projectNode = node };
1276       incref projectSettingsDialog;
1277       projectSettingsDialog.Modal();
1278       delete projectSettingsDialog;
1279       ide.UpdateToolBarActiveConfigs(false);
1280       Update(null);
1281       ide.AdjustMenus();
1282       return true;
1283    }
1284
1285    bool FileProperties(MenuItem selection, Modifiers mods)
1286    {
1287       DataRow row = fileList.currentRow;
1288       if(row)
1289       {
1290          ProjectNode node = (ProjectNode)row.tag;
1291          NodeProperties { parent = parent, master = this, node = node, 
1292                position = { position.x + 100, position.y + 100 } }.Create();
1293       }
1294       return true;
1295    }
1296
1297    bool FileOpenFile(MenuItem selection, Modifiers mods)
1298    {
1299       OpenSelectedNodes(mods.ctrl && mods.shift);
1300       return true;
1301    }
1302
1303    bool FileRemoveFile(MenuItem selection, Modifiers mods)
1304    {
1305       RemoveSelectedNodes();
1306       return true;
1307    }
1308
1309    bool FileCompile(MenuItem selection, Modifiers mods)
1310    {
1311       OldLink item;
1312       OldList selectedRows;
1313       Project project = null;
1314       List<ProjectNode> nodes { };
1315       fileList.GetMultiSelection(selectedRows);
1316       for(item = selectedRows.first; item; item = item.next)
1317       {
1318          OldLink i;
1319          DataRow row = item.data;
1320          ProjectNode node = (ProjectNode)row.tag;
1321          if(!project)
1322             project = node.project;
1323          else if(node.project != project)
1324          {
1325             project = null;
1326             break;
1327          }
1328          nodes.Add(node);
1329       }
1330       selectedRows.Free(null);
1331       if(project)
1332          Compile(project, nodes, mods.ctrl && mods.shift, normal);
1333       else
1334          ide.outputView.buildBox.Logf($"Please select files from a single project.\n");
1335       delete nodes;
1336       return true;
1337    }
1338
1339    bool FileClean(MenuItem selection, Modifiers mods)
1340    {
1341       OldLink item;
1342       OldList selectedRows;
1343       Project project = null;
1344       List<ProjectNode> nodes { };
1345       fileList.GetMultiSelection(selectedRows);
1346       for(item = selectedRows.first; item; item = item.next)
1347       {
1348          OldLink i;
1349          DataRow row = item.data;
1350          ProjectNode node = (ProjectNode)row.tag;
1351          if(!project)
1352             project = node.project;
1353          else if(node.project != project)
1354          {
1355             project = null;
1356             break;
1357          }
1358          nodes.Add(node);
1359       }
1360       selectedRows.Free(null);
1361       if(project)
1362          Clean(project, nodes, mods.ctrl && mods.shift);
1363       else
1364          ide.outputView.buildBox.Logf($"Please select files from a single project.\n");
1365       delete nodes;
1366       return true;
1367    }
1368
1369    bool FileDebugPrecompile(MenuItem selection, Modifiers mods)
1370    {
1371       DataRow row = fileList.currentRow;
1372       ProjectNode node = row ? (ProjectNode)row.tag : null;
1373       if(node)
1374       {
1375          List<ProjectNode> nodes { };
1376          nodes.Add(node);
1377          if(node.type == project)
1378             ProjectBuild(selection, mods);
1379          ide.Update(null);
1380          if(!stopBuild)
1381             Compile(node.project, nodes, mods.ctrl && mods.shift, debugPrecompile);
1382          delete nodes;
1383       }
1384       return true;
1385    }
1386
1387    bool FileDebugCompile(MenuItem selection, Modifiers mods)
1388    {
1389       DataRow row = fileList.currentRow;
1390       ProjectNode node = row ? (ProjectNode)row.tag : null;
1391       if(node)
1392       {
1393          List<ProjectNode> nodes { };
1394          nodes.Add(node);
1395          if(node.type == project)
1396             ProjectBuild(selection, mods);
1397          else
1398             Compile(node.project, nodes, mods.ctrl && mods.shift, normal);
1399          if(!stopBuild)
1400             Compile(node.project, nodes, mods.ctrl && mods.shift, debugCompile);
1401          delete nodes;
1402       }
1403       return true;
1404    }
1405
1406    bool FileDebugGenerateSymbols(MenuItem selection, Modifiers mods)
1407    {
1408       DataRow row = fileList.currentRow;
1409       ProjectNode node = row ? (ProjectNode)row.tag : null;
1410       if(node)
1411       {
1412          List<ProjectNode> nodes { };
1413          nodes.Add(node);
1414          if(node.type == project)
1415             ProjectBuild(selection, mods);
1416          else
1417             Compile(node.project, nodes, mods.ctrl && mods.shift, normal);
1418          if(!stopBuild)
1419             Compile(node.project, nodes, mods.ctrl && mods.shift, debugGenerateSymbols);
1420          delete nodes;
1421       }
1422       return true;
1423    }
1424
1425    Project GetSelectedProject(bool useSelection)
1426    {
1427       Project prj = project;
1428       if(useSelection)
1429       {
1430          DataRow row = fileList.currentRow;
1431          ProjectNode node = row ? (ProjectNode)row.tag : null;
1432          if(node)
1433             prj = node.project;
1434       }
1435       return prj;
1436    }
1437    
1438    void SelectNextProject(bool backwards)
1439    {
1440       DataRow row = fileList.currentRow;
1441       DataRow currentRow = row;
1442       ProjectNode node = (ProjectNode)row.tag;
1443       if(node.type != project)
1444          row = node.project.topNode.row;
1445       else if(backwards)
1446          row = row.previous ? row.previous : fileList.lastRow;
1447       if(!backwards)
1448          row = row.next ? row.next : fileList.firstRow;
1449       if(row && row != currentRow)
1450       {
1451          fileList.SelectRow(row);
1452          fileList.currentRow = row;
1453       }
1454    }
1455
1456    ProjectNode GetSelectedNode(bool useSelection)
1457    {
1458       ProjectNode node = null;
1459       if(useSelection)
1460       {
1461          DataRow row = fileList.currentRow;
1462          if(row)
1463             node = (ProjectNode)row.tag;
1464       }
1465       return node;
1466    }
1467
1468    bool MenuBrowseFolder(MenuItem selection, Modifiers mods)
1469    {
1470       char folder[MAX_LOCATION];
1471       Project prj;
1472       ProjectNode node = GetSelectedNode(true);
1473       if(!node)
1474          node = project.topNode;
1475       prj = node.project;
1476
1477       strcpy(folder, prj.topNode.path);
1478       if(node != prj.topNode)
1479          PathCatSlash(folder, node.path);
1480       ShellOpen(folder);
1481       return true;
1482    }
1483
1484    bool Run(MenuItem selection, Modifiers mods)
1485    {
1486       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1487       ProjectConfig config = project.config;
1488       int bitDepth = ide.workspace.bitDepth;
1489       String args = new char[maxPathLen];
1490       args[0] = '\0';
1491       if(ide.workspace.commandLineArgs)
1492          //ide.debugger.GetCommandLineArgs(args);
1493          strcpy(args, ide.workspace.commandLineArgs);
1494       if(ide.debugger.isActive)
1495          project.Run(args, compiler, config, bitDepth);
1496       /*else if(config.targetType == sharedLibrary || config.targetType == staticLibrary)
1497          MessageBox { master = ide, type = ok, text = "Run", contents = "Shared and static libraries cannot be run like executables." }.Modal();*/
1498       else if(BuildInterrim(project, run, compiler, config, bitDepth, false))
1499          project.Run(args, compiler, config, bitDepth);
1500       delete args;
1501       delete compiler;
1502       return true;
1503    }
1504
1505    bool DebugStart()
1506    {
1507       bool result = false;
1508       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1509       ProjectConfig config = project.config;
1510       int bitDepth = ide.workspace.bitDepth;
1511       TargetTypes targetType = project.GetTargetType(config);
1512       if(targetType == sharedLibrary || targetType == staticLibrary)
1513          MessageBox { master = ide, type = ok, text = $"Run", contents = $"Shared and static libraries cannot be run like executables." }.Modal();
1514       else if(project.GetCompress(config))
1515          MessageBox { master = ide, text = $"Starting Debug", contents = $"Debugging compressed applications is not supported\n" }.Modal();
1516       else if(project.GetDebug(config) ||
1517          MessageBox { master = ide, type = okCancel, text = $"Starting Debug", contents = $"Attempting to debug non-debug configuration\nProceed anyways?" }.Modal() == ok)
1518       {
1519          if(/*!IsProjectModified() ||*/ BuildInterrim(project, start, compiler, config, bitDepth, false))
1520          {
1521             if(compiler.type.isVC)
1522             {
1523                //bool result = false;
1524                char oldwd[MAX_LOCATION];
1525                PathBackup pathBackup { };
1526                char command[MAX_LOCATION];
1527
1528                ide.SetPath(false, compiler, config, bitDepth);
1529                
1530                GetWorkingDir(oldwd, sizeof(oldwd));
1531                ChangeWorkingDir(project.topNode.path);
1532
1533                sprintf(command, "%s /useenv %s.sln /projectconfig \"%s|Win32\" /command \"%s\"" , "devenv", project.name, config.name, "Debug.Start");
1534                Execute(command);
1535                ChangeWorkingDir(oldwd);
1536
1537                delete pathBackup;
1538             }
1539             else
1540             {
1541                ide.debugger.Start(compiler, config, bitDepth);
1542                result = true;
1543             }
1544          }
1545       }
1546       delete compiler;
1547       return result;      
1548    }
1549
1550    void GoToError(const char * line, const bool noParsing)
1551    {
1552       char * colon;
1553       
1554       while(isspace(*line)) line++;
1555       colon = strstr(line, ":");
1556
1557       {
1558          int lineNumber = 0;
1559          int col = 1;
1560          bool lookForLineNumber = true;
1561
1562          // deal with linking error
1563          if(colon && colon[1] == ' ')
1564          {
1565             colon = strstr(colon + 1, ":");
1566             if(colon && !colon[1])
1567             {
1568                colon = strstr(line, ":");
1569                lookForLineNumber = false;
1570             }
1571             else if(colon && !isdigit(colon[1]))
1572             {
1573                line = colon + 1;
1574                colon = strstr(line, ":");
1575             }
1576          }
1577          // Don't be mistaken by the drive letter colon
1578          if(colon && (colon[1] == '/' || colon[1] == '\\'))
1579             colon = strstr(colon + 1, ":");
1580          if(colon && lookForLineNumber)
1581          {
1582             char * comma;
1583 #if 0 // MSVS Errors -- todo fix this later
1584             char * par = RSearchString(line, "(", colon - line, true, false);
1585             if(par && strstr(par, ")"))
1586                colon = par;
1587             else if((colon+1)[0] == ' ')
1588             {
1589                // NOTE: This is the same thing as saying 'colon = line'
1590                for( ; colon != line; colon--)
1591                   /*if(*colon == '(')
1592                      break*/;
1593             }
1594 #endif
1595             lineNumber = atoi(colon + 1);
1596             /*
1597             comma = strchr(colon, ',');
1598             if(comma)
1599                col = atoi(comma+1);
1600             */
1601             comma = strchr(colon+1, ':');
1602             if(comma)
1603                col = atoi(comma+1);
1604          }
1605          
1606          {
1607             char moduleName[MAX_LOCATION], filePath[MAX_LOCATION] = "";
1608             char ext[MAX_EXTENSION] = "";
1609             char * bracket;
1610             ProjectNode node = null;
1611             if(colon)
1612             {
1613                char * inFileIncludedFrom = strstr(line, stringInFileIncludedFrom);
1614                char * from = strstr(line, "from ");
1615                char * start = inFileIncludedFrom ? inFileIncludedFrom + strlen(stringInFileIncludedFrom) : from ? from + strlen("from ") : line;
1616                int len;
1617                if(colon < start)
1618                   start = line;
1619                len = Min((int)(colon - start), MAX_LOCATION-1);
1620                // Cut module name
1621                strncpy(moduleName, start, len);
1622                moduleName[len] = '\0';
1623             }
1624             else
1625                strcpy(moduleName, line);
1626
1627             if(!colon)
1628             {
1629                char * msg;
1630                if((msg = strstr(moduleName, " - ")))
1631                {
1632                   bool found = false;
1633                   msg[0] = '\0';
1634                   for(prj : ide.workspace.projects)
1635                   {
1636                      strcpy(filePath, prj.topNode.path);
1637                      PathCatSlash(filePath, moduleName);
1638                      if(FileExists(filePath).isFile)
1639                      {
1640                         found = true;
1641                         break;
1642                      }
1643                   }
1644                   if(!found)
1645                   {
1646                      msg[0] = ' ';
1647                      filePath[0] = '\0';
1648                   }
1649                }
1650                else if((msg = strstr(moduleName, "...")) && (colon = strchr(moduleName, ' ')) && (++colon)[0])
1651                {
1652                   bool found = false;
1653                   msg[0] = '\0';
1654                   for(prj : ide.workspace.projects)
1655                   {
1656                      if((node = prj.resNode.Find(colon, true)))
1657                      {
1658                         strcpy(filePath, prj.topNode.path);
1659                         PathCatSlash(filePath, node.path);
1660                         PathCatSlash(filePath, node.name);
1661
1662                         found = true;
1663                         break;
1664                      }
1665                   }
1666                   if(!found)
1667                   {
1668                      msg[0] = '.';
1669                      filePath[0] = '\0';
1670                   }
1671                }
1672                colon = null;
1673             }
1674             // Remove stuff in brackets
1675             /*
1676             bracket = strstr(moduleName, "(");
1677             if(bracket) *bracket = '\0';
1678             */
1679             MakeSlashPath(moduleName);
1680             GetExtension(moduleName, ext);
1681
1682             if(!colon && !filePath[0])
1683             {
1684                bool dotMain = false;
1685                // Check if it's one of our modules
1686                node = project.topNode.Find(moduleName, false);
1687                if(node)
1688                {
1689                   strcpy(filePath, node.path);
1690                   PathCatSlash(filePath, node.name);
1691                }
1692                else
1693                {
1694                   IntermediateFileType type = none;
1695                   ProjectConfig config;
1696                   if(ext[0])
1697                   {
1698                      char ext2[MAX_EXTENSION];
1699                      char stripExt[MAX_LOCATION];
1700                      strcpy(stripExt, moduleName);
1701                      StripExtension(stripExt);
1702                      GetExtension(stripExt, ext2);
1703                      if(ext2[0] && !strcmp(ext2, "main"))
1704                         dotMain = true;
1705                      if(!strcmp(ext, "ec"))
1706                         type = ec;
1707                      else if(!strcmp(ext, "c"))
1708                         type = c;
1709                      else if(!strcmp(ext, "sym"))
1710                         type = sym;
1711                      else if(!strcmp(ext, "imp"))
1712                         type = imp;
1713                      else if(!strcmp(ext, "bowl"))
1714                         type = bowl;
1715                      else if(!strcmp(ext, "o"))
1716                         type = o;
1717
1718                      if(type)
1719                      {
1720                         for(prj : ide.workspace.projects; prj.lastBuildConfigName)
1721                         {
1722                            if((config = prj.GetConfig(prj.lastBuildConfigName)))
1723                               node = prj.FindNodeByObjectFileName(moduleName, type, dotMain, config);
1724                            if(node)
1725                               break;
1726                         }
1727                      }
1728                   }
1729                   if(node)
1730                   {
1731                      char name[MAX_FILENAME];
1732                      Project project = node.project;
1733                      CompilerConfig compiler = ideSettings.GetCompilerConfig(project.lastBuildCompilerName);
1734                      if(compiler)
1735                      {
1736                         int bitDepth = ide.workspace.bitDepth;
1737                         DirExpression objDir = project.GetObjDir(compiler, config, bitDepth);
1738                         strcpy(filePath, project.topNode.path);
1739                         PathCatSlash(filePath, objDir.dir);
1740                         node.GetObjectFileName(name, project.lastBuildNamesInfo, type, dotMain);
1741                         PathCatSlash(filePath, name);
1742                         delete objDir;
1743                      }
1744                      delete compiler;
1745                   }
1746                }
1747                if(!node)
1748                   filePath[0] = '\0';
1749             }
1750             if(!strcmp(ext, "a") || !strcmp(ext, "o") || !strcmp(ext, "lib") ||
1751                   !strcmp(ext, "dll") || !strcmp(ext, "exe") || !strcmp(ext, "mo"))
1752                moduleName[0] = 0;    // Avoid opening binary files
1753             if(moduleName[0])
1754             {
1755                CodeEditor codeEditor;
1756                if(!filePath[0])
1757                {
1758                   strcpy(filePath, project.topNode.path);
1759                   PathCatSlash(filePath, moduleName);
1760                }
1761       
1762                codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal, noParsing);
1763                if(!codeEditor && !strcmp(ext, "c"))
1764                {
1765                   char ecName[MAX_LOCATION];
1766                   ChangeExtension(filePath, "ec", ecName);
1767                   codeEditor = (CodeEditor)ide.OpenFile(ecName, normal, true, null, no, normal, noParsing);
1768                }
1769                if(!codeEditor)
1770                {
1771                   char path[MAX_LOCATION];
1772                   // TOFIX: Improve on this, don't use only filename, make a function
1773                   if(ide && ide.workspace)
1774                   {
1775                      for(prj : ide.workspace.projects)
1776                      {
1777                         ProjectNode node;
1778                         MakePathRelative(filePath, prj.topNode.path, path);
1779
1780                         if((node = prj.topNode.FindWithPath(path, false)))
1781                         {
1782                            strcpy(filePath, prj.topNode.path);
1783                            PathCatSlash(filePath, node.path);
1784                            PathCatSlash(filePath, node.name);
1785                            codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal, noParsing);
1786                            if(codeEditor)
1787                               break;
1788                         }
1789                      }
1790                      if(!codeEditor && !strchr(moduleName, '/') && !strchr(moduleName, '\\'))
1791                      {
1792                         for(prj : ide.workspace.projects)
1793                         {
1794                            ProjectNode node;
1795                            if((node = prj.topNode.Find(moduleName, false)))
1796                            {
1797                               strcpy(filePath, prj.topNode.path);
1798                               PathCatSlash(filePath, node.path);
1799                               PathCatSlash(filePath, node.name);
1800                               codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal, noParsing);
1801                               if(codeEditor)
1802                                  break;
1803                            }
1804                         }
1805                      }
1806                   }
1807                }
1808                if(codeEditor && lineNumber)
1809                {
1810                   EditBox editBox = codeEditor.editBox;
1811                   editBox.GoToLineNum(lineNumber - 1);
1812                   editBox.GoToPosition(editBox.line, lineNumber - 1, col ? (col - 1) : 0);
1813                }
1814             }
1815          }
1816       }
1817    }
1818
1819    bool OpenNode(ProjectNode node, bool noParsing)
1820    {
1821       char filePath[MAX_LOCATION];
1822       node.GetFullFilePath(filePath);
1823       return ide.OpenFile(filePath, normal, true/*false Why was it opening hidden?*/, null, something, normal, noParsing) ? true : false;
1824    }
1825
1826    void AddNode(ProjectNode node, DataRow addTo)
1827    {
1828       DataRow row = addTo ? addTo.AddRow() : fileList.AddRow();
1829
1830       row.tag = (int64)node;
1831       node.row = row;
1832
1833       if(node.type == resources)
1834          resourceRow = row;
1835
1836       row.SetData(null, node);
1837
1838       if(node.files && node.files.first && node.parent && 
1839             !(!node.parent.parent && 
1840                (!strcmpi(node.name, "notes") || !strcmpi(node.name, "sources") || 
1841                   !strcmpi(node.name, "src") || !strcmpi(node.name, "tools"))))
1842          row.collapsed = true;
1843       else if(node.type == folder)
1844          node.icon = openFolder;
1845
1846       if(node.files)
1847       {
1848          for(child : node.files)
1849             AddNode(child, row);
1850       }
1851    }
1852
1853    void DeleteNode(ProjectNode projectNode)
1854    {
1855       if(projectNode.files)
1856       {
1857          ProjectNode child;
1858          while(child = projectNode.files.first)
1859             DeleteNode(child);
1860       }
1861       fileList.DeleteRow(projectNode.row);
1862       projectNode.Delete();
1863    }
1864
1865    bool ProjectSave(MenuItem selection, Modifiers mods)
1866    {
1867       DataRow row = fileList.currentRow;
1868       ProjectNode node = row ? (ProjectNode)row.tag : null;
1869       Project prj = node ? node.project : null;
1870       if(prj)
1871       {
1872          prj.StopMonitoring();
1873          if(prj.Save(prj.filePath))
1874          {
1875             Project modPrj = null;
1876             prj.topNode.modified = false;
1877             for(p : ide.workspace.projects)
1878             {
1879                if(p.topNode.modified)
1880                { 
1881                   modPrj = p;
1882                   break;
1883                }
1884             }
1885             if(!modPrj)
1886                modifiedDocument = false;
1887             Update(null);
1888          }
1889          prj.StartMonitoring();
1890       }
1891       return true;
1892    }
1893
1894    bool ShowOutputBuildLog(bool cleanLog)
1895    {
1896       OutputView output = ide.outputView;
1897       if(cleanLog)
1898          output.ShowClearSelectTab(build);
1899       else
1900       {
1901          output.SelectTab(build);
1902          output.Show();
1903       }
1904       return true;
1905    }
1906
1907    bool DebugRestart()
1908    {
1909       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1910       ProjectConfig config = project.config;
1911       int bitDepth = ide.workspace.bitDepth;
1912
1913       bool result = false;
1914       if(/*!IsProjectModified() ||*/ BuildInterrim(project, restart, compiler, config, bitDepth, false))
1915       {
1916          // For Restart, compiler and config will only be used if for
1917          // whatever reason (if at all possible) the Debugger is in a
1918          // 'terminated' or 'none' state
1919          ide.debugger.Restart(compiler, config, bitDepth);
1920          result = true;
1921       }
1922
1923       delete compiler;
1924       return result;
1925    }
1926
1927    bool DebugResume()
1928    {
1929       ide.debugger.Resume();
1930       return true;
1931    }
1932
1933    bool DebugBreak()
1934    {
1935       ide.debugger.Break();
1936       return true;
1937    }
1938
1939    bool DebugStop()
1940    {
1941       ide.debugger.Stop();
1942       return true;
1943    }
1944
1945    bool DebugStepInto()
1946    {
1947       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1948       ProjectConfig config = project.config;
1949       int bitDepth = ide.workspace.bitDepth;
1950
1951       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config, bitDepth, false)))
1952          ide.debugger.StepInto(compiler, config, bitDepth);
1953       delete compiler;
1954       return true;
1955    }
1956
1957    bool DebugStepOver(bool skip)
1958    {
1959       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1960       ProjectConfig config = project.config;
1961       int bitDepth = ide.workspace.bitDepth;
1962
1963       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config, bitDepth, false)))
1964          ide.debugger.StepOver(compiler, config, bitDepth, skip);
1965
1966       delete compiler;
1967       return true;
1968    }
1969
1970    bool DebugStepOut(bool skip)
1971    {
1972       ide.debugger.StepOut(skip);
1973       return true;
1974    }
1975
1976    void ImportFolder(ProjectNode toNode)
1977    {
1978       if(toNode)
1979       {
1980          //bool isFolder = toNode.type == folder;
1981          //bool isRes = toNode.isInResources;
1982          
1983          FileDialog fileDialog = importFileDialog;
1984          fileDialog.master = parent;
1985          if(fileDialog.Modal() == ok)
1986          {
1987             ImportFolderFSI fsi { projectView = this };
1988             fsi.stack.Add(toNode);
1989             fsi.Iterate(fileDialog.filePath);
1990             delete fsi;
1991          }
1992       }
1993    }
1994
1995    ProjectNode NewFolder(ProjectNode parentNode, char * name, bool showProperties)
1996    {
1997       if(parentNode)
1998       {
1999          ProjectNode folderNode;
2000          Project prj = parentNode.project;
2001          int c;
2002          ProjectNode after = null;
2003          for(node : parentNode.files)
2004          {
2005             if(node.type != folder)
2006                break;
2007             after = node;
2008          }
2009          
2010          if(name && name[0])
2011             folderNode = parentNode.Add(prj, name, after, folder, folder, true);
2012          else
2013          {
2014             for(c = 0; c < 100; c++)
2015             {
2016                char string[16];
2017                sprintf(string, c ? "New Folder (%d)" : "New Folder", c);
2018                if((folderNode = parentNode.Add(prj, string, after, folder, folder, true)))
2019                   break;
2020             }
2021          }
2022
2023          if(folderNode)
2024          {
2025             NodeProperties nodeProperties;
2026             if(!showProperties)
2027             {
2028                modifiedDocument = true;
2029                prj.topNode.modified = true;
2030             }
2031             Update(null);
2032             folderNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
2033             folderNode.row.tag = (int64)folderNode;
2034                
2035             folderNode.row.SetData(null, folderNode);
2036             fileList.currentRow = folderNode.row;
2037             
2038             if(showProperties)
2039             {
2040                nodeProperties = NodeProperties
2041                {
2042                   parent, this, mode = newFolder, node = folderNode;
2043                   position = { position.x + 100, position.y + 100 };
2044                };
2045                nodeProperties.Create();   // Modal?
2046             }
2047             return folderNode;
2048          }
2049       }
2050       return null;
2051    }
2052
2053    void AddFiles(bool resources)
2054    {
2055       FileDialog fileDialog = (!resources) ? this.fileDialog : resourceFileDialog;
2056       fileDialog.type = multiOpen;
2057       fileDialog.text = !resources ? $"Add Files to Project" : $"Add Resources to Project";
2058       fileDialog.master = parent;
2059
2060       if(fileDialog.Modal() == ok)
2061       {
2062          int c;
2063          DataRow row = fileList.currentRow;
2064          ProjectNode parentNode = (ProjectNode)row.tag;
2065          bool addFailed = false;
2066          int numSelections = fileDialog.numSelections;
2067          char ** multiFilePaths = fileDialog.multiFilePaths;
2068
2069          Array<String> nameConflictFiles { };
2070
2071          for(c = 0; c < numSelections; c++)
2072          {
2073             char * filePath = multiFilePaths[c];
2074             FileAttribs exists = FileExists(filePath);
2075             bool addThisFile = true;
2076
2077             if(exists.isDirectory)
2078                addThisFile = false;
2079             else if(!exists)
2080             {
2081                if(MessageBox { master = ide, type = yesNo, text = filePath, 
2082                      contents = $"File doesn't exist. Create?" }.Modal() == yes)
2083                {
2084                   File f = FileOpen(filePath, write);
2085                   if(f)
2086                   {
2087                      addThisFile = true;
2088                      delete f;
2089                   }
2090                   else
2091                   {
2092                      MessageBox { master = ide, type = ok, text = filePath, 
2093                            contents = $"Couldn't create file."}.Modal();
2094                      addThisFile = false;
2095                   }
2096                }
2097             }
2098
2099             if(addThisFile)
2100             {
2101                /*addFailed = */if(!AddFile(parentNode, filePath, resources, false))//;
2102                {
2103                   nameConflictFiles.Add(CopyString(filePath));
2104                   addFailed = true;
2105                }
2106             }
2107          }
2108          if(addFailed)
2109          {
2110             int len = 0;
2111             char * part1 = $"The following file";
2112             char * opt1 = $" was ";
2113             char * opt2 = $"s were ";
2114             char * part2 = $"not added because of identical file name conflict within the project.\n\n";
2115             char * message;
2116             len += strlen(part1);
2117             len += strlen(part2);
2118             len += nameConflictFiles.count > 1 ? strlen(opt2) : strlen(opt1);
2119             for(s : nameConflictFiles)
2120                len += strlen(s) + 1;
2121             message = new char[len + 1];
2122             strcpy(message, part1);
2123             strcat(message, nameConflictFiles.count > 1 ? opt2 : opt1);
2124             strcat(message, part2);
2125             for(s : nameConflictFiles)
2126             {
2127                strcat(message, s);
2128                strcat(message, "\n");
2129             }
2130             MessageBox { master = ide, type = ok, text = $"Name Conflict", 
2131                   contents = message }.Modal();
2132             delete message;
2133          }
2134          nameConflictFiles.Free();
2135          delete nameConflictFiles;
2136       }
2137    }
2138
2139    ProjectNode AddFile(ProjectNode parentNode, char * filePath, bool resources, bool isTemporary)
2140    {
2141       ProjectNode result = null;
2142       ProjectNode after = null;
2143       for(node : parentNode.files)
2144       {
2145          if(node.type != folder && node.type != file && node.type)
2146             break;
2147          after = node;
2148       }
2149
2150       result = parentNode.Add(parentNode.project, filePath, after, file, NodeIcons::SelectFileIcon(filePath), !resources);
2151
2152       if(result)
2153       {
2154          if(!isTemporary)
2155          {
2156             modifiedDocument = true;
2157             parentNode.project.topNode.modified = true;
2158             parentNode.project.ModifiedAllConfigs(true, false, true, true);
2159          }
2160          Update(null);
2161          result.row = parentNode.row.AddRowAfter(after ? after.row : null);
2162          result.row.tag = (int64)result;
2163          result.row.SetData(null, result);
2164       }
2165       return result;
2166    }
2167
2168    CodeEditor CreateNew(char * upper, char * lower, char * base, char * className)
2169    {
2170       CodeEditor codeEditor = null;
2171       ProjectNode projectNode;
2172       ProjectNode after = null;
2173       DataRow row = fileList.currentRow;
2174       ProjectNode parentNode;
2175       int c;
2176
2177       if(!row) row = project.topNode.row;
2178
2179       parentNode = (ProjectNode)row.tag;
2180
2181       for(node : parentNode.files)
2182       {
2183          if(node.type != folder && node.type != file && node.type)
2184             break;
2185          after = node;
2186       }
2187       for(c = 1; c < 100; c++)
2188       {
2189          char string[16];
2190          sprintf(string, c ? "%s%d.ec" : "%s.ec", lower, c);
2191          if((projectNode = parentNode.Add(project, string, after, file, genFile, true)))
2192             break;
2193       }
2194       if(projectNode)
2195       {
2196          char name[256];
2197          char filePath[MAX_LOCATION];
2198
2199          modifiedDocument = true;
2200          project.topNode.modified = true;
2201          Update(null);
2202          project.ModifiedAllConfigs(true, false, false, true);
2203          projectNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
2204          projectNode.row.tag =(int64)projectNode;
2205             
2206          projectNode.row.SetData(null, projectNode);
2207          fileList.currentRow = projectNode.row;
2208
2209          strcpy(filePath, project.topNode.path);
2210          PathCat(filePath, projectNode.path);
2211          PathCat(filePath, projectNode.name);
2212
2213          codeEditor = (CodeEditor)ide.FindWindow(filePath);
2214          if(!codeEditor)
2215          {
2216             Class baseClass = eSystem_FindClass(__thisModule, base);
2217             subclass(ClassDesignerBase) designerClass = eClass_GetDesigner(baseClass);
2218             if(designerClass)
2219             {
2220                codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, false, null, whatever, normal, false);
2221                strcpy(name, projectNode.name);
2222                sprintf(name, "%s%d", upper, c);
2223                if(className)
2224                   strcpy(className, name);
2225
2226                designerClass.CreateNew(codeEditor.editBox, codeEditor.clientSize, name, base);
2227
2228                //codeEditor.modifiedDocument = false;
2229                //codeEditor.designer.modifiedDocument = false;
2230
2231                //Code_EnsureUpToDate(codeEditor);
2232             }
2233          }
2234          else // TODO: fix no symbols generated when ommiting {} for following else
2235          {
2236             codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, false, null, whatever, normal, false);
2237          }
2238          ide.sheet.visible = true;
2239          ide.sheet.Activate();
2240          if(codeEditor)
2241          {
2242             codeEditor.ViewDesigner();
2243             codeEditor.codeModified = true;
2244          }
2245       }
2246       //visible = false;
2247       return codeEditor;   
2248    }
2249
2250    // Returns true if we opened something
2251    bool OpenSelectedNodes(bool noParsing)
2252    {
2253       bool result = false;
2254       OldList selection;
2255       OldLink item;
2256
2257       fileList.GetMultiSelection(selection);
2258       for(item = selection.first; item; item = item.next)
2259       {
2260          DataRow row = item.data;
2261          ProjectNode node = (ProjectNode)row.tag;
2262          if(node.type == file)
2263          {
2264             OpenNode(node, noParsing);
2265             result = true;
2266          }
2267       }
2268       selection.Free(null);
2269       ide.RepositionWindows(false);
2270       return result;
2271    }
2272
2273    void RemoveSelectedNodes()
2274    {
2275       OldList selection;
2276       OldLink item, next;
2277       
2278       fileList.GetMultiSelection(selection);
2279
2280       // Remove children of parents we're deleting
2281       for(item = selection.first; item; item = next)
2282       {
2283          OldLink i;
2284          DataRow row = item.data;
2285          ProjectNode n, node = (ProjectNode)row.tag;
2286          bool remove = false;
2287
2288          next = item.next;
2289          for(i = selection.first; i && !remove; i = i.next)
2290          {
2291             ProjectNode iNode = (ProjectNode)((DataRow)i.data).tag;
2292             for(n = node.parent; n; n = n.parent)
2293             {
2294                if(iNode == n)
2295                {
2296                   remove = true;
2297                   break;
2298                }
2299             }
2300          }
2301          if(remove)
2302             selection.Delete(item);
2303       }
2304
2305       for(item = selection.first; item; item = item.next)
2306       {
2307          DataRow row = item.data;
2308          ProjectNode node = (ProjectNode)row.tag;
2309          ProjectNode resNode;
2310          for(resNode = node.parent; resNode; resNode = resNode.parent)
2311             if(resNode.type == resources)
2312                break;
2313          if(node.type == file)
2314          {
2315             Project prj = node.project;
2316             DeleteNode(node);
2317             modifiedDocument = true;
2318             prj.topNode.modified = true;
2319             Update(null);
2320             prj.ModifiedAllConfigs(true, false, true, true);
2321          }
2322          else if(node.type == folder)
2323          {
2324             char message[1024];
2325             sprintf(message, $"Are you sure you want to remove the folder \"%s\"\n"
2326                   "and all of its contents from the project?", node.name);
2327             if(MessageBox { master = ide, type = yesNo, text = $"Delete Folder", contents = message }.Modal() == yes)
2328             {
2329                Project prj = node.project;
2330                if(node.containsFile)
2331                   prj.ModifiedAllConfigs(true, false, true, true);
2332                DeleteNode(node);
2333                modifiedDocument = true;
2334                prj.topNode.modified = true;
2335             }
2336          }
2337          else if(node.type == project && node != project.topNode && !buildInProgress)
2338          {
2339             char message[1024];
2340             Project prj = null;
2341             for(p : workspace.projects)
2342             {
2343                if(p.topNode == node)
2344                {
2345                   prj = p;
2346                   break;
2347                }
2348             }
2349             sprintf(message, $"Are you sure you want to remove the \"%s\" project\n" "from this workspace?", node.name);
2350             if(MessageBox { master = ide, type = yesNo, text = $"Remove Project", contents = message }.Modal() == yes)
2351             {
2352                // THIS GOES FIRST!
2353                DeleteNode(node);
2354                if(prj)
2355                   workspace.RemoveProject(prj);
2356                //modifiedDocument = true; // when project view is a workspace view
2357             }
2358          }
2359       }
2360       selection.Free(null);
2361    }
2362 }