1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.editor.wxEditor
3 # Purpose: An intelligent text editor with colorization capabilities.
6 # Authors: Dirk Holtwic, Robin Dunn
9 # Authors: Adam Feuer, Steve Howell
12 # This code used to support a fairly complex subclass that did
13 # syntax coloring and outliner collapse mode. Adam and Steve
14 # inherited the code, and added a lot of basic editor
15 # functionality that had not been there before, such as cut-and-paste.
18 # Created: 15-Dec-1999
20 # Copyright: (c) 1999 by Dirk Holtwick, 1999
21 # Licence: wxWindows license
22 #----------------------------------------------------------------------
23 # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
25 # o 2.5 compatability update.
36 #----------------------------
38 def ForceBetween(min, val
, max):
46 def LineTrimmer(lineOfText
):
47 if len(lineOfText
) == 0:
49 elif lineOfText
[-1] == '\r':
50 return lineOfText
[:-1]
54 def LineSplitter(text
):
55 return map (LineTrimmer
, text
.split('\n'))
58 #----------------------------
61 def __init__(self
, parent
):
68 def SetScrollbars(self
, fw
, fh
, w
, h
, x
, y
):
69 if (self
.ow
!= w
or self
.oh
!= h
or self
.ox
!= x
or self
.oy
!= y
):
70 self
.parent
.SetScrollbars(fw
, fh
, w
, h
, x
, y
)
76 #----------------------------------------------------------------------
78 class wxEditor(wx
.ScrolledWindow
):
80 def __init__(self
, parent
, id,
81 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
, style
=0):
83 wx
.ScrolledWindow
.__init
__(self
, parent
, id,
87 self
.isDrawing
= False
94 self
.InitDoubleBuffering()
101 ##------------------ Init stuff
103 def InitCoords(self
):
116 self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeftDown
)
117 self
.Bind(wx
.EVT_LEFT_UP
, self
.OnLeftUp
)
118 self
.Bind(wx
.EVT_MOTION
, self
.OnMotion
)
119 self
.Bind(wx
.EVT_SCROLLWIN
, self
.OnScroll
)
120 self
.Bind(wx
.EVT_CHAR
, self
.OnChar
)
121 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
122 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
123 self
.Bind(wx
.EVT_WINDOW_DESTROY
, self
.OnDestroy
)
124 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
)
126 ##------------------- Platform-specific stuff
128 def NiceFontForPlatform(self
):
129 if wx
.Platform
== "__WXMSW__":
130 return wx
.Font(10, wx
.MODERN
, wx
.NORMAL
, wx
.NORMAL
)
132 return wx
.Font(12, wx
.MODERN
, wx
.NORMAL
, wx
.NORMAL
, False)
134 def UnixKeyHack(self
, key
):
136 # this will be obsolete when we get the new wxWindows patch
140 # Which patch? I don't know if this is needed, but I don't know
141 # why it's here either. Play it safe; leave it in.
147 ##-------------------- UpdateView/Cursor code
149 def OnSize(self
, event
):
150 self
.AdjustScrollbars()
153 def SetCharDimensions(self
):
154 # TODO: We need a code review on this. It appears that Linux
155 # improperly reports window dimensions when the scrollbar's there.
156 self
.bw
, self
.bh
= self
.GetClientSize()
158 if wx
.Platform
== "__WXMSW__":
159 self
.sh
= self
.bh
/ self
.fh
160 self
.sw
= (self
.bw
/ self
.fw
) - 1
162 self
.sh
= self
.bh
/ self
.fh
163 if self
.LinesInFile() >= self
.sh
:
164 self
.bw
= self
.bw
- wx
.SystemSettings_GetMetric(wx
.SYS_VSCROLL_X
)
165 self
.sw
= (self
.bw
/ self
.fw
) - 1
167 self
.sw
= (self
.bw
/ self
.fw
) - 1
168 if self
.CalcMaxLineLen() >= self
.sw
:
169 self
.bh
= self
.bh
- wx
.SystemSettings_GetMetric(wx
.SYS_HSCROLL_Y
)
170 self
.sh
= self
.bh
/ self
.fh
173 def UpdateView(self
, dc
= None):
175 dc
= wx
.ClientDC(self
)
177 self
.SetCharDimensions()
178 self
.KeepCursorOnScreen()
179 self
.DrawSimpleCursor(0,0, dc
, True)
182 def OnPaint(self
, event
):
183 dc
= wx
.PaintDC(self
)
186 self
.isDrawing
= True
188 wx
.CallAfter(self
.AdjustScrollbars
)
189 self
.isDrawing
= False
191 def OnEraseBackground(self
, evt
):
194 ##-------------------- Drawing code
197 dc
= wx
.ClientDC(self
)
198 self
.font
= self
.NiceFontForPlatform()
199 dc
.SetFont(self
.font
)
200 self
.fw
= dc
.GetCharWidth()
201 self
.fh
= dc
.GetCharHeight()
204 self
.fgColor
= wx
.NamedColour('black')
205 self
.bgColor
= wx
.NamedColour('white')
206 self
.selectColor
= wx
.Colour(238, 220, 120) # r, g, b = emacsOrange
208 def InitDoubleBuffering(self
):
211 def DrawEditText(self
, t
, x
, y
, dc
):
212 dc
.DrawText(t
, (x
* self
.fw
, y
* self
.fh
))
214 def DrawLine(self
, line
, dc
):
215 if self
.IsLine(line
):
218 dc
.SetTextForeground(self
.fgColor
)
219 fragments
= selection
.Selection(
220 self
.SelectBegin
, self
.SelectEnd
,
221 self
.sx
, self
.sw
, line
, t
)
223 for (data
, selected
) in fragments
:
225 dc
.SetTextBackground(self
.selectColor
)
226 if x
== 0 and len(data
) == 0 and len(fragments
) == 1:
229 dc
.SetTextBackground(self
.bgColor
)
230 self
.DrawEditText(data
, x
, line
- self
.sy
, dc
)
233 def Draw(self
, odc
=None):
235 odc
= wx
.ClientDC(self
)
237 bmp
= wx
.EmptyBitmap(max(1,self
.bw
), max(1,self
.bh
))
238 dc
= wx
.BufferedDC(odc
, bmp
)
240 dc
.SetFont(self
.font
)
241 dc
.SetBackgroundMode(wx
.SOLID
)
242 dc
.SetTextBackground(self
.bgColor
)
243 dc
.SetTextForeground(self
.fgColor
)
245 for line
in range(self
.sy
, self
.sy
+ self
.sh
):
246 self
.DrawLine(line
, dc
)
247 if len(self
.lines
) < self
.sh
+ self
.sy
:
248 self
.DrawEofMarker(dc
)
251 ##------------------ eofMarker stuff
253 def LoadImages(self
):
254 self
.eofMarker
= images
.GetBitmap(images
.EofImageData
)
256 def DrawEofMarker(self
,dc
):
258 y
= (len(self
.lines
) - self
.sy
) * self
.fh
260 dc
.DrawBitmap(self
.eofMarker
, (x
, y
), hasTransparency
)
262 ##------------------ cursor-related functions
264 def DrawCursor(self
, dc
= None):
266 dc
= wx
.ClientDC(self
)
268 if (self
.LinesInFile())<self
.cy
: #-1 ?
269 self
.cy
= self
.LinesInFile()-1
270 s
= self
.lines
[self
.cy
]
272 x
= self
.cx
- self
.sx
273 y
= self
.cy
- self
.sy
274 self
.DrawSimpleCursor(x
, y
, dc
)
277 def DrawSimpleCursor(self
, xp
, yp
, dc
= None, old
=False):
279 dc
= wx
.ClientDC(self
)
289 dc
.Blit((x
,y
), (szx
,szy
), dc
, (x
,y
), wx
.SRC_INVERT
)
293 ##-------- Enforcing screen boundaries, cursor movement
295 def CalcMaxLineLen(self
):
296 """get length of longest line on screen"""
298 for line
in self
.lines
[self
.sy
:self
.sy
+self
.sh
]:
299 if len(line
) >maxlen
:
303 def KeepCursorOnScreen(self
):
304 self
.sy
= ForceBetween(max(0, self
.cy
-self
.sh
), self
.sy
, self
.cy
)
305 self
.sx
= ForceBetween(max(0, self
.cx
-self
.sw
), self
.sx
, self
.cx
)
306 self
.AdjustScrollbars()
308 def HorizBoundaries(self
):
309 self
.SetCharDimensions()
310 maxLineLen
= self
.CalcMaxLineLen()
311 self
.sx
= ForceBetween(0, self
.sx
, max(self
.sw
, maxLineLen
- self
.sw
+ 1))
312 self
.cx
= ForceBetween(self
.sx
, self
.cx
, self
.sx
+ self
.sw
- 1)
314 def VertBoundaries(self
):
315 self
.SetCharDimensions()
316 self
.sy
= ForceBetween(0, self
.sy
, max(self
.sh
, self
.LinesInFile() - self
.sh
+ 1))
317 self
.cy
= ForceBetween(self
.sy
, self
.cy
, self
.sy
+ self
.sh
- 1)
319 def cVert(self
, num
):
320 self
.cy
= self
.cy
+ num
321 self
.cy
= ForceBetween(0, self
.cy
, self
.LinesInFile() - 1)
322 self
.sy
= ForceBetween(self
.cy
- self
.sh
+ 1, self
.sy
, self
.cy
)
323 self
.cx
= min(self
.cx
, self
.CurrentLineLength())
325 def cHoriz(self
, num
):
326 self
.cx
= self
.cx
+ num
327 self
.cx
= ForceBetween(0, self
.cx
, self
.CurrentLineLength())
328 self
.sx
= ForceBetween(self
.cx
- self
.sw
+ 1, self
.sx
, self
.cx
)
330 def AboveScreen(self
, row
):
333 def BelowScreen(self
, row
):
334 return row
>= self
.sy
+ self
.sh
336 def LeftOfScreen(self
, col
):
339 def RightOfScreen(self
, col
):
340 return col
>= self
.sx
+ self
.sw
342 ##----------------- data structure helper functions
347 def SetText(self
, lines
):
352 self
.AdjustScrollbars()
353 self
.UpdateView(None)
355 def IsLine(self
, lineNum
):
356 return (0<=lineNum
) and (lineNum
<self
.LinesInFile())
358 def GetTextLine(self
, lineNum
):
359 if self
.IsLine(lineNum
):
360 return self
.lines
[lineNum
]
363 def SetTextLine(self
, lineNum
, text
):
364 if self
.IsLine(lineNum
):
365 self
.lines
[lineNum
] = text
367 def CurrentLineLength(self
):
368 return len(self
.lines
[self
.cy
])
370 def LinesInFile(self
):
371 return len(self
.lines
)
373 def UnTouchBuffer(self
):
374 self
.bufferTouched
= False
376 def BufferWasTouched(self
):
377 return self
.bufferTouched
379 def TouchBuffer(self
):
380 self
.bufferTouched
= True
383 ##-------------------------- Mouse scroll timing functions
385 def InitScrolling(self
):
386 # we don't rely on the windows system to scroll for us; we just
387 # redraw the screen manually every time
388 self
.EnableScrolling(False, False)
389 self
.nextScrollTime
= 0
390 self
.SCROLLDELAY
= 0.050 # seconds
391 self
.scrollTimer
= wx
.Timer(self
)
392 self
.scroller
= Scroller(self
)
395 if time
.time() > self
.nextScrollTime
:
396 self
.nextScrollTime
= time
.time() + self
.SCROLLDELAY
401 def SetScrollTimer(self
):
403 self
.scrollTimer
.Start(1000*self
.SCROLLDELAY
/2, oneShot
)
404 self
.Bind(wx
.EVT_TIMER
, self
.OnTimer
)
406 def OnTimer(self
, event
):
407 screenX
, screenY
= wx
.GetMousePosition()
408 x
, y
= self
.ScreenToClientXY(screenX
, screenY
)
413 ##-------------------------- Mouse off screen functions
415 def HandleAboveScreen(self
, row
):
416 self
.SetScrollTimer()
422 def HandleBelowScreen(self
, row
):
423 self
.SetScrollTimer()
425 row
= self
.sy
+ self
.sh
426 row
= min(row
, self
.LinesInFile() - 1)
429 def HandleLeftOfScreen(self
, col
):
430 self
.SetScrollTimer()
436 def HandleRightOfScreen(self
, col
):
437 self
.SetScrollTimer()
439 col
= self
.sx
+ self
.sw
440 col
= min(col
, self
.CurrentLineLength())
443 ##------------------------ mousing functions
445 def MouseToRow(self
, mouseY
):
446 row
= self
.sy
+ (mouseY
/ self
.fh
)
447 if self
.AboveScreen(row
):
448 self
.HandleAboveScreen(row
)
449 elif self
.BelowScreen(row
):
450 self
.HandleBelowScreen(row
)
452 self
.cy
= min(row
, self
.LinesInFile() - 1)
454 def MouseToCol(self
, mouseX
):
455 col
= self
.sx
+ (mouseX
/ self
.fw
)
456 if self
.LeftOfScreen(col
):
457 self
.HandleLeftOfScreen(col
)
458 elif self
.RightOfScreen(col
):
459 self
.HandleRightOfScreen(col
)
461 self
.cx
= min(col
, self
.CurrentLineLength())
463 def MouseToCursor(self
, event
):
464 self
.MouseToRow(event
.GetY())
465 self
.MouseToCol(event
.GetX())
467 def OnMotion(self
, event
):
468 if event
.LeftIsDown() and self
.HasCapture():
469 self
.Selecting
= True
470 self
.MouseToCursor(event
)
473 def OnLeftDown(self
, event
):
474 self
.MouseToCursor(event
)
475 self
.SelectBegin
= (self
.cy
, self
.cx
)
476 self
.SelectEnd
= None
480 def OnLeftUp(self
, event
):
481 if not self
.HasCapture():
484 if self
.SelectEnd
is None:
487 self
.Selecting
= False
488 self
.SelectNotify(False, self
.SelectBegin
, self
.SelectEnd
)
491 self
.scrollTimer
.Stop()
494 #------------------------- Scrolling
496 def HorizScroll(self
, event
, eventType
):
497 maxLineLen
= self
.CalcMaxLineLen()
499 if eventType
== wx
.EVT_SCROLLWIN_LINEUP
:
501 elif eventType
== wx
.EVT_SCROLLWIN_LINEDOWN
:
503 elif eventType
== wx
.EVT_SCROLLWIN_PAGEUP
:
505 elif eventType
== wx
.EVT_SCROLLWIN_PAGEDOWN
:
507 elif eventType
== wx
.EVT_SCROLLWIN_TOP
:
508 self
.sx
= self
.cx
= 0
509 elif eventType
== wx
.EVT_SCROLLWIN_BOTTOM
:
510 self
.sx
= maxLineLen
- self
.sw
513 self
.sx
= event
.GetPosition()
515 self
.HorizBoundaries()
517 def VertScroll(self
, event
, eventType
):
518 if eventType
== wx
.EVT_SCROLLWIN_LINEUP
:
520 elif eventType
== wx
.EVT_SCROLLWIN_LINEDOWN
:
522 elif eventType
== wx
.EVT_SCROLLWIN_PAGEUP
:
524 elif eventType
== wx
.EVT_SCROLLWIN_PAGEDOWN
:
526 elif eventType
== wx
.EVT_SCROLLWIN_TOP
:
527 self
.sy
= self
.cy
= 0
528 elif eventType
== wx
.EVT_SCROLLWIN_BOTTOM
:
529 self
.sy
= self
.LinesInFile() - self
.sh
530 self
.cy
= self
.LinesInFile()
532 self
.sy
= event
.GetPosition()
534 self
.VertBoundaries()
536 def OnScroll(self
, event
):
537 dir = event
.GetOrientation()
538 eventType
= event
.GetEventType()
539 if dir == wx
.HORIZONTAL
:
540 self
.HorizScroll(event
, eventType
)
542 self
.VertScroll(event
, eventType
)
546 def AdjustScrollbars(self
):
548 self
.SetCharDimensions()
549 self
.scroller
.SetScrollbars(
551 self
.CalcMaxLineLen()+3, max(self
.LinesInFile()+1, self
.sh
),
554 #------------ backspace, delete, return
556 def BreakLine(self
, event
):
557 if self
.IsLine(self
.cy
):
558 t
= self
.lines
[self
.cy
]
559 self
.lines
= self
.lines
[:self
.cy
] + [t
[:self
.cx
],t
[self
.cx
:]] + self
.lines
[self
.cy
+1:]
564 def InsertChar(self
,char
):
565 if self
.IsLine(self
.cy
):
566 t
= self
.lines
[self
.cy
]
567 t
= t
[:self
.cx
] + char
+ t
[self
.cx
:]
568 self
.SetTextLine(self
.cy
, t
)
573 t1
= self
.lines
[self
.cy
]
574 t2
= self
.lines
[self
.cy
+1]
576 self
.lines
= self
.lines
[:self
.cy
] + [t1
+ t2
] + self
.lines
[self
.cy
+2:]
580 def DeleteChar(self
,x
,y
,oldtext
):
581 newtext
= oldtext
[:x
] + oldtext
[x
+1:]
582 self
.SetTextLine(y
, newtext
)
586 def BackSpace(self
, event
):
587 t
= self
.GetTextLine(self
.cy
)
589 self
.DeleteChar(self
.cx
-1,self
.cy
,t
)
600 def Delete(self
, event
):
601 t
= self
.GetTextLine(self
.cy
)
603 self
.DeleteChar(self
.cx
,self
.cy
,t
)
606 if self
.cy
< len(self
.lines
) - 1:
610 def Escape(self
, event
):
613 def TabKey(self
, event
):
614 numSpaces
= self
.SpacesPerTab
- (self
.cx
% self
.SpacesPerTab
)
615 self
.SingleLineInsert(' ' * numSpaces
)
617 ##----------- selection routines
619 def SelectUpdate(self
):
620 self
.SelectEnd
= (self
.cy
, self
.cx
)
621 self
.SelectNotify(self
.Selecting
, self
.SelectBegin
, self
.SelectEnd
)
624 def NormalizedSelect(self
):
625 (begin
, end
) = (self
.SelectBegin
, self
.SelectEnd
)
638 def FindSelection(self
):
639 if self
.SelectEnd
is None or self
.SelectBegin
is None:
642 (begin
, end
) = self
.NormalizedSelect()
645 return (bRow
, bCol
, eRow
, eCol
)
648 self
.SelectBegin
= None
649 self
.SelectEnd
= None
650 self
.Selecting
= False
651 self
.SelectNotify(False,None,None)
653 def CopySelection(self
, event
):
654 selection
= self
.FindSelection()
655 if selection
is None:
657 (bRow
, bCol
, eRow
, eCol
) = selection
660 self
.SingleLineCopy(bRow
, bCol
, eCol
)
662 self
.MultipleLineCopy(bRow
, bCol
, eRow
, eCol
)
664 def OnCopySelection(self
, event
):
665 self
.CopySelection(event
)
668 def CopyToClipboard(self
, linesOfText
):
669 do
= wx
.TextDataObject()
670 do
.SetText(os
.linesep
.join(linesOfText
))
671 wx
.TheClipboard
.Open()
672 wx
.TheClipboard
.SetData(do
)
673 wx
.TheClipboard
.Close()
675 def SingleLineCopy(self
, Row
, bCol
, eCol
):
676 Line
= self
.GetTextLine(Row
)
677 self
.CopyToClipboard([Line
[bCol
:eCol
]])
679 def MultipleLineCopy(self
, bRow
, bCol
, eRow
, eCol
):
680 bLine
= self
.GetTextLine(bRow
)[bCol
:]
681 eLine
= self
.GetTextLine(eRow
)[:eCol
]
682 self
.CopyToClipboard([bLine
] + [l
for l
in self
.lines
[bRow
+ 1:eRow
]] + [eLine
])
684 def OnDeleteSelection(self
, event
):
685 selection
= self
.FindSelection()
686 if selection
is None:
688 (bRow
, bCol
, eRow
, eCol
) = selection
691 self
.SingleLineDelete(bRow
, bCol
, eCol
)
693 self
.MultipleLineDelete(bRow
, bCol
, eRow
, eCol
)
703 def SingleLineDelete(self
, Row
, bCol
, eCol
):
704 ModLine
= self
.GetTextLine(Row
)
705 ModLine
= ModLine
[:bCol
] + ModLine
[eCol
:]
706 self
.SetTextLine(Row
,ModLine
)
708 def MultipleLineDelete(self
, bRow
, bCol
, eRow
, eCol
):
709 bLine
= self
.GetTextLine(bRow
)
710 eLine
= self
.GetTextLine(eRow
)
711 ModLine
= bLine
[:bCol
] + eLine
[eCol
:]
712 self
.lines
[bRow
:eRow
+ 1] = [ModLine
]
714 def OnPaste(self
, event
):
715 do
= wx
.TextDataObject()
716 wx
.TheClipboard
.Open()
717 success
= wx
.TheClipboard
.GetData(do
)
718 wx
.TheClipboard
.Close()
720 pastedLines
= LineSplitter(do
.GetText())
724 if len(pastedLines
) == 0:
727 elif len(pastedLines
) == 1:
728 self
.SingleLineInsert(pastedLines
[0])
730 self
.MultipleLinePaste(pastedLines
)
732 def SingleLineInsert(self
, newText
):
733 ModLine
= self
.GetTextLine(self
.cy
)
734 ModLine
= ModLine
[:self
.cx
] + newText
+ ModLine
[self
.cx
:]
735 self
.SetTextLine(self
.cy
, ModLine
)
736 self
.cHoriz(len(newText
))
740 def MultipleLinePaste(self
, pastedLines
):
741 FirstLine
= LastLine
= self
.GetTextLine(self
.cy
)
742 FirstLine
= FirstLine
[:self
.cx
] + pastedLines
[0]
743 LastLine
= pastedLines
[-1] + LastLine
[self
.cx
:]
745 NewSlice
= [FirstLine
]
746 NewSlice
+= [l
for l
in pastedLines
[1:-1]]
747 NewSlice
+= [LastLine
]
748 self
.lines
[self
.cy
:self
.cy
+ 1] = NewSlice
750 self
.cy
= self
.cy
+ len(pastedLines
)-1
751 self
.cx
= len(pastedLines
[-1])
755 def OnCutSelection(self
,event
):
756 self
.CopySelection(event
)
757 self
.OnDeleteSelection(event
)
759 #-------------- Keyboard movement implementations
761 def MoveDown(self
, event
):
764 def MoveUp(self
, event
):
767 def MoveLeft(self
, event
):
773 self
.cx
= self
.CurrentLineLength()
777 def MoveRight(self
, event
):
778 linelen
= self
.CurrentLineLength()
779 if self
.cx
== linelen
:
780 if self
.cy
== len(self
.lines
) - 1:
789 def MovePageDown(self
, event
):
792 def MovePageUp(self
, event
):
795 def MoveHome(self
, event
):
798 def MoveEnd(self
, event
):
799 self
.cx
= self
.CurrentLineLength()
801 def MoveStartOfFile(self
, event
):
805 def MoveEndOfFile(self
, event
):
806 self
.cy
= len(self
.lines
) - 1
807 self
.cx
= self
.CurrentLineLength()
809 #-------------- Key handler mapping tables
811 def SetMoveSpecialFuncs(self
, action
):
812 action
[wx
.WXK_DOWN
] = self
.MoveDown
813 action
[wx
.WXK_UP
] = self
.MoveUp
814 action
[wx
.WXK_LEFT
] = self
.MoveLeft
815 action
[wx
.WXK_RIGHT
] = self
.MoveRight
816 action
[wx
.WXK_NEXT
] = self
.MovePageDown
817 action
[wx
.WXK_PRIOR
] = self
.MovePageUp
818 action
[wx
.WXK_HOME
] = self
.MoveHome
819 action
[wx
.WXK_END
] = self
.MoveEnd
821 def SetMoveSpecialControlFuncs(self
, action
):
822 action
[wx
.WXK_HOME
] = self
.MoveStartOfFile
823 action
[wx
.WXK_END
] = self
.MoveEndOfFile
825 def SetAltFuncs(self
, action
):
826 # subclass implements
829 def SetControlFuncs(self
, action
):
830 action
['c'] = self
.OnCopySelection
831 action
['d'] = self
.OnDeleteSelection
832 action
['v'] = self
.OnPaste
833 action
['x'] = self
.OnCutSelection
835 def SetSpecialControlFuncs(self
, action
):
836 action
[wx
.WXK_INSERT
] = self
.OnCopySelection
838 def SetShiftFuncs(self
, action
):
839 action
[wx
.WXK_DELETE
] = self
.OnCutSelection
840 action
[wx
.WXK_INSERT
] = self
.OnPaste
842 def SetSpecialFuncs(self
, action
):
843 action
[wx
.WXK_BACK
] = self
.BackSpace
844 action
[wx
.WXK_DELETE
] = self
.Delete
845 action
[wx
.WXK_RETURN
] = self
.BreakLine
846 action
[wx
.WXK_ESCAPE
] = self
.Escape
847 action
[wx
.WXK_TAB
] = self
.TabKey
849 ##-------------- Logic for key handlers
852 def Move(self
, keySettingFunction
, key
, event
):
854 keySettingFunction(action
)
856 if not action
.has_key(key
):
859 if event
.ShiftDown():
860 if not self
.Selecting
:
861 self
.Selecting
= True
862 self
.SelectBegin
= (self
.cy
, self
.cx
)
864 self
.SelectEnd
= (self
.cy
, self
.cx
)
868 self
.Selecting
= False
870 self
.SelectNotify(self
.Selecting
, self
.SelectBegin
, self
.SelectEnd
)
874 def MoveSpecialKey(self
, event
, key
):
875 return self
.Move(self
.SetMoveSpecialFuncs
, key
, event
)
877 def MoveSpecialControlKey(self
, event
, key
):
878 if not event
.ControlDown():
880 return self
.Move(self
.SetMoveSpecialControlFuncs
, key
, event
)
882 def Dispatch(self
, keySettingFunction
, key
, event
):
884 keySettingFunction(action
)
885 if action
.has_key(key
):
891 def ModifierKey(self
, key
, event
, modifierKeyDown
, MappingFunc
):
892 if not modifierKeyDown
:
895 key
= self
.UnixKeyHack(key
)
900 if not self
.Dispatch(MappingFunc
, key
, event
):
904 def ControlKey(self
, event
, key
):
905 return self
.ModifierKey(key
, event
, event
.ControlDown(), self
.SetControlFuncs
)
907 def AltKey(self
, event
, key
):
908 return self
.ModifierKey(key
, event
, event
.AltDown(), self
.SetAltFuncs
)
910 def SpecialControlKey(self
, event
, key
):
911 if not event
.ControlDown():
913 if not self
.Dispatch(self
.SetSpecialControlFuncs
, key
, event
):
917 def ShiftKey(self
, event
, key
):
918 if not event
.ShiftDown():
920 return self
.Dispatch(self
.SetShiftFuncs
, key
, event
)
922 def NormalChar(self
, event
, key
):
926 if not self
.Dispatch(self
.SetSpecialFuncs
, key
, event
):
927 if (key
>31) and (key
<256):
928 self
.InsertChar(chr(key
))
933 self
.AdjustScrollbars()
935 def OnChar(self
, event
):
936 key
= event
.KeyCode()
937 filters
= [self
.AltKey
,
938 self
.MoveSpecialControlKey
,
940 self
.SpecialControlKey
,
944 for filter in filters
:
945 if filter(event
,key
):
949 #----------------------- Eliminate memory leaks
951 def OnDestroy(self
, event
):
957 self
.selectColor
= None
958 self
.scrollTimer
= None
959 self
.eofMarker
= None
961 #-------------------- Abstract methods for subclasses
966 def SelectNotify(self
, Selecting
, SelectionBegin
, SelectionEnd
):