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"
21 #include "wx/richtext/richtextctrl.h"
27 #include "wx/textfile.h"
29 #include "wx/settings.h"
30 #include "wx/filename.h"
31 #include "wx/dcbuffer.h"
32 #include "wx/arrimpl.cpp"
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
42 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
43 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
45 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
48 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
50 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
51 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
53 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
55 EVT_PAINT(wxRichTextCtrl::OnPaint
)
56 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
57 EVT_IDLE(wxRichTextCtrl::OnIdle
)
58 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
59 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
60 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
61 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
62 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
63 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
64 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
65 EVT_CHAR(wxRichTextCtrl::OnChar
)
66 EVT_SIZE(wxRichTextCtrl::OnSize
)
67 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
68 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
69 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
71 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
72 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
74 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
75 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
77 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
78 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
80 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
81 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
83 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
84 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
86 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
87 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
89 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
90 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
97 wxRichTextCtrl::wxRichTextCtrl()
98 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
99 : wxScrollHelper(this)
105 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
106 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
107 : wxScrollHelper(this)
111 Create(parent
, id
, pos
, size
, style
);
115 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
117 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
118 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
122 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
129 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
132 GetBuffer().SetRichTextCtrl(this);
134 wxTextAttrEx attributes
;
135 attributes
.SetFont(GetFont());
136 attributes
.SetTextColour(*wxBLACK
);
137 attributes
.SetBackgroundColour(*wxWHITE
);
138 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
139 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
141 SetDefaultStyle(attributes
);
142 SetBasicStyle(attributes
);
144 SetBackgroundColour(*wxWHITE
);
145 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
147 // Tell the sizers to use the given or best size
148 SetBestFittingSize(size
);
151 RecreateBuffer(size
);
153 SetCursor(wxCursor(wxCURSOR_IBEAM
));
158 wxRichTextCtrl::~wxRichTextCtrl()
160 delete m_contextMenu
;
163 /// Member initialisation
164 void wxRichTextCtrl::Init()
167 m_contextMenu
= NULL
;
169 m_caretPosition
= -1;
170 m_selectionRange
.SetRange(-2, -2);
171 m_selectionAnchor
= -2;
173 m_caretAtLineStart
= false;
175 m_fullLayoutRequired
= false;
176 m_fullLayoutTime
= 0;
177 m_fullLayoutSavedPosition
= 0;
178 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
181 /// Call Freeze to prevent refresh
182 void wxRichTextCtrl::Freeze()
187 /// Call Thaw to refresh
188 void wxRichTextCtrl::Thaw()
192 if (m_freezeCount
== 0)
200 void wxRichTextCtrl::Clear()
203 m_buffer
.SetDirty(true);
204 m_caretPosition
= -1;
205 m_caretAtLineStart
= false;
206 m_selectionRange
.SetRange(-2, -2);
208 if (m_freezeCount
== 0)
217 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
223 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
224 //wxLogDebug(wxT("OnPaint"));
228 if (m_freezeCount
> 0)
231 dc
.SetFont(GetFont());
233 // Paint the background
236 wxRegion dirtyRegion
= GetUpdateRegion();
238 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
239 wxRect
availableSpace(GetClientSize());
240 if (GetBuffer().GetDirty())
242 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
243 GetBuffer().SetDirty(false);
247 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
256 // Empty implementation, to prevent flicker
257 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
261 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
263 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
272 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
281 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
287 dc
.SetFont(GetFont());
290 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
292 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
294 m_dragStart
= event
.GetLogicalPosition(dc
);
300 bool caretAtLineStart
= false;
302 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
304 // If we're at the start of a line (but not first in para)
305 // then we should keep the caret showing at the start of the line
306 // by showing the m_caretAtLineStart flag.
307 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
308 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
310 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
311 caretAtLineStart
= true;
315 MoveCaret(position
, caretAtLineStart
);
316 SetDefaultStyleToCursorStyle();
319 wxWindow
* p
= GetParent();
320 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
323 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
326 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
327 frame
->SetStatusText(msg
, 1);
336 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
341 if (GetCapture() == this)
347 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
349 if (!event
.Dragging())
357 dc
.SetFont(GetFont());
360 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
361 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
363 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
365 // TODO: test closeness
367 bool caretAtLineStart
= false;
369 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
371 // If we're at the start of a line (but not first in para)
372 // then we should keep the caret showing at the start of the line
373 // by showing the m_caretAtLineStart flag.
374 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
375 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
377 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
378 caretAtLineStart
= true;
382 if (m_caretPosition
!= position
)
384 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
386 MoveCaret(position
, caretAtLineStart
);
387 SetDefaultStyleToCursorStyle();
396 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
402 /// Left-double-click
403 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
409 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
415 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
418 if (event
.ControlDown())
419 flags
|= wxRICHTEXT_CTRL_DOWN
;
420 if (event
.ShiftDown())
421 flags
|= wxRICHTEXT_SHIFT_DOWN
;
423 flags
|= wxRICHTEXT_ALT_DOWN
;
425 if (event
.GetKeyCode() == WXK_LEFT
||
426 event
.GetKeyCode() == WXK_RIGHT
||
427 event
.GetKeyCode() == WXK_UP
||
428 event
.GetKeyCode() == WXK_DOWN
||
429 event
.GetKeyCode() == WXK_HOME
||
430 event
.GetKeyCode() == WXK_PAGEUP
||
431 event
.GetKeyCode() == WXK_PAGEDOWN
||
432 event
.GetKeyCode() == WXK_PRIOR
||
433 event
.GetKeyCode() == WXK_NEXT
||
434 event
.GetKeyCode() == WXK_END
)
436 KeyboardNavigate(event
.GetKeyCode(), flags
);
438 else if (event
.GetKeyCode() == WXK_RETURN
)
440 BeginBatchUndo(_("Insert Text"));
442 long newPos
= m_caretPosition
;
444 DeleteSelectedContent(& newPos
);
446 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
448 wxRichTextEvent
cmdEvent(
449 wxEVT_COMMAND_RICHTEXT_RETURN
,
451 cmdEvent
.SetEventObject(this);
452 cmdEvent
.SetFlags(flags
);
453 GetEventHandler()->ProcessEvent(cmdEvent
);
456 SetDefaultStyleToCursorStyle();
458 else if (event
.GetKeyCode() == WXK_BACK
)
460 BeginBatchUndo(_("Delete Text"));
462 // Submit range in character positions, which are greater than caret positions,
463 // so subtract 1 for deleted character and add 1 for conversion to character position.
464 if (m_caretPosition
> -1 && !HasSelection())
466 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
467 m_caretPosition
, // Current caret position
468 m_caretPosition
-1, // New caret position
472 DeleteSelectedContent();
476 // Shouldn't this be in Do()?
477 if (GetLastPosition() == -1)
481 m_caretPosition
= -1;
483 SetDefaultStyleToCursorStyle();
487 else if (event
.GetKeyCode() == WXK_DELETE
)
489 BeginBatchUndo(_("Delete Text"));
491 // Submit range in character positions, which are greater than caret positions,
492 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
494 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
495 m_caretPosition
, // Current caret position
496 m_caretPosition
+1, // New caret position
500 DeleteSelectedContent();
504 // Shouldn't this be in Do()?
505 if (GetLastPosition() == -1)
509 m_caretPosition
= -1;
511 SetDefaultStyleToCursorStyle();
516 BeginBatchUndo(_("Insert Text"));
518 long newPos
= m_caretPosition
;
519 DeleteSelectedContent(& newPos
);
521 wxString str
= (wxChar
) event
.GetKeyCode();
522 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
526 SetDefaultStyleToCursorStyle();
534 /// Delete content if there is a selection, e.g. when pressing a key.
535 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
539 long pos
= m_selectionRange
.GetStart();
540 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
541 m_caretPosition
, // Current caret position
542 pos
, // New caret position
544 m_selectionRange
.SetRange(-2, -2);
554 /// Keyboard navigation
558 Left: left one character
559 Right: right one character
562 Ctrl-Left: left one word
563 Ctrl-Right: right one word
564 Ctrl-Up: previous paragraph start
565 Ctrl-Down: next start of paragraph
568 Ctrl-Home: start of document
569 Ctrl-End: end of document
571 Page-Down: Down a screen
575 Ctrl-Alt-PgUp: Start of window
576 Ctrl-Alt-PgDn: End of window
577 F8: Start selection mode
578 Esc: End selection mode
580 Adding Shift does the above but starts/extends selection.
585 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
587 bool success
= false;
589 if (keyCode
== WXK_RIGHT
)
591 if (flags
& wxRICHTEXT_CTRL_DOWN
)
592 success
= WordRight(1, flags
);
594 success
= MoveRight(1, flags
);
596 else if (keyCode
== WXK_LEFT
)
598 if (flags
& wxRICHTEXT_CTRL_DOWN
)
599 success
= WordLeft(1, flags
);
601 success
= MoveLeft(1, flags
);
603 else if (keyCode
== WXK_UP
)
605 if (flags
& wxRICHTEXT_CTRL_DOWN
)
606 success
= MoveToParagraphStart(flags
);
608 success
= MoveUp(1, flags
);
610 else if (keyCode
== WXK_DOWN
)
612 if (flags
& wxRICHTEXT_CTRL_DOWN
)
613 success
= MoveToParagraphEnd(flags
);
615 success
= MoveDown(1, flags
);
617 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_PRIOR
)
619 success
= PageUp(1, flags
);
621 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NEXT
)
623 success
= PageDown(1, flags
);
625 else if (keyCode
== WXK_HOME
)
627 if (flags
& wxRICHTEXT_CTRL_DOWN
)
628 success
= MoveHome(flags
);
630 success
= MoveToLineStart(flags
);
632 else if (keyCode
== WXK_END
)
634 if (flags
& wxRICHTEXT_CTRL_DOWN
)
635 success
= MoveEnd(flags
);
637 success
= MoveToLineEnd(flags
);
642 ScrollIntoView(m_caretPosition
, keyCode
);
643 SetDefaultStyleToCursorStyle();
649 /// Extend the selection. Selections are in caret positions.
650 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
652 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
654 // If not currently selecting, start selecting
655 if (m_selectionRange
.GetStart() == -2)
657 m_selectionAnchor
= oldPos
;
660 m_selectionRange
.SetRange(newPos
+1, oldPos
);
662 m_selectionRange
.SetRange(oldPos
+1, newPos
);
666 // Always ensure that the selection range start is greater than
668 if (newPos
> m_selectionAnchor
)
669 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
671 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
674 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
676 wxLogDebug(wxT("Strange selection range"));
685 /// Scroll into view, returning true if we scrolled.
686 /// This takes a _caret_ position.
687 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
689 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
695 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
698 GetViewStart(& startX
, & startY
);
700 startY
= startY
* ppuY
;
703 GetVirtualSize(& sx
, & sy
);
708 wxRect rect
= line
->GetRect();
710 bool scrolled
= false;
712 wxSize clientSize
= GetClientSize();
715 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_NEXT
|| keyCode
== WXK_PAGEDOWN
)
717 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
719 // Make it scroll so this item is at the bottom
721 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
722 y
= (int) (0.5 + y
/ppuY
);
726 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
730 else if (rect
.y
< startY
)
732 // Make it scroll so this item is at the top
735 y
= (int) (0.5 + y
/ppuY
);
739 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
745 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PRIOR
|| keyCode
== WXK_PAGEUP
)
749 // Make it scroll so this item is at the top
752 y
= (int) (0.5 + y
/ppuY
);
756 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
760 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
762 // Make it scroll so this item is at the bottom
764 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
765 y
= (int) (0.5 + y
/ppuY
);
769 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
779 /// Is the given position visible on the screen?
780 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
782 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
788 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
791 GetViewStart(& startX
, & startY
);
793 startY
= startY
* ppuY
;
796 GetVirtualSize(& sx
, & sy
);
801 wxRect rect
= line
->GetRect();
803 wxSize clientSize
= GetClientSize();
805 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
808 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
810 m_caretPosition
= position
;
811 m_caretAtLineStart
= showAtLineStart
;
814 /// Move caret one visual step forward: this may mean setting a flag
815 /// and keeping the same position if we're going from the end of one line
816 /// to the start of the next, which may be the exact same caret position.
817 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
819 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
821 // Only do the check if we're not at the end of the paragraph (where things work OK
823 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
825 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
829 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
831 // We're at the end of a line. See whether we need to
832 // stay at the same actual caret position but change visual
834 if (oldPosition
== lineRange
.GetEnd())
836 if (m_caretAtLineStart
)
838 // We're already at the start of the line, so actually move on now.
839 m_caretPosition
= oldPosition
+ 1;
840 m_caretAtLineStart
= false;
844 // We're showing at the end of the line, so keep to
845 // the same position but indicate that we're to show
846 // at the start of the next line.
847 m_caretPosition
= oldPosition
;
848 m_caretAtLineStart
= true;
850 SetDefaultStyleToCursorStyle();
856 SetDefaultStyleToCursorStyle();
859 /// Move caret one visual step backward: this may mean setting a flag
860 /// and keeping the same position if we're going from the end of one line
861 /// to the start of the next, which may be the exact same caret position.
862 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
864 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
866 // Only do the check if we're not at the start of the paragraph (where things work OK
868 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
870 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
874 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
876 // We're at the start of a line. See whether we need to
877 // stay at the same actual caret position but change visual
879 if (oldPosition
== lineRange
.GetStart())
881 m_caretPosition
= oldPosition
-1;
882 m_caretAtLineStart
= true;
885 else if (oldPosition
== lineRange
.GetEnd())
887 if (m_caretAtLineStart
)
889 // We're at the start of the line, so keep the same caret position
890 // but clear the start-of-line flag.
891 m_caretPosition
= oldPosition
;
892 m_caretAtLineStart
= false;
896 // We're showing at the end of the line, so go back
897 // to the previous character position.
898 m_caretPosition
= oldPosition
- 1;
900 SetDefaultStyleToCursorStyle();
906 SetDefaultStyleToCursorStyle();
910 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
912 long endPos
= GetBuffer().GetRange().GetEnd();
914 if (m_caretPosition
+ noPositions
< endPos
)
916 long oldPos
= m_caretPosition
;
917 long newPos
= m_caretPosition
+ noPositions
;
919 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
923 // Determine by looking at oldPos and m_caretPosition whether
924 // we moved from the end of a line to the start of the next line, in which case
925 // we want to adjust the caret position such that it is positioned at the
926 // start of the next line, rather than jumping past the first character of the
928 if (noPositions
== 1 && !extendSel
)
929 MoveCaretForward(oldPos
);
931 SetCaretPosition(newPos
);
934 SetDefaultStyleToCursorStyle();
945 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
949 if (m_caretPosition
> startPos
- noPositions
+ 1)
951 long oldPos
= m_caretPosition
;
952 long newPos
= m_caretPosition
- noPositions
;
953 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
957 if (noPositions
== 1 && !extendSel
)
958 MoveCaretBack(oldPos
);
960 SetCaretPosition(newPos
);
963 SetDefaultStyleToCursorStyle();
974 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
976 return MoveDown(- noLines
, flags
);
980 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
985 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
986 wxPoint pt
= GetCaret()->GetPosition();
987 long newLine
= lineNumber
+ noLines
;
989 if (lineNumber
!= -1)
993 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
995 if (newLine
> lastLine
)
1005 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1008 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1014 wxClientDC
dc(this);
1016 dc
.SetFont(GetFont());
1018 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1020 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1022 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1023 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1024 // so we view the caret at the start of the line.
1025 bool caretLineStart
= false;
1026 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1028 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1029 wxRichTextRange lineRange
;
1031 lineRange
= thisLine
->GetAbsoluteRange();
1033 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1036 caretLineStart
= true;
1040 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1041 if (para
&& para
->GetRange().GetStart() == newPos
)
1046 long newSelEnd
= newPos
;
1048 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1052 SetCaretPosition(newPos
, caretLineStart
);
1054 SetDefaultStyleToCursorStyle();
1064 /// Move to the end of the paragraph
1065 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1067 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1070 long newPos
= para
->GetRange().GetEnd() - 1;
1071 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1075 SetCaretPosition(newPos
);
1077 SetDefaultStyleToCursorStyle();
1087 /// Move to the start of the paragraph
1088 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1090 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1093 long newPos
= para
->GetRange().GetStart() - 1;
1094 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1098 SetCaretPosition(newPos
);
1100 SetDefaultStyleToCursorStyle();
1110 /// Move to the end of the line
1111 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1113 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1117 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1118 long newPos
= lineRange
.GetEnd();
1119 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1123 SetCaretPosition(newPos
);
1125 SetDefaultStyleToCursorStyle();
1135 /// Move to the start of the line
1136 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1138 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1141 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1142 long newPos
= lineRange
.GetStart()-1;
1144 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1148 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1150 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1152 SetDefaultStyleToCursorStyle();
1162 /// Move to the start of the buffer
1163 bool wxRichTextCtrl::MoveHome(int flags
)
1165 if (m_caretPosition
!= -1)
1167 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1171 SetCaretPosition(-1);
1173 SetDefaultStyleToCursorStyle();
1183 /// Move to the end of the buffer
1184 bool wxRichTextCtrl::MoveEnd(int flags
)
1186 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1188 if (m_caretPosition
!= endPos
)
1190 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1194 SetCaretPosition(endPos
);
1196 SetDefaultStyleToCursorStyle();
1206 /// Move noPages pages up
1207 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1209 return PageDown(- noPages
, flags
);
1212 /// Move noPages pages down
1213 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1215 // Calculate which line occurs noPages * screen height further down.
1216 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1219 wxSize clientSize
= GetClientSize();
1220 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1222 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1225 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1226 long pos
= lineRange
.GetStart()-1;
1227 if (pos
!= m_caretPosition
)
1229 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1231 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1235 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1237 SetDefaultStyleToCursorStyle();
1249 // Finds the caret position for the next word
1250 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1252 long endPos
= GetBuffer().GetRange().GetEnd();
1256 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1258 // First skip current text to space
1259 while (i
< endPos
&& i
> -1)
1261 // i is in character, not caret positions
1262 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1263 if (text
!= wxT(" ") && !text
.empty())
1270 while (i
< endPos
&& i
> -1)
1272 // i is in character, not caret positions
1273 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1274 if (text
.empty()) // End of paragraph, or maybe an image
1275 return wxMax(-1, i
- 1);
1276 else if (text
== wxT(" ") || text
.empty())
1280 // Convert to caret position
1281 return wxMax(-1, i
- 1);
1290 long i
= m_caretPosition
;
1292 // First skip white space
1293 while (i
< endPos
&& i
> -1)
1295 // i is in character, not caret positions
1296 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1297 if (text
.empty()) // End of paragraph, or maybe an image
1299 else if (text
== wxT(" ") || text
.empty())
1304 // Next skip current text to space
1305 while (i
< endPos
&& i
> -1)
1307 // i is in character, not caret positions
1308 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1309 if (text
!= wxT(" ") /* && !text.empty() */)
1322 /// Move n words left
1323 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1325 long pos
= FindNextWordPosition(-1);
1326 if (pos
!= m_caretPosition
)
1328 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1330 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1334 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1336 SetDefaultStyleToCursorStyle();
1346 /// Move n words right
1347 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1349 long pos
= FindNextWordPosition(1);
1350 if (pos
!= m_caretPosition
)
1352 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1354 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1358 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1360 SetDefaultStyleToCursorStyle();
1371 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1373 // Only do sizing optimization for large buffers
1374 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1376 m_fullLayoutRequired
= true;
1377 m_fullLayoutTime
= wxGetLocalTimeMillis();
1378 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1379 LayoutContent(true /* onlyVisibleRect */);
1382 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1390 /// Idle-time processing
1391 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1393 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1395 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1397 m_fullLayoutRequired
= false;
1398 m_fullLayoutTime
= 0;
1399 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1400 ShowPosition(m_fullLayoutSavedPosition
);
1407 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1413 /// Set up scrollbars, e.g. after a resize
1414 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1419 if (GetBuffer().IsEmpty())
1421 SetScrollbars(0, 0, 0, 0, 0, 0);
1425 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1426 // of pixels. See e.g. wxVScrolledWindow for ideas.
1427 int pixelsPerUnit
= 5; // 10;
1428 wxSize clientSize
= GetClientSize();
1430 int maxHeight
= GetBuffer().GetCachedSize().y
;
1432 int unitsY
= maxHeight
/pixelsPerUnit
;
1434 int startX
= 0, startY
= 0;
1436 GetViewStart(& startX
, & startY
);
1438 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1439 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1441 // Move to previous scroll position if
1443 SetScrollbars(0, pixelsPerUnit
,
1445 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1448 /// Paint the background
1449 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1451 wxColour backgroundColour
= GetBackgroundColour();
1452 if (!backgroundColour
.Ok())
1453 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1455 // Clear the background
1456 dc
.SetBrush(wxBrush(backgroundColour
));
1457 dc
.SetPen(*wxTRANSPARENT_PEN
);
1458 wxRect
windowRect(GetClientSize());
1459 windowRect
.x
-= 2; windowRect
.y
-= 2;
1460 windowRect
.width
+= 4; windowRect
.height
+= 4;
1462 // We need to shift the rectangle to take into account
1463 // scrolling. Converting device to logical coordinates.
1464 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1465 dc
.DrawRectangle(windowRect
);
1468 /// Recreate buffer bitmap if necessary
1469 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1472 if (sz
== wxDefaultSize
)
1473 sz
= GetClientSize();
1475 if (sz
.x
< 1 || sz
.y
< 1)
1478 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1479 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1480 return m_bufferBitmap
.Ok();
1483 // ----------------------------------------------------------------------------
1484 // file IO functions
1485 // ----------------------------------------------------------------------------
1487 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1489 bool success
= GetBuffer().LoadFile(filename
, type
);
1491 m_filename
= filename
;
1494 SetInsertionPoint(0);
1497 SetupScrollbars(true);
1505 wxLogError(_("File couldn't be loaded."));
1511 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1513 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1514 if ( filenameToUse
.empty() )
1516 // what kind of message to give? is it an error or a program bug?
1517 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1522 if (GetBuffer().SaveFile(filenameToUse
, type
))
1524 m_filename
= filenameToUse
;
1532 wxLogError(_("The text couldn't be saved."));
1537 // ----------------------------------------------------------------------------
1538 // wxRichTextCtrl specific functionality
1539 // ----------------------------------------------------------------------------
1541 /// Add a new paragraph of text to the end of the buffer
1542 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1544 return GetBuffer().AddParagraph(text
);
1548 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1550 return GetBuffer().AddImage(image
);
1553 // ----------------------------------------------------------------------------
1554 // selection and ranges
1555 // ----------------------------------------------------------------------------
1557 void wxRichTextCtrl::SelectAll()
1559 SetSelection(0, GetLastPosition());
1560 m_selectionAnchor
= -1;
1564 void wxRichTextCtrl::SelectNone()
1566 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1567 SetSelection(-2, -2);
1568 m_selectionAnchor
= -2;
1571 wxString
wxRichTextCtrl::GetStringSelection() const
1574 GetSelection(&from
, &to
);
1576 return GetRange(from
, to
);
1579 // do the window-specific processing after processing the update event
1580 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1582 if ( event
.GetSetEnabled() )
1583 Enable(event
.GetEnabled());
1585 if ( event
.GetSetText() )
1587 if ( event
.GetText() != GetValue() )
1588 SetValue(event
.GetText());
1592 // ----------------------------------------------------------------------------
1594 // ----------------------------------------------------------------------------
1596 wxTextCtrlHitTestResult
1597 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1599 // implement in terms of the other overload as the native ports typically
1600 // can get the position and not (x, y) pair directly (although wxUniv
1601 // directly gets x and y -- and so overrides this method as well)
1603 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1605 if ( rc
!= wxTE_HT_UNKNOWN
)
1607 PositionToXY(pos
, x
, y
);
1613 wxTextCtrlHitTestResult
1614 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1617 wxClientDC
dc((wxRichTextCtrl
*) this);
1618 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1620 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1621 if (hit
== wxRICHTEXT_HITTEST_BEFORE
)
1622 return wxTE_HT_BEFORE
;
1623 else if (hit
== wxRICHTEXT_HITTEST_AFTER
)
1624 return wxTE_HT_BEYOND
;
1625 else if (hit
== wxRICHTEXT_HITTEST_ON
)
1626 return wxTE_HT_ON_TEXT
;
1628 return wxTE_HT_UNKNOWN
;
1631 // ----------------------------------------------------------------------------
1632 // set/get the controls text
1633 // ----------------------------------------------------------------------------
1635 wxString
wxRichTextCtrl::GetValue() const
1637 return GetBuffer().GetText();
1640 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1642 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1645 void wxRichTextCtrl::SetValue(const wxString
& value
)
1649 // if the text is long enough, it's faster to just set it instead of first
1650 // comparing it with the old one (chances are that it will be different
1651 // anyhow, this comparison is there to avoid flicker for small single-line
1652 // edit controls mostly)
1653 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1655 DoWriteText(value
, false /* not selection only */);
1657 // for compatibility, don't move the cursor when doing SetValue()
1658 SetInsertionPoint(0);
1662 // still send an event for consistency
1666 // we should reset the modified flag even if the value didn't really change
1668 // mark the control as being not dirty - we changed its text, not the
1673 void wxRichTextCtrl::WriteText(const wxString
& value
)
1678 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1680 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1683 void wxRichTextCtrl::AppendText(const wxString
& text
)
1685 SetInsertionPointEnd();
1690 /// Write an image at the current insertion point
1691 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1693 wxRichTextImageBlock imageBlock
;
1695 wxImage image2
= image
;
1696 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1697 return WriteImage(imageBlock
);
1702 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1704 wxRichTextImageBlock imageBlock
;
1707 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1708 return WriteImage(imageBlock
);
1713 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1715 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1718 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1722 wxRichTextImageBlock imageBlock
;
1724 wxImage image
= bitmap
.ConvertToImage();
1725 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1726 return WriteImage(imageBlock
);
1733 /// Insert a newline (actually paragraph) at the current insertion point.
1734 bool wxRichTextCtrl::Newline()
1736 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1740 // ----------------------------------------------------------------------------
1741 // Clipboard operations
1742 // ----------------------------------------------------------------------------
1744 void wxRichTextCtrl::Copy()
1748 wxRichTextRange range
= GetSelectionRange();
1749 GetBuffer().CopyToClipboard(range
);
1753 void wxRichTextCtrl::Cut()
1757 wxRichTextRange range
= GetSelectionRange();
1758 GetBuffer().CopyToClipboard(range
);
1760 DeleteSelectedContent();
1766 void wxRichTextCtrl::Paste()
1770 BeginBatchUndo(_("Paste"));
1772 long newPos
= m_caretPosition
;
1773 DeleteSelectedContent(& newPos
);
1775 GetBuffer().PasteFromClipboard(newPos
);
1781 void wxRichTextCtrl::DeleteSelection()
1783 if (CanDeleteSelection())
1785 DeleteSelectedContent();
1789 bool wxRichTextCtrl::HasSelection() const
1791 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1794 bool wxRichTextCtrl::CanCopy() const
1796 // Can copy if there's a selection
1797 return HasSelection();
1800 bool wxRichTextCtrl::CanCut() const
1802 return HasSelection() && IsEditable();
1805 bool wxRichTextCtrl::CanPaste() const
1807 if ( !IsEditable() )
1810 return GetBuffer().CanPasteFromClipboard();
1813 bool wxRichTextCtrl::CanDeleteSelection() const
1815 return HasSelection() && IsEditable();
1819 // ----------------------------------------------------------------------------
1821 // ----------------------------------------------------------------------------
1823 void wxRichTextCtrl::SetEditable(bool editable
)
1825 m_editable
= editable
;
1828 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1832 m_caretPosition
= pos
- 1;
1835 void wxRichTextCtrl::SetInsertionPointEnd()
1837 long pos
= GetLastPosition();
1838 SetInsertionPoint(pos
);
1841 long wxRichTextCtrl::GetInsertionPoint() const
1843 return m_caretPosition
+1;
1846 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1848 return GetBuffer().GetRange().GetEnd();
1851 // If the return values from and to are the same, there is no
1853 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1855 *from
= m_selectionRange
.GetStart();
1856 *to
= m_selectionRange
.GetEnd();
1859 bool wxRichTextCtrl::IsEditable() const
1864 // ----------------------------------------------------------------------------
1866 // ----------------------------------------------------------------------------
1868 void wxRichTextCtrl::SetSelection(long from
, long to
)
1870 // if from and to are both -1, it means (in wxWidgets) that all text should
1872 if ( (from
== -1) && (to
== -1) )
1875 to
= GetLastPosition();
1878 DoSetSelection(from
, to
);
1881 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1883 m_selectionAnchor
= from
;
1884 m_selectionRange
.SetRange(from
, to
);
1889 // ----------------------------------------------------------------------------
1891 // ----------------------------------------------------------------------------
1893 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1895 BeginBatchUndo(_("Replace"));
1897 DeleteSelectedContent();
1899 DoWriteText(value
, true /* selection only */);
1904 void wxRichTextCtrl::Remove(long from
, long to
)
1908 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1909 m_caretPosition
, // Current caret position
1910 from
, // New caret position
1918 bool wxRichTextCtrl::IsModified() const
1920 return m_buffer
.IsModified();
1923 void wxRichTextCtrl::MarkDirty()
1925 m_buffer
.Modify(true);
1928 void wxRichTextCtrl::DiscardEdits()
1930 m_buffer
.Modify(false);
1931 m_buffer
.GetCommandProcessor()->ClearCommands();
1934 int wxRichTextCtrl::GetNumberOfLines() const
1936 return GetBuffer().GetParagraphCount();
1939 // ----------------------------------------------------------------------------
1940 // Positions <-> coords
1941 // ----------------------------------------------------------------------------
1943 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1945 return GetBuffer().XYToPosition(x
, y
);
1948 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1950 return GetBuffer().PositionToXY(pos
, x
, y
);
1953 // ----------------------------------------------------------------------------
1955 // ----------------------------------------------------------------------------
1957 void wxRichTextCtrl::ShowPosition(long pos
)
1959 if (!IsPositionVisible(pos
))
1960 ScrollIntoView(pos
-1, WXK_DOWN
);
1963 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1965 return GetBuffer().GetParagraphLength(lineNo
);
1968 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1970 return GetBuffer().GetParagraphText(lineNo
);
1973 // ----------------------------------------------------------------------------
1975 // ----------------------------------------------------------------------------
1977 void wxRichTextCtrl::Undo()
1981 GetCommandProcessor()->Undo();
1985 void wxRichTextCtrl::Redo()
1989 GetCommandProcessor()->Redo();
1993 bool wxRichTextCtrl::CanUndo() const
1995 return GetCommandProcessor()->CanUndo();
1998 bool wxRichTextCtrl::CanRedo() const
2000 return GetCommandProcessor()->CanRedo();
2003 // ----------------------------------------------------------------------------
2004 // implemenation details
2005 // ----------------------------------------------------------------------------
2007 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2009 SetValue(event
.GetString());
2010 GetEventHandler()->ProcessEvent(event
);
2013 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2015 // By default, load the first file into the text window.
2016 if (event
.GetNumberOfFiles() > 0)
2018 LoadFile(event
.GetFiles()[0]);
2022 // ----------------------------------------------------------------------------
2023 // text control event processing
2024 // ----------------------------------------------------------------------------
2026 bool wxRichTextCtrl::SendUpdateEvent()
2028 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2029 InitCommandEvent(event
);
2031 return GetEventHandler()->ProcessEvent(event
);
2034 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2036 event
.SetEventObject((wxControlBase
*)this); // const_cast
2038 switch ( m_clientDataType
)
2040 case wxClientData_Void
:
2041 event
.SetClientData(GetClientData());
2044 case wxClientData_Object
:
2045 event
.SetClientObject(GetClientObject());
2048 case wxClientData_None
:
2055 wxSize
wxRichTextCtrl::DoGetBestSize() const
2057 return wxSize(10, 10);
2060 // ----------------------------------------------------------------------------
2061 // standard handlers for standard edit menu events
2062 // ----------------------------------------------------------------------------
2064 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2069 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2074 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2079 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2084 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2089 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2094 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2096 event
.Enable( CanCut() );
2099 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2101 event
.Enable( CanCopy() );
2104 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2106 event
.Enable( CanDeleteSelection() );
2109 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2111 event
.Enable( CanPaste() );
2114 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2116 event
.Enable( CanUndo() );
2117 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2120 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2122 event
.Enable( CanRedo() );
2123 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2126 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2131 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2133 event
.Enable(GetLastPosition() > 0);
2136 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2140 m_contextMenu
= new wxMenu
;
2141 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2142 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2143 m_contextMenu
->AppendSeparator();
2144 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2145 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2146 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2147 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2148 m_contextMenu
->AppendSeparator();
2149 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2151 PopupMenu(m_contextMenu
);
2155 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2157 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2160 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2162 return GetBuffer().SetStyle(range
, style
);
2165 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2167 return GetBuffer().SetDefaultStyle(style
);
2170 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2172 return GetBuffer().GetDefaultStyle();
2175 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2177 return GetBuffer().GetStyle(position
, style
);
2180 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2182 return GetBuffer().GetStyle(position
, style
);
2185 /// Set font, and also the buffer attributes
2186 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2188 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2189 wxControl::SetFont(font
);
2191 wxScrolledWindow::SetFont(font
);
2194 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2196 GetBuffer().SetBasicStyle(attr
);
2197 GetBuffer().SetDefaultStyle(attr
);
2202 /// Transform logical to physical
2203 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2206 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2211 /// Transform physical to logical
2212 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2215 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2220 /// Position the caret
2221 void wxRichTextCtrl::PositionCaret()
2226 //wxLogDebug(wxT("PositionCaret"));
2229 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2231 wxPoint originalPt
= caretRect
.GetPosition();
2232 wxPoint pt
= GetPhysicalPoint(originalPt
);
2233 if (GetCaret()->GetPosition() != pt
)
2235 GetCaret()->Move(pt
);
2236 GetCaret()->SetSize(caretRect
.GetSize());
2241 /// Get the caret height and position for the given character position
2242 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2244 wxClientDC
dc(this);
2245 dc
.SetFont(GetFont());
2252 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2254 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2261 /// Gets the line for the visible caret position. If the caret is
2262 /// shown at the very end of the line, it means the next character is actually
2263 /// on the following line. So let's get the line we're expecting to find
2264 /// if this is the case.
2265 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2267 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2268 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2271 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2272 if (caretPosition
== lineRange
.GetStart()-1 &&
2273 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2275 if (!m_caretAtLineStart
)
2276 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2283 /// Move the caret to the given character position
2284 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2286 if (GetBuffer().GetDirty())
2289 if (pos
<= GetBuffer().GetRange().GetEnd())
2291 SetCaretPosition(pos
, showAtLineStart
);
2301 /// Layout the buffer: which we must do before certain operations, such as
2302 /// setting the caret position.
2303 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2305 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2307 wxRect
availableSpace(GetClientSize());
2308 if (availableSpace
.width
== 0)
2309 availableSpace
.width
= 10;
2310 if (availableSpace
.height
== 0)
2311 availableSpace
.height
= 10;
2313 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2314 if (onlyVisibleRect
)
2316 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2317 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2320 wxClientDC
dc(this);
2321 dc
.SetFont(GetFont());
2325 GetBuffer().Defragment();
2326 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2327 GetBuffer().Layout(dc
, availableSpace
, flags
);
2328 GetBuffer().SetDirty(false);
2337 /// Is all of the selection bold?
2338 bool wxRichTextCtrl::IsSelectionBold() const
2342 wxRichTextAttr attr
;
2343 wxRichTextRange range
= GetSelectionRange();
2344 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2345 attr
.SetFontWeight(wxBOLD
);
2347 return HasCharacterAttributes(range
, attr
);
2351 // If no selection, then we need to combine current style with default style
2352 // to see what the effect would be if we started typing.
2353 wxRichTextAttr attr
;
2354 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2355 if (GetStyle(GetCaretPosition()+1, attr
))
2357 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2358 return attr
.GetFontWeight() == wxBOLD
;
2364 /// Is all of the selection italics?
2365 bool wxRichTextCtrl::IsSelectionItalics() const
2369 wxRichTextRange range
= GetSelectionRange();
2370 wxRichTextAttr attr
;
2371 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2372 attr
.SetFontStyle(wxITALIC
);
2374 return HasCharacterAttributes(range
, attr
);
2378 // If no selection, then we need to combine current style with default style
2379 // to see what the effect would be if we started typing.
2380 wxRichTextAttr attr
;
2381 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2382 if (GetStyle(GetCaretPosition()+1, attr
))
2384 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2385 return attr
.GetFontStyle() == wxITALIC
;
2391 /// Is all of the selection underlined?
2392 bool wxRichTextCtrl::IsSelectionUnderlined() const
2396 wxRichTextRange range
= GetSelectionRange();
2397 wxRichTextAttr attr
;
2398 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2399 attr
.SetFontUnderlined(true);
2401 return HasCharacterAttributes(range
, attr
);
2405 // If no selection, then we need to combine current style with default style
2406 // to see what the effect would be if we started typing.
2407 wxRichTextAttr attr
;
2408 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2409 if (GetStyle(GetCaretPosition()+1, attr
))
2411 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2412 return attr
.GetFontUnderlined();
2418 /// Apply bold to the selection
2419 bool wxRichTextCtrl::ApplyBoldToSelection()
2421 wxRichTextAttr attr
;
2422 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2423 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2426 return SetStyle(GetSelectionRange(), attr
);
2428 SetDefaultStyle(attr
);
2432 /// Apply italic to the selection
2433 bool wxRichTextCtrl::ApplyItalicToSelection()
2435 wxRichTextAttr attr
;
2436 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2437 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2440 return SetStyle(GetSelectionRange(), attr
);
2442 SetDefaultStyle(attr
);
2446 /// Apply underline to the selection
2447 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2449 wxRichTextAttr attr
;
2450 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2451 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2454 return SetStyle(GetSelectionRange(), attr
);
2456 SetDefaultStyle(attr
);
2460 /// Is all of the selection aligned according to the specified flag?
2461 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2465 wxRichTextRange range
= GetSelectionRange();
2466 wxRichTextAttr attr
;
2467 attr
.SetAlignment(alignment
);
2469 return HasParagraphAttributes(range
, attr
);
2473 // If no selection, then we need to get information from the current paragraph.
2474 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2476 return para
->GetAttributes().GetAlignment() == alignment
;
2481 /// Apply alignment to the selection
2482 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2484 wxRichTextAttr attr
;
2485 attr
.SetAlignment(alignment
);
2487 return SetStyle(GetSelectionRange(), attr
);
2490 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2492 return SetStyle(para
->GetRange(), attr
);
2497 /// Sets the default style to the style under the cursor
2498 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2501 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2503 if (GetStyle(GetCaretPosition(), attr
))
2505 SetDefaultStyle(attr
);
2512 /// Returns the first visible position in the current view
2513 long wxRichTextCtrl::GetFirstVisiblePosition() const
2515 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2517 return line
->GetAbsoluteRange().GetStart();