[Gtk-sharp-list] Hover to click problem

Leo Spalteholz leo@thewoodpecker.ca
Fri, 17 Oct 2003 16:43:03 -0700

Hi everyone,
I'm trying to write an application that will click a button if the mouse=20
hovers over it for 2 seconds.
I have a Entered eventhandler on the button that sets a global variable to=
the button that the mouse is over and starts a timer. =A0When the timer=20
fires for the first time it checks if the button property is still set and=
if it is, fires that button's Click event. The Click eventhandler is=20
called properly but for some reason some of the code inside doesnt=20
actually take effect until I leave the button with the mouse or click. =20
=46or example something like a Console.WriteLine in the clicked eventhandle=
will execute but anything that changes things on the screen will not (like=
quitting the application).
This is the code I have:

using System;
using System.Threading;
using Gtk;
using GtkSharp;

class TimerState {
=A0 =A0 public Timer tmr;
=A0 =A0 public Button btnTemp;

class hovertest {
=A0 =A0 static TimerState s;
=A0 =A0 public static void Main() {
=A0 =A0 =A0 =A0 Application.Init();

=A0 =A0 =A0 =A0 Window winMain =3D new Window("HoverTest");

=A0 =A0 =A0 =A0 Button btnQuit =3D new Button("Quit");
=A0 =A0 =A0 =A0 btnQuit.Clicked +=3D new EventHandler(EvtBtnQuitClicked);
=A0 =A0 =A0 =A0 btnQuit.Entered +=3D new EventHandler(EvtBtnQuitEntered);
=A0 =A0 =A0 =A0 btnQuit.Left +=3D new EventHandler(EvtBtnQuitLeft);

=A0 =A0 =A0 =A0 s =3D new TimerState();
=A0 =A0 =A0 =A0 TimerCallback timerDelegate =3D new TimerCallback(CheckButt=

=A0 =A0 =A0 =A0 s.tmr =3D new Timer(timerDelegate, s ,50000, 123000);

=A0 =A0 =A0 =A0 winMain.Add(btnQuit);
=A0 =A0 =A0 =A0 winMain.ShowAll();
=A0 =A0 =A0 =A0 Application.Run();
=A0 =A0 }

=A0 =A0 private static void CheckButtonStatus(object state) {
=A0 =A0 =A0 =A0 TimerState scr =3D (TimerState)state;
=A0 =A0 =A0 =A0 // there is a button
=A0 =A0 =A0 =A0 Console.WriteLine("Timer hit");
=A0 =A0 =A0 =A0 if(scr.btnTemp !=3D null) {
=A0 =A0 =A0 =A0 =A0 =A0 Console.WriteLine("Call button Click event");
=A0 =A0 =A0 =A0 =A0 =A0 scr.btnTemp.Click();

=A0 =A0 =A0 =A0 =A0 =A0 Console.WriteLine("Shouldn't we have quit by now?");
=A0 =A0 =A0 =A0 =A0 =A0 scr.btnTemp =3D null;
=A0 =A0 =A0 =A0 }
=A0 =A0 }

=A0 =A0 static void EvtBtnQuitClicked(object obj, EventArgs ea) {
=A0 =A0 =A0 =A0 Application.Quit();
=A0 =A0 =A0 =A0 Console.WriteLine("Application.Quit has been called");
=A0 =A0 }

=A0 =A0 static void EvtBtnQuitEntered(object obj, EventArgs ea) {
=A0 =A0 =A0 =A0 Console.WriteLine("Button entered");
=A0 =A0 =A0 =A0 s.btnTemp =3D (Button)(obj);
=A0 =A0 =A0 =A0 s.tmr.Change(2000, 10000);
=A0 =A0 }

=A0 =A0 static void EvtBtnQuitLeft(object obj, EventArgs ea) {
=A0 =A0 =A0 =A0 s.btnTemp =3D null;
=A0 =A0 =A0 =A0 Console.WriteLine("Finally we quit after firing the Left ev=
=A0 =A0 }

The output from running the above code and hovering over the quit button is=
leo@pherthyl:~$ mono hovertest.exe
Button entered
Timer hit
Call button Click event
Application.Quit has been called
Shouldn't we have quit by now?
=46inally we quit after firing the Left event

Any clues why this is? =A0If I don't use the timer and call the Click event=
right from the Entered eventhandler it works fine but of course is useless=
for what I'm trying to do.
Is this a sane way of doing hover-to-click or is there a better way?

I use Gtk# 0.11 and Mono 0.26 under Debian Linux.

Thank you,