Пример #1
0
        static string SwfFilter(FileDialogFilters filters)
        {
            var  builder = new StringBuilder();
            bool first   = true;

            foreach (var f in filters.Filters)
            {
                if (!first)
                {
                    builder.Append("|");
                }
                else
                {
                    first = false;
                }
                builder.Append(f.Name);
                builder.Append(" (");
                var exts = string.Join(";", f.Extensions.Select((x) => {
                    if (x.Contains("."))
                    {
                        return(x);
                    }
                    else
                    {
                        return("*." + x);
                    }
                }));
                builder.Append(exts).Append(")|").Append(exts);
            }
            builder.Append("|All files (*.*)|*.*");
            return(builder.ToString());
        }
Пример #2
0
 public static string Open(FileDialogFilters filters = null)
 {
     if (Platform.RunningOS == OS.Windows)
     {
         if (Win32.Win32OpenDialog(Win32.ConvertFilters(filters), null, out string result))
         {
             return(result);
         }
         else
         {
             return(null);
         }
     }
     else if (Platform.RunningOS == OS.Linux)
     {
         if (kdialog)
         {
             return(KDialogOpen(filters));
         }
         else if (parentWindow != IntPtr.Zero)
         {
             return(Gtk2.GtkOpen(parentWindow, filters));
         }
         else
         {
             return(Gtk3.GtkOpen(filters));
         }
     }
     else
     {
         //Mac
         throw new NotImplementedException();
     }
 }
Пример #3
0
        public static string ConvertFilters(FileDialogFilters filters)
        {
            if (filters == null)
            {
                return(null);
            }
            var  builder = new StringBuilder();
            bool first   = true;

            foreach (var f in filters.Filters)
            {
                if (!first)
                {
                    builder.Append(";");
                }
                first = false;
                builder.Append(string.Join(",", f.Extensions.Select((x) =>
                {
                    int index = x.IndexOf('.');
                    if (index != -1)
                    {
                        return(x.Substring(index + 1));
                    }
                    return(x);
                })));
            }
            return(builder.ToString());
        }
Пример #4
0
        public static string GtkSave(IntPtr parent, FileDialogFilters filters)
        {
            if (!gtk_init_check(IntPtr.Zero, IntPtr.Zero))
            {
                throw new Exception();
            }
            var dlg = gtk_file_chooser_dialog_new("Save File", IntPtr.Zero,
                                                  GTK_FILE_CHOOSER_ACTION_SAVE,
                                                  IntPtr.Zero);

            gtk_dialog_add_button(dlg, "_Cancel", GTK_RESPONSE_CANCEL);
            gtk_dialog_add_button(dlg, "_Accept", GTK_RESPONSE_ACCEPT);
            SetFilters(dlg, filters);
            gtk_widget_realize(dlg);
            XSetTransientForHint(gdk_x11_get_default_xdisplay(),
                                 gdk_x11_drawable_get_xid(gtk_widget_get_window(dlg)), parent);
            string result = null;

            if (gtk_dialog_run(dlg) == GTK_RESPONSE_ACCEPT)
            {
                result = UnsafeHelpers.PtrToStringUTF8(gtk_file_chooser_get_filename(dlg));
            }
            WaitCleanup();
            gtk_widget_destroy(dlg);
            WaitCleanup();
            return(result);
        }
Пример #5
0
        static string KDialogOpen(FileDialogFilters filters)
        {
            if (String.IsNullOrEmpty(lastOpen))
            {
                lastOpen = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            }
            var ret = KDialogProcess(string.Format("--getopenfilename \"{0}\" \"{1}\"", lastOpen, KDialogFilter(filters)));

            lastOpen = ret ?? lastOpen;
            return(ret);
        }
Пример #6
0
        static string KDialogSave(FileDialogFilters filters)
        {
            if (string.IsNullOrEmpty(lastSave))
            {
                lastSave = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            }
            var ret = KDialogProcess(string.Format("--getsavefilename \"{0}\" \"{1}\"", lastSave, KDialogFilter(filters)));

            lastSave = ret ?? lastSave;
            return(ret);
        }
Пример #7
0
 public static string Open(FileDialogFilters filters = null)
 {
     if (Platform.RunningOS == OS.Windows)
     {
         string result = null;
         using (var ofd = NewObj("System.Windows.Forms.OpenFileDialog"))
         {
             if (parentForm != null)
             {
                 ofd.Parent = parentForm;
             }
             if (filters != null)
             {
                 ofd.Filter = SwfFilter(filters);
             }
             if (ofd.ShowDialog() == SwfOk())
             {
                 result = ofd.FileName;
             }
         }
         WinformsDoEvents();
         return(result);
     }
     else if (Platform.RunningOS == OS.Linux)
     {
         if (kdialog)
         {
             return(KDialogOpen());
         }
         else if (parentWindow != IntPtr.Zero)
         {
             return(Gtk2.GtkOpen(parentWindow, filters));
         }
         else
         {
             return(Gtk3.GtkOpen(filters));
         }
     }
     else
     {
         //Mac
         throw new NotImplementedException();
     }
 }
Пример #8
0
        static void SetFilters(IntPtr dlg, FileDialogFilters filters)
        {
            if (filters == null)
            {
                return;
            }
            foreach (var managed in filters.Filters)
            {
                var f = gtk_file_filter_new();
                gtk_file_filter_set_name(f, managed.Name);
                foreach (var ext in managed.Extensions)
                {
                    gtk_file_filter_add_pattern(f, "*." + ext);
                }
                gtk_file_chooser_add_filter(dlg, f);
            }
            //Add wildcards
            var wc = gtk_file_filter_new();

            gtk_file_filter_set_name(wc, "*.*");
            gtk_file_filter_add_pattern(wc, "*");
            gtk_file_chooser_add_filter(dlg, wc);
        }
Пример #9
0
        public static string GtkSave(FileDialogFilters filters)
        {
            if (!gtk_init_check(IntPtr.Zero, IntPtr.Zero))
            {
                throw new Exception();
            }
            var dlg = gtk_file_chooser_dialog_new("Save File", IntPtr.Zero,
                                                  GTK_FILE_CHOOSER_ACTION_SAVE,
                                                  IntPtr.Zero);

            gtk_dialog_add_button(dlg, "_Cancel", GTK_RESPONSE_CANCEL);
            gtk_dialog_add_button(dlg, "_Accept", GTK_RESPONSE_ACCEPT);
            SetFilters(dlg, filters);
            string result = null;

            if (gtk_dialog_run_HACK(dlg) == GTK_RESPONSE_ACCEPT)
            {
                result = UnsafeHelpers.PtrToStringUTF8(gtk_file_chooser_get_filename(dlg));
            }
            WaitCleanup();
            gtk_widget_destroy(dlg);
            WaitCleanup();
            return(result);
        }