Skip to content

Instantly share code, notes, and snippets.

@myokoym
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myokoym/9489619 to your computer and use it in GitHub Desktop.
Save myokoym/9489619 to your computer and use it in GitHub Desktop.
デスクトップアプリケーションを作る会@札幌 2014-03-16 http://atnd.org/events/48050 のサンプルアプリケーション『横に何枚でも並べられる画像ビューアー』です。引数に指定した画像ファイルを表示します。また、右クリックメニューから非表示にできます。事前に `gem install gtk2` してから実行してください。(3/14追記:OS XでFinder-XQuartz間のドラッグ&ドロップができないようなので、一部修正しました)
#!/usr/bin/env ruby
require "gtk2"
window = Gtk::Window.new
window.title = "Image Viewer sample"
window.set_default_size(640, 480)
window.signal_connect("destroy") do
Gtk.main_quit
end
scrolled_window = Gtk::ScrolledWindow.new
scrolled_window.set_policy(:automatic, :automatic)
window.add(scrolled_window)
hbox = Gtk::HBox.new(false, 8)
hbox.border_width = 8
scrolled_window.add_with_viewport(hbox)
menu = Gtk::Menu.new
menu_item = Gtk::ImageMenuItem.new(Gtk::Stock::DELETE)
menu_item.signal_connect("activate") do
hbox.remove(menu.attach_widget)
end
menu.append(menu_item)
menu.show_all
ARGV.each do |path_or_wildcard|
Dir.glob(File.expand_path(path_or_wildcard)) do |path|
begin
pixbuf = Gdk::Pixbuf.new(path)
rescue GLib::FileError, Gdk::PixbufError
$stderr.puts($!.message)
next
end
event_box = Gtk::EventBox.new
event_box.signal_connect("button-press-event") do |widget, event|
if event.kind_of?(Gdk::EventButton) and event.button == 3
menu.attach_widget = widget
menu.popup(nil, nil, event.button, event.time)
end
end
hbox.add(event_box)
image = Gtk::Image.new
image.pixbuf = pixbuf
event_box.add(image)
end
end
window.show_all
Gtk.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment