ide; debugger; valgrind integration. gdb dialog command history. old valgrind launche...
[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       bool useValgrind = ide.workspace.useValgrind;
1512       TargetTypes targetType = project.GetTargetType(config);
1513       if(targetType == sharedLibrary || targetType == staticLibrary)
1514          MessageBox { master = ide, type = ok, text = $"Run", contents = $"Shared and static libraries cannot be run like executables." }.Modal();
1515       else if(project.GetCompress(config))
1516          MessageBox { master = ide, text = $"Starting Debug", contents = $"Debugging compressed applications is not supported\n" }.Modal();
1517       else if(project.GetDebug(config) ||
1518          MessageBox { master = ide, type = okCancel, text = $"Starting Debug", contents = $"Attempting to debug non-debug configuration\nProceed anyways?" }.Modal() == ok)
1519       {
1520          if(/*!IsProjectModified() ||*/ BuildInterrim(project, start, compiler, config, bitDepth, false))
1521          {
1522             if(compiler.type.isVC)
1523             {
1524                //bool result = false;
1525                char oldwd[MAX_LOCATION];
1526                PathBackup pathBackup { };
1527                char command[MAX_LOCATION];
1528
1529                ide.SetPath(false, compiler, config, bitDepth);
1530                
1531                GetWorkingDir(oldwd, sizeof(oldwd));
1532                ChangeWorkingDir(project.topNode.path);
1533
1534                sprintf(command, "%s /useenv %s.sln /projectconfig \"%s|Win32\" /command \"%s\"" , "devenv", project.name, config.name, "Debug.Start");
1535                Execute(command);
1536                ChangeWorkingDir(oldwd);
1537
1538                delete pathBackup;
1539             }
1540             else
1541             {
1542                ide.debugger.Start(compiler, config, bitDepth, useValgrind);
1543                result = true;
1544             }
1545          }
1546       }
1547       delete compiler;
1548       return result;      
1549    }
1550
1551    void GoToError(const char * line, const bool noParsing)
1552    {
1553       char * colon;
1554       
1555       while(isspace(*line)) line++;
1556       colon = strstr(line, ":");
1557
1558       {
1559          int lineNumber = 0;
1560          int col = 1;
1561          bool lookForLineNumber = true;
1562
1563          // deal with linking error
1564          if(colon && colon[1] == ' ')
1565          {
1566             colon = strstr(colon + 1, ":");
1567             if(colon && !colon[1])
1568             {
1569                colon = strstr(line, ":");
1570                lookForLineNumber = false;
1571             }
1572             else if(colon && !isdigit(colon[1]))
1573             {
1574                line = colon + 1;
1575                colon = strstr(line, ":");
1576             }
1577          }
1578          // Don't be mistaken by the drive letter colon
1579          if(colon && (colon[1] == '/' || colon[1] == '\\'))
1580             colon = strstr(colon + 1, ":");
1581          if(colon && lookForLineNumber)
1582          {
1583             char * comma;
1584 #if 0 // MSVS Errors -- todo fix this later
1585             char * par = RSearchString(line, "(", colon - line, true, false);
1586             if(par && strstr(par, ")"))
1587                colon = par;
1588             else if((colon+1)[0] == ' ')
1589             {
1590                // NOTE: This is the same thing as saying 'colon = line'
1591                for( ; colon != line; colon--)
1592                   /*if(*colon == '(')
1593                      break*/;
1594             }
1595 #endif
1596             lineNumber = atoi(colon + 1);
1597             /*
1598             comma = strchr(colon, ',');
1599             if(comma)
1600                col = atoi(comma+1);
1601             */
1602             comma = strchr(colon+1, ':');
1603             if(comma)
1604                col = atoi(comma+1);
1605          }
1606          
1607          {
1608             char moduleName[MAX_LOCATION], filePath[MAX_LOCATION] = "";
1609             char ext[MAX_EXTENSION] = "";
1610             char * bracket;
1611             ProjectNode node = null;
1612             if(colon)
1613             {
1614                char * inFileIncludedFrom = strstr(line, stringInFileIncludedFrom);
1615                char * from = strstr(line, "from ");
1616                char * start = inFileIncludedFrom ? inFileIncludedFrom + strlen(stringInFileIncludedFrom) : from ? from + strlen("from ") : line;
1617                int len;
1618                if(colon < start)
1619                   start = line;
1620                len = Min((int)(colon - start), MAX_LOCATION-1);
1621                // Cut module name
1622                strncpy(moduleName, start, len);
1623                moduleName[len] = '\0';
1624             }
1625             else
1626                strcpy(moduleName, line);
1627
1628             if(!colon)
1629             {
1630                char * msg;
1631                if((msg = strstr(moduleName, " - ")))
1632                {
1633                   bool found = false;
1634                   msg[0] = '\0';
1635                   for(prj : ide.workspace.projects)
1636                   {
1637                      strcpy(filePath, prj.topNode.path);
1638                      PathCatSlash(filePath, moduleName);
1639                      if(FileExists(filePath).isFile)
1640                      {
1641                         found = true;
1642                         break;
1643                      }
1644                   }
1645                   if(!found)
1646                   {
1647                      msg[0] = ' ';
1648                      filePath[0] = '\0';
1649                   }
1650                }
1651                else if((msg = strstr(moduleName, "...")) && (colon = strchr(moduleName, ' ')) && (++colon)[0])
1652                {
1653                   bool found = false;
1654                   msg[0] = '\0';
1655                   for(prj : ide.workspace.projects)
1656                   {
1657                      if((node = prj.resNode.Find(colon, true)))
1658                      {
1659                         strcpy(filePath, prj.topNode.path);
1660                         PathCatSlash(filePath, node.path);
1661                         PathCatSlash(filePath, node.name);
1662
1663                         found = true;
1664                         break;
1665                      }
1666                   }
1667                   if(!found)
1668                   {
1669                      msg[0] = '.';
1670                      filePath[0] = '\0';
1671                   }
1672                }
1673                colon = null;
1674             }
1675             // Remove stuff in brackets
1676             /*
1677             bracket = strstr(moduleName, "(");
1678             if(bracket) *bracket = '\0';
1679             */
1680             MakeSlashPath(moduleName);
1681             GetExtension(moduleName, ext);
1682
1683             if(!colon && !filePath[0])
1684             {
1685                bool dotMain = false;
1686                // Check if it's one of our modules
1687                node = project.topNode.Find(moduleName, false);
1688                if(node)
1689                {
1690                   strcpy(filePath, node.path);
1691                   PathCatSlash(filePath, node.name);
1692                }
1693                else
1694                {
1695                   IntermediateFileType type = none;
1696                   ProjectConfig config;
1697                   if(ext[0])
1698                   {
1699                      char ext2[MAX_EXTENSION];
1700                      char stripExt[MAX_LOCATION];
1701                      strcpy(stripExt, moduleName);
1702                      StripExtension(stripExt);
1703                      GetExtension(stripExt, ext2);
1704                      if(ext2[0] && !strcmp(ext2, "main"))
1705                         dotMain = true;
1706                      if(!strcmp(ext, "ec"))
1707                         type = ec;
1708                      else if(!strcmp(ext, "c"))
1709                         type = c;
1710                      else if(!strcmp(ext, "sym"))
1711                         type = sym;
1712                      else if(!strcmp(ext, "imp"))
1713                         type = imp;
1714                      else if(!strcmp(ext, "bowl"))
1715                         type = bowl;
1716                      else if(!strcmp(ext, "o"))
1717                         type = o;
1718
1719                      if(type)
1720                      {
1721                         for(prj : ide.workspace.projects; prj.lastBuildConfigName)
1722                         {
1723                            if((config = prj.GetConfig(prj.lastBuildConfigName)))
1724                               node = prj.FindNodeByObjectFileName(moduleName, type, dotMain, config);
1725                            if(node)
1726                               break;
1727                         }
1728                      }
1729                   }
1730                   if(node)
1731                   {
1732                      char name[MAX_FILENAME];
1733                      Project project = node.project;
1734                      CompilerConfig compiler = ideSettings.GetCompilerConfig(project.lastBuildCompilerName);
1735                      if(compiler)
1736                      {
1737                         int bitDepth = ide.workspace.bitDepth;
1738                         DirExpression objDir = project.GetObjDir(compiler, config, bitDepth);
1739                         strcpy(filePath, project.topNode.path);
1740                         PathCatSlash(filePath, objDir.dir);
1741                         node.GetObjectFileName(name, project.lastBuildNamesInfo, type, dotMain);
1742                         PathCatSlash(filePath, name);
1743                         delete objDir;
1744                      }
1745                      delete compiler;
1746                   }
1747                }
1748                if(!node)
1749                   filePath[0] = '\0';
1750             }
1751             if(!strcmp(ext, "a") || !strcmp(ext, "o") || !strcmp(ext, "lib") ||
1752                   !strcmp(ext, "dll") || !strcmp(ext, "exe") || !strcmp(ext, "mo"))
1753                moduleName[0] = 0;    // Avoid opening binary files
1754             if(moduleName[0])
1755             {
1756                CodeEditor codeEditor;
1757                if(!filePath[0])
1758                {
1759                   strcpy(filePath, project.topNode.path);
1760                   PathCatSlash(filePath, moduleName);
1761                }
1762       
1763                codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal, noParsing);
1764                if(!codeEditor && !strcmp(ext, "c"))
1765                {
1766                   char ecName[MAX_LOCATION];
1767                   ChangeExtension(filePath, "ec", ecName);
1768                   codeEditor = (CodeEditor)ide.OpenFile(ecName, normal, true, null, no, normal, noParsing);
1769                }
1770                if(!codeEditor)
1771                {
1772                   char path[MAX_LOCATION];
1773                   // TOFIX: Improve on this, don't use only filename, make a function
1774                   if(ide && ide.workspace)
1775                   {
1776                      for(prj : ide.workspace.projects)
1777                      {
1778                         ProjectNode node;
1779                         MakePathRelative(filePath, prj.topNode.path, path);
1780
1781                         if((node = prj.topNode.FindWithPath(path, false)))
1782                         {
1783                            strcpy(filePath, prj.topNode.path);
1784                            PathCatSlash(filePath, node.path);
1785                            PathCatSlash(filePath, node.name);
1786                            codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal, noParsing);
1787                            if(codeEditor)
1788                               break;
1789                         }
1790                      }
1791                      if(!codeEditor && !strchr(moduleName, '/') && !strchr(moduleName, '\\'))
1792                      {
1793                         for(prj : ide.workspace.projects)
1794                         {
1795                            ProjectNode node;
1796                            if((node = prj.topNode.Find(moduleName, false)))
1797                            {
1798                               strcpy(filePath, prj.topNode.path);
1799                               PathCatSlash(filePath, node.path);
1800                               PathCatSlash(filePath, node.name);
1801                               codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, true, null, no, normal, noParsing);
1802                               if(codeEditor)
1803                                  break;
1804                            }
1805                         }
1806                      }
1807                   }
1808                }
1809                if(codeEditor && lineNumber)
1810                {
1811                   EditBox editBox = codeEditor.editBox;
1812                   editBox.GoToLineNum(lineNumber - 1);
1813                   editBox.GoToPosition(editBox.line, lineNumber - 1, col ? (col - 1) : 0);
1814                }
1815             }
1816          }
1817       }
1818    }
1819
1820    bool OpenNode(ProjectNode node, bool noParsing)
1821    {
1822       char filePath[MAX_LOCATION];
1823       node.GetFullFilePath(filePath);
1824       return ide.OpenFile(filePath, normal, true/*false Why was it opening hidden?*/, null, something, normal, noParsing) ? true : false;
1825    }
1826
1827    void AddNode(ProjectNode node, DataRow addTo)
1828    {
1829       DataRow row = addTo ? addTo.AddRow() : fileList.AddRow();
1830
1831       row.tag = (int64)node;
1832       node.row = row;
1833
1834       if(node.type == resources)
1835          resourceRow = row;
1836
1837       row.SetData(null, node);
1838
1839       if(node.files && node.files.first && node.parent && 
1840             !(!node.parent.parent && 
1841                (!strcmpi(node.name, "notes") || !strcmpi(node.name, "sources") || 
1842                   !strcmpi(node.name, "src") || !strcmpi(node.name, "tools"))))
1843          row.collapsed = true;
1844       else if(node.type == folder)
1845          node.icon = openFolder;
1846
1847       if(node.files)
1848       {
1849          for(child : node.files)
1850             AddNode(child, row);
1851       }
1852    }
1853
1854    void DeleteNode(ProjectNode projectNode)
1855    {
1856       if(projectNode.files)
1857       {
1858          ProjectNode child;
1859          while(child = projectNode.files.first)
1860             DeleteNode(child);
1861       }
1862       fileList.DeleteRow(projectNode.row);
1863       projectNode.Delete();
1864    }
1865
1866    bool ProjectSave(MenuItem selection, Modifiers mods)
1867    {
1868       DataRow row = fileList.currentRow;
1869       ProjectNode node = row ? (ProjectNode)row.tag : null;
1870       Project prj = node ? node.project : null;
1871       if(prj)
1872       {
1873          prj.StopMonitoring();
1874          if(prj.Save(prj.filePath))
1875          {
1876             Project modPrj = null;
1877             prj.topNode.modified = false;
1878             for(p : ide.workspace.projects)
1879             {
1880                if(p.topNode.modified)
1881                { 
1882                   modPrj = p;
1883                   break;
1884                }
1885             }
1886             if(!modPrj)
1887                modifiedDocument = false;
1888             Update(null);
1889          }
1890          prj.StartMonitoring();
1891       }
1892       return true;
1893    }
1894
1895    bool ShowOutputBuildLog(bool cleanLog)
1896    {
1897       OutputView output = ide.outputView;
1898       if(cleanLog)
1899          output.ShowClearSelectTab(build);
1900       else
1901       {
1902          output.SelectTab(build);
1903          output.Show();
1904       }
1905       return true;
1906    }
1907
1908    bool DebugRestart()
1909    {
1910       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1911       ProjectConfig config = project.config;
1912       int bitDepth = ide.workspace.bitDepth;
1913       bool useValgrind = ide.workspace.useValgrind;
1914
1915       bool result = false;
1916       if(/*!IsProjectModified() ||*/ BuildInterrim(project, restart, compiler, config, bitDepth, false))
1917       {
1918          // For Restart, compiler and config will only be used if for
1919          // whatever reason (if at all possible) the Debugger is in a
1920          // 'terminated' or 'none' state
1921          ide.debugger.Restart(compiler, config, bitDepth, useValgrind);
1922          result = true;
1923       }
1924
1925       delete compiler;
1926       return result;
1927    }
1928
1929    bool DebugResume()
1930    {
1931       ide.debugger.Resume();
1932       return true;
1933    }
1934
1935    bool DebugBreak()
1936    {
1937       ide.debugger.Break();
1938       return true;
1939    }
1940
1941    bool DebugStop()
1942    {
1943       ide.debugger.Stop();
1944       return true;
1945    }
1946
1947    bool DebugStepInto()
1948    {
1949       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1950       ProjectConfig config = project.config;
1951       int bitDepth = ide.workspace.bitDepth;
1952       bool useValgrind = ide.workspace.useValgrind;
1953
1954       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config, bitDepth, false)))
1955          ide.debugger.StepInto(compiler, config, bitDepth, useValgrind);
1956       delete compiler;
1957       return true;
1958    }
1959
1960    bool DebugStepOver(bool skip)
1961    {
1962       CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
1963       ProjectConfig config = project.config;
1964       int bitDepth = ide.workspace.bitDepth;
1965       bool useValgrind = ide.workspace.useValgrind;
1966
1967       if((ide.debugger.isActive) || (!buildInProgress && BuildInterrim(project, start, compiler, config, bitDepth, false)))
1968          ide.debugger.StepOver(compiler, config, bitDepth, useValgrind, skip);
1969
1970       delete compiler;
1971       return true;
1972    }
1973
1974    bool DebugStepOut(bool skip)
1975    {
1976       ide.debugger.StepOut(skip);
1977       return true;
1978    }
1979
1980    void ImportFolder(ProjectNode toNode)
1981    {
1982       if(toNode)
1983       {
1984          //bool isFolder = toNode.type == folder;
1985          //bool isRes = toNode.isInResources;
1986          
1987          FileDialog fileDialog = importFileDialog;
1988          fileDialog.master = parent;
1989          if(fileDialog.Modal() == ok)
1990          {
1991             ImportFolderFSI fsi { projectView = this };
1992             fsi.stack.Add(toNode);
1993             fsi.Iterate(fileDialog.filePath);
1994             delete fsi;
1995          }
1996       }
1997    }
1998
1999    ProjectNode NewFolder(ProjectNode parentNode, char * name, bool showProperties)
2000    {
2001       if(parentNode)
2002       {
2003          ProjectNode folderNode;
2004          Project prj = parentNode.project;
2005          int c;
2006          ProjectNode after = null;
2007          for(node : parentNode.files)
2008          {
2009             if(node.type != folder)
2010                break;
2011             after = node;
2012          }
2013          
2014          if(name && name[0])
2015             folderNode = parentNode.Add(prj, name, after, folder, folder, true);
2016          else
2017          {
2018             for(c = 0; c < 100; c++)
2019             {
2020                char string[16];
2021                sprintf(string, c ? "New Folder (%d)" : "New Folder", c);
2022                if((folderNode = parentNode.Add(prj, string, after, folder, folder, true)))
2023                   break;
2024             }
2025          }
2026
2027          if(folderNode)
2028          {
2029             NodeProperties nodeProperties;
2030             if(!showProperties)
2031             {
2032                modifiedDocument = true;
2033                prj.topNode.modified = true;
2034             }
2035             Update(null);
2036             folderNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
2037             folderNode.row.tag = (int64)folderNode;
2038                
2039             folderNode.row.SetData(null, folderNode);
2040             fileList.currentRow = folderNode.row;
2041             
2042             if(showProperties)
2043             {
2044                nodeProperties = NodeProperties
2045                {
2046                   parent, this, mode = newFolder, node = folderNode;
2047                   position = { position.x + 100, position.y + 100 };
2048                };
2049                nodeProperties.Create();   // Modal?
2050             }
2051             return folderNode;
2052          }
2053       }
2054       return null;
2055    }
2056
2057    void AddFiles(bool resources)
2058    {
2059       FileDialog fileDialog = (!resources) ? this.fileDialog : resourceFileDialog;
2060       fileDialog.type = multiOpen;
2061       fileDialog.text = !resources ? $"Add Files to Project" : $"Add Resources to Project";
2062       fileDialog.master = parent;
2063
2064       if(fileDialog.Modal() == ok)
2065       {
2066          int c;
2067          DataRow row = fileList.currentRow;
2068          ProjectNode parentNode = (ProjectNode)row.tag;
2069          bool addFailed = false;
2070          int numSelections = fileDialog.numSelections;
2071          char ** multiFilePaths = fileDialog.multiFilePaths;
2072
2073          Array<String> nameConflictFiles { };
2074
2075          for(c = 0; c < numSelections; c++)
2076          {
2077             char * filePath = multiFilePaths[c];
2078             FileAttribs exists = FileExists(filePath);
2079             bool addThisFile = true;
2080
2081             if(exists.isDirectory)
2082                addThisFile = false;
2083             else if(!exists)
2084             {
2085                if(MessageBox { master = ide, type = yesNo, text = filePath, 
2086                      contents = $"File doesn't exist. Create?" }.Modal() == yes)
2087                {
2088                   File f = FileOpen(filePath, write);
2089                   if(f)
2090                   {
2091                      addThisFile = true;
2092                      delete f;
2093                   }
2094                   else
2095                   {
2096                      MessageBox { master = ide, type = ok, text = filePath, 
2097                            contents = $"Couldn't create file."}.Modal();
2098                      addThisFile = false;
2099                   }
2100                }
2101             }
2102
2103             if(addThisFile)
2104             {
2105                /*addFailed = */if(!AddFile(parentNode, filePath, resources, false))//;
2106                {
2107                   nameConflictFiles.Add(CopyString(filePath));
2108                   addFailed = true;
2109                }
2110             }
2111          }
2112          if(addFailed)
2113          {
2114             int len = 0;
2115             char * part1 = $"The following file";
2116             char * opt1 = $" was ";
2117             char * opt2 = $"s were ";
2118             char * part2 = $"not added because of identical file name conflict within the project.\n\n";
2119             char * message;
2120             len += strlen(part1);
2121             len += strlen(part2);
2122             len += nameConflictFiles.count > 1 ? strlen(opt2) : strlen(opt1);
2123             for(s : nameConflictFiles)
2124                len += strlen(s) + 1;
2125             message = new char[len + 1];
2126             strcpy(message, part1);
2127             strcat(message, nameConflictFiles.count > 1 ? opt2 : opt1);
2128             strcat(message, part2);
2129             for(s : nameConflictFiles)
2130             {
2131                strcat(message, s);
2132                strcat(message, "\n");
2133             }
2134             MessageBox { master = ide, type = ok, text = $"Name Conflict", 
2135                   contents = message }.Modal();
2136             delete message;
2137          }
2138          nameConflictFiles.Free();
2139          delete nameConflictFiles;
2140       }
2141    }
2142
2143    ProjectNode AddFile(ProjectNode parentNode, char * filePath, bool resources, bool isTemporary)
2144    {
2145       ProjectNode result = null;
2146       ProjectNode after = null;
2147       for(node : parentNode.files)
2148       {
2149          if(node.type != folder && node.type != file && node.type)
2150             break;
2151          after = node;
2152       }
2153
2154       result = parentNode.Add(parentNode.project, filePath, after, file, NodeIcons::SelectFileIcon(filePath), !resources);
2155
2156       if(result)
2157       {
2158          if(!isTemporary)
2159          {
2160             modifiedDocument = true;
2161             parentNode.project.topNode.modified = true;
2162             parentNode.project.ModifiedAllConfigs(true, false, true, true);
2163          }
2164          Update(null);
2165          result.row = parentNode.row.AddRowAfter(after ? after.row : null);
2166          result.row.tag = (int64)result;
2167          result.row.SetData(null, result);
2168       }
2169       return result;
2170    }
2171
2172    CodeEditor CreateNew(char * upper, char * lower, char * base, char * className)
2173    {
2174       CodeEditor codeEditor = null;
2175       ProjectNode projectNode;
2176       ProjectNode after = null;
2177       DataRow row = fileList.currentRow;
2178       ProjectNode parentNode;
2179       int c;
2180
2181       if(!row) row = project.topNode.row;
2182
2183       parentNode = (ProjectNode)row.tag;
2184
2185       for(node : parentNode.files)
2186       {
2187          if(node.type != folder && node.type != file && node.type)
2188             break;
2189          after = node;
2190       }
2191       for(c = 1; c < 100; c++)
2192       {
2193          char string[16];
2194          sprintf(string, c ? "%s%d.ec" : "%s.ec", lower, c);
2195          if((projectNode = parentNode.Add(project, string, after, file, genFile, true)))
2196             break;
2197       }
2198       if(projectNode)
2199       {
2200          char name[256];
2201          char filePath[MAX_LOCATION];
2202
2203          modifiedDocument = true;
2204          project.topNode.modified = true;
2205          Update(null);
2206          project.ModifiedAllConfigs(true, false, false, true);
2207          projectNode.row = parentNode.row.AddRowAfter(after ? after.row : null);
2208          projectNode.row.tag =(int64)projectNode;
2209             
2210          projectNode.row.SetData(null, projectNode);
2211          fileList.currentRow = projectNode.row;
2212
2213          strcpy(filePath, project.topNode.path);
2214          PathCat(filePath, projectNode.path);
2215          PathCat(filePath, projectNode.name);
2216
2217          codeEditor = (CodeEditor)ide.FindWindow(filePath);
2218          if(!codeEditor)
2219          {
2220             Class baseClass = eSystem_FindClass(__thisModule, base);
2221             subclass(ClassDesignerBase) designerClass = eClass_GetDesigner(baseClass);
2222             if(designerClass)
2223             {
2224                codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, false, null, whatever, normal, false);
2225                strcpy(name, projectNode.name);
2226                sprintf(name, "%s%d", upper, c);
2227                if(className)
2228                   strcpy(className, name);
2229
2230                designerClass.CreateNew(codeEditor.editBox, codeEditor.clientSize, name, base);
2231
2232                //codeEditor.modifiedDocument = false;
2233                //codeEditor.designer.modifiedDocument = false;
2234
2235                //Code_EnsureUpToDate(codeEditor);
2236             }
2237          }
2238          else // TODO: fix no symbols generated when ommiting {} for following else
2239          {
2240             codeEditor = (CodeEditor)ide.OpenFile(filePath, normal, false, null, whatever, normal, false);
2241          }
2242          ide.sheet.visible = true;
2243          ide.sheet.Activate();
2244          if(codeEditor)
2245          {
2246             codeEditor.ViewDesigner();
2247             codeEditor.codeModified = true;
2248          }
2249       }
2250       //visible = false;
2251       return codeEditor;   
2252    }
2253
2254    // Returns true if we opened something
2255    bool OpenSelectedNodes(bool noParsing)
2256    {
2257       bool result = false;
2258       OldList selection;
2259       OldLink item;
2260
2261       fileList.GetMultiSelection(selection);
2262       for(item = selection.first; item; item = item.next)
2263       {
2264          DataRow row = item.data;
2265          ProjectNode node = (ProjectNode)row.tag;
2266          if(node.type == file)
2267          {
2268             OpenNode(node, noParsing);
2269             result = true;
2270          }
2271       }
2272       selection.Free(null);
2273       ide.RepositionWindows(false);
2274       return result;
2275    }
2276
2277    void RemoveSelectedNodes()
2278    {
2279       OldList selection;
2280       OldLink item, next;
2281       
2282       fileList.GetMultiSelection(selection);
2283
2284       // Remove children of parents we're deleting
2285       for(item = selection.first; item; item = next)
2286       {
2287          OldLink i;
2288          DataRow row = item.data;
2289          ProjectNode n, node = (ProjectNode)row.tag;
2290          bool remove = false;
2291
2292          next = item.next;
2293          for(i = selection.first; i && !remove; i = i.next)
2294          {
2295             ProjectNode iNode = (ProjectNode)((DataRow)i.data).tag;
2296             for(n = node.parent; n; n = n.parent)
2297             {
2298                if(iNode == n)
2299                {
2300                   remove = true;
2301                   break;
2302                }
2303             }
2304          }
2305          if(remove)
2306             selection.Delete(item);
2307       }
2308
2309       for(item = selection.first; item; item = item.next)
2310       {
2311          DataRow row = item.data;
2312          ProjectNode node = (ProjectNode)row.tag;
2313          ProjectNode resNode;
2314          for(resNode = node.parent; resNode; resNode = resNode.parent)
2315             if(resNode.type == resources)
2316                break;
2317          if(node.type == file)
2318          {
2319             Project prj = node.project;
2320             DeleteNode(node);
2321             modifiedDocument = true;
2322             prj.topNode.modified = true;
2323             Update(null);
2324             prj.ModifiedAllConfigs(true, false, true, true);
2325          }
2326          else if(node.type == folder)
2327          {
2328             char message[1024];
2329             sprintf(message, $"Are you sure you want to remove the folder \"%s\"\n"
2330                   "and all of its contents from the project?", node.name);
2331             if(MessageBox { master = ide, type = yesNo, text = $"Delete Folder", contents = message }.Modal() == yes)
2332             {
2333                Project prj = node.project;
2334                if(node.containsFile)
2335                   prj.ModifiedAllConfigs(true, false, true, true);
2336                DeleteNode(node);
2337                modifiedDocument = true;
2338                prj.topNode.modified = true;
2339             }
2340          }
2341          else if(node.type == project && node != project.topNode && !buildInProgress)
2342          {
2343             char message[1024];
2344             Project prj = null;
2345             for(p : workspace.projects)
2346             {
2347                if(p.topNode == node)
2348                {
2349                   prj = p;
2350                   break;
2351                }
2352             }
2353             sprintf(message, $"Are you sure you want to remove the \"%s\" project\n" "from this workspace?", node.name);
2354             if(MessageBox { master = ide, type = yesNo, text = $"Remove Project", contents = message }.Modal() == yes)
2355             {
2356                // THIS GOES FIRST!
2357                DeleteNode(node);
2358                if(prj)
2359                   workspace.RemoveProject(prj);
2360                //modifiedDocument = true; // when project view is a workspace view
2361             }
2362          }
2363       }
2364       selection.Free(null);
2365    }
2366 }