This commit is contained in:
Sam Parkinson 2016-04-06 07:15:21 +10:00
commit ab0f5a18aa
2 changed files with 61 additions and 21 deletions

View File

@ -3,27 +3,20 @@ from gi.repository import Gtk
from sugar3.graphics.combobox import ComboBox from sugar3.graphics.combobox import ComboBox
def _destroy_cb(widget, data=None): def __combo_changed_cb(combo):
Gtk.main_quit() print 'Combo changed to %r' % combo.get_value()
def __combo_changed_cb(widget, data=None):
print 'combo-changed'
w = Gtk.Window() w = Gtk.Window()
w.connect("destroy", _destroy_cb) w.connect("destroy", Gtk.main_quit)
box = Gtk.VBox()
w.add(box)
combo = ComboBox() combo = ComboBox()
combo.append_item(0, 'one') combo.append_item(True, 'one')
combo.append_item(1, 'two', 'go-next') combo.append_item(2, 'two', 'go-next')
combo.append_item(2, 'three') combo.append_item('3', 'three')
# This will make 'two' active (zero indexed)
combo.set_active(1) combo.set_active(1)
combo.connect('changed', __combo_changed_cb) combo.connect('changed', __combo_changed_cb)
box.pack_start(combo, False, False, 0) w.add(combo)
w.show_all() w.show_all()
Gtk.main() Gtk.main()

View File

@ -15,9 +15,15 @@
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA. # Boston, MA 02111-1307, USA.
""" '''
STABLE. The combobox module provides a combo box; a button like widget which
""" creates a list popup when clicked. It's best used outside of a
toolbar when the user needs to choose from a *short* list of items.
Example:
.. literalinclude:: ../examples/combobox.py
'''
from gi.repository import GObject from gi.repository import GObject
from gi.repository import Gtk from gi.repository import Gtk
@ -25,7 +31,11 @@ from gi.repository import GdkPixbuf
class ComboBox(Gtk.ComboBox): class ComboBox(Gtk.ComboBox):
'''
This class provides a simple wrapper based on the :class:`Gtk.ComboBox`.
This lets you make a list of items, with a value, label and optionally an
icon.
'''
__gtype_name__ = 'SugarComboBox' __gtype_name__ = 'SugarComboBox'
def __init__(self): def __init__(self):
@ -43,6 +53,13 @@ class ComboBox(Gtk.ComboBox):
self.set_row_separator_func(self._is_separator, None) self.set_row_separator_func(self._is_separator, None)
def get_value(self): def get_value(self):
'''
The value of the currently selected item; the same as the `value`
argument that was passed to the `append_item` func.
Returns:
object, value of selected item
'''
row = self.get_active_item() row = self.get_active_item()
if not row: if not row:
return None return None
@ -61,7 +78,22 @@ class ComboBox(Gtk.ComboBox):
del info del info
return fname return fname
def append_item(self, action_id, text, icon_name=None, file_name=None): def append_item(self, value, text, icon_name=None, file_name=None):
'''
This function adds another item to the bottom of the combo box list.
If either `icon_name` or `file_name` are supplied and icon column
will be added to the combo box list.
Args:
value (object): the value that will be returned by `get_value`,
when this item is selected
text (str): the user visible label for the item
icon_name (str): the name of the icon in the theme to use for
this item, optional and conflicting with `file_name`
file_name (str): the path to a sugar (svg) icon to use for this
item, optional and conflicting with `icon_name`
'''
if not self._icon_renderer and (icon_name or file_name): if not self._icon_renderer and (icon_name or file_name):
self._icon_renderer = Gtk.CellRendererPixbuf() self._icon_renderer = Gtk.CellRendererPixbuf()
@ -93,12 +125,24 @@ class ComboBox(Gtk.ComboBox):
else: else:
pixbuf = None pixbuf = None
self._model.append([action_id, text, pixbuf, False]) self._model.append([value, text, pixbuf, False])
def append_separator(self): def append_separator(self):
'''
Add a separator to the bottom of the combo box list. The separator
can not be selected.
'''
self._model.append([0, None, None, True]) self._model.append([0, None, None, True])
def get_active_item(self): def get_active_item(self):
'''
Get the row of data for the currently selected item.
Returns:
row of data in the format::
[value, text, pixbuf, is_separator]
'''
index = self.get_active() index = self.get_active()
if index == -1: if index == -1:
index = 0 index = 0
@ -109,6 +153,9 @@ class ComboBox(Gtk.ComboBox):
return self._model[row] return self._model[row]
def remove_all(self): def remove_all(self):
'''
Remove all list items from the combo box.
'''
self._model.clear() self._model.clear()
def _is_separator(self, model, row, data): def _is_separator(self, model, row, data):