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