root / src / SolidGui / MainWindowView.cs
History | View | Annotate | Download
| 1 | using System; |
|---|---|
| 2 | using System.IO; |
| 3 | using System.Windows.Forms; |
| 4 | using System.Text; |
| 5 | using Solid.Engine; |
| 6 | using SolidGui.Engine; |
| 7 | using SolidGui.Export; |
| 8 | using SolidGui.Properties; |
| 9 | using SolidGui.Search; |
| 10 | |
| 11 | namespace SolidGui |
| 12 | {
|
| 13 | /// <summary> |
| 14 | /// View component of MainWindow. The logic is in the MainWindowPM. |
| 15 | /// </summary> |
| 16 | public partial class MainWindowView : Form |
| 17 | {
|
| 18 | private readonly MainWindowPM _mainWindowPM; |
| 19 | private int _filterIndex; |
| 20 | |
| 21 | public MainWindowView(MainWindowPM mainWindowPM) |
| 22 | {
|
| 23 | |
| 24 | InitializeComponent(); |
| 25 | if (DesignMode) |
| 26 | {
|
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | _mainWindowPM = mainWindowPM; |
| 31 | _filterIndex = 0; |
| 32 | _sfmEditorView.BindModel(_mainWindowPM.SfmEditorModel); |
| 33 | _recordNavigatorView.BindModel(_mainWindowPM.NavigatorModel); |
| 34 | _filterChooserView.BindModel(_mainWindowPM.FilterChooserModel); |
| 35 | |
| 36 | this.KeyPreview = true; |
| 37 | |
| 38 | _mainWindowPM.DictionaryProcessed += this.OnDictionaryProcessed; |
| 39 | _mainWindowPM.NavigatorModel.RecordChanged += _sfmEditorView.OnRecordChanged; |
| 40 | _mainWindowPM.NavigatorModel.FilterChanged += _recordNavigatorView.OnFilterChanged; |
| 41 | _mainWindowPM.FilterChooserModel.RecordFilterChanged += _mainWindowPM.NavigatorModel.OnFilterChanged; |
| 42 | _mainWindowPM.FilterChooserModel.RecordFilterChanged += _filterChooserView.OnFilterChanged; |
| 43 | _mainWindowPM.FilterChooserModel.RecordFilterChanged += _markerDetails.OnFilterChanged; |
| 44 | _mainWindowPM.SearchModel.WordFound += OnWordFound; |
| 45 | |
| 46 | //_markerDetails.RecordFilterChanged += _mainWindowPM.NavigatorModel.OnFilterChanged; |
| 47 | |
| 48 | // Event wiring for child views. |
| 49 | _recordNavigatorView._recheckButton.Click += _sfmEditorView.OnRecheckClicked; |
| 50 | |
| 51 | // Event wiring for the main view. |
| 52 | _markerDetails.MarkerSettingPossiblyChanged += OnMarkerSettingPossiblyChanged; |
| 53 | _sfmEditorView.RecordTextChanged += OnRecordTextChanged; |
| 54 | _recordNavigatorView.SearchButtonClicked += OnSearchClick; |
| 55 | |
| 56 | splitContainer1.Panel1.Enabled = false; |
| 57 | splitContainer2.Panel1.Enabled = false; |
| 58 | splitContainer2.Panel2.Enabled = false; |
| 59 | _sfmEditorView.Enabled = false; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | private bool ReturnFalse() |
| 64 | {
|
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | public void OnDictionaryProcessed(object sender, EventArgs e) |
| 69 | {
|
| 70 | //wire up the change of record event to our record display widget |
| 71 | _markerDetails.BindModel( |
| 72 | _mainWindowPM.MarkerSettingsModel, |
| 73 | _mainWindowPM.FilterChooserModel, |
| 74 | _mainWindowPM.WorkingDictionary, |
| 75 | _mainWindowPM.Settings |
| 76 | ); |
| 77 | _filterChooserView.UpdateDisplay(); |
| 78 | _markerDetails.UpdateDisplay(); |
| 79 | UpdateDisplay(); |
| 80 | } |
| 81 | |
| 82 | private void OnOpenClick(object sender, EventArgs e) |
| 83 | {
|
| 84 | if (NeedsSave()) |
| 85 | {
|
| 86 | DialogResult result = MessageBox.Show(this, "Changes may have been made to your current work. Before opening a new file would you like to save your work?", "Solid", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); |
| 87 | if (result == DialogResult.Cancel) |
| 88 | {
|
| 89 | return; |
| 90 | } |
| 91 | else if (result == DialogResult.Yes) |
| 92 | {
|
| 93 | OnSaveClick(sender, e); |
| 94 | } |
| 95 | } |
| 96 | ChooseProject(); |
| 97 | } |
| 98 | |
| 99 | private void ChooseProject() |
| 100 | {
|
| 101 | string initialDirectory = null; |
| 102 | if (!String.IsNullOrEmpty(Settings.Default.PreviousPathToDictionary)) |
| 103 | {
|
| 104 | try |
| 105 | {
|
| 106 | if (File.Exists(Settings.Default.PreviousPathToDictionary)) |
| 107 | {
|
| 108 | initialDirectory = Path.GetDirectoryName(Settings.Default.PreviousPathToDictionary); |
| 109 | } |
| 110 | } |
| 111 | catch |
| 112 | {
|
| 113 | //swallow |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (initialDirectory == null || initialDirectory == "") |
| 118 | {
|
| 119 | initialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
| 120 | } |
| 121 | |
| 122 | OpenFileDialog dlg = new OpenFileDialog(); |
| 123 | dlg.Title = "Open Dictionary File..."; |
| 124 | dlg.DefaultExt = ".db"; |
| 125 | dlg.FileName = Settings.Default.PreviousPathToDictionary; |
| 126 | dlg.Filter = "Toolbox Database (.db .txt .lex .sfm)|*.db;*.txt;*.lex;*.sfm|All Files (*.*)|*.*"; |
| 127 | dlg.Multiselect = false; |
| 128 | dlg.InitialDirectory = initialDirectory; |
| 129 | if (DialogResult.OK != dlg.ShowDialog(this)) |
| 130 | {
|
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | Settings.Default.PreviousPathToDictionary = dlg.FileName; |
| 135 | |
| 136 | Cursor = Cursors.WaitCursor; |
| 137 | string templatePath = null; |
| 138 | if (_mainWindowPM.ShouldAskForTemplateBeforeOpening(dlg.FileName)) |
| 139 | {
|
| 140 | templatePath = RequestTemplatePath(dlg.FileName, false); |
| 141 | if (string.IsNullOrEmpty(templatePath)) |
| 142 | {
|
| 143 | return; //they cancelled |
| 144 | } |
| 145 | } |
| 146 | Cursor = Cursors.WaitCursor; |
| 147 | _mainWindowPM.OpenDictionary(dlg.FileName, templatePath ); |
| 148 | OnFileLoaded(dlg.FileName); |
| 149 | Cursor = Cursors.Default; |
| 150 | } |
| 151 | |
| 152 | public void OnFileLoaded(string name) |
| 153 | {
|
| 154 | Text = "SOLID " + name; |
| 155 | splitContainer1.Panel1.Enabled = true; |
| 156 | splitContainer2.Panel1.Enabled = true; |
| 157 | splitContainer2.Panel2.Enabled = true; |
| 158 | _sfmEditorView.Enabled = true; |
| 159 | _markerDetails.SelectMarker("lx");
|
| 160 | _mainWindowPM.NavigatorModel.StartupOrReset(); |
| 161 | _sfmEditorView.Focus(); |
| 162 | } |
| 163 | |
| 164 | public bool NeedsSave() |
| 165 | {
|
| 166 | return _saveButton.Enabled; |
| 167 | } |
| 168 | |
| 169 | private void MainWindowView_Load(object sender, EventArgs e) |
| 170 | {
|
| 171 | if (DesignMode) |
| 172 | {
|
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | UpdateDisplay(); |
| 177 | } |
| 178 | |
| 179 | private void OnRecordTextChanged(object sender, EventArgs e) |
| 180 | {
|
| 181 | _saveButton.Enabled = true; |
| 182 | } |
| 183 | |
| 184 | private void OnMarkerSettingPossiblyChanged(object sender, EventArgs e) |
| 185 | {
|
| 186 | _saveButton.Enabled = true; |
| 187 | _sfmEditorView.OnSolidSettingsChange(); |
| 188 | } |
| 189 | |
| 190 | private void OnRecheckButtonClick(object sender, EventArgs e) |
| 191 | {
|
| 192 | Cursor = Cursors.WaitCursor; |
| 193 | _sfmEditorView.UpdateModel(); |
| 194 | _mainWindowPM.ProcessLexicon(); |
| 195 | _sfmEditorView.HighlightMarkers = _mainWindowPM.NavigatorModel.ActiveFilter.HighlightMarkers; |
| 196 | |
| 197 | _sfmEditorView.Reload(); |
| 198 | Cursor = Cursors.Default; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | private void UpdateDisplay() |
| 204 | {
|
| 205 | _filterChooserView.Enabled = _mainWindowPM.CanProcessLexicon; |
| 206 | _changeTemplate.Enabled = _mainWindowPM.CanProcessLexicon; |
| 207 | _exportButton.Enabled = _mainWindowPM.CanProcessLexicon; |
| 208 | _recordNavigatorView.Enabled = _mainWindowPM.WorkingDictionary.Count > 0; |
| 209 | _quickFixButton.Enabled = _mainWindowPM.CanProcessLexicon; |
| 210 | } |
| 211 | |
| 212 | private void OnSaveClick(object sender, EventArgs e) |
| 213 | {
|
| 214 | _sfmEditorView.UpdateModel(); |
| 215 | _mainWindowPM.DictionaryAndSettingsSave(); |
| 216 | _saveButton.Enabled = false; |
| 217 | } |
| 218 | |
| 219 | private void OnWordFound(object sender, SearchViewModel.SearchResultEventArgs e) |
| 220 | {
|
| 221 | _mainWindowPM.FilterChooserModel.ActiveRecordFilter = e.SearchResult.Filter; |
| 222 | _mainWindowPM.NavigatorModel.CurrentRecordIndex = e.SearchResult.RecordIndex; |
| 223 | _recordNavigatorView.UpdateDisplay(); |
| 224 | _sfmEditorView.Highlight(e.SearchResult.TextIndex, e.SearchResult.ResultLength); |
| 225 | } |
| 226 | |
| 227 | private void OnAboutBoxButton_Click(object sender, EventArgs e) |
| 228 | {
|
| 229 | AboutBox box = new AboutBox(); |
| 230 | box.ShowDialog(); |
| 231 | box.Dispose(); |
| 232 | } |
| 233 | |
| 234 | private void MainWindowView_FormClosing(object sender, FormClosingEventArgs e) |
| 235 | {
|
| 236 | |
| 237 | if(_saveButton.Enabled)//HACK this is so stupid to use the ui as the dirty bit, but it's all over |
| 238 | {
|
| 239 | var answer = MessageBox.Show("Save changes before quitting?", "SOLID: Save first?", MessageBoxButtons.YesNoCancel,
|
| 240 | MessageBoxIcon.Question); |
| 241 | switch (answer) |
| 242 | {
|
| 243 | case System.Windows.Forms.DialogResult.Cancel: |
| 244 | e.Cancel = true; |
| 245 | break; |
| 246 | case System.Windows.Forms.DialogResult.Yes: |
| 247 | OnSaveClick(this,null); |
| 248 | break; |
| 249 | case System.Windows.Forms.DialogResult.No: |
| 250 | break; |
| 251 | |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | private void OnChangeTemplate_Click(object sender, EventArgs e) |
| 257 | {
|
| 258 | string path = RequestTemplatePath(_mainWindowPM.PathToCurrentDictionary, true); |
| 259 | if(!String.IsNullOrEmpty(path)) |
| 260 | {
|
| 261 | _mainWindowPM.UseSolidSettingsTemplate(path); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private string RequestTemplatePath(string dictionaryPath, bool wouldBeReplacingExistingSettings) |
| 266 | {
|
| 267 | TemplateChooser chooser = new TemplateChooser(_mainWindowPM.Settings); |
| 268 | chooser.CustomizedSolidDestinationName = Path.GetFileName(SolidSettings.GetSettingsFilePathFromDictionaryPath(dictionaryPath)); |
| 269 | chooser.TemplatePaths = _mainWindowPM.TemplatePaths; |
| 270 | chooser.WouldBeReplacingExistingSettings = wouldBeReplacingExistingSettings; |
| 271 | chooser.ShowDialog(); |
| 272 | if (chooser.DialogResult == DialogResult.OK && chooser.PathToChosenTemplate != _mainWindowPM.PathToCurrentSolidSettingsFile) |
| 273 | {
|
| 274 | return chooser.PathToChosenTemplate; |
| 275 | } |
| 276 | return null; |
| 277 | } |
| 278 | |
| 279 | private void OnSearchClick(object sender, EventArgs e) |
| 280 | {
|
| 281 | _searchView = SearchView.CreateSearchView(_mainWindowPM.NavigatorModel, _sfmEditorView); |
| 282 | _searchView.TopMost = true; |
| 283 | _searchView.SearchModel = _mainWindowPM.SearchModel; |
| 284 | _searchView.Show(); |
| 285 | _searchView.Focus(); |
| 286 | } |
| 287 | |
| 288 | private void MainWindowView_KeyDown(object sender, KeyEventArgs e) |
| 289 | {
|
| 290 | |
| 291 | } |
| 292 | |
| 293 | private void MainWindowView_KeyUp(object sender, KeyEventArgs e) |
| 294 | {
|
| 295 | if (e.Control == true && e.KeyCode == Keys.F) |
| 296 | {
|
| 297 | OnSearchClick(this, new EventArgs()); |
| 298 | } |
| 299 | if (e.Control == true && e.KeyCode == Keys.O) |
| 300 | {
|
| 301 | OnOpenClick(this, new EventArgs()); |
| 302 | } |
| 303 | if (e.Control == true && e.KeyCode == Keys.S) |
| 304 | {
|
| 305 | if (_saveButton.Enabled) |
| 306 | {
|
| 307 | OnSaveClick(this, new EventArgs()); |
| 308 | } |
| 309 | } |
| 310 | if (e.Control == true && e.KeyCode == Keys.R) |
| 311 | {
|
| 312 | OnRecheckButtonClick(this, new EventArgs()); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | public string ExportFilterString() |
| 317 | {
|
| 318 | StringBuilder builder = new StringBuilder(); |
| 319 | ExportFactory f = ExportFactory.Singleton(); |
| 320 | foreach (ExportHeader header in f.ExportSettings) |
| 321 | {
|
| 322 | if (builder.Length > 0) |
| 323 | {
|
| 324 | builder.Append("|");
|
| 325 | } |
| 326 | builder.Append(header.FileNameFilter); |
| 327 | } |
| 328 | return builder.ToString(); |
| 329 | } |
| 330 | |
| 331 | private void OnExportButton_Click(object sender, EventArgs e) |
| 332 | {
|
| 333 | SaveFileDialog saveDialog = new SaveFileDialog(); |
| 334 | saveDialog.Title = "Export As"; |
| 335 | saveDialog.AddExtension = true; |
| 336 | saveDialog.Filter = ExportFilterString(); |
| 337 | saveDialog.FileName = Path.GetFileNameWithoutExtension(_mainWindowPM.DictionaryRealFilePath); |
| 338 | saveDialog.FilterIndex = _filterIndex; |
| 339 | if (DialogResult.OK != saveDialog.ShowDialog(this)) |
| 340 | {
|
| 341 | return; |
| 342 | } |
| 343 | _filterIndex = saveDialog.FilterIndex; |
| 344 | var destinationFilePath = saveDialog.FileName; |
| 345 | |
| 346 | try |
| 347 | {
|
| 348 | |
| 349 | _mainWindowPM.Export(saveDialog.FilterIndex - 1, destinationFilePath); |
| 350 | } |
| 351 | catch (Exception exception) |
| 352 | {
|
| 353 | string message = exception.Message; |
| 354 | if (exception.InnerException != null) |
| 355 | {
|
| 356 | message += exception.InnerException.Message; |
| 357 | } |
| 358 | MessageBox.Show(this, message, "Solid Export Error"); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | private void OnEditMarkerPropertiesClick(object sender, EventArgs e) |
| 363 | {
|
| 364 | _markerDetails.OpenSettingsDialog(null); |
| 365 | } |
| 366 | |
| 367 | private void OnQuickFix(object sender, EventArgs e) |
| 368 | {
|
| 369 | QuickFixer fixer = new QuickFixer(_mainWindowPM.WorkingDictionary); |
| 370 | var dlg = new QuickFixForm(fixer); |
| 371 | if (dlg.ShowDialog() != DialogResult.OK) |
| 372 | {
|
| 373 | return; |
| 374 | } |
| 375 | _mainWindowPM.ProcessLexicon(); |
| 376 | _sfmEditorView.Reload(); |
| 377 | _saveButton.Enabled = true; |
| 378 | |
| 379 | } |
| 380 | } |
| 381 | } |