1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
27 #include "wx/textfile.h"
29 #include "wx/settings.h"
30 #include "wx/filename.h"
31 #include "wx/dcbuffer.h"
32 #include "wx/arrimpl.cpp"
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
42 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
43 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
45 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
48 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
50 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
51 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
53 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
55 EVT_PAINT(wxRichTextCtrl::OnPaint
)
56 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
57 EVT_IDLE(wxRichTextCtrl::OnIdle
)
58 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
59 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
60 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
61 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
62 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
63 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
64 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
65 EVT_CHAR(wxRichTextCtrl::OnChar
)
66 EVT_SIZE(wxRichTextCtrl::OnSize
)
67 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
68 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
69 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
71 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
72 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
74 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
75 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
77 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
78 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
80 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
81 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
83 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
84 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
86 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
87 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
89 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
90 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
97 wxRichTextCtrl::wxRichTextCtrl()
98 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
99 : wxScrollHelper(this)
105 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
106 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
107 : wxScrollHelper(this)
111 Create(parent
, id
, pos
, size
, style
);
115 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
117 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
118 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
122 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
129 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
132 GetBuffer().SetRichTextCtrl(this);
134 wxTextAttrEx attributes
;
135 attributes
.SetFont(GetFont());
136 attributes
.SetTextColour(*wxBLACK
);
137 attributes
.SetBackgroundColour(*wxWHITE
);
138 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
139 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
141 SetDefaultStyle(attributes
);
142 SetBasicStyle(attributes
);
144 SetBackgroundColour(*wxWHITE
);
145 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
147 // Tell the sizers to use the given or best size
148 SetBestFittingSize(size
);
151 RecreateBuffer(size
);
153 SetCursor(wxCursor(wxCURSOR_IBEAM
));
158 wxRichTextCtrl::~wxRichTextCtrl()
160 delete m_contextMenu
;
163 /// Member initialisation
164 void wxRichTextCtrl::Init()
167 m_contextMenu
= NULL
;
169 m_caretPosition
= -1;
170 m_selectionRange
.SetRange(-2, -2);
171 m_selectionAnchor
= -2;
173 m_caretAtLineStart
= false;
175 m_fullLayoutRequired
= false;
176 m_fullLayoutTime
= 0;
177 m_fullLayoutSavedPosition
= 0;
178 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
181 /// Call Freeze to prevent refresh
182 void wxRichTextCtrl::Freeze()
187 /// Call Thaw to refresh
188 void wxRichTextCtrl::Thaw()
192 if (m_freezeCount
== 0)
200 void wxRichTextCtrl::Clear()
203 m_buffer
.SetDirty(true);
204 m_caretPosition
= -1;
205 m_caretAtLineStart
= false;
206 m_selectionRange
.SetRange(-2, -2);
208 if (m_freezeCount
== 0)
217 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
223 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
224 //wxLogDebug(wxT("OnPaint"));
228 if (m_freezeCount
> 0)
231 dc
.SetFont(GetFont());
233 // Paint the background
236 wxRegion dirtyRegion
= GetUpdateRegion();
238 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
239 wxRect
availableSpace(GetClientSize());
240 if (GetBuffer().GetDirty())
242 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
243 GetBuffer().SetDirty(false);
247 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
256 // Empty implementation, to prevent flicker
257 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
261 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
263 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
272 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
281 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
287 dc
.SetFont(GetFont());
290 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
292 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
294 m_dragStart
= event
.GetLogicalPosition(dc
);
300 bool caretAtLineStart
= false;
302 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
304 // If we're at the start of a line (but not first in para)
305 // then we should keep the caret showing at the start of the line
306 // by showing the m_caretAtLineStart flag.
307 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
308 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
310 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
311 caretAtLineStart
= true;
315 MoveCaret(position
, caretAtLineStart
);
316 SetDefaultStyleToCursorStyle();
319 wxWindow
* p
= GetParent();
320 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
323 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
326 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
327 frame
->SetStatusText(msg
, 1);
336 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
341 if (GetCapture() == this)
347 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
349 if (!event
.Dragging())
357 dc
.SetFont(GetFont());
360 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
361 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
363 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
365 // TODO: test closeness
367 bool caretAtLineStart
= false;
369 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
371 // If we're at the start of a line (but not first in para)
372 // then we should keep the caret showing at the start of the line
373 // by showing the m_caretAtLineStart flag.
374 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
375 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
377 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
378 caretAtLineStart
= true;
382 if (m_caretPosition
!= position
)
384 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
386 MoveCaret(position
, caretAtLineStart
);
387 SetDefaultStyleToCursorStyle();
396 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
402 /// Left-double-click
403 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
409 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
415 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
418 if (event
.ControlDown())
419 flags
|= wxRICHTEXT_CTRL_DOWN
;
420 if (event
.ShiftDown())
421 flags
|= wxRICHTEXT_SHIFT_DOWN
;
423 flags
|= wxRICHTEXT_ALT_DOWN
;
425 if (event
.GetKeyCode() == WXK_LEFT
||
426 event
.GetKeyCode() == WXK_RIGHT
||
427 event
.GetKeyCode() == WXK_UP
||
428 event
.GetKeyCode() == WXK_DOWN
||
429 event
.GetKeyCode() == WXK_HOME
||
430 event
.GetKeyCode() == WXK_PAGEUP
||
431 event
.GetKeyCode() == WXK_PAGEDOWN
||
432 event
.GetKeyCode() == WXK_PRIOR
||
433 event
.GetKeyCode() == WXK_NEXT
||
434 event
.GetKeyCode() == WXK_END
)
436 KeyboardNavigate(event
.GetKeyCode(), flags
);
438 else if (event
.GetKeyCode() == WXK_RETURN
)
440 BeginBatchUndo(_("Insert Text"));
442 long newPos
= m_caretPosition
;
444 DeleteSelectedContent(& newPos
);
446 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
448 wxRichTextEvent
cmdEvent(
449 wxEVT_COMMAND_RICHTEXT_RETURN
,
451 cmdEvent
.SetEventObject(this);
452 cmdEvent
.SetFlags(flags
);
453 GetEventHandler()->ProcessEvent(cmdEvent
);
456 SetDefaultStyleToCursorStyle();
458 else if (event
.GetKeyCode() == WXK_BACK
)
460 BeginBatchUndo(_("Delete Text"));
462 // Submit range in character positions, which are greater than caret positions,
463 // so subtract 1 for deleted character and add 1 for conversion to character position.
464 if (m_caretPosition
> -1 && !HasSelection())
466 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
467 m_caretPosition
, // Current caret position
468 m_caretPosition
-1, // New caret position
472 DeleteSelectedContent();
476 // Shouldn't this be in Do()?
477 if (GetLastPosition() == -1)
481 m_caretPosition
= -1;
483 SetDefaultStyleToCursorStyle();
487 else if (event
.GetKeyCode() == WXK_DELETE
)
489 BeginBatchUndo(_("Delete Text"));
491 // Submit range in character positions, which are greater than caret positions,
492 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
494 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
495 m_caretPosition
, // Current caret position
496 m_caretPosition
+1, // New caret position
500 DeleteSelectedContent();
504 // Shouldn't this be in Do()?
505 if (GetLastPosition() == -1)
509 m_caretPosition
= -1;
511 SetDefaultStyleToCursorStyle();
516 BeginBatchUndo(_("Insert Text"));
518 long newPos
= m_caretPosition
;
519 DeleteSelectedContent(& newPos
);
521 wxString str
= (wxChar
) event
.GetKeyCode();
522 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
526 SetDefaultStyleToCursorStyle();
534 /// Delete content if there is a selection, e.g. when pressing a key.
535 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
539 long pos
= m_selectionRange
.GetStart();
540 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
541 m_caretPosition
, // Current caret position
542 pos
, // New caret position
544 m_selectionRange
.SetRange(-2, -2);
554 /// Keyboard navigation
558 Left: left one character
559 Right: right one character
562 Ctrl-Left: left one word
563 Ctrl-Right: right one word
564 Ctrl-Up: previous paragraph start
565 Ctrl-Down: next start of paragraph
568 Ctrl-Home: start of document
569 Ctrl-End: end of document
571 Page-Down: Down a screen
575 Ctrl-Alt-PgUp: Start of window
576 Ctrl-Alt-PgDn: End of window
577 F8: Start selection mode
578 Esc: End selection mode
580 Adding Shift does the above but starts/extends selection.
585 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
587 bool success
= false;
589 if (keyCode
== WXK_RIGHT
)
591 if (flags
& wxRICHTEXT_CTRL_DOWN
)
592 success
= WordRight(1, flags
);
594 success
= MoveRight(1, flags
);
596 else if (keyCode
== WXK_LEFT
)
598 if (flags
& wxRICHTEXT_CTRL_DOWN
)
599 success
= WordLeft(1, flags
);
601 success
= MoveLeft(1, flags
);
603 else if (keyCode
== WXK_UP
)
605 if (flags
& wxRICHTEXT_CTRL_DOWN
)
606 success
= MoveToParagraphStart(flags
);
608 success
= MoveUp(1, flags
);
610 else if (keyCode
== WXK_DOWN
)
612 if (flags
& wxRICHTEXT_CTRL_DOWN
)
613 success
= MoveToParagraphEnd(flags
);
615 success
= MoveDown(1, flags
);
617 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_PRIOR
)
619 success
= PageUp(1, flags
);
621 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NEXT
)
623 success
= PageDown(1, flags
);
625 else if (keyCode
== WXK_HOME
)
627 if (flags
& wxRICHTEXT_CTRL_DOWN
)
628 success
= MoveHome(flags
);
630 success
= MoveToLineStart(flags
);
632 else if (keyCode
== WXK_END
)
634 if (flags
& wxRICHTEXT_CTRL_DOWN
)
635 success
= MoveEnd(flags
);
637 success
= MoveToLineEnd(flags
);
642 ScrollIntoView(m_caretPosition
, keyCode
);
643 SetDefaultStyleToCursorStyle();
649 /// Extend the selection. Selections are in caret positions.
650 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
652 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
654 // If not currently selecting, start selecting
655 if (m_selectionRange
.GetStart() == -2)
657 m_selectionAnchor
= oldPos
;
660 m_selectionRange
.SetRange(newPos
+1, oldPos
);
662 m_selectionRange
.SetRange(oldPos
+1, newPos
);
666 // Always ensure that the selection range start is greater than
668 if (newPos
> m_selectionAnchor
)
669 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
671 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
674 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
676 wxLogDebug(wxT("Strange selection range"));
685 /// Scroll into view, returning true if we scrolled.
686 /// This takes a _caret_ position.
687 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
689 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
695 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
698 GetViewStart(& startX
, & startY
);
700 startY
= startY
* ppuY
;
703 GetVirtualSize(& sx
, & sy
);
708 wxRect rect
= line
->GetRect();
710 bool scrolled
= false;
712 wxSize clientSize
= GetClientSize();
715 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_NEXT
|| keyCode
== WXK_PAGEDOWN
)
717 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
719 // Make it scroll so this item is at the bottom
721 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
722 y
= (int) (0.5 + y
/ppuY
);
726 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
730 else if (rect
.y
< startY
)
732 // Make it scroll so this item is at the top
735 y
= (int) (0.5 + y
/ppuY
);
739 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
745 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PRIOR
|| keyCode
== WXK_PAGEUP
)
749 // Make it scroll so this item is at the top
752 y
= (int) (0.5 + y
/ppuY
);
756 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
760 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
762 // Make it scroll so this item is at the bottom
764 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
765 y
= (int) (0.5 + y
/ppuY
);
769 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
779 /// Is the given position visible on the screen?
780 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
782 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
788 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
791 GetViewStart(& startX
, & startY
);
793 startY
= startY
* ppuY
;
796 GetVirtualSize(& sx
, & sy
);
801 wxRect rect
= line
->GetRect();
803 wxSize clientSize
= GetClientSize();
805 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
808 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
810 m_caretPosition
= position
;
811 m_caretAtLineStart
= showAtLineStart
;
814 /// Move caret one visual step forward: this may mean setting a flag
815 /// and keeping the same position if we're going from the end of one line
816 /// to the start of the next, which may be the exact same caret position.
817 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
819 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
821 // Only do the check if we're not at the end of the paragraph (where things work OK
823 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
825 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
829 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
831 // We're at the end of a line. See whether we need to
832 // stay at the same actual caret position but change visual
834 if (oldPosition
== lineRange
.GetEnd())
836 if (m_caretAtLineStart
)
838 // We're already at the start of the line, so actually move on now.
839 m_caretPosition
= oldPosition
+ 1;
840 m_caretAtLineStart
= false;
844 // We're showing at the end of the line, so keep to
845 // the same position but indicate that we're to show
846 // at the start of the next line.
847 m_caretPosition
= oldPosition
;
848 m_caretAtLineStart
= true;
850 SetDefaultStyleToCursorStyle();
856 SetDefaultStyleToCursorStyle();
859 /// Move caret one visual step backward: this may mean setting a flag
860 /// and keeping the same position if we're going from the end of one line
861 /// to the start of the next, which may be the exact same caret position.
862 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
864 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
866 // Only do the check if we're not at the start of the paragraph (where things work OK
868 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
870 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
874 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
876 // We're at the start of a line. See whether we need to
877 // stay at the same actual caret position but change visual
879 if (oldPosition
== lineRange
.GetStart())
881 m_caretPosition
= oldPosition
-1;
882 m_caretAtLineStart
= true;
885 else if (oldPosition
== lineRange
.GetEnd())
887 if (m_caretAtLineStart
)
889 // We're at the start of the line, so keep the same caret position
890 // but clear the start-of-line flag.
891 m_caretPosition
= oldPosition
;
892 m_caretAtLineStart
= false;
896 // We're showing at the end of the line, so go back
897 // to the previous character position.
898 m_caretPosition
= oldPosition
- 1;
900 SetDefaultStyleToCursorStyle();
906 SetDefaultStyleToCursorStyle();
910 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
912 long endPos
= GetBuffer().GetRange().GetEnd();
914 if (m_caretPosition
+ noPositions
< endPos
)
916 long oldPos
= m_caretPosition
;
917 long newPos
= m_caretPosition
+ noPositions
;
919 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
923 // Determine by looking at oldPos and m_caretPosition whether
924 // we moved from the end of a line to the start of the next line, in which case
925 // we want to adjust the caret position such that it is positioned at the
926 // start of the next line, rather than jumping past the first character of the
928 if (noPositions
== 1 && !extendSel
)
929 MoveCaretForward(oldPos
);
931 SetCaretPosition(newPos
);
934 SetDefaultStyleToCursorStyle();
945 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
949 if (m_caretPosition
> startPos
- noPositions
+ 1)
951 long oldPos
= m_caretPosition
;
952 long newPos
= m_caretPosition
- noPositions
;
953 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
957 if (noPositions
== 1 && !extendSel
)
958 MoveCaretBack(oldPos
);
960 SetCaretPosition(newPos
);
963 SetDefaultStyleToCursorStyle();
974 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
976 return MoveDown(- noLines
, flags
);
980 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
985 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
986 wxPoint pt
= GetCaret()->GetPosition();
987 long newLine
= lineNumber
+ noLines
;
989 if (lineNumber
!= -1)
993 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
995 if (newLine
> lastLine
)
1005 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1008 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1014 wxClientDC
dc(this);
1016 dc
.SetFont(GetFont());
1018 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1020 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1022 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1023 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1024 // so we view the caret at the start of the line.
1025 bool caretLineStart
= false;
1026 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1028 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1029 wxRichTextRange lineRange
;
1031 lineRange
= thisLine
->GetAbsoluteRange();
1033 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1036 caretLineStart
= true;
1040 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1041 if (para
&& para
->GetRange().GetStart() == newPos
)
1046 long newSelEnd
= newPos
;
1048 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1052 SetCaretPosition(newPos
, caretLineStart
);
1054 SetDefaultStyleToCursorStyle();
1064 /// Move to the end of the paragraph
1065 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1067 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1070 long newPos
= para
->GetRange().GetEnd() - 1;
1071 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1075 SetCaretPosition(newPos
);
1077 SetDefaultStyleToCursorStyle();
1087 /// Move to the start of the paragraph
1088 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1090 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1093 long newPos
= para
->GetRange().GetStart() - 1;
1094 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1098 SetCaretPosition(newPos
);
1100 SetDefaultStyleToCursorStyle();
1110 /// Move to the end of the line
1111 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1113 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1117 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1118 long newPos
= lineRange
.GetEnd();
1119 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1123 SetCaretPosition(newPos
);
1125 SetDefaultStyleToCursorStyle();
1135 /// Move to the start of the line
1136 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1138 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1141 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1142 long newPos
= lineRange
.GetStart()-1;
1144 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1148 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1150 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1152 SetDefaultStyleToCursorStyle();
1162 /// Move to the start of the buffer
1163 bool wxRichTextCtrl::MoveHome(int flags
)
1165 if (m_caretPosition
!= -1)
1167 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1171 SetCaretPosition(-1);
1173 SetDefaultStyleToCursorStyle();
1183 /// Move to the end of the buffer
1184 bool wxRichTextCtrl::MoveEnd(int flags
)
1186 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1188 if (m_caretPosition
!= endPos
)
1190 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1194 SetCaretPosition(endPos
);
1196 SetDefaultStyleToCursorStyle();
1206 /// Move noPages pages up
1207 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1209 return PageDown(- noPages
, flags
);
1212 /// Move noPages pages down
1213 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1215 // Calculate which line occurs noPages * screen height further down.
1216 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1219 wxSize clientSize
= GetClientSize();
1220 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1222 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1225 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1226 long pos
= lineRange
.GetStart()-1;
1227 if (pos
!= m_caretPosition
)
1229 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1231 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1235 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1237 SetDefaultStyleToCursorStyle();
1249 // Finds the caret position for the next word
1250 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1252 long endPos
= GetBuffer().GetRange().GetEnd();
1256 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1258 // First skip current text to space
1259 while (i
< endPos
&& i
> -1)
1261 // i is in character, not caret positions
1262 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1263 if (text
!= wxT(" ") && !text
.empty())
1270 while (i
< endPos
&& i
> -1)
1272 // i is in character, not caret positions
1273 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1274 if (text
.empty()) // End of paragraph, or maybe an image
1275 return wxMax(-1, i
- 1);
1276 else if (text
== wxT(" ") || text
.empty())
1280 // Convert to caret position
1281 return wxMax(-1, i
- 1);
1290 long i
= m_caretPosition
;
1292 // First skip white space
1293 while (i
< endPos
&& i
> -1)
1295 // i is in character, not caret positions
1296 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1297 if (text
.empty()) // End of paragraph, or maybe an image
1299 else if (text
== wxT(" ") || text
.empty())
1304 // Next skip current text to space
1305 while (i
< endPos
&& i
> -1)
1307 // i is in character, not caret positions
1308 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1309 if (text
!= wxT(" ") /* && !text.empty() */)
1322 /// Move n words left
1323 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1325 long pos
= FindNextWordPosition(-1);
1326 if (pos
!= m_caretPosition
)
1328 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1330 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1334 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1336 SetDefaultStyleToCursorStyle();
1346 /// Move n words right
1347 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1349 long pos
= FindNextWordPosition(1);
1350 if (pos
!= m_caretPosition
)
1352 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1354 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1358 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1360 SetDefaultStyleToCursorStyle();
1371 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1373 // Only do sizing optimization for large buffers
1374 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1376 m_fullLayoutRequired
= true;
1377 m_fullLayoutTime
= wxGetLocalTimeMillis();
1378 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1379 LayoutContent(true /* onlyVisibleRect */);
1382 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1390 /// Idle-time processing
1391 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1393 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1395 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1397 m_fullLayoutRequired
= false;
1398 m_fullLayoutTime
= 0;
1399 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1400 ShowPosition(m_fullLayoutSavedPosition
);
1407 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1413 /// Set up scrollbars, e.g. after a resize
1414 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1419 if (GetBuffer().IsEmpty())
1421 SetScrollbars(0, 0, 0, 0, 0, 0);
1425 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1426 // of pixels. See e.g. wxVScrolledWindow for ideas.
1427 int pixelsPerUnit
= 5; // 10;
1428 wxSize clientSize
= GetClientSize();
1430 int maxHeight
= GetBuffer().GetCachedSize().y
;
1432 int unitsY
= maxHeight
/pixelsPerUnit
;
1434 int startX
= 0, startY
= 0;
1436 GetViewStart(& startX
, & startY
);
1438 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1439 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1441 // Move to previous scroll position if
1443 SetScrollbars(0, pixelsPerUnit
,
1445 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1448 /// Paint the background
1449 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1451 wxColour backgroundColour
= GetBackgroundColour();
1452 if (!backgroundColour
.Ok())
1453 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1455 // Clear the background
1456 dc
.SetBrush(wxBrush(backgroundColour
));
1457 dc
.SetPen(*wxTRANSPARENT_PEN
);
1458 wxRect
windowRect(GetClientSize());
1459 windowRect
.x
-= 2; windowRect
.y
-= 2;
1460 windowRect
.width
+= 4; windowRect
.height
+= 4;
1462 // We need to shift the rectangle to take into account
1463 // scrolling. Converting device to logical coordinates.
1464 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1465 dc
.DrawRectangle(windowRect
);
1468 /// Recreate buffer bitmap if necessary
1469 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1472 if (sz
== wxDefaultSize
)
1473 sz
= GetClientSize();
1475 if (sz
.x
< 1 || sz
.y
< 1)
1478 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1479 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1480 return m_bufferBitmap
.Ok();
1483 // ----------------------------------------------------------------------------
1484 // file IO functions
1485 // ----------------------------------------------------------------------------
1487 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1489 bool success
= GetBuffer().LoadFile(filename
, type
);
1491 m_filename
= filename
;
1494 SetInsertionPoint(0);
1497 SetupScrollbars(true);
1505 wxLogError(_("File couldn't be loaded."));
1511 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1513 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1514 if ( filenameToUse
.empty() )
1516 // what kind of message to give? is it an error or a program bug?
1517 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1522 if (GetBuffer().SaveFile(filenameToUse
, type
))
1524 m_filename
= filenameToUse
;
1532 wxLogError(_("The text couldn't be saved."));
1537 // ----------------------------------------------------------------------------
1538 // wxRichTextCtrl specific functionality
1539 // ----------------------------------------------------------------------------
1541 /// Add a new paragraph of text to the end of the buffer
1542 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1544 return GetBuffer().AddParagraph(text
);
1548 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1550 return GetBuffer().AddImage(image
);
1553 // ----------------------------------------------------------------------------
1554 // selection and ranges
1555 // ----------------------------------------------------------------------------
1557 void wxRichTextCtrl::SelectAll()
1559 SetSelection(0, GetLastPosition());
1560 m_selectionAnchor
= -1;
1564 void wxRichTextCtrl::SelectNone()
1566 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1567 SetSelection(-2, -2);
1568 m_selectionAnchor
= -2;
1571 wxString
wxRichTextCtrl::GetStringSelection() const
1574 GetSelection(&from
, &to
);
1576 return GetRange(from
, to
);
1579 // do the window-specific processing after processing the update event
1580 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1582 if ( event
.GetSetEnabled() )
1583 Enable(event
.GetEnabled());
1585 if ( event
.GetSetText() )
1587 if ( event
.GetText() != GetValue() )
1588 SetValue(event
.GetText());
1592 // ----------------------------------------------------------------------------
1594 // ----------------------------------------------------------------------------
1596 wxTextCtrlHitTestResult
1597 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1599 // implement in terms of the other overload as the native ports typically
1600 // can get the position and not (x, y) pair directly (although wxUniv
1601 // directly gets x and y -- and so overrides this method as well)
1603 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1605 if ( rc
!= wxTE_HT_UNKNOWN
)
1607 PositionToXY(pos
, x
, y
);
1613 wxTextCtrlHitTestResult
1614 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1617 wxClientDC
dc((wxRichTextCtrl
*) this);
1618 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1620 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1624 case wxRICHTEXT_HITTEST_BEFORE
:
1625 return wxTE_HT_BEFORE
;
1627 case wxRICHTEXT_HITTEST_AFTER
:
1628 return wxTE_HT_BEYOND
;
1630 case wxRICHTEXT_HITTEST_ON
:
1631 return wxTE_HT_ON_TEXT
;
1634 return wxTE_HT_UNKNOWN
;
1637 // ----------------------------------------------------------------------------
1638 // set/get the controls text
1639 // ----------------------------------------------------------------------------
1641 wxString
wxRichTextCtrl::GetValue() const
1643 return GetBuffer().GetText();
1646 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1648 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1651 void wxRichTextCtrl::SetValue(const wxString
& value
)
1655 // if the text is long enough, it's faster to just set it instead of first
1656 // comparing it with the old one (chances are that it will be different
1657 // anyhow, this comparison is there to avoid flicker for small single-line
1658 // edit controls mostly)
1659 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1661 DoWriteText(value
, false /* not selection only */);
1663 // for compatibility, don't move the cursor when doing SetValue()
1664 SetInsertionPoint(0);
1668 // still send an event for consistency
1672 // we should reset the modified flag even if the value didn't really change
1674 // mark the control as being not dirty - we changed its text, not the
1679 void wxRichTextCtrl::WriteText(const wxString
& value
)
1684 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1686 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1689 void wxRichTextCtrl::AppendText(const wxString
& text
)
1691 SetInsertionPointEnd();
1696 /// Write an image at the current insertion point
1697 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1699 wxRichTextImageBlock imageBlock
;
1701 wxImage image2
= image
;
1702 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1703 return WriteImage(imageBlock
);
1708 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1710 wxRichTextImageBlock imageBlock
;
1713 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1714 return WriteImage(imageBlock
);
1719 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1721 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1724 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1728 wxRichTextImageBlock imageBlock
;
1730 wxImage image
= bitmap
.ConvertToImage();
1731 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1732 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();