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 {
|
|
15 public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList)
|
|
16 {
|
|
17 FillCombo(combo, itemList, delegate(LIST_TYPE obj){return obj.ToString();});
|
|
18 }
|
|
19
|
|
20 public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList, ObjectToStringRenderingMethod<LIST_TYPE> toStringMethod)
|
|
21 {
|
|
22 combo.Clear();
|
|
23 CellRendererText cell = new CellRendererText();
|
|
24 combo.PackStart(cell, false);
|
|
25 combo.AddAttribute(cell, "text", 1);
|
|
26 ListStore store = new ListStore(typeof(LIST_TYPE), typeof(string));
|
|
27 combo.Model = store;
|
|
28
|
|
29 foreach (LIST_TYPE item in itemList)
|
|
30 {
|
|
31 store.AppendValues(item, toStringMethod(item));
|
|
32 }
|
|
33 }
|
|
34
|
|
35 public static void SelectItem(ComboBox combo, object item)
|
|
36 {
|
|
37 TreeModel model = combo.Model;
|
|
38 TreeIter iter;
|
|
39 model.GetIterFirst(out iter);
|
|
40
|
|
41 do
|
|
42 {
|
|
43 GLib.Value rowItem = new GLib.Value();
|
|
44 combo.Model.GetValue (iter, 0, ref rowItem);
|
|
45
|
|
46 if (item.Equals(rowItem))
|
|
47 {
|
|
48 combo.SetActiveIter(iter);
|
|
49 break;
|
|
50 }
|
|
51 }
|
|
52 while (combo.Model.IterNext(ref iter));
|
|
53 }
|
|
54
|
|
55 public static void SelectIndex(ComboBox combo, int idx)
|
|
56 {
|
|
57 Gtk.TreeIter iter;
|
|
58 combo.Model.IterNthChild(out iter, idx);
|
|
59 combo.SetActiveIter(iter);
|
|
60 }
|
|
61 }
|
|
62 }
|