comp.lang.ada
 help / color / mirror / Atom feed
From: ake.ragnar.dahlgren@gmail.com
Cc: mailbox@dmitry-kazakov.de
Subject: Re: Access type to member procedure of instance (Object Oriented programming in Ada)
Date: Mon, 3 Dec 2012 12:21:45 -0800 (PST)
Date: 2012-12-03T12:21:45-08:00	[thread overview]
Message-ID: <ea14b7bd-8ff9-4334-bd57-b2c94cc9ce3d@googlegroups.com> (raw)
In-Reply-To: <agc909hka7hk$.offs8wrgrm0x$.dlg@40tude.net>

On Monday, December 3, 2012 12:21:00 PM UTC+1, Dmitry A. Kazakov wrote:
> On Sun, 2 Dec 2012 12:42:27 -0800 (PST), ake.ragnar.dahlgren@gmail.com
> 
> wrote:
> 
> 
> 
> > I gladly provide more details by giving an example of Ada code. The with
> 
> > statements have been omitted and the GUI described by the
> 
> > main_window.glade file is assumed to contain a Label called Main_Label and
> 
> > the window has a destroy signal called Main_Window_Quit:
> 
> 
> 
> 1. I cannot comment on Glade, I don't use it.
> 
> 
> 
> 2. What is the problem?
> 
> 
> 
> Why handling Quit signal requires MVC, especially, because there is no such
> 
> signal, AFAIK?
> 
> 
> 
> 3. Is it about exiting GTK main loop? That does not need MVC at all.
> 
> 
> 
> In order to quit Gtk.Main.Main you have to call Gtk.Main.Quit. This is made
> 
> from a handler of the "destroy" event of Gtk_Object_Record, which is called
> 
> when a Gtk object is about to be destroyed. The object here is the main
> 
> window. That is it.
> 
> 
> 
> In order to initiate destruction of the main window otherwise than by means
> 
> of the window manager, the window manager does that when you press the
> 
> button [x] on window title, you should call the Destroy operation of
> 
> Gtk_Object_Record. The main window is Gtk_Window_Record, a descendant of.
> 
> 
> 
> Here is a minimal working sample of such application with a button to close
> 
> it:
> 
> 
> 
> ----------------- simple.adb -----------------------------
> 
> with Gtk.Object;  use Gtk.Object;
> 
> with Gtk.Button;  use Gtk.Button;
> 
> with Gtk.Window;  use Gtk.Window;
> 
> with Gtk.Widget;  use Gtk.Widget;
> 
> with Gtk.Table;   use Gtk.Table;
> 
> 
> 
> with Gtk.Handlers;
> 
> with Gtk.Main;
> 
> 
> 
> procedure Simple is
> 
> 
> 
>    package No_Data_Handlers is -- Event handlers with no user parameters
> 
>       new Gtk.Handlers.Callback (Gtk_Widget_Record);
> 
>    package Killer_Handlers is -- Handlers that take Gtk_Object
> 
>       new Gtk.Handlers.User_Callback (Gtk_Widget_Record, Gtk_Object);
> 
> 
> 
>    procedure Destroy (Widget : access Gtk_Widget_Record'Class) is
> 
>    begin
> 
>      Gtk.Main.Main_Quit; -- Exit the message loop, we are done
> 
>    end Destroy;
> 
> 
> 
>    procedure Clicked
> 
>              (  Widget : access Gtk_Widget_Record'Class;
> 
>                 Object : Gtk_Object
> 
>              )  is
> 
>    begin
> 
>       Destroy (Object); -- Kill the object passed
> 
>    end Clicked;
> 
> 
> 
>    Window : Gtk_Window;
> 
>    Grid   : Gtk_Table;
> 
>    Button : Gtk_Button;
> 
> 
> 
> begin
> 
>    Gtk.Main.Init;
> 
>    Gtk.Window.Gtk_New (Window);
> 
>    Gtk_New (Grid, 1, 1, False);
> 
>    Add (Window, Grid);
> 
>    Gtk_New (Button, "Clicked to kill");
> 
>    Attach (Grid, Button, 0, 1, 0, 1);
> 
>    No_Data_Handlers.Connect
> 
>    (  Window,
> 
>       "destroy",
> 
>       Destroy'Access
> 
>    );
> 
>    Killer_Handlers.Connect
> 
>    (  Button,
> 
>       "clicked",
> 
>       Clicked'Access,
> 
>       Gtk_Object_Record'Class (Window.all)'Access
> 
>    );
> 
>    Show_All (Grid);
> 
>    Show (Window);
> 
> 
> 
>    Gtk.Main.Main;
> 
> end Simple;
> 
> -----------------------------------------------------------
> 
> As you see it is almost trivial.
> 
> 
> 
> 4. Regarding MVC. When deploying it, the first questions to answer are:
> 
> 
> 
> 4.1. What is the model?
> 
> 4.2. What is the view?
> 
> 4.3. What is the controller?
> 
> 
> 
> http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
> 
> 
> 
> An example of using MVC in Gtk is combo box:
> 
> 
> 
> The model is Gtk_List_Store_Record, contains a row per box entry. A
> 
> designated column of the row determines the entry's contents.
> 
> 
> 
> The view is Gtk_Combo_Box_Record widget itself
> 
> 
> 
> The controller is anything else, which modifies the model, e.g. adds new
> 
> rows to the model. Typically an event callback responding to user actions.
> 
> 
> 
> -- 
> 
> Regards,
> 
> Dmitry A. Kazakov
> 
> http://www.dmitry-kazakov.de

I agree with you that the example is almost trivial and that it is a good demonstratiion on how to use Gtkada. However, I am currently toying with the idea of making Gtkada applications using Glade-3 which is very similiar but a little bit different.

> 2. What is the problem?
> 
> 
> 
> Why handling Quit signal requires MVC, especially, because there is no such
> 
> signal, AFAIK?

There are two problems that together would require one to structure the code as I have done.

1) Automated tests (AUnit). The code should be testable. Perhaps one would like to have a test that verifies that Gtk.Main.Main_Quit is called when the "destroy" event of Gtk_Object_Record is triggered from the main window and so on. To make the code testable one needs to separate the GUI code from the logic somehow.
2) Perhaps one would like two have two views who are "connected" to the same controller instance (code reusability). If one updates a field on the controller instance then that change should be reflected in the two views. Something like the following:

package body Application is

   Main_Window_Controller : Controllers.Main_Window.Controller_Ref_Type;

   Main_Window_1 : Views.Main_Window.Main_Window_Ref_Type;
   Main_Window_2 : Views.Main_Window.Main_Window_Ref_Type;

   procedure On_Main_Window_Quit(Object : access Gtkada.Builder.Gtkada_Builder_Record'Class)
                                 renames Main_Window_Controller.On_Quit;

   procedure Main is
   begin

      Main_Window_Controller := new Controllers.Main_Window.Controller_Type;

      Main_Window_1 := Views.Main_Window.Create_Main_Window(Main_Window_Controller,                                                          On_Main_Window_Quit'Access);
      Main_Window_2 := Views.Main_Window.Create_Main_Window(Main_Window_Controller,                                                          On_Main_Window_Quit'Access);


      Main_Window_1.Show;
      Main_Window_2.Show;

      Gtk.Main.Main;
      -- Enter the main gtk loop.
   end Main;

begin
   Gtk.Main.Init;
end Application;

Not that I have in mind an actual real world application that would require two identical windows, but of theoretical interest. It is possible in other languages, and I'm thinking why not Ada?

Best regards,
Åke Ragnar Dahlgren



  reply	other threads:[~2012-12-03 20:21 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-19  9:59 Access type to member procedure of instance (Object Oriented programming in Ada) ake.ragnar.dahlgren
2012-11-19 11:13 ` Georg Bauhaus
2012-11-19 11:39 ` Brian Drummond
2012-11-20 11:43   ` Brian Drummond
2012-11-20 21:57     ` Randy Brukardt
2012-11-19 13:03 ` sbelmont700
2012-11-19 16:18 ` Adam Beneschan
2012-11-19 17:02   ` Peter C. Chapin
2012-11-19 18:23     ` Adam Beneschan
2012-11-19 20:57       ` Peter C. Chapin
2012-11-19 21:26       ` Dmitry A. Kazakov
2012-11-19 22:19         ` Adam Beneschan
2012-11-20 10:12           ` Dmitry A. Kazakov
2012-11-20 21:51             ` Randy Brukardt
2012-11-21  8:24               ` Dmitry A. Kazakov
2012-11-21 22:19                 ` Randy Brukardt
2012-11-20 10:59     ` Brian Drummond
2012-11-19 20:22 ` ake.ragnar.dahlgren
2012-11-20 11:16   ` Brian Drummond
2012-11-19 20:52 ` ake.ragnar.dahlgren
2012-11-19 21:56   ` Dmitry A. Kazakov
2012-11-22  9:49     ` ake.ragnar.dahlgren
2012-11-19 22:13   ` sbelmont700
2012-11-19 23:59 ` Randy Brukardt
2012-11-20  0:05   ` Randy Brukardt
2012-11-20  1:00     ` Adam Beneschan
2012-11-20 21:38       ` Randy Brukardt
2012-11-20 23:43         ` Adam Beneschan
2012-11-21 22:12           ` Randy Brukardt
2012-11-22  1:59             ` Adam Beneschan
2012-11-29  2:43               ` Randy Brukardt
2012-11-20  0:52   ` Adam Beneschan
2012-11-20 21:34     ` Randy Brukardt
2012-11-20 11:28   ` Brian Drummond
2012-11-20 14:27     ` Georg Bauhaus
2012-11-20 15:52     ` Adam Beneschan
2012-11-22  9:47 ` ake.ragnar.dahlgren
2012-11-22 10:25   ` Dmitry A. Kazakov
2012-12-02 20:42     ` ake.ragnar.dahlgren
2012-12-03 11:21       ` Dmitry A. Kazakov
2012-12-03 20:21         ` ake.ragnar.dahlgren [this message]
2012-12-03 22:15           ` Dmitry A. Kazakov
2012-12-25 21:51           ` Gustaf Thorslund
2012-12-26 18:11             ` ake.ragnar.dahlgren
2012-11-22 12:13   ` Brian Drummond
2012-12-03 16:17     ` ake.ragnar.dahlgren
2012-12-03 21:56       ` Brian Drummond
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox