From 9852fe0d99c73bc825583bb447756075330e481e Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Mon, 7 Jan 2019 15:01:03 +0100
Subject: [PATCH] [gui] Add helper function for selecting a file

replaces GToolbox.select_file that no longer exists in lablgtk3
---
 src/plugins/gui/gtk_helper.ml  | 39 ++++++++++++++++++++++++++++++++++
 src/plugins/gui/gtk_helper.mli |  9 ++++++++
 2 files changed, 48 insertions(+)

diff --git a/src/plugins/gui/gtk_helper.ml b/src/plugins/gui/gtk_helper.ml
index 66d03ad7135..a2125f34f98 100644
--- a/src/plugins/gui/gtk_helper.ml
+++ b/src/plugins/gui/gtk_helper.ml
@@ -960,6 +960,45 @@ let source_files_chooser (main_ui: source_files_chooser_host) defaults f =
   dialog#show ();
   ()
 
+let default_dir = ref ""
+
+let select_file ?title ?(dir=default_dir) ?(filename="") () =
+  let filename =
+    if Filename.is_relative filename then
+      if !dir <> "" then !dir ^ "/" ^ filename
+      else ""
+    else begin
+      dir:= Filename.dirname filename;
+      filename
+    end
+  in
+  let dialog: GWindow.Buttons.file_selection GWindow.file_chooser_dialog =
+    GWindow.file_chooser_dialog
+      ~action:`OPEN
+      ?title
+      ~filename
+      ~modal:true
+      ()
+  in
+  let result = ref None in
+  let action r =
+    (match r with
+     | `OK ->
+       let file = dialog#filename in
+       (match file with
+        | None -> ()
+        | Some file ->
+          dir := Filename.dirname file;
+          result := Some file)
+     | _ -> ());
+    dialog#destroy ()
+  in
+  dialog#add_select_button "Open" `OK;
+  dialog#add_button "Cancel" `CANCEL;
+  dialog#show ();
+  action (dialog#run ());
+  !result
+
 let spawn_command ?(timeout=0) ?stdout ?stderr s args f =
   let check_result = Command.command_async s ?stdout ?stderr args in
   let has_timeout = timeout > 0 in
diff --git a/src/plugins/gui/gtk_helper.mli b/src/plugins/gui/gtk_helper.mli
index 1d99d79c245..60a28341d81 100644
--- a/src/plugins/gui/gtk_helper.mli
+++ b/src/plugins/gui/gtk_helper.mli
@@ -321,6 +321,15 @@ val source_files_chooser:
   (string list -> unit) ->
   unit
 
+(** Launches a standard gtk file chooser window and returns the name
+    of the selected file. Replaces GToolbox.select_file that has not been
+    ported to lablgtk3.
+
+    @since Frama-C+dev
+*)
+val select_file:
+  ?title:string -> ?dir:(string ref)-> ?filename:string -> unit -> string option
+
 (* ************************************************************************** *)
 (** {2 Miscellaneous} *)
 (* ************************************************************************** *)
-- 
GitLab