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/>.
26 from ....util.callback import callback, in_callback
29 class Selection (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=None,
42 default=None, selection_style='single', *args, **kwargs):
43 super(Selection, self).__init__(*args, **kwargs)
45 self._options = options
48 self._callbacks = callbacks
49 self._selection_style = selection_style
53 'text': wx.StaticText(
54 parent=self, label=message, style=wx.ALIGN_CENTRE),
55 'button': wx.Button(parent=self, id=button_id),
56 'cancel': wx.Button(self, wx.ID_CANCEL),
58 size = wx.Size(175, 200)
59 if selection_style == 'single':
60 self._c['listbox'] = wx.ListBox(
61 parent=self, size=size, choices=options)
63 self._c['listbox'].SetSelection(default)
65 assert selection_style == 'multiple', selection_style
66 self._c['listbox'] = wx.CheckListBox(
67 parent=self, size=size, choices=options)
69 self._c['listbox'].Check(default)
70 self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])
71 self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])
73 b = wx.BoxSizer(wx.HORIZONTAL)
74 self._add(b, 'button')
75 self._add(b, 'cancel')
76 v = wx.BoxSizer(wx.VERTICAL)
78 self._add(v, 'listbox')
79 self._add(v, wx.StaticLine(
80 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
86 def _add(self, sizer, item,
87 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
89 kwargs = {'flag':flag, 'border':border}
90 if isinstance(item, types.StringTypes):
92 kwargs['item'] = item # window
96 def cancel(self, event):
100 self.EndModal(wx.ID_CANCEL)
102 def button(self, event):
103 """Call ._button_callback() and close the dialog.
105 if self._selection_style == 'single':
106 selected = self._c['listbox'].GetSelection()
108 assert self._selection_style == 'multiple', self._selection_style
109 selected = self._c['listbox'].GetChecked()
110 self.selected = selected
111 in_callback(self, options=self._options, selected=selected)
112 self.EndModal(wx.ID_CLOSE)