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"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
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
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
)
58 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
60 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
62 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
63 EVT_PAINT(wxRichTextCtrl::OnPaint
)
64 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
65 EVT_IDLE(wxRichTextCtrl::OnIdle
)
66 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
67 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
68 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
69 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
70 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
71 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
72 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
73 EVT_CHAR(wxRichTextCtrl::OnChar
)
74 EVT_SIZE(wxRichTextCtrl::OnSize
)
75 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
76 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
77 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
78 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
80 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
81 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
83 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
84 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
86 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
87 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
89 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
90 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
92 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
93 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
95 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
96 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
98 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
99 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
106 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
108 wxRichTextCtrl::wxRichTextCtrl()
109 : wxScrollHelper(this)
114 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
116 const wxString
& value
,
120 const wxValidator
& validator
,
121 const wxString
& name
)
122 : wxScrollHelper(this)
125 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
129 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
130 const wxValidator
& validator
, const wxString
& name
)
132 if (!wxControl::Create(parent
, id
, pos
, size
,
133 style
|wxFULL_REPAINT_ON_RESIZE
,
139 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
143 GetBuffer().SetRichTextCtrl(this);
145 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
148 if (style
& wxTE_READONLY
)
151 // The base attributes must all have default values
152 wxTextAttr attributes
;
153 attributes
.SetFont(GetFont());
154 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
155 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
156 attributes
.SetLineSpacing(10);
157 attributes
.SetParagraphSpacingAfter(10);
158 attributes
.SetParagraphSpacingBefore(0);
159 attributes
.SetTextEffects(0);
160 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
162 SetBasicStyle(attributes
);
164 // The default attributes will be merged with base attributes, so
165 // can be empty to begin with
166 wxTextAttr defaultAttributes
;
167 SetDefaultStyle(defaultAttributes
);
169 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
170 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
172 // Tell the sizers to use the given or best size
173 SetInitialSize(size
);
175 #if wxRICHTEXT_BUFFERED_PAINTING
177 RecreateBuffer(size
);
180 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
181 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
183 SetCursor(m_textCursor
);
185 if (!value
.IsEmpty())
188 GetBuffer().AddEventHandler(this);
193 wxRichTextCtrl::~wxRichTextCtrl()
195 GetBuffer().RemoveEventHandler(this);
197 delete m_contextMenu
;
200 /// Member initialisation
201 void wxRichTextCtrl::Init()
204 m_contextMenu
= NULL
;
206 m_caretPosition
= -1;
207 m_selectionRange
.SetRange(-2, -2);
208 m_selectionAnchor
= -2;
210 m_caretAtLineStart
= false;
212 m_fullLayoutRequired
= false;
213 m_fullLayoutTime
= 0;
214 m_fullLayoutSavedPosition
= 0;
215 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
216 m_caretPositionForDefaultStyle
= -2;
219 /// Call Freeze to prevent refresh
220 void wxRichTextCtrl::Freeze()
225 /// Call Thaw to refresh
226 void wxRichTextCtrl::Thaw()
230 if (m_freezeCount
== 0)
232 if (GetBuffer().GetDirty())
241 void wxRichTextCtrl::Clear()
243 m_buffer
.ResetAndClearCommands();
244 m_buffer
.SetDirty(true);
245 m_caretPosition
= -1;
246 m_caretPositionForDefaultStyle
= -2;
247 m_caretAtLineStart
= false;
248 m_selectionRange
.SetRange(-2, -2);
250 if (m_freezeCount
== 0)
256 wxTextCtrl::SendTextUpdatedEvent(this);
260 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
262 if (GetCaret() && GetCaret()->IsVisible())
266 #if wxRICHTEXT_BUFFERED_PAINTING
267 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
273 if (m_freezeCount
> 0)
276 dc
.SetFont(GetFont());
278 // Paint the background
281 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
283 wxRect
drawingArea(GetUpdateRegion().GetBox());
284 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
286 wxRect
availableSpace(GetClientSize());
287 if (GetBuffer().GetDirty())
289 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
290 GetBuffer().SetDirty(false);
294 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
297 if (GetCaret() && !GetCaret()->IsVisible())
303 // Empty implementation, to prevent flicker
304 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
308 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
312 if (!GetCaret()->IsVisible())
321 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
323 if (GetCaret() && GetCaret()->IsVisible())
330 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
336 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
342 dc
.SetFont(GetFont());
345 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
347 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
349 m_dragStart
= event
.GetLogicalPosition(dc
);
355 bool caretAtLineStart
= false;
357 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
359 // If we're at the start of a line (but not first in para)
360 // then we should keep the caret showing at the start of the line
361 // by showing the m_caretAtLineStart flag.
362 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
363 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
365 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
366 caretAtLineStart
= true;
370 MoveCaret(position
, caretAtLineStart
);
371 SetDefaultStyleToCursorStyle();
378 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
383 if (GetCapture() == this)
386 // See if we clicked on a URL
389 dc
.SetFont(GetFont());
392 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
393 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
395 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
397 wxRichTextEvent
cmdEvent(
398 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
400 cmdEvent
.SetEventObject(this);
401 cmdEvent
.SetPosition(m_caretPosition
+1);
403 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
406 if (GetStyle(position
, attr
))
408 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
410 wxString urlTarget
= attr
.GetURL();
411 if (!urlTarget
.IsEmpty())
413 wxMouseEvent
mouseEvent(event
);
415 long startPos
= 0, endPos
= 0;
416 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
419 startPos
= obj
->GetRange().GetStart();
420 endPos
= obj
->GetRange().GetEnd();
423 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
424 InitCommandEvent(urlEvent
);
426 urlEvent
.SetString(urlTarget
);
428 GetEventHandler()->ProcessEvent(urlEvent
);
438 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
442 dc
.SetFont(GetFont());
445 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
446 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
448 // See if we need to change the cursor
451 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
454 if (GetStyle(position
, attr
))
456 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
458 SetCursor(m_urlCursor
);
460 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
462 SetCursor(m_textCursor
);
467 SetCursor(m_textCursor
);
470 if (!event
.Dragging())
476 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
478 // TODO: test closeness
480 bool caretAtLineStart
= false;
482 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
484 // If we're at the start of a line (but not first in para)
485 // then we should keep the caret showing at the start of the line
486 // by showing the m_caretAtLineStart flag.
487 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
488 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
490 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
491 caretAtLineStart
= true;
495 if (m_caretPosition
!= position
)
497 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
499 MoveCaret(position
, caretAtLineStart
);
500 SetDefaultStyleToCursorStyle();
509 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& WXUNUSED(event
))
513 wxRichTextEvent
cmdEvent(
514 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
516 cmdEvent
.SetEventObject(this);
517 cmdEvent
.SetPosition(m_caretPosition
+1);
519 GetEventHandler()->ProcessEvent(cmdEvent
);
522 /// Left-double-click
523 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
525 wxRichTextEvent
cmdEvent(
526 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
528 cmdEvent
.SetEventObject(this);
529 cmdEvent
.SetPosition(m_caretPosition
+1);
531 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
533 SelectWord(GetCaretPosition()+1);
538 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
540 wxRichTextEvent
cmdEvent(
541 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
543 cmdEvent
.SetEventObject(this);
544 cmdEvent
.SetPosition(m_caretPosition
+1);
546 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
551 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
555 flags
|= wxRICHTEXT_CTRL_DOWN
;
556 if (event
.ShiftDown())
557 flags
|= wxRICHTEXT_SHIFT_DOWN
;
559 flags
|= wxRICHTEXT_ALT_DOWN
;
561 if (event
.GetKeyCode() == WXK_LEFT
||
562 event
.GetKeyCode() == WXK_RIGHT
||
563 event
.GetKeyCode() == WXK_UP
||
564 event
.GetKeyCode() == WXK_DOWN
||
565 event
.GetKeyCode() == WXK_HOME
||
566 event
.GetKeyCode() == WXK_PAGEUP
||
567 event
.GetKeyCode() == WXK_PAGEDOWN
||
568 event
.GetKeyCode() == WXK_END
||
570 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
571 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
572 event
.GetKeyCode() == WXK_NUMPAD_UP
||
573 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
574 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
575 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
576 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
577 event
.GetKeyCode() == WXK_NUMPAD_END
)
579 KeyboardNavigate(event
.GetKeyCode(), flags
);
583 // all the other keys modify the controls contents which shouldn't be
584 // possible if we're read-only
591 if (event
.GetKeyCode() == WXK_RETURN
)
593 BeginBatchUndo(_("Insert Text"));
595 long newPos
= m_caretPosition
;
597 DeleteSelectedContent(& newPos
);
599 if (event
.ShiftDown())
602 text
= wxRichTextLineBreakChar
;
603 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
606 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
609 SetDefaultStyleToCursorStyle();
611 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
613 wxRichTextEvent
cmdEvent(
614 wxEVT_COMMAND_RICHTEXT_RETURN
,
616 cmdEvent
.SetEventObject(this);
617 cmdEvent
.SetFlags(flags
);
618 cmdEvent
.SetPosition(newPos
+1);
620 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
622 // Generate conventional event
623 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
624 InitCommandEvent(textEvent
);
626 GetEventHandler()->ProcessEvent(textEvent
);
630 else if (event
.GetKeyCode() == WXK_BACK
)
632 BeginBatchUndo(_("Delete Text"));
634 // Submit range in character positions, which are greater than caret positions,
635 // so subtract 1 for deleted character and add 1 for conversion to character position.
636 if (m_caretPosition
> -1 && !HasSelection())
638 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
641 DeleteSelectedContent();
645 if (GetLastPosition() == -1)
649 m_caretPosition
= -1;
651 SetDefaultStyleToCursorStyle();
654 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
656 wxRichTextEvent
cmdEvent(
657 wxEVT_COMMAND_RICHTEXT_DELETE
,
659 cmdEvent
.SetEventObject(this);
660 cmdEvent
.SetFlags(flags
);
661 cmdEvent
.SetPosition(m_caretPosition
+1);
662 GetEventHandler()->ProcessEvent(cmdEvent
);
666 else if (event
.GetKeyCode() == WXK_DELETE
)
668 BeginBatchUndo(_("Delete Text"));
670 // Submit range in character positions, which are greater than caret positions,
671 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
673 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
676 DeleteSelectedContent();
680 if (GetLastPosition() == -1)
684 m_caretPosition
= -1;
686 SetDefaultStyleToCursorStyle();
689 wxRichTextEvent
cmdEvent(
690 wxEVT_COMMAND_RICHTEXT_DELETE
,
692 cmdEvent
.SetEventObject(this);
693 cmdEvent
.SetFlags(flags
);
694 cmdEvent
.SetPosition(m_caretPosition
+1);
695 GetEventHandler()->ProcessEvent(cmdEvent
);
701 long keycode
= event
.GetKeyCode();
774 case WXK_NUMPAD_SPACE
:
776 case WXK_NUMPAD_ENTER
:
781 case WXK_NUMPAD_HOME
:
782 case WXK_NUMPAD_LEFT
:
784 case WXK_NUMPAD_RIGHT
:
785 case WXK_NUMPAD_DOWN
:
786 case WXK_NUMPAD_PAGEUP
:
787 case WXK_NUMPAD_PAGEDOWN
:
789 case WXK_NUMPAD_BEGIN
:
790 case WXK_NUMPAD_INSERT
:
791 case WXK_NUMPAD_DELETE
:
792 case WXK_NUMPAD_EQUAL
:
793 case WXK_NUMPAD_MULTIPLY
:
795 case WXK_NUMPAD_SEPARATOR
:
796 case WXK_NUMPAD_SUBTRACT
:
797 case WXK_NUMPAD_DECIMAL
:
798 case WXK_WINDOWS_LEFT
:
806 if (event
.CmdDown() || event
.AltDown())
812 wxRichTextEvent
cmdEvent(
813 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
815 cmdEvent
.SetEventObject(this);
816 cmdEvent
.SetFlags(flags
);
818 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
820 cmdEvent
.SetCharacter((wxChar
) keycode
);
822 cmdEvent
.SetPosition(m_caretPosition
+1);
824 if (keycode
== wxT('\t'))
826 // See if we need to promote or demote the selection or paragraph at the cursor
827 // position, instead of inserting a tab.
828 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
829 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
830 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
832 wxRichTextRange range
;
834 range
= GetSelectionRange();
836 range
= para
->GetRange().FromInternal();
838 int promoteBy
= event
.ShiftDown() ? 1 : -1;
840 PromoteList(promoteBy
, range
, NULL
);
842 GetEventHandler()->ProcessEvent(cmdEvent
);
848 BeginBatchUndo(_("Insert Text"));
850 long newPos
= m_caretPosition
;
851 DeleteSelectedContent(& newPos
);
854 wxString str
= event
.GetUnicodeKey();
856 wxString str
= (wxChar
) event
.GetKeyCode();
858 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
862 SetDefaultStyleToCursorStyle();
863 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
865 GetEventHandler()->ProcessEvent(cmdEvent
);
873 /// Delete content if there is a selection, e.g. when pressing a key.
874 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
878 long pos
= m_selectionRange
.GetStart();
879 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
880 m_selectionRange
.SetRange(-2, -2);
890 /// Keyboard navigation
894 Left: left one character
895 Right: right one character
898 Ctrl-Left: left one word
899 Ctrl-Right: right one word
900 Ctrl-Up: previous paragraph start
901 Ctrl-Down: next start of paragraph
904 Ctrl-Home: start of document
905 Ctrl-End: end of document
907 Page-Down: Down a screen
911 Ctrl-Alt-PgUp: Start of window
912 Ctrl-Alt-PgDn: End of window
913 F8: Start selection mode
914 Esc: End selection mode
916 Adding Shift does the above but starts/extends selection.
921 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
923 bool success
= false;
925 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
927 if (flags
& wxRICHTEXT_CTRL_DOWN
)
928 success
= WordRight(1, flags
);
930 success
= MoveRight(1, flags
);
932 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
934 if (flags
& wxRICHTEXT_CTRL_DOWN
)
935 success
= WordLeft(1, flags
);
937 success
= MoveLeft(1, flags
);
939 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
941 if (flags
& wxRICHTEXT_CTRL_DOWN
)
942 success
= MoveToParagraphStart(flags
);
944 success
= MoveUp(1, flags
);
946 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
948 if (flags
& wxRICHTEXT_CTRL_DOWN
)
949 success
= MoveToParagraphEnd(flags
);
951 success
= MoveDown(1, flags
);
953 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
955 success
= PageUp(1, flags
);
957 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
959 success
= PageDown(1, flags
);
961 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
963 if (flags
& wxRICHTEXT_CTRL_DOWN
)
964 success
= MoveHome(flags
);
966 success
= MoveToLineStart(flags
);
968 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
970 if (flags
& wxRICHTEXT_CTRL_DOWN
)
971 success
= MoveEnd(flags
);
973 success
= MoveToLineEnd(flags
);
978 ScrollIntoView(m_caretPosition
, keyCode
);
979 SetDefaultStyleToCursorStyle();
985 /// Extend the selection. Selections are in caret positions.
986 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
988 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
990 // If not currently selecting, start selecting
991 if (m_selectionRange
.GetStart() == -2)
993 m_selectionAnchor
= oldPos
;
996 m_selectionRange
.SetRange(newPos
+1, oldPos
);
998 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1002 // Always ensure that the selection range start is greater than
1004 if (newPos
> m_selectionAnchor
)
1005 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1007 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1010 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1012 wxLogDebug(wxT("Strange selection range"));
1021 /// Scroll into view, returning true if we scrolled.
1022 /// This takes a _caret_ position.
1023 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1025 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1031 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1033 int startXUnits
, startYUnits
;
1034 GetViewStart(& startXUnits
, & startYUnits
);
1035 int startY
= startYUnits
* ppuY
;
1038 GetVirtualSize(& sx
, & sy
);
1044 wxRect rect
= line
->GetRect();
1046 bool scrolled
= false;
1048 wxSize clientSize
= GetClientSize();
1051 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1052 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1053 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1054 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1056 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1058 // Make it scroll so this item is at the bottom
1060 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1061 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1063 // If we're still off the screen, scroll another line down
1064 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1067 if (startYUnits
!= yUnits
)
1069 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1073 else if (rect
.y
< startY
)
1075 // Make it scroll so this item is at the top
1078 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1080 if (startYUnits
!= yUnits
)
1082 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1088 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1089 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1090 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1091 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1093 if (rect
.y
< startY
)
1095 // Make it scroll so this item is at the top
1098 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1100 if (startYUnits
!= yUnits
)
1102 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1106 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1108 // Make it scroll so this item is at the bottom
1110 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1111 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1113 // If we're still off the screen, scroll another line down
1114 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1117 if (startYUnits
!= yUnits
)
1119 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1129 /// Is the given position visible on the screen?
1130 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1132 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1138 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1141 GetViewStart(& startX
, & startY
);
1143 startY
= startY
* ppuY
;
1145 wxRect rect
= line
->GetRect();
1146 wxSize clientSize
= GetClientSize();
1148 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1151 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1153 m_caretPosition
= position
;
1154 m_caretAtLineStart
= showAtLineStart
;
1157 /// Move caret one visual step forward: this may mean setting a flag
1158 /// and keeping the same position if we're going from the end of one line
1159 /// to the start of the next, which may be the exact same caret position.
1160 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1162 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1164 // Only do the check if we're not at the end of the paragraph (where things work OK
1166 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1168 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1172 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1174 // We're at the end of a line. See whether we need to
1175 // stay at the same actual caret position but change visual
1176 // position, or not.
1177 if (oldPosition
== lineRange
.GetEnd())
1179 if (m_caretAtLineStart
)
1181 // We're already at the start of the line, so actually move on now.
1182 m_caretPosition
= oldPosition
+ 1;
1183 m_caretAtLineStart
= false;
1187 // We're showing at the end of the line, so keep to
1188 // the same position but indicate that we're to show
1189 // at the start of the next line.
1190 m_caretPosition
= oldPosition
;
1191 m_caretAtLineStart
= true;
1193 SetDefaultStyleToCursorStyle();
1199 SetDefaultStyleToCursorStyle();
1202 /// Move caret one visual step backward: this may mean setting a flag
1203 /// and keeping the same position if we're going from the end of one line
1204 /// to the start of the next, which may be the exact same caret position.
1205 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1207 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1209 // Only do the check if we're not at the start of the paragraph (where things work OK
1211 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1213 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1217 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1219 // We're at the start of a line. See whether we need to
1220 // stay at the same actual caret position but change visual
1221 // position, or not.
1222 if (oldPosition
== lineRange
.GetStart())
1224 m_caretPosition
= oldPosition
-1;
1225 m_caretAtLineStart
= true;
1228 else if (oldPosition
== lineRange
.GetEnd())
1230 if (m_caretAtLineStart
)
1232 // We're at the start of the line, so keep the same caret position
1233 // but clear the start-of-line flag.
1234 m_caretPosition
= oldPosition
;
1235 m_caretAtLineStart
= false;
1239 // We're showing at the end of the line, so go back
1240 // to the previous character position.
1241 m_caretPosition
= oldPosition
- 1;
1243 SetDefaultStyleToCursorStyle();
1249 SetDefaultStyleToCursorStyle();
1253 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1255 long endPos
= GetBuffer().GetRange().GetEnd();
1257 if (m_caretPosition
+ noPositions
< endPos
)
1259 long oldPos
= m_caretPosition
;
1260 long newPos
= m_caretPosition
+ noPositions
;
1262 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1266 // Determine by looking at oldPos and m_caretPosition whether
1267 // we moved from the end of a line to the start of the next line, in which case
1268 // we want to adjust the caret position such that it is positioned at the
1269 // start of the next line, rather than jumping past the first character of the
1271 if (noPositions
== 1 && !extendSel
)
1272 MoveCaretForward(oldPos
);
1274 SetCaretPosition(newPos
);
1277 SetDefaultStyleToCursorStyle();
1288 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1292 if (m_caretPosition
> startPos
- noPositions
+ 1)
1294 long oldPos
= m_caretPosition
;
1295 long newPos
= m_caretPosition
- noPositions
;
1296 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1300 if (noPositions
== 1 && !extendSel
)
1301 MoveCaretBack(oldPos
);
1303 SetCaretPosition(newPos
);
1306 SetDefaultStyleToCursorStyle();
1317 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1319 return MoveDown(- noLines
, flags
);
1323 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1328 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1329 wxPoint pt
= GetCaret()->GetPosition();
1330 long newLine
= lineNumber
+ noLines
;
1332 if (lineNumber
!= -1)
1336 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1338 if (newLine
> lastLine
)
1348 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1351 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1357 wxClientDC
dc(this);
1359 dc
.SetFont(GetFont());
1361 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1363 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1365 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1366 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1367 // so we view the caret at the start of the line.
1368 bool caretLineStart
= false;
1369 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1371 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1372 wxRichTextRange lineRange
;
1374 lineRange
= thisLine
->GetAbsoluteRange();
1376 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1379 caretLineStart
= true;
1383 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1384 if (para
&& para
->GetRange().GetStart() == newPos
)
1389 long newSelEnd
= newPos
;
1391 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1395 SetCaretPosition(newPos
, caretLineStart
);
1397 SetDefaultStyleToCursorStyle();
1407 /// Move to the end of the paragraph
1408 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1410 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1413 long newPos
= para
->GetRange().GetEnd() - 1;
1414 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1418 SetCaretPosition(newPos
);
1420 SetDefaultStyleToCursorStyle();
1430 /// Move to the start of the paragraph
1431 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1433 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1436 long newPos
= para
->GetRange().GetStart() - 1;
1437 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1441 SetCaretPosition(newPos
);
1443 SetDefaultStyleToCursorStyle();
1453 /// Move to the end of the line
1454 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1456 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1460 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1461 long newPos
= lineRange
.GetEnd();
1462 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1466 SetCaretPosition(newPos
);
1468 SetDefaultStyleToCursorStyle();
1478 /// Move to the start of the line
1479 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1481 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1484 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1485 long newPos
= lineRange
.GetStart()-1;
1487 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1491 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1493 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1495 SetDefaultStyleToCursorStyle();
1505 /// Move to the start of the buffer
1506 bool wxRichTextCtrl::MoveHome(int flags
)
1508 if (m_caretPosition
!= -1)
1510 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1514 SetCaretPosition(-1);
1516 SetDefaultStyleToCursorStyle();
1526 /// Move to the end of the buffer
1527 bool wxRichTextCtrl::MoveEnd(int flags
)
1529 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1531 if (m_caretPosition
!= endPos
)
1533 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1537 SetCaretPosition(endPos
);
1539 SetDefaultStyleToCursorStyle();
1549 /// Move noPages pages up
1550 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1552 return PageDown(- noPages
, flags
);
1555 /// Move noPages pages down
1556 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1558 // Calculate which line occurs noPages * screen height further down.
1559 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1562 wxSize clientSize
= GetClientSize();
1563 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1565 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1568 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1569 long pos
= lineRange
.GetStart()-1;
1570 if (pos
!= m_caretPosition
)
1572 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1574 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1578 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1580 SetDefaultStyleToCursorStyle();
1592 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1594 return str
== wxT(" ") || str
== wxT("\t");
1597 // Finds the caret position for the next word
1598 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1600 long endPos
= GetBuffer().GetRange().GetEnd();
1604 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1606 // First skip current text to space
1607 while (i
< endPos
&& i
> -1)
1609 // i is in character, not caret positions
1610 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1611 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1612 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1616 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1623 while (i
< endPos
&& i
> -1)
1625 // i is in character, not caret positions
1626 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1627 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1628 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1629 return wxMax(-1, i
);
1631 if (text
.empty()) // End of paragraph, or maybe an image
1632 return wxMax(-1, i
- 1);
1633 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1637 // Convert to caret position
1638 return wxMax(-1, i
- 1);
1647 long i
= m_caretPosition
;
1649 // First skip white space
1650 while (i
< endPos
&& i
> -1)
1652 // i is in character, not caret positions
1653 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1654 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1656 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1658 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1663 // Next skip current text to space
1664 while (i
< endPos
&& i
> -1)
1666 // i is in character, not caret positions
1667 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1668 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1669 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1672 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1685 /// Move n words left
1686 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1688 long pos
= FindNextWordPosition(-1);
1689 if (pos
!= m_caretPosition
)
1691 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1693 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1697 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1699 SetDefaultStyleToCursorStyle();
1709 /// Move n words right
1710 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1712 long pos
= FindNextWordPosition(1);
1713 if (pos
!= m_caretPosition
)
1715 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1717 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1721 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1723 SetDefaultStyleToCursorStyle();
1734 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1736 // Only do sizing optimization for large buffers
1737 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1739 m_fullLayoutRequired
= true;
1740 m_fullLayoutTime
= wxGetLocalTimeMillis();
1741 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1742 LayoutContent(true /* onlyVisibleRect */);
1745 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1747 #if wxRICHTEXT_BUFFERED_PAINTING
1755 /// Idle-time processing
1756 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1758 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1760 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1762 m_fullLayoutRequired
= false;
1763 m_fullLayoutTime
= 0;
1764 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1765 ShowPosition(m_fullLayoutSavedPosition
);
1769 if (m_caretPositionForDefaultStyle
!= -2)
1771 // If the caret position has changed, no longer reflect the default style
1773 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1774 m_caretPositionForDefaultStyle
= -2;
1781 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1787 /// Set up scrollbars, e.g. after a resize
1788 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1793 if (GetBuffer().IsEmpty())
1795 SetScrollbars(0, 0, 0, 0, 0, 0);
1799 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1800 // of pixels. See e.g. wxVScrolledWindow for ideas.
1801 int pixelsPerUnit
= 5;
1802 wxSize clientSize
= GetClientSize();
1804 int maxHeight
= GetBuffer().GetCachedSize().y
;
1806 // Round up so we have at least maxHeight pixels
1807 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1809 int startX
= 0, startY
= 0;
1811 GetViewStart(& startX
, & startY
);
1813 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1814 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1816 // Move to previous scroll position if
1818 SetScrollbars(0, pixelsPerUnit
,
1820 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1823 /// Paint the background
1824 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1826 wxColour backgroundColour
= GetBackgroundColour();
1827 if (!backgroundColour
.Ok())
1828 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1830 // Clear the background
1831 dc
.SetBrush(wxBrush(backgroundColour
));
1832 dc
.SetPen(*wxTRANSPARENT_PEN
);
1833 wxRect
windowRect(GetClientSize());
1834 windowRect
.x
-= 2; windowRect
.y
-= 2;
1835 windowRect
.width
+= 4; windowRect
.height
+= 4;
1837 // We need to shift the rectangle to take into account
1838 // scrolling. Converting device to logical coordinates.
1839 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1840 dc
.DrawRectangle(windowRect
);
1843 #if wxRICHTEXT_BUFFERED_PAINTING
1844 /// Recreate buffer bitmap if necessary
1845 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1848 if (sz
== wxDefaultSize
)
1849 sz
= GetClientSize();
1851 if (sz
.x
< 1 || sz
.y
< 1)
1854 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1855 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1856 return m_bufferBitmap
.Ok();
1860 // ----------------------------------------------------------------------------
1861 // file IO functions
1862 // ----------------------------------------------------------------------------
1864 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1866 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1868 m_filename
= filename
;
1871 SetInsertionPoint(0);
1874 SetupScrollbars(true);
1876 wxTextCtrl::SendTextUpdatedEvent(this);
1882 wxLogError(_("File couldn't be loaded."));
1888 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1890 if (GetBuffer().SaveFile(filename
, fileType
))
1892 m_filename
= filename
;
1899 wxLogError(_("The text couldn't be saved."));
1904 // ----------------------------------------------------------------------------
1905 // wxRichTextCtrl specific functionality
1906 // ----------------------------------------------------------------------------
1908 /// Add a new paragraph of text to the end of the buffer
1909 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1911 return GetBuffer().AddParagraph(text
);
1915 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1917 return GetBuffer().AddImage(image
);
1920 // ----------------------------------------------------------------------------
1921 // selection and ranges
1922 // ----------------------------------------------------------------------------
1924 void wxRichTextCtrl::SelectAll()
1926 SetSelection(0, GetLastPosition()+1);
1927 m_selectionAnchor
= -1;
1931 void wxRichTextCtrl::SelectNone()
1933 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1934 SetSelection(-2, -2);
1935 m_selectionAnchor
= -2;
1938 static bool wxIsWordDelimiter(const wxString
& text
)
1940 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1943 /// Select the word at the given character position
1944 bool wxRichTextCtrl::SelectWord(long position
)
1946 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1949 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1953 long positionStart
= position
;
1954 long positionEnd
= position
;
1956 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1958 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1959 if (wxIsWordDelimiter(text
))
1965 if (positionStart
< para
->GetRange().GetStart())
1966 positionStart
= para
->GetRange().GetStart();
1968 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1970 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1971 if (wxIsWordDelimiter(text
))
1977 if (positionEnd
>= para
->GetRange().GetEnd())
1978 positionEnd
= para
->GetRange().GetEnd();
1980 SetSelection(positionStart
, positionEnd
+1);
1982 if (positionStart
>= 0)
1984 MoveCaret(positionStart
-1, true);
1985 SetDefaultStyleToCursorStyle();
1991 wxString
wxRichTextCtrl::GetStringSelection() const
1994 GetSelection(&from
, &to
);
1996 return GetRange(from
, to
);
1999 // ----------------------------------------------------------------------------
2001 // ----------------------------------------------------------------------------
2003 wxTextCtrlHitTestResult
2004 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2006 // implement in terms of the other overload as the native ports typically
2007 // can get the position and not (x, y) pair directly (although wxUniv
2008 // directly gets x and y -- and so overrides this method as well)
2010 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2012 if ( rc
!= wxTE_HT_UNKNOWN
)
2014 PositionToXY(pos
, x
, y
);
2020 wxTextCtrlHitTestResult
2021 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2024 wxClientDC
dc((wxRichTextCtrl
*) this);
2025 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2027 // Buffer uses logical position (relative to start of buffer)
2029 wxPoint pt2
= GetLogicalPoint(pt
);
2031 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2033 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2034 return wxTE_HT_BEFORE
;
2035 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2036 return wxTE_HT_BEYOND
;
2037 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2038 return wxTE_HT_ON_TEXT
;
2040 return wxTE_HT_UNKNOWN
;
2043 // ----------------------------------------------------------------------------
2044 // set/get the controls text
2045 // ----------------------------------------------------------------------------
2047 wxString
wxRichTextCtrl::GetValue() const
2049 return GetBuffer().GetText();
2052 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2054 // Public API for range is different from internals
2055 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2058 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2062 if (!value
.IsEmpty())
2064 // Remove empty paragraph
2065 GetBuffer().Clear();
2068 // for compatibility, don't move the cursor when doing SetValue()
2069 SetInsertionPoint(0);
2073 // still send an event for consistency
2074 if (flags
& SetValue_SendEvent
)
2075 wxTextCtrl::SendTextUpdatedEvent(this);
2080 void wxRichTextCtrl::WriteText(const wxString
& value
)
2085 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2087 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2089 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2091 if ( flags
& SetValue_SendEvent
)
2092 wxTextCtrl::SendTextUpdatedEvent(this);
2095 void wxRichTextCtrl::AppendText(const wxString
& text
)
2097 SetInsertionPointEnd();
2102 /// Write an image at the current insertion point
2103 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2105 wxRichTextImageBlock imageBlock
;
2107 wxImage image2
= image
;
2108 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2109 return WriteImage(imageBlock
);
2114 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2116 wxRichTextImageBlock imageBlock
;
2119 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2120 return WriteImage(imageBlock
);
2125 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2127 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2130 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2134 wxRichTextImageBlock imageBlock
;
2136 wxImage image
= bitmap
.ConvertToImage();
2137 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2138 return WriteImage(imageBlock
);
2144 /// Insert a newline (actually paragraph) at the current insertion point.
2145 bool wxRichTextCtrl::Newline()
2147 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2150 /// Insert a line break at the current insertion point.
2151 bool wxRichTextCtrl::LineBreak()
2154 text
= wxRichTextLineBreakChar
;
2155 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2158 // ----------------------------------------------------------------------------
2159 // Clipboard operations
2160 // ----------------------------------------------------------------------------
2162 void wxRichTextCtrl::Copy()
2166 wxRichTextRange range
= GetInternalSelectionRange();
2167 GetBuffer().CopyToClipboard(range
);
2171 void wxRichTextCtrl::Cut()
2175 wxRichTextRange range
= GetInternalSelectionRange();
2176 GetBuffer().CopyToClipboard(range
);
2178 DeleteSelectedContent();
2184 void wxRichTextCtrl::Paste()
2188 BeginBatchUndo(_("Paste"));
2190 long newPos
= m_caretPosition
;
2191 DeleteSelectedContent(& newPos
);
2193 GetBuffer().PasteFromClipboard(newPos
);
2199 void wxRichTextCtrl::DeleteSelection()
2201 if (CanDeleteSelection())
2203 DeleteSelectedContent();
2207 bool wxRichTextCtrl::HasSelection() const
2209 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2212 bool wxRichTextCtrl::CanCopy() const
2214 // Can copy if there's a selection
2215 return HasSelection();
2218 bool wxRichTextCtrl::CanCut() const
2220 return HasSelection() && IsEditable();
2223 bool wxRichTextCtrl::CanPaste() const
2225 if ( !IsEditable() )
2228 return GetBuffer().CanPasteFromClipboard();
2231 bool wxRichTextCtrl::CanDeleteSelection() const
2233 return HasSelection() && IsEditable();
2237 // ----------------------------------------------------------------------------
2239 // ----------------------------------------------------------------------------
2241 void wxRichTextCtrl::SetEditable(bool editable
)
2243 m_editable
= editable
;
2246 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2250 m_caretPosition
= pos
- 1;
2253 void wxRichTextCtrl::SetInsertionPointEnd()
2255 long pos
= GetLastPosition();
2256 SetInsertionPoint(pos
);
2259 long wxRichTextCtrl::GetInsertionPoint() const
2261 return m_caretPosition
+1;
2264 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2266 return GetBuffer().GetRange().GetEnd();
2269 // If the return values from and to are the same, there is no
2271 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2273 *from
= m_selectionRange
.GetStart();
2274 *to
= m_selectionRange
.GetEnd();
2275 if ((*to
) != -1 && (*to
) != -2)
2279 bool wxRichTextCtrl::IsEditable() const
2284 // ----------------------------------------------------------------------------
2286 // ----------------------------------------------------------------------------
2288 void wxRichTextCtrl::SetSelection(long from
, long to
)
2290 // if from and to are both -1, it means (in wxWidgets) that all text should
2292 if ( (from
== -1) && (to
== -1) )
2295 to
= GetLastPosition()+1;
2298 DoSetSelection(from
, to
);
2301 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2303 m_selectionAnchor
= from
;
2304 m_selectionRange
.SetRange(from
, to
-1);
2306 m_caretPosition
= from
-1;
2312 // ----------------------------------------------------------------------------
2314 // ----------------------------------------------------------------------------
2316 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2317 const wxString
& value
)
2319 BeginBatchUndo(_("Replace"));
2321 DeleteSelectedContent();
2323 DoWriteText(value
, SetValue_SelectionOnly
);
2328 void wxRichTextCtrl::Remove(long from
, long to
)
2332 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2339 bool wxRichTextCtrl::IsModified() const
2341 return m_buffer
.IsModified();
2344 void wxRichTextCtrl::MarkDirty()
2346 m_buffer
.Modify(true);
2349 void wxRichTextCtrl::DiscardEdits()
2351 m_caretPositionForDefaultStyle
= -2;
2352 m_buffer
.Modify(false);
2353 m_buffer
.GetCommandProcessor()->ClearCommands();
2356 int wxRichTextCtrl::GetNumberOfLines() const
2358 return GetBuffer().GetParagraphCount();
2361 // ----------------------------------------------------------------------------
2362 // Positions <-> coords
2363 // ----------------------------------------------------------------------------
2365 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2367 return GetBuffer().XYToPosition(x
, y
);
2370 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2372 return GetBuffer().PositionToXY(pos
, x
, y
);
2375 // ----------------------------------------------------------------------------
2377 // ----------------------------------------------------------------------------
2379 void wxRichTextCtrl::ShowPosition(long pos
)
2381 if (!IsPositionVisible(pos
))
2382 ScrollIntoView(pos
-1, WXK_DOWN
);
2385 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2387 return GetBuffer().GetParagraphLength(lineNo
);
2390 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2392 return GetBuffer().GetParagraphText(lineNo
);
2395 // ----------------------------------------------------------------------------
2397 // ----------------------------------------------------------------------------
2399 void wxRichTextCtrl::Undo()
2403 GetCommandProcessor()->Undo();
2407 void wxRichTextCtrl::Redo()
2411 GetCommandProcessor()->Redo();
2415 bool wxRichTextCtrl::CanUndo() const
2417 return GetCommandProcessor()->CanUndo();
2420 bool wxRichTextCtrl::CanRedo() const
2422 return GetCommandProcessor()->CanRedo();
2425 // ----------------------------------------------------------------------------
2426 // implementation details
2427 // ----------------------------------------------------------------------------
2429 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2431 SetValue(event
.GetString());
2432 GetEventHandler()->ProcessEvent(event
);
2435 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2437 // By default, load the first file into the text window.
2438 if (event
.GetNumberOfFiles() > 0)
2440 LoadFile(event
.GetFiles()[0]);
2444 wxSize
wxRichTextCtrl::DoGetBestSize() const
2446 return wxSize(10, 10);
2449 // ----------------------------------------------------------------------------
2450 // standard handlers for standard edit menu events
2451 // ----------------------------------------------------------------------------
2453 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2458 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2463 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2468 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2473 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2478 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2483 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2485 event
.Enable( CanCut() );
2488 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2490 event
.Enable( CanCopy() );
2493 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2495 event
.Enable( CanDeleteSelection() );
2498 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2500 event
.Enable( CanPaste() );
2503 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2505 event
.Enable( CanUndo() );
2506 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2509 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2511 event
.Enable( CanRedo() );
2512 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2515 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2520 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2522 event
.Enable(GetLastPosition() > 0);
2525 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2527 if (event
.GetEventObject() != this)
2535 m_contextMenu
= new wxMenu
;
2536 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2537 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2538 m_contextMenu
->AppendSeparator();
2539 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2540 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2541 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2542 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2543 m_contextMenu
->AppendSeparator();
2544 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2546 PopupMenu(m_contextMenu
);
2550 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2552 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2555 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2557 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2560 // extended style setting operation with flags including:
2561 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2562 // see richtextbuffer.h for more details.
2564 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2566 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2569 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2571 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2574 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2576 return GetBuffer().GetDefaultStyle();
2579 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2581 return GetBuffer().GetStyle(position
, style
);
2584 // get the common set of styles for the range
2585 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2587 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2590 /// Get the content (uncombined) attributes for this position.
2591 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2593 return GetBuffer().GetUncombinedStyle(position
, style
);
2596 /// Set font, and also the buffer attributes
2597 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2599 wxControl::SetFont(font
);
2601 wxTextAttr attr
= GetBuffer().GetAttributes();
2603 GetBuffer().SetBasicStyle(attr
);
2605 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2611 /// Transform logical to physical
2612 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2615 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2620 /// Transform physical to logical
2621 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2624 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2629 /// Position the caret
2630 void wxRichTextCtrl::PositionCaret()
2635 //wxLogDebug(wxT("PositionCaret"));
2638 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2640 wxPoint newPt
= caretRect
.GetPosition();
2641 wxSize newSz
= caretRect
.GetSize();
2642 wxPoint pt
= GetPhysicalPoint(newPt
);
2643 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2645 GetCaret()->Move(pt
);
2646 GetCaret()->SetSize(newSz
);
2651 /// Get the caret height and position for the given character position
2652 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2654 wxClientDC
dc(this);
2655 dc
.SetFont(GetFont());
2662 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2664 // Caret height can't be zero
2666 height
= dc
.GetCharHeight();
2668 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2675 /// Gets the line for the visible caret position. If the caret is
2676 /// shown at the very end of the line, it means the next character is actually
2677 /// on the following line. So let's get the line we're expecting to find
2678 /// if this is the case.
2679 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2681 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2682 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2685 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2686 if (caretPosition
== lineRange
.GetStart()-1 &&
2687 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2689 if (!m_caretAtLineStart
)
2690 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2697 /// Move the caret to the given character position
2698 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2700 if (GetBuffer().GetDirty())
2703 if (pos
<= GetBuffer().GetRange().GetEnd())
2705 SetCaretPosition(pos
, showAtLineStart
);
2715 /// Layout the buffer: which we must do before certain operations, such as
2716 /// setting the caret position.
2717 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2719 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2721 wxRect
availableSpace(GetClientSize());
2722 if (availableSpace
.width
== 0)
2723 availableSpace
.width
= 10;
2724 if (availableSpace
.height
== 0)
2725 availableSpace
.height
= 10;
2727 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2728 if (onlyVisibleRect
)
2730 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2731 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2734 wxClientDC
dc(this);
2735 dc
.SetFont(GetFont());
2739 GetBuffer().Defragment();
2740 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2741 GetBuffer().Layout(dc
, availableSpace
, flags
);
2742 GetBuffer().SetDirty(false);
2751 /// Is all of the selection bold?
2752 bool wxRichTextCtrl::IsSelectionBold()
2757 wxRichTextRange range
= GetSelectionRange();
2758 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2759 attr
.SetFontWeight(wxBOLD
);
2761 return HasCharacterAttributes(range
, attr
);
2765 // If no selection, then we need to combine current style with default style
2766 // to see what the effect would be if we started typing.
2768 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2770 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2771 if (GetStyle(pos
, attr
))
2773 if (IsDefaultStyleShowing())
2774 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2775 return attr
.GetFontWeight() == wxBOLD
;
2781 /// Is all of the selection italics?
2782 bool wxRichTextCtrl::IsSelectionItalics()
2786 wxRichTextRange range
= GetSelectionRange();
2788 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2789 attr
.SetFontStyle(wxITALIC
);
2791 return HasCharacterAttributes(range
, attr
);
2795 // If no selection, then we need to combine current style with default style
2796 // to see what the effect would be if we started typing.
2798 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2800 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2801 if (GetStyle(pos
, attr
))
2803 if (IsDefaultStyleShowing())
2804 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2805 return attr
.GetFontStyle() == wxITALIC
;
2811 /// Is all of the selection underlined?
2812 bool wxRichTextCtrl::IsSelectionUnderlined()
2816 wxRichTextRange range
= GetSelectionRange();
2818 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2819 attr
.SetFontUnderlined(true);
2821 return HasCharacterAttributes(range
, attr
);
2825 // If no selection, then we need to combine current style with default style
2826 // to see what the effect would be if we started typing.
2828 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2829 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2831 if (GetStyle(pos
, attr
))
2833 if (IsDefaultStyleShowing())
2834 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2835 return attr
.GetFontUnderlined();
2841 /// Apply bold to the selection
2842 bool wxRichTextCtrl::ApplyBoldToSelection()
2845 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2846 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2849 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2851 SetAndShowDefaultStyle(attr
);
2855 /// Apply italic to the selection
2856 bool wxRichTextCtrl::ApplyItalicToSelection()
2859 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2860 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2863 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2865 SetAndShowDefaultStyle(attr
);
2869 /// Apply underline to the selection
2870 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2873 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2874 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2877 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2879 SetAndShowDefaultStyle(attr
);
2883 /// Is all of the selection aligned according to the specified flag?
2884 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2886 wxRichTextRange range
;
2888 range
= GetSelectionRange();
2890 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2893 attr
.SetAlignment(alignment
);
2895 return HasParagraphAttributes(range
, attr
);
2898 /// Apply alignment to the selection
2899 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2902 attr
.SetAlignment(alignment
);
2904 return SetStyle(GetSelectionRange(), attr
);
2907 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2909 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2914 /// Apply a named style to the selection
2915 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2917 // Flags are defined within each definition, so only certain
2918 // attributes are applied.
2919 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2921 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2923 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2925 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2927 wxRichTextRange range
;
2930 range
= GetSelectionRange();
2933 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2934 range
= wxRichTextRange(pos
, pos
+1);
2937 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2940 // Make sure the attr has the style name
2941 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2943 attr
.SetParagraphStyleName(def
->GetName());
2945 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2946 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2947 // to change its style independently.
2948 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2951 attr
.SetCharacterStyleName(def
->GetName());
2954 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2957 SetAndShowDefaultStyle(attr
);
2962 /// Apply the style sheet to the buffer, for example if the styles have changed.
2963 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2966 styleSheet
= GetBuffer().GetStyleSheet();
2970 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2972 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2980 /// Sets the default style to the style under the cursor
2981 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2984 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2986 // If at the start of a paragraph, use the next position.
2987 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2989 if (GetUncombinedStyle(pos
, attr
))
2991 SetDefaultStyle(attr
);
2998 /// Returns the first visible position in the current view
2999 long wxRichTextCtrl::GetFirstVisiblePosition() const
3001 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3003 return line
->GetAbsoluteRange().GetStart();
3008 /// Get the first visible point in the window
3009 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3012 int startXUnits
, startYUnits
;
3014 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3015 GetViewStart(& startXUnits
, & startYUnits
);
3017 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3020 /// The adjusted caret position is the character position adjusted to take
3021 /// into account whether we're at the start of a paragraph, in which case
3022 /// style information should be taken from the next position, not current one.
3023 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3025 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3027 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3032 /// Get/set the selection range in character positions. -1, -1 means no selection.
3033 /// The range is in API convention, i.e. a single character selection is denoted
3035 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3037 wxRichTextRange range
= GetInternalSelectionRange();
3038 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3039 range
.SetEnd(range
.GetEnd() + 1);
3043 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3045 wxRichTextRange
range1(range
);
3046 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3047 range1
.SetEnd(range1
.GetEnd() - 1);
3049 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3051 SetInternalSelectionRange(range1
);
3055 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3057 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3060 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3062 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3065 /// Clear list for given range
3066 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3068 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3071 /// Number/renumber any list elements in the given range
3072 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3074 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3077 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3079 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3082 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3083 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3085 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3088 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3090 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3093 /// Deletes the content in the given range
3094 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3096 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3099 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3101 if (sm_availableFontNames
.GetCount() == 0)
3103 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3104 sm_availableFontNames
.Sort();
3106 return sm_availableFontNames
;
3109 void wxRichTextCtrl::ClearAvailableFontNames()
3111 sm_availableFontNames
.Clear();