[giraffe-user] Label/TextView in giraffe library

Phil Clayton phil.clayton at veonix.com
Tue Mar 5 06:43:37 CST 2024


Hi,

On 05/03/2024 11:51, Kyle DENG wrote:
> I hope to create a Label/TextView here and can setText or getText at any 
> time. But I can't find the relevant interface documentation and can 
> query how to implement it.

The GTK documentation can be found at
https://docs.gtk.org/gtk3/

The original GTK documentation, which some find more readable is at
https://developer-old.gnome.org/gtk3/

There is not currently SML-specific documentation but you can look at 
the SML signatures in your Giraffe Library installation.  For the Gtk 
namespace we have
     structure Gtk : GTK
and the signatures in
     ${GIRAFFEHOME}/lib/sml/gtk-3.0/
define the SML interface, e.g.
     GTK-sig.sml
     GTK_LABEL-sig.sml

The general form of the SML interface for a library is described here:
https://giraffelibrary.org/ug-index.html#sml-interface


> |val textEntry = Entry.new () val () = Grid.attach grid (textEntry, 0, 
> 0, 5, 1)|

Note that these are not in the scope of `open Gtk` so would produce a 
compiler error about `Entry` and `Grid` being unknown.  You could write 
e.g. `Gtk.Entry.new` but you likely want to declare these inside 
`activate` anyway.  At the top-level, SML would keep a reference to 
these objects, which you don't want.


> |fun activate app () = let open Gtk (* create a new window, and set its 
> title *) val window = ApplicationWindow.new app val () = Window.setTitle 
> window "Window" val () = Container.setBorderWidth window 10 (* Here we 
> construct the container that is going pack our buttons *) val grid = 
> Grid.new () (* Pack the container in the window *) val () = 
> Container.add window grid (* val textEntry = Label.new (SOME "Input: ") 
> *) val textEntry = Entry.new () val () = Grid.attach grid (textEntry, 0, 
> 0, 5, 1) val () = Widget.showAll window in () end |
> 
> I want to know how to create a Label/TextView and be able to setText or 
> getText at any time.

You can see the SML interface for these classes in
     GTK_LABEL-sig.sml
     GTK_TEXT_VIEW-sig.sml
and GtkEntry is in
     GTK_ENTRY-sig.sml

For example, the following copies the text entry to the output label 
(and stdout) when the update button is pressed:

fun activate app () =
   let
     open Gtk

     (* create a new window, and set its title *)
     val window = ApplicationWindow.new app
     val () = Window.setTitle window "Window"
     val () = Container.setBorderWidth window 10

     (* Here we construct the container that is going pack our buttons *)
     val grid = Grid.new ()

     (* Pack the container in the window *)
     val () = Container.add window grid

     val inputLabel = Label.new (SOME "Input: ")
     val inputEntry = Entry.new ()
     val () = Grid.attach grid (inputLabel, 0, 0, 1, 1)
     val () = Grid.attach grid (inputEntry, 1, 0, 5, 1)

     val updateButton = Button.newWithLabel "Update"
     val outputLabel = Label.new NONE
     val () = Grid.attach grid (updateButton, 6, 0, 1, 1)
     val () = Grid.attach grid (outputLabel,  7, 0, 1, 1)

     fun onUpdate () =
       let
         val text = Entry.getText inputEntry
         val () = Label.setText outputLabel text
         val () = List.app print [text, "\n"]
       in
         ()
       end
     val _ = Signal.connect updateButton (Button.clickedSig, onUpdate)

     val () = Widget.showAll window
   in
     ()
   end



More information about the giraffe-user mailing list