6bf8a649a433c58a108f8661341fdb0f91ba3d41
[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
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.
9 #
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.
14 #
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/>.
18
19 """Selection dialog.
20 """
21
22 from os import remove
23 import types
24
25 import wx
26
27 from ....util.callback import callback, in_callback
28
29
30 class Selection (wx.Dialog):
31     """A selection dialog box.
32
33     Lists options and two buttons.  The first button is setup by the
34     caller.  The second button cancels the dialog.
35
36     The button appearance can be specified by selecting one of the
37     `standard wx IDs`_.
38
39     .. _standard wx IDs:
40       http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid
41     """
42     def __init__(self, options, message, button_id, callbacks=None,
43                  default=None, selection_style='single', *args, **kwargs):
44         super(Selection, self).__init__(*args, **kwargs)
45
46         self._options = options
47         if callbacks == None:
48             callbacks = {}
49         self._callbacks = callbacks
50         self._selection_style = selection_style
51         self.canceled = False
52
53         self._c = {
54             'text': wx.StaticText(
55                 parent=self, label=message, style=wx.ALIGN_CENTRE),
56             'button': wx.Button(parent=self, id=button_id),
57             'cancel': wx.Button(self, wx.ID_CANCEL),
58             }
59         size = wx.Size(175, 200)
60         if selection_style == 'single':
61             self._c['listbox'] = wx.ListBox(
62                 parent=self, size=size, choices=options)
63             if default != None:
64                 self._c['listbox'].SetSelection(default)
65         else:
66             assert selection_style == 'multiple', selection_style
67             self._c['listbox'] = wx.CheckListBox(
68                 parent=self, size=size, choices=options)
69             if default != None:
70                 self._c['listbox'].Check(default)
71         self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])
72         self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])
73
74         b = wx.BoxSizer(wx.HORIZONTAL)
75         self._add(b, 'button')
76         self._add(b, 'cancel')
77         v = wx.BoxSizer(wx.VERTICAL)
78         self._add(v, 'text')
79         self._add(v, 'listbox')
80         self._add(v, wx.StaticLine(
81                 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
82                   flag=wx.GROW)
83         self._add(v, b)
84         self.SetSizer(v)
85         v.Fit(self)
86
87     def _add(self, sizer, item,
88             flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
89             border=5):
90         kwargs = {'flag':flag, 'border':border}
91         if isinstance(item, types.StringTypes):
92             item = self._c[item]
93         kwargs['item'] = item # window
94         sizer.Add(**kwargs)
95
96     @callback
97     def cancel(self, event):
98         """Close the dialog.
99         """
100         self.canceled = True
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         self.selected = selected
112         in_callback(self, options=self._options, selected=selected)
113         self.EndModal(wx.ID_CLOSE)