[gtkada] badly need help with Gtk.Tree_View

John Stoneham captnjameskirk at gmail.com
Tue Feb 22 16:37:01 CET 2005


If I can just get this simple case to work, I can move on and actually
do my project. Until then I'm stuck, and right now I AM STUCK! :) The
simple case has a couple of restrictions (necessary for the full
project): The gui is designed with glade-2 using it's Ada95 source
output, and consists of a main window, a menu with only File->Open and
File->Quit, and a TreeView. That's all. The File->Open option just
fills the TreeView with two Parent items (the text strings "parent0"
and "parent1") each having two Child items (the text strings "child0"
and "child1", and "child2" and "child3" respectively); these strings
can be hard-coded in the File->Open callback for this simple case, but
will be loaded from a text file in the full project. Right now I'm
only interested in putting the text strings in the TreeView however I
can, using the files generated by glade.

With the name of "treetest" for the glade project and the main window,
glade produces .ads and .adb files for treetest_pkg and
treetest_pkg-callbacks, along with a couple of other files. Right now
I have no idea how to code the simple treeview I mentioned above! Do I
set up a Tree_Store in the Treetest_Pkg.Initialize procedure? Is there
anything else I need to add there to use the TreeView? And since the
TreeView isn't passed as an argument to the File->Open callback, how
do I access it to fill in the tree?

Below are the two .adb files I mentioned as produced by glade (with a
small edit by me in the callbacks to call Gtk_Exit). Would some kind
soul PLEASE HELP and show me what to add where, to produce the simple
tree I described above?

----------------------------------------------
--- treetest_pkg.adb
----------------------------------------------
with Glib; use Glib;
with Gtk; use Gtk;
with Gdk.Types; use Gdk.Types;
with Gtk.Widget; use Gtk.Widget;
with Gtk.Enums; use Gtk.Enums;
with Gtkada.Handlers; use Gtkada.Handlers;
with Callbacks_Treetest; use Callbacks_Treetest;
with Treetest_Intl; use Treetest_Intl;
with Treetest_Pkg.Callbacks; use Treetest_Pkg.Callbacks;

package body Treetest_Pkg is

procedure Gtk_New (Treetest : out Treetest_Access) is
begin
   Treetest := new Treetest_Record;
   Treetest_Pkg.Initialize (Treetest);
end Gtk_New;

procedure Initialize (Treetest : access Treetest_Record'Class) is
   pragma Suppress (All_Checks);
   Pixmaps_Dir : constant String := "pixmaps/";
begin
   Gtk.Window.Initialize (Treetest, Window_Toplevel);
   Set_Title (Treetest, -"treetest");
   Set_Position (Treetest, Win_Pos_None);
   Set_Modal (Treetest, False);

   Gtk_New_Vbox (Treetest.Vbox1, False, 0);

   Gtk_New (Treetest.Menubar);

   Gtk_New_With_Mnemonic (Treetest.Menuitem1, -("_File"));

   Gtk_New (Treetest.Menuitem1_Menu);

   Gtk_New_From_Stock (Treetest.Open, "gtk-open");

   Image_Menu_Item_Callback.Connect
     (Treetest.Open, "activate",
      Image_Menu_Item_Callback.To_Marshaller (On_Open_Activate'Access), False);
   Append (Treetest.Menuitem1_Menu, Treetest.Open);
   Gtk_New_From_Stock (Treetest.Quit, "gtk-quit");

   Image_Menu_Item_Callback.Connect
     (Treetest.Quit, "activate",
      Image_Menu_Item_Callback.To_Marshaller (On_Quit_Activate'Access), False);
   Append (Treetest.Menuitem1_Menu, Treetest.Quit);
   Set_Submenu (Treetest.Menuitem1, Treetest.Menuitem1_Menu);
   Append (Treetest.Menubar, Treetest.Menuitem1);
   Pack_Start
     (Treetest.Vbox1,
      Treetest.Menubar,
      Expand  => False,
      Fill    => False,
      Padding => 0);
   Gtk_New (Treetest.Scrolledwindow1);
   Set_Policy (Treetest.Scrolledwindow1, Policy_Always, Policy_Always);
   Set_Shadow_Type (Treetest.Scrolledwindow1, Shadow_In);

   Gtk_New (Treetest.Treeview);
   Set_Headers_Visible (Treetest.Treeview, True);
   Set_Rules_Hint (Treetest.Treeview, False);
   Set_Reorderable (Treetest.Treeview, False);
   Set_Enable_Search (Treetest.Treeview, True);

   Add (Treetest.Scrolledwindow1, Treetest.Treeview);
   Pack_Start
     (Treetest.Vbox1,
      Treetest.Scrolledwindow1,
      Expand  => True,
      Fill    => True,
      Padding => 0);
   Add (Treetest, Treetest.Vbox1);
end Initialize;

end Treetest_Pkg;

----------------------------------------------
--- treetest_pkg-callbacks.adb
----------------------------------------------
with System; use System;
with Glib; use Glib;
with Gdk.Event; use Gdk.Event;
with Gdk.Types; use Gdk.Types;
with Gtk.Accel_Group; use Gtk.Accel_Group;
with Gtk.Object; use Gtk.Object;
with Gtk.Enums; use Gtk.Enums;
with Gtk.Style; use Gtk.Style;
with Gtk.Widget; use Gtk.Widget;

---- begin hand edit
with Gtk.Main; use Gtk.Main;
---- end hand edit

package body Treetest_Pkg.Callbacks is

   use Gtk.Arguments;

   ----------------------
   -- On_Open_Activate --
   ----------------------

   procedure On_Open_Activate
     (Object : access Gtk_Image_Menu_Item_Record'Class)
   is
   begin
      null;
   end On_Open_Activate;

   ----------------------
   -- On_Quit_Activate --
   ----------------------

   procedure On_Quit_Activate
     (Object : access Gtk_Image_Menu_Item_Record'Class)
   is
   begin
      ---- begin hand edit
      Gtk_Exit(0);
      ---- end hand edit
   end On_Quit_Activate;

end Treetest_Pkg.Callbacks;



More information about the gtkada mailing list