|
From: Kevin A. <al...@se...> - 2001-11-14 23:07:49
|
Simon and I spent quite a bit of time today playing with cross-platform
(Windows, Linux) issues with TextArea. The PythonCard TextArea is really
just a wxPython wxTextCtrl with style flags: wxTE_PROCESS_TAB,
wxTE_MULTILINE.
I fixed a bug in the TextArea event handling, so that you can intercept a
key character such as tab and process it in your app. The textRouter sample
has the following routine that inserts a tab character into the field when
you press TAB rather than changing the focus to the next widget.
def on_area1_keyPress(self, target, event):
nEvent = event.getNativeEvent()
if nEvent.GetKeyCode() == 9:
target.replaceSelection("\t")
else:
event.skip()
I ended up putting all the platform code into a replaceSelection method in
widget.py which is shown at the end of this message. replaceSelection has
two purposes. The default is to act on the current selection in the field
replacing the text with the string passed in. The behavior should be
identical to what you would get if you had pasted the string from the
clipboard and should respect newlines in the selection as well as the
replacement string. The insertion point is the selection if there is no
highlighted text. By using a keyPress handler and replaceSelection, you
should be able to do most editor operations, though some of the bindings you
might want to handle with a menu item and accelerator key (e.g. Ctrl-B)
instead of a keyPress handler.
The second form (select=1) selects the text after the replacing the
selection. You would typically use this form during a find/replace
operation.
Anyway, as you can see we had to handle the operations differently depending
on the platform. Consequently, there might be bugs in how the routine works
or the code might be more complicated than it needs to be. Under Windows,
you have to compensate for newlines in the text and you don't have to do
that under Linux. There are also some differences in how the insertion point
and selection are handled, so we'll be doing further tests. If you run into
any problems or see unexpected behavior, please report it to the list.
ka
---
from widget.py, class TextField
def replaceSelection(self, aString, select=0):
if wxPlatform == "__WXMSW__":
if select:
sel = self._delegate.GetSelection()
numNewlines = aString.count('\n')
self._delegate.WriteText(aString)
self._delegate.SetSelection( sel[0], sel[0] + len(aString) +
numNewlines)
else:
self._delegate.WriteText(aString)
else:
# Linux
sel = self._delegate.GetSelection()
self._delegate.Remove(sel[0], sel[1])
self._delegate.WriteText(aString)
if select:
self._delegate.SetSelection(sel[0], sel[0] + len(aString))
|