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();
338 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
343 if (GetCapture() == this)
349 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
351 if (!event
.Dragging())
359 dc
.SetFont(GetFont());
362 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
363 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
365 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
367 // TODO: test closeness
369 bool caretAtLineStart
= false;
371 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
373 // If we're at the start of a line (but not first in para)
374 // then we should keep the caret showing at the start of the line
375 // by showing the m_caretAtLineStart flag.
376 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
377 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
379 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
380 caretAtLineStart
= true;
384 if (m_caretPosition
!= position
)
386 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
388 MoveCaret(position
, caretAtLineStart
);
389 SetDefaultStyleToCursorStyle();
398 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
404 /// Left-double-click
405 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
407 SelectWord(GetCaretPosition()+1);
412 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
418 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
421 if (event
.ControlDown())
422 flags
|= wxRICHTEXT_CTRL_DOWN
;
423 if (event
.ShiftDown())
424 flags
|= wxRICHTEXT_SHIFT_DOWN
;
426 flags
|= wxRICHTEXT_ALT_DOWN
;
428 if (event
.GetKeyCode() == WXK_LEFT
||
429 event
.GetKeyCode() == WXK_RIGHT
||
430 event
.GetKeyCode() == WXK_UP
||
431 event
.GetKeyCode() == WXK_DOWN
||
432 event
.GetKeyCode() == WXK_HOME
||
433 event
.GetKeyCode() == WXK_PAGEUP
||
434 event
.GetKeyCode() == WXK_PAGEDOWN
||
435 event
.GetKeyCode() == WXK_END
)
437 KeyboardNavigate(event
.GetKeyCode(), flags
);
441 // all the other keys modify the controls contents which shouldn't be
442 // possible if we're read-only
449 if (event
.GetKeyCode() == WXK_RETURN
)
451 BeginBatchUndo(_("Insert Text"));
453 long newPos
= m_caretPosition
;
455 DeleteSelectedContent(& newPos
);
457 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
459 wxRichTextEvent
cmdEvent(
460 wxEVT_COMMAND_RICHTEXT_RETURN
,
462 cmdEvent
.SetEventObject(this);
463 cmdEvent
.SetFlags(flags
);
464 GetEventHandler()->ProcessEvent(cmdEvent
);
467 SetDefaultStyleToCursorStyle();
469 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
471 else if (event
.GetKeyCode() == WXK_BACK
)
473 BeginBatchUndo(_("Delete Text"));
475 // Submit range in character positions, which are greater than caret positions,
476 // so subtract 1 for deleted character and add 1 for conversion to character position.
477 if (m_caretPosition
> -1 && !HasSelection())
479 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
480 m_caretPosition
, // Current caret position
481 m_caretPosition
-1, // New caret position
485 DeleteSelectedContent();
489 // Shouldn't this be in Do()?
490 if (GetLastPosition() == -1)
494 m_caretPosition
= -1;
496 SetDefaultStyleToCursorStyle();
499 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
501 else if (event
.GetKeyCode() == WXK_DELETE
)
503 BeginBatchUndo(_("Delete Text"));
505 // Submit range in character positions, which are greater than caret positions,
506 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
508 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
509 m_caretPosition
, // Current caret position
510 m_caretPosition
+1, // New caret position
514 DeleteSelectedContent();
518 // Shouldn't this be in Do()?
519 if (GetLastPosition() == -1)
523 m_caretPosition
= -1;
525 SetDefaultStyleToCursorStyle();
530 BeginBatchUndo(_("Insert Text"));
532 long newPos
= m_caretPosition
;
533 DeleteSelectedContent(& newPos
);
535 wxString str
= (wxChar
) event
.GetKeyCode();
536 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
540 SetDefaultStyleToCursorStyle();
541 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
545 /// Delete content if there is a selection, e.g. when pressing a key.
546 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
550 long pos
= m_selectionRange
.GetStart();
551 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
552 m_caretPosition
, // Current caret position
553 pos
, // New caret position
555 m_selectionRange
.SetRange(-2, -2);
565 /// Keyboard navigation
569 Left: left one character
570 Right: right one character
573 Ctrl-Left: left one word
574 Ctrl-Right: right one word
575 Ctrl-Up: previous paragraph start
576 Ctrl-Down: next start of paragraph
579 Ctrl-Home: start of document
580 Ctrl-End: end of document
582 Page-Down: Down a screen
586 Ctrl-Alt-PgUp: Start of window
587 Ctrl-Alt-PgDn: End of window
588 F8: Start selection mode
589 Esc: End selection mode
591 Adding Shift does the above but starts/extends selection.
596 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
598 bool success
= false;
600 if (keyCode
== WXK_RIGHT
)
602 if (flags
& wxRICHTEXT_CTRL_DOWN
)
603 success
= WordRight(1, flags
);
605 success
= MoveRight(1, flags
);
607 else if (keyCode
== WXK_LEFT
)
609 if (flags
& wxRICHTEXT_CTRL_DOWN
)
610 success
= WordLeft(1, flags
);
612 success
= MoveLeft(1, flags
);
614 else if (keyCode
== WXK_UP
)
616 if (flags
& wxRICHTEXT_CTRL_DOWN
)
617 success
= MoveToParagraphStart(flags
);
619 success
= MoveUp(1, flags
);
621 else if (keyCode
== WXK_DOWN
)
623 if (flags
& wxRICHTEXT_CTRL_DOWN
)
624 success
= MoveToParagraphEnd(flags
);
626 success
= MoveDown(1, flags
);
628 else if (keyCode
== WXK_PAGEUP
)
630 success
= PageUp(1, flags
);
632 else if (keyCode
== WXK_PAGEDOWN
)
634 success
= PageDown(1, flags
);
636 else if (keyCode
== WXK_HOME
)
638 if (flags
& wxRICHTEXT_CTRL_DOWN
)
639 success
= MoveHome(flags
);
641 success
= MoveToLineStart(flags
);
643 else if (keyCode
== WXK_END
)
645 if (flags
& wxRICHTEXT_CTRL_DOWN
)
646 success
= MoveEnd(flags
);
648 success
= MoveToLineEnd(flags
);
653 ScrollIntoView(m_caretPosition
, keyCode
);
654 SetDefaultStyleToCursorStyle();
660 /// Extend the selection. Selections are in caret positions.
661 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
663 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
665 // If not currently selecting, start selecting
666 if (m_selectionRange
.GetStart() == -2)
668 m_selectionAnchor
= oldPos
;
671 m_selectionRange
.SetRange(newPos
+1, oldPos
);
673 m_selectionRange
.SetRange(oldPos
+1, newPos
);
677 // Always ensure that the selection range start is greater than
679 if (newPos
> m_selectionAnchor
)
680 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
682 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
685 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
687 wxLogDebug(wxT("Strange selection range"));
696 /// Scroll into view, returning true if we scrolled.
697 /// This takes a _caret_ position.
698 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
700 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
706 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
708 int startXUnits
, startYUnits
;
709 GetViewStart(& startXUnits
, & startYUnits
);
710 int startY
= startYUnits
* ppuY
;
713 GetVirtualSize(& sx
, & sy
);
719 wxRect rect
= line
->GetRect();
721 bool scrolled
= false;
723 wxSize clientSize
= GetClientSize();
726 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
728 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
730 // Make it scroll so this item is at the bottom
732 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
733 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
735 // If we're still off the screen, scroll another line down
736 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
739 if (startYUnits
!= yUnits
)
741 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
745 else if (rect
.y
< startY
)
747 // Make it scroll so this item is at the top
750 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
752 if (startYUnits
!= yUnits
)
754 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
760 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
764 // Make it scroll so this item is at the top
767 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
769 if (startYUnits
!= yUnits
)
771 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
775 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
777 // Make it scroll so this item is at the bottom
779 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
780 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
782 // If we're still off the screen, scroll another line down
783 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
786 if (startYUnits
!= yUnits
)
788 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
798 /// Is the given position visible on the screen?
799 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
801 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
807 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
810 GetViewStart(& startX
, & startY
);
812 startY
= startY
* ppuY
;
815 GetVirtualSize(& sx
, & sy
);
820 wxRect rect
= line
->GetRect();
822 wxSize clientSize
= GetClientSize();
824 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
827 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
829 m_caretPosition
= position
;
830 m_caretAtLineStart
= showAtLineStart
;
833 /// Move caret one visual step forward: this may mean setting a flag
834 /// and keeping the same position if we're going from the end of one line
835 /// to the start of the next, which may be the exact same caret position.
836 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
838 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
840 // Only do the check if we're not at the end of the paragraph (where things work OK
842 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
844 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
848 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
850 // We're at the end of a line. See whether we need to
851 // stay at the same actual caret position but change visual
853 if (oldPosition
== lineRange
.GetEnd())
855 if (m_caretAtLineStart
)
857 // We're already at the start of the line, so actually move on now.
858 m_caretPosition
= oldPosition
+ 1;
859 m_caretAtLineStart
= false;
863 // We're showing at the end of the line, so keep to
864 // the same position but indicate that we're to show
865 // at the start of the next line.
866 m_caretPosition
= oldPosition
;
867 m_caretAtLineStart
= true;
869 SetDefaultStyleToCursorStyle();
875 SetDefaultStyleToCursorStyle();
878 /// Move caret one visual step backward: this may mean setting a flag
879 /// and keeping the same position if we're going from the end of one line
880 /// to the start of the next, which may be the exact same caret position.
881 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
883 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
885 // Only do the check if we're not at the start of the paragraph (where things work OK
887 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
889 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
893 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
895 // We're at the start of a line. See whether we need to
896 // stay at the same actual caret position but change visual
898 if (oldPosition
== lineRange
.GetStart())
900 m_caretPosition
= oldPosition
-1;
901 m_caretAtLineStart
= true;
904 else if (oldPosition
== lineRange
.GetEnd())
906 if (m_caretAtLineStart
)
908 // We're at the start of the line, so keep the same caret position
909 // but clear the start-of-line flag.
910 m_caretPosition
= oldPosition
;
911 m_caretAtLineStart
= false;
915 // We're showing at the end of the line, so go back
916 // to the previous character position.
917 m_caretPosition
= oldPosition
- 1;
919 SetDefaultStyleToCursorStyle();
925 SetDefaultStyleToCursorStyle();
929 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
931 long endPos
= GetBuffer().GetRange().GetEnd();
933 if (m_caretPosition
+ noPositions
< endPos
)
935 long oldPos
= m_caretPosition
;
936 long newPos
= m_caretPosition
+ noPositions
;
938 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
942 // Determine by looking at oldPos and m_caretPosition whether
943 // we moved from the end of a line to the start of the next line, in which case
944 // we want to adjust the caret position such that it is positioned at the
945 // start of the next line, rather than jumping past the first character of the
947 if (noPositions
== 1 && !extendSel
)
948 MoveCaretForward(oldPos
);
950 SetCaretPosition(newPos
);
953 SetDefaultStyleToCursorStyle();
964 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
968 if (m_caretPosition
> startPos
- noPositions
+ 1)
970 long oldPos
= m_caretPosition
;
971 long newPos
= m_caretPosition
- noPositions
;
972 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
976 if (noPositions
== 1 && !extendSel
)
977 MoveCaretBack(oldPos
);
979 SetCaretPosition(newPos
);
982 SetDefaultStyleToCursorStyle();
993 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
995 return MoveDown(- noLines
, flags
);
999 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1004 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1005 wxPoint pt
= GetCaret()->GetPosition();
1006 long newLine
= lineNumber
+ noLines
;
1008 if (lineNumber
!= -1)
1012 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1014 if (newLine
> lastLine
)
1024 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1027 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1033 wxClientDC
dc(this);
1035 dc
.SetFont(GetFont());
1037 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1039 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1041 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1042 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1043 // so we view the caret at the start of the line.
1044 bool caretLineStart
= false;
1045 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1047 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1048 wxRichTextRange lineRange
;
1050 lineRange
= thisLine
->GetAbsoluteRange();
1052 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1055 caretLineStart
= true;
1059 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1060 if (para
&& para
->GetRange().GetStart() == newPos
)
1065 long newSelEnd
= newPos
;
1067 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1071 SetCaretPosition(newPos
, caretLineStart
);
1073 SetDefaultStyleToCursorStyle();
1083 /// Move to the end of the paragraph
1084 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1086 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1089 long newPos
= para
->GetRange().GetEnd() - 1;
1090 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1094 SetCaretPosition(newPos
);
1096 SetDefaultStyleToCursorStyle();
1106 /// Move to the start of the paragraph
1107 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1109 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1112 long newPos
= para
->GetRange().GetStart() - 1;
1113 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1117 SetCaretPosition(newPos
);
1119 SetDefaultStyleToCursorStyle();
1129 /// Move to the end of the line
1130 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1132 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1136 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1137 long newPos
= lineRange
.GetEnd();
1138 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1142 SetCaretPosition(newPos
);
1144 SetDefaultStyleToCursorStyle();
1154 /// Move to the start of the line
1155 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1157 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1160 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1161 long newPos
= lineRange
.GetStart()-1;
1163 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1167 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1169 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1171 SetDefaultStyleToCursorStyle();
1181 /// Move to the start of the buffer
1182 bool wxRichTextCtrl::MoveHome(int flags
)
1184 if (m_caretPosition
!= -1)
1186 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1190 SetCaretPosition(-1);
1192 SetDefaultStyleToCursorStyle();
1202 /// Move to the end of the buffer
1203 bool wxRichTextCtrl::MoveEnd(int flags
)
1205 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1207 if (m_caretPosition
!= endPos
)
1209 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1213 SetCaretPosition(endPos
);
1215 SetDefaultStyleToCursorStyle();
1225 /// Move noPages pages up
1226 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1228 return PageDown(- noPages
, flags
);
1231 /// Move noPages pages down
1232 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1234 // Calculate which line occurs noPages * screen height further down.
1235 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1238 wxSize clientSize
= GetClientSize();
1239 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1241 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1244 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1245 long pos
= lineRange
.GetStart()-1;
1246 if (pos
!= m_caretPosition
)
1248 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1250 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1254 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1256 SetDefaultStyleToCursorStyle();
1268 // Finds the caret position for the next word
1269 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1271 long endPos
= GetBuffer().GetRange().GetEnd();
1275 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1277 // First skip current text to space
1278 while (i
< endPos
&& i
> -1)
1280 // i is in character, not caret positions
1281 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1282 if (text
!= wxT(" ") && !text
.empty())
1289 while (i
< endPos
&& i
> -1)
1291 // i is in character, not caret positions
1292 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1293 if (text
.empty()) // End of paragraph, or maybe an image
1294 return wxMax(-1, i
- 1);
1295 else if (text
== wxT(" ") || text
.empty())
1299 // Convert to caret position
1300 return wxMax(-1, i
- 1);
1309 long i
= m_caretPosition
;
1311 // First skip white space
1312 while (i
< endPos
&& i
> -1)
1314 // i is in character, not caret positions
1315 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1316 if (text
.empty()) // End of paragraph, or maybe an image
1318 else if (text
== wxT(" ") || text
.empty())
1323 // Next skip current text to 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
!= wxT(" ") /* && !text.empty() */)
1341 /// Move n words left
1342 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1344 long pos
= FindNextWordPosition(-1);
1345 if (pos
!= m_caretPosition
)
1347 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1349 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1353 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1355 SetDefaultStyleToCursorStyle();
1365 /// Move n words right
1366 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1368 long pos
= FindNextWordPosition(1);
1369 if (pos
!= m_caretPosition
)
1371 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1373 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1377 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1379 SetDefaultStyleToCursorStyle();
1390 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1392 // Only do sizing optimization for large buffers
1393 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1395 m_fullLayoutRequired
= true;
1396 m_fullLayoutTime
= wxGetLocalTimeMillis();
1397 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1398 LayoutContent(true /* onlyVisibleRect */);
1401 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1409 /// Idle-time processing
1410 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1412 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1414 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1416 m_fullLayoutRequired
= false;
1417 m_fullLayoutTime
= 0;
1418 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1419 ShowPosition(m_fullLayoutSavedPosition
);
1423 if (m_caretPositionForDefaultStyle
!= -2)
1425 // If the caret position has changed, no longer reflect the default style
1427 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1428 m_caretPositionForDefaultStyle
= -2;
1435 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1441 /// Set up scrollbars, e.g. after a resize
1442 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1447 if (GetBuffer().IsEmpty())
1449 SetScrollbars(0, 0, 0, 0, 0, 0);
1453 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1454 // of pixels. See e.g. wxVScrolledWindow for ideas.
1455 int pixelsPerUnit
= 5;
1456 wxSize clientSize
= GetClientSize();
1458 int maxHeight
= GetBuffer().GetCachedSize().y
;
1460 // Round up so we have at least maxHeight pixels
1461 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1463 int startX
= 0, startY
= 0;
1465 GetViewStart(& startX
, & startY
);
1467 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1468 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1470 // Move to previous scroll position if
1472 SetScrollbars(0, pixelsPerUnit
,
1474 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1477 /// Paint the background
1478 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1480 wxColour backgroundColour
= GetBackgroundColour();
1481 if (!backgroundColour
.Ok())
1482 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1484 // Clear the background
1485 dc
.SetBrush(wxBrush(backgroundColour
));
1486 dc
.SetPen(*wxTRANSPARENT_PEN
);
1487 wxRect
windowRect(GetClientSize());
1488 windowRect
.x
-= 2; windowRect
.y
-= 2;
1489 windowRect
.width
+= 4; windowRect
.height
+= 4;
1491 // We need to shift the rectangle to take into account
1492 // scrolling. Converting device to logical coordinates.
1493 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1494 dc
.DrawRectangle(windowRect
);
1497 /// Recreate buffer bitmap if necessary
1498 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1501 if (sz
== wxDefaultSize
)
1502 sz
= GetClientSize();
1504 if (sz
.x
< 1 || sz
.y
< 1)
1507 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1508 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1509 return m_bufferBitmap
.Ok();
1512 // ----------------------------------------------------------------------------
1513 // file IO functions
1514 // ----------------------------------------------------------------------------
1516 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1517 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int fileType
)
1519 return DoLoadFile(filename
, fileType
);
1522 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int fileType
)
1524 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1525 if ( filenameToUse
.empty() )
1527 // what kind of message to give? is it an error or a program bug?
1528 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1533 return DoSaveFile(filenameToUse
, fileType
);
1537 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1539 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1541 m_filename
= filename
;
1544 SetInsertionPoint(0);
1547 SetupScrollbars(true);
1555 wxLogError(_("File couldn't be loaded."));
1561 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1563 if (GetBuffer().SaveFile(filename
, fileType
))
1565 m_filename
= filename
;
1572 wxLogError(_("The text couldn't be saved."));
1577 // ----------------------------------------------------------------------------
1578 // wxRichTextCtrl specific functionality
1579 // ----------------------------------------------------------------------------
1581 /// Add a new paragraph of text to the end of the buffer
1582 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1584 return GetBuffer().AddParagraph(text
);
1588 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1590 return GetBuffer().AddImage(image
);
1593 // ----------------------------------------------------------------------------
1594 // selection and ranges
1595 // ----------------------------------------------------------------------------
1597 void wxRichTextCtrl::SelectAll()
1599 SetSelection(0, GetLastPosition()+1);
1600 m_selectionAnchor
= -1;
1604 void wxRichTextCtrl::SelectNone()
1606 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1607 SetSelection(-2, -2);
1608 m_selectionAnchor
= -2;
1611 static bool wxIsWordDelimiter(const wxString
& text
)
1613 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1616 /// Select the word at the given character position
1617 bool wxRichTextCtrl::SelectWord(long position
)
1619 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1622 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1626 long positionStart
= position
;
1627 long positionEnd
= position
;
1629 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1631 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1632 if (wxIsWordDelimiter(text
))
1638 if (positionStart
< para
->GetRange().GetStart())
1639 positionStart
= para
->GetRange().GetStart();
1641 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1643 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1644 if (wxIsWordDelimiter(text
))
1650 if (positionEnd
>= para
->GetRange().GetEnd())
1651 positionEnd
= para
->GetRange().GetEnd();
1653 SetSelection(positionStart
, positionEnd
+1);
1655 if (positionStart
>= 0)
1657 MoveCaret(positionStart
-1, true);
1658 SetDefaultStyleToCursorStyle();
1664 wxString
wxRichTextCtrl::GetStringSelection() const
1667 GetSelection(&from
, &to
);
1669 return GetRange(from
, to
);
1672 // do the window-specific processing after processing the update event
1673 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1674 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1677 wxWindowBase::DoUpdateWindowUI(event
);
1680 if ( event
.GetSetText() )
1682 if ( event
.GetText() != GetValue() )
1683 SetValue(event
.GetText());
1686 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1688 // ----------------------------------------------------------------------------
1690 // ----------------------------------------------------------------------------
1692 wxTextCtrlHitTestResult
1693 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1695 // implement in terms of the other overload as the native ports typically
1696 // can get the position and not (x, y) pair directly (although wxUniv
1697 // directly gets x and y -- and so overrides this method as well)
1699 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1701 if ( rc
!= wxTE_HT_UNKNOWN
)
1703 PositionToXY(pos
, x
, y
);
1709 wxTextCtrlHitTestResult
1710 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1713 wxClientDC
dc((wxRichTextCtrl
*) this);
1714 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1716 // Buffer uses logical position (relative to start of buffer)
1718 wxPoint pt2
= GetLogicalPoint(pt
);
1720 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1724 case wxRICHTEXT_HITTEST_BEFORE
:
1725 return wxTE_HT_BEFORE
;
1727 case wxRICHTEXT_HITTEST_AFTER
:
1728 return wxTE_HT_BEYOND
;
1730 case wxRICHTEXT_HITTEST_ON
:
1731 return wxTE_HT_ON_TEXT
;
1734 return wxTE_HT_UNKNOWN
;
1737 // ----------------------------------------------------------------------------
1738 // set/get the controls text
1739 // ----------------------------------------------------------------------------
1741 wxString
wxRichTextCtrl::GetValue() const
1743 return GetBuffer().GetText();
1746 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1748 // Public API for range is different from internals
1749 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1752 void wxRichTextCtrl::SetValue(const wxString
& value
)
1756 // if the text is long enough, it's faster to just set it instead of first
1757 // comparing it with the old one (chances are that it will be different
1758 // anyhow, this comparison is there to avoid flicker for small single-line
1759 // edit controls mostly)
1760 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1762 DoWriteText(value
, false /* not selection only */);
1764 // for compatibility, don't move the cursor when doing SetValue()
1765 SetInsertionPoint(0);
1769 // still send an event for consistency
1773 // we should reset the modified flag even if the value didn't really change
1775 // mark the control as being not dirty - we changed its text, not the
1780 void wxRichTextCtrl::WriteText(const wxString
& value
)
1785 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1787 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1789 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1792 void wxRichTextCtrl::AppendText(const wxString
& text
)
1794 SetInsertionPointEnd();
1799 /// Write an image at the current insertion point
1800 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1802 wxRichTextImageBlock imageBlock
;
1804 wxImage image2
= image
;
1805 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1806 return WriteImage(imageBlock
);
1811 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1813 wxRichTextImageBlock imageBlock
;
1816 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1817 return WriteImage(imageBlock
);
1822 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1824 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1827 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1831 wxRichTextImageBlock imageBlock
;
1833 wxImage image
= bitmap
.ConvertToImage();
1834 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1835 return WriteImage(imageBlock
);
1841 /// Insert a newline (actually paragraph) at the current insertion point.
1842 bool wxRichTextCtrl::Newline()
1844 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1848 // ----------------------------------------------------------------------------
1849 // Clipboard operations
1850 // ----------------------------------------------------------------------------
1852 void wxRichTextCtrl::Copy()
1856 wxRichTextRange range
= GetInternalSelectionRange();
1857 GetBuffer().CopyToClipboard(range
);
1861 void wxRichTextCtrl::Cut()
1865 wxRichTextRange range
= GetInternalSelectionRange();
1866 GetBuffer().CopyToClipboard(range
);
1868 DeleteSelectedContent();
1874 void wxRichTextCtrl::Paste()
1878 BeginBatchUndo(_("Paste"));
1880 long newPos
= m_caretPosition
;
1881 DeleteSelectedContent(& newPos
);
1883 GetBuffer().PasteFromClipboard(newPos
);
1889 void wxRichTextCtrl::DeleteSelection()
1891 if (CanDeleteSelection())
1893 DeleteSelectedContent();
1897 bool wxRichTextCtrl::HasSelection() const
1899 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1902 bool wxRichTextCtrl::CanCopy() const
1904 // Can copy if there's a selection
1905 return HasSelection();
1908 bool wxRichTextCtrl::CanCut() const
1910 return HasSelection() && IsEditable();
1913 bool wxRichTextCtrl::CanPaste() const
1915 if ( !IsEditable() )
1918 return GetBuffer().CanPasteFromClipboard();
1921 bool wxRichTextCtrl::CanDeleteSelection() const
1923 return HasSelection() && IsEditable();
1927 // ----------------------------------------------------------------------------
1929 // ----------------------------------------------------------------------------
1931 void wxRichTextCtrl::SetEditable(bool editable
)
1933 m_editable
= editable
;
1936 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1940 m_caretPosition
= pos
- 1;
1943 void wxRichTextCtrl::SetInsertionPointEnd()
1945 long pos
= GetLastPosition();
1946 SetInsertionPoint(pos
);
1949 long wxRichTextCtrl::GetInsertionPoint() const
1951 return m_caretPosition
+1;
1954 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1956 return GetBuffer().GetRange().GetEnd();
1959 // If the return values from and to are the same, there is no
1961 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1963 *from
= m_selectionRange
.GetStart();
1964 *to
= m_selectionRange
.GetEnd();
1965 if ((*to
) != -1 && (*to
) != -2)
1969 bool wxRichTextCtrl::IsEditable() const
1974 // ----------------------------------------------------------------------------
1976 // ----------------------------------------------------------------------------
1978 void wxRichTextCtrl::SetSelection(long from
, long to
)
1980 // if from and to are both -1, it means (in wxWidgets) that all text should
1982 if ( (from
== -1) && (to
== -1) )
1985 to
= GetLastPosition()+1;
1988 DoSetSelection(from
, to
);
1991 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1993 m_selectionAnchor
= from
;
1994 m_selectionRange
.SetRange(from
, to
-1);
2000 // ----------------------------------------------------------------------------
2002 // ----------------------------------------------------------------------------
2004 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
2006 BeginBatchUndo(_("Replace"));
2008 DeleteSelectedContent();
2010 DoWriteText(value
, true /* selection only */);
2015 void wxRichTextCtrl::Remove(long from
, long to
)
2019 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2020 m_caretPosition
, // Current caret position
2021 from
, // New caret position
2029 bool wxRichTextCtrl::IsModified() const
2031 return m_buffer
.IsModified();
2034 void wxRichTextCtrl::MarkDirty()
2036 m_buffer
.Modify(true);
2039 void wxRichTextCtrl::DiscardEdits()
2041 m_caretPositionForDefaultStyle
= -2;
2042 m_buffer
.Modify(false);
2043 m_buffer
.GetCommandProcessor()->ClearCommands();
2046 int wxRichTextCtrl::GetNumberOfLines() const
2048 return GetBuffer().GetParagraphCount();
2051 // ----------------------------------------------------------------------------
2052 // Positions <-> coords
2053 // ----------------------------------------------------------------------------
2055 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2057 return GetBuffer().XYToPosition(x
, y
);
2060 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2062 return GetBuffer().PositionToXY(pos
, x
, y
);
2065 // ----------------------------------------------------------------------------
2067 // ----------------------------------------------------------------------------
2069 void wxRichTextCtrl::ShowPosition(long pos
)
2071 if (!IsPositionVisible(pos
))
2072 ScrollIntoView(pos
-1, WXK_DOWN
);
2075 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2077 return GetBuffer().GetParagraphLength(lineNo
);
2080 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2082 return GetBuffer().GetParagraphText(lineNo
);
2085 // ----------------------------------------------------------------------------
2087 // ----------------------------------------------------------------------------
2089 void wxRichTextCtrl::Undo()
2093 GetCommandProcessor()->Undo();
2097 void wxRichTextCtrl::Redo()
2101 GetCommandProcessor()->Redo();
2105 bool wxRichTextCtrl::CanUndo() const
2107 return GetCommandProcessor()->CanUndo();
2110 bool wxRichTextCtrl::CanRedo() const
2112 return GetCommandProcessor()->CanRedo();
2115 // ----------------------------------------------------------------------------
2116 // implementation details
2117 // ----------------------------------------------------------------------------
2119 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2121 SetValue(event
.GetString());
2122 GetEventHandler()->ProcessEvent(event
);
2125 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2127 // By default, load the first file into the text window.
2128 if (event
.GetNumberOfFiles() > 0)
2130 LoadFile(event
.GetFiles()[0]);
2134 // ----------------------------------------------------------------------------
2135 // text control event processing
2136 // ----------------------------------------------------------------------------
2138 bool wxRichTextCtrl::SendUpdateEvent()
2140 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2141 InitCommandEvent(event
);
2143 return GetEventHandler()->ProcessEvent(event
);
2146 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2148 event
.SetEventObject((wxControlBase
*)this); // const_cast
2150 switch ( m_clientDataType
)
2152 case wxClientData_Void
:
2153 event
.SetClientData(GetClientData());
2156 case wxClientData_Object
:
2157 event
.SetClientObject(GetClientObject());
2160 case wxClientData_None
:
2167 wxSize
wxRichTextCtrl::DoGetBestSize() const
2169 return wxSize(10, 10);
2172 // ----------------------------------------------------------------------------
2173 // standard handlers for standard edit menu events
2174 // ----------------------------------------------------------------------------
2176 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2181 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2186 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2191 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2196 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2201 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2206 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2208 event
.Enable( CanCut() );
2211 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2213 event
.Enable( CanCopy() );
2216 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2218 event
.Enable( CanDeleteSelection() );
2221 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2223 event
.Enable( CanPaste() );
2226 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2228 event
.Enable( CanUndo() );
2229 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2232 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2234 event
.Enable( CanRedo() );
2235 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2238 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2243 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2245 event
.Enable(GetLastPosition() > 0);
2248 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2252 m_contextMenu
= new wxMenu
;
2253 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2254 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2255 m_contextMenu
->AppendSeparator();
2256 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2257 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2258 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2259 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2260 m_contextMenu
->AppendSeparator();
2261 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2263 PopupMenu(m_contextMenu
);
2267 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2269 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2272 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2274 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2277 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2279 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2282 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2284 return GetBuffer().SetDefaultStyle(style
);
2287 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2289 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2292 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2294 return GetBuffer().GetDefaultStyle();
2297 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2299 return GetBuffer().GetDefaultStyle();
2302 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2304 wxTextAttrEx
attr(style
);
2305 if (GetBuffer().GetStyle(position
, attr
))
2314 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2316 return GetBuffer().GetStyle(position
, style
);
2319 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2321 return GetBuffer().GetStyle(position
, style
);
2324 /// Get the content (uncombined) attributes for this position.
2326 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2328 wxTextAttrEx
attr(style
);
2329 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2338 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2340 return GetBuffer().GetUncombinedStyle(position
, style
);
2343 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2345 return GetBuffer().GetUncombinedStyle(position
, style
);
2348 /// Set font, and also the buffer attributes
2349 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2351 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2352 wxControl::SetFont(font
);
2354 wxScrolledWindow::SetFont(font
);
2357 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2359 GetBuffer().SetBasicStyle(attr
);
2361 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2362 // Don't set the default style, since it will be inherited from
2364 GetBuffer().SetDefaultStyle(attr
);
2367 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2373 /// Transform logical to physical
2374 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2377 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2382 /// Transform physical to logical
2383 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2386 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2391 /// Position the caret
2392 void wxRichTextCtrl::PositionCaret()
2397 //wxLogDebug(wxT("PositionCaret"));
2400 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2402 wxPoint originalPt
= caretRect
.GetPosition();
2403 wxPoint pt
= GetPhysicalPoint(originalPt
);
2404 if (GetCaret()->GetPosition() != pt
)
2406 GetCaret()->Move(pt
);
2407 GetCaret()->SetSize(caretRect
.GetSize());
2412 /// Get the caret height and position for the given character position
2413 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2415 wxClientDC
dc(this);
2416 dc
.SetFont(GetFont());
2423 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2425 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2432 /// Gets the line for the visible caret position. If the caret is
2433 /// shown at the very end of the line, it means the next character is actually
2434 /// on the following line. So let's get the line we're expecting to find
2435 /// if this is the case.
2436 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2438 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2439 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2442 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2443 if (caretPosition
== lineRange
.GetStart()-1 &&
2444 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2446 if (!m_caretAtLineStart
)
2447 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2454 /// Move the caret to the given character position
2455 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2457 if (GetBuffer().GetDirty())
2460 if (pos
<= GetBuffer().GetRange().GetEnd())
2462 SetCaretPosition(pos
, showAtLineStart
);
2472 /// Layout the buffer: which we must do before certain operations, such as
2473 /// setting the caret position.
2474 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2476 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2478 wxRect
availableSpace(GetClientSize());
2479 if (availableSpace
.width
== 0)
2480 availableSpace
.width
= 10;
2481 if (availableSpace
.height
== 0)
2482 availableSpace
.height
= 10;
2484 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2485 if (onlyVisibleRect
)
2487 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2488 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2491 wxClientDC
dc(this);
2492 dc
.SetFont(GetFont());
2496 GetBuffer().Defragment();
2497 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2498 GetBuffer().Layout(dc
, availableSpace
, flags
);
2499 GetBuffer().SetDirty(false);
2508 /// Is all of the selection bold?
2509 bool wxRichTextCtrl::IsSelectionBold()
2513 wxRichTextAttr attr
;
2514 wxRichTextRange range
= GetInternalSelectionRange();
2515 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2516 attr
.SetFontWeight(wxBOLD
);
2518 return HasCharacterAttributes(range
, attr
);
2522 // If no selection, then we need to combine current style with default style
2523 // to see what the effect would be if we started typing.
2524 wxRichTextAttr attr
;
2525 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2527 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2528 if (GetStyle(pos
, attr
))
2530 if (IsDefaultStyleShowing())
2531 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2532 return attr
.GetFontWeight() == wxBOLD
;
2538 /// Is all of the selection italics?
2539 bool wxRichTextCtrl::IsSelectionItalics()
2543 wxRichTextRange range
= GetInternalSelectionRange();
2544 wxRichTextAttr attr
;
2545 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2546 attr
.SetFontStyle(wxITALIC
);
2548 return HasCharacterAttributes(range
, attr
);
2552 // If no selection, then we need to combine current style with default style
2553 // to see what the effect would be if we started typing.
2554 wxRichTextAttr attr
;
2555 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2557 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2558 if (GetStyle(pos
, attr
))
2560 if (IsDefaultStyleShowing())
2561 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2562 return attr
.GetFontStyle() == wxITALIC
;
2568 /// Is all of the selection underlined?
2569 bool wxRichTextCtrl::IsSelectionUnderlined()
2573 wxRichTextRange range
= GetInternalSelectionRange();
2574 wxRichTextAttr attr
;
2575 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2576 attr
.SetFontUnderlined(true);
2578 return HasCharacterAttributes(range
, attr
);
2582 // If no selection, then we need to combine current style with default style
2583 // to see what the effect would be if we started typing.
2584 wxRichTextAttr attr
;
2585 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2586 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2588 if (GetStyle(pos
, attr
))
2590 if (IsDefaultStyleShowing())
2591 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2592 return attr
.GetFontUnderlined();
2598 /// Apply bold to the selection
2599 bool wxRichTextCtrl::ApplyBoldToSelection()
2601 wxRichTextAttr attr
;
2602 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2603 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2606 return SetStyle(GetSelectionRange(), attr
);
2608 SetAndShowDefaultStyle(attr
);
2612 /// Apply italic to the selection
2613 bool wxRichTextCtrl::ApplyItalicToSelection()
2615 wxRichTextAttr attr
;
2616 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2617 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2620 return SetStyle(GetSelectionRange(), attr
);
2622 SetAndShowDefaultStyle(attr
);
2626 /// Apply underline to the selection
2627 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2629 wxRichTextAttr attr
;
2630 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2631 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2634 return SetStyle(GetSelectionRange(), attr
);
2636 SetAndShowDefaultStyle(attr
);
2640 /// Is all of the selection aligned according to the specified flag?
2641 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2643 wxRichTextRange range
;
2645 range
= GetInternalSelectionRange();
2647 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2649 wxRichTextAttr attr
;
2650 attr
.SetAlignment(alignment
);
2652 return HasParagraphAttributes(range
, attr
);
2655 /// Apply alignment to the selection
2656 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2658 wxRichTextAttr attr
;
2659 attr
.SetAlignment(alignment
);
2661 return SetStyle(GetSelectionRange(), attr
);
2664 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2666 return SetStyle(para
->GetRange().FromInternal(), attr
);
2671 /// Apply a named style to the selection
2672 void wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2674 // Flags are defined within each definition, so only certain
2675 // attributes are applied.
2676 wxRichTextAttr
attr(def
->GetStyle());
2678 // Make sure the attr has the style name
2679 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2680 attr
.SetParagraphStyleName(def
->GetName());
2682 attr
.SetCharacterStyleName(def
->GetName());
2685 SetStyle(GetSelectionRange(), attr
);
2687 SetAndShowDefaultStyle(attr
);
2690 /// Apply the style sheet to the buffer, for example if the styles have changed.
2691 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2694 styleSheet
= GetBuffer().GetStyleSheet();
2698 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2700 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2708 /// Sets the default style to the style under the cursor
2709 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2712 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2714 // If at the start of a paragraph, use the next position.
2715 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2717 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2718 if (GetUncombinedStyle(pos
, attr
))
2720 if (GetStyle(pos
, attr
))
2723 SetDefaultStyle(attr
);
2730 /// Returns the first visible position in the current view
2731 long wxRichTextCtrl::GetFirstVisiblePosition() const
2733 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2735 return line
->GetAbsoluteRange().GetStart();
2740 /// The adjusted caret position is the character position adjusted to take
2741 /// into account whether we're at the start of a paragraph, in which case
2742 /// style information should be taken from the next position, not current one.
2743 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2745 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2747 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2752 /// Get/set the selection range in character positions. -1, -1 means no selection.
2753 /// The range is in API convention, i.e. a single character selection is denoted
2755 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2757 wxRichTextRange range
= GetInternalSelectionRange();
2758 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2759 range
.SetEnd(range
.GetEnd() + 1);
2763 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2765 wxRichTextRange
range1(range
);
2766 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2767 range1
.SetEnd(range1
.GetEnd() - 1);
2769 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2771 SetInternalSelectionRange(range1
);