ruby-****@sourc*****
ruby-****@sourc*****
2012年 11月 9日 (金) 12:56:09 JST
------------------------- REMOTE_ADDR = 184.145.95.170 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-tb ------------------------- @@ -73,7 +73,6 @@ Gtk.main - New toolbars are created with Gtk::Toolbar.new. This creates an empty toolbar. We created our toolbar before calling our own method create_toolbar(toolbar, entry), which populates the toolbar. Gtk::Toolbar class provides a number of properties for customizing how it appears and interacts with the user including the orientation, button style, and the ability to give access to items that do not fit in the toolbar. The one property managing instance method worth mentioning here is the Gtk::Toolbar#toolbar_style, because it requires you to be aware of the appropriate constants (((<GtkToolbarStyle|Gtk::Toolbar#GtkToolbarStyle>))). Also note the Gtk::Toolbar#show_arrow method @@ -81,9 +80,303 @@ tb.show_arrow = true tb.toolbar_style = Gtk::Toolbar::Style::BOTH - Signal handling callbacks are straight forward, except that we are using methods defined in the Gtk::Editable module such as 'cut_clipboard', ... 'select_region'. - :Gtk::Editable: The Gtk::Editable is a module for widgets for editing text, such as Gtk::Entry. The editable class contains methods for generically manipulating an editable widget, a large number of action signals used for key bindings, and several signals that an application can connect to modify the behaviour of a widget. + +Another thing worth mentioning is that you should avid using the deprecated Gtk::Toolbar instance methods which use the deprecated Gtk::Tooltips class. Instead you should be using the((*tooltip_**))instance methods defined in Gtk::Widget. + + +Note also, that you can use only one Gtk::SeparatorToolItem object on the toolbar. + + tb.insert(2, separator) + +{{br}} + +{{image_right("toolb-L-menus-s1.png")}} + +It is also possible to add menus and sub-menu's as toolbar items, as well as you can implement tear offs. However, notice that menu items emit a different signal((*'activate'*))than tool-menu items((*'clicked'*))when clicked. Following is expanded example program including the menu toolbar item with the language sub-menu, you already saw in previous section. In addition to the toolbar menu item, we also included two Gtk::ToolButton objects with user supplied icon image. The image can be passed to the Gtk::ToolButton constructor as an((*image file*))or as a((*pixbuf.*))Our 'toolbar-item-menu.rb' example shows both of these two ways of supplying image objects to the toolbar item constructor. Note also our custom the menu toolbar item called 'My Stuff' includes the tear-off menu item: + +((*toolbar-item-menu.rb*)) + + #!/usr/bin/env ruby + require 'gtk2' + + # Create a toolbar with Cut, Copy, Paste and Select All + # toolbar items. + def create_toolbar(tb, ent) + cut = Gtk::ToolButton.new(Gtk::Stock::CUT) + copy = Gtk::ToolButton.new(Gtk::Stock::COPY) + paste = Gtk::ToolButton.new(Gtk::Stock::PASTE) + selectall = Gtk::ToolButton.new(Gtk::Stock::SELECT_ALL) + separator = Gtk::SeparatorToolItem.new + + mytb_menu = Gtk::MenuToolButton.new(Gtk::Stock::PREFERENCES) + # To be sure file exists use more elaborate check making the bixbuf + pixbuf = get_pixbuf_if_file_exists("gnu-baby-32x32.jpg") + my_stuff = Gtk::ToolButton.new(icon_widget=Gtk::Image.new(pixbuf), label="My Stuff") + # or if you are sure you you really have the image file: + my_stuff1 = Gtk::ToolButton.new(icon_widget=Gtk::Image.new("gnu-head-42x42.jpg"), label="My Stuff-1") + + # use Gtk::Widget#tooltip_text= instead of deprecated Gtk::Toolbar + # methods utilizing deprecated Gtk::Tooltips class! + mytb_menu.tooltip_text = "Demonstrate that menus with submenus work." + cut.tooltip_text = "Cut saves the selected text in the clipboard,\n" + + "and removes it from the editable widget," + copy.tooltip_text = "Copy saves the selected text in the clipboard." + paste.tooltip_text = "Paste retrieves last text saved in the clipboard\n" + + "and places it at the cursor position in the edit field." + selectall.tooltip_text = "Select all the text in the edit field." + + tb.show_arrow = true + tb.toolbar_style = Gtk::Toolbar::Style::BOTH + + tb.insert(0, mytb_menu) + tb.insert(1, my_stuff) + tb.insert(2, cut) + tb.insert(3, copy) + tb.insert(4, paste) + tb.insert(5, separator) + tb.insert(6, selectall) + tb.insert(7, my_stuff1) + + cut.signal_connect('clicked') { ent.cut_clipboard; p "Cut" } + copy.signal_connect('clicked') { ent.copy_clipboard; p "Copy" } + paste.signal_connect('clicked') { ent.paste_clipboard; p "Paste" } + # Select all of the text in the editable (Gtk::Editable#select_region) + selectall.signal_connect('clicked') { ent.select_region(0, -1); p "Sel. All" } + my_stuff.signal_connect('clicked') { p "My Stuff selected." } + my_stuff1.signal_connect('clicked') { p "My Stuff-1 selected." } + + menutearoff = Gtk::TearoffMenuItem.new + testi1 = Gtk::MenuItem.new("Test Item #1") + testi2 = Gtk::MenuItem.new("Test Item #2") + testi3 = Gtk::MenuItem.new("Test Item #3") + langi = Gtk::MenuItem.new("Languages") + testi1.signal_connect('activate') { |w| puts "w=#{w.class}:Test Item-1 selected" } + testi2.signal_connect('activate') { |w| puts "w=#{w.class}:Test Item-2 selected" } + testi3.signal_connect('activate') { |w| puts "w=#{w.class}:Test Item-3 selected" } + + # Create Test Menu + testmenu = Gtk::Menu.new + testmenu.append(menutearoff) + testmenu.append(testi1) + testmenu.append(testi2) + testmenu.append(testi3) + testmenu.append(langi) + + langmenu = Gtk::Menu.new + langi.submenu = langmenu + + english = Gtk::MenuItem.new("English") + french = Gtk::MenuItem.new("French") + german = Gtk::MenuItem.new("German") + russian = Gtk::MenuItem.new("Russian") + italian = Gtk::MenuItem.new("Italian") + langmenu.append(english) + langmenu.append(french) + langmenu.append(german) + langmenu.append(russian) + langmenu.append(italian) + + english.signal_connect('activate') { |w| puts "w=#{w.class}:English selected" } + french.signal_connect('activate') { |w| puts "w=#{w.class}:French selected" } + german.signal_connect('activate') { |w| puts "w=#{w.class}:German selected" } + russian.signal_connect('activate') { |w| puts "w=#{w.class}:Russian selected" } + italian.signal_connect('activate') { |w| puts "w=#{w.class}:Italian selected" } + + testmenu.show_all + mytb_menu.menu = testmenu + + end + + # Turn image file into pixbuf, and return nil in case of file error. + def get_pixbuf_if_file_exists(file) + begin + pixbuf = Gdk::Pixbuf.new(file) + rescue GLib::FileError => err + print "I/O ERROR (%s): %s\n" % [err.class, err] + pixbuf = nil + end + pixbuf + end + + window = Gtk::Window.new("Toolbars w/menus") + window.resizable = true + window.border_width = 10 + window.signal_connect('destroy') { Gtk.main_quit } + window.set_size_request(350, -1) + + entry = Gtk::Entry.new + toolbar = Gtk::Toolbar.new + create_toolbar(toolbar, entry) + + vbox = Gtk::VBox.new(false, 5) + vbox.pack_start_defaults(toolbar) + vbox.pack_start_defaults(entry) + + window.add(vbox) + window.show_all + Gtk.main + + + +{{br}} + +{{image_left("toolbars-orientation.png")}} + +You can set the toolbar orientation by setting the Gtk::Toolbar.orientation= to one of the constants defined in Gtk::Orientation. By default it is set to Gtk::ORIENTATION_VERTICAL. All you need to do is include the following line in your code: + + toolbar.orientation = Gtk::ORIENTATION_VERTICAL + +To place your toolbar on the side vertically, requires a different screen layout organization. In our example program we use the following layout: + + toolbar.orientation = Gtk::ORIENTATION_VERTICAL + hbox = Gtk::HBox.new(false, 5) + toolbar.set_size_request(50, 350) + toolframe = Gtk::Frame.new("Tools") + toolframe.add(toolbar) + hbox.pack_start_defaults(toolframe) + + right_vbox = Gtk::VBox.new(false, 5) + emtyl = Gtk::Label.new + right_vbox.pack_start(child=entry, expand=false, fill=false, padding=5) + right_vbox.pack_start(child=emtyl, expand=true, fill=true, padding=5) + hbox.pack_start_defaults(right_vbox) + + window.add(hbox) + +Following is the entire program: + + +((*toolbars-orientation.rb*)) + + #!/usr/bin/env ruby + require 'gtk2' + + # Create a toolbar with Cut, Copy, Paste and Select All + # toolbar items. + def create_toolbar(tb, ent) + cut = Gtk::ToolButton.new(Gtk::Stock::CUT) + copy = Gtk::ToolButton.new(Gtk::Stock::COPY) + paste = Gtk::ToolButton.new(Gtk::Stock::PASTE) + selectall = Gtk::ToolButton.new(Gtk::Stock::SELECT_ALL) + separator = Gtk::SeparatorToolItem.new + mytb_menu = Gtk::MenuToolButton.new(Gtk::Stock::PREFERENCES) + + # To be sure file exists use more elaborate check making the bixbuf + pixbuf = get_pixbuf_if_file_exists("gnu-baby-32x32.jpg") + my_stuff = Gtk::ToolButton.new(icon_widget=Gtk::Image.new(pixbuf), label="My Stuff") + # or if you are sure you you really have the image file: + my_stuff1 = Gtk::ToolButton.new(icon_widget=Gtk::Image.new("gnu-head-42x42.jpg"), label="My Stuff-1") + + # use Gtk::Widget#tooltip_text= instead of deprecated Gtk::Toolbar + # methods utilizing deprecated Gtk::Tooltips class! + mytb_menu.tooltip_text = "Demonstrate that menus with submenus work." + cut.tooltip_text = "Cut saves the selected text in the clipboard,\n" + + "and removes it from the editable widget," + copy.tooltip_text = "Copy saves the selected text in the clipboard." + paste.tooltip_text = "Paste retrieves last text saved in the clipboard\n" + + "and places it at the cursor position in the edit field." + selectall.tooltip_text = "Select all the text in the edit field." + + tb.show_arrow = true + tb.toolbar_style = Gtk::Toolbar::Style::BOTH + + tb.insert(0, mytb_menu) + tb.insert(1, my_stuff) + tb.insert(2, cut) + tb.insert(3, copy) + tb.insert(4, paste) + tb.insert(5, separator) + tb.insert(6, selectall) + tb.insert(7, my_stuff1) + + cut.signal_connect('clicked') { ent.cut_clipboard; p "Cut" } + copy.signal_connect('clicked') { ent.copy_clipboard; p "Copy" } + paste.signal_connect('clicked') { ent.paste_clipboard; p "Paste" } + # Select all of the text in the editable (Gtk::Editable#select_region) + selectall.signal_connect('clicked') { ent.select_region(0, -1); p "Sel. All" } + my_stuff.signal_connect('clicked') { p "My Stuff selected." } + my_stuff1.signal_connect('clicked') { p "My Stuff-1 selected." } + + menutearoff = Gtk::TearoffMenuItem.new + testi1 = Gtk::MenuItem.new("Test Item #1") + testi2 = Gtk::MenuItem.new("Test Item #2") + testi3 = Gtk::MenuItem.new("Test Item #3") + langi = Gtk::MenuItem.new("Languages") + testi1.signal_connect('activate') { |w| puts "w=#{w.class}:Test Item-1 selected" } + testi2.signal_connect('activate') { |w| puts "w=#{w.class}:Test Item-2 selected" } + testi3.signal_connect('activate') { |w| puts "w=#{w.class}:Test Item-3 selected" } + + # Create Test Menu + testmenu = Gtk::Menu.new + testmenu.append(menutearoff) + testmenu.append(testi1) + testmenu.append(testi2) + testmenu.append(testi3) + testmenu.append(langi) + + langmenu = Gtk::Menu.new + langi.submenu = langmenu + + english = Gtk::MenuItem.new("English") + french = Gtk::MenuItem.new("French") + german = Gtk::MenuItem.new("German") + russian = Gtk::MenuItem.new("Russian") + italian = Gtk::MenuItem.new("Italian") + langmenu.append(english) + langmenu.append(french) + langmenu.append(german) + langmenu.append(russian) + langmenu.append(italian) + + english.signal_connect('activate') { |w| puts "w=#{w.class}:English selected" } + french.signal_connect('activate') { |w| puts "w=#{w.class}:French selected" } + german.signal_connect('activate') { |w| puts "w=#{w.class}:German selected" } + russian.signal_connect('activate') { |w| puts "w=#{w.class}:Russian selected" } + italian.signal_connect('activate') { |w| puts "w=#{w.class}:Italian selected" } + + testmenu.show_all + mytb_menu.menu = testmenu + end + + # Turn image file into pixbuf, and return nil in case of file error. + def get_pixbuf_if_file_exists(file) + begin + pixbuf = Gdk::Pixbuf.new(file) + rescue GLib::FileError => err + print "I/O ERROR (%s): %s\n" % [err.class, err] + pixbuf = nil + end + pixbuf + end + + window = Gtk::Window.new("Toolbars (orientation)") + window.resizable = true + window.border_width = 10 + window.signal_connect('destroy') { Gtk.main_quit } + window.set_size_request(250, -1) + + entry = Gtk::Entry.new + entry.set_size_request(100, -1) + toolbar = Gtk::Toolbar.new + + create_toolbar(toolbar, entry) + + toolbar.orientation = Gtk::ORIENTATION_VERTICAL + hbox = Gtk::HBox.new(false, 5) + toolbar.set_size_request(50, 350) + toolframe = Gtk::Frame.new("Tools") + toolframe.add(toolbar) + hbox.pack_start_defaults(toolframe) + + right_vbox = Gtk::VBox.new(false, 5) + emtyl = Gtk::Label.new + right_vbox.pack_start(child=entry, expand=false, fill=false, padding=5) + right_vbox.pack_start(child=emtyl, expand=true, fill=true, padding=5) + hbox.pack_start_defaults(right_vbox) + + window.add(hbox) + window.show_all + Gtk.main