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 if (m_freezeCount
== 0)
224 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
230 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
231 //wxLogDebug(wxT("OnPaint"));
235 if (m_freezeCount
> 0)
238 dc
.SetFont(GetFont());
240 // Paint the background
243 wxRegion dirtyRegion
= GetUpdateRegion();
245 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
246 wxRect
availableSpace(GetClientSize());
247 if (GetBuffer().GetDirty())
249 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
250 GetBuffer().SetDirty(false);
254 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
263 // Empty implementation, to prevent flicker
264 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
268 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
270 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
279 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
288 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
294 dc
.SetFont(GetFont());
297 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
299 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
301 m_dragStart
= event
.GetLogicalPosition(dc
);
307 bool caretAtLineStart
= false;
309 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
311 // If we're at the start of a line (but not first in para)
312 // then we should keep the caret showing at the start of the line
313 // by showing the m_caretAtLineStart flag.
314 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
315 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
317 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
318 caretAtLineStart
= true;
322 MoveCaret(position
, caretAtLineStart
);
323 SetDefaultStyleToCursorStyle();
326 wxWindow
* p
= GetParent();
327 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
330 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
333 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
334 frame
->SetStatusText(msg
, 1);
343 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
348 if (GetCapture() == this)
354 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
356 if (!event
.Dragging())
364 dc
.SetFont(GetFont());
367 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
368 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
370 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
372 // TODO: test closeness
374 bool caretAtLineStart
= false;
376 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
378 // If we're at the start of a line (but not first in para)
379 // then we should keep the caret showing at the start of the line
380 // by showing the m_caretAtLineStart flag.
381 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
382 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
384 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
385 caretAtLineStart
= true;
389 if (m_caretPosition
!= position
)
391 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
393 MoveCaret(position
, caretAtLineStart
);
394 SetDefaultStyleToCursorStyle();
403 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
409 /// Left-double-click
410 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
416 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
422 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
425 if (event
.ControlDown())
426 flags
|= wxRICHTEXT_CTRL_DOWN
;
427 if (event
.ShiftDown())
428 flags
|= wxRICHTEXT_SHIFT_DOWN
;
430 flags
|= wxRICHTEXT_ALT_DOWN
;
432 if (event
.GetKeyCode() == WXK_LEFT
||
433 event
.GetKeyCode() == WXK_RIGHT
||
434 event
.GetKeyCode() == WXK_UP
||
435 event
.GetKeyCode() == WXK_DOWN
||
436 event
.GetKeyCode() == WXK_HOME
||
437 event
.GetKeyCode() == WXK_PAGEUP
||
438 event
.GetKeyCode() == WXK_PAGEDOWN
||
439 event
.GetKeyCode() == WXK_END
)
441 KeyboardNavigate(event
.GetKeyCode(), flags
);
445 // all the other keys modify the controls contents which shouldn't be
446 // possible if we're read-only
453 if (event
.GetKeyCode() == WXK_RETURN
)
455 BeginBatchUndo(_("Insert Text"));
457 long newPos
= m_caretPosition
;
459 DeleteSelectedContent(& newPos
);
461 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
463 wxRichTextEvent
cmdEvent(
464 wxEVT_COMMAND_RICHTEXT_RETURN
,
466 cmdEvent
.SetEventObject(this);
467 cmdEvent
.SetFlags(flags
);
468 GetEventHandler()->ProcessEvent(cmdEvent
);
471 SetDefaultStyleToCursorStyle();
473 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
475 else if (event
.GetKeyCode() == WXK_BACK
)
477 BeginBatchUndo(_("Delete Text"));
479 // Submit range in character positions, which are greater than caret positions,
480 // so subtract 1 for deleted character and add 1 for conversion to character position.
481 if (m_caretPosition
> -1 && !HasSelection())
483 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
484 m_caretPosition
, // Current caret position
485 m_caretPosition
-1, // New caret position
489 DeleteSelectedContent();
493 // Shouldn't this be in Do()?
494 if (GetLastPosition() == -1)
498 m_caretPosition
= -1;
500 SetDefaultStyleToCursorStyle();
503 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
505 else if (event
.GetKeyCode() == WXK_DELETE
)
507 BeginBatchUndo(_("Delete Text"));
509 // Submit range in character positions, which are greater than caret positions,
510 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
512 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
513 m_caretPosition
, // Current caret position
514 m_caretPosition
+1, // New caret position
518 DeleteSelectedContent();
522 // Shouldn't this be in Do()?
523 if (GetLastPosition() == -1)
527 m_caretPosition
= -1;
529 SetDefaultStyleToCursorStyle();
534 BeginBatchUndo(_("Insert Text"));
536 long newPos
= m_caretPosition
;
537 DeleteSelectedContent(& newPos
);
539 wxString str
= (wxChar
) event
.GetKeyCode();
540 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
544 SetDefaultStyleToCursorStyle();
545 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
549 /// Delete content if there is a selection, e.g. when pressing a key.
550 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
554 long pos
= m_selectionRange
.GetStart();
555 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
556 m_caretPosition
, // Current caret position
557 pos
, // New caret position
559 m_selectionRange
.SetRange(-2, -2);
569 /// Keyboard navigation
573 Left: left one character
574 Right: right one character
577 Ctrl-Left: left one word
578 Ctrl-Right: right one word
579 Ctrl-Up: previous paragraph start
580 Ctrl-Down: next start of paragraph
583 Ctrl-Home: start of document
584 Ctrl-End: end of document
586 Page-Down: Down a screen
590 Ctrl-Alt-PgUp: Start of window
591 Ctrl-Alt-PgDn: End of window
592 F8: Start selection mode
593 Esc: End selection mode
595 Adding Shift does the above but starts/extends selection.
600 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
602 bool success
= false;
604 if (keyCode
== WXK_RIGHT
)
606 if (flags
& wxRICHTEXT_CTRL_DOWN
)
607 success
= WordRight(1, flags
);
609 success
= MoveRight(1, flags
);
611 else if (keyCode
== WXK_LEFT
)
613 if (flags
& wxRICHTEXT_CTRL_DOWN
)
614 success
= WordLeft(1, flags
);
616 success
= MoveLeft(1, flags
);
618 else if (keyCode
== WXK_UP
)
620 if (flags
& wxRICHTEXT_CTRL_DOWN
)
621 success
= MoveToParagraphStart(flags
);
623 success
= MoveUp(1, flags
);
625 else if (keyCode
== WXK_DOWN
)
627 if (flags
& wxRICHTEXT_CTRL_DOWN
)
628 success
= MoveToParagraphEnd(flags
);
630 success
= MoveDown(1, flags
);
632 else if (keyCode
== WXK_PAGEUP
)
634 success
= PageUp(1, flags
);
636 else if (keyCode
== WXK_PAGEDOWN
)
638 success
= PageDown(1, flags
);
640 else if (keyCode
== WXK_HOME
)
642 if (flags
& wxRICHTEXT_CTRL_DOWN
)
643 success
= MoveHome(flags
);
645 success
= MoveToLineStart(flags
);
647 else if (keyCode
== WXK_END
)
649 if (flags
& wxRICHTEXT_CTRL_DOWN
)
650 success
= MoveEnd(flags
);
652 success
= MoveToLineEnd(flags
);
657 ScrollIntoView(m_caretPosition
, keyCode
);
658 SetDefaultStyleToCursorStyle();
664 /// Extend the selection. Selections are in caret positions.
665 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
667 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
669 // If not currently selecting, start selecting
670 if (m_selectionRange
.GetStart() == -2)
672 m_selectionAnchor
= oldPos
;
675 m_selectionRange
.SetRange(newPos
+1, oldPos
);
677 m_selectionRange
.SetRange(oldPos
+1, newPos
);
681 // Always ensure that the selection range start is greater than
683 if (newPos
> m_selectionAnchor
)
684 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
686 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
689 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
691 wxLogDebug(wxT("Strange selection range"));
700 /// Scroll into view, returning true if we scrolled.
701 /// This takes a _caret_ position.
702 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
704 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
710 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
713 GetViewStart(& startX
, & startY
);
715 startY
= startY
* ppuY
;
718 GetVirtualSize(& sx
, & sy
);
723 wxRect rect
= line
->GetRect();
725 bool scrolled
= false;
727 wxSize clientSize
= GetClientSize();
730 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_PAGEDOWN
)
732 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
734 // Make it scroll so this item is at the bottom
736 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
737 y
= (int) (0.5 + y
/ppuY
);
741 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
745 else if (rect
.y
< startY
)
747 // Make it scroll so this item is at the top
750 y
= (int) (0.5 + y
/ppuY
);
754 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
760 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PAGEUP
)
764 // Make it scroll so this item is at the top
767 y
= (int) (0.5 + y
/ppuY
);
771 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
775 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
777 // Make it scroll so this item is at the bottom
779 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
780 y
= (int) (0.5 + y
/ppuY
);
784 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, y
);
794 /// Is the given position visible on the screen?
795 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
797 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
803 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
806 GetViewStart(& startX
, & startY
);
808 startY
= startY
* ppuY
;
811 GetVirtualSize(& sx
, & sy
);
816 wxRect rect
= line
->GetRect();
818 wxSize clientSize
= GetClientSize();
820 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
823 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
825 m_caretPosition
= position
;
826 m_caretAtLineStart
= showAtLineStart
;
829 /// Move caret one visual step forward: this may mean setting a flag
830 /// and keeping the same position if we're going from the end of one line
831 /// to the start of the next, which may be the exact same caret position.
832 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
834 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
836 // Only do the check if we're not at the end of the paragraph (where things work OK
838 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
840 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
844 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
846 // We're at the end of a line. See whether we need to
847 // stay at the same actual caret position but change visual
849 if (oldPosition
== lineRange
.GetEnd())
851 if (m_caretAtLineStart
)
853 // We're already at the start of the line, so actually move on now.
854 m_caretPosition
= oldPosition
+ 1;
855 m_caretAtLineStart
= false;
859 // We're showing at the end of the line, so keep to
860 // the same position but indicate that we're to show
861 // at the start of the next line.
862 m_caretPosition
= oldPosition
;
863 m_caretAtLineStart
= true;
865 SetDefaultStyleToCursorStyle();
871 SetDefaultStyleToCursorStyle();
874 /// Move caret one visual step backward: this may mean setting a flag
875 /// and keeping the same position if we're going from the end of one line
876 /// to the start of the next, which may be the exact same caret position.
877 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
879 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
881 // Only do the check if we're not at the start of the paragraph (where things work OK
883 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
885 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
889 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
891 // We're at the start of a line. See whether we need to
892 // stay at the same actual caret position but change visual
894 if (oldPosition
== lineRange
.GetStart())
896 m_caretPosition
= oldPosition
-1;
897 m_caretAtLineStart
= true;
900 else if (oldPosition
== lineRange
.GetEnd())
902 if (m_caretAtLineStart
)
904 // We're at the start of the line, so keep the same caret position
905 // but clear the start-of-line flag.
906 m_caretPosition
= oldPosition
;
907 m_caretAtLineStart
= false;
911 // We're showing at the end of the line, so go back
912 // to the previous character position.
913 m_caretPosition
= oldPosition
- 1;
915 SetDefaultStyleToCursorStyle();
921 SetDefaultStyleToCursorStyle();
925 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
927 long endPos
= GetBuffer().GetRange().GetEnd();
929 if (m_caretPosition
+ noPositions
< endPos
)
931 long oldPos
= m_caretPosition
;
932 long newPos
= m_caretPosition
+ noPositions
;
934 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
938 // Determine by looking at oldPos and m_caretPosition whether
939 // we moved from the end of a line to the start of the next line, in which case
940 // we want to adjust the caret position such that it is positioned at the
941 // start of the next line, rather than jumping past the first character of the
943 if (noPositions
== 1 && !extendSel
)
944 MoveCaretForward(oldPos
);
946 SetCaretPosition(newPos
);
949 SetDefaultStyleToCursorStyle();
960 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
964 if (m_caretPosition
> startPos
- noPositions
+ 1)
966 long oldPos
= m_caretPosition
;
967 long newPos
= m_caretPosition
- noPositions
;
968 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
972 if (noPositions
== 1 && !extendSel
)
973 MoveCaretBack(oldPos
);
975 SetCaretPosition(newPos
);
978 SetDefaultStyleToCursorStyle();
989 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
991 return MoveDown(- noLines
, flags
);
995 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1000 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1001 wxPoint pt
= GetCaret()->GetPosition();
1002 long newLine
= lineNumber
+ noLines
;
1004 if (lineNumber
!= -1)
1008 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1010 if (newLine
> lastLine
)
1020 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1023 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1029 wxClientDC
dc(this);
1031 dc
.SetFont(GetFont());
1033 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1035 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1037 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1038 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1039 // so we view the caret at the start of the line.
1040 bool caretLineStart
= false;
1041 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1043 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1044 wxRichTextRange lineRange
;
1046 lineRange
= thisLine
->GetAbsoluteRange();
1048 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1051 caretLineStart
= true;
1055 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1056 if (para
&& para
->GetRange().GetStart() == newPos
)
1061 long newSelEnd
= newPos
;
1063 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1067 SetCaretPosition(newPos
, caretLineStart
);
1069 SetDefaultStyleToCursorStyle();
1079 /// Move to the end of the paragraph
1080 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1082 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1085 long newPos
= para
->GetRange().GetEnd() - 1;
1086 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1090 SetCaretPosition(newPos
);
1092 SetDefaultStyleToCursorStyle();
1102 /// Move to the start of the paragraph
1103 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1105 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1108 long newPos
= para
->GetRange().GetStart() - 1;
1109 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1113 SetCaretPosition(newPos
);
1115 SetDefaultStyleToCursorStyle();
1125 /// Move to the end of the line
1126 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1128 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1132 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1133 long newPos
= lineRange
.GetEnd();
1134 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1138 SetCaretPosition(newPos
);
1140 SetDefaultStyleToCursorStyle();
1150 /// Move to the start of the line
1151 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1153 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1156 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1157 long newPos
= lineRange
.GetStart()-1;
1159 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1163 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1165 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1167 SetDefaultStyleToCursorStyle();
1177 /// Move to the start of the buffer
1178 bool wxRichTextCtrl::MoveHome(int flags
)
1180 if (m_caretPosition
!= -1)
1182 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1186 SetCaretPosition(-1);
1188 SetDefaultStyleToCursorStyle();
1198 /// Move to the end of the buffer
1199 bool wxRichTextCtrl::MoveEnd(int flags
)
1201 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1203 if (m_caretPosition
!= endPos
)
1205 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1209 SetCaretPosition(endPos
);
1211 SetDefaultStyleToCursorStyle();
1221 /// Move noPages pages up
1222 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1224 return PageDown(- noPages
, flags
);
1227 /// Move noPages pages down
1228 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1230 // Calculate which line occurs noPages * screen height further down.
1231 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1234 wxSize clientSize
= GetClientSize();
1235 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1237 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1240 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1241 long pos
= lineRange
.GetStart()-1;
1242 if (pos
!= m_caretPosition
)
1244 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1246 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1250 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1252 SetDefaultStyleToCursorStyle();
1264 // Finds the caret position for the next word
1265 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1267 long endPos
= GetBuffer().GetRange().GetEnd();
1271 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1273 // First skip current text to space
1274 while (i
< endPos
&& i
> -1)
1276 // i is in character, not caret positions
1277 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1278 if (text
!= wxT(" ") && !text
.empty())
1285 while (i
< endPos
&& i
> -1)
1287 // i is in character, not caret positions
1288 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1289 if (text
.empty()) // End of paragraph, or maybe an image
1290 return wxMax(-1, i
- 1);
1291 else if (text
== wxT(" ") || text
.empty())
1295 // Convert to caret position
1296 return wxMax(-1, i
- 1);
1305 long i
= m_caretPosition
;
1307 // First skip white space
1308 while (i
< endPos
&& i
> -1)
1310 // i is in character, not caret positions
1311 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1312 if (text
.empty()) // End of paragraph, or maybe an image
1314 else if (text
== wxT(" ") || text
.empty())
1319 // Next skip current text to space
1320 while (i
< endPos
&& i
> -1)
1322 // i is in character, not caret positions
1323 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1324 if (text
!= wxT(" ") /* && !text.empty() */)
1337 /// Move n words left
1338 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1340 long pos
= FindNextWordPosition(-1);
1341 if (pos
!= m_caretPosition
)
1343 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1345 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1349 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1351 SetDefaultStyleToCursorStyle();
1361 /// Move n words right
1362 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1364 long pos
= FindNextWordPosition(1);
1365 if (pos
!= m_caretPosition
)
1367 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1369 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1373 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1375 SetDefaultStyleToCursorStyle();
1386 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1388 // Only do sizing optimization for large buffers
1389 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1391 m_fullLayoutRequired
= true;
1392 m_fullLayoutTime
= wxGetLocalTimeMillis();
1393 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1394 LayoutContent(true /* onlyVisibleRect */);
1397 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1405 /// Idle-time processing
1406 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1408 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1410 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1412 m_fullLayoutRequired
= false;
1413 m_fullLayoutTime
= 0;
1414 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1415 ShowPosition(m_fullLayoutSavedPosition
);
1422 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1428 /// Set up scrollbars, e.g. after a resize
1429 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1434 if (GetBuffer().IsEmpty())
1436 SetScrollbars(0, 0, 0, 0, 0, 0);
1440 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1441 // of pixels. See e.g. wxVScrolledWindow for ideas.
1442 int pixelsPerUnit
= 5; // 10;
1443 wxSize clientSize
= GetClientSize();
1445 int maxHeight
= GetBuffer().GetCachedSize().y
;
1447 int unitsY
= maxHeight
/pixelsPerUnit
;
1449 int startX
= 0, startY
= 0;
1451 GetViewStart(& startX
, & startY
);
1453 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1454 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1456 // Move to previous scroll position if
1458 SetScrollbars(0, pixelsPerUnit
,
1460 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1463 /// Paint the background
1464 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1466 wxColour backgroundColour
= GetBackgroundColour();
1467 if (!backgroundColour
.Ok())
1468 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1470 // Clear the background
1471 dc
.SetBrush(wxBrush(backgroundColour
));
1472 dc
.SetPen(*wxTRANSPARENT_PEN
);
1473 wxRect
windowRect(GetClientSize());
1474 windowRect
.x
-= 2; windowRect
.y
-= 2;
1475 windowRect
.width
+= 4; windowRect
.height
+= 4;
1477 // We need to shift the rectangle to take into account
1478 // scrolling. Converting device to logical coordinates.
1479 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1480 dc
.DrawRectangle(windowRect
);
1483 /// Recreate buffer bitmap if necessary
1484 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1487 if (sz
== wxDefaultSize
)
1488 sz
= GetClientSize();
1490 if (sz
.x
< 1 || sz
.y
< 1)
1493 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1494 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1495 return m_bufferBitmap
.Ok();
1498 // ----------------------------------------------------------------------------
1499 // file IO functions
1500 // ----------------------------------------------------------------------------
1502 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1504 bool success
= GetBuffer().LoadFile(filename
, type
);
1506 m_filename
= filename
;
1509 SetInsertionPoint(0);
1512 SetupScrollbars(true);
1520 wxLogError(_("File couldn't be loaded."));
1526 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1528 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1529 if ( filenameToUse
.empty() )
1531 // what kind of message to give? is it an error or a program bug?
1532 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1537 if (GetBuffer().SaveFile(filenameToUse
, type
))
1539 m_filename
= filenameToUse
;
1547 wxLogError(_("The text couldn't be saved."));
1552 // ----------------------------------------------------------------------------
1553 // wxRichTextCtrl specific functionality
1554 // ----------------------------------------------------------------------------
1556 /// Add a new paragraph of text to the end of the buffer
1557 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1559 return GetBuffer().AddParagraph(text
);
1563 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1565 return GetBuffer().AddImage(image
);
1568 // ----------------------------------------------------------------------------
1569 // selection and ranges
1570 // ----------------------------------------------------------------------------
1572 void wxRichTextCtrl::SelectAll()
1574 SetSelection(0, GetLastPosition());
1575 m_selectionAnchor
= -1;
1579 void wxRichTextCtrl::SelectNone()
1581 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1582 SetSelection(-2, -2);
1583 m_selectionAnchor
= -2;
1586 wxString
wxRichTextCtrl::GetStringSelection() const
1589 GetSelection(&from
, &to
);
1591 return GetRange(from
, to
);
1594 // do the window-specific processing after processing the update event
1595 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1596 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1599 wxWindowBase::DoUpdateWindowUI(event
);
1602 if ( event
.GetSetText() )
1604 if ( event
.GetText() != GetValue() )
1605 SetValue(event
.GetText());
1608 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1610 // ----------------------------------------------------------------------------
1612 // ----------------------------------------------------------------------------
1614 wxTextCtrlHitTestResult
1615 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1617 // implement in terms of the other overload as the native ports typically
1618 // can get the position and not (x, y) pair directly (although wxUniv
1619 // directly gets x and y -- and so overrides this method as well)
1621 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1623 if ( rc
!= wxTE_HT_UNKNOWN
)
1625 PositionToXY(pos
, x
, y
);
1631 wxTextCtrlHitTestResult
1632 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1635 wxClientDC
dc((wxRichTextCtrl
*) this);
1636 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1638 // Buffer uses logical position (relative to start of buffer)
1640 wxPoint pt2
= GetLogicalPoint(pt
);
1642 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1646 case wxRICHTEXT_HITTEST_BEFORE
:
1647 return wxTE_HT_BEFORE
;
1649 case wxRICHTEXT_HITTEST_AFTER
:
1650 return wxTE_HT_BEYOND
;
1652 case wxRICHTEXT_HITTEST_ON
:
1653 return wxTE_HT_ON_TEXT
;
1656 return wxTE_HT_UNKNOWN
;
1659 // ----------------------------------------------------------------------------
1660 // set/get the controls text
1661 // ----------------------------------------------------------------------------
1663 wxString
wxRichTextCtrl::GetValue() const
1665 return GetBuffer().GetText();
1668 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1670 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1673 void wxRichTextCtrl::SetValue(const wxString
& value
)
1677 // if the text is long enough, it's faster to just set it instead of first
1678 // comparing it with the old one (chances are that it will be different
1679 // anyhow, this comparison is there to avoid flicker for small single-line
1680 // edit controls mostly)
1681 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1683 DoWriteText(value
, false /* not selection only */);
1685 // for compatibility, don't move the cursor when doing SetValue()
1686 SetInsertionPoint(0);
1690 // still send an event for consistency
1694 // we should reset the modified flag even if the value didn't really change
1696 // mark the control as being not dirty - we changed its text, not the
1701 void wxRichTextCtrl::WriteText(const wxString
& value
)
1706 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1708 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1711 void wxRichTextCtrl::AppendText(const wxString
& text
)
1713 SetInsertionPointEnd();
1718 /// Write an image at the current insertion point
1719 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1721 wxRichTextImageBlock imageBlock
;
1723 wxImage image2
= image
;
1724 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1725 return WriteImage(imageBlock
);
1730 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1732 wxRichTextImageBlock imageBlock
;
1735 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1736 return WriteImage(imageBlock
);
1741 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1743 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1746 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1750 wxRichTextImageBlock imageBlock
;
1752 wxImage image
= bitmap
.ConvertToImage();
1753 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1754 return WriteImage(imageBlock
);
1760 /// Insert a newline (actually paragraph) at the current insertion point.
1761 bool wxRichTextCtrl::Newline()
1763 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1767 // ----------------------------------------------------------------------------
1768 // Clipboard operations
1769 // ----------------------------------------------------------------------------
1771 void wxRichTextCtrl::Copy()
1775 wxRichTextRange range
= GetSelectionRange();
1776 GetBuffer().CopyToClipboard(range
);
1780 void wxRichTextCtrl::Cut()
1784 wxRichTextRange range
= GetSelectionRange();
1785 GetBuffer().CopyToClipboard(range
);
1787 DeleteSelectedContent();
1793 void wxRichTextCtrl::Paste()
1797 BeginBatchUndo(_("Paste"));
1799 long newPos
= m_caretPosition
;
1800 DeleteSelectedContent(& newPos
);
1802 GetBuffer().PasteFromClipboard(newPos
);
1808 void wxRichTextCtrl::DeleteSelection()
1810 if (CanDeleteSelection())
1812 DeleteSelectedContent();
1816 bool wxRichTextCtrl::HasSelection() const
1818 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1821 bool wxRichTextCtrl::CanCopy() const
1823 // Can copy if there's a selection
1824 return HasSelection();
1827 bool wxRichTextCtrl::CanCut() const
1829 return HasSelection() && IsEditable();
1832 bool wxRichTextCtrl::CanPaste() const
1834 if ( !IsEditable() )
1837 return GetBuffer().CanPasteFromClipboard();
1840 bool wxRichTextCtrl::CanDeleteSelection() const
1842 return HasSelection() && IsEditable();
1846 // ----------------------------------------------------------------------------
1848 // ----------------------------------------------------------------------------
1850 void wxRichTextCtrl::SetEditable(bool editable
)
1852 m_editable
= editable
;
1855 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1859 m_caretPosition
= pos
- 1;
1862 void wxRichTextCtrl::SetInsertionPointEnd()
1864 long pos
= GetLastPosition();
1865 SetInsertionPoint(pos
);
1868 long wxRichTextCtrl::GetInsertionPoint() const
1870 return m_caretPosition
+1;
1873 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1875 return GetBuffer().GetRange().GetEnd();
1878 // If the return values from and to are the same, there is no
1880 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1882 *from
= m_selectionRange
.GetStart();
1883 *to
= m_selectionRange
.GetEnd();
1886 bool wxRichTextCtrl::IsEditable() const
1891 // ----------------------------------------------------------------------------
1893 // ----------------------------------------------------------------------------
1895 void wxRichTextCtrl::SetSelection(long from
, long to
)
1897 // if from and to are both -1, it means (in wxWidgets) that all text should
1899 if ( (from
== -1) && (to
== -1) )
1902 to
= GetLastPosition();
1905 DoSetSelection(from
, to
);
1908 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1910 m_selectionAnchor
= from
;
1911 m_selectionRange
.SetRange(from
, to
);
1916 // ----------------------------------------------------------------------------
1918 // ----------------------------------------------------------------------------
1920 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1922 BeginBatchUndo(_("Replace"));
1924 DeleteSelectedContent();
1926 DoWriteText(value
, true /* selection only */);
1931 void wxRichTextCtrl::Remove(long from
, long to
)
1935 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1936 m_caretPosition
, // Current caret position
1937 from
, // New caret position
1945 bool wxRichTextCtrl::IsModified() const
1947 return m_buffer
.IsModified();
1950 void wxRichTextCtrl::MarkDirty()
1952 m_buffer
.Modify(true);
1955 void wxRichTextCtrl::DiscardEdits()
1957 m_buffer
.Modify(false);
1958 m_buffer
.GetCommandProcessor()->ClearCommands();
1961 int wxRichTextCtrl::GetNumberOfLines() const
1963 return GetBuffer().GetParagraphCount();
1966 // ----------------------------------------------------------------------------
1967 // Positions <-> coords
1968 // ----------------------------------------------------------------------------
1970 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1972 return GetBuffer().XYToPosition(x
, y
);
1975 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1977 return GetBuffer().PositionToXY(pos
, x
, y
);
1980 // ----------------------------------------------------------------------------
1982 // ----------------------------------------------------------------------------
1984 void wxRichTextCtrl::ShowPosition(long pos
)
1986 if (!IsPositionVisible(pos
))
1987 ScrollIntoView(pos
-1, WXK_DOWN
);
1990 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1992 return GetBuffer().GetParagraphLength(lineNo
);
1995 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1997 return GetBuffer().GetParagraphText(lineNo
);
2000 // ----------------------------------------------------------------------------
2002 // ----------------------------------------------------------------------------
2004 void wxRichTextCtrl::Undo()
2008 GetCommandProcessor()->Undo();
2012 void wxRichTextCtrl::Redo()
2016 GetCommandProcessor()->Redo();
2020 bool wxRichTextCtrl::CanUndo() const
2022 return GetCommandProcessor()->CanUndo();
2025 bool wxRichTextCtrl::CanRedo() const
2027 return GetCommandProcessor()->CanRedo();
2030 // ----------------------------------------------------------------------------
2031 // implementation details
2032 // ----------------------------------------------------------------------------
2034 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2036 SetValue(event
.GetString());
2037 GetEventHandler()->ProcessEvent(event
);
2040 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2042 // By default, load the first file into the text window.
2043 if (event
.GetNumberOfFiles() > 0)
2045 LoadFile(event
.GetFiles()[0]);
2049 // ----------------------------------------------------------------------------
2050 // text control event processing
2051 // ----------------------------------------------------------------------------
2053 bool wxRichTextCtrl::SendUpdateEvent()
2055 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2056 InitCommandEvent(event
);
2058 return GetEventHandler()->ProcessEvent(event
);
2061 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2063 event
.SetEventObject((wxControlBase
*)this); // const_cast
2065 switch ( m_clientDataType
)
2067 case wxClientData_Void
:
2068 event
.SetClientData(GetClientData());
2071 case wxClientData_Object
:
2072 event
.SetClientObject(GetClientObject());
2075 case wxClientData_None
:
2082 wxSize
wxRichTextCtrl::DoGetBestSize() const
2084 return wxSize(10, 10);
2087 // ----------------------------------------------------------------------------
2088 // standard handlers for standard edit menu events
2089 // ----------------------------------------------------------------------------
2091 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2096 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2101 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2106 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2111 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2116 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2121 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2123 event
.Enable( CanCut() );
2126 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2128 event
.Enable( CanCopy() );
2131 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2133 event
.Enable( CanDeleteSelection() );
2136 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2138 event
.Enable( CanPaste() );
2141 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2143 event
.Enable( CanUndo() );
2144 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2147 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2149 event
.Enable( CanRedo() );
2150 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2153 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2158 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2160 event
.Enable(GetLastPosition() > 0);
2163 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2167 m_contextMenu
= new wxMenu
;
2168 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2169 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2170 m_contextMenu
->AppendSeparator();
2171 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2172 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2173 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2174 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2175 m_contextMenu
->AppendSeparator();
2176 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2178 PopupMenu(m_contextMenu
);
2182 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2184 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2187 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2189 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), wxTextAttrEx(style
));
2192 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2194 return GetBuffer().SetStyle(range
, style
);
2197 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2199 return GetBuffer().SetDefaultStyle(style
);
2202 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2204 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2207 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2209 return GetBuffer().GetDefaultStyle();
2212 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2214 return GetBuffer().GetDefaultStyle();
2217 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
) const
2220 if (GetBuffer().GetStyle(position
, attr
))
2229 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2231 return GetBuffer().GetStyle(position
, style
);
2234 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2236 return GetBuffer().GetStyle(position
, style
);
2239 /// Set font, and also the buffer attributes
2240 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2242 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2243 wxControl::SetFont(font
);
2245 wxScrolledWindow::SetFont(font
);
2248 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2250 GetBuffer().SetBasicStyle(attr
);
2251 GetBuffer().SetDefaultStyle(attr
);
2256 /// Transform logical to physical
2257 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2260 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2265 /// Transform physical to logical
2266 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2269 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2274 /// Position the caret
2275 void wxRichTextCtrl::PositionCaret()
2280 //wxLogDebug(wxT("PositionCaret"));
2283 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2285 wxPoint originalPt
= caretRect
.GetPosition();
2286 wxPoint pt
= GetPhysicalPoint(originalPt
);
2287 if (GetCaret()->GetPosition() != pt
)
2289 GetCaret()->Move(pt
);
2290 GetCaret()->SetSize(caretRect
.GetSize());
2295 /// Get the caret height and position for the given character position
2296 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2298 wxClientDC
dc(this);
2299 dc
.SetFont(GetFont());
2306 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2308 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2315 /// Gets the line for the visible caret position. If the caret is
2316 /// shown at the very end of the line, it means the next character is actually
2317 /// on the following line. So let's get the line we're expecting to find
2318 /// if this is the case.
2319 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2321 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2322 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2325 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2326 if (caretPosition
== lineRange
.GetStart()-1 &&
2327 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2329 if (!m_caretAtLineStart
)
2330 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2337 /// Move the caret to the given character position
2338 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2340 if (GetBuffer().GetDirty())
2343 if (pos
<= GetBuffer().GetRange().GetEnd())
2345 SetCaretPosition(pos
, showAtLineStart
);
2355 /// Layout the buffer: which we must do before certain operations, such as
2356 /// setting the caret position.
2357 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2359 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2361 wxRect
availableSpace(GetClientSize());
2362 if (availableSpace
.width
== 0)
2363 availableSpace
.width
= 10;
2364 if (availableSpace
.height
== 0)
2365 availableSpace
.height
= 10;
2367 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2368 if (onlyVisibleRect
)
2370 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2371 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2374 wxClientDC
dc(this);
2375 dc
.SetFont(GetFont());
2379 GetBuffer().Defragment();
2380 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2381 GetBuffer().Layout(dc
, availableSpace
, flags
);
2382 GetBuffer().SetDirty(false);
2391 /// Is all of the selection bold?
2392 bool wxRichTextCtrl::IsSelectionBold() const
2396 wxRichTextAttr attr
;
2397 wxRichTextRange range
= GetSelectionRange();
2398 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2399 attr
.SetFontWeight(wxBOLD
);
2401 return HasCharacterAttributes(range
, attr
);
2405 // If no selection, then we need to combine current style with default style
2406 // to see what the effect would be if we started typing.
2407 wxRichTextAttr attr
;
2408 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2409 if (GetStyle(GetCaretPosition()+1, attr
))
2411 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2412 return attr
.GetFontWeight() == wxBOLD
;
2418 /// Is all of the selection italics?
2419 bool wxRichTextCtrl::IsSelectionItalics() const
2423 wxRichTextRange range
= GetSelectionRange();
2424 wxRichTextAttr attr
;
2425 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2426 attr
.SetFontStyle(wxITALIC
);
2428 return HasCharacterAttributes(range
, attr
);
2432 // If no selection, then we need to combine current style with default style
2433 // to see what the effect would be if we started typing.
2434 wxRichTextAttr attr
;
2435 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2436 if (GetStyle(GetCaretPosition()+1, attr
))
2438 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2439 return attr
.GetFontStyle() == wxITALIC
;
2445 /// Is all of the selection underlined?
2446 bool wxRichTextCtrl::IsSelectionUnderlined() const
2450 wxRichTextRange range
= GetSelectionRange();
2451 wxRichTextAttr attr
;
2452 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2453 attr
.SetFontUnderlined(true);
2455 return HasCharacterAttributes(range
, attr
);
2459 // If no selection, then we need to combine current style with default style
2460 // to see what the effect would be if we started typing.
2461 wxRichTextAttr attr
;
2462 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2463 if (GetStyle(GetCaretPosition()+1, attr
))
2465 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2466 return attr
.GetFontUnderlined();
2472 /// Apply bold to the selection
2473 bool wxRichTextCtrl::ApplyBoldToSelection()
2475 wxRichTextAttr attr
;
2476 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2477 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2480 return SetStyle(GetSelectionRange(), attr
);
2482 SetDefaultStyle(attr
);
2486 /// Apply italic to the selection
2487 bool wxRichTextCtrl::ApplyItalicToSelection()
2489 wxRichTextAttr attr
;
2490 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2491 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2494 return SetStyle(GetSelectionRange(), attr
);
2496 SetDefaultStyle(attr
);
2500 /// Apply underline to the selection
2501 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2503 wxRichTextAttr attr
;
2504 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2505 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2508 return SetStyle(GetSelectionRange(), attr
);
2510 SetDefaultStyle(attr
);
2514 /// Is all of the selection aligned according to the specified flag?
2515 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2519 wxRichTextRange range
= GetSelectionRange();
2520 wxRichTextAttr attr
;
2521 attr
.SetAlignment(alignment
);
2523 return HasParagraphAttributes(range
, attr
);
2527 // If no selection, then we need to get information from the current paragraph.
2528 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2530 return para
->GetAttributes().GetAlignment() == alignment
;
2535 /// Apply alignment to the selection
2536 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2538 wxRichTextAttr attr
;
2539 attr
.SetAlignment(alignment
);
2541 return SetStyle(GetSelectionRange(), attr
);
2544 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2546 return SetStyle(para
->GetRange(), attr
);
2551 /// Sets the default style to the style under the cursor
2552 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2555 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2557 if (GetStyle(GetCaretPosition(), attr
))
2559 SetDefaultStyle(attr
);
2566 /// Returns the first visible position in the current view
2567 long wxRichTextCtrl::GetFirstVisiblePosition() const
2569 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2571 return line
->GetAbsoluteRange().GetStart();