Re: [Vala] getting started with vala and glade
- From: Al Thomas <astavale yahoo co uk>
- To: Luc Chante <luc chante gmail com>, "vala-list gnome org" <vala-list gnome org>
- Subject: Re: [Vala] getting started with vala and glade
- Date: Mon, 27 Jun 2016 15:27:57 +0000 (UTC)
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.xmlvalac
--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]