GtkSharp/Source/sample/GtkDemo/DemoIconView.cs

165 lines
4.1 KiB
C#
Raw Normal View History

using System;
using System.IO;
using Gtk;
#if GTK_SHARP_2_6
namespace GtkDemo
{
[Demo ("Icon View", "DemoIconView.cs")]
public class DemoIconView : Window
{
const int COL_PATH = 0;
const int COL_DISPLAY_NAME = 1;
const int COL_PIXBUF = 2;
const int COL_IS_DIRECTORY = 3;
DirectoryInfo parent = new DirectoryInfo ("/");
Gdk.Pixbuf dirIcon, fileIcon;
ListStore store;
ToolButton upButton;
public DemoIconView () : base ("Gtk.IconView demo")
{
SetDefaultSize (650, 400);
DeleteEvent += new DeleteEventHandler (OnWinDelete);
VBox vbox = new VBox (false, 0);
Add (vbox);
Toolbar toolbar = new Toolbar ();
vbox.PackStart (toolbar, false, false, 0);
upButton = new ToolButton (Stock.GoUp);
upButton.IsImportant = true;
upButton.Sensitive = false;
toolbar.Insert (upButton, -1);
ToolButton homeButton = new ToolButton (Stock.Home);
homeButton.IsImportant = true;
toolbar.Insert (homeButton, -1);
fileIcon = GetIcon (Stock.File);
dirIcon = GetIcon (Stock.Open);
ScrolledWindow sw = new ScrolledWindow ();
sw.ShadowType = ShadowType.EtchedIn;
sw.SetPolicy (PolicyType.Automatic, PolicyType.Automatic);
vbox.PackStart (sw, true, true, 0);
// Create the store and fill it with the contents of '/'
store = CreateStore ();
FillStore ();
IconView iconView = new IconView (store);
iconView.SelectionMode = SelectionMode.Multiple;
upButton.Clicked += new EventHandler (OnUpClicked);
homeButton.Clicked += new EventHandler (OnHomeClicked);
iconView.TextColumn = COL_DISPLAY_NAME;
iconView.PixbufColumn = COL_PIXBUF;
iconView.ItemActivated += new ItemActivatedHandler (OnItemActivated);
sw.Add (iconView);
iconView.GrabFocus ();
ShowAll ();
}
Gdk.Pixbuf GetIcon (string name)
{
return Gtk.IconTheme.Default.LoadIcon (name, 48, (IconLookupFlags) 0);
}
ListStore CreateStore ()
{
// path, name, pixbuf, is_dir
* parser/gapi2xml.pl: make note of _get_type methods for enums * */*-api.xml: Regen, adding gtype="..." to many enum types * generator/EnumGen.cs (Generate): if the enum has the "gtype" property, add a GTypeAttribute pointing to an internal FooGType class whose GType property can be used to get the enum's GType. * generator/ObjectGen.cs: s/ObjectManager.RegisterType/GType.Register/ * glib/GTypeAttribute.cs: attribute for indicating a property that will return the GType of a type (particularly for enums, which can't have GType properties added to them). * glib/GType.cs: renamed from Type.cs to match the type name (public static readonly GType ...): add a few missing types. (Register): moved from ObjectManager.RegisterType (LookupGType): moved from TypeConverter.LookupType and extended to handle GTypeAttribute. Also, fix mappings for sbyte/byte/char, and return specific GTypes for Object subclasses rather than always returning GType.Object. [Fixes #74699] (LookupType): moved from ObjectWrapper.LookupType (ToString): return the type name * glib/Object.cs (RegisterGType): s/ObjectManager.Register/GType.Register/ (LookupGType): Make this protected internal so GType can access it. * glib/ObjectManager.cs (RegisterType): deprecate in favor of GType.Register. (LookupType): moved to GType * glib/TypeConverter.cs (LookupType): now a deprecated wrapper around GType.LookupGType. * glib/Value.cs: Use GType casts rather than TypeConverter * gtk/NodeStore.cs (ScanType): * gtk/ListStore.custom (ListStore): * gtk/TreeStore.custom (TreeStore): Use (GType) cast rather than TypeConverter. Remove the error check and exception, since the cast never returns GType.Invalid. (The check probably predates GLib.ManagedValue.) * gnome/PanelAppletFactory.cs (Register): Use a GType cast rather than GLib.Object.LookupGType (which is no longer accessible after an mcs bugfix) * sample/GtkDemo/DemoIconView.cs (CreateStore): use the Type[] constructor rather than the GType[] constructor, since it translates typeof(Gdk.Pixbuf) correctly now. svn path=/trunk/gtk-sharp/; revision=44038
2005-05-04 16:54:24 +00:00
ListStore store = new ListStore (typeof (string), typeof (string), typeof (Gdk.Pixbuf), typeof (bool));
// Set sort column and function
store.DefaultSortFunc = new TreeIterCompareFunc (SortFunc);
store.SetSortColumnId (COL_DISPLAY_NAME, SortType.Ascending);
return store;
}
void FillStore ()
{
// first clear the store
store.Clear ();
// Now go through the directory and extract all the file information
if (!parent.Exists)
return;
foreach (DirectoryInfo di in parent.GetDirectories ())
{
if (!di.Name.StartsWith ("."))
store.AppendValues (di.FullName, di.Name, dirIcon, true);
}
foreach (FileInfo file in parent.GetFiles ())
{
if (!file.Name.StartsWith ("."))
store.AppendValues (file.FullName, file.Name, fileIcon, false);
}
}
int SortFunc (TreeModel model, TreeIter a, TreeIter b)
{
// sorts folders before files
bool a_is_dir = (bool) model.GetValue (a, COL_IS_DIRECTORY);
bool b_is_dir = (bool) model.GetValue (b, COL_IS_DIRECTORY);
string a_name = (string) model.GetValue (a, COL_DISPLAY_NAME);
string b_name = (string) model.GetValue (b, COL_DISPLAY_NAME);
if (!a_is_dir && b_is_dir)
return 1;
else if (a_is_dir && !b_is_dir)
return -1;
else
return String.Compare (a_name, b_name);
}
void OnHomeClicked (object sender, EventArgs a)
{
parent = new DirectoryInfo (Environment.GetFolderPath (Environment.SpecialFolder.Personal));
FillStore ();
upButton.Sensitive = true;
}
void OnItemActivated (object sender, ItemActivatedArgs a)
{
TreeIter iter;
store.GetIter (out iter, a.Path);
string path = (string) store.GetValue (iter, COL_PATH);
bool isDir = (bool) store.GetValue (iter, COL_IS_DIRECTORY);
if (!isDir)
return;
// Replace parent with path and re-fill the model
parent = new DirectoryInfo (path);
FillStore ();
// Sensitize the up button
upButton.Sensitive = true;
}
void OnUpClicked (object sender, EventArgs a)
{
parent = parent.Parent;
FillStore ();
upButton.Sensitive = (parent.FullName == "/" ? false : true);
}
void OnWinDelete (object sender, DeleteEventArgs a)
{
Hide ();
Dispose ();
}
}
}
#endif