Mercurial > repos > IBBoard.GtkSharp
view ComboBoxUtils.cs @ 37:c7645414cb3e
* Add translatable CheckButton (checkbox)
* Update package naming rules
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 05 Oct 2011 21:02:19 +0100 |
parents | ffda5e5f1617 |
children |
line wrap: on
line source
// This file (ComboBoxUtils.cs) is a part of the IBBoard.GtkSharp project and is copyright 2010 IBBoard // // 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. using System; using System.Collections.Generic; using Gtk; namespace IBBoard.GtkSharp { public delegate string ObjectToStringRenderingMethod<OBJECT_TYPE>(OBJECT_TYPE obj); public class ComboBoxUtils { private static int DEFAULT_VALUE_COLUMN = 1; private static int DEFAULT_TEXT_COLUMN = 0; public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList) { FillCombo(combo, itemList, delegate(LIST_TYPE obj){return obj.ToString();}); } public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList, ObjectToStringRenderingMethod<LIST_TYPE> objectToString) { combo.Clear(); CellRendererText cell = new CellRendererText(); combo.PackStart(cell, false); combo.AddAttribute(cell, "text", 0); Type[] types = new Type[2]; types[DEFAULT_VALUE_COLUMN] = typeof(LIST_TYPE); types[DEFAULT_TEXT_COLUMN] = typeof(string); ListStore store = new ListStore(types); combo.Model = store; foreach (LIST_TYPE item in itemList) { object[] values = new object[2]; values[DEFAULT_VALUE_COLUMN] = item; values[DEFAULT_TEXT_COLUMN] = objectToString(item); store.AppendValues(values); } } public static void SelectItem(ComboBox combo, object item) { TreeModel model = combo.Model; TreeIter iter; model.GetIterFirst(out iter); do { GLib.Value rowItem = new GLib.Value(); combo.Model.GetValue(iter, DEFAULT_VALUE_COLUMN, ref rowItem); if (item.Equals(rowItem.Val)) { combo.SetActiveIter(iter); break; } } while (combo.Model.IterNext(ref iter)); } public static void SelectIndex(ComboBox combo, int idx) { Gtk.TreeIter iter; combo.Model.IterNthChild(out iter, idx); combo.SetActiveIter(iter); } public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo) { return GetSelectedItem<LIST_TYPE>(combo, DEFAULT_VALUE_COLUMN); } public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo, int valueColumn) { TreeIter iter; combo.GetActiveIter(out iter); return (LIST_TYPE)combo.Model.GetValue(iter, valueColumn); } } }