1 """GNUmed TextCtrl sbuclass."""
2
3 __author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
4 __license__ = "GPL v2 or later (details at http://www.gnu.org)"
5
6 import logging
7 import sys
8
9
10 import wx
11
12
13 if __name__ == '__main__':
14 sys.path.insert(0, '../../')
15
16 from Gnumed.wxpython import gmKeywordExpansionWidgets
17
18
19 _log = logging.getLogger('gm.txtctrl')
20
21
22 color_tctrl_invalid = 'pink'
23 color_tctrl_partially_invalid = 'yellow'
24
25 -class cTextCtrl(wx.TextCtrl, gmKeywordExpansionWidgets.cKeywordExpansion_TextCtrlMixin):
26
27 - def __init__(self, *args, **kwargs):
28
29 wx.TextCtrl.__init__(self, *args, **kwargs)
30
31 self._on_set_focus_callbacks = []
32 self._on_lose_focus_callbacks = []
33 self._on_modified_callbacks = []
34
35 self.__initial_background_color = self.GetBackgroundColour()
36
37 gmKeywordExpansionWidgets.cKeywordExpansion_TextCtrlMixin.__init__(self)
38 self.enable_keyword_expansions()
39
40
41
42
43 - def add_callback_on_set_focus(self, callback=None):
44 """Add a callback for invocation when getting focus."""
45 if not callable(callback):
46 raise ValueError('[add_callback_on_set_focus]: ignoring callback [%s] - not callable' % callback)
47
48 self._on_set_focus_callbacks.append(callback)
49 if len(self._on_set_focus_callbacks) == 1:
50 self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus)
51
52 - def add_callback_on_lose_focus(self, callback=None):
53 """Add a callback for invocation when losing focus."""
54 if not callable(callback):
55 raise ValueError('[add_callback_on_lose_focus]: ignoring callback [%s] - not callable' % callback)
56
57 self._on_lose_focus_callbacks.append(callback)
58 if len(self._on_lose_focus_callbacks) == 1:
59 self.Bind(wx.EVT_KILL_FOCUS, self._on_lose_focus)
60
61 - def add_callback_on_modified(self, callback=None):
62 """Add a callback for invocation when the content is modified.
63
64 This callback will NOT be passed any values.
65 """
66 if not callable(callback):
67 raise ValueError('[add_callback_on_modified]: ignoring callback [%s] - not callable' % callback)
68
69 self._on_modified_callbacks.append(callback)
70 if len(self._on_modified_callbacks) == 1:
71 self.Bind(wx.EVT_TEXT, self._on_text_update)
72
73
74
75
76 - def display_as_valid(self, valid=None, partially_invalid=False):
77 if valid is True:
78 self.SetBackgroundColour(self.__initial_background_color)
79 elif valid is False:
80 if partially_invalid:
81 self.SetBackgroundColour(color_tctrl_partially_invalid)
82 else:
83 self.SetBackgroundColour(color_tctrl_invalid)
84 else:
85 raise ValueError(u'<valid> must be True or False')
86 self.Refresh()
87
88 - def display_as_disabled(self, disabled=None):
89 if disabled is True:
90 self.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_BACKGROUND))
91 elif disabled is False:
92 self.SetBackgroundColour(self.__initial_background_color)
93 else:
94 raise ValueError(u'<disabled> must be True or False')
95 self.Refresh()
96
97
98
99 - def _on_set_focus(self, event):
100 event.Skip()
101 for callback in self._on_set_focus_callbacks:
102 callback()
103 return True
104
105 - def _on_lose_focus(self, event):
106 """Do stuff when leaving the control.
107
108 The user has had her say, so don't second guess
109 intentions but do report error conditions.
110 """
111 event.Skip()
112 wx.CallAfter(self.__on_lost_focus)
113 return True
114
115 - def __on_lost_focus(self):
116 for callback in self._on_lose_focus_callbacks:
117 callback()
118
119 - def _on_text_update (self, event):
120 """Internal handler for wx.EVT_TEXT.
121
122 Called when text was changed by user or by SetValue().
123 """
124 for callback in self._on_modified_callbacks:
125 callback()
126 return
127
128
129
130
131 if __name__ == '__main__':
132
133 if len(sys.argv) < 2:
134 sys.exit()
135
136 if sys.argv[1] != u'test':
137 sys.exit()
138
139 from Gnumed.pycommon import gmI18N
140 gmI18N.activate_locale()
141 gmI18N.install_domain(domain='gnumed')
142
143
145 app = wx.PyWidgetTester(size = (200, 50))
146 tc = cTextCtrl(parent = app.frame, id = -1)
147
148 app.frame.Show(True)
149 app.MainLoop()
150 return True
151
152 test_gm_textctrl()
153
154
155