Ran update_copyright.py
[hooke.git] / hooke / ui / gui / panel / selection.py
1 # Copyright (C) 2010 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
25 import wx
26
27 from ....util.callback import callback, in_callback
28
29
30 class SelectionDialog (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,
43                  default=None, selection_style='single', *args, **kwargs):
44         super(Selection, self).__init__(*args, **kwargs)
45
46         self._options = options
47         self._callbacks = callbacks
48         self._selection_style = selection_style
49
50         self._c = {
51             'text': wx.StaticText(
52                 parent=self, label=message, style=wx.ALIGN_CENTRE),
53             'button': wx.Button(parent=self, id=button_id),
54             'cancel': wx.Button(self, wx.ID_CANCEL),
55             }
56         size = wx.Size(175, 200)
57         if selection_style == 'single':
58             self._c['listbox'] = wx.ListBox(
59                 parent=self, size=size, list=options)
60             if default != None:
61                 self._c['listbox'].SetSelection(default)
62         else:
63             assert selection_style == 'multiple', selection_style
64             self._c['listbox'] = wx.CheckListBox(
65                 parent=self, size=size, list=options)
66             if default != None:
67                 self._c['listbox'].Check(default)
68         self.Bind(wx.EVT_BUTTON, self.button, self._c['button'])
69         self.Bind(wx.EVT_BUTTON, self.cancel, self._c['cancel'])
70
71         border_width = 5
72
73         b = wx.BoxSizer(wx.HORIZONTAL)
74         b.Add(window=self._c['button'],
75               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
76               border=border_width)
77         b.Add(window=self._c['cancel'],
78               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
79               border=border_width)
80
81         v = wx.BoxSizer(wx.VERTICAL)
82         v.Add(window=self._c['text'],
83               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
84               border=border_width)
85         v.Add(window=self._c['listbox'],
86               proportion=1,
87               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL,
88               border=border_width)
89         v.Add(window=wx.StaticLine(
90                 parent=self, size=(20,-1), style=wx.LI_HORIZONTAL),
91               flag=wx.GROW,
92               border=border_width)
93         v.Add(window=b,
94               flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,
95               border=border_width)
96         self.SetSizer(v)
97         v.Fit(self)
98
99     @callback
100     def cancel(self, event):
101         """Close the dialog.
102         """
103         self.EndModal(wx.ID_CANCEL)
104
105     def button(self, event):
106         """Call ._button_callback() and close the dialog.
107         """
108         if self._selection_style == 'single':
109             selected = self._c['listbox'].GetSelection()
110         else:
111             assert self._selection_style == 'multiple', self._selection_style
112             selected = self._c['listbox'].GetChecked())
113         in_callback(self, options=self._options, selected=selected)
114         self.EndModal(wx.ID_CLOSE)