help-smalltalk
[Top][All Lists]
Advanced

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

[Help-smalltalk] Counter Tutorial with Gtk and GNU Smalltalk


From: Gwenaël Casaccio
Subject: [Help-smalltalk] Counter Tutorial with Gtk and GNU Smalltalk
Date: Wed, 20 Apr 2011 12:09:50 +0200

Hi,

The counter tutorial is a simple window with two buttons and a label;
we will see how to add widgets in the main window and how to handle the
Gtk events.

" We still need to load the Gtk-Tools package "

 PackageLoader fileInPackage: 'GtkTools'.

" Counter class "

Object subclass: Counter [

    | counter |

    counter: anInteger [
        <category: 'accessing'>

" the setter "

        counter := anInteger
    ]

    counterString [
        <category: 'accessing'>

" return the counter value as a string "

        ^ counter asString
    ]


    increment [
        <category: 'increment'>

" increment the counter and signal that the value as changed "

        counter := counter + 1.
        self changed: #counterString
    ]

    decrement [
        <category: 'decrement'>

" decrement the counter and signal that the value as changed "

        counter := counter - 1.
        self changed: #counterString
    ]

]

    | b1 b2 counter label frame window |

" The window "
    window := GtkTools GtkMainWindow new.

" The counter with an initial value set to 0 "
    counter := Counter new
                    counter: 0;
                    yourself.

" The layout where widgets will be displayed "
    frame := GTK GtkFixed new.

" A new button "
    b1 := GTK GtkButton labeled: '+'.

" Send the increment message to counter when the user click
  on the button "
    b1 onClick: #increment to: counter.

" Add the button to the frame "
    frame at: address@hidden put: b1.

" A new button "
    b2 := GTK GtkButton labeled: '-'.

" Send the decrement message to counter when the user click
  on the button "
    b2 onClick: #decrement to: counter.

" Add the button to the frame "
    frame at: address@hidden put: b2.

" The label "
    label := GTK GtkLabel new: '0'.

" Update the label when counter update its value "
    label refreshFrom: #counterString for: counter.

    frame at: address@hidden put: label.

    window
        centralWidget: frame;
        title: 'simple';
        statusMessage: 'Simple GTK Application';

" Set the application size "
        resize: address@hidden;

" Set the application icon "
        icon: (GTK.GdkPixbuf fromFile: 'visualgst.png');
        showAll.

    counter increment.

    GTK Gtk main


Cheers,
Gwen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]