[ruby-gnome2-doc-cvs] [Ruby-GNOME2 Project Website] update - tut-gtk2-dialog-custom

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2009年 1月 24日 (土) 03:00:44 JST


-------------------------
REMOTE_ADDR = 74.15.84.244
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-dialog-custom
-------------------------
@@ -194,3 +194,80 @@
        * filename(String) - Creates a new Gtk::Image displaying the file filename. If the file isn't found or can't be loaded, the resulting Gtk::Image will display a "broken image" icon. This function never returns nil, it always returns a valid Gtk::Image widget. If the file contains an animation, the image will contain an animation. If you need to detect failures to load the file, use Gdk::Pixbuf.new(filename) to load the file yourself, then create the Gtk::Image from the pixbuf. (Or for animations, use Gdk::PixbufAnimation.new(filenam. The storage type (Gtk::Image#storage_type) of the returned image is not defined, it will be whatever is appropriate for displaying the file.
        * Gdk::Pixbuf - Creates a new Gtk::Image from Gdk::Pixbuf. This function just creates an Gtk::Image. The Gtk::Image created will not react to state changes. Should you want that, you should use Gtk::IconSet.
        * Gdk::PixbufAnimation - Creates a Gtk::Image displaying the given animation. The Gtk::Image does not assume a reference to the animation; you still need to unref it if you own references. Gtk::Image will add its own reference rather than adopting yours.
+
+
+== Another Dialogue Example
+
+So far you have created only a simple message dialogues spawned when a user clicked a dialogue triggering widget from scratch. It is time to create a more elaborate dialogue. But this time it will be without the parent window. There are also a few new minor tricks you will get acquainted with about how to obtain system information by utilizing ((*Glib*)) utilities. Of course this information is not changed, but is merely printed on the screen if user clicks the OK button. This example illustrates the fact, that regardless of the complexity of the dialogue, the basic principles of how responses are handled remain the same. Note that the dialogue is implemented as modal, but could as well be nonmodal, however this would not be of any use since the dialogue itself is the application's top-level window.
+
+In our example we had only a single button to care about but to process multiple responses would be no different:
+
+ dialog.run do |response|
+   case response
+     when Gtk::Dialog::RESPONSE_REJECT
+       do_something()
+     when Gtk::Dialog::RESPONSE_ACCEPT
+       do_something_else()
+     else
+       do_nothing_since_dialog_was_cancelled()
+   end
+   dialog.destroy
+ end
+
+{{image_right("dialog-custom.png")}}
+{{br}}
+
+ #!/usr/bin/env ruby
+ require 'gtk2'
+
+ # Create a new message dialogue telling the user that 
+ # button was clicked.
+
+ dialog = Gtk::Dialog.new(
+     "Edit User Information",
+     nil,
+     Gtk::Dialog::MODAL,
+     [ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK ],
+     [ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ]
+ )
+ dialog.default_response = Gtk::Dialog::RESPONSE_OK
+ label1 = Gtk::Label.new("User Name")
+ label2 = Gtk::Label.new("Real Name")
+ label3 = Gtk::Label.new("Home Dir")
+ label4 = Gtk::Label.new("Host Name")
+  
+ user = Gtk::Entry.new
+ real = Gtk::Entry.new
+ home = Gtk::Entry.new
+ host = Gtk::Entry.new
+ user.text = (GLib::user_name)
+ real.text = (GLib::real_name)
+ home.text = (GLib::home_dir)
+ host.text = (GLib::host_name)
+  
+ table = Gtk::Table.new(4, 2, false)
+ table.attach_defaults(label1, 0, 1, 0, 1)
+ table.attach_defaults(label2, 0, 1, 1, 2)
+ table.attach_defaults(label3, 0, 1, 2, 3)
+ table.attach_defaults(label4, 0, 1, 3, 4) 
+ table.attach_defaults(user,   1, 2, 0, 1)
+ table.attach_defaults(real,   1, 2, 1, 2)
+ table.attach_defaults(home,   1, 2, 2, 3)
+ table.attach_defaults(host,   1, 2, 3, 4)
+ table.row_spacings = 5
+ table.column_spacings = 5
+ table.border_width = 10
+
+ dialog.vbox.add(table)
+ dialog.show_all
+  
+ # Run the dialog and output the data if user okays it
+ dialog.run do |response|
+   if response == Gtk::Dialog::RESPONSE_OK
+     print "User Name: %s\n" % [user.text]
+     print "Real Name: %s\n" % [real.text]
+     print "Home Directory: %s\n" % [home.text]
+     print "Host Name: %s\n" % [host.text]
+   end
+ end
+ dialog.destroy



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