[Gtk-sharp-list] Problem when desensitizing button

Alexandros Frantzis alfius at freemail.gr
Fri Jun 17 11:57:38 EDT 2005


On Fri, 2005-06-17 at 08:50 -0400, Dan Winship wrote:
> On Fri, 2005-06-17 at 13:26 +0300, Alexandros Frantzis wrote:
> > Hello,
> > 
> > after clicking on a Gtk.Button, I desensitize it so that I can't click
> > it again until an (asynchronous) operation finishes. The problem is that
> > when the operation finishes and I make the button sensitive again, the
> > button can't be clicked immediately! I must first move the mouse cursor
> > outside the button and then in again before I am able to click.
> > 
> > Is there way around this?
> 
> No, this is an old gtk bug
> (http://bugzilla.gnome.org/show_bug.cgi?id=56070). It's not easy to fix
> the way that gtk currently works.
> 

After I playing around a bit, I have found a quick and dirty way to make
the button act as expected: hide and immediately show the button after
making it sensitive. I don't know the amount of overhead this incurs but
I think its small enough for normal buttons and usage. At least I can
have correct button behaviour until the bug gets fixed in gtk+ (if
ever).

The whole thing can be done manually right after setting
button.Sensitive to true, or in a StateChanged handler as in the example
below. 

I hope this is useful,
Alexandros

---- Code Start ----
using System;
using Gtk;

class ButtonTest
{
	Window win;
	Button button;
	
	public static void Main(string[] args)
	{
		Application.Init();
		new ButtonTest();
		Application.Run();	
	}

	public ButtonTest()
	{
		win = new Window("Button Test");
		win.DeleteEvent += OnWindowDeleteEvent;
		
		button=new Button("Click me");
		button.Clicked += OnButtonClicked;
		button.StateChanged += OnButtonStateChanged;
		
		win.Add(button);
		win.ShowAll();
	}

	void OnButtonClicked(object o, EventArgs args)
	{
		button.Sensitive = false;
		Console.WriteLine("Click!");
		
		GLib.Timeout.Add(1000, OnTimeoutExpired);
	}
	
	bool OnTimeoutExpired()
	{
		button.Sensitive = true;
				
		return false;
	}
	
	void OnButtonStateChanged(object o, StateChangedArgs args)
	{
		StateType prevState = args.PreviousState;
		
		if (prevState == StateType.Insensitive
		    && button.State == StateType.Normal) {
			button.Visible=false;
			button.Visible=true;
		}
	}
	
	void OnWindowDeleteEvent(object o, EventArgs args)
	{
		Application.Quit();
	}

}
---- Code End ----



More information about the Gtk-sharp-list mailing list