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 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
460 else if (event
.GetKeyCode() == WXK_BACK
)
462 BeginBatchUndo(_("Delete Text"));
464 // Submit range in character positions, which are greater than caret positions,
465 // so subtract 1 for deleted character and add 1 for conversion to character position.
466 if (m_caretPosition
> -1 && !HasSelection())
468 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
469 m_caretPosition
, // Current caret position
470 m_caretPosition
-1, // New caret position
474 DeleteSelectedContent();
478 // Shouldn't this be in Do()?
479 if (GetLastPosition() == -1)
483 m_caretPosition
= -1;
485 SetDefaultStyleToCursorStyle();
488 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
490 else if (event
.GetKeyCode() == WXK_DELETE
)
492 BeginBatchUndo(_("Delete Text"));
494 // Submit range in character positions, which are greater than caret positions,
495 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
497 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
498 m_caretPosition
, // Current caret position
499 m_caretPosition
+1, // New caret position
503 DeleteSelectedContent();
507 // Shouldn't this be in Do()?
508 if (GetLastPosition() == -1)
512 m_caretPosition
= -1;
514 SetDefaultStyleToCursorStyle();
519 BeginBatchUndo(_("Insert Text"));
521 long newPos
= m_caretPosition
;
522 DeleteSelectedContent(& newPos
);
524 wxString str
= (wxChar
) event
.GetKeyCode();
525 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
529 SetDefaultStyleToCursorStyle();
530 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
538 /// Delete content if there is a selection, e.g. when pressing a key.
539 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
543 long pos
= m_selectionRange
.GetStart();
544 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
545 m_caretPosition
, // Current caret position
546 pos
, // New caret position
548 m_selectionRange
.SetRange(-2, -2);
558 /// Keyboard navigation
562 Left: left one character
563 Right: right one character
566 Ctrl-Left: left one word
567 Ctrl-Right: right one word
568 Ctrl-Up: previous paragraph start
569 Ctrl-Down: next start of paragraph
572 Ctrl-Home: start of document
573 Ctrl-End: end of document
575 Page-Down: Down a screen
579 Ctrl-Alt-PgUp: Start of window
580 Ctrl-Alt-PgDn: End of window
581 F8: Start selection mode
582 Esc: End selection mode
584 Adding Shift does the above but starts/extends selection.
589 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
591 bool success
= false;
593 if (keyCode
== WXK_RIGHT
)
595 if (flags
& wxRICHTEXT_CTRL_DOWN
)
596 success
= WordRight(1, flags
);
598 success
= MoveRight(1, flags
);
600 else if (keyCode
== WXK_LEFT
)
602 if (flags
& wxRICHTEXT_CTRL_DOWN
)
603 success
= WordLeft(1, flags
);
605 success
= MoveLeft(1, flags
);
607 else if (keyCode
== WXK_UP
)
609 if (flags
& wxRICHTEXT_CTRL_DOWN
)
610 success
= MoveToParagraphStart(flags
);
612 success
= MoveUp(1, flags
);
614 else if (keyCode
== WXK_DOWN
)
616 if (flags
& wxRICHTEXT_CTRL_DOWN
)
617 success
= MoveToParagraphEnd(flags
);
619 success
= MoveDown(1, flags
);
621 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_PRIOR
)
623 success
= PageUp(1, flags
);
625 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NEXT
)
627 success
= PageDown(1, flags
);
629 else if (keyCode
== WXK_HOME
)
631 if (flags
& wxRICHTEXT_CTRL_DOWN
)
632 success
= MoveHome(flags
);
634 success
= MoveToLineStart(flags
);
636 else if (keyCode
== WXK_END
)
638 if (flags
& wxRICHTEXT_CTRL_DOWN
)
639 success
= MoveEnd(flags
);
641 success
= MoveToLineEnd(flags
);
646 ScrollIntoView(m_caretPosition
, keyCode
);
647 SetDefaultStyleToCursorStyle();
653 /// Extend the selection. Selections are in caret positions.
654 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
656 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
658 // If not currently selecting, start selecting
659 if (m_selectionRange
.GetStart() == -2)
661 m_selectionAnchor
= oldPos
;
664 m_selectionRange
.SetRange(newPos
+1, oldPos
);
666 m_selectionRange
.SetRange(oldPos
+1, newPos
);
670 // Always ensure that the selection range start is greater than
672 if (newPos
> m_selectionAnchor
)
673 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
675 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
678 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
680 wxLogDebug(wxT("Strange selection range"));
689 /// Scroll into view, returning true if we scrolled.
690 /// This takes a _caret_ position.
691 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
693 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
699 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
702 GetViewStart(& startX
, & startY
);
704 startY
= startY
* ppuY
;
707 GetVirtualSize(& sx
, & sy
);
712 wxRect rect
= line
->GetRect();
714 bool scrolled
= false;
716 wxSize clientSize
= GetClientSize();
719 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_NEXT
|| keyCode
== WXK_PAGEDOWN
)
721 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
723 // Make it scroll so this item is at the bottom
725 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
726 y
= (int) (0.5 + y
/ppuY
);
730 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
734 else if (rect
.y
< startY
)
736 // Make it scroll so this item is at the top
739 y
= (int) (0.5 + y
/ppuY
);
743 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
749 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PRIOR
|| keyCode
== WXK_PAGEUP
)
753 // Make it scroll so this item is at the top
756 y
= (int) (0.5 + y
/ppuY
);
760 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
764 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
766 // Make it scroll so this item is at the bottom
768 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
769 y
= (int) (0.5 + y
/ppuY
);
773 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
783 /// Is the given position visible on the screen?
784 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
786 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
792 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
795 GetViewStart(& startX
, & startY
);
797 startY
= startY
* ppuY
;
800 GetVirtualSize(& sx
, & sy
);
805 wxRect rect
= line
->GetRect();
807 wxSize clientSize
= GetClientSize();
809 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
812 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
814 m_caretPosition
= position
;
815 m_caretAtLineStart
= showAtLineStart
;
818 /// Move caret one visual step forward: this may mean setting a flag
819 /// and keeping the same position if we're going from the end of one line
820 /// to the start of the next, which may be the exact same caret position.
821 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
823 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
825 // Only do the check if we're not at the end of the paragraph (where things work OK
827 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
829 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
833 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
835 // We're at the end of a line. See whether we need to
836 // stay at the same actual caret position but change visual
838 if (oldPosition
== lineRange
.GetEnd())
840 if (m_caretAtLineStart
)
842 // We're already at the start of the line, so actually move on now.
843 m_caretPosition
= oldPosition
+ 1;
844 m_caretAtLineStart
= false;
848 // We're showing at the end of the line, so keep to
849 // the same position but indicate that we're to show
850 // at the start of the next line.
851 m_caretPosition
= oldPosition
;
852 m_caretAtLineStart
= true;
854 SetDefaultStyleToCursorStyle();
860 SetDefaultStyleToCursorStyle();
863 /// Move caret one visual step backward: this may mean setting a flag
864 /// and keeping the same position if we're going from the end of one line
865 /// to the start of the next, which may be the exact same caret position.
866 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
868 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
870 // Only do the check if we're not at the start of the paragraph (where things work OK
872 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
874 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
878 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
880 // We're at the start of a line. See whether we need to
881 // stay at the same actual caret position but change visual
883 if (oldPosition
== lineRange
.GetStart())
885 m_caretPosition
= oldPosition
-1;
886 m_caretAtLineStart
= true;
889 else if (oldPosition
== lineRange
.GetEnd())
891 if (m_caretAtLineStart
)
893 // We're at the start of the line, so keep the same caret position
894 // but clear the start-of-line flag.
895 m_caretPosition
= oldPosition
;
896 m_caretAtLineStart
= false;
900 // We're showing at the end of the line, so go back
901 // to the previous character position.
902 m_caretPosition
= oldPosition
- 1;
904 SetDefaultStyleToCursorStyle();
910 SetDefaultStyleToCursorStyle();
914 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
916 long endPos
= GetBuffer().GetRange().GetEnd();
918 if (m_caretPosition
+ noPositions
< endPos
)
920 long oldPos
= m_caretPosition
;
921 long newPos
= m_caretPosition
+ noPositions
;
923 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
927 // Determine by looking at oldPos and m_caretPosition whether
928 // we moved from the end of a line to the start of the next line, in which case
929 // we want to adjust the caret position such that it is positioned at the
930 // start of the next line, rather than jumping past the first character of the
932 if (noPositions
== 1 && !extendSel
)
933 MoveCaretForward(oldPos
);
935 SetCaretPosition(newPos
);
938 SetDefaultStyleToCursorStyle();
949 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
953 if (m_caretPosition
> startPos
- noPositions
+ 1)
955 long oldPos
= m_caretPosition
;
956 long newPos
= m_caretPosition
- noPositions
;
957 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
961 if (noPositions
== 1 && !extendSel
)
962 MoveCaretBack(oldPos
);
964 SetCaretPosition(newPos
);
967 SetDefaultStyleToCursorStyle();
978 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
980 return MoveDown(- noLines
, flags
);
984 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
989 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
990 wxPoint pt
= GetCaret()->GetPosition();
991 long newLine
= lineNumber
+ noLines
;
993 if (lineNumber
!= -1)
997 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
999 if (newLine
> lastLine
)
1009 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1012 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1018 wxClientDC
dc(this);
1020 dc
.SetFont(GetFont());
1022 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1024 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1026 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1027 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1028 // so we view the caret at the start of the line.
1029 bool caretLineStart
= false;
1030 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1032 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1033 wxRichTextRange lineRange
;
1035 lineRange
= thisLine
->GetAbsoluteRange();
1037 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1040 caretLineStart
= true;
1044 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1045 if (para
&& para
->GetRange().GetStart() == newPos
)
1050 long newSelEnd
= newPos
;
1052 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1056 SetCaretPosition(newPos
, caretLineStart
);
1058 SetDefaultStyleToCursorStyle();
1068 /// Move to the end of the paragraph
1069 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1071 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1074 long newPos
= para
->GetRange().GetEnd() - 1;
1075 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1079 SetCaretPosition(newPos
);
1081 SetDefaultStyleToCursorStyle();
1091 /// Move to the start of the paragraph
1092 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1094 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1097 long newPos
= para
->GetRange().GetStart() - 1;
1098 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1102 SetCaretPosition(newPos
);
1104 SetDefaultStyleToCursorStyle();
1114 /// Move to the end of the line
1115 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1117 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1121 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1122 long newPos
= lineRange
.GetEnd();
1123 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1127 SetCaretPosition(newPos
);
1129 SetDefaultStyleToCursorStyle();
1139 /// Move to the start of the line
1140 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1142 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1145 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1146 long newPos
= lineRange
.GetStart()-1;
1148 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1152 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1154 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1156 SetDefaultStyleToCursorStyle();
1166 /// Move to the start of the buffer
1167 bool wxRichTextCtrl::MoveHome(int flags
)
1169 if (m_caretPosition
!= -1)
1171 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1175 SetCaretPosition(-1);
1177 SetDefaultStyleToCursorStyle();
1187 /// Move to the end of the buffer
1188 bool wxRichTextCtrl::MoveEnd(int flags
)
1190 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1192 if (m_caretPosition
!= endPos
)
1194 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1198 SetCaretPosition(endPos
);
1200 SetDefaultStyleToCursorStyle();
1210 /// Move noPages pages up
1211 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1213 return PageDown(- noPages
, flags
);
1216 /// Move noPages pages down
1217 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1219 // Calculate which line occurs noPages * screen height further down.
1220 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1223 wxSize clientSize
= GetClientSize();
1224 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1226 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1229 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1230 long pos
= lineRange
.GetStart()-1;
1231 if (pos
!= m_caretPosition
)
1233 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1235 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1239 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1241 SetDefaultStyleToCursorStyle();
1253 // Finds the caret position for the next word
1254 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1256 long endPos
= GetBuffer().GetRange().GetEnd();
1260 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1262 // First skip current text to space
1263 while (i
< endPos
&& i
> -1)
1265 // i is in character, not caret positions
1266 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1267 if (text
!= wxT(" ") && !text
.empty())
1274 while (i
< endPos
&& i
> -1)
1276 // i is in character, not caret positions
1277 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1278 if (text
.empty()) // End of paragraph, or maybe an image
1279 return wxMax(-1, i
- 1);
1280 else if (text
== wxT(" ") || text
.empty())
1284 // Convert to caret position
1285 return wxMax(-1, i
- 1);
1294 long i
= m_caretPosition
;
1296 // First skip white space
1297 while (i
< endPos
&& i
> -1)
1299 // i is in character, not caret positions
1300 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1301 if (text
.empty()) // End of paragraph, or maybe an image
1303 else if (text
== wxT(" ") || text
.empty())
1308 // Next skip current text to space
1309 while (i
< endPos
&& i
> -1)
1311 // i is in character, not caret positions
1312 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1313 if (text
!= wxT(" ") /* && !text.empty() */)
1326 /// Move n words left
1327 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1329 long pos
= FindNextWordPosition(-1);
1330 if (pos
!= m_caretPosition
)
1332 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1334 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1338 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1340 SetDefaultStyleToCursorStyle();
1350 /// Move n words right
1351 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1353 long pos
= FindNextWordPosition(1);
1354 if (pos
!= m_caretPosition
)
1356 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1358 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1362 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1364 SetDefaultStyleToCursorStyle();
1375 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1377 // Only do sizing optimization for large buffers
1378 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1380 m_fullLayoutRequired
= true;
1381 m_fullLayoutTime
= wxGetLocalTimeMillis();
1382 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1383 LayoutContent(true /* onlyVisibleRect */);
1386 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1394 /// Idle-time processing
1395 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1397 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1399 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1401 m_fullLayoutRequired
= false;
1402 m_fullLayoutTime
= 0;
1403 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1404 ShowPosition(m_fullLayoutSavedPosition
);
1411 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1417 /// Set up scrollbars, e.g. after a resize
1418 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1423 if (GetBuffer().IsEmpty())
1425 SetScrollbars(0, 0, 0, 0, 0, 0);
1429 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1430 // of pixels. See e.g. wxVScrolledWindow for ideas.
1431 int pixelsPerUnit
= 5; // 10;
1432 wxSize clientSize
= GetClientSize();
1434 int maxHeight
= GetBuffer().GetCachedSize().y
;
1436 int unitsY
= maxHeight
/pixelsPerUnit
;
1438 int startX
= 0, startY
= 0;
1440 GetViewStart(& startX
, & startY
);
1442 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1443 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1445 // Move to previous scroll position if
1447 SetScrollbars(0, pixelsPerUnit
,
1449 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1452 /// Paint the background
1453 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1455 wxColour backgroundColour
= GetBackgroundColour();
1456 if (!backgroundColour
.Ok())
1457 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1459 // Clear the background
1460 dc
.SetBrush(wxBrush(backgroundColour
));
1461 dc
.SetPen(*wxTRANSPARENT_PEN
);
1462 wxRect
windowRect(GetClientSize());
1463 windowRect
.x
-= 2; windowRect
.y
-= 2;
1464 windowRect
.width
+= 4; windowRect
.height
+= 4;
1466 // We need to shift the rectangle to take into account
1467 // scrolling. Converting device to logical coordinates.
1468 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1469 dc
.DrawRectangle(windowRect
);
1472 /// Recreate buffer bitmap if necessary
1473 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1476 if (sz
== wxDefaultSize
)
1477 sz
= GetClientSize();
1479 if (sz
.x
< 1 || sz
.y
< 1)
1482 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1483 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1484 return m_bufferBitmap
.Ok();
1487 // ----------------------------------------------------------------------------
1488 // file IO functions
1489 // ----------------------------------------------------------------------------
1491 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1493 bool success
= GetBuffer().LoadFile(filename
, type
);
1495 m_filename
= filename
;
1498 SetInsertionPoint(0);
1501 SetupScrollbars(true);
1509 wxLogError(_("File couldn't be loaded."));
1515 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1517 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1518 if ( filenameToUse
.empty() )
1520 // what kind of message to give? is it an error or a program bug?
1521 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1526 if (GetBuffer().SaveFile(filenameToUse
, type
))
1528 m_filename
= filenameToUse
;
1536 wxLogError(_("The text couldn't be saved."));
1541 // ----------------------------------------------------------------------------
1542 // wxRichTextCtrl specific functionality
1543 // ----------------------------------------------------------------------------
1545 /// Add a new paragraph of text to the end of the buffer
1546 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1548 return GetBuffer().AddParagraph(text
);
1552 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1554 return GetBuffer().AddImage(image
);
1557 // ----------------------------------------------------------------------------
1558 // selection and ranges
1559 // ----------------------------------------------------------------------------
1561 void wxRichTextCtrl::SelectAll()
1563 SetSelection(0, GetLastPosition());
1564 m_selectionAnchor
= -1;
1568 void wxRichTextCtrl::SelectNone()
1570 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1571 SetSelection(-2, -2);
1572 m_selectionAnchor
= -2;
1575 wxString
wxRichTextCtrl::GetStringSelection() const
1578 GetSelection(&from
, &to
);
1580 return GetRange(from
, to
);
1583 // do the window-specific processing after processing the update event
1584 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1586 if ( event
.GetSetEnabled() )
1587 Enable(event
.GetEnabled());
1589 if ( event
.GetSetText() )
1591 if ( event
.GetText() != GetValue() )
1592 SetValue(event
.GetText());
1596 // ----------------------------------------------------------------------------
1598 // ----------------------------------------------------------------------------
1600 wxTextCtrlHitTestResult
1601 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1603 // implement in terms of the other overload as the native ports typically
1604 // can get the position and not (x, y) pair directly (although wxUniv
1605 // directly gets x and y -- and so overrides this method as well)
1607 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1609 if ( rc
!= wxTE_HT_UNKNOWN
)
1611 PositionToXY(pos
, x
, y
);
1617 wxTextCtrlHitTestResult
1618 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1621 wxClientDC
dc((wxRichTextCtrl
*) this);
1622 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1624 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1628 case wxRICHTEXT_HITTEST_BEFORE
:
1629 return wxTE_HT_BEFORE
;
1631 case wxRICHTEXT_HITTEST_AFTER
:
1632 return wxTE_HT_BEYOND
;
1634 case wxRICHTEXT_HITTEST_ON
:
1635 return wxTE_HT_ON_TEXT
;
1638 return wxTE_HT_UNKNOWN
;
1641 // ----------------------------------------------------------------------------
1642 // set/get the controls text
1643 // ----------------------------------------------------------------------------
1645 wxString
wxRichTextCtrl::GetValue() const
1647 return GetBuffer().GetText();
1650 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1652 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1655 void wxRichTextCtrl::SetValue(const wxString
& value
)
1659 // if the text is long enough, it's faster to just set it instead of first
1660 // comparing it with the old one (chances are that it will be different
1661 // anyhow, this comparison is there to avoid flicker for small single-line
1662 // edit controls mostly)
1663 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1665 DoWriteText(value
, false /* not selection only */);
1667 // for compatibility, don't move the cursor when doing SetValue()
1668 SetInsertionPoint(0);
1672 // still send an event for consistency
1676 // we should reset the modified flag even if the value didn't really change
1678 // mark the control as being not dirty - we changed its text, not the
1683 void wxRichTextCtrl::WriteText(const wxString
& value
)
1688 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1690 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1693 void wxRichTextCtrl::AppendText(const wxString
& text
)
1695 SetInsertionPointEnd();
1700 /// Write an image at the current insertion point
1701 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1703 wxRichTextImageBlock imageBlock
;
1705 wxImage image2
= image
;
1706 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1707 return WriteImage(imageBlock
);
1712 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1714 wxRichTextImageBlock imageBlock
;
1717 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1718 return WriteImage(imageBlock
);
1723 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1725 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1728 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1732 wxRichTextImageBlock imageBlock
;
1734 wxImage image
= bitmap
.ConvertToImage();
1735 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1736 return WriteImage(imageBlock
);
1742 /// Insert a newline (actually paragraph) at the current insertion point.
1743 bool wxRichTextCtrl::Newline()
1745 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1749 // ----------------------------------------------------------------------------
1750 // Clipboard operations
1751 // ----------------------------------------------------------------------------
1753 void wxRichTextCtrl::Copy()
1757 wxRichTextRange range
= GetSelectionRange();
1758 GetBuffer().CopyToClipboard(range
);
1762 void wxRichTextCtrl::Cut()
1766 wxRichTextRange range
= GetSelectionRange();
1767 GetBuffer().CopyToClipboard(range
);
1769 DeleteSelectedContent();
1775 void wxRichTextCtrl::Paste()
1779 BeginBatchUndo(_("Paste"));
1781 long newPos
= m_caretPosition
;
1782 DeleteSelectedContent(& newPos
);
1784 GetBuffer().PasteFromClipboard(newPos
);
1790 void wxRichTextCtrl::DeleteSelection()
1792 if (CanDeleteSelection())
1794 DeleteSelectedContent();
1798 bool wxRichTextCtrl::HasSelection() const
1800 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1803 bool wxRichTextCtrl::CanCopy() const
1805 // Can copy if there's a selection
1806 return HasSelection();
1809 bool wxRichTextCtrl::CanCut() const
1811 return HasSelection() && IsEditable();
1814 bool wxRichTextCtrl::CanPaste() const
1816 if ( !IsEditable() )
1819 return GetBuffer().CanPasteFromClipboard();
1822 bool wxRichTextCtrl::CanDeleteSelection() const
1824 return HasSelection() && IsEditable();
1828 // ----------------------------------------------------------------------------
1830 // ----------------------------------------------------------------------------
1832 void wxRichTextCtrl::SetEditable(bool editable
)
1834 m_editable
= editable
;
1837 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1841 m_caretPosition
= pos
- 1;
1844 void wxRichTextCtrl::SetInsertionPointEnd()
1846 long pos
= GetLastPosition();
1847 SetInsertionPoint(pos
);
1850 long wxRichTextCtrl::GetInsertionPoint() const
1852 return m_caretPosition
+1;
1855 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1857 return GetBuffer().GetRange().GetEnd();
1860 // If the return values from and to are the same, there is no
1862 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1864 *from
= m_selectionRange
.GetStart();
1865 *to
= m_selectionRange
.GetEnd();
1868 bool wxRichTextCtrl::IsEditable() const
1873 // ----------------------------------------------------------------------------
1875 // ----------------------------------------------------------------------------
1877 void wxRichTextCtrl::SetSelection(long from
, long to
)
1879 // if from and to are both -1, it means (in wxWidgets) that all text should
1881 if ( (from
== -1) && (to
== -1) )
1884 to
= GetLastPosition();
1887 DoSetSelection(from
, to
);
1890 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1892 m_selectionAnchor
= from
;
1893 m_selectionRange
.SetRange(from
, to
);
1898 // ----------------------------------------------------------------------------
1900 // ----------------------------------------------------------------------------
1902 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1904 BeginBatchUndo(_("Replace"));
1906 DeleteSelectedContent();
1908 DoWriteText(value
, true /* selection only */);
1913 void wxRichTextCtrl::Remove(long from
, long to
)
1917 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1918 m_caretPosition
, // Current caret position
1919 from
, // New caret position
1927 bool wxRichTextCtrl::IsModified() const
1929 return m_buffer
.IsModified();
1932 void wxRichTextCtrl::MarkDirty()
1934 m_buffer
.Modify(true);
1937 void wxRichTextCtrl::DiscardEdits()
1939 m_buffer
.Modify(false);
1940 m_buffer
.GetCommandProcessor()->ClearCommands();
1943 int wxRichTextCtrl::GetNumberOfLines() const
1945 return GetBuffer().GetParagraphCount();
1948 // ----------------------------------------------------------------------------
1949 // Positions <-> coords
1950 // ----------------------------------------------------------------------------
1952 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1954 return GetBuffer().XYToPosition(x
, y
);
1957 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1959 return GetBuffer().PositionToXY(pos
, x
, y
);
1962 // ----------------------------------------------------------------------------
1964 // ----------------------------------------------------------------------------
1966 void wxRichTextCtrl::ShowPosition(long pos
)
1968 if (!IsPositionVisible(pos
))
1969 ScrollIntoView(pos
-1, WXK_DOWN
);
1972 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1974 return GetBuffer().GetParagraphLength(lineNo
);
1977 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1979 return GetBuffer().GetParagraphText(lineNo
);
1982 // ----------------------------------------------------------------------------
1984 // ----------------------------------------------------------------------------
1986 void wxRichTextCtrl::Undo()
1990 GetCommandProcessor()->Undo();
1994 void wxRichTextCtrl::Redo()
1998 GetCommandProcessor()->Redo();
2002 bool wxRichTextCtrl::CanUndo() const
2004 return GetCommandProcessor()->CanUndo();
2007 bool wxRichTextCtrl::CanRedo() const
2009 return GetCommandProcessor()->CanRedo();
2012 // ----------------------------------------------------------------------------
2013 // implemenation details
2014 // ----------------------------------------------------------------------------
2016 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2018 SetValue(event
.GetString());
2019 GetEventHandler()->ProcessEvent(event
);
2022 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2024 // By default, load the first file into the text window.
2025 if (event
.GetNumberOfFiles() > 0)
2027 LoadFile(event
.GetFiles()[0]);
2031 // ----------------------------------------------------------------------------
2032 // text control event processing
2033 // ----------------------------------------------------------------------------
2035 bool wxRichTextCtrl::SendUpdateEvent()
2037 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2038 InitCommandEvent(event
);
2040 return GetEventHandler()->ProcessEvent(event
);
2043 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2045 event
.SetEventObject((wxControlBase
*)this); // const_cast
2047 switch ( m_clientDataType
)
2049 case wxClientData_Void
:
2050 event
.SetClientData(GetClientData());
2053 case wxClientData_Object
:
2054 event
.SetClientObject(GetClientObject());
2057 case wxClientData_None
:
2064 wxSize
wxRichTextCtrl::DoGetBestSize() const
2066 return wxSize(10, 10);
2069 // ----------------------------------------------------------------------------
2070 // standard handlers for standard edit menu events
2071 // ----------------------------------------------------------------------------
2073 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2078 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2083 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2088 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2093 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2098 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2103 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2105 event
.Enable( CanCut() );
2108 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2110 event
.Enable( CanCopy() );
2113 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2115 event
.Enable( CanDeleteSelection() );
2118 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2120 event
.Enable( CanPaste() );
2123 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2125 event
.Enable( CanUndo() );
2126 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2129 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2131 event
.Enable( CanRedo() );
2132 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2135 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2140 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2142 event
.Enable(GetLastPosition() > 0);
2145 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2149 m_contextMenu
= new wxMenu
;
2150 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2151 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2152 m_contextMenu
->AppendSeparator();
2153 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2154 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2155 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2156 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2157 m_contextMenu
->AppendSeparator();
2158 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2160 PopupMenu(m_contextMenu
);
2164 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2166 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2169 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2171 return GetBuffer().SetStyle(range
, style
);
2174 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2176 return GetBuffer().SetDefaultStyle(style
);
2179 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2181 return GetBuffer().GetDefaultStyle();
2184 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2186 return GetBuffer().GetStyle(position
, style
);
2189 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2191 return GetBuffer().GetStyle(position
, style
);
2194 /// Set font, and also the buffer attributes
2195 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2197 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2198 wxControl::SetFont(font
);
2200 wxScrolledWindow::SetFont(font
);
2203 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2205 GetBuffer().SetBasicStyle(attr
);
2206 GetBuffer().SetDefaultStyle(attr
);
2211 /// Transform logical to physical
2212 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2215 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2220 /// Transform physical to logical
2221 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2224 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2229 /// Position the caret
2230 void wxRichTextCtrl::PositionCaret()
2235 //wxLogDebug(wxT("PositionCaret"));
2238 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2240 wxPoint originalPt
= caretRect
.GetPosition();
2241 wxPoint pt
= GetPhysicalPoint(originalPt
);
2242 if (GetCaret()->GetPosition() != pt
)
2244 GetCaret()->Move(pt
);
2245 GetCaret()->SetSize(caretRect
.GetSize());
2250 /// Get the caret height and position for the given character position
2251 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2253 wxClientDC
dc(this);
2254 dc
.SetFont(GetFont());
2261 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2263 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2270 /// Gets the line for the visible caret position. If the caret is
2271 /// shown at the very end of the line, it means the next character is actually
2272 /// on the following line. So let's get the line we're expecting to find
2273 /// if this is the case.
2274 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2276 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2277 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2280 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2281 if (caretPosition
== lineRange
.GetStart()-1 &&
2282 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2284 if (!m_caretAtLineStart
)
2285 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2292 /// Move the caret to the given character position
2293 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2295 if (GetBuffer().GetDirty())
2298 if (pos
<= GetBuffer().GetRange().GetEnd())
2300 SetCaretPosition(pos
, showAtLineStart
);
2310 /// Layout the buffer: which we must do before certain operations, such as
2311 /// setting the caret position.
2312 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2314 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2316 wxRect
availableSpace(GetClientSize());
2317 if (availableSpace
.width
== 0)
2318 availableSpace
.width
= 10;
2319 if (availableSpace
.height
== 0)
2320 availableSpace
.height
= 10;
2322 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2323 if (onlyVisibleRect
)
2325 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2326 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2329 wxClientDC
dc(this);
2330 dc
.SetFont(GetFont());
2334 GetBuffer().Defragment();
2335 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2336 GetBuffer().Layout(dc
, availableSpace
, flags
);
2337 GetBuffer().SetDirty(false);
2346 /// Is all of the selection bold?
2347 bool wxRichTextCtrl::IsSelectionBold() const
2351 wxRichTextAttr attr
;
2352 wxRichTextRange range
= GetSelectionRange();
2353 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2354 attr
.SetFontWeight(wxBOLD
);
2356 return HasCharacterAttributes(range
, attr
);
2360 // If no selection, then we need to combine current style with default style
2361 // to see what the effect would be if we started typing.
2362 wxRichTextAttr attr
;
2363 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2364 if (GetStyle(GetCaretPosition()+1, attr
))
2366 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2367 return attr
.GetFontWeight() == wxBOLD
;
2373 /// Is all of the selection italics?
2374 bool wxRichTextCtrl::IsSelectionItalics() const
2378 wxRichTextRange range
= GetSelectionRange();
2379 wxRichTextAttr attr
;
2380 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2381 attr
.SetFontStyle(wxITALIC
);
2383 return HasCharacterAttributes(range
, attr
);
2387 // If no selection, then we need to combine current style with default style
2388 // to see what the effect would be if we started typing.
2389 wxRichTextAttr attr
;
2390 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2391 if (GetStyle(GetCaretPosition()+1, attr
))
2393 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2394 return attr
.GetFontStyle() == wxITALIC
;
2400 /// Is all of the selection underlined?
2401 bool wxRichTextCtrl::IsSelectionUnderlined() const
2405 wxRichTextRange range
= GetSelectionRange();
2406 wxRichTextAttr attr
;
2407 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2408 attr
.SetFontUnderlined(true);
2410 return HasCharacterAttributes(range
, attr
);
2414 // If no selection, then we need to combine current style with default style
2415 // to see what the effect would be if we started typing.
2416 wxRichTextAttr attr
;
2417 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2418 if (GetStyle(GetCaretPosition()+1, attr
))
2420 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2421 return attr
.GetFontUnderlined();
2427 /// Apply bold to the selection
2428 bool wxRichTextCtrl::ApplyBoldToSelection()
2430 wxRichTextAttr attr
;
2431 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2432 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2435 return SetStyle(GetSelectionRange(), attr
);
2437 SetDefaultStyle(attr
);
2441 /// Apply italic to the selection
2442 bool wxRichTextCtrl::ApplyItalicToSelection()
2444 wxRichTextAttr attr
;
2445 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2446 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2449 return SetStyle(GetSelectionRange(), attr
);
2451 SetDefaultStyle(attr
);
2455 /// Apply underline to the selection
2456 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2458 wxRichTextAttr attr
;
2459 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2460 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2463 return SetStyle(GetSelectionRange(), attr
);
2465 SetDefaultStyle(attr
);
2469 /// Is all of the selection aligned according to the specified flag?
2470 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2474 wxRichTextRange range
= GetSelectionRange();
2475 wxRichTextAttr attr
;
2476 attr
.SetAlignment(alignment
);
2478 return HasParagraphAttributes(range
, attr
);
2482 // If no selection, then we need to get information from the current paragraph.
2483 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2485 return para
->GetAttributes().GetAlignment() == alignment
;
2490 /// Apply alignment to the selection
2491 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2493 wxRichTextAttr attr
;
2494 attr
.SetAlignment(alignment
);
2496 return SetStyle(GetSelectionRange(), attr
);
2499 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2501 return SetStyle(para
->GetRange(), attr
);
2506 /// Sets the default style to the style under the cursor
2507 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2510 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2512 if (GetStyle(GetCaretPosition(), attr
))
2514 SetDefaultStyle(attr
);
2521 /// Returns the first visible position in the current view
2522 long wxRichTextCtrl::GetFirstVisiblePosition() const
2524 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2526 return line
->GetAbsoluteRange().GetStart();