[ruby-gnome2-doc-cvs] [Hiki] delete - tut-gtk2-helloworld-details

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2003年 8月 16日 (土) 02:57:23 JST


-------------------------
REMOTE_ADDR = 217.117.54.155
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/?tut-gtk2-helloworld-details
-------------------------
= Ruby/GTK2 Hello World (commented)

Now that we just saw theory of signals and events, we are ready to look in details in the "Hello World"
program.

First of all, it initializes Ruby/GTK2.  This code is required for all Ruby/GTK2 programs.

  #!/usr/bin/env ruby
  =begin
    helloworld.rb - Ruby/GTK first sample script.
  
    Copyright (c) 2002,2003 Ruby-GNOME2 Project Team
    This program is licenced under the same licence as Ruby-GNOME2.
  
    $Id: helloworld.rb,v 1.4 2003/02/01 16:46:22 mutoh Exp $
  =end
  
  require 'gtk2'
  Gtk.init

Here we create a button named "Hello World".
Gtk::Button#new creates a new GTK button and sets its label to the String parameter we provide.
Currently, the button is not part of a GTK window, and is not yet visible.

  button = Gtk::Button.new("Hello World")

The following code will prints "Hello World" to the terminal each time the button is pushed.

This is done by connecting a block code which puts our message to the "clicked" signal of the button. 

  button.signal_connect("clicked") {
    puts "Hello World"
  }

Now it's time to create the window.  Like the button, the window is not yet visible on screen.

  window = Gtk::Window.new

We decide to connect several signal handlers to the window:

* "delete_event" will be issued when the window is killed by the window manager (usually when the user 
  closes it manually).  Note that we return a false value in this handler: it means that the processing 
  of this event is not yet finished.  GTK will therefore raise the "destroy" signal.
* "destroy" will be raised just after "delete_event".  
  In this handler, we close the application by calling Gtk#main_quit.

2 messages will be printed when the user will close the window: first of all "delete event occured", 
and then "destroy event occured".

  window.signal_connect("delete_event") {
    puts "delete event occurred"
    #true
    false
  }

  window.signal_connect("destroy") {
    puts "destroy event occurred"
    Gtk.main_quit
  }

We set here the border width of the window to 10 pixels.  What does it mean?

GTK windows are in fact containers.  A container is a widget which can host other widget(s) inside it.
Our window here will display a 10 pixels border around the contained widget.  

  window.border_width = 10

It's the moment to associate our button with our window.  This is done by packing the button inside the
window. 

  window.add(button)

The next step is to display our work on screen.  We need to display two widgets: the button and the window.

So we could wrote this:

  button.show
  window.show

But since the button is packed into the window, we can therefore call Gtk::Widget#show_all on the window.
This method will call Gtk::Widget#show on the window and on each contained widget (here there is only one 
packed widget: the button).

  window.show_all

Finally, we enter the main loop, by calling Gtk#main.  The program will thus sleep until a X Window event
is raised.  Notice that Gtk#main won't return to the caller.  

But the user will still be able to quit the program since we call Gtk#main_quit when the "destroy" signal
is received.  

  Gtk.main  






ruby-gnome2-cvs メーリングリストの案内
Back to archive index