Oops, meant to include hooke.ui.gui.panel.selection in last commit.
authorW. Trevor King <wking@drexel.edu>
Tue, 27 Jul 2010 14:39:21 +0000 (10:39 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 27 Jul 2010 14:39:21 +0000 (10:39 -0400)
hooke/ui/gui/panel/selection.py [new file with mode: 0644]

diff --git a/hooke/ui/gui/panel/selection.py b/hooke/ui/gui/panel/selection.py
new file mode 100644 (file)
index 0000000..a42ecb3
--- /dev/null
@@ -0,0 +1,97 @@
+# Copyright\r
+\r
+"""Selection dialog.\r
+"""\r
+\r
+from os import remove\r
+\r
+import wx\r
+\r
+from ....util.callback import callback, in_callback\r
+\r
+\r
+class SelectionDialog (wx.Dialog):\r
+    """A selection dialog box.\r
+\r
+    Lists options and two buttons.  The first button is setup by the\r
+    caller.  The second button cancels the dialog.\r
+\r
+    The button appearance can be specified by selecting one of the\r
+    `standard wx IDs`_.\r
+\r
+    .. _standard wx IDs:\r
+      http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid\r
+    """\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._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
+            'button': wx.Button(parent=self, id=button_id),\r
+            'cancel': wx.Button(self, wx.ID_CANCEL),\r
+            }\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
+        b = wx.BoxSizer(wx.HORIZONTAL)\r
+        b.Add(window=self._c['button'],\r
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,\r
+              border=border_width)\r
+        b.Add(window=self._c['cancel'],\r
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,\r
+              border=border_width)\r
+\r
+        v = wx.BoxSizer(wx.VERTICAL)\r
+        v.Add(window=self._c['text'],\r
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,\r
+              border=border_width)\r
+        v.Add(window=self._c['listbox'],\r
+              proportion=1,\r
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,\r
+              border=border_width)\r
+        v.Add(window=wx.StaticLine(\r
+                parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),\r
+              flag=wx.GROW,\r
+              border=border_width)\r
+        v.Add(window=b,\r
+              flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,\r
+              border=border_width)\r
+        self.SetSizer(v)\r
+        v.Fit(self)\r
+\r
+    @callback\r
+    def cancel(self, event):\r
+        """Close the dialog.\r
+        """\r
+        self.EndModal(wx.ID_CANCEL)\r
+\r
+    def button(self, event):\r
+        """Call ._button_callback() and close the dialog.\r
+        """\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