9
|
1 // This file (ComboBoxUtils.cs) is a part of the IBBoard.GtkSharp project and is copyright 2010 IBBoard
|
|
2 //
|
|
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
|
|
4
|
|
5 using System;
|
|
6 using System.Collections.Generic;
|
|
7 using Gtk;
|
|
8
|
|
9 namespace IBBoard.GtkSharp
|
|
10 {
|
|
11 public delegate string ObjectToStringRenderingMethod<OBJECT_TYPE>(OBJECT_TYPE obj);
|
|
12
|
|
13 public class ComboBoxUtils
|
|
14 {
|
11
|
15 private static int DEFAULT_VALUE_COLUMN = 1;
|
|
16 private static int DEFAULT_TEXT_COLUMN = 0;
|
|
17
|
9
|
18 public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList)
|
|
19 {
|
|
20 FillCombo(combo, itemList, delegate(LIST_TYPE obj){return obj.ToString();});
|
|
21 }
|
|
22
|
11
|
23 public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList, ObjectToStringRenderingMethod<LIST_TYPE> objectToString)
|
9
|
24 {
|
|
25 combo.Clear();
|
|
26 CellRendererText cell = new CellRendererText();
|
|
27 combo.PackStart(cell, false);
|
10
|
28 combo.AddAttribute(cell, "text", 0);
|
11
|
29 Type[] types = new Type[2];
|
|
30 types[DEFAULT_VALUE_COLUMN] = typeof(LIST_TYPE);
|
|
31 types[DEFAULT_TEXT_COLUMN] = typeof(string);
|
|
32 ListStore store = new ListStore(types);
|
9
|
33 combo.Model = store;
|
|
34
|
|
35 foreach (LIST_TYPE item in itemList)
|
|
36 {
|
11
|
37 object[] values = new object[2];
|
|
38 values[DEFAULT_VALUE_COLUMN] = item;
|
|
39 values[DEFAULT_TEXT_COLUMN] = objectToString(item);
|
|
40 store.AppendValues(values);
|
9
|
41 }
|
|
42 }
|
|
43
|
|
44 public static void SelectItem(ComboBox combo, object item)
|
|
45 {
|
|
46 TreeModel model = combo.Model;
|
|
47 TreeIter iter;
|
|
48 model.GetIterFirst(out iter);
|
|
49
|
|
50 do
|
|
51 {
|
|
52 GLib.Value rowItem = new GLib.Value();
|
12
|
53 combo.Model.GetValue(iter, DEFAULT_VALUE_COLUMN, ref rowItem);
|
9
|
54
|
12
|
55 if (item.Equals(rowItem.Val))
|
9
|
56 {
|
|
57 combo.SetActiveIter(iter);
|
|
58 break;
|
|
59 }
|
|
60 }
|
|
61 while (combo.Model.IterNext(ref iter));
|
|
62 }
|
|
63
|
|
64 public static void SelectIndex(ComboBox combo, int idx)
|
|
65 {
|
|
66 Gtk.TreeIter iter;
|
|
67 combo.Model.IterNthChild(out iter, idx);
|
|
68 combo.SetActiveIter(iter);
|
|
69 }
|
11
|
70
|
|
71 public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo)
|
|
72 {
|
|
73 return GetSelectedItem<LIST_TYPE>(combo, DEFAULT_VALUE_COLUMN);
|
|
74 }
|
|
75
|
|
76 public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo, int valueColumn)
|
|
77 {
|
|
78 TreeIter iter;
|
|
79 combo.GetActiveIter(out iter);
|
|
80 return (LIST_TYPE)combo.Model.GetValue(iter, valueColumn);
|
|
81 }
|
9
|
82 }
|
|
83 }
|