3

When I try to subclass a GTK IconView in Vala using Glade, I get a segmentation fault. Is this a bug in Vala, or am I just doing something wrong? This is using vala 0.42.3. Maybe this is related to how IconView doesn't have a base() constructor? (see: Chain up to 'Gtk.Box.new' not supported)

test.vala:

using Gtk;

public class IconViewSubclass : Gtk.IconView {
  public IconViewSubclass() {

  }
}

public static int main(string[] args) {
  Gtk.init(ref args);
  var builder = new Builder.from_file("test.glade");
  var window = builder.get_object("window") as Window;
  var iconViewSubclass = builder.get_object("iconViewSubclass") as IconViewSubclass;
  iconViewSubclass.set_pixbuf_column(0);
  iconViewSubclass.set_text_column(1);

  window.show_all();
  Gtk.main();
  return 0;
}

test.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkListStore" id="store">
    <columns>
      <!-- column-name pixbuf -->
      <column type="GdkPixbuf"/>
      <!-- column-name text -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkIconView" id="iconViewSubclass">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="margin">6</property>
        <property name="model">store</property>
      </object>
    </child>
  </object>
</interface>
$ valac --pkg gtk+-3.0 test.vala && ./test
Segmentation fault
user9889635
  • 123
  • 7

2 Answers2

4

It looks like you need to let the Gtk.Builder know IconViewSubclass exists using expose_object(). This allows the sub-type to be used in the Builder UI definition file. Here's an example that compiles and does not segfault:

test.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.18"/>
  <object class="GtkWindow" id="window">
    <property name="window-position">GTK_WIN_POS_CENTER</property>
    <property name="default-height">250</property>
    <property name="default-width">250</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="IconViewSubclass" id="iconViewSubclass">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="margin">6</property>
      </object>
    </child>
  </object>
</interface>

and test.vala:

using Gtk;

public class IconViewSubclass : Gtk.IconView {}

public static int main(string[] args) {
  Gtk.init(ref args);
  var builder = new Builder ();
  builder.expose_object ("IconViewSubclass", new IconViewSubclass ());
  try {
    builder.add_from_file ("test.ui");
  } catch (Error error) {
    print (@"$(error.message)");
  }
  var window = builder.get_object ("window") as Window;
  var iconViewSubclass = (IconViewSubclass)builder.get_object ("iconViewSubclass");
  iconViewSubclass.set_pixbuf_column (0);
  iconViewSubclass.set_text_column (1);

  window.show_all();
  Gtk.main();
  return 0;
}

You may want to look into using templates with Vala [GtkTemplate], [GtkChild] and [GtkCallback] attributes. The attributes will tell Vala to generate the boiler plate code for you.

AlThomas
  • 4,169
  • 12
  • 22
1

AIThomas' code worked great, however, in order to continue editing the UI file in Glade I had to add a catalog file, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<glade-catalog name="test" depends="gtk+">
  <glade-widget-classes>
    <glade-widget-class name="IconViewSubclass" generic-name="iconviewsubclass" title="IconViewSubclass" parent="GtkIconView"/>
  </glade-widget-classes>
  <glade-widget-group name="test" title="test">
    <glade-widget-class-ref name="IconViewSubclass"/>
  </glade-widget-group>
</glade-catalog>

I also discovered that the subclass name in Vala must correspond exactly with the widget name in the UI XML, i.e. changing IconViewSubclass to IconViewSubclass2 in test.vala will give you:

$ valac --pkg gtk+-3.0 test.vala && ./test
test.ui:14:1 Invalid object type 'IconViewSubclass'Segmentation fault

You can also use a namespace as part of the class name, i.e. namespace Foo { public class Bar : Baz } should work for a widget of type FooBar in the UI XML file.

user9889635
  • 123
  • 7