[gtkada] Getting draw in a drawable area

Jean-Etienne Doucet doucet at laas.fr
Thu Mar 6 10:53:53 CET 2003


From: Nacho <nachooya at teleline.es>
| 
| I've to learn gtkada to do a postcript viewer for a university work. 
| My main problem is that i can get to paint in a drawable area.
| I've made a lot of tries to get this and the only i get are runtime errors.
| Can somebody says me why this code have runtime errors?
| Thank you and sorry for my english.

(Mine may not be any better...)

There are many things that missing:

First, it's not that clever to use the same name for the main procedure
and one of its variable, although it does no harm here.

Second, you need to use Gtk.Main.Init (to initialize the Gtk library)
and Gtk.Main.Main to allow events management.

Then, you need to add a callback for "expose_event" for your Drawing_Area,
thus add Gtk.Handlers in your context clauses; this callback will in turn call Draw.

So, the program may in the end look like this:

| with Glib;
| with Gdk.Window;
| with Gdk.Drawable;
| with Gdk.GC;
  with Gdk.Color;                         -- added
| with Gdk.Font;
| with Gtk.Drawing_Area;
| with Gtk.Window;
  with Gtk.Main;                          -- added
  with Gtk.Handlers;                      -- added
| 
| procedure Dibujo is
| 
     -- Gdkw : Gdk.Window.Gdk_Window;     -- modified
     -- GC   : Gdk.GC.Gdk_GC;             -- modified
     -- Font : Gdk.Font.Gdk_Font;         -- modified
     -- these variables are only used in the Draw procedure
|    Dibujo : Gtk.Drawing_Area.Gtk_Drawing_Area;
|    Ventana : Gtk.Window.Gtk_Window;
   
     package Cb is new 
        Gtk.Handlers.Callback(Gtk.Drawing_Area.Gtk_Drawing_Area_Record);  -- added
     use Cb;                              -- added
   
|    procedure Draw (Drawing : in out Gtk.Drawing_Area.Gtk_Drawing_Area) is
|       Gdkw : Gdk.Window.Gdk_Window;
|       GC   : Gdk.GC.Gdk_GC;
|       Font : Gdk.Font.Gdk_Font;
|       use type Glib.Gint;
| 
|    begin
|       -- Get the Gdk window
| 
|       Gdkw := Gtk.Drawing_Area.Get_Window (Drawing) ;
| 
|       -- Clear the window
| 
|       GDK.Window.Clear (Gdkw) ;
| 
|       -- Create a graphic context associated with this window
| 
|       Gdk.GC.Gdk_New (GC, Gdkw);
        Gdk.GC.Set_Foreground (GC, Black(Get_System));   -- added: set the color
| 
|       -- Draw a line in this window
| 
|       Gdk.Drawable.Draw_Line
|         (Drawable => Gdkw,
|          GC => GC,
|          X1 =>   0, Y1 =>   0,
|          X2 => 100, Y2 => 100);
| 
|       -- Draw an arc
| 
|       Gdk.Drawable.Draw_Arc
|         (Drawable => Gdkw,
|          Gc       => gc,
|          Filled   => True,
|          X        => 100,
|          Y        => 100,
|          Width    => 200,
|          Height   => 100,
|          Angle1   => 0 * 64,
|          Angle2   => 270 * 64);
| 
|       -- Ask for a given font
| 
|       Gdk.Font.Load (Font,
|       --             "-adobe-courier-medium-i-*-*-15-*-*-*-*-*-*-*"); --bad name!
                       "-*-courier-medium-r-normal-*-*-160-*-*-*-*-*-*"); -- replaced
|     Gdk.Drawable.Draw_Text
|       (Drawable    => Gdkw,
|        Font        => Font,
|        Gc          => gc,
|        X           => 50,
|        Y           => 50,
|        Text        => "Hello World");
|     Gdk.GC.Destroy (GC);
|    end Draw;
|  
     -- the callback proc for expose_event
     procedure Expose (W : access Gtk.Drawing_Area.Gtk_Drawing_Area_Record'Class)
     is
     begin
        Draw (Dibujo);
     end Expose;

| 
| begin

     Gtk.Main.Init;  --added
| 
|    Gtk.Window.Gtk_New (Ventana);
|    Gtk.Drawing_Area.Gtk_New (Dibujo);
     Connect (Dibujo, "expose_event", To_Marshaller(Expose'Access));  -- added
     Gtk.Drawing_Area.Size (Dibujo, 400, 400);   -- added
|    Gtk.Window.Add (Ventana, Dibujo);
|    Gtk.Window.Show_All (Ventana);
|    Draw (Dibujo);
| 
     Gtk.Main.Main;  -- added

| end Dibujo;


I for me would add a callback to "delete_event" for the main window (Ventana)
to quit the program more properly than with a Ctl-C.

Hope this will help...

--jed--



More information about the gtkada mailing list