From 87e00f641fcf6d9e956494993e2a78ad1cff8e49 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Mon, 4 Jul 2011 10:23:34 -0300 Subject: [Windows] Actually use native open file dialogs. --- .../addins/WindowsPlatform/CustomOpenFileDialog.cs | 232 +++++++++++++++++++++ .../src/addins/WindowsPlatform/EncodingComboBox.cs | 108 ++++++++++ .../WindowsPlatform/OpenFileDialogHandler.cs | 71 +++++++ .../WindowsPlatform/WindowsPlatform.addin.xml | 1 + .../addins/WindowsPlatform/WindowsPlatform.csproj | 3 + 5 files changed, 415 insertions(+) create mode 100644 main/src/addins/WindowsPlatform/CustomOpenFileDialog.cs create mode 100644 main/src/addins/WindowsPlatform/EncodingComboBox.cs create mode 100644 main/src/addins/WindowsPlatform/OpenFileDialogHandler.cs (limited to 'main/src/addins/WindowsPlatform') diff --git a/main/src/addins/WindowsPlatform/CustomOpenFileDialog.cs b/main/src/addins/WindowsPlatform/CustomOpenFileDialog.cs new file mode 100644 index 0000000000..61c021952c --- /dev/null +++ b/main/src/addins/WindowsPlatform/CustomOpenFileDialog.cs @@ -0,0 +1,232 @@ +// +// CustomOpenFileDialog.cs +// +// Author: +// Carlos Alberto Cortez +// +// Copyright (c) 2011 Novell, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.IO; +using System.Text; +using System.Windows.Forms; +using CustomControls.Controls; +using MonoDevelop.Ide; +using MonoDevelop.Ide.Gui; +using MonoDevelop.Ide.Extensions; +using MonoDevelop.Platform; +using MonoDevelop.Core; + +namespace MonoDevelop.Platform +{ + public class CustomOpenFileDialog : OpenFileDialogEx + { + Label encodingLabel; + Label viewerLabel; + EncodingBox encodingBox; + ComboBox viewerBox; + CheckBox closeSolutionBox; + + const string EncodingText = "Encoding:"; + const string ViewerText = "Open with:"; + + const int DialogWidth = 460; // predefined/desired width + + public CustomOpenFileDialog (FileDialog dialog, OpenFileDialogData data) + : base (dialog) + { + Initialize (data); + + StartLocation = AddonWindowLocation.Bottom; + } + + void Initialize (OpenFileDialogData data) + { + SuspendLayout (); + + Point location = new Point (10, 5); // start location for controls. + int padding = 5; + int height = padding * 2; // initial/minimum height. + + int labelWidth = GetMaxLabelWidth (data.ShowEncodingSelector, data.ShowViewerSelector); + + if (data.ShowEncodingSelector) { + encodingLabel = new Label () { + Text = GettextCatalog.GetString (EncodingText), + Location = location, + AutoSize = true + }; + + encodingBox = new EncodingBox (data.Action != Gtk.FileChooserAction.Save) { + Location = new Point (labelWidth + 20, location.Y), + Width = DialogWidth - (labelWidth + 20 + padding), + SelectedEncodingId = data.Encoding, + Enabled = false + }; + + Controls.AddRange (new Control [] { encodingLabel, encodingBox }); + + location.Y = encodingLabel.Bottom + padding; + height += encodingBox.Height + padding; + } + + if (data.ShowViewerSelector && FileDialog is OpenFileDialog) { + viewerLabel = new Label () { + Text = GettextCatalog.GetString (ViewerText), + Location = location, + AutoSize = true + }; + + viewerBox = new ComboBox () { + Location = new Point (labelWidth + 20, location.Y), + Width = DialogWidth - (labelWidth + 20 + padding), + DropDownStyle = ComboBoxStyle.DropDownList, + Enabled = false + }; + + location.Y = viewerBox.Bottom + padding; + height += viewerBox.Height + padding; + + if (IdeApp.Workspace.IsOpen) { + closeSolutionBox = new CheckBox () { + Text = GettextCatalog.GetString ("Close current workspace"), + Location = location, + AutoSize = true, + Checked = true, + Enabled = false + }; + + height += closeSolutionBox.Height + padding; + } + + if (encodingBox != null) { + viewerBox.SelectedIndexChanged += delegate { + int idx = viewerBox.SelectedIndex; + encodingBox.Enabled = !(idx == 0 && currentViewers [0] == null); + }; + } + + Controls.AddRange (new Control [] { viewerLabel, viewerBox }); + if (closeSolutionBox != null) + Controls.Add (closeSolutionBox); + } + + AutoScaleDimensions = new SizeF (6F, 13F); + AutoScaleMode = AutoScaleMode.Font; + Size = new Size (DialogWidth, height); + + ResumeLayout (); + } + + public string SelectedEncodingId { + get { + return encodingBox == null ? null : encodingBox.ToString (); + } + } + + public FileViewer SelectedViewer { + get { + if (viewerBox == null || currentViewers.Count == 0) + return null; + + return currentViewers [viewerBox.SelectedIndex]; + } + } + + public bool CloseCurrentWorkspace { + get { + return closeSolutionBox == null ? true : closeSolutionBox.Checked; + } + } + + List currentViewers = new List (); + + public override void OnFileNameChanged (string fileName) + { + base.OnFileNameChanged (fileName); + + bool slnViewerSelected = false; // whether the selected file is a project/solution file + + if (viewerBox != null) { + FillViewers (currentViewers, viewerBox, fileName); + if (currentViewers.Count > 0 && currentViewers [0] == null) + slnViewerSelected = true; + + if (closeSolutionBox != null) + closeSolutionBox.Enabled = slnViewerSelected; + } + + if (encodingBox != null) + encodingBox.Enabled = !slnViewerSelected; + } + + // Sort of ported from the MacSupport addin + static void FillViewers (List currentViewers, ComboBox viewerBox, string fileName) + { + currentViewers.Clear (); + viewerBox.Items.Clear (); + + if (fileName == null || fileName.Length == 0) { + viewerBox.Enabled = false; + return; + } + + if (Directory.Exists (fileName)) + return; + + var projectService = IdeApp.Services.ProjectService; + if (projectService.IsWorkspaceItemFile (fileName) || projectService.IsSolutionItemFile (fileName)) { + viewerBox.Items.Add (GettextCatalog.GetString ("Solution Workbench")); + currentViewers.Add (null); + } + + foreach (var vw in DisplayBindingService.GetFileViewers (fileName, null)) { + if (!vw.IsExternal) { + viewerBox.Items.Add (vw.Title); + currentViewers.Add (vw); + } + } + + viewerBox.Enabled = currentViewers.Count > 1; + viewerBox.SelectedIndex = 0; + } + + int GetMaxLabelWidth (bool showEncoding, bool showViewer) + { + if (!showEncoding && !showViewer) + return 0; + + Graphics g = CreateGraphics (); + int encodingWidth = 0; + int viewerWidth = 0; + + if (showEncoding) + encodingWidth = (int)g.MeasureString (GettextCatalog.GetString (EncodingText), Font).Width; + + if (showViewer) + viewerWidth = (int)g.MeasureString (GettextCatalog.GetString (ViewerText), Font).Width; + + return Math.Max (encodingWidth, viewerWidth); + } + } +} diff --git a/main/src/addins/WindowsPlatform/EncodingComboBox.cs b/main/src/addins/WindowsPlatform/EncodingComboBox.cs new file mode 100644 index 0000000000..b90882d816 --- /dev/null +++ b/main/src/addins/WindowsPlatform/EncodingComboBox.cs @@ -0,0 +1,108 @@ +// +// EncodingComboBox.cs +// +// Author: +// Carlos Alberto Cortez +// +// Copyright (c) 2011 Novell, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.Data; +using System.Text; +using System.Windows.Forms; +using CustomControls.Controls; +using MonoDevelop.Ide.Extensions; +using MonoDevelop.Platform; +using MonoDevelop.Core; +using MonoDevelop.Projects.Text; + +namespace MonoDevelop.Platform +{ + public class EncodingBox : ComboBox + { + TextEncoding [] encodings; + bool showAutoDetected; + + public EncodingBox (bool showAutoDetected) + { + this.showAutoDetected = showAutoDetected; + + DropDownStyle = ComboBoxStyle.DropDownList; + + Populate (false); + SelectedEncodingId = null; + } + + public string SelectedEncodingId { + get { + int selectedIndex = SelectedIndex; + if (selectedIndex < 0 || (showAutoDetected && selectedIndex == 0)) + return null; + + return encodings [showAutoDetected ? selectedIndex - 1 : selectedIndex].Id; + } + set { + if (!String.IsNullOrEmpty (value)) + for (int i = 0; i < encodings.Length; i++) + if (encodings [i].Id == value) { + SelectedIndex = showAutoDetected ? i + 1 : i; + return; + } + + SelectedIndex = 0; + } + } + + void Populate (bool clear) + { + encodings = TextEncoding.ConversionEncodings; + if (encodings == null || encodings.Length == 0) + encodings = new TextEncoding [] { TextEncoding.GetEncoding (TextEncoding.DefaultEncoding) }; + + BeginUpdate (); + + if (clear) + Items.Clear (); + + if (showAutoDetected) + Items.Add (GettextCatalog.GetString ("Auto Detected")); + + foreach (var encoding in TextEncoding.ConversionEncodings) + Items.Add (String.Format ("{0} ({1})", encoding.Name, encoding.Id)); + + Items.Add (GettextCatalog.GetString ("Add or Remove...")); + + EndUpdate (); + } + + protected override void OnSelectedIndexChanged (EventArgs e) + { + base.OnSelectedIndexChanged (e); + + // TODO - Implement support for the add/remove encodings panel + if (Items.Count > 0 && SelectedIndex == Items.Count -1) + SelectedIndex = 0; + } + } +} + diff --git a/main/src/addins/WindowsPlatform/OpenFileDialogHandler.cs b/main/src/addins/WindowsPlatform/OpenFileDialogHandler.cs new file mode 100644 index 0000000000..6c6ebf85c2 --- /dev/null +++ b/main/src/addins/WindowsPlatform/OpenFileDialogHandler.cs @@ -0,0 +1,71 @@ +// +// OpenFileDialogHandler.cs +// +// Author: +// Carlos Alberto Cortez +// +// Copyright (c) 2011 Novell, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Windows.Forms; +using CustomControls.Controls; +using MonoDevelop.Core; +using MonoDevelop.Ide.Extensions; +using MonoDevelop.Platform; + +namespace MonoDevelop.Platform +{ + public class OpenFileDialogHandler : IOpenFileDialogHandler + { + public bool Run (OpenFileDialogData data) + { + FileDialog fileDlg = null; + if (data.Action == Gtk.FileChooserAction.Open) + fileDlg = new OpenFileDialog (); + else + fileDlg = new SaveFileDialog (); + + var dlg = new CustomOpenFileDialog (fileDlg, data); + + SelectFileDialogHandler.SetCommonFormProperties (data, dlg.FileDialog); + + using (dlg) { + WinFormsRoot root = new WinFormsRoot (); + if (dlg.ShowDialog (root) == DialogResult.Cancel) + return false; + + FilePath[] paths = new FilePath [fileDlg.FileNames.Length]; + for (int n = 0; n < fileDlg.FileNames.Length; n++) + paths [n] = fileDlg.FileNames [n]; + data.SelectedFiles = paths; + + if (dlg.SelectedEncodingId != null) + data.Encoding = dlg.SelectedEncodingId; + if (dlg.SelectedViewer != null) { + data.SelectedViewer = dlg.SelectedViewer; + data.CloseCurrentWorkspace = dlg.CloseCurrentWorkspace; + } + } + + return true; + } + } +} + diff --git a/main/src/addins/WindowsPlatform/WindowsPlatform.addin.xml b/main/src/addins/WindowsPlatform/WindowsPlatform.addin.xml index a959563d10..d48f32ae2b 100644 --- a/main/src/addins/WindowsPlatform/WindowsPlatform.addin.xml +++ b/main/src/addins/WindowsPlatform/WindowsPlatform.addin.xml @@ -28,6 +28,7 @@ diff --git a/main/src/addins/WindowsPlatform/WindowsPlatform.csproj b/main/src/addins/WindowsPlatform/WindowsPlatform.csproj index d07b6693e3..91dc57eaef 100644 --- a/main/src/addins/WindowsPlatform/WindowsPlatform.csproj +++ b/main/src/addins/WindowsPlatform/WindowsPlatform.csproj @@ -85,6 +85,9 @@ + + + -- cgit v1.2.3