[Gtk-sharp-list] Gtk# - Glade#. Problems, a newbie is starting

Gustavo Ramos eureko@grmexico.com.mx
Wed, 20 Aug 2003 02:53:16 -0500


See answers below...

> class MainWindow
> 	{
> 		int retValLogin = -1;
> 		[Glade.Widget("wndMain")]
> 		Gtk.Window wndMain;
>   		static void Main()
> 			{
> 				Appliction.Init();
> 				Glade.XML gXML = new
> Glade.XML(null, "file.glade", "wndMain", null);
> 				gXML.Autoconnect(this);
> 				Application.Run();
> 			}
>
> 		public void on_mnuLogin_clicked(System.Object
> Sender, EventArgs args)
> 			{
> 				Login x = new Login(ref retValLogin);
> 				x.Show();
> 				Console.WriteLine("This is executed
> inmediatly after the Show method.");
> 			}
> 	}
>
> class Login()
>    {
> 		[Glade.Widget("wndLogin")]
> 		Gtk.Window wndLogin;
> 		int ret;
> 		public Login(ref int retval)
> 		{
> 			ret = retval;
> 		}
> 		public void Show()
> 		{
> 			Glade.XML gLogin = new Glade.XML(null,
> "file.glade", "wndLogin", null);
> 			gLogin.Autoconnect(this);
>
> 		}
> 		public void on_btnLogin_clicked(System.Object
> Sender, EventArgs args)
> 		{
> 			ret = 1;
> 			wndLogin.Destroy();
> 		}
> 		public void on_btnLogin_clicked(System.Object
> Sender, EventArgs args)
> 		{
> 			ret = 0;
> 			wndLogin.Destroy();
> 		}
>    }

> The problem of this code is that i must to give reference variables to
> return value to class MainWindow from Login class.
> I do this beacause when i call Method Show, the program "flow"
> does not go
> with the method. It means that execute the show
> method and return the flow control, and execute the next code in caller
> code.
> It Is this correct? it is "the" way to do this?

I would love to use wnd.ShowDialog() instead .Show(). That way, the control
would return only when the dialog has been closed, and you should be able to
access public members of the dialog. Somewhat like
public void on_mnuLogin_clicked(System.Object Sender, EventArgs args)
{
	Login x = new Login();
	x.ShowDialog();
	Console.WriteLine("The value on the Login window is {0}.",
		x.MyValue.ToString());
}
...but that isn't the case, I don't know if that is possible. I'll read
about it tomorrow.

> It Is this the only way to return values? beacause i must return
> the value  when the BtnLogin is clicked.

No, you can use events for dealing return values of non-modal windows. See
the
sample below (note that i am not very familiar with glade, gladelib and
friends, so, I've left the glade things almost as you has posted it). For
this to work, we need: a special EventArgs class that will return the value
we want to know (LoginEventArgs); a public delegate that "declares" the
signature of the event-consumer function (LoginEventHandler); a public event
inside the Login class (OnLogin); a client event-subscriber function
(OnLoginAttempt); and some event-publisher code (the code inside
on_btnLogin_clicked).

class App
{
	public static void Main()
	{
		Application.Init();
		// init and autoconnect things here

	}

	public void on_mnuLogin_clicked(System.Object Sender, EventArgs args)
	{
		Login x = new Login(0); // Default value = 0
		x.OnLogin += new LoginEventHandler(OnLoginAttempt);
		x.Show();
	}

	public void OnLoginAttempt(object sender, LoginEventArgs e)
	{
		Console.WriteLine("The value sent by the Login window is {0}.",
			e.ReturnValue.ToString());
	}
}

public class LoginEventArgs : EventArgs
{
	int retVal;
	public LoginEventArgs(int returnValue)
	{
		retVal = returnValue;
	}
	public int ReturnValue
	{
		get { return retVal; }
	}
}

public delegate LoginEventHandler(object sender, LoginEventArgs e);
class Login()
{
	[Glade.Widget("wndLogin")]
	Gtk.Window wndLogin;
	int ret;
	public Login(int defaultValue)
	{
		ret = defaultValue;
	}
	public void Show()
	{
		Glade.XML gLogin = new Glade.XML(null,
			"file.glade", "wndLogin", null);
		gLogin.Autoconnect(this);
	}
	public event LoginEventHandler OnLogin;
	public void on_btnLogin_clicked(System.Object Sender, EventArgs args)
	{
		ret = 1;
		if ( OnLogin != null )
		{
			LoginEventArgs lea = new LoginEventArgs(ret);
			OnLogin(lea);
		}
		wndLogin.Destroy();
	}

	// why this function is declared twice? Note that i changed slightly the
name
	public void on_btnLogin_clicked2(System.Object Sender, EventArgs args)
	{
		ret = 0;
		if ( OnLogin != null )
		{
			LoginEventArgs lea = new LoginEventArgs(ret);
			OnLogin(lea);
		}		wndLogin.Destroy();
	}
	public void on_btnCancel_clicked(object sender, EventArgs e)
	{
		wndLogin.Destroy();
	}
}

> It is the way to show windows creating an instance of Glade.XML?

I'm not sure


> What does Application.Init and Application.Run? (This is Off-Topic, but i
> dont know to)

Please post other message with that question, it will be easier to find
later.


> I'm doing to much questions? :P (do not response this)

Yes. Oops! It is already responded :-)


> And I have other problemas. When i do a window in Glade, with a NoteBook
> (control) inside (2 Panels) and
> I run this window in my applicacion I have some Warning error, and the
> window show without the NoteBook Control.

I had similar problems when creating all the controls manually, but luckily
solved that. I'm not sure how to fine-tune the properties assignment with
glade. My mistake was:
this.Add(textView) instead of this.scrollableWindow.Add(textView)


> The same problem with Default Button in a window, It give me some Warning
> Advice and no default button is set.

In glade don't set any widget as default. After autoconnecting, set the
desiredControl.CanDefault property to true, and later, do a call to
desiredControl.GrabDefault()


> An at last the same problem with Sensitive Property (set in glade
> interfase) in a button that is inside of a toolbar.

/// TODO: I have to learn at least two words about the Sensitive property
:-)


> Wath kind of error are this? are bugs? i must report them? someone have
> tested this thinks?

Please report the glade# issues using bugzilla.ximian.com, so the bug are
attacked when a developer think: What's next? (and it won't be forgiven).


>
> It will be very helpfull some information, (im thinking in kill
> myself for
> this problem :P ) And THANKS TO ALL.

I don't have a spoon nearby me for you :-)


A last word: Please DONT expect me to answer always. Today the answer is
just because yesterday I've learnt a lot on gtk#

Other last word: Please post just one question per message, in the simplest
form.

Best Regards,
Gustavo, Mexico