File: list.cs

package info (click to toggle)
mono-tools 4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,868 kB
  • sloc: cs: 100,909; sh: 3,845; makefile: 2,040; xml: 126
file content (351 lines) | stat: -rw-r--r-- 8,468 bytes parent folder | download | duplicates (10)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// list.cs: A list widget that scales for lots of data.
//
// Authors:
//   Miguel de Icaza (miguel@ximian.com)
//   Alp Toker (alp@atoker.com)
//
// (C) 2003 Ximian, Inc.
//
namespace Monodoc {
using Gdk;
using Gtk;
using System;
using System.Collections;

delegate void ItemSelected (int index);
delegate void ItemActivated (int index);

class BigList : Gtk.DrawingArea {
	Gtk.Adjustment adjustment;
	int top_displayed;
	Style my_style;
	Widget style_widget;
	Pango.Layout layout;
	IListModel provider;
	const string ellipsis = "...";
	Hashtable ellipses = new Hashtable ();
	int old_width;
	int ellipsis_width, en_width, line_height;
	int selected_line = -1;
	int rows = 1;
	bool is_dragging = false;
	bool reloaded = true;
	public bool CanDeselect = false;
	int old_selected;
	
	public BigList (IListModel provider)
	{
		this.provider = provider;

		//Accessibility
		RefAccessible ().Role = Atk.Role.List;

		adjustment = new Gtk.Adjustment (0, 0, provider.Rows, 1, 1, 1);
		adjustment.ValueChanged += new EventHandler (ValueChangedHandler);

		layout = new Pango.Layout (PangoContext);

		ExposeEvent += new ExposeEventHandler (ExposeHandler);
		ButtonPressEvent += new ButtonPressEventHandler (ButtonPressEventHandler);
		ButtonReleaseEvent += new ButtonReleaseEventHandler (ButtonReleaseEventHandler);
		KeyPressEvent += new KeyPressEventHandler (KeyHandler);
		Realized += new EventHandler (RealizeHandler);
		Unrealized += new EventHandler (UnrealizeHandler);
		ScrollEvent += new ScrollEventHandler (ScrollHandler);
                SizeAllocated += new SizeAllocatedHandler (SizeAllocatedHandler);
		MotionNotifyEvent += new MotionNotifyEventHandler (MotionNotifyEventHandler);

		AddEvents ((int) EventMask.ButtonPressMask | (int) EventMask.ButtonReleaseMask | (int) EventMask.KeyPressMask | (int) EventMask.PointerMotionMask);
		CanFocus = true;

		style_widget = new EventBox ();
		style_widget.StyleSet += new StyleSetHandler (StyleHandler);

		//
		// Compute the height and ellipsis width of the font,
		// and the en_width for our ellipsizing algorithm
		//
		layout.SetMarkup (ellipsis);
		layout.GetPixelSize (out ellipsis_width, out line_height);

		layout.SetMarkup ("n");
		layout.GetPixelSize (out en_width, out line_height);
		
		layout.SetMarkup ("W");
		layout.GetPixelSize (out en_width, out line_height);

		old_width = Allocation.Width;
	}

	void MotionNotifyEventHandler (object o, MotionNotifyEventArgs args)
	{
		if (!is_dragging)
			return;

		int x, y;
		GetPointer (out x, out y);
		Selected = HitTest (y);
	}

	void SizeAllocatedHandler (object obj, SizeAllocatedArgs args)
	{
                Gdk.Rectangle rect = args.Allocation;
                if (rect.Equals (Gdk.Rectangle.Zero))
                        Console.WriteLine ("ERROR: Allocation is null!");

		int nrows = Allocation.Height / line_height;
		if (nrows != rows){
			rows = nrows;
			Reload ();
		}

		if (args.Allocation.Width != old_width)
			ellipses.Clear ();

		old_width = args.Allocation.Width;
	}

	void StyleHandler (object obj, StyleSetArgs args)
	{
		if (Style == my_style)
			return;

		my_style = style_widget.Style.Copy ();
		Style = my_style;
		my_style.SetBackgroundGC (StateType.Normal, Style.BaseGC (StateType.Normal));
		Refresh ();
	}

	void RealizeHandler (object o, EventArgs sender)
	{
	}

	void UnrealizeHandler (object o, EventArgs sender)
	{
	}
	
	void ValueChangedHandler (object obj, EventArgs e)
	{
		top_displayed = (int) Adjustment.Value;
		Refresh ();
	}

	void RedrawLine (int line)
	{
		QueueDrawArea (0, line * line_height, Allocation.Width, (line+1) * line_height);
	}

	public void Reload ()
	{
		adjustment.SetBounds (0, provider.Rows, 1, rows, rows);
		ellipses.Clear ();
		reloaded = true;
	}

	public void Refresh ()
	{
		QueueDrawArea (0, 0, Allocation.Width, Allocation.Height);
	}

