SOLVED Vala: modify the state of widgets in a gtkbuilder gui

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

SOLVED Vala: modify the state of widgets in a gtkbuilder gui

#1 Post by disciple »

I'm trying to port a VB.NET program I maintain to Vala and Gtkbuilder.
My first problem is that I can't figure out how to refer to a widget if I want to do something to it. e.g. how would I modify the example code below, so that when you click one of the buttons and it is made insensitive, the other button is also made sensitive?

Code: Select all

using Gtk;

public void on_button1_clicked (Button source) {
    source.label = "Thank you!";
    source.sensitive = false;
}

public void on_button2_clicked (Button source) {
    source.label = "Thanks!";
    source.sensitive = false;
}

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

    try {
        var builder = new Builder ();
        builder.add_from_file ("sample.ui");
        builder.connect_signals (null);
        var window = builder.get_object ("window") as Window;
        window.show_all ();
        Gtk.main ();
    } catch (Error e) {
        stderr.printf ("Could not load UI: %s\n", e.message);
        return 1;
    } 

    return 0;
}

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <property name="border_width">10</property>
    <property name="title" translatable="yes">Sample Application</property>
    <property name="window_position">center</property>
    <property name="default_width">300</property>
    <property name="default_height">70</property>
    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkHBox" id="hbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="spacing">4</property>
        <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">Click me!</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
            <signal name="clicked" handler="on_button1_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label" translatable="yes">Me too!</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
            <signal name="clicked" handler="on_button2_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
Last edited by disciple on Sat 28 Apr 2012, 07:33, edited 1 time in total.
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#2 Post by Mobeus »

Hi disciple,

You may have solved this since it has been 3 weeks. In case not here is a example in genie code. Use builder.get_object() to get a reference.

Code: Select all

/* save as test.gs
 * compile with:
 * valac --pkg gtk+-2.0 --pkg gmodule-2.0 test.gs
 */

[indent=4]

uses Gtk

builder : Builder

def on_button1_clicked (source : Button) 
    source.label = "Thank you!"
    source.sensitive = false
    var btn2 = builder.get_object ("button2") as Button
    btn2.sensitive = true


def on_button2_clicked (source : Button)
    source.label = "Thanks!"
    source.sensitive = false
    var btn1 = builder.get_object ("button1") as Button
    btn1.sensitive = true


init

    Gtk.init (ref args)
    builder = new Builder ()
    try
        builder.add_from_file ("sample.ui")
    except ex : GLib.Error
        print ("%s", "Could not load sample.ui -- " + ex.message)
    builder.connect_signals (null)
    var window = builder.get_object ("window") as Window
    window.show_all ()
    Gtk.main ()
/root for the home team

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#3 Post by disciple »

Well, I figured out that I can do this inside main:

Code: Select all

        var button2 = builder.get_object ("button2") as Widget;
        button2.sensitive = false;
But I couldn't figure out how to refer to button2 from on_button1_clicked.
I was thinking I needed to make a class or something (as you can tell, I'm not much of a programmer). It'll have to wait to next weekend now.
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#4 Post by Mobeus »

I'm not much of a programmer either, it's just a hobby. For me the trick to make this work is a global builder object that can be used anywhere to get glade gui object refs. I suppose you could get the parent of the button object that is passed to the button callback and not have a global builder object, but I don't know how to do that.
/root for the home team

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#5 Post by disciple »

Ah, does "object" = "class"? Or am I mixing up my terminology?
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#6 Post by Mobeus »

Not being a professional programmer, this has always been my understanding of objects.

gtkbuilder is a class. When you create an instance of it, mybuilder = new Builder(), you have a Builder object, named mybuilder. So an object is just a unique instance of a class identified by a variable, and the variable/object has the same scope rules as any other variable.
/root for the home team

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#7 Post by disciple »

Ah, cool, you can convert genie code to vala with e.g.

Code: Select all

valac --pkg gtk+-2.0 --pkg gmodule-2.0 --dump-tree sample1.vala sample1.gs
And this achieves what I want:

Code: Select all

using Gtk;
public Builder builder;

public void on_button1_clicked (Button source) {
    source.label = "Thank you!";
    source.sensitive = false;
    Button button2 = builder.get_object ("button2") as Button;
    button2.sensitive = true;
}

public void on_button2_clicked (Button source) {
    source.label = "Thanks!";
    source.sensitive = false;
    Button button1 = builder.get_object ("button1") as Button;
    button1.sensitive = true;
}

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

    try {
        builder = new Builder ();
        builder.add_from_file ("sample.ui");
        builder.connect_signals (null);
        var window = builder.get_object ("window") as Window;
        window.show_all ();
        Gtk.main ();
    } catch (Error e) {
        stderr.printf ("Could not load UI: %s\n", e.message);
        return 1;
    } 

    return 0;
}
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

Post Reply