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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
// ListView.cs
//
// Author:
// Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using Gtk;
using Gdk;
using Pango;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace MonoDevelop.Components
{
[ToolboxItem (true)]
public class ListView: Gtk.DrawingArea
{
int margin = 0;
int padding = 4;
// int listWidth = 300;
Pango.Layout layout;
bool allowMultipleSelection;
List<int> selectedRows = new List<int> ();
int page = 0;
int visibleRows = -1;
int rowHeight;
bool buttonPressed;
bool disableSelection;
int viewWidth;
const int IconWidth = 24;
const int ColumnGap = 2;
IListViewDataSource dataProvider;
public event EventHandler SelectionChanged;
public event EventHandler ItemActivated;
public ListView ()
{
base.CanFocus = true;
this.Events = EventMask.ButtonPressMask |
EventMask.ButtonReleaseMask |
EventMask.PointerMotionMask |
EventMask.ScrollMask;
layout = new Pango.Layout (this.PangoContext);
layout.Wrap = Pango.WrapMode.Char;
FontDescription des = this.Style.FontDescription.Copy();
layout.FontDescription = des;
}
public void Reset ()
{
if (dataProvider == null) {
selectedRows.Clear ();
return;
}
if (dataProvider.ItemCount == 0) {
selectedRows.Clear ();
}
else {
selectedRows.Clear ();
selectedRows.Add (0);
}
page = 0;
disableSelection = false;
if (IsRealized) {
UpdateStyle ();
QueueDraw ();
}
if (SelectionChanged != null)
SelectionChanged (this, EventArgs.Empty);
}
public void Refresh ()
{
// Remove any selections that no longer exist.
// We need to build a list of 'items to remove', because
// collections are annoying to remove from.
List<int> toRemove = new List<int> ();
foreach (int i in selectedRows) {
if (i >= RowCount)
toRemove.Add (i);
}
foreach (int i in toRemove) {
selectedRows.Remove (i);
}
// If there are no selections left, select the last item.
if (selectedRows.Count == 0)
SelectedRow = RowCount - 1;
UpdatePage (false, true);
QueueDraw ();
}
public bool AllowMultipleSelection
{
get {
return allowMultipleSelection;
}
set {
allowMultipleSelection = value;
}
}
public int SelectedRow
{
get {
// Just return the most recent selection.
if (selectedRows.Count > 0)
return selectedRows[selectedRows.Count - 1];
else
return -1;
}
set {
// Just set the selection to a specific item.
if (value < 0)
value = 0;
if (value >= RowCount)
value = RowCount - 1;
if (value != this.SelectedRow || selectedRows.Count > 1)
{
selectedRows.Clear ();
selectedRows.Add (value);
UpdatePage (false, true);
if (SelectionChanged != null)
SelectionChanged (this, EventArgs.Empty);
}
if (disableSelection)
disableSelection = false;
this.QueueDraw ();
}
}
public ReadOnlyCollection<int> SelectedRows
{
get {
return new ReadOnlyCollection<int> (selectedRows);
}
}
public void SelectRow (int index)
{
if (!allowMultipleSelection) {
this.SelectedRow = index;
return;
}
if (index < 0 || index >= this.RowCount)
return;
// Remove it if it already exists.
selectedRows.Remove (index);
selectedRows.Add (index);
if (SelectionChanged != null)
SelectionChanged (this, EventArgs.Empty);
this.QueueDraw ();
}
public void UnselectRow (int index)
{
if (!allowMultipleSelection) {
if (this.SelectedRow == index) {
this.SelectedRow = -1;
return;
}
}
if (selectedRows.Remove (index)) {
if (SelectionChanged != null)
SelectionChanged (this, EventArgs.Empty);
this.QueueDraw ();
}
}
public void ToggleRowSelection (int index)
{
if (selectedRows.Contains (index))
UnselectRow (index);
else
SelectRow (index);
}
public void ClearSelection()
{
selectedRows.Clear();
this.QueueDraw();
}
public void ModifySelection(bool up, bool page, bool addTo)
{
// Note that we don't really handle "Shift+PageUp" type selection,
// because that just gets confusing for the user when there are
// multiple selection ranges. Therefore, 'page' only works for
// non-additive selections.
if (up)
{
// See if the most recent selection was one down from the previous.
// If so, we actually want to turn it off.
if (addTo && !page) {
if (selectedRows.Count > 1 && this.SelectedRow == (selectedRows[selectedRows.Count-2] + 1)) {
UnselectRow (this.SelectedRow);
}
else {
SelectRow (this.SelectedRow - 1);
}
}
else {
if (page)
this.SelectedRow -= VisibleRows - 1;
else
this.SelectedRow --;
}
}
else
{
if (addTo && !page) {
if (selectedRows.Count > 1 && this.SelectedRow == (selectedRows[selectedRows.Count-2] - 1)) {
UnselectRow (this.SelectedRow);
}
else {
SelectRow (this.SelectedRow + 1);
}
}
else {
if (page)
this.SelectedRow += VisibleRows - 1;
else
this.SelectedRow ++;
}
}
}
public void CenterViewToSelection ()
{
if (vAdjustement != null) {
int val = SelectedRow - VisibleRows / 2;
if (val + VisibleRows >= RowCount)
val = RowCount - VisibleRows;
vAdjustement.Value = val > 0 ? val : 0;
}
}
int RowCount {
get { return dataProvider != null ? dataProvider.ItemCount : 0; }
}
void UpdatePage (bool centerRow, bool keepSelectionInView)
{
if (!IsRealized) {
page = 0;
return;
}
CalcVisibleRows ();
if (SelectedRow < page || SelectedRow >= page + VisibleRows) {
if (centerRow) {
page = SelectedRow - (VisibleRows / 2);
} else if (keepSelectionInView) {
if (SelectedRow < page)
page = SelectedRow;
else
page = SelectedRow - VisibleRows + 1;
}
if (page < 0) page = 0;
}
UpdateAdjustments ();
if (vAdjustement != null)
vAdjustement.Value = page;
}
public bool SelectionDisabled
{
get { return disableSelection; }
set {
disableSelection = value;
this.QueueDraw ();
}
}
public int Page
{
get {
return page;
}
set {
page = value;
this.QueueDraw ();
}
}
protected override void OnSizeAllocated (Gdk.Rectangle allocation)
{
base.OnSizeAllocated (allocation);
UpdatePage (false, true);
}
Gtk.Adjustment hAdjustement;
Gtk.Adjustment vAdjustement;
protected override void OnSetScrollAdjustments (Gtk.Adjustment hadj, Gtk.Adjustment vadj)
{
if (hAdjustement != null)
hAdjustement.ValueChanged -= HandleHorValueChanged;
if (vAdjustement!= null)
vAdjustement.ValueChanged -= HandleVerValueChanged;
hAdjustement = hadj;
vAdjustement = vadj;
if (hAdjustement == null || vAdjustement == null)
return;
UpdateAdjustments ();
hAdjustement.ValueChanged += HandleHorValueChanged;
vAdjustement.ValueChanged += HandleVerValueChanged;
}
void HandleVerValueChanged (object sender, EventArgs e)
{
page = (int) vAdjustement.Value;
UpdatePage (false, false);
QueueDraw ();
}
void HandleHorValueChanged(object sender, EventArgs e)
{
QueueDraw ();
}
void UpdateAdjustments ()
{
if (hAdjustement == null)
return;
vAdjustement.SetBounds (0, dataProvider.ItemCount, 1, VisibleRows, VisibleRows);
hAdjustement.SetBounds (0, viewWidth, 1, Allocation.Width, Allocation.Width);
}
protected override bool OnKeyPressEvent (Gdk.EventKey evnt)
{
if (ProcessKey (evnt.Key, evnt.State))
return true;
else
return base.OnKeyPressEvent (evnt);
}
public bool ProcessKey (Gdk.Key key, Gdk.ModifierType modifier)
{
switch (key)
{
case Gdk.Key.Up:
if (SelectionDisabled)
SelectionDisabled = false;
else
ModifySelection (true, false, (modifier & ModifierType.ShiftMask) == ModifierType.ShiftMask);
return true;
case Gdk.Key.Down:
if (SelectionDisabled)
SelectionDisabled = false;
else
ModifySelection (false, false, (modifier & ModifierType.ShiftMask) == ModifierType.ShiftMask);
return true;
case Gdk.Key.Page_Up:
ModifySelection (true, true, (modifier & ModifierType.ShiftMask) == ModifierType.ShiftMask);
return true;
case Gdk.Key.Page_Down:
ModifySelection (false, true, (modifier & ModifierType.ShiftMask) == ModifierType.ShiftMask);
return true;
case Gdk.Key.Left:
//if (curPos == 0) return KeyAction.CloseWindow | KeyAction.Process;
//curPos--;
return true;
case Gdk.Key.Right:
//if (curPos == word.Length) return KeyAction.CloseWindow | KeyAction.Process;
//curPos++;
return true;
case Gdk.Key.Home:
SelectedRow = 0;
return true;
case Gdk.Key.End:
if (dataProvider != null)
SelectedRow = RowCount - 1;
return true;
case Gdk.Key.Return:
if (ItemActivated != null)
ItemActivated (this, EventArgs.Empty);
return true;
}
return false;
}
protected override bool OnButtonPressEvent (EventButton e)
{
try {
GrabFocus ();
int row = GetRowByPosition ((int) e.Y);
if ((e.State & ModifierType.ControlMask) == ModifierType.ControlMask)
ToggleRowSelection (row);
else
this.SelectedRow = row;
buttonPressed = true;
if (e.Type == EventType.TwoButtonPress) {
if (ItemActivated != null)
ItemActivated (this, EventArgs.Empty);
}
return base.OnButtonPressEvent (e);
} catch (Exception ex) {
GLib.ExceptionManager.RaiseUnhandledException (ex, false);
return false;
}
}
protected override bool OnButtonReleaseEvent (EventButton e)
{
buttonPressed = false;
return base.OnButtonReleaseEvent (e);
}
protected override bool OnMotionNotifyEvent (EventMotion e)
{
if (!buttonPressed)
return base.OnMotionNotifyEvent (e);
int winWidth, winHeight;
this.GdkWindow.GetSize (out winWidth, out winHeight);
int row = GetRowByPosition ((int) e.Y);
if ((e.State & ModifierType.ControlMask) == ModifierType.ControlMask)
SelectRow (row);
else
this.SelectedRow = row;
return true;
}
protected override bool OnExposeEvent (Gdk.EventExpose args)
{
base.OnExposeEvent (args);
DrawList (args);
return true;
}
//FIXME: we could use the expose event's clipbox to make the drawing more efficient
void DrawList (Gdk.EventExpose args)
{
var window = args.Window;
int winWidth, winHeight;
window.GetSize (out winWidth, out winHeight);
int ypos = margin;
int lineWidth = winWidth - margin*2;
int xpos = margin + padding - (int) hAdjustement.Value;
//avoid recreating the GC objects that we use multiple times
var textGCNormal = this.Style.TextGC (StateType.Normal);
var fgGCNormal = this.Style.ForegroundGC (StateType.Normal);
int n = 0;
while (ypos < winHeight - margin && (page + n) < RowCount)
{
bool isSelected = selectedRows.Contains (page + n);
bool hasMarkup = SetLayoutText (page + n, isSelected);
Gdk.Pixbuf icon = dataProvider.GetIcon (page + n);
int iconHeight = icon != null? icon.Height : IconWidth;
int iconWidth = icon != null? icon.Width : IconWidth;
int wi, he, typos, iypos;
layout.GetPixelSize (out wi, out he);
typos = he < rowHeight ? ypos + (rowHeight - he) / 2 : ypos;
iypos = iconHeight < rowHeight ? ypos + (rowHeight - iconHeight) / 2 : ypos;
if (isSelected) {
if (!disableSelection) {
window.DrawRectangle (this.Style.BaseGC (StateType.Selected),
true, margin, ypos, lineWidth, he + padding);
window.DrawLayout (this.Style.TextGC (StateType.Selected),
xpos + iconWidth + ColumnGap, typos, layout);
}
else {
window.DrawRectangle (this.Style.BaseGC (StateType.Selected),
false, margin, ypos, lineWidth, he + padding);
window.DrawLayout (textGCNormal, xpos + iconWidth + ColumnGap, typos, layout);
}
// Draw a 'most recent selection' rectangle.
if (this.SelectedRow == page + n) {
window.DrawRectangle (this.Style.BaseGC (StateType.Active),
false, margin, ypos, lineWidth, he + padding - 1);
window.DrawLayout (this.Style.TextGC (StateType.Active),
xpos + iconWidth + ColumnGap, typos, layout);
}
}
else
window.DrawLayout (textGCNormal, xpos + iconWidth + ColumnGap, typos, layout);
if (icon != null)
window.DrawPixbuf (fgGCNormal, icon, 0, 0, xpos, iypos, iconWidth, iconHeight, RgbDither.None, 0, 0);
ypos += rowHeight;
n++;
//reset the markup or it carries over to the next SetText
if (hasMarkup)
layout.SetMarkup (string.Empty);
}
}
bool SetLayoutText (int n, bool isSelected)
{
bool hasMarkup = dataProvider.UseMarkup (n);
string text = isSelected ? dataProvider.GetSelectedText (n) : dataProvider.GetText (n);
if (hasMarkup)
layout.SetMarkup (text ?? "<null>");
else
layout.SetText (text ?? "<null>");
return hasMarkup;
}
int GetRowByPosition (int ypos)
{
if (visibleRows == -1) CalcVisibleRows ();
return page + (ypos-margin) / rowHeight;
}
public Gdk.Rectangle GetRowArea (int row)
{
row -= page;
int winWidth, winHeight;
this.GdkWindow.GetSize (out winWidth, out winHeight);
return new Gdk.Rectangle (margin, margin + rowHeight * row, winWidth, rowHeight);
}
public int VisibleRows
{
get {
if (visibleRows == -1) CalcVisibleRows ();
return visibleRows;
}
}
public IListViewDataSource DataSource {
get {
return dataProvider;
}
set {
dataProvider = value;
Refresh ();
}
}
void CalcVisibleRows ()
{
// int winHeight = 200;
int rowWidth;
// int lvWidth = Allocation.Width;
int lvHeight = Allocation.Height;
layout.GetPixelSize (out rowWidth, out rowHeight);
rowHeight += padding;
visibleRows = (lvHeight + padding - margin * 2) / rowHeight;
// int newHeight;
// int count = RowCount;
// if (count > this.visibleRows)
// newHeight = (rowHeight * visibleRows) + margin * 2;
// else
// newHeight = (rowHeight * count) + margin * 2;
if (dataProvider != null) {
int maxr = Math.Min (page + visibleRows, dataProvider.ItemCount);
for (int n=page; n<maxr; n++) {
SetLayoutText (n, false);
int rh, rw;
layout.GetPixelSize (out rw, out rh);
rw += (margin + padding) * 2 + IconWidth + ColumnGap;
if (rw > viewWidth)
viewWidth = rw;
}
}
}
protected override void OnRealized ()
{
base.OnRealized ();
UpdateStyle ();
UpdatePage (false, true);
}
void UpdateStyle ()
{
this.GdkWindow.Background = this.Style.Base (StateType.Normal);
CalcVisibleRows ();
}
}
public interface IListViewDataSource
{
int ItemCount { get; }
string GetText (int n);
string GetSelectedText (int n);
bool UseMarkup (int n);
Gdk.Pixbuf GetIcon (int n);
}
}
|