ruby-****@sourc*****
ruby-****@sourc*****
2012年 11月 19日 (月) 05:18:41 JST
------------------------- REMOTE_ADDR = 184.145.95.170 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-tb ------------------------- @@ -96,8 +96,19 @@ {{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: +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. +{{image_left("gnu-baby-32x32.jpg")}}{{image_left("gnu-head-42x42.jpg")}} For your convenience I have included the two images here on the left. Their names are 'gnu-baby-32x32.jpg' and 'gnu-head-42x42.jpg' respectively. You should copy these image files into the directory, where you plan to run our 'toolbar-item-menu.rb' program example. + +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. + + +{{image_right("toolb-tearoff.png")}} + +Note also our custom toolbar menu under 'Preferences' includes the tear-off menu item shown here on the image on the left. + +{{br}} + ((*toolbar-item-menu.rb*)) #!/usr/bin/env ruby @@ -248,138 +259,18 @@ window.add(hbox) -Following is the entire program: {{br}} -((*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 +=== Dragging And Dropping Tool Bars