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
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
)
59 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
61 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
63 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
64 EVT_PAINT(wxRichTextCtrl::OnPaint
)
65 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
66 EVT_IDLE(wxRichTextCtrl::OnIdle
)
67 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
68 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
69 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
70 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
71 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
72 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
73 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
74 EVT_CHAR(wxRichTextCtrl::OnChar
)
75 EVT_SIZE(wxRichTextCtrl::OnSize
)
76 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
77 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
78 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
79 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
80 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
82 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
83 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
85 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
86 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
88 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
89 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
91 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
92 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
94 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
95 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
97 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
98 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
100 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
101 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
108 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
110 wxRichTextCtrl::wxRichTextCtrl()
111 : wxScrollHelper(this)
116 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
118 const wxString
& value
,
122 const wxValidator
& validator
,
123 const wxString
& name
)
124 : wxScrollHelper(this)
127 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
131 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
132 const wxValidator
& validator
, const wxString
& name
)
134 if (!wxControl::Create(parent
, id
, pos
, size
,
135 style
|wxFULL_REPAINT_ON_RESIZE
,
141 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
144 if (style
& wxTE_READONLY
)
147 // The base attributes must all have default values
148 wxTextAttr attributes
;
149 attributes
.SetFont(GetFont());
150 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
151 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
152 attributes
.SetLineSpacing(10);
153 attributes
.SetParagraphSpacingAfter(10);
154 attributes
.SetParagraphSpacingBefore(0);
155 attributes
.SetTextEffects(0);
156 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
158 SetBasicStyle(attributes
);
160 // The default attributes will be merged with base attributes, so
161 // can be empty to begin with
162 wxTextAttr defaultAttributes
;
163 SetDefaultStyle(defaultAttributes
);
165 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
166 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
169 GetBuffer().SetRichTextCtrl(this);
171 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
174 // Tell the sizers to use the given or best size
175 SetInitialSize(size
);
177 #if wxRICHTEXT_BUFFERED_PAINTING
179 RecreateBuffer(size
);
182 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
183 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
185 SetCursor(m_textCursor
);
187 if (!value
.IsEmpty())
190 GetBuffer().AddEventHandler(this);
195 wxRichTextCtrl::~wxRichTextCtrl()
197 GetBuffer().RemoveEventHandler(this);
199 delete m_contextMenu
;
202 /// Member initialisation
203 void wxRichTextCtrl::Init()
205 m_contextMenu
= NULL
;
207 m_caretPosition
= -1;
208 m_selectionRange
.SetRange(-2, -2);
209 m_selectionAnchor
= -2;
211 m_caretAtLineStart
= false;
213 m_fullLayoutRequired
= false;
214 m_fullLayoutTime
= 0;
215 m_fullLayoutSavedPosition
= 0;
216 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
217 m_caretPositionForDefaultStyle
= -2;
220 void wxRichTextCtrl::DoThaw()
222 if (GetBuffer().GetDirty())
230 void wxRichTextCtrl::Clear()
232 m_buffer
.ResetAndClearCommands();
233 m_buffer
.SetDirty(true);
234 m_caretPosition
= -1;
235 m_caretPositionForDefaultStyle
= -2;
236 m_caretAtLineStart
= false;
237 m_selectionRange
.SetRange(-2, -2);
245 wxTextCtrl::SendTextUpdatedEvent(this);
249 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
251 if (GetCaret() && GetCaret()->IsVisible())
255 #if wxRICHTEXT_BUFFERED_PAINTING
256 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
266 dc
.SetFont(GetFont());
268 // Paint the background
271 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
273 wxRect
drawingArea(GetUpdateRegion().GetBox());
274 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
276 wxRect
availableSpace(GetClientSize());
277 if (GetBuffer().GetDirty())
279 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
280 GetBuffer().SetDirty(false);
284 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
287 if (GetCaret() && !GetCaret()->IsVisible())
293 // Empty implementation, to prevent flicker
294 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
298 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
302 if (!GetCaret()->IsVisible())
311 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
313 if (GetCaret() && GetCaret()->IsVisible())
320 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
326 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
332 dc
.SetFont(GetFont());
335 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
337 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
339 m_dragStart
= event
.GetLogicalPosition(dc
);
345 bool caretAtLineStart
= false;
347 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
349 // If we're at the start of a line (but not first in para)
350 // then we should keep the caret showing at the start of the line
351 // by showing the m_caretAtLineStart flag.
352 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
353 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
355 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
356 caretAtLineStart
= true;
360 MoveCaret(position
, caretAtLineStart
);
361 SetDefaultStyleToCursorStyle();
368 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
373 if (GetCapture() == this)
376 // See if we clicked on a URL
379 dc
.SetFont(GetFont());
382 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
383 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
385 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
387 wxRichTextEvent
cmdEvent(
388 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
390 cmdEvent
.SetEventObject(this);
391 cmdEvent
.SetPosition(m_caretPosition
+1);
393 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
396 if (GetStyle(position
, attr
))
398 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
400 wxString urlTarget
= attr
.GetURL();
401 if (!urlTarget
.IsEmpty())
403 wxMouseEvent
mouseEvent(event
);
405 long startPos
= 0, endPos
= 0;
406 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
409 startPos
= obj
->GetRange().GetStart();
410 endPos
= obj
->GetRange().GetEnd();
413 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
414 InitCommandEvent(urlEvent
);
416 urlEvent
.SetString(urlTarget
);
418 GetEventHandler()->ProcessEvent(urlEvent
);
428 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
432 dc
.SetFont(GetFont());
435 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
436 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
438 // See if we need to change the cursor
441 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
444 if (GetStyle(position
, attr
))
446 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
448 SetCursor(m_urlCursor
);
450 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
452 SetCursor(m_textCursor
);
457 SetCursor(m_textCursor
);
460 if (!event
.Dragging())
466 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
468 // TODO: test closeness
470 bool caretAtLineStart
= false;
472 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
474 // If we're at the start of a line (but not first in para)
475 // then we should keep the caret showing at the start of the line
476 // by showing the m_caretAtLineStart flag.
477 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
478 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
480 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
481 caretAtLineStart
= true;
485 if (m_caretPosition
!= position
)
487 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
489 MoveCaret(position
, caretAtLineStart
);
490 SetDefaultStyleToCursorStyle();
499 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
503 wxRichTextEvent
cmdEvent(
504 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
506 cmdEvent
.SetEventObject(this);
507 cmdEvent
.SetPosition(m_caretPosition
+1);
509 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
513 /// Left-double-click
514 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
516 wxRichTextEvent
cmdEvent(
517 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
519 cmdEvent
.SetEventObject(this);
520 cmdEvent
.SetPosition(m_caretPosition
+1);
522 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
524 SelectWord(GetCaretPosition()+1);
529 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
531 wxRichTextEvent
cmdEvent(
532 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
534 cmdEvent
.SetEventObject(this);
535 cmdEvent
.SetPosition(m_caretPosition
+1);
537 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
542 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
546 flags
|= wxRICHTEXT_CTRL_DOWN
;
547 if (event
.ShiftDown())
548 flags
|= wxRICHTEXT_SHIFT_DOWN
;
550 flags
|= wxRICHTEXT_ALT_DOWN
;
552 if (event
.GetKeyCode() == WXK_LEFT
||
553 event
.GetKeyCode() == WXK_RIGHT
||
554 event
.GetKeyCode() == WXK_UP
||
555 event
.GetKeyCode() == WXK_DOWN
||
556 event
.GetKeyCode() == WXK_HOME
||
557 event
.GetKeyCode() == WXK_PAGEUP
||
558 event
.GetKeyCode() == WXK_PAGEDOWN
||
559 event
.GetKeyCode() == WXK_END
||
561 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
562 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
563 event
.GetKeyCode() == WXK_NUMPAD_UP
||
564 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
565 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
566 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
567 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
568 event
.GetKeyCode() == WXK_NUMPAD_END
)
570 KeyboardNavigate(event
.GetKeyCode(), flags
);
574 // all the other keys modify the controls contents which shouldn't be
575 // possible if we're read-only
582 if (event
.GetKeyCode() == WXK_RETURN
)
584 BeginBatchUndo(_("Insert Text"));
586 long newPos
= m_caretPosition
;
588 DeleteSelectedContent(& newPos
);
590 if (event
.ShiftDown())
593 text
= wxRichTextLineBreakChar
;
594 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
597 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
600 SetDefaultStyleToCursorStyle();
602 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
604 wxRichTextEvent
cmdEvent(
605 wxEVT_COMMAND_RICHTEXT_RETURN
,
607 cmdEvent
.SetEventObject(this);
608 cmdEvent
.SetFlags(flags
);
609 cmdEvent
.SetPosition(newPos
+1);
611 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
613 // Generate conventional event
614 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
615 InitCommandEvent(textEvent
);
617 GetEventHandler()->ProcessEvent(textEvent
);
621 else if (event
.GetKeyCode() == WXK_BACK
)
623 BeginBatchUndo(_("Delete Text"));
625 // Submit range in character positions, which are greater than caret positions,
626 // so subtract 1 for deleted character and add 1 for conversion to character position.
627 if (m_caretPosition
> -1 && !HasSelection())
629 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
632 DeleteSelectedContent();
636 if (GetLastPosition() == -1)
640 m_caretPosition
= -1;
642 SetDefaultStyleToCursorStyle();
645 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
647 wxRichTextEvent
cmdEvent(
648 wxEVT_COMMAND_RICHTEXT_DELETE
,
650 cmdEvent
.SetEventObject(this);
651 cmdEvent
.SetFlags(flags
);
652 cmdEvent
.SetPosition(m_caretPosition
+1);
653 GetEventHandler()->ProcessEvent(cmdEvent
);
657 else if (event
.GetKeyCode() == WXK_DELETE
)
659 BeginBatchUndo(_("Delete Text"));
661 // Submit range in character positions, which are greater than caret positions,
662 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
664 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
667 DeleteSelectedContent();
671 if (GetLastPosition() == -1)
675 m_caretPosition
= -1;
677 SetDefaultStyleToCursorStyle();
680 wxRichTextEvent
cmdEvent(
681 wxEVT_COMMAND_RICHTEXT_DELETE
,
683 cmdEvent
.SetEventObject(this);
684 cmdEvent
.SetFlags(flags
);
685 cmdEvent
.SetPosition(m_caretPosition
+1);
686 GetEventHandler()->ProcessEvent(cmdEvent
);
692 long keycode
= event
.GetKeyCode();
765 case WXK_NUMPAD_SPACE
:
767 case WXK_NUMPAD_ENTER
:
772 case WXK_NUMPAD_HOME
:
773 case WXK_NUMPAD_LEFT
:
775 case WXK_NUMPAD_RIGHT
:
776 case WXK_NUMPAD_DOWN
:
777 case WXK_NUMPAD_PAGEUP
:
778 case WXK_NUMPAD_PAGEDOWN
:
780 case WXK_NUMPAD_BEGIN
:
781 case WXK_NUMPAD_INSERT
:
782 case WXK_NUMPAD_DELETE
:
783 case WXK_NUMPAD_EQUAL
:
784 case WXK_NUMPAD_MULTIPLY
:
786 case WXK_NUMPAD_SEPARATOR
:
787 case WXK_NUMPAD_SUBTRACT
:
788 case WXK_NUMPAD_DECIMAL
:
789 case WXK_WINDOWS_LEFT
:
797 if (event
.CmdDown() || event
.AltDown())
803 wxRichTextEvent
cmdEvent(
804 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
806 cmdEvent
.SetEventObject(this);
807 cmdEvent
.SetFlags(flags
);
809 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
811 cmdEvent
.SetCharacter((wxChar
) keycode
);
813 cmdEvent
.SetPosition(m_caretPosition
+1);
815 if (keycode
== wxT('\t'))
817 // See if we need to promote or demote the selection or paragraph at the cursor
818 // position, instead of inserting a tab.
819 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
820 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
821 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
823 wxRichTextRange range
;
825 range
= GetSelectionRange();
827 range
= para
->GetRange().FromInternal();
829 int promoteBy
= event
.ShiftDown() ? 1 : -1;
831 PromoteList(promoteBy
, range
, NULL
);
833 GetEventHandler()->ProcessEvent(cmdEvent
);
839 BeginBatchUndo(_("Insert Text"));
841 long newPos
= m_caretPosition
;
842 DeleteSelectedContent(& newPos
);
845 wxString str
= event
.GetUnicodeKey();
847 wxString str
= (wxChar
) event
.GetKeyCode();
849 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
853 SetDefaultStyleToCursorStyle();
854 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
856 GetEventHandler()->ProcessEvent(cmdEvent
);
864 /// Delete content if there is a selection, e.g. when pressing a key.
865 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
869 long pos
= m_selectionRange
.GetStart();
870 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
871 m_selectionRange
.SetRange(-2, -2);
881 /// Keyboard navigation
885 Left: left one character
886 Right: right one character
889 Ctrl-Left: left one word
890 Ctrl-Right: right one word
891 Ctrl-Up: previous paragraph start
892 Ctrl-Down: next start of paragraph
895 Ctrl-Home: start of document
896 Ctrl-End: end of document
898 Page-Down: Down a screen
902 Ctrl-Alt-PgUp: Start of window
903 Ctrl-Alt-PgDn: End of window
904 F8: Start selection mode
905 Esc: End selection mode
907 Adding Shift does the above but starts/extends selection.
912 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
914 bool success
= false;
916 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
918 if (flags
& wxRICHTEXT_CTRL_DOWN
)
919 success
= WordRight(1, flags
);
921 success
= MoveRight(1, flags
);
923 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
925 if (flags
& wxRICHTEXT_CTRL_DOWN
)
926 success
= WordLeft(1, flags
);
928 success
= MoveLeft(1, flags
);
930 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
932 if (flags
& wxRICHTEXT_CTRL_DOWN
)
933 success
= MoveToParagraphStart(flags
);
935 success
= MoveUp(1, flags
);
937 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
939 if (flags
& wxRICHTEXT_CTRL_DOWN
)
940 success
= MoveToParagraphEnd(flags
);
942 success
= MoveDown(1, flags
);
944 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
946 success
= PageUp(1, flags
);
948 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
950 success
= PageDown(1, flags
);
952 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
954 if (flags
& wxRICHTEXT_CTRL_DOWN
)
955 success
= MoveHome(flags
);
957 success
= MoveToLineStart(flags
);
959 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
961 if (flags
& wxRICHTEXT_CTRL_DOWN
)
962 success
= MoveEnd(flags
);
964 success
= MoveToLineEnd(flags
);
969 ScrollIntoView(m_caretPosition
, keyCode
);
970 SetDefaultStyleToCursorStyle();
976 /// Extend the selection. Selections are in caret positions.
977 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
979 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
981 // If not currently selecting, start selecting
982 if (m_selectionRange
.GetStart() == -2)
984 m_selectionAnchor
= oldPos
;
987 m_selectionRange
.SetRange(newPos
+1, oldPos
);
989 m_selectionRange
.SetRange(oldPos
+1, newPos
);
993 // Always ensure that the selection range start is greater than
995 if (newPos
> m_selectionAnchor
)
996 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
998 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1001 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1003 wxLogDebug(wxT("Strange selection range"));
1012 /// Scroll into view, returning true if we scrolled.
1013 /// This takes a _caret_ position.
1014 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1016 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1022 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1024 int startXUnits
, startYUnits
;
1025 GetViewStart(& startXUnits
, & startYUnits
);
1026 int startY
= startYUnits
* ppuY
;
1029 GetVirtualSize(& sx
, & sy
);
1035 wxRect rect
= line
->GetRect();
1037 bool scrolled
= false;
1039 wxSize clientSize
= GetClientSize();
1042 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1043 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1044 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1045 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1047 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1049 // Make it scroll so this item is at the bottom
1051 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1052 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1054 // If we're still off the screen, scroll another line down
1055 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1058 if (startYUnits
!= yUnits
)
1060 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1064 else if (rect
.y
< startY
)
1066 // Make it scroll so this item is at the top
1069 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1071 if (startYUnits
!= yUnits
)
1073 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1079 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1080 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1081 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1082 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1084 if (rect
.y
< startY
)
1086 // Make it scroll so this item is at the top
1089 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1091 if (startYUnits
!= yUnits
)
1093 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1097 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1099 // Make it scroll so this item is at the bottom
1101 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1102 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1104 // If we're still off the screen, scroll another line down
1105 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1108 if (startYUnits
!= yUnits
)
1110 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1120 /// Is the given position visible on the screen?
1121 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1123 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1129 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1132 GetViewStart(& startX
, & startY
);
1134 startY
= startY
* ppuY
;
1136 wxRect rect
= line
->GetRect();
1137 wxSize clientSize
= GetClientSize();
1139 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1142 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1144 m_caretPosition
= position
;
1145 m_caretAtLineStart
= showAtLineStart
;
1148 /// Move caret one visual step forward: this may mean setting a flag
1149 /// and keeping the same position if we're going from the end of one line
1150 /// to the start of the next, which may be the exact same caret position.
1151 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1153 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1155 // Only do the check if we're not at the end of the paragraph (where things work OK
1157 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1159 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1163 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1165 // We're at the end of a line. See whether we need to
1166 // stay at the same actual caret position but change visual
1167 // position, or not.
1168 if (oldPosition
== lineRange
.GetEnd())
1170 if (m_caretAtLineStart
)
1172 // We're already at the start of the line, so actually move on now.
1173 m_caretPosition
= oldPosition
+ 1;
1174 m_caretAtLineStart
= false;
1178 // We're showing at the end of the line, so keep to
1179 // the same position but indicate that we're to show
1180 // at the start of the next line.
1181 m_caretPosition
= oldPosition
;
1182 m_caretAtLineStart
= true;
1184 SetDefaultStyleToCursorStyle();
1190 SetDefaultStyleToCursorStyle();
1193 /// Move caret one visual step backward: this may mean setting a flag
1194 /// and keeping the same position if we're going from the end of one line
1195 /// to the start of the next, which may be the exact same caret position.
1196 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1198 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1200 // Only do the check if we're not at the start of the paragraph (where things work OK
1202 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1204 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1208 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1210 // We're at the start of a line. See whether we need to
1211 // stay at the same actual caret position but change visual
1212 // position, or not.
1213 if (oldPosition
== lineRange
.GetStart())
1215 m_caretPosition
= oldPosition
-1;
1216 m_caretAtLineStart
= true;
1219 else if (oldPosition
== lineRange
.GetEnd())
1221 if (m_caretAtLineStart
)
1223 // We're at the start of the line, so keep the same caret position
1224 // but clear the start-of-line flag.
1225 m_caretPosition
= oldPosition
;
1226 m_caretAtLineStart
= false;
1230 // We're showing at the end of the line, so go back
1231 // to the previous character position.
1232 m_caretPosition
= oldPosition
- 1;
1234 SetDefaultStyleToCursorStyle();
1240 SetDefaultStyleToCursorStyle();
1244 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1246 long endPos
= GetBuffer().GetRange().GetEnd();
1248 if (m_caretPosition
+ noPositions
< endPos
)
1250 long oldPos
= m_caretPosition
;
1251 long newPos
= m_caretPosition
+ noPositions
;
1253 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1257 // Determine by looking at oldPos and m_caretPosition whether
1258 // we moved from the end of a line to the start of the next line, in which case
1259 // we want to adjust the caret position such that it is positioned at the
1260 // start of the next line, rather than jumping past the first character of the
1262 if (noPositions
== 1 && !extendSel
)
1263 MoveCaretForward(oldPos
);
1265 SetCaretPosition(newPos
);
1268 SetDefaultStyleToCursorStyle();
1279 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1283 if (m_caretPosition
> startPos
- noPositions
+ 1)
1285 long oldPos
= m_caretPosition
;
1286 long newPos
= m_caretPosition
- noPositions
;
1287 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1291 if (noPositions
== 1 && !extendSel
)
1292 MoveCaretBack(oldPos
);
1294 SetCaretPosition(newPos
);
1297 SetDefaultStyleToCursorStyle();
1308 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1310 return MoveDown(- noLines
, flags
);
1314 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1319 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1320 wxPoint pt
= GetCaret()->GetPosition();
1321 long newLine
= lineNumber
+ noLines
;
1323 if (lineNumber
!= -1)
1327 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1329 if (newLine
> lastLine
)
1339 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1342 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1348 wxClientDC
dc(this);
1350 dc
.SetFont(GetFont());
1352 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1354 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1356 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1357 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1358 // so we view the caret at the start of the line.
1359 bool caretLineStart
= false;
1360 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1362 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1363 wxRichTextRange lineRange
;
1365 lineRange
= thisLine
->GetAbsoluteRange();
1367 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1370 caretLineStart
= true;
1374 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1375 if (para
&& para
->GetRange().GetStart() == newPos
)
1380 long newSelEnd
= newPos
;
1382 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1386 SetCaretPosition(newPos
, caretLineStart
);
1388 SetDefaultStyleToCursorStyle();
1398 /// Move to the end of the paragraph
1399 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1401 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1404 long newPos
= para
->GetRange().GetEnd() - 1;
1405 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1409 SetCaretPosition(newPos
);
1411 SetDefaultStyleToCursorStyle();
1421 /// Move to the start of the paragraph
1422 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1424 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1427 long newPos
= para
->GetRange().GetStart() - 1;
1428 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1432 SetCaretPosition(newPos
);
1434 SetDefaultStyleToCursorStyle();
1444 /// Move to the end of the line
1445 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1447 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1451 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1452 long newPos
= lineRange
.GetEnd();
1453 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1457 SetCaretPosition(newPos
);
1459 SetDefaultStyleToCursorStyle();
1469 /// Move to the start of the line
1470 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1472 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1475 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1476 long newPos
= lineRange
.GetStart()-1;
1478 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1482 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1484 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1486 SetDefaultStyleToCursorStyle();
1496 /// Move to the start of the buffer
1497 bool wxRichTextCtrl::MoveHome(int flags
)
1499 if (m_caretPosition
!= -1)
1501 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1505 SetCaretPosition(-1);
1507 SetDefaultStyleToCursorStyle();
1517 /// Move to the end of the buffer
1518 bool wxRichTextCtrl::MoveEnd(int flags
)
1520 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1522 if (m_caretPosition
!= endPos
)
1524 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1528 SetCaretPosition(endPos
);
1530 SetDefaultStyleToCursorStyle();
1540 /// Move noPages pages up
1541 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1543 return PageDown(- noPages
, flags
);
1546 /// Move noPages pages down
1547 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1549 // Calculate which line occurs noPages * screen height further down.
1550 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1553 wxSize clientSize
= GetClientSize();
1554 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1556 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1559 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1560 long pos
= lineRange
.GetStart()-1;
1561 if (pos
!= m_caretPosition
)
1563 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1565 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1569 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1571 SetDefaultStyleToCursorStyle();
1583 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1585 return str
== wxT(" ") || str
== wxT("\t");
1588 // Finds the caret position for the next word
1589 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1591 long endPos
= GetBuffer().GetRange().GetEnd();
1595 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1597 // First skip current text to space
1598 while (i
< endPos
&& i
> -1)
1600 // i is in character, not caret positions
1601 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1602 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1603 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1607 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
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()))
1620 return wxMax(-1, i
);
1622 if (text
.empty()) // End of paragraph, or maybe an image
1623 return wxMax(-1, i
- 1);
1624 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1628 // Convert to caret position
1629 return wxMax(-1, i
- 1);
1638 long i
= m_caretPosition
;
1640 // First skip white space
1641 while (i
< endPos
&& i
> -1)
1643 // i is in character, not caret positions
1644 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1645 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1647 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1649 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1654 // Next skip current text to space
1655 while (i
< endPos
&& i
> -1)
1657 // i is in character, not caret positions
1658 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1659 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1660 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1663 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1676 /// Move n words left
1677 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1679 long pos
= FindNextWordPosition(-1);
1680 if (pos
!= m_caretPosition
)
1682 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1684 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1688 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1690 SetDefaultStyleToCursorStyle();
1700 /// Move n words right
1701 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1703 long pos
= FindNextWordPosition(1);
1704 if (pos
!= m_caretPosition
)
1706 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1708 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1712 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1714 SetDefaultStyleToCursorStyle();
1725 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1727 // Only do sizing optimization for large buffers
1728 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1730 m_fullLayoutRequired
= true;
1731 m_fullLayoutTime
= wxGetLocalTimeMillis();
1732 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1733 LayoutContent(true /* onlyVisibleRect */);
1736 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1738 #if wxRICHTEXT_BUFFERED_PAINTING
1746 /// Idle-time processing
1747 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1749 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1751 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1753 m_fullLayoutRequired
= false;
1754 m_fullLayoutTime
= 0;
1755 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1756 ShowPosition(m_fullLayoutSavedPosition
);
1760 if (m_caretPositionForDefaultStyle
!= -2)
1762 // If the caret position has changed, no longer reflect the default style
1764 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1765 m_caretPositionForDefaultStyle
= -2;
1772 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1778 /// Set up scrollbars, e.g. after a resize
1779 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1784 if (GetBuffer().IsEmpty())
1786 SetScrollbars(0, 0, 0, 0, 0, 0);
1790 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1791 // of pixels. See e.g. wxVScrolledWindow for ideas.
1792 int pixelsPerUnit
= 5;
1793 wxSize clientSize
= GetClientSize();
1795 int maxHeight
= GetBuffer().GetCachedSize().y
;
1797 // Round up so we have at least maxHeight pixels
1798 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1800 int startX
= 0, startY
= 0;
1802 GetViewStart(& startX
, & startY
);
1804 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1805 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1807 // Move to previous scroll position if
1809 SetScrollbars(0, pixelsPerUnit
,
1811 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1814 /// Paint the background
1815 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1817 wxColour backgroundColour
= GetBackgroundColour();
1818 if (!backgroundColour
.Ok())
1819 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1821 // Clear the background
1822 dc
.SetBrush(wxBrush(backgroundColour
));
1823 dc
.SetPen(*wxTRANSPARENT_PEN
);
1824 wxRect
windowRect(GetClientSize());
1825 windowRect
.x
-= 2; windowRect
.y
-= 2;
1826 windowRect
.width
+= 4; windowRect
.height
+= 4;
1828 // We need to shift the rectangle to take into account
1829 // scrolling. Converting device to logical coordinates.
1830 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1831 dc
.DrawRectangle(windowRect
);
1834 #if wxRICHTEXT_BUFFERED_PAINTING
1835 /// Recreate buffer bitmap if necessary
1836 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1839 if (sz
== wxDefaultSize
)
1840 sz
= GetClientSize();
1842 if (sz
.x
< 1 || sz
.y
< 1)
1845 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1846 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1847 return m_bufferBitmap
.Ok();
1851 // ----------------------------------------------------------------------------
1852 // file IO functions
1853 // ----------------------------------------------------------------------------
1855 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1857 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1859 m_filename
= filename
;
1862 SetInsertionPoint(0);
1865 SetupScrollbars(true);
1867 wxTextCtrl::SendTextUpdatedEvent(this);
1873 wxLogError(_("File couldn't be loaded."));
1879 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1881 if (GetBuffer().SaveFile(filename
, fileType
))
1883 m_filename
= filename
;
1890 wxLogError(_("The text couldn't be saved."));
1895 // ----------------------------------------------------------------------------
1896 // wxRichTextCtrl specific functionality
1897 // ----------------------------------------------------------------------------
1899 /// Add a new paragraph of text to the end of the buffer
1900 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1902 return GetBuffer().AddParagraph(text
);
1906 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1908 return GetBuffer().AddImage(image
);
1911 // ----------------------------------------------------------------------------
1912 // selection and ranges
1913 // ----------------------------------------------------------------------------
1915 void wxRichTextCtrl::SelectAll()
1917 SetSelection(0, GetLastPosition()+1);
1918 m_selectionAnchor
= -1;
1922 void wxRichTextCtrl::SelectNone()
1924 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1925 SetSelection(-2, -2);
1926 m_selectionAnchor
= -2;
1929 static bool wxIsWordDelimiter(const wxString
& text
)
1931 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1934 /// Select the word at the given character position
1935 bool wxRichTextCtrl::SelectWord(long position
)
1937 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1940 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1944 long positionStart
= position
;
1945 long positionEnd
= position
;
1947 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1949 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1950 if (wxIsWordDelimiter(text
))
1956 if (positionStart
< para
->GetRange().GetStart())
1957 positionStart
= para
->GetRange().GetStart();
1959 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1961 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1962 if (wxIsWordDelimiter(text
))
1968 if (positionEnd
>= para
->GetRange().GetEnd())
1969 positionEnd
= para
->GetRange().GetEnd();
1971 SetSelection(positionStart
, positionEnd
+1);
1973 if (positionStart
>= 0)
1975 MoveCaret(positionStart
-1, true);
1976 SetDefaultStyleToCursorStyle();
1982 wxString
wxRichTextCtrl::GetStringSelection() const
1985 GetSelection(&from
, &to
);
1987 return GetRange(from
, to
);
1990 // ----------------------------------------------------------------------------
1992 // ----------------------------------------------------------------------------
1994 wxTextCtrlHitTestResult
1995 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1997 // implement in terms of the other overload as the native ports typically
1998 // can get the position and not (x, y) pair directly (although wxUniv
1999 // directly gets x and y -- and so overrides this method as well)
2001 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2003 if ( rc
!= wxTE_HT_UNKNOWN
)
2005 PositionToXY(pos
, x
, y
);
2011 wxTextCtrlHitTestResult
2012 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2015 wxClientDC
dc((wxRichTextCtrl
*) this);
2016 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2018 // Buffer uses logical position (relative to start of buffer)
2020 wxPoint pt2
= GetLogicalPoint(pt
);
2022 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2024 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2025 return wxTE_HT_BEFORE
;
2026 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2027 return wxTE_HT_BEYOND
;
2028 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2029 return wxTE_HT_ON_TEXT
;
2031 return wxTE_HT_UNKNOWN
;
2034 // ----------------------------------------------------------------------------
2035 // set/get the controls text
2036 // ----------------------------------------------------------------------------
2038 wxString
wxRichTextCtrl::GetValue() const
2040 return GetBuffer().GetText();
2043 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2045 // Public API for range is different from internals
2046 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2049 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2053 if (!value
.IsEmpty())
2055 // Remove empty paragraph
2056 GetBuffer().Clear();
2059 // for compatibility, don't move the cursor when doing SetValue()
2060 SetInsertionPoint(0);
2064 // still send an event for consistency
2065 if (flags
& SetValue_SendEvent
)
2066 wxTextCtrl::SendTextUpdatedEvent(this);
2071 void wxRichTextCtrl::WriteText(const wxString
& value
)
2076 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2078 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2080 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2082 if ( flags
& SetValue_SendEvent
)
2083 wxTextCtrl::SendTextUpdatedEvent(this);
2086 void wxRichTextCtrl::AppendText(const wxString
& text
)
2088 SetInsertionPointEnd();
2093 /// Write an image at the current insertion point
2094 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2096 wxRichTextImageBlock imageBlock
;
2098 wxImage image2
= image
;
2099 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2100 return WriteImage(imageBlock
);
2105 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2107 wxRichTextImageBlock imageBlock
;
2110 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2111 return WriteImage(imageBlock
);
2116 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2118 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2121 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2125 wxRichTextImageBlock imageBlock
;
2127 wxImage image
= bitmap
.ConvertToImage();
2128 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2129 return WriteImage(imageBlock
);
2135 /// Insert a newline (actually paragraph) at the current insertion point.
2136 bool wxRichTextCtrl::Newline()
2138 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2141 /// Insert a line break at the current insertion point.
2142 bool wxRichTextCtrl::LineBreak()
2145 text
= wxRichTextLineBreakChar
;
2146 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2149 // ----------------------------------------------------------------------------
2150 // Clipboard operations
2151 // ----------------------------------------------------------------------------
2153 void wxRichTextCtrl::Copy()
2157 wxRichTextRange range
= GetInternalSelectionRange();
2158 GetBuffer().CopyToClipboard(range
);
2162 void wxRichTextCtrl::Cut()
2166 wxRichTextRange range
= GetInternalSelectionRange();
2167 GetBuffer().CopyToClipboard(range
);
2169 DeleteSelectedContent();
2175 void wxRichTextCtrl::Paste()
2179 BeginBatchUndo(_("Paste"));
2181 long newPos
= m_caretPosition
;
2182 DeleteSelectedContent(& newPos
);
2184 GetBuffer().PasteFromClipboard(newPos
);
2190 void wxRichTextCtrl::DeleteSelection()
2192 if (CanDeleteSelection())
2194 DeleteSelectedContent();
2198 bool wxRichTextCtrl::HasSelection() const
2200 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2203 bool wxRichTextCtrl::CanCopy() const
2205 // Can copy if there's a selection
2206 return HasSelection();
2209 bool wxRichTextCtrl::CanCut() const
2211 return HasSelection() && IsEditable();
2214 bool wxRichTextCtrl::CanPaste() const
2216 if ( !IsEditable() )
2219 return GetBuffer().CanPasteFromClipboard();
2222 bool wxRichTextCtrl::CanDeleteSelection() const
2224 return HasSelection() && IsEditable();
2228 // ----------------------------------------------------------------------------
2230 // ----------------------------------------------------------------------------
2232 void wxRichTextCtrl::SetEditable(bool editable
)
2234 m_editable
= editable
;
2237 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2241 m_caretPosition
= pos
- 1;
2246 void wxRichTextCtrl::SetInsertionPointEnd()
2248 long pos
= GetLastPosition();
2249 SetInsertionPoint(pos
);
2252 long wxRichTextCtrl::GetInsertionPoint() const
2254 return m_caretPosition
+1;
2257 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2259 return GetBuffer().GetRange().GetEnd();
2262 // If the return values from and to are the same, there is no
2264 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2266 *from
= m_selectionRange
.GetStart();
2267 *to
= m_selectionRange
.GetEnd();
2268 if ((*to
) != -1 && (*to
) != -2)
2272 bool wxRichTextCtrl::IsEditable() const
2277 // ----------------------------------------------------------------------------
2279 // ----------------------------------------------------------------------------
2281 void wxRichTextCtrl::SetSelection(long from
, long to
)
2283 // if from and to are both -1, it means (in wxWidgets) that all text should
2285 if ( (from
== -1) && (to
== -1) )
2288 to
= GetLastPosition()+1;
2291 DoSetSelection(from
, to
);
2294 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2296 m_selectionAnchor
= from
;
2297 m_selectionRange
.SetRange(from
, to
-1);
2299 m_caretPosition
= from
-1;
2305 // ----------------------------------------------------------------------------
2307 // ----------------------------------------------------------------------------
2309 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2310 const wxString
& value
)
2312 BeginBatchUndo(_("Replace"));
2314 DeleteSelectedContent();
2316 DoWriteText(value
, SetValue_SelectionOnly
);
2321 void wxRichTextCtrl::Remove(long from
, long to
)
2325 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2332 bool wxRichTextCtrl::IsModified() const
2334 return m_buffer
.IsModified();
2337 void wxRichTextCtrl::MarkDirty()
2339 m_buffer
.Modify(true);
2342 void wxRichTextCtrl::DiscardEdits()
2344 m_caretPositionForDefaultStyle
= -2;
2345 m_buffer
.Modify(false);
2346 m_buffer
.GetCommandProcessor()->ClearCommands();
2349 int wxRichTextCtrl::GetNumberOfLines() const
2351 return GetBuffer().GetParagraphCount();
2354 // ----------------------------------------------------------------------------
2355 // Positions <-> coords
2356 // ----------------------------------------------------------------------------
2358 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2360 return GetBuffer().XYToPosition(x
, y
);
2363 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2365 return GetBuffer().PositionToXY(pos
, x
, y
);
2368 // ----------------------------------------------------------------------------
2370 // ----------------------------------------------------------------------------
2372 void wxRichTextCtrl::ShowPosition(long pos
)
2374 if (!IsPositionVisible(pos
))
2375 ScrollIntoView(pos
-1, WXK_DOWN
);
2378 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2380 return GetBuffer().GetParagraphLength(lineNo
);
2383 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2385 return GetBuffer().GetParagraphText(lineNo
);
2388 // ----------------------------------------------------------------------------
2390 // ----------------------------------------------------------------------------
2392 void wxRichTextCtrl::Undo()
2396 GetCommandProcessor()->Undo();
2400 void wxRichTextCtrl::Redo()
2404 GetCommandProcessor()->Redo();
2408 bool wxRichTextCtrl::CanUndo() const
2410 return GetCommandProcessor()->CanUndo();
2413 bool wxRichTextCtrl::CanRedo() const
2415 return GetCommandProcessor()->CanRedo();
2418 // ----------------------------------------------------------------------------
2419 // implementation details
2420 // ----------------------------------------------------------------------------
2422 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2424 SetValue(event
.GetString());
2425 GetEventHandler()->ProcessEvent(event
);
2428 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2430 // By default, load the first file into the text window.
2431 if (event
.GetNumberOfFiles() > 0)
2433 LoadFile(event
.GetFiles()[0]);
2437 wxSize
wxRichTextCtrl::DoGetBestSize() const
2439 return wxSize(10, 10);
2442 // ----------------------------------------------------------------------------
2443 // standard handlers for standard edit menu events
2444 // ----------------------------------------------------------------------------
2446 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2451 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2456 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2461 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2466 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2471 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2476 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2478 event
.Enable( CanCut() );
2481 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2483 event
.Enable( CanCopy() );
2486 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2488 event
.Enable( CanDeleteSelection() );
2491 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2493 event
.Enable( CanPaste() );
2496 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2498 event
.Enable( CanUndo() );
2499 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2502 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2504 event
.Enable( CanRedo() );
2505 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2508 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2513 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2515 event
.Enable(GetLastPosition() > 0);
2518 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2520 if (event
.GetEventObject() != this)
2528 m_contextMenu
= new wxMenu
;
2529 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2530 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2531 m_contextMenu
->AppendSeparator();
2532 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2533 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2534 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2535 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2536 m_contextMenu
->AppendSeparator();
2537 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2539 PopupMenu(m_contextMenu
);
2543 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2545 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2548 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2550 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2553 // extended style setting operation with flags including:
2554 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2555 // see richtextbuffer.h for more details.
2557 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2559 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2562 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2564 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2567 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2569 return GetBuffer().GetDefaultStyle();
2572 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2574 return GetBuffer().GetStyle(position
, style
);
2577 // get the common set of styles for the range
2578 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2580 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2583 /// Get the content (uncombined) attributes for this position.
2584 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2586 return GetBuffer().GetUncombinedStyle(position
, style
);
2589 /// Set font, and also the buffer attributes
2590 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2592 wxControl::SetFont(font
);
2594 wxTextAttr attr
= GetBuffer().GetAttributes();
2596 GetBuffer().SetBasicStyle(attr
);
2598 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2604 /// Transform logical to physical
2605 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2608 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2613 /// Transform physical to logical
2614 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2617 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2622 /// Position the caret
2623 void wxRichTextCtrl::PositionCaret()
2628 //wxLogDebug(wxT("PositionCaret"));
2631 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2633 wxPoint newPt
= caretRect
.GetPosition();
2634 wxSize newSz
= caretRect
.GetSize();
2635 wxPoint pt
= GetPhysicalPoint(newPt
);
2636 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2638 GetCaret()->Move(pt
);
2639 GetCaret()->SetSize(newSz
);
2644 /// Get the caret height and position for the given character position
2645 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2647 wxClientDC
dc(this);
2648 dc
.SetFont(GetFont());
2655 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2657 // Caret height can't be zero
2659 height
= dc
.GetCharHeight();
2661 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2668 /// Gets the line for the visible caret position. If the caret is
2669 /// shown at the very end of the line, it means the next character is actually
2670 /// on the following line. So let's get the line we're expecting to find
2671 /// if this is the case.
2672 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2674 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2675 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2678 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2679 if (caretPosition
== lineRange
.GetStart()-1 &&
2680 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2682 if (!m_caretAtLineStart
)
2683 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2690 /// Move the caret to the given character position
2691 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2693 if (GetBuffer().GetDirty())
2696 if (pos
<= GetBuffer().GetRange().GetEnd())
2698 SetCaretPosition(pos
, showAtLineStart
);
2708 /// Layout the buffer: which we must do before certain operations, such as
2709 /// setting the caret position.
2710 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2712 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2714 wxRect
availableSpace(GetClientSize());
2715 if (availableSpace
.width
== 0)
2716 availableSpace
.width
= 10;
2717 if (availableSpace
.height
== 0)
2718 availableSpace
.height
= 10;
2720 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2721 if (onlyVisibleRect
)
2723 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2724 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2727 wxClientDC
dc(this);
2728 dc
.SetFont(GetFont());
2732 GetBuffer().Defragment();
2733 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2734 GetBuffer().Layout(dc
, availableSpace
, flags
);
2735 GetBuffer().SetDirty(false);
2744 /// Is all of the selection bold?
2745 bool wxRichTextCtrl::IsSelectionBold()
2750 wxRichTextRange range
= GetSelectionRange();
2751 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2752 attr
.SetFontWeight(wxBOLD
);
2754 return HasCharacterAttributes(range
, attr
);
2758 // If no selection, then we need to combine current style with default style
2759 // to see what the effect would be if we started typing.
2761 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2763 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2764 if (GetStyle(pos
, attr
))
2766 if (IsDefaultStyleShowing())
2767 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2768 return attr
.GetFontWeight() == wxBOLD
;
2774 /// Is all of the selection italics?
2775 bool wxRichTextCtrl::IsSelectionItalics()
2779 wxRichTextRange range
= GetSelectionRange();
2781 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2782 attr
.SetFontStyle(wxITALIC
);
2784 return HasCharacterAttributes(range
, attr
);
2788 // If no selection, then we need to combine current style with default style
2789 // to see what the effect would be if we started typing.
2791 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2793 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2794 if (GetStyle(pos
, attr
))
2796 if (IsDefaultStyleShowing())
2797 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2798 return attr
.GetFontStyle() == wxITALIC
;
2804 /// Is all of the selection underlined?
2805 bool wxRichTextCtrl::IsSelectionUnderlined()
2809 wxRichTextRange range
= GetSelectionRange();
2811 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2812 attr
.SetFontUnderlined(true);
2814 return HasCharacterAttributes(range
, attr
);
2818 // If no selection, then we need to combine current style with default style
2819 // to see what the effect would be if we started typing.
2821 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2822 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2824 if (GetStyle(pos
, attr
))
2826 if (IsDefaultStyleShowing())
2827 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2828 return attr
.GetFontUnderlined();
2834 /// Apply bold to the selection
2835 bool wxRichTextCtrl::ApplyBoldToSelection()
2838 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2839 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2842 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2844 SetAndShowDefaultStyle(attr
);
2848 /// Apply italic to the selection
2849 bool wxRichTextCtrl::ApplyItalicToSelection()
2852 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2853 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2856 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2858 SetAndShowDefaultStyle(attr
);
2862 /// Apply underline to the selection
2863 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2866 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2867 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2870 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2872 SetAndShowDefaultStyle(attr
);
2876 /// Is all of the selection aligned according to the specified flag?
2877 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2879 wxRichTextRange range
;
2881 range
= GetSelectionRange();
2883 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2886 attr
.SetAlignment(alignment
);
2888 return HasParagraphAttributes(range
, attr
);
2891 /// Apply alignment to the selection
2892 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2895 attr
.SetAlignment(alignment
);
2897 return SetStyle(GetSelectionRange(), attr
);
2900 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2902 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2907 /// Apply a named style to the selection
2908 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2910 // Flags are defined within each definition, so only certain
2911 // attributes are applied.
2912 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2914 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2916 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2918 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2920 wxRichTextRange range
;
2923 range
= GetSelectionRange();
2926 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2927 range
= wxRichTextRange(pos
, pos
+1);
2930 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2933 // Make sure the attr has the style name
2934 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2936 attr
.SetParagraphStyleName(def
->GetName());
2938 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2939 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2940 // to change its style independently.
2941 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2944 attr
.SetCharacterStyleName(def
->GetName());
2947 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2950 SetAndShowDefaultStyle(attr
);
2955 /// Apply the style sheet to the buffer, for example if the styles have changed.
2956 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2959 styleSheet
= GetBuffer().GetStyleSheet();
2963 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2965 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2973 /// Sets the default style to the style under the cursor
2974 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2977 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2979 // If at the start of a paragraph, use the next position.
2980 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2982 if (GetUncombinedStyle(pos
, attr
))
2984 SetDefaultStyle(attr
);
2991 /// Returns the first visible position in the current view
2992 long wxRichTextCtrl::GetFirstVisiblePosition() const
2994 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2996 return line
->GetAbsoluteRange().GetStart();
3001 /// Get the first visible point in the window
3002 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3005 int startXUnits
, startYUnits
;
3007 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3008 GetViewStart(& startXUnits
, & startYUnits
);
3010 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3013 /// The adjusted caret position is the character position adjusted to take
3014 /// into account whether we're at the start of a paragraph, in which case
3015 /// style information should be taken from the next position, not current one.
3016 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3018 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3020 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3025 /// Get/set the selection range in character positions. -1, -1 means no selection.
3026 /// The range is in API convention, i.e. a single character selection is denoted
3028 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3030 wxRichTextRange range
= GetInternalSelectionRange();
3031 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3032 range
.SetEnd(range
.GetEnd() + 1);
3036 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3038 wxRichTextRange
range1(range
);
3039 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3040 range1
.SetEnd(range1
.GetEnd() - 1);
3042 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3044 SetInternalSelectionRange(range1
);
3048 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3050 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3053 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3055 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3058 /// Clear list for given range
3059 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3061 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3064 /// Number/renumber any list elements in the given range
3065 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3067 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3070 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3072 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3075 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3076 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3078 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3081 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3083 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3086 /// Deletes the content in the given range
3087 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3089 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3092 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3094 if (sm_availableFontNames
.GetCount() == 0)
3096 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3097 sm_availableFontNames
.Sort();
3099 return sm_availableFontNames
;
3102 void wxRichTextCtrl::ClearAvailableFontNames()
3104 sm_availableFontNames
.Clear();
3107 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3109 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3111 wxTextAttrEx basicStyle
= GetBasicStyle();
3112 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3113 SetBasicStyle(basicStyle
);
3114 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));