gtkd-developers/GtkD

How to extend GtkWidget/GtkContainer ?

Closed this issue · 5 comments

In the constructor I need to call super but since it is an abstract class it will make gtk crash.

super(cast(GtkWidget*)g_object_newv(getType(), 0, null), true);

Do you have a more complete example to try?

Here is one with GTK 4 (GTK 4 encourages the use of Widget as base class)

import gtk.Widget;
import gtk.WindowHandle;
import gtk.CenterBox;
import gtk.PopoverMenuBar;
import gtk.Box;
import gtk.WindowControls;
import gtk.Label;
import gtk.Application;
import gtk.BinLayout;

public class MenuTitleBar : Widget {
    import gobject.c.functions : g_object_newv;

    private BinLayout binLayout;

    this(Application app) {
        super(cast(GtkWidget*)g_object_newv(getType(), 0, null), true);

        auto titleBar = new WindowHandle();
        auto centerBox = new CenterBox();
        titleBar.addCssClass("titlebar");
        centerBox.setCenterWidget(new Label("Hexacode"));

        auto startControlsAndMenu = new Box(Orientation.HORIZONTAL, 0);
        startControlsAndMenu.append(new WindowControls(PackType.START));
        auto menuBar = new PopoverMenuBar(app.getMenubar());
        startControlsAndMenu.append(menuBar);
        centerBox.setStartWidget(startControlsAndMenu);
        centerBox.setEndWidget(new WindowControls(PackType.END));
        titleBar.setChild(centerBox);
        binLayout = new BinLayout();
        setLayoutManager(binLayout);
        titleBar.setParent(this);
    }
}

Add this to your class:

import gtkd.Implement;

mixin ImplementClass!GtkWidget;

The ImplementClass template takes care of the boilerplate needed to register your new class with GObject type system, and also makes sure that the functions you override are called by Glib/GTK.

Thanks, maybe add an example in doc ?