/pyface/ui/wx/list_box.py

https://github.com/enthought/pyface · Python · 151 lines · 60 code · 44 blank · 47 comment · 3 complexity · 96242caaed7273f816cd5bf2814c810d MD5 · raw file

  1. # (C) Copyright 2005-2022 Enthought, Inc., Austin, TX
  2. # All rights reserved.
  3. #
  4. # This software is provided without warranty under the terms of the BSD
  5. # license included in LICENSE.txt and may be redistributed only under
  6. # the conditions described in the aforementioned license. The license
  7. # is also available online at http://www.enthought.com/licenses/BSD.txt
  8. #
  9. # Thanks for using Enthought open source!
  10. """ A simple list box widget with a model-view architecture. """
  11. import warnings
  12. import wx
  13. from traits.api import Event, Instance, Int
  14. from pyface.list_box_model import ListBoxModel
  15. from .layout_widget import LayoutWidget
  16. class ListBox(LayoutWidget):
  17. """ A simple list box widget with a model-view architecture. """
  18. # The model that provides the data for the list box.
  19. model = Instance(ListBoxModel)
  20. # The objects currently selected in the list.
  21. selection = Int(-1)
  22. # Events.
  23. # An item has been activated.
  24. item_activated = Event()
  25. # Default style.
  26. STYLE = wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_NEEDED_SB
  27. def __init__(self, parent=None, **traits):
  28. """ Creates a new list box. """
  29. create = traits.pop('create', True)
  30. # Base-class constructors.
  31. super().__init__(parent=parent, **traits)
  32. # Create the widget!
  33. if create:
  34. self.create()
  35. warnings.warn(
  36. "automatic widget creation is deprecated and will be removed "
  37. "in a future Pyface version, use create=False and explicitly "
  38. "call create() for future behaviour",
  39. PendingDeprecationWarning,
  40. )
  41. def _create(self):
  42. super()._create()
  43. self._populate()
  44. # Listen for changes to the model.
  45. self.model.observe(self._on_model_changed, "list_changed")
  46. def dispose(self):
  47. self.model.observe(
  48. self._on_model_changed, "list_changed", remove=True
  49. )
  50. self.model.dispose()
  51. # ------------------------------------------------------------------------
  52. # 'ListBox' interface.
  53. # ------------------------------------------------------------------------
  54. def refresh(self):
  55. """ Refreshes the list box. """
  56. # For now we just clear out the entire list.
  57. self.control.Clear()
  58. # Populate the list.
  59. self._populate()
  60. # ------------------------------------------------------------------------
  61. # wx event handlers.
  62. # ------------------------------------------------------------------------
  63. def _on_item_selected(self, event):
  64. """ Called when an item in the list is selected. """
  65. listbox = event.GetEventObject()
  66. self.selection = listbox.GetSelection()
  67. def _on_item_activated(self, event):
  68. """ Called when an item in the list is activated. """
  69. listbox = event.GetEventObject()
  70. index = listbox.GetSelection()
  71. # Trait event notification.
  72. self.item_activated = index
  73. # ------------------------------------------------------------------------
  74. # Trait handlers.
  75. # ------------------------------------------------------------------------
  76. # Static ---------------------------------------------------------------
  77. def _selection_changed(self, index):
  78. """ Called when the selected item is changed. """
  79. if index != -1:
  80. self.control.SetSelection(index)
  81. # Dynamic -------------------------------------------------------------#
  82. def _on_model_changed(self, event):
  83. """ Called when the model has changed. """
  84. # For now we just clear out the entire list.
  85. self.refresh()
  86. # ------------------------------------------------------------------------
  87. # Private interface.
  88. # ------------------------------------------------------------------------
  89. def _create_control(self, parent):
  90. """ Creates the widget. """
  91. control = wx.ListBox(parent, -1, style=self.STYLE)
  92. # Wire it up!
  93. control.Bind(
  94. wx.EVT_LISTBOX, self._on_item_selected, id=self.control.GetId()
  95. )
  96. control.Bind(
  97. wx.EVT_LISTBOX_DCLICK,
  98. self._on_item_activated,
  99. id=self.control.GetId(),
  100. )
  101. # Populate the list.
  102. return control
  103. def _populate(self):
  104. """ Populates the list box. """
  105. for index in range(self.model.get_item_count()):
  106. label, item = self.model.get_item_at(index)
  107. self.control.Append(label, item)