[gtkada] Addressing external data in a GUI
Stephen Leake
stephen_leake at acm.org
Thu Jun 9 11:41:06 CEST 2005
Rick Duley <30294025 at student.murdoch.edu.au> writes:
> I don't know how GPS is involved here.
GPS is the Gnat (or Gnu?) Programming System; it is an example of a
large GUI program. Apparently it does not use global variables, so it
is an example of what you are trying to do.
It would be instructive for you to try to read the sources for GPS;
there are lots of nice GUI ideas in there. Be warned; it's HUGE.
> What I have done is include the "necessary" access type in the
> record that defines the Window. Oh, I am _sure_ it is not the
> _right_ technique - it's terrible Ada! That's why I am trying to
> find out what the _right_ technique _is_!
I'd need to see the code to see what you are doing, and whether it is
"terrible Ada".
The "right technique" is to have the appropriate window type contain
the data, and then convert the widget pointer you get in a callback to
the "right" widget pointer, using some variant of Get_Parent. For
example, here is a fragment from one of my callbacks for a button
click:
procedure On_Next_Week_Clicked
(Button : access Gtk.Button.Gtk_Button_Record'Class)
is
Window : constant Gtk_Window :=
Gtk_Window (Gtk.Button.Get_Toplevel (Button));
begin
VCR.Model.Data.Next_Week (Window.Data.all);
Here 'Gtk_Window' is the access type for my GUI main window; it
contains my data, and it is the top level parent for the button, which
is nested in boxes. VCR.Model.Data.Next_Week is a procedure that
operates on that data.
This involves a run-time check of the tag of Button; that's the price
you pay for the flexibility of Gtk.
Sometimes you need to use Get_Parent (possibly several times, possibly
in a loop) instead of Get_Toplevel, if the data you are operating on
is owned not by the top level window, but by some intermediate window.
I have not read the Gtk user's guide recently, so I don't remember if
this is in there.
Hope this helps.
--
-- Stephe
More information about the gtkada
mailing list