	void ScrollHandler (object o, ScrollEventArgs args)
	{
		Gdk.EventScroll es = args.Event;
                double newloc = 0.0;
		int steps = Math.Max (rows / 6, 2);
		
                switch (es.Direction){
                case ScrollDirection.Up:
                        newloc = adjustment.Value - steps;
                        break;
                        
                case ScrollDirection.Down:
                        newloc = adjustment.Value + steps;
                        break;
                }

		newloc = Math.Max (newloc, 0);
		newloc = Math.Min (newloc, provider.Rows - rows);

                adjustment.Value = newloc;
	}

	void KeyHandler (object o, KeyPressEventArgs args)
	{
		args.RetVal = true;
		
		switch (args.Event.Key) {
			case Gdk.Key.Down:
				Selected++;
				break;
			case Gdk.Key.Up:
				Selected--;
				break;
			case Gdk.Key.Page_Down:
				Selected += rows - 1;
				break;
			case Gdk.Key.Page_Up:
				Selected -= rows - 1;
				break;
			case Gdk.Key.End:
				Selected = provider.Rows;
				break;
			case Gdk.Key.Home:
				Selected = 0;
				break;
			case Gdk.Key.Return:
				if (ItemActivated != null)
					ItemActivated (Selected);
				break;
			default:
				args.RetVal = false;
				break;
		}
	}

	uint last_click_time = 0;

	void ButtonPressEventHandler (object o, ButtonPressEventArgs a)
	{
		if (a.Event.Type != Gdk.EventType.ButtonPress)
			return;

		if (a.Event.Button == 1)
			is_dragging = true;

		HasFocus = true;
		Gdk.EventButton pos = a.Event;
		int new_selected = HitTest (pos.Y);

		int double_click_time = Settings.DoubleClickTime;

		if ((new_selected == Selected) && (a.Event.Time - last_click_time <= double_click_time)) {
			Selected = new_selected;
			Grab.Remove (this);
			if (ItemActivated != null)
				ItemActivated (Selected);
		} else
			Selected = new_selected;

		last_click_time = a.Event.Time;
		a.RetVal = true;
	}

	void ButtonReleaseEventHandler (object o, ButtonReleaseEventArgs a)
	{
		if (a.Event.Button == 1)
			is_dragging = false;

		old_selected = Selected;
	}

	int HitTest (double y)
	{
		int line = (int) y / line_height;
		if (line + top_displayed > provider.Rows - 1)
			return -2;
		return (line + top_displayed);
	}

	public int Selected {
		get {
			return selected_line;
		}
		set {
			int selected = value;

			if (selected != -2) {
				selected = Math.Max (selected, 0);
				selected = Math.Min (selected, provider.Rows - 1);
			}

			if (selected == Selected && reloaded == false)
				return;

			reloaded = false;

			if (selected_line != -1)
				RedrawLine (selected_line - top_displayed);

			if (selected == -2) {
				if (CanDeselect) {
					//Translate the selection number
					selected_line = -1;
					if (ItemSelected != null) ItemSelected (-1);
					return;
				} else return;
			}
			
			if (selected < top_displayed){
				top_displayed = selected;
				adjustment.Value = top_displayed;
				Refresh ();
			} else if (selected >= (top_displayed + rows)){
				top_displayed = selected - rows + 1;
				top_displayed = Math.Max (top_displayed, 0);
				adjustment.Value = top_displayed;
				Refresh ();
			} else
				RedrawLine (selected - top_displayed);

			selected_line = selected;
			if (ItemSelected != null)
				ItemSelected (selected);
		}
	}
	
	public event ItemSelected ItemSelected;
	
	public event ItemActivated ItemActivated;
	
        void ExposeHandler (object obj, ExposeEventArgs args)
        {
                Gdk.Window win = args.Event.Window;
                Gdk.Rectangle area = args.Event.Area;

		win.DrawRectangle (Style.BaseGC (StateType.Normal), true, area);

		for (int y = 0, row = top_displayed; y < area.Y + area.Height; y += line_height, row++){

			Gdk.GC gc;
			Gdk.Rectangle region_area = new Gdk.Rectangle (0, y, Allocation.Width, line_height);
			StateType state = StateType.Normal;

			if (row == selected_line) {
				if (HasFocus)
					state = StateType.Selected;
				else
					state = StateType.Active;

				win.DrawRectangle (Style.BaseGC (state), true, region_area);
			}
			
			//FIXME: we have a ghost row at the end of the list!
			//Console.WriteLine (row + " " + provider.Rows);

			if (row >= provider.Rows)
				return;

			string text = "";
			if (ellipses [row] == null){
				text = provider.GetValue (row);
				text = ELabel.Ellipsize (layout, text, Allocation.Width - 2 * 2, ellipsis_width, en_width);
				ellipses [row] = text;
			} else
				text = (string) ellipses [row];
			layout.SetText (text);

			gc = Style.TextGC (state);
			win.DrawLayout (gc, 2, y, layout);
		}

		args.RetVal = true;
	}

	public Gtk.Adjustment Adjustment {
		get {
			return adjustment;
		}
	}
}
}