1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
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_END
)
434 KeyboardNavigate(event
.GetKeyCode(), flags
);
436 else if (event
.GetKeyCode() == WXK_RETURN
)
438 BeginBatchUndo(_("Insert Text"));
440 long newPos
= m_caretPosition
;
442 DeleteSelectedContent(& newPos
);
444 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
446 wxRichTextEvent
cmdEvent(
447 wxEVT_COMMAND_RICHTEXT_RETURN
,
449 cmdEvent
.SetEventObject(this);
450 cmdEvent
.SetFlags(flags
);
451 GetEventHandler()->ProcessEvent(cmdEvent
);
454 SetDefaultStyleToCursorStyle();
456 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
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();
486 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
488 else if (event
.GetKeyCode() == WXK_DELETE
)
490 BeginBatchUndo(_("Delete Text"));
492 // Submit range in character positions, which are greater than caret positions,
493 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
495 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
496 m_caretPosition
, // Current caret position
497 m_caretPosition
+1, // New caret position
501 DeleteSelectedContent();
505 // Shouldn't this be in Do()?
506 if (GetLastPosition() == -1)
510 m_caretPosition
= -1;
512 SetDefaultStyleToCursorStyle();
517 BeginBatchUndo(_("Insert Text"));
519 long newPos
= m_caretPosition
;
520 DeleteSelectedContent(& newPos
);
522 wxString str
= (wxChar
) event
.GetKeyCode();
523 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
527 SetDefaultStyleToCursorStyle();
528 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
536 /// Delete content if there is a selection, e.g. when pressing a key.
537 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
541 long pos
= m_selectionRange
.GetStart();
542 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
543 m_caretPosition
, // Current caret position
544 pos
, // New caret position
546 m_selectionRange
.SetRange(-2, -2);
556 /// Keyboard navigation
560 Left: left one character
561 Right: right one character
564 Ctrl-Left: left one word
565 Ctrl-Right: right one word
566 Ctrl-Up: previous paragraph start
567 Ctrl-Down: next start of paragraph
570 Ctrl-Home: start of document
571 Ctrl-End: end of document
573 Page-Down: Down a screen
577 Ctrl-Alt-PgUp: Start of window
578 Ctrl-Alt-PgDn: End of window
579 F8: Start selection mode
580 Esc: End selection mode
582 Adding Shift does the above but starts/extends selection.
587 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
589 bool success
= false;
591 if (keyCode
== WXK_RIGHT
)
593 if (flags
& wxRICHTEXT_CTRL_DOWN
)
594 success
= WordRight(1, flags
);
596 success
= MoveRight(1, flags
);
598 else if (keyCode
== WXK_LEFT
)
600 if (flags
& wxRICHTEXT_CTRL_DOWN
)
601 success
= WordLeft(1, flags
);
603 success
= MoveLeft(1, flags
);
605 else if (keyCode
== WXK_UP
)
607 if (flags
& wxRICHTEXT_CTRL_DOWN
)
608 success
= MoveToParagraphStart(flags
);
610 success
= MoveUp(1, flags
);
612 else if (keyCode
== WXK_DOWN
)
614 if (flags
& wxRICHTEXT_CTRL_DOWN
)
615 success
= MoveToParagraphEnd(flags
);
617 success
= MoveDown(1, flags
);
619 else if (keyCode
== WXK_PAGEUP
)
621 success
= PageUp(1, flags
);
623 else if (keyCode
== WXK_PAGEDOWN
)
625 success
= PageDown(1, flags
);
627 else if (keyCode
== WXK_HOME
)
629 if (flags
& wxRICHTEXT_CTRL_DOWN
)
630 success
= MoveHome(flags
);
632 success
= MoveToLineStart(flags
);
634 else if (keyCode
== WXK_END
)
636 if (flags
& wxRICHTEXT_CTRL_DOWN
)
637 success
= MoveEnd(flags
);
639 success
= MoveToLineEnd(flags
);
644 ScrollIntoView(m_caretPosition
, keyCode
);
645 SetDefaultStyleToCursorStyle();
651 /// Extend the selection. Selections are in caret positions.
652 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
654 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
656 // If not currently selecting, start selecting
657 if (m_selectionRange
.GetStart() == -2)
659 m_selectionAnchor
= oldPos
;
662 m_selectionRange
.SetRange(newPos
+1, oldPos
);
664 m_selectionRange
.SetRange(oldPos
+1, newPos
);
668 // Always ensure that the selection range start is greater than
670 if (newPos
> m_selectionAnchor
)
671 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
673 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
676 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
678 wxLogDebug(wxT("Strange selection range"));
687 /// Scroll into view, returning true if we scrolled.
688 /// This takes a _caret_ position.
689 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
691 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
697 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
700 GetViewStart(& startX
, & startY
);
702 startY
= startY
* ppuY
;
705 GetVirtualSize(& sx
, & sy
);
710 wxRect rect
= line
->GetRect();
712 bool scrolled
= false;
714 wxSize clientSize
= GetClientSize();
717 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
719 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
721 // Make it scroll so this item is at the bottom
723 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
724 y
= (int) (0.5 + y
/ppuY
);
728 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
732 else if (rect
.y
< startY
)
734 // Make it scroll so this item is at the top
737 y
= (int) (0.5 + y
/ppuY
);
741 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
747 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
751 // Make it scroll so this item is at the top
754 y
= (int) (0.5 + y
/ppuY
);
758 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
762 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
764 // Make it scroll so this item is at the bottom
766 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
767 y
= (int) (0.5 + y
/ppuY
);
771 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
781 /// Is the given position visible on the screen?
782 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
784 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
790 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
793 GetViewStart(& startX
, & startY
);
795 startY
= startY
* ppuY
;
798 GetVirtualSize(& sx
, & sy
);
803 wxRect rect
= line
->GetRect();
805 wxSize clientSize
= GetClientSize();
807 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
810 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
812 m_caretPosition
= position
;
813 m_caretAtLineStart
= showAtLineStart
;
816 /// Move caret one visual step forward: this may mean setting a flag
817 /// and keeping the same position if we're going from the end of one line
818 /// to the start of the next, which may be the exact same caret position.
819 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
821 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
823 // Only do the check if we're not at the end of the paragraph (where things work OK
825 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
827 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
831 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
833 // We're at the end of a line. See whether we need to
834 // stay at the same actual caret position but change visual
836 if (oldPosition
== lineRange
.GetEnd())
838 if (m_caretAtLineStart
)
840 // We're already at the start of the line, so actually move on now.
841 m_caretPosition
= oldPosition
+ 1;
842 m_caretAtLineStart
= false;
846 // We're showing at the end of the line, so keep to
847 // the same position but indicate that we're to show
848 // at the start of the next line.
849 m_caretPosition
= oldPosition
;
850 m_caretAtLineStart
= true;
852 SetDefaultStyleToCursorStyle();
858 SetDefaultStyleToCursorStyle();
861 /// Move caret one visual step backward: this may mean setting a flag
862 /// and keeping the same position if we're going from the end of one line
863 /// to the start of the next, which may be the exact same caret position.
864 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
866 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
868 // Only do the check if we're not at the start of the paragraph (where things work OK
870 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
872 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
876 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
878 // We're at the start of a line. See whether we need to
879 // stay at the same actual caret position but change visual
881 if (oldPosition
== lineRange
.GetStart())
883 m_caretPosition
= oldPosition
-1;
884 m_caretAtLineStart
= true;
887 else if (oldPosition
== lineRange
.GetEnd())
889 if (m_caretAtLineStart
)
891 // We're at the start of the line, so keep the same caret position
892 // but clear the start-of-line flag.
893 m_caretPosition
= oldPosition
;
894 m_caretAtLineStart
= false;
898 // We're showing at the end of the line, so go back
899 // to the previous character position.
900 m_caretPosition
= oldPosition
- 1;
902 SetDefaultStyleToCursorStyle();
908 SetDefaultStyleToCursorStyle();
912 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
914 long endPos
= GetBuffer().GetRange().GetEnd();
916 if (m_caretPosition
+ noPositions
< endPos
)
918 long oldPos
= m_caretPosition
;
919 long newPos
= m_caretPosition
+ noPositions
;
921 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
925 // Determine by looking at oldPos and m_caretPosition whether
926 // we moved from the end of a line to the start of the next line, in which case
927 // we want to adjust the caret position such that it is positioned at the
928 // start of the next line, rather than jumping past the first character of the
930 if (noPositions
== 1 && !extendSel
)
931 MoveCaretForward(oldPos
);
933 SetCaretPosition(newPos
);
936 SetDefaultStyleToCursorStyle();
947 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
951 if (m_caretPosition
> startPos
- noPositions
+ 1)
953 long oldPos
= m_caretPosition
;
954 long newPos
= m_caretPosition
- noPositions
;
955 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
959 if (noPositions
== 1 && !extendSel
)
960 MoveCaretBack(oldPos
);
962 SetCaretPosition(newPos
);
965 SetDefaultStyleToCursorStyle();
976 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
978 return MoveDown(- noLines
, flags
);
982 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
987 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
988 wxPoint pt
= GetCaret()->GetPosition();
989 long newLine
= lineNumber
+ noLines
;
991 if (lineNumber
!= -1)
995 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
997 if (newLine
> lastLine
)
1007 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1010 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1016 wxClientDC
dc(this);
1018 dc
.SetFont(GetFont());
1020 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1022 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1024 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1025 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1026 // so we view the caret at the start of the line.
1027 bool caretLineStart
= false;
1028 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1030 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1031 wxRichTextRange lineRange
;
1033 lineRange
= thisLine
->GetAbsoluteRange();
1035 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1038 caretLineStart
= true;
1042 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1043 if (para
&& para
->GetRange().GetStart() == newPos
)
1048 long newSelEnd
= newPos
;
1050 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1054 SetCaretPosition(newPos
, caretLineStart
);
1056 SetDefaultStyleToCursorStyle();
1066 /// Move to the end of the paragraph
1067 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1069 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1072 long newPos
= para
->GetRange().GetEnd() - 1;
1073 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1077 SetCaretPosition(newPos
);
1079 SetDefaultStyleToCursorStyle();
1089 /// Move to the start of the paragraph
1090 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1092 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1095 long newPos
= para
->GetRange().GetStart() - 1;
1096 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1100 SetCaretPosition(newPos
);
1102 SetDefaultStyleToCursorStyle();
1112 /// Move to the end of the line
1113 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1115 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1119 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1120 long newPos
= lineRange
.GetEnd();
1121 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1125 SetCaretPosition(newPos
);
1127 SetDefaultStyleToCursorStyle();
1137 /// Move to the start of the line
1138 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1140 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1143 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1144 long newPos
= lineRange
.GetStart()-1;
1146 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1150 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1152 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1154 SetDefaultStyleToCursorStyle();
1164 /// Move to the start of the buffer
1165 bool wxRichTextCtrl::MoveHome(int flags
)
1167 if (m_caretPosition
!= -1)
1169 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1173 SetCaretPosition(-1);
1175 SetDefaultStyleToCursorStyle();
1185 /// Move to the end of the buffer
1186 bool wxRichTextCtrl::MoveEnd(int flags
)
1188 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1190 if (m_caretPosition
!= endPos
)
1192 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1196 SetCaretPosition(endPos
);
1198 SetDefaultStyleToCursorStyle();
1208 /// Move noPages pages up
1209 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1211 return PageDown(- noPages
, flags
);
1214 /// Move noPages pages down
1215 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1217 // Calculate which line occurs noPages * screen height further down.
1218 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1221 wxSize clientSize
= GetClientSize();
1222 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1224 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1227 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1228 long pos
= lineRange
.GetStart()-1;
1229 if (pos
!= m_caretPosition
)
1231 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1233 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1237 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1239 SetDefaultStyleToCursorStyle();
1251 // Finds the caret position for the next word
1252 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1254 long endPos
= GetBuffer().GetRange().GetEnd();
1258 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1260 // First skip current text to space
1261 while (i
< endPos
&& i
> -1)
1263 // i is in character, not caret positions
1264 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1265 if (text
!= wxT(" ") && !text
.empty())
1272 while (i
< endPos
&& i
> -1)
1274 // i is in character, not caret positions
1275 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1276 if (text
.empty()) // End of paragraph, or maybe an image
1277 return wxMax(-1, i
- 1);
1278 else if (text
== wxT(" ") || text
.empty())
1282 // Convert to caret position
1283 return wxMax(-1, i
- 1);
1292 long i
= m_caretPosition
;
1294 // First skip white space
1295 while (i
< endPos
&& i
> -1)
1297 // i is in character, not caret positions
1298 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1299 if (text
.empty()) // End of paragraph, or maybe an image
1301 else if (text
== wxT(" ") || text
.empty())
1306 // Next skip current text to space
1307 while (i
< endPos
&& i
> -1)
1309 // i is in character, not caret positions
1310 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1311 if (text
!= wxT(" ") /* && !text.empty() */)
1324 /// Move n words left
1325 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1327 long pos
= FindNextWordPosition(-1);
1328 if (pos
!= m_caretPosition
)
1330 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1332 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1336 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1338 SetDefaultStyleToCursorStyle();
1348 /// Move n words right
1349 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1351 long pos
= FindNextWordPosition(1);
1352 if (pos
!= m_caretPosition
)
1354 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1356 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1360 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1362 SetDefaultStyleToCursorStyle();
1373 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1375 // Only do sizing optimization for large buffers
1376 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1378 m_fullLayoutRequired
= true;
1379 m_fullLayoutTime
= wxGetLocalTimeMillis();
1380 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1381 LayoutContent(true /* onlyVisibleRect */);
1384 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1392 /// Idle-time processing
1393 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1395 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1397 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1399 m_fullLayoutRequired
= false;
1400 m_fullLayoutTime
= 0;
1401 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1402 ShowPosition(m_fullLayoutSavedPosition
);
1409 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1415 /// Set up scrollbars, e.g. after a resize
1416 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1421 if (GetBuffer().IsEmpty())
1423 SetScrollbars(0, 0, 0, 0, 0, 0);
1427 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1428 // of pixels. See e.g. wxVScrolledWindow for ideas.
1429 int pixelsPerUnit
= 5; // 10;
1430 wxSize clientSize
= GetClientSize();
1432 int maxHeight
= GetBuffer().GetCachedSize().y
;
1434 int unitsY
= maxHeight
/pixelsPerUnit
;
1436 int startX
= 0, startY
= 0;
1438 GetViewStart(& startX
, & startY
);
1440 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1441 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1443 // Move to previous scroll position if
1445 SetScrollbars(0, pixelsPerUnit
,
1447 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1450 /// Paint the background
1451 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1453 wxColour backgroundColour
= GetBackgroundColour();
1454 if (!backgroundColour
.Ok())
1455 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1457 // Clear the background
1458 dc
.SetBrush(wxBrush(backgroundColour
));
1459 dc
.SetPen(*wxTRANSPARENT_PEN
);
1460 wxRect
windowRect(GetClientSize());
1461 windowRect
.x
-= 2; windowRect
.y
-= 2;
1462 windowRect
.width
+= 4; windowRect
.height
+= 4;
1464 // We need to shift the rectangle to take into account
1465 // scrolling. Converting device to logical coordinates.
1466 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1467 dc
.DrawRectangle(windowRect
);
1470 /// Recreate buffer bitmap if necessary
1471 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1474 if (sz
== wxDefaultSize
)
1475 sz
= GetClientSize();
1477 if (sz
.x
< 1 || sz
.y
< 1)
1480 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1481 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1482 return m_bufferBitmap
.Ok();
1485 // ----------------------------------------------------------------------------
1486 // file IO functions
1487 // ----------------------------------------------------------------------------
1489 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1491 bool success
= GetBuffer().LoadFile(filename
, type
);
1493 m_filename
= filename
;
1496 SetInsertionPoint(0);
1499 SetupScrollbars(true);
1507 wxLogError(_("File couldn't be loaded."));
1513 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1515 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1516 if ( filenameToUse
.empty() )
1518 // what kind of message to give? is it an error or a program bug?
1519 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1524 if (GetBuffer().SaveFile(filenameToUse
, type
))
1526 m_filename
= filenameToUse
;
1534 wxLogError(_("The text couldn't be saved."));
1539 // ----------------------------------------------------------------------------
1540 // wxRichTextCtrl specific functionality
1541 // ----------------------------------------------------------------------------
1543 /// Add a new paragraph of text to the end of the buffer
1544 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1546 return GetBuffer().AddParagraph(text
);
1550 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1552 return GetBuffer().AddImage(image
);
1555 // ----------------------------------------------------------------------------
1556 // selection and ranges
1557 // ----------------------------------------------------------------------------
1559 void wxRichTextCtrl::SelectAll()
1561 SetSelection(0, GetLastPosition());
1562 m_selectionAnchor
= -1;
1566 void wxRichTextCtrl::SelectNone()
1568 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1569 SetSelection(-2, -2);
1570 m_selectionAnchor
= -2;
1573 wxString
wxRichTextCtrl::GetStringSelection() const
1576 GetSelection(&from
, &to
);
1578 return GetRange(from
, to
);
1581 // do the window-specific processing after processing the update event
1582 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1583 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1586 wxWindowBase::DoUpdateWindowUI(event
);
1589 if ( event
.GetSetText() )
1591 if ( event
.GetText() != GetValue() )
1592 SetValue(event
.GetText());
1595 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1597 // ----------------------------------------------------------------------------
1599 // ----------------------------------------------------------------------------
1601 wxTextCtrlHitTestResult
1602 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1604 // implement in terms of the other overload as the native ports typically
1605 // can get the position and not (x, y) pair directly (although wxUniv
1606 // directly gets x and y -- and so overrides this method as well)
1608 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1610 if ( rc
!= wxTE_HT_UNKNOWN
)
1612 PositionToXY(pos
, x
, y
);
1618 wxTextCtrlHitTestResult
1619 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1622 wxClientDC
dc((wxRichTextCtrl
*) this);
1623 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1625 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1629 case wxRICHTEXT_HITTEST_BEFORE
:
1630 return wxTE_HT_BEFORE
;
1632 case wxRICHTEXT_HITTEST_AFTER
:
1633 return wxTE_HT_BEYOND
;
1635 case wxRICHTEXT_HITTEST_ON
:
1636 return wxTE_HT_ON_TEXT
;
1639 return wxTE_HT_UNKNOWN
;
1642 // ----------------------------------------------------------------------------
1643 // set/get the controls text
1644 // ----------------------------------------------------------------------------
1646 wxString
wxRichTextCtrl::GetValue() const
1648 return GetBuffer().GetText();
1651 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1653 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1656 void wxRichTextCtrl::SetValue(const wxString
& value
)
1660 // if the text is long enough, it's faster to just set it instead of first
1661 // comparing it with the old one (chances are that it will be different
1662 // anyhow, this comparison is there to avoid flicker for small single-line
1663 // edit controls mostly)
1664 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1666 DoWriteText(value
, false /* not selection only */);
1668 // for compatibility, don't move the cursor when doing SetValue()
1669 SetInsertionPoint(0);
1673 // still send an event for consistency
1677 // we should reset the modified flag even if the value didn't really change
1679 // mark the control as being not dirty - we changed its text, not the
1684 void wxRichTextCtrl::WriteText(const wxString
& value
)
1689 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1691 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1694 void wxRichTextCtrl::AppendText(const wxString
& text
)
1696 SetInsertionPointEnd();
1701 /// Write an image at the current insertion point
1702 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1704 wxRichTextImageBlock imageBlock
;
1706 wxImage image2
= image
;
1707 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1708 return WriteImage(imageBlock
);
1713 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1715 wxRichTextImageBlock imageBlock
;
1718 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1719 return WriteImage(imageBlock
);
1724 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1726 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1729 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1733 wxRichTextImageBlock imageBlock
;
1735 wxImage image
= bitmap
.ConvertToImage();
1736 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1737 return WriteImage(imageBlock
);
1743 /// Insert a newline (actually paragraph) at the current insertion point.
1744 bool wxRichTextCtrl::Newline()
1746 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1750 // ----------------------------------------------------------------------------
1751 // Clipboard operations
1752 // ----------------------------------------------------------------------------
1754 void wxRichTextCtrl::Copy()
1758 wxRichTextRange range
= GetSelectionRange();
1759 GetBuffer().CopyToClipboard(range
);
1763 void wxRichTextCtrl::Cut()
1767 wxRichTextRange range
= GetSelectionRange();
1768 GetBuffer().CopyToClipboard(range
);
1770 DeleteSelectedContent();
1776 void wxRichTextCtrl::Paste()
1780 BeginBatchUndo(_("Paste"));
1782 long newPos
= m_caretPosition
;
1783 DeleteSelectedContent(& newPos
);
1785 GetBuffer().PasteFromClipboard(newPos
);
1791 void wxRichTextCtrl::DeleteSelection()
1793 if (CanDeleteSelection())
1795 DeleteSelectedContent();
1799 bool wxRichTextCtrl::HasSelection() const
1801 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1804 bool wxRichTextCtrl::CanCopy() const
1806 // Can copy if there's a selection
1807 return HasSelection();
1810 bool wxRichTextCtrl::CanCut() const
1812 return HasSelection() && IsEditable();
1815 bool wxRichTextCtrl::CanPaste() const
1817 if ( !IsEditable() )
1820 return GetBuffer().CanPasteFromClipboard();
1823 bool wxRichTextCtrl::CanDeleteSelection() const
1825 return HasSelection() && IsEditable();
1829 // ----------------------------------------------------------------------------
1831 // ----------------------------------------------------------------------------
1833 void wxRichTextCtrl::SetEditable(bool editable
)
1835 m_editable
= editable
;
1838 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1842 m_caretPosition
= pos
- 1;
1845 void wxRichTextCtrl::SetInsertionPointEnd()
1847 long pos
= GetLastPosition();
1848 SetInsertionPoint(pos
);
1851 long wxRichTextCtrl::GetInsertionPoint() const
1853 return m_caretPosition
+1;
1856 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1858 return GetBuffer().GetRange().GetEnd();
1861 // If the return values from and to are the same, there is no
1863 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1865 *from
= m_selectionRange
.GetStart();
1866 *to
= m_selectionRange
.GetEnd();
1869 bool wxRichTextCtrl::IsEditable() const
1874 // ----------------------------------------------------------------------------
1876 // ----------------------------------------------------------------------------
1878 void wxRichTextCtrl::SetSelection(long from
, long to
)
1880 // if from and to are both -1, it means (in wxWidgets) that all text should
1882 if ( (from
== -1) && (to
== -1) )
1885 to
= GetLastPosition();
1888 DoSetSelection(from
, to
);
1891 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1893 m_selectionAnchor
= from
;
1894 m_selectionRange
.SetRange(from
, to
);
1899 // ----------------------------------------------------------------------------
1901 // ----------------------------------------------------------------------------
1903 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1905 BeginBatchUndo(_("Replace"));
1907 DeleteSelectedContent();
1909 DoWriteText(value
, true /* selection only */);
1914 void wxRichTextCtrl::Remove(long from
, long to
)
1918 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1919 m_caretPosition
, // Current caret position
1920 from
, // New caret position
1928 bool wxRichTextCtrl::IsModified() const
1930 return m_buffer
.IsModified();
1933 void wxRichTextCtrl::MarkDirty()
1935 m_buffer
.Modify(true);
1938 void wxRichTextCtrl::DiscardEdits()
1940 m_buffer
.Modify(false);
1941 m_buffer
.GetCommandProcessor()->ClearCommands();
1944 int wxRichTextCtrl::GetNumberOfLines() const
1946 return GetBuffer().GetParagraphCount();
1949 // ----------------------------------------------------------------------------
1950 // Positions <-> coords
1951 // ----------------------------------------------------------------------------
1953 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1955 return GetBuffer().XYToPosition(x
, y
);
1958 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1960 return GetBuffer().PositionToXY(pos
, x
, y
);
1963 // ----------------------------------------------------------------------------
1965 // ----------------------------------------------------------------------------
1967 void wxRichTextCtrl::ShowPosition(long pos
)
1969 if (!IsPositionVisible(pos
))
1970 ScrollIntoView(pos
-1, WXK_DOWN
);
1973 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1975 return GetBuffer().GetParagraphLength(lineNo
);
1978 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1980 return GetBuffer().GetParagraphText(lineNo
);
1983 // ----------------------------------------------------------------------------
1985 // ----------------------------------------------------------------------------
1987 void wxRichTextCtrl::Undo()
1991 GetCommandProcessor()->Undo();
1995 void wxRichTextCtrl::Redo()
1999 GetCommandProcessor()->Redo();
2003 bool wxRichTextCtrl::CanUndo() const
2005 return GetCommandProcessor()->CanUndo();
2008 bool wxRichTextCtrl::CanRedo() const
2010 return GetCommandProcessor()->CanRedo();
2013 // ----------------------------------------------------------------------------
2014 // implemenation details
2015 // ----------------------------------------------------------------------------
2017 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2019 SetValue(event
.GetString());
2020 GetEventHandler()->ProcessEvent(event
);
2023 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2025 // By default, load the first file into the text window.
2026 if (event
.GetNumberOfFiles() > 0)
2028 LoadFile(event
.GetFiles()[0]);
2032 // ----------------------------------------------------------------------------
2033 // text control event processing
2034 // ----------------------------------------------------------------------------
2036 bool wxRichTextCtrl::SendUpdateEvent()
2038 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2039 InitCommandEvent(event
);
2041 return GetEventHandler()->ProcessEvent(event
);
2044 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2046 event
.SetEventObject((wxControlBase
*)this); // const_cast
2048 switch ( m_clientDataType
)
2050 case wxClientData_Void
:
2051 event
.SetClientData(GetClientData());
2054 case wxClientData_Object
:
2055 event
.SetClientObject(GetClientObject());
2058 case wxClientData_None
:
2065 wxSize
wxRichTextCtrl::DoGetBestSize() const
2067 return wxSize(10, 10);
2070 // ----------------------------------------------------------------------------
2071 // standard handlers for standard edit menu events
2072 // ----------------------------------------------------------------------------
2074 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2079 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2084 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2089 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2094 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2099 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2104 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2106 event
.Enable( CanCut() );
2109 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2111 event
.Enable( CanCopy() );
2114 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2116 event
.Enable( CanDeleteSelection() );
2119 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2121 event
.Enable( CanPaste() );
2124 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2126 event
.Enable( CanUndo() );
2127 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2130 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2132 event
.Enable( CanRedo() );
2133 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2136 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2141 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2143 event
.Enable(GetLastPosition() > 0);
2146 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2150 m_contextMenu
= new wxMenu
;
2151 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2152 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2153 m_contextMenu
->AppendSeparator();
2154 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2155 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2156 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2157 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2158 m_contextMenu
->AppendSeparator();
2159 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2161 PopupMenu(m_contextMenu
);
2165 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2167 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2170 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2172 return GetBuffer().SetStyle(range
, style
);
2175 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2177 return GetBuffer().SetDefaultStyle(style
);
2180 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2182 return GetBuffer().GetDefaultStyle();
2185 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2187 return GetBuffer().GetStyle(position
, style
);
2190 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2192 return GetBuffer().GetStyle(position
, style
);
2195 /// Set font, and also the buffer attributes
2196 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2198 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2199 wxControl::SetFont(font
);
2201 wxScrolledWindow::SetFont(font
);
2204 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2206 GetBuffer().SetBasicStyle(attr
);
2207 GetBuffer().SetDefaultStyle(attr
);
2212 /// Transform logical to physical
2213 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2216 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2221 /// Transform physical to logical
2222 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2225 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2230 /// Position the caret
2231 void wxRichTextCtrl::PositionCaret()
2236 //wxLogDebug(wxT("PositionCaret"));
2239 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2241 wxPoint originalPt
= caretRect
.GetPosition();
2242 wxPoint pt
= GetPhysicalPoint(originalPt
);
2243 if (GetCaret()->GetPosition() != pt
)
2245 GetCaret()->Move(pt
);
2246 GetCaret()->SetSize(caretRect
.GetSize());
2251 /// Get the caret height and position for the given character position
2252 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2254 wxClientDC
dc(this);
2255 dc
.SetFont(GetFont());
2262 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2264 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2271 /// Gets the line for the visible caret position. If the caret is
2272 /// shown at the very end of the line, it means the next character is actually
2273 /// on the following line. So let's get the line we're expecting to find
2274 /// if this is the case.
2275 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2277 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2278 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2281 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2282 if (caretPosition
== lineRange
.GetStart()-1 &&
2283 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2285 if (!m_caretAtLineStart
)
2286 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2293 /// Move the caret to the given character position
2294 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2296 if (GetBuffer().GetDirty())
2299 if (pos
<= GetBuffer().GetRange().GetEnd())
2301 SetCaretPosition(pos
, showAtLineStart
);
2311 /// Layout the buffer: which we must do before certain operations, such as
2312 /// setting the caret position.
2313 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2315 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2317 wxRect
availableSpace(GetClientSize());
2318 if (availableSpace
.width
== 0)
2319 availableSpace
.width
= 10;
2320 if (availableSpace
.height
== 0)
2321 availableSpace
.height
= 10;
2323 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2324 if (onlyVisibleRect
)
2326 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2327 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2330 wxClientDC
dc(this);
2331 dc
.SetFont(GetFont());
2335 GetBuffer().Defragment();
2336 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2337 GetBuffer().Layout(dc
, availableSpace
, flags
);
2338 GetBuffer().SetDirty(false);
2347 /// Is all of the selection bold?
2348 bool wxRichTextCtrl::IsSelectionBold() const
2352 wxRichTextAttr attr
;
2353 wxRichTextRange range
= GetSelectionRange();
2354 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2355 attr
.SetFontWeight(wxBOLD
);
2357 return HasCharacterAttributes(range
, attr
);
2361 // If no selection, then we need to combine current style with default style
2362 // to see what the effect would be if we started typing.
2363 wxRichTextAttr attr
;
2364 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2365 if (GetStyle(GetCaretPosition()+1, attr
))
2367 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2368 return attr
.GetFontWeight() == wxBOLD
;
2374 /// Is all of the selection italics?
2375 bool wxRichTextCtrl::IsSelectionItalics() const
2379 wxRichTextRange range
= GetSelectionRange();
2380 wxRichTextAttr attr
;
2381 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2382 attr
.SetFontStyle(wxITALIC
);
2384 return HasCharacterAttributes(range
, attr
);
2388 // If no selection, then we need to combine current style with default style
2389 // to see what the effect would be if we started typing.
2390 wxRichTextAttr attr
;
2391 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2392 if (GetStyle(GetCaretPosition()+1, attr
))
2394 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2395 return attr
.GetFontStyle() == wxITALIC
;
2401 /// Is all of the selection underlined?
2402 bool wxRichTextCtrl::IsSelectionUnderlined() const
2406 wxRichTextRange range
= GetSelectionRange();
2407 wxRichTextAttr attr
;
2408 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2409 attr
.SetFontUnderlined(true);
2411 return HasCharacterAttributes(range
, attr
);
2415 // If no selection, then we need to combine current style with default style
2416 // to see what the effect would be if we started typing.
2417 wxRichTextAttr attr
;
2418 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2419 if (GetStyle(GetCaretPosition()+1, attr
))
2421 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2422 return attr
.GetFontUnderlined();
2428 /// Apply bold to the selection
2429 bool wxRichTextCtrl::ApplyBoldToSelection()
2431 wxRichTextAttr attr
;
2432 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2433 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2436 return SetStyle(GetSelectionRange(), attr
);
2438 SetDefaultStyle(attr
);
2442 /// Apply italic to the selection
2443 bool wxRichTextCtrl::ApplyItalicToSelection()
2445 wxRichTextAttr attr
;
2446 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2447 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2450 return SetStyle(GetSelectionRange(), attr
);
2452 SetDefaultStyle(attr
);
2456 /// Apply underline to the selection
2457 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2459 wxRichTextAttr attr
;
2460 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2461 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2464 return SetStyle(GetSelectionRange(), attr
);
2466 SetDefaultStyle(attr
);
2470 /// Is all of the selection aligned according to the specified flag?
2471 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2475 wxRichTextRange range
= GetSelectionRange();
2476 wxRichTextAttr attr
;
2477 attr
.SetAlignment(alignment
);
2479 return HasParagraphAttributes(range
, attr
);
2483 // If no selection, then we need to get information from the current paragraph.
2484 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2486 return para
->GetAttributes().GetAlignment() == alignment
;
2491 /// Apply alignment to the selection
2492 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2494 wxRichTextAttr attr
;
2495 attr
.SetAlignment(alignment
);
2497 return SetStyle(GetSelectionRange(), attr
);
2500 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2502 return SetStyle(para
->GetRange(), attr
);
2507 /// Sets the default style to the style under the cursor
2508 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2511 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2513 if (GetStyle(GetCaretPosition(), attr
))
2515 SetDefaultStyle(attr
);
2522 /// Returns the first visible position in the current view
2523 long wxRichTextCtrl::GetFirstVisiblePosition() const
2525 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2527 return line
->GetAbsoluteRange().GetStart();