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"
35 // DLL options compatibility check:
37 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
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
)
47 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
48 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
50 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
53 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
55 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
56 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
58 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
60 EVT_PAINT(wxRichTextCtrl::OnPaint
)
61 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
62 EVT_IDLE(wxRichTextCtrl::OnIdle
)
63 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
64 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
65 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
66 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
67 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
68 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
69 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
70 EVT_CHAR(wxRichTextCtrl::OnChar
)
71 EVT_SIZE(wxRichTextCtrl::OnSize
)
72 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
73 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
74 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
76 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
77 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
79 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
80 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
82 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
83 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
85 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
86 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
88 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
89 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
91 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
92 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
94 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
95 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
102 wxRichTextCtrl::wxRichTextCtrl()
103 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
104 : wxScrollHelper(this)
110 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
111 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
112 : wxScrollHelper(this)
116 Create(parent
, id
, value
, pos
, size
, style
);
120 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
122 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
123 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
127 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
134 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
137 GetBuffer().SetRichTextCtrl(this);
139 wxTextAttrEx attributes
;
140 attributes
.SetFont(GetFont());
141 attributes
.SetTextColour(*wxBLACK
);
142 attributes
.SetBackgroundColour(*wxWHITE
);
143 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
144 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
145 SetBasicStyle(attributes
);
147 // The default attributes will be merged with base attributes, so
148 // can be empty to begin with
149 wxTextAttrEx defaultAttributes
;
150 SetDefaultStyle(defaultAttributes
);
152 SetBackgroundColour(*wxWHITE
);
153 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
155 // Tell the sizers to use the given or best size
156 SetBestFittingSize(size
);
159 RecreateBuffer(size
);
161 SetCursor(wxCursor(wxCURSOR_IBEAM
));
163 if (!value
.IsEmpty())
169 wxRichTextCtrl::~wxRichTextCtrl()
171 delete m_contextMenu
;
174 /// Member initialisation
175 void wxRichTextCtrl::Init()
178 m_contextMenu
= NULL
;
180 m_caretPosition
= -1;
181 m_selectionRange
.SetRange(-2, -2);
182 m_selectionAnchor
= -2;
184 m_caretAtLineStart
= false;
186 m_fullLayoutRequired
= false;
187 m_fullLayoutTime
= 0;
188 m_fullLayoutSavedPosition
= 0;
189 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
190 m_caretPositionForDefaultStyle
= -2;
193 /// Call Freeze to prevent refresh
194 void wxRichTextCtrl::Freeze()
199 /// Call Thaw to refresh
200 void wxRichTextCtrl::Thaw()
204 if (m_freezeCount
== 0)
212 void wxRichTextCtrl::Clear()
215 m_buffer
.SetDirty(true);
216 m_caretPosition
= -1;
217 m_caretPositionForDefaultStyle
= -2;
218 m_caretAtLineStart
= false;
219 m_selectionRange
.SetRange(-2, -2);
221 SetScrollbars(0, 0, 0, 0, 0, 0);
223 if (m_freezeCount
== 0)
232 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
238 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
239 //wxLogDebug(wxT("OnPaint"));
243 if (m_freezeCount
> 0)
246 dc
.SetFont(GetFont());
248 // Paint the background
251 wxRegion dirtyRegion
= GetUpdateRegion();
253 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
254 wxRect
availableSpace(GetClientSize());
255 if (GetBuffer().GetDirty())
257 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
258 GetBuffer().SetDirty(false);
262 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
271 // Empty implementation, to prevent flicker
272 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
276 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
278 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
287 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
296 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
302 dc
.SetFont(GetFont());
305 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
307 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
309 m_dragStart
= event
.GetLogicalPosition(dc
);
315 bool caretAtLineStart
= false;
317 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
319 // If we're at the start of a line (but not first in para)
320 // then we should keep the caret showing at the start of the line
321 // by showing the m_caretAtLineStart flag.
322 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
323 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
325 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
326 caretAtLineStart
= true;
330 MoveCaret(position
, caretAtLineStart
);
331 SetDefaultStyleToCursorStyle();
334 wxWindow
* p
= GetParent();
335 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
338 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
341 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
342 frame
->SetStatusText(msg
, 1);
351 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
356 if (GetCapture() == this)
362 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
364 if (!event
.Dragging())
372 dc
.SetFont(GetFont());
375 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
376 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
378 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
380 // TODO: test closeness
382 bool caretAtLineStart
= false;
384 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
386 // If we're at the start of a line (but not first in para)
387 // then we should keep the caret showing at the start of the line
388 // by showing the m_caretAtLineStart flag.
389 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
390 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
392 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
393 caretAtLineStart
= true;
397 if (m_caretPosition
!= position
)
399 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
401 MoveCaret(position
, caretAtLineStart
);
402 SetDefaultStyleToCursorStyle();
411 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
417 /// Left-double-click
418 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
424 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
430 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
433 if (event
.ControlDown())
434 flags
|= wxRICHTEXT_CTRL_DOWN
;
435 if (event
.ShiftDown())
436 flags
|= wxRICHTEXT_SHIFT_DOWN
;
438 flags
|= wxRICHTEXT_ALT_DOWN
;
440 if (event
.GetKeyCode() == WXK_LEFT
||
441 event
.GetKeyCode() == WXK_RIGHT
||
442 event
.GetKeyCode() == WXK_UP
||
443 event
.GetKeyCode() == WXK_DOWN
||
444 event
.GetKeyCode() == WXK_HOME
||
445 event
.GetKeyCode() == WXK_PAGEUP
||
446 event
.GetKeyCode() == WXK_PAGEDOWN
||
447 event
.GetKeyCode() == WXK_END
)
449 KeyboardNavigate(event
.GetKeyCode(), flags
);
453 // all the other keys modify the controls contents which shouldn't be
454 // possible if we're read-only
461 if (event
.GetKeyCode() == WXK_RETURN
)
463 BeginBatchUndo(_("Insert Text"));
465 long newPos
= m_caretPosition
;
467 DeleteSelectedContent(& newPos
);
469 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
471 wxRichTextEvent
cmdEvent(
472 wxEVT_COMMAND_RICHTEXT_RETURN
,
474 cmdEvent
.SetEventObject(this);
475 cmdEvent
.SetFlags(flags
);
476 GetEventHandler()->ProcessEvent(cmdEvent
);
479 SetDefaultStyleToCursorStyle();
481 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
483 else if (event
.GetKeyCode() == WXK_BACK
)
485 BeginBatchUndo(_("Delete Text"));
487 // Submit range in character positions, which are greater than caret positions,
488 // so subtract 1 for deleted character and add 1 for conversion to character position.
489 if (m_caretPosition
> -1 && !HasSelection())
491 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
492 m_caretPosition
, // Current caret position
493 m_caretPosition
-1, // New caret position
497 DeleteSelectedContent();
501 // Shouldn't this be in Do()?
502 if (GetLastPosition() == -1)
506 m_caretPosition
= -1;
508 SetDefaultStyleToCursorStyle();
511 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
513 else if (event
.GetKeyCode() == WXK_DELETE
)
515 BeginBatchUndo(_("Delete Text"));
517 // Submit range in character positions, which are greater than caret positions,
518 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
520 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
521 m_caretPosition
, // Current caret position
522 m_caretPosition
+1, // New caret position
526 DeleteSelectedContent();
530 // Shouldn't this be in Do()?
531 if (GetLastPosition() == -1)
535 m_caretPosition
= -1;
537 SetDefaultStyleToCursorStyle();
542 BeginBatchUndo(_("Insert Text"));
544 long newPos
= m_caretPosition
;
545 DeleteSelectedContent(& newPos
);
547 wxString str
= (wxChar
) event
.GetKeyCode();
548 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
552 SetDefaultStyleToCursorStyle();
553 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
557 /// Delete content if there is a selection, e.g. when pressing a key.
558 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
562 long pos
= m_selectionRange
.GetStart();
563 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
564 m_caretPosition
, // Current caret position
565 pos
, // New caret position
567 m_selectionRange
.SetRange(-2, -2);
577 /// Keyboard navigation
581 Left: left one character
582 Right: right one character
585 Ctrl-Left: left one word
586 Ctrl-Right: right one word
587 Ctrl-Up: previous paragraph start
588 Ctrl-Down: next start of paragraph
591 Ctrl-Home: start of document
592 Ctrl-End: end of document
594 Page-Down: Down a screen
598 Ctrl-Alt-PgUp: Start of window
599 Ctrl-Alt-PgDn: End of window
600 F8: Start selection mode
601 Esc: End selection mode
603 Adding Shift does the above but starts/extends selection.
608 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
610 bool success
= false;
612 if (keyCode
== WXK_RIGHT
)
614 if (flags
& wxRICHTEXT_CTRL_DOWN
)
615 success
= WordRight(1, flags
);
617 success
= MoveRight(1, flags
);
619 else if (keyCode
== WXK_LEFT
)
621 if (flags
& wxRICHTEXT_CTRL_DOWN
)
622 success
= WordLeft(1, flags
);
624 success
= MoveLeft(1, flags
);
626 else if (keyCode
== WXK_UP
)
628 if (flags
& wxRICHTEXT_CTRL_DOWN
)
629 success
= MoveToParagraphStart(flags
);
631 success
= MoveUp(1, flags
);
633 else if (keyCode
== WXK_DOWN
)
635 if (flags
& wxRICHTEXT_CTRL_DOWN
)
636 success
= MoveToParagraphEnd(flags
);
638 success
= MoveDown(1, flags
);
640 else if (keyCode
== WXK_PAGEUP
)
642 success
= PageUp(1, flags
);
644 else if (keyCode
== WXK_PAGEDOWN
)
646 success
= PageDown(1, flags
);
648 else if (keyCode
== WXK_HOME
)
650 if (flags
& wxRICHTEXT_CTRL_DOWN
)
651 success
= MoveHome(flags
);
653 success
= MoveToLineStart(flags
);
655 else if (keyCode
== WXK_END
)
657 if (flags
& wxRICHTEXT_CTRL_DOWN
)
658 success
= MoveEnd(flags
);
660 success
= MoveToLineEnd(flags
);
665 ScrollIntoView(m_caretPosition
, keyCode
);
666 SetDefaultStyleToCursorStyle();
672 /// Extend the selection. Selections are in caret positions.
673 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
675 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
677 // If not currently selecting, start selecting
678 if (m_selectionRange
.GetStart() == -2)
680 m_selectionAnchor
= oldPos
;
683 m_selectionRange
.SetRange(newPos
+1, oldPos
);
685 m_selectionRange
.SetRange(oldPos
+1, newPos
);
689 // Always ensure that the selection range start is greater than
691 if (newPos
> m_selectionAnchor
)
692 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
694 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
697 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
699 wxLogDebug(wxT("Strange selection range"));
708 /// Scroll into view, returning true if we scrolled.
709 /// This takes a _caret_ position.
710 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
712 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
718 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
720 int startXUnits
, startYUnits
;
721 GetViewStart(& startXUnits
, & startYUnits
);
722 int startY
= startYUnits
* ppuY
;
725 GetVirtualSize(& sx
, & sy
);
731 wxRect rect
= line
->GetRect();
733 bool scrolled
= false;
735 wxSize clientSize
= GetClientSize();
738 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
740 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
742 // Make it scroll so this item is at the bottom
744 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
745 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
747 // If we're still off the screen, scroll another line down
748 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
751 if (startYUnits
!= yUnits
)
753 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
757 else if (rect
.y
< startY
)
759 // Make it scroll so this item is at the top
762 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
764 if (startYUnits
!= yUnits
)
766 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
772 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
776 // Make it scroll so this item is at the top
779 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
781 if (startYUnits
!= yUnits
)
783 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
787 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
789 // Make it scroll so this item is at the bottom
791 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
792 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
794 // If we're still off the screen, scroll another line down
795 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
798 if (startYUnits
!= yUnits
)
800 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
810 /// Is the given position visible on the screen?
811 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
813 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
819 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
822 GetViewStart(& startX
, & startY
);
824 startY
= startY
* ppuY
;
827 GetVirtualSize(& sx
, & sy
);
832 wxRect rect
= line
->GetRect();
834 wxSize clientSize
= GetClientSize();
836 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
839 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
841 m_caretPosition
= position
;
842 m_caretAtLineStart
= showAtLineStart
;
845 /// Move caret one visual step forward: this may mean setting a flag
846 /// and keeping the same position if we're going from the end of one line
847 /// to the start of the next, which may be the exact same caret position.
848 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
850 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
852 // Only do the check if we're not at the end of the paragraph (where things work OK
854 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
856 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
860 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
862 // We're at the end of a line. See whether we need to
863 // stay at the same actual caret position but change visual
865 if (oldPosition
== lineRange
.GetEnd())
867 if (m_caretAtLineStart
)
869 // We're already at the start of the line, so actually move on now.
870 m_caretPosition
= oldPosition
+ 1;
871 m_caretAtLineStart
= false;
875 // We're showing at the end of the line, so keep to
876 // the same position but indicate that we're to show
877 // at the start of the next line.
878 m_caretPosition
= oldPosition
;
879 m_caretAtLineStart
= true;
881 SetDefaultStyleToCursorStyle();
887 SetDefaultStyleToCursorStyle();
890 /// Move caret one visual step backward: this may mean setting a flag
891 /// and keeping the same position if we're going from the end of one line
892 /// to the start of the next, which may be the exact same caret position.
893 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
895 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
897 // Only do the check if we're not at the start of the paragraph (where things work OK
899 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
901 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
905 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
907 // We're at the start of a line. See whether we need to
908 // stay at the same actual caret position but change visual
910 if (oldPosition
== lineRange
.GetStart())
912 m_caretPosition
= oldPosition
-1;
913 m_caretAtLineStart
= true;
916 else if (oldPosition
== lineRange
.GetEnd())
918 if (m_caretAtLineStart
)
920 // We're at the start of the line, so keep the same caret position
921 // but clear the start-of-line flag.
922 m_caretPosition
= oldPosition
;
923 m_caretAtLineStart
= false;
927 // We're showing at the end of the line, so go back
928 // to the previous character position.
929 m_caretPosition
= oldPosition
- 1;
931 SetDefaultStyleToCursorStyle();
937 SetDefaultStyleToCursorStyle();
941 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
943 long endPos
= GetBuffer().GetRange().GetEnd();
945 if (m_caretPosition
+ noPositions
< endPos
)
947 long oldPos
= m_caretPosition
;
948 long newPos
= m_caretPosition
+ noPositions
;
950 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
954 // Determine by looking at oldPos and m_caretPosition whether
955 // we moved from the end of a line to the start of the next line, in which case
956 // we want to adjust the caret position such that it is positioned at the
957 // start of the next line, rather than jumping past the first character of the
959 if (noPositions
== 1 && !extendSel
)
960 MoveCaretForward(oldPos
);
962 SetCaretPosition(newPos
);
965 SetDefaultStyleToCursorStyle();
976 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
980 if (m_caretPosition
> startPos
- noPositions
+ 1)
982 long oldPos
= m_caretPosition
;
983 long newPos
= m_caretPosition
- noPositions
;
984 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
988 if (noPositions
== 1 && !extendSel
)
989 MoveCaretBack(oldPos
);
991 SetCaretPosition(newPos
);
994 SetDefaultStyleToCursorStyle();
1005 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1007 return MoveDown(- noLines
, flags
);
1011 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1016 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1017 wxPoint pt
= GetCaret()->GetPosition();
1018 long newLine
= lineNumber
+ noLines
;
1020 if (lineNumber
!= -1)
1024 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1026 if (newLine
> lastLine
)
1036 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1039 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1045 wxClientDC
dc(this);
1047 dc
.SetFont(GetFont());
1049 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1051 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1053 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1054 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1055 // so we view the caret at the start of the line.
1056 bool caretLineStart
= false;
1057 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1059 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1060 wxRichTextRange lineRange
;
1062 lineRange
= thisLine
->GetAbsoluteRange();
1064 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1067 caretLineStart
= true;
1071 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1072 if (para
&& para
->GetRange().GetStart() == newPos
)
1077 long newSelEnd
= newPos
;
1079 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1083 SetCaretPosition(newPos
, caretLineStart
);
1085 SetDefaultStyleToCursorStyle();
1095 /// Move to the end of the paragraph
1096 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1098 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1101 long newPos
= para
->GetRange().GetEnd() - 1;
1102 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1106 SetCaretPosition(newPos
);
1108 SetDefaultStyleToCursorStyle();
1118 /// Move to the start of the paragraph
1119 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1121 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1124 long newPos
= para
->GetRange().GetStart() - 1;
1125 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1129 SetCaretPosition(newPos
);
1131 SetDefaultStyleToCursorStyle();
1141 /// Move to the end of the line
1142 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1144 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1148 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1149 long newPos
= lineRange
.GetEnd();
1150 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1154 SetCaretPosition(newPos
);
1156 SetDefaultStyleToCursorStyle();
1166 /// Move to the start of the line
1167 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1169 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1172 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1173 long newPos
= lineRange
.GetStart()-1;
1175 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1179 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1181 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1183 SetDefaultStyleToCursorStyle();
1193 /// Move to the start of the buffer
1194 bool wxRichTextCtrl::MoveHome(int flags
)
1196 if (m_caretPosition
!= -1)
1198 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1202 SetCaretPosition(-1);
1204 SetDefaultStyleToCursorStyle();
1214 /// Move to the end of the buffer
1215 bool wxRichTextCtrl::MoveEnd(int flags
)
1217 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1219 if (m_caretPosition
!= endPos
)
1221 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1225 SetCaretPosition(endPos
);
1227 SetDefaultStyleToCursorStyle();
1237 /// Move noPages pages up
1238 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1240 return PageDown(- noPages
, flags
);
1243 /// Move noPages pages down
1244 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1246 // Calculate which line occurs noPages * screen height further down.
1247 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1250 wxSize clientSize
= GetClientSize();
1251 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1253 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1256 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1257 long pos
= lineRange
.GetStart()-1;
1258 if (pos
!= m_caretPosition
)
1260 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1262 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1266 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1268 SetDefaultStyleToCursorStyle();
1280 // Finds the caret position for the next word
1281 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1283 long endPos
= GetBuffer().GetRange().GetEnd();
1287 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1289 // First skip current text to space
1290 while (i
< endPos
&& i
> -1)
1292 // i is in character, not caret positions
1293 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1294 if (text
!= wxT(" ") && !text
.empty())
1301 while (i
< endPos
&& i
> -1)
1303 // i is in character, not caret positions
1304 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1305 if (text
.empty()) // End of paragraph, or maybe an image
1306 return wxMax(-1, i
- 1);
1307 else if (text
== wxT(" ") || text
.empty())
1311 // Convert to caret position
1312 return wxMax(-1, i
- 1);
1321 long i
= m_caretPosition
;
1323 // First skip white space
1324 while (i
< endPos
&& i
> -1)
1326 // i is in character, not caret positions
1327 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1328 if (text
.empty()) // End of paragraph, or maybe an image
1330 else if (text
== wxT(" ") || text
.empty())
1335 // Next skip current text to space
1336 while (i
< endPos
&& i
> -1)
1338 // i is in character, not caret positions
1339 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1340 if (text
!= wxT(" ") /* && !text.empty() */)
1353 /// Move n words left
1354 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1356 long pos
= FindNextWordPosition(-1);
1357 if (pos
!= m_caretPosition
)
1359 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1361 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1365 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1367 SetDefaultStyleToCursorStyle();
1377 /// Move n words right
1378 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1380 long pos
= FindNextWordPosition(1);
1381 if (pos
!= m_caretPosition
)
1383 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1385 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1389 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1391 SetDefaultStyleToCursorStyle();
1402 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1404 // Only do sizing optimization for large buffers
1405 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1407 m_fullLayoutRequired
= true;
1408 m_fullLayoutTime
= wxGetLocalTimeMillis();
1409 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1410 LayoutContent(true /* onlyVisibleRect */);
1413 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1421 /// Idle-time processing
1422 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1424 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1426 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1428 m_fullLayoutRequired
= false;
1429 m_fullLayoutTime
= 0;
1430 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1431 ShowPosition(m_fullLayoutSavedPosition
);
1435 if (m_caretPositionForDefaultStyle
!= -2)
1437 // If the caret position has changed, no longer reflect the default style
1439 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1440 m_caretPositionForDefaultStyle
= -2;
1447 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1453 /// Set up scrollbars, e.g. after a resize
1454 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1459 if (GetBuffer().IsEmpty())
1461 SetScrollbars(0, 0, 0, 0, 0, 0);
1465 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1466 // of pixels. See e.g. wxVScrolledWindow for ideas.
1467 int pixelsPerUnit
= 5;
1468 wxSize clientSize
= GetClientSize();
1470 int maxHeight
= GetBuffer().GetCachedSize().y
;
1472 // Round up so we have at least maxHeight pixels
1473 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1475 int startX
= 0, startY
= 0;
1477 GetViewStart(& startX
, & startY
);
1479 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1480 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1482 // Move to previous scroll position if
1484 SetScrollbars(0, pixelsPerUnit
,
1486 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1489 /// Paint the background
1490 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1492 wxColour backgroundColour
= GetBackgroundColour();
1493 if (!backgroundColour
.Ok())
1494 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1496 // Clear the background
1497 dc
.SetBrush(wxBrush(backgroundColour
));
1498 dc
.SetPen(*wxTRANSPARENT_PEN
);
1499 wxRect
windowRect(GetClientSize());
1500 windowRect
.x
-= 2; windowRect
.y
-= 2;
1501 windowRect
.width
+= 4; windowRect
.height
+= 4;
1503 // We need to shift the rectangle to take into account
1504 // scrolling. Converting device to logical coordinates.
1505 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1506 dc
.DrawRectangle(windowRect
);
1509 /// Recreate buffer bitmap if necessary
1510 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1513 if (sz
== wxDefaultSize
)
1514 sz
= GetClientSize();
1516 if (sz
.x
< 1 || sz
.y
< 1)
1519 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1520 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1521 return m_bufferBitmap
.Ok();
1524 // ----------------------------------------------------------------------------
1525 // file IO functions
1526 // ----------------------------------------------------------------------------
1528 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1529 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int fileType
)
1531 return DoLoadFile(filename
, fileType
);
1534 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int fileType
)
1536 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1537 if ( filenameToUse
.empty() )
1539 // what kind of message to give? is it an error or a program bug?
1540 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1545 return DoSaveFile(filenameToUse
, fileType
);
1549 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1551 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1553 m_filename
= filename
;
1556 SetInsertionPoint(0);
1559 SetupScrollbars(true);
1567 wxLogError(_("File couldn't be loaded."));
1573 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1575 if (GetBuffer().SaveFile(filename
, fileType
))
1577 m_filename
= filename
;
1584 wxLogError(_("The text couldn't be saved."));
1589 // ----------------------------------------------------------------------------
1590 // wxRichTextCtrl specific functionality
1591 // ----------------------------------------------------------------------------
1593 /// Add a new paragraph of text to the end of the buffer
1594 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1596 return GetBuffer().AddParagraph(text
);
1600 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1602 return GetBuffer().AddImage(image
);
1605 // ----------------------------------------------------------------------------
1606 // selection and ranges
1607 // ----------------------------------------------------------------------------
1609 void wxRichTextCtrl::SelectAll()
1611 SetSelection(0, GetLastPosition()+1);
1612 m_selectionAnchor
= -1;
1616 void wxRichTextCtrl::SelectNone()
1618 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1619 SetSelection(-2, -2);
1620 m_selectionAnchor
= -2;
1623 wxString
wxRichTextCtrl::GetStringSelection() const
1626 GetSelection(&from
, &to
);
1628 return GetRange(from
, to
);
1631 // do the window-specific processing after processing the update event
1632 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1633 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1636 wxWindowBase::DoUpdateWindowUI(event
);
1639 if ( event
.GetSetText() )
1641 if ( event
.GetText() != GetValue() )
1642 SetValue(event
.GetText());
1645 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1647 // ----------------------------------------------------------------------------
1649 // ----------------------------------------------------------------------------
1651 wxTextCtrlHitTestResult
1652 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1654 // implement in terms of the other overload as the native ports typically
1655 // can get the position and not (x, y) pair directly (although wxUniv
1656 // directly gets x and y -- and so overrides this method as well)
1658 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1660 if ( rc
!= wxTE_HT_UNKNOWN
)
1662 PositionToXY(pos
, x
, y
);
1668 wxTextCtrlHitTestResult
1669 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1672 wxClientDC
dc((wxRichTextCtrl
*) this);
1673 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1675 // Buffer uses logical position (relative to start of buffer)
1677 wxPoint pt2
= GetLogicalPoint(pt
);
1679 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1683 case wxRICHTEXT_HITTEST_BEFORE
:
1684 return wxTE_HT_BEFORE
;
1686 case wxRICHTEXT_HITTEST_AFTER
:
1687 return wxTE_HT_BEYOND
;
1689 case wxRICHTEXT_HITTEST_ON
:
1690 return wxTE_HT_ON_TEXT
;
1693 return wxTE_HT_UNKNOWN
;
1696 // ----------------------------------------------------------------------------
1697 // set/get the controls text
1698 // ----------------------------------------------------------------------------
1700 wxString
wxRichTextCtrl::GetValue() const
1702 return GetBuffer().GetText();
1705 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1707 // Public API for range is different from internals
1708 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1711 void wxRichTextCtrl::SetValue(const wxString
& value
)
1715 // if the text is long enough, it's faster to just set it instead of first
1716 // comparing it with the old one (chances are that it will be different
1717 // anyhow, this comparison is there to avoid flicker for small single-line
1718 // edit controls mostly)
1719 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1721 DoWriteText(value
, false /* not selection only */);
1723 // for compatibility, don't move the cursor when doing SetValue()
1724 SetInsertionPoint(0);
1728 // still send an event for consistency
1732 // we should reset the modified flag even if the value didn't really change
1734 // mark the control as being not dirty - we changed its text, not the
1739 void wxRichTextCtrl::WriteText(const wxString
& value
)
1744 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1746 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1748 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueDos
, this);
1751 void wxRichTextCtrl::AppendText(const wxString
& text
)
1753 SetInsertionPointEnd();
1758 /// Write an image at the current insertion point
1759 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1761 wxRichTextImageBlock imageBlock
;
1763 wxImage image2
= image
;
1764 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1765 return WriteImage(imageBlock
);
1770 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1772 wxRichTextImageBlock imageBlock
;
1775 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1776 return WriteImage(imageBlock
);
1781 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1783 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1786 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1790 wxRichTextImageBlock imageBlock
;
1792 wxImage image
= bitmap
.ConvertToImage();
1793 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1794 return WriteImage(imageBlock
);
1800 /// Insert a newline (actually paragraph) at the current insertion point.
1801 bool wxRichTextCtrl::Newline()
1803 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1807 // ----------------------------------------------------------------------------
1808 // Clipboard operations
1809 // ----------------------------------------------------------------------------
1811 void wxRichTextCtrl::Copy()
1815 wxRichTextRange range
= GetInternalSelectionRange();
1816 GetBuffer().CopyToClipboard(range
);
1820 void wxRichTextCtrl::Cut()
1824 wxRichTextRange range
= GetInternalSelectionRange();
1825 GetBuffer().CopyToClipboard(range
);
1827 DeleteSelectedContent();
1833 void wxRichTextCtrl::Paste()
1837 BeginBatchUndo(_("Paste"));
1839 long newPos
= m_caretPosition
;
1840 DeleteSelectedContent(& newPos
);
1842 GetBuffer().PasteFromClipboard(newPos
);
1848 void wxRichTextCtrl::DeleteSelection()
1850 if (CanDeleteSelection())
1852 DeleteSelectedContent();
1856 bool wxRichTextCtrl::HasSelection() const
1858 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1861 bool wxRichTextCtrl::CanCopy() const
1863 // Can copy if there's a selection
1864 return HasSelection();
1867 bool wxRichTextCtrl::CanCut() const
1869 return HasSelection() && IsEditable();
1872 bool wxRichTextCtrl::CanPaste() const
1874 if ( !IsEditable() )
1877 return GetBuffer().CanPasteFromClipboard();
1880 bool wxRichTextCtrl::CanDeleteSelection() const
1882 return HasSelection() && IsEditable();
1886 // ----------------------------------------------------------------------------
1888 // ----------------------------------------------------------------------------
1890 void wxRichTextCtrl::SetEditable(bool editable
)
1892 m_editable
= editable
;
1895 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1899 m_caretPosition
= pos
- 1;
1902 void wxRichTextCtrl::SetInsertionPointEnd()
1904 long pos
= GetLastPosition();
1905 SetInsertionPoint(pos
);
1908 long wxRichTextCtrl::GetInsertionPoint() const
1910 return m_caretPosition
+1;
1913 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1915 return GetBuffer().GetRange().GetEnd();
1918 // If the return values from and to are the same, there is no
1920 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1922 *from
= m_selectionRange
.GetStart();
1923 *to
= m_selectionRange
.GetEnd();
1924 if ((*to
) != -1 && (*to
) != -2)
1928 bool wxRichTextCtrl::IsEditable() const
1933 // ----------------------------------------------------------------------------
1935 // ----------------------------------------------------------------------------
1937 void wxRichTextCtrl::SetSelection(long from
, long to
)
1939 // if from and to are both -1, it means (in wxWidgets) that all text should
1941 if ( (from
== -1) && (to
== -1) )
1944 to
= GetLastPosition()+1;
1947 DoSetSelection(from
, to
);
1950 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1952 m_selectionAnchor
= from
;
1953 m_selectionRange
.SetRange(from
, to
-1);
1958 // ----------------------------------------------------------------------------
1960 // ----------------------------------------------------------------------------
1962 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1964 BeginBatchUndo(_("Replace"));
1966 DeleteSelectedContent();
1968 DoWriteText(value
, true /* selection only */);
1973 void wxRichTextCtrl::Remove(long from
, long to
)
1977 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1978 m_caretPosition
, // Current caret position
1979 from
, // New caret position
1987 bool wxRichTextCtrl::IsModified() const
1989 return m_buffer
.IsModified();
1992 void wxRichTextCtrl::MarkDirty()
1994 m_buffer
.Modify(true);
1997 void wxRichTextCtrl::DiscardEdits()
1999 m_caretPositionForDefaultStyle
= -2;
2000 m_buffer
.Modify(false);
2001 m_buffer
.GetCommandProcessor()->ClearCommands();
2004 int wxRichTextCtrl::GetNumberOfLines() const
2006 return GetBuffer().GetParagraphCount();
2009 // ----------------------------------------------------------------------------
2010 // Positions <-> coords
2011 // ----------------------------------------------------------------------------
2013 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2015 return GetBuffer().XYToPosition(x
, y
);
2018 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2020 return GetBuffer().PositionToXY(pos
, x
, y
);
2023 // ----------------------------------------------------------------------------
2025 // ----------------------------------------------------------------------------
2027 void wxRichTextCtrl::ShowPosition(long pos
)
2029 if (!IsPositionVisible(pos
))
2030 ScrollIntoView(pos
-1, WXK_DOWN
);
2033 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2035 return GetBuffer().GetParagraphLength(lineNo
);
2038 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2040 return GetBuffer().GetParagraphText(lineNo
);
2043 // ----------------------------------------------------------------------------
2045 // ----------------------------------------------------------------------------
2047 void wxRichTextCtrl::Undo()
2051 GetCommandProcessor()->Undo();
2055 void wxRichTextCtrl::Redo()
2059 GetCommandProcessor()->Redo();
2063 bool wxRichTextCtrl::CanUndo() const
2065 return GetCommandProcessor()->CanUndo();
2068 bool wxRichTextCtrl::CanRedo() const
2070 return GetCommandProcessor()->CanRedo();
2073 // ----------------------------------------------------------------------------
2074 // implementation details
2075 // ----------------------------------------------------------------------------
2077 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2079 SetValue(event
.GetString());
2080 GetEventHandler()->ProcessEvent(event
);
2083 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2085 // By default, load the first file into the text window.
2086 if (event
.GetNumberOfFiles() > 0)
2088 LoadFile(event
.GetFiles()[0]);
2092 // ----------------------------------------------------------------------------
2093 // text control event processing
2094 // ----------------------------------------------------------------------------
2096 bool wxRichTextCtrl::SendUpdateEvent()
2098 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2099 InitCommandEvent(event
);
2101 return GetEventHandler()->ProcessEvent(event
);
2104 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2106 event
.SetEventObject((wxControlBase
*)this); // const_cast
2108 switch ( m_clientDataType
)
2110 case wxClientData_Void
:
2111 event
.SetClientData(GetClientData());
2114 case wxClientData_Object
:
2115 event
.SetClientObject(GetClientObject());
2118 case wxClientData_None
:
2125 wxSize
wxRichTextCtrl::DoGetBestSize() const
2127 return wxSize(10, 10);
2130 // ----------------------------------------------------------------------------
2131 // standard handlers for standard edit menu events
2132 // ----------------------------------------------------------------------------
2134 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2139 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2144 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2149 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2154 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2159 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2164 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2166 event
.Enable( CanCut() );
2169 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2171 event
.Enable( CanCopy() );
2174 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2176 event
.Enable( CanDeleteSelection() );
2179 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2181 event
.Enable( CanPaste() );
2184 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2186 event
.Enable( CanUndo() );
2187 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2190 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2192 event
.Enable( CanRedo() );
2193 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2196 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2201 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2203 event
.Enable(GetLastPosition() > 0);
2206 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2210 m_contextMenu
= new wxMenu
;
2211 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2212 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2213 m_contextMenu
->AppendSeparator();
2214 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2215 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2216 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2217 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2218 m_contextMenu
->AppendSeparator();
2219 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2221 PopupMenu(m_contextMenu
);
2225 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2227 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2230 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2232 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2235 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2237 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2240 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2242 return GetBuffer().SetDefaultStyle(style
);
2245 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2247 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2250 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2252 return GetBuffer().GetDefaultStyle();
2255 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2257 return GetBuffer().GetDefaultStyle();
2260 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2262 wxTextAttrEx
attr(style
);
2263 if (GetBuffer().GetStyle(position
, attr
))
2272 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2274 return GetBuffer().GetStyle(position
, style
);
2277 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2279 return GetBuffer().GetStyle(position
, style
);
2282 /// Get the content (uncombined) attributes for this position.
2284 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2286 wxTextAttrEx
attr(style
);
2287 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2296 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2298 return GetBuffer().GetUncombinedStyle(position
, style
);
2301 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2303 return GetBuffer().GetUncombinedStyle(position
, style
);
2306 /// Set font, and also the buffer attributes
2307 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2309 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2310 wxControl::SetFont(font
);
2312 wxScrolledWindow::SetFont(font
);
2315 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2317 GetBuffer().SetBasicStyle(attr
);
2319 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2320 // Don't set the default style, since it will be inherited from
2322 GetBuffer().SetDefaultStyle(attr
);
2325 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2331 /// Transform logical to physical
2332 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2335 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2340 /// Transform physical to logical
2341 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2344 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2349 /// Position the caret
2350 void wxRichTextCtrl::PositionCaret()
2355 //wxLogDebug(wxT("PositionCaret"));
2358 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2360 wxPoint originalPt
= caretRect
.GetPosition();
2361 wxPoint pt
= GetPhysicalPoint(originalPt
);
2362 if (GetCaret()->GetPosition() != pt
)
2364 GetCaret()->Move(pt
);
2365 GetCaret()->SetSize(caretRect
.GetSize());
2370 /// Get the caret height and position for the given character position
2371 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2373 wxClientDC
dc(this);
2374 dc
.SetFont(GetFont());
2381 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2383 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2390 /// Gets the line for the visible caret position. If the caret is
2391 /// shown at the very end of the line, it means the next character is actually
2392 /// on the following line. So let's get the line we're expecting to find
2393 /// if this is the case.
2394 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2396 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2397 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2400 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2401 if (caretPosition
== lineRange
.GetStart()-1 &&
2402 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2404 if (!m_caretAtLineStart
)
2405 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2412 /// Move the caret to the given character position
2413 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2415 if (GetBuffer().GetDirty())
2418 if (pos
<= GetBuffer().GetRange().GetEnd())
2420 SetCaretPosition(pos
, showAtLineStart
);
2430 /// Layout the buffer: which we must do before certain operations, such as
2431 /// setting the caret position.
2432 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2434 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2436 wxRect
availableSpace(GetClientSize());
2437 if (availableSpace
.width
== 0)
2438 availableSpace
.width
= 10;
2439 if (availableSpace
.height
== 0)
2440 availableSpace
.height
= 10;
2442 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2443 if (onlyVisibleRect
)
2445 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2446 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2449 wxClientDC
dc(this);
2450 dc
.SetFont(GetFont());
2454 GetBuffer().Defragment();
2455 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2456 GetBuffer().Layout(dc
, availableSpace
, flags
);
2457 GetBuffer().SetDirty(false);
2466 /// Is all of the selection bold?
2467 bool wxRichTextCtrl::IsSelectionBold()
2471 wxRichTextAttr attr
;
2472 wxRichTextRange range
= GetInternalSelectionRange();
2473 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2474 attr
.SetFontWeight(wxBOLD
);
2476 return HasCharacterAttributes(range
, attr
);
2480 // If no selection, then we need to combine current style with default style
2481 // to see what the effect would be if we started typing.
2482 wxRichTextAttr attr
;
2483 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2485 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2486 if (GetStyle(pos
, attr
))
2488 if (IsDefaultStyleShowing())
2489 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2490 return attr
.GetFontWeight() == wxBOLD
;
2496 /// Is all of the selection italics?
2497 bool wxRichTextCtrl::IsSelectionItalics()
2501 wxRichTextRange range
= GetInternalSelectionRange();
2502 wxRichTextAttr attr
;
2503 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2504 attr
.SetFontStyle(wxITALIC
);
2506 return HasCharacterAttributes(range
, attr
);
2510 // If no selection, then we need to combine current style with default style
2511 // to see what the effect would be if we started typing.
2512 wxRichTextAttr attr
;
2513 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2515 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2516 if (GetStyle(pos
, attr
))
2518 if (IsDefaultStyleShowing())
2519 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2520 return attr
.GetFontStyle() == wxITALIC
;
2526 /// Is all of the selection underlined?
2527 bool wxRichTextCtrl::IsSelectionUnderlined()
2531 wxRichTextRange range
= GetInternalSelectionRange();
2532 wxRichTextAttr attr
;
2533 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2534 attr
.SetFontUnderlined(true);
2536 return HasCharacterAttributes(range
, attr
);
2540 // If no selection, then we need to combine current style with default style
2541 // to see what the effect would be if we started typing.
2542 wxRichTextAttr attr
;
2543 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2544 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2546 if (GetStyle(pos
, attr
))
2548 if (IsDefaultStyleShowing())
2549 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2550 return attr
.GetFontUnderlined();
2556 /// Apply bold to the selection
2557 bool wxRichTextCtrl::ApplyBoldToSelection()
2559 wxRichTextAttr attr
;
2560 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2561 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2564 return SetStyle(GetSelectionRange(), attr
);
2566 SetAndShowDefaultStyle(attr
);
2570 /// Apply italic to the selection
2571 bool wxRichTextCtrl::ApplyItalicToSelection()
2573 wxRichTextAttr attr
;
2574 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2575 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2578 return SetStyle(GetSelectionRange(), attr
);
2580 SetAndShowDefaultStyle(attr
);
2584 /// Apply underline to the selection
2585 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2587 wxRichTextAttr attr
;
2588 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2589 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2592 return SetStyle(GetSelectionRange(), attr
);
2594 SetAndShowDefaultStyle(attr
);
2598 /// Is all of the selection aligned according to the specified flag?
2599 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2603 wxRichTextRange range
= GetInternalSelectionRange();
2604 wxRichTextAttr attr
;
2605 attr
.SetAlignment(alignment
);
2607 return HasParagraphAttributes(range
, attr
);
2611 // If no selection, then we need to get information from the current paragraph.
2612 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2614 return para
->GetAttributes().GetAlignment() == alignment
;
2619 /// Apply alignment to the selection
2620 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2622 wxRichTextAttr attr
;
2623 attr
.SetAlignment(alignment
);
2625 return SetStyle(GetSelectionRange(), attr
);
2628 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2630 return SetStyle(para
->GetRange().FromInternal(), attr
);
2635 /// Apply a named style to the selection
2636 void wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2638 // Flags are defined within each definition, so only certain
2639 // attributes are applied.
2640 wxRichTextAttr
attr(def
->GetStyle());
2642 // Make sure the attr has the style name
2643 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2644 attr
.SetParagraphStyleName(def
->GetName());
2646 attr
.SetCharacterStyleName(def
->GetName());
2649 SetStyle(GetSelectionRange(), attr
);
2651 SetAndShowDefaultStyle(attr
);
2654 /// Apply the style sheet to the buffer, for example if the styles have changed.
2655 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2658 styleSheet
= GetBuffer().GetStyleSheet();
2662 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2664 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2672 /// Sets the default style to the style under the cursor
2673 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2676 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2678 // If at the start of a paragraph, use the next position.
2679 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2681 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2682 if (GetUncombinedStyle(pos
, attr
))
2684 if (GetStyle(pos
, attr
))
2687 SetDefaultStyle(attr
);
2694 /// Returns the first visible position in the current view
2695 long wxRichTextCtrl::GetFirstVisiblePosition() const
2697 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2699 return line
->GetAbsoluteRange().GetStart();
2704 /// The adjusted caret position is the character position adjusted to take
2705 /// into account whether we're at the start of a paragraph, in which case
2706 /// style information should be taken from the next position, not current one.
2707 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2709 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2711 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2716 /// Get/set the selection range in character positions. -1, -1 means no selection.
2717 /// The range is in API convention, i.e. a single character selection is denoted
2719 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2721 wxRichTextRange range
= GetInternalSelectionRange();
2722 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2723 range
.SetEnd(range
.GetEnd() + 1);
2727 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2729 wxRichTextRange
range1(range
);
2730 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2731 range1
.SetEnd(range1
.GetEnd() - 1);
2733 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2735 SetInternalSelectionRange(range1
);