5421e29968f938199a0d58c34ee635bf5b35a3f8
[hooke.git] / hooke / ui / gui / dialog / selection.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
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 import types
23
24 import wx
25
26 from ....util.callback import callback, in_callback
27
28
29 class Selection (wx.Dialog):
30     """A selection dialog box.
31
32     Lists options and two buttons.  The first button is setup by the
33     caller.  The second button cancels the dialog.
34
35     The button appearance can be specified by selecting one of the
36     `standard wx IDs`_.
37
38     .. _standard wx IDs:
39       http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid
40     """
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)
44
45         self._options = options
46         if callbacks == None:
47             callbacks = {}
48         self._callbacks = callbacks
49         self._selection_style = selection_style
50         self.canceled = False
51
52         self._c = {
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),
57             }
58         size = wx.Size(175, 200)
59         if selection_style == 'single':
60             self._c['listbox'] = wx.ListBox(
61                 parent=self, size=size, choices=options)
62             if default != None:
63                 self._c['listbox'].SetSelection(default)
64         else:
65             assert selection_style == 'multiple', selection_style
66             self._c['listbox'] = wx.CheckListBox(
67                 parent=self, size=size, choices=options)
68             if default != None:
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'])
72
73         b = wx.BoxSizer(wx.HORIZONTAL)
74         self._add(b, 'button')
75         self._add(b, 'cancel')
76         v = wx.BoxSizer(wx.VERTICAL)
77         self._add(v, 'text')
78         self._add(v, 'listbox')
79         self._add(v, wx.StaticLine(
80                 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
81                   flag=wx.GROW)
82         self._add(v, b)
83         self.SetSizer(v)
84         v.Fit(self)
85
86     def _add(self, sizer, item,
87             flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
88             border=5):
89         kwargs = {'flag':flag, 'border':border}
90         if isinstance(item, types.StringTypes):
91             item = self._c[item]
92         kwargs['item'] = item # window
93         sizer.Add(**kwargs)
94
95     @callback
96     def cancel(self, event):
97         """Close the dialog.
98         """
99         self.canceled = True
100         self.EndModal(wx.ID_CANCEL)
101
102     def button(self, event):
103         """Call ._button_callback() and close the dialog.
104         """
105         if self._selection_style == 'single':
106             selected = self._c['listbox'].GetSelection()
107         else:
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)