1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
24 #include "wx/settings.h"
28 #include "wx/stopwatch.h"
31 #include "wx/textfile.h"
33 #include "wx/filename.h"
34 #include "wx/dcbuffer.h"
35 #include "wx/arrimpl.cpp"
37 // DLL options compatibility check:
39 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
49 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
50 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
52 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
55 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
57 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
58 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
60 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
62 EVT_PAINT(wxRichTextCtrl::OnPaint
)
63 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
64 EVT_IDLE(wxRichTextCtrl::OnIdle
)
65 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
66 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
67 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
68 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
69 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
70 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
71 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
72 EVT_CHAR(wxRichTextCtrl::OnChar
)
73 EVT_SIZE(wxRichTextCtrl::OnSize
)
74 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
75 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
76 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
78 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
79 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
81 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
82 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
84 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
85 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
87 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
88 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
90 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
91 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
93 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
94 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
96 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
97 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
104 wxRichTextCtrl::wxRichTextCtrl()
105 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
106 : wxScrollHelper(this)
112 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
113 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
114 : wxScrollHelper(this)
118 Create(parent
, id
, value
, pos
, size
, style
);
122 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
124 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
125 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
129 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
136 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
139 GetBuffer().SetRichTextCtrl(this);
141 wxTextAttrEx attributes
;
142 attributes
.SetFont(GetFont());
143 attributes
.SetTextColour(*wxBLACK
);
144 attributes
.SetBackgroundColour(*wxWHITE
);
145 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
146 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
148 SetDefaultStyle(attributes
);
149 SetBasicStyle(attributes
);
151 SetBackgroundColour(*wxWHITE
);
152 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
154 // Tell the sizers to use the given or best size
155 SetBestFittingSize(size
);
158 RecreateBuffer(size
);
160 SetCursor(wxCursor(wxCURSOR_IBEAM
));
162 if (!value
.IsEmpty())
168 wxRichTextCtrl::~wxRichTextCtrl()
170 delete m_contextMenu
;
173 /// Member initialisation
174 void wxRichTextCtrl::Init()
177 m_contextMenu
= NULL
;
179 m_caretPosition
= -1;
180 m_selectionRange
.SetRange(-2, -2);
181 m_selectionAnchor
= -2;
183 m_caretAtLineStart
= false;
185 m_fullLayoutRequired
= false;
186 m_fullLayoutTime
= 0;
187 m_fullLayoutSavedPosition
= 0;
188 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
191 /// Call Freeze to prevent refresh
192 void wxRichTextCtrl::Freeze()
197 /// Call Thaw to refresh
198 void wxRichTextCtrl::Thaw()
202 if (m_freezeCount
== 0)
210 void wxRichTextCtrl::Clear()
213 m_buffer
.SetDirty(true);
214 m_caretPosition
= -1;
215 m_caretAtLineStart
= false;
216 m_selectionRange
.SetRange(-2, -2);
218 SetScrollbars(0, 0, 0, 0, 0, 0);
220 if (m_freezeCount
== 0)
229 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
235 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
236 //wxLogDebug(wxT("OnPaint"));
240 if (m_freezeCount
> 0)
243 dc
.SetFont(GetFont());
245 // Paint the background
248 wxRegion dirtyRegion
= GetUpdateRegion();
250 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
251 wxRect
availableSpace(GetClientSize());
252 if (GetBuffer().GetDirty())
254 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
255 GetBuffer().SetDirty(false);
259 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
268 // Empty implementation, to prevent flicker
269 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
273 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
275 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
284 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
293 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
299 dc
.SetFont(GetFont());
302 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
304 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
306 m_dragStart
= event
.GetLogicalPosition(dc
);
312 bool caretAtLineStart
= false;
314 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
316 // If we're at the start of a line (but not first in para)
317 // then we should keep the caret showing at the start of the line
318 // by showing the m_caretAtLineStart flag.
319 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
320 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
322 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
323 caretAtLineStart
= true;
327 MoveCaret(position
, caretAtLineStart
);
328 SetDefaultStyleToCursorStyle();
331 wxWindow
* p
= GetParent();
332 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
335 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
338 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
339 frame
->SetStatusText(msg
, 1);
348 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
353 if (GetCapture() == this)
359 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
361 if (!event
.Dragging())
369 dc
.SetFont(GetFont());
372 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
373 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
375 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
377 // TODO: test closeness
379 bool caretAtLineStart
= false;
381 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
383 // If we're at the start of a line (but not first in para)
384 // then we should keep the caret showing at the start of the line
385 // by showing the m_caretAtLineStart flag.
386 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
387 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
389 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
390 caretAtLineStart
= true;
394 if (m_caretPosition
!= position
)
396 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
398 MoveCaret(position
, caretAtLineStart
);
399 SetDefaultStyleToCursorStyle();
408 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
414 /// Left-double-click
415 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
421 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
427 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
430 if (event
.ControlDown())
431 flags
|= wxRICHTEXT_CTRL_DOWN
;
432 if (event
.ShiftDown())
433 flags
|= wxRICHTEXT_SHIFT_DOWN
;
435 flags
|= wxRICHTEXT_ALT_DOWN
;
437 if (event
.GetKeyCode() == WXK_LEFT
||
438 event
.GetKeyCode() == WXK_RIGHT
||
439 event
.GetKeyCode() == WXK_UP
||
440 event
.GetKeyCode() == WXK_DOWN
||
441 event
.GetKeyCode() == WXK_HOME
||
442 event
.GetKeyCode() == WXK_PAGEUP
||
443 event
.GetKeyCode() == WXK_PAGEDOWN
||
444 event
.GetKeyCode() == WXK_END
)
446 KeyboardNavigate(event
.GetKeyCode(), flags
);
450 // all the other keys modify the controls contents which shouldn't be
451 // possible if we're read-only
458 if (event
.GetKeyCode() == WXK_RETURN
)
460 BeginBatchUndo(_("Insert Text"));
462 long newPos
= m_caretPosition
;
464 DeleteSelectedContent(& newPos
);
466 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
468 wxRichTextEvent
cmdEvent(
469 wxEVT_COMMAND_RICHTEXT_RETURN
,
471 cmdEvent
.SetEventObject(this);
472 cmdEvent
.SetFlags(flags
);
473 GetEventHandler()->ProcessEvent(cmdEvent
);
476 SetDefaultStyleToCursorStyle();
478 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
480 else if (event
.GetKeyCode() == WXK_BACK
)
482 BeginBatchUndo(_("Delete Text"));
484 // Submit range in character positions, which are greater than caret positions,
485 // so subtract 1 for deleted character and add 1 for conversion to character position.
486 if (m_caretPosition
> -1 && !HasSelection())
488 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
489 m_caretPosition
, // Current caret position
490 m_caretPosition
-1, // New caret position
494 DeleteSelectedContent();
498 // Shouldn't this be in Do()?
499 if (GetLastPosition() == -1)
503 m_caretPosition
= -1;
505 SetDefaultStyleToCursorStyle();
508 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
510 else if (event
.GetKeyCode() == WXK_DELETE
)
512 BeginBatchUndo(_("Delete Text"));
514 // Submit range in character positions, which are greater than caret positions,
515 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
517 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
518 m_caretPosition
, // Current caret position
519 m_caretPosition
+1, // New caret position
523 DeleteSelectedContent();
527 // Shouldn't this be in Do()?
528 if (GetLastPosition() == -1)
532 m_caretPosition
= -1;
534 SetDefaultStyleToCursorStyle();
539 BeginBatchUndo(_("Insert Text"));
541 long newPos
= m_caretPosition
;
542 DeleteSelectedContent(& newPos
);
544 wxString str
= (wxChar
) event
.GetKeyCode();
545 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
549 SetDefaultStyleToCursorStyle();
550 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
554 /// Delete content if there is a selection, e.g. when pressing a key.
555 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
559 long pos
= m_selectionRange
.GetStart();
560 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
561 m_caretPosition
, // Current caret position
562 pos
, // New caret position
564 m_selectionRange
.SetRange(-2, -2);
574 /// Keyboard navigation
578 Left: left one character
579 Right: right one character
582 Ctrl-Left: left one word
583 Ctrl-Right: right one word
584 Ctrl-Up: previous paragraph start
585 Ctrl-Down: next start of paragraph
588 Ctrl-Home: start of document
589 Ctrl-End: end of document
591 Page-Down: Down a screen
595 Ctrl-Alt-PgUp: Start of window
596 Ctrl-Alt-PgDn: End of window
597 F8: Start selection mode
598 Esc: End selection mode
600 Adding Shift does the above but starts/extends selection.
605 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
607 bool success
= false;
609 if (keyCode
== WXK_RIGHT
)
611 if (flags
& wxRICHTEXT_CTRL_DOWN
)
612 success
= WordRight(1, flags
);
614 success
= MoveRight(1, flags
);
616 else if (keyCode
== WXK_LEFT
)
618 if (flags
& wxRICHTEXT_CTRL_DOWN
)
619 success
= WordLeft(1, flags
);
621 success
= MoveLeft(1, flags
);
623 else if (keyCode
== WXK_UP
)
625 if (flags
& wxRICHTEXT_CTRL_DOWN
)
626 success
= MoveToParagraphStart(flags
);
628 success
= MoveUp(1, flags
);
630 else if (keyCode
== WXK_DOWN
)
632 if (flags
& wxRICHTEXT_CTRL_DOWN
)
633 success
= MoveToParagraphEnd(flags
);
635 success
= MoveDown(1, flags
);
637 else if (keyCode
== WXK_PAGEUP
)
639 success
= PageUp(1, flags
);
641 else if (keyCode
== WXK_PAGEDOWN
)
643 success
= PageDown(1, flags
);
645 else if (keyCode
== WXK_HOME
)
647 if (flags
& wxRICHTEXT_CTRL_DOWN
)
648 success
= MoveHome(flags
);
650 success
= MoveToLineStart(flags
);
652 else if (keyCode
== WXK_END
)
654 if (flags
& wxRICHTEXT_CTRL_DOWN
)
655 success
= MoveEnd(flags
);
657 success
= MoveToLineEnd(flags
);
662 ScrollIntoView(m_caretPosition
, keyCode
);
663 SetDefaultStyleToCursorStyle();
669 /// Extend the selection. Selections are in caret positions.
670 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
672 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
674 // If not currently selecting, start selecting
675 if (m_selectionRange
.GetStart() == -2)
677 m_selectionAnchor
= oldPos
;
680 m_selectionRange
.SetRange(newPos
+1, oldPos
);
682 m_selectionRange
.SetRange(oldPos
+1, newPos
);
686 // Always ensure that the selection range start is greater than
688 if (newPos
> m_selectionAnchor
)
689 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
691 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
694 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
696 wxLogDebug(wxT("Strange selection range"));
705 /// Scroll into view, returning true if we scrolled.
706 /// This takes a _caret_ position.
707 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
709 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
715 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
717 int startXUnits
, startYUnits
;
718 GetViewStart(& startXUnits
, & startYUnits
);
719 int startY
= startYUnits
* ppuY
;
722 GetVirtualSize(& sx
, & sy
);
728 wxRect rect
= line
->GetRect();
730 bool scrolled
= false;
732 wxSize clientSize
= GetClientSize();
735 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
737 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
739 // Make it scroll so this item is at the bottom
741 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
742 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
744 // If we're still off the screen, scroll another line down
745 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
748 if (startYUnits
!= yUnits
)
750 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
754 else if (rect
.y
< startY
)
756 // Make it scroll so this item is at the top
759 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
761 if (startYUnits
!= yUnits
)
763 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
769 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
773 // Make it scroll so this item is at the top
776 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
778 if (startYUnits
!= yUnits
)
780 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
784 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
786 // Make it scroll so this item is at the bottom
788 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
789 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
791 // If we're still off the screen, scroll another line down
792 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
795 if (startYUnits
!= yUnits
)
797 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
807 /// Is the given position visible on the screen?
808 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
810 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
816 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
819 GetViewStart(& startX
, & startY
);
821 startY
= startY
* ppuY
;
824 GetVirtualSize(& sx
, & sy
);
829 wxRect rect
= line
->GetRect();
831 wxSize clientSize
= GetClientSize();
833 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
836 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
838 m_caretPosition
= position
;
839 m_caretAtLineStart
= showAtLineStart
;
842 /// Move caret one visual step forward: this may mean setting a flag
843 /// and keeping the same position if we're going from the end of one line
844 /// to the start of the next, which may be the exact same caret position.
845 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
847 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
849 // Only do the check if we're not at the end of the paragraph (where things work OK
851 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
853 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
857 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
859 // We're at the end of a line. See whether we need to
860 // stay at the same actual caret position but change visual
862 if (oldPosition
== lineRange
.GetEnd())
864 if (m_caretAtLineStart
)
866 // We're already at the start of the line, so actually move on now.
867 m_caretPosition
= oldPosition
+ 1;
868 m_caretAtLineStart
= false;
872 // We're showing at the end of the line, so keep to
873 // the same position but indicate that we're to show
874 // at the start of the next line.
875 m_caretPosition
= oldPosition
;
876 m_caretAtLineStart
= true;
878 SetDefaultStyleToCursorStyle();
884 SetDefaultStyleToCursorStyle();
887 /// Move caret one visual step backward: this may mean setting a flag
888 /// and keeping the same position if we're going from the end of one line
889 /// to the start of the next, which may be the exact same caret position.
890 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
892 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
894 // Only do the check if we're not at the start of the paragraph (where things work OK
896 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
898 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
902 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
904 // We're at the start of a line. See whether we need to
905 // stay at the same actual caret position but change visual
907 if (oldPosition
== lineRange
.GetStart())
909 m_caretPosition
= oldPosition
-1;
910 m_caretAtLineStart
= true;
913 else if (oldPosition
== lineRange
.GetEnd())
915 if (m_caretAtLineStart
)
917 // We're at the start of the line, so keep the same caret position
918 // but clear the start-of-line flag.
919 m_caretPosition
= oldPosition
;
920 m_caretAtLineStart
= false;
924 // We're showing at the end of the line, so go back
925 // to the previous character position.
926 m_caretPosition
= oldPosition
- 1;
928 SetDefaultStyleToCursorStyle();
934 SetDefaultStyleToCursorStyle();
938 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
940 long endPos
= GetBuffer().GetRange().GetEnd();
942 if (m_caretPosition
+ noPositions
< endPos
)
944 long oldPos
= m_caretPosition
;
945 long newPos
= m_caretPosition
+ noPositions
;
947 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
951 // Determine by looking at oldPos and m_caretPosition whether
952 // we moved from the end of a line to the start of the next line, in which case
953 // we want to adjust the caret position such that it is positioned at the
954 // start of the next line, rather than jumping past the first character of the
956 if (noPositions
== 1 && !extendSel
)
957 MoveCaretForward(oldPos
);
959 SetCaretPosition(newPos
);
962 SetDefaultStyleToCursorStyle();
973 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
977 if (m_caretPosition
> startPos
- noPositions
+ 1)
979 long oldPos
= m_caretPosition
;
980 long newPos
= m_caretPosition
- noPositions
;
981 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
985 if (noPositions
== 1 && !extendSel
)
986 MoveCaretBack(oldPos
);
988 SetCaretPosition(newPos
);
991 SetDefaultStyleToCursorStyle();
1002 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1004 return MoveDown(- noLines
, flags
);
1008 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1013 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1014 wxPoint pt
= GetCaret()->GetPosition();
1015 long newLine
= lineNumber
+ noLines
;
1017 if (lineNumber
!= -1)
1021 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1023 if (newLine
> lastLine
)
1033 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1036 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1042 wxClientDC
dc(this);
1044 dc
.SetFont(GetFont());
1046 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1048 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1050 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1051 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1052 // so we view the caret at the start of the line.
1053 bool caretLineStart
= false;
1054 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1056 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1057 wxRichTextRange lineRange
;
1059 lineRange
= thisLine
->GetAbsoluteRange();
1061 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1064 caretLineStart
= true;
1068 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1069 if (para
&& para
->GetRange().GetStart() == newPos
)
1074 long newSelEnd
= newPos
;
1076 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1080 SetCaretPosition(newPos
, caretLineStart
);
1082 SetDefaultStyleToCursorStyle();
1092 /// Move to the end of the paragraph
1093 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1095 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1098 long newPos
= para
->GetRange().GetEnd() - 1;
1099 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1103 SetCaretPosition(newPos
);
1105 SetDefaultStyleToCursorStyle();
1115 /// Move to the start of the paragraph
1116 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1118 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1121 long newPos
= para
->GetRange().GetStart() - 1;
1122 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1126 SetCaretPosition(newPos
);
1128 SetDefaultStyleToCursorStyle();
1138 /// Move to the end of the line
1139 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1141 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1145 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1146 long newPos
= lineRange
.GetEnd();
1147 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1151 SetCaretPosition(newPos
);
1153 SetDefaultStyleToCursorStyle();
1163 /// Move to the start of the line
1164 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1166 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1169 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1170 long newPos
= lineRange
.GetStart()-1;
1172 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1176 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1178 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1180 SetDefaultStyleToCursorStyle();
1190 /// Move to the start of the buffer
1191 bool wxRichTextCtrl::MoveHome(int flags
)
1193 if (m_caretPosition
!= -1)
1195 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1199 SetCaretPosition(-1);
1201 SetDefaultStyleToCursorStyle();
1211 /// Move to the end of the buffer
1212 bool wxRichTextCtrl::MoveEnd(int flags
)
1214 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1216 if (m_caretPosition
!= endPos
)
1218 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1222 SetCaretPosition(endPos
);
1224 SetDefaultStyleToCursorStyle();
1234 /// Move noPages pages up
1235 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1237 return PageDown(- noPages
, flags
);
1240 /// Move noPages pages down
1241 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1243 // Calculate which line occurs noPages * screen height further down.
1244 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1247 wxSize clientSize
= GetClientSize();
1248 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1250 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1253 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1254 long pos
= lineRange
.GetStart()-1;
1255 if (pos
!= m_caretPosition
)
1257 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1259 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1263 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1265 SetDefaultStyleToCursorStyle();
1277 // Finds the caret position for the next word
1278 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1280 long endPos
= GetBuffer().GetRange().GetEnd();
1284 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1286 // First skip current text to space
1287 while (i
< endPos
&& i
> -1)
1289 // i is in character, not caret positions
1290 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1291 if (text
!= wxT(" ") && !text
.empty())
1298 while (i
< endPos
&& i
> -1)
1300 // i is in character, not caret positions
1301 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1302 if (text
.empty()) // End of paragraph, or maybe an image
1303 return wxMax(-1, i
- 1);
1304 else if (text
== wxT(" ") || text
.empty())
1308 // Convert to caret position
1309 return wxMax(-1, i
- 1);
1318 long i
= m_caretPosition
;
1320 // First skip white space
1321 while (i
< endPos
&& i
> -1)
1323 // i is in character, not caret positions
1324 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1325 if (text
.empty()) // End of paragraph, or maybe an image
1327 else if (text
== wxT(" ") || text
.empty())
1332 // Next skip current text to space
1333 while (i
< endPos
&& i
> -1)
1335 // i is in character, not caret positions
1336 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1337 if (text
!= wxT(" ") /* && !text.empty() */)
1350 /// Move n words left
1351 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1353 long pos
= FindNextWordPosition(-1);
1354 if (pos
!= m_caretPosition
)
1356 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1358 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1362 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1364 SetDefaultStyleToCursorStyle();
1374 /// Move n words right
1375 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1377 long pos
= FindNextWordPosition(1);
1378 if (pos
!= m_caretPosition
)
1380 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1382 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1386 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1388 SetDefaultStyleToCursorStyle();
1399 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1401 // Only do sizing optimization for large buffers
1402 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1404 m_fullLayoutRequired
= true;
1405 m_fullLayoutTime
= wxGetLocalTimeMillis();
1406 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1407 LayoutContent(true /* onlyVisibleRect */);
1410 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1418 /// Idle-time processing
1419 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1421 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1423 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1425 m_fullLayoutRequired
= false;
1426 m_fullLayoutTime
= 0;
1427 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1428 ShowPosition(m_fullLayoutSavedPosition
);
1435 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1441 /// Set up scrollbars, e.g. after a resize
1442 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1447 if (GetBuffer().IsEmpty())
1449 SetScrollbars(0, 0, 0, 0, 0, 0);
1453 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1454 // of pixels. See e.g. wxVScrolledWindow for ideas.
1455 int pixelsPerUnit
= 5;
1456 wxSize clientSize
= GetClientSize();
1458 int maxHeight
= GetBuffer().GetCachedSize().y
;
1460 // Round up so we have at least maxHeight pixels
1461 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1463 int startX
= 0, startY
= 0;
1465 GetViewStart(& startX
, & startY
);
1467 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1468 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1470 // Move to previous scroll position if
1472 SetScrollbars(0, pixelsPerUnit
,
1474 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1477 /// Paint the background
1478 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1480 wxColour backgroundColour
= GetBackgroundColour();
1481 if (!backgroundColour
.Ok())
1482 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1484 // Clear the background
1485 dc
.SetBrush(wxBrush(backgroundColour
));
1486 dc
.SetPen(*wxTRANSPARENT_PEN
);
1487 wxRect
windowRect(GetClientSize());
1488 windowRect
.x
-= 2; windowRect
.y
-= 2;
1489 windowRect
.width
+= 4; windowRect
.height
+= 4;
1491 // We need to shift the rectangle to take into account
1492 // scrolling. Converting device to logical coordinates.
1493 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1494 dc
.DrawRectangle(windowRect
);
1497 /// Recreate buffer bitmap if necessary
1498 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1501 if (sz
== wxDefaultSize
)
1502 sz
= GetClientSize();
1504 if (sz
.x
< 1 || sz
.y
< 1)
1507 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1508 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1509 return m_bufferBitmap
.Ok();
1512 // ----------------------------------------------------------------------------
1513 // file IO functions
1514 // ----------------------------------------------------------------------------
1516 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1518 bool success
= GetBuffer().LoadFile(filename
, type
);
1520 m_filename
= filename
;
1523 SetInsertionPoint(0);
1526 SetupScrollbars(true);
1534 wxLogError(_("File couldn't be loaded."));
1540 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1542 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1543 if ( filenameToUse
.empty() )
1545 // what kind of message to give? is it an error or a program bug?
1546 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1551 if (GetBuffer().SaveFile(filenameToUse
, type
))
1553 m_filename
= filenameToUse
;
1561 wxLogError(_("The text couldn't be saved."));
1566 // ----------------------------------------------------------------------------
1567 // wxRichTextCtrl specific functionality
1568 // ----------------------------------------------------------------------------
1570 /// Add a new paragraph of text to the end of the buffer
1571 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1573 return GetBuffer().AddParagraph(text
);
1577 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1579 return GetBuffer().AddImage(image
);
1582 // ----------------------------------------------------------------------------
1583 // selection and ranges
1584 // ----------------------------------------------------------------------------
1586 void wxRichTextCtrl::SelectAll()
1588 SetSelection(0, GetLastPosition());
1589 m_selectionAnchor
= -1;
1593 void wxRichTextCtrl::SelectNone()
1595 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1596 SetSelection(-2, -2);
1597 m_selectionAnchor
= -2;
1600 wxString
wxRichTextCtrl::GetStringSelection() const
1603 GetSelection(&from
, &to
);
1605 return GetRange(from
, to
);
1608 // do the window-specific processing after processing the update event
1609 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1610 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1613 wxWindowBase::DoUpdateWindowUI(event
);
1616 if ( event
.GetSetText() )
1618 if ( event
.GetText() != GetValue() )
1619 SetValue(event
.GetText());
1622 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1624 // ----------------------------------------------------------------------------
1626 // ----------------------------------------------------------------------------
1628 wxTextCtrlHitTestResult
1629 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1631 // implement in terms of the other overload as the native ports typically
1632 // can get the position and not (x, y) pair directly (although wxUniv
1633 // directly gets x and y -- and so overrides this method as well)
1635 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1637 if ( rc
!= wxTE_HT_UNKNOWN
)
1639 PositionToXY(pos
, x
, y
);
1645 wxTextCtrlHitTestResult
1646 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1649 wxClientDC
dc((wxRichTextCtrl
*) this);
1650 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1652 // Buffer uses logical position (relative to start of buffer)
1654 wxPoint pt2
= GetLogicalPoint(pt
);
1656 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1660 case wxRICHTEXT_HITTEST_BEFORE
:
1661 return wxTE_HT_BEFORE
;
1663 case wxRICHTEXT_HITTEST_AFTER
:
1664 return wxTE_HT_BEYOND
;
1666 case wxRICHTEXT_HITTEST_ON
:
1667 return wxTE_HT_ON_TEXT
;
1670 return wxTE_HT_UNKNOWN
;
1673 // ----------------------------------------------------------------------------
1674 // set/get the controls text
1675 // ----------------------------------------------------------------------------
1677 wxString
wxRichTextCtrl::GetValue() const
1679 return GetBuffer().GetText();
1682 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1684 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1687 void wxRichTextCtrl::SetValue(const wxString
& value
)
1691 // if the text is long enough, it's faster to just set it instead of first
1692 // comparing it with the old one (chances are that it will be different
1693 // anyhow, this comparison is there to avoid flicker for small single-line
1694 // edit controls mostly)
1695 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1697 DoWriteText(value
, false /* not selection only */);
1699 // for compatibility, don't move the cursor when doing SetValue()
1700 SetInsertionPoint(0);
1704 // still send an event for consistency
1708 // we should reset the modified flag even if the value didn't really change
1710 // mark the control as being not dirty - we changed its text, not the
1715 void wxRichTextCtrl::WriteText(const wxString
& value
)
1720 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1722 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1725 void wxRichTextCtrl::AppendText(const wxString
& text
)
1727 SetInsertionPointEnd();
1732 /// Write an image at the current insertion point
1733 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1735 wxRichTextImageBlock imageBlock
;
1737 wxImage image2
= image
;
1738 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1739 return WriteImage(imageBlock
);
1744 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1746 wxRichTextImageBlock imageBlock
;
1749 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1750 return WriteImage(imageBlock
);
1755 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1757 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1760 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1764 wxRichTextImageBlock imageBlock
;
1766 wxImage image
= bitmap
.ConvertToImage();
1767 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1768 return WriteImage(imageBlock
);
1774 /// Insert a newline (actually paragraph) at the current insertion point.
1775 bool wxRichTextCtrl::Newline()
1777 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1781 // ----------------------------------------------------------------------------
1782 // Clipboard operations
1783 // ----------------------------------------------------------------------------
1785 void wxRichTextCtrl::Copy()
1789 wxRichTextRange range
= GetSelectionRange();
1790 GetBuffer().CopyToClipboard(range
);
1794 void wxRichTextCtrl::Cut()
1798 wxRichTextRange range
= GetSelectionRange();
1799 GetBuffer().CopyToClipboard(range
);
1801 DeleteSelectedContent();
1807 void wxRichTextCtrl::Paste()
1811 BeginBatchUndo(_("Paste"));
1813 long newPos
= m_caretPosition
;
1814 DeleteSelectedContent(& newPos
);
1816 GetBuffer().PasteFromClipboard(newPos
);
1822 void wxRichTextCtrl::DeleteSelection()
1824 if (CanDeleteSelection())
1826 DeleteSelectedContent();
1830 bool wxRichTextCtrl::HasSelection() const
1832 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1835 bool wxRichTextCtrl::CanCopy() const
1837 // Can copy if there's a selection
1838 return HasSelection();
1841 bool wxRichTextCtrl::CanCut() const
1843 return HasSelection() && IsEditable();
1846 bool wxRichTextCtrl::CanPaste() const
1848 if ( !IsEditable() )
1851 return GetBuffer().CanPasteFromClipboard();
1854 bool wxRichTextCtrl::CanDeleteSelection() const
1856 return HasSelection() && IsEditable();
1860 // ----------------------------------------------------------------------------
1862 // ----------------------------------------------------------------------------
1864 void wxRichTextCtrl::SetEditable(bool editable
)
1866 m_editable
= editable
;
1869 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1873 m_caretPosition
= pos
- 1;
1876 void wxRichTextCtrl::SetInsertionPointEnd()
1878 long pos
= GetLastPosition();
1879 SetInsertionPoint(pos
);
1882 long wxRichTextCtrl::GetInsertionPoint() const
1884 return m_caretPosition
+1;
1887 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1889 return GetBuffer().GetRange().GetEnd();
1892 // If the return values from and to are the same, there is no
1894 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1896 *from
= m_selectionRange
.GetStart();
1897 *to
= m_selectionRange
.GetEnd();
1900 bool wxRichTextCtrl::IsEditable() const
1905 // ----------------------------------------------------------------------------
1907 // ----------------------------------------------------------------------------
1909 void wxRichTextCtrl::SetSelection(long from
, long to
)
1911 // if from and to are both -1, it means (in wxWidgets) that all text should
1913 if ( (from
== -1) && (to
== -1) )
1916 to
= GetLastPosition();
1919 DoSetSelection(from
, to
);
1922 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1924 m_selectionAnchor
= from
;
1925 m_selectionRange
.SetRange(from
, to
);
1930 // ----------------------------------------------------------------------------
1932 // ----------------------------------------------------------------------------
1934 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1936 BeginBatchUndo(_("Replace"));
1938 DeleteSelectedContent();
1940 DoWriteText(value
, true /* selection only */);
1945 void wxRichTextCtrl::Remove(long from
, long to
)
1949 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1950 m_caretPosition
, // Current caret position
1951 from
, // New caret position
1959 bool wxRichTextCtrl::IsModified() const
1961 return m_buffer
.IsModified();
1964 void wxRichTextCtrl::MarkDirty()
1966 m_buffer
.Modify(true);
1969 void wxRichTextCtrl::DiscardEdits()
1971 m_buffer
.Modify(false);
1972 m_buffer
.GetCommandProcessor()->ClearCommands();
1975 int wxRichTextCtrl::GetNumberOfLines() const
1977 return GetBuffer().GetParagraphCount();
1980 // ----------------------------------------------------------------------------
1981 // Positions <-> coords
1982 // ----------------------------------------------------------------------------
1984 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1986 return GetBuffer().XYToPosition(x
, y
);
1989 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1991 return GetBuffer().PositionToXY(pos
, x
, y
);
1994 // ----------------------------------------------------------------------------
1996 // ----------------------------------------------------------------------------
1998 void wxRichTextCtrl::ShowPosition(long pos
)
2000 if (!IsPositionVisible(pos
))
2001 ScrollIntoView(pos
-1, WXK_DOWN
);
2004 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2006 return GetBuffer().GetParagraphLength(lineNo
);
2009 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2011 return GetBuffer().GetParagraphText(lineNo
);
2014 // ----------------------------------------------------------------------------
2016 // ----------------------------------------------------------------------------
2018 void wxRichTextCtrl::Undo()
2022 GetCommandProcessor()->Undo();
2026 void wxRichTextCtrl::Redo()
2030 GetCommandProcessor()->Redo();
2034 bool wxRichTextCtrl::CanUndo() const
2036 return GetCommandProcessor()->CanUndo();
2039 bool wxRichTextCtrl::CanRedo() const
2041 return GetCommandProcessor()->CanRedo();
2044 // ----------------------------------------------------------------------------
2045 // implementation details
2046 // ----------------------------------------------------------------------------
2048 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2050 SetValue(event
.GetString());
2051 GetEventHandler()->ProcessEvent(event
);
2054 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2056 // By default, load the first file into the text window.
2057 if (event
.GetNumberOfFiles() > 0)
2059 LoadFile(event
.GetFiles()[0]);
2063 // ----------------------------------------------------------------------------
2064 // text control event processing
2065 // ----------------------------------------------------------------------------
2067 bool wxRichTextCtrl::SendUpdateEvent()
2069 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2070 InitCommandEvent(event
);
2072 return GetEventHandler()->ProcessEvent(event
);
2075 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2077 event
.SetEventObject((wxControlBase
*)this); // const_cast
2079 switch ( m_clientDataType
)
2081 case wxClientData_Void
:
2082 event
.SetClientData(GetClientData());
2085 case wxClientData_Object
:
2086 event
.SetClientObject(GetClientObject());
2089 case wxClientData_None
:
2096 wxSize
wxRichTextCtrl::DoGetBestSize() const
2098 return wxSize(10, 10);
2101 // ----------------------------------------------------------------------------
2102 // standard handlers for standard edit menu events
2103 // ----------------------------------------------------------------------------
2105 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2110 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2115 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2120 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2125 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2130 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2135 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2137 event
.Enable( CanCut() );
2140 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2142 event
.Enable( CanCopy() );
2145 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2147 event
.Enable( CanDeleteSelection() );
2150 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2152 event
.Enable( CanPaste() );
2155 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2157 event
.Enable( CanUndo() );
2158 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2161 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2163 event
.Enable( CanRedo() );
2164 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2167 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2172 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2174 event
.Enable(GetLastPosition() > 0);
2177 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2181 m_contextMenu
= new wxMenu
;
2182 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2183 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2184 m_contextMenu
->AppendSeparator();
2185 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2186 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2187 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2188 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2189 m_contextMenu
->AppendSeparator();
2190 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2192 PopupMenu(m_contextMenu
);
2196 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2198 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2201 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2203 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), wxTextAttrEx(style
));
2206 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2208 return GetBuffer().SetStyle(range
, style
);
2211 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2213 return GetBuffer().SetDefaultStyle(style
);
2216 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2218 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2221 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2223 return GetBuffer().GetDefaultStyle();
2226 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2228 return GetBuffer().GetDefaultStyle();
2231 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
) const
2234 if (GetBuffer().GetStyle(position
, attr
))
2243 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2245 return GetBuffer().GetStyle(position
, style
);
2248 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2250 return GetBuffer().GetStyle(position
, style
);
2253 /// Set font, and also the buffer attributes
2254 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2256 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2257 wxControl::SetFont(font
);
2259 wxScrolledWindow::SetFont(font
);
2262 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2264 GetBuffer().SetBasicStyle(attr
);
2265 GetBuffer().SetDefaultStyle(attr
);
2270 /// Transform logical to physical
2271 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2274 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2279 /// Transform physical to logical
2280 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2283 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2288 /// Position the caret
2289 void wxRichTextCtrl::PositionCaret()
2294 //wxLogDebug(wxT("PositionCaret"));
2297 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2299 wxPoint originalPt
= caretRect
.GetPosition();
2300 wxPoint pt
= GetPhysicalPoint(originalPt
);
2301 if (GetCaret()->GetPosition() != pt
)
2303 GetCaret()->Move(pt
);
2304 GetCaret()->SetSize(caretRect
.GetSize());
2309 /// Get the caret height and position for the given character position
2310 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2312 wxClientDC
dc(this);
2313 dc
.SetFont(GetFont());
2320 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2322 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2329 /// Gets the line for the visible caret position. If the caret is
2330 /// shown at the very end of the line, it means the next character is actually
2331 /// on the following line. So let's get the line we're expecting to find
2332 /// if this is the case.
2333 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2335 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2336 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2339 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2340 if (caretPosition
== lineRange
.GetStart()-1 &&
2341 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2343 if (!m_caretAtLineStart
)
2344 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2351 /// Move the caret to the given character position
2352 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2354 if (GetBuffer().GetDirty())
2357 if (pos
<= GetBuffer().GetRange().GetEnd())
2359 SetCaretPosition(pos
, showAtLineStart
);
2369 /// Layout the buffer: which we must do before certain operations, such as
2370 /// setting the caret position.
2371 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2373 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2375 wxRect
availableSpace(GetClientSize());
2376 if (availableSpace
.width
== 0)
2377 availableSpace
.width
= 10;
2378 if (availableSpace
.height
== 0)
2379 availableSpace
.height
= 10;
2381 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2382 if (onlyVisibleRect
)
2384 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2385 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2388 wxClientDC
dc(this);
2389 dc
.SetFont(GetFont());
2393 GetBuffer().Defragment();
2394 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2395 GetBuffer().Layout(dc
, availableSpace
, flags
);
2396 GetBuffer().SetDirty(false);
2405 /// Is all of the selection bold?
2406 bool wxRichTextCtrl::IsSelectionBold() const
2410 wxRichTextAttr attr
;
2411 wxRichTextRange range
= GetSelectionRange();
2412 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2413 attr
.SetFontWeight(wxBOLD
);
2415 return HasCharacterAttributes(range
, attr
);
2419 // If no selection, then we need to combine current style with default style
2420 // to see what the effect would be if we started typing.
2421 wxRichTextAttr attr
;
2422 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2423 if (GetStyle(GetCaretPosition()+1, attr
))
2425 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2426 return attr
.GetFontWeight() == wxBOLD
;
2432 /// Is all of the selection italics?
2433 bool wxRichTextCtrl::IsSelectionItalics() const
2437 wxRichTextRange range
= GetSelectionRange();
2438 wxRichTextAttr attr
;
2439 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2440 attr
.SetFontStyle(wxITALIC
);
2442 return HasCharacterAttributes(range
, attr
);
2446 // If no selection, then we need to combine current style with default style
2447 // to see what the effect would be if we started typing.
2448 wxRichTextAttr attr
;
2449 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2450 if (GetStyle(GetCaretPosition()+1, attr
))
2452 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2453 return attr
.GetFontStyle() == wxITALIC
;
2459 /// Is all of the selection underlined?
2460 bool wxRichTextCtrl::IsSelectionUnderlined() const
2464 wxRichTextRange range
= GetSelectionRange();
2465 wxRichTextAttr attr
;
2466 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2467 attr
.SetFontUnderlined(true);
2469 return HasCharacterAttributes(range
, attr
);
2473 // If no selection, then we need to combine current style with default style
2474 // to see what the effect would be if we started typing.
2475 wxRichTextAttr attr
;
2476 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2477 if (GetStyle(GetCaretPosition()+1, attr
))
2479 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2480 return attr
.GetFontUnderlined();
2486 /// Apply bold to the selection
2487 bool wxRichTextCtrl::ApplyBoldToSelection()
2489 wxRichTextAttr attr
;
2490 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2491 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2494 return SetStyle(GetSelectionRange(), attr
);
2496 SetDefaultStyle(attr
);
2500 /// Apply italic to the selection
2501 bool wxRichTextCtrl::ApplyItalicToSelection()
2503 wxRichTextAttr attr
;
2504 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2505 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2508 return SetStyle(GetSelectionRange(), attr
);
2510 SetDefaultStyle(attr
);
2514 /// Apply underline to the selection
2515 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2517 wxRichTextAttr attr
;
2518 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2519 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2522 return SetStyle(GetSelectionRange(), attr
);
2524 SetDefaultStyle(attr
);
2528 /// Is all of the selection aligned according to the specified flag?
2529 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2533 wxRichTextRange range
= GetSelectionRange();
2534 wxRichTextAttr attr
;
2535 attr
.SetAlignment(alignment
);
2537 return HasParagraphAttributes(range
, attr
);
2541 // If no selection, then we need to get information from the current paragraph.
2542 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2544 return para
->GetAttributes().GetAlignment() == alignment
;
2549 /// Apply alignment to the selection
2550 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2552 wxRichTextAttr attr
;
2553 attr
.SetAlignment(alignment
);
2555 return SetStyle(GetSelectionRange(), attr
);
2558 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2560 return SetStyle(para
->GetRange(), attr
);
2565 /// Sets the default style to the style under the cursor
2566 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2569 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2571 if (GetStyle(GetCaretPosition(), attr
))
2573 SetDefaultStyle(attr
);
2580 /// Returns the first visible position in the current view
2581 long wxRichTextCtrl::GetFirstVisiblePosition() const
2583 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2585 return line
->GetAbsoluteRange().GetStart();