File: DemoStockBrowser.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 (197 lines) | stat: -rw-r--r-- 5,321 bytes parent folder | download | duplicates (5)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Stock Item and Icon Browser
 *
 * This source code for this demo doesn't demonstrate anything
 * particularly useful in applications. The purpose of the "demo" is
 * just to provide a handy place to browse the available stock icons
 * and stock items.
 */

using System;
using System.Collections;
using System.Reflection;
using Gtk;

namespace GtkDemo
{
	[Demo ("Stock Item and Icon Browser", "DemoStockBrowser.cs")]
	public class DemoStockBrowser : Gtk.Window
	{
		enum Column {
			Id,
			Name,
			Label,
			Accel,
		};

		Label typeLabel, nameLabel, idLabel, accelLabel;
		Image iconImage;

		public DemoStockBrowser () : base ("Stock Icons and Items")
		{
			SetDefaultSize (-1, 500);
			BorderWidth = 8;

			HBox hbox = new HBox (false, 8);
			Add (hbox);

			ScrolledWindow sw = new ScrolledWindow ();
			sw.SetPolicy (PolicyType.Never, PolicyType.Automatic);
			hbox.PackStart (sw, false, false, 0);

			ListStore model = CreateModel ();

			TreeView treeview = new TreeView (model);
			sw.Add (treeview);

			TreeViewColumn column = new TreeViewColumn ();
			column.Title = "Name";
			CellRenderer renderer = new CellRendererPixbuf ();
			column.PackStart (renderer, false);
			column.SetAttributes (renderer, "stock_id", Column.Id);
			renderer = new CellRendererText ();
			column.PackStart (renderer, true);
			column.SetAttributes (renderer, "text", Column.Name);

			treeview.AppendColumn (column);
			treeview.AppendColumn ("Label", new CellRendererText (), "text", Column.Label);
			treeview.AppendColumn ("Accel", new CellRendererText (), "text", Column.Accel);
			treeview.AppendColumn ("ID", new CellRendererText (), "text", Column.Id);

			Alignment align = new Alignment (0.5f, 0.0f, 0.0f, 0.0f);
			hbox.PackEnd (align, false, false, 0);

			Frame frame = new Frame ("Selected Item");
			align.Add (frame);

			VBox vbox = new VBox (false, 8);
			vbox.BorderWidth = 8;
			frame.Add (vbox);

			typeLabel = new Label ();
			vbox.PackStart (typeLabel, false, false, 0);
			iconImage = new Gtk.Image ();
			vbox.PackStart (iconImage, false, false, 0);
			accelLabel = new Label ();
			vbox.PackStart (accelLabel, false, false, 0);
			nameLabel = new Label ();
			vbox.PackStart (nameLabel, false, false, 0);
			idLabel = new Label ();
			vbox.PackStart (idLabel, false, false, 0);

			treeview.Selection.Mode = Gtk.SelectionMode.Single;
			treeview.Selection.Changed += new EventHandler (SelectionChanged);

			ShowAll ();
		}

		private ListStore CreateModel ()
		{
			ListStore store = new Gtk.ListStore (typeof (string), typeof(string), typeof(string), typeof(string), typeof (string));

			string[] stockIds = Gtk.Stock.ListIds ();
			Array.Sort (stockIds);

			// Use reflection to get the list of C# names
			Hashtable idToName = new Hashtable ();
			foreach (PropertyInfo info in typeof (Gtk.Stock).GetProperties (BindingFlags.Public | BindingFlags.Static)) {
				if (info.PropertyType == typeof (string))
					idToName[info.GetValue (null, null)] = "Gtk.Stock." + info.Name;
			}

			foreach (string id in stockIds) {
				Gtk.StockItem si;
				string accel;

				si = Gtk.Stock.Lookup (id);
				if (si.Keyval != 0)
					accel = Accelerator.Name (si.Keyval, si.Modifier);
				else
					accel = "";

				store.AppendValues (id, idToName[id], si.Label, accel);
			}

			return store;
		}

		void SelectionChanged (object o, EventArgs args)
		{
			TreeSelection selection = (TreeSelection)o;
			TreeIter iter;
			ITreeModel model;

			if (selection.GetSelected (out model, out iter)) {
				string id = (string) model.GetValue (iter, (int)Column.Id);
				string name = (string) model.GetValue (iter, (int)Column.Name);
				string label = (string) model.GetValue (iter, (int)Column.Label);
				string accel = (string) model.GetValue (iter, (int)Column.Accel);

				IconSize size = GetLargestSize (id);
				bool icon = (size != IconSize.Invalid);

				if (icon && label != null)
					typeLabel.Text = "Icon and Item";
				else if (icon)
					typeLabel.Text = "Icon Only";
				else if (label != null)
					typeLabel.Text = "Item Only";
				else
					typeLabel.Text = "???????";

				if (name != null)
					nameLabel.Text = name;
				else
					nameLabel.Text = "";

				idLabel.Text = id;

				if (label != null)
					accelLabel.TextWithMnemonic = label + " " + accel;
				else
					accelLabel.Text = "";

				if (icon)
					iconImage.SetFromStock (id, size);
				else
					iconImage.Pixbuf = null;
			} else {
				typeLabel.Text = "No selected item";
				nameLabel.Text = "";
				idLabel.Text = "";
				accelLabel.Text = "";
				iconImage.Pixbuf = null;
			}
		}

		// Finds the largest size at which the given image stock id is
		// available. This would not be useful for a normal application

		private IconSize GetLargestSize (string stockId)
		{
			IconSet set = IconFactory.LookupDefault (stockId);
			if (set == null)
				return IconSize.Invalid;

			IconSize[] sizes = set.Sizes;
			IconSize bestSize = IconSize.Invalid;
			int bestPixels = 0;

			foreach (IconSize size in sizes) {
				int width, height;
				Gtk.Icon.SizeLookup (size, out width, out height);
				if (width * height > bestPixels) {
					bestSize = size;
					bestPixels = width * height;
				}
			}

			return bestSize;
		}

		protected override bool OnDeleteEvent (Gdk.Event evt)
		{
			Destroy ();
			return true;
		}
	}
}