OCaml + LablGTK2: Multi-Line Text Box

1k views Asked by At

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.

3

There are 3 answers

0
rafix On

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
0
rgrinberg On

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

3
barti_ddu On

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 ()