Ada and GTK on windows

Ada and GTK+ on windows – hello world sample

Create new project with project wizard.  After initial project creation add project dependency to GtkAda.gpr from “gtk ada” installation folder: \GtkAda\lib\gnat\gtkada.gpr.

2016-08-02 22_11_26-GPS - Project - Gtk2Sample projectOr you can simply open project file (file with ‘gpr’ extension) and add “with”  statement :

with "gtkada";

project Gtk2Sample is

   type Build_Kind is
      ("static", "relocatable");
   LIBRARY_TYPE : Build_Kind := external ("LIBRARY_TYPE");

   case LIBRARY_TYPE is

      when "static" =>
         for Main use ("main.adb");

      when "relocatable" =>

   end case;

end Gtk2Sample;

“\GtkAda\bin” folder should be in the PATH.

And don’t forget to add special “windows GUI” directives to the linker, go to Project/Properties , under Build/Switches/Ada Linker add this directives in the field :


If you don’t do this, linker will create “windows console application” and additional command line window will always open with your application.

with Gtk.Box;         use Gtk.Box;
with Gtk.Label;       use Gtk.Label;
with Gtk.Widget;      use Gtk.Widget;
with Gtk.Main;
with Gtk.Window;      use Gtk.Window;

procedure Main is
   Win   	: Gtk_Window;
   Label 	: Gtk_Label;
   Box   	: Gtk_Vbox;
   --  Initialize GtkAda.

   --  Create a window with a size of 400x400
   Gtk_New (Win);
   Win.Set_Default_Size (400, 400);

   --  Create a box to organize vertically the contents of the window
   Gtk_New_Vbox (Box);
   Win.Add (Box);

   --  Add a label
   Gtk_New (Label, "Hello from Ada and Gtk !");
   Box.Add (Label);

   --  Show the window
   --  Start the Gtk+ main loop
end Main;

If everything was successful, compile / build will create “main.exe”  program file and if  you run from inside GPS  or from the windows file explorer, only the GTK program window should open.

2016-08-02 22_42_28-GPS - Run_ main - Gtk2Sample projectThis is probably minimal GTK project, perfect to learn Ada with graphical interface.

If you have problems with the installation, please check if everything is installed properly… You can find some hints about installation here.

Ada database – first steps

Connect to database

Using Ada for application development require access to database.

With GNATColl library is possible to work with PostgreSQL and Sqlite (embedded database).

Checking if everything is in place for database development

If you successfully install AdaCore and compile GNATColl library, you can start writing and checking if everything works as expected.

Create new project, project file must contain gnatcoll reference:

My dbsample1.gpr file contains:

with "gnatcoll";
with "gnatcoll_postgres";

project Dbsample1 is
    for Source_Dirs use ("src");
    for Object_Dir use "obj";
    for Main use ("main.adb");
end Dbsample1;

And main.adb file:

with gnatcoll.SQL.Postgres;  use gnatcoll.SQL.Postgres;
with gnatcoll.SQL.Exec;      use gnatcoll.SQL.Exec;
with Ada.Text_IO;            use Ada.Text_IO;

procedure Main is
   DB_Descr : Database_Description;
   DB       : Database_Connection;
   IsOpen   : Boolean;
   DB_Descr := Setup(Database => "appdb",
                     User => "postgres",
                     Host => "localhost",
                     Password => "postgres",
                     Port => 5432
   DB := DB_Descr.Build_Connection;

   IsOpen := DB.Check_Connection;
   if IsOpen then
      Put_Line("Connection is open.");
      Put_Line("Last Db error = " & DB.Error);
   end if;

   -- reset state of connection for reuse

   Free (DB);
   Free (DB_Descr);

end Main;

You do not forget to add PostgreSQL/bin folder to local windows PATH, or copy postgres libraries to executing application folder.

If you do everything as needed, main.exe will be created and when you run it, you  will get something minimal but without any errors :

Connection is open.
[2016-07-23 22:38:22] process terminated successfully, elapsed time: 00.30s