1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
""" A simple list box widget with a model-view architecture. """
from __future__ import absolute_import
# Major package imports.
import wx
# Enthought library imports.
from traits.api import Event, Instance, Int
# Local imports.
from pyface.list_box_model import ListBoxModel
from .widget import Widget
class ListBox(Widget):
""" A simple list box widget with a model-view architecture. """
# The model that provides the data for the list box.
model = Instance(ListBoxModel)
# The objects currently selected in the list.
selection = Int(-1)
# Events.
# An item has been activated.
item_activated = Event
# Default style.
STYLE = wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_NEEDED_SB
def __init__(self, parent, **traits):
""" Creates a new list box. """
# Base-class constructors.
super(ListBox, self).__init__(**traits)
# Create the widget!
self._create_control(parent)
# Listen for changes to the model.
self.model.on_trait_change(self._on_model_changed, "list_changed")
return
def dispose(self):
self.model.on_trait_change(self._on_model_changed, "list_changed",
remove = True)
self.model.dispose()
return
###########################################################################
# 'ListBox' interface.
###########################################################################
def refresh(self):
""" Refreshes the list box. """
# For now we just clear out the entire list.
self.control.Clear()
# Populate the list.
self._populate()
return
###########################################################################
# wx event handlers.
###########################################################################
def _on_item_selected(self, event):
""" Called when an item in the list is selected. """
listbox = event.GetEventObject()
self.selection = listbox.GetSelection()
return
def _on_item_activated(self, event):
""" Called when an item in the list is activated. """
listbox = event.GetEventObject()
index = listbox.GetSelection()
# Trait event notification.
self.item_activated = index
return
###########################################################################
# Trait handlers.
###########################################################################
#### Static ###############################################################
def _selection_changed(self, index):
""" Called when the selected item is changed. """
if index != -1:
self.control.SetSelection(index)
return
#### Dynamic ##############################################################
def _on_model_changed(self, event):
""" Called when the model has changed. """
# For now we just clear out the entire list.
self.refresh()
return
###########################################################################
# Private interface.
###########################################################################
def _create_control(self, parent):
""" Creates the widget. """
self.control = wx.ListBox(parent, -1, style = self.STYLE)
# Wire it up!
wx.EVT_LISTBOX(self.control, self.control.GetId(),
self._on_item_selected)
wx.EVT_LISTBOX_DCLICK(self.control, self.control.GetId(),
self._on_item_activated)
# Populate the list.
self._populate()
return
def _populate(self):
""" Populates the list box. """
for index in range(self.model.get_item_count()):
label, item = self.model.get_item_at(index)
self.control.Append(label, item)
return
#### EOF ######################################################################
|