Question

I am trying to get GMenu and composite templates to work together.

gtk-mumble.vala

using GLib;
using Gtk;

namespace GtkMumble {

[GtkTemplate (ui = "/net/antiochus/gtk-mumble/gtk_mumble.ui")]
public class MainWindow : Gtk.ApplicationWindow {
    public MainWindow (Gtk.Application app) {
        Object (application: app, title: "gtk-mumble");
        var about_action = new SimpleAction ("about", null);
        about_action.activate.connect (this.about_cb);
        this.add_action (about_action);
        this.show ();
    }

    void about_cb (SimpleAction simple, Variant? parameter) {
        print ("This does nothing.  It is only a demonstration.\n");
    }

    [GtkCallback]
    public void on_destroy () 
    {
        application.quit ();
    }
}

public class Application : Gtk.Application {

    public Application () {
        Object (application_id: "net.antiochus.gtk-mumble");
    }

    protected override void activate () {
        assert(this is Gtk.Application);
        new MainWindow (this);
    }

    protected override void startup () {
        base.startup ();

        var menu = new GLib.Menu ();
        menu.append ("About", "win.about");
        menu.append ("Quit", "app.quit");
        this.app_menu = menu;

        var quit_action = new SimpleAction ("quit", null);
        quit_action.activate.connect (this.quit);
        this.add_action (quit_action);
    }
}

int main (string[] args) {
    return new Application ().run (args);
}

}

gtk_mumble.ui

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.0 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <template class="GtkMumbleMainWindow" parent="GtkApplicationWindow">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">gtk-mumble</property>
    <property name="default_width">499</property>
    <property name="default_height">399</property>
    <signal name="destroy" handler="on_destroy" swapped="no"/>
    <child>
      <object class="GtkButton" id="connect_button">
        <property name="label" translatable="yes">Connect</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <signal name="clicked" handler="on_clicked" swapped="no"/>
      </object>
    </child>
  </template>
</interface>

resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/net/antiochus/gtk-mumble">
    <file compressed="true" preprocess="xml-stripblanks">gtk_mumble.ui</file>
  </gresource>
</gresources>

Build commands:

glib-compile-resources resources.xml --target=resources.c --sourcedir=. --c-name gtk_mumble --generate-source
valac -o gtk-mumble gtk-mumble.vala resources.c --target-glib=2.40 --pkg gtk+-3.0 --pkg gee-0.8 --gresources resources.xml

The code causes a failed assertion at runtime:

(gtk_mumble:19708): Gtk-CRITICAL **: gtk_application_get_menubar: assertion 'GTK_IS_APPLICATION (application)' failed

The GMenu does not work, but the rest of the UI does (e.g. ui template is loaded, the button is there and signals work).

When I remove all the [Gtk..] attributes the GMenu works and the assertion does not fail, but of course the rest of the UI does not work anymore.

Edit: As a workaround I am creating a Gtk.Box with a template and add this box to the main window. That works, but I would still like to know why the direct approach does not.

The versions I am using are:

  • Vala 0.22.1 (I also tried with 0.23.3)
  • GLib 2.40.0
  • Gtk+ 3.12.0
Was it helpful?

Solution 2

After a lot trial and error I found the problem.

In the ui file there is a directive that sets the window to visible:

<property name="visible">True</property>

If I remove that directive and instead add an explicit show (); to the contstructor of the window class the assertion no longer fires and the GMenu is working with the template :).

OTHER TIPS

Ok it's very weird, but changing the application-id to something else works. In particular, it seems that hyphens are not graceful, try with gtkmumble instead of gtk-mumble. Then you need to do add_window (new MainWindow (this));. These two steps should be enough to get you started.

Try keeping the application alive:

var app = new Application ();
return app.run (args);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top