annotate ComboBoxUtils.cs @ 9:d0e26a896d72

Re #26: Add GTK# wrapper methods * Add ComboBox class with methods to: * Fill a ComboBox * Set selection to item * Set selection to index
author IBBoard <dev@ibboard.co.uk>
date Sat, 16 Jan 2010 15:10:41 +0000
parents
children 3277cb2ecb71
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (ComboBoxUtils.cs) is a part of the IBBoard.GtkSharp project and is copyright 2010 IBBoard
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using Gtk;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 namespace IBBoard.GtkSharp
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 public delegate string ObjectToStringRenderingMethod<OBJECT_TYPE>(OBJECT_TYPE obj);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 public class ComboBoxUtils
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList)
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 FillCombo(combo, itemList, delegate(LIST_TYPE obj){return obj.ToString();});
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList, ObjectToStringRenderingMethod<LIST_TYPE> toStringMethod)
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 combo.Clear();
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 CellRendererText cell = new CellRendererText();
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 combo.PackStart(cell, false);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 combo.AddAttribute(cell, "text", 1);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 ListStore store = new ListStore(typeof(LIST_TYPE), typeof(string));
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 combo.Model = store;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 foreach (LIST_TYPE item in itemList)
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 store.AppendValues(item, toStringMethod(item));
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 public static void SelectItem(ComboBox combo, object item)
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 TreeModel model = combo.Model;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 TreeIter iter;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 model.GetIterFirst(out iter);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 do
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 GLib.Value rowItem = new GLib.Value();
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 combo.Model.GetValue (iter, 0, ref rowItem);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 if (item.Equals(rowItem))
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 combo.SetActiveIter(iter);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 break;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 while (combo.Model.IterNext(ref iter));
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 public static void SelectIndex(ComboBox combo, int idx)
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 Gtk.TreeIter iter;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 combo.Model.IterNthChild(out iter, idx);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 combo.SetActiveIter(iter);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 }