1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.editor.Editor
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.
27 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
29 # o wxEditor -> Editor
40 #----------------------------
42 def ForceBetween(min, val
, max):
50 def LineTrimmer(lineOfText
):
51 if len(lineOfText
) == 0:
53 elif lineOfText
[-1] == '\r':
54 return lineOfText
[:-1]
58 def LineSplitter(text
):
59 return map (LineTrimmer
, text
.split('\n'))
62 #----------------------------
65 def __init__(self
, parent
):
72 def SetScrollbars(self
, fw
, fh
, w
, h
, x
, y
):
73 if (self
.ow
!= w
or self
.oh
!= h
or self
.ox
!= x
or self
.oy
!= y
):
74 self
.parent
.SetScrollbars(fw
, fh
, w
, h
, x
, y
)
80 #----------------------------------------------------------------------
82 class Editor(wx
.ScrolledWindow
):
84 def __init__(self
, parent
, id,
85 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
, style
=0):
87 wx
.ScrolledWindow
.__init
__(self
, parent
, id,
91 self
.isDrawing
= False
98 self
.InitDoubleBuffering()
103 self
.SpacesPerTab
= 4
105 ##------------------ Init stuff
107 def InitCoords(self
):
120 self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeftDown
)
121 self
.Bind(wx
.EVT_LEFT_UP
, self
.OnLeftUp
)
122 self
.Bind(wx
.EVT_MOTION
, self
.OnMotion
)
123 self
.Bind(wx
.EVT_SCROLLWIN
, self
.OnScroll
)
124 self
.Bind(wx
.EVT_CHAR
, self
.OnChar
)
125 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
126 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
127 self
.Bind(wx
.EVT_WINDOW_DESTROY
, self
.OnDestroy
)
128 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
)
130 ##------------------- Platform-specific stuff
132 def NiceFontForPlatform(self
):
133 if wx
.Platform
== "__WXMSW__":
134 return wx
.Font(10, wx
.MODERN
, wx
.NORMAL
, wx
.NORMAL
)
136 return wx
.Font(12, wx
.MODERN
, wx
.NORMAL
, wx
.NORMAL
, False)
138 def UnixKeyHack(self
, key
):
140 # this will be obsolete when we get the new wxWindows patch
144 # Which patch? I don't know if this is needed, but I don't know
145 # why it's here either. Play it safe; leave it in.
151 ##-------------------- UpdateView/Cursor code
153 def OnSize(self
, event
):
154 self
.AdjustScrollbars()
157 def SetCharDimensions(self
):
158 # TODO: We need a code review on this. It appears that Linux
159 # improperly reports window dimensions when the scrollbar's there.
160 self
.bw
, self
.bh
= self
.GetClientSize()
162 if wx
.Platform
== "__WXMSW__":
163 self
.sh
= self
.bh
/ self
.fh
164 self
.sw
= (self
.bw
/ self
.fw
) - 1
166 self
.sh
= self
.bh
/ self
.fh
167 if self
.LinesInFile() >= self
.sh
:
168 self
.bw
= self
.bw
- wx
.SystemSettings_GetMetric(wx
.SYS_VSCROLL_X
)
169 self
.sw
= (self
.bw
/ self
.fw
) - 1
171 self
.sw
= (self
.bw
/ self
.fw
) - 1
172 if self
.CalcMaxLineLen() >= self
.sw
:
173 self
.bh
= self
.bh
- wx
.SystemSettings_GetMetric(wx
.SYS_HSCROLL_Y
)
174 self
.sh
= self
.bh
/ self
.fh
177 def UpdateView(self
, dc
= None):
179 dc
= wx
.ClientDC(self
)
181 self
.SetCharDimensions()
182 self
.KeepCursorOnScreen()
183 self
.DrawSimpleCursor(0,0, dc
, True)
186 def OnPaint(self
, event
):
187 dc
= wx
.PaintDC(self
)
190 self
.isDrawing
= True
192 wx
.CallAfter(self
.AdjustScrollbars
)
193 self
.isDrawing
= False
195 def OnEraseBackground(self
, evt
):
198 ##-------------------- Drawing code
201 dc
= wx
.ClientDC(self
)
202 self
.font
= self
.NiceFontForPlatform()
203 dc
.SetFont(self
.font
)
204 self
.fw
= dc
.GetCharWidth()
205 self
.fh
= dc
.GetCharHeight()
208 self
.fgColor
= wx
.NamedColour('black')
209 self
.bgColor
= wx
.NamedColour('white')
210 self
.selectColor
= wx
.Colour(238, 220, 120) # r, g, b = emacsOrange
212 def InitDoubleBuffering(self
):
215 def DrawEditText(self
, t
, x
, y
, dc
):
216 dc
.DrawText(t
, (x
* self
.fw
, y
* self
.fh
))
218 def DrawLine(self
, line
, dc
):
219 if self
.IsLine(line
):
222 dc
.SetTextForeground(self
.fgColor
)
223 fragments
= selection
.Selection(
224 self
.SelectBegin
, self
.SelectEnd
,
225 self
.sx
, self
.sw
, line
, t
)
227 for (data
, selected
) in fragments
:
229 dc
.SetTextBackground(self
.selectColor
)
230 if x
== 0 and len(data
) == 0 and len(fragments
) == 1:
233 dc
.SetTextBackground(self
.bgColor
)
234 self
.DrawEditText(data
, x
, line
- self
.sy
, dc
)
237 def Draw(self
, odc
=None):
239 odc
= wx
.ClientDC(self
)
241 bmp
= wx
.EmptyBitmap(max(1,self
.bw
), max(1,self
.bh
))
242 dc
= wx
.BufferedDC(odc
, bmp
)
244 dc
.SetFont(self
.font
)
245 dc
.SetBackgroundMode(wx
.SOLID
)
246 dc
.SetTextBackground(self
.bgColor
)
247 dc
.SetTextForeground(self
.fgColor
)
249 for line
in range(self
.sy
, self
.sy
+ self
.sh
):
250 self
.DrawLine(line
, dc
)
251 if len(self
.lines
) < self
.sh
+ self
.sy
:
252 self
.DrawEofMarker(dc
)
255 ##------------------ eofMarker stuff
257 def LoadImages(self
):
258 self
.eofMarker
= images
.GetBitmap(images
.EofImageData
)
260 def DrawEofMarker(self
,dc
):
262 y
= (len(self
.lines
) - self
.sy
) * self
.fh
264 dc
.DrawBitmap(self
.eofMarker
, (x
, y
), hasTransparency
)
266 ##------------------ cursor-related functions
268 def DrawCursor(self
, dc
= None):
270 dc
= wx
.ClientDC(self
)
272 if (self
.LinesInFile())<self
.cy
: #-1 ?
273 self
.cy
= self
.LinesInFile()-1
274 s
= self
.lines
[self
.cy
]
276 x
= self
.cx
- self
.sx
277 y
= self
.cy
- self
.sy
278 self
.DrawSimpleCursor(x
, y
, dc
)
281 def DrawSimpleCursor(self
, xp
, yp
, dc
= None, old
=False):
283 dc
= wx
.ClientDC(self
)
293 dc
.Blit((x
,y
), (szx
,szy
), dc
, (x
,y
), wx
.SRC_INVERT
)
297 ##-------- Enforcing screen boundaries, cursor movement
299 def CalcMaxLineLen(self
):
300 """get length of longest line on screen"""
302 for line
in self
.lines
[self
.sy
:self
.sy
+self
.sh
]:
303 if len(line
) >maxlen
:
307 def KeepCursorOnScreen(self
):
308 self
.sy
= ForceBetween(max(0, self
.cy
-self
.sh
), self
.sy
, self
.cy
)
309 self
.sx
= ForceBetween(max(0, self
.cx
-self
.sw
), self
.sx
, self
.cx
)
310 self
.AdjustScrollbars()
312 def HorizBoundaries(self
):
313 self
.SetCharDimensions()
314 maxLineLen
= self
.CalcMaxLineLen()
315 self
.sx
= ForceBetween(0, self
.sx
, max(self
.sw
, maxLineLen
- self
.sw
+ 1))
316 self
.cx
= ForceBetween(self
.sx
, self
.cx
, self
.sx
+ self
.sw
- 1)
318 def VertBoundaries(self
):
319 self
.SetCharDimensions()
320 self
.sy
= ForceBetween(0, self
.sy
, max(self
.sh
, self
.LinesInFile() - self
.sh
+ 1))
321 self
.cy
= ForceBetween(self
.sy
, self
.cy
, self
.sy
+ self
.sh
- 1)
323 def cVert(self
, num
):
324 self
.cy
= self
.cy
+ num
325 self
.cy
= ForceBetween(0, self
.cy
, self
.LinesInFile() - 1)
326 self
.sy
= ForceBetween(self
.cy
- self
.sh
+ 1, self
.sy
, self
.cy
)
327 self
.cx
= min(self
.cx
, self
.CurrentLineLength())
329 def cHoriz(self
, num
):
330 self
.cx
= self
.cx
+ num
331 self
.cx
= ForceBetween(0, self
.cx
, self
.CurrentLineLength())
332 self
.sx
= ForceBetween(self
.cx
- self
.sw
+ 1, self
.sx
, self
.cx
)
334 def AboveScreen(self
, row
):
337 def BelowScreen(self
, row
):
338 return row
>= self
.sy
+ self
.sh
340 def LeftOfScreen(self
, col
):
343 def RightOfScreen(self
, col
):
344 return col
>= self
.sx
+ self
.sw
346 ##----------------- data structure helper functions
351 def SetText(self
, lines
):
356 self
.AdjustScrollbars()
357 self
.UpdateView(None)
359 def IsLine(self
, lineNum
):
360 return (0<=lineNum
) and (lineNum
<self
.LinesInFile())
362 def GetTextLine(self
, lineNum
):
363 if self
.IsLine(lineNum
):
364 return self
.lines
[lineNum
]
367 def SetTextLine(self
, lineNum
, text
):
368 if self
.IsLine(lineNum
):
369 self
.lines
[lineNum
] = text
371 def CurrentLineLength(self
):
372 return len(self
.lines
[self
.cy
])
374 def LinesInFile(self
):
375 return len(self
.lines
)
377 def UnTouchBuffer(self
):
378 self
.bufferTouched
= False
380 def BufferWasTouched(self
):
381 return self
.bufferTouched
383 def TouchBuffer(self
):
384 self
.bufferTouched
= True
387 ##-------------------------- Mouse scroll timing functions
389 def InitScrolling(self
):
390 # we don't rely on the windows system to scroll for us; we just
391 # redraw the screen manually every time
392 self
.EnableScrolling(False, False)
393 self
.nextScrollTime
= 0
394 self
.SCROLLDELAY
= 0.050 # seconds
395 self
.scrollTimer
= wx
.Timer(self
)
396 self
.scroller
= Scroller(self
)
399 if time
.time() > self
.nextScrollTime
:
400 self
.nextScrollTime
= time
.time() + self
.SCROLLDELAY
405 def SetScrollTimer(self
):
407 self
.scrollTimer
.Start(1000*self
.SCROLLDELAY
/2, oneShot
)
408 self
.Bind(wx
.EVT_TIMER
, self
.OnTimer
)
410 def OnTimer(self
, event
):
411 screenX
, screenY
= wx
.GetMousePosition()
412 x
, y
= self
.ScreenToClientXY(screenX
, screenY
)
417 ##-------------------------- Mouse off screen functions
419 def HandleAboveScreen(self
, row
):
420 self
.SetScrollTimer()
426 def HandleBelowScreen(self
, row
):
427 self
.SetScrollTimer()
429 row
= self
.sy
+ self
.sh
430 row
= min(row
, self
.LinesInFile() - 1)
433 def HandleLeftOfScreen(self
, col
):
434 self
.SetScrollTimer()
440 def HandleRightOfScreen(self
, col
):
441 self
.SetScrollTimer()
443 col
= self
.sx
+ self
.sw
444 col
= min(col
, self
.CurrentLineLength())
447 ##------------------------ mousing functions
449 def MouseToRow(self
, mouseY
):
450 row
= self
.sy
+ (mouseY
/ self
.fh
)
451 if self
.AboveScreen(row
):
452 self
.HandleAboveScreen(row
)
453 elif self
.BelowScreen(row
):
454 self
.HandleBelowScreen(row
)
456 self
.cy
= min(row
, self
.LinesInFile() - 1)
458 def MouseToCol(self
, mouseX
):
459 col
= self
.sx
+ (mouseX
/ self
.fw
)
460 if self
.LeftOfScreen(col
):
461 self
.HandleLeftOfScreen(col
)
462 elif self
.RightOfScreen(col
):
463 self
.HandleRightOfScreen(col
)
465 self
.cx
= min(col
, self
.CurrentLineLength())
467 def MouseToCursor(self
, event
):
468 self
.MouseToRow(event
.GetY())
469 self
.MouseToCol(event
.GetX())
471 def OnMotion(self
, event
):
472 if event
.LeftIsDown() and self
.HasCapture():
473 self
.Selecting
= True
474 self
.MouseToCursor(event
)
477 def OnLeftDown(self
, event
):
478 self
.MouseToCursor(event
)
479 self
.SelectBegin
= (self
.cy
, self
.cx
)
480 self
.SelectEnd
= None
484 def OnLeftUp(self
, event
):
485 if not self
.HasCapture():
488 if self
.SelectEnd
is None:
491 self
.Selecting
= False
492 self
.SelectNotify(False, self
.SelectBegin
, self
.SelectEnd
)
495 self
.scrollTimer
.Stop()
498 #------------------------- Scrolling
500 def HorizScroll(self
, event
, eventType
):
501 maxLineLen
= self
.CalcMaxLineLen()
503 if eventType
== wx
.EVT_SCROLLWIN_LINEUP
:
505 elif eventType
== wx
.EVT_SCROLLWIN_LINEDOWN
:
507 elif eventType
== wx
.EVT_SCROLLWIN_PAGEUP
:
509 elif eventType
== wx
.EVT_SCROLLWIN_PAGEDOWN
:
511 elif eventType
== wx
.EVT_SCROLLWIN_TOP
:
512 self
.sx
= self
.cx
= 0
513 elif eventType
== wx
.EVT_SCROLLWIN_BOTTOM
:
514 self
.sx
= maxLineLen
- self
.sw
517 self
.sx
= event
.GetPosition()
519 self
.HorizBoundaries()
521 def VertScroll(self
, event
, eventType
):
522 if eventType
== wx
.EVT_SCROLLWIN_LINEUP
:
524 elif eventType
== wx
.EVT_SCROLLWIN_LINEDOWN
:
526 elif eventType
== wx
.EVT_SCROLLWIN_PAGEUP
:
528 elif eventType
== wx
.EVT_SCROLLWIN_PAGEDOWN
:
530 elif eventType
== wx
.EVT_SCROLLWIN_TOP
:
531 self
.sy
= self
.cy
= 0
532 elif eventType
== wx
.EVT_SCROLLWIN_BOTTOM
:
533 self
.sy
= self
.LinesInFile() - self
.sh
534 self
.cy
= self
.LinesInFile()
536 self
.sy
= event
.GetPosition()
538 self
.VertBoundaries()
540 def OnScroll(self
, event
):
541 dir = event
.GetOrientation()
542 eventType
= event
.GetEventType()
543 if dir == wx
.HORIZONTAL
:
544 self
.HorizScroll(event
, eventType
)
546 self
.VertScroll(event
, eventType
)
550 def AdjustScrollbars(self
):
552 self
.SetCharDimensions()
553 self
.scroller
.SetScrollbars(
555 self
.CalcMaxLineLen()+3, max(self
.LinesInFile()+1, self
.sh
),
558 #------------ backspace, delete, return
560 def BreakLine(self
, event
):
561 if self
.IsLine(self
.cy
):
562 t
= self
.lines
[self
.cy
]
563 self
.lines
= self
.lines
[:self
.cy
] + [t
[:self
.cx
],t
[self
.cx
:]] + self
.lines
[self
.cy
+1:]
568 def InsertChar(self
,char
):
569 if self
.IsLine(self
.cy
):
570 t
= self
.lines
[self
.cy
]
571 t
= t
[:self
.cx
] + char
+ t
[self
.cx
:]
572 self
.SetTextLine(self
.cy
, t
)
577 t1
= self
.lines
[self
.cy
]
578 t2
= self
.lines
[self
.cy
+1]
580 self
.lines
= self
.lines
[:self
.cy
] + [t1
+ t2
] + self
.lines
[self
.cy
+2:]
584 def DeleteChar(self
,x
,y
,oldtext
):
585 newtext
= oldtext
[:x
] + oldtext
[x
+1:]
586 self
.SetTextLine(y
, newtext
)
590 def BackSpace(self
, event
):
591 t
= self
.GetTextLine(self
.cy
)
593 self
.DeleteChar(self
.cx
-1,self
.cy
,t
)
604 def Delete(self
, event
):
605 t
= self
.GetTextLine(self
.cy
)
607 self
.DeleteChar(self
.cx
,self
.cy
,t
)
610 if self
.cy
< len(self
.lines
) - 1:
614 def Escape(self
, event
):
617 def TabKey(self
, event
):
618 numSpaces
= self
.SpacesPerTab
- (self
.cx
% self
.SpacesPerTab
)
619 self
.SingleLineInsert(' ' * numSpaces
)
621 ##----------- selection routines
623 def SelectUpdate(self
):
624 self
.SelectEnd
= (self
.cy
, self
.cx
)
625 self
.SelectNotify(self
.Selecting
, self
.SelectBegin
, self
.SelectEnd
)
628 def NormalizedSelect(self
):
629 (begin
, end
) = (self
.SelectBegin
, self
.SelectEnd
)
642 def FindSelection(self
):
643 if self
.SelectEnd
is None or self
.SelectBegin
is None:
646 (begin
, end
) = self
.NormalizedSelect()
649 return (bRow
, bCol
, eRow
, eCol
)
652 self
.SelectBegin
= None
653 self
.SelectEnd
= None
654 self
.Selecting
= False
655 self
.SelectNotify(False,None,None)
657 def CopySelection(self
, event
):
658 selection
= self
.FindSelection()
659 if selection
is None:
661 (bRow
, bCol
, eRow
, eCol
) = selection
664 self
.SingleLineCopy(bRow
, bCol
, eCol
)
666 self
.MultipleLineCopy(bRow
, bCol
, eRow
, eCol
)
668 def OnCopySelection(self
, event
):
669 self
.CopySelection(event
)
672 def CopyToClipboard(self
, linesOfText
):
673 do
= wx
.TextDataObject()
674 do
.SetText(os
.linesep
.join(linesOfText
))
675 wx
.TheClipboard
.Open()
676 wx
.TheClipboard
.SetData(do
)
677 wx
.TheClipboard
.Close()
679 def SingleLineCopy(self
, Row
, bCol
, eCol
):
680 Line
= self
.GetTextLine(Row
)
681 self
.CopyToClipboard([Line
[bCol
:eCol
]])
683 def MultipleLineCopy(self
, bRow
, bCol
, eRow
, eCol
):
684 bLine
= self
.GetTextLine(bRow
)[bCol
:]
685 eLine
= self
.GetTextLine(eRow
)[:eCol
]
686 self
.CopyToClipboard([bLine
] + [l
for l
in self
.lines
[bRow
+ 1:eRow
]] + [eLine
])
688 def OnDeleteSelection(self
, event
):
689 selection
= self
.FindSelection()
690 if selection
is None:
692 (bRow
, bCol
, eRow
, eCol
) = selection
695 self
.SingleLineDelete(bRow
, bCol
, eCol
)
697 self
.MultipleLineDelete(bRow
, bCol
, eRow
, eCol
)
707 def SingleLineDelete(self
, Row
, bCol
, eCol
):
708 ModLine
= self
.GetTextLine(Row
)
709 ModLine
= ModLine
[:bCol
] + ModLine
[eCol
:]
710 self
.SetTextLine(Row
,ModLine
)
712 def MultipleLineDelete(self
, bRow
, bCol
, eRow
, eCol
):
713 bLine
= self
.GetTextLine(bRow
)
714 eLine
= self
.GetTextLine(eRow
)
715 ModLine
= bLine
[:bCol
] + eLine
[eCol
:]
716 self
.lines
[bRow
:eRow
+ 1] = [ModLine
]
718 def OnPaste(self
, event
):
719 do
= wx
.TextDataObject()
720 wx
.TheClipboard
.Open()
721 success
= wx
.TheClipboard
.GetData(do
)
722 wx
.TheClipboard
.Close()
724 pastedLines
= LineSplitter(do
.GetText())
728 if len(pastedLines
) == 0:
731 elif len(pastedLines
) == 1:
732 self
.SingleLineInsert(pastedLines
[0])
734 self
.MultipleLinePaste(pastedLines
)
736 def SingleLineInsert(self
, newText
):
737 ModLine
= self
.GetTextLine(self
.cy
)
738 ModLine
= ModLine
[:self
.cx
] + newText
+ ModLine
[self
.cx
:]
739 self
.SetTextLine(self
.cy
, ModLine
)
740 self
.cHoriz(len(newText
))
744 def MultipleLinePaste(self
, pastedLines
):
745 FirstLine
= LastLine
= self
.GetTextLine(self
.cy
)
746 FirstLine
= FirstLine
[:self
.cx
] + pastedLines
[0]
747 LastLine
= pastedLines
[-1] + LastLine
[self
.cx
:]
749 NewSlice
= [FirstLine
]
750 NewSlice
+= [l
for l
in pastedLines
[1:-1]]
751 NewSlice
+= [LastLine
]
752 self
.lines
[self
.cy
:self
.cy
+ 1] = NewSlice
754 self
.cy
= self
.cy
+ len(pastedLines
)-1
755 self
.cx
= len(pastedLines
[-1])
759 def OnCutSelection(self
,event
):
760 self
.CopySelection(event
)
761 self
.OnDeleteSelection(event
)
763 #-------------- Keyboard movement implementations
765 def MoveDown(self
, event
):
768 def MoveUp(self
, event
):
771 def MoveLeft(self
, event
):
777 self
.cx
= self
.CurrentLineLength()
781 def MoveRight(self
, event
):
782 linelen
= self
.CurrentLineLength()
783 if self
.cx
== linelen
:
784 if self
.cy
== len(self
.lines
) - 1:
793 def MovePageDown(self
, event
):
796 def MovePageUp(self
, event
):
799 def MoveHome(self
, event
):
802 def MoveEnd(self
, event
):
803 self
.cx
= self
.CurrentLineLength()
805 def MoveStartOfFile(self
, event
):
809 def MoveEndOfFile(self
, event
):
810 self
.cy
= len(self
.lines
) - 1
811 self
.cx
= self
.CurrentLineLength()
813 #-------------- Key handler mapping tables
815 def SetMoveSpecialFuncs(self
, action
):
816 action
[wx
.WXK_DOWN
] = self
.MoveDown
817 action
[wx
.WXK_UP
] = self
.MoveUp
818 action
[wx
.WXK_LEFT
] = self
.MoveLeft
819 action
[wx
.WXK_RIGHT
] = self
.MoveRight
820 action
[wx
.WXK_NEXT
] = self
.MovePageDown
821 action
[wx
.WXK_PRIOR
] = self
.MovePageUp
822 action
[wx
.WXK_HOME
] = self
.MoveHome
823 action
[wx
.WXK_END
] = self
.MoveEnd
825 def SetMoveSpecialControlFuncs(self
, action
):
826 action
[wx
.WXK_HOME
] = self
.MoveStartOfFile
827 action
[wx
.WXK_END
] = self
.MoveEndOfFile
829 def SetAltFuncs(self
, action
):
830 # subclass implements
833 def SetControlFuncs(self
, action
):
834 action
['c'] = self
.OnCopySelection
835 action
['d'] = self
.OnDeleteSelection
836 action
['v'] = self
.OnPaste
837 action
['x'] = self
.OnCutSelection
839 def SetSpecialControlFuncs(self
, action
):
840 action
[wx
.WXK_INSERT
] = self
.OnCopySelection
842 def SetShiftFuncs(self
, action
):
843 action
[wx
.WXK_DELETE
] = self
.OnCutSelection
844 action
[wx
.WXK_INSERT
] = self
.OnPaste
846 def SetSpecialFuncs(self
, action
):
847 action
[wx
.WXK_BACK
] = self
.BackSpace
848 action
[wx
.WXK_DELETE
] = self
.Delete
849 action
[wx
.WXK_RETURN
] = self
.BreakLine
850 action
[wx
.WXK_ESCAPE
] = self
.Escape
851 action
[wx
.WXK_TAB
] = self
.TabKey
853 ##-------------- Logic for key handlers
856 def Move(self
, keySettingFunction
, key
, event
):
858 keySettingFunction(action
)
860 if not action
.has_key(key
):
863 if event
.ShiftDown():
864 if not self
.Selecting
:
865 self
.Selecting
= True
866 self
.SelectBegin
= (self
.cy
, self
.cx
)
868 self
.SelectEnd
= (self
.cy
, self
.cx
)
872 self
.Selecting
= False
874 self
.SelectNotify(self
.Selecting
, self
.SelectBegin
, self
.SelectEnd
)
878 def MoveSpecialKey(self
, event
, key
):
879 return self
.Move(self
.SetMoveSpecialFuncs
, key
, event
)
881 def MoveSpecialControlKey(self
, event
, key
):
882 if not event
.ControlDown():
884 return self
.Move(self
.SetMoveSpecialControlFuncs
, key
, event
)
886 def Dispatch(self
, keySettingFunction
, key
, event
):
888 keySettingFunction(action
)
889 if action
.has_key(key
):
895 def ModifierKey(self
, key
, event
, modifierKeyDown
, MappingFunc
):
896 if not modifierKeyDown
:
899 key
= self
.UnixKeyHack(key
)
904 if not self
.Dispatch(MappingFunc
, key
, event
):
908 def ControlKey(self
, event
, key
):
909 return self
.ModifierKey(key
, event
, event
.ControlDown(), self
.SetControlFuncs
)
911 def AltKey(self
, event
, key
):
912 return self
.ModifierKey(key
, event
, event
.AltDown(), self
.SetAltFuncs
)
914 def SpecialControlKey(self
, event
, key
):
915 if not event
.ControlDown():
917 if not self
.Dispatch(self
.SetSpecialControlFuncs
, key
, event
):
921 def ShiftKey(self
, event
, key
):
922 if not event
.ShiftDown():
924 return self
.Dispatch(self
.SetShiftFuncs
, key
, event
)
926 def NormalChar(self
, event
, key
):
930 if not self
.Dispatch(self
.SetSpecialFuncs
, key
, event
):
931 if (key
>31) and (key
<256):
932 self
.InsertChar(chr(key
))
937 self
.AdjustScrollbars()
939 def OnChar(self
, event
):
940 key
= event
.KeyCode()
941 filters
= [self
.AltKey
,
942 self
.MoveSpecialControlKey
,
944 self
.SpecialControlKey
,
948 for filter in filters
:
949 if filter(event
,key
):
953 #----------------------- Eliminate memory leaks
955 def OnDestroy(self
, event
):
961 self
.selectColor
= None
962 self
.scrollTimer
= None
963 self
.eofMarker
= None
965 #-------------------- Abstract methods for subclasses
970 def SelectNotify(self
, Selecting
, SelectionBegin
, SelectionEnd
):