ruby-****@sourc*****
ruby-****@sourc*****
2012年 1月 17日 (火) 03:02:10 JST
------------------------- REMOTE_ADDR = 184.145.90.60 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3AFileChooser ------------------------- @@ -96,63 +96,74 @@ #!/usr/bin/env ruby require 'gtk2' - - def folder_changed(c_dir, c_file) - dir = c_dir.filename - c_file.current_folder = dir + + # It is imperative to understand what is happening in "folder_changed" + # First note, that both "folder" (dir) and "file" buttons are passed + # into the method. This is because we want the two buttons to be related. + # The first "folder" button should provide the directory from which + # the second button should by default pick a file. + + def folder_changed(choo_dir, choo_file) + dir = choo_dir.filename + choo_file.current_folder = dir end - def file_changed(c_file, lab) - file = c_file.filename + + def file_changed(choo_file, lab) + file = choo_file.filename file = "" if file == nil lab.text = file end - window = Gtk::Window.new(Gtk::Window::TOPLEVEL) - window.set_title "File Chooser Buttons" + window = Gtk::Window.new("File Chooser Buttons") window.border_width = 10 - window.signal_connect('delete_event') { Gtk.main_quit } - + window.signal_connect('destroy') { Gtk.main_quit } + label = Gtk::Label.new chooser_dir = Gtk::FileChooserButton.new( "Choose a Folder", Gtk::FileChooser::ACTION_SELECT_FOLDER) chooser_file = Gtk::FileChooserButton.new( "Choose a File", Gtk::FileChooser::ACTION_OPEN) - + # Let's add an extra widget (a button) to the {{ dialog }} - extra_button = Gtk::Button.new("Extra button") - extra_button.signal_connect("clicked") do - puts "extra button is clicked" - end + extra_button1 = Gtk::Button.new("Extra button") + extra_button2 = Gtk::Button.new("Extra button #2") + chooser_file.extra_widget = extra_button1 + chooser_dir.extra_widget = extra_button2 + chooser_dir.signal_connect('selection_changed') do |w| - # {{ w }} = {{ FileChooserDialog }} - w.extra_widget = extra_button + # puts "In chooser_dir(): #{w.class}=#{w}" # <<< w=Gtk::FileChooserButton folder_changed(w, chooser_file) end chooser_file.signal_connect('selection_changed') do |w| + # puts "In chooser_file(): #{w.class}=#{w}" # <<< w=Gtk::FileChooserButton file_changed(chooser_file, label) + end + extra_button1.signal_connect("clicked") do + puts "extra button #1 clicked" + end + extra_button2.signal_connect("clicked") do + puts "extra button #2 clicked" end + chooser_dir.current_folder = GLib.home_dir - # setting a folder on file button has no effect - # chooser_file.current_folder = GLib.home_dir - + # chooser_file.current_folder = "/tmp" # <--- will have no effect, since callback overrides it + # chooser_file.filename = "/home/iwk/wk/a_cfile.c" # <--- would work if file existed + filter1 = Gtk::FileFilter.new filter2 = Gtk::FileFilter.new - filter1.name = "Image Files" filter2.name = "All Files" - filter1.add_pattern('*.png') filter1.add_pattern('*.jpg') filter1.add_pattern('*.gif') filter2.add_pattern('*') - + chooser_file.add_filter(filter2) # 1st added will be the default chooser_file.add_filter(filter1) - chooser_file.add_filter(filter2) - + vbox = Gtk::VBox.new(true, 5) vbox.pack_start_defaults(chooser_dir) vbox.pack_start_defaults(chooser_file) vbox.pack_start_defaults(label) - + window.add(vbox) window.show_all Gtk.main @@ -682,7 +693,19 @@ nil, Gtk::FileChooser::ACTION_SAVE, nil, - [Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL]) + [Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_ACCEPT], + [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL]) chooser.do_overwrite_confirmation = true chooser.signal_connect("confirm-overwrite") { |fc| retval = nil