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"
25 #include "wx/settings.h"
28 #include "wx/textfile.h"
30 #include "wx/filename.h"
31 #include "wx/dcbuffer.h"
32 #include "wx/arrimpl.cpp"
34 // DLL options compatibility check:
36 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
46 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
47 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
49 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
52 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
54 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
55 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
57 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
59 EVT_PAINT(wxRichTextCtrl::OnPaint
)
60 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
61 EVT_IDLE(wxRichTextCtrl::OnIdle
)
62 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
63 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
64 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
65 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
66 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
67 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
68 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
69 EVT_CHAR(wxRichTextCtrl::OnChar
)
70 EVT_SIZE(wxRichTextCtrl::OnSize
)
71 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
72 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
73 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
75 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
76 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
78 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
79 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
81 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
82 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
84 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
85 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
87 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
88 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
90 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
91 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
93 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
94 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
101 wxRichTextCtrl::wxRichTextCtrl()
102 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
103 : wxScrollHelper(this)
109 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
110 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
111 : wxScrollHelper(this)
115 Create(parent
, id
, value
, pos
, size
, style
);
119 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
121 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
122 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
126 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
133 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
136 GetBuffer().SetRichTextCtrl(this);
138 wxTextAttrEx attributes
;
139 attributes
.SetFont(GetFont());
140 attributes
.SetTextColour(*wxBLACK
);
141 attributes
.SetBackgroundColour(*wxWHITE
);
142 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
143 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
145 SetDefaultStyle(attributes
);
146 SetBasicStyle(attributes
);
148 SetBackgroundColour(*wxWHITE
);
149 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
151 // Tell the sizers to use the given or best size
152 SetBestFittingSize(size
);
155 RecreateBuffer(size
);
157 SetCursor(wxCursor(wxCURSOR_IBEAM
));
159 if (!value
.IsEmpty())
165 wxRichTextCtrl::~wxRichTextCtrl()
167 delete m_contextMenu
;
170 /// Member initialisation
171 void wxRichTextCtrl::Init()
174 m_contextMenu
= NULL
;
176 m_caretPosition
= -1;
177 m_selectionRange
.SetRange(-2, -2);
178 m_selectionAnchor
= -2;
180 m_caretAtLineStart
= false;
182 m_fullLayoutRequired
= false;
183 m_fullLayoutTime
= 0;
184 m_fullLayoutSavedPosition
= 0;
185 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
188 /// Call Freeze to prevent refresh
189 void wxRichTextCtrl::Freeze()
194 /// Call Thaw to refresh
195 void wxRichTextCtrl::Thaw()
199 if (m_freezeCount
== 0)
207 void wxRichTextCtrl::Clear()
210 m_buffer
.SetDirty(true);
211 m_caretPosition
= -1;
212 m_caretAtLineStart
= false;
213 m_selectionRange
.SetRange(-2, -2);
215 SetScrollbars(0, 0, 0, 0, 0, 0);
217 if (m_freezeCount
== 0)
226 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
232 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
233 //wxLogDebug(wxT("OnPaint"));
237 if (m_freezeCount
> 0)
240 dc
.SetFont(GetFont());
242 // Paint the background
245 wxRegion dirtyRegion
= GetUpdateRegion();
247 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
248 wxRect
availableSpace(GetClientSize());
249 if (GetBuffer().GetDirty())
251 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
252 GetBuffer().SetDirty(false);
256 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
265 // Empty implementation, to prevent flicker
266 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
270 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
272 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
281 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
290 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
296 dc
.SetFont(GetFont());
299 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
301 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
303 m_dragStart
= event
.GetLogicalPosition(dc
);
309 bool caretAtLineStart
= false;
311 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
313 // If we're at the start of a line (but not first in para)
314 // then we should keep the caret showing at the start of the line
315 // by showing the m_caretAtLineStart flag.
316 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
317 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
319 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
320 caretAtLineStart
= true;
324 MoveCaret(position
, caretAtLineStart
);
325 SetDefaultStyleToCursorStyle();
328 wxWindow
* p
= GetParent();
329 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
332 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
335 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
336 frame
->SetStatusText(msg
, 1);
345 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
350 if (GetCapture() == this)
356 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
358 if (!event
.Dragging())
366 dc
.SetFont(GetFont());
369 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
370 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
372 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
374 // TODO: test closeness
376 bool caretAtLineStart
= false;
378 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
380 // If we're at the start of a line (but not first in para)
381 // then we should keep the caret showing at the start of the line
382 // by showing the m_caretAtLineStart flag.
383 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
384 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
386 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
387 caretAtLineStart
= true;
391 if (m_caretPosition
!= position
)
393 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
395 MoveCaret(position
, caretAtLineStart
);
396 SetDefaultStyleToCursorStyle();
405 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
411 /// Left-double-click
412 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
418 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
424 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
427 if (event
.ControlDown())
428 flags
|= wxRICHTEXT_CTRL_DOWN
;
429 if (event
.ShiftDown())
430 flags
|= wxRICHTEXT_SHIFT_DOWN
;
432 flags
|= wxRICHTEXT_ALT_DOWN
;
434 if (event
.GetKeyCode() == WXK_LEFT
||
435 event
.GetKeyCode() == WXK_RIGHT
||
436 event
.GetKeyCode() == WXK_UP
||
437 event
.GetKeyCode() == WXK_DOWN
||
438 event
.GetKeyCode() == WXK_HOME
||
439 event
.GetKeyCode() == WXK_PAGEUP
||
440 event
.GetKeyCode() == WXK_PAGEDOWN
||
441 event
.GetKeyCode() == WXK_END
)
443 KeyboardNavigate(event
.GetKeyCode(), flags
);
447 // all the other keys modify the controls contents which shouldn't be
448 // possible if we're read-only
455 if (event
.GetKeyCode() == WXK_RETURN
)
457 BeginBatchUndo(_("Insert Text"));
459 long newPos
= m_caretPosition
;
461 DeleteSelectedContent(& newPos
);
463 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
465 wxRichTextEvent
cmdEvent(
466 wxEVT_COMMAND_RICHTEXT_RETURN
,
468 cmdEvent
.SetEventObject(this);
469 cmdEvent
.SetFlags(flags
);
470 GetEventHandler()->ProcessEvent(cmdEvent
);
473 SetDefaultStyleToCursorStyle();
475 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
477 else if (event
.GetKeyCode() == WXK_BACK
)
479 BeginBatchUndo(_("Delete Text"));
481 // Submit range in character positions, which are greater than caret positions,
482 // so subtract 1 for deleted character and add 1 for conversion to character position.
483 if (m_caretPosition
> -1 && !HasSelection())
485 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
486 m_caretPosition
, // Current caret position
487 m_caretPosition
-1, // New caret position
491 DeleteSelectedContent();
495 // Shouldn't this be in Do()?
496 if (GetLastPosition() == -1)
500 m_caretPosition
= -1;
502 SetDefaultStyleToCursorStyle();
505 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
507 else if (event
.GetKeyCode() == WXK_DELETE
)
509 BeginBatchUndo(_("Delete Text"));
511 // Submit range in character positions, which are greater than caret positions,
512 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
514 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
515 m_caretPosition
, // Current caret position
516 m_caretPosition
+1, // New caret position
520 DeleteSelectedContent();
524 // Shouldn't this be in Do()?
525 if (GetLastPosition() == -1)
529 m_caretPosition
= -1;
531 SetDefaultStyleToCursorStyle();
536 BeginBatchUndo(_("Insert Text"));
538 long newPos
= m_caretPosition
;
539 DeleteSelectedContent(& newPos
);
541 wxString str
= (wxChar
) event
.GetKeyCode();
542 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
546 SetDefaultStyleToCursorStyle();
547 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
551 /// Delete content if there is a selection, e.g. when pressing a key.
552 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
556 long pos
= m_selectionRange
.GetStart();
557 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
558 m_caretPosition
, // Current caret position
559 pos
, // New caret position
561 m_selectionRange
.SetRange(-2, -2);
571 /// Keyboard navigation
575 Left: left one character
576 Right: right one character
579 Ctrl-Left: left one word
580 Ctrl-Right: right one word
581 Ctrl-Up: previous paragraph start
582 Ctrl-Down: next start of paragraph
585 Ctrl-Home: start of document
586 Ctrl-End: end of document
588 Page-Down: Down a screen
592 Ctrl-Alt-PgUp: Start of window
593 Ctrl-Alt-PgDn: End of window
594 F8: Start selection mode
595 Esc: End selection mode
597 Adding Shift does the above but starts/extends selection.
602 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
604 bool success
= false;
606 if (keyCode
== WXK_RIGHT
)
608 if (flags
& wxRICHTEXT_CTRL_DOWN
)
609 success
= WordRight(1, flags
);
611 success
= MoveRight(1, flags
);
613 else if (keyCode
== WXK_LEFT
)
615 if (flags
& wxRICHTEXT_CTRL_DOWN
)
616 success
= WordLeft(1, flags
);
618 success
= MoveLeft(1, flags
);
620 else if (keyCode
== WXK_UP
)
622 if (flags
& wxRICHTEXT_CTRL_DOWN
)
623 success
= MoveToParagraphStart(flags
);
625 success
= MoveUp(1, flags
);
627 else if (keyCode
== WXK_DOWN
)
629 if (flags
& wxRICHTEXT_CTRL_DOWN
)
630 success
= MoveToParagraphEnd(flags
);
632 success
= MoveDown(1, flags
);
634 else if (keyCode
== WXK_PAGEUP
)
636 success
= PageUp(1, flags
);
638 else if (keyCode
== WXK_PAGEDOWN
)
640 success
= PageDown(1, flags
);
642 else if (keyCode
== WXK_HOME
)
644 if (flags
& wxRICHTEXT_CTRL_DOWN
)
645 success
= MoveHome(flags
);
647 success
= MoveToLineStart(flags
);
649 else if (keyCode
== WXK_END
)
651 if (flags
& wxRICHTEXT_CTRL_DOWN
)
652 success
= MoveEnd(flags
);
654 success
= MoveToLineEnd(flags
);
659 ScrollIntoView(m_caretPosition
, keyCode
);
660 SetDefaultStyleToCursorStyle();
666 /// Extend the selection. Selections are in caret positions.
667 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
669 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
671 // If not currently selecting, start selecting
672 if (m_selectionRange
.GetStart() == -2)
674 m_selectionAnchor
= oldPos
;
677 m_selectionRange
.SetRange(newPos
+1, oldPos
);
679 m_selectionRange
.SetRange(oldPos
+1, newPos
);
683 // Always ensure that the selection range start is greater than
685 if (newPos
> m_selectionAnchor
)
686 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
688 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
691 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
693 wxLogDebug(wxT("Strange selection range"));
702 /// Scroll into view, returning true if we scrolled.
703 /// This takes a _caret_ position.
704 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
706 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
712 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
714 int startXUnits
, startYUnits
;
715 GetViewStart(& startXUnits
, & startYUnits
);
716 int startY
= startYUnits
* ppuY
;
719 GetVirtualSize(& sx
, & sy
);
725 wxRect rect
= line
->GetRect();
727 bool scrolled
= false;
729 wxSize clientSize
= GetClientSize();
732 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
734 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
736 // Make it scroll so this item is at the bottom
738 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
739 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
741 // If we're still off the screen, scroll another line down
742 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
745 if (startYUnits
!= yUnits
)
747 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
751 else if (rect
.y
< startY
)
753 // Make it scroll so this item is at the top
756 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
758 if (startYUnits
!= yUnits
)
760 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
766 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
770 // Make it scroll so this item is at the top
773 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
775 if (startYUnits
!= yUnits
)
777 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
781 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
783 // Make it scroll so this item is at the bottom
785 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
786 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
788 // If we're still off the screen, scroll another line down
789 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
792 if (startYUnits
!= yUnits
)
794 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
804 /// Is the given position visible on the screen?
805 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
807 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
813 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
816 GetViewStart(& startX
, & startY
);
818 startY
= startY
* ppuY
;
821 GetVirtualSize(& sx
, & sy
);
826 wxRect rect
= line
->GetRect();
828 wxSize clientSize
= GetClientSize();
830 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
833 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
835 m_caretPosition
= position
;
836 m_caretAtLineStart
= showAtLineStart
;
839 /// Move caret one visual step forward: this may mean setting a flag
840 /// and keeping the same position if we're going from the end of one line
841 /// to the start of the next, which may be the exact same caret position.
842 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
844 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
846 // Only do the check if we're not at the end of the paragraph (where things work OK
848 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
850 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
854 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
856 // We're at the end of a line. See whether we need to
857 // stay at the same actual caret position but change visual
859 if (oldPosition
== lineRange
.GetEnd())
861 if (m_caretAtLineStart
)
863 // We're already at the start of the line, so actually move on now.
864 m_caretPosition
= oldPosition
+ 1;
865 m_caretAtLineStart
= false;
869 // We're showing at the end of the line, so keep to
870 // the same position but indicate that we're to show
871 // at the start of the next line.
872 m_caretPosition
= oldPosition
;
873 m_caretAtLineStart
= true;
875 SetDefaultStyleToCursorStyle();
881 SetDefaultStyleToCursorStyle();
884 /// Move caret one visual step backward: this may mean setting a flag
885 /// and keeping the same position if we're going from the end of one line
886 /// to the start of the next, which may be the exact same caret position.
887 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
889 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
891 // Only do the check if we're not at the start of the paragraph (where things work OK
893 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
895 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
899 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
901 // We're at the start of a line. See whether we need to
902 // stay at the same actual caret position but change visual
904 if (oldPosition
== lineRange
.GetStart())
906 m_caretPosition
= oldPosition
-1;
907 m_caretAtLineStart
= true;
910 else if (oldPosition
== lineRange
.GetEnd())
912 if (m_caretAtLineStart
)
914 // We're at the start of the line, so keep the same caret position
915 // but clear the start-of-line flag.
916 m_caretPosition
= oldPosition
;
917 m_caretAtLineStart
= false;
921 // We're showing at the end of the line, so go back
922 // to the previous character position.
923 m_caretPosition
= oldPosition
- 1;
925 SetDefaultStyleToCursorStyle();
931 SetDefaultStyleToCursorStyle();
935 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
937 long endPos
= GetBuffer().GetRange().GetEnd();
939 if (m_caretPosition
+ noPositions
< endPos
)
941 long oldPos
= m_caretPosition
;
942 long newPos
= m_caretPosition
+ noPositions
;
944 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
948 // Determine by looking at oldPos and m_caretPosition whether
949 // we moved from the end of a line to the start of the next line, in which case
950 // we want to adjust the caret position such that it is positioned at the
951 // start of the next line, rather than jumping past the first character of the
953 if (noPositions
== 1 && !extendSel
)
954 MoveCaretForward(oldPos
);
956 SetCaretPosition(newPos
);
959 SetDefaultStyleToCursorStyle();
970 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
974 if (m_caretPosition
> startPos
- noPositions
+ 1)
976 long oldPos
= m_caretPosition
;
977 long newPos
= m_caretPosition
- noPositions
;
978 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
982 if (noPositions
== 1 && !extendSel
)
983 MoveCaretBack(oldPos
);
985 SetCaretPosition(newPos
);
988 SetDefaultStyleToCursorStyle();
999 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1001 return MoveDown(- noLines
, flags
);
1005 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1010 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1011 wxPoint pt
= GetCaret()->GetPosition();
1012 long newLine
= lineNumber
+ noLines
;
1014 if (lineNumber
!= -1)
1018 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1020 if (newLine
> lastLine
)
1030 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1033 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1039 wxClientDC
dc(this);
1041 dc
.SetFont(GetFont());
1043 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1045 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1047 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1048 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1049 // so we view the caret at the start of the line.
1050 bool caretLineStart
= false;
1051 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1053 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1054 wxRichTextRange lineRange
;
1056 lineRange
= thisLine
->GetAbsoluteRange();
1058 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1061 caretLineStart
= true;
1065 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1066 if (para
&& para
->GetRange().GetStart() == newPos
)
1071 long newSelEnd
= newPos
;
1073 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1077 SetCaretPosition(newPos
, caretLineStart
);
1079 SetDefaultStyleToCursorStyle();
1089 /// Move to the end of the paragraph
1090 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1092 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1095 long newPos
= para
->GetRange().GetEnd() - 1;
1096 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1100 SetCaretPosition(newPos
);
1102 SetDefaultStyleToCursorStyle();
1112 /// Move to the start of the paragraph
1113 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1115 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1118 long newPos
= para
->GetRange().GetStart() - 1;
1119 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1123 SetCaretPosition(newPos
);
1125 SetDefaultStyleToCursorStyle();
1135 /// Move to the end of the line
1136 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1138 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1142 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1143 long newPos
= lineRange
.GetEnd();
1144 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1148 SetCaretPosition(newPos
);
1150 SetDefaultStyleToCursorStyle();
1160 /// Move to the start of the line
1161 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1163 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1166 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1167 long newPos
= lineRange
.GetStart()-1;
1169 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1173 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1175 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1177 SetDefaultStyleToCursorStyle();
1187 /// Move to the start of the buffer
1188 bool wxRichTextCtrl::MoveHome(int flags
)
1190 if (m_caretPosition
!= -1)
1192 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1196 SetCaretPosition(-1);
1198 SetDefaultStyleToCursorStyle();
1208 /// Move to the end of the buffer
1209 bool wxRichTextCtrl::MoveEnd(int flags
)
1211 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1213 if (m_caretPosition
!= endPos
)
1215 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1219 SetCaretPosition(endPos
);
1221 SetDefaultStyleToCursorStyle();
1231 /// Move noPages pages up
1232 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1234 return PageDown(- noPages
, flags
);
1237 /// Move noPages pages down
1238 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1240 // Calculate which line occurs noPages * screen height further down.
1241 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1244 wxSize clientSize
= GetClientSize();
1245 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1247 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1250 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1251 long pos
= lineRange
.GetStart()-1;
1252 if (pos
!= m_caretPosition
)
1254 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1256 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1260 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1262 SetDefaultStyleToCursorStyle();
1274 // Finds the caret position for the next word
1275 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1277 long endPos
= GetBuffer().GetRange().GetEnd();
1281 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1283 // First skip current text to space
1284 while (i
< endPos
&& i
> -1)
1286 // i is in character, not caret positions
1287 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1288 if (text
!= wxT(" ") && !text
.empty())
1295 while (i
< endPos
&& i
> -1)
1297 // i is in character, not caret positions
1298 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1299 if (text
.empty()) // End of paragraph, or maybe an image
1300 return wxMax(-1, i
- 1);
1301 else if (text
== wxT(" ") || text
.empty())
1305 // Convert to caret position
1306 return wxMax(-1, i
- 1);
1315 long i
= m_caretPosition
;
1317 // First skip white space
1318 while (i
< endPos
&& i
> -1)
1320 // i is in character, not caret positions
1321 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1322 if (text
.empty()) // End of paragraph, or maybe an image
1324 else if (text
== wxT(" ") || text
.empty())
1329 // Next skip current text to space
1330 while (i
< endPos
&& i
> -1)
1332 // i is in character, not caret positions
1333 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1334 if (text
!= wxT(" ") /* && !text.empty() */)
1347 /// Move n words left
1348 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1350 long pos
= FindNextWordPosition(-1);
1351 if (pos
!= m_caretPosition
)
1353 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1355 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1359 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1361 SetDefaultStyleToCursorStyle();
1371 /// Move n words right
1372 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1374 long pos
= FindNextWordPosition(1);
1375 if (pos
!= m_caretPosition
)
1377 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1379 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1383 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1385 SetDefaultStyleToCursorStyle();
1396 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1398 // Only do sizing optimization for large buffers
1399 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1401 m_fullLayoutRequired
= true;
1402 m_fullLayoutTime
= wxGetLocalTimeMillis();
1403 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1404 LayoutContent(true /* onlyVisibleRect */);
1407 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1415 /// Idle-time processing
1416 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1418 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1420 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1422 m_fullLayoutRequired
= false;
1423 m_fullLayoutTime
= 0;
1424 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1425 ShowPosition(m_fullLayoutSavedPosition
);
1432 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1438 /// Set up scrollbars, e.g. after a resize
1439 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1444 if (GetBuffer().IsEmpty())
1446 SetScrollbars(0, 0, 0, 0, 0, 0);
1450 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1451 // of pixels. See e.g. wxVScrolledWindow for ideas.
1452 int pixelsPerUnit
= 5;
1453 wxSize clientSize
= GetClientSize();
1455 int maxHeight
= GetBuffer().GetCachedSize().y
;
1457 // Round up so we have at least maxHeight pixels
1458 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1460 int startX
= 0, startY
= 0;
1462 GetViewStart(& startX
, & startY
);
1464 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1465 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1467 // Move to previous scroll position if
1469 SetScrollbars(0, pixelsPerUnit
,
1471 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1474 /// Paint the background
1475 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1477 wxColour backgroundColour
= GetBackgroundColour();
1478 if (!backgroundColour
.Ok())
1479 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1481 // Clear the background
1482 dc
.SetBrush(wxBrush(backgroundColour
));
1483 dc
.SetPen(*wxTRANSPARENT_PEN
);
1484 wxRect
windowRect(GetClientSize());
1485 windowRect
.x
-= 2; windowRect
.y
-= 2;
1486 windowRect
.width
+= 4; windowRect
.height
+= 4;
1488 // We need to shift the rectangle to take into account
1489 // scrolling. Converting device to logical coordinates.
1490 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1491 dc
.DrawRectangle(windowRect
);
1494 /// Recreate buffer bitmap if necessary
1495 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1498 if (sz
== wxDefaultSize
)
1499 sz
= GetClientSize();
1501 if (sz
.x
< 1 || sz
.y
< 1)
1504 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1505 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1506 return m_bufferBitmap
.Ok();
1509 // ----------------------------------------------------------------------------
1510 // file IO functions
1511 // ----------------------------------------------------------------------------
1513 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1515 bool success
= GetBuffer().LoadFile(filename
, type
);
1517 m_filename
= filename
;
1520 SetInsertionPoint(0);
1523 SetupScrollbars(true);
1531 wxLogError(_("File couldn't be loaded."));
1537 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1539 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1540 if ( filenameToUse
.empty() )
1542 // what kind of message to give? is it an error or a program bug?
1543 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1548 if (GetBuffer().SaveFile(filenameToUse
, type
))
1550 m_filename
= filenameToUse
;
1558 wxLogError(_("The text couldn't be saved."));
1563 // ----------------------------------------------------------------------------
1564 // wxRichTextCtrl specific functionality
1565 // ----------------------------------------------------------------------------
1567 /// Add a new paragraph of text to the end of the buffer
1568 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1570 return GetBuffer().AddParagraph(text
);
1574 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1576 return GetBuffer().AddImage(image
);
1579 // ----------------------------------------------------------------------------
1580 // selection and ranges
1581 // ----------------------------------------------------------------------------
1583 void wxRichTextCtrl::SelectAll()
1585 SetSelection(0, GetLastPosition());
1586 m_selectionAnchor
= -1;
1590 void wxRichTextCtrl::SelectNone()
1592 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1593 SetSelection(-2, -2);
1594 m_selectionAnchor
= -2;
1597 wxString
wxRichTextCtrl::GetStringSelection() const
1600 GetSelection(&from
, &to
);
1602 return GetRange(from
, to
);
1605 // do the window-specific processing after processing the update event
1606 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1607 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1610 wxWindowBase::DoUpdateWindowUI(event
);
1613 if ( event
.GetSetText() )
1615 if ( event
.GetText() != GetValue() )
1616 SetValue(event
.GetText());
1619 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1621 // ----------------------------------------------------------------------------
1623 // ----------------------------------------------------------------------------
1625 wxTextCtrlHitTestResult
1626 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1628 // implement in terms of the other overload as the native ports typically
1629 // can get the position and not (x, y) pair directly (although wxUniv
1630 // directly gets x and y -- and so overrides this method as well)
1632 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1634 if ( rc
!= wxTE_HT_UNKNOWN
)
1636 PositionToXY(pos
, x
, y
);
1642 wxTextCtrlHitTestResult
1643 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1646 wxClientDC
dc((wxRichTextCtrl
*) this);
1647 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1649 // Buffer uses logical position (relative to start of buffer)
1651 wxPoint pt2
= GetLogicalPoint(pt
);
1653 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1657 case wxRICHTEXT_HITTEST_BEFORE
:
1658 return wxTE_HT_BEFORE
;
1660 case wxRICHTEXT_HITTEST_AFTER
:
1661 return wxTE_HT_BEYOND
;
1663 case wxRICHTEXT_HITTEST_ON
:
1664 return wxTE_HT_ON_TEXT
;
1667 return wxTE_HT_UNKNOWN
;
1670 // ----------------------------------------------------------------------------
1671 // set/get the controls text
1672 // ----------------------------------------------------------------------------
1674 wxString
wxRichTextCtrl::GetValue() const
1676 return GetBuffer().GetText();
1679 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1681 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1684 void wxRichTextCtrl::SetValue(const wxString
& value
)
1688 // if the text is long enough, it's faster to just set it instead of first
1689 // comparing it with the old one (chances are that it will be different
1690 // anyhow, this comparison is there to avoid flicker for small single-line
1691 // edit controls mostly)
1692 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1694 DoWriteText(value
, false /* not selection only */);
1696 // for compatibility, don't move the cursor when doing SetValue()
1697 SetInsertionPoint(0);
1701 // still send an event for consistency
1705 // we should reset the modified flag even if the value didn't really change
1707 // mark the control as being not dirty - we changed its text, not the
1712 void wxRichTextCtrl::WriteText(const wxString
& value
)
1717 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1719 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1722 void wxRichTextCtrl::AppendText(const wxString
& text
)
1724 SetInsertionPointEnd();
1729 /// Write an image at the current insertion point
1730 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1732 wxRichTextImageBlock imageBlock
;
1734 wxImage image2
= image
;
1735 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1736 return WriteImage(imageBlock
);
1741 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1743 wxRichTextImageBlock imageBlock
;
1746 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1747 return WriteImage(imageBlock
);
1752 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1754 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1757 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1761 wxRichTextImageBlock imageBlock
;
1763 wxImage image
= bitmap
.ConvertToImage();
1764 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1765 return WriteImage(imageBlock
);
1771 /// Insert a newline (actually paragraph) at the current insertion point.
1772 bool wxRichTextCtrl::Newline()
1774 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1778 // ----------------------------------------------------------------------------
1779 // Clipboard operations
1780 // ----------------------------------------------------------------------------
1782 void wxRichTextCtrl::Copy()
1786 wxRichTextRange range
= GetSelectionRange();
1787 GetBuffer().CopyToClipboard(range
);
1791 void wxRichTextCtrl::Cut()
1795 wxRichTextRange range
= GetSelectionRange();
1796 GetBuffer().CopyToClipboard(range
);
1798 DeleteSelectedContent();
1804 void wxRichTextCtrl::Paste()
1808 BeginBatchUndo(_("Paste"));
1810 long newPos
= m_caretPosition
;
1811 DeleteSelectedContent(& newPos
);
1813 GetBuffer().PasteFromClipboard(newPos
);
1819 void wxRichTextCtrl::DeleteSelection()
1821 if (CanDeleteSelection())
1823 DeleteSelectedContent();
1827 bool wxRichTextCtrl::HasSelection() const
1829 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1832 bool wxRichTextCtrl::CanCopy() const
1834 // Can copy if there's a selection
1835 return HasSelection();
1838 bool wxRichTextCtrl::CanCut() const
1840 return HasSelection() && IsEditable();
1843 bool wxRichTextCtrl::CanPaste() const
1845 if ( !IsEditable() )
1848 return GetBuffer().CanPasteFromClipboard();
1851 bool wxRichTextCtrl::CanDeleteSelection() const
1853 return HasSelection() && IsEditable();
1857 // ----------------------------------------------------------------------------
1859 // ----------------------------------------------------------------------------
1861 void wxRichTextCtrl::SetEditable(bool editable
)
1863 m_editable
= editable
;
1866 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1870 m_caretPosition
= pos
- 1;
1873 void wxRichTextCtrl::SetInsertionPointEnd()
1875 long pos
= GetLastPosition();
1876 SetInsertionPoint(pos
);
1879 long wxRichTextCtrl::GetInsertionPoint() const
1881 return m_caretPosition
+1;
1884 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1886 return GetBuffer().GetRange().GetEnd();
1889 // If the return values from and to are the same, there is no
1891 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1893 *from
= m_selectionRange
.GetStart();
1894 *to
= m_selectionRange
.GetEnd();
1897 bool wxRichTextCtrl::IsEditable() const
1902 // ----------------------------------------------------------------------------
1904 // ----------------------------------------------------------------------------
1906 void wxRichTextCtrl::SetSelection(long from
, long to
)
1908 // if from and to are both -1, it means (in wxWidgets) that all text should
1910 if ( (from
== -1) && (to
== -1) )
1913 to
= GetLastPosition();
1916 DoSetSelection(from
, to
);
1919 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1921 m_selectionAnchor
= from
;
1922 m_selectionRange
.SetRange(from
, to
);
1927 // ----------------------------------------------------------------------------
1929 // ----------------------------------------------------------------------------
1931 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1933 BeginBatchUndo(_("Replace"));
1935 DeleteSelectedContent();
1937 DoWriteText(value
, true /* selection only */);
1942 void wxRichTextCtrl::Remove(long from
, long to
)
1946 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1947 m_caretPosition
, // Current caret position
1948 from
, // New caret position
1956 bool wxRichTextCtrl::IsModified() const
1958 return m_buffer
.IsModified();
1961 void wxRichTextCtrl::MarkDirty()
1963 m_buffer
.Modify(true);
1966 void wxRichTextCtrl::DiscardEdits()
1968 m_buffer
.Modify(false);
1969 m_buffer
.GetCommandProcessor()->ClearCommands();
1972 int wxRichTextCtrl::GetNumberOfLines() const
1974 return GetBuffer().GetParagraphCount();
1977 // ----------------------------------------------------------------------------
1978 // Positions <-> coords
1979 // ----------------------------------------------------------------------------
1981 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1983 return GetBuffer().XYToPosition(x
, y
);
1986 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1988 return GetBuffer().PositionToXY(pos
, x
, y
);
1991 // ----------------------------------------------------------------------------
1993 // ----------------------------------------------------------------------------
1995 void wxRichTextCtrl::ShowPosition(long pos
)
1997 if (!IsPositionVisible(pos
))
1998 ScrollIntoView(pos
-1, WXK_DOWN
);
2001 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2003 return GetBuffer().GetParagraphLength(lineNo
);
2006 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2008 return GetBuffer().GetParagraphText(lineNo
);
2011 // ----------------------------------------------------------------------------
2013 // ----------------------------------------------------------------------------
2015 void wxRichTextCtrl::Undo()
2019 GetCommandProcessor()->Undo();
2023 void wxRichTextCtrl::Redo()
2027 GetCommandProcessor()->Redo();
2031 bool wxRichTextCtrl::CanUndo() const
2033 return GetCommandProcessor()->CanUndo();
2036 bool wxRichTextCtrl::CanRedo() const
2038 return GetCommandProcessor()->CanRedo();
2041 // ----------------------------------------------------------------------------
2042 // implementation details
2043 // ----------------------------------------------------------------------------
2045 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2047 SetValue(event
.GetString());
2048 GetEventHandler()->ProcessEvent(event
);
2051 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2053 // By default, load the first file into the text window.
2054 if (event
.GetNumberOfFiles() > 0)
2056 LoadFile(event
.GetFiles()[0]);
2060 // ----------------------------------------------------------------------------
2061 // text control event processing
2062 // ----------------------------------------------------------------------------
2064 bool wxRichTextCtrl::SendUpdateEvent()
2066 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2067 InitCommandEvent(event
);
2069 return GetEventHandler()->ProcessEvent(event
);
2072 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2074 event
.SetEventObject((wxControlBase
*)this); // const_cast
2076 switch ( m_clientDataType
)
2078 case wxClientData_Void
:
2079 event
.SetClientData(GetClientData());
2082 case wxClientData_Object
:
2083 event
.SetClientObject(GetClientObject());
2086 case wxClientData_None
:
2093 wxSize
wxRichTextCtrl::DoGetBestSize() const
2095 return wxSize(10, 10);
2098 // ----------------------------------------------------------------------------
2099 // standard handlers for standard edit menu events
2100 // ----------------------------------------------------------------------------
2102 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2107 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2112 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2117 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2122 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2127 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2132 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2134 event
.Enable( CanCut() );
2137 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2139 event
.Enable( CanCopy() );
2142 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2144 event
.Enable( CanDeleteSelection() );
2147 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2149 event
.Enable( CanPaste() );
2152 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2154 event
.Enable( CanUndo() );
2155 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2158 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2160 event
.Enable( CanRedo() );
2161 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2164 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2169 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2171 event
.Enable(GetLastPosition() > 0);
2174 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2178 m_contextMenu
= new wxMenu
;
2179 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2180 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2181 m_contextMenu
->AppendSeparator();
2182 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2183 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2184 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2185 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2186 m_contextMenu
->AppendSeparator();
2187 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2189 PopupMenu(m_contextMenu
);
2193 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2195 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2198 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2200 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), wxTextAttrEx(style
));
2203 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2205 return GetBuffer().SetStyle(range
, style
);
2208 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2210 return GetBuffer().SetDefaultStyle(style
);
2213 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2215 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2218 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2220 return GetBuffer().GetDefaultStyle();
2223 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2225 return GetBuffer().GetDefaultStyle();
2228 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
) const
2231 if (GetBuffer().GetStyle(position
, attr
))
2240 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2242 return GetBuffer().GetStyle(position
, style
);
2245 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2247 return GetBuffer().GetStyle(position
, style
);
2250 /// Set font, and also the buffer attributes
2251 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2253 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2254 wxControl::SetFont(font
);
2256 wxScrolledWindow::SetFont(font
);
2259 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2261 GetBuffer().SetBasicStyle(attr
);
2262 GetBuffer().SetDefaultStyle(attr
);
2267 /// Transform logical to physical
2268 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2271 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2276 /// Transform physical to logical
2277 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2280 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2285 /// Position the caret
2286 void wxRichTextCtrl::PositionCaret()
2291 //wxLogDebug(wxT("PositionCaret"));
2294 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2296 wxPoint originalPt
= caretRect
.GetPosition();
2297 wxPoint pt
= GetPhysicalPoint(originalPt
);
2298 if (GetCaret()->GetPosition() != pt
)
2300 GetCaret()->Move(pt
);
2301 GetCaret()->SetSize(caretRect
.GetSize());
2306 /// Get the caret height and position for the given character position
2307 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2309 wxClientDC
dc(this);
2310 dc
.SetFont(GetFont());
2317 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2319 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2326 /// Gets the line for the visible caret position. If the caret is
2327 /// shown at the very end of the line, it means the next character is actually
2328 /// on the following line. So let's get the line we're expecting to find
2329 /// if this is the case.
2330 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2332 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2333 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2336 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2337 if (caretPosition
== lineRange
.GetStart()-1 &&
2338 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2340 if (!m_caretAtLineStart
)
2341 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2348 /// Move the caret to the given character position
2349 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2351 if (GetBuffer().GetDirty())
2354 if (pos
<= GetBuffer().GetRange().GetEnd())
2356 SetCaretPosition(pos
, showAtLineStart
);
2366 /// Layout the buffer: which we must do before certain operations, such as
2367 /// setting the caret position.
2368 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2370 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2372 wxRect
availableSpace(GetClientSize());
2373 if (availableSpace
.width
== 0)
2374 availableSpace
.width
= 10;
2375 if (availableSpace
.height
== 0)
2376 availableSpace
.height
= 10;
2378 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2379 if (onlyVisibleRect
)
2381 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2382 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2385 wxClientDC
dc(this);
2386 dc
.SetFont(GetFont());
2390 GetBuffer().Defragment();
2391 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2392 GetBuffer().Layout(dc
, availableSpace
, flags
);
2393 GetBuffer().SetDirty(false);
2402 /// Is all of the selection bold?
2403 bool wxRichTextCtrl::IsSelectionBold() const
2407 wxRichTextAttr attr
;
2408 wxRichTextRange range
= GetSelectionRange();
2409 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2410 attr
.SetFontWeight(wxBOLD
);
2412 return HasCharacterAttributes(range
, attr
);
2416 // If no selection, then we need to combine current style with default style
2417 // to see what the effect would be if we started typing.
2418 wxRichTextAttr attr
;
2419 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2420 if (GetStyle(GetCaretPosition()+1, attr
))
2422 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2423 return attr
.GetFontWeight() == wxBOLD
;
2429 /// Is all of the selection italics?
2430 bool wxRichTextCtrl::IsSelectionItalics() const
2434 wxRichTextRange range
= GetSelectionRange();
2435 wxRichTextAttr attr
;
2436 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2437 attr
.SetFontStyle(wxITALIC
);
2439 return HasCharacterAttributes(range
, attr
);
2443 // If no selection, then we need to combine current style with default style
2444 // to see what the effect would be if we started typing.
2445 wxRichTextAttr attr
;
2446 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2447 if (GetStyle(GetCaretPosition()+1, attr
))
2449 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2450 return attr
.GetFontStyle() == wxITALIC
;
2456 /// Is all of the selection underlined?
2457 bool wxRichTextCtrl::IsSelectionUnderlined() const
2461 wxRichTextRange range
= GetSelectionRange();
2462 wxRichTextAttr attr
;
2463 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2464 attr
.SetFontUnderlined(true);
2466 return HasCharacterAttributes(range
, attr
);
2470 // If no selection, then we need to combine current style with default style
2471 // to see what the effect would be if we started typing.
2472 wxRichTextAttr attr
;
2473 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2474 if (GetStyle(GetCaretPosition()+1, attr
))
2476 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2477 return attr
.GetFontUnderlined();
2483 /// Apply bold to the selection
2484 bool wxRichTextCtrl::ApplyBoldToSelection()
2486 wxRichTextAttr attr
;
2487 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2488 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2491 return SetStyle(GetSelectionRange(), attr
);
2493 SetDefaultStyle(attr
);
2497 /// Apply italic to the selection
2498 bool wxRichTextCtrl::ApplyItalicToSelection()
2500 wxRichTextAttr attr
;
2501 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2502 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2505 return SetStyle(GetSelectionRange(), attr
);
2507 SetDefaultStyle(attr
);
2511 /// Apply underline to the selection
2512 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2514 wxRichTextAttr attr
;
2515 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2516 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2519 return SetStyle(GetSelectionRange(), attr
);
2521 SetDefaultStyle(attr
);
2525 /// Is all of the selection aligned according to the specified flag?
2526 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2530 wxRichTextRange range
= GetSelectionRange();
2531 wxRichTextAttr attr
;
2532 attr
.SetAlignment(alignment
);
2534 return HasParagraphAttributes(range
, attr
);
2538 // If no selection, then we need to get information from the current paragraph.
2539 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2541 return para
->GetAttributes().GetAlignment() == alignment
;
2546 /// Apply alignment to the selection
2547 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2549 wxRichTextAttr attr
;
2550 attr
.SetAlignment(alignment
);
2552 return SetStyle(GetSelectionRange(), attr
);
2555 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2557 return SetStyle(para
->GetRange(), attr
);
2562 /// Sets the default style to the style under the cursor
2563 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2566 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2568 if (GetStyle(GetCaretPosition(), attr
))
2570 SetDefaultStyle(attr
);
2577 /// Returns the first visible position in the current view
2578 long wxRichTextCtrl::GetFirstVisiblePosition() const
2580 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2582 return line
->GetAbsoluteRange().GetStart();