annotate ComboBoxUtils.cs @ 21:0364560fcec2

Re #47: Add translatable GTK# widgets * Handle menu items and actions (which can't obviously be made ITranslatable using Stetic)
author IBBoard <dev@ibboard.co.uk>
date Tue, 28 Dec 2010 19:24:06 +0000
parents ffda5e5f1617
children
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 {
11
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
15 private static int DEFAULT_VALUE_COLUMN = 1;
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
16 private static int DEFAULT_TEXT_COLUMN = 0;
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
17
9
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 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
19 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 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
21 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
11
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
23 public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList, ObjectToStringRenderingMethod<LIST_TYPE> objectToString)
9
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 combo.Clear();
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 CellRendererText cell = new CellRendererText();
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 combo.PackStart(cell, false);
10
3277cb2ecb71 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
28 combo.AddAttribute(cell, "text", 0);
11
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
29 Type[] types = new Type[2];
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
30 types[DEFAULT_VALUE_COLUMN] = typeof(LIST_TYPE);
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
31 types[DEFAULT_TEXT_COLUMN] = typeof(string);
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
32 ListStore store = new ListStore(types);
9
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 combo.Model = store;
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 foreach (LIST_TYPE item in itemList)
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
11
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
37 object[] values = new object[2];
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
38 values[DEFAULT_VALUE_COLUMN] = item;
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
39 values[DEFAULT_TEXT_COLUMN] = objectToString(item);
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
40 store.AppendValues(values);
9
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 }
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
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 public static void SelectItem(ComboBox combo, object item)
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 TreeModel model = combo.Model;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 TreeIter iter;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 model.GetIterFirst(out iter);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 do
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 GLib.Value rowItem = new GLib.Value();
12
ffda5e5f1617 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
53 combo.Model.GetValue(iter, DEFAULT_VALUE_COLUMN, ref rowItem);
9
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54
12
ffda5e5f1617 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
55 if (item.Equals(rowItem.Val))
9
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 combo.SetActiveIter(iter);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 break;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 }
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 while (combo.Model.IterNext(ref iter));
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 public static void SelectIndex(ComboBox combo, int idx)
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 {
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 Gtk.TreeIter iter;
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 combo.Model.IterNthChild(out iter, idx);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 combo.SetActiveIter(iter);
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 }
11
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
70
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
71 public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo)
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
72 {
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
73 return GetSelectedItem<LIST_TYPE>(combo, DEFAULT_VALUE_COLUMN);
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
74 }
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
75
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
76 public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo, int valueColumn)
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
77 {
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
78 TreeIter iter;
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
79 combo.GetActiveIter(out iter);
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
80 return (LIST_TYPE)combo.Model.GetValue(iter, valueColumn);
43d0b0ec1657 Re #26: GTK# wrappers
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
81 }
9
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 }
d0e26a896d72 Re #26: Add GTK# wrapper methods
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 }