ruby-****@sourc*****
ruby-****@sourc*****
2012年 11月 19日 (月) 13:10:10 JST
------------------------- REMOTE_ADDR = 184.145.95.170 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-tb ------------------------- @@ -301,3 +301,162 @@ {{br}} == Dragging And Dropping Tool Bars + + +There are two ways you would want to move toolbars around. One is to detach a toolbar from the window, another is to place it to a different place (usually the edge) inside the window. We have two example programs showing both of these two techniques. It would be hard, though perhaps not impossible, to allow one and the same toolbar to be either detached from or dragged to a different location in the window. + +{{image_left("detaching-toolbar-n-handlebox-s1.png")}} +{{image_right("dnd-toolbar-colage-w-frame-ph.png")}} + +The image on the left here shows from the main window detached toolbar, whereas the image on the right shows you the same toolbar dragged to a different location within the window. + +((*detaching-toolbar-w-HandleBox.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 in Gtk::HandleBox") + window.resizable = true + window.border_width = 10 + window.signal_connect('destroy') { Gtk.main_quit } + window.set_size_request(650, -1) + + entry = Gtk::Entry.new + toolbar = Gtk::Toolbar.new + create_toolbar(toolbar, entry) + + # Create handle box wrapper + # ----------------------------------------------------------------------- + handle = Gtk::HandleBox.new + + # Add a shadow to the handle box, set the handle position + # on the left and set the snap edge to the top of the widget. + handle.shadow_type = Gtk::SHADOW_IN + handle.handle_position = Gtk::POS_TOP # or ... Gtk::POS_LEFT + handle.snap_edge = Gtk::POS_TOP # or ... Gtk::POS_LEFT + + #toolbar.set_size_request(600, -1) # -- Important, otherwise only arrow + # item box would be moved out! + + handle.signal_connect('child-detached') do |w, child| + puts "dettached: child=#{child.class}" + child.set_size_request(80, 450) + child.orientation = Gtk::Orientation::VERTICAL + end + handle.signal_connect('child-attached') do |w, child| + child.set_size_request(600, -1) + child.orientation = Gtk::Orientation::HORIZONTAL + puts "attached: child=#{child.class}" + end + + handle.add(toolbar) + + vbox = Gtk::VBox.new(false, 5) + vbox.pack_start_defaults(handle) + # ----------------------------------------------------------------------- + vbox.pack_start_defaults(entry) + + window.add(vbox) + window.show_all + Gtk.main + +{{br}}