Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a custom GTK# Widget with its own controls?

Tags:

c#

mono

gtk#

I am trying to create a custom GTK Widget by subclassing Gtk.Bin. I am not using the Stetic GUI builder. This widget will contain several standard Gtk widgets (VBoxs, Labels, Buttons, etc).

public class MyWidget : Gtk.Bin
{
    public MyWidget : base ()
    {
        build ();
    }
    private void build ()
    {
        VBox vbox1 = new Vbox (true, 0);
        vbox1.PackStart (new Label ("MyWidget"), true, true, 0);
        this.Add (vbox1);
    }
}

Meanwhile when I add my custom widget to the main window, I don't see anything. The windows other controls show up, space is allocated for this custom widget. I expect to see the label "MyWidget" in its space, but nothing shows up. I step thru the code in the debugger and it all gets called but its a no show at runtime.

Any help would be appreciated.

like image 230
Ronaldo Nascimento Avatar asked Mar 04 '11 16:03

Ronaldo Nascimento


People also ask

How can I make my own GTK theme?

To create a GTK3 theme, developers can start with an empty file or they can use a pre-existing theme as a template. It may help beginners to start with a pre-existing theme. For instance, a theme can be copied to the user's home folder and then the developer can start editing the files.

How do I make my own gnome theme?

Enabling Custom GNOME Shell ThemesEnable the “User Themes” extension, as shown in the screenshot below. Be sure that the “Extensions” toggle is enabled at the top. Once you are finished with this step, close and relaunch the Tweaks app. Now, you will be able to change the GS theme from the “Appearance” tab.


2 Answers

need to override:

    protected override void OnSizeAllocated (Gdk.Rectangle allocation)
    {
        if (this.Child != null)
        {
            this.Child.Allocation = allocation;
        }
    }

    protected override void OnSizeRequested (ref Requisition requisition)
    {
        if (this.Child != null)
        {
            requisition = this.Child.SizeRequest ();
        }
    }
like image 95
Ronaldo Nascimento Avatar answered Sep 25 '22 18:09

Ronaldo Nascimento


Or probably more likely, ShowAll () as the last line in the build method after all children are packed in, unless you don't want some of them to be visible by default.

like image 44
mkestner Avatar answered Sep 24 '22 18:09

mkestner