Re: [Vala] getting started with vala and glade



Hi,

Just to reply to :
"...
- For setting properties on the parent in a constructor what is wrong with
   this.property_name = xyz;
..."
It is not wrong, but the behaviour is not exactly the same.

I get stuck on this before I realize the error.
When you use the constructed() method (to initialize ui by exemple), if you
use "this.something = some_arg", something isn't initialized in the
constructed() method, but it is when you use Object(something: some_arg).

Approximative generated C code for a class "Ns.ClassName" without Object()
call in the constructor :

NsClassName* ns_class_name_construct (GType object_type, type some_arg) {
    NsClassName * self = NULL;
    ...
    self = (NsClassName*) g_object_new (object_type, NULL);
    ...
    ns_class_name_set_something (self, some_arg);
    ...
    return self;
}

the constructed() method is called at the end of g_object_new(), so
ns_class_name_set_something() isn't called yet.

Same thing with a the use of Object() :

NsClassName* ns_class_name_construct (GType object_type, type some_arg) {
    NsClassName * self = NULL;
    ...
    self = (NsClassName*) g_object_new (object_type, "something", some_arg,
NULL);
    ...
    return self;
}

In this case, when the constructed() method is called something has the
value some_arg.

Regards,
Luc.

Le lun. 27 juin 2016 à 17:30, Al Thomas <astavale yahoo co uk> a écrit :


------------------------------
*From:* Luc Chante <luc chante gmail com>
*Sent:* Monday, 27 June 2016, 11:36
*Subject:* Re: [Vala] getting started with vala and glade


I think it would be a good start to use gtk templates.
So you can use your ui file and create the ApplicationWindow by it's
constructor (which takes an Application instance).

Join to this mail a really simple example using ui file and gtk templates.
Tested with msys 2.

This is a great start for a code sample on the wiki, maybe:
https://wiki.gnome.org/Projects/Vala/GTKTemplateSample

As a code sample I have a couple of doubts about the current coding style:

 - I think the main() method should be on its own, not buried in an object

 - For setting properties on the parent in a constructor what is wrong with
   this.property_name = xyz;
   instead of using Vala specific GObject plumbing like Object(
property_name: value );
  Although these low level GObject features exist they add additional
complexity when
  initially learning the language and are unnecessary in most cases

So a revised main.vala:

/**
 * Application
 */
public class MyApplication : Gtk.Application
{
    public MyApplication(string? app_id = null, ApplicationFlags flags =
ApplicationFlags.FLAGS_NONE)
    {
    if (app_id != null && Application.id_is_valid (app_id) == false) {
        error( "Application ID is not valid");
    }
    this.application_id = app_id;
    this.flags = flags;
    }

    public override void activate () {
        var window = new Template.ApplicationWindow (this);
        window.present ();
    }
}

/**
 * Application window
 */
[GtkTemplate(ui = "/template/window.ui")]
public class Template.ApplicationWindow : Gtk.ApplicationWindow
{
    [GtkChild]
    Gtk.Button test_button;

    public ApplicationWindow(Gtk.Application application)
    {
        this.application = application;
    }

    [GtkCallback]
    public void on_test_button_clicked(Gtk.Button button)
    {
        GLib.message("Button clicked !!!");
    }
}

/** Application entry point */
public static int main(string[] args)
{
    var app = new MyApplication ("my.application");
    return app.run (args);
}

Finally as a more generic way to compile use:

glib-compile-resources --sourcedir ./ --generate-source --target
resources.c template.gresource.xml
valac --pkg gtk+-3.0 --target-glib 2.38 --gresources
template.gresource.xml resources.c main.vala --output myapp

The Makefile is nice though.

Regards,

Al




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]