1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
27 #include "wx/textfile.h"
29 #include "wx/settings.h"
30 #include "wx/filename.h"
31 #include "wx/dcbuffer.h"
33 #include "wx/richtext/richtextctrl.h"
34 #include "wx/arrimpl.cpp"
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
44 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
45 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
47 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
50 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
52 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
53 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
55 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
57 EVT_PAINT(wxRichTextCtrl::OnPaint
)
58 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
59 EVT_IDLE(wxRichTextCtrl::OnIdle
)
60 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
61 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
62 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
63 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
64 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
65 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
66 EVT_CHAR(wxRichTextCtrl::OnChar
)
67 EVT_SIZE(wxRichTextCtrl::OnSize
)
68 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
69 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
70 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
72 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
73 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
75 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
76 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
78 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
79 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
81 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
82 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
84 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
85 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
87 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
88 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
90 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
91 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
98 wxRichTextCtrl::wxRichTextCtrl()
99 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
100 : wxScrollHelper(this)
106 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
107 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
108 : wxScrollHelper(this)
112 Create(parent
, id
, pos
, size
, style
);
116 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
118 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
119 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
123 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
130 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
133 GetBuffer().SetRichTextCtrl(this);
135 wxTextAttrEx attributes
;
136 attributes
.SetFont(GetFont());
137 attributes
.SetTextColour(*wxBLACK
);
138 attributes
.SetBackgroundColour(*wxWHITE
);
139 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
140 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
142 SetDefaultStyle(attributes
);
143 SetBasicStyle(attributes
);
145 SetBackgroundColour(*wxWHITE
);
146 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
148 // Tell the sizers to use the given or best size
149 SetBestFittingSize(size
);
152 RecreateBuffer(size
);
154 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
159 SetCursor(wxCursor(wxCURSOR_IBEAM
));
164 wxRichTextCtrl::~wxRichTextCtrl()
166 delete m_contextMenu
;
169 /// Member initialisation
170 void wxRichTextCtrl::Init()
173 m_contextMenu
= NULL
;
175 m_caretPosition
= -1;
176 m_selectionRange
.SetRange(-2, -2);
177 m_selectionAnchor
= -2;
179 m_caretAtLineStart
= false;
181 m_fullLayoutRequired
= false;
182 m_fullLayoutTime
= 0;
183 m_fullLayoutSavedPosition
= 0;
184 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
187 /// Call Freeze to prevent refresh
188 void wxRichTextCtrl::Freeze()
193 /// Call Thaw to refresh
194 void wxRichTextCtrl::Thaw(bool refresh
)
198 if (m_freezeCount
== 0 && refresh
)
206 void wxRichTextCtrl::Clear()
209 m_buffer
.SetDirty(true);
210 m_caretPosition
= -1;
211 m_caretAtLineStart
= false;
212 m_selectionRange
.SetRange(-2, -2);
214 if (m_freezeCount
== 0)
223 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
225 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
229 if (m_freezeCount
> 0)
232 dc
.SetFont(GetFont());
234 // Paint the background
237 wxRegion dirtyRegion
= GetUpdateRegion();
239 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
240 wxRect
availableSpace(GetClientSize());
241 if (GetBuffer().GetDirty())
243 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
244 GetBuffer().SetDirty(false);
249 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
252 // Empty implementation, to prevent flicker
253 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
257 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
263 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
270 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
276 dc
.SetFont(GetFont());
279 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
281 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
283 m_dragStart
= event
.GetLogicalPosition(dc
);
289 bool caretAtLineStart
= false;
291 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
293 // If we're at the start of a line (but not first in para)
294 // then we should keep the caret showing at the start of the line
295 // by showing the m_caretAtLineStart flag.
296 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
297 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
299 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
300 caretAtLineStart
= true;
304 MoveCaret(position
, caretAtLineStart
);
305 SetDefaultStyleToCursorStyle();
308 wxWindow
* p
= GetParent();
309 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
312 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
315 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
316 frame
->SetStatusText(msg
, 1);
325 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
330 if (GetCapture() == this)
336 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
338 if (!event
.Dragging())
346 dc
.SetFont(GetFont());
349 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
350 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
352 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
354 // TODO: test closeness
356 bool caretAtLineStart
= false;
358 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
360 // If we're at the start of a line (but not first in para)
361 // then we should keep the caret showing at the start of the line
362 // by showing the m_caretAtLineStart flag.
363 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
364 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
366 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
367 caretAtLineStart
= true;
371 if (m_caretPosition
!= position
)
373 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
375 MoveCaret(position
, caretAtLineStart
);
376 SetDefaultStyleToCursorStyle();
385 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
391 /// Left-double-click
392 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
398 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
404 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
407 if (event
.ControlDown())
408 flags
|= wxRICHTEXT_CTRL_DOWN
;
409 if (event
.ShiftDown())
410 flags
|= wxRICHTEXT_SHIFT_DOWN
;
412 flags
|= wxRICHTEXT_ALT_DOWN
;
414 if (event
.GetKeyCode() == WXK_LEFT
||
415 event
.GetKeyCode() == WXK_RIGHT
||
416 event
.GetKeyCode() == WXK_UP
||
417 event
.GetKeyCode() == WXK_DOWN
||
418 event
.GetKeyCode() == WXK_HOME
||
419 event
.GetKeyCode() == WXK_PAGEUP
||
420 event
.GetKeyCode() == WXK_PAGEDOWN
||
421 event
.GetKeyCode() == WXK_PRIOR
||
422 event
.GetKeyCode() == WXK_NEXT
||
423 event
.GetKeyCode() == WXK_END
)
425 Navigate(event
.GetKeyCode(), flags
);
427 else if (event
.GetKeyCode() == WXK_RETURN
)
429 BeginBatchUndo(_("Insert Text"));
431 long newPos
= m_caretPosition
;
433 DeleteSelectedContent(& newPos
);
435 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
437 wxRichTextEvent
cmdEvent(
438 wxEVT_COMMAND_RICHTEXT_RETURN
,
440 cmdEvent
.SetEventObject(this);
441 cmdEvent
.SetFlags(flags
);
442 GetEventHandler()->ProcessEvent(cmdEvent
);
445 SetDefaultStyleToCursorStyle();
447 else if (event
.GetKeyCode() == WXK_BACK
)
449 BeginBatchUndo(_("Delete Text"));
451 // Submit range in character positions, which are greater than caret positions,
452 // so subtract 1 for deleted character and add 1 for conversion to character position.
453 if (m_caretPosition
> -1 && !HasSelection())
455 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
456 m_caretPosition
, // Current caret position
457 m_caretPosition
-1, // New caret position
461 DeleteSelectedContent();
465 // Shouldn't this be in Do()?
466 if (GetLastPosition() == -1)
470 m_caretPosition
= -1;
472 SetDefaultStyleToCursorStyle();
476 else if (event
.GetKeyCode() == WXK_DELETE
)
478 BeginBatchUndo(_("Delete Text"));
480 // Submit range in character positions, which are greater than caret positions,
481 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
483 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
484 m_caretPosition
, // Current caret position
485 m_caretPosition
+1, // New caret position
489 DeleteSelectedContent();
493 // Shouldn't this be in Do()?
494 if (GetLastPosition() == -1)
498 m_caretPosition
= -1;
500 SetDefaultStyleToCursorStyle();
505 BeginBatchUndo(_("Insert Text"));
507 long newPos
= m_caretPosition
;
508 DeleteSelectedContent(& newPos
);
510 wxString str
= (wxChar
) event
.GetKeyCode();
511 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
515 SetDefaultStyleToCursorStyle();
523 /// Delete content if there is a selection, e.g. when pressing a key.
524 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
528 long pos
= m_selectionRange
.GetStart();
529 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
530 m_caretPosition
, // Current caret position
531 pos
, // New caret position
533 m_selectionRange
.SetRange(-2, -2);
543 /// Keyboard navigation
547 Left: left one character
548 Right: right one character
551 Ctrl-Left: left one word
552 Ctrl-Right: right one word
553 Ctrl-Up: previous paragraph start
554 Ctrl-Down: next start of paragraph
557 Ctrl-Home: start of document
558 Ctrl-End: end of document
560 Page-Down: Down a screen
564 Ctrl-Alt-PgUp: Start of window
565 Ctrl-Alt-PgDn: End of window
566 F8: Start selection mode
567 Esc: End selection mode
569 Adding Shift does the above but starts/extends selection.
574 bool wxRichTextCtrl::Navigate(int keyCode
, int flags
)
576 bool success
= false;
579 if (keyCode
== WXK_RIGHT
)
581 if (flags
& wxRICHTEXT_CTRL_DOWN
)
582 success
= WordRight(1, flags
);
584 success
= MoveRight(1, flags
);
586 else if (keyCode
== WXK_LEFT
)
588 if (flags
& wxRICHTEXT_CTRL_DOWN
)
589 success
= WordLeft(1, flags
);
591 success
= MoveLeft(1, flags
);
593 else if (keyCode
== WXK_UP
)
595 if (flags
& wxRICHTEXT_CTRL_DOWN
)
596 success
= MoveToParagraphStart(flags
);
598 success
= MoveUp(1, flags
);
600 else if (keyCode
== WXK_DOWN
)
602 if (flags
& wxRICHTEXT_CTRL_DOWN
)
603 success
= MoveToParagraphEnd(flags
);
605 success
= MoveDown(1, flags
);
607 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_PRIOR
)
609 success
= PageUp(1, flags
);
611 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NEXT
)
613 success
= PageDown(1, flags
);
615 else if (keyCode
== WXK_HOME
)
617 if (flags
& wxRICHTEXT_CTRL_DOWN
)
618 success
= MoveHome(flags
);
620 success
= MoveToLineStart(flags
);
622 else if (keyCode
== WXK_END
)
624 if (flags
& wxRICHTEXT_CTRL_DOWN
)
625 success
= MoveEnd(flags
);
627 success
= MoveToLineEnd(flags
);
632 ScrollIntoView(m_caretPosition
, keyCode
);
633 SetDefaultStyleToCursorStyle();
636 // Only refresh if something changed
642 /// Extend the selection. Selections are in caret positions.
643 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
645 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
647 // If not currently selecting, start selecting
648 if (m_selectionRange
.GetStart() == -2)
650 m_selectionAnchor
= oldPos
;
653 m_selectionRange
.SetRange(newPos
+1, oldPos
);
655 m_selectionRange
.SetRange(oldPos
+1, newPos
);
659 // Always ensure that the selection range start is greater than
661 if (newPos
> m_selectionAnchor
)
662 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
664 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
667 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
669 wxLogDebug(wxT("Strange selection range"));
678 /// Scroll into view, returning true if we scrolled.
679 /// This takes a _caret_ position.
680 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
682 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
688 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
691 GetViewStart(& startX
, & startY
);
693 startY
= startY
* ppuY
;
696 GetVirtualSize(& sx
, & sy
);
701 wxRect rect
= line
->GetRect();
703 bool scrolled
= false;
705 wxSize clientSize
= GetClientSize();
708 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_NEXT
|| keyCode
== WXK_PAGEDOWN
)
710 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
712 // Make it scroll so this item is at the bottom
714 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
715 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
717 else if (rect
.y
< startY
)
719 // Make it scroll so this item is at the top
722 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
727 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PRIOR
|| keyCode
== WXK_PAGEUP
)
731 // Make it scroll so this item is at the top
734 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
736 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
738 // Make it scroll so this item is at the bottom
740 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
741 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
750 /// Is the given position visible on the screen?
751 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
753 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
759 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
762 GetViewStart(& startX
, & startY
);
764 startY
= startY
* ppuY
;
767 GetVirtualSize(& sx
, & sy
);
772 wxRect rect
= line
->GetRect();
774 wxSize clientSize
= GetClientSize();
776 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
779 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
781 m_caretPosition
= position
;
782 m_caretAtLineStart
= showAtLineStart
;
785 /// Move caret one visual step forward: this may mean setting a flag
786 /// and keeping the same position if we're going from the end of one line
787 /// to the start of the next, which may be the exact same caret position.
788 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
790 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
792 // Only do the check if we're not at the end of the paragraph (where things work OK
794 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
796 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
800 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
802 // We're at the end of a line. See whether we need to
803 // stay at the same actual caret position but change visual
805 if (oldPosition
== lineRange
.GetEnd())
807 if (m_caretAtLineStart
)
809 // We're already at the start of the line, so actually move on now.
810 m_caretPosition
= oldPosition
+ 1;
811 m_caretAtLineStart
= false;
815 // We're showing at the end of the line, so keep to
816 // the same position but indicate that we're to show
817 // at the start of the next line.
818 m_caretPosition
= oldPosition
;
819 m_caretAtLineStart
= true;
821 SetDefaultStyleToCursorStyle();
827 SetDefaultStyleToCursorStyle();
830 /// Move caret one visual step backward: this may mean setting a flag
831 /// and keeping the same position if we're going from the end of one line
832 /// to the start of the next, which may be the exact same caret position.
833 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
835 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
837 // Only do the check if we're not at the start of the paragraph (where things work OK
839 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
841 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
845 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
847 // We're at the start of a line. See whether we need to
848 // stay at the same actual caret position but change visual
850 if (oldPosition
== lineRange
.GetStart())
852 m_caretPosition
= oldPosition
-1;
853 m_caretAtLineStart
= true;
856 else if (oldPosition
== lineRange
.GetEnd())
858 if (m_caretAtLineStart
)
860 // We're at the start of the line, so keep the same caret position
861 // but clear the start-of-line flag.
862 m_caretPosition
= oldPosition
;
863 m_caretAtLineStart
= false;
867 // We're showing at the end of the line, so go back
868 // to the previous character position.
869 m_caretPosition
= oldPosition
- 1;
871 SetDefaultStyleToCursorStyle();
877 SetDefaultStyleToCursorStyle();
881 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
883 long endPos
= GetBuffer().GetRange().GetEnd();
885 if (m_caretPosition
+ noPositions
< endPos
)
887 long oldPos
= m_caretPosition
;
888 long newPos
= m_caretPosition
+ noPositions
;
890 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
894 // Determine by looking at oldPos and m_caretPosition whether
895 // we moved from the end of a line to the start of the next line, in which case
896 // we want to adjust the caret position such that it is positioned at the
897 // start of the next line, rather than jumping past the first character of the
899 if (noPositions
== 1 && !extendSel
)
900 MoveCaretForward(oldPos
);
902 SetCaretPosition(newPos
);
905 SetDefaultStyleToCursorStyle();
908 Refresh(); // TODO: optimize so that if we didn't change the selection, we don't refresh
916 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
920 if (m_caretPosition
> startPos
- noPositions
+ 1)
922 long oldPos
= m_caretPosition
;
923 long newPos
= m_caretPosition
- noPositions
;
924 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
928 if (noPositions
== 1 && !extendSel
)
929 MoveCaretBack(oldPos
);
931 SetCaretPosition(newPos
);
934 SetDefaultStyleToCursorStyle();
945 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
947 return MoveDown(- noLines
, flags
);
951 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
953 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
954 wxPoint pt
= GetCaret()->GetPosition();
955 long newLine
= lineNumber
+ noLines
;
957 if (lineNumber
!= -1)
961 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
963 if (newLine
> lastLine
)
973 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
976 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
984 dc
.SetFont(GetFont());
986 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
988 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
990 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
991 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
992 // so we view the caret at the start of the line.
993 bool caretLineStart
= false;
994 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
996 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
997 wxRichTextRange lineRange
;
999 lineRange
= thisLine
->GetAbsoluteRange();
1001 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1004 caretLineStart
= true;
1008 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1009 if (para
&& para
->GetRange().GetStart() == newPos
)
1014 long newSelEnd
= newPos
;
1016 if (!ExtendSelection(m_caretPosition
, newSelEnd
, flags
))
1019 SetCaretPosition(newPos
, caretLineStart
);
1021 SetDefaultStyleToCursorStyle();
1031 /// Move to the end of the paragraph
1032 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1034 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1037 long newPos
= para
->GetRange().GetEnd() - 1;
1038 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1041 SetCaretPosition(newPos
);
1043 SetDefaultStyleToCursorStyle();
1053 /// Move to the start of the paragraph
1054 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1056 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1059 long newPos
= para
->GetRange().GetStart() - 1;
1060 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1063 SetCaretPosition(newPos
);
1065 SetDefaultStyleToCursorStyle();
1075 /// Move to the end of the line
1076 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1078 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1082 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1083 long newPos
= lineRange
.GetEnd();
1084 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1087 SetCaretPosition(newPos
);
1089 SetDefaultStyleToCursorStyle();
1099 /// Move to the start of the line
1100 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1102 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1105 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1106 long newPos
= lineRange
.GetStart()-1;
1108 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1111 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1113 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1115 SetDefaultStyleToCursorStyle();
1125 /// Move to the start of the buffer
1126 bool wxRichTextCtrl::MoveHome(int flags
)
1128 if (m_caretPosition
!= -1)
1130 if (!ExtendSelection(m_caretPosition
, -1, flags
))
1133 SetCaretPosition(-1);
1135 SetDefaultStyleToCursorStyle();
1145 /// Move to the end of the buffer
1146 bool wxRichTextCtrl::MoveEnd(int flags
)
1148 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1150 if (m_caretPosition
!= endPos
)
1152 if (!ExtendSelection(m_caretPosition
, endPos
, flags
))
1155 SetCaretPosition(endPos
);
1157 SetDefaultStyleToCursorStyle();
1167 /// Move noPages pages up
1168 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1170 return PageDown(- noPages
, flags
);
1173 /// Move noPages pages down
1174 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1176 // Calculate which line occurs noPages * screen height further down.
1177 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1180 wxSize clientSize
= GetClientSize();
1181 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1183 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1186 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1187 long pos
= lineRange
.GetStart()-1;
1188 if (pos
!= m_caretPosition
)
1190 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1192 if (!ExtendSelection(m_caretPosition
, pos
, flags
))
1195 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1197 SetDefaultStyleToCursorStyle();
1209 // Finds the caret position for the next word
1210 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1212 long endPos
= GetBuffer().GetRange().GetEnd();
1216 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1218 // First skip current text to space
1219 while (i
< endPos
&& i
> -1)
1221 // i is in character, not caret positions
1222 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1223 if (text
!= wxT(" ") && !text
.empty())
1230 while (i
< endPos
&& i
> -1)
1232 // i is in character, not caret positions
1233 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1234 if (text
.empty()) // End of paragraph, or maybe an image
1235 return wxMax(-1, i
- 1);
1236 else if (text
== wxT(" ") || text
.empty())
1240 // Convert to caret position
1241 return wxMax(-1, i
- 1);
1250 long i
= m_caretPosition
;
1252 // First skip white space
1253 while (i
< endPos
&& i
> -1)
1255 // i is in character, not caret positions
1256 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1257 if (text
.empty()) // End of paragraph, or maybe an image
1259 else if (text
== wxT(" ") || text
.empty())
1264 // Next skip current text to space
1265 while (i
< endPos
&& i
> -1)
1267 // i is in character, not caret positions
1268 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1269 if (text
!= wxT(" ") /* && !text.empty() */)
1282 /// Move n words left
1283 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1285 long pos
= FindNextWordPosition(-1);
1286 if (pos
!= m_caretPosition
)
1288 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1290 if (!ExtendSelection(m_caretPosition
, pos
, flags
))
1293 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1295 SetDefaultStyleToCursorStyle();
1305 /// Move n words right
1306 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1308 long pos
= FindNextWordPosition(1);
1309 if (pos
!= m_caretPosition
)
1311 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1313 if (!ExtendSelection(m_caretPosition
, pos
, flags
))
1316 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1318 SetDefaultStyleToCursorStyle();
1329 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1331 // Only do sizing optimization for large buffers
1332 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1334 m_fullLayoutRequired
= true;
1335 m_fullLayoutTime
= wxGetLocalTimeMillis();
1336 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1337 Layout(true /* onlyVisibleRect */);
1340 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1348 /// Idle-time processing
1349 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1351 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1353 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1355 m_fullLayoutRequired
= false;
1356 m_fullLayoutTime
= 0;
1357 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1358 ShowPosition(m_fullLayoutSavedPosition
);
1364 /// Set up scrollbars, e.g. after a resize
1365 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1370 if (GetBuffer().IsEmpty())
1372 SetScrollbars(0, 0, 0, 0, 0, 0);
1376 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1377 // of pixels. See e.g. wxVScrolledWindow for ideas.
1378 int pixelsPerUnit
= 5; // 10;
1379 wxSize clientSize
= GetClientSize();
1381 int maxHeight
= GetBuffer().GetCachedSize().y
;
1383 int unitsY
= maxHeight
/pixelsPerUnit
;
1385 int startX
= 0, startY
= 0;
1387 GetViewStart(& startX
, & startY
);
1389 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1390 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1392 // Move to previous scroll position if
1394 SetScrollbars(0, pixelsPerUnit
,
1396 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1399 /// Paint the background
1400 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1402 wxColour backgroundColour
= GetBackgroundColour();
1403 if (!backgroundColour
.Ok())
1404 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1406 // Clear the background
1407 dc
.SetBrush(wxBrush(backgroundColour
));
1408 dc
.SetPen(*wxTRANSPARENT_PEN
);
1409 wxRect
windowRect(GetClientSize());
1410 windowRect
.x
-= 2; windowRect
.y
-= 2;
1411 windowRect
.width
+= 4; windowRect
.height
+= 4;
1413 // We need to shift the rectangle to take into account
1414 // scrolling. Converting device to logical coordinates.
1415 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1416 dc
.DrawRectangle(windowRect
);
1419 /// Recreate buffer bitmap if necessary
1420 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1423 if (sz
== wxDefaultSize
)
1424 sz
= GetClientSize();
1426 if (sz
.x
< 1 || sz
.y
< 1)
1429 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1430 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1431 return m_bufferBitmap
.Ok();
1434 // ----------------------------------------------------------------------------
1435 // file IO functions
1436 // ----------------------------------------------------------------------------
1438 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1440 bool success
= GetBuffer().LoadFile(filename
, type
);
1442 m_filename
= filename
;
1445 SetInsertionPoint(0);
1448 SetupScrollbars(true);
1456 wxLogError(_("File couldn't be loaded."));
1462 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1464 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1465 if ( filenameToUse
.empty() )
1467 // what kind of message to give? is it an error or a program bug?
1468 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1473 if (GetBuffer().SaveFile(filenameToUse
, type
))
1475 m_filename
= filenameToUse
;
1483 wxLogError(_("The text couldn't be saved."));
1488 // ----------------------------------------------------------------------------
1489 // wxRichTextCtrl specific functionality
1490 // ----------------------------------------------------------------------------
1492 /// Add a new paragraph of text to the end of the buffer
1493 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1495 return GetBuffer().AddParagraph(text
);
1499 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1501 return GetBuffer().AddImage(image
);
1504 // ----------------------------------------------------------------------------
1505 // selection and ranges
1506 // ----------------------------------------------------------------------------
1508 void wxRichTextCtrl::SelectAll()
1510 SetSelection(0, GetLastPosition());
1511 m_selectionAnchor
= -1;
1515 void wxRichTextCtrl::SelectNone()
1517 SetSelection(-2, -2);
1518 m_selectionAnchor
= -2;
1521 wxString
wxRichTextCtrl::GetStringSelection() const
1524 GetSelection(&from
, &to
);
1526 return GetRange(from
, to
);
1529 // do the window-specific processing after processing the update event
1530 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1532 if ( event
.GetSetEnabled() )
1533 Enable(event
.GetEnabled());
1535 if ( event
.GetSetText() )
1537 if ( event
.GetText() != GetValue() )
1538 SetValue(event
.GetText());
1542 // ----------------------------------------------------------------------------
1544 // ----------------------------------------------------------------------------
1546 wxTextCtrlHitTestResult
1547 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1549 // implement in terms of the other overload as the native ports typically
1550 // can get the position and not (x, y) pair directly (although wxUniv
1551 // directly gets x and y -- and so overrides this method as well)
1553 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1555 if ( rc
!= wxTE_HT_UNKNOWN
)
1557 PositionToXY(pos
, x
, y
);
1563 wxTextCtrlHitTestResult
1564 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1567 wxClientDC
dc((wxRichTextCtrl
*) this);
1568 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1570 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1571 if (hit
== wxRICHTEXT_HITTEST_BEFORE
)
1572 return wxTE_HT_BEFORE
;
1573 else if (hit
== wxRICHTEXT_HITTEST_AFTER
)
1574 return wxTE_HT_BEYOND
;
1575 else if (hit
== wxRICHTEXT_HITTEST_ON
)
1576 return wxTE_HT_ON_TEXT
;
1578 return wxTE_HT_UNKNOWN
;
1581 // ----------------------------------------------------------------------------
1582 // set/get the controls text
1583 // ----------------------------------------------------------------------------
1585 wxString
wxRichTextCtrl::GetValue() const
1587 return GetBuffer().GetText();
1590 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1592 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1595 void wxRichTextCtrl::SetValue(const wxString
& value
)
1599 // if the text is long enough, it's faster to just set it instead of first
1600 // comparing it with the old one (chances are that it will be different
1601 // anyhow, this comparison is there to avoid flicker for small single-line
1602 // edit controls mostly)
1603 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1605 DoWriteText(value
, false /* not selection only */);
1607 // for compatibility, don't move the cursor when doing SetValue()
1608 SetInsertionPoint(0);
1612 // still send an event for consistency
1616 // we should reset the modified flag even if the value didn't really change
1618 // mark the control as being not dirty - we changed its text, not the
1623 void wxRichTextCtrl::WriteText(const wxString
& value
)
1628 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1630 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1633 void wxRichTextCtrl::AppendText(const wxString
& text
)
1635 SetInsertionPointEnd();
1640 /// Write an image at the current insertion point
1641 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1643 wxRichTextImageBlock imageBlock
;
1645 wxImage image2
= image
;
1646 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1647 return WriteImage(imageBlock
);
1652 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1654 wxRichTextImageBlock imageBlock
;
1657 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1658 return WriteImage(imageBlock
);
1663 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1665 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1668 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1672 wxRichTextImageBlock imageBlock
;
1674 wxImage image
= bitmap
.ConvertToImage();
1675 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1676 return WriteImage(imageBlock
);
1683 /// Insert a newline (actually paragraph) at the current insertion point.
1684 bool wxRichTextCtrl::Newline()
1686 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1690 // ----------------------------------------------------------------------------
1691 // Clipboard operations
1692 // ----------------------------------------------------------------------------
1694 void wxRichTextCtrl::Copy()
1698 wxRichTextRange range
= GetSelectionRange();
1699 GetBuffer().CopyToClipboard(range
);
1703 void wxRichTextCtrl::Cut()
1707 wxRichTextRange range
= GetSelectionRange();
1708 GetBuffer().CopyToClipboard(range
);
1710 DeleteSelectedContent();
1716 void wxRichTextCtrl::Paste()
1720 BeginBatchUndo(_("Paste"));
1722 long newPos
= m_caretPosition
;
1723 DeleteSelectedContent(& newPos
);
1725 GetBuffer().PasteFromClipboard(newPos
);
1731 void wxRichTextCtrl::DeleteSelection()
1733 if (CanDeleteSelection())
1735 DeleteSelectedContent();
1739 bool wxRichTextCtrl::HasSelection() const
1741 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1744 bool wxRichTextCtrl::CanCopy() const
1746 // Can copy if there's a selection
1747 return HasSelection();
1750 bool wxRichTextCtrl::CanCut() const
1752 return HasSelection() && IsEditable();
1755 bool wxRichTextCtrl::CanPaste() const
1757 if ( !IsEditable() )
1760 return GetBuffer().CanPasteFromClipboard();
1763 bool wxRichTextCtrl::CanDeleteSelection() const
1765 return HasSelection() && IsEditable();
1769 // ----------------------------------------------------------------------------
1771 // ----------------------------------------------------------------------------
1773 void wxRichTextCtrl::SetEditable(bool editable
)
1775 m_editable
= editable
;
1778 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1782 m_caretPosition
= pos
- 1;
1785 void wxRichTextCtrl::SetInsertionPointEnd()
1787 long pos
= GetLastPosition();
1788 SetInsertionPoint(pos
);
1791 long wxRichTextCtrl::GetInsertionPoint() const
1793 return m_caretPosition
+1;
1796 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1798 return GetBuffer().GetRange().GetEnd();
1801 // If the return values from and to are the same, there is no
1803 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1805 *from
= m_selectionRange
.GetStart();
1806 *to
= m_selectionRange
.GetEnd();
1809 bool wxRichTextCtrl::IsEditable() const
1814 // ----------------------------------------------------------------------------
1816 // ----------------------------------------------------------------------------
1818 void wxRichTextCtrl::SetSelection(long from
, long to
)
1820 // if from and to are both -1, it means (in wxWidgets) that all text should
1822 if ( (from
== -1) && (to
== -1) )
1825 to
= GetLastPosition();
1828 DoSetSelection(from
, to
);
1831 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1833 m_selectionAnchor
= from
;
1834 m_selectionRange
.SetRange(from
, to
);
1840 // ----------------------------------------------------------------------------
1842 // ----------------------------------------------------------------------------
1844 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1846 BeginBatchUndo(_("Replace"));
1848 DeleteSelectedContent();
1850 DoWriteText(value
, true /* selection only */);
1855 void wxRichTextCtrl::Remove(long from
, long to
)
1859 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1860 m_caretPosition
, // Current caret position
1861 from
, // New caret position
1869 bool wxRichTextCtrl::IsModified() const
1871 return m_buffer
.IsModified();
1874 void wxRichTextCtrl::MarkDirty()
1876 m_buffer
.Modify(true);
1879 void wxRichTextCtrl::DiscardEdits()
1881 m_buffer
.Modify(false);
1882 m_buffer
.GetCommandProcessor()->ClearCommands();
1885 int wxRichTextCtrl::GetNumberOfLines() const
1887 return GetBuffer().GetParagraphCount();
1890 // ----------------------------------------------------------------------------
1891 // Positions <-> coords
1892 // ----------------------------------------------------------------------------
1894 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1896 return GetBuffer().XYToPosition(x
, y
);
1899 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1901 return GetBuffer().PositionToXY(pos
, x
, y
);
1904 // ----------------------------------------------------------------------------
1906 // ----------------------------------------------------------------------------
1908 void wxRichTextCtrl::ShowPosition(long pos
)
1910 if (!IsPositionVisible(pos
))
1911 ScrollIntoView(pos
-1, WXK_DOWN
);
1914 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1916 return GetBuffer().GetParagraphLength(lineNo
);
1919 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1921 return GetBuffer().GetParagraphText(lineNo
);
1924 // ----------------------------------------------------------------------------
1926 // ----------------------------------------------------------------------------
1928 void wxRichTextCtrl::Undo()
1932 GetCommandProcessor()->Undo();
1936 void wxRichTextCtrl::Redo()
1940 GetCommandProcessor()->Redo();
1944 bool wxRichTextCtrl::CanUndo() const
1946 return GetCommandProcessor()->CanUndo();
1949 bool wxRichTextCtrl::CanRedo() const
1951 return GetCommandProcessor()->CanRedo();
1954 // ----------------------------------------------------------------------------
1955 // implemenation details
1956 // ----------------------------------------------------------------------------
1958 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
1960 SetValue(event
.GetString());
1961 GetEventHandler()->ProcessEvent(event
);
1964 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1966 // By default, load the first file into the text window.
1967 if (event
.GetNumberOfFiles() > 0)
1969 LoadFile(event
.GetFiles()[0]);
1973 // ----------------------------------------------------------------------------
1974 // text control event processing
1975 // ----------------------------------------------------------------------------
1977 bool wxRichTextCtrl::SendUpdateEvent()
1979 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1980 InitCommandEvent(event
);
1982 return GetEventHandler()->ProcessEvent(event
);
1985 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
1987 event
.SetEventObject((wxControlBase
*)this); // const_cast
1989 switch ( m_clientDataType
)
1991 case wxClientData_Void
:
1992 event
.SetClientData(GetClientData());
1995 case wxClientData_Object
:
1996 event
.SetClientObject(GetClientObject());
1999 case wxClientData_None
:
2006 wxSize
wxRichTextCtrl::DoGetBestSize() const
2008 return wxSize(10, 10);
2011 // ----------------------------------------------------------------------------
2012 // standard handlers for standard edit menu events
2013 // ----------------------------------------------------------------------------
2015 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2020 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2025 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2030 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2035 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2040 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2045 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2047 event
.Enable( CanCut() );
2050 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2052 event
.Enable( CanCopy() );
2055 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2057 event
.Enable( CanDeleteSelection() );
2060 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2062 event
.Enable( CanPaste() );
2065 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2067 event
.Enable( CanUndo() );
2068 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2071 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2073 event
.Enable( CanRedo() );
2074 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2077 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2082 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2084 event
.Enable(GetLastPosition() > 0);
2087 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2091 m_contextMenu
= new wxMenu
;
2092 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2093 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2094 m_contextMenu
->AppendSeparator();
2095 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2096 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2097 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2098 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2099 m_contextMenu
->AppendSeparator();
2100 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2102 PopupMenu(m_contextMenu
);
2106 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2108 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2111 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2113 return GetBuffer().SetStyle(range
, style
);
2116 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2118 return GetBuffer().SetDefaultStyle(style
);
2121 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2123 return GetBuffer().GetDefaultStyle();
2126 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2128 return GetBuffer().GetStyle(position
, style
);
2131 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2133 return GetBuffer().GetStyle(position
, style
);
2136 /// Set font, and also the buffer attributes
2137 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2139 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2140 wxControl::SetFont(font
);
2142 wxScrolledWindow::SetFont(font
);
2145 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2147 GetBuffer().SetBasicStyle(attr
);
2148 GetBuffer().SetDefaultStyle(attr
);
2153 /// Transform logical to physical
2154 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2157 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2162 /// Transform physical to logical
2163 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2166 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2171 /// Position the caret
2172 void wxRichTextCtrl::PositionCaret()
2175 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2177 wxPoint originalPt
= caretRect
.GetPosition();
2178 wxPoint pt
= GetPhysicalPoint(originalPt
);
2180 GetCaret()->Move(pt
);
2181 GetCaret()->SetSize(caretRect
.GetSize());
2185 /// Get the caret height and position for the given character position
2186 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2188 wxClientDC
dc(this);
2189 dc
.SetFont(GetFont());
2196 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2198 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2205 /// Gets the line for the visible caret position. If the caret is
2206 /// shown at the very end of the line, it means the next character is actually
2207 /// on the following line. So let's get the line we're expecting to find
2208 /// if this is the case.
2209 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2211 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2212 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2215 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2216 if (caretPosition
== lineRange
.GetStart()-1 &&
2217 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2219 if (!m_caretAtLineStart
)
2220 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2227 /// Move the caret to the given character position
2228 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2230 if (GetBuffer().GetDirty())
2233 if (pos
<= GetBuffer().GetRange().GetEnd())
2235 SetCaretPosition(pos
, showAtLineStart
);
2245 /// Layout the buffer: which we must do before certain operations, such as
2246 /// setting the caret position.
2247 bool wxRichTextCtrl::Layout(bool onlyVisibleRect
)
2249 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2251 wxRect
availableSpace(GetClientSize());
2252 if (availableSpace
.width
== 0)
2253 availableSpace
.width
= 10;
2254 if (availableSpace
.height
== 0)
2255 availableSpace
.height
= 10;
2257 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2258 if (onlyVisibleRect
)
2260 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2261 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2264 wxClientDC
dc(this);
2265 dc
.SetFont(GetFont());
2269 GetBuffer().Defragment();
2270 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2271 GetBuffer().Layout(dc
, availableSpace
, flags
);
2272 GetBuffer().SetDirty(false);
2281 /// Is all of the selection bold?
2282 bool wxRichTextCtrl::IsSelectionBold() const
2286 wxRichTextAttr attr
;
2287 wxRichTextRange range
= GetSelectionRange();
2288 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2289 attr
.SetFontWeight(wxBOLD
);
2291 return HasCharacterAttributes(range
, attr
);
2295 // If no selection, then we need to combine current style with default style
2296 // to see what the effect would be if we started typing.
2297 wxRichTextAttr attr
;
2298 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2299 if (GetStyle(GetCaretPosition()+1, attr
))
2301 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2302 return attr
.GetFontWeight() == wxBOLD
;
2308 /// Is all of the selection italics?
2309 bool wxRichTextCtrl::IsSelectionItalics() const
2313 wxRichTextRange range
= GetSelectionRange();
2314 wxRichTextAttr attr
;
2315 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2316 attr
.SetFontStyle(wxITALIC
);
2318 return HasCharacterAttributes(range
, attr
);
2322 // If no selection, then we need to combine current style with default style
2323 // to see what the effect would be if we started typing.
2324 wxRichTextAttr attr
;
2325 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2326 if (GetStyle(GetCaretPosition()+1, attr
))
2328 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2329 return attr
.GetFontStyle() == wxITALIC
;
2335 /// Is all of the selection underlined?
2336 bool wxRichTextCtrl::IsSelectionUnderlined() const
2340 wxRichTextRange range
= GetSelectionRange();
2341 wxRichTextAttr attr
;
2342 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2343 attr
.SetFontUnderlined(true);
2345 return HasCharacterAttributes(range
, attr
);
2349 // If no selection, then we need to combine current style with default style
2350 // to see what the effect would be if we started typing.
2351 wxRichTextAttr attr
;
2352 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2353 if (GetStyle(GetCaretPosition()+1, attr
))
2355 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2356 return attr
.GetFontUnderlined();
2362 /// Apply bold to the selection
2363 bool wxRichTextCtrl::ApplyBoldToSelection()
2365 wxRichTextAttr attr
;
2366 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2367 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2370 return SetStyle(GetSelectionRange(), attr
);
2372 SetDefaultStyle(attr
);
2376 /// Apply italic to the selection
2377 bool wxRichTextCtrl::ApplyItalicToSelection()
2379 wxRichTextAttr attr
;
2380 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2381 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2384 return SetStyle(GetSelectionRange(), attr
);
2386 SetDefaultStyle(attr
);
2390 /// Apply underline to the selection
2391 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2393 wxRichTextAttr attr
;
2394 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2395 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2398 return SetStyle(GetSelectionRange(), attr
);
2400 SetDefaultStyle(attr
);
2404 /// Is all of the selection aligned according to the specified flag?
2405 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2409 wxRichTextRange range
= GetSelectionRange();
2410 wxRichTextAttr attr
;
2411 attr
.SetAlignment(alignment
);
2413 return HasParagraphAttributes(range
, attr
);
2417 // If no selection, then we need to get information from the current paragraph.
2418 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2420 return para
->GetAttributes().GetAlignment() == alignment
;
2425 /// Apply alignment to the selection
2426 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2428 wxRichTextAttr attr
;
2429 attr
.SetAlignment(alignment
);
2431 return SetStyle(GetSelectionRange(), attr
);
2434 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2436 return SetStyle(para
->GetRange(), attr
);
2441 /// Sets the default style to the style under the cursor
2442 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2445 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2447 if (GetStyle(GetCaretPosition(), attr
))
2449 SetDefaultStyle(attr
);
2456 /// Returns the first visible position in the current view
2457 long wxRichTextCtrl::GetFirstVisiblePosition() const
2459 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2461 return line
->GetAbsoluteRange().GetStart();