Skip to content

Instantly share code, notes, and snippets.

@ozcan
Created January 16, 2014 17:11
Show Gist options
  • Save ozcan/8459013 to your computer and use it in GitHub Desktop.
Save ozcan/8459013 to your computer and use it in GitHub Desktop.
require 'gtk3'
class FileManager
INDEX = 0
COL_PATH, COL_DISPLAY_NAME, COL_IS_DIR, COL_PIXBUF = (0..3).to_a
def initialize
win = Gtk::Window.new
menu = Gtk::Menu.new
swin = Gtk::ScrolledWindow.new
menus = create_menubar
toolbuttons = create_toolbar
viewport = Gtk::Viewport.new(swin.hadjustment, swin.vadjustment)
vbox = Gtk::Box.new(:vertical, 2)
hbox = Gtk::Box.new(:horizontal, 3)
vbox.pack_start(menus, :expand => false, :fill => false, :padding => 2)
vbox.pack_start(toolbuttons, :expand => false, :fill => false, :padding => 2)
vbox.pack_start(swin, :expand => true, :fill => true, :padding => 1)
create_store()
fill_store()
iconview = Gtk::IconView.new(@store)
iconview.selection_mode = :multiple
iconview.text_column = COL_DISPLAY_NAME
iconview.pixbuf_column = COL_PIXBUF
swin.add(iconview)
# win settings
win.set_default_size(600, 600)
win.set_title("File Manager")
win.signal_connect('destroy'){Gtk.main_quit}
win.set_window_position(Gtk::Window::Position::CENTER)
win.add(vbox)
win.show_all
end
def create_store
@store = Gtk::ListStore.new(String, String, TrueClass, Gdk::Pixbuf)
end
def fill_store
@store.clear
icon_theme = Gtk::IconTheme.default
file_icon = icon_theme.load_icon("gnome-fs-regular", 48, Gtk::IconTheme::LookupFlags::FORCE_SVG)
dir_icon = icon_theme.load_icon("gnome-fs-directory", 48, Gtk::IconTheme::LookupFlags::FORCE_SVG)
Dir.glob(File.join("/", "*")).each do |path|
is_dir = FileTest.directory?(path)
iter = @store.append
iter[COL_DISPLAY_NAME] = GLib.filename_to_utf8(File.basename(path))
iter[COL_PATH] = path
iter[COL_IS_DIR] = is_dir
iter[COL_PIXBUF] = is_dir ? dir_icon : file_icon
end
end
def create_toolbar
toolbar = Gtk::Toolbar.new
# tool buttons
back_toolbut = Gtk::ToolButton.new(:stock_id => Gtk::Stock::GO_BACK)
next_toolbut = Gtk::ToolButton.new(:stock_id => Gtk::Stock::GO_FORWARD)
home_toolbut = Gtk::ToolButton.new(:stock_id => Gtk::Stock::HOME)
up_toolbut = Gtk::ToolButton.new(:stock_id => Gtk::Stock::GO_UP)
toolbar.insert(back_toolbut, 0)
toolbar.insert(next_toolbut, 1)
toolbar.insert(up_toolbut, 2)
toolbar.insert(home_toolbut, 3)
end
def create_menubar
menubar = Gtk::MenuBar.new
# menus
filemenu = Gtk::Menu.new
viewmenu = Gtk::Menu.new
helpmenu = Gtk::Menu.new
# file items
filem = Gtk::MenuItem.new("File")
newtab_item = Gtk::MenuItem.new("New Tab")
newwindow_item = Gtk::MenuItem.new("New Window")
closetab_item = Gtk::MenuItem.new("Close Tab")
closewindow_item = Gtk::MenuItem.new("Close Window")
properties_item = Gtk::MenuItem.new("Properties")
filem.set_submenu(filemenu)
filemenu.append(newtab_item)
filemenu.append(newwindow_item)
filemenu.append(closetab_item)
filemenu.append(closewindow_item)
filemenu.append(properties_item)
# view items
viewm = Gtk::MenuItem.new("View")
reload_item = Gtk::MenuItem.new("Reload")
viewm.set_submenu(viewmenu)
viewmenu.append(reload_item)
# help items
helpm = Gtk::MenuItem.new("Help")
projectwebsite_item = Gtk::MenuItem.new("Project Web Site")
about_item = Gtk::MenuItem.new("About")
helpm.set_submenu(helpmenu)
helpmenu.append(projectwebsite_item)
helpmenu.append(about_item)
menubar.append(filem)
menubar.append(viewm)
menubar.append(helpm)
return menubar
end
app = FileManager.new
Gtk.main
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment