comp.lang.ada
 help / color / mirror / Atom feed
* Mouse Clicks in a Drawing Area
@ 2014-02-04  6:46 ldries46
  2014-02-04  8:52 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 6+ messages in thread
From: ldries46 @ 2014-02-04  6:46 UTC (permalink / raw)


I do want to draw something at a certain point in a Drawing_Area. That point 
should be derived from a mouse click.
To do this I have created a handler "On Button_Press".
I already have a draw event connected that works. I have concluded this from 
the facts I can see and from the breakpoint I positioned inside the 
"On_Area_Draw" Handler. The mouse click handler is created analog to the 
Draw handler.
After creating the Drawing Area "Sudoku_Area" the handlers are initiated as 
follows:

Add_Events(Sudoku_Area, BUTTON_PRESS_MASK);
CR := Create(Get_Window (Sudoku_Area));
Event_Cb.Connect (Sudoku_Area, "draw",
                  Event_Cb.To_Marshaller 
(On_Area_Draw'Unrestricted_Access));
Event_Cb.Connect (Sudoku_Area, "button_press_event",
                  Event_Cb.To_Marshaller 
(On_Button_Press'Unrestricted_Access));

where:

package Event_Cb is new Gtk.Handlers.Return_Callback
  (Gtk_Drawing_Area_Record, Boolean);

The compilation of this is faultless.

I do use the following handlers:

function On_Area_Draw (Object : access Gtk_Drawing_Area_Record'Class; 
Context : Cairo.Cairo_Context) return boolean;

function On_Button_Press (Object : access Gtk_Drawing_Area_Record'Class; 
event : Gdk_Event) return boolean;

Both program build faultless.
I put a breakpoint inside the On_Button_Press function at a point where it 
should always be reached.
Running the program and clicking within the drawn area that breakpoint is 
never reached and the program runs without reporting an error.
I am using Gtk 3.4 and GPS.
What do I do wrong? 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mouse Clicks in a Drawing Area
  2014-02-04  6:46 Mouse Clicks in a Drawing Area ldries46
@ 2014-02-04  8:52 ` Dmitry A. Kazakov
  2014-02-04 10:08   ` ldries46
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2014-02-04  8:52 UTC (permalink / raw)


On Tue, 4 Feb 2014 07:46:51 +0100, ldries46 wrote:

> I do want to draw something at a certain point in a Drawing_Area. That point 
> should be derived from a mouse click.
> To do this I have created a handler "On Button_Press".

[...] 

> Both program build faultless.
> I put a breakpoint inside the On_Button_Press function at a point where it 
> should always be reached.
> Running the program and clicking within the drawn area that breakpoint is 
> never reached and the program runs without reporting an error.
> I am using Gtk 3.4 and GPS.
> What do I do wrong?

By default the button-press-event and the button-release-event signals are
filtered out for a widget. See:

https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-button-press-event

You must enable their propagation in order to be able to handle them. Use
Set_Events for this purpose on the widget with the appropriate event mask.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mouse Clicks in a Drawing Area
  2014-02-04  8:52 ` Dmitry A. Kazakov
@ 2014-02-04 10:08   ` ldries46
  2014-02-04 10:43     ` dontspam365
  2014-02-04 10:44     ` Dmitry A. Kazakov
  0 siblings, 2 replies; 6+ messages in thread
From: ldries46 @ 2014-02-04 10:08 UTC (permalink / raw)


I now het changed the Add_Events into Set_Events (within the first code 
listing but the result is the same, nothing happens.
I had done it that way before but

http://askubuntu.com/questions/157290/how-to-draw-on-mouse-click-in-gtk-drawingarea-using-pygi

suggested the add_events routine. That code is now

Set_Events(Sudoku_Area, BUTTON_PRESS_MASK);
CR := Create(Get_Window (Sudoku_Area));
Event_Cb.Connect (Sudoku_Area, "draw",
                  Event_Cb.To_Marshaller 
(On_Area_Draw'Unrestricted_Access));
Event_Cb.Connect (Sudoku_Area, "button_press_event",
                  Event_Cb.To_Marshaller 
(On_Button_Press'Unrestricted_Access));

The problem stays the same as.

Just for extra information the code od On_Button_Press:

function On_Button_Press (Object : access Gtk_Drawing_Area_Record'Class; 
event : Gdk_Event) return boolean is
   pragma Unreferenced (Object);
begin
   ev := event.Button;  -- line where the breakpoint is invoked
   nb := ev.Button;
   if nb = 1 then
      x := ev.X;
      y := ev.Y;
   end if;
   return nb = 1;
end On_Button_Press;


"Dmitry A. Kazakov"  schreef in bericht 
news:qtjn56kj6yh4$.1iqlazgzyni86.dlg@40tude.net...

On Tue, 4 Feb 2014 07:46:51 +0100, ldries46 wrote:

> I do want to draw something at a certain point in a Drawing_Area. That 
> point
> should be derived from a mouse click.
> To do this I have created a handler "On Button_Press".

[...]

> Both program build faultless.
> I put a breakpoint inside the On_Button_Press function at a point where it
> should always be reached.
> Running the program and clicking within the drawn area that breakpoint is
> never reached and the program runs without reporting an error.
> I am using Gtk 3.4 and GPS.
> What do I do wrong?

By default the button-press-event and the button-release-event signals are
filtered out for a widget. See:

https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-button-press-event

You must enable their propagation in order to be able to handle them. Use
Set_Events for this purpose on the widget with the appropriate event mask.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mouse Clicks in a Drawing Area
  2014-02-04 10:08   ` ldries46
@ 2014-02-04 10:43     ` dontspam365
  2014-02-04 10:44     ` Dmitry A. Kazakov
  1 sibling, 0 replies; 6+ messages in thread
From: dontspam365 @ 2014-02-04 10:43 UTC (permalink / raw)


>function On_Button_Press (Object : access Gtk_Drawing_Area_Record'Class; 
>event : Gdk_Event) return boolean is 

I have some code that does this and seems to work. I am using:
Add_Events
        (Window1.The_Area,
         Button_Press_Mask or Button_Release_Mask or Button_Motion_Mask or Key_Press_Mask);

and 

function On_The_Button_Press_Event
  (Object : access Gtk_Widget_Record'Class;
   Params : Gtk.Arguments.Gtk_Args)

I see a difference between your attempt and mine, and this last line.

Frank

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mouse Clicks in a Drawing Area
  2014-02-04 10:08   ` ldries46
  2014-02-04 10:43     ` dontspam365
@ 2014-02-04 10:44     ` Dmitry A. Kazakov
  2014-02-04 14:01       ` ldries46
  1 sibling, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2014-02-04 10:44 UTC (permalink / raw)


On Tue, 4 Feb 2014 11:08:30 +0100, ldries46 wrote:

> I now het changed the Add_Events into Set_Events (within the first code 
> listing but the result is the same, nothing happens.
> I had done it that way before but
> 
> http://askubuntu.com/questions/157290/how-to-draw-on-mouse-click-in-gtk-drawingarea-using-pygi
> 
> suggested the add_events routine. That code is now
> 
> Set_Events(Sudoku_Area, BUTTON_PRESS_MASK);
> CR := Create(Get_Window (Sudoku_Area));
> Event_Cb.Connect (Sudoku_Area, "draw",
>                   Event_Cb.To_Marshaller 
> (On_Area_Draw'Unrestricted_Access));
> Event_Cb.Connect (Sudoku_Area, "button_press_event",
>                   Event_Cb.To_Marshaller 
> (On_Button_Press'Unrestricted_Access));
> 
> The problem stays the same as.
> 
Typically for mouse tracking you would do:

Sudoku_Area.Set_Can_Focus (True);
Sudoku_Area.Set_Events
(  Exposure_Mask
or Leave_Notify_Mask
or Button_Press_Mask
or Button_Release_Mask
or Pointer_Motion_Mask
or Pointer_Motion_Hint_Mask
);

> Just for extra information the code od On_Button_Press:
> 
> function On_Button_Press (Object : access Gtk_Drawing_Area_Record'Class; 
> event : Gdk_Event) return boolean is
>    pragma Unreferenced (Object);
> begin
>    ev := event.Button;  -- line where the breakpoint is invoked
>    nb := ev.Button;
>    if nb = 1 then
>       x := ev.X;
>       y := ev.Y;
>    end if;
>    return nb = 1;
> end On_Button_Press;

You would use Get_Button to determine which mouse button was pressed.

P.S. It is not a good idea to use global variables as you do. For this
reason Connect operations to attach callbacks have very limited use. In
most cases you should instantiate User_Callback or User_Return_Callback to
be able to pass an additional parameter to the handler.

P.P.S. You should not use debugger with GTK. It works very rarely if at
all. Do tracing instead. And you must always handle all exceptions within
signal handlers. Propagation out of a handler will certainly corrupt stack
and crush the program. Use Glib.Messages it is a great help.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mouse Clicks in a Drawing Area
  2014-02-04 10:44     ` Dmitry A. Kazakov
@ 2014-02-04 14:01       ` ldries46
  0 siblings, 0 replies; 6+ messages in thread
From: ldries46 @ 2014-02-04 14:01 UTC (permalink / raw)


Firstly this answer nor the one "dontspam365" did not work, but finally I 
found within Glade3 that I use to create the GUI visually under the tab 
"common" the entrance Structure a point where I could enable button _press 
and that worked.

"Dmitry A. Kazakov"  schreef in bericht 
news:17s5lkm60lq92.a7lidua6gycy$.dlg@40tude.net...

On Tue, 4 Feb 2014 11:08:30 +0100, ldries46 wrote:

> I now het changed the Add_Events into Set_Events (within the first code
> listing but the result is the same, nothing happens.
> I had done it that way before but
>
> http://askubuntu.com/questions/157290/how-to-draw-on-mouse-click-in-gtk-drawingarea-using-pygi
>
> suggested the add_events routine. That code is now
>
> Set_Events(Sudoku_Area, BUTTON_PRESS_MASK);
> CR := Create(Get_Window (Sudoku_Area));
> Event_Cb.Connect (Sudoku_Area, "draw",
>                   Event_Cb.To_Marshaller
> (On_Area_Draw'Unrestricted_Access));
> Event_Cb.Connect (Sudoku_Area, "button_press_event",
>                   Event_Cb.To_Marshaller
> (On_Button_Press'Unrestricted_Access));
>
> The problem stays the same as.
>
Typically for mouse tracking you would do:

Sudoku_Area.Set_Can_Focus (True);
Sudoku_Area.Set_Events
(  Exposure_Mask
or Leave_Notify_Mask
or Button_Press_Mask
or Button_Release_Mask
or Pointer_Motion_Mask
or Pointer_Motion_Hint_Mask
);

> Just for extra information the code od On_Button_Press:
>
> function On_Button_Press (Object : access Gtk_Drawing_Area_Record'Class;
> event : Gdk_Event) return boolean is
>    pragma Unreferenced (Object);
> begin
>    ev := event.Button;  -- line where the breakpoint is invoked
>    nb := ev.Button;
>    if nb = 1 then
>       x := ev.X;
>       y := ev.Y;
>    end if;
>    return nb = 1;
> end On_Button_Press;

You would use Get_Button to determine which mouse button was pressed.

P.S. It is not a good idea to use global variables as you do. For this
reason Connect operations to attach callbacks have very limited use. In
most cases you should instantiate User_Callback or User_Return_Callback to
be able to pass an additional parameter to the handler.

P.P.S. You should not use debugger with GTK. It works very rarely if at
all. Do tracing instead. And you must always handle all exceptions within
signal handlers. Propagation out of a handler will certainly corrupt stack
and crush the program. Use Glib.Messages it is a great help.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-02-04 14:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-04  6:46 Mouse Clicks in a Drawing Area ldries46
2014-02-04  8:52 ` Dmitry A. Kazakov
2014-02-04 10:08   ` ldries46
2014-02-04 10:43     ` dontspam365
2014-02-04 10:44     ` Dmitry A. Kazakov
2014-02-04 14:01       ` ldries46

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