test/data/vclamp_jpk/README: Document sample versions
[hooke.git] / hooke / ui / gui / panel / selection.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Selection dialog.
19 """
20
21 from os import remove
22
23 import wx
24
25 from ....util.callback import callback, in_callback
26
27
28 class SelectionDialog (wx.Dialog):
29     """A selection dialog box.
30
31     Lists options and two buttons.  The first button is setup by the
32     caller.  The second button cancels the dialog.
33
34     The button appearance can be specified by selecting one of the
35     `standard wx IDs`_.
36
37     .. _standard wx IDs:
38       http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid
39     """
40     def __init__(self, options, message, button_id, callbacks,
41                  default=None, selection_style='single', *args, **kwargs):
42         super(Selection, self).__init__(*args, **kwargs)
43
44         self._options = options
45         self._callbacks = callbacks
46         self._selection_style = selection_style
47
48         self._c = {
49             'text': wx.StaticText(
50                 parent=self, label=message, style=wx.ALIGN_CENTRE),
51             'button': wx.Button(parent=self, id=button_id),
52             'cancel': wx.Button(self, wx.ID_CANCEL),
53             }
54         size = wx.Size(175, 200)
55         if selection_style == 'single':
56             self._c['listbox'] = wx.ListBox(
57                 parent=self, size=size, list=options)
58             if default != None:
59                 self._c['listbox'].SetSelection(default)
60         else:
61             assert selection_style == 'multiple', selection_style
62             self._c['listbox'] = wx.CheckListBox(
63                 parent=self, size=size, list=options)
64             if default != None:
65                 self._c['listbox'].Check(default)
66         self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])
67         self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])
68
69         border_width = 5
70
71         b = wx.BoxSizer(wx.HORIZONTAL)
72         b.Add(window=self._c['button'],
73               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
74               border=border_width)
75         b.Add(window=self._c['cancel'],
76               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
77               border=border_width)
78
79         v = wx.BoxSizer(wx.VERTICAL)
80         v.Add(window=self._c['text'],
81               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
82               border=border_width)
83         v.Add(window=self._c['listbox'],
84               proportion=1,
85               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
86               border=border_width)
87         v.Add(window=wx.StaticLine(
88                 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
89               flag=wx.GROW,
90               border=border_width)
91         v.Add(window=b,
92               flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,
93               border=border_width)
94         self.SetSizer(v)
95         v.Fit(self)
96
97     @callback
98     def cancel(self, event):
99         """Close the dialog.
100         """
101         self.EndModal(wx.ID_CANCEL)
102
103     def button(self, event):
104         """Call ._button_callback() and close the dialog.
105         """
106         if self._selection_style == 'single':
107             selected = self._c['listbox'].GetSelection()
108         else:
109             assert self._selection_style == 'multiple', self._selection_style
110             selected = self._c['listbox'].GetChecked()
111         in_callback(self, options=self._options, selected=selected)
112         self.EndModal(wx.ID_CLOSE)