1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
27 #include "wx/textfile.h"
29 #include "wx/settings.h"
30 #include "wx/filename.h"
31 #include "wx/dcbuffer.h"
33 #include "wx/richtext/richtextctrl.h"
34 #include "wx/arrimpl.cpp"
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
44 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
45 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
47 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
50 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
52 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
53 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
55 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
57 EVT_PAINT(wxRichTextCtrl::OnPaint
)
58 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
59 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
60 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
61 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
62 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
63 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
64 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
65 EVT_CHAR(wxRichTextCtrl::OnChar
)
66 EVT_SIZE(wxRichTextCtrl::OnSize
)
67 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
68 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
69 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
71 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
72 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
74 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
75 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
77 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
78 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
80 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
81 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
83 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
84 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
86 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
87 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
89 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
90 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
97 wxRichTextCtrl::wxRichTextCtrl()
98 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
99 : wxScrollHelper(this)
105 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
106 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
107 : wxScrollHelper(this)
111 Create(parent
, id
, pos
, size
, style
);
115 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
117 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
118 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
122 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
129 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
132 GetBuffer().SetRichTextCtrl(this);
134 wxTextAttrEx attributes
;
135 attributes
.SetFont(GetFont());
136 attributes
.SetTextColour(*wxBLACK
);
137 attributes
.SetBackgroundColour(*wxWHITE
);
138 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
139 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
141 SetDefaultStyle(attributes
);
142 SetBasicStyle(attributes
);
144 SetBackgroundColour(*wxWHITE
);
145 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
147 // Tell the sizers to use the given or best size
148 SetBestFittingSize(size
);
151 RecreateBuffer(size
);
153 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
158 SetCursor(wxCursor(wxCURSOR_IBEAM
));
163 wxRichTextCtrl::~wxRichTextCtrl()
165 delete m_contextMenu
;
168 /// Member initialisation
169 void wxRichTextCtrl::Init()
172 m_contextMenu
= NULL
;
174 m_caretPosition
= -1;
175 m_selectionRange
.SetRange(-2, -2);
176 m_selectionAnchor
= -2;
178 m_caretAtLineStart
= false;
182 /// Call Freeze to prevent refresh
183 void wxRichTextCtrl::Freeze()
188 /// Call Thaw to refresh
189 void wxRichTextCtrl::Thaw(bool refresh
)
193 if (m_freezeCount
== 0 && refresh
)
201 void wxRichTextCtrl::Clear()
204 m_buffer
.SetDirty(true);
205 m_caretPosition
= -1;
206 m_caretAtLineStart
= false;
207 m_selectionRange
.SetRange(-2, -2);
209 if (m_freezeCount
== 0)
218 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
220 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
224 if (m_freezeCount
> 0)
227 dc
.SetFont(GetFont());
229 // Paint the background
232 wxRegion dirtyRegion
= GetUpdateRegion();
234 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
235 wxRect
availableSpace(GetClientSize());
236 if (GetBuffer().GetDirty())
238 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
239 GetBuffer().SetDirty(false);
244 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
247 // Empty implementation, to prevent flicker
248 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
252 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
258 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
265 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
271 dc
.SetFont(GetFont());
274 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
276 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
278 m_dragStart
= event
.GetLogicalPosition(dc
);
284 bool caretAtLineStart
= false;
286 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
288 // If we're at the start of a line (but not first in para)
289 // then we should keep the caret showing at the start of the line
290 // by showing the m_caretAtLineStart flag.
291 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
292 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
294 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
295 caretAtLineStart
= true;
299 MoveCaret(position
, caretAtLineStart
);
300 SetDefaultStyleToCursorStyle();
303 wxWindow
* p
= GetParent();
304 while (p
&& !p
->IsKindOf(CLASSINFO(wxFrame
)))
307 wxFrame
* frame
= wxDynamicCast(p
, wxFrame
);
310 wxString msg
= wxString::Format(wxT("Found position %ld"), position
);
311 frame
->SetStatusText(msg
, 1);
320 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
325 if (GetCapture() == this)
331 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
333 if (!event
.Dragging())
341 dc
.SetFont(GetFont());
344 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
345 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
347 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
349 // TODO: test closeness
351 bool caretAtLineStart
= false;
353 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
355 // If we're at the start of a line (but not first in para)
356 // then we should keep the caret showing at the start of the line
357 // by showing the m_caretAtLineStart flag.
358 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
359 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
361 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
362 caretAtLineStart
= true;
366 if (m_caretPosition
!= position
)
368 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
370 MoveCaret(position
, caretAtLineStart
);
371 SetDefaultStyleToCursorStyle();
380 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
386 /// Left-double-click
387 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
393 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
399 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
402 if (event
.ControlDown())
403 flags
|= wxRICHTEXT_CTRL_DOWN
;
404 if (event
.ShiftDown())
405 flags
|= wxRICHTEXT_SHIFT_DOWN
;
407 flags
|= wxRICHTEXT_ALT_DOWN
;
409 if (event
.GetKeyCode() == WXK_LEFT
||
410 event
.GetKeyCode() == WXK_RIGHT
||
411 event
.GetKeyCode() == WXK_UP
||
412 event
.GetKeyCode() == WXK_DOWN
||
413 event
.GetKeyCode() == WXK_HOME
||
414 event
.GetKeyCode() == WXK_PAGEUP
||
415 event
.GetKeyCode() == WXK_PAGEDOWN
||
416 event
.GetKeyCode() == WXK_PRIOR
||
417 event
.GetKeyCode() == WXK_NEXT
||
418 event
.GetKeyCode() == WXK_END
)
420 Navigate(event
.GetKeyCode(), flags
);
422 else if (event
.GetKeyCode() == WXK_RETURN
)
424 BeginBatchUndo(_("Insert Text"));
426 long newPos
= m_caretPosition
;
428 DeleteSelectedContent(& newPos
);
430 GetBuffer().InsertNewlineWithUndo(newPos
+1, this);
432 wxRichTextEvent
cmdEvent(
433 wxEVT_COMMAND_RICHTEXT_RETURN
,
435 cmdEvent
.SetEventObject(this);
436 cmdEvent
.SetFlags(flags
);
437 GetEventHandler()->ProcessEvent(cmdEvent
);
440 SetDefaultStyleToCursorStyle();
442 else if (event
.GetKeyCode() == WXK_BACK
)
444 BeginBatchUndo(_("Delete Text"));
446 // Submit range in character positions, which are greater than caret positions,
447 // so subtract 1 for deleted character and add 1 for conversion to character position.
448 if (m_caretPosition
> -1 && !HasSelection())
450 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
451 m_caretPosition
, // Current caret position
452 m_caretPosition
-1, // New caret position
456 DeleteSelectedContent();
460 // Shouldn't this be in Do()?
461 if (GetLastPosition() == -1)
465 m_caretPosition
= -1;
467 SetDefaultStyleToCursorStyle();
471 else if (event
.GetKeyCode() == WXK_DELETE
)
473 BeginBatchUndo(_("Delete Text"));
475 // Submit range in character positions, which are greater than caret positions,
476 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
478 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
479 m_caretPosition
, // Current caret position
480 m_caretPosition
+1, // New caret position
484 DeleteSelectedContent();
488 // Shouldn't this be in Do()?
489 if (GetLastPosition() == -1)
493 m_caretPosition
= -1;
495 SetDefaultStyleToCursorStyle();
500 BeginBatchUndo(_("Insert Text"));
502 long newPos
= m_caretPosition
;
503 DeleteSelectedContent(& newPos
);
505 wxString str
= (wxChar
) event
.GetKeyCode();
506 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this);
510 SetDefaultStyleToCursorStyle();
518 /// Delete content if there is a selection, e.g. when pressing a key.
519 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
523 long pos
= m_selectionRange
.GetStart();
524 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
525 m_caretPosition
, // Current caret position
526 pos
, // New caret position
528 m_selectionRange
.SetRange(-2, -2);
538 /// Keyboard navigation
542 Left: left one character
543 Right: right one character
546 Ctrl-Left: left one word
547 Ctrl-Right: right one word
548 Ctrl-Up: previous paragraph start
549 Ctrl-Down: next start of paragraph
552 Ctrl-Home: start of document
553 Ctrl-End: end of document
555 Page-Down: Down a screen
559 Ctrl-Alt-PgUp: Start of window
560 Ctrl-Alt-PgDn: End of window
561 F8: Start selection mode
562 Esc: End selection mode
564 Adding Shift does the above but starts/extends selection.
569 bool wxRichTextCtrl::Navigate(int keyCode
, int flags
)
571 bool success
= false;
574 if (keyCode
== WXK_RIGHT
)
576 if (flags
& wxRICHTEXT_CTRL_DOWN
)
577 success
= WordRight(1, flags
);
579 success
= MoveRight(1, flags
);
581 else if (keyCode
== WXK_LEFT
)
583 if (flags
& wxRICHTEXT_CTRL_DOWN
)
584 success
= WordLeft(1, flags
);
586 success
= MoveLeft(1, flags
);
588 else if (keyCode
== WXK_UP
)
590 if (flags
& wxRICHTEXT_CTRL_DOWN
)
591 success
= MoveToParagraphStart(flags
);
593 success
= MoveUp(1, flags
);
595 else if (keyCode
== WXK_DOWN
)
597 if (flags
& wxRICHTEXT_CTRL_DOWN
)
598 success
= MoveToParagraphEnd(flags
);
600 success
= MoveDown(1, flags
);
602 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_PRIOR
)
604 success
= PageUp(1, flags
);
606 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NEXT
)
608 success
= PageDown(1, flags
);
610 else if (keyCode
== WXK_HOME
)
612 if (flags
& wxRICHTEXT_CTRL_DOWN
)
613 success
= MoveHome(flags
);
615 success
= MoveToLineStart(flags
);
617 else if (keyCode
== WXK_END
)
619 if (flags
& wxRICHTEXT_CTRL_DOWN
)
620 success
= MoveEnd(flags
);
622 success
= MoveToLineEnd(flags
);
627 ScrollIntoView(m_caretPosition
, keyCode
);
628 SetDefaultStyleToCursorStyle();
631 // Only refresh if something changed
637 /// Extend the selection. Selections are in caret positions.
638 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
640 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
642 // If not currently selecting, start selecting
643 if (m_selectionRange
.GetStart() == -2)
645 m_selectionAnchor
= oldPos
;
648 m_selectionRange
.SetRange(newPos
+1, oldPos
);
650 m_selectionRange
.SetRange(oldPos
+1, newPos
);
654 // Always ensure that the selection range start is greater than
656 if (newPos
> m_selectionAnchor
)
657 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
659 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
662 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
664 wxLogDebug(wxT("Strange selection range"));
673 /// Scroll into view, returning true if we scrolled.
674 /// This takes a _caret_ position.
675 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
677 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
683 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
686 GetViewStart(& startX
, & startY
);
688 startY
= startY
* ppuY
;
691 GetVirtualSize(& sx
, & sy
);
696 wxRect rect
= line
->GetRect();
698 bool scrolled
= false;
700 wxSize clientSize
= GetClientSize();
703 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_RIGHT
|| keyCode
== WXK_END
|| keyCode
== WXK_NEXT
|| keyCode
== WXK_PAGEDOWN
)
705 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
707 // Make it scroll so this item is at the bottom
709 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
710 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
712 else if (rect
.y
< startY
)
714 // Make it scroll so this item is at the top
717 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
722 else if (keyCode
== WXK_UP
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_HOME
|| keyCode
== WXK_PRIOR
|| keyCode
== WXK_PAGEUP
)
726 // Make it scroll so this item is at the top
729 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
731 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
733 // Make it scroll so this item is at the bottom
735 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
736 SetScrollbars(ppuX
, ppuY
, sx
, sy
, 0, (int) (0.5 + y
/ppuY
));
745 /// Is the given position visible on the screen?
746 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
748 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
754 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
757 GetViewStart(& startX
, & startY
);
759 startY
= startY
* ppuY
;
762 GetVirtualSize(& sx
, & sy
);
767 wxRect rect
= line
->GetRect();
769 wxSize clientSize
= GetClientSize();
771 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
774 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
776 m_caretPosition
= position
;
777 m_caretAtLineStart
= showAtLineStart
;
780 /// Move caret one visual step forward: this may mean setting a flag
781 /// and keeping the same position if we're going from the end of one line
782 /// to the start of the next, which may be the exact same caret position.
783 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
785 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
787 // Only do the check if we're not at the end of the paragraph (where things work OK
789 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
791 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
795 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
797 // We're at the end of a line. See whether we need to
798 // stay at the same actual caret position but change visual
800 if (oldPosition
== lineRange
.GetEnd())
802 if (m_caretAtLineStart
)
804 // We're already at the start of the line, so actually move on now.
805 m_caretPosition
= oldPosition
+ 1;
806 m_caretAtLineStart
= false;
810 // We're showing at the end of the line, so keep to
811 // the same position but indicate that we're to show
812 // at the start of the next line.
813 m_caretPosition
= oldPosition
;
814 m_caretAtLineStart
= true;
816 SetDefaultStyleToCursorStyle();
822 SetDefaultStyleToCursorStyle();
825 /// Move caret one visual step backward: this may mean setting a flag
826 /// and keeping the same position if we're going from the end of one line
827 /// to the start of the next, which may be the exact same caret position.
828 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
830 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
832 // Only do the check if we're not at the start of the paragraph (where things work OK
834 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
836 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
840 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
842 // We're at the start of a line. See whether we need to
843 // stay at the same actual caret position but change visual
845 if (oldPosition
== lineRange
.GetStart())
847 m_caretPosition
= oldPosition
-1;
848 m_caretAtLineStart
= true;
851 else if (oldPosition
== lineRange
.GetEnd())
853 if (m_caretAtLineStart
)
855 // We're at the start of the line, so keep the same caret position
856 // but clear the start-of-line flag.
857 m_caretPosition
= oldPosition
;
858 m_caretAtLineStart
= false;
862 // We're showing at the end of the line, so go back
863 // to the previous character position.
864 m_caretPosition
= oldPosition
- 1;
866 SetDefaultStyleToCursorStyle();
872 SetDefaultStyleToCursorStyle();
876 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
878 long endPos
= GetBuffer().GetRange().GetEnd();
880 if (m_caretPosition
+ noPositions
< endPos
)
882 long oldPos
= m_caretPosition
;
883 long newPos
= m_caretPosition
+ noPositions
;
885 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
889 // Determine by looking at oldPos and m_caretPosition whether
890 // we moved from the end of a line to the start of the next line, in which case
891 // we want to adjust the caret position such that it is positioned at the
892 // start of the next line, rather than jumping past the first character of the
894 if (noPositions
== 1 && !extendSel
)
895 MoveCaretForward(oldPos
);
897 SetCaretPosition(newPos
);
900 SetDefaultStyleToCursorStyle();
903 Refresh(); // TODO: optimize so that if we didn't change the selection, we don't refresh
911 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
915 if (m_caretPosition
> startPos
- noPositions
+ 1)
917 long oldPos
= m_caretPosition
;
918 long newPos
= m_caretPosition
- noPositions
;
919 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
923 if (noPositions
== 1 && !extendSel
)
924 MoveCaretBack(oldPos
);
926 SetCaretPosition(newPos
);
929 SetDefaultStyleToCursorStyle();
940 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
942 return MoveDown(- noLines
, flags
);
946 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
948 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
949 wxPoint pt
= GetCaret()->GetPosition();
950 long newLine
= lineNumber
+ noLines
;
952 if (lineNumber
!= -1)
956 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
958 if (newLine
> lastLine
)
968 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
971 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
979 dc
.SetFont(GetFont());
981 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
983 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
985 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
986 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
987 // so we view the caret at the start of the line.
988 bool caretLineStart
= false;
989 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
991 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
992 wxRichTextRange lineRange
;
994 lineRange
= thisLine
->GetAbsoluteRange();
996 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
999 caretLineStart
= true;
1003 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1004 if (para
&& para
->GetRange().GetStart() == newPos
)
1009 long newSelEnd
= newPos
;
1011 if (!ExtendSelection(m_caretPosition
, newSelEnd
, flags
))
1014 SetCaretPosition(newPos
, caretLineStart
);
1016 SetDefaultStyleToCursorStyle();
1026 /// Move to the end of the paragraph
1027 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1029 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1032 long newPos
= para
->GetRange().GetEnd() - 1;
1033 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1036 SetCaretPosition(newPos
);
1038 SetDefaultStyleToCursorStyle();
1048 /// Move to the start of the paragraph
1049 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1051 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1054 long newPos
= para
->GetRange().GetStart() - 1;
1055 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1058 SetCaretPosition(newPos
);
1060 SetDefaultStyleToCursorStyle();
1070 /// Move to the end of the line
1071 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1073 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1077 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1078 long newPos
= lineRange
.GetEnd();
1079 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1082 SetCaretPosition(newPos
);
1084 SetDefaultStyleToCursorStyle();
1094 /// Move to the start of the line
1095 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1097 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1100 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1101 long newPos
= lineRange
.GetStart()-1;
1103 if (!ExtendSelection(m_caretPosition
, newPos
, flags
))
1106 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1108 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1110 SetDefaultStyleToCursorStyle();
1120 /// Move to the start of the buffer
1121 bool wxRichTextCtrl::MoveHome(int flags
)
1123 if (m_caretPosition
!= -1)
1125 if (!ExtendSelection(m_caretPosition
, -1, flags
))
1128 SetCaretPosition(-1);
1130 SetDefaultStyleToCursorStyle();
1140 /// Move to the end of the buffer
1141 bool wxRichTextCtrl::MoveEnd(int flags
)
1143 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1145 if (m_caretPosition
!= endPos
)
1147 if (!ExtendSelection(m_caretPosition
, endPos
, flags
))
1150 SetCaretPosition(endPos
);
1152 SetDefaultStyleToCursorStyle();
1162 /// Move noPages pages up
1163 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1165 return PageDown(- noPages
, flags
);
1168 /// Move noPages pages down
1169 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1171 // Calculate which line occurs noPages * screen height further down.
1172 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1175 wxSize clientSize
= GetClientSize();
1176 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1178 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1181 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1182 long pos
= lineRange
.GetStart()-1;
1183 if (pos
!= m_caretPosition
)
1185 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1187 if (!ExtendSelection(m_caretPosition
, pos
, flags
))
1190 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1192 SetDefaultStyleToCursorStyle();
1204 // Finds the caret position for the next word
1205 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1207 long endPos
= GetBuffer().GetRange().GetEnd();
1211 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1213 // First skip current text to space
1214 while (i
< endPos
&& i
> -1)
1216 // i is in character, not caret positions
1217 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1218 if (text
!= wxT(" ") && !text
.empty())
1225 while (i
< endPos
&& i
> -1)
1227 // i is in character, not caret positions
1228 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1229 if (text
.empty()) // End of paragraph, or maybe an image
1230 return wxMax(-1, i
- 1);
1231 else if (text
== wxT(" ") || text
.empty())
1235 // Convert to caret position
1236 return wxMax(-1, i
- 1);
1245 long i
= m_caretPosition
;
1247 // First skip white space
1248 while (i
< endPos
&& i
> -1)
1250 // i is in character, not caret positions
1251 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1252 if (text
.empty()) // End of paragraph, or maybe an image
1254 else if (text
== wxT(" ") || text
.empty())
1259 // Next skip current text to space
1260 while (i
< endPos
&& i
> -1)
1262 // i is in character, not caret positions
1263 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1264 if (text
!= wxT(" ") /* && !text.empty() */)
1277 /// Move n words left
1278 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1280 long pos
= FindNextWordPosition(-1);
1281 if (pos
!= m_caretPosition
)
1283 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1285 if (!ExtendSelection(m_caretPosition
, pos
, flags
))
1288 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1290 SetDefaultStyleToCursorStyle();
1300 /// Move n words right
1301 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1303 long pos
= FindNextWordPosition(1);
1304 if (pos
!= m_caretPosition
)
1306 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1308 if (!ExtendSelection(m_caretPosition
, pos
, flags
))
1311 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1313 SetDefaultStyleToCursorStyle();
1324 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1326 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1333 /// Set up scrollbars, e.g. after a resize
1334 void wxRichTextCtrl::SetupScrollbars()
1339 if (GetBuffer().IsEmpty())
1341 SetScrollbars(0, 0, 0, 0, 0, 0);
1345 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1346 // of pixels. See e.g. wxVScrolledWindow for ideas.
1347 int pixelsPerUnit
= 5; // 10;
1348 wxSize clientSize
= GetClientSize();
1350 int maxHeight
= GetBuffer().GetCachedSize().y
;
1352 int unitsY
= maxHeight
/pixelsPerUnit
;
1355 GetViewStart(& startX
, & startY
);
1357 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1358 int maxPositionY
= (wxMax(maxHeight
- clientSize
.y
, 0))/pixelsPerUnit
;
1360 // Move to previous scroll position if
1362 SetScrollbars(0, pixelsPerUnit
,
1364 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1367 /// Paint the background
1368 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1370 wxColour backgroundColour
= GetBackgroundColour();
1371 if (!backgroundColour
.Ok())
1372 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1374 // Clear the background
1375 dc
.SetBrush(wxBrush(backgroundColour
));
1376 dc
.SetPen(*wxTRANSPARENT_PEN
);
1377 wxRect
windowRect(GetClientSize());
1378 windowRect
.x
-= 2; windowRect
.y
-= 2;
1379 windowRect
.width
+= 4; windowRect
.height
+= 4;
1381 // We need to shift the rectangle to take into account
1382 // scrolling. Converting device to logical coordinates.
1383 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1384 dc
.DrawRectangle(windowRect
);
1387 /// Recreate buffer bitmap if necessary
1388 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1391 if (sz
== wxDefaultSize
)
1392 sz
= GetClientSize();
1394 if (sz
.x
< 1 || sz
.y
< 1)
1397 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1398 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1399 return m_bufferBitmap
.Ok();
1402 // ----------------------------------------------------------------------------
1403 // file IO functions
1404 // ----------------------------------------------------------------------------
1406 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int type
)
1408 bool success
= GetBuffer().LoadFile(filename
, type
);
1410 m_filename
= filename
;
1413 SetInsertionPoint(0);
1423 wxLogError(_("File couldn't be loaded."));
1429 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int type
)
1431 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1432 if ( filenameToUse
.empty() )
1434 // what kind of message to give? is it an error or a program bug?
1435 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1440 if (GetBuffer().SaveFile(filenameToUse
, type
))
1442 m_filename
= filenameToUse
;
1450 wxLogError(_("The text couldn't be saved."));
1455 // ----------------------------------------------------------------------------
1456 // wxRichTextCtrl specific functionality
1457 // ----------------------------------------------------------------------------
1459 /// Add a new paragraph of text to the end of the buffer
1460 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1462 return GetBuffer().AddParagraph(text
);
1466 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1468 return GetBuffer().AddImage(image
);
1471 // ----------------------------------------------------------------------------
1472 // selection and ranges
1473 // ----------------------------------------------------------------------------
1475 void wxRichTextCtrl::SelectAll()
1477 SetSelection(0, GetLastPosition());
1478 m_selectionAnchor
= -1;
1482 void wxRichTextCtrl::SelectNone()
1484 SetSelection(-2, -2);
1485 m_selectionAnchor
= -2;
1488 wxString
wxRichTextCtrl::GetStringSelection() const
1491 GetSelection(&from
, &to
);
1493 return GetRange(from
, to
);
1496 // do the window-specific processing after processing the update event
1497 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1499 if ( event
.GetSetEnabled() )
1500 Enable(event
.GetEnabled());
1502 if ( event
.GetSetText() )
1504 if ( event
.GetText() != GetValue() )
1505 SetValue(event
.GetText());
1509 // ----------------------------------------------------------------------------
1511 // ----------------------------------------------------------------------------
1513 wxTextCtrlHitTestResult
1514 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1516 // implement in terms of the other overload as the native ports typically
1517 // can get the position and not (x, y) pair directly (although wxUniv
1518 // directly gets x and y -- and so overrides this method as well)
1520 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1522 if ( rc
!= wxTE_HT_UNKNOWN
)
1524 PositionToXY(pos
, x
, y
);
1530 wxTextCtrlHitTestResult
1531 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1534 wxClientDC
dc((wxRichTextCtrl
*) this);
1535 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1537 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt
, *pos
);
1538 if (hit
== wxRICHTEXT_HITTEST_BEFORE
)
1539 return wxTE_HT_BEFORE
;
1540 else if (hit
== wxRICHTEXT_HITTEST_AFTER
)
1541 return wxTE_HT_BEYOND
;
1542 else if (hit
== wxRICHTEXT_HITTEST_ON
)
1543 return wxTE_HT_ON_TEXT
;
1545 return wxTE_HT_UNKNOWN
;
1548 // ----------------------------------------------------------------------------
1549 // set/get the controls text
1550 // ----------------------------------------------------------------------------
1552 wxString
wxRichTextCtrl::GetValue() const
1554 return GetBuffer().GetText();
1557 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1559 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
));
1562 void wxRichTextCtrl::SetValue(const wxString
& value
)
1566 // if the text is long enough, it's faster to just set it instead of first
1567 // comparing it with the old one (chances are that it will be different
1568 // anyhow, this comparison is there to avoid flicker for small single-line
1569 // edit controls mostly)
1570 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1572 DoWriteText(value
, false /* not selection only */);
1574 // for compatibility, don't move the cursor when doing SetValue()
1575 SetInsertionPoint(0);
1579 // still send an event for consistency
1583 // we should reset the modified flag even if the value didn't really change
1585 // mark the control as being not dirty - we changed its text, not the
1590 void wxRichTextCtrl::WriteText(const wxString
& value
)
1595 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1597 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, value
, this);
1600 void wxRichTextCtrl::AppendText(const wxString
& text
)
1602 SetInsertionPointEnd();
1607 /// Write an image at the current insertion point
1608 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1610 wxRichTextImageBlock imageBlock
;
1612 wxImage image2
= image
;
1613 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1614 return WriteImage(imageBlock
);
1619 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1621 wxRichTextImageBlock imageBlock
;
1624 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1625 return WriteImage(imageBlock
);
1630 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1632 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1635 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1639 wxRichTextImageBlock imageBlock
;
1641 wxImage image
= bitmap
.ConvertToImage();
1642 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1643 return WriteImage(imageBlock
);
1650 /// Insert a newline (actually paragraph) at the current insertion point.
1651 bool wxRichTextCtrl::Newline()
1653 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1657 // ----------------------------------------------------------------------------
1658 // Clipboard operations
1659 // ----------------------------------------------------------------------------
1661 void wxRichTextCtrl::Copy()
1665 wxRichTextRange range
= GetSelectionRange();
1666 GetBuffer().CopyToClipboard(range
);
1670 void wxRichTextCtrl::Cut()
1674 wxRichTextRange range
= GetSelectionRange();
1675 GetBuffer().CopyToClipboard(range
);
1677 DeleteSelectedContent();
1683 void wxRichTextCtrl::Paste()
1687 BeginBatchUndo(_("Paste"));
1689 long newPos
= m_caretPosition
;
1690 DeleteSelectedContent(& newPos
);
1692 GetBuffer().PasteFromClipboard(newPos
);
1698 void wxRichTextCtrl::DeleteSelection()
1700 if (CanDeleteSelection())
1702 DeleteSelectedContent();
1706 bool wxRichTextCtrl::HasSelection() const
1708 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1711 bool wxRichTextCtrl::CanCopy() const
1713 // Can copy if there's a selection
1714 return HasSelection();
1717 bool wxRichTextCtrl::CanCut() const
1719 return HasSelection() && IsEditable();
1722 bool wxRichTextCtrl::CanPaste() const
1724 if ( !IsEditable() )
1727 return GetBuffer().CanPasteFromClipboard();
1730 bool wxRichTextCtrl::CanDeleteSelection() const
1732 return HasSelection() && IsEditable();
1736 // ----------------------------------------------------------------------------
1738 // ----------------------------------------------------------------------------
1740 void wxRichTextCtrl::SetEditable(bool editable
)
1742 m_editable
= editable
;
1745 void wxRichTextCtrl::SetInsertionPoint(long pos
)
1749 m_caretPosition
= pos
- 1;
1752 void wxRichTextCtrl::SetInsertionPointEnd()
1754 long pos
= GetLastPosition();
1755 SetInsertionPoint(pos
);
1758 long wxRichTextCtrl::GetInsertionPoint() const
1760 return m_caretPosition
+1;
1763 wxTextPos
wxRichTextCtrl::GetLastPosition() const
1765 return GetBuffer().GetRange().GetEnd();
1768 // If the return values from and to are the same, there is no
1770 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
1772 *from
= m_selectionRange
.GetStart();
1773 *to
= m_selectionRange
.GetEnd();
1776 bool wxRichTextCtrl::IsEditable() const
1781 // ----------------------------------------------------------------------------
1783 // ----------------------------------------------------------------------------
1785 void wxRichTextCtrl::SetSelection(long from
, long to
)
1787 // if from and to are both -1, it means (in wxWidgets) that all text should
1789 if ( (from
== -1) && (to
== -1) )
1792 to
= GetLastPosition();
1795 DoSetSelection(from
, to
);
1798 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
1800 m_selectionAnchor
= from
;
1801 m_selectionRange
.SetRange(from
, to
);
1807 // ----------------------------------------------------------------------------
1809 // ----------------------------------------------------------------------------
1811 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
1813 BeginBatchUndo(_("Replace"));
1815 DeleteSelectedContent();
1817 DoWriteText(value
, true /* selection only */);
1822 void wxRichTextCtrl::Remove(long from
, long to
)
1826 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
1827 m_caretPosition
, // Current caret position
1828 from
, // New caret position
1836 bool wxRichTextCtrl::IsModified() const
1838 return m_buffer
.IsModified();
1841 void wxRichTextCtrl::MarkDirty()
1843 m_buffer
.Modify(true);
1846 void wxRichTextCtrl::DiscardEdits()
1848 m_buffer
.Modify(false);
1849 m_buffer
.GetCommandProcessor()->ClearCommands();
1852 int wxRichTextCtrl::GetNumberOfLines() const
1854 return GetBuffer().GetParagraphCount();
1857 // ----------------------------------------------------------------------------
1858 // Positions <-> coords
1859 // ----------------------------------------------------------------------------
1861 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
1863 return GetBuffer().XYToPosition(x
, y
);
1866 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1868 return GetBuffer().PositionToXY(pos
, x
, y
);
1871 // ----------------------------------------------------------------------------
1873 // ----------------------------------------------------------------------------
1875 void wxRichTextCtrl::ShowPosition(long pos
)
1877 if (!IsPositionVisible(pos
))
1878 ScrollIntoView(pos
-1, WXK_DOWN
);
1881 int wxRichTextCtrl::GetLineLength(long lineNo
) const
1883 return GetBuffer().GetParagraphLength(lineNo
);
1886 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
1888 return GetBuffer().GetParagraphText(lineNo
);
1891 // ----------------------------------------------------------------------------
1893 // ----------------------------------------------------------------------------
1895 void wxRichTextCtrl::Undo()
1899 GetCommandProcessor()->Undo();
1903 void wxRichTextCtrl::Redo()
1907 GetCommandProcessor()->Redo();
1911 bool wxRichTextCtrl::CanUndo() const
1913 return GetCommandProcessor()->CanUndo();
1916 bool wxRichTextCtrl::CanRedo() const
1918 return GetCommandProcessor()->CanRedo();
1921 // ----------------------------------------------------------------------------
1922 // implemenation details
1923 // ----------------------------------------------------------------------------
1925 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
1927 SetValue(event
.GetString());
1928 GetEventHandler()->ProcessEvent(event
);
1931 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1933 // By default, load the first file into the text window.
1934 if (event
.GetNumberOfFiles() > 0)
1936 LoadFile(event
.GetFiles()[0]);
1940 // ----------------------------------------------------------------------------
1941 // text control event processing
1942 // ----------------------------------------------------------------------------
1944 bool wxRichTextCtrl::SendUpdateEvent()
1946 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1947 InitCommandEvent(event
);
1949 return GetEventHandler()->ProcessEvent(event
);
1952 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
1954 event
.SetEventObject((wxControlBase
*)this); // const_cast
1956 switch ( m_clientDataType
)
1958 case wxClientData_Void
:
1959 event
.SetClientData(GetClientData());
1962 case wxClientData_Object
:
1963 event
.SetClientObject(GetClientObject());
1966 case wxClientData_None
:
1973 wxSize
wxRichTextCtrl::DoGetBestSize() const
1975 return wxSize(10, 10);
1978 // ----------------------------------------------------------------------------
1979 // standard handlers for standard edit menu events
1980 // ----------------------------------------------------------------------------
1982 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1987 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
1992 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1997 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2002 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2007 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2012 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2014 event
.Enable( CanCut() );
2017 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2019 event
.Enable( CanCopy() );
2022 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2024 event
.Enable( CanDeleteSelection() );
2027 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2029 event
.Enable( CanPaste() );
2032 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2034 event
.Enable( CanUndo() );
2035 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2038 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2040 event
.Enable( CanRedo() );
2041 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2044 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2049 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2051 event
.Enable(GetLastPosition() > 0);
2054 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2058 m_contextMenu
= new wxMenu
;
2059 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2060 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2061 m_contextMenu
->AppendSeparator();
2062 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2063 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2064 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2065 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2066 m_contextMenu
->AppendSeparator();
2067 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2069 PopupMenu(m_contextMenu
);
2073 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2075 return GetBuffer().SetStyle(wxRichTextRange(start
, end
), style
);
2078 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2080 return GetBuffer().SetStyle(range
, style
);
2083 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2085 return GetBuffer().SetDefaultStyle(style
);
2088 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2090 return GetBuffer().GetDefaultStyle();
2093 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) const
2095 return GetBuffer().GetStyle(position
, style
);
2098 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) const
2100 return GetBuffer().GetStyle(position
, style
);
2103 /// Set font, and also the buffer attributes
2104 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2106 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2107 wxControl::SetFont(font
);
2109 wxScrolledWindow::SetFont(font
);
2112 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2114 GetBuffer().SetBasicStyle(attr
);
2115 GetBuffer().SetDefaultStyle(attr
);
2120 /// Transform logical to physical (unscrolling)
2121 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
)
2124 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2129 /// Transform physical to logical
2130 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
)
2133 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2138 /// Position the caret
2139 void wxRichTextCtrl::PositionCaret()
2142 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2144 wxPoint originalPt
= caretRect
.GetPosition();
2145 wxPoint pt
= GetPhysicalPoint(originalPt
);
2147 GetCaret()->Move(pt
);
2148 GetCaret()->SetSize(caretRect
.GetSize());
2152 /// Get the caret height and position for the given character position
2153 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2155 wxClientDC
dc(this);
2156 dc
.SetFont(GetFont());
2163 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2165 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2172 /// Gets the line for the visible caret position. If the caret is
2173 /// shown at the very end of the line, it means the next character is actually
2174 /// on the following line. So let's get the line we're expecting to find
2175 /// if this is the case.
2176 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2178 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2179 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2182 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2183 if (caretPosition
== lineRange
.GetStart()-1 &&
2184 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2186 if (!m_caretAtLineStart
)
2187 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2194 /// Move the caret to the given character position
2195 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2197 if (GetBuffer().GetDirty())
2200 if (pos
<= GetBuffer().GetRange().GetEnd())
2202 SetCaretPosition(pos
, showAtLineStart
);
2212 /// Layout the buffer: which we must do before certain operations, such as
2213 /// setting the caret position.
2214 bool wxRichTextCtrl::Layout()
2216 if (GetBuffer().GetDirty())
2218 wxRect
availableSpace(GetClientSize());
2219 if (availableSpace
.width
== 0)
2220 availableSpace
.width
= 10;
2221 if (availableSpace
.height
== 0)
2222 availableSpace
.height
= 10;
2224 wxClientDC
dc(this);
2225 dc
.SetFont(GetFont());
2229 GetBuffer().Defragment();
2230 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2231 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
2232 GetBuffer().SetDirty(false);
2241 /// Is all of the selection bold?
2242 bool wxRichTextCtrl::IsSelectionBold() const
2246 wxRichTextAttr attr
;
2247 wxRichTextRange range
= GetSelectionRange();
2248 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2249 attr
.SetFontWeight(wxBOLD
);
2251 return HasCharacterAttributes(range
, attr
);
2255 // If no selection, then we need to combine current style with default style
2256 // to see what the effect would be if we started typing.
2257 wxRichTextAttr attr
;
2258 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2259 if (GetStyle(GetCaretPosition()+1, attr
))
2261 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2262 return attr
.GetFontWeight() == wxBOLD
;
2268 /// Is all of the selection italics?
2269 bool wxRichTextCtrl::IsSelectionItalics() const
2273 wxRichTextRange range
= GetSelectionRange();
2274 wxRichTextAttr attr
;
2275 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2276 attr
.SetFontStyle(wxITALIC
);
2278 return HasCharacterAttributes(range
, attr
);
2282 // If no selection, then we need to combine current style with default style
2283 // to see what the effect would be if we started typing.
2284 wxRichTextAttr attr
;
2285 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2286 if (GetStyle(GetCaretPosition()+1, attr
))
2288 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2289 return attr
.GetFontStyle() == wxITALIC
;
2295 /// Is all of the selection underlined?
2296 bool wxRichTextCtrl::IsSelectionUnderlined() const
2300 wxRichTextRange range
= GetSelectionRange();
2301 wxRichTextAttr attr
;
2302 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2303 attr
.SetFontUnderlined(true);
2305 return HasCharacterAttributes(range
, attr
);
2309 // If no selection, then we need to combine current style with default style
2310 // to see what the effect would be if we started typing.
2311 wxRichTextAttr attr
;
2312 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2313 if (GetStyle(GetCaretPosition()+1, attr
))
2315 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2316 return attr
.GetFontUnderlined();
2322 /// Apply bold to the selection
2323 bool wxRichTextCtrl::ApplyBoldToSelection()
2325 wxRichTextAttr attr
;
2326 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2327 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2330 return SetStyle(GetSelectionRange(), attr
);
2332 SetDefaultStyle(attr
);
2336 /// Apply italic to the selection
2337 bool wxRichTextCtrl::ApplyItalicToSelection()
2339 wxRichTextAttr attr
;
2340 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2341 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2344 return SetStyle(GetSelectionRange(), attr
);
2346 SetDefaultStyle(attr
);
2350 /// Apply underline to the selection
2351 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2353 wxRichTextAttr attr
;
2354 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2355 attr
.SetFontWeight(!IsSelectionUnderlined());
2358 return SetStyle(GetSelectionRange(), attr
);
2360 SetDefaultStyle(attr
);
2364 /// Is all of the selection aligned according to the specified flag?
2365 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) const
2369 wxRichTextRange range
= GetSelectionRange();
2370 wxRichTextAttr attr
;
2371 attr
.SetAlignment(alignment
);
2373 return HasParagraphAttributes(range
, attr
);
2377 // If no selection, then we need to get information from the current paragraph.
2378 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2380 return para
->GetAttributes().GetAlignment() == alignment
;
2385 /// Apply alignment to the selection
2386 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2388 wxRichTextAttr attr
;
2389 attr
.SetAlignment(alignment
);
2391 return SetStyle(GetSelectionRange(), attr
);
2394 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2396 return SetStyle(para
->GetRange(), attr
);
2401 /// Sets the default style to the style under the cursor
2402 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2405 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2407 if (GetStyle(GetCaretPosition(), attr
))
2409 SetDefaultStyle(attr
);