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