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