1 # Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
3 # This file is part of Hooke.
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
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
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/>.
25 from ....util.callback import callback, in_callback
28 class SelectionDialog (wx.Dialog):
29 """A selection dialog box.
31 Lists options and two buttons. The first button is setup by the
32 caller. The second button cancels the dialog.
34 The button appearance can be specified by selecting one of the
38 http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid
40 def __init__(self, options, message, button_id, callbacks,
41 default=None, selection_style='single', *args, **kwargs):
42 super(Selection, self).__init__(*args, **kwargs)
44 self._options = options
45 self._callbacks = callbacks
46 self._selection_style = selection_style
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),
54 size = wx.Size(175, 200)
55 if selection_style == 'single':
56 self._c['listbox'] = wx.ListBox(
57 parent=self, size=size, list=options)
59 self._c['listbox'].SetSelection(default)
61 assert selection_style == 'multiple', selection_style
62 self._c['listbox'] = wx.CheckListBox(
63 parent=self, size=size, list=options)
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'])
71 b = wx.BoxSizer(wx.HORIZONTAL)
72 b.Add(window=self._c['button'],
73 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
75 b.Add(window=self._c['cancel'],
76 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
79 v = wx.BoxSizer(wx.VERTICAL)
80 v.Add(window=self._c['text'],
81 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
83 v.Add(window=self._c['listbox'],
85 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
87 v.Add(window=wx.StaticLine(
88 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
92 flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,
98 def cancel(self, event):
101 self.EndModal(wx.ID_CANCEL)
103 def button(self, event):
104 """Call ._button_callback() and close the dialog.
106 if self._selection_style == 'single':
107 selected = self._c['listbox'].GetSelection()
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)