2

I am trying to figure out how to instantiate a multi-line text box inside a graphical widget. LablGTK2 appears to be quite limited in terms of documentation and the API is scarce for the things that start to look like what I want.

I have started to cross-reference the original GTK2 documentation, https://developer.gnome.org/gtk3/stable/gtkobjects.html, against the Lablgtk2 documentation, http://wwwfun.kurims.kyoto-u.ac.jp/soft/lsl/lablgtk/html/GText.html.

However, the best tutorial or simple/clear example I have found makes use of the very limiting single line text entry box: http://plus.kaist.ac.kr/~shoh/ocaml/lablgtk2/lablgtk2-tutorial/x1155.html

I have found that some clear simple, derived examples are great for learning the basics. Does anyone have sample vignette that shows how to set up a multi-line text using OCaml & Lablgtk/lablgtk2? Or better recommendations for tutorials that will show to develop a multi-line text box (which is a pretty important feature in any GUI-based program)? Ideally, I want to connect the text input into this multi-line text to an OCaml module I have written that will process that text and then the GUI will display that processing results back on the GUI. Any help would be greatly appreciated.

anol
  • 8,264
  • 3
  • 34
  • 78
9codeMan9
  • 802
  • 6
  • 11
  • 25

3 Answers3

3

You can use GtkTextView widget for multi-line text:

let _ =
    (* prepare main window *)
    let window = GWindow.window () in
    window#connect#destroy ~callback:GMain.Main.quit;

    (* add text view with scroll bars *)
    let scroll = GBin.scrolled_window
                 ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
                 ~packing:window#add () in
    let textview = GText.view ~packing:scroll#add_with_viewport () in

    (* set text *)
    textview#buffer#set_text "multi-\nline\ntext";

    (* show everything and enter main loop *)
    window#show ();
    GMain.Main.main ()
barti_ddu
  • 10,179
  • 1
  • 45
  • 53
  • This is sweet, exactly what I was looking for! – 9codeMan9 May 01 '13 at 00:20
  • One addition, do you know how I could nest this multi-line text box? I want to have a file menu bar display above and a few buttons below, but keep getting the following error when I try to put the multi-line box code within the system: (myprog.exe:9460): Gtk-WARNING **: Attempting to add a widget with type GtkScrolledWindow to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one widget at a time; it already contains a widget of type GtkVBox – 9codeMan9 May 03 '13 at 04:10
  • @9codeMan9: GtkWindow can hold only single widget (as in example above); if you have more then you should pack scrolled window into appropriate container i.e. box (most likely vbox in your case) or table (for more complex layout). See http://plus.kaist.ac.kr/~shoh/ocaml/lablgtk2/lablgtk2-tutorial/c383.html for packing widgets. – barti_ddu May 03 '13 at 06:12
1

You're right that the docs are very sparse. Therefore we must learn by copying each other. Like monkeys...

I would wager that the ocamleditor should contain an example of how to do this: https://forge.ocamlcore.org/projects/ocamleditor/

Also OCP is creating a simple OCaml editor which should also be helpful: https://github.com/OCamlPro/ocp-edit-simple

rgrinberg
  • 9,638
  • 7
  • 27
  • 44
1

The lablgtk2 source code provides basic code snippets that are easy to understand for the beginning (at least easier than real world code)

If you use godi, they are installed under $GODI_PRFIX/doc/godi-lablgtk2/examples . You can see them in action from the command line with the script lablgtk2, e.g

lablgtk2 /opt/wodi32/doc/godi-lablgtk2/examples/editor.ml
lablgtk2 /opt/wodi32/doc/godi-lablgtk2/examples/text/text-demo.ml
rafix
  • 1,739
  • 9
  • 9