Exemplo n.º 1
0
		/// <summary>
		/// Opens a file dialog with the given settings.
		/// </summary>
		public bool FileDialog(FileDialogDef def)
		{
			// decide which action to use
			gtk.FileChooserAction action = gtk.FileChooserAction.Save;
			string accept = "";
			if (def.Type == FileDialogType.Open)
			{
				action = gtk.FileChooserAction.Open;
				accept = "Open";
			}
			else if (def.Type == FileDialogType.SaveAs)
			{
				action = gtk.FileChooserAction.Save;
				accept = "Save";
			}
			
			var dialog = new gtk.FileChooserDialog(def.Title, 
							this.Toplevel as gtk.Window, action,
							"Cancel", gtk.ResponseType.Cancel,
                            accept, gtk.ResponseType.Accept);
			if (def.FileName != null)
				dialog.SetFilename(def.FileName);
			
			// assemble the filters
			foreach (var ext in def.Extensions)
			{
				var filter = new gtk.FileFilter();
				var pattern = "*." + ext;
				filter.AddPattern(pattern);
				filter.Name = def.GetDescription(ext) + " (" + pattern + ")";
				dialog.AddFilter(filter);
			}
			
			var ret = dialog.Run() == (int)gtk.ResponseType.Accept;
			def.FileName = dialog.Filename;
			dialog.Destroy();
			return ret;
		}
Exemplo n.º 2
0
		/// <summary>
		/// Creates a file dialog.
		/// </summary>
		public bool FileDialog(FileDialogDef dialogDef)
		{
			Microsoft.Win32.FileDialog dialog = null;

			// create the dialog
			if (dialogDef.Type == FileDialogType.Open)
				dialog = new Microsoft.Win32.OpenFileDialog();
			else if (dialogDef.Type == FileDialogType.SaveAs)
				dialog = new Microsoft.Win32.SaveFileDialog();

			// set the options from the def
			if (dialogDef.FileName != null)
				dialog.FileName = "Document"; // Default file name
			foreach (var ext in dialogDef.Extensions)
			{
				dialog.Filter = String.Format("{0} (.{1})|*.{1}", dialogDef.GetDescription(ext), ext);
			}
			if (dialogDef.Extensions.Count > 0)
				dialog.DefaultExt = "." + dialogDef.Extensions[0]; // Default file extension

			// Show the dialog
			Nullable<bool> result = dialog.ShowDialog();
			var success = result != null && (bool)result;
			if (success)
				dialogDef.FileName = dialog.FileName;
			return success;
		}