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

lablGTK question

Expand Messages
  • Jaques Bertrand
    Hello, I have a question concerning example in the lablgtk tutorial concerning the subject notebook , which provides tab view of text content. I am not able
    Message 1 of 3 , Apr 27, 2011
    • 0 Attachment
      Hello,

      I have a question concerning example in the lablgtk tutorial concerning the subject "notebook", which provides tab view of text content. I am not able to see the cause of the error. I kind of understand why it doesn't work, but I am not firm on it.

      The following example does not compile, the error message is

      Error: this expression has type GObject.widget -> int
      but an expression was expected of GObject.widget -> unit

      The line in question has an arrow next to it:

      for i = 1 to 5 do
      let text = "Append Frame" ^ string_of_int i in
      let label = GBin.frame ~label:text ~width:100 ~height:75 ~border_width:10
      --> ~packing:(notebook#append_page ~tab_label:label#coerce) () in
      let label = GMisc.label ~text ~packing:frame#add () in
      ()
      done;

      The iteration has the int appended as string_of_int in the "packing" routine, however, the definition of 'notebook' has coordinates for the position which are of the type int. I believe that this is the problem. But the method has unit () type at the end before the keyword 'done', so I am kind of lost there.

      Here's the entire text of the example. You can compile it with the line "ocamlc -I +lablgtk2 -o ntbk lablgtk.cma gtkInit.cmo -w -a notebook.ml".

      -----------------------------------------------------------------------------
      (* file: notebook.ml *)

      (* This function rotates the position of the tabs *)
      let rotate_book notebook () =
      notebook#set_tab_pos
      (match notebook#tab_pos with
      | `BOTTOM -> `LEFT
      | `LEFT -> `TOP
      | `TOP -> `RIGHT
      | `RIGHT -> `BOTTOM
      )

      (* Add/Remove the page tabs and the borders *)
      let tabsborder_book notebook () =
      notebook#set_show_tabs (not notebook#show_tabs);
      notebook#set_show_border (not notebook#show_border)

      (* Remove a page from the notebook *)
      let remove_book notebook () =
      notebook#remove_page notebook#current_page;
      ()

      let main () =
      let window = GWindow.window ~title:"Notebook demo" ~border_width:10 ()in
      window#connect#destroy ~callback:GMain.Main.quit;

      let table = GPack.table ~rows:3 ~columns:6 ~packing:window#add () in

      (* Create a new notebook, place the position of the tabs *)
      let notebook = GPack.notebook ~tab_pos:`TOP
      ~packing:(table#attach ~left:0 ~right:6 ~top:0) () in

      for i = 1 to 5 do
      let text = "Append Frame " ^ string_of_int i in
      let label = GMisc.label ~text:("Page " ^ string_of_int i) () in
      let frame = GBin.frame ~label:text ~width:100 ~height:75 ~border_width:10
      ~packing:(notebook#append_page ~tab_label:label#coerce) () in
      let label = GMisc.label ~text ~packing:frame#add () in
      ()
      done;

      (* Now let's add a page to a specified spot *)
      let label = GMisc.label ~text:"Add page" () in
      let checkbutton = GButton.check_button ~label:"Check me please!"
      ~packing:(notebook#insert_page ~tab_label:label#coerce ~pos:2) ()
      in
      checkbutton#misc#set_size_request ~width:100 ~height:75 ();

      (* Now finaly let's prepend pages to the notebook *)
      for i = 1 to 5 do
      let text = "Prepend Frame " ^ string_of_int i in
      let label = GMisc.label ~text:("PPage " ^ string_of_int i) () in
      let frame = GBin.frame ~label:text ~width:100 ~height:75 ~border_width:10
      ~packing:(notebook#prepend_page ~tab_label:label#coerce) () in
      let label = GMisc.label ~text ~packing:frame#add () in
      ()
      done;

      (* Set what page to start at (page 4) *)
      notebook#goto_page 3;

      (* Create a bunch of buttons *)
      let button = GButton.button ~label:"close"
      ~packing:(table#attach ~left:0 ~top:1) () in
      button#connect#clicked ~callback:GMain.Main.quit;

      let button = GButton.button ~label:"next page"
      ~packing:(table#attach ~left:1 ~top:1) () in
      button#connect#clicked ~callback:notebook#next_page;

      let button = GButton.button ~label:"prev page"
      ~packing:(table#attach ~left:2 ~top:1) () in
      button#connect#clicked ~callback:notebook#previous_page;

      let button = GButton.button ~label:"tab position"
      ~packing:(table#attach ~left:3 ~top:1) () in
      button#connect#clicked ~callback:(rotate_book notebook);

      let button = GButton.button ~label:"tabs/border on/off"
      ~packing:(table#attach ~left:4 ~top:1) () in
      button#connect#clicked ~callback:(tabsborder_book notebook);

      let button = GButton.button ~label:"remove page"
      ~packing:(table#attach ~left:5 ~top:1) () in
      button#connect#clicked ~callback:(remove_book notebook);

      window#show ();
      GMain.Main.main ()

      let _ = Printexc.print main ()
      ---------------------------------------------------------------------------

      Merci
    • Adrien
      Hi, Depending on which documentation you use, you might get old and outdated info. I know that one of the HTML documentation available on the internet for
      Message 2 of 3 , Apr 27, 2011
      • 0 Attachment
        Hi,

        Depending on which documentation you use, you might get old and
        outdated info. I know that one of the HTML documentation available on
        the internet for lablgtk has this issue.
        I don't have the time to check this what you are seeing but it's possible.

        In doubt, check the local libraries, with ocamlbrowser which is really
        handy, in particular when it comes to lablgtk.


        Your issue is that append_page returns an int if my memory serves
        well, and the ~packing function must return an int. I think it mostly
        serves as error-checking: the C API returns -1 on error.
        It might also be for consistency with the other functions to add new
        pages (iirc, you can use values that are too small or too big without
        error, including negatives).

        This change (not checked) should solve your issue:

        - ~packing:(notebook#append_page ~tab_label:label#coerce) () in
        + ~packing:(fun o -> ignore(notebook#append_page
        ~tab_label:label#coerce o)) () in


        Hope this helps,

        Adrien Nader
      • Jaques Bertrand
        Hi, Thanks for the response. The same error creeps in on lines 38, 46, and 55. As far as I can tell, the documentation and example was last updated 02/21/2011,
        Message 3 of 3 , Apr 27, 2011
        • 0 Attachment
          Hi,
          Thanks for the response. The same error creeps in on lines 38, 46, and 55. As far as I can tell, the documentation and example was last updated 02/21/2011, but some changes to the library upstream are not necessarily amended downstream. Setting the return type to "ignore" now compiles fine.

          Thanks.
          --- En date de : Mer 27.4.11, Adrien <camaradetux@...> a écrit :

          De: Adrien <camaradetux@...>
          Objet: Re: "ocaml_beginners"::[] lablGTK question
          À: ocaml_beginners@yahoogroups.com
          Date: Mercredi 27 avril 2011, 17h37
















           









          Hi,



          Depending on which documentation you use, you might get old and

          outdated info. I know that one of the HTML documentation available on

          the internet for lablgtk has this issue.

          I don't have the time to check this what you are seeing but it's possible.



          In doubt, check the local libraries, with ocamlbrowser which is really

          handy, in particular when it comes to lablgtk.



          Your issue is that append_page returns an int if my memory serves

          well, and the ~packing function must return an int. I think it mostly

          serves as error-checking: the C API returns -1 on error.

          It might also be for consistency with the other functions to add new

          pages (iirc, you can use values that are too small or too big without

          error, including negatives).



          This change (not checked) should solve your issue:



          - ~packing:(notebook#append_page ~tab_label:label#coerce) () in

          + ~packing:(fun o -> ignore(notebook#append_page

          ~tab_label:label#coerce o)) () in



          Hope this helps,



          Adrien Nader

























          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.