Mercurial > repos > IBBoard.GtkSharp
view ComboBoxUtils.cs @ 11:43d0b0ec1657
Re #26: GTK# wrappers
* Set and use indexes for string and value
* Add methods for returning selected value
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 16 Jan 2010 16:14:07 +0000 |
parents | 3277cb2ecb71 |
children | ffda5e5f1617 |
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, 0, ref rowItem); if (item.Equals(rowItem)) { 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); } } }