[gtkada] Problem in GTK ADA

Ludovic Brenta ludovic.brenta at insalien.org
Fri Feb 20 19:28:58 CET 2004


This is not specific to GtkAda; you need to understand general Ada
programming a little bit better.

"GOUGOU1" <nicolasmac at free.fr> writes:

>    procedure Change_Label (Widget : access Gtk_Widget_Record'class) is
> 
>       Chaine : UTF8_String(1..20);
>    begin
>       Chaine := Gtk.Gentry.Get_Text(saisie);

Gtk.GEntry.Get_Text returns a String whose size is unknown at compile
time (an "unconstrained array of characters").  Yet you are trying to
copy it into a String of fixed size.  You will get a Constraint_Error
whenever Get_Text returns a String with bounds that are different from
the bounds of Chaine.

>       Gtk.Label.Set_Text(Label,Chaine);
>    end Change_Label;

Better to write:

procedure Change_Label (Widget : access Gtk_Widget_Record'class) is
   Chaine : String := Gtk.Gentry.Get_Text(saisie);
   --  The above is not a copy; it is an initialisation.  The size of
   --  Chaine needs not be known at compile time.
begin
   --  Modify Chaine here if you want to.  Do not make assumptions
   --  about the bounds; use Chaine'Range, Chaine'First and
   --  Chaine'Last.  For example:
   for J in Chaine'Range loop
      Ada.Text_IO.Put (Chaine (J));
   end loop;
   Gtk.Label.Set_Text (Label, Chaine);
end Change_Label;

(p.s. little English-to-French dictionary just in case:
bounds = bornes
assumption = hypothèse)

HTH (= Hope This Helps)

-- 
Ludovic Brenta.




More information about the gtkada mailing list