please someone has an idea on how i can to solve this problem??? here is my code.
main.adb
pragma Ada_2022;
with ada.text_io; use ada.text_io;
with Gtk.Window; use Gtk.Window;
with Gtk.Main; use Gtk.Main;
with Gdk.Pixbuf; use Gdk.Pixbuf;
with Glib.Error; use Glib.Error;
procedure main is
Win : Gtk_Window;
Pix, Pix2 : Gdk_Pixbuf;
Erreur : Glib.Error.GError;
begin
Init;
Gtk_New (Win);
Win.Fullscreen;
Gdk_New_From_File (Pix, “Image.jpg”, Erreur);
Pix2 := Scale_Simple (Pix, 0, 0); – line 22.
Set_Icon (Win, Pix2);
Win.Show_All;
Main;
end main;
when i run my code, it raise an exception :
raised CONSTRAINT_ERROR : glib-object.adb:138 access check failed
[/home/dimitrilesdos/project/software/main/bin/main]
0x578d21 glib__object__get_object.part.0 at ???
0x579b41 gdk__pixbuf__scale_simple at ???
0x5691ff main at main.adb:22
0x56a682 Main at b__main.adb:1093
[/lib/x86_64-linux-gnu/libc.so.6]
0x7fc4ca060248
0x7fc4ca060303
[/home/dimitrilesdos/project/software/main/bin/main]
0x5690bf _start at ???
0xfffffffffffffffe
You should handle errors. Add this after Gdk_New_From_File:
Gdk_New_From_File (Pix, "Image.jpg", Erreur);
if Erreur /= null then
Put_Line ("Gdk_New_From_File error: " & Get_Message (Erreur));
Error_Free (Erreur); -- Erreur must be freed
return;
end if;
1 Like
okay but the error message tell me that : failed to open file “image.jpg” : no such file or directory.
However, I put the image file where my program is located. why it can’t to open the file???
Because there is no such file? “Image.jpg” /= “image.jpg” in many OSes.
BTW, when building a GTK application you can use embedded images instead of loading external files.
the file exists, but the program cannot find it I think. but why it not find my file??? and how to do for that it find???
Use Ada.Directories.Current_Directory to check the working directory and Ada.Directories.Exists to check the file name.
thanks for your help. I don’t think that it’s the only solution but it works.
. . .
bool : boolean;
begin
. …
bool := Ada.Directories.exists (“image.jpg”);
if bool = true then
Gdk_New_From_File (Pix,“image.jpg”, Erreur);
Pix2 := Scale_Simple (Pix, 0, 0);
if Erreur /= null then
Put_Line ("Gdk_New_From_File error: " & Get_Message (Erreur));
Error_Free (Erreur); – Erreur must be freed
return;
end if;
end if;
I use embedded images, so setting the icon never fails. I also use Set_Icon_List with 16x16, 24x24, 32x32 icons. GTK selects the most appropriate one for each case from the list.
When you load an external image you should not use a lossy formats like jpg and scaling. Scaling works very poorly for small images.