1 # Copyright (C) 2010-2011 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
26 from ....util.callback import callback, in_callback
29 class SelectionDialog (wx.Dialog):
30 """A selection dialog box.
32 Lists options and two buttons. The first button is setup by the
33 caller. The second button cancels the dialog.
35 The button appearance can be specified by selecting one of the
39 http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid
41 def __init__(self, options, message, button_id, callbacks,
42 default=None, selection_style='single', *args, **kwargs):
43 super(Selection, self).__init__(*args, **kwargs)
45 self._options = options
46 self._callbacks = callbacks
47 self._selection_style = selection_style
50 'text': wx.StaticText(
51 parent=self, label=message, style=wx.ALIGN_CENTRE),
52 'button': wx.Button(parent=self, id=button_id),
53 'cancel': wx.Button(self, wx.ID_CANCEL),
55 size = wx.Size(175, 200)
56 if selection_style == 'single':
57 self._c['listbox'] = wx.ListBox(
58 parent=self, size=size, list=options)
60 self._c['listbox'].SetSelection(default)
62 assert selection_style == 'multiple', selection_style
63 self._c['listbox'] = wx.CheckListBox(
64 parent=self, size=size, list=options)
66 self._c['listbox'].Check(default)
67 self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])
68 self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])
72 b = wx.BoxSizer(wx.HORIZONTAL)
73 b.Add(window=self._c['button'],
74 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
76 b.Add(window=self._c['cancel'],
77 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
80 v = wx.BoxSizer(wx.VERTICAL)
81 v.Add(window=self._c['text'],
82 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
84 v.Add(window=self._c['listbox'],
86 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
88 v.Add(window=wx.StaticLine(
89 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
93 flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,
99 def cancel(self, event):
102 self.EndModal(wx.ID_CANCEL)
104 def button(self, event):
105 """Call ._button_callback() and close the dialog.
107 if self._selection_style == 'single':
108 selected = self._c['listbox'].GetSelection()
110 assert self._selection_style == 'multiple', self._selection_style
111 selected = self._c['listbox'].GetChecked()
112 in_callback(self, options=self._options, selected=selected)
113 self.EndModal(wx.ID_CLOSE)