File: DemoStockBrowser.cs

package info (click to toggle)
gtk-sharp 1%3A1.0.10-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,416 kB
  • ctags: 4,465
  • sloc: xml: 224,361; cs: 17,961; sh: 8,422; ansic: 2,724; makefile: 1,604; perl: 1,089
file content (218 lines) | stat: -rw-r--r-- 5,273 bytes parent folder | download
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// StockBrowser.cs, port of stock_browser.c from gtk-demo
//
// Author: Daniel Kornhauser <dkor@media.mit.edu>
//
// (C) 2003 Ximian, Inc.

using System;
using System.Collections;
using Gtk;

namespace GtkDemo
{	
	public class DemoStockBrowser : Gtk.Window
	{
		class StockInfo
		{
			internal string Name;
			internal string Label;
			internal string Accel;
			internal string ID;
			internal Gdk.Pixbuf Icon;
		}

		// in a real application this would be
		// split into its own file
		class StockFrame : Gtk.Frame
		{
			StockInfo info;
			Label category;
			Label name;
			Label id;
			Label label;
			Image icon;

			internal StockFrame () : base ("Selected Item")
			{
				this.SetSizeRequest (200, -1);
				// Icon and Item / Icon Only / ???
				category = new Label ("???");
				// icon / blank
				icon = new Image ("");
				// _Add / blank
				label = new Label ();
				label.UseUnderline = true;
				// Gtk.Stock.Cancel
				name = new Label ();
				// gtk-stock-cancel
				id = new Label ();

				VBox vbox = new VBox (false, 3);
				vbox.PackStart (category, false, true, 0);
				vbox.PackStart (icon, false, true, 0);
				vbox.PackStart (label, false, true, 0);
				vbox.PackStart (name, false, true, 0);
				vbox.PackStart (id, false, true, 0);

				this.Add (vbox);
				this.ShowAll ();
			}

			internal StockInfo Info
			{
				get { return info; }
				set {
					info = value;
					name.Text = info.Name;
					label.Text = info.Label;
					id.Text = info.ID;
					icon.Pixbuf = info.Icon;
				}
			}
		}

		StockFrame stockFrame;

		public DemoStockBrowser () : base ("Stock Item Browser Demo")
		{	
			this.SetDefaultSize (600, 400);
			this.DeleteEvent += new DeleteEventHandler (WindowDelete);
			this.BorderWidth = 8;

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

			ScrolledWindow scrolledWindow = new ScrolledWindow (null, null);
			scrolledWindow.SetPolicy (PolicyType.Never, PolicyType.Automatic);
			hbox.PackStart (scrolledWindow, true, true, 0);
			
			TreeView list = new TreeView ();
			list.AppendColumn ("Icon", new CellRendererPixbuf (), "pixbuf", 0);
			list.AppendColumn ("Name", new CellRendererText (), "text", 1);
			list.AppendColumn ("Label", new CellRendererText (), "text", 2);
			list.AppendColumn ("Accel", new CellRendererText (), "text", 3);			
			list.AppendColumn ("ID", new CellRendererText (), "text", 4);			
			list.Model = CreateStore ();

			list.Selection.Changed += new EventHandler (OnSelectionChanged);
			scrolledWindow.Add (list);
			
			stockFrame = new StockFrame ();
			hbox.PackStart (stockFrame, false, false, 0);
			
			this.ShowAll ();
		}	

		private ListStore CreateStore ()
		{
			// FIXME: tremendous duplication of info
			// image, name, label, accel, id, StockInfo
			ListStore store = new Gtk.ListStore (typeof (Gdk.Pixbuf), typeof(string), typeof(string), typeof(string), typeof (string), typeof (StockInfo));

			string[] stock_ids = Gtk.Stock.ListIds ();		

			foreach (string s in stock_ids)
			{
				Gtk.StockItem si = new StockItem ();
				if (Gtk.StockManager.Lookup (s, ref si)) {
					Gdk.Pixbuf icon = this.RenderIcon (s, IconSize.Menu, "");
					StockInfo info = new StockInfo ();
					info.Icon = icon;
					info.Name = GetCLSName (si.StockId);
					info.Label = si.Label;
					info.Accel = GetKeyName (si);
					info.ID = si.StockId;

					// FIXME: si.Label needs to _AccelAware
					store.AppendValues (icon, GetCLSName (si.StockId), si.Label, GetKeyName (si), si.StockId, info);
				}
				else {
					//Console.WriteLine ("StockItem '{0}' could not be found.", s);
				}
			}
			
			return store;
		}

		// changes 'gtk-stock-close' into 'Gtk.Stock.Close'
		// should use StudlyCaps from gapi2xml.pl instead
		string GetCLSName (string stockID)
		{
			string cls = "";
			if (stockID.StartsWith ("gtk-"))
				cls = stockID.Substring (4, stockID.Length - 4);

			char[] split = cls.ToCharArray ();
			bool raiseNext = false;
			ArrayList tmp = new ArrayList ();
			tmp.Add (char.ToUpper (split[0]));

			for (int i = 1; i < split.Length; i ++)
			{
				if (split[i] == '-') {
					raiseNext = true;
					continue;
				}

				if (raiseNext) {
					tmp.Add (char.ToUpper (split[i]));
					raiseNext = false;
				}
				else {
					tmp.Add (split[i]);
				}
			}

			split = new char[tmp.Count];
			int j = 0;
			foreach (char c in tmp)
				split[j++] = c;

			return "Gtk.Stock." + new string (split);
		}

		// use si.Keyval and si.Modifier
		// to produce a reasonable representation
		// of the key binding
		string GetKeyName (StockItem si)
		{
			string mod = "";
			string key = "";

			switch (si.Modifier) {
				// seems to be the only one used
				case Gdk.ModifierType.ControlMask:
					mod = "<Control>";
					break;
				default:
					break;
			}

			if (si.Keyval > 0)
				key = Gdk.Keyval.Name (si.Keyval);

			return String.Format ("{0} {1}", mod, key);
		}

		void OnSelectionChanged (object o, EventArgs args)
		{
			TreeIter iter;
			TreeModel model;

			if (((TreeSelection) o).GetSelected (out model, out iter))
			{
				StockInfo info = (StockInfo) model.GetValue (iter, 5);
				stockFrame.Info = info;
			}
		}
		
  		private void WindowDelete (object o, DeleteEventArgs args)
		{
			this.Hide ();
			this.Destroy ();
			args.RetVal = true;
		}
	}
}