[gtkada] Addressing external data in a GUI

Preben Randhol randhol+gtkada at pvv.org
Tue Jun 14 17:39:37 CEST 2005


On Tue, Jun 14, 2005 at 02:48:11PM +0800, Rick Duley wrote:
> Hi Preben
> 
> I did try Get_Toplevel as I explained further down the posting.  Yes, 
> it returns
> a widget.
> 
> What I am trying to do is get access to a user-defined data structure I have
> included in the Gtk_Window_Record which defines the Toplevel window.  As I
> showed, the widget I get from Get_Toplevel does _not_ allow me to do that.
> 
> Here's the MAIN_QUESTION_:
> 
> How do you get data into a GUI, then access it within the GUI so that
> the User can alter it using the GUI, then get it out of the GUI again?
> 
> Here's the second question:
> 
> If you cannot do the things in the _MAIN_QUESTION_ then what's the use
>in_Window : Main_Window_Access := null;
of a GUI?
> 

GUI = Graphic User Interface /= Database.

This is my opinion. I don't want to store my data in the gui at all. I
want it to be seperated so one can use a different GUI if desired.

Now how one does things differs a great deal depening on what one wants
to do and the nature of the program. If one can assume that your program
has one main window I would do:

   package Gui is

      type Main_Window_Record is new Gtk_Window_Record with private;
      type Main_Window_Access is access Main_Window_Record'Class;

   ...

   private

      type Main_Window_Record is new Gtk_Window_Record with record
           Main_Window_Vbox        : Gtk_Vbox;
           Main_Window_Menubar     : Gtk_Menu_Bar;
            ...
        end record;

      The_Main_Window : Main_Window_Access := null;

   end Gui;

Now you can simply access the mian window from your callback if you
want.

However if I want to set a Default directory for where the files are to
be stored I would do:

   package File_IO.Settings is

      function Get_Storage_Directory return String;
      procedure Set_Storage_Directory (Directory : String);

      ...

   private

      Storage_Directory : Unbounded_String := To_Unbounded_String ("");

      ...
   end File_IO.Settings;

In the occations you have several widgets of the same type you or you
want that things are more general you do this:


   package Window1_Pkg is

      type Window1_Record is new Gtk_Window_Record with record
         Vbox1 : Gtk_Vbox;
         Hbox1 : Gtk_Hbox;
         Label2 : Gtk_Label;
         Entry1 : Gtk_Entry;
         Label1 : Gtk_Label;
      end record;
      type Window1_Access is access all Window1_Record'Class;

-- Note that it says access all above and not only access.

      procedure Gtk_New (Window1 : out Window1_Access);
      procedure Initialize (Window1 : access Window1_Record'Class);

   end Window1_Pkg;

   and then you hook the Entry1 like this:

      Widget_Callback.Object_Connect
           (Window1.Entry1, "activate", Widget_Callback.To_Marshaller
                 (On_Entry1_Activate'Access), Window1);

and then the callback look like this:

   procedure On_Entry1_Activate
     (Object : access Gtk_Widget_Record'Class)
   is
      W : constant Window1_Pkg.Window1_Access :=
         Window1_Pkg.Window1_Access (Object);
   begin
      Set_Text (W.Label1, Get_Text (W.Entry1));
   end On_Entry1_Activate;

For working example see:

   http://www.pvv.org/~randhol/Ada95/CB.tar.gz
   or
   http://www.pvv.org/~randhol/Ada95/CB.zip


- 
Preben Randhol -------------- http://www.pvv.org/~randhol/Ada95 --
                 «For me, Ada95 puts back the joy in programming.»



More information about the gtkada mailing list