1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
__doc__ = """GNUmed TextCtrl sbuclass."""
#===================================================
__author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
__license__ = "GPL v2 or later (details at http://www.gnu.org)"
import logging
import sys
import wx
import wx.lib.expando
if __name__ == '__main__':
sys.path.insert(0, '../../')
from Gnumed.pycommon import gmShellAPI
from Gnumed.wxpython import gmKeywordExpansionWidgets
_log = logging.getLogger('gm.txtctrl')
#============================================================
color_tctrl_invalid = 'pink'
color_tctrl_partially_invalid = 'yellow'
class cColoredStatus_TextCtrlMixin():
"""Mixin for setting background color based on validity of content.
Note that due to Python MRO classes using this mixin must
list it before their base class (because we override Enable/Disable).
"""
def __init__(self, *args, **kwargs):
if not isinstance(self, (wx.TextCtrl)):
raise TypeError('[%s]: can only be applied to wx.TextCtrl, not [%s]' % (cColoredStatus_TextCtrlMixin, self.__class__.__name__))
self.__initial_background_color = self.GetBackgroundColour()
self.__previous_enabled_bg_color = self.__initial_background_color
#--------------------------------------------------------
def display_as_valid(self, valid=None):
if valid is True:
color2show = self.__initial_background_color
elif valid is False:
color2show = color_tctrl_invalid
elif valid is None:
color2show = color_tctrl_partially_invalid
else:
raise ValueError('<valid> must be True or False or None')
if self.IsEnabled():
self.SetBackgroundColour(color2show)
self.Refresh()
return
# remember for when it gets enabled
self.__previous_enabled_bg_color = color2show
#--------------------------------------------------------
def display_as_disabled(self, disabled=None):
current_enabled_state = self.IsEnabled()
desired_enabled_state = disabled is False
if current_enabled_state is desired_enabled_state:
return
if disabled is True:
self.__previous_enabled_bg_color = self.GetBackgroundColour()
color2show = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND)
elif disabled is False:
color2show = self.__previous_enabled_bg_color
else:
raise ValueError('<disabled> must be True or False')
self.SetBackgroundColour(color2show)
self.Refresh()
#--------------------------------------------------------
def Disable(self):
self.Enable(enable = False)
#--------------------------------------------------------
def Enable(self, enable=True):
if self.IsEnabled() is enable:
return
wx.TextCtrl.Enable(self, enable)
if enable is True:
self.SetBackgroundColour(self.__previous_enabled_bg_color)
elif enable is False:
self.__previous_enabled_bg_color = self.GetBackgroundColour()
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND))
else:
raise ValueError('<enable> must be True or False')
self.Refresh()
#============================================================
_KNOWN_UNICODE_SELECTORS = [
'kcharselect', # KDE
'gucharmap', # GNOME
'BabelMap.exe', # Windows, supposed to be better than charmap.exe
'charmap.exe', # Microsoft utility
'gm-unicode2clipboard' # generic GNUmed workaround
# Mac OSX supposedly features built-in support
]
class cUnicodeInsertion_TextCtrlMixin():
"""Mixin for inserting unicode characters via selection tool."""
_unicode_selector = None
def __init__(self, *args, **kwargs):
if not isinstance(self, (wx.TextCtrl, wx.stc.StyledTextCtrl)):
raise TypeError('[%s]: can only be applied to wx.TextCtrl or wx.stc.StyledTextCtrl, not [%s]' % (cUnicodeInsertion_TextCtrlMixin, self.__class__.__name__))
if cUnicodeInsertion_TextCtrlMixin._unicode_selector is None:
found, cUnicodeInsertion_TextCtrlMixin._unicode_selector = gmShellAPI.find_first_binary(binaries = _KNOWN_UNICODE_SELECTORS)
if found:
_log.debug('found [%s] for unicode character selection', cUnicodeInsertion_TextCtrlMixin._unicode_selector)
else:
_log.error('no unicode character selection tool found')
#--------------------------------------------------------
def mixin_insert_unicode_character(self):
if cUnicodeInsertion_TextCtrlMixin._unicode_selector is None:
return False
# read clipboard
if wx.TheClipboard.IsOpened():
_log.error('clipboard already open')
return False
if not wx.TheClipboard.Open():
_log.error('cannot open clipboard')
return False
data_obj = wx.TextDataObject()
prev_clip = None
got_it = wx.TheClipboard.GetData(data_obj)
if got_it:
prev_clip = data_obj.Text
# run selector
if not gmShellAPI.run_command_in_shell(command = cUnicodeInsertion_TextCtrlMixin._unicode_selector, blocking = True):
wx.TheClipboard.Close()
return False
# read clipboard again
got_it = wx.TheClipboard.GetData(data_obj)
wx.TheClipboard.Close()
if not got_it:
_log.debug('clipboard does not contain text')
return False
curr_clip = data_obj.Text
# insert clip if so
if curr_clip == prev_clip:
# nothing put into clipboard (that is, clipboard still the same)
return False
self.WriteText(curr_clip)
return True
#============================================================
class cTextSearch_TextCtrlMixin():
"""Code using classes with this mixin must call
show_find_dialog() at appropriate times. Everything
else will be handled.
"""
def __init__(self, *args, **kwargs):
if not isinstance(self, (wx.TextCtrl, wx.stc.StyledTextCtrl)):
raise TypeError('[%s]: can only be applied to wx.TextCtrl or wx.stc.StyledTextCtrl, not [%s]' % (cTextSearch_TextCtrlMixin, self.__class__.__name__))
self.__mixin_find_replace_data = None
self.__mixin_find_replace_dlg = None
self.__mixin_find_replace_last_match_start = 0
self.__mixin_find_replace_last_match_end = 0
self.__mixin_find_replace_last_match_attr = None
#--------------------------------------------------------
def show_find_dialog(self, title=None):
if self.__mixin_find_replace_dlg is not None:
return self.__mixin_find_replace_dlg
self.__mixin_find_replace_last_match_end = 0
if title is None:
title = _('Find text')
self.__mixin_find_replace_data = wx.FindReplaceData()
self.__mixin_find_replace_dlg = wx.FindReplaceDialog (
self,
self.__mixin_find_replace_data,
title,
wx.FR_NOUPDOWN | wx.FR_NOMATCHCASE | wx.FR_NOWHOLEWORD
)
self.__mixin_find_replace_dlg.Bind(wx.EVT_FIND, self._mixin_on_find)
self.__mixin_find_replace_dlg.Bind(wx.EVT_FIND_NEXT, self._mixin_on_find)
self.__mixin_find_replace_dlg.Bind(wx.EVT_FIND_CLOSE, self._mixin_on_find_close)
self.__mixin_find_replace_dlg.Show()
return self.__mixin_find_replace_dlg
#--------------------------------------------------------
# events
#--------------------------------------------------------
def _mixin_on_find(self, evt):
# reset style of previous match
if self.__mixin_find_replace_last_match_attr is not None:
self.SetStyle (
self.__mixin_find_replace_last_match_start,
self.__mixin_find_replace_last_match_end,
self.__mixin_find_replace_last_match_attr
)
# find current match
search_term = self.__mixin_find_replace_data.GetFindString().lower()
match_start = self.Value.lower().find(search_term, self.__mixin_find_replace_last_match_end)
if match_start == -1:
# wrap around
self.__mixin_find_replace_last_match_start = 0
self.__mixin_find_replace_last_match_end = 0
wx.Bell()
return
# remember current match for next time around
attr = wx.TextAttr()
if self.GetStyle(match_start, attr):
self.__mixin_find_replace_last_match_attr = attr
else:
self.__mixin_find_replace_last_match_attr = None
self.__mixin_find_replace_last_match_start = match_start
self.__mixin_find_replace_last_match_end = match_start + len(search_term)
# react to current match
self.Freeze()
self.SetStyle (
self.__mixin_find_replace_last_match_start,
self.__mixin_find_replace_last_match_end,
wx.TextAttr("red", "black")
)
self.ShowPosition(0)
self.ShowPosition(self.__mixin_find_replace_last_match_end)
self.Thaw()
#--------------------------------------------------------
def _mixin_on_find_close(self, evt):
# cleanup after last match if any
if self.__mixin_find_replace_last_match_attr is not None:
self.SetStyle (
self.__mixin_find_replace_last_match_start,
self.__mixin_find_replace_last_match_end,
self.__mixin_find_replace_last_match_attr
)
# deactivate the events
self.__mixin_find_replace_dlg.Unbind(wx.EVT_FIND)
self.__mixin_find_replace_dlg.Unbind(wx.EVT_FIND_NEXT)
self.__mixin_find_replace_dlg.Unbind(wx.EVT_FIND_CLOSE)
# unshow dialog
self.__mixin_find_replace_dlg.DestroyLater()
self.__mixin_find_replace_data = None
self.__mixin_find_replace_dlg = None
self.__mixin_find_replace_last_match_end = 0
#============================================================
class cTextCtrl(gmKeywordExpansionWidgets.cKeywordExpansion_TextCtrlMixin, cTextSearch_TextCtrlMixin, cColoredStatus_TextCtrlMixin, cUnicodeInsertion_TextCtrlMixin, wx.TextCtrl):
def __init__(self, *args, **kwargs):
self._on_set_focus_callbacks = []
self._on_lose_focus_callbacks = []
self._on_modified_callbacks = []
wx.TextCtrl.__init__(self, *args, **kwargs)
gmKeywordExpansionWidgets.cKeywordExpansion_TextCtrlMixin.__init__(self)
cTextSearch_TextCtrlMixin.__init__(self)
cColoredStatus_TextCtrlMixin.__init__(self)
cUnicodeInsertion_TextCtrlMixin.__init__(self)
self.enable_keyword_expansions()
#--------------------------------------------------------
# callback API
#--------------------------------------------------------
def add_callback_on_set_focus(self, callback=None):
"""Add a callback for invocation when getting focus."""
if not callable(callback):
raise ValueError('[add_callback_on_set_focus]: ignoring callback [%s] - not callable' % callback)
self._on_set_focus_callbacks.append(callback)
if len(self._on_set_focus_callbacks) == 1:
self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus)
#---------------------------------------------------------
def add_callback_on_lose_focus(self, callback=None):
"""Add a callback for invocation when losing focus."""
if not callable(callback):
raise ValueError('[add_callback_on_lose_focus]: ignoring callback [%s] - not callable' % callback)
self._on_lose_focus_callbacks.append(callback)
if len(self._on_lose_focus_callbacks) == 1:
self.Bind(wx.EVT_KILL_FOCUS, self._on_lose_focus)
#---------------------------------------------------------
def add_callback_on_modified(self, callback=None):
"""Add a callback for invocation when the content is modified.
This callback will NOT be passed any values.
"""
if not callable(callback):
raise ValueError('[add_callback_on_modified]: ignoring callback [%s] - not callable' % callback)
self._on_modified_callbacks.append(callback)
if len(self._on_modified_callbacks) == 1:
self.Bind(wx.EVT_TEXT, self._on_text_update)
#--------------------------------------------------------
# event handlers
#--------------------------------------------------------
def _on_set_focus(self, event):
event.Skip()
for callback in self._on_set_focus_callbacks:
callback()
return True
#--------------------------------------------------------
def _on_lose_focus(self, event):
"""Do stuff when leaving the control.
The user has had her say, so don't second guess
intentions but do report error conditions.
"""
event.Skip()
wx.CallAfter(self.__on_lost_focus)
return True
#--------------------------------------------------------
def __on_lost_focus(self):
for callback in self._on_lose_focus_callbacks:
callback()
#--------------------------------------------------------
def _on_text_update (self, event):
"""Internal handler for wx.EVT_TEXT.
Called when text was changed by user or by SetValue().
"""
for callback in self._on_modified_callbacks:
callback()
return
#============================================================
# expando based text ctrl classes
#============================================================
class cExpandoTextCtrlHandling_PanelMixin():
"""Mixin for panels wishing to handel expando text ctrls within themselves.
Panels using this mixin will need to call
self.bind_expando_layout_event(<expando_field>)
on each <expando_field> they wish to auto-expand.
"""
#--------------------------------------------------------
def bind_expando_layout_event(self, expando):
self.Bind(wx.lib.expando.EVT_ETC_LAYOUT_NEEDED, self._on_expando_needs_layout)
#--------------------------------------------------------
def _on_expando_needs_layout(self, evt):
# need to tell ourselves to re-Layout to refresh scroll bars
# provoke adding scrollbar if needed
#self.Fit() # works on Linux but not on Windows
self.FitInside() # needed on Windows rather than self.Fit()
if self.HasScrollbar(wx.VERTICAL):
# scroll panel to show cursor
expando = self.FindWindowById(evt.GetId())
y_expando = expando.GetPosition()[1]
h_expando = expando.GetSize()[1]
line_of_cursor = expando.PositionToXY(expando.GetInsertionPoint())[2] + 1
if expando.NumberOfLines == 0:
no_of_lines = 1
else:
no_of_lines = expando.NumberOfLines
y_cursor = int(round((float(line_of_cursor) / no_of_lines) * h_expando))
y_desired_visible = y_expando + y_cursor
y_view = self.GetViewStart()[1]
h_view = self.GetClientSize()[1]
# print "expando:", y_expando, "->", h_expando, ", lines:", expando.NumberOfLines
# print "cursor :", y_cursor, "at line", line_of_cursor, ", insertion point:", expando.GetInsertionPoint()
# print "wanted :", y_desired_visible
# print "view-y :", y_view
# print "scroll2:", h_view
# expando starts before view
if y_desired_visible < y_view:
# print "need to scroll up"
self.Scroll(0, y_desired_visible)
if y_desired_visible > h_view:
# print "need to scroll down"
self.Scroll(0, y_desired_visible)
#============================================================
class cExpandoTextCtrl(gmKeywordExpansionWidgets.cKeywordExpansion_TextCtrlMixin, cTextSearch_TextCtrlMixin, cColoredStatus_TextCtrlMixin, wx.lib.expando.ExpandoTextCtrl):
"""Expando based text ctrl
- auto-sizing on input
- keyword based text expansion
- text search on show_find_dialog()
- (on demand) status based background color
Parent panels should apply the cExpandoTextCtrlHandling_PanelMixin.
"""
def __init__(self, *args, **kwargs):
wx.lib.expando.ExpandoTextCtrl.__init__(self, *args, **kwargs)
gmKeywordExpansionWidgets.cKeywordExpansion_TextCtrlMixin.__init__(self)
cTextSearch_TextCtrlMixin.__init__(self)
cColoredStatus_TextCtrlMixin.__init__(self)
self.__register_interests()
self.enable_keyword_expansions()
#------------------------------------------------
# event handling
#------------------------------------------------
def __register_interests(self):
self.Bind(wx.EVT_SET_FOCUS, self.__cExpandoTextCtrl_on_focus)
#--------------------------------------------------------
def __cExpandoTextCtrl_on_focus(self, evt):
evt.Skip() # allow other processing to happen
wx.CallAfter(self._cExpandoTextCtrl_after_on_focus)
#--------------------------------------------------------
def _cExpandoTextCtrl_after_on_focus(self):
# robustify against Py__DeadObjectError (RuntimeError) - since
# we are called from wx's CallAfter this SoapCtrl may be gone
# by the time we get around to handling this layout request,
# say, on patient change or some such
if not self:
return
#wx. CallAfter(self._adjustCtrl)
evt = wx.PyCommandEvent(wx.lib.expando.wxEVT_ETC_LAYOUT_NEEDED, self.GetId())
evt.SetEventObject(self)
#evt.height = None
#evt.numLines = None
#evt.height = self.GetSize().height
#evt.numLines = self.GetNumberOfLines()
self.GetEventHandler().ProcessEvent(evt)
#------------------------------------------------
# fix platform expando.py if needed
#------------------------------------------------
def _wrapLine(self, line, dc, max_width):
if wx.MAJOR_VERSION > 2:
return wx.lib.expando.ExpandoTextCtrl._wrapLine(self, line, dc, max_width)
if (wx.MAJOR_VERSION == 2) and (wx.MINOR_VERSION > 8):
return wx.lib.expando.ExpandoTextCtrl._wrapLine(self, line, dc, max_width)
# THIS FIX LIFTED FROM TRUNK IN SVN:
# Estimate where the control will wrap the lines and
# return the count of extra lines needed.
partial_text_extents = dc.GetPartialTextExtents(line)
max_width -= wx.SystemSettings.GetMetric(wx.SYS_VSCROLL_X)
idx = 0
start = 0
count_of_extra_lines_needed = 0
idx_of_last_blank = -1
while idx < len(partial_text_extents):
if line[idx] == ' ':
idx_of_last_blank = idx
if (partial_text_extents[idx] - start) > max_width:
# we've reached the max width, add a new line
count_of_extra_lines_needed += 1
# did we see a space? if so restart the count at that pos
if idx_of_last_blank != -1:
idx = idx_of_last_blank + 1
idx_of_last_blank = -1
if idx < len(partial_text_extents):
start = partial_text_extents[idx]
else:
idx += 1
return count_of_extra_lines_needed
#===================================================
# main
#---------------------------------------------------
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit()
if sys.argv[1] != 'test':
sys.exit()
from Gnumed.pycommon import gmI18N
gmI18N.activate_locale()
gmI18N.install_domain(domain='gnumed')
#-----------------------------------------------
def test_gm_textctrl():
app = wx.PyWidgetTester(size = (200, 50))
tc = cTextCtrl(app.frame, -1)
#tc.enable_keyword_expansions()
#tc.Enable(False)
app.frame.Show(True)
app.MainLoop()
return True
#-----------------------------------------------
test_gm_textctrl()
|