//protected override void OnGotFocus(EventArgs e) { _c.Focus(); } /// <summary> /// Loads existing or new workspace. /// If fails, shows a task dialog with several choices - retry, load another, create new, cancel. If then fails, ends process. /// Sets Model and Text properties of the main form. Updates recent files. /// By default runs startup script. /// </summary> /// <param name="wsDir"> /// Workspace's directory. The directory should contain file "files.xml" and subdirectory "files". /// If null, loads the last used workspace (its path is in settings). /// If the setting does not exist, uses AFolders.ThisAppDocuments + @"Main". /// If the file does not exist, copies from AFolders.ThisApp + @"Default\Workspace". /// </param> public FilesModel ZLoadWorkspace(string wsDir = null) { wsDir ??= Program.Settings.workspace; if(wsDir.NE()) wsDir = AFolders.ThisAppDocuments + "Main"; var xmlFile = wsDir + @"\files.xml"; var oldModel = _model; FilesModel m = null; _isNewWorkspace = false; g1: try { //SHOULDDO: if editor runs as admin, the workspace directory should be write-protected from non-admin processes. if(_isNewWorkspace = !AFile.ExistsAsFile(xmlFile)) { AFile.Copy(AFolders.ThisAppBS + @"Default\Workspace", wsDir); } _model?.UnloadingWorkspace(); //saves all, closes documents, sets current file = null m = new FilesModel(_c, xmlFile); _c.Model = m; } catch(Exception ex) { m?.Dispose(); m = null; //AOutput.Write($"Failed to load '{wsDir}'. {ex.Message}"); switch(ADialog.ShowError("Failed to load workspace", wsDir, "1 Retry|2 Load another|3 Create new|0 Cancel", owner: this, expandedText: ex.ToString())) { case 1: goto g1; case 2: m = ZLoadAnotherWorkspace(); break; case 3: m = ZLoadNewWorkspace(); break; } if(m != null) return m; if(_model != null) return _model; Environment.Exit(1); } oldModel?.Dispose(); Program.Model = _model = m; //CONSIDER: unexpand path if(wsDir != Program.Settings.workspace) { if(Program.Settings.workspace != null) { var ar = Program.Settings.recentWS ?? Array.Empty<string>(); int i = Array.IndexOf(ar, wsDir); if(i >= 0) ar = ar.RemoveAt(i); Program.Settings.recentWS = ar.InsertAt(0, Program.Settings.workspace); } Program.Settings.workspace = wsDir; } Program.MainForm.ZSetTitle(); if(Program.Loaded >= EProgramState.LoadedWorkspace) { ZOpenDocuments(); ZModel.RunStartupScripts(); } return _model; }