added flexpaper sourcecode
[swftools.git] / wx / gui / fields.py
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 #
4 # gpdf2swf.py
5 # graphical user interface for pdf2swf
6 #
7 # Part of the swftools package.
8
9 # Copyright (c) 2008,2009 Matthias Kramm <kramm@quiss.org> 
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
24
25 import wx
26 from choicetext import ChoiceInt
27
28 class Option(object):
29     def __init__(self, name, label, data, selection):
30         self.name = name
31         self.label = label
32         self.data = data
33         self.selection = selection
34
35     def _Update(self):
36         self.value = self.ctrl.GetValue()
37
38     def _SetControlValue(self, value):
39         self.value = value
40         self.ctrl.SetValue(value)
41
42     def _OnUpdate(self, event):
43         self._Update()
44         event.Skip()
45
46     def SetValue(self, value):
47         self._SetControlValue(value)
48
49 class Hidden:
50     def __init__(self, name, value):
51         self.name = name
52         self.value = value
53
54     def draw(self, parent):
55         return None, None
56
57     def SetValue(self, value):
58         self.value = value
59
60 class Choose(Option):
61     klass = wx.Choice
62     def __init__(self, name, label, data, selection):
63         Option.__init__(self, name, label, data, selection)
64         self.choices = data[0::2]
65         self.data = data[1::2]
66
67     def draw(self, parent):
68         label = wx.StaticText(parent, label=self.label)
69         self.ctrl = ctrl = self.klass(parent, choices=self.choices)
70         ctrl.Bind(wx.EVT_CHOICE, self._OnUpdate)
71         self._SetControlValue(self.selection)
72         return label, ctrl
73
74     def _Update(self):
75         n = self.ctrl.GetSelection()
76         self.value = self.data[n]
77
78     def _SetControlValue(self, n):
79         self.ctrl.SetSelection(n)
80         self.value = self.data[n]
81
82     def SetValue(self, value):
83         # in python < 2.6 tuples doesnt have the index method
84         # be sure that we are using a list
85         tmp = list(self.data)
86         try:
87             n = tmp.index(value)
88         except ValueError:
89             n = self.selection
90         self._SetControlValue(n)
91
92 class ChooseAndInt(Choose):
93     klass = ChoiceInt
94     def __init__(self, name, label, data, selection, editselection, editvalue):
95         Choose.__init__(self, name, label, data, selection)
96         self.editselection = editselection
97         self.editvalue = editvalue
98
99     def draw(self, parent):
100         label = wx.StaticText(parent, label=self.label)
101         self.ctrl = ctrl = self.klass(parent, choices=self.choices,
102                                       editselection=self.editselection)
103         ctrl.choice.Bind(wx.EVT_CHOICE, self._OnUpdate)
104         ctrl.text.Bind(wx.EVT_TEXT, self._OnUpdate)
105         self._SetControlValue(self.selection)
106         return label, ctrl
107
108     def _Update(self):
109         n = self.ctrl.GetSelection()
110         if  self.ctrl.IsEditableSelection(n):
111             self.value = self.ctrl.GetValue()
112         else:
113             self.value = self.data[n]
114
115     def _SetControlValue(self, n):
116         self.ctrl.SetSelectionAndValue(n, self.editvalue)
117         self.value = self.data[n]
118
119     def SetValue(self, value):
120         # in python < 2.6 tuples doesnt have the index method
121         # be sure that we are using a list
122         tmp = list(self.data)
123         try:
124             n = tmp.index(value)
125         except ValueError:
126             n = self.editselection
127             self.editvalue = value
128         self._SetControlValue(n)
129
130 class Radio(Option):
131     klass = wx.RadioBox
132     def __init__(self, name, label, data, selection):
133         Option.__init__(self, name, label, data, selection)
134         self.choices = data[0::2]
135         self.data = data[1::2]
136         self.selection = selection
137
138     def draw(self, parent):
139         self.ctrl = ctrl = self.klass(parent, label=self.label,
140                                       choices=self.choices,
141                                       majorDimension=1,
142                                       style=wx.RA_SPECIFY_COLS)
143         ctrl.Bind(wx.EVT_RADIOBOX, self._OnUpdate)
144         self._SetControlValue(self.selection)
145         return ctrl
146
147     def _Update(self):
148         n = self.ctrl.GetSelection()
149         self.value = self.data[n]
150
151     def _SetControlValue(self, n):
152         self.ctrl.SetSelection(n)
153         self.value = self.data[n]
154
155     def SetValue(self, value):
156         # in python < 2.6 tuples doesnt have the index method
157         # be sure that we are using a list
158         tmp = list(self.data)
159         try:
160             n = tmp.index(value)
161         except ValueError:
162             n = self.selection
163         self._SetControlValue(n)
164
165 class Spinner(Option):
166     klass = wx.SpinCtrl
167     def __init__(self, name, label, data, selection):
168         Option.__init__(self, name, label, data, selection)
169         self.min = data[0]
170         self.max = data[1]
171
172     def draw(self, parent):
173         label = wx.StaticText(parent, label=self.label)
174         self.ctrl = ctrl = self.klass(parent, min=self.min, max=self.max,
175                                       initial=self.selection)
176         ctrl.Bind(wx.EVT_SPINCTRL, self._OnUpdate)
177         self._SetControlValue(self.selection)
178         return label, ctrl
179
180 class Slider(Option):
181     klass = wx.Slider
182     def __init__(self, name, label, data, selection):
183         Option.__init__(self, name, label, data, selection)
184         self.min = data[0]
185         self.max = data[1]
186
187     def draw(self, parent):
188         label = wx.StaticText(parent, label=self.label)
189         self.ctrl = ctrl = self.klass(parent, minValue=self.min,
190                                       maxValue=self.max,
191                                       value=self.selection,
192                                       style=wx.SL_LABELS)
193         ctrl.Bind(wx.EVT_SCROLL, self._OnUpdate)
194         self._SetControlValue(self.selection)
195         return label, ctrl