[ease] [general] Extendable and Document open/save dialogs



commit e2c0212b35658138b04a46d1e976df0f00f8558d
Author: Nate Stedman <natesm gmail com>
Date:   Sun Aug 8 20:35:55 2010 -0400

    [general] Extendable and Document open/save dialogs
    
    save_ext and open_ext Dialog functions allow the caller
    to provide a closure (or a function, if you want) to
    make changes to the FileChooserDialog before it is
    displayed.
    
    save_document and open_document use this functionality
    to add file filters.

 ease-core/ease-dialogs.vala   |   96 +++++++++++++++++++++++++++++++++++++++--
 ease/ease-editor-window.vala  |    4 +-
 ease/ease-welcome-window.vala |    2 +-
 3 files changed, 95 insertions(+), 7 deletions(-)
---
diff --git a/ease-core/ease-dialogs.vala b/ease-core/ease-dialogs.vala
index e344647..d5b09de 100644
--- a/ease-core/ease-dialogs.vala
+++ b/ease-core/ease-dialogs.vala
@@ -22,13 +22,29 @@ namespace Ease.Dialog
 {
 	/**
 	 * Displays an "Open" dialog with the specified title. Returns null if
-	 * cancelled, otherwise returns the selected path
+	 * cancelled, otherwise returns the selected path.
 	 *
 	 * @param title The dialog's title.
 	 * @param modal The window that the dialog should be modal for.
 	 */
 	public string? open(string title, Gtk.Window? modal)
 	{
+		return open_ext(title, modal, null);
+	}
+	
+	/**
+	 * Displays an "Open" dialog with the specified title. The
+	 * { link FileChooserDialogExtension} can be used to modify the
+	 * dialog before it is displayed. Returns null if cancelled, otherwise
+	 * returns the selected path.
+	 *
+	 * @param title The dialog's title.
+	 * @param modal The window that the dialog should be modal for.
+	 * @param ext A function to modify the dialog before it is displayed.
+	 */
+	public string? open_ext(string title, Gtk.Window? modal,
+	                        FileChooserDialogExtension? ext)
+	{
 		var dialog = new Gtk.FileChooserDialog(title,
 			                                   modal,
 			                                   Gtk.FileChooserAction.OPEN,
@@ -36,6 +52,7 @@ namespace Ease.Dialog
 			                                   Gtk.ResponseType.CANCEL,
 			                                   "gtk-open",
 			                                   Gtk.ResponseType.ACCEPT);
+		if (ext != null) ext(dialog);
 
 		if (dialog.run() == Gtk.ResponseType.ACCEPT)
 		{
@@ -46,16 +63,55 @@ namespace Ease.Dialog
 		dialog.destroy();
 		return null;
 	}
-
+	
 	/**
-	 * Creates and runs a "save" dialog with the given title. Returns null if
-	 * cancelled, otherwise returns the selected path
+	 * Displays an "Open" dialog for an Ease { link Document}. Returns null if
+	 * cancelled, otherwise returns the selected path.
+	 *
+	 * @param modal The window that the dialog should be modal for.
+	 */
+	public string? open_document(Gtk.Window? modal)
+	{
+		return open_ext(_("Open Document"), modal, (dialog) => {
+			// add a filter for ease documents
+			var filter = new Gtk.FileFilter();
+			filter.add_pattern("*.ease");
+			filter.set_name(_("Ease Presentations"));
+			dialog.add_filter(filter);
+			
+			// add a filter for all files
+			filter = new Gtk.FileFilter();
+			filter.set_name(_("All Files"));
+			filter.add_pattern("*");
+			dialog.add_filter(filter);
+		});
+	}
+	
+	/**
+	 * Displays an "Save" dialog with the specified title. Returns null if
+	 * cancelled, otherwise returns the selected path.
 	 *
 	 * @param title The dialog's title.
 	 * @param modal The window that the dialog should be modal for.
 	 */
 	public string? save(string title, Gtk.Window? modal)
 	{
+		return save_ext(title, modal, null);
+	}
+
+	/**
+	 * Displays an "Save" dialog with the specified title. The
+	 * { link FileChooserDialogExtension} can be used to modify the
+	 * dialog before it is displayed. Returns null if cancelled, otherwise
+	 * returns the selected path.
+	 *
+	 * @param title The dialog's title.
+	 * @param modal The window that the dialog should be modal for.
+	 * @param ext A function to modify the dialog before it is displayed.
+	 */
+	public string? save_ext(string title, Gtk.Window? modal,
+	                        FileChooserDialogExtension? ext)
+	{
 		var dialog = new Gtk.FileChooserDialog(title,
 			                                   modal,
 			                                   Gtk.FileChooserAction.SAVE,
@@ -64,6 +120,7 @@ namespace Ease.Dialog
 			                                   "gtk-cancel",
 			                                   Gtk.ResponseType.CANCEL,
 			                                   null);
+		if (ext != null) ext(dialog);
 		
 		if (dialog.run() == Gtk.ResponseType.ACCEPT)
 		{
@@ -75,4 +132,35 @@ namespace Ease.Dialog
 		dialog.destroy();
 		return null;
 	}
+	
+	/**
+	 * Displays an "Save" dialog for an Ease { link Document}. Returns null if
+	 * cancelled, otherwise returns the selected path. The title parameter
+	 * is provided to differentiate between "Save", "Save as", etc.
+	 *
+	 * @param title The dialog's title.
+	 * @param modal The window that the dialog should be modal for.
+	 */
+	public string? save_document(string title, Gtk.Window? modal)
+	{
+		return save_ext(title, modal, (dialog) => {
+			// add a filter for ease documents
+			var filter = new Gtk.FileFilter();
+			filter.add_pattern("*.ease");
+			filter.set_name(_("Ease Presentations"));
+			dialog.add_filter(filter);
+			
+			// add a filter for all files
+			filter = new Gtk.FileFilter();
+			filter.set_name(_("All Files"));
+			filter.add_pattern("*");
+			dialog.add_filter(filter);
+		});
+	}
+	
+	/**
+	 * Allows a caller to manipulate a dialog before is is displayed.
+	 */
+	public delegate void FileChooserDialogExtension(Gtk.FileChooserDialog d);
 }
+
diff --git a/ease/ease-editor-window.vala b/ease/ease-editor-window.vala
index 9d7ec96..7b7faed 100644
--- a/ease/ease-editor-window.vala
+++ b/ease/ease-editor-window.vala
@@ -315,7 +315,7 @@ internal class Ease.EditorWindow : Gtk.Window
 	[CCode (instance_pos = -1)]
 	internal void on_open(Gtk.Widget sender)
 	{
-		var filename = Dialog.open(_("Open Document"), this);
+		var filename = Dialog.open_document(this);
 		if (filename != null) Main.open_file(filename);
 	}
 	
@@ -549,7 +549,7 @@ internal class Ease.EditorWindow : Gtk.Window
 	{
 		if (document.filename == null)
 		{
-			var filename = Dialog.save(_("Save Document"), this);
+			var filename = Dialog.save_document(_("Save Document"), this);
 			
 			if (filename != null)
 			{
diff --git a/ease/ease-welcome-window.vala b/ease/ease-welcome-window.vala
index 6787cfb..5e56400 100644
--- a/ease/ease-welcome-window.vala
+++ b/ease/ease-welcome-window.vala
@@ -269,7 +269,7 @@ internal class Ease.WelcomeWindow : Gtk.Window
 	[CCode (instance_pos = -1)]
 	internal void on_open_pres_button_clicked (Gtk.Widget sender)
 	{
-		var filename = Dialog.open(_("Open Document"), this);
+		var filename = Dialog.open_document(this);
 		if (filename != null) Main.open_file(filename);
 		hide();
 	}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]