Question

I'm following a tutorial on using glade and vala, which can be found here

I've followed the instructions and confirmed that my code is the same as the example.

The xml from Vala is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="on_window1_destroy" swapped="no"/>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkEntry" id="entry1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="invisible_char">•</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_button1_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface> 

The code that I wrote is as follows:

using Gtk;

/* When button click signal received */
public void on_button1_clicked (Button source) {
    /* change button label to clicked! */
    source.label = "Clicked!";
    stderr.printf ("Clicked! --> ");
}

/* when window close signal received */
public void on_window1_destroy (Window source) {
    Gtk.main_quit();
}

int main (string[] args) {
    Gtk.init (ref args);

    var builder = new Builder ();

    /* getting the glade file */
    builder.add_from_file ("sample.ui");
    builder.connect_signals (null);

    var window = builder.get_object ("window1") as Window;
    var entry = builder.get_object ("entry1") as Entry;
    var button = builder.get_object ("button1") as Button;

    /* that's another way to do something when signal received */
    button.clicked.connect (() => {
        stderr.printf ("%s\n", entry.get_text ());
    });
    window.show_all ();
    Gtk.main ();

    return 0;
}

When I run the compiled vala, I get the window (as per the example):

enter image description here

So that's all good. But when I try and click on the text field, I get a Gtk-CRITICAL error message in the console:

(example:16109): Gdk-CRITICAL **: gdk_device_get_n_axes: assertion 'gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD' failed

I don't get focus on the field and as such I cannot type anything into the field. I'm not sure why this is occurring. I've reviewed what I've done and I cannot see where I've gone wrong.

Btw, when I compile I've noticed the following warnings:

$ valac --pkg gtk+-3.0 --pkg gmodule-2.0 example.vala
example.vala:22.5-22.39: warning: unhandled error `GLib.Error'
    builder.add_from_file ("sample.ui");
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.vala.c: In function ‘main’:
example.vala.c:141:2: warning: ‘g_type_init’ is deprecated (declared at /usr/include/glib-2.0/gobject/gtype.h:669) [-Wdeprecated-declarations]
  g_type_init ();
  ^
Compilation succeeded - 1 warning(s)
Was it helpful?

Solution

You need to set the can-focus property of the text entry to True to be able to focus it (although I don't get the run-time error with your code).

If you are using a recent version of GTK (3.10) you might want to read about template/composite widgets as this saves messing about with Gtk.Builder directly, you just put a couple of annotations and vala sets everything up for you.

In GTK3 the expand & fill child properties are deprecated in favour of the expand, hexpand, vexpand, halign & valign widget properties which offer more flexibility in the distribution and use of extra space.

The vala warning about the unhandled error is because the code doesn't catch errors such as file not found or markup errors in the ui definition returned builder.add_from_file.

Don't worry too much about the second warning, vala is generating code for an older version of glib than the one you have installed. Add --target-glib 2.36 to the vala command line to fix it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top