Exemplo n.º 1
0
 private static string GetAcceptString(GtkFileChooserAction type)
 {
     return(type switch
     {
         GtkFileChooserAction.Open => "_Open",
         GtkFileChooserAction.Save => "_Save",
         _ => "_Select",
     });
Exemplo n.º 2
0
 public static extern IntPtr CreateFileDialog(
     IntPtr title,
     IntPtr parent,
     GtkFileChooserAction action,
     IntPtr firstButtonText,
     GtkResponseType firstButtonResponse,
     IntPtr secondButtonText,
     GtkResponseType secondButtonResponse,
     IntPtr terminator);
Exemplo n.º 3
0
        private string GetAcceptString(GtkFileChooserAction type)
        {
            switch (type)
            {
            case GtkFileChooserAction.Open:
                return("_Open");

            case GtkFileChooserAction.Save:
                return("_Save");

            case GtkFileChooserAction.SelectFolder:
            default:
                return("_Select");
            }
        }
Exemplo n.º 4
0
        public unsafe static Task <string[]> ShowDialog(string title, GtkWindow parent, GtkFileChooserAction action,
                                                        bool multiselect, string initialFileName, Action <GtkFileChooser> modify)
        {
            GtkFileChooser dlg;

            parent = parent ?? GtkWindow.Null;
            using (var name = new Utf8Buffer(title))
                dlg = Native.GtkFileChooserDialogNew(name, parent, action, IntPtr.Zero);
            modify?.Invoke(dlg);
            if (multiselect)
            {
                Native.GtkFileChooserSetSelectMultiple(dlg, true);
            }

            Native.GtkWindowSetModal(dlg, true);
            var tcs = new TaskCompletionSource <string[]>();
            List <IDisposable> disposables = null;
            Action             dispose     = () =>
            {
                // ReSharper disable once PossibleNullReferenceException
                foreach (var d in disposables)
                {
                    d.Dispose();
                }
                disposables.Clear();
            };

            disposables = new List <IDisposable>
            {
                Signal.Connect <Native.D.signal_generic>(dlg, "close", delegate
                {
                    tcs.TrySetResult(null);
                    dispose();
                    return(false);
                }),
                Signal.Connect <Native.D.signal_dialog_response>(dlg, "response", (_, resp, __) =>
                {
                    string[] result = null;
                    if (resp == GtkResponseType.Accept)
                    {
                        var rlst = new List <string>();
                        var gs   = Native.GtkFileChooserGetFilenames(dlg);
                        var cgs  = gs;
                        while (cgs != null)
                        {
                            if (cgs->Data != IntPtr.Zero)
                            {
                                rlst.Add(Utf8Buffer.StringFromPtr(cgs->Data));
                            }
                            cgs = cgs->Next;
                        }

                        Native.GSlistFree(gs);
                        result = rlst.ToArray();
                    }

                    Native.GtkWidgetHide(dlg);
                    dispose();
                    tcs.TrySetResult(result);
                    return(false);
                }),
                dlg
            };
            using (var open = new Utf8Buffer("Open"))
                Native.GtkDialogAddButton(dlg, open, GtkResponseType.Accept);
            using (var open = new Utf8Buffer("Cancel"))
                Native.GtkDialogAddButton(dlg, open, GtkResponseType.Cancel);
            if (initialFileName != null)
            {
                using (var fn = new Utf8Buffer(initialFileName))
                    Native.GtkFileChooserSetFilename(dlg, fn);
            }
            Native.GtkWindowPresent(dlg);
            return(tcs.Task);
        }
Exemplo n.º 5
0
 static extern IntPtr gtk_file_chooser_dialog_new([MarshalAs(UnmanagedType.LPUTF8Str)] string title,
                                                  IntPtr parent, GtkFileChooserAction action,
                                                  [MarshalAs(UnmanagedType.LPUTF8Str)] string btn1, GtkResponseType resp1,
                                                  [MarshalAs(UnmanagedType.LPUTF8Str)] string btn2, GtkResponseType res2,
                                                  IntPtr terminatingNull);
Exemplo n.º 6
0
 public static extern IntPtr gtk_file_chooser_dialog_new(Utf8Buffer title, IntPtr parent,
                                                         GtkFileChooserAction action, IntPtr ignore);
Exemplo n.º 7
0
 public static extern IntPtr CreateNativeFileDialog(IntPtr title, IntPtr parent, GtkFileChooserAction action, IntPtr acceptLabel, IntPtr cancelLabel);
Exemplo n.º 8
0
        private unsafe Task <string[]> ShowDialog(string title, IWindowImpl parent, GtkFileChooserAction action,
                                                  bool multiSelect, string initialFileName, IEnumerable <FileDialogFilter> filters, string defaultExtension, bool overwritePrompt)
        {
            IntPtr dlg;

            using (var name = new Utf8Buffer(title))
                dlg = gtk_file_chooser_dialog_new(name, IntPtr.Zero, action, IntPtr.Zero);
            UpdateParent(dlg, parent);
            if (multiSelect)
            {
                gtk_file_chooser_set_select_multiple(dlg, true);
            }

            gtk_window_set_modal(dlg, true);
            var tcs = new TaskCompletionSource <string[]>();
            List <IDisposable> disposables = null;

            void Dispose()
            {
                // ReSharper disable once PossibleNullReferenceException
                foreach (var d in disposables)
                {
                    d.Dispose();
                }
                disposables.Clear();
            }

            var filtersDic = new Dictionary <IntPtr, FileDialogFilter>();

            if (filters != null)
            {
                foreach (var f in filters)
                {
                    var filter = gtk_file_filter_new();
                    filtersDic[filter] = f;
                    using (var b = new Utf8Buffer(f.Name))
                        gtk_file_filter_set_name(filter, b);

                    foreach (var e in f.Extensions)
                    {
                        using (var b = new Utf8Buffer("*." + e))
                            gtk_file_filter_add_pattern(filter, b);
                    }

                    gtk_file_chooser_add_filter(dlg, filter);
                }
            }

            disposables = new List <IDisposable>
            {
                ConnectSignal <signal_generic>(dlg, "close", delegate
                {
                    tcs.TrySetResult(null);
                    Dispose();
                    return(false);
                }),
                ConnectSignal <signal_dialog_response>(dlg, "response", (_, resp, __) =>
                {
                    string[] result = null;
                    if (resp == GtkResponseType.Accept)
                    {
                        var resultList = new List <string>();
                        var gs         = gtk_file_chooser_get_filenames(dlg);
                        var cgs        = gs;
                        while (cgs != null)
                        {
                            if (cgs->Data != IntPtr.Zero)
                            {
                                resultList.Add(Utf8Buffer.StringFromPtr(cgs->Data));
                            }
                            cgs = cgs->Next;
                        }
                        g_slist_free(gs);
                        result = resultList.ToArray();

                        // GTK doesn't auto-append the extension, so we need to do that manually
                        if (action == GtkFileChooserAction.Save)
                        {
                            var currentFilter = gtk_file_chooser_get_filter(dlg);
                            filtersDic.TryGetValue(currentFilter, out var selectedFilter);
                            for (var c = 0; c < result.Length; c++)
                            {
                                result[c] = NameWithExtension(result[c], defaultExtension, selectedFilter);
                            }
                        }
                    }

                    gtk_widget_hide(dlg);
                    Dispose();
                    tcs.TrySetResult(result);
                    return(false);
                })
            };
            using (var open = new Utf8Buffer(
                       action == GtkFileChooserAction.Save ? "Save"
                : action == GtkFileChooserAction.SelectFolder ? "Select"
                : "Open"))
                gtk_dialog_add_button(dlg, open, GtkResponseType.Accept);
            using (var open = new Utf8Buffer("Cancel"))
                gtk_dialog_add_button(dlg, open, GtkResponseType.Cancel);
            if (initialFileName != null)
            {
                using (var fn = new Utf8Buffer(initialFileName))
                {
                    if (action == GtkFileChooserAction.Save)
                    {
                        gtk_file_chooser_set_current_name(dlg, fn);
                    }
                    else
                    {
                        gtk_file_chooser_set_filename(dlg, fn);
                    }
                }
            }

            gtk_file_chooser_set_do_overwrite_confirmation(dlg, overwritePrompt);

            gtk_window_present(dlg);
            return(tcs.Task);
        }

        string NameWithExtension(string path, string defaultExtension, FileDialogFilter filter)
        {
            var name = Path.GetFileName(path);

            if (name != null && !name.Contains("."))
            {
                if (filter?.Extensions?.Count > 0)
                {
                    if (defaultExtension != null &&
                        filter.Extensions.Contains(defaultExtension))
                    {
                        return(path + "." + defaultExtension.TrimStart('.'));
                    }

                    var ext = filter.Extensions.FirstOrDefault(x => x != "*");
                    if (ext != null)
                    {
                        return(path + "." + ext.TrimStart('.'));
                    }
                }

                if (defaultExtension != null)
                {
                    path += "." + defaultExtension.TrimStart('.');
                }
            }

            return(path);
        }
Exemplo n.º 9
0
        private unsafe Task <string[]> ShowDialog(string title, IWindowImpl parent, GtkFileChooserAction action,
                                                  bool multiSelect, string initialFileName, IEnumerable <FileDialogFilter> filters)
        {
            IntPtr dlg;

            using (var name = new Utf8Buffer(title))
                dlg = gtk_file_chooser_dialog_new(name, IntPtr.Zero, action, IntPtr.Zero);
            UpdateParent(dlg, parent);
            if (multiSelect)
            {
                gtk_file_chooser_set_select_multiple(dlg, true);
            }

            gtk_window_set_modal(dlg, true);
            var tcs = new TaskCompletionSource <string[]>();
            List <IDisposable> disposables = null;

            void Dispose()
            {
                // ReSharper disable once PossibleNullReferenceException
                foreach (var d in disposables)
                {
                    d.Dispose();
                }
                disposables.Clear();
            }

            if (filters != null)
            {
                foreach (var f in filters)
                {
                    var filter = gtk_file_filter_new();
                    using (var b = new Utf8Buffer(f.Name))
                        gtk_file_filter_set_name(filter, b);

                    foreach (var e in f.Extensions)
                    {
                        using (var b = new Utf8Buffer("*." + e))
                            gtk_file_filter_add_pattern(filter, b);
                    }

                    gtk_file_chooser_add_filter(dlg, filter);
                }
            }

            disposables = new List <IDisposable>
            {
                ConnectSignal <signal_generic>(dlg, "close", delegate
                {
                    tcs.TrySetResult(null);
                    Dispose();
                    return(false);
                }),
                ConnectSignal <signal_dialog_response>(dlg, "response", (_, resp, __) =>
                {
                    string[] result = null;
                    if (resp == GtkResponseType.Accept)
                    {
                        var resultList = new List <string>();
                        var gs         = gtk_file_chooser_get_filenames(dlg);
                        var cgs        = gs;
                        while (cgs != null)
                        {
                            if (cgs->Data != IntPtr.Zero)
                            {
                                resultList.Add(Utf8Buffer.StringFromPtr(cgs->Data));
                            }
                            cgs = cgs->Next;
                        }
                        g_slist_free(gs);
                        result = resultList.ToArray();
                    }

                    gtk_widget_hide(dlg);
                    Dispose();
                    tcs.TrySetResult(result);
                    return(false);
                })
            };
            using (var open = new Utf8Buffer(
                       action == GtkFileChooserAction.Save ? "Save"
                : action == GtkFileChooserAction.SelectFolder ? "Select"
                : "Open"))
                gtk_dialog_add_button(dlg, open, GtkResponseType.Accept);
            using (var open = new Utf8Buffer("Cancel"))
                gtk_dialog_add_button(dlg, open, GtkResponseType.Cancel);
            if (initialFileName != null)
            {
                using (var fn = new Utf8Buffer(initialFileName))
                {
                    if (action == GtkFileChooserAction.Save)
                    {
                        gtk_file_chooser_set_current_name(dlg, fn);
                    }
                    else
                    {
                        gtk_file_chooser_set_filename(dlg, fn);
                    }
                }
            }

            gtk_window_present(dlg);
            return(tcs.Task);
        }
Exemplo n.º 10
0
        public string OpenFileDialog(string caption, INativeWindow parentWindow)
        {
            IntPtr parent = IntPtr.Zero;
            //IntPtr display = parentWindow.WindowInfo.Handle;

            /***
             *          object display = KS.Foundation.ReflectionUtils.GetPropertyValue (parentWindow.WindowInfo, "Display");
             *          IntPtr disp_pointer = new IntPtr (display.SafeInt());
             *
             *          object RootWindow = KS.Foundation.ReflectionUtils.GetPropertyValue (parentWindow.WindowInfo, "FBConfig");
             *          IntPtr root_pointer = new IntPtr (RootWindow.SafeInt());
             *
             *          parent = root_pointer;
             ***/


            // ToDo: gtk_window_set_transient_for()

            // Unfortunately our GameWindow is not a GTK-Window :(


            //parent = parentWindow.WindowInfo.Handle

            //XSetTransientForHint

            //var info = (OpenTK.Platform.X11.X11WindowInfo)parentWindow.WindowInfo;

            //parent = parentWindow.WindowInfo.Handle;

            //OpenTK.Platform.Utilities.

            /***
             *          Utilities.CreateX11WindowInfo(IntPtr.Zero, 0,
             *                  parentWindow.WindowInfo.Handle,
             *                  IntPtr.Zero,
             *                  IntPtr.Zero
             *          );
             ***/
            //Utilities.CreateX11WindowInfo(
            //IntPtr GtkWindow = (this.GlWindow.WindowInfo as OpenTK.Platform.X11.).Visual;

            GtkFileChooserAction action = GtkFileChooserAction.GTK_FILE_CHOOSER_ACTION_OPEN;

            if (String.IsNullOrEmpty(caption))
            {
                caption = "Open File";
            }
            IntPtr title = Marshal.StringToHGlobalAnsi(caption);

            IntPtr dialog = IntPtr.Zero;
            IntPtr result = IntPtr.Zero;

            IntPtr button1   = Marshal.StringToHGlobalAnsi("_Cancel");
            int    response1 = (int)GtkResponseType.GTK_RESPONSE_CANCEL;

            IntPtr button2 = Marshal.StringToHGlobalAnsi("_Open");
            //IntPtr button2 = Marshal.StringToHGlobalAnsi("_Save");
            int response2 = (int)GtkResponseType.GTK_RESPONSE_ACCEPT;

            try {
                gtk_init(0, IntPtr.Zero);
                //gtk_init(0, parent);

                dialog = gtk_file_chooser_dialog_new(title, parent, (int)action,
                                                     button1,
                                                     response1,
                                                     button2,
                                                     response2,
                                                     IntPtr.Zero);

                //XReparentWindow(disp_pointer, dialog, parentWindow.WindowInfo.Handle, 0, 0);

                // set_transient_for()
                // gtk_window_set_modal()

                int ret = gtk_dialog_run(dialog);
                if (ret == (int)GtkResponseType.GTK_RESPONSE_ACCEPT)
                {
                    result = gtk_file_chooser_get_filename(dialog);
                }

                if (dialog != IntPtr.Zero)
                {
                    gtk_widget_destroy(dialog);
                    while (gtk_events_pending() != 0)
                    {
                        gtk_main_iteration();
                    }
                }
            } catch (Exception ex) {
                ex.LogError();
                return(String.Empty);
            } finally {
                Marshal.FreeHGlobal(title);
                Marshal.FreeHGlobal(button1);
                Marshal.FreeHGlobal(button2);
            }

            if (result == IntPtr.Zero)
            {
                return(String.Empty);
            }

            string filename = result.ToAnsiString();

            Marshal.FreeHGlobal(result);
            return(filename);
        }
Exemplo n.º 11
0
        private unsafe Task <string[]?> ShowDialog(string?title, IWindowImpl parent, GtkFileChooserAction action,
                                                   bool multiSelect, IStorageFolder?initialFolder, string?initialFileName,
                                                   IEnumerable <FilePickerFileType>?filters, string?defaultExtension, bool overwritePrompt)
        {
            IntPtr dlg;

            using (var name = new Utf8Buffer(title))
            {
                dlg = gtk_file_chooser_dialog_new(name, IntPtr.Zero, action, IntPtr.Zero);
            }

            UpdateParent(dlg, parent);
            if (multiSelect)
            {
                gtk_file_chooser_set_select_multiple(dlg, true);
            }

            gtk_window_set_modal(dlg, true);
            var tcs = new TaskCompletionSource <string[]?>();
            List <IDisposable>?disposables = null;

            void Dispose()
            {
                foreach (var d in disposables !)
                {
                    d.Dispose();
                }

                disposables.Clear();
            }

            var filtersDic = new Dictionary <IntPtr, FilePickerFileType>();

            if (filters != null)
            {
                foreach (var f in filters)
                {
                    if (f.Patterns?.Any() == true || f.MimeTypes?.Any() == true)
                    {
                        var filter = gtk_file_filter_new();
                        filtersDic[filter] = f;
                        using (var b = new Utf8Buffer(f.Name))
                        {
                            gtk_file_filter_set_name(filter, b);
                        }

                        if (f.Patterns is not null)
                        {
                            foreach (var e in f.Patterns)
                            {
                                using (var b = new Utf8Buffer(e))
                                {
                                    gtk_file_filter_add_pattern(filter, b);
                                }
                            }
                        }

                        if (f.MimeTypes is not null)
                        {
                            foreach (var e in f.MimeTypes)
                            {
                                using (var b = new Utf8Buffer(e))
                                {
                                    gtk_file_filter_add_mime_type(filter, b);
                                }
                            }
                        }

                        gtk_file_chooser_add_filter(dlg, filter);
                    }
                }
            }

            disposables = new List <IDisposable>
            {
                ConnectSignal <signal_generic>(dlg, "close", delegate
                {
                    tcs.TrySetResult(null);
                    Dispose();
                    return(false);
                }),
                ConnectSignal <signal_dialog_response>(dlg, "response", (_, resp, __) =>
                {
                    string[]? result = null;
                    if (resp == GtkResponseType.Accept)
                    {
                        var resultList = new List <string>();
                        var gs         = gtk_file_chooser_get_filenames(dlg);
                        var cgs        = gs;
                        while (cgs != null)
                        {
                            if (cgs->Data != IntPtr.Zero &&
                                Utf8Buffer.StringFromPtr(cgs->Data) is string str)
                            {
                                resultList.Add(str);
                            }
                            cgs = cgs->Next;
                        }
                        g_slist_free(gs);
                        result = resultList.ToArray();

                        // GTK doesn't auto-append the extension, so we need to do that manually
                        if (action == GtkFileChooserAction.Save)
                        {
                            var currentFilter = gtk_file_chooser_get_filter(dlg);
                            filtersDic.TryGetValue(currentFilter, out var selectedFilter);
                            for (var c = 0; c < result.Length; c++)
                            {
                                result[c] = StorageProviderHelpers.NameWithExtension(result[c], defaultExtension, selectedFilter);
                            }
                        }
                    }

                    gtk_widget_hide(dlg);
                    Dispose();
                    tcs.TrySetResult(result);
                    return(false);
                })
            };
            using (var open = new Utf8Buffer(
                       action == GtkFileChooserAction.Save ? "Save"
                : action == GtkFileChooserAction.SelectFolder ? "Select"
                : "Open"))
            {
                gtk_dialog_add_button(dlg, open, GtkResponseType.Accept);
            }

            using (var open = new Utf8Buffer("Cancel"))
            {
                gtk_dialog_add_button(dlg, open, GtkResponseType.Cancel);
            }

            Uri?folderPath = null;

            if (initialFolder?.TryGetUri(out folderPath) == true)
            {
                using var dir = new Utf8Buffer(folderPath.LocalPath);
                gtk_file_chooser_set_current_folder(dlg, dir);
            }

            if (initialFileName != null)
            {
                // gtk_file_chooser_set_filename() expects full path
                using var fn = action == GtkFileChooserAction.Open
                    ? new Utf8Buffer(Path.Combine(folderPath?.LocalPath ?? "", initialFileName))
                    : new Utf8Buffer(initialFileName);

                if (action == GtkFileChooserAction.Save)
                {
                    gtk_file_chooser_set_current_name(dlg, fn);
                }
                else
                {
                    gtk_file_chooser_set_filename(dlg, fn);
                }
            }

            gtk_file_chooser_set_do_overwrite_confirmation(dlg, overwritePrompt);

            gtk_window_present(dlg);
            return(tcs.Task);
        }