Reputation: 6500
Im trying to position a GTK# dialog in the center of a window,also i would like to make the parent window non editable when this dialog appears.
I tried setting the parent
property and of the dialog and set it's position to center on parent
.(The parent windows position is always centered,the parent appers centered but a portion of it is below the taskbar)
What im i doing wrong?
UPDATE:
I tried setting the transcientfor property.
mywin d = new mywin();
d.Parent = this;
d.WindowPosition = WindowPosition.CenterOnParent;
d.TransientFor = this;
d.WidthRequest = 360;
d.HeightRequest =260;
d.Resizable = false;
d.Show ();
Also i get this error in Application Output when i do this
(myapp:5844): Gtk-WARNING **: Can't set a parent on a toplevel widget
(myapp:5844): Gdk-CRITICAL **: inner_clipboard_window_procedure: assertion 'success' failed
UPDATE: Calling the dialog from parent
protected void OnButton7Clicked (object sender, EventArgs e)
{
var mydiag = new mydiag( this );
if ( ( (Gtk.ResponseType) mydiag.Run() ) == Gtk.ResponseType.Ok ) {
// do whatever here...
}
}
Dialog code
using System;
namespace myproj
{
public partial class mydiag : Gtk.Dialog
{
public mydiag (Gtk.Window parent)
{
this.Build ();
this.Title = parent.Title + " more info";
this.Icon = parent.Icon;
this.Parent = parent;
this.TransientFor = parent;
this.SetPosition( Gtk.WindowPosition.CenterOnParent );
this.ShowAll();
}
}
}
Upvotes: 2
Views: 2662
Reputation: 12202
The problem is being reported here:
(myapp:5844): Gtk-WARNING **: Can't set a parent on a toplevel widget
Your mywin class is not a Gtk.Dialog, but probably a Gtk.Window. You should create your main window this way:
namespace Whatever {
public class MainWindow: Gtk.Window {
public MainWindow()
: base( Gtk.WindowType.Toplevel )
{
this.Title = "Gtk# App";
this.Build();
}
private void Build() {
// create your widgets
}
// more things...
}
Suppose now that you need to open a dialog for some function in your app.
namespace Whatever {
public class MainWindow: Gtk.Window {
// ...more things
private void OnWhatever() {
var dlg = new DlgMoreInfo( this );
if ( ( (Gtk.ResponseType) dlg.Run() ) == Gtk.ResponseType.Ok ) {
// do whatever here...
}
dlg.Destroy();
}
}
}
Finally, you need to create your dialog DlgMoreInfo centered, etc.
namespace Whatever {
public class DlgMoreInfo : Gtk.Dialog {
public DlgMoreInfo(Gtk.Window parent) {
this.Build();
this.Title = parent.Title + " more info";
this.Icon = parent.Icon;
this.TransientFor = parent;
this.SetPosition( Gtk.WindowPosition.CenterOnParent );
this.ShowAll();
}
private void Build() {
// Create widgets here...
// Buttons
this.AddButton( Gtk.Stock.Cancel, Gtk.ResponseType.Cancel );
this.AddButton( Gtk.Stock.Ok, Gtk.ResponseType.Ok );
this.DefaultResponse = Gtk.ResponseType.Ok;
}
}
}
You can only create dialogs which are childs of a top level window; never a window can be a child of another one.
Note that this code does not use the designer of Xamarin Studio. If you use the designer, then call HideAll() just before ShowAll(), if you need to use the widgets, or move the call to Build() after the call to SetPosition().
Hope this helps.
Upvotes: 3