Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 27 Nov 2014, 00:34
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
SOLVED Vala: modify the state of widgets in a gtkbuilder gui
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
disciple

Joined: 20 May 2006
Posts: 6458
Location: Auckland, New Zealand

PostPosted: Tue 03 Apr 2012, 08:19    Post_subject:  SOLVED Vala: modify the state of widgets in a gtkbuilder gui
Sub_title: How do I refer to a widget?
 

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:
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:
<?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>

_________________
DEATH TO SPREADSHEETS
- - -
Classic Puppy quotes
- - -
Beware the demented serfers!

Edited_time_total
Back to top
View user's profile Send_private_message 
Mobeus


Joined: 26 Aug 2010
Posts: 94

PostPosted: Sun 22 Apr 2012, 01:08    Post_subject:  

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:

/* 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
Back to top
View user's profile Send_private_message 
disciple

Joined: 20 May 2006
Posts: 6458
Location: Auckland, New Zealand

PostPosted: Sun 22 Apr 2012, 03:49    Post_subject:  

Well, I figured out that I can do this inside main:
Code:
        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.

_________________
DEATH TO SPREADSHEETS
- - -
Classic Puppy quotes
- - -
Beware the demented serfers!
Back to top
View user's profile Send_private_message 
Mobeus


Joined: 26 Aug 2010
Posts: 94

PostPosted: Sun 22 Apr 2012, 08:23    Post_subject:  

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
Back to top
View user's profile Send_private_message 
disciple

Joined: 20 May 2006
Posts: 6458
Location: Auckland, New Zealand

PostPosted: Mon 23 Apr 2012, 02:13    Post_subject:  

Ah, does "object" = "class"? Or am I mixing up my terminology?
_________________
DEATH TO SPREADSHEETS
- - -
Classic Puppy quotes
- - -
Beware the demented serfers!
Back to top
View user's profile Send_private_message 
Mobeus


Joined: 26 Aug 2010
Posts: 94

PostPosted: Mon 23 Apr 2012, 14:18    Post_subject:  

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
Back to top
View user's profile Send_private_message 
disciple

Joined: 20 May 2006
Posts: 6458
Location: Auckland, New Zealand

PostPosted: Sat 28 Apr 2012, 03:30    Post_subject:  

Ah, cool, you can convert genie code to vala with e.g.
Code:
valac --pkg gtk+-2.0 --pkg gmodule-2.0 --dump-tree sample1.vala sample1.gs

And this achieves what I want:
Code:
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;
}

_________________
DEATH TO SPREADSHEETS
- - -
Classic Puppy quotes
- - -
Beware the demented serfers!
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0737s ][ Queries: 11 (0.0039s) ][ GZIP on ]