[racket] Extending racket/gui with Gtk widgets

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Mar 2 08:20:54 EST 2012

At Thu, 01 Mar 2012 16:25:56 -0300, Diogo F. S. Ramos wrote:
> Experimenting with different codes, I get the impression that a panel%,
> using gtk_container_add(), can hold only one child. So, my idea is to
> create subclasses of panel% and each of these will have a new GtkWidget
> inside.
> Unfortunately my new objects do not show if I add them to a panel% but
> they make an appearance inside a frame%.

I think the problem in the example below is the outer panel `pan'. It's
an instance of `panel%', which implements no layout. As a result, the
`label%' and `message%' are stacked up.

Make `pan' a `vertical-panel%', instead. Then, when you resize the
window, you'll see "GtkLabel". If you set the minimum height of the
panel in `label%', then you won't have to resize to see "GtkLabel".

> Below there is a code example of my proposed technique and the ninja
> class (under certain weather conditions) label%, which is redundant
> because of message% but illustrates my point. The object `message'
> appears, but not `label'.
> For the record, I'm using Racket 5.1.3.
> #lang racket/gui
> (require racket/class)
> (require ffi/unsafe)
> (define win (new frame% (label "hello, world")))
> (define pan (new panel% (parent win)))
> (define gtk-label-new (get-ffi-obj "gtk_label_new" #f (_fun _string -> 
> _pointer)))
> (define gtk-container-add (get-ffi-obj "gtk_container_add" #f (_fun _pointer 
> _pointer -> _void)))
> (define gtk-widget-show (get-ffi-obj "gtk_widget_show" #f (_fun _pointer -> 
> _void)))
> (define label%
>   (class panel%
>     (init parent text)
>     (super-new (parent parent))
>     (define ptr (gtk-label-new text))
>     (gtk-container-add (send this get-client-handle) ptr)
>     (gtk-widget-show ptr)))
> (define label (new label%
>                    (text "GtkLabel")
>                    (parent pan)))
> (define message (new message%
>                      (label "Message")
>                      (parent pan)))
> (send label show #t)
> (send win show #t)
> -- 
> Diogo F. S. Ramos

Posted on the users mailing list.