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
;
1146 GetVirtualSize(& sx
, & sy
);
1151 wxRect rect
= line
->GetRect();
1153 wxSize clientSize
= GetClientSize();
1155 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1158 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1160 m_caretPosition
= position
;
1161 m_caretAtLineStart
= showAtLineStart
;
1164 /// Move caret one visual step forward: this may mean setting a flag
1165 /// and keeping the same position if we're going from the end of one line
1166 /// to the start of the next, which may be the exact same caret position.
1167 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1169 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1171 // Only do the check if we're not at the end of the paragraph (where things work OK
1173 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1175 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1179 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1181 // We're at the end of a line. See whether we need to
1182 // stay at the same actual caret position but change visual
1183 // position, or not.
1184 if (oldPosition
== lineRange
.GetEnd())
1186 if (m_caretAtLineStart
)
1188 // We're already at the start of the line, so actually move on now.
1189 m_caretPosition
= oldPosition
+ 1;
1190 m_caretAtLineStart
= false;
1194 // We're showing at the end of the line, so keep to
1195 // the same position but indicate that we're to show
1196 // at the start of the next line.
1197 m_caretPosition
= oldPosition
;
1198 m_caretAtLineStart
= true;
1200 SetDefaultStyleToCursorStyle();
1206 SetDefaultStyleToCursorStyle();
1209 /// Move caret one visual step backward: this may mean setting a flag
1210 /// and keeping the same position if we're going from the end of one line
1211 /// to the start of the next, which may be the exact same caret position.
1212 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1214 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1216 // Only do the check if we're not at the start of the paragraph (where things work OK
1218 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1220 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1224 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1226 // We're at the start of a line. See whether we need to
1227 // stay at the same actual caret position but change visual
1228 // position, or not.
1229 if (oldPosition
== lineRange
.GetStart())
1231 m_caretPosition
= oldPosition
-1;
1232 m_caretAtLineStart
= true;
1235 else if (oldPosition
== lineRange
.GetEnd())
1237 if (m_caretAtLineStart
)
1239 // We're at the start of the line, so keep the same caret position
1240 // but clear the start-of-line flag.
1241 m_caretPosition
= oldPosition
;
1242 m_caretAtLineStart
= false;
1246 // We're showing at the end of the line, so go back
1247 // to the previous character position.
1248 m_caretPosition
= oldPosition
- 1;
1250 SetDefaultStyleToCursorStyle();
1256 SetDefaultStyleToCursorStyle();
1260 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1262 long endPos
= GetBuffer().GetRange().GetEnd();
1264 if (m_caretPosition
+ noPositions
< endPos
)
1266 long oldPos
= m_caretPosition
;
1267 long newPos
= m_caretPosition
+ noPositions
;
1269 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1273 // Determine by looking at oldPos and m_caretPosition whether
1274 // we moved from the end of a line to the start of the next line, in which case
1275 // we want to adjust the caret position such that it is positioned at the
1276 // start of the next line, rather than jumping past the first character of the
1278 if (noPositions
== 1 && !extendSel
)
1279 MoveCaretForward(oldPos
);
1281 SetCaretPosition(newPos
);
1284 SetDefaultStyleToCursorStyle();
1295 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1299 if (m_caretPosition
> startPos
- noPositions
+ 1)
1301 long oldPos
= m_caretPosition
;
1302 long newPos
= m_caretPosition
- noPositions
;
1303 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1307 if (noPositions
== 1 && !extendSel
)
1308 MoveCaretBack(oldPos
);
1310 SetCaretPosition(newPos
);
1313 SetDefaultStyleToCursorStyle();
1324 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1326 return MoveDown(- noLines
, flags
);
1330 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1335 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1336 wxPoint pt
= GetCaret()->GetPosition();
1337 long newLine
= lineNumber
+ noLines
;
1339 if (lineNumber
!= -1)
1343 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1345 if (newLine
> lastLine
)
1355 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1358 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1364 wxClientDC
dc(this);
1366 dc
.SetFont(GetFont());
1368 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1370 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1372 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1373 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1374 // so we view the caret at the start of the line.
1375 bool caretLineStart
= false;
1376 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1378 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1379 wxRichTextRange lineRange
;
1381 lineRange
= thisLine
->GetAbsoluteRange();
1383 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1386 caretLineStart
= true;
1390 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1391 if (para
&& para
->GetRange().GetStart() == newPos
)
1396 long newSelEnd
= newPos
;
1398 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1402 SetCaretPosition(newPos
, caretLineStart
);
1404 SetDefaultStyleToCursorStyle();
1414 /// Move to the end of the paragraph
1415 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1417 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1420 long newPos
= para
->GetRange().GetEnd() - 1;
1421 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1425 SetCaretPosition(newPos
);
1427 SetDefaultStyleToCursorStyle();
1437 /// Move to the start of the paragraph
1438 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1440 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1443 long newPos
= para
->GetRange().GetStart() - 1;
1444 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1448 SetCaretPosition(newPos
);
1450 SetDefaultStyleToCursorStyle();
1460 /// Move to the end of the line
1461 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1463 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1467 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1468 long newPos
= lineRange
.GetEnd();
1469 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1473 SetCaretPosition(newPos
);
1475 SetDefaultStyleToCursorStyle();
1485 /// Move to the start of the line
1486 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1488 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1491 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1492 long newPos
= lineRange
.GetStart()-1;
1494 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1498 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1500 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1502 SetDefaultStyleToCursorStyle();
1512 /// Move to the start of the buffer
1513 bool wxRichTextCtrl::MoveHome(int flags
)
1515 if (m_caretPosition
!= -1)
1517 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1521 SetCaretPosition(-1);
1523 SetDefaultStyleToCursorStyle();
1533 /// Move to the end of the buffer
1534 bool wxRichTextCtrl::MoveEnd(int flags
)
1536 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1538 if (m_caretPosition
!= endPos
)
1540 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1544 SetCaretPosition(endPos
);
1546 SetDefaultStyleToCursorStyle();
1556 /// Move noPages pages up
1557 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1559 return PageDown(- noPages
, flags
);
1562 /// Move noPages pages down
1563 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1565 // Calculate which line occurs noPages * screen height further down.
1566 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1569 wxSize clientSize
= GetClientSize();
1570 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1572 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1575 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1576 long pos
= lineRange
.GetStart()-1;
1577 if (pos
!= m_caretPosition
)
1579 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1581 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1585 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1587 SetDefaultStyleToCursorStyle();
1599 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1601 return str
== wxT(" ") || str
== wxT("\t");
1604 // Finds the caret position for the next word
1605 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1607 long endPos
= GetBuffer().GetRange().GetEnd();
1611 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1613 // First skip current text to space
1614 while (i
< endPos
&& i
> -1)
1616 // i is in character, not caret positions
1617 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1618 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1619 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1623 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1630 while (i
< endPos
&& i
> -1)
1632 // i is in character, not caret positions
1633 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1634 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1635 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1636 return wxMax(-1, i
);
1638 if (text
.empty()) // End of paragraph, or maybe an image
1639 return wxMax(-1, i
- 1);
1640 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1644 // Convert to caret position
1645 return wxMax(-1, i
- 1);
1654 long i
= m_caretPosition
;
1656 // First skip white space
1657 while (i
< endPos
&& i
> -1)
1659 // i is in character, not caret positions
1660 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1661 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1663 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1665 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1670 // Next skip current text to space
1671 while (i
< endPos
&& i
> -1)
1673 // i is in character, not caret positions
1674 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1675 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1676 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1679 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1692 /// Move n words left
1693 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1695 long pos
= FindNextWordPosition(-1);
1696 if (pos
!= m_caretPosition
)
1698 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1700 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1704 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1706 SetDefaultStyleToCursorStyle();
1716 /// Move n words right
1717 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1719 long pos
= FindNextWordPosition(1);
1720 if (pos
!= m_caretPosition
)
1722 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1724 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1728 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1730 SetDefaultStyleToCursorStyle();
1741 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1743 // Only do sizing optimization for large buffers
1744 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1746 m_fullLayoutRequired
= true;
1747 m_fullLayoutTime
= wxGetLocalTimeMillis();
1748 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1749 LayoutContent(true /* onlyVisibleRect */);
1752 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1754 #if wxRICHTEXT_BUFFERED_PAINTING
1762 /// Idle-time processing
1763 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1765 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1767 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1769 m_fullLayoutRequired
= false;
1770 m_fullLayoutTime
= 0;
1771 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1772 ShowPosition(m_fullLayoutSavedPosition
);
1776 if (m_caretPositionForDefaultStyle
!= -2)
1778 // If the caret position has changed, no longer reflect the default style
1780 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1781 m_caretPositionForDefaultStyle
= -2;
1788 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1794 /// Set up scrollbars, e.g. after a resize
1795 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1800 if (GetBuffer().IsEmpty())
1802 SetScrollbars(0, 0, 0, 0, 0, 0);
1806 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1807 // of pixels. See e.g. wxVScrolledWindow for ideas.
1808 int pixelsPerUnit
= 5;
1809 wxSize clientSize
= GetClientSize();
1811 int maxHeight
= GetBuffer().GetCachedSize().y
;
1813 // Round up so we have at least maxHeight pixels
1814 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1816 int startX
= 0, startY
= 0;
1818 GetViewStart(& startX
, & startY
);
1820 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1821 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1823 // Move to previous scroll position if
1825 SetScrollbars(0, pixelsPerUnit
,
1827 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1830 /// Paint the background
1831 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1833 wxColour backgroundColour
= GetBackgroundColour();
1834 if (!backgroundColour
.Ok())
1835 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1837 // Clear the background
1838 dc
.SetBrush(wxBrush(backgroundColour
));
1839 dc
.SetPen(*wxTRANSPARENT_PEN
);
1840 wxRect
windowRect(GetClientSize());
1841 windowRect
.x
-= 2; windowRect
.y
-= 2;
1842 windowRect
.width
+= 4; windowRect
.height
+= 4;
1844 // We need to shift the rectangle to take into account
1845 // scrolling. Converting device to logical coordinates.
1846 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1847 dc
.DrawRectangle(windowRect
);
1850 #if wxRICHTEXT_BUFFERED_PAINTING
1851 /// Recreate buffer bitmap if necessary
1852 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1855 if (sz
== wxDefaultSize
)
1856 sz
= GetClientSize();
1858 if (sz
.x
< 1 || sz
.y
< 1)
1861 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1862 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1863 return m_bufferBitmap
.Ok();
1867 // ----------------------------------------------------------------------------
1868 // file IO functions
1869 // ----------------------------------------------------------------------------
1871 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1873 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1875 m_filename
= filename
;
1878 SetInsertionPoint(0);
1881 SetupScrollbars(true);
1883 wxTextCtrl::SendTextUpdatedEvent(this);
1889 wxLogError(_("File couldn't be loaded."));
1895 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1897 if (GetBuffer().SaveFile(filename
, fileType
))
1899 m_filename
= filename
;
1906 wxLogError(_("The text couldn't be saved."));
1911 // ----------------------------------------------------------------------------
1912 // wxRichTextCtrl specific functionality
1913 // ----------------------------------------------------------------------------
1915 /// Add a new paragraph of text to the end of the buffer
1916 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1918 return GetBuffer().AddParagraph(text
);
1922 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1924 return GetBuffer().AddImage(image
);
1927 // ----------------------------------------------------------------------------
1928 // selection and ranges
1929 // ----------------------------------------------------------------------------
1931 void wxRichTextCtrl::SelectAll()
1933 SetSelection(0, GetLastPosition()+1);
1934 m_selectionAnchor
= -1;
1938 void wxRichTextCtrl::SelectNone()
1940 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1941 SetSelection(-2, -2);
1942 m_selectionAnchor
= -2;
1945 static bool wxIsWordDelimiter(const wxString
& text
)
1947 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1950 /// Select the word at the given character position
1951 bool wxRichTextCtrl::SelectWord(long position
)
1953 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1956 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1960 long positionStart
= position
;
1961 long positionEnd
= position
;
1963 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1965 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1966 if (wxIsWordDelimiter(text
))
1972 if (positionStart
< para
->GetRange().GetStart())
1973 positionStart
= para
->GetRange().GetStart();
1975 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1977 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1978 if (wxIsWordDelimiter(text
))
1984 if (positionEnd
>= para
->GetRange().GetEnd())
1985 positionEnd
= para
->GetRange().GetEnd();
1987 SetSelection(positionStart
, positionEnd
+1);
1989 if (positionStart
>= 0)
1991 MoveCaret(positionStart
-1, true);
1992 SetDefaultStyleToCursorStyle();
1998 wxString
wxRichTextCtrl::GetStringSelection() const
2001 GetSelection(&from
, &to
);
2003 return GetRange(from
, to
);
2006 // ----------------------------------------------------------------------------
2008 // ----------------------------------------------------------------------------
2010 wxTextCtrlHitTestResult
2011 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2013 // implement in terms of the other overload as the native ports typically
2014 // can get the position and not (x, y) pair directly (although wxUniv
2015 // directly gets x and y -- and so overrides this method as well)
2017 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2019 if ( rc
!= wxTE_HT_UNKNOWN
)
2021 PositionToXY(pos
, x
, y
);
2027 wxTextCtrlHitTestResult
2028 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2031 wxClientDC
dc((wxRichTextCtrl
*) this);
2032 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2034 // Buffer uses logical position (relative to start of buffer)
2036 wxPoint pt2
= GetLogicalPoint(pt
);
2038 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2040 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2041 return wxTE_HT_BEFORE
;
2042 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2043 return wxTE_HT_BEYOND
;
2044 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2045 return wxTE_HT_ON_TEXT
;
2047 return wxTE_HT_UNKNOWN
;
2050 // ----------------------------------------------------------------------------
2051 // set/get the controls text
2052 // ----------------------------------------------------------------------------
2054 wxString
wxRichTextCtrl::GetValue() const
2056 return GetBuffer().GetText();
2059 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2061 // Public API for range is different from internals
2062 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2065 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2069 if (!value
.IsEmpty())
2071 // Remove empty paragraph
2072 GetBuffer().Clear();
2075 // for compatibility, don't move the cursor when doing SetValue()
2076 SetInsertionPoint(0);
2080 // still send an event for consistency
2081 if (flags
& SetValue_SendEvent
)
2082 wxTextCtrl::SendTextUpdatedEvent(this);
2087 void wxRichTextCtrl::WriteText(const wxString
& value
)
2092 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2094 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2096 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2098 if ( flags
& SetValue_SendEvent
)
2099 wxTextCtrl::SendTextUpdatedEvent(this);
2102 void wxRichTextCtrl::AppendText(const wxString
& text
)
2104 SetInsertionPointEnd();
2109 /// Write an image at the current insertion point
2110 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2112 wxRichTextImageBlock imageBlock
;
2114 wxImage image2
= image
;
2115 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2116 return WriteImage(imageBlock
);
2121 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2123 wxRichTextImageBlock imageBlock
;
2126 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2127 return WriteImage(imageBlock
);
2132 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2134 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2137 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2141 wxRichTextImageBlock imageBlock
;
2143 wxImage image
= bitmap
.ConvertToImage();
2144 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2145 return WriteImage(imageBlock
);
2151 /// Insert a newline (actually paragraph) at the current insertion point.
2152 bool wxRichTextCtrl::Newline()
2154 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2157 /// Insert a line break at the current insertion point.
2158 bool wxRichTextCtrl::LineBreak()
2161 text
= wxRichTextLineBreakChar
;
2162 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2165 // ----------------------------------------------------------------------------
2166 // Clipboard operations
2167 // ----------------------------------------------------------------------------
2169 void wxRichTextCtrl::Copy()
2173 wxRichTextRange range
= GetInternalSelectionRange();
2174 GetBuffer().CopyToClipboard(range
);
2178 void wxRichTextCtrl::Cut()
2182 wxRichTextRange range
= GetInternalSelectionRange();
2183 GetBuffer().CopyToClipboard(range
);
2185 DeleteSelectedContent();
2191 void wxRichTextCtrl::Paste()
2195 BeginBatchUndo(_("Paste"));
2197 long newPos
= m_caretPosition
;
2198 DeleteSelectedContent(& newPos
);
2200 GetBuffer().PasteFromClipboard(newPos
);
2206 void wxRichTextCtrl::DeleteSelection()
2208 if (CanDeleteSelection())
2210 DeleteSelectedContent();
2214 bool wxRichTextCtrl::HasSelection() const
2216 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2219 bool wxRichTextCtrl::CanCopy() const
2221 // Can copy if there's a selection
2222 return HasSelection();
2225 bool wxRichTextCtrl::CanCut() const
2227 return HasSelection() && IsEditable();
2230 bool wxRichTextCtrl::CanPaste() const
2232 if ( !IsEditable() )
2235 return GetBuffer().CanPasteFromClipboard();
2238 bool wxRichTextCtrl::CanDeleteSelection() const
2240 return HasSelection() && IsEditable();
2244 // ----------------------------------------------------------------------------
2246 // ----------------------------------------------------------------------------
2248 void wxRichTextCtrl::SetEditable(bool editable
)
2250 m_editable
= editable
;
2253 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2257 m_caretPosition
= pos
- 1;
2260 void wxRichTextCtrl::SetInsertionPointEnd()
2262 long pos
= GetLastPosition();
2263 SetInsertionPoint(pos
);
2266 long wxRichTextCtrl::GetInsertionPoint() const
2268 return m_caretPosition
+1;
2271 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2273 return GetBuffer().GetRange().GetEnd();
2276 // If the return values from and to are the same, there is no
2278 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2280 *from
= m_selectionRange
.GetStart();
2281 *to
= m_selectionRange
.GetEnd();
2282 if ((*to
) != -1 && (*to
) != -2)
2286 bool wxRichTextCtrl::IsEditable() const
2291 // ----------------------------------------------------------------------------
2293 // ----------------------------------------------------------------------------
2295 void wxRichTextCtrl::SetSelection(long from
, long to
)
2297 // if from and to are both -1, it means (in wxWidgets) that all text should
2299 if ( (from
== -1) && (to
== -1) )
2302 to
= GetLastPosition()+1;
2305 DoSetSelection(from
, to
);
2308 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2310 m_selectionAnchor
= from
;
2311 m_selectionRange
.SetRange(from
, to
-1);
2313 m_caretPosition
= from
-1;
2319 // ----------------------------------------------------------------------------
2321 // ----------------------------------------------------------------------------
2323 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2324 const wxString
& value
)
2326 BeginBatchUndo(_("Replace"));
2328 DeleteSelectedContent();
2330 DoWriteText(value
, SetValue_SelectionOnly
);
2335 void wxRichTextCtrl::Remove(long from
, long to
)
2339 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2346 bool wxRichTextCtrl::IsModified() const
2348 return m_buffer
.IsModified();
2351 void wxRichTextCtrl::MarkDirty()
2353 m_buffer
.Modify(true);
2356 void wxRichTextCtrl::DiscardEdits()
2358 m_caretPositionForDefaultStyle
= -2;
2359 m_buffer
.Modify(false);
2360 m_buffer
.GetCommandProcessor()->ClearCommands();
2363 int wxRichTextCtrl::GetNumberOfLines() const
2365 return GetBuffer().GetParagraphCount();
2368 // ----------------------------------------------------------------------------
2369 // Positions <-> coords
2370 // ----------------------------------------------------------------------------
2372 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2374 return GetBuffer().XYToPosition(x
, y
);
2377 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2379 return GetBuffer().PositionToXY(pos
, x
, y
);
2382 // ----------------------------------------------------------------------------
2384 // ----------------------------------------------------------------------------
2386 void wxRichTextCtrl::ShowPosition(long pos
)
2388 if (!IsPositionVisible(pos
))
2389 ScrollIntoView(pos
-1, WXK_DOWN
);
2392 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2394 return GetBuffer().GetParagraphLength(lineNo
);
2397 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2399 return GetBuffer().GetParagraphText(lineNo
);
2402 // ----------------------------------------------------------------------------
2404 // ----------------------------------------------------------------------------
2406 void wxRichTextCtrl::Undo()
2410 GetCommandProcessor()->Undo();
2414 void wxRichTextCtrl::Redo()
2418 GetCommandProcessor()->Redo();
2422 bool wxRichTextCtrl::CanUndo() const
2424 return GetCommandProcessor()->CanUndo();
2427 bool wxRichTextCtrl::CanRedo() const
2429 return GetCommandProcessor()->CanRedo();
2432 // ----------------------------------------------------------------------------
2433 // implementation details
2434 // ----------------------------------------------------------------------------
2436 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2438 SetValue(event
.GetString());
2439 GetEventHandler()->ProcessEvent(event
);
2442 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2444 // By default, load the first file into the text window.
2445 if (event
.GetNumberOfFiles() > 0)
2447 LoadFile(event
.GetFiles()[0]);
2451 wxSize
wxRichTextCtrl::DoGetBestSize() const
2453 return wxSize(10, 10);
2456 // ----------------------------------------------------------------------------
2457 // standard handlers for standard edit menu events
2458 // ----------------------------------------------------------------------------
2460 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2465 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2470 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2475 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2480 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2485 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2490 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2492 event
.Enable( CanCut() );
2495 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2497 event
.Enable( CanCopy() );
2500 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2502 event
.Enable( CanDeleteSelection() );
2505 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2507 event
.Enable( CanPaste() );
2510 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2512 event
.Enable( CanUndo() );
2513 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2516 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2518 event
.Enable( CanRedo() );
2519 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2522 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2527 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2529 event
.Enable(GetLastPosition() > 0);
2532 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2534 if (event
.GetEventObject() != this)
2542 m_contextMenu
= new wxMenu
;
2543 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2544 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2545 m_contextMenu
->AppendSeparator();
2546 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2547 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2548 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2549 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2550 m_contextMenu
->AppendSeparator();
2551 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2553 PopupMenu(m_contextMenu
);
2557 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2559 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2562 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2564 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2567 // extended style setting operation with flags including:
2568 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2569 // see richtextbuffer.h for more details.
2571 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2573 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2576 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2578 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2581 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2583 return GetBuffer().GetDefaultStyle();
2586 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2588 return GetBuffer().GetStyle(position
, style
);
2591 // get the common set of styles for the range
2592 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2594 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2597 /// Get the content (uncombined) attributes for this position.
2598 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2600 return GetBuffer().GetUncombinedStyle(position
, style
);
2603 /// Set font, and also the buffer attributes
2604 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2606 wxControl::SetFont(font
);
2608 wxTextAttr attr
= GetBuffer().GetAttributes();
2610 GetBuffer().SetBasicStyle(attr
);
2612 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2618 /// Transform logical to physical
2619 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2622 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2627 /// Transform physical to logical
2628 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2631 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2636 /// Position the caret
2637 void wxRichTextCtrl::PositionCaret()
2642 //wxLogDebug(wxT("PositionCaret"));
2645 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2647 wxPoint newPt
= caretRect
.GetPosition();
2648 wxSize newSz
= caretRect
.GetSize();
2649 wxPoint pt
= GetPhysicalPoint(newPt
);
2650 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2652 GetCaret()->Move(pt
);
2653 GetCaret()->SetSize(newSz
);
2658 /// Get the caret height and position for the given character position
2659 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2661 wxClientDC
dc(this);
2662 dc
.SetFont(GetFont());
2669 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2671 // Caret height can't be zero
2673 height
= dc
.GetCharHeight();
2675 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2682 /// Gets the line for the visible caret position. If the caret is
2683 /// shown at the very end of the line, it means the next character is actually
2684 /// on the following line. So let's get the line we're expecting to find
2685 /// if this is the case.
2686 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2688 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2689 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2692 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2693 if (caretPosition
== lineRange
.GetStart()-1 &&
2694 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2696 if (!m_caretAtLineStart
)
2697 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2704 /// Move the caret to the given character position
2705 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2707 if (GetBuffer().GetDirty())
2710 if (pos
<= GetBuffer().GetRange().GetEnd())
2712 SetCaretPosition(pos
, showAtLineStart
);
2722 /// Layout the buffer: which we must do before certain operations, such as
2723 /// setting the caret position.
2724 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2726 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2728 wxRect
availableSpace(GetClientSize());
2729 if (availableSpace
.width
== 0)
2730 availableSpace
.width
= 10;
2731 if (availableSpace
.height
== 0)
2732 availableSpace
.height
= 10;
2734 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2735 if (onlyVisibleRect
)
2737 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2738 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2741 wxClientDC
dc(this);
2742 dc
.SetFont(GetFont());
2746 GetBuffer().Defragment();
2747 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2748 GetBuffer().Layout(dc
, availableSpace
, flags
);
2749 GetBuffer().SetDirty(false);
2758 /// Is all of the selection bold?
2759 bool wxRichTextCtrl::IsSelectionBold()
2764 wxRichTextRange range
= GetSelectionRange();
2765 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2766 attr
.SetFontWeight(wxBOLD
);
2768 return HasCharacterAttributes(range
, attr
);
2772 // If no selection, then we need to combine current style with default style
2773 // to see what the effect would be if we started typing.
2775 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2777 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2778 if (GetStyle(pos
, attr
))
2780 if (IsDefaultStyleShowing())
2781 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2782 return attr
.GetFontWeight() == wxBOLD
;
2788 /// Is all of the selection italics?
2789 bool wxRichTextCtrl::IsSelectionItalics()
2793 wxRichTextRange range
= GetSelectionRange();
2795 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2796 attr
.SetFontStyle(wxITALIC
);
2798 return HasCharacterAttributes(range
, attr
);
2802 // If no selection, then we need to combine current style with default style
2803 // to see what the effect would be if we started typing.
2805 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2807 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2808 if (GetStyle(pos
, attr
))
2810 if (IsDefaultStyleShowing())
2811 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2812 return attr
.GetFontStyle() == wxITALIC
;
2818 /// Is all of the selection underlined?
2819 bool wxRichTextCtrl::IsSelectionUnderlined()
2823 wxRichTextRange range
= GetSelectionRange();
2825 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2826 attr
.SetFontUnderlined(true);
2828 return HasCharacterAttributes(range
, attr
);
2832 // If no selection, then we need to combine current style with default style
2833 // to see what the effect would be if we started typing.
2835 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2836 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2838 if (GetStyle(pos
, attr
))
2840 if (IsDefaultStyleShowing())
2841 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2842 return attr
.GetFontUnderlined();
2848 /// Apply bold to the selection
2849 bool wxRichTextCtrl::ApplyBoldToSelection()
2852 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2853 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2856 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2858 SetAndShowDefaultStyle(attr
);
2862 /// Apply italic to the selection
2863 bool wxRichTextCtrl::ApplyItalicToSelection()
2866 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2867 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2870 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2872 SetAndShowDefaultStyle(attr
);
2876 /// Apply underline to the selection
2877 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2880 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2881 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2884 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2886 SetAndShowDefaultStyle(attr
);
2890 /// Is all of the selection aligned according to the specified flag?
2891 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2893 wxRichTextRange range
;
2895 range
= GetSelectionRange();
2897 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2900 attr
.SetAlignment(alignment
);
2902 return HasParagraphAttributes(range
, attr
);
2905 /// Apply alignment to the selection
2906 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2909 attr
.SetAlignment(alignment
);
2911 return SetStyle(GetSelectionRange(), attr
);
2914 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2916 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2921 /// Apply a named style to the selection
2922 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2924 // Flags are defined within each definition, so only certain
2925 // attributes are applied.
2926 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2928 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2930 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2932 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2934 wxRichTextRange range
;
2937 range
= GetSelectionRange();
2940 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2941 range
= wxRichTextRange(pos
, pos
+1);
2944 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2947 // Make sure the attr has the style name
2948 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2950 attr
.SetParagraphStyleName(def
->GetName());
2952 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2953 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2954 // to change its style independently.
2955 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2958 attr
.SetCharacterStyleName(def
->GetName());
2961 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2964 SetAndShowDefaultStyle(attr
);
2969 /// Apply the style sheet to the buffer, for example if the styles have changed.
2970 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2973 styleSheet
= GetBuffer().GetStyleSheet();
2977 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2979 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2987 /// Sets the default style to the style under the cursor
2988 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2991 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2993 // If at the start of a paragraph, use the next position.
2994 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2996 if (GetUncombinedStyle(pos
, attr
))
2998 SetDefaultStyle(attr
);
3005 /// Returns the first visible position in the current view
3006 long wxRichTextCtrl::GetFirstVisiblePosition() const
3008 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3010 return line
->GetAbsoluteRange().GetStart();
3015 /// Get the first visible point in the window
3016 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3019 int startXUnits
, startYUnits
;
3021 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3022 GetViewStart(& startXUnits
, & startYUnits
);
3024 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3027 /// The adjusted caret position is the character position adjusted to take
3028 /// into account whether we're at the start of a paragraph, in which case
3029 /// style information should be taken from the next position, not current one.
3030 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3032 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3034 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3039 /// Get/set the selection range in character positions. -1, -1 means no selection.
3040 /// The range is in API convention, i.e. a single character selection is denoted
3042 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3044 wxRichTextRange range
= GetInternalSelectionRange();
3045 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3046 range
.SetEnd(range
.GetEnd() + 1);
3050 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3052 wxRichTextRange
range1(range
);
3053 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3054 range1
.SetEnd(range1
.GetEnd() - 1);
3056 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3058 SetInternalSelectionRange(range1
);
3062 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3064 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3067 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3069 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3072 /// Clear list for given range
3073 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3075 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3078 /// Number/renumber any list elements in the given range
3079 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3081 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3084 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3086 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3089 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3090 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3092 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3095 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3097 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3100 /// Deletes the content in the given range
3101 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3103 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3106 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3108 if (sm_availableFontNames
.GetCount() == 0)
3110 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3111 sm_availableFontNames
.Sort();
3113 return sm_availableFontNames
;
3116 void wxRichTextCtrl::ClearAvailableFontNames()
3118 sm_availableFontNames
.Clear();