1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
37 // DLL options compatibility check:
39 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
)
58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
)
60 #if wxRICHTEXT_USE_OWN_CARET
65 * This implements a non-flashing cursor in case there
66 * are platform-specific problems with the generic caret.
67 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
70 class wxRichTextCaret
: public wxCaret
75 // default - use Create()
76 wxRichTextCaret() { Init(); }
77 // creates a block caret associated with the given window
78 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
79 : wxCaret(window
, width
, height
) { Init(); m_richTextCtrl
= window
; }
80 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
81 : wxCaret(window
, size
) { Init(); m_richTextCtrl
= window
; }
83 virtual ~wxRichTextCaret();
88 // called by wxWindow (not using the event tables)
89 virtual void OnSetFocus();
90 virtual void OnKillFocus();
92 // draw the caret on the given DC
93 void DoDraw(wxDC
*dc
);
95 // get the visible count
96 int GetVisibleCount() const { return m_countVisible
; }
98 // delay repositioning
99 bool GetNeedsUpdate() const { return m_needsUpdate
; }
100 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
103 virtual void DoShow();
104 virtual void DoHide();
105 virtual void DoMove();
106 virtual void DoSize();
116 bool m_hasFocus
; // true => our window has focus
117 bool m_needsUpdate
; // must be repositioned
119 wxRichTextCtrl
* m_richTextCtrl
;
123 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
125 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
127 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
128 EVT_PAINT(wxRichTextCtrl::OnPaint
)
129 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
130 EVT_IDLE(wxRichTextCtrl::OnIdle
)
131 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
132 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
133 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
134 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
135 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
136 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
137 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
138 EVT_CHAR(wxRichTextCtrl::OnChar
)
139 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
140 EVT_SIZE(wxRichTextCtrl::OnSize
)
141 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
142 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
143 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
144 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
145 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
147 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
148 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
150 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
151 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
153 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
154 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
156 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
157 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
159 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
160 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
162 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
163 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
165 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
166 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
173 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
175 wxRichTextCtrl::wxRichTextCtrl()
176 : wxScrollHelper(this)
181 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
183 const wxString
& value
,
187 const wxValidator
& validator
,
188 const wxString
& name
)
189 : wxScrollHelper(this)
192 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
196 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
197 const wxValidator
& validator
, const wxString
& name
)
199 if (!wxControl::Create(parent
, id
, pos
, size
,
200 style
|wxFULL_REPAINT_ON_RESIZE
,
206 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
209 if (style
& wxTE_READONLY
)
212 // The base attributes must all have default values
213 wxTextAttr attributes
;
214 attributes
.SetFont(GetFont());
215 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
216 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
217 attributes
.SetLineSpacing(10);
218 attributes
.SetParagraphSpacingAfter(10);
219 attributes
.SetParagraphSpacingBefore(0);
221 SetBasicStyle(attributes
);
223 // The default attributes will be merged with base attributes, so
224 // can be empty to begin with
225 wxTextAttr defaultAttributes
;
226 SetDefaultStyle(defaultAttributes
);
228 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
229 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
232 GetBuffer().SetRichTextCtrl(this);
234 #if wxRICHTEXT_USE_OWN_CARET
235 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
237 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
240 // Tell the sizers to use the given or best size
241 SetInitialSize(size
);
243 #if wxRICHTEXT_BUFFERED_PAINTING
245 RecreateBuffer(size
);
248 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
249 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
251 SetCursor(m_textCursor
);
253 if (!value
.IsEmpty())
256 GetBuffer().AddEventHandler(this);
259 wxAcceleratorEntry entries
[4];
261 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
262 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
263 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
264 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
266 wxAcceleratorTable
accel(4, entries
);
267 SetAcceleratorTable(accel
);
272 wxRichTextCtrl::~wxRichTextCtrl()
274 GetBuffer().RemoveEventHandler(this);
276 delete m_contextMenu
;
279 /// Member initialisation
280 void wxRichTextCtrl::Init()
282 m_contextMenu
= NULL
;
284 m_caretPosition
= -1;
285 m_selectionRange
.SetRange(-2, -2);
286 m_selectionAnchor
= -2;
288 m_caretAtLineStart
= false;
290 m_fullLayoutRequired
= false;
291 m_fullLayoutTime
= 0;
292 m_fullLayoutSavedPosition
= 0;
293 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
294 m_caretPositionForDefaultStyle
= -2;
297 void wxRichTextCtrl::DoThaw()
299 if (GetBuffer().GetDirty())
308 void wxRichTextCtrl::Clear()
310 m_buffer
.ResetAndClearCommands();
311 m_buffer
.SetDirty(true);
312 m_caretPosition
= -1;
313 m_caretPositionForDefaultStyle
= -2;
314 m_caretAtLineStart
= false;
315 m_selectionRange
.SetRange(-2, -2);
325 wxTextCtrl::SendTextUpdatedEvent(this);
329 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
331 #if !wxRICHTEXT_USE_OWN_CARET
332 if (GetCaret() && !IsFrozen())
337 #if wxRICHTEXT_BUFFERED_PAINTING
338 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
348 dc
.SetFont(GetFont());
350 // Paint the background
353 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
355 wxRect
drawingArea(GetUpdateRegion().GetBox());
356 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
358 wxRect
availableSpace(GetClientSize());
359 if (GetBuffer().GetDirty())
361 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
362 GetBuffer().SetDirty(false);
366 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
367 #if wxRICHTEXT_USE_OWN_CARET
368 if (GetCaret()->IsVisible())
370 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
375 #if !wxRICHTEXT_USE_OWN_CARET
382 // Empty implementation, to prevent flicker
383 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
387 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
391 #if !wxRICHTEXT_USE_OWN_CARET
397 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
398 // Work around dropouts when control is focused
406 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
411 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
412 // Work around dropouts when control is focused
420 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
426 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
432 dc
.SetFont(GetFont());
435 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
437 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
439 m_dragStart
= event
.GetLogicalPosition(dc
);
443 bool caretAtLineStart
= false;
445 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
447 // If we're at the start of a line (but not first in para)
448 // then we should keep the caret showing at the start of the line
449 // by showing the m_caretAtLineStart flag.
450 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
451 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
453 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
454 caretAtLineStart
= true;
458 long oldCaretPos
= m_caretPosition
;
460 MoveCaret(position
, caretAtLineStart
);
461 SetDefaultStyleToCursorStyle();
463 if (event
.ShiftDown())
465 if (m_selectionRange
.GetStart() == -2)
466 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
468 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
478 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
483 if (GetCapture() == this)
486 // See if we clicked on a URL
489 dc
.SetFont(GetFont());
492 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
493 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
495 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
497 wxRichTextEvent
cmdEvent(
498 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
500 cmdEvent
.SetEventObject(this);
501 cmdEvent
.SetPosition(m_caretPosition
+1);
503 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
506 if (GetStyle(position
, attr
))
508 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
510 wxString urlTarget
= attr
.GetURL();
511 if (!urlTarget
.IsEmpty())
513 wxMouseEvent
mouseEvent(event
);
515 long startPos
= 0, endPos
= 0;
516 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
519 startPos
= obj
->GetRange().GetStart();
520 endPos
= obj
->GetRange().GetEnd();
523 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
524 InitCommandEvent(urlEvent
);
526 urlEvent
.SetString(urlTarget
);
528 GetEventHandler()->ProcessEvent(urlEvent
);
538 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
542 dc
.SetFont(GetFont());
545 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
546 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
548 // See if we need to change the cursor
551 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
554 if (GetStyle(position
, attr
))
556 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
558 SetCursor(m_urlCursor
);
560 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
562 SetCursor(m_textCursor
);
567 SetCursor(m_textCursor
);
570 if (!event
.Dragging())
576 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
578 // TODO: test closeness
580 bool caretAtLineStart
= false;
582 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
584 // If we're at the start of a line (but not first in para)
585 // then we should keep the caret showing at the start of the line
586 // by showing the m_caretAtLineStart flag.
587 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
588 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
590 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
591 caretAtLineStart
= true;
595 if (m_caretPosition
!= position
)
597 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
599 MoveCaret(position
, caretAtLineStart
);
600 SetDefaultStyleToCursorStyle();
606 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
610 wxRichTextEvent
cmdEvent(
611 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
613 cmdEvent
.SetEventObject(this);
614 cmdEvent
.SetPosition(m_caretPosition
+1);
616 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
620 /// Left-double-click
621 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
623 wxRichTextEvent
cmdEvent(
624 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
626 cmdEvent
.SetEventObject(this);
627 cmdEvent
.SetPosition(m_caretPosition
+1);
629 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
631 SelectWord(GetCaretPosition()+1);
636 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
638 wxRichTextEvent
cmdEvent(
639 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
641 cmdEvent
.SetEventObject(this);
642 cmdEvent
.SetPosition(m_caretPosition
+1);
644 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
649 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
653 flags
|= wxRICHTEXT_CTRL_DOWN
;
654 if (event
.ShiftDown())
655 flags
|= wxRICHTEXT_SHIFT_DOWN
;
657 flags
|= wxRICHTEXT_ALT_DOWN
;
659 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
661 if (event
.GetKeyCode() == WXK_LEFT
||
662 event
.GetKeyCode() == WXK_RIGHT
||
663 event
.GetKeyCode() == WXK_UP
||
664 event
.GetKeyCode() == WXK_DOWN
||
665 event
.GetKeyCode() == WXK_HOME
||
666 event
.GetKeyCode() == WXK_PAGEUP
||
667 event
.GetKeyCode() == WXK_PAGEDOWN
||
668 event
.GetKeyCode() == WXK_END
||
670 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
671 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
672 event
.GetKeyCode() == WXK_NUMPAD_UP
||
673 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
674 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
675 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
676 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
677 event
.GetKeyCode() == WXK_NUMPAD_END
)
679 KeyboardNavigate(event
.GetKeyCode(), flags
);
683 long keycode
= event
.GetKeyCode();
743 case WXK_NUMPAD_HOME
:
744 case WXK_NUMPAD_LEFT
:
746 case WXK_NUMPAD_RIGHT
:
747 case WXK_NUMPAD_DOWN
:
748 case WXK_NUMPAD_PAGEUP
:
749 case WXK_NUMPAD_PAGEDOWN
:
751 case WXK_NUMPAD_BEGIN
:
752 case WXK_NUMPAD_INSERT
:
753 case WXK_NUMPAD_DELETE
:
754 case WXK_WINDOWS_LEFT
:
763 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
764 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
766 BeginBatchUndo(_("Delete Text"));
768 long newPos
= m_caretPosition
;
770 DeleteSelectedContent(& newPos
);
772 // Submit range in character positions, which are greater than caret positions,
773 // so subtract 1 for deleted character and add 1 for conversion to character position.
776 bool processed
= false;
779 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
782 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
788 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
793 if (GetLastPosition() == -1)
797 m_caretPosition
= -1;
799 SetDefaultStyleToCursorStyle();
802 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
804 wxRichTextEvent
cmdEvent(
805 wxEVT_COMMAND_RICHTEXT_DELETE
,
807 cmdEvent
.SetEventObject(this);
808 cmdEvent
.SetFlags(flags
);
809 cmdEvent
.SetPosition(m_caretPosition
+1);
810 GetEventHandler()->ProcessEvent(cmdEvent
);
820 // all the other keys modify the controls contents which shouldn't be
821 // possible if we're read-only
828 if (event
.GetKeyCode() == WXK_RETURN
)
830 BeginBatchUndo(_("Insert Text"));
832 long newPos
= m_caretPosition
;
834 DeleteSelectedContent(& newPos
);
836 if (event
.ShiftDown())
839 text
= wxRichTextLineBreakChar
;
840 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
841 m_caretAtLineStart
= true;
845 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
848 SetDefaultStyleToCursorStyle();
850 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
852 wxRichTextEvent
cmdEvent(
853 wxEVT_COMMAND_RICHTEXT_RETURN
,
855 cmdEvent
.SetEventObject(this);
856 cmdEvent
.SetFlags(flags
);
857 cmdEvent
.SetPosition(newPos
+1);
859 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
861 // Generate conventional event
862 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
863 InitCommandEvent(textEvent
);
865 GetEventHandler()->ProcessEvent(textEvent
);
869 else if (event
.GetKeyCode() == WXK_BACK
)
871 BeginBatchUndo(_("Delete Text"));
873 long newPos
= m_caretPosition
;
875 DeleteSelectedContent(& newPos
);
877 // Submit range in character positions, which are greater than caret positions,
878 // so subtract 1 for deleted character and add 1 for conversion to character position.
881 bool processed
= false;
884 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
887 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
893 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
898 if (GetLastPosition() == -1)
902 m_caretPosition
= -1;
904 SetDefaultStyleToCursorStyle();
907 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
909 wxRichTextEvent
cmdEvent(
910 wxEVT_COMMAND_RICHTEXT_DELETE
,
912 cmdEvent
.SetEventObject(this);
913 cmdEvent
.SetFlags(flags
);
914 cmdEvent
.SetPosition(m_caretPosition
+1);
915 GetEventHandler()->ProcessEvent(cmdEvent
);
919 else if (event
.GetKeyCode() == WXK_DELETE
)
921 BeginBatchUndo(_("Delete Text"));
923 long newPos
= m_caretPosition
;
925 DeleteSelectedContent(& newPos
);
927 // Submit range in character positions, which are greater than caret positions,
928 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
930 bool processed
= false;
933 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
934 if (pos
!= -1 && (pos
> newPos
))
936 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
942 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
947 if (GetLastPosition() == -1)
951 m_caretPosition
= -1;
953 SetDefaultStyleToCursorStyle();
956 wxRichTextEvent
cmdEvent(
957 wxEVT_COMMAND_RICHTEXT_DELETE
,
959 cmdEvent
.SetEventObject(this);
960 cmdEvent
.SetFlags(flags
);
961 cmdEvent
.SetPosition(m_caretPosition
+1);
962 GetEventHandler()->ProcessEvent(cmdEvent
);
968 long keycode
= event
.GetKeyCode();
982 if (event
.CmdDown() || event
.AltDown())
989 wxRichTextEvent
cmdEvent(
990 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
992 cmdEvent
.SetEventObject(this);
993 cmdEvent
.SetFlags(flags
);
995 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
997 cmdEvent
.SetCharacter((wxChar
) keycode
);
999 cmdEvent
.SetPosition(m_caretPosition
+1);
1001 if (keycode
== wxT('\t'))
1003 // See if we need to promote or demote the selection or paragraph at the cursor
1004 // position, instead of inserting a tab.
1005 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1006 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1007 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1009 wxRichTextRange range
;
1011 range
= GetSelectionRange();
1013 range
= para
->GetRange().FromInternal();
1015 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1017 PromoteList(promoteBy
, range
, NULL
);
1019 GetEventHandler()->ProcessEvent(cmdEvent
);
1025 BeginBatchUndo(_("Insert Text"));
1027 long newPos
= m_caretPosition
;
1028 DeleteSelectedContent(& newPos
);
1031 wxString str
= event
.GetUnicodeKey();
1033 wxString str
= (wxChar
) event
.GetKeyCode();
1035 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1039 SetDefaultStyleToCursorStyle();
1040 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1042 GetEventHandler()->ProcessEvent(cmdEvent
);
1050 /// Delete content if there is a selection, e.g. when pressing a key.
1051 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1055 long pos
= m_selectionRange
.GetStart();
1056 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1057 m_selectionRange
.SetRange(-2, -2);
1067 /// Keyboard navigation
1071 Left: left one character
1072 Right: right one character
1075 Ctrl-Left: left one word
1076 Ctrl-Right: right one word
1077 Ctrl-Up: previous paragraph start
1078 Ctrl-Down: next start of paragraph
1081 Ctrl-Home: start of document
1082 Ctrl-End: end of document
1083 Page-Up: Up a screen
1084 Page-Down: Down a screen
1088 Ctrl-Alt-PgUp: Start of window
1089 Ctrl-Alt-PgDn: End of window
1090 F8: Start selection mode
1091 Esc: End selection mode
1093 Adding Shift does the above but starts/extends selection.
1098 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1100 bool success
= false;
1102 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1104 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1105 success
= WordRight(1, flags
);
1107 success
= MoveRight(1, flags
);
1109 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1111 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1112 success
= WordLeft(1, flags
);
1114 success
= MoveLeft(1, flags
);
1116 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1118 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1119 success
= MoveToParagraphStart(flags
);
1121 success
= MoveUp(1, flags
);
1123 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1125 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1126 success
= MoveToParagraphEnd(flags
);
1128 success
= MoveDown(1, flags
);
1130 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1132 success
= PageUp(1, flags
);
1134 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1136 success
= PageDown(1, flags
);
1138 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1140 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1141 success
= MoveHome(flags
);
1143 success
= MoveToLineStart(flags
);
1145 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1147 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1148 success
= MoveEnd(flags
);
1150 success
= MoveToLineEnd(flags
);
1155 ScrollIntoView(m_caretPosition
, keyCode
);
1156 SetDefaultStyleToCursorStyle();
1162 /// Extend the selection. Selections are in caret positions.
1163 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1165 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1167 if (oldPos
== newPos
)
1170 wxRichTextRange oldSelection
= m_selectionRange
;
1172 // If not currently selecting, start selecting
1173 if (m_selectionRange
.GetStart() == -2)
1175 m_selectionAnchor
= oldPos
;
1177 if (oldPos
> newPos
)
1178 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1180 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1184 // Always ensure that the selection range start is greater than
1186 if (newPos
> m_selectionAnchor
)
1187 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1188 else if (newPos
== m_selectionAnchor
)
1189 m_selectionRange
= wxRichTextRange(-2, -2);
1191 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1194 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1196 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1198 wxLogDebug(wxT("Strange selection range"));
1207 /// Scroll into view, returning true if we scrolled.
1208 /// This takes a _caret_ position.
1209 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1211 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1217 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1219 int startXUnits
, startYUnits
;
1220 GetViewStart(& startXUnits
, & startYUnits
);
1221 int startY
= startYUnits
* ppuY
;
1224 GetVirtualSize(& sx
, & sy
);
1230 wxRect rect
= line
->GetRect();
1232 bool scrolled
= false;
1234 wxSize clientSize
= GetClientSize();
1237 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1238 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1239 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1240 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1242 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1244 // Make it scroll so this item is at the bottom
1246 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1247 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1249 // If we're still off the screen, scroll another line down
1250 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1253 if (startYUnits
!= yUnits
)
1255 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1259 else if (rect
.y
< startY
)
1261 // Make it scroll so this item is at the top
1264 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1266 if (startYUnits
!= yUnits
)
1268 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1274 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1275 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1276 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1277 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1279 if (rect
.y
< startY
)
1281 // Make it scroll so this item is at the top
1284 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1286 if (startYUnits
!= yUnits
)
1288 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1292 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1294 // Make it scroll so this item is at the bottom
1296 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1297 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1299 // If we're still off the screen, scroll another line down
1300 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1303 if (startYUnits
!= yUnits
)
1305 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1311 #if !wxRICHTEXT_USE_OWN_CARET
1319 /// Is the given position visible on the screen?
1320 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1322 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1328 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1331 GetViewStart(& startX
, & startY
);
1333 startY
= startY
* ppuY
;
1335 wxRect rect
= line
->GetRect();
1336 wxSize clientSize
= GetClientSize();
1338 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1341 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1343 m_caretPosition
= position
;
1344 m_caretAtLineStart
= showAtLineStart
;
1347 /// Move caret one visual step forward: this may mean setting a flag
1348 /// and keeping the same position if we're going from the end of one line
1349 /// to the start of the next, which may be the exact same caret position.
1350 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1352 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1354 // Only do the check if we're not at the end of the paragraph (where things work OK
1356 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1358 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1362 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1364 // We're at the end of a line. See whether we need to
1365 // stay at the same actual caret position but change visual
1366 // position, or not.
1367 if (oldPosition
== lineRange
.GetEnd())
1369 if (m_caretAtLineStart
)
1371 // We're already at the start of the line, so actually move on now.
1372 m_caretPosition
= oldPosition
+ 1;
1373 m_caretAtLineStart
= false;
1377 // We're showing at the end of the line, so keep to
1378 // the same position but indicate that we're to show
1379 // at the start of the next line.
1380 m_caretPosition
= oldPosition
;
1381 m_caretAtLineStart
= true;
1383 SetDefaultStyleToCursorStyle();
1389 SetDefaultStyleToCursorStyle();
1392 /// Move caret one visual step backward: this may mean setting a flag
1393 /// and keeping the same position if we're going from the end of one line
1394 /// to the start of the next, which may be the exact same caret position.
1395 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1397 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1399 // Only do the check if we're not at the start of the paragraph (where things work OK
1401 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1403 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1407 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1409 // We're at the start of a line. See whether we need to
1410 // stay at the same actual caret position but change visual
1411 // position, or not.
1412 if (oldPosition
== lineRange
.GetStart())
1414 m_caretPosition
= oldPosition
-1;
1415 m_caretAtLineStart
= true;
1418 else if (oldPosition
== lineRange
.GetEnd())
1420 if (m_caretAtLineStart
)
1422 // We're at the start of the line, so keep the same caret position
1423 // but clear the start-of-line flag.
1424 m_caretPosition
= oldPosition
;
1425 m_caretAtLineStart
= false;
1429 // We're showing at the end of the line, so go back
1430 // to the previous character position.
1431 m_caretPosition
= oldPosition
- 1;
1433 SetDefaultStyleToCursorStyle();
1439 SetDefaultStyleToCursorStyle();
1443 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1445 long endPos
= GetBuffer().GetRange().GetEnd();
1447 if (m_caretPosition
+ noPositions
< endPos
)
1449 long oldPos
= m_caretPosition
;
1450 long newPos
= m_caretPosition
+ noPositions
;
1452 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1456 // Determine by looking at oldPos and m_caretPosition whether
1457 // we moved from the end of a line to the start of the next line, in which case
1458 // we want to adjust the caret position such that it is positioned at the
1459 // start of the next line, rather than jumping past the first character of the
1461 if (noPositions
== 1 && !extendSel
)
1462 MoveCaretForward(oldPos
);
1464 SetCaretPosition(newPos
);
1467 SetDefaultStyleToCursorStyle();
1476 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1480 if (m_caretPosition
> startPos
- noPositions
+ 1)
1482 long oldPos
= m_caretPosition
;
1483 long newPos
= m_caretPosition
- noPositions
;
1484 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1488 if (noPositions
== 1 && !extendSel
)
1489 MoveCaretBack(oldPos
);
1491 SetCaretPosition(newPos
);
1494 SetDefaultStyleToCursorStyle();
1503 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1505 return MoveDown(- noLines
, flags
);
1509 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1514 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1515 wxPoint pt
= GetCaret()->GetPosition();
1516 long newLine
= lineNumber
+ noLines
;
1518 if (lineNumber
!= -1)
1522 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1524 if (newLine
> lastLine
)
1534 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1537 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1543 wxClientDC
dc(this);
1545 dc
.SetFont(GetFont());
1547 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1549 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1551 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1552 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1553 // so we view the caret at the start of the line.
1554 bool caretLineStart
= false;
1555 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1557 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1558 wxRichTextRange lineRange
;
1560 lineRange
= thisLine
->GetAbsoluteRange();
1562 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1565 caretLineStart
= true;
1569 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1570 if (para
&& para
->GetRange().GetStart() == newPos
)
1575 long newSelEnd
= newPos
;
1577 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1581 SetCaretPosition(newPos
, caretLineStart
);
1583 SetDefaultStyleToCursorStyle();
1591 /// Move to the end of the paragraph
1592 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1594 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1597 long newPos
= para
->GetRange().GetEnd() - 1;
1598 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1602 SetCaretPosition(newPos
);
1604 SetDefaultStyleToCursorStyle();
1612 /// Move to the start of the paragraph
1613 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1615 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1618 long newPos
= para
->GetRange().GetStart() - 1;
1619 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1623 SetCaretPosition(newPos
);
1625 SetDefaultStyleToCursorStyle();
1633 /// Move to the end of the line
1634 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1636 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1640 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1641 long newPos
= lineRange
.GetEnd();
1642 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1646 SetCaretPosition(newPos
);
1648 SetDefaultStyleToCursorStyle();
1656 /// Move to the start of the line
1657 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1659 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1662 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1663 long newPos
= lineRange
.GetStart()-1;
1665 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1669 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1671 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1673 SetDefaultStyleToCursorStyle();
1681 /// Move to the start of the buffer
1682 bool wxRichTextCtrl::MoveHome(int flags
)
1684 if (m_caretPosition
!= -1)
1686 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1690 SetCaretPosition(-1);
1692 SetDefaultStyleToCursorStyle();
1700 /// Move to the end of the buffer
1701 bool wxRichTextCtrl::MoveEnd(int flags
)
1703 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1705 if (m_caretPosition
!= endPos
)
1707 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1711 SetCaretPosition(endPos
);
1713 SetDefaultStyleToCursorStyle();
1721 /// Move noPages pages up
1722 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1724 return PageDown(- noPages
, flags
);
1727 /// Move noPages pages down
1728 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1730 // Calculate which line occurs noPages * screen height further down.
1731 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1734 wxSize clientSize
= GetClientSize();
1735 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1737 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1740 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1741 long pos
= lineRange
.GetStart()-1;
1742 if (pos
!= m_caretPosition
)
1744 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1746 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1750 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1752 SetDefaultStyleToCursorStyle();
1762 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1764 return str
== wxT(" ") || str
== wxT("\t");
1767 // Finds the caret position for the next word
1768 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1770 long endPos
= GetBuffer().GetRange().GetEnd();
1774 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1776 // First skip current text to space
1777 while (i
< endPos
&& i
> -1)
1779 // i is in character, not caret positions
1780 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1781 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1782 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1786 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1793 while (i
< endPos
&& i
> -1)
1795 // i is in character, not caret positions
1796 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1797 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1798 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1799 return wxMax(-1, i
);
1801 if (text
.empty()) // End of paragraph, or maybe an image
1802 return wxMax(-1, i
- 1);
1803 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1807 // Convert to caret position
1808 return wxMax(-1, i
- 1);
1817 long i
= m_caretPosition
;
1819 // First skip white space
1820 while (i
< endPos
&& i
> -1)
1822 // i is in character, not caret positions
1823 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1824 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1826 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1828 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1833 // Next skip current text to space
1834 while (i
< endPos
&& i
> -1)
1836 // i is in character, not caret positions
1837 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1838 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1839 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1842 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1855 /// Move n words left
1856 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1858 long pos
= FindNextWordPosition(-1);
1859 if (pos
!= m_caretPosition
)
1861 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1863 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1867 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1869 SetDefaultStyleToCursorStyle();
1877 /// Move n words right
1878 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1880 long pos
= FindNextWordPosition(1);
1881 if (pos
!= m_caretPosition
)
1883 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1885 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1889 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1891 SetDefaultStyleToCursorStyle();
1900 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1902 // Only do sizing optimization for large buffers
1903 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1905 m_fullLayoutRequired
= true;
1906 m_fullLayoutTime
= wxGetLocalTimeMillis();
1907 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1908 LayoutContent(true /* onlyVisibleRect */);
1911 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1913 #if wxRICHTEXT_BUFFERED_PAINTING
1921 /// Idle-time processing
1922 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1924 #if wxRICHTEXT_USE_OWN_CARET
1925 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1927 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1933 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1935 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1937 m_fullLayoutRequired
= false;
1938 m_fullLayoutTime
= 0;
1939 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1940 ShowPosition(m_fullLayoutSavedPosition
);
1944 if (m_caretPositionForDefaultStyle
!= -2)
1946 // If the caret position has changed, no longer reflect the default style
1948 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1949 m_caretPositionForDefaultStyle
= -2;
1956 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1958 #if wxRICHTEXT_USE_OWN_CARET
1959 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1962 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1969 /// Set up scrollbars, e.g. after a resize
1970 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1975 if (GetBuffer().IsEmpty())
1977 SetScrollbars(0, 0, 0, 0, 0, 0);
1981 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1982 // of pixels. See e.g. wxVScrolledWindow for ideas.
1983 int pixelsPerUnit
= 5;
1984 wxSize clientSize
= GetClientSize();
1986 int maxHeight
= GetBuffer().GetCachedSize().y
;
1988 // Round up so we have at least maxHeight pixels
1989 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1991 int startX
= 0, startY
= 0;
1993 GetViewStart(& startX
, & startY
);
1995 int maxPositionX
= 0;
1996 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1998 int newStartX
= wxMin(maxPositionX
, startX
);
1999 int newStartY
= wxMin(maxPositionY
, startY
);
2001 int oldPPUX
, oldPPUY
;
2002 int oldStartX
, oldStartY
;
2003 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2004 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2005 GetViewStart(& oldStartX
, & oldStartY
);
2006 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2008 oldVirtualSizeY
/= oldPPUY
;
2010 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2013 // Don't set scrollbars if there were none before, and there will be none now.
2014 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2017 // Move to previous scroll position if
2019 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2022 /// Paint the background
2023 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2025 wxColour backgroundColour
= GetBackgroundColour();
2026 if (!backgroundColour
.Ok())
2027 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2029 // Clear the background
2030 dc
.SetBrush(wxBrush(backgroundColour
));
2031 dc
.SetPen(*wxTRANSPARENT_PEN
);
2032 wxRect
windowRect(GetClientSize());
2033 windowRect
.x
-= 2; windowRect
.y
-= 2;
2034 windowRect
.width
+= 4; windowRect
.height
+= 4;
2036 // We need to shift the rectangle to take into account
2037 // scrolling. Converting device to logical coordinates.
2038 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2039 dc
.DrawRectangle(windowRect
);
2042 #if wxRICHTEXT_BUFFERED_PAINTING
2043 /// Recreate buffer bitmap if necessary
2044 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2047 if (sz
== wxDefaultSize
)
2048 sz
= GetClientSize();
2050 if (sz
.x
< 1 || sz
.y
< 1)
2053 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2054 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2055 return m_bufferBitmap
.Ok();
2059 // ----------------------------------------------------------------------------
2060 // file IO functions
2061 // ----------------------------------------------------------------------------
2063 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2065 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2067 m_filename
= filename
;
2070 SetInsertionPoint(0);
2073 SetupScrollbars(true);
2075 wxTextCtrl::SendTextUpdatedEvent(this);
2081 wxLogError(_("File couldn't be loaded."));
2087 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2089 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2091 m_filename
= filename
;
2098 wxLogError(_("The text couldn't be saved."));
2103 // ----------------------------------------------------------------------------
2104 // wxRichTextCtrl specific functionality
2105 // ----------------------------------------------------------------------------
2107 /// Add a new paragraph of text to the end of the buffer
2108 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2110 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2116 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2118 wxRichTextRange range
= GetBuffer().AddImage(image
);
2123 // ----------------------------------------------------------------------------
2124 // selection and ranges
2125 // ----------------------------------------------------------------------------
2127 void wxRichTextCtrl::SelectAll()
2129 SetSelection(0, GetLastPosition()+1);
2130 m_selectionAnchor
= -1;
2134 void wxRichTextCtrl::SelectNone()
2136 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2138 wxRichTextRange oldSelection
= m_selectionRange
;
2140 m_selectionRange
= wxRichTextRange(-2, -2);
2142 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2144 m_selectionAnchor
= -2;
2147 static bool wxIsWordDelimiter(const wxString
& text
)
2149 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2152 /// Select the word at the given character position
2153 bool wxRichTextCtrl::SelectWord(long position
)
2155 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2158 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2162 if (position
== para
->GetRange().GetEnd())
2165 long positionStart
= position
;
2166 long positionEnd
= position
;
2168 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2170 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2171 if (wxIsWordDelimiter(text
))
2177 if (positionStart
< para
->GetRange().GetStart())
2178 positionStart
= para
->GetRange().GetStart();
2180 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2182 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2183 if (wxIsWordDelimiter(text
))
2189 if (positionEnd
>= para
->GetRange().GetEnd())
2190 positionEnd
= para
->GetRange().GetEnd();
2192 if (positionEnd
< positionStart
)
2195 SetSelection(positionStart
, positionEnd
+1);
2197 if (positionStart
>= 0)
2199 MoveCaret(positionStart
-1, true);
2200 SetDefaultStyleToCursorStyle();
2206 wxString
wxRichTextCtrl::GetStringSelection() const
2209 GetSelection(&from
, &to
);
2211 return GetRange(from
, to
);
2214 // ----------------------------------------------------------------------------
2216 // ----------------------------------------------------------------------------
2218 wxTextCtrlHitTestResult
2219 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2221 // implement in terms of the other overload as the native ports typically
2222 // can get the position and not (x, y) pair directly (although wxUniv
2223 // directly gets x and y -- and so overrides this method as well)
2225 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2227 if ( rc
!= wxTE_HT_UNKNOWN
)
2229 PositionToXY(pos
, x
, y
);
2235 wxTextCtrlHitTestResult
2236 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2239 wxClientDC
dc((wxRichTextCtrl
*) this);
2240 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2242 // Buffer uses logical position (relative to start of buffer)
2244 wxPoint pt2
= GetLogicalPoint(pt
);
2246 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2248 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2249 return wxTE_HT_BEFORE
;
2250 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2251 return wxTE_HT_BEYOND
;
2252 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2253 return wxTE_HT_ON_TEXT
;
2255 return wxTE_HT_UNKNOWN
;
2258 // ----------------------------------------------------------------------------
2259 // set/get the controls text
2260 // ----------------------------------------------------------------------------
2262 wxString
wxRichTextCtrl::GetValue() const
2264 return GetBuffer().GetText();
2267 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2269 // Public API for range is different from internals
2270 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2273 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2275 // Don't call Clear here, since it always sends a text updated event
2276 m_buffer
.ResetAndClearCommands();
2277 m_buffer
.SetDirty(true);
2278 m_caretPosition
= -1;
2279 m_caretPositionForDefaultStyle
= -2;
2280 m_caretAtLineStart
= false;
2281 m_selectionRange
.SetRange(-2, -2);
2291 if (!value
.IsEmpty())
2293 // Remove empty paragraph
2294 GetBuffer().Clear();
2295 DoWriteText(value
, flags
);
2297 // for compatibility, don't move the cursor when doing SetValue()
2298 SetInsertionPoint(0);
2302 // still send an event for consistency
2303 if (flags
& SetValue_SendEvent
)
2304 wxTextCtrl::SendTextUpdatedEvent(this);
2309 void wxRichTextCtrl::WriteText(const wxString
& value
)
2314 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2316 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2318 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2320 if ( flags
& SetValue_SendEvent
)
2321 wxTextCtrl::SendTextUpdatedEvent(this);
2324 void wxRichTextCtrl::AppendText(const wxString
& text
)
2326 SetInsertionPointEnd();
2331 /// Write an image at the current insertion point
2332 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2334 wxRichTextImageBlock imageBlock
;
2336 wxImage image2
= image
;
2337 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2338 return WriteImage(imageBlock
);
2343 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2345 wxRichTextImageBlock imageBlock
;
2348 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2349 return WriteImage(imageBlock
);
2354 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2356 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2359 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2363 wxRichTextImageBlock imageBlock
;
2365 wxImage image
= bitmap
.ConvertToImage();
2366 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2367 return WriteImage(imageBlock
);
2373 /// Insert a newline (actually paragraph) at the current insertion point.
2374 bool wxRichTextCtrl::Newline()
2376 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2379 /// Insert a line break at the current insertion point.
2380 bool wxRichTextCtrl::LineBreak()
2383 text
= wxRichTextLineBreakChar
;
2384 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2387 // ----------------------------------------------------------------------------
2388 // Clipboard operations
2389 // ----------------------------------------------------------------------------
2391 void wxRichTextCtrl::Copy()
2395 wxRichTextRange range
= GetInternalSelectionRange();
2396 GetBuffer().CopyToClipboard(range
);
2400 void wxRichTextCtrl::Cut()
2404 wxRichTextRange range
= GetInternalSelectionRange();
2405 GetBuffer().CopyToClipboard(range
);
2407 DeleteSelectedContent();
2413 void wxRichTextCtrl::Paste()
2417 BeginBatchUndo(_("Paste"));
2419 long newPos
= m_caretPosition
;
2420 DeleteSelectedContent(& newPos
);
2422 GetBuffer().PasteFromClipboard(newPos
);
2428 void wxRichTextCtrl::DeleteSelection()
2430 if (CanDeleteSelection())
2432 DeleteSelectedContent();
2436 bool wxRichTextCtrl::HasSelection() const
2438 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2441 bool wxRichTextCtrl::CanCopy() const
2443 // Can copy if there's a selection
2444 return HasSelection();
2447 bool wxRichTextCtrl::CanCut() const
2449 return HasSelection() && IsEditable();
2452 bool wxRichTextCtrl::CanPaste() const
2454 if ( !IsEditable() )
2457 return GetBuffer().CanPasteFromClipboard();
2460 bool wxRichTextCtrl::CanDeleteSelection() const
2462 return HasSelection() && IsEditable();
2466 // ----------------------------------------------------------------------------
2468 // ----------------------------------------------------------------------------
2470 void wxRichTextCtrl::SetEditable(bool editable
)
2472 m_editable
= editable
;
2475 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2479 m_caretPosition
= pos
- 1;
2484 void wxRichTextCtrl::SetInsertionPointEnd()
2486 long pos
= GetLastPosition();
2487 SetInsertionPoint(pos
);
2490 long wxRichTextCtrl::GetInsertionPoint() const
2492 return m_caretPosition
+1;
2495 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2497 return GetBuffer().GetRange().GetEnd();
2500 // If the return values from and to are the same, there is no
2502 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2504 *from
= m_selectionRange
.GetStart();
2505 *to
= m_selectionRange
.GetEnd();
2506 if ((*to
) != -1 && (*to
) != -2)
2510 bool wxRichTextCtrl::IsEditable() const
2515 // ----------------------------------------------------------------------------
2517 // ----------------------------------------------------------------------------
2519 void wxRichTextCtrl::SetSelection(long from
, long to
)
2521 // if from and to are both -1, it means (in wxWidgets) that all text should
2523 if ( (from
== -1) && (to
== -1) )
2526 to
= GetLastPosition()+1;
2529 DoSetSelection(from
, to
);
2532 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2540 wxRichTextRange oldSelection
= m_selectionRange
;
2541 m_selectionAnchor
= from
;
2542 m_selectionRange
.SetRange(from
, to
-1);
2544 m_caretPosition
= from
-1;
2546 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2551 // ----------------------------------------------------------------------------
2553 // ----------------------------------------------------------------------------
2555 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2556 const wxString
& value
)
2558 BeginBatchUndo(_("Replace"));
2560 DeleteSelectedContent();
2562 DoWriteText(value
, SetValue_SelectionOnly
);
2567 void wxRichTextCtrl::Remove(long from
, long to
)
2571 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2578 bool wxRichTextCtrl::IsModified() const
2580 return m_buffer
.IsModified();
2583 void wxRichTextCtrl::MarkDirty()
2585 m_buffer
.Modify(true);
2588 void wxRichTextCtrl::DiscardEdits()
2590 m_caretPositionForDefaultStyle
= -2;
2591 m_buffer
.Modify(false);
2592 m_buffer
.GetCommandProcessor()->ClearCommands();
2595 int wxRichTextCtrl::GetNumberOfLines() const
2597 return GetBuffer().GetParagraphCount();
2600 // ----------------------------------------------------------------------------
2601 // Positions <-> coords
2602 // ----------------------------------------------------------------------------
2604 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2606 return GetBuffer().XYToPosition(x
, y
);
2609 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2611 return GetBuffer().PositionToXY(pos
, x
, y
);
2614 // ----------------------------------------------------------------------------
2616 // ----------------------------------------------------------------------------
2618 void wxRichTextCtrl::ShowPosition(long pos
)
2620 if (!IsPositionVisible(pos
))
2621 ScrollIntoView(pos
-1, WXK_DOWN
);
2624 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2626 return GetBuffer().GetParagraphLength(lineNo
);
2629 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2631 return GetBuffer().GetParagraphText(lineNo
);
2634 // ----------------------------------------------------------------------------
2636 // ----------------------------------------------------------------------------
2638 void wxRichTextCtrl::Undo()
2642 GetCommandProcessor()->Undo();
2646 void wxRichTextCtrl::Redo()
2650 GetCommandProcessor()->Redo();
2654 bool wxRichTextCtrl::CanUndo() const
2656 return GetCommandProcessor()->CanUndo();
2659 bool wxRichTextCtrl::CanRedo() const
2661 return GetCommandProcessor()->CanRedo();
2664 // ----------------------------------------------------------------------------
2665 // implementation details
2666 // ----------------------------------------------------------------------------
2668 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2670 SetValue(event
.GetString());
2671 GetEventHandler()->ProcessEvent(event
);
2674 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2676 // By default, load the first file into the text window.
2677 if (event
.GetNumberOfFiles() > 0)
2679 LoadFile(event
.GetFiles()[0]);
2683 wxSize
wxRichTextCtrl::DoGetBestSize() const
2685 return wxSize(10, 10);
2688 // ----------------------------------------------------------------------------
2689 // standard handlers for standard edit menu events
2690 // ----------------------------------------------------------------------------
2692 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2697 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2702 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2707 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2712 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2717 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2722 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2724 event
.Enable( CanCut() );
2727 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2729 event
.Enable( CanCopy() );
2732 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2734 event
.Enable( CanDeleteSelection() );
2737 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2739 event
.Enable( CanPaste() );
2742 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2744 event
.Enable( CanUndo() );
2745 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2748 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2750 event
.Enable( CanRedo() );
2751 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2754 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2759 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2761 event
.Enable(GetLastPosition() > 0);
2764 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2766 if (event
.GetEventObject() != this)
2774 m_contextMenu
= new wxMenu
;
2775 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2776 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2777 m_contextMenu
->AppendSeparator();
2778 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2779 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2780 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2781 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2782 m_contextMenu
->AppendSeparator();
2783 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2785 PopupMenu(m_contextMenu
);
2789 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2791 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2794 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2796 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2799 // extended style setting operation with flags including:
2800 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2801 // see richtextbuffer.h for more details.
2803 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2805 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2808 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2810 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2813 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2815 return GetBuffer().GetDefaultStyle();
2818 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2820 return GetBuffer().GetStyle(position
, style
);
2823 // get the common set of styles for the range
2824 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2826 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2829 /// Get the content (uncombined) attributes for this position.
2830 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2832 return GetBuffer().GetUncombinedStyle(position
, style
);
2835 /// Set font, and also the buffer attributes
2836 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2838 wxControl::SetFont(font
);
2840 wxTextAttr attr
= GetBuffer().GetAttributes();
2842 GetBuffer().SetBasicStyle(attr
);
2844 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2850 /// Transform logical to physical
2851 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2854 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2859 /// Transform physical to logical
2860 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2863 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2868 /// Position the caret
2869 void wxRichTextCtrl::PositionCaret()
2874 //wxLogDebug(wxT("PositionCaret"));
2877 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2879 wxPoint newPt
= caretRect
.GetPosition();
2880 wxSize newSz
= caretRect
.GetSize();
2881 wxPoint pt
= GetPhysicalPoint(newPt
);
2882 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2885 if (GetCaret()->GetSize() != newSz
)
2886 GetCaret()->SetSize(newSz
);
2887 GetCaret()->Move(pt
);
2893 /// Get the caret height and position for the given character position
2894 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2896 wxClientDC
dc(this);
2897 dc
.SetFont(GetFont());
2904 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2906 // Caret height can't be zero
2908 height
= dc
.GetCharHeight();
2910 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2917 /// Gets the line for the visible caret position. If the caret is
2918 /// shown at the very end of the line, it means the next character is actually
2919 /// on the following line. So let's get the line we're expecting to find
2920 /// if this is the case.
2921 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2923 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2924 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2927 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2928 if (caretPosition
== lineRange
.GetStart()-1 &&
2929 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2931 if (!m_caretAtLineStart
)
2932 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2939 /// Move the caret to the given character position
2940 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2942 if (GetBuffer().GetDirty())
2945 if (pos
<= GetBuffer().GetRange().GetEnd())
2947 SetCaretPosition(pos
, showAtLineStart
);
2957 /// Layout the buffer: which we must do before certain operations, such as
2958 /// setting the caret position.
2959 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2961 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2963 wxRect
availableSpace(GetClientSize());
2964 if (availableSpace
.width
== 0)
2965 availableSpace
.width
= 10;
2966 if (availableSpace
.height
== 0)
2967 availableSpace
.height
= 10;
2969 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2970 if (onlyVisibleRect
)
2972 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2973 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2976 wxClientDC
dc(this);
2977 dc
.SetFont(GetFont());
2981 GetBuffer().Defragment();
2982 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2983 GetBuffer().Layout(dc
, availableSpace
, flags
);
2984 GetBuffer().SetDirty(false);
2993 /// Is all of the selection bold?
2994 bool wxRichTextCtrl::IsSelectionBold()
2999 wxRichTextRange range
= GetSelectionRange();
3000 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3001 attr
.SetFontWeight(wxBOLD
);
3003 return HasCharacterAttributes(range
, attr
);
3007 // If no selection, then we need to combine current style with default style
3008 // to see what the effect would be if we started typing.
3010 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3012 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3013 if (GetStyle(pos
, attr
))
3015 if (IsDefaultStyleShowing())
3016 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3017 return attr
.GetFontWeight() == wxBOLD
;
3023 /// Is all of the selection italics?
3024 bool wxRichTextCtrl::IsSelectionItalics()
3028 wxRichTextRange range
= GetSelectionRange();
3030 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3031 attr
.SetFontStyle(wxITALIC
);
3033 return HasCharacterAttributes(range
, attr
);
3037 // If no selection, then we need to combine current style with default style
3038 // to see what the effect would be if we started typing.
3040 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3042 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3043 if (GetStyle(pos
, attr
))
3045 if (IsDefaultStyleShowing())
3046 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3047 return attr
.GetFontStyle() == wxITALIC
;
3053 /// Is all of the selection underlined?
3054 bool wxRichTextCtrl::IsSelectionUnderlined()
3058 wxRichTextRange range
= GetSelectionRange();
3060 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3061 attr
.SetFontUnderlined(true);
3063 return HasCharacterAttributes(range
, attr
);
3067 // If no selection, then we need to combine current style with default style
3068 // to see what the effect would be if we started typing.
3070 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3071 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3073 if (GetStyle(pos
, attr
))
3075 if (IsDefaultStyleShowing())
3076 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3077 return attr
.GetFontUnderlined();
3083 /// Apply bold to the selection
3084 bool wxRichTextCtrl::ApplyBoldToSelection()
3087 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3088 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3091 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3094 wxRichTextAttr current
= GetDefaultStyleEx();
3095 current
.Apply(attr
);
3096 SetAndShowDefaultStyle(current
);
3101 /// Apply italic to the selection
3102 bool wxRichTextCtrl::ApplyItalicToSelection()
3105 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3106 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3109 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3112 wxRichTextAttr current
= GetDefaultStyleEx();
3113 current
.Apply(attr
);
3114 SetAndShowDefaultStyle(current
);
3119 /// Apply underline to the selection
3120 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3123 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3124 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3127 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3130 wxRichTextAttr current
= GetDefaultStyleEx();
3131 current
.Apply(attr
);
3132 SetAndShowDefaultStyle(current
);
3137 /// Is all of the selection aligned according to the specified flag?
3138 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3140 wxRichTextRange range
;
3142 range
= GetSelectionRange();
3144 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3147 attr
.SetAlignment(alignment
);
3149 return HasParagraphAttributes(range
, attr
);
3152 /// Apply alignment to the selection
3153 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3156 attr
.SetAlignment(alignment
);
3158 return SetStyle(GetSelectionRange(), attr
);
3161 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3163 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3168 /// Apply a named style to the selection
3169 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3171 // Flags are defined within each definition, so only certain
3172 // attributes are applied.
3173 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3175 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3177 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3179 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3181 wxRichTextRange range
;
3184 range
= GetSelectionRange();
3187 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3188 range
= wxRichTextRange(pos
, pos
+1);
3191 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3194 // Make sure the attr has the style name
3195 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3197 attr
.SetParagraphStyleName(def
->GetName());
3199 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3200 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3201 // to change its style independently.
3202 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3205 attr
.SetCharacterStyleName(def
->GetName());
3208 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3211 wxRichTextAttr current
= GetDefaultStyleEx();
3212 current
.Apply(attr
);
3213 SetAndShowDefaultStyle(current
);
3218 /// Apply the style sheet to the buffer, for example if the styles have changed.
3219 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3222 styleSheet
= GetBuffer().GetStyleSheet();
3226 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3228 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3236 /// Sets the default style to the style under the cursor
3237 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3240 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3242 // If at the start of a paragraph, use the next position.
3243 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3245 if (GetUncombinedStyle(pos
, attr
))
3247 SetDefaultStyle(attr
);
3254 /// Returns the first visible position in the current view
3255 long wxRichTextCtrl::GetFirstVisiblePosition() const
3257 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3259 return line
->GetAbsoluteRange().GetStart();
3264 /// Get the first visible point in the window
3265 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3268 int startXUnits
, startYUnits
;
3270 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3271 GetViewStart(& startXUnits
, & startYUnits
);
3273 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3276 /// The adjusted caret position is the character position adjusted to take
3277 /// into account whether we're at the start of a paragraph, in which case
3278 /// style information should be taken from the next position, not current one.
3279 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3281 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3283 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3288 /// Get/set the selection range in character positions. -1, -1 means no selection.
3289 /// The range is in API convention, i.e. a single character selection is denoted
3291 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3293 wxRichTextRange range
= GetInternalSelectionRange();
3294 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3295 range
.SetEnd(range
.GetEnd() + 1);
3299 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3301 wxRichTextRange
range1(range
);
3302 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3303 range1
.SetEnd(range1
.GetEnd() - 1);
3305 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3307 SetInternalSelectionRange(range1
);
3311 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3313 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3316 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3318 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3321 /// Clear list for given range
3322 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3324 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3327 /// Number/renumber any list elements in the given range
3328 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3330 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3333 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3335 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3338 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3339 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3341 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3344 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3346 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3349 /// Deletes the content in the given range
3350 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3352 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3355 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3357 if (sm_availableFontNames
.GetCount() == 0)
3359 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3360 sm_availableFontNames
.Sort();
3362 return sm_availableFontNames
;
3365 void wxRichTextCtrl::ClearAvailableFontNames()
3367 sm_availableFontNames
.Clear();
3370 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3372 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3374 wxTextAttrEx basicStyle
= GetBasicStyle();
3375 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3376 SetBasicStyle(basicStyle
);
3377 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3382 // Refresh the area affected by a selection change
3383 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3385 // Calculate the refresh rectangle - just the affected lines
3386 long firstPos
, lastPos
;
3387 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3389 firstPos
= newSelection
.GetStart();
3390 lastPos
= newSelection
.GetEnd();
3392 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3394 firstPos
= oldSelection
.GetStart();
3395 lastPos
= oldSelection
.GetEnd();
3397 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3403 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3404 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3407 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3408 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3410 if (firstLine
&& lastLine
)
3412 wxSize clientSize
= GetClientSize();
3413 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3414 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3417 pt1
.y
= wxMax(0, pt1
.y
);
3419 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3421 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3422 RefreshRect(rect
, false);
3430 #if wxRICHTEXT_USE_OWN_CARET
3432 // ----------------------------------------------------------------------------
3433 // initialization and destruction
3434 // ----------------------------------------------------------------------------
3436 void wxRichTextCaret::Init()
3442 m_richTextCtrl
= NULL
;
3443 m_needsUpdate
= false;
3446 wxRichTextCaret::~wxRichTextCaret()
3450 // ----------------------------------------------------------------------------
3451 // showing/hiding/moving the caret (base class interface)
3452 // ----------------------------------------------------------------------------
3454 void wxRichTextCaret::DoShow()
3459 void wxRichTextCaret::DoHide()
3464 void wxRichTextCaret::DoMove()
3470 if (m_xOld
!= -1 && m_yOld
!= -1)
3474 wxRect
rect(GetPosition(), GetSize());
3475 m_richTextCtrl
->RefreshRect(rect
, false);
3484 void wxRichTextCaret::DoSize()
3486 int countVisible
= m_countVisible
;
3487 if (countVisible
> 0)
3493 if (countVisible
> 0)
3495 m_countVisible
= countVisible
;
3500 // ----------------------------------------------------------------------------
3501 // handling the focus
3502 // ----------------------------------------------------------------------------
3504 void wxRichTextCaret::OnSetFocus()
3512 void wxRichTextCaret::OnKillFocus()
3517 // ----------------------------------------------------------------------------
3518 // drawing the caret
3519 // ----------------------------------------------------------------------------
3521 void wxRichTextCaret::Refresh()
3525 wxRect
rect(GetPosition(), GetSize());
3526 m_richTextCtrl
->RefreshRect(rect
, false);
3530 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3532 dc
->SetPen( *wxBLACK_PEN
);
3534 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3535 dc
->SetPen(*wxBLACK_PEN
);
3537 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3538 // done under wxGTK - no idea why
3539 //dc->SetLogicalFunction(wxINVERT);
3541 wxPoint
pt(m_x
, m_y
);
3545 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3547 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3550 // wxRICHTEXT_USE_OWN_CARET