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 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1585 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1588 wxWindowBase::DoUpdateWindowUI(event
);
1591 if ( event
.GetSetText() )
1593 if ( event
.GetText() != GetValue() )
1594 SetValue(event
.GetText());
1597 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1599 // ----------------------------------------------------------------------------
1601 // ----------------------------------------------------------------------------
1603 wxTextCtrlHitTestResult
1604 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1606 // implement in terms of the other overload as the native ports typically
1607 // can get the position and not (x, y) pair directly (although wxUniv
1608 // directly gets x and y -- and so overrides this method as well)
1610 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1612 if ( rc
!= wxTE_HT_UNKNOWN
)
1614 PositionToXY(pos
, x
, y
);
1620 wxTextCtrlHitTestResult
1621 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1624 wxClientDC
dc((wxRichTextCtrl
*) this);
1625 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1627 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1631 case wxRICHTEXT_HITTEST_BEFORE
:
1632 return wxTE_HT_BEFORE
;
1634 case wxRICHTEXT_HITTEST_AFTER
:
1635 return wxTE_HT_BEYOND
;
1637 case wxRICHTEXT_HITTEST_ON
:
1638 return wxTE_HT_ON_TEXT
;
1641 return wxTE_HT_UNKNOWN
;
1644 // ----------------------------------------------------------------------------
1645 // set/get the controls text
1646 // ----------------------------------------------------------------------------
1648 wxString
wxRichTextCtrl::GetValue() const
1650 return GetBuffer().GetText();
1653 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1655 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1658 void wxRichTextCtrl::SetValue(const wxString
& value
)
1662 // if the text is long enough, it's faster to just set it instead of first
1663 // comparing it with the old one (chances are that it will be different
1664 // anyhow, this comparison is there to avoid flicker for small single-line
1665 // edit controls mostly)
1666 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1668 DoWriteText(value
, false /* not selection only */);
1670 // for compatibility, don't move the cursor when doing SetValue()
1671 SetInsertionPoint(0);
1675 // still send an event for consistency
1679 // we should reset the modified flag even if the value didn't really change
1681 // mark the control as being not dirty - we changed its text, not the
1686 void wxRichTextCtrl::WriteText(const wxString
& value
)
1691 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1693 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1696 void wxRichTextCtrl::AppendText(const wxString
& text
)
1698 SetInsertionPointEnd();
1703 /// Write an image at the current insertion point
1704 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1706 wxRichTextImageBlock imageBlock
;
1708 wxImage image2
= image
;
1709 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1710 return WriteImage(imageBlock
);
1715 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1717 wxRichTextImageBlock imageBlock
;
1720 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1721 return WriteImage(imageBlock
);
1726 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1728 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1731 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1735 wxRichTextImageBlock imageBlock
;
1737 wxImage image
= bitmap
.ConvertToImage();
1738 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1739 return WriteImage(imageBlock
);
1745 /// Insert a newline (actually paragraph) at the current insertion point.
1746 bool wxRichTextCtrl::Newline()
1748 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1752 // ----------------------------------------------------------------------------
1753 // Clipboard operations
1754 // ----------------------------------------------------------------------------
1756 void wxRichTextCtrl::Copy()
1760 wxRichTextRange range
= GetSelectionRange();
1761 GetBuffer().CopyToClipboard(range
);
1765 void wxRichTextCtrl::Cut()
1769 wxRichTextRange range
= GetSelectionRange();
1770 GetBuffer().CopyToClipboard(range
);
1772 DeleteSelectedContent();
1778 void wxRichTextCtrl::Paste()
1782 BeginBatchUndo(_("Paste"));
1784 long newPos
= m_caretPosition
;
1785 DeleteSelectedContent(& newPos
);
1787 GetBuffer().PasteFromClipboard(newPos
);
1793 void wxRichTextCtrl::DeleteSelection()
1795 if (CanDeleteSelection())
1797 DeleteSelectedContent();
1801 bool wxRichTextCtrl::HasSelection() const
1803 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1806 bool wxRichTextCtrl::CanCopy() const
1808 // Can copy if there's a selection
1809 return HasSelection();
1812 bool wxRichTextCtrl::CanCut() const
1814 return HasSelection() && IsEditable();
1817 bool wxRichTextCtrl::CanPaste() const
1819 if ( !IsEditable() )
1822 return GetBuffer().CanPasteFromClipboard();
1825 bool wxRichTextCtrl::CanDeleteSelection() const
1827 return HasSelection() && IsEditable();
1831 // ----------------------------------------------------------------------------
1833 // ----------------------------------------------------------------------------
1835 void wxRichTextCtrl::SetEditable(bool editable
)
1837 m_editable
= editable
;
1840 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1844 m_caretPosition
= pos
- 1;
1847 void wxRichTextCtrl::SetInsertionPointEnd()
1849 long pos
= GetLastPosition();
1850 SetInsertionPoint(pos
);
1853 long wxRichTextCtrl::GetInsertionPoint() const
1855 return m_caretPosition
+1;
1858 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1860 return GetBuffer().GetRange().GetEnd();
1863 // If the return values from and to are the same, there is no
1865 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1867 *from
= m_selectionRange
.GetStart();
1868 *to
= m_selectionRange
.GetEnd();
1871 bool wxRichTextCtrl::IsEditable() const
1876 // ----------------------------------------------------------------------------
1878 // ----------------------------------------------------------------------------
1880 void wxRichTextCtrl::SetSelection(long from
, long to
)
1882 // if from and to are both -1, it means (in wxWidgets) that all text should
1884 if ( (from
== -1) && (to
== -1) )
1887 to
= GetLastPosition();
1890 DoSetSelection(from
, to
);
1893 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1895 m_selectionAnchor
= from
;
1896 m_selectionRange
.SetRange(from
, to
);
1901 // ----------------------------------------------------------------------------
1903 // ----------------------------------------------------------------------------
1905 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1907 BeginBatchUndo(_("Replace"));
1909 DeleteSelectedContent();
1911 DoWriteText(value
, true /* selection only */);
1916 void wxRichTextCtrl::Remove(long from
, long to
)
1920 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1921 m_caretPosition
, // Current caret position
1922 from
, // New caret position
1930 bool wxRichTextCtrl::IsModified() const
1932 return m_buffer
.IsModified();
1935 void wxRichTextCtrl::MarkDirty()
1937 m_buffer
.Modify(true);
1940 void wxRichTextCtrl::DiscardEdits()
1942 m_buffer
.Modify(false);
1943 m_buffer
.GetCommandProcessor()->ClearCommands();
1946 int wxRichTextCtrl::GetNumberOfLines() const
1948 return GetBuffer().GetParagraphCount();
1951 // ----------------------------------------------------------------------------
1952 // Positions <-> coords
1953 // ----------------------------------------------------------------------------
1955 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1957 return GetBuffer().XYToPosition(x
, y
);
1960 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1962 return GetBuffer().PositionToXY(pos
, x
, y
);
1965 // ----------------------------------------------------------------------------
1967 // ----------------------------------------------------------------------------
1969 void wxRichTextCtrl::ShowPosition(long pos
)
1971 if (!IsPositionVisible(pos
))
1972 ScrollIntoView(pos
-1, WXK_DOWN
);
1975 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1977 return GetBuffer().GetParagraphLength(lineNo
);
1980 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1982 return GetBuffer().GetParagraphText(lineNo
);
1985 // ----------------------------------------------------------------------------
1987 // ----------------------------------------------------------------------------
1989 void wxRichTextCtrl::Undo()
1993 GetCommandProcessor()->Undo();
1997 void wxRichTextCtrl::Redo()
2001 GetCommandProcessor()->Redo();
2005 bool wxRichTextCtrl::CanUndo() const
2007 return GetCommandProcessor()->CanUndo();
2010 bool wxRichTextCtrl::CanRedo() const
2012 return GetCommandProcessor()->CanRedo();
2015 // ----------------------------------------------------------------------------
2016 // implemenation details
2017 // ----------------------------------------------------------------------------
2019 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2021 SetValue(event
.GetString());
2022 GetEventHandler()->ProcessEvent(event
);
2025 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2027 // By default, load the first file into the text window.
2028 if (event
.GetNumberOfFiles() > 0)
2030 LoadFile(event
.GetFiles()[0]);
2034 // ----------------------------------------------------------------------------
2035 // text control event processing
2036 // ----------------------------------------------------------------------------
2038 bool wxRichTextCtrl::SendUpdateEvent()
2040 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2041 InitCommandEvent(event
);
2043 return GetEventHandler()->ProcessEvent(event
);
2046 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2048 event
.SetEventObject((wxControlBase
*)this); // const_cast
2050 switch ( m_clientDataType
)
2052 case wxClientData_Void
:
2053 event
.SetClientData(GetClientData());
2056 case wxClientData_Object
:
2057 event
.SetClientObject(GetClientObject());
2060 case wxClientData_None
:
2067 wxSize
wxRichTextCtrl::DoGetBestSize() const
2069 return wxSize(10, 10);
2072 // ----------------------------------------------------------------------------
2073 // standard handlers for standard edit menu events
2074 // ----------------------------------------------------------------------------
2076 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2081 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2086 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2091 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2096 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2101 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2106 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2108 event
.Enable( CanCut() );
2111 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2113 event
.Enable( CanCopy() );
2116 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2118 event
.Enable( CanDeleteSelection() );
2121 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2123 event
.Enable( CanPaste() );
2126 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2128 event
.Enable( CanUndo() );
2129 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2132 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2134 event
.Enable( CanRedo() );
2135 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2138 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2143 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2145 event
.Enable(GetLastPosition() > 0);
2148 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2152 m_contextMenu
= new wxMenu
;
2153 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2154 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2155 m_contextMenu
->AppendSeparator();
2156 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2157 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2158 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2159 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2160 m_contextMenu
->AppendSeparator();
2161 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2163 PopupMenu(m_contextMenu
);
2167 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2169 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2172 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2174 return GetBuffer().SetStyle(range
, style
);
2177 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2179 return GetBuffer().SetDefaultStyle(style
);
2182 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2184 return GetBuffer().GetDefaultStyle();
2187 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2189 return GetBuffer().GetStyle(position
, style
);
2192 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2194 return GetBuffer().GetStyle(position
, style
);
2197 /// Set font, and also the buffer attributes
2198 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2200 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2201 wxControl::SetFont(font
);
2203 wxScrolledWindow::SetFont(font
);
2206 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2208 GetBuffer().SetBasicStyle(attr
);
2209 GetBuffer().SetDefaultStyle(attr
);
2214 /// Transform logical to physical
2215 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2218 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2223 /// Transform physical to logical
2224 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2227 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2232 /// Position the caret
2233 void wxRichTextCtrl::PositionCaret()
2238 //wxLogDebug(wxT("PositionCaret"));
2241 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2243 wxPoint originalPt
= caretRect
.GetPosition();
2244 wxPoint pt
= GetPhysicalPoint(originalPt
);
2245 if (GetCaret()->GetPosition() != pt
)
2247 GetCaret()->Move(pt
);
2248 GetCaret()->SetSize(caretRect
.GetSize());
2253 /// Get the caret height and position for the given character position
2254 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2256 wxClientDC
dc(this);
2257 dc
.SetFont(GetFont());
2264 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2266 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2273 /// Gets the line for the visible caret position. If the caret is
2274 /// shown at the very end of the line, it means the next character is actually
2275 /// on the following line. So let's get the line we're expecting to find
2276 /// if this is the case.
2277 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2279 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2280 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2283 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2284 if (caretPosition
== lineRange
.GetStart()-1 &&
2285 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2287 if (!m_caretAtLineStart
)
2288 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2295 /// Move the caret to the given character position
2296 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2298 if (GetBuffer().GetDirty())
2301 if (pos
<= GetBuffer().GetRange().GetEnd())
2303 SetCaretPosition(pos
, showAtLineStart
);
2313 /// Layout the buffer: which we must do before certain operations, such as
2314 /// setting the caret position.
2315 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2317 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2319 wxRect
availableSpace(GetClientSize());
2320 if (availableSpace
.width
== 0)
2321 availableSpace
.width
= 10;
2322 if (availableSpace
.height
== 0)
2323 availableSpace
.height
= 10;
2325 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2326 if (onlyVisibleRect
)
2328 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2329 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2332 wxClientDC
dc(this);
2333 dc
.SetFont(GetFont());
2337 GetBuffer().Defragment();
2338 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2339 GetBuffer().Layout(dc
, availableSpace
, flags
);
2340 GetBuffer().SetDirty(false);
2349 /// Is all of the selection bold?
2350 bool wxRichTextCtrl::IsSelectionBold() const
2354 wxRichTextAttr attr
;
2355 wxRichTextRange range
= GetSelectionRange();
2356 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2357 attr
.SetFontWeight(wxBOLD
);
2359 return HasCharacterAttributes(range
, attr
);
2363 // If no selection, then we need to combine current style with default style
2364 // to see what the effect would be if we started typing.
2365 wxRichTextAttr attr
;
2366 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2367 if (GetStyle(GetCaretPosition()+1, attr
))
2369 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2370 return attr
.GetFontWeight() == wxBOLD
;
2376 /// Is all of the selection italics?
2377 bool wxRichTextCtrl::IsSelectionItalics() const
2381 wxRichTextRange range
= GetSelectionRange();
2382 wxRichTextAttr attr
;
2383 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2384 attr
.SetFontStyle(wxITALIC
);
2386 return HasCharacterAttributes(range
, attr
);
2390 // If no selection, then we need to combine current style with default style
2391 // to see what the effect would be if we started typing.
2392 wxRichTextAttr attr
;
2393 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2394 if (GetStyle(GetCaretPosition()+1, attr
))
2396 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2397 return attr
.GetFontStyle() == wxITALIC
;
2403 /// Is all of the selection underlined?
2404 bool wxRichTextCtrl::IsSelectionUnderlined() const
2408 wxRichTextRange range
= GetSelectionRange();
2409 wxRichTextAttr attr
;
2410 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2411 attr
.SetFontUnderlined(true);
2413 return HasCharacterAttributes(range
, attr
);
2417 // If no selection, then we need to combine current style with default style
2418 // to see what the effect would be if we started typing.
2419 wxRichTextAttr attr
;
2420 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2421 if (GetStyle(GetCaretPosition()+1, attr
))
2423 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2424 return attr
.GetFontUnderlined();
2430 /// Apply bold to the selection
2431 bool wxRichTextCtrl::ApplyBoldToSelection()
2433 wxRichTextAttr attr
;
2434 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2435 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2438 return SetStyle(GetSelectionRange(), attr
);
2440 SetDefaultStyle(attr
);
2444 /// Apply italic to the selection
2445 bool wxRichTextCtrl::ApplyItalicToSelection()
2447 wxRichTextAttr attr
;
2448 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2449 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2452 return SetStyle(GetSelectionRange(), attr
);
2454 SetDefaultStyle(attr
);
2458 /// Apply underline to the selection
2459 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2461 wxRichTextAttr attr
;
2462 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2463 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2466 return SetStyle(GetSelectionRange(), attr
);
2468 SetDefaultStyle(attr
);
2472 /// Is all of the selection aligned according to the specified flag?
2473 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2477 wxRichTextRange range
= GetSelectionRange();
2478 wxRichTextAttr attr
;
2479 attr
.SetAlignment(alignment
);
2481 return HasParagraphAttributes(range
, attr
);
2485 // If no selection, then we need to get information from the current paragraph.
2486 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2488 return para
->GetAttributes().GetAlignment() == alignment
;
2493 /// Apply alignment to the selection
2494 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2496 wxRichTextAttr attr
;
2497 attr
.SetAlignment(alignment
);
2499 return SetStyle(GetSelectionRange(), attr
);
2502 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2504 return SetStyle(para
->GetRange(), attr
);
2509 /// Sets the default style to the style under the cursor
2510 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2513 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2515 if (GetStyle(GetCaretPosition(), attr
))
2517 SetDefaultStyle(attr
);
2524 /// Returns the first visible position in the current view
2525 long wxRichTextCtrl::GetFirstVisiblePosition() const
2527 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2529 return line
->GetAbsoluteRange().GetStart();