Loading ...
Sorry, an error occurred while loading the content.

Ocaml-GTK file selection: how do you get the chosen filename?

Expand Messages
  • Stalkern 2
    Hello to everybody I m working with Ocaml-GTK and I would like to grab a filename. To do so, I use the fully featured File Selection Widget. This gives
    Message 1 of 4 , Jan 16, 2003
    • 0 Attachment
      Hello to everybody
      I'm working with Ocaml-GTK and I would like to grab a filename.

      To do so, I use the fully featured File Selection Widget.
      This gives generally a modal window ("modal" means "blocking" for the
      graphical application, i.e. it catches the focus and does not let you use the
      rest).

      After the file selection itself, I have two buttons, one is "OK", and the
      other is "Cancel".

      So I define a simple printing function,
      let print_selected_fileOrDir the_selected_fileOrDir () =
      print_endline ("the_selected_fileOrDir is "^the_selected_fileOrDir);
      flush stdout;;

      and then I use it as a test callback for the "OK" button, writing:
      fs # ok_button # connect # clicked ~callback:(print_selected_fileOrDir fs #
      get_filename)

      but this always prints the base filename that the file selection started at,
      not my last grab.
      Now, how would I get my last grab?

      TIA
      Ernesto

      PS the lack of sources for ocaml-GTK is just *s c a r y*. However, "real"
      graphics can make the difference in the spread of Ocaml... beyond teaching
      purposes.
    • Stalkern 2
      ... SOLUTION FOUND : this is achieved by forcing the evaluation order. let print_selected_fileOrDir the_selected_fileOrDir = print_endline
      Message 2 of 4 , Jan 16, 2003
      • 0 Attachment
        Il Thursday 16 January 2003 10:13, Stalkern 2 ha scritto:
        > Hello to everybody
        > I'm working with Ocaml-GTK and I would like to grab a filename.
        >
        > To do so, I use the fully featured File Selection Widget.


        SOLUTION FOUND : this is achieved by forcing the evaluation order.


        let print_selected_fileOrDir the_selected_fileOrDir =
        print_endline ("the_selected_fileOrDir is "^the_selected_fileOrDir);
        flush stdout;;

        If I join the callback in the following way

        fs # ok_button # connect#clicked ~callback:(fun () ->
        let name = fs#get_filename
        in
        print_selected_fileOrDir name)


        things work.

        Ciao!
        Ernesto
        PS Evaluation order is the first-ranking problem in this list... :-)
        a popular version of it are the missing brackets.
        PS2 a little more ocaml-gtk code can be found at
        http://www.cis.upenn.edu/~bcpierce/unison/
      • Remi VANICAT
        ... well, it s not exactly the problem you seem to believe. when you do a : fs #ok_button #connect #clicked ~callback:(print_selected_fileOrDir fs
        Message 3 of 4 , Jan 16, 2003
        • 0 Attachment
          Stalkern 2 <stalkern2@...> writes:

          > Il Thursday 16 January 2003 10:13, Stalkern 2 ha scritto:
          >> Hello to everybody
          >> I'm working with Ocaml-GTK and I would like to grab a filename.
          >>
          >> To do so, I use the fully featured File Selection Widget.
          >
          >
          > SOLUTION FOUND : this is achieved by forcing the evaluation order.
          >
          >
          > let print_selected_fileOrDir the_selected_fileOrDir =
          > print_endline ("the_selected_fileOrDir is "^the_selected_fileOrDir);
          > flush stdout;;
          >
          > If I join the callback in the following way
          >
          > fs # ok_button # connect#clicked ~callback:(fun () ->
          > let name = fs#get_filename
          > in
          > print_selected_fileOrDir name)
          >
          >
          > things work.

          well, it's not exactly the problem you seem to believe.

          when you do a :
          fs #ok_button #connect #clicked
          ~callback:(print_selected_fileOrDir fs #get_filename)

          the call of the method #get_filename is done at the moment of the
          #connect, and not when the callback will be call.

          Why ? because you are doing a partial evaluation of
          (print_selected_fileOrDir fs #get_filename) : everything that could be
          evaluated is before the call of the method #connect

          if you do a :

          fs #ok_button #connect #clicked
          ~callback:(fun () -> print_selected_fileOrDir fs #get_filename ())

          then you are building a new function that will call the #get_filename
          method when it will be call, no partial evaluation there.


          --
          Rémi Vanicat
          vanicat@labri.u-bordeaux.fr
          http://dept-info.labri.u-bordeaux.fr/~vanicat
        • Stalkern 2
          ... Thanks Rémi! Ernesto
          Message 4 of 4 , Jan 16, 2003
          • 0 Attachment
            Il Thursday 16 January 2003 16:01, Remi VANICAT ha scritto:
            > well, it's not exactly the problem you seem to believe.

            Thanks Rémi!

            Ernesto
          Your message has been successfully submitted and would be delivered to recipients shortly.