File: DemoIconView.cs

package info (click to toggle)
gtk-sharp3 2.99.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,488 kB
  • sloc: xml: 308,885; cs: 38,796; sh: 11,336; perl: 1,295; makefile: 1,099; ansic: 903
file content (164 lines) | stat: -rw-r--r-- 4,178 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
			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