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"
25 #include "wx/settings.h"
28 #include "wx/textfile.h"
30 #include "wx/filename.h"
31 #include "wx/dcbuffer.h"
32 #include "wx/arrimpl.cpp"
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
42 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
43 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
45 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
48 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
50 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
51 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
53 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
55 EVT_PAINT(wxRichTextCtrl::OnPaint
)
56 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
57 EVT_IDLE(wxRichTextCtrl::OnIdle
)
58 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
59 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
60 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
61 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
62 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
63 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
64 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
65 EVT_CHAR(wxRichTextCtrl::OnChar
)
66 EVT_SIZE(wxRichTextCtrl::OnSize
)
67 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
68 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
69 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
71 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
72 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
74 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
75 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
77 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
78 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
80 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
81 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
83 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
84 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
86 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
87 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
89 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
90 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
97 wxRichTextCtrl::wxRichTextCtrl()
98 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
99 : wxScrollHelper(this)
105 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
106 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
107 : wxScrollHelper(this)
111 Create(parent
, id
, pos
, size
, style
);
115 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
117 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
118 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
122 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
129 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
132 GetBuffer().SetRichTextCtrl(this);
134 wxTextAttrEx attributes
;
135 attributes
.SetFont(GetFont());
136 attributes
.SetTextColour(*wxBLACK
);
137 attributes
.SetBackgroundColour(*wxWHITE
);
138 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
139 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
141 SetDefaultStyle(attributes
);
142 SetBasicStyle(attributes
);
144 SetBackgroundColour(*wxWHITE
);
145 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
147 // Tell the sizers to use the given or best size
148 SetBestFittingSize(size
);
151 RecreateBuffer(size
);
153 SetCursor(wxCursor(wxCURSOR_IBEAM
));
158 wxRichTextCtrl::~wxRichTextCtrl()
160 delete m_contextMenu
;
163 /// Member initialisation
164 void wxRichTextCtrl::Init()
167 m_contextMenu
= NULL
;
169 m_caretPosition
= -1;
170 m_selectionRange
.SetRange(-2, -2);
171 m_selectionAnchor
= -2;
173 m_caretAtLineStart
= false;
175 m_fullLayoutRequired
= false;
176 m_fullLayoutTime
= 0;
177 m_fullLayoutSavedPosition
= 0;
178 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
181 /// Call Freeze to prevent refresh
182 void wxRichTextCtrl::Freeze()
187 /// Call Thaw to refresh
188 void wxRichTextCtrl::Thaw()
192 if (m_freezeCount
== 0)
200 void wxRichTextCtrl::Clear()
203 m_buffer
.SetDirty(true);
204 m_caretPosition
= -1;
205 m_caretAtLineStart
= false;
206 m_selectionRange
.SetRange(-2, -2);
208 if (m_freezeCount
== 0)
217 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
223 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
224 //wxLogDebug(wxT("OnPaint"));
228 if (m_freezeCount
> 0)
231 dc
.SetFont(GetFont());
233 // Paint the background
236 wxRegion dirtyRegion
= GetUpdateRegion();
238 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
239 wxRect
availableSpace(GetClientSize());
240 if (GetBuffer().GetDirty())
242 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
243 GetBuffer().SetDirty(false);
247 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
256 // Empty implementation, to prevent flicker
257 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
261 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
263 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
272 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
281 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
287 dc
.SetFont(GetFont());
290 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
292 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
294 m_dragStart
= event
.GetLogicalPosition(dc
);
300 bool caretAtLineStart
= false;
302 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
304 // If we're at the start of a line (but not first in para)
305 // then we should keep the caret showing at the start of the line
306 // by showing the m_caretAtLineStart flag.
307 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
308 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
310 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
311 caretAtLineStart
= true;
315 MoveCaret(position
, caretAtLineStart
);
316 SetDefaultStyleToCursorStyle();
319 wxWindow
* p
= GetParent();
320 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
323 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
326 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
327 frame
->SetStatusText(msg
, 1);
336 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
341 if (GetCapture() == this)
347 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
349 if (!event
.Dragging())
357 dc
.SetFont(GetFont());
360 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
361 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
363 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
365 // TODO: test closeness
367 bool caretAtLineStart
= false;
369 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
371 // If we're at the start of a line (but not first in para)
372 // then we should keep the caret showing at the start of the line
373 // by showing the m_caretAtLineStart flag.
374 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
375 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
377 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
378 caretAtLineStart
= true;
382 if (m_caretPosition
!= position
)
384 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
386 MoveCaret(position
, caretAtLineStart
);
387 SetDefaultStyleToCursorStyle();
396 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
402 /// Left-double-click
403 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
409 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
415 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
418 if (event
.ControlDown())
419 flags
|= wxRICHTEXT_CTRL_DOWN
;
420 if (event
.ShiftDown())
421 flags
|= wxRICHTEXT_SHIFT_DOWN
;
423 flags
|= wxRICHTEXT_ALT_DOWN
;
425 if (event
.GetKeyCode() == WXK_LEFT
||
426 event
.GetKeyCode() == WXK_RIGHT
||
427 event
.GetKeyCode() == WXK_UP
||
428 event
.GetKeyCode() == WXK_DOWN
||
429 event
.GetKeyCode() == WXK_HOME
||
430 event
.GetKeyCode() == WXK_PAGEUP
||
431 event
.GetKeyCode() == WXK_PAGEDOWN
||
432 event
.GetKeyCode() == WXK_END
)
434 KeyboardNavigate(event
.GetKeyCode(), flags
);
438 // all the other keys modify the controls contents which shouldn't be
439 // possible if we're read-only
446 if (event
.GetKeyCode() == WXK_RETURN
)
448 BeginBatchUndo(_("Insert Text"));
450 long newPos
= m_caretPosition
;
452 DeleteSelectedContent(& newPos
);
454 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
456 wxRichTextEvent
cmdEvent(
457 wxEVT_COMMAND_RICHTEXT_RETURN
,
459 cmdEvent
.SetEventObject(this);
460 cmdEvent
.SetFlags(flags
);
461 GetEventHandler()->ProcessEvent(cmdEvent
);
464 SetDefaultStyleToCursorStyle();
466 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
468 else if (event
.GetKeyCode() == WXK_BACK
)
470 BeginBatchUndo(_("Delete Text"));
472 // Submit range in character positions, which are greater than caret positions,
473 // so subtract 1 for deleted character and add 1 for conversion to character position.
474 if (m_caretPosition
> -1 && !HasSelection())
476 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
477 m_caretPosition
, // Current caret position
478 m_caretPosition
-1, // New caret position
482 DeleteSelectedContent();
486 // Shouldn't this be in Do()?
487 if (GetLastPosition() == -1)
491 m_caretPosition
= -1;
493 SetDefaultStyleToCursorStyle();
496 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
498 else if (event
.GetKeyCode() == WXK_DELETE
)
500 BeginBatchUndo(_("Delete Text"));
502 // Submit range in character positions, which are greater than caret positions,
503 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
505 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
506 m_caretPosition
, // Current caret position
507 m_caretPosition
+1, // New caret position
511 DeleteSelectedContent();
515 // Shouldn't this be in Do()?
516 if (GetLastPosition() == -1)
520 m_caretPosition
= -1;
522 SetDefaultStyleToCursorStyle();
527 BeginBatchUndo(_("Insert Text"));
529 long newPos
= m_caretPosition
;
530 DeleteSelectedContent(& newPos
);
532 wxString str
= (wxChar
) event
.GetKeyCode();
533 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
537 SetDefaultStyleToCursorStyle();
538 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
542 /// Delete content if there is a selection, e.g. when pressing a key.
543 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
547 long pos
= m_selectionRange
.GetStart();
548 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
549 m_caretPosition
, // Current caret position
550 pos
, // New caret position
552 m_selectionRange
.SetRange(-2, -2);
562 /// Keyboard navigation
566 Left: left one character
567 Right: right one character
570 Ctrl-Left: left one word
571 Ctrl-Right: right one word
572 Ctrl-Up: previous paragraph start
573 Ctrl-Down: next start of paragraph
576 Ctrl-Home: start of document
577 Ctrl-End: end of document
579 Page-Down: Down a screen
583 Ctrl-Alt-PgUp: Start of window
584 Ctrl-Alt-PgDn: End of window
585 F8: Start selection mode
586 Esc: End selection mode
588 Adding Shift does the above but starts/extends selection.
593 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
595 bool success
= false;
597 if (keyCode
== WXK_RIGHT
)
599 if (flags
& wxRICHTEXT_CTRL_DOWN
)
600 success
= WordRight(1, flags
);
602 success
= MoveRight(1, flags
);
604 else if (keyCode
== WXK_LEFT
)
606 if (flags
& wxRICHTEXT_CTRL_DOWN
)
607 success
= WordLeft(1, flags
);
609 success
= MoveLeft(1, flags
);
611 else if (keyCode
== WXK_UP
)
613 if (flags
& wxRICHTEXT_CTRL_DOWN
)
614 success
= MoveToParagraphStart(flags
);
616 success
= MoveUp(1, flags
);
618 else if (keyCode
== WXK_DOWN
)
620 if (flags
& wxRICHTEXT_CTRL_DOWN
)
621 success
= MoveToParagraphEnd(flags
);
623 success
= MoveDown(1, flags
);
625 else if (keyCode
== WXK_PAGEUP
)
627 success
= PageUp(1, flags
);
629 else if (keyCode
== WXK_PAGEDOWN
)
631 success
= PageDown(1, flags
);
633 else if (keyCode
== WXK_HOME
)
635 if (flags
& wxRICHTEXT_CTRL_DOWN
)
636 success
= MoveHome(flags
);
638 success
= MoveToLineStart(flags
);
640 else if (keyCode
== WXK_END
)
642 if (flags
& wxRICHTEXT_CTRL_DOWN
)
643 success
= MoveEnd(flags
);
645 success
= MoveToLineEnd(flags
);
650 ScrollIntoView(m_caretPosition
, keyCode
);
651 SetDefaultStyleToCursorStyle();
657 /// Extend the selection. Selections are in caret positions.
658 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
660 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
662 // If not currently selecting, start selecting
663 if (m_selectionRange
.GetStart() == -2)
665 m_selectionAnchor
= oldPos
;
668 m_selectionRange
.SetRange(newPos
+1, oldPos
);
670 m_selectionRange
.SetRange(oldPos
+1, newPos
);
674 // Always ensure that the selection range start is greater than
676 if (newPos
> m_selectionAnchor
)
677 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
679 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
682 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
684 wxLogDebug(wxT("Strange selection range"));
693 /// Scroll into view, returning true if we scrolled.
694 /// This takes a _caret_ position.
695 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
697 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
703 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
706 GetViewStart(& startX
, & startY
);
708 startY
= startY
* ppuY
;
711 GetVirtualSize(& sx
, & sy
);
716 wxRect rect
= line
->GetRect();
718 bool scrolled
= false;
720 wxSize clientSize
= GetClientSize();
723 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
725 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
727 // Make it scroll so this item is at the bottom
729 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
730 y
= (int) (0.5 + y
/ppuY
);
734 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
738 else if (rect
.y
< startY
)
740 // Make it scroll so this item is at the top
743 y
= (int) (0.5 + y
/ppuY
);
747 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
753 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
757 // Make it scroll so this item is at the top
760 y
= (int) (0.5 + y
/ppuY
);
764 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
768 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
770 // Make it scroll so this item is at the bottom
772 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
773 y
= (int) (0.5 + y
/ppuY
);
777 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
787 /// Is the given position visible on the screen?
788 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
790 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
796 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
799 GetViewStart(& startX
, & startY
);
801 startY
= startY
* ppuY
;
804 GetVirtualSize(& sx
, & sy
);
809 wxRect rect
= line
->GetRect();
811 wxSize clientSize
= GetClientSize();
813 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
816 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
818 m_caretPosition
= position
;
819 m_caretAtLineStart
= showAtLineStart
;
822 /// Move caret one visual step forward: this may mean setting a flag
823 /// and keeping the same position if we're going from the end of one line
824 /// to the start of the next, which may be the exact same caret position.
825 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
827 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
829 // Only do the check if we're not at the end of the paragraph (where things work OK
831 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
833 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
837 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
839 // We're at the end of a line. See whether we need to
840 // stay at the same actual caret position but change visual
842 if (oldPosition
== lineRange
.GetEnd())
844 if (m_caretAtLineStart
)
846 // We're already at the start of the line, so actually move on now.
847 m_caretPosition
= oldPosition
+ 1;
848 m_caretAtLineStart
= false;
852 // We're showing at the end of the line, so keep to
853 // the same position but indicate that we're to show
854 // at the start of the next line.
855 m_caretPosition
= oldPosition
;
856 m_caretAtLineStart
= true;
858 SetDefaultStyleToCursorStyle();
864 SetDefaultStyleToCursorStyle();
867 /// Move caret one visual step backward: this may mean setting a flag
868 /// and keeping the same position if we're going from the end of one line
869 /// to the start of the next, which may be the exact same caret position.
870 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
872 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
874 // Only do the check if we're not at the start of the paragraph (where things work OK
876 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
878 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
882 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
884 // We're at the start of a line. See whether we need to
885 // stay at the same actual caret position but change visual
887 if (oldPosition
== lineRange
.GetStart())
889 m_caretPosition
= oldPosition
-1;
890 m_caretAtLineStart
= true;
893 else if (oldPosition
== lineRange
.GetEnd())
895 if (m_caretAtLineStart
)
897 // We're at the start of the line, so keep the same caret position
898 // but clear the start-of-line flag.
899 m_caretPosition
= oldPosition
;
900 m_caretAtLineStart
= false;
904 // We're showing at the end of the line, so go back
905 // to the previous character position.
906 m_caretPosition
= oldPosition
- 1;
908 SetDefaultStyleToCursorStyle();
914 SetDefaultStyleToCursorStyle();
918 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
920 long endPos
= GetBuffer().GetRange().GetEnd();
922 if (m_caretPosition
+ noPositions
< endPos
)
924 long oldPos
= m_caretPosition
;
925 long newPos
= m_caretPosition
+ noPositions
;
927 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
931 // Determine by looking at oldPos and m_caretPosition whether
932 // we moved from the end of a line to the start of the next line, in which case
933 // we want to adjust the caret position such that it is positioned at the
934 // start of the next line, rather than jumping past the first character of the
936 if (noPositions
== 1 && !extendSel
)
937 MoveCaretForward(oldPos
);
939 SetCaretPosition(newPos
);
942 SetDefaultStyleToCursorStyle();
953 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
957 if (m_caretPosition
> startPos
- noPositions
+ 1)
959 long oldPos
= m_caretPosition
;
960 long newPos
= m_caretPosition
- noPositions
;
961 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
965 if (noPositions
== 1 && !extendSel
)
966 MoveCaretBack(oldPos
);
968 SetCaretPosition(newPos
);
971 SetDefaultStyleToCursorStyle();
982 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
984 return MoveDown(- noLines
, flags
);
988 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
993 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
994 wxPoint pt
= GetCaret()->GetPosition();
995 long newLine
= lineNumber
+ noLines
;
997 if (lineNumber
!= -1)
1001 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1003 if (newLine
> lastLine
)
1013 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1016 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1022 wxClientDC
dc(this);
1024 dc
.SetFont(GetFont());
1026 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1028 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1030 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1031 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1032 // so we view the caret at the start of the line.
1033 bool caretLineStart
= false;
1034 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1036 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1037 wxRichTextRange lineRange
;
1039 lineRange
= thisLine
->GetAbsoluteRange();
1041 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1044 caretLineStart
= true;
1048 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1049 if (para
&& para
->GetRange().GetStart() == newPos
)
1054 long newSelEnd
= newPos
;
1056 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1060 SetCaretPosition(newPos
, caretLineStart
);
1062 SetDefaultStyleToCursorStyle();
1072 /// Move to the end of the paragraph
1073 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1075 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1078 long newPos
= para
->GetRange().GetEnd() - 1;
1079 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1083 SetCaretPosition(newPos
);
1085 SetDefaultStyleToCursorStyle();
1095 /// Move to the start of the paragraph
1096 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1098 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1101 long newPos
= para
->GetRange().GetStart() - 1;
1102 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1106 SetCaretPosition(newPos
);
1108 SetDefaultStyleToCursorStyle();
1118 /// Move to the end of the line
1119 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1121 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1125 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1126 long newPos
= lineRange
.GetEnd();
1127 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1131 SetCaretPosition(newPos
);
1133 SetDefaultStyleToCursorStyle();
1143 /// Move to the start of the line
1144 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1146 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1149 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1150 long newPos
= lineRange
.GetStart()-1;
1152 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1156 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1158 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1160 SetDefaultStyleToCursorStyle();
1170 /// Move to the start of the buffer
1171 bool wxRichTextCtrl::MoveHome(int flags
)
1173 if (m_caretPosition
!= -1)
1175 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1179 SetCaretPosition(-1);
1181 SetDefaultStyleToCursorStyle();
1191 /// Move to the end of the buffer
1192 bool wxRichTextCtrl::MoveEnd(int flags
)
1194 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1196 if (m_caretPosition
!= endPos
)
1198 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1202 SetCaretPosition(endPos
);
1204 SetDefaultStyleToCursorStyle();
1214 /// Move noPages pages up
1215 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1217 return PageDown(- noPages
, flags
);
1220 /// Move noPages pages down
1221 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1223 // Calculate which line occurs noPages * screen height further down.
1224 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1227 wxSize clientSize
= GetClientSize();
1228 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1230 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1233 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1234 long pos
= lineRange
.GetStart()-1;
1235 if (pos
!= m_caretPosition
)
1237 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1239 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1243 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1245 SetDefaultStyleToCursorStyle();
1257 // Finds the caret position for the next word
1258 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1260 long endPos
= GetBuffer().GetRange().GetEnd();
1264 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1266 // First skip current text to space
1267 while (i
< endPos
&& i
> -1)
1269 // i is in character, not caret positions
1270 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1271 if (text
!= wxT(" ") && !text
.empty())
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
.empty()) // End of paragraph, or maybe an image
1283 return wxMax(-1, i
- 1);
1284 else if (text
== wxT(" ") || text
.empty())
1288 // Convert to caret position
1289 return wxMax(-1, i
- 1);
1298 long i
= m_caretPosition
;
1300 // First skip white space
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
1307 else if (text
== wxT(" ") || text
.empty())
1312 // Next skip current text to space
1313 while (i
< endPos
&& i
> -1)
1315 // i is in character, not caret positions
1316 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1317 if (text
!= wxT(" ") /* && !text.empty() */)
1330 /// Move n words left
1331 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1333 long pos
= FindNextWordPosition(-1);
1334 if (pos
!= m_caretPosition
)
1336 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1338 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1342 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1344 SetDefaultStyleToCursorStyle();
1354 /// Move n words right
1355 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1357 long pos
= FindNextWordPosition(1);
1358 if (pos
!= m_caretPosition
)
1360 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1362 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1366 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1368 SetDefaultStyleToCursorStyle();
1379 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1381 // Only do sizing optimization for large buffers
1382 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1384 m_fullLayoutRequired
= true;
1385 m_fullLayoutTime
= wxGetLocalTimeMillis();
1386 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1387 LayoutContent(true /* onlyVisibleRect */);
1390 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1398 /// Idle-time processing
1399 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1401 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1403 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1405 m_fullLayoutRequired
= false;
1406 m_fullLayoutTime
= 0;
1407 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1408 ShowPosition(m_fullLayoutSavedPosition
);
1415 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1421 /// Set up scrollbars, e.g. after a resize
1422 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1427 if (GetBuffer().IsEmpty())
1429 SetScrollbars(0, 0, 0, 0, 0, 0);
1433 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1434 // of pixels. See e.g. wxVScrolledWindow for ideas.
1435 int pixelsPerUnit
= 5; // 10;
1436 wxSize clientSize
= GetClientSize();
1438 int maxHeight
= GetBuffer().GetCachedSize().y
;
1440 int unitsY
= maxHeight
/pixelsPerUnit
;
1442 int startX
= 0, startY
= 0;
1444 GetViewStart(& startX
, & startY
);
1446 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1447 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1449 // Move to previous scroll position if
1451 SetScrollbars(0, pixelsPerUnit
,
1453 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1456 /// Paint the background
1457 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1459 wxColour backgroundColour
= GetBackgroundColour();
1460 if (!backgroundColour
.Ok())
1461 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1463 // Clear the background
1464 dc
.SetBrush(wxBrush(backgroundColour
));
1465 dc
.SetPen(*wxTRANSPARENT_PEN
);
1466 wxRect
windowRect(GetClientSize());
1467 windowRect
.x
-= 2; windowRect
.y
-= 2;
1468 windowRect
.width
+= 4; windowRect
.height
+= 4;
1470 // We need to shift the rectangle to take into account
1471 // scrolling. Converting device to logical coordinates.
1472 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1473 dc
.DrawRectangle(windowRect
);
1476 /// Recreate buffer bitmap if necessary
1477 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1480 if (sz
== wxDefaultSize
)
1481 sz
= GetClientSize();
1483 if (sz
.x
< 1 || sz
.y
< 1)
1486 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1487 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1488 return m_bufferBitmap
.Ok();
1491 // ----------------------------------------------------------------------------
1492 // file IO functions
1493 // ----------------------------------------------------------------------------
1495 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1497 bool success
= GetBuffer().LoadFile(filename
, type
);
1499 m_filename
= filename
;
1502 SetInsertionPoint(0);
1505 SetupScrollbars(true);
1513 wxLogError(_("File couldn't be loaded."));
1519 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1521 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1522 if ( filenameToUse
.empty() )
1524 // what kind of message to give? is it an error or a program bug?
1525 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1530 if (GetBuffer().SaveFile(filenameToUse
, type
))
1532 m_filename
= filenameToUse
;
1540 wxLogError(_("The text couldn't be saved."));
1545 // ----------------------------------------------------------------------------
1546 // wxRichTextCtrl specific functionality
1547 // ----------------------------------------------------------------------------
1549 /// Add a new paragraph of text to the end of the buffer
1550 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1552 return GetBuffer().AddParagraph(text
);
1556 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1558 return GetBuffer().AddImage(image
);
1561 // ----------------------------------------------------------------------------
1562 // selection and ranges
1563 // ----------------------------------------------------------------------------
1565 void wxRichTextCtrl::SelectAll()
1567 SetSelection(0, GetLastPosition());
1568 m_selectionAnchor
= -1;
1572 void wxRichTextCtrl::SelectNone()
1574 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1575 SetSelection(-2, -2);
1576 m_selectionAnchor
= -2;
1579 wxString
wxRichTextCtrl::GetStringSelection() const
1582 GetSelection(&from
, &to
);
1584 return GetRange(from
, to
);
1587 // do the window-specific processing after processing the update event
1588 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1589 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1592 wxWindowBase::DoUpdateWindowUI(event
);
1595 if ( event
.GetSetText() )
1597 if ( event
.GetText() != GetValue() )
1598 SetValue(event
.GetText());
1601 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1603 // ----------------------------------------------------------------------------
1605 // ----------------------------------------------------------------------------
1607 wxTextCtrlHitTestResult
1608 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1610 // implement in terms of the other overload as the native ports typically
1611 // can get the position and not (x, y) pair directly (although wxUniv
1612 // directly gets x and y -- and so overrides this method as well)
1614 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1616 if ( rc
!= wxTE_HT_UNKNOWN
)
1618 PositionToXY(pos
, x
, y
);
1624 wxTextCtrlHitTestResult
1625 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1628 wxClientDC
dc((wxRichTextCtrl
*) this);
1629 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1631 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1635 case wxRICHTEXT_HITTEST_BEFORE
:
1636 return wxTE_HT_BEFORE
;
1638 case wxRICHTEXT_HITTEST_AFTER
:
1639 return wxTE_HT_BEYOND
;
1641 case wxRICHTEXT_HITTEST_ON
:
1642 return wxTE_HT_ON_TEXT
;
1645 return wxTE_HT_UNKNOWN
;
1648 // ----------------------------------------------------------------------------
1649 // set/get the controls text
1650 // ----------------------------------------------------------------------------
1652 wxString
wxRichTextCtrl::GetValue() const
1654 return GetBuffer().GetText();
1657 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1659 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1662 void wxRichTextCtrl::SetValue(const wxString
& value
)
1666 // if the text is long enough, it's faster to just set it instead of first
1667 // comparing it with the old one (chances are that it will be different
1668 // anyhow, this comparison is there to avoid flicker for small single-line
1669 // edit controls mostly)
1670 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1672 DoWriteText(value
, false /* not selection only */);
1674 // for compatibility, don't move the cursor when doing SetValue()
1675 SetInsertionPoint(0);
1679 // still send an event for consistency
1683 // we should reset the modified flag even if the value didn't really change
1685 // mark the control as being not dirty - we changed its text, not the
1690 void wxRichTextCtrl::WriteText(const wxString
& value
)
1695 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1697 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1700 void wxRichTextCtrl::AppendText(const wxString
& text
)
1702 SetInsertionPointEnd();
1707 /// Write an image at the current insertion point
1708 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1710 wxRichTextImageBlock imageBlock
;
1712 wxImage image2
= image
;
1713 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1714 return WriteImage(imageBlock
);
1719 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1721 wxRichTextImageBlock imageBlock
;
1724 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1725 return WriteImage(imageBlock
);
1730 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1732 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1735 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1739 wxRichTextImageBlock imageBlock
;
1741 wxImage image
= bitmap
.ConvertToImage();
1742 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1743 return WriteImage(imageBlock
);
1749 /// Insert a newline (actually paragraph) at the current insertion point.
1750 bool wxRichTextCtrl::Newline()
1752 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1756 // ----------------------------------------------------------------------------
1757 // Clipboard operations
1758 // ----------------------------------------------------------------------------
1760 void wxRichTextCtrl::Copy()
1764 wxRichTextRange range
= GetSelectionRange();
1765 GetBuffer().CopyToClipboard(range
);
1769 void wxRichTextCtrl::Cut()
1773 wxRichTextRange range
= GetSelectionRange();
1774 GetBuffer().CopyToClipboard(range
);
1776 DeleteSelectedContent();
1782 void wxRichTextCtrl::Paste()
1786 BeginBatchUndo(_("Paste"));
1788 long newPos
= m_caretPosition
;
1789 DeleteSelectedContent(& newPos
);
1791 GetBuffer().PasteFromClipboard(newPos
);
1797 void wxRichTextCtrl::DeleteSelection()
1799 if (CanDeleteSelection())
1801 DeleteSelectedContent();
1805 bool wxRichTextCtrl::HasSelection() const
1807 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1810 bool wxRichTextCtrl::CanCopy() const
1812 // Can copy if there's a selection
1813 return HasSelection();
1816 bool wxRichTextCtrl::CanCut() const
1818 return HasSelection() && IsEditable();
1821 bool wxRichTextCtrl::CanPaste() const
1823 if ( !IsEditable() )
1826 return GetBuffer().CanPasteFromClipboard();
1829 bool wxRichTextCtrl::CanDeleteSelection() const
1831 return HasSelection() && IsEditable();
1835 // ----------------------------------------------------------------------------
1837 // ----------------------------------------------------------------------------
1839 void wxRichTextCtrl::SetEditable(bool editable
)
1841 m_editable
= editable
;
1844 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1848 m_caretPosition
= pos
- 1;
1851 void wxRichTextCtrl::SetInsertionPointEnd()
1853 long pos
= GetLastPosition();
1854 SetInsertionPoint(pos
);
1857 long wxRichTextCtrl::GetInsertionPoint() const
1859 return m_caretPosition
+1;
1862 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1864 return GetBuffer().GetRange().GetEnd();
1867 // If the return values from and to are the same, there is no
1869 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1871 *from
= m_selectionRange
.GetStart();
1872 *to
= m_selectionRange
.GetEnd();
1875 bool wxRichTextCtrl::IsEditable() const
1880 // ----------------------------------------------------------------------------
1882 // ----------------------------------------------------------------------------
1884 void wxRichTextCtrl::SetSelection(long from
, long to
)
1886 // if from and to are both -1, it means (in wxWidgets) that all text should
1888 if ( (from
== -1) && (to
== -1) )
1891 to
= GetLastPosition();
1894 DoSetSelection(from
, to
);
1897 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1899 m_selectionAnchor
= from
;
1900 m_selectionRange
.SetRange(from
, to
);
1905 // ----------------------------------------------------------------------------
1907 // ----------------------------------------------------------------------------
1909 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1911 BeginBatchUndo(_("Replace"));
1913 DeleteSelectedContent();
1915 DoWriteText(value
, true /* selection only */);
1920 void wxRichTextCtrl::Remove(long from
, long to
)
1924 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1925 m_caretPosition
, // Current caret position
1926 from
, // New caret position
1934 bool wxRichTextCtrl::IsModified() const
1936 return m_buffer
.IsModified();
1939 void wxRichTextCtrl::MarkDirty()
1941 m_buffer
.Modify(true);
1944 void wxRichTextCtrl::DiscardEdits()
1946 m_buffer
.Modify(false);
1947 m_buffer
.GetCommandProcessor()->ClearCommands();
1950 int wxRichTextCtrl::GetNumberOfLines() const
1952 return GetBuffer().GetParagraphCount();
1955 // ----------------------------------------------------------------------------
1956 // Positions <-> coords
1957 // ----------------------------------------------------------------------------
1959 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1961 return GetBuffer().XYToPosition(x
, y
);
1964 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1966 return GetBuffer().PositionToXY(pos
, x
, y
);
1969 // ----------------------------------------------------------------------------
1971 // ----------------------------------------------------------------------------
1973 void wxRichTextCtrl::ShowPosition(long pos
)
1975 if (!IsPositionVisible(pos
))
1976 ScrollIntoView(pos
-1, WXK_DOWN
);
1979 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1981 return GetBuffer().GetParagraphLength(lineNo
);
1984 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1986 return GetBuffer().GetParagraphText(lineNo
);
1989 // ----------------------------------------------------------------------------
1991 // ----------------------------------------------------------------------------
1993 void wxRichTextCtrl::Undo()
1997 GetCommandProcessor()->Undo();
2001 void wxRichTextCtrl::Redo()
2005 GetCommandProcessor()->Redo();
2009 bool wxRichTextCtrl::CanUndo() const
2011 return GetCommandProcessor()->CanUndo();
2014 bool wxRichTextCtrl::CanRedo() const
2016 return GetCommandProcessor()->CanRedo();
2019 // ----------------------------------------------------------------------------
2020 // implemenation details
2021 // ----------------------------------------------------------------------------
2023 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2025 SetValue(event
.GetString());
2026 GetEventHandler()->ProcessEvent(event
);
2029 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2031 // By default, load the first file into the text window.
2032 if (event
.GetNumberOfFiles() > 0)
2034 LoadFile(event
.GetFiles()[0]);
2038 // ----------------------------------------------------------------------------
2039 // text control event processing
2040 // ----------------------------------------------------------------------------
2042 bool wxRichTextCtrl::SendUpdateEvent()
2044 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2045 InitCommandEvent(event
);
2047 return GetEventHandler()->ProcessEvent(event
);
2050 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2052 event
.SetEventObject((wxControlBase
*)this); // const_cast
2054 switch ( m_clientDataType
)
2056 case wxClientData_Void
:
2057 event
.SetClientData(GetClientData());
2060 case wxClientData_Object
:
2061 event
.SetClientObject(GetClientObject());
2064 case wxClientData_None
:
2071 wxSize
wxRichTextCtrl::DoGetBestSize() const
2073 return wxSize(10, 10);
2076 // ----------------------------------------------------------------------------
2077 // standard handlers for standard edit menu events
2078 // ----------------------------------------------------------------------------
2080 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2085 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2090 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2095 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2100 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2105 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2110 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2112 event
.Enable( CanCut() );
2115 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2117 event
.Enable( CanCopy() );
2120 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2122 event
.Enable( CanDeleteSelection() );
2125 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2127 event
.Enable( CanPaste() );
2130 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2132 event
.Enable( CanUndo() );
2133 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2136 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2138 event
.Enable( CanRedo() );
2139 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2142 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2147 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2149 event
.Enable(GetLastPosition() > 0);
2152 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2156 m_contextMenu
= new wxMenu
;
2157 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2158 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2159 m_contextMenu
->AppendSeparator();
2160 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2161 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2162 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2163 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2164 m_contextMenu
->AppendSeparator();
2165 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2167 PopupMenu(m_contextMenu
);
2171 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2173 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2176 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2178 return GetBuffer().SetStyle(range
, style
);
2181 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2183 return GetBuffer().SetDefaultStyle(style
);
2186 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2188 return GetBuffer().GetDefaultStyle();
2191 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2193 return GetBuffer().GetStyle(position
, style
);
2196 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2198 return GetBuffer().GetStyle(position
, style
);
2201 /// Set font, and also the buffer attributes
2202 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2204 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2205 wxControl::SetFont(font
);
2207 wxScrolledWindow::SetFont(font
);
2210 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2212 GetBuffer().SetBasicStyle(attr
);
2213 GetBuffer().SetDefaultStyle(attr
);
2218 /// Transform logical to physical
2219 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2222 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2227 /// Transform physical to logical
2228 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2231 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2236 /// Position the caret
2237 void wxRichTextCtrl::PositionCaret()
2242 //wxLogDebug(wxT("PositionCaret"));
2245 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2247 wxPoint originalPt
= caretRect
.GetPosition();
2248 wxPoint pt
= GetPhysicalPoint(originalPt
);
2249 if (GetCaret()->GetPosition() != pt
)
2251 GetCaret()->Move(pt
);
2252 GetCaret()->SetSize(caretRect
.GetSize());
2257 /// Get the caret height and position for the given character position
2258 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2260 wxClientDC
dc(this);
2261 dc
.SetFont(GetFont());
2268 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2270 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2277 /// Gets the line for the visible caret position. If the caret is
2278 /// shown at the very end of the line, it means the next character is actually
2279 /// on the following line. So let's get the line we're expecting to find
2280 /// if this is the case.
2281 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2283 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2284 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2287 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2288 if (caretPosition
== lineRange
.GetStart()-1 &&
2289 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2291 if (!m_caretAtLineStart
)
2292 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2299 /// Move the caret to the given character position
2300 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2302 if (GetBuffer().GetDirty())
2305 if (pos
<= GetBuffer().GetRange().GetEnd())
2307 SetCaretPosition(pos
, showAtLineStart
);
2317 /// Layout the buffer: which we must do before certain operations, such as
2318 /// setting the caret position.
2319 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2321 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2323 wxRect
availableSpace(GetClientSize());
2324 if (availableSpace
.width
== 0)
2325 availableSpace
.width
= 10;
2326 if (availableSpace
.height
== 0)
2327 availableSpace
.height
= 10;
2329 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2330 if (onlyVisibleRect
)
2332 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2333 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2336 wxClientDC
dc(this);
2337 dc
.SetFont(GetFont());
2341 GetBuffer().Defragment();
2342 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2343 GetBuffer().Layout(dc
, availableSpace
, flags
);
2344 GetBuffer().SetDirty(false);
2353 /// Is all of the selection bold?
2354 bool wxRichTextCtrl::IsSelectionBold() const
2358 wxRichTextAttr attr
;
2359 wxRichTextRange range
= GetSelectionRange();
2360 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2361 attr
.SetFontWeight(wxBOLD
);
2363 return HasCharacterAttributes(range
, attr
);
2367 // If no selection, then we need to combine current style with default style
2368 // to see what the effect would be if we started typing.
2369 wxRichTextAttr attr
;
2370 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2371 if (GetStyle(GetCaretPosition()+1, attr
))
2373 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2374 return attr
.GetFontWeight() == wxBOLD
;
2380 /// Is all of the selection italics?
2381 bool wxRichTextCtrl::IsSelectionItalics() const
2385 wxRichTextRange range
= GetSelectionRange();
2386 wxRichTextAttr attr
;
2387 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2388 attr
.SetFontStyle(wxITALIC
);
2390 return HasCharacterAttributes(range
, attr
);
2394 // If no selection, then we need to combine current style with default style
2395 // to see what the effect would be if we started typing.
2396 wxRichTextAttr attr
;
2397 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2398 if (GetStyle(GetCaretPosition()+1, attr
))
2400 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2401 return attr
.GetFontStyle() == wxITALIC
;
2407 /// Is all of the selection underlined?
2408 bool wxRichTextCtrl::IsSelectionUnderlined() const
2412 wxRichTextRange range
= GetSelectionRange();
2413 wxRichTextAttr attr
;
2414 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2415 attr
.SetFontUnderlined(true);
2417 return HasCharacterAttributes(range
, attr
);
2421 // If no selection, then we need to combine current style with default style
2422 // to see what the effect would be if we started typing.
2423 wxRichTextAttr attr
;
2424 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2425 if (GetStyle(GetCaretPosition()+1, attr
))
2427 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2428 return attr
.GetFontUnderlined();
2434 /// Apply bold to the selection
2435 bool wxRichTextCtrl::ApplyBoldToSelection()
2437 wxRichTextAttr attr
;
2438 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2439 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2442 return SetStyle(GetSelectionRange(), attr
);
2444 SetDefaultStyle(attr
);
2448 /// Apply italic to the selection
2449 bool wxRichTextCtrl::ApplyItalicToSelection()
2451 wxRichTextAttr attr
;
2452 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2453 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2456 return SetStyle(GetSelectionRange(), attr
);
2458 SetDefaultStyle(attr
);
2462 /// Apply underline to the selection
2463 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2465 wxRichTextAttr attr
;
2466 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2467 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2470 return SetStyle(GetSelectionRange(), attr
);
2472 SetDefaultStyle(attr
);
2476 /// Is all of the selection aligned according to the specified flag?
2477 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2481 wxRichTextRange range
= GetSelectionRange();
2482 wxRichTextAttr attr
;
2483 attr
.SetAlignment(alignment
);
2485 return HasParagraphAttributes(range
, attr
);
2489 // If no selection, then we need to get information from the current paragraph.
2490 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2492 return para
->GetAttributes().GetAlignment() == alignment
;
2497 /// Apply alignment to the selection
2498 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2500 wxRichTextAttr attr
;
2501 attr
.SetAlignment(alignment
);
2503 return SetStyle(GetSelectionRange(), attr
);
2506 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2508 return SetStyle(para
->GetRange(), attr
);
2513 /// Sets the default style to the style under the cursor
2514 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2517 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2519 if (GetStyle(GetCaretPosition(), attr
))
2521 SetDefaultStyle(attr
);
2528 /// Returns the first visible position in the current view
2529 long wxRichTextCtrl::GetFirstVisiblePosition() const
2531 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2533 return line
->GetAbsoluteRange().GetStart();