1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
27 #include "wx/textfile.h"
29 #include "wx/settings.h"
30 #include "wx/filename.h"
31 #include "wx/dcbuffer.h"
33 #include "wx/richtext/richtextctrl.h"
34 #include "wx/arrimpl.cpp"
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
44 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
45 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
47 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
50 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
52 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
53 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
55 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
57 EVT_PAINT(wxRichTextCtrl::OnPaint
)
58 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
59 EVT_IDLE(wxRichTextCtrl::OnIdle
)
60 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
61 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
62 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
63 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
64 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
65 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
66 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
67 EVT_CHAR(wxRichTextCtrl::OnChar
)
68 EVT_SIZE(wxRichTextCtrl::OnSize
)
69 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
70 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
71 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
73 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
74 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
76 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
77 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
79 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
80 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
82 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
83 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
85 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
86 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
88 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
89 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
91 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
92 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
99 wxRichTextCtrl::wxRichTextCtrl()
100 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
101 : wxScrollHelper(this)
107 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
108 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
109 : wxScrollHelper(this)
113 Create(parent
, id
, pos
, size
, style
);
117 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
119 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
120 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
124 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
131 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
134 GetBuffer().SetRichTextCtrl(this);
136 wxTextAttrEx attributes
;
137 attributes
.SetFont(GetFont());
138 attributes
.SetTextColour(*wxBLACK
);
139 attributes
.SetBackgroundColour(*wxWHITE
);
140 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
141 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
143 SetDefaultStyle(attributes
);
144 SetBasicStyle(attributes
);
146 SetBackgroundColour(*wxWHITE
);
147 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
149 // Tell the sizers to use the given or best size
150 SetBestFittingSize(size
);
153 RecreateBuffer(size
);
155 SetCursor(wxCursor(wxCURSOR_IBEAM
));
160 wxRichTextCtrl::~wxRichTextCtrl()
162 delete m_contextMenu
;
165 /// Member initialisation
166 void wxRichTextCtrl::Init()
169 m_contextMenu
= NULL
;
171 m_caretPosition
= -1;
172 m_selectionRange
.SetRange(-2, -2);
173 m_selectionAnchor
= -2;
175 m_caretAtLineStart
= false;
177 m_fullLayoutRequired
= false;
178 m_fullLayoutTime
= 0;
179 m_fullLayoutSavedPosition
= 0;
180 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
183 /// Call Freeze to prevent refresh
184 void wxRichTextCtrl::Freeze()
189 /// Call Thaw to refresh
190 void wxRichTextCtrl::Thaw(bool refresh
)
194 if (m_freezeCount
== 0 && refresh
)
202 void wxRichTextCtrl::Clear()
205 m_buffer
.SetDirty(true);
206 m_caretPosition
= -1;
207 m_caretAtLineStart
= false;
208 m_selectionRange
.SetRange(-2, -2);
210 if (m_freezeCount
== 0)
219 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
225 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
226 //wxLogDebug(wxT("OnPaint"));
230 if (m_freezeCount
> 0)
233 dc
.SetFont(GetFont());
235 // Paint the background
238 wxRegion dirtyRegion
= GetUpdateRegion();
240 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
241 wxRect
availableSpace(GetClientSize());
242 if (GetBuffer().GetDirty())
244 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
245 GetBuffer().SetDirty(false);
249 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
258 // Empty implementation, to prevent flicker
259 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
263 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
265 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
274 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
283 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
289 dc
.SetFont(GetFont());
292 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
294 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
296 m_dragStart
= event
.GetLogicalPosition(dc
);
302 bool caretAtLineStart
= false;
304 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
306 // If we're at the start of a line (but not first in para)
307 // then we should keep the caret showing at the start of the line
308 // by showing the m_caretAtLineStart flag.
309 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
310 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
312 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
313 caretAtLineStart
= true;
317 MoveCaret(position
, caretAtLineStart
);
318 SetDefaultStyleToCursorStyle();
321 wxWindow
* p
= GetParent();
322 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
325 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
328 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
329 frame
->SetStatusText(msg
, 1);
338 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
343 if (GetCapture() == this)
349 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
351 if (!event
.Dragging())
359 dc
.SetFont(GetFont());
362 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
363 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
365 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
367 // TODO: test closeness
369 bool caretAtLineStart
= false;
371 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
373 // If we're at the start of a line (but not first in para)
374 // then we should keep the caret showing at the start of the line
375 // by showing the m_caretAtLineStart flag.
376 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
377 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
379 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
380 caretAtLineStart
= true;
384 if (m_caretPosition
!= position
)
386 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
388 MoveCaret(position
, caretAtLineStart
);
389 SetDefaultStyleToCursorStyle();
398 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
404 /// Left-double-click
405 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
411 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
417 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
420 if (event
.ControlDown())
421 flags
|= wxRICHTEXT_CTRL_DOWN
;
422 if (event
.ShiftDown())
423 flags
|= wxRICHTEXT_SHIFT_DOWN
;
425 flags
|= wxRICHTEXT_ALT_DOWN
;
427 if (event
.GetKeyCode() == WXK_LEFT
||
428 event
.GetKeyCode() == WXK_RIGHT
||
429 event
.GetKeyCode() == WXK_UP
||
430 event
.GetKeyCode() == WXK_DOWN
||
431 event
.GetKeyCode() == WXK_HOME
||
432 event
.GetKeyCode() == WXK_PAGEUP
||
433 event
.GetKeyCode() == WXK_PAGEDOWN
||
434 event
.GetKeyCode() == WXK_PRIOR
||
435 event
.GetKeyCode() == WXK_NEXT
||
436 event
.GetKeyCode() == WXK_END
)
438 Navigate(event
.GetKeyCode(), flags
);
440 else if (event
.GetKeyCode() == WXK_RETURN
)
442 BeginBatchUndo(_("Insert Text"));
444 long newPos
= m_caretPosition
;
446 DeleteSelectedContent(& newPos
);
448 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
450 wxRichTextEvent
cmdEvent(
451 wxEVT_COMMAND_RICHTEXT_RETURN
,
453 cmdEvent
.SetEventObject(this);
454 cmdEvent
.SetFlags(flags
);
455 GetEventHandler()->ProcessEvent(cmdEvent
);
458 SetDefaultStyleToCursorStyle();
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();
489 else if (event
.GetKeyCode() == WXK_DELETE
)
491 BeginBatchUndo(_("Delete Text"));
493 // Submit range in character positions, which are greater than caret positions,
494 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
496 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
497 m_caretPosition
, // Current caret position
498 m_caretPosition
+1, // New caret position
502 DeleteSelectedContent();
506 // Shouldn't this be in Do()?
507 if (GetLastPosition() == -1)
511 m_caretPosition
= -1;
513 SetDefaultStyleToCursorStyle();
518 BeginBatchUndo(_("Insert Text"));
520 long newPos
= m_caretPosition
;
521 DeleteSelectedContent(& newPos
);
523 wxString str
= (wxChar
) event
.GetKeyCode();
524 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
528 SetDefaultStyleToCursorStyle();
536 /// Delete content if there is a selection, e.g. when pressing a key.
537 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
541 long pos
= m_selectionRange
.GetStart();
542 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
543 m_caretPosition
, // Current caret position
544 pos
, // New caret position
546 m_selectionRange
.SetRange(-2, -2);
556 /// Keyboard navigation
560 Left: left one character
561 Right: right one character
564 Ctrl-Left: left one word
565 Ctrl-Right: right one word
566 Ctrl-Up: previous paragraph start
567 Ctrl-Down: next start of paragraph
570 Ctrl-Home: start of document
571 Ctrl-End: end of document
573 Page-Down: Down a screen
577 Ctrl-Alt-PgUp: Start of window
578 Ctrl-Alt-PgDn: End of window
579 F8: Start selection mode
580 Esc: End selection mode
582 Adding Shift does the above but starts/extends selection.
587 bool wxRichTextCtrl::Navigate(int keyCode
, int flags
)
589 bool success
= false;
592 if (keyCode
== WXK_RIGHT
)
594 if (flags
& wxRICHTEXT_CTRL_DOWN
)
595 success
= WordRight(1, flags
);
597 success
= MoveRight(1, flags
);
599 else if (keyCode
== WXK_LEFT
)
601 if (flags
& wxRICHTEXT_CTRL_DOWN
)
602 success
= WordLeft(1, flags
);
604 success
= MoveLeft(1, flags
);
606 else if (keyCode
== WXK_UP
)
608 if (flags
& wxRICHTEXT_CTRL_DOWN
)
609 success
= MoveToParagraphStart(flags
);
611 success
= MoveUp(1, flags
);
613 else if (keyCode
== WXK_DOWN
)
615 if (flags
& wxRICHTEXT_CTRL_DOWN
)
616 success
= MoveToParagraphEnd(flags
);
618 success
= MoveDown(1, flags
);
620 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_PRIOR
)
622 success
= PageUp(1, flags
);
624 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NEXT
)
626 success
= PageDown(1, flags
);
628 else if (keyCode
== WXK_HOME
)
630 if (flags
& wxRICHTEXT_CTRL_DOWN
)
631 success
= MoveHome(flags
);
633 success
= MoveToLineStart(flags
);
635 else if (keyCode
== WXK_END
)
637 if (flags
& wxRICHTEXT_CTRL_DOWN
)
638 success
= MoveEnd(flags
);
640 success
= MoveToLineEnd(flags
);
645 ScrollIntoView(m_caretPosition
, keyCode
);
646 SetDefaultStyleToCursorStyle();
654 /// Extend the selection. Selections are in caret positions.
655 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
657 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
659 // If not currently selecting, start selecting
660 if (m_selectionRange
.GetStart() == -2)
662 m_selectionAnchor
= oldPos
;
665 m_selectionRange
.SetRange(newPos
+1, oldPos
);
667 m_selectionRange
.SetRange(oldPos
+1, newPos
);
671 // Always ensure that the selection range start is greater than
673 if (newPos
> m_selectionAnchor
)
674 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
676 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
679 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
681 wxLogDebug(wxT("Strange selection range"));
690 /// Scroll into view, returning true if we scrolled.
691 /// This takes a _caret_ position.
692 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
694 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
700 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
703 GetViewStart(& startX
, & startY
);
705 startY
= startY
* ppuY
;
708 GetVirtualSize(& sx
, & sy
);
713 wxRect rect
= line
->GetRect();
715 bool scrolled
= false;
717 wxSize clientSize
= GetClientSize();
720 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_NEXT
|| keyCode
== WXK_PAGEDOWN
)
722 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
724 // Make it scroll so this item is at the bottom
726 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
727 y
= (int) (0.5 + y
/ppuY
);
731 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
735 else if (rect
.y
< startY
)
737 // Make it scroll so this item is at the top
740 y
= (int) (0.5 + y
/ppuY
);
744 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
750 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PRIOR
|| keyCode
== WXK_PAGEUP
)
754 // Make it scroll so this item is at the top
757 y
= (int) (0.5 + y
/ppuY
);
761 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
765 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
767 // Make it scroll so this item is at the bottom
769 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
770 y
= (int) (0.5 + y
/ppuY
);
774 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
784 /// Is the given position visible on the screen?
785 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
787 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
793 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
796 GetViewStart(& startX
, & startY
);
798 startY
= startY
* ppuY
;
801 GetVirtualSize(& sx
, & sy
);
806 wxRect rect
= line
->GetRect();
808 wxSize clientSize
= GetClientSize();
810 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
813 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
815 m_caretPosition
= position
;
816 m_caretAtLineStart
= showAtLineStart
;
819 /// Move caret one visual step forward: this may mean setting a flag
820 /// and keeping the same position if we're going from the end of one line
821 /// to the start of the next, which may be the exact same caret position.
822 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
824 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
826 // Only do the check if we're not at the end of the paragraph (where things work OK
828 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
830 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
834 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
836 // We're at the end of a line. See whether we need to
837 // stay at the same actual caret position but change visual
839 if (oldPosition
== lineRange
.GetEnd())
841 if (m_caretAtLineStart
)
843 // We're already at the start of the line, so actually move on now.
844 m_caretPosition
= oldPosition
+ 1;
845 m_caretAtLineStart
= false;
849 // We're showing at the end of the line, so keep to
850 // the same position but indicate that we're to show
851 // at the start of the next line.
852 m_caretPosition
= oldPosition
;
853 m_caretAtLineStart
= true;
855 SetDefaultStyleToCursorStyle();
861 SetDefaultStyleToCursorStyle();
864 /// Move caret one visual step backward: this may mean setting a flag
865 /// and keeping the same position if we're going from the end of one line
866 /// to the start of the next, which may be the exact same caret position.
867 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
869 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
871 // Only do the check if we're not at the start of the paragraph (where things work OK
873 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
875 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
879 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
881 // We're at the start of a line. See whether we need to
882 // stay at the same actual caret position but change visual
884 if (oldPosition
== lineRange
.GetStart())
886 m_caretPosition
= oldPosition
-1;
887 m_caretAtLineStart
= true;
890 else if (oldPosition
== lineRange
.GetEnd())
892 if (m_caretAtLineStart
)
894 // We're at the start of the line, so keep the same caret position
895 // but clear the start-of-line flag.
896 m_caretPosition
= oldPosition
;
897 m_caretAtLineStart
= false;
901 // We're showing at the end of the line, so go back
902 // to the previous character position.
903 m_caretPosition
= oldPosition
- 1;
905 SetDefaultStyleToCursorStyle();
911 SetDefaultStyleToCursorStyle();
915 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
917 long endPos
= GetBuffer().GetRange().GetEnd();
919 if (m_caretPosition
+ noPositions
< endPos
)
921 long oldPos
= m_caretPosition
;
922 long newPos
= m_caretPosition
+ noPositions
;
924 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
928 // Determine by looking at oldPos and m_caretPosition whether
929 // we moved from the end of a line to the start of the next line, in which case
930 // we want to adjust the caret position such that it is positioned at the
931 // start of the next line, rather than jumping past the first character of the
933 if (noPositions
== 1 && !extendSel
)
934 MoveCaretForward(oldPos
);
936 SetCaretPosition(newPos
);
939 SetDefaultStyleToCursorStyle();
950 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
954 if (m_caretPosition
> startPos
- noPositions
+ 1)
956 long oldPos
= m_caretPosition
;
957 long newPos
= m_caretPosition
- noPositions
;
958 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
962 if (noPositions
== 1 && !extendSel
)
963 MoveCaretBack(oldPos
);
965 SetCaretPosition(newPos
);
968 SetDefaultStyleToCursorStyle();
979 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
981 return MoveDown(- noLines
, flags
);
985 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
990 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
991 wxPoint pt
= GetCaret()->GetPosition();
992 long newLine
= lineNumber
+ noLines
;
994 if (lineNumber
!= -1)
998 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1000 if (newLine
> lastLine
)
1010 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1013 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1019 wxClientDC
dc(this);
1021 dc
.SetFont(GetFont());
1023 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1025 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1027 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1028 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1029 // so we view the caret at the start of the line.
1030 bool caretLineStart
= false;
1031 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1033 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1034 wxRichTextRange lineRange
;
1036 lineRange
= thisLine
->GetAbsoluteRange();
1038 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1041 caretLineStart
= true;
1045 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1046 if (para
&& para
->GetRange().GetStart() == newPos
)
1051 long newSelEnd
= newPos
;
1053 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1057 SetCaretPosition(newPos
, caretLineStart
);
1059 SetDefaultStyleToCursorStyle();
1069 /// Move to the end of the paragraph
1070 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1072 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1075 long newPos
= para
->GetRange().GetEnd() - 1;
1076 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1080 SetCaretPosition(newPos
);
1082 SetDefaultStyleToCursorStyle();
1092 /// Move to the start of the paragraph
1093 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1095 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1098 long newPos
= para
->GetRange().GetStart() - 1;
1099 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1103 SetCaretPosition(newPos
);
1105 SetDefaultStyleToCursorStyle();
1115 /// Move to the end of the line
1116 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1118 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1122 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1123 long newPos
= lineRange
.GetEnd();
1124 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1128 SetCaretPosition(newPos
);
1130 SetDefaultStyleToCursorStyle();
1140 /// Move to the start of the line
1141 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1143 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1146 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1147 long newPos
= lineRange
.GetStart()-1;
1149 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1153 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1155 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1157 SetDefaultStyleToCursorStyle();
1167 /// Move to the start of the buffer
1168 bool wxRichTextCtrl::MoveHome(int flags
)
1170 if (m_caretPosition
!= -1)
1172 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1176 SetCaretPosition(-1);
1178 SetDefaultStyleToCursorStyle();
1188 /// Move to the end of the buffer
1189 bool wxRichTextCtrl::MoveEnd(int flags
)
1191 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1193 if (m_caretPosition
!= endPos
)
1195 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1199 SetCaretPosition(endPos
);
1201 SetDefaultStyleToCursorStyle();
1211 /// Move noPages pages up
1212 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1214 return PageDown(- noPages
, flags
);
1217 /// Move noPages pages down
1218 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1220 // Calculate which line occurs noPages * screen height further down.
1221 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1224 wxSize clientSize
= GetClientSize();
1225 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1227 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1230 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1231 long pos
= lineRange
.GetStart()-1;
1232 if (pos
!= m_caretPosition
)
1234 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1236 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1240 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1242 SetDefaultStyleToCursorStyle();
1254 // Finds the caret position for the next word
1255 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1257 long endPos
= GetBuffer().GetRange().GetEnd();
1261 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1263 // First skip current text to space
1264 while (i
< endPos
&& i
> -1)
1266 // i is in character, not caret positions
1267 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1268 if (text
!= wxT(" ") && !text
.empty())
1275 while (i
< endPos
&& i
> -1)
1277 // i is in character, not caret positions
1278 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1279 if (text
.empty()) // End of paragraph, or maybe an image
1280 return wxMax(-1, i
- 1);
1281 else if (text
== wxT(" ") || text
.empty())
1285 // Convert to caret position
1286 return wxMax(-1, i
- 1);
1295 long i
= m_caretPosition
;
1297 // First skip white space
1298 while (i
< endPos
&& i
> -1)
1300 // i is in character, not caret positions
1301 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1302 if (text
.empty()) // End of paragraph, or maybe an image
1304 else if (text
== wxT(" ") || text
.empty())
1309 // Next skip current text to space
1310 while (i
< endPos
&& i
> -1)
1312 // i is in character, not caret positions
1313 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1314 if (text
!= wxT(" ") /* && !text.empty() */)
1327 /// Move n words left
1328 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1330 long pos
= FindNextWordPosition(-1);
1331 if (pos
!= m_caretPosition
)
1333 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1335 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1339 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1341 SetDefaultStyleToCursorStyle();
1351 /// Move n words right
1352 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1354 long pos
= FindNextWordPosition(1);
1355 if (pos
!= m_caretPosition
)
1357 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1359 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1363 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1365 SetDefaultStyleToCursorStyle();
1376 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1378 // Only do sizing optimization for large buffers
1379 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1381 m_fullLayoutRequired
= true;
1382 m_fullLayoutTime
= wxGetLocalTimeMillis();
1383 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1384 LayoutContent(true /* onlyVisibleRect */);
1387 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1395 /// Idle-time processing
1396 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1398 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1400 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1402 m_fullLayoutRequired
= false;
1403 m_fullLayoutTime
= 0;
1404 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1405 ShowPosition(m_fullLayoutSavedPosition
);
1412 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1418 /// Set up scrollbars, e.g. after a resize
1419 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1424 if (GetBuffer().IsEmpty())
1426 SetScrollbars(0, 0, 0, 0, 0, 0);
1430 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1431 // of pixels. See e.g. wxVScrolledWindow for ideas.
1432 int pixelsPerUnit
= 5; // 10;
1433 wxSize clientSize
= GetClientSize();
1435 int maxHeight
= GetBuffer().GetCachedSize().y
;
1437 int unitsY
= maxHeight
/pixelsPerUnit
;
1439 int startX
= 0, startY
= 0;
1441 GetViewStart(& startX
, & startY
);
1443 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1444 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1446 // Move to previous scroll position if
1448 SetScrollbars(0, pixelsPerUnit
,
1450 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1453 /// Paint the background
1454 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1456 wxColour backgroundColour
= GetBackgroundColour();
1457 if (!backgroundColour
.Ok())
1458 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1460 // Clear the background
1461 dc
.SetBrush(wxBrush(backgroundColour
));
1462 dc
.SetPen(*wxTRANSPARENT_PEN
);
1463 wxRect
windowRect(GetClientSize());
1464 windowRect
.x
-= 2; windowRect
.y
-= 2;
1465 windowRect
.width
+= 4; windowRect
.height
+= 4;
1467 // We need to shift the rectangle to take into account
1468 // scrolling. Converting device to logical coordinates.
1469 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1470 dc
.DrawRectangle(windowRect
);
1473 /// Recreate buffer bitmap if necessary
1474 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1477 if (sz
== wxDefaultSize
)
1478 sz
= GetClientSize();
1480 if (sz
.x
< 1 || sz
.y
< 1)
1483 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1484 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1485 return m_bufferBitmap
.Ok();
1488 // ----------------------------------------------------------------------------
1489 // file IO functions
1490 // ----------------------------------------------------------------------------
1492 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1494 bool success
= GetBuffer().LoadFile(filename
, type
);
1496 m_filename
= filename
;
1499 SetInsertionPoint(0);
1502 SetupScrollbars(true);
1510 wxLogError(_("File couldn't be loaded."));
1516 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1518 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1519 if ( filenameToUse
.empty() )
1521 // what kind of message to give? is it an error or a program bug?
1522 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1527 if (GetBuffer().SaveFile(filenameToUse
, type
))
1529 m_filename
= filenameToUse
;
1537 wxLogError(_("The text couldn't be saved."));
1542 // ----------------------------------------------------------------------------
1543 // wxRichTextCtrl specific functionality
1544 // ----------------------------------------------------------------------------
1546 /// Add a new paragraph of text to the end of the buffer
1547 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1549 return GetBuffer().AddParagraph(text
);
1553 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1555 return GetBuffer().AddImage(image
);
1558 // ----------------------------------------------------------------------------
1559 // selection and ranges
1560 // ----------------------------------------------------------------------------
1562 void wxRichTextCtrl::SelectAll()
1564 SetSelection(0, GetLastPosition());
1565 m_selectionAnchor
= -1;
1569 void wxRichTextCtrl::SelectNone()
1571 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1572 SetSelection(-2, -2);
1573 m_selectionAnchor
= -2;
1576 wxString
wxRichTextCtrl::GetStringSelection() const
1579 GetSelection(&from
, &to
);
1581 return GetRange(from
, to
);
1584 // do the window-specific processing after processing the update event
1585 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1587 if ( event
.GetSetEnabled() )
1588 Enable(event
.GetEnabled());
1590 if ( event
.GetSetText() )
1592 if ( event
.GetText() != GetValue() )
1593 SetValue(event
.GetText());
1597 // ----------------------------------------------------------------------------
1599 // ----------------------------------------------------------------------------
1601 wxTextCtrlHitTestResult
1602 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1604 // implement in terms of the other overload as the native ports typically
1605 // can get the position and not (x, y) pair directly (although wxUniv
1606 // directly gets x and y -- and so overrides this method as well)
1608 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1610 if ( rc
!= wxTE_HT_UNKNOWN
)
1612 PositionToXY(pos
, x
, y
);
1618 wxTextCtrlHitTestResult
1619 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1622 wxClientDC
dc((wxRichTextCtrl
*) this);
1623 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1625 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1626 if (hit
== wxRICHTEXT_HITTEST_BEFORE
)
1627 return wxTE_HT_BEFORE
;
1628 else if (hit
== wxRICHTEXT_HITTEST_AFTER
)
1629 return wxTE_HT_BEYOND
;
1630 else if (hit
== wxRICHTEXT_HITTEST_ON
)
1631 return wxTE_HT_ON_TEXT
;
1633 return wxTE_HT_UNKNOWN
;
1636 // ----------------------------------------------------------------------------
1637 // set/get the controls text
1638 // ----------------------------------------------------------------------------
1640 wxString
wxRichTextCtrl::GetValue() const
1642 return GetBuffer().GetText();
1645 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1647 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1650 void wxRichTextCtrl::SetValue(const wxString
& value
)
1654 // if the text is long enough, it's faster to just set it instead of first
1655 // comparing it with the old one (chances are that it will be different
1656 // anyhow, this comparison is there to avoid flicker for small single-line
1657 // edit controls mostly)
1658 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1660 DoWriteText(value
, false /* not selection only */);
1662 // for compatibility, don't move the cursor when doing SetValue()
1663 SetInsertionPoint(0);
1667 // still send an event for consistency
1671 // we should reset the modified flag even if the value didn't really change
1673 // mark the control as being not dirty - we changed its text, not the
1678 void wxRichTextCtrl::WriteText(const wxString
& value
)
1683 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1685 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1688 void wxRichTextCtrl::AppendText(const wxString
& text
)
1690 SetInsertionPointEnd();
1695 /// Write an image at the current insertion point
1696 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1698 wxRichTextImageBlock imageBlock
;
1700 wxImage image2
= image
;
1701 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1702 return WriteImage(imageBlock
);
1707 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1709 wxRichTextImageBlock imageBlock
;
1712 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1713 return WriteImage(imageBlock
);
1718 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1720 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1723 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1727 wxRichTextImageBlock imageBlock
;
1729 wxImage image
= bitmap
.ConvertToImage();
1730 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1731 return WriteImage(imageBlock
);
1738 /// Insert a newline (actually paragraph) at the current insertion point.
1739 bool wxRichTextCtrl::Newline()
1741 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1745 // ----------------------------------------------------------------------------
1746 // Clipboard operations
1747 // ----------------------------------------------------------------------------
1749 void wxRichTextCtrl::Copy()
1753 wxRichTextRange range
= GetSelectionRange();
1754 GetBuffer().CopyToClipboard(range
);
1758 void wxRichTextCtrl::Cut()
1762 wxRichTextRange range
= GetSelectionRange();
1763 GetBuffer().CopyToClipboard(range
);
1765 DeleteSelectedContent();
1771 void wxRichTextCtrl::Paste()
1775 BeginBatchUndo(_("Paste"));
1777 long newPos
= m_caretPosition
;
1778 DeleteSelectedContent(& newPos
);
1780 GetBuffer().PasteFromClipboard(newPos
);
1786 void wxRichTextCtrl::DeleteSelection()
1788 if (CanDeleteSelection())
1790 DeleteSelectedContent();
1794 bool wxRichTextCtrl::HasSelection() const
1796 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1799 bool wxRichTextCtrl::CanCopy() const
1801 // Can copy if there's a selection
1802 return HasSelection();
1805 bool wxRichTextCtrl::CanCut() const
1807 return HasSelection() && IsEditable();
1810 bool wxRichTextCtrl::CanPaste() const
1812 if ( !IsEditable() )
1815 return GetBuffer().CanPasteFromClipboard();
1818 bool wxRichTextCtrl::CanDeleteSelection() const
1820 return HasSelection() && IsEditable();
1824 // ----------------------------------------------------------------------------
1826 // ----------------------------------------------------------------------------
1828 void wxRichTextCtrl::SetEditable(bool editable
)
1830 m_editable
= editable
;
1833 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1837 m_caretPosition
= pos
- 1;
1840 void wxRichTextCtrl::SetInsertionPointEnd()
1842 long pos
= GetLastPosition();
1843 SetInsertionPoint(pos
);
1846 long wxRichTextCtrl::GetInsertionPoint() const
1848 return m_caretPosition
+1;
1851 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1853 return GetBuffer().GetRange().GetEnd();
1856 // If the return values from and to are the same, there is no
1858 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1860 *from
= m_selectionRange
.GetStart();
1861 *to
= m_selectionRange
.GetEnd();
1864 bool wxRichTextCtrl::IsEditable() const
1869 // ----------------------------------------------------------------------------
1871 // ----------------------------------------------------------------------------
1873 void wxRichTextCtrl::SetSelection(long from
, long to
)
1875 // if from and to are both -1, it means (in wxWidgets) that all text should
1877 if ( (from
== -1) && (to
== -1) )
1880 to
= GetLastPosition();
1883 DoSetSelection(from
, to
);
1886 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1888 m_selectionAnchor
= from
;
1889 m_selectionRange
.SetRange(from
, to
);
1894 // ----------------------------------------------------------------------------
1896 // ----------------------------------------------------------------------------
1898 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1900 BeginBatchUndo(_("Replace"));
1902 DeleteSelectedContent();
1904 DoWriteText(value
, true /* selection only */);
1909 void wxRichTextCtrl::Remove(long from
, long to
)
1913 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1914 m_caretPosition
, // Current caret position
1915 from
, // New caret position
1923 bool wxRichTextCtrl::IsModified() const
1925 return m_buffer
.IsModified();
1928 void wxRichTextCtrl::MarkDirty()
1930 m_buffer
.Modify(true);
1933 void wxRichTextCtrl::DiscardEdits()
1935 m_buffer
.Modify(false);
1936 m_buffer
.GetCommandProcessor()->ClearCommands();
1939 int wxRichTextCtrl::GetNumberOfLines() const
1941 return GetBuffer().GetParagraphCount();
1944 // ----------------------------------------------------------------------------
1945 // Positions <-> coords
1946 // ----------------------------------------------------------------------------
1948 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1950 return GetBuffer().XYToPosition(x
, y
);
1953 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1955 return GetBuffer().PositionToXY(pos
, x
, y
);
1958 // ----------------------------------------------------------------------------
1960 // ----------------------------------------------------------------------------
1962 void wxRichTextCtrl::ShowPosition(long pos
)
1964 if (!IsPositionVisible(pos
))
1965 ScrollIntoView(pos
-1, WXK_DOWN
);
1968 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1970 return GetBuffer().GetParagraphLength(lineNo
);
1973 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1975 return GetBuffer().GetParagraphText(lineNo
);
1978 // ----------------------------------------------------------------------------
1980 // ----------------------------------------------------------------------------
1982 void wxRichTextCtrl::Undo()
1986 GetCommandProcessor()->Undo();
1990 void wxRichTextCtrl::Redo()
1994 GetCommandProcessor()->Redo();
1998 bool wxRichTextCtrl::CanUndo() const
2000 return GetCommandProcessor()->CanUndo();
2003 bool wxRichTextCtrl::CanRedo() const
2005 return GetCommandProcessor()->CanRedo();
2008 // ----------------------------------------------------------------------------
2009 // implemenation details
2010 // ----------------------------------------------------------------------------
2012 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2014 SetValue(event
.GetString());
2015 GetEventHandler()->ProcessEvent(event
);
2018 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2020 // By default, load the first file into the text window.
2021 if (event
.GetNumberOfFiles() > 0)
2023 LoadFile(event
.GetFiles()[0]);
2027 // ----------------------------------------------------------------------------
2028 // text control event processing
2029 // ----------------------------------------------------------------------------
2031 bool wxRichTextCtrl::SendUpdateEvent()
2033 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2034 InitCommandEvent(event
);
2036 return GetEventHandler()->ProcessEvent(event
);
2039 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2041 event
.SetEventObject((wxControlBase
*)this); // const_cast
2043 switch ( m_clientDataType
)
2045 case wxClientData_Void
:
2046 event
.SetClientData(GetClientData());
2049 case wxClientData_Object
:
2050 event
.SetClientObject(GetClientObject());
2053 case wxClientData_None
:
2060 wxSize
wxRichTextCtrl::DoGetBestSize() const
2062 return wxSize(10, 10);
2065 // ----------------------------------------------------------------------------
2066 // standard handlers for standard edit menu events
2067 // ----------------------------------------------------------------------------
2069 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2074 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2079 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2084 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2089 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2094 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2099 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2101 event
.Enable( CanCut() );
2104 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2106 event
.Enable( CanCopy() );
2109 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2111 event
.Enable( CanDeleteSelection() );
2114 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2116 event
.Enable( CanPaste() );
2119 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2121 event
.Enable( CanUndo() );
2122 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2125 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2127 event
.Enable( CanRedo() );
2128 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2131 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2136 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2138 event
.Enable(GetLastPosition() > 0);
2141 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2145 m_contextMenu
= new wxMenu
;
2146 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2147 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2148 m_contextMenu
->AppendSeparator();
2149 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2150 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2151 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2152 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2153 m_contextMenu
->AppendSeparator();
2154 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2156 PopupMenu(m_contextMenu
);
2160 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2162 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2165 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2167 return GetBuffer().SetStyle(range
, style
);
2170 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2172 return GetBuffer().SetDefaultStyle(style
);
2175 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2177 return GetBuffer().GetDefaultStyle();
2180 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2182 return GetBuffer().GetStyle(position
, style
);
2185 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2187 return GetBuffer().GetStyle(position
, style
);
2190 /// Set font, and also the buffer attributes
2191 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2193 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2194 wxControl::SetFont(font
);
2196 wxScrolledWindow::SetFont(font
);
2199 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2201 GetBuffer().SetBasicStyle(attr
);
2202 GetBuffer().SetDefaultStyle(attr
);
2207 /// Transform logical to physical
2208 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2211 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2216 /// Transform physical to logical
2217 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2220 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2225 /// Position the caret
2226 void wxRichTextCtrl::PositionCaret()
2231 //wxLogDebug(wxT("PositionCaret"));
2234 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2236 wxPoint originalPt
= caretRect
.GetPosition();
2237 wxPoint pt
= GetPhysicalPoint(originalPt
);
2238 if (GetCaret()->GetPosition() != pt
)
2240 GetCaret()->Move(pt
);
2241 GetCaret()->SetSize(caretRect
.GetSize());
2246 /// Get the caret height and position for the given character position
2247 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2249 wxClientDC
dc(this);
2250 dc
.SetFont(GetFont());
2257 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2259 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2266 /// Gets the line for the visible caret position. If the caret is
2267 /// shown at the very end of the line, it means the next character is actually
2268 /// on the following line. So let's get the line we're expecting to find
2269 /// if this is the case.
2270 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2272 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2273 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2276 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2277 if (caretPosition
== lineRange
.GetStart()-1 &&
2278 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2280 if (!m_caretAtLineStart
)
2281 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2288 /// Move the caret to the given character position
2289 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2291 if (GetBuffer().GetDirty())
2294 if (pos
<= GetBuffer().GetRange().GetEnd())
2296 SetCaretPosition(pos
, showAtLineStart
);
2306 /// Layout the buffer: which we must do before certain operations, such as
2307 /// setting the caret position.
2308 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2310 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2312 wxRect
availableSpace(GetClientSize());
2313 if (availableSpace
.width
== 0)
2314 availableSpace
.width
= 10;
2315 if (availableSpace
.height
== 0)
2316 availableSpace
.height
= 10;
2318 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2319 if (onlyVisibleRect
)
2321 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2322 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2325 wxClientDC
dc(this);
2326 dc
.SetFont(GetFont());
2330 GetBuffer().Defragment();
2331 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2332 GetBuffer().Layout(dc
, availableSpace
, flags
);
2333 GetBuffer().SetDirty(false);
2342 /// Is all of the selection bold?
2343 bool wxRichTextCtrl::IsSelectionBold() const
2347 wxRichTextAttr attr
;
2348 wxRichTextRange range
= GetSelectionRange();
2349 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2350 attr
.SetFontWeight(wxBOLD
);
2352 return HasCharacterAttributes(range
, attr
);
2356 // If no selection, then we need to combine current style with default style
2357 // to see what the effect would be if we started typing.
2358 wxRichTextAttr attr
;
2359 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2360 if (GetStyle(GetCaretPosition()+1, attr
))
2362 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2363 return attr
.GetFontWeight() == wxBOLD
;
2369 /// Is all of the selection italics?
2370 bool wxRichTextCtrl::IsSelectionItalics() const
2374 wxRichTextRange range
= GetSelectionRange();
2375 wxRichTextAttr attr
;
2376 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2377 attr
.SetFontStyle(wxITALIC
);
2379 return HasCharacterAttributes(range
, attr
);
2383 // If no selection, then we need to combine current style with default style
2384 // to see what the effect would be if we started typing.
2385 wxRichTextAttr attr
;
2386 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2387 if (GetStyle(GetCaretPosition()+1, attr
))
2389 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2390 return attr
.GetFontStyle() == wxITALIC
;
2396 /// Is all of the selection underlined?
2397 bool wxRichTextCtrl::IsSelectionUnderlined() const
2401 wxRichTextRange range
= GetSelectionRange();
2402 wxRichTextAttr attr
;
2403 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2404 attr
.SetFontUnderlined(true);
2406 return HasCharacterAttributes(range
, attr
);
2410 // If no selection, then we need to combine current style with default style
2411 // to see what the effect would be if we started typing.
2412 wxRichTextAttr attr
;
2413 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2414 if (GetStyle(GetCaretPosition()+1, attr
))
2416 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2417 return attr
.GetFontUnderlined();
2423 /// Apply bold to the selection
2424 bool wxRichTextCtrl::ApplyBoldToSelection()
2426 wxRichTextAttr attr
;
2427 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2428 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2431 return SetStyle(GetSelectionRange(), attr
);
2433 SetDefaultStyle(attr
);
2437 /// Apply italic to the selection
2438 bool wxRichTextCtrl::ApplyItalicToSelection()
2440 wxRichTextAttr attr
;
2441 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2442 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2445 return SetStyle(GetSelectionRange(), attr
);
2447 SetDefaultStyle(attr
);
2451 /// Apply underline to the selection
2452 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2454 wxRichTextAttr attr
;
2455 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2456 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2459 return SetStyle(GetSelectionRange(), attr
);
2461 SetDefaultStyle(attr
);
2465 /// Is all of the selection aligned according to the specified flag?
2466 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2470 wxRichTextRange range
= GetSelectionRange();
2471 wxRichTextAttr attr
;
2472 attr
.SetAlignment(alignment
);
2474 return HasParagraphAttributes(range
, attr
);
2478 // If no selection, then we need to get information from the current paragraph.
2479 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2481 return para
->GetAttributes().GetAlignment() == alignment
;
2486 /// Apply alignment to the selection
2487 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2489 wxRichTextAttr attr
;
2490 attr
.SetAlignment(alignment
);
2492 return SetStyle(GetSelectionRange(), attr
);
2495 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2497 return SetStyle(para
->GetRange(), attr
);
2502 /// Sets the default style to the style under the cursor
2503 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2506 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2508 if (GetStyle(GetCaretPosition(), attr
))
2510 SetDefaultStyle(attr
);
2517 /// Returns the first visible position in the current view
2518 long wxRichTextCtrl::GetFirstVisiblePosition() const
2520 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2522 return line
->GetAbsoluteRange().GetStart();