hooke.ui.gui was getting complicated, so I stripped it down for a moment.
[hooke.git] / hooke / ui / gui / dialog / selection.py
similarity index 57%
rename from hooke/ui/gui/panel/selection.py
rename to hooke/ui/gui/dialog/selection.py
index 4979ad444704714d3304883d9b9d2b999f48a5da..15baec2cccee822f93ac00d5d37ef4b8769daaff 100644 (file)
@@ -7,6 +7,8 @@ from os import remove
 \r
 import wx\r
 \r
+from ....util.callback import callback, in_callback\r
+\r
 \r
 class Selection (wx.Dialog):\r
     """A selection dialog box.\r
@@ -20,22 +22,34 @@ class Selection (wx.Dialog):
     .. _standard wx IDs:\r
       http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid\r
     """\r
-    def __init__(self, options, message, button_id, button_callback, *args, **kwargs):\r
+    def __init__(self, options, message, button_id, callbacks,\r
+                 default=None, selection_style='single', *args, **kwargs):\r
         super(Selection, self).__init__(*args, **kwargs)\r
 \r
-        self._button_callback = button_callback\r
+        self._options = options\r
+        self._callbacks = callbacks\r
+        self._selection_style = selection_style\r
 \r
         self._c = {\r
             'text': wx.StaticText(\r
                 parent=self, label=message, style=wx.ALIGN_CENTRE),\r
-            'listbox': wx.CheckListBox(\r
-                parent=self, size=wx.Size(175, 200), list=options),\r
             'button': wx.Button(parent=self, id=button_id),\r
             'cancel': wx.Button(self, wx.ID_CANCEL),\r
             }\r
-        self.Bind(wx.EVT_CHECKLISTBOX, self._on_check, self._c['listbox'])\r
-        self.Bind(wx.EVT_BUTTON, self._on_button, self._c['button'])\r
-        self.Bind(wx.EVT_BUTTON, self._on_cancel, self._c['cancel'])\r
+        size = wx.Size(175, 200)\r
+        if selection_style == 'single':\r
+            self._c['listbox'] = wx.ListBox(\r
+                parent=self, size=size, list=options)\r
+            if default != None:\r
+                self._c['listbox'].SetSelection(default)\r
+        else:\r
+            assert selection_style == 'multiple', selection_style\r
+            self._c['listbox'] = wx.CheckListBox(\r
+                parent=self, size=size, list=options)\r
+            if default != None:\r
+                self._c['listbox'].Check(default)\r
+        self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])\r
+        self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])\r
 \r
         border_width = 5\r
 \r
@@ -65,22 +79,19 @@ class Selection (wx.Dialog):
         self.SetSizer(v)\r
         v.Fit(self)\r
 \r
-    def _on_check(self, event):\r
-        """Refocus on the first checked item.\r
-        """\r
-        index = event.GetSelection()\r
-        self.listbox.SetSelection(index)\r
-\r
-    def _on_cancel(self, event):\r
+    @callback\r
+    def cancel(self, event):\r
         """Close the dialog.\r
         """\r
         self.EndModal(wx.ID_CANCEL)\r
 \r
-    def _on_button(self, event):\r
+    def button(self, event):\r
         """Call ._button_callback() and close the dialog.\r
         """\r
-        self._button_callback(\r
-            event=event,\r
-            items=self._c['listbox'].GetItems(),\r
-            selected_items=self._c['listbox'].GetChecked())\r
+        if self._selection_style == 'single':\r
+            selected = self._c['listbox'].GetSelection()\r
+        else:\r
+            assert self._selection_style == 'multiple', self._selection_style\r
+            selected = self._c['listbox'].GetChecked())\r
+        in_callback(self, options=self._options, selected=selected)\r
         self.EndModal(wx.ID_CLOSE)\r