1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
24 #include "wx/settings.h"
27 #include "wx/stopwatch.h"
30 #include "wx/textfile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
48 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
49 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
51 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
54 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
56 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
57 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
59 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
61 EVT_PAINT(wxRichTextCtrl
::OnPaint
)
62 EVT_ERASE_BACKGROUND(wxRichTextCtrl
::OnEraseBackground
)
63 EVT_IDLE(wxRichTextCtrl
::OnIdle
)
64 EVT_SCROLLWIN(wxRichTextCtrl
::OnScroll
)
65 EVT_LEFT_DOWN(wxRichTextCtrl
::OnLeftClick
)
66 EVT_MOTION(wxRichTextCtrl
::OnMoveMouse
)
67 EVT_LEFT_UP(wxRichTextCtrl
::OnLeftUp
)
68 EVT_RIGHT_DOWN(wxRichTextCtrl
::OnRightClick
)
69 EVT_MIDDLE_DOWN(wxRichTextCtrl
::OnMiddleClick
)
70 EVT_LEFT_DCLICK(wxRichTextCtrl
::OnLeftDClick
)
71 EVT_CHAR(wxRichTextCtrl
::OnChar
)
72 EVT_SIZE(wxRichTextCtrl
::OnSize
)
73 EVT_SET_FOCUS(wxRichTextCtrl
::OnSetFocus
)
74 EVT_KILL_FOCUS(wxRichTextCtrl
::OnKillFocus
)
75 EVT_CONTEXT_MENU(wxRichTextCtrl
::OnContextMenu
)
77 EVT_MENU(wxID_UNDO
, wxRichTextCtrl
::OnUndo
)
78 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl
::OnUpdateUndo
)
80 EVT_MENU(wxID_REDO
, wxRichTextCtrl
::OnRedo
)
81 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl
::OnUpdateRedo
)
83 EVT_MENU(wxID_COPY
, wxRichTextCtrl
::OnCopy
)
84 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl
::OnUpdateCopy
)
86 EVT_MENU(wxID_PASTE
, wxRichTextCtrl
::OnPaste
)
87 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl
::OnUpdatePaste
)
89 EVT_MENU(wxID_CUT
, wxRichTextCtrl
::OnCut
)
90 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl
::OnUpdateCut
)
92 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl
::OnClear
)
93 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl
::OnUpdateClear
)
95 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl
::OnSelectAll
)
96 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl
::OnUpdateSelectAll
)
103 wxRichTextCtrl
::wxRichTextCtrl()
104 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
105 : wxScrollHelper(this)
111 wxRichTextCtrl
::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
112 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
113 : wxScrollHelper(this)
117 Create(parent
, id
, value
, pos
, size
, style
);
121 bool wxRichTextCtrl
::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
123 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
124 if (!wxTextCtrlBase
::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
128 if (!wxScrolledWindow
::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
135 SetFont(wxSystemSettings
::GetFont(wxSYS_DEFAULT_GUI_FONT
));
138 GetBuffer().SetRichTextCtrl(this);
140 wxTextAttrEx attributes
;
141 attributes
.SetFont(GetFont());
142 attributes
.SetTextColour(*wxBLACK
);
143 attributes
.SetBackgroundColour(*wxWHITE
);
144 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
145 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
147 SetDefaultStyle(attributes
);
148 SetBasicStyle(attributes
);
150 SetBackgroundColour(*wxWHITE
);
151 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
153 // Tell the sizers to use the given or best size
154 SetBestFittingSize(size
);
157 RecreateBuffer(size
);
159 SetCursor(wxCursor(wxCURSOR_IBEAM
));
161 if (!value
.IsEmpty())
167 wxRichTextCtrl
::~wxRichTextCtrl()
169 delete m_contextMenu
;
172 /// Member initialisation
173 void wxRichTextCtrl
::Init()
176 m_contextMenu
= NULL
;
178 m_caretPosition
= -1;
179 m_selectionRange
.SetRange(-2, -2);
180 m_selectionAnchor
= -2;
182 m_caretAtLineStart
= false;
184 m_fullLayoutRequired
= false;
185 m_fullLayoutTime
= 0;
186 m_fullLayoutSavedPosition
= 0;
187 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
190 /// Call Freeze to prevent refresh
191 void wxRichTextCtrl
::Freeze()
196 /// Call Thaw to refresh
197 void wxRichTextCtrl
::Thaw()
201 if (m_freezeCount
== 0)
209 void wxRichTextCtrl
::Clear()
212 m_buffer
.SetDirty(true);
213 m_caretPosition
= -1;
214 m_caretAtLineStart
= false;
215 m_selectionRange
.SetRange(-2, -2);
217 SetScrollbars(0, 0, 0, 0, 0, 0);
219 if (m_freezeCount
== 0)
228 void wxRichTextCtrl
::OnPaint(wxPaintEvent
& WXUNUSED(event
))
234 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
235 //wxLogDebug(wxT("OnPaint"));
239 if (m_freezeCount
> 0)
242 dc
.SetFont(GetFont());
244 // Paint the background
247 wxRegion dirtyRegion
= GetUpdateRegion();
249 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
250 wxRect
availableSpace(GetClientSize());
251 if (GetBuffer().GetDirty())
253 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
254 GetBuffer().SetDirty(false);
258 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
267 // Empty implementation, to prevent flicker
268 void wxRichTextCtrl
::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
272 void wxRichTextCtrl
::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
274 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
283 void wxRichTextCtrl
::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
292 void wxRichTextCtrl
::OnLeftClick(wxMouseEvent
& event
)
298 dc
.SetFont(GetFont());
301 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
303 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
305 m_dragStart
= event
.GetLogicalPosition(dc
);
311 bool caretAtLineStart
= false;
313 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
315 // If we're at the start of a line (but not first in para)
316 // then we should keep the caret showing at the start of the line
317 // by showing the m_caretAtLineStart flag.
318 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
319 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
321 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
322 caretAtLineStart
= true;
326 MoveCaret(position
, caretAtLineStart
);
327 SetDefaultStyleToCursorStyle();
330 wxWindow
* p
= GetParent();
331 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
334 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
337 wxString msg
= wxString
::Format(wxT("Found position %ld"), position
);
338 frame
->SetStatusText(msg
, 1);
347 void wxRichTextCtrl
::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
352 if (GetCapture() == this)
358 void wxRichTextCtrl
::OnMoveMouse(wxMouseEvent
& event
)
360 if (!event
.Dragging())
368 dc
.SetFont(GetFont());
371 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
372 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
374 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
376 // TODO: test closeness
378 bool caretAtLineStart
= false;
380 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
382 // If we're at the start of a line (but not first in para)
383 // then we should keep the caret showing at the start of the line
384 // by showing the m_caretAtLineStart flag.
385 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
386 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
388 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
389 caretAtLineStart
= true;
393 if (m_caretPosition
!= position
)
395 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
397 MoveCaret(position
, caretAtLineStart
);
398 SetDefaultStyleToCursorStyle();
407 void wxRichTextCtrl
::OnRightClick(wxMouseEvent
& event
)
413 /// Left-double-click
414 void wxRichTextCtrl
::OnLeftDClick(wxMouseEvent
& event
)
420 void wxRichTextCtrl
::OnMiddleClick(wxMouseEvent
& event
)
426 void wxRichTextCtrl
::OnChar(wxKeyEvent
& event
)
429 if (event
.ControlDown())
430 flags
|= wxRICHTEXT_CTRL_DOWN
;
431 if (event
.ShiftDown())
432 flags
|= wxRICHTEXT_SHIFT_DOWN
;
434 flags
|= wxRICHTEXT_ALT_DOWN
;
436 if (event
.GetKeyCode() == WXK_LEFT
||
437 event
.GetKeyCode() == WXK_RIGHT
||
438 event
.GetKeyCode() == WXK_UP
||
439 event
.GetKeyCode() == WXK_DOWN
||
440 event
.GetKeyCode() == WXK_HOME
||
441 event
.GetKeyCode() == WXK_PAGEUP
||
442 event
.GetKeyCode() == WXK_PAGEDOWN
||
443 event
.GetKeyCode() == WXK_END
)
445 KeyboardNavigate(event
.GetKeyCode(), flags
);
449 // all the other keys modify the controls contents which shouldn't be
450 // possible if we're read-only
457 if (event
.GetKeyCode() == WXK_RETURN
)
459 BeginBatchUndo(_("Insert Text"));
461 long newPos
= m_caretPosition
;
463 DeleteSelectedContent(& newPos
);
465 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
467 wxRichTextEvent
cmdEvent(
468 wxEVT_COMMAND_RICHTEXT_RETURN
,
470 cmdEvent
.SetEventObject(this);
471 cmdEvent
.SetFlags(flags
);
472 GetEventHandler()->ProcessEvent(cmdEvent
);
475 SetDefaultStyleToCursorStyle();
477 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
479 else if (event
.GetKeyCode() == WXK_BACK
)
481 BeginBatchUndo(_("Delete Text"));
483 // Submit range in character positions, which are greater than caret positions,
484 // so subtract 1 for deleted character and add 1 for conversion to character position.
485 if (m_caretPosition
> -1 && !HasSelection())
487 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
488 m_caretPosition
, // Current caret position
489 m_caretPosition
-1, // New caret position
493 DeleteSelectedContent();
497 // Shouldn't this be in Do()?
498 if (GetLastPosition() == -1)
502 m_caretPosition
= -1;
504 SetDefaultStyleToCursorStyle();
507 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
509 else if (event
.GetKeyCode() == WXK_DELETE
)
511 BeginBatchUndo(_("Delete Text"));
513 // Submit range in character positions, which are greater than caret positions,
514 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
516 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
517 m_caretPosition
, // Current caret position
518 m_caretPosition
+1, // New caret position
522 DeleteSelectedContent();
526 // Shouldn't this be in Do()?
527 if (GetLastPosition() == -1)
531 m_caretPosition
= -1;
533 SetDefaultStyleToCursorStyle();
538 BeginBatchUndo(_("Insert Text"));
540 long newPos
= m_caretPosition
;
541 DeleteSelectedContent(& newPos
);
543 wxString str
= (wxChar
) event
.GetKeyCode();
544 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
548 SetDefaultStyleToCursorStyle();
549 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
553 /// Delete content if there is a selection, e.g. when pressing a key.
554 bool wxRichTextCtrl
::DeleteSelectedContent(long* newPos
)
558 long pos
= m_selectionRange
.GetStart();
559 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
560 m_caretPosition
, // Current caret position
561 pos
, // New caret position
563 m_selectionRange
.SetRange(-2, -2);
573 /// Keyboard navigation
577 Left: left one character
578 Right: right one character
581 Ctrl-Left: left one word
582 Ctrl-Right: right one word
583 Ctrl-Up: previous paragraph start
584 Ctrl-Down: next start of paragraph
587 Ctrl-Home: start of document
588 Ctrl-End: end of document
590 Page-Down: Down a screen
594 Ctrl-Alt-PgUp: Start of window
595 Ctrl-Alt-PgDn: End of window
596 F8: Start selection mode
597 Esc: End selection mode
599 Adding Shift does the above but starts/extends selection.
604 bool wxRichTextCtrl
::KeyboardNavigate(int keyCode
, int flags
)
606 bool success
= false;
608 if (keyCode
== WXK_RIGHT
)
610 if (flags
& wxRICHTEXT_CTRL_DOWN
)
611 success
= WordRight(1, flags
);
613 success
= MoveRight(1, flags
);
615 else if (keyCode
== WXK_LEFT
)
617 if (flags
& wxRICHTEXT_CTRL_DOWN
)
618 success
= WordLeft(1, flags
);
620 success
= MoveLeft(1, flags
);
622 else if (keyCode
== WXK_UP
)
624 if (flags
& wxRICHTEXT_CTRL_DOWN
)
625 success
= MoveToParagraphStart(flags
);
627 success
= MoveUp(1, flags
);
629 else if (keyCode
== WXK_DOWN
)
631 if (flags
& wxRICHTEXT_CTRL_DOWN
)
632 success
= MoveToParagraphEnd(flags
);
634 success
= MoveDown(1, flags
);
636 else if (keyCode
== WXK_PAGEUP
)
638 success
= PageUp(1, flags
);
640 else if (keyCode
== WXK_PAGEDOWN
)
642 success
= PageDown(1, flags
);
644 else if (keyCode
== WXK_HOME
)
646 if (flags
& wxRICHTEXT_CTRL_DOWN
)
647 success
= MoveHome(flags
);
649 success
= MoveToLineStart(flags
);
651 else if (keyCode
== WXK_END
)
653 if (flags
& wxRICHTEXT_CTRL_DOWN
)
654 success
= MoveEnd(flags
);
656 success
= MoveToLineEnd(flags
);
661 ScrollIntoView(m_caretPosition
, keyCode
);
662 SetDefaultStyleToCursorStyle();
668 /// Extend the selection. Selections are in caret positions.
669 bool wxRichTextCtrl
::ExtendSelection(long oldPos
, long newPos
, int flags
)
671 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
673 // If not currently selecting, start selecting
674 if (m_selectionRange
.GetStart() == -2)
676 m_selectionAnchor
= oldPos
;
679 m_selectionRange
.SetRange(newPos
+1, oldPos
);
681 m_selectionRange
.SetRange(oldPos
+1, newPos
);
685 // Always ensure that the selection range start is greater than
687 if (newPos
> m_selectionAnchor
)
688 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
690 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
693 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
695 wxLogDebug(wxT("Strange selection range"));
704 /// Scroll into view, returning true if we scrolled.
705 /// This takes a _caret_ position.
706 bool wxRichTextCtrl
::ScrollIntoView(long position
, int keyCode
)
708 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
714 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
716 int startXUnits
, startYUnits
;
717 GetViewStart(& startXUnits
, & startYUnits
);
718 int startY
= startYUnits
* ppuY
;
721 GetVirtualSize(& sx
, & sy
);
727 wxRect rect
= line
->GetRect();
729 bool scrolled
= false;
731 wxSize clientSize
= GetClientSize();
734 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
736 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
738 // Make it scroll so this item is at the bottom
740 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
741 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
743 // If we're still off the screen, scroll another line down
744 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
747 if (startYUnits
!= yUnits
)
749 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
753 else if (rect
.y
< startY
)
755 // Make it scroll so this item is at the top
758 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
760 if (startYUnits
!= yUnits
)
762 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
768 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
772 // Make it scroll so this item is at the top
775 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
777 if (startYUnits
!= yUnits
)
779 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
783 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
785 // Make it scroll so this item is at the bottom
787 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
788 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
790 // If we're still off the screen, scroll another line down
791 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
794 if (startYUnits
!= yUnits
)
796 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
806 /// Is the given position visible on the screen?
807 bool wxRichTextCtrl
::IsPositionVisible(long pos
) const
809 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
815 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
818 GetViewStart(& startX
, & startY
);
820 startY
= startY
* ppuY
;
823 GetVirtualSize(& sx
, & sy
);
828 wxRect rect
= line
->GetRect();
830 wxSize clientSize
= GetClientSize();
832 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
835 void wxRichTextCtrl
::SetCaretPosition(long position
, bool showAtLineStart
)
837 m_caretPosition
= position
;
838 m_caretAtLineStart
= showAtLineStart
;
841 /// Move caret one visual step forward: this may mean setting a flag
842 /// and keeping the same position if we're going from the end of one line
843 /// to the start of the next, which may be the exact same caret position.
844 void wxRichTextCtrl
::MoveCaretForward(long oldPosition
)
846 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
848 // Only do the check if we're not at the end of the paragraph (where things work OK
850 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
852 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
856 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
858 // We're at the end of a line. See whether we need to
859 // stay at the same actual caret position but change visual
861 if (oldPosition
== lineRange
.GetEnd())
863 if (m_caretAtLineStart
)
865 // We're already at the start of the line, so actually move on now.
866 m_caretPosition
= oldPosition
+ 1;
867 m_caretAtLineStart
= false;
871 // We're showing at the end of the line, so keep to
872 // the same position but indicate that we're to show
873 // at the start of the next line.
874 m_caretPosition
= oldPosition
;
875 m_caretAtLineStart
= true;
877 SetDefaultStyleToCursorStyle();
883 SetDefaultStyleToCursorStyle();
886 /// Move caret one visual step backward: this may mean setting a flag
887 /// and keeping the same position if we're going from the end of one line
888 /// to the start of the next, which may be the exact same caret position.
889 void wxRichTextCtrl
::MoveCaretBack(long oldPosition
)
891 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
893 // Only do the check if we're not at the start of the paragraph (where things work OK
895 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
897 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
901 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
903 // We're at the start of a line. See whether we need to
904 // stay at the same actual caret position but change visual
906 if (oldPosition
== lineRange
.GetStart())
908 m_caretPosition
= oldPosition
-1;
909 m_caretAtLineStart
= true;
912 else if (oldPosition
== lineRange
.GetEnd())
914 if (m_caretAtLineStart
)
916 // We're at the start of the line, so keep the same caret position
917 // but clear the start-of-line flag.
918 m_caretPosition
= oldPosition
;
919 m_caretAtLineStart
= false;
923 // We're showing at the end of the line, so go back
924 // to the previous character position.
925 m_caretPosition
= oldPosition
- 1;
927 SetDefaultStyleToCursorStyle();
933 SetDefaultStyleToCursorStyle();
937 bool wxRichTextCtrl
::MoveRight(int noPositions
, int flags
)
939 long endPos
= GetBuffer().GetRange().GetEnd();
941 if (m_caretPosition
+ noPositions
< endPos
)
943 long oldPos
= m_caretPosition
;
944 long newPos
= m_caretPosition
+ noPositions
;
946 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
950 // Determine by looking at oldPos and m_caretPosition whether
951 // we moved from the end of a line to the start of the next line, in which case
952 // we want to adjust the caret position such that it is positioned at the
953 // start of the next line, rather than jumping past the first character of the
955 if (noPositions
== 1 && !extendSel
)
956 MoveCaretForward(oldPos
);
958 SetCaretPosition(newPos
);
961 SetDefaultStyleToCursorStyle();
972 bool wxRichTextCtrl
::MoveLeft(int noPositions
, int flags
)
976 if (m_caretPosition
> startPos
- noPositions
+ 1)
978 long oldPos
= m_caretPosition
;
979 long newPos
= m_caretPosition
- noPositions
;
980 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
984 if (noPositions
== 1 && !extendSel
)
985 MoveCaretBack(oldPos
);
987 SetCaretPosition(newPos
);
990 SetDefaultStyleToCursorStyle();
1001 bool wxRichTextCtrl
::MoveUp(int noLines
, int flags
)
1003 return MoveDown(- noLines
, flags
);
1007 bool wxRichTextCtrl
::MoveDown(int noLines
, int flags
)
1012 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1013 wxPoint pt
= GetCaret()->GetPosition();
1014 long newLine
= lineNumber
+ noLines
;
1016 if (lineNumber
!= -1)
1020 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1022 if (newLine
> lastLine
)
1032 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1035 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1041 wxClientDC
dc(this);
1043 dc
.SetFont(GetFont());
1045 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1047 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1049 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1050 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1051 // so we view the caret at the start of the line.
1052 bool caretLineStart
= false;
1053 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1055 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1056 wxRichTextRange lineRange
;
1058 lineRange
= thisLine
->GetAbsoluteRange();
1060 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1063 caretLineStart
= true;
1067 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1068 if (para
&& para
->GetRange().GetStart() == newPos
)
1073 long newSelEnd
= newPos
;
1075 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1079 SetCaretPosition(newPos
, caretLineStart
);
1081 SetDefaultStyleToCursorStyle();
1091 /// Move to the end of the paragraph
1092 bool wxRichTextCtrl
::MoveToParagraphEnd(int flags
)
1094 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1097 long newPos
= para
->GetRange().GetEnd() - 1;
1098 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1102 SetCaretPosition(newPos
);
1104 SetDefaultStyleToCursorStyle();
1114 /// Move to the start of the paragraph
1115 bool wxRichTextCtrl
::MoveToParagraphStart(int flags
)
1117 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1120 long newPos
= para
->GetRange().GetStart() - 1;
1121 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1125 SetCaretPosition(newPos
);
1127 SetDefaultStyleToCursorStyle();
1137 /// Move to the end of the line
1138 bool wxRichTextCtrl
::MoveToLineEnd(int flags
)
1140 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1144 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1145 long newPos
= lineRange
.GetEnd();
1146 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1150 SetCaretPosition(newPos
);
1152 SetDefaultStyleToCursorStyle();
1162 /// Move to the start of the line
1163 bool wxRichTextCtrl
::MoveToLineStart(int flags
)
1165 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1168 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1169 long newPos
= lineRange
.GetStart()-1;
1171 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1175 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1177 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1179 SetDefaultStyleToCursorStyle();
1189 /// Move to the start of the buffer
1190 bool wxRichTextCtrl
::MoveHome(int flags
)
1192 if (m_caretPosition
!= -1)
1194 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1198 SetCaretPosition(-1);
1200 SetDefaultStyleToCursorStyle();
1210 /// Move to the end of the buffer
1211 bool wxRichTextCtrl
::MoveEnd(int flags
)
1213 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1215 if (m_caretPosition
!= endPos
)
1217 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1221 SetCaretPosition(endPos
);
1223 SetDefaultStyleToCursorStyle();
1233 /// Move noPages pages up
1234 bool wxRichTextCtrl
::PageUp(int noPages
, int flags
)
1236 return PageDown(- noPages
, flags
);
1239 /// Move noPages pages down
1240 bool wxRichTextCtrl
::PageDown(int noPages
, int flags
)
1242 // Calculate which line occurs noPages * screen height further down.
1243 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1246 wxSize clientSize
= GetClientSize();
1247 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1249 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1252 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1253 long pos
= lineRange
.GetStart()-1;
1254 if (pos
!= m_caretPosition
)
1256 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1258 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1262 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1264 SetDefaultStyleToCursorStyle();
1276 // Finds the caret position for the next word
1277 long wxRichTextCtrl
::FindNextWordPosition(int direction
) const
1279 long endPos
= GetBuffer().GetRange().GetEnd();
1283 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1285 // First skip current text to space
1286 while (i
< endPos
&& i
> -1)
1288 // i is in character, not caret positions
1289 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1290 if (text
!= wxT(" ") && !text
.empty())
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
1302 return wxMax(-1, i
- 1);
1303 else if (text
== wxT(" ") || text
.empty())
1307 // Convert to caret position
1308 return wxMax(-1, i
- 1);
1317 long i
= m_caretPosition
;
1319 // First skip white space
1320 while (i
< endPos
&& i
> -1)
1322 // i is in character, not caret positions
1323 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1324 if (text
.empty()) // End of paragraph, or maybe an image
1326 else if (text
== wxT(" ") || text
.empty())
1331 // Next skip current text to space
1332 while (i
< endPos
&& i
> -1)
1334 // i is in character, not caret positions
1335 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1336 if (text
!= wxT(" ") /* && !text.empty() */)
1349 /// Move n words left
1350 bool wxRichTextCtrl
::WordLeft(int WXUNUSED(n
), int flags
)
1352 long pos
= FindNextWordPosition(-1);
1353 if (pos
!= m_caretPosition
)
1355 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1357 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1361 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1363 SetDefaultStyleToCursorStyle();
1373 /// Move n words right
1374 bool wxRichTextCtrl
::WordRight(int WXUNUSED(n
), int flags
)
1376 long pos
= FindNextWordPosition(1);
1377 if (pos
!= m_caretPosition
)
1379 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1381 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1385 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1387 SetDefaultStyleToCursorStyle();
1398 void wxRichTextCtrl
::OnSize(wxSizeEvent
& event
)
1400 // Only do sizing optimization for large buffers
1401 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1403 m_fullLayoutRequired
= true;
1404 m_fullLayoutTime
= wxGetLocalTimeMillis();
1405 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1406 LayoutContent(true /* onlyVisibleRect */);
1409 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1417 /// Idle-time processing
1418 void wxRichTextCtrl
::OnIdle(wxIdleEvent
& event
)
1420 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1422 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1424 m_fullLayoutRequired
= false;
1425 m_fullLayoutTime
= 0;
1426 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1427 ShowPosition(m_fullLayoutSavedPosition
);
1434 void wxRichTextCtrl
::OnScroll(wxScrollWinEvent
& event
)
1440 /// Set up scrollbars, e.g. after a resize
1441 void wxRichTextCtrl
::SetupScrollbars(bool atTop
)
1446 if (GetBuffer().IsEmpty())
1448 SetScrollbars(0, 0, 0, 0, 0, 0);
1452 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1453 // of pixels. See e.g. wxVScrolledWindow for ideas.
1454 int pixelsPerUnit
= 5;
1455 wxSize clientSize
= GetClientSize();
1457 int maxHeight
= GetBuffer().GetCachedSize().y
;
1459 // Round up so we have at least maxHeight pixels
1460 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1462 int startX
= 0, startY
= 0;
1464 GetViewStart(& startX
, & startY
);
1466 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1467 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1469 // Move to previous scroll position if
1471 SetScrollbars(0, pixelsPerUnit
,
1473 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1476 /// Paint the background
1477 void wxRichTextCtrl
::PaintBackground(wxDC
& dc
)
1479 wxColour backgroundColour
= GetBackgroundColour();
1480 if (!backgroundColour
.Ok())
1481 backgroundColour
= wxSystemSettings
::GetColour(wxSYS_COLOUR_3DFACE
);
1483 // Clear the background
1484 dc
.SetBrush(wxBrush(backgroundColour
));
1485 dc
.SetPen(*wxTRANSPARENT_PEN
);
1486 wxRect
windowRect(GetClientSize());
1487 windowRect
.x
-= 2; windowRect
.y
-= 2;
1488 windowRect
.width
+= 4; windowRect
.height
+= 4;
1490 // We need to shift the rectangle to take into account
1491 // scrolling. Converting device to logical coordinates.
1492 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1493 dc
.DrawRectangle(windowRect
);
1496 /// Recreate buffer bitmap if necessary
1497 bool wxRichTextCtrl
::RecreateBuffer(const wxSize
& size
)
1500 if (sz
== wxDefaultSize
)
1501 sz
= GetClientSize();
1503 if (sz
.x
< 1 || sz
.y
< 1)
1506 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1507 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1508 return m_bufferBitmap
.Ok();
1511 // ----------------------------------------------------------------------------
1512 // file IO functions
1513 // ----------------------------------------------------------------------------
1515 bool wxRichTextCtrl
::LoadFile(const wxString
& filename
, int type
)
1517 bool success
= GetBuffer().LoadFile(filename
, type
);
1519 m_filename
= filename
;
1522 SetInsertionPoint(0);
1525 SetupScrollbars(true);
1533 wxLogError(_("File couldn't be loaded."));
1539 bool wxRichTextCtrl
::SaveFile(const wxString
& filename
, int type
)
1541 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1542 if ( filenameToUse
.empty() )
1544 // what kind of message to give? is it an error or a program bug?
1545 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1550 if (GetBuffer().SaveFile(filenameToUse
, type
))
1552 m_filename
= filenameToUse
;
1560 wxLogError(_("The text couldn't be saved."));
1565 // ----------------------------------------------------------------------------
1566 // wxRichTextCtrl specific functionality
1567 // ----------------------------------------------------------------------------
1569 /// Add a new paragraph of text to the end of the buffer
1570 wxRichTextRange wxRichTextCtrl
::AddParagraph(const wxString
& text
)
1572 return GetBuffer().AddParagraph(text
);
1576 wxRichTextRange wxRichTextCtrl
::AddImage(const wxImage
& image
)
1578 return GetBuffer().AddImage(image
);
1581 // ----------------------------------------------------------------------------
1582 // selection and ranges
1583 // ----------------------------------------------------------------------------
1585 void wxRichTextCtrl
::SelectAll()
1587 SetSelection(0, GetLastPosition());
1588 m_selectionAnchor
= -1;
1592 void wxRichTextCtrl
::SelectNone()
1594 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1595 SetSelection(-2, -2);
1596 m_selectionAnchor
= -2;
1599 wxString wxRichTextCtrl
::GetStringSelection() const
1602 GetSelection(&from
, &to
);
1604 return GetRange(from
, to
);
1607 // do the window-specific processing after processing the update event
1608 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1609 void wxRichTextCtrl
::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1612 wxWindowBase
::DoUpdateWindowUI(event
);
1615 if ( event
.GetSetText() )
1617 if ( event
.GetText() != GetValue() )
1618 SetValue(event
.GetText());
1621 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1623 // ----------------------------------------------------------------------------
1625 // ----------------------------------------------------------------------------
1627 wxTextCtrlHitTestResult
1628 wxRichTextCtrl
::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1630 // implement in terms of the other overload as the native ports typically
1631 // can get the position and not (x, y) pair directly (although wxUniv
1632 // directly gets x and y -- and so overrides this method as well)
1634 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1636 if ( rc
!= wxTE_HT_UNKNOWN
)
1638 PositionToXY(pos
, x
, y
);
1644 wxTextCtrlHitTestResult
1645 wxRichTextCtrl
::HitTest(const wxPoint
& pt
,
1648 wxClientDC
dc((wxRichTextCtrl
*) this);
1649 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1651 // Buffer uses logical position (relative to start of buffer)
1653 wxPoint pt2
= GetLogicalPoint(pt
);
1655 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1659 case wxRICHTEXT_HITTEST_BEFORE
:
1660 return wxTE_HT_BEFORE
;
1662 case wxRICHTEXT_HITTEST_AFTER
:
1663 return wxTE_HT_BEYOND
;
1665 case wxRICHTEXT_HITTEST_ON
:
1666 return wxTE_HT_ON_TEXT
;
1669 return wxTE_HT_UNKNOWN
;
1672 // ----------------------------------------------------------------------------
1673 // set/get the controls text
1674 // ----------------------------------------------------------------------------
1676 wxString wxRichTextCtrl
::GetValue() const
1678 return GetBuffer().GetText();
1681 wxString wxRichTextCtrl
::GetRange(long from
, long to
) const
1683 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1686 void wxRichTextCtrl
::SetValue(const wxString
& value
)
1690 // if the text is long enough, it's faster to just set it instead of first
1691 // comparing it with the old one (chances are that it will be different
1692 // anyhow, this comparison is there to avoid flicker for small single-line
1693 // edit controls mostly)
1694 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1696 DoWriteText(value
, false /* not selection only */);
1698 // for compatibility, don't move the cursor when doing SetValue()
1699 SetInsertionPoint(0);
1703 // still send an event for consistency
1707 // we should reset the modified flag even if the value didn't really change
1709 // mark the control as being not dirty - we changed its text, not the
1714 void wxRichTextCtrl
::WriteText(const wxString
& value
)
1719 void wxRichTextCtrl
::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1721 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1724 void wxRichTextCtrl
::AppendText(const wxString
& text
)
1726 SetInsertionPointEnd();
1731 /// Write an image at the current insertion point
1732 bool wxRichTextCtrl
::WriteImage(const wxImage
& image
, int bitmapType
)
1734 wxRichTextImageBlock imageBlock
;
1736 wxImage image2
= image
;
1737 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1738 return WriteImage(imageBlock
);
1743 bool wxRichTextCtrl
::WriteImage(const wxString
& filename
, int bitmapType
)
1745 wxRichTextImageBlock imageBlock
;
1748 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1749 return WriteImage(imageBlock
);
1754 bool wxRichTextCtrl
::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1756 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1759 bool wxRichTextCtrl
::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1763 wxRichTextImageBlock imageBlock
;
1765 wxImage image
= bitmap
.ConvertToImage();
1766 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1767 return WriteImage(imageBlock
);
1773 /// Insert a newline (actually paragraph) at the current insertion point.
1774 bool wxRichTextCtrl
::Newline()
1776 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1780 // ----------------------------------------------------------------------------
1781 // Clipboard operations
1782 // ----------------------------------------------------------------------------
1784 void wxRichTextCtrl
::Copy()
1788 wxRichTextRange range
= GetSelectionRange();
1789 GetBuffer().CopyToClipboard(range
);
1793 void wxRichTextCtrl
::Cut()
1797 wxRichTextRange range
= GetSelectionRange();
1798 GetBuffer().CopyToClipboard(range
);
1800 DeleteSelectedContent();
1806 void wxRichTextCtrl
::Paste()
1810 BeginBatchUndo(_("Paste"));
1812 long newPos
= m_caretPosition
;
1813 DeleteSelectedContent(& newPos
);
1815 GetBuffer().PasteFromClipboard(newPos
);
1821 void wxRichTextCtrl
::DeleteSelection()
1823 if (CanDeleteSelection())
1825 DeleteSelectedContent();
1829 bool wxRichTextCtrl
::HasSelection() const
1831 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1834 bool wxRichTextCtrl
::CanCopy() const
1836 // Can copy if there's a selection
1837 return HasSelection();
1840 bool wxRichTextCtrl
::CanCut() const
1842 return HasSelection() && IsEditable();
1845 bool wxRichTextCtrl
::CanPaste() const
1847 if ( !IsEditable() )
1850 return GetBuffer().CanPasteFromClipboard();
1853 bool wxRichTextCtrl
::CanDeleteSelection() const
1855 return HasSelection() && IsEditable();
1859 // ----------------------------------------------------------------------------
1861 // ----------------------------------------------------------------------------
1863 void wxRichTextCtrl
::SetEditable(bool editable
)
1865 m_editable
= editable
;
1868 void wxRichTextCtrl
::SetInsertionPoint(long pos
)
1872 m_caretPosition
= pos
- 1;
1875 void wxRichTextCtrl
::SetInsertionPointEnd()
1877 long pos
= GetLastPosition();
1878 SetInsertionPoint(pos
);
1881 long wxRichTextCtrl
::GetInsertionPoint() const
1883 return m_caretPosition
+1;
1886 wxTextPos wxRichTextCtrl
::GetLastPosition() const
1888 return GetBuffer().GetRange().GetEnd();
1891 // If the return values from and to are the same, there is no
1893 void wxRichTextCtrl
::GetSelection(long* from
, long* to
) const
1895 *from
= m_selectionRange
.GetStart();
1896 *to
= m_selectionRange
.GetEnd();
1899 bool wxRichTextCtrl
::IsEditable() const
1904 // ----------------------------------------------------------------------------
1906 // ----------------------------------------------------------------------------
1908 void wxRichTextCtrl
::SetSelection(long from
, long to
)
1910 // if from and to are both -1, it means (in wxWidgets) that all text should
1912 if ( (from
== -1) && (to
== -1) )
1915 to
= GetLastPosition();
1918 DoSetSelection(from
, to
);
1921 void wxRichTextCtrl
::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1923 m_selectionAnchor
= from
;
1924 m_selectionRange
.SetRange(from
, to
);
1929 // ----------------------------------------------------------------------------
1931 // ----------------------------------------------------------------------------
1933 void wxRichTextCtrl
::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1935 BeginBatchUndo(_("Replace"));
1937 DeleteSelectedContent();
1939 DoWriteText(value
, true /* selection only */);
1944 void wxRichTextCtrl
::Remove(long from
, long to
)
1948 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1949 m_caretPosition
, // Current caret position
1950 from
, // New caret position
1958 bool wxRichTextCtrl
::IsModified() const
1960 return m_buffer
.IsModified();
1963 void wxRichTextCtrl
::MarkDirty()
1965 m_buffer
.Modify(true);
1968 void wxRichTextCtrl
::DiscardEdits()
1970 m_buffer
.Modify(false);
1971 m_buffer
.GetCommandProcessor()->ClearCommands();
1974 int wxRichTextCtrl
::GetNumberOfLines() const
1976 return GetBuffer().GetParagraphCount();
1979 // ----------------------------------------------------------------------------
1980 // Positions <-> coords
1981 // ----------------------------------------------------------------------------
1983 long wxRichTextCtrl
::XYToPosition(long x
, long y
) const
1985 return GetBuffer().XYToPosition(x
, y
);
1988 bool wxRichTextCtrl
::PositionToXY(long pos
, long *x
, long *y
) const
1990 return GetBuffer().PositionToXY(pos
, x
, y
);
1993 // ----------------------------------------------------------------------------
1995 // ----------------------------------------------------------------------------
1997 void wxRichTextCtrl
::ShowPosition(long pos
)
1999 if (!IsPositionVisible(pos
))
2000 ScrollIntoView(pos
-1, WXK_DOWN
);
2003 int wxRichTextCtrl
::GetLineLength(long lineNo
) const
2005 return GetBuffer().GetParagraphLength(lineNo
);
2008 wxString wxRichTextCtrl
::GetLineText(long lineNo
) const
2010 return GetBuffer().GetParagraphText(lineNo
);
2013 // ----------------------------------------------------------------------------
2015 // ----------------------------------------------------------------------------
2017 void wxRichTextCtrl
::Undo()
2021 GetCommandProcessor()->Undo();
2025 void wxRichTextCtrl
::Redo()
2029 GetCommandProcessor()->Redo();
2033 bool wxRichTextCtrl
::CanUndo() const
2035 return GetCommandProcessor()->CanUndo();
2038 bool wxRichTextCtrl
::CanRedo() const
2040 return GetCommandProcessor()->CanRedo();
2043 // ----------------------------------------------------------------------------
2044 // implementation details
2045 // ----------------------------------------------------------------------------
2047 void wxRichTextCtrl
::Command(wxCommandEvent
& event
)
2049 SetValue(event
.GetString());
2050 GetEventHandler()->ProcessEvent(event
);
2053 void wxRichTextCtrl
::OnDropFiles(wxDropFilesEvent
& event
)
2055 // By default, load the first file into the text window.
2056 if (event
.GetNumberOfFiles() > 0)
2058 LoadFile(event
.GetFiles()[0]);
2062 // ----------------------------------------------------------------------------
2063 // text control event processing
2064 // ----------------------------------------------------------------------------
2066 bool wxRichTextCtrl
::SendUpdateEvent()
2068 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2069 InitCommandEvent(event
);
2071 return GetEventHandler()->ProcessEvent(event
);
2074 void wxRichTextCtrl
::InitCommandEvent(wxCommandEvent
& event
) const
2076 event
.SetEventObject((wxControlBase
*)this); // const_cast
2078 switch ( m_clientDataType
)
2080 case wxClientData_Void
:
2081 event
.SetClientData(GetClientData());
2084 case wxClientData_Object
:
2085 event
.SetClientObject(GetClientObject());
2088 case wxClientData_None
:
2095 wxSize wxRichTextCtrl
::DoGetBestSize() const
2097 return wxSize(10, 10);
2100 // ----------------------------------------------------------------------------
2101 // standard handlers for standard edit menu events
2102 // ----------------------------------------------------------------------------
2104 void wxRichTextCtrl
::OnCut(wxCommandEvent
& WXUNUSED(event
))
2109 void wxRichTextCtrl
::OnClear(wxCommandEvent
& WXUNUSED(event
))
2114 void wxRichTextCtrl
::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2119 void wxRichTextCtrl
::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2124 void wxRichTextCtrl
::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2129 void wxRichTextCtrl
::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2134 void wxRichTextCtrl
::OnUpdateCut(wxUpdateUIEvent
& event
)
2136 event
.Enable( CanCut() );
2139 void wxRichTextCtrl
::OnUpdateCopy(wxUpdateUIEvent
& event
)
2141 event
.Enable( CanCopy() );
2144 void wxRichTextCtrl
::OnUpdateClear(wxUpdateUIEvent
& event
)
2146 event
.Enable( CanDeleteSelection() );
2149 void wxRichTextCtrl
::OnUpdatePaste(wxUpdateUIEvent
& event
)
2151 event
.Enable( CanPaste() );
2154 void wxRichTextCtrl
::OnUpdateUndo(wxUpdateUIEvent
& event
)
2156 event
.Enable( CanUndo() );
2157 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2160 void wxRichTextCtrl
::OnUpdateRedo(wxUpdateUIEvent
& event
)
2162 event
.Enable( CanRedo() );
2163 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2166 void wxRichTextCtrl
::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2171 void wxRichTextCtrl
::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2173 event
.Enable(GetLastPosition() > 0);
2176 void wxRichTextCtrl
::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2180 m_contextMenu
= new wxMenu
;
2181 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2182 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2183 m_contextMenu
->AppendSeparator();
2184 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2185 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2186 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2187 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2188 m_contextMenu
->AppendSeparator();
2189 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2191 PopupMenu(m_contextMenu
);
2195 bool wxRichTextCtrl
::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2197 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2200 bool wxRichTextCtrl
::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2202 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), wxTextAttrEx(style
));
2205 bool wxRichTextCtrl
::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2207 return GetBuffer().SetStyle(range
, style
);
2210 bool wxRichTextCtrl
::SetDefaultStyle(const wxTextAttrEx
& style
)
2212 return GetBuffer().SetDefaultStyle(style
);
2215 bool wxRichTextCtrl
::SetDefaultStyle(const wxTextAttr
& style
)
2217 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2220 const wxTextAttrEx
& wxRichTextCtrl
::GetDefaultStyleEx() const
2222 return GetBuffer().GetDefaultStyle();
2225 const wxTextAttr
& wxRichTextCtrl
::GetDefaultStyle() const
2227 return GetBuffer().GetDefaultStyle();
2230 bool wxRichTextCtrl
::GetStyle(long position
, wxTextAttr
& style
) const
2233 if (GetBuffer().GetStyle(position
, attr
))
2242 bool wxRichTextCtrl
::GetStyle(long position
, wxTextAttrEx
& style
) const
2244 return GetBuffer().GetStyle(position
, style
);
2247 bool wxRichTextCtrl
::GetStyle(long position
, wxRichTextAttr
& style
) const
2249 return GetBuffer().GetStyle(position
, style
);
2252 /// Set font, and also the buffer attributes
2253 bool wxRichTextCtrl
::SetFont(const wxFont
& font
)
2255 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2256 wxControl
::SetFont(font
);
2258 wxScrolledWindow
::SetFont(font
);
2261 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2263 GetBuffer().SetBasicStyle(attr
);
2264 GetBuffer().SetDefaultStyle(attr
);
2269 /// Transform logical to physical
2270 wxPoint wxRichTextCtrl
::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2273 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2278 /// Transform physical to logical
2279 wxPoint wxRichTextCtrl
::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2282 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2287 /// Position the caret
2288 void wxRichTextCtrl
::PositionCaret()
2293 //wxLogDebug(wxT("PositionCaret"));
2296 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2298 wxPoint originalPt
= caretRect
.GetPosition();
2299 wxPoint pt
= GetPhysicalPoint(originalPt
);
2300 if (GetCaret()->GetPosition() != pt
)
2302 GetCaret()->Move(pt
);
2303 GetCaret()->SetSize(caretRect
.GetSize());
2308 /// Get the caret height and position for the given character position
2309 bool wxRichTextCtrl
::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2311 wxClientDC
dc(this);
2312 dc
.SetFont(GetFont());
2319 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2321 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2328 /// Gets the line for the visible caret position. If the caret is
2329 /// shown at the very end of the line, it means the next character is actually
2330 /// on the following line. So let's get the line we're expecting to find
2331 /// if this is the case.
2332 wxRichTextLine
* wxRichTextCtrl
::GetVisibleLineForCaretPosition(long caretPosition
) const
2334 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2335 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2338 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2339 if (caretPosition
== lineRange
.GetStart()-1 &&
2340 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2342 if (!m_caretAtLineStart
)
2343 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2350 /// Move the caret to the given character position
2351 bool wxRichTextCtrl
::MoveCaret(long pos
, bool showAtLineStart
)
2353 if (GetBuffer().GetDirty())
2356 if (pos
<= GetBuffer().GetRange().GetEnd())
2358 SetCaretPosition(pos
, showAtLineStart
);
2368 /// Layout the buffer: which we must do before certain operations, such as
2369 /// setting the caret position.
2370 bool wxRichTextCtrl
::LayoutContent(bool onlyVisibleRect
)
2372 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2374 wxRect
availableSpace(GetClientSize());
2375 if (availableSpace
.width
== 0)
2376 availableSpace
.width
= 10;
2377 if (availableSpace
.height
== 0)
2378 availableSpace
.height
= 10;
2380 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2381 if (onlyVisibleRect
)
2383 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2384 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2387 wxClientDC
dc(this);
2388 dc
.SetFont(GetFont());
2392 GetBuffer().Defragment();
2393 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2394 GetBuffer().Layout(dc
, availableSpace
, flags
);
2395 GetBuffer().SetDirty(false);
2404 /// Is all of the selection bold?
2405 bool wxRichTextCtrl
::IsSelectionBold() const
2409 wxRichTextAttr attr
;
2410 wxRichTextRange range
= GetSelectionRange();
2411 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2412 attr
.SetFontWeight(wxBOLD
);
2414 return HasCharacterAttributes(range
, attr
);
2418 // If no selection, then we need to combine current style with default style
2419 // to see what the effect would be if we started typing.
2420 wxRichTextAttr attr
;
2421 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2422 if (GetStyle(GetCaretPosition()+1, attr
))
2424 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2425 return attr
.GetFontWeight() == wxBOLD
;
2431 /// Is all of the selection italics?
2432 bool wxRichTextCtrl
::IsSelectionItalics() const
2436 wxRichTextRange range
= GetSelectionRange();
2437 wxRichTextAttr attr
;
2438 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2439 attr
.SetFontStyle(wxITALIC
);
2441 return HasCharacterAttributes(range
, attr
);
2445 // If no selection, then we need to combine current style with default style
2446 // to see what the effect would be if we started typing.
2447 wxRichTextAttr attr
;
2448 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2449 if (GetStyle(GetCaretPosition()+1, attr
))
2451 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2452 return attr
.GetFontStyle() == wxITALIC
;
2458 /// Is all of the selection underlined?
2459 bool wxRichTextCtrl
::IsSelectionUnderlined() const
2463 wxRichTextRange range
= GetSelectionRange();
2464 wxRichTextAttr attr
;
2465 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2466 attr
.SetFontUnderlined(true);
2468 return HasCharacterAttributes(range
, attr
);
2472 // If no selection, then we need to combine current style with default style
2473 // to see what the effect would be if we started typing.
2474 wxRichTextAttr attr
;
2475 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2476 if (GetStyle(GetCaretPosition()+1, attr
))
2478 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2479 return attr
.GetFontUnderlined();
2485 /// Apply bold to the selection
2486 bool wxRichTextCtrl
::ApplyBoldToSelection()
2488 wxRichTextAttr attr
;
2489 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2490 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2493 return SetStyle(GetSelectionRange(), attr
);
2495 SetDefaultStyle(attr
);
2499 /// Apply italic to the selection
2500 bool wxRichTextCtrl
::ApplyItalicToSelection()
2502 wxRichTextAttr attr
;
2503 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2504 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2507 return SetStyle(GetSelectionRange(), attr
);
2509 SetDefaultStyle(attr
);
2513 /// Apply underline to the selection
2514 bool wxRichTextCtrl
::ApplyUnderlineToSelection()
2516 wxRichTextAttr attr
;
2517 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2518 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2521 return SetStyle(GetSelectionRange(), attr
);
2523 SetDefaultStyle(attr
);
2527 /// Is all of the selection aligned according to the specified flag?
2528 bool wxRichTextCtrl
::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2532 wxRichTextRange range
= GetSelectionRange();
2533 wxRichTextAttr attr
;
2534 attr
.SetAlignment(alignment
);
2536 return HasParagraphAttributes(range
, attr
);
2540 // If no selection, then we need to get information from the current paragraph.
2541 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2543 return para
->GetAttributes().GetAlignment() == alignment
;
2548 /// Apply alignment to the selection
2549 bool wxRichTextCtrl
::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2551 wxRichTextAttr attr
;
2552 attr
.SetAlignment(alignment
);
2554 return SetStyle(GetSelectionRange(), attr
);
2557 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2559 return SetStyle(para
->GetRange(), attr
);
2564 /// Sets the default style to the style under the cursor
2565 bool wxRichTextCtrl
::SetDefaultStyleToCursorStyle()
2568 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2570 if (GetStyle(GetCaretPosition(), attr
))
2572 SetDefaultStyle(attr
);
2579 /// Returns the first visible position in the current view
2580 long wxRichTextCtrl
::GetFirstVisiblePosition() const
2582 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2584 return line
->GetAbsoluteRange().GetStart();