Ran update_copyright.py.
[hooke.git] / hooke / ui / gui / dialog / selection.py
1 # Copyright (C) 2010-2011 Massimo Sandal <devicerandom@gmail.com>
2 #                         W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
14 # Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke.  If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 """Selection dialog.
21 """
22
23 from os import remove
24 import types
25
26 import wx
27
28 from ....util.callback import callback, in_callback
29
30
31 class Selection (wx.Dialog):
32     """A selection dialog box.
33
34     Lists options and two buttons.  The first button is setup by the
35     caller.  The second button cancels the dialog.
36
37     The button appearance can be specified by selecting one of the
38     `standard wx IDs`_.
39
40     .. _standard wx IDs:
41       http://docs.wxwidgets.org/stable/wx_stdevtid.html#stdevtid
42     """
43     def __init__(self, options, message, button_id, callbacks=None,
44                  default=None, selection_style='single', *args, **kwargs):
45         super(Selection, self).__init__(*args, **kwargs)
46
47         self._options = options
48         if callbacks == None:
49             callbacks = {}
50         self._callbacks = callbacks
51         self._selection_style = selection_style
52         self.canceled = False
53
54         self._c = {
55             'text': wx.StaticText(
56                 parent=self, label=message, style=wx.ALIGN_CENTRE),
57             'button': wx.Button(parent=self, id=button_id),
58             'cancel': wx.Button(self, wx.ID_CANCEL),
59             }
60         size = wx.Size(175, 200)
61         if selection_style == 'single':
62             self._c['listbox'] = wx.ListBox(
63                 parent=self, size=size, choices=options)
64             if default != None:
65                 self._c['listbox'].SetSelection(default)
66         else:
67             assert selection_style == 'multiple', selection_style
68             self._c['listbox'] = wx.CheckListBox(
69                 parent=self, size=size, choices=options)
70             if default != None:
71                 self._c['listbox'].Check(default)
72         self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])
73         self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])
74
75         b = wx.BoxSizer(wx.HORIZONTAL)
76         self._add(b, 'button')
77         self._add(b, 'cancel')
78         v = wx.BoxSizer(wx.VERTICAL)
79         self._add(v, 'text')
80         self._add(v, 'listbox')
81         self._add(v, wx.StaticLine(
82                 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
83                   flag=wx.GROW)
84         self._add(v, b)
85         self.SetSizer(v)
86         v.Fit(self)
87
88     def _add(self, sizer, item,
89             flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
90             border=5):
91         kwargs = {'flag':flag, 'border':border}
92         if isinstance(item, types.StringTypes):
93             item = self._c[item]
94         kwargs['item'] = item # window
95         sizer.Add(**kwargs)
96
97     @callback
98     def cancel(self, event):
99         """Close the dialog.
100         """
101         self.canceled = True
102         self.EndModal(wx.ID_CANCEL)
103
104     def button(self, event):
105         """Call ._button_callback() and close the dialog.
106         """
107         if self._selection_style == 'single':
108             selected = self._c['listbox'].GetSelection()
109         else:
110             assert self._selection_style == 'multiple', self._selection_style
111             selected = self._c['listbox'].GetChecked()
112         self.selected = selected
113         in_callback(self, options=self._options, selected=selected)
114         self.EndModal(wx.ID_CLOSE)