3 import Gtk from "gtk-sharp"
4 import Gdk from "gdk-sharp"
5 import Glade from "glade-sharp"
8 public static final phile = """stuffbrowser.glade"""
10 //Unfortunately, Gtk does not provide a builtin "IsSupported" function for images.
11 public static final supportedExtensions = ( ".png", ".jpeg", ".jpg", ".gif", ".tga", ".bmp", ".ico", ".tiff" )
14 //Connect event handlers and mainStuff, aboutDialog, etc, to their respective methods/handlers.
15 for widget in ("mainStuff", "aboutDialog", "directorySelector"):
16 gxml = Glade.XML(phile, widget, null)
17 gxml.Autoconnect(self)
19 def OnStuffClose(sender as object, e as DeleteEventArgs):
20 """The user is leaving the application via the Big Red 'X'"""
23 def OnStuffQuit(sender as object, e as EventArgs):
24 """The user is leaving the application via File-->Quit"""
27 def OnAboutActivate(sender as object, e as EventArgs):
28 """The user is trying to view the about dialog"""
31 def OnAboutDelete(sender as object, e as DeleteEventArgs):
35 def OnAboutResponse(sender as object, e as ResponseArgs):
36 if e.ResponseId == ResponseType.Close:
39 def OnOpenDirectory(sender as object, e as EventArgs):
41 directorySelector.FileList.Sensitive = false
42 directorySelector.Show()
44 def OnDirectoryDelete(sender as object, e as DeleteEventArgs):
46 directorySelector.Hide() //Don't worry, the "Destroy" event will kill it.
48 def OnDirectoryResponse(sender as object, args as ResponseArgs):
49 directorySelector.Hide()
51 if args.ResponseId == ResponseType.Ok and directorySelector.Filename.Length != 0:
52 LoadDirectory(directorySelector.Filename)
54 private def LoadDirectory(path as string):
55 //Create a new store that has two columns; the first is a Pixbuf (image) the second, a string.
56 store = ListStore( (Gdk.Pixbuf, string) )
57 stuffThumbs.Model = store
58 cell = CellRendererPixbuf()
59 column = TreeViewColumn()
60 column.PackStart(cell, false)
62 //Bind the element in column 0 of the store to the pixbuf property in this column's CellRendererPixbuf!
63 column.AddAttribute(cell, "pixbuf", 0)
66 //Detach the old column.
67 if stuffThumbs.GetColumn(0) is not null:
68 stuffThumbs.RemoveColumn(stuffThumbs.GetColumn(0))
70 //Finally, attach the new column so it will be updated in real time.
71 stuffThumbs.AppendColumn(column)
72 files = Directory.GetFiles(path)
73 validFiles = e for e as string in files if System.IO.FileInfo(e).Extension in StuffBrowser.supportedExtensions
75 for i as int, file as string in enumerate(validFiles):
76 //The using construct ensures that the program's memory size does not inflate wildly out of control.
77 //Having these "image" floating around afterwards ensures lots of memory consuming havoc until garbaged collected!
79 // If we couldn't read a file pass over it.
81 using image = Gdk.Pixbuf(file):
82 store.AppendValues( (image.ScaleSimple(128, 128, InterpType.Bilinear), file ) )
84 print " Couldn't read file ${file} "
86 print "Indexing start: ${DateTime.Now}"
87 //Update the treeview in real time by starting an asynchronous operation.
88 parse.BeginInvoke( { print "Indexing complete: ${DateTime.Now}" } ) //Spin off a new thread; fill the progress bar.
90 def OnPictureActivated(sender as object, args as RowActivatedArgs):
91 //Grabs the TreeIter object that represents the currently selected node -- icky stuff with "ref" keyword. ;(
93 stuffThumbs.Model.GetIter(x, args.Path)
95 //In the second column we've stored the filename that represents the thumbnail image in the treeview.
96 //Grab this filename and then use it to inform the user what file he's viewing, as well as loading it.
97 image = stuffThumbs.Model.GetValue(x, 1) as string
98 stuffImage.Pixbuf = Gdk.Pixbuf(image)
99 imageName.Text = Path.GetFileName(image)
101 //All of the widgets we want Autoconnect to hook up to Glade-created widgets in stuffBrowser.glade
102 [Glade.Widget] aboutDialog as Gtk.Dialog
103 [Glade.Widget] mainStuff as Gtk.Window
104 [Glade.Widget] directorySelector as Gtk.FileSelection
105 [Glade.Widget] stuffImage as Gtk.Image
106 [Glade.Widget] stuffThumbs as Gtk.TreeView
107 [Glade.Widget] imageName as Gtk.Label
109 //Run the application.