lf_araujo
lf_araujo

Reputation: 2063

Alternatives to vala lambdas in Genie

The lack of lambdas in Genie poses some problems to the workflow. There is a particular circumstance that I am unable to circumvent.

My aim in this particular exercise is to create a Notebook in which there is a Button that upon click will take the user to the next page of the same Notebook. There are four pages and the button on the last one will take the user back to the first (similar to the one here).

It seems that in vala, that could be easily done with lambdas. I tried the approach suggested here of using variables shared within a class, but the problem is that although I manage to access the variable in the function called (callback? Still not completely sure about the particular jargon) by the button.click.connect, it is still not recognized as a Notebook.

Here is my approach:

[indent=4]
uses Gtk

class TestWindow : Window
    notebook:Gtk.Notebook

    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        var notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var label3 = new Gtk.Label("Page three")
        var label4 = new Gtk.Label("Page four")

        var child1 = new Button.with_label ("Go to next page")
        child1.clicked += def ()
            notebook.set_current_page(2)
        var child2 = new Button.with_label ("Go to next page")
        child2.clicked += def ()
            notebook.set_current_page(3)
        var child3 = new Button.with_label ("Go to next page")
        child3.clicked += def ()
            notebook.set_current_page(4)
        var child4 = new Button.with_label ("Go to first page")
        child4.clicked += def ()
            notebook.set_current_page(1)

        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)
        notebook.append_page(child3, label3)
        notebook.append_page(child4, label4)

        // Now building the grid
        var grid = new Grid()
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button 2")

        // Attaching all elements into the grid
        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
        add(grid)


init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()

The error I get at runtime:

(gtkcontainerswithgrid:23039): Gtk-CRITICAL **: gtk_notebook_append_page: assertion 'GTK_IS_NOTEBOOK (notebook)' failed

So I suppose that in notebook.set_current_page(2) notebook is not inheriting the properties of a Notebook.

I would appreciate some pointers about how to circumvent this problem, because I run out of ideas. I've tried creating functions to substitute the deprecated syntax += def(), and I stumbled in similar problems.

Upvotes: 3

Views: 228

Answers (1)

txasatonga
txasatonga

Reputation: 419

uses Gtk
class TestWindow : Window
    notebook:Gtk.Notebook
    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var label3 = new Gtk.Label("Page three")
        var label4 = new Gtk.Label("Page four")

        var child1 = new Button.with_label ("Go to next page")
        child1.clicked.connect (childclicked1)
        var child2 = new Button.with_label ("Go to next page")
        child2.clicked.connect (childclicked2)
        var child3 = new Button.with_label ("Go to next page")
        child3.clicked.connect (childclicked3)
        var child4 = new Button.with_label ("Go to first page")
        child4.clicked.connect (childclicked4)


        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)
        notebook.append_page(child3, label3)
        notebook.append_page(child4, label4)

        // Now building the grid
        var grid = new Grid()
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button 2")

        // Attaching all elements into the grid
        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
        add(grid)

    def childclicked1()
        notebook.set_current_page(1)

    def childclicked2()
        notebook.set_current_page(2)

    def childclicked3()
        notebook.set_current_page(3)

    def childclicked4()
        notebook.set_current_page(0)

init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()

I think the only alternative is this. Is not supported.

Upvotes: 3

Related Questions