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_SCROLLWIN(wxRichTextCtrl::OnScroll
)
61 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
62 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
63 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
64 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
65 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
66 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
67 EVT_CHAR(wxRichTextCtrl::OnChar
)
68 EVT_SIZE(wxRichTextCtrl::OnSize
)
69 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
70 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
71 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
73 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
74 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
76 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
77 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
79 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
80 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
82 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
83 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
85 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
86 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
88 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
89 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
91 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
92 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
99 wxRichTextCtrl::wxRichTextCtrl()
100 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
101 : wxScrollHelper(this)
107 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
108 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
109 : wxScrollHelper(this)
113 Create(parent
, id
, pos
, size
, style
);
117 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
119 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
120 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
124 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
131 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
134 GetBuffer().SetRichTextCtrl(this);
136 wxTextAttrEx attributes
;
137 attributes
.SetFont(GetFont());
138 attributes
.SetTextColour(*wxBLACK
);
139 attributes
.SetBackgroundColour(*wxWHITE
);
140 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
141 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
143 SetDefaultStyle(attributes
);
144 SetBasicStyle(attributes
);
146 SetBackgroundColour(*wxWHITE
);
147 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
149 // Tell the sizers to use the given or best size
150 SetBestFittingSize(size
);
153 RecreateBuffer(size
);
155 SetCursor(wxCursor(wxCURSOR_IBEAM
));
160 wxRichTextCtrl::~wxRichTextCtrl()
162 delete m_contextMenu
;
165 /// Member initialisation
166 void wxRichTextCtrl::Init()
169 m_contextMenu
= NULL
;
171 m_caretPosition
= -1;
172 m_selectionRange
.SetRange(-2, -2);
173 m_selectionAnchor
= -2;
175 m_caretAtLineStart
= false;
177 m_fullLayoutRequired
= false;
178 m_fullLayoutTime
= 0;
179 m_fullLayoutSavedPosition
= 0;
180 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
183 /// Call Freeze to prevent refresh
184 void wxRichTextCtrl::Freeze()
189 /// Call Thaw to refresh
190 void wxRichTextCtrl::Thaw(bool refresh
)
194 if (m_freezeCount
== 0 && refresh
)
202 void wxRichTextCtrl::Clear()
205 m_buffer
.SetDirty(true);
206 m_caretPosition
= -1;
207 m_caretAtLineStart
= false;
208 m_selectionRange
.SetRange(-2, -2);
210 if (m_freezeCount
== 0)
219 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
222 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
223 //wxLogDebug(wxT("OnPaint"));
227 if (m_freezeCount
> 0)
230 dc
.SetFont(GetFont());
232 // Paint the background
235 wxRegion dirtyRegion
= GetUpdateRegion();
237 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
238 wxRect
availableSpace(GetClientSize());
239 if (GetBuffer().GetDirty())
241 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
242 GetBuffer().SetDirty(false);
246 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
251 // Empty implementation, to prevent flicker
252 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
256 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
258 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
267 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
276 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
282 dc
.SetFont(GetFont());
285 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
287 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
289 m_dragStart
= event
.GetLogicalPosition(dc
);
295 bool caretAtLineStart
= false;
297 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
299 // If we're at the start of a line (but not first in para)
300 // then we should keep the caret showing at the start of the line
301 // by showing the m_caretAtLineStart flag.
302 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
303 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
305 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
306 caretAtLineStart
= true;
310 MoveCaret(position
, caretAtLineStart
);
311 SetDefaultStyleToCursorStyle();
314 wxWindow
* p
= GetParent();
315 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
318 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
321 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
322 frame
->SetStatusText(msg
, 1);
331 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
336 if (GetCapture() == this)
342 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
344 if (!event
.Dragging())
352 dc
.SetFont(GetFont());
355 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
356 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
358 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
360 // TODO: test closeness
362 bool caretAtLineStart
= false;
364 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
366 // If we're at the start of a line (but not first in para)
367 // then we should keep the caret showing at the start of the line
368 // by showing the m_caretAtLineStart flag.
369 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
370 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
372 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
373 caretAtLineStart
= true;
377 if (m_caretPosition
!= position
)
379 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
381 MoveCaret(position
, caretAtLineStart
);
382 SetDefaultStyleToCursorStyle();
391 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
397 /// Left-double-click
398 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
404 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
410 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
413 if (event
.ControlDown())
414 flags
|= wxRICHTEXT_CTRL_DOWN
;
415 if (event
.ShiftDown())
416 flags
|= wxRICHTEXT_SHIFT_DOWN
;
418 flags
|= wxRICHTEXT_ALT_DOWN
;
420 if (event
.GetKeyCode() == WXK_LEFT
||
421 event
.GetKeyCode() == WXK_RIGHT
||
422 event
.GetKeyCode() == WXK_UP
||
423 event
.GetKeyCode() == WXK_DOWN
||
424 event
.GetKeyCode() == WXK_HOME
||
425 event
.GetKeyCode() == WXK_PAGEUP
||
426 event
.GetKeyCode() == WXK_PAGEDOWN
||
427 event
.GetKeyCode() == WXK_PRIOR
||
428 event
.GetKeyCode() == WXK_NEXT
||
429 event
.GetKeyCode() == WXK_END
)
431 Navigate(event
.GetKeyCode(), flags
);
433 else if (event
.GetKeyCode() == WXK_RETURN
)
435 BeginBatchUndo(_("Insert Text"));
437 long newPos
= m_caretPosition
;
439 DeleteSelectedContent(& newPos
);
441 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
443 wxRichTextEvent
cmdEvent(
444 wxEVT_COMMAND_RICHTEXT_RETURN
,
446 cmdEvent
.SetEventObject(this);
447 cmdEvent
.SetFlags(flags
);
448 GetEventHandler()->ProcessEvent(cmdEvent
);
451 SetDefaultStyleToCursorStyle();
453 else if (event
.GetKeyCode() == WXK_BACK
)
455 BeginBatchUndo(_("Delete Text"));
457 // Submit range in character positions, which are greater than caret positions,
458 // so subtract 1 for deleted character and add 1 for conversion to character position.
459 if (m_caretPosition
> -1 && !HasSelection())
461 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
462 m_caretPosition
, // Current caret position
463 m_caretPosition
-1, // New caret position
467 DeleteSelectedContent();
471 // Shouldn't this be in Do()?
472 if (GetLastPosition() == -1)
476 m_caretPosition
= -1;
478 SetDefaultStyleToCursorStyle();
482 else if (event
.GetKeyCode() == WXK_DELETE
)
484 BeginBatchUndo(_("Delete Text"));
486 // Submit range in character positions, which are greater than caret positions,
487 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
489 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
490 m_caretPosition
, // Current caret position
491 m_caretPosition
+1, // New caret position
495 DeleteSelectedContent();
499 // Shouldn't this be in Do()?
500 if (GetLastPosition() == -1)
504 m_caretPosition
= -1;
506 SetDefaultStyleToCursorStyle();
511 BeginBatchUndo(_("Insert Text"));
513 long newPos
= m_caretPosition
;
514 DeleteSelectedContent(& newPos
);
516 wxString str
= (wxChar
) event
.GetKeyCode();
517 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
521 SetDefaultStyleToCursorStyle();
529 /// Delete content if there is a selection, e.g. when pressing a key.
530 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
534 long pos
= m_selectionRange
.GetStart();
535 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
536 m_caretPosition
, // Current caret position
537 pos
, // New caret position
539 m_selectionRange
.SetRange(-2, -2);
549 /// Keyboard navigation
553 Left: left one character
554 Right: right one character
557 Ctrl-Left: left one word
558 Ctrl-Right: right one word
559 Ctrl-Up: previous paragraph start
560 Ctrl-Down: next start of paragraph
563 Ctrl-Home: start of document
564 Ctrl-End: end of document
566 Page-Down: Down a screen
570 Ctrl-Alt-PgUp: Start of window
571 Ctrl-Alt-PgDn: End of window
572 F8: Start selection mode
573 Esc: End selection mode
575 Adding Shift does the above but starts/extends selection.
580 bool wxRichTextCtrl::Navigate(int keyCode
, int flags
)
582 bool success
= false;
585 if (keyCode
== WXK_RIGHT
)
587 if (flags
& wxRICHTEXT_CTRL_DOWN
)
588 success
= WordRight(1, flags
);
590 success
= MoveRight(1, flags
);
592 else if (keyCode
== WXK_LEFT
)
594 if (flags
& wxRICHTEXT_CTRL_DOWN
)
595 success
= WordLeft(1, flags
);
597 success
= MoveLeft(1, flags
);
599 else if (keyCode
== WXK_UP
)
601 if (flags
& wxRICHTEXT_CTRL_DOWN
)
602 success
= MoveToParagraphStart(flags
);
604 success
= MoveUp(1, flags
);
606 else if (keyCode
== WXK_DOWN
)
608 if (flags
& wxRICHTEXT_CTRL_DOWN
)
609 success
= MoveToParagraphEnd(flags
);
611 success
= MoveDown(1, flags
);
613 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_PRIOR
)
615 success
= PageUp(1, flags
);
617 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NEXT
)
619 success
= PageDown(1, flags
);
621 else if (keyCode
== WXK_HOME
)
623 if (flags
& wxRICHTEXT_CTRL_DOWN
)
624 success
= MoveHome(flags
);
626 success
= MoveToLineStart(flags
);
628 else if (keyCode
== WXK_END
)
630 if (flags
& wxRICHTEXT_CTRL_DOWN
)
631 success
= MoveEnd(flags
);
633 success
= MoveToLineEnd(flags
);
638 ScrollIntoView(m_caretPosition
, keyCode
);
639 SetDefaultStyleToCursorStyle();
647 /// Extend the selection. Selections are in caret positions.
648 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
650 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
652 // If not currently selecting, start selecting
653 if (m_selectionRange
.GetStart() == -2)
655 m_selectionAnchor
= oldPos
;
658 m_selectionRange
.SetRange(newPos
+1, oldPos
);
660 m_selectionRange
.SetRange(oldPos
+1, newPos
);
664 // Always ensure that the selection range start is greater than
666 if (newPos
> m_selectionAnchor
)
667 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
669 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
672 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
674 wxLogDebug(wxT("Strange selection range"));
683 /// Scroll into view, returning true if we scrolled.
684 /// This takes a _caret_ position.
685 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
687 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
693 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
696 GetViewStart(& startX
, & startY
);
698 startY
= startY
* ppuY
;
701 GetVirtualSize(& sx
, & sy
);
706 wxRect rect
= line
->GetRect();
708 bool scrolled
= false;
710 wxSize clientSize
= GetClientSize();
713 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_NEXT
|| keyCode
== WXK_PAGEDOWN
)
715 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
717 // Make it scroll so this item is at the bottom
719 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
720 y
= (int) (0.5 + y
/ppuY
);
724 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
728 else if (rect
.y
< startY
)
730 // Make it scroll so this item is at the top
733 y
= (int) (0.5 + y
/ppuY
);
737 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
743 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PRIOR
|| keyCode
== WXK_PAGEUP
)
747 // Make it scroll so this item is at the top
750 y
= (int) (0.5 + y
/ppuY
);
754 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
758 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
760 // Make it scroll so this item is at the bottom
762 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
763 y
= (int) (0.5 + y
/ppuY
);
767 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
777 /// Is the given position visible on the screen?
778 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
780 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
786 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
789 GetViewStart(& startX
, & startY
);
791 startY
= startY
* ppuY
;
794 GetVirtualSize(& sx
, & sy
);
799 wxRect rect
= line
->GetRect();
801 wxSize clientSize
= GetClientSize();
803 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
806 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
808 m_caretPosition
= position
;
809 m_caretAtLineStart
= showAtLineStart
;
812 /// Move caret one visual step forward: this may mean setting a flag
813 /// and keeping the same position if we're going from the end of one line
814 /// to the start of the next, which may be the exact same caret position.
815 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
817 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
819 // Only do the check if we're not at the end of the paragraph (where things work OK
821 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
823 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
827 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
829 // We're at the end of a line. See whether we need to
830 // stay at the same actual caret position but change visual
832 if (oldPosition
== lineRange
.GetEnd())
834 if (m_caretAtLineStart
)
836 // We're already at the start of the line, so actually move on now.
837 m_caretPosition
= oldPosition
+ 1;
838 m_caretAtLineStart
= false;
842 // We're showing at the end of the line, so keep to
843 // the same position but indicate that we're to show
844 // at the start of the next line.
845 m_caretPosition
= oldPosition
;
846 m_caretAtLineStart
= true;
848 SetDefaultStyleToCursorStyle();
854 SetDefaultStyleToCursorStyle();
857 /// Move caret one visual step backward: this may mean setting a flag
858 /// and keeping the same position if we're going from the end of one line
859 /// to the start of the next, which may be the exact same caret position.
860 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
862 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
864 // Only do the check if we're not at the start of the paragraph (where things work OK
866 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
868 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
872 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
874 // We're at the start of a line. See whether we need to
875 // stay at the same actual caret position but change visual
877 if (oldPosition
== lineRange
.GetStart())
879 m_caretPosition
= oldPosition
-1;
880 m_caretAtLineStart
= true;
883 else if (oldPosition
== lineRange
.GetEnd())
885 if (m_caretAtLineStart
)
887 // We're at the start of the line, so keep the same caret position
888 // but clear the start-of-line flag.
889 m_caretPosition
= oldPosition
;
890 m_caretAtLineStart
= false;
894 // We're showing at the end of the line, so go back
895 // to the previous character position.
896 m_caretPosition
= oldPosition
- 1;
898 SetDefaultStyleToCursorStyle();
904 SetDefaultStyleToCursorStyle();
908 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
910 long endPos
= GetBuffer().GetRange().GetEnd();
912 if (m_caretPosition
+ noPositions
< endPos
)
914 long oldPos
= m_caretPosition
;
915 long newPos
= m_caretPosition
+ noPositions
;
917 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
921 // Determine by looking at oldPos and m_caretPosition whether
922 // we moved from the end of a line to the start of the next line, in which case
923 // we want to adjust the caret position such that it is positioned at the
924 // start of the next line, rather than jumping past the first character of the
926 if (noPositions
== 1 && !extendSel
)
927 MoveCaretForward(oldPos
);
929 SetCaretPosition(newPos
);
932 SetDefaultStyleToCursorStyle();
943 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
947 if (m_caretPosition
> startPos
- noPositions
+ 1)
949 long oldPos
= m_caretPosition
;
950 long newPos
= m_caretPosition
- noPositions
;
951 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
955 if (noPositions
== 1 && !extendSel
)
956 MoveCaretBack(oldPos
);
958 SetCaretPosition(newPos
);
961 SetDefaultStyleToCursorStyle();
972 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
974 return MoveDown(- noLines
, flags
);
978 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
983 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
984 wxPoint pt
= GetCaret()->GetPosition();
985 long newLine
= lineNumber
+ noLines
;
987 if (lineNumber
!= -1)
991 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
993 if (newLine
> lastLine
)
1003 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1006 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1012 wxClientDC
dc(this);
1014 dc
.SetFont(GetFont());
1016 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1018 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1020 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1021 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1022 // so we view the caret at the start of the line.
1023 bool caretLineStart
= false;
1024 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1026 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1027 wxRichTextRange lineRange
;
1029 lineRange
= thisLine
->GetAbsoluteRange();
1031 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1034 caretLineStart
= true;
1038 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1039 if (para
&& para
->GetRange().GetStart() == newPos
)
1044 long newSelEnd
= newPos
;
1046 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1050 SetCaretPosition(newPos
, caretLineStart
);
1052 SetDefaultStyleToCursorStyle();
1062 /// Move to the end of the paragraph
1063 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1065 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1068 long newPos
= para
->GetRange().GetEnd() - 1;
1069 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1073 SetCaretPosition(newPos
);
1075 SetDefaultStyleToCursorStyle();
1085 /// Move to the start of the paragraph
1086 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1088 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1091 long newPos
= para
->GetRange().GetStart() - 1;
1092 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1096 SetCaretPosition(newPos
);
1098 SetDefaultStyleToCursorStyle();
1108 /// Move to the end of the line
1109 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1111 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1115 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1116 long newPos
= lineRange
.GetEnd();
1117 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1121 SetCaretPosition(newPos
);
1123 SetDefaultStyleToCursorStyle();
1133 /// Move to the start of the line
1134 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1136 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1139 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1140 long newPos
= lineRange
.GetStart()-1;
1142 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1146 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1148 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1150 SetDefaultStyleToCursorStyle();
1160 /// Move to the start of the buffer
1161 bool wxRichTextCtrl::MoveHome(int flags
)
1163 if (m_caretPosition
!= -1)
1165 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1169 SetCaretPosition(-1);
1171 SetDefaultStyleToCursorStyle();
1181 /// Move to the end of the buffer
1182 bool wxRichTextCtrl::MoveEnd(int flags
)
1184 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1186 if (m_caretPosition
!= endPos
)
1188 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1192 SetCaretPosition(endPos
);
1194 SetDefaultStyleToCursorStyle();
1204 /// Move noPages pages up
1205 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1207 return PageDown(- noPages
, flags
);
1210 /// Move noPages pages down
1211 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1213 // Calculate which line occurs noPages * screen height further down.
1214 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1217 wxSize clientSize
= GetClientSize();
1218 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1220 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1223 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1224 long pos
= lineRange
.GetStart()-1;
1225 if (pos
!= m_caretPosition
)
1227 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1229 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1233 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1235 SetDefaultStyleToCursorStyle();
1247 // Finds the caret position for the next word
1248 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1250 long endPos
= GetBuffer().GetRange().GetEnd();
1254 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1256 // First skip current text to space
1257 while (i
< endPos
&& i
> -1)
1259 // i is in character, not caret positions
1260 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1261 if (text
!= wxT(" ") && !text
.empty())
1268 while (i
< endPos
&& i
> -1)
1270 // i is in character, not caret positions
1271 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1272 if (text
.empty()) // End of paragraph, or maybe an image
1273 return wxMax(-1, i
- 1);
1274 else if (text
== wxT(" ") || text
.empty())
1278 // Convert to caret position
1279 return wxMax(-1, i
- 1);
1288 long i
= m_caretPosition
;
1290 // First skip white space
1291 while (i
< endPos
&& i
> -1)
1293 // i is in character, not caret positions
1294 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1295 if (text
.empty()) // End of paragraph, or maybe an image
1297 else if (text
== wxT(" ") || text
.empty())
1302 // Next skip current text to space
1303 while (i
< endPos
&& i
> -1)
1305 // i is in character, not caret positions
1306 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1307 if (text
!= wxT(" ") /* && !text.empty() */)
1320 /// Move n words left
1321 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1323 long pos
= FindNextWordPosition(-1);
1324 if (pos
!= m_caretPosition
)
1326 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1328 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1332 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1334 SetDefaultStyleToCursorStyle();
1344 /// Move n words right
1345 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1347 long pos
= FindNextWordPosition(1);
1348 if (pos
!= m_caretPosition
)
1350 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1352 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1356 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1358 SetDefaultStyleToCursorStyle();
1369 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1371 // Only do sizing optimization for large buffers
1372 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1374 m_fullLayoutRequired
= true;
1375 m_fullLayoutTime
= wxGetLocalTimeMillis();
1376 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1377 Layout(true /* onlyVisibleRect */);
1380 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1388 /// Idle-time processing
1389 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1391 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1393 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1395 m_fullLayoutRequired
= false;
1396 m_fullLayoutTime
= 0;
1397 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1398 ShowPosition(m_fullLayoutSavedPosition
);
1405 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1411 /// Set up scrollbars, e.g. after a resize
1412 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1417 if (GetBuffer().IsEmpty())
1419 SetScrollbars(0, 0, 0, 0, 0, 0);
1423 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1424 // of pixels. See e.g. wxVScrolledWindow for ideas.
1425 int pixelsPerUnit
= 5; // 10;
1426 wxSize clientSize
= GetClientSize();
1428 int maxHeight
= GetBuffer().GetCachedSize().y
;
1430 int unitsY
= maxHeight
/pixelsPerUnit
;
1432 int startX
= 0, startY
= 0;
1434 GetViewStart(& startX
, & startY
);
1436 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1437 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1439 // Move to previous scroll position if
1441 SetScrollbars(0, pixelsPerUnit
,
1443 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1446 /// Paint the background
1447 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1449 wxColour backgroundColour
= GetBackgroundColour();
1450 if (!backgroundColour
.Ok())
1451 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1453 // Clear the background
1454 dc
.SetBrush(wxBrush(backgroundColour
));
1455 dc
.SetPen(*wxTRANSPARENT_PEN
);
1456 wxRect
windowRect(GetClientSize());
1457 windowRect
.x
-= 2; windowRect
.y
-= 2;
1458 windowRect
.width
+= 4; windowRect
.height
+= 4;
1460 // We need to shift the rectangle to take into account
1461 // scrolling. Converting device to logical coordinates.
1462 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1463 dc
.DrawRectangle(windowRect
);
1466 /// Recreate buffer bitmap if necessary
1467 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1470 if (sz
== wxDefaultSize
)
1471 sz
= GetClientSize();
1473 if (sz
.x
< 1 || sz
.y
< 1)
1476 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1477 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1478 return m_bufferBitmap
.Ok();
1481 // ----------------------------------------------------------------------------
1482 // file IO functions
1483 // ----------------------------------------------------------------------------
1485 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1487 bool success
= GetBuffer().LoadFile(filename
, type
);
1489 m_filename
= filename
;
1492 SetInsertionPoint(0);
1495 SetupScrollbars(true);
1503 wxLogError(_("File couldn't be loaded."));
1509 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1511 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1512 if ( filenameToUse
.empty() )
1514 // what kind of message to give? is it an error or a program bug?
1515 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1520 if (GetBuffer().SaveFile(filenameToUse
, type
))
1522 m_filename
= filenameToUse
;
1530 wxLogError(_("The text couldn't be saved."));
1535 // ----------------------------------------------------------------------------
1536 // wxRichTextCtrl specific functionality
1537 // ----------------------------------------------------------------------------
1539 /// Add a new paragraph of text to the end of the buffer
1540 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1542 return GetBuffer().AddParagraph(text
);
1546 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1548 return GetBuffer().AddImage(image
);
1551 // ----------------------------------------------------------------------------
1552 // selection and ranges
1553 // ----------------------------------------------------------------------------
1555 void wxRichTextCtrl::SelectAll()
1557 SetSelection(0, GetLastPosition());
1558 m_selectionAnchor
= -1;
1562 void wxRichTextCtrl::SelectNone()
1564 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1565 SetSelection(-2, -2);
1566 m_selectionAnchor
= -2;
1569 wxString
wxRichTextCtrl::GetStringSelection() const
1572 GetSelection(&from
, &to
);
1574 return GetRange(from
, to
);
1577 // do the window-specific processing after processing the update event
1578 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1580 if ( event
.GetSetEnabled() )
1581 Enable(event
.GetEnabled());
1583 if ( event
.GetSetText() )
1585 if ( event
.GetText() != GetValue() )
1586 SetValue(event
.GetText());
1590 // ----------------------------------------------------------------------------
1592 // ----------------------------------------------------------------------------
1594 wxTextCtrlHitTestResult
1595 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1597 // implement in terms of the other overload as the native ports typically
1598 // can get the position and not (x, y) pair directly (although wxUniv
1599 // directly gets x and y -- and so overrides this method as well)
1601 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1603 if ( rc
!= wxTE_HT_UNKNOWN
)
1605 PositionToXY(pos
, x
, y
);
1611 wxTextCtrlHitTestResult
1612 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1615 wxClientDC
dc((wxRichTextCtrl
*) this);
1616 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1618 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1619 if (hit
== wxRICHTEXT_HITTEST_BEFORE
)
1620 return wxTE_HT_BEFORE
;
1621 else if (hit
== wxRICHTEXT_HITTEST_AFTER
)
1622 return wxTE_HT_BEYOND
;
1623 else if (hit
== wxRICHTEXT_HITTEST_ON
)
1624 return wxTE_HT_ON_TEXT
;
1626 return wxTE_HT_UNKNOWN
;
1629 // ----------------------------------------------------------------------------
1630 // set/get the controls text
1631 // ----------------------------------------------------------------------------
1633 wxString
wxRichTextCtrl::GetValue() const
1635 return GetBuffer().GetText();
1638 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1640 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1643 void wxRichTextCtrl::SetValue(const wxString
& value
)
1647 // if the text is long enough, it's faster to just set it instead of first
1648 // comparing it with the old one (chances are that it will be different
1649 // anyhow, this comparison is there to avoid flicker for small single-line
1650 // edit controls mostly)
1651 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1653 DoWriteText(value
, false /* not selection only */);
1655 // for compatibility, don't move the cursor when doing SetValue()
1656 SetInsertionPoint(0);
1660 // still send an event for consistency
1664 // we should reset the modified flag even if the value didn't really change
1666 // mark the control as being not dirty - we changed its text, not the
1671 void wxRichTextCtrl::WriteText(const wxString
& value
)
1676 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1678 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1681 void wxRichTextCtrl::AppendText(const wxString
& text
)
1683 SetInsertionPointEnd();
1688 /// Write an image at the current insertion point
1689 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1691 wxRichTextImageBlock imageBlock
;
1693 wxImage image2
= image
;
1694 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1695 return WriteImage(imageBlock
);
1700 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1702 wxRichTextImageBlock imageBlock
;
1705 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1706 return WriteImage(imageBlock
);
1711 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1713 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1716 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1720 wxRichTextImageBlock imageBlock
;
1722 wxImage image
= bitmap
.ConvertToImage();
1723 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1724 return WriteImage(imageBlock
);
1731 /// Insert a newline (actually paragraph) at the current insertion point.
1732 bool wxRichTextCtrl::Newline()
1734 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1738 // ----------------------------------------------------------------------------
1739 // Clipboard operations
1740 // ----------------------------------------------------------------------------
1742 void wxRichTextCtrl::Copy()
1746 wxRichTextRange range
= GetSelectionRange();
1747 GetBuffer().CopyToClipboard(range
);
1751 void wxRichTextCtrl::Cut()
1755 wxRichTextRange range
= GetSelectionRange();
1756 GetBuffer().CopyToClipboard(range
);
1758 DeleteSelectedContent();
1764 void wxRichTextCtrl::Paste()
1768 BeginBatchUndo(_("Paste"));
1770 long newPos
= m_caretPosition
;
1771 DeleteSelectedContent(& newPos
);
1773 GetBuffer().PasteFromClipboard(newPos
);
1779 void wxRichTextCtrl::DeleteSelection()
1781 if (CanDeleteSelection())
1783 DeleteSelectedContent();
1787 bool wxRichTextCtrl::HasSelection() const
1789 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1792 bool wxRichTextCtrl::CanCopy() const
1794 // Can copy if there's a selection
1795 return HasSelection();
1798 bool wxRichTextCtrl::CanCut() const
1800 return HasSelection() && IsEditable();
1803 bool wxRichTextCtrl::CanPaste() const
1805 if ( !IsEditable() )
1808 return GetBuffer().CanPasteFromClipboard();
1811 bool wxRichTextCtrl::CanDeleteSelection() const
1813 return HasSelection() && IsEditable();
1817 // ----------------------------------------------------------------------------
1819 // ----------------------------------------------------------------------------
1821 void wxRichTextCtrl::SetEditable(bool editable
)
1823 m_editable
= editable
;
1826 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1830 m_caretPosition
= pos
- 1;
1833 void wxRichTextCtrl::SetInsertionPointEnd()
1835 long pos
= GetLastPosition();
1836 SetInsertionPoint(pos
);
1839 long wxRichTextCtrl::GetInsertionPoint() const
1841 return m_caretPosition
+1;
1844 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1846 return GetBuffer().GetRange().GetEnd();
1849 // If the return values from and to are the same, there is no
1851 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1853 *from
= m_selectionRange
.GetStart();
1854 *to
= m_selectionRange
.GetEnd();
1857 bool wxRichTextCtrl::IsEditable() const
1862 // ----------------------------------------------------------------------------
1864 // ----------------------------------------------------------------------------
1866 void wxRichTextCtrl::SetSelection(long from
, long to
)
1868 // if from and to are both -1, it means (in wxWidgets) that all text should
1870 if ( (from
== -1) && (to
== -1) )
1873 to
= GetLastPosition();
1876 DoSetSelection(from
, to
);
1879 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1881 m_selectionAnchor
= from
;
1882 m_selectionRange
.SetRange(from
, to
);
1887 // ----------------------------------------------------------------------------
1889 // ----------------------------------------------------------------------------
1891 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1893 BeginBatchUndo(_("Replace"));
1895 DeleteSelectedContent();
1897 DoWriteText(value
, true /* selection only */);
1902 void wxRichTextCtrl::Remove(long from
, long to
)
1906 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1907 m_caretPosition
, // Current caret position
1908 from
, // New caret position
1916 bool wxRichTextCtrl::IsModified() const
1918 return m_buffer
.IsModified();
1921 void wxRichTextCtrl::MarkDirty()
1923 m_buffer
.Modify(true);
1926 void wxRichTextCtrl::DiscardEdits()
1928 m_buffer
.Modify(false);
1929 m_buffer
.GetCommandProcessor()->ClearCommands();
1932 int wxRichTextCtrl::GetNumberOfLines() const
1934 return GetBuffer().GetParagraphCount();
1937 // ----------------------------------------------------------------------------
1938 // Positions <-> coords
1939 // ----------------------------------------------------------------------------
1941 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1943 return GetBuffer().XYToPosition(x
, y
);
1946 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1948 return GetBuffer().PositionToXY(pos
, x
, y
);
1951 // ----------------------------------------------------------------------------
1953 // ----------------------------------------------------------------------------
1955 void wxRichTextCtrl::ShowPosition(long pos
)
1957 if (!IsPositionVisible(pos
))
1958 ScrollIntoView(pos
-1, WXK_DOWN
);
1961 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1963 return GetBuffer().GetParagraphLength(lineNo
);
1966 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1968 return GetBuffer().GetParagraphText(lineNo
);
1971 // ----------------------------------------------------------------------------
1973 // ----------------------------------------------------------------------------
1975 void wxRichTextCtrl::Undo()
1979 GetCommandProcessor()->Undo();
1983 void wxRichTextCtrl::Redo()
1987 GetCommandProcessor()->Redo();
1991 bool wxRichTextCtrl::CanUndo() const
1993 return GetCommandProcessor()->CanUndo();
1996 bool wxRichTextCtrl::CanRedo() const
1998 return GetCommandProcessor()->CanRedo();
2001 // ----------------------------------------------------------------------------
2002 // implemenation details
2003 // ----------------------------------------------------------------------------
2005 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2007 SetValue(event
.GetString());
2008 GetEventHandler()->ProcessEvent(event
);
2011 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2013 // By default, load the first file into the text window.
2014 if (event
.GetNumberOfFiles() > 0)
2016 LoadFile(event
.GetFiles()[0]);
2020 // ----------------------------------------------------------------------------
2021 // text control event processing
2022 // ----------------------------------------------------------------------------
2024 bool wxRichTextCtrl::SendUpdateEvent()
2026 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2027 InitCommandEvent(event
);
2029 return GetEventHandler()->ProcessEvent(event
);
2032 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2034 event
.SetEventObject((wxControlBase
*)this); // const_cast
2036 switch ( m_clientDataType
)
2038 case wxClientData_Void
:
2039 event
.SetClientData(GetClientData());
2042 case wxClientData_Object
:
2043 event
.SetClientObject(GetClientObject());
2046 case wxClientData_None
:
2053 wxSize
wxRichTextCtrl::DoGetBestSize() const
2055 return wxSize(10, 10);
2058 // ----------------------------------------------------------------------------
2059 // standard handlers for standard edit menu events
2060 // ----------------------------------------------------------------------------
2062 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2067 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2072 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2077 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2082 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2087 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2092 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2094 event
.Enable( CanCut() );
2097 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2099 event
.Enable( CanCopy() );
2102 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2104 event
.Enable( CanDeleteSelection() );
2107 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2109 event
.Enable( CanPaste() );
2112 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2114 event
.Enable( CanUndo() );
2115 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2118 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2120 event
.Enable( CanRedo() );
2121 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2124 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2129 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2131 event
.Enable(GetLastPosition() > 0);
2134 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2138 m_contextMenu
= new wxMenu
;
2139 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2140 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2141 m_contextMenu
->AppendSeparator();
2142 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2143 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2144 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2145 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2146 m_contextMenu
->AppendSeparator();
2147 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2149 PopupMenu(m_contextMenu
);
2153 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2155 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2158 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2160 return GetBuffer().SetStyle(range
, style
);
2163 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2165 return GetBuffer().SetDefaultStyle(style
);
2168 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2170 return GetBuffer().GetDefaultStyle();
2173 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2175 return GetBuffer().GetStyle(position
, style
);
2178 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2180 return GetBuffer().GetStyle(position
, style
);
2183 /// Set font, and also the buffer attributes
2184 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2186 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2187 wxControl::SetFont(font
);
2189 wxScrolledWindow::SetFont(font
);
2192 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2194 GetBuffer().SetBasicStyle(attr
);
2195 GetBuffer().SetDefaultStyle(attr
);
2200 /// Transform logical to physical
2201 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2204 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2209 /// Transform physical to logical
2210 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2213 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2218 /// Position the caret
2219 void wxRichTextCtrl::PositionCaret()
2224 //wxLogDebug(wxT("PositionCaret"));
2227 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2229 wxPoint originalPt
= caretRect
.GetPosition();
2230 wxPoint pt
= GetPhysicalPoint(originalPt
);
2231 if (GetCaret()->GetPosition() != pt
)
2233 GetCaret()->Move(pt
);
2234 GetCaret()->SetSize(caretRect
.GetSize());
2239 /// Get the caret height and position for the given character position
2240 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2242 wxClientDC
dc(this);
2243 dc
.SetFont(GetFont());
2250 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2252 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2259 /// Gets the line for the visible caret position. If the caret is
2260 /// shown at the very end of the line, it means the next character is actually
2261 /// on the following line. So let's get the line we're expecting to find
2262 /// if this is the case.
2263 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2265 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2266 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2269 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2270 if (caretPosition
== lineRange
.GetStart()-1 &&
2271 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2273 if (!m_caretAtLineStart
)
2274 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2281 /// Move the caret to the given character position
2282 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2284 if (GetBuffer().GetDirty())
2287 if (pos
<= GetBuffer().GetRange().GetEnd())
2289 SetCaretPosition(pos
, showAtLineStart
);
2299 /// Layout the buffer: which we must do before certain operations, such as
2300 /// setting the caret position.
2301 bool wxRichTextCtrl::Layout(bool onlyVisibleRect
)
2303 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2305 wxRect
availableSpace(GetClientSize());
2306 if (availableSpace
.width
== 0)
2307 availableSpace
.width
= 10;
2308 if (availableSpace
.height
== 0)
2309 availableSpace
.height
= 10;
2311 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2312 if (onlyVisibleRect
)
2314 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2315 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2318 wxClientDC
dc(this);
2319 dc
.SetFont(GetFont());
2323 GetBuffer().Defragment();
2324 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2325 GetBuffer().Layout(dc
, availableSpace
, flags
);
2326 GetBuffer().SetDirty(false);
2335 /// Is all of the selection bold?
2336 bool wxRichTextCtrl::IsSelectionBold() const
2340 wxRichTextAttr attr
;
2341 wxRichTextRange range
= GetSelectionRange();
2342 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2343 attr
.SetFontWeight(wxBOLD
);
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_WEIGHT
);
2353 if (GetStyle(GetCaretPosition()+1, attr
))
2355 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2356 return attr
.GetFontWeight() == wxBOLD
;
2362 /// Is all of the selection italics?
2363 bool wxRichTextCtrl::IsSelectionItalics() const
2367 wxRichTextRange range
= GetSelectionRange();
2368 wxRichTextAttr attr
;
2369 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2370 attr
.SetFontStyle(wxITALIC
);
2372 return HasCharacterAttributes(range
, attr
);
2376 // If no selection, then we need to combine current style with default style
2377 // to see what the effect would be if we started typing.
2378 wxRichTextAttr attr
;
2379 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2380 if (GetStyle(GetCaretPosition()+1, attr
))
2382 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2383 return attr
.GetFontStyle() == wxITALIC
;
2389 /// Is all of the selection underlined?
2390 bool wxRichTextCtrl::IsSelectionUnderlined() const
2394 wxRichTextRange range
= GetSelectionRange();
2395 wxRichTextAttr attr
;
2396 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2397 attr
.SetFontUnderlined(true);
2399 return HasCharacterAttributes(range
, attr
);
2403 // If no selection, then we need to combine current style with default style
2404 // to see what the effect would be if we started typing.
2405 wxRichTextAttr attr
;
2406 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2407 if (GetStyle(GetCaretPosition()+1, attr
))
2409 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2410 return attr
.GetFontUnderlined();
2416 /// Apply bold to the selection
2417 bool wxRichTextCtrl::ApplyBoldToSelection()
2419 wxRichTextAttr attr
;
2420 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2421 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2424 return SetStyle(GetSelectionRange(), attr
);
2426 SetDefaultStyle(attr
);
2430 /// Apply italic to the selection
2431 bool wxRichTextCtrl::ApplyItalicToSelection()
2433 wxRichTextAttr attr
;
2434 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2435 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2438 return SetStyle(GetSelectionRange(), attr
);
2440 SetDefaultStyle(attr
);
2444 /// Apply underline to the selection
2445 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2447 wxRichTextAttr attr
;
2448 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2449 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2452 return SetStyle(GetSelectionRange(), attr
);
2454 SetDefaultStyle(attr
);
2458 /// Is all of the selection aligned according to the specified flag?
2459 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2463 wxRichTextRange range
= GetSelectionRange();
2464 wxRichTextAttr attr
;
2465 attr
.SetAlignment(alignment
);
2467 return HasParagraphAttributes(range
, attr
);
2471 // If no selection, then we need to get information from the current paragraph.
2472 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2474 return para
->GetAttributes().GetAlignment() == alignment
;
2479 /// Apply alignment to the selection
2480 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2482 wxRichTextAttr attr
;
2483 attr
.SetAlignment(alignment
);
2485 return SetStyle(GetSelectionRange(), attr
);
2488 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2490 return SetStyle(para
->GetRange(), attr
);
2495 /// Sets the default style to the style under the cursor
2496 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2499 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2501 if (GetStyle(GetCaretPosition(), attr
))
2503 SetDefaultStyle(attr
);
2510 /// Returns the first visible position in the current view
2511 long wxRichTextCtrl::GetFirstVisiblePosition() const
2513 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2515 return line
->GetAbsoluteRange().GetStart();