Ran update_copyright.py
[hooke.git] / hooke / ui / gui / panel / selection.py
index 4979ad444704714d3304883d9b9d2b999f48a5da..13da12ec416742ab8bc5a3ff3f05edbbdc9906fb 100644 (file)
-# Copyright\r
-\r
-"""Selection dialog.\r
-"""\r
-\r
-from os import remove\r
-\r
-import wx\r
-\r
-\r
-class Selection (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, button_callback, *args, **kwargs):\r
-        super(Selection, self).__init__(*args, **kwargs)\r
-\r
-        self._button_callback = button_callback\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
-\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
-    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
-        """Close the dialog.\r
-        """\r
-        self.EndModal(wx.ID_CANCEL)\r
-\r
-    def _on_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
-        self.EndModal(wx.ID_CLOSE)\r
+# Copyright (C) 2010 Massimo Sandal <devicerandom@gmail.com>
+#                    W. Trevor King <wking@drexel.edu>
+#
+# This file is part of Hooke.
+#
+# Hooke is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# Hooke is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Hooke.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+"""Selection dialog.
+"""
+
+from os import remove
+
+import wx
+
+from ....util.callback import callback, in_callback
+
+
+class SelectionDialog (wx.Dialog):
+    """A selection dialog box.
+
+    Lists options and two buttons.  The first button is setup by the
+    caller.  The second button cancels the dialog.
+
+    The button appearance can be specified by selecting one of the
+    `standard wx IDs`_.
+
+    .. _standard wx IDs:
+      http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid
+    """
+    def __init__(self, options, message, button_id, callbacks,
+                 default=None, selection_style='single', *args, **kwargs):
+        super(Selection, self).__init__(*args, **kwargs)
+
+        self._options = options
+        self._callbacks = callbacks
+        self._selection_style = selection_style
+
+        self._c = {
+            'text': wx.StaticText(
+                parent=self, label=message, style=wx.ALIGN_CENTRE),
+            'button': wx.Button(parent=self, id=button_id),
+            'cancel': wx.Button(self, wx.ID_CANCEL),
+            }
+        size = wx.Size(175, 200)
+        if selection_style == 'single':
+            self._c['listbox'] = wx.ListBox(
+                parent=self, size=size, list=options)
+            if default != None:
+                self._c['listbox'].SetSelection(default)
+        else:
+            assert selection_style == 'multiple', selection_style
+            self._c['listbox'] = wx.CheckListBox(
+                parent=self, size=size, list=options)
+            if default != None:
+                self._c['listbox'].Check(default)
+        self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])
+        self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])
+
+        border_width = 5
+
+        b = wx.BoxSizer(wx.HORIZONTAL)
+        b.Add(window=self._c['button'],
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
+              border=border_width)
+        b.Add(window=self._c['cancel'],
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
+              border=border_width)
+
+        v = wx.BoxSizer(wx.VERTICAL)
+        v.Add(window=self._c['text'],
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
+              border=border_width)
+        v.Add(window=self._c['listbox'],
+              proportion=1,
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
+              border=border_width)
+        v.Add(window=wx.StaticLine(
+                parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
+              flag=wx.GROW,
+              border=border_width)
+        v.Add(window=b,
+              flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,
+              border=border_width)
+        self.SetSizer(v)
+        v.Fit(self)
+
+    @callback
+    def cancel(self, event):
+        """Close the dialog.
+        """
+        self.EndModal(wx.ID_CANCEL)
+
+    def button(self, event):
+        """Call ._button_callback() and close the dialog.
+        """
+        if self._selection_style == 'single':
+            selected = self._c['listbox'].GetSelection()
+        else:
+            assert self._selection_style == 'multiple', self._selection_style
+            selected = self._c['listbox'].GetChecked())
+        in_callback(self, options=self._options, selected=selected)
+        self.EndModal(wx.ID_CLOSE)