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 (!wxTextCtrlBase::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 wxTextAttrEx attributes
;
153 attributes
.SetFont(GetFont());
154 attributes
.SetTextColour(*wxBLACK
);
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 wxTextAttrEx defaultAttributes
;
167 SetDefaultStyle(defaultAttributes
);
169 SetBackgroundColour(*wxWHITE
);
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)
238 void wxRichTextCtrl::Clear()
240 m_buffer
.ResetAndClearCommands();
241 m_buffer
.SetDirty(true);
242 m_caretPosition
= -1;
243 m_caretPositionForDefaultStyle
= -2;
244 m_caretAtLineStart
= false;
245 m_selectionRange
.SetRange(-2, -2);
247 SetScrollbars(0, 0, 0, 0, 0, 0);
249 if (m_freezeCount
== 0)
254 SendTextUpdatedEvent();
258 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
260 if (GetCaret() && GetCaret()->IsVisible())
264 #if wxRICHTEXT_BUFFERED_PAINTING
265 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
271 if (m_freezeCount
> 0)
274 dc
.SetFont(GetFont());
276 // Paint the background
279 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
281 wxRect
drawingArea(GetUpdateRegion().GetBox());
282 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
284 wxRect
availableSpace(GetClientSize());
285 if (GetBuffer().GetDirty())
287 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
288 GetBuffer().SetDirty(false);
292 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
295 if (GetCaret() && !GetCaret()->IsVisible())
301 // Empty implementation, to prevent flicker
302 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
306 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
310 if (!GetCaret()->IsVisible())
319 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
321 if (GetCaret() && GetCaret()->IsVisible())
328 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
334 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
340 dc
.SetFont(GetFont());
343 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
345 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
347 m_dragStart
= event
.GetLogicalPosition(dc
);
353 bool caretAtLineStart
= false;
355 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
357 // If we're at the start of a line (but not first in para)
358 // then we should keep the caret showing at the start of the line
359 // by showing the m_caretAtLineStart flag.
360 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
361 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
363 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
364 caretAtLineStart
= true;
368 MoveCaret(position
, caretAtLineStart
);
369 SetDefaultStyleToCursorStyle();
376 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
381 if (GetCapture() == this)
384 // See if we clicked on a URL
387 dc
.SetFont(GetFont());
390 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
391 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
393 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
395 wxRichTextEvent
cmdEvent(
396 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
398 cmdEvent
.SetEventObject(this);
399 cmdEvent
.SetPosition(m_caretPosition
+1);
401 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
404 if (GetStyle(position
, attr
))
406 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
408 wxString urlTarget
= attr
.GetURL();
409 if (!urlTarget
.IsEmpty())
411 wxMouseEvent
mouseEvent(event
);
413 long startPos
= 0, endPos
= 0;
414 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
417 startPos
= obj
->GetRange().GetStart();
418 endPos
= obj
->GetRange().GetEnd();
421 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
422 InitCommandEvent(urlEvent
);
424 urlEvent
.SetString(urlTarget
);
426 GetEventHandler()->ProcessEvent(urlEvent
);
436 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
440 dc
.SetFont(GetFont());
443 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
444 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
446 // See if we need to change the cursor
449 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
452 if (GetStyle(position
, attr
))
454 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
456 SetCursor(m_urlCursor
);
458 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
460 SetCursor(m_textCursor
);
465 SetCursor(m_textCursor
);
468 if (!event
.Dragging())
474 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
476 // TODO: test closeness
478 bool caretAtLineStart
= false;
480 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
482 // If we're at the start of a line (but not first in para)
483 // then we should keep the caret showing at the start of the line
484 // by showing the m_caretAtLineStart flag.
485 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
486 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
488 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
489 caretAtLineStart
= true;
493 if (m_caretPosition
!= position
)
495 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
497 MoveCaret(position
, caretAtLineStart
);
498 SetDefaultStyleToCursorStyle();
507 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& WXUNUSED(event
))
511 wxRichTextEvent
cmdEvent(
512 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
514 cmdEvent
.SetEventObject(this);
515 cmdEvent
.SetPosition(m_caretPosition
+1);
517 GetEventHandler()->ProcessEvent(cmdEvent
);
520 /// Left-double-click
521 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
523 wxRichTextEvent
cmdEvent(
524 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
526 cmdEvent
.SetEventObject(this);
527 cmdEvent
.SetPosition(m_caretPosition
+1);
529 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
531 SelectWord(GetCaretPosition()+1);
536 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
538 wxRichTextEvent
cmdEvent(
539 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
541 cmdEvent
.SetEventObject(this);
542 cmdEvent
.SetPosition(m_caretPosition
+1);
544 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
549 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
553 flags
|= wxRICHTEXT_CTRL_DOWN
;
554 if (event
.ShiftDown())
555 flags
|= wxRICHTEXT_SHIFT_DOWN
;
557 flags
|= wxRICHTEXT_ALT_DOWN
;
559 if (event
.GetKeyCode() == WXK_LEFT
||
560 event
.GetKeyCode() == WXK_RIGHT
||
561 event
.GetKeyCode() == WXK_UP
||
562 event
.GetKeyCode() == WXK_DOWN
||
563 event
.GetKeyCode() == WXK_HOME
||
564 event
.GetKeyCode() == WXK_PAGEUP
||
565 event
.GetKeyCode() == WXK_PAGEDOWN
||
566 event
.GetKeyCode() == WXK_END
||
568 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
569 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
570 event
.GetKeyCode() == WXK_NUMPAD_UP
||
571 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
572 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
573 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
574 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
575 event
.GetKeyCode() == WXK_NUMPAD_END
)
577 KeyboardNavigate(event
.GetKeyCode(), flags
);
581 // all the other keys modify the controls contents which shouldn't be
582 // possible if we're read-only
589 if (event
.GetKeyCode() == WXK_RETURN
)
591 BeginBatchUndo(_("Insert Text"));
593 long newPos
= m_caretPosition
;
595 DeleteSelectedContent(& newPos
);
597 if (event
.ShiftDown())
600 text
= wxRichTextLineBreakChar
;
601 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
604 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
607 SetDefaultStyleToCursorStyle();
609 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
611 wxRichTextEvent
cmdEvent(
612 wxEVT_COMMAND_RICHTEXT_RETURN
,
614 cmdEvent
.SetEventObject(this);
615 cmdEvent
.SetFlags(flags
);
616 cmdEvent
.SetPosition(newPos
+1);
618 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
620 // Generate conventional event
621 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
622 InitCommandEvent(textEvent
);
624 GetEventHandler()->ProcessEvent(textEvent
);
628 else if (event
.GetKeyCode() == WXK_BACK
)
630 BeginBatchUndo(_("Delete Text"));
632 // Submit range in character positions, which are greater than caret positions,
633 // so subtract 1 for deleted character and add 1 for conversion to character position.
634 if (m_caretPosition
> -1 && !HasSelection())
636 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
639 DeleteSelectedContent();
643 if (GetLastPosition() == -1)
647 m_caretPosition
= -1;
649 SetDefaultStyleToCursorStyle();
652 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
654 wxRichTextEvent
cmdEvent(
655 wxEVT_COMMAND_RICHTEXT_DELETE
,
657 cmdEvent
.SetEventObject(this);
658 cmdEvent
.SetFlags(flags
);
659 cmdEvent
.SetPosition(m_caretPosition
+1);
660 GetEventHandler()->ProcessEvent(cmdEvent
);
664 else if (event
.GetKeyCode() == WXK_DELETE
)
666 BeginBatchUndo(_("Delete Text"));
668 // Submit range in character positions, which are greater than caret positions,
669 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
671 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
674 DeleteSelectedContent();
678 if (GetLastPosition() == -1)
682 m_caretPosition
= -1;
684 SetDefaultStyleToCursorStyle();
687 wxRichTextEvent
cmdEvent(
688 wxEVT_COMMAND_RICHTEXT_DELETE
,
690 cmdEvent
.SetEventObject(this);
691 cmdEvent
.SetFlags(flags
);
692 cmdEvent
.SetPosition(m_caretPosition
+1);
693 GetEventHandler()->ProcessEvent(cmdEvent
);
699 long keycode
= event
.GetKeyCode();
772 case WXK_NUMPAD_SPACE
:
774 case WXK_NUMPAD_ENTER
:
779 case WXK_NUMPAD_HOME
:
780 case WXK_NUMPAD_LEFT
:
782 case WXK_NUMPAD_RIGHT
:
783 case WXK_NUMPAD_DOWN
:
784 case WXK_NUMPAD_PAGEUP
:
785 case WXK_NUMPAD_PAGEDOWN
:
787 case WXK_NUMPAD_BEGIN
:
788 case WXK_NUMPAD_INSERT
:
789 case WXK_NUMPAD_DELETE
:
790 case WXK_NUMPAD_EQUAL
:
791 case WXK_NUMPAD_MULTIPLY
:
793 case WXK_NUMPAD_SEPARATOR
:
794 case WXK_NUMPAD_SUBTRACT
:
795 case WXK_NUMPAD_DECIMAL
:
796 case WXK_WINDOWS_LEFT
:
804 if (event
.CmdDown() || event
.AltDown())
810 wxRichTextEvent
cmdEvent(
811 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
813 cmdEvent
.SetEventObject(this);
814 cmdEvent
.SetFlags(flags
);
816 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
818 cmdEvent
.SetCharacter((wxChar
) keycode
);
820 cmdEvent
.SetPosition(m_caretPosition
+1);
822 if (keycode
== wxT('\t'))
824 // See if we need to promote or demote the selection or paragraph at the cursor
825 // position, instead of inserting a tab.
826 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
827 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
828 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
830 wxRichTextRange range
;
832 range
= GetSelectionRange();
834 range
= para
->GetRange().FromInternal();
836 int promoteBy
= event
.ShiftDown() ? 1 : -1;
838 PromoteList(promoteBy
, range
, NULL
);
840 GetEventHandler()->ProcessEvent(cmdEvent
);
846 BeginBatchUndo(_("Insert Text"));
848 long newPos
= m_caretPosition
;
849 DeleteSelectedContent(& newPos
);
852 wxString str
= event
.GetUnicodeKey();
854 wxString str
= (wxChar
) event
.GetKeyCode();
856 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
860 SetDefaultStyleToCursorStyle();
861 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
863 GetEventHandler()->ProcessEvent(cmdEvent
);
871 /// Delete content if there is a selection, e.g. when pressing a key.
872 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
876 long pos
= m_selectionRange
.GetStart();
877 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
878 m_selectionRange
.SetRange(-2, -2);
888 /// Keyboard navigation
892 Left: left one character
893 Right: right one character
896 Ctrl-Left: left one word
897 Ctrl-Right: right one word
898 Ctrl-Up: previous paragraph start
899 Ctrl-Down: next start of paragraph
902 Ctrl-Home: start of document
903 Ctrl-End: end of document
905 Page-Down: Down a screen
909 Ctrl-Alt-PgUp: Start of window
910 Ctrl-Alt-PgDn: End of window
911 F8: Start selection mode
912 Esc: End selection mode
914 Adding Shift does the above but starts/extends selection.
919 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
921 bool success
= false;
923 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
925 if (flags
& wxRICHTEXT_CTRL_DOWN
)
926 success
= WordRight(1, flags
);
928 success
= MoveRight(1, flags
);
930 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
932 if (flags
& wxRICHTEXT_CTRL_DOWN
)
933 success
= WordLeft(1, flags
);
935 success
= MoveLeft(1, flags
);
937 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
939 if (flags
& wxRICHTEXT_CTRL_DOWN
)
940 success
= MoveToParagraphStart(flags
);
942 success
= MoveUp(1, flags
);
944 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
946 if (flags
& wxRICHTEXT_CTRL_DOWN
)
947 success
= MoveToParagraphEnd(flags
);
949 success
= MoveDown(1, flags
);
951 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
953 success
= PageUp(1, flags
);
955 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
957 success
= PageDown(1, flags
);
959 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
961 if (flags
& wxRICHTEXT_CTRL_DOWN
)
962 success
= MoveHome(flags
);
964 success
= MoveToLineStart(flags
);
966 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
968 if (flags
& wxRICHTEXT_CTRL_DOWN
)
969 success
= MoveEnd(flags
);
971 success
= MoveToLineEnd(flags
);
976 ScrollIntoView(m_caretPosition
, keyCode
);
977 SetDefaultStyleToCursorStyle();
983 /// Extend the selection. Selections are in caret positions.
984 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
986 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
988 // If not currently selecting, start selecting
989 if (m_selectionRange
.GetStart() == -2)
991 m_selectionAnchor
= oldPos
;
994 m_selectionRange
.SetRange(newPos
+1, oldPos
);
996 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1000 // Always ensure that the selection range start is greater than
1002 if (newPos
> m_selectionAnchor
)
1003 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1005 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1008 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1010 wxLogDebug(wxT("Strange selection range"));
1019 /// Scroll into view, returning true if we scrolled.
1020 /// This takes a _caret_ position.
1021 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1023 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1029 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1031 int startXUnits
, startYUnits
;
1032 GetViewStart(& startXUnits
, & startYUnits
);
1033 int startY
= startYUnits
* ppuY
;
1036 GetVirtualSize(& sx
, & sy
);
1042 wxRect rect
= line
->GetRect();
1044 bool scrolled
= false;
1046 wxSize clientSize
= GetClientSize();
1049 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1050 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1051 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1052 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1054 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1056 // Make it scroll so this item is at the bottom
1058 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1059 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1061 // If we're still off the screen, scroll another line down
1062 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1065 if (startYUnits
!= yUnits
)
1067 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1071 else if (rect
.y
< startY
)
1073 // Make it scroll so this item is at the top
1076 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1078 if (startYUnits
!= yUnits
)
1080 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1086 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1087 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1088 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1089 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1091 if (rect
.y
< startY
)
1093 // Make it scroll so this item is at the top
1096 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1098 if (startYUnits
!= yUnits
)
1100 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1104 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1106 // Make it scroll so this item is at the bottom
1108 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1109 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1111 // If we're still off the screen, scroll another line down
1112 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1115 if (startYUnits
!= yUnits
)
1117 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1127 /// Is the given position visible on the screen?
1128 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1130 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1136 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1139 GetViewStart(& startX
, & startY
);
1141 startY
= startY
* ppuY
;
1144 GetVirtualSize(& sx
, & sy
);
1149 wxRect rect
= line
->GetRect();
1151 wxSize clientSize
= GetClientSize();
1153 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1156 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1158 m_caretPosition
= position
;
1159 m_caretAtLineStart
= showAtLineStart
;
1162 /// Move caret one visual step forward: this may mean setting a flag
1163 /// and keeping the same position if we're going from the end of one line
1164 /// to the start of the next, which may be the exact same caret position.
1165 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1167 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1169 // Only do the check if we're not at the end of the paragraph (where things work OK
1171 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1173 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1177 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1179 // We're at the end of a line. See whether we need to
1180 // stay at the same actual caret position but change visual
1181 // position, or not.
1182 if (oldPosition
== lineRange
.GetEnd())
1184 if (m_caretAtLineStart
)
1186 // We're already at the start of the line, so actually move on now.
1187 m_caretPosition
= oldPosition
+ 1;
1188 m_caretAtLineStart
= false;
1192 // We're showing at the end of the line, so keep to
1193 // the same position but indicate that we're to show
1194 // at the start of the next line.
1195 m_caretPosition
= oldPosition
;
1196 m_caretAtLineStart
= true;
1198 SetDefaultStyleToCursorStyle();
1204 SetDefaultStyleToCursorStyle();
1207 /// Move caret one visual step backward: this may mean setting a flag
1208 /// and keeping the same position if we're going from the end of one line
1209 /// to the start of the next, which may be the exact same caret position.
1210 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1212 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1214 // Only do the check if we're not at the start of the paragraph (where things work OK
1216 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1218 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1222 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1224 // We're at the start of a line. See whether we need to
1225 // stay at the same actual caret position but change visual
1226 // position, or not.
1227 if (oldPosition
== lineRange
.GetStart())
1229 m_caretPosition
= oldPosition
-1;
1230 m_caretAtLineStart
= true;
1233 else if (oldPosition
== lineRange
.GetEnd())
1235 if (m_caretAtLineStart
)
1237 // We're at the start of the line, so keep the same caret position
1238 // but clear the start-of-line flag.
1239 m_caretPosition
= oldPosition
;
1240 m_caretAtLineStart
= false;
1244 // We're showing at the end of the line, so go back
1245 // to the previous character position.
1246 m_caretPosition
= oldPosition
- 1;
1248 SetDefaultStyleToCursorStyle();
1254 SetDefaultStyleToCursorStyle();
1258 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1260 long endPos
= GetBuffer().GetRange().GetEnd();
1262 if (m_caretPosition
+ noPositions
< endPos
)
1264 long oldPos
= m_caretPosition
;
1265 long newPos
= m_caretPosition
+ noPositions
;
1267 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1271 // Determine by looking at oldPos and m_caretPosition whether
1272 // we moved from the end of a line to the start of the next line, in which case
1273 // we want to adjust the caret position such that it is positioned at the
1274 // start of the next line, rather than jumping past the first character of the
1276 if (noPositions
== 1 && !extendSel
)
1277 MoveCaretForward(oldPos
);
1279 SetCaretPosition(newPos
);
1282 SetDefaultStyleToCursorStyle();
1293 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1297 if (m_caretPosition
> startPos
- noPositions
+ 1)
1299 long oldPos
= m_caretPosition
;
1300 long newPos
= m_caretPosition
- noPositions
;
1301 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1305 if (noPositions
== 1 && !extendSel
)
1306 MoveCaretBack(oldPos
);
1308 SetCaretPosition(newPos
);
1311 SetDefaultStyleToCursorStyle();
1322 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1324 return MoveDown(- noLines
, flags
);
1328 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1333 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1334 wxPoint pt
= GetCaret()->GetPosition();
1335 long newLine
= lineNumber
+ noLines
;
1337 if (lineNumber
!= -1)
1341 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1343 if (newLine
> lastLine
)
1353 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1356 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1362 wxClientDC
dc(this);
1364 dc
.SetFont(GetFont());
1366 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1368 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1370 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1371 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1372 // so we view the caret at the start of the line.
1373 bool caretLineStart
= false;
1374 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1376 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1377 wxRichTextRange lineRange
;
1379 lineRange
= thisLine
->GetAbsoluteRange();
1381 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1384 caretLineStart
= true;
1388 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1389 if (para
&& para
->GetRange().GetStart() == newPos
)
1394 long newSelEnd
= newPos
;
1396 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1400 SetCaretPosition(newPos
, caretLineStart
);
1402 SetDefaultStyleToCursorStyle();
1412 /// Move to the end of the paragraph
1413 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1415 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1418 long newPos
= para
->GetRange().GetEnd() - 1;
1419 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1423 SetCaretPosition(newPos
);
1425 SetDefaultStyleToCursorStyle();
1435 /// Move to the start of the paragraph
1436 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1438 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1441 long newPos
= para
->GetRange().GetStart() - 1;
1442 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1446 SetCaretPosition(newPos
);
1448 SetDefaultStyleToCursorStyle();
1458 /// Move to the end of the line
1459 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1461 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1465 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1466 long newPos
= lineRange
.GetEnd();
1467 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1471 SetCaretPosition(newPos
);
1473 SetDefaultStyleToCursorStyle();
1483 /// Move to the start of the line
1484 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1486 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1489 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1490 long newPos
= lineRange
.GetStart()-1;
1492 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1496 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1498 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1500 SetDefaultStyleToCursorStyle();
1510 /// Move to the start of the buffer
1511 bool wxRichTextCtrl::MoveHome(int flags
)
1513 if (m_caretPosition
!= -1)
1515 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1519 SetCaretPosition(-1);
1521 SetDefaultStyleToCursorStyle();
1531 /// Move to the end of the buffer
1532 bool wxRichTextCtrl::MoveEnd(int flags
)
1534 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1536 if (m_caretPosition
!= endPos
)
1538 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1542 SetCaretPosition(endPos
);
1544 SetDefaultStyleToCursorStyle();
1554 /// Move noPages pages up
1555 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1557 return PageDown(- noPages
, flags
);
1560 /// Move noPages pages down
1561 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1563 // Calculate which line occurs noPages * screen height further down.
1564 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1567 wxSize clientSize
= GetClientSize();
1568 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1570 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1573 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1574 long pos
= lineRange
.GetStart()-1;
1575 if (pos
!= m_caretPosition
)
1577 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1579 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1583 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1585 SetDefaultStyleToCursorStyle();
1597 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1599 return str
== wxT(" ") || str
== wxT("\t");
1602 // Finds the caret position for the next word
1603 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1605 long endPos
= GetBuffer().GetRange().GetEnd();
1609 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1611 // First skip current text to space
1612 while (i
< endPos
&& i
> -1)
1614 // i is in character, not caret positions
1615 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1616 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1617 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1621 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1628 while (i
< endPos
&& i
> -1)
1630 // i is in character, not caret positions
1631 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1632 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1633 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1634 return wxMax(-1, i
);
1636 if (text
.empty()) // End of paragraph, or maybe an image
1637 return wxMax(-1, i
- 1);
1638 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1642 // Convert to caret position
1643 return wxMax(-1, i
- 1);
1652 long i
= m_caretPosition
;
1654 // First skip white 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);
1661 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1663 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1668 // Next skip current text to space
1669 while (i
< endPos
&& i
> -1)
1671 // i is in character, not caret positions
1672 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1673 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1674 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1677 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1690 /// Move n words left
1691 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1693 long pos
= FindNextWordPosition(-1);
1694 if (pos
!= m_caretPosition
)
1696 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1698 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1702 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1704 SetDefaultStyleToCursorStyle();
1714 /// Move n words right
1715 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1717 long pos
= FindNextWordPosition(1);
1718 if (pos
!= m_caretPosition
)
1720 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1722 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1726 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1728 SetDefaultStyleToCursorStyle();
1739 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1741 // Only do sizing optimization for large buffers
1742 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1744 m_fullLayoutRequired
= true;
1745 m_fullLayoutTime
= wxGetLocalTimeMillis();
1746 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1747 LayoutContent(true /* onlyVisibleRect */);
1750 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1752 #if wxRICHTEXT_BUFFERED_PAINTING
1760 /// Idle-time processing
1761 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1763 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1765 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1767 m_fullLayoutRequired
= false;
1768 m_fullLayoutTime
= 0;
1769 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1770 ShowPosition(m_fullLayoutSavedPosition
);
1774 if (m_caretPositionForDefaultStyle
!= -2)
1776 // If the caret position has changed, no longer reflect the default style
1778 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1779 m_caretPositionForDefaultStyle
= -2;
1786 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1792 /// Set up scrollbars, e.g. after a resize
1793 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1798 if (GetBuffer().IsEmpty())
1800 SetScrollbars(0, 0, 0, 0, 0, 0);
1804 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1805 // of pixels. See e.g. wxVScrolledWindow for ideas.
1806 int pixelsPerUnit
= 5;
1807 wxSize clientSize
= GetClientSize();
1809 int maxHeight
= GetBuffer().GetCachedSize().y
;
1811 // Round up so we have at least maxHeight pixels
1812 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1814 int startX
= 0, startY
= 0;
1816 GetViewStart(& startX
, & startY
);
1818 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1819 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1821 // Move to previous scroll position if
1823 SetScrollbars(0, pixelsPerUnit
,
1825 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1828 /// Paint the background
1829 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1831 wxColour backgroundColour
= GetBackgroundColour();
1832 if (!backgroundColour
.Ok())
1833 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1835 // Clear the background
1836 dc
.SetBrush(wxBrush(backgroundColour
));
1837 dc
.SetPen(*wxTRANSPARENT_PEN
);
1838 wxRect
windowRect(GetClientSize());
1839 windowRect
.x
-= 2; windowRect
.y
-= 2;
1840 windowRect
.width
+= 4; windowRect
.height
+= 4;
1842 // We need to shift the rectangle to take into account
1843 // scrolling. Converting device to logical coordinates.
1844 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1845 dc
.DrawRectangle(windowRect
);
1848 #if wxRICHTEXT_BUFFERED_PAINTING
1849 /// Recreate buffer bitmap if necessary
1850 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1853 if (sz
== wxDefaultSize
)
1854 sz
= GetClientSize();
1856 if (sz
.x
< 1 || sz
.y
< 1)
1859 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1860 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1861 return m_bufferBitmap
.Ok();
1865 // ----------------------------------------------------------------------------
1866 // file IO functions
1867 // ----------------------------------------------------------------------------
1869 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1871 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1873 m_filename
= filename
;
1876 SetInsertionPoint(0);
1879 SetupScrollbars(true);
1881 SendTextUpdatedEvent();
1887 wxLogError(_("File couldn't be loaded."));
1893 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1895 if (GetBuffer().SaveFile(filename
, fileType
))
1897 m_filename
= filename
;
1904 wxLogError(_("The text couldn't be saved."));
1909 // ----------------------------------------------------------------------------
1910 // wxRichTextCtrl specific functionality
1911 // ----------------------------------------------------------------------------
1913 /// Add a new paragraph of text to the end of the buffer
1914 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1916 return GetBuffer().AddParagraph(text
);
1920 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1922 return GetBuffer().AddImage(image
);
1925 // ----------------------------------------------------------------------------
1926 // selection and ranges
1927 // ----------------------------------------------------------------------------
1929 void wxRichTextCtrl::SelectAll()
1931 SetSelection(0, GetLastPosition()+1);
1932 m_selectionAnchor
= -1;
1936 void wxRichTextCtrl::SelectNone()
1938 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1939 SetSelection(-2, -2);
1940 m_selectionAnchor
= -2;
1943 static bool wxIsWordDelimiter(const wxString
& text
)
1945 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1948 /// Select the word at the given character position
1949 bool wxRichTextCtrl::SelectWord(long position
)
1951 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1954 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1958 long positionStart
= position
;
1959 long positionEnd
= position
;
1961 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1963 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1964 if (wxIsWordDelimiter(text
))
1970 if (positionStart
< para
->GetRange().GetStart())
1971 positionStart
= para
->GetRange().GetStart();
1973 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1975 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1976 if (wxIsWordDelimiter(text
))
1982 if (positionEnd
>= para
->GetRange().GetEnd())
1983 positionEnd
= para
->GetRange().GetEnd();
1985 SetSelection(positionStart
, positionEnd
+1);
1987 if (positionStart
>= 0)
1989 MoveCaret(positionStart
-1, true);
1990 SetDefaultStyleToCursorStyle();
1996 wxString
wxRichTextCtrl::GetStringSelection() const
1999 GetSelection(&from
, &to
);
2001 return GetRange(from
, to
);
2004 // ----------------------------------------------------------------------------
2006 // ----------------------------------------------------------------------------
2008 wxTextCtrlHitTestResult
2009 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2011 // implement in terms of the other overload as the native ports typically
2012 // can get the position and not (x, y) pair directly (although wxUniv
2013 // directly gets x and y -- and so overrides this method as well)
2015 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2017 if ( rc
!= wxTE_HT_UNKNOWN
)
2019 PositionToXY(pos
, x
, y
);
2025 wxTextCtrlHitTestResult
2026 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2029 wxClientDC
dc((wxRichTextCtrl
*) this);
2030 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2032 // Buffer uses logical position (relative to start of buffer)
2034 wxPoint pt2
= GetLogicalPoint(pt
);
2036 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2038 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2039 return wxTE_HT_BEFORE
;
2040 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2041 return wxTE_HT_BEYOND
;
2042 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2043 return wxTE_HT_ON_TEXT
;
2045 return wxTE_HT_UNKNOWN
;
2048 // ----------------------------------------------------------------------------
2049 // set/get the controls text
2050 // ----------------------------------------------------------------------------
2052 wxString
wxRichTextCtrl::GetValue() const
2054 return GetBuffer().GetText();
2057 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2059 // Public API for range is different from internals
2060 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2063 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2067 if (!value
.IsEmpty())
2069 // Remove empty paragraph
2070 GetBuffer().Clear();
2073 // for compatibility, don't move the cursor when doing SetValue()
2074 SetInsertionPoint(0);
2078 // still send an event for consistency
2079 if (flags
& SetValue_SendEvent
)
2080 SendTextUpdatedEvent();
2085 void wxRichTextCtrl::WriteText(const wxString
& value
)
2090 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2092 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2094 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2096 if ( flags
& SetValue_SendEvent
)
2097 SendTextUpdatedEvent();
2100 void wxRichTextCtrl::AppendText(const wxString
& text
)
2102 SetInsertionPointEnd();
2107 /// Write an image at the current insertion point
2108 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2110 wxRichTextImageBlock imageBlock
;
2112 wxImage image2
= image
;
2113 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2114 return WriteImage(imageBlock
);
2119 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2121 wxRichTextImageBlock imageBlock
;
2124 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2125 return WriteImage(imageBlock
);
2130 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2132 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2135 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2139 wxRichTextImageBlock imageBlock
;
2141 wxImage image
= bitmap
.ConvertToImage();
2142 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2143 return WriteImage(imageBlock
);
2149 /// Insert a newline (actually paragraph) at the current insertion point.
2150 bool wxRichTextCtrl::Newline()
2152 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2155 /// Insert a line break at the current insertion point.
2156 bool wxRichTextCtrl::LineBreak()
2159 text
= wxRichTextLineBreakChar
;
2160 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2163 // ----------------------------------------------------------------------------
2164 // Clipboard operations
2165 // ----------------------------------------------------------------------------
2167 void wxRichTextCtrl::Copy()
2171 wxRichTextRange range
= GetInternalSelectionRange();
2172 GetBuffer().CopyToClipboard(range
);
2176 void wxRichTextCtrl::Cut()
2180 wxRichTextRange range
= GetInternalSelectionRange();
2181 GetBuffer().CopyToClipboard(range
);
2183 DeleteSelectedContent();
2189 void wxRichTextCtrl::Paste()
2193 BeginBatchUndo(_("Paste"));
2195 long newPos
= m_caretPosition
;
2196 DeleteSelectedContent(& newPos
);
2198 GetBuffer().PasteFromClipboard(newPos
);
2204 void wxRichTextCtrl::DeleteSelection()
2206 if (CanDeleteSelection())
2208 DeleteSelectedContent();
2212 bool wxRichTextCtrl::HasSelection() const
2214 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2217 bool wxRichTextCtrl::CanCopy() const
2219 // Can copy if there's a selection
2220 return HasSelection();
2223 bool wxRichTextCtrl::CanCut() const
2225 return HasSelection() && IsEditable();
2228 bool wxRichTextCtrl::CanPaste() const
2230 if ( !IsEditable() )
2233 return GetBuffer().CanPasteFromClipboard();
2236 bool wxRichTextCtrl::CanDeleteSelection() const
2238 return HasSelection() && IsEditable();
2242 // ----------------------------------------------------------------------------
2244 // ----------------------------------------------------------------------------
2246 void wxRichTextCtrl::SetEditable(bool editable
)
2248 m_editable
= editable
;
2251 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2255 m_caretPosition
= pos
- 1;
2258 void wxRichTextCtrl::SetInsertionPointEnd()
2260 long pos
= GetLastPosition();
2261 SetInsertionPoint(pos
);
2264 long wxRichTextCtrl::GetInsertionPoint() const
2266 return m_caretPosition
+1;
2269 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2271 return GetBuffer().GetRange().GetEnd();
2274 // If the return values from and to are the same, there is no
2276 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2278 *from
= m_selectionRange
.GetStart();
2279 *to
= m_selectionRange
.GetEnd();
2280 if ((*to
) != -1 && (*to
) != -2)
2284 bool wxRichTextCtrl::IsEditable() const
2289 // ----------------------------------------------------------------------------
2291 // ----------------------------------------------------------------------------
2293 void wxRichTextCtrl::SetSelection(long from
, long to
)
2295 // if from and to are both -1, it means (in wxWidgets) that all text should
2297 if ( (from
== -1) && (to
== -1) )
2300 to
= GetLastPosition()+1;
2303 DoSetSelection(from
, to
);
2306 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2308 m_selectionAnchor
= from
;
2309 m_selectionRange
.SetRange(from
, to
-1);
2311 m_caretPosition
= from
-1;
2317 // ----------------------------------------------------------------------------
2319 // ----------------------------------------------------------------------------
2321 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2322 const wxString
& value
)
2324 BeginBatchUndo(_("Replace"));
2326 DeleteSelectedContent();
2328 DoWriteText(value
, SetValue_SelectionOnly
);
2333 void wxRichTextCtrl::Remove(long from
, long to
)
2337 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2344 bool wxRichTextCtrl::IsModified() const
2346 return m_buffer
.IsModified();
2349 void wxRichTextCtrl::MarkDirty()
2351 m_buffer
.Modify(true);
2354 void wxRichTextCtrl::DiscardEdits()
2356 m_caretPositionForDefaultStyle
= -2;
2357 m_buffer
.Modify(false);
2358 m_buffer
.GetCommandProcessor()->ClearCommands();
2361 int wxRichTextCtrl::GetNumberOfLines() const
2363 return GetBuffer().GetParagraphCount();
2366 // ----------------------------------------------------------------------------
2367 // Positions <-> coords
2368 // ----------------------------------------------------------------------------
2370 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2372 return GetBuffer().XYToPosition(x
, y
);
2375 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2377 return GetBuffer().PositionToXY(pos
, x
, y
);
2380 // ----------------------------------------------------------------------------
2382 // ----------------------------------------------------------------------------
2384 void wxRichTextCtrl::ShowPosition(long pos
)
2386 if (!IsPositionVisible(pos
))
2387 ScrollIntoView(pos
-1, WXK_DOWN
);
2390 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2392 return GetBuffer().GetParagraphLength(lineNo
);
2395 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2397 return GetBuffer().GetParagraphText(lineNo
);
2400 // ----------------------------------------------------------------------------
2402 // ----------------------------------------------------------------------------
2404 void wxRichTextCtrl::Undo()
2408 GetCommandProcessor()->Undo();
2412 void wxRichTextCtrl::Redo()
2416 GetCommandProcessor()->Redo();
2420 bool wxRichTextCtrl::CanUndo() const
2422 return GetCommandProcessor()->CanUndo();
2425 bool wxRichTextCtrl::CanRedo() const
2427 return GetCommandProcessor()->CanRedo();
2430 // ----------------------------------------------------------------------------
2431 // implementation details
2432 // ----------------------------------------------------------------------------
2434 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2436 SetValue(event
.GetString());
2437 GetEventHandler()->ProcessEvent(event
);
2440 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2442 // By default, load the first file into the text window.
2443 if (event
.GetNumberOfFiles() > 0)
2445 LoadFile(event
.GetFiles()[0]);
2449 wxSize
wxRichTextCtrl::DoGetBestSize() const
2451 return wxSize(10, 10);
2454 // ----------------------------------------------------------------------------
2455 // standard handlers for standard edit menu events
2456 // ----------------------------------------------------------------------------
2458 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2463 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2468 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2473 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2478 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2483 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2488 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2490 event
.Enable( CanCut() );
2493 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2495 event
.Enable( CanCopy() );
2498 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2500 event
.Enable( CanDeleteSelection() );
2503 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2505 event
.Enable( CanPaste() );
2508 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2510 event
.Enable( CanUndo() );
2511 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2514 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2516 event
.Enable( CanRedo() );
2517 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2520 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2525 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2527 event
.Enable(GetLastPosition() > 0);
2530 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2534 m_contextMenu
= new wxMenu
;
2535 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2536 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2537 m_contextMenu
->AppendSeparator();
2538 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2539 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2540 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2541 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2542 m_contextMenu
->AppendSeparator();
2543 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2545 PopupMenu(m_contextMenu
);
2549 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2551 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2554 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2556 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2559 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2561 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2564 // extended style setting operation with flags including:
2565 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2566 // see richtextbuffer.h for more details.
2567 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2569 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2572 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2574 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2577 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2579 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2582 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2584 return GetBuffer().SetDefaultStyle(style
);
2587 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2589 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2592 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2594 return GetBuffer().GetDefaultStyle();
2597 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2599 return GetBuffer().GetDefaultStyle();
2602 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2604 wxTextAttrEx
attr(style
);
2605 if (GetBuffer().GetStyle(position
, attr
))
2614 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2616 return GetBuffer().GetStyle(position
, style
);
2619 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2621 return GetBuffer().GetStyle(position
, style
);
2624 // get the common set of styles for the range
2625 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2627 wxTextAttrEx styleEx
;
2628 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2637 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2639 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2642 /// Get the content (uncombined) attributes for this position.
2644 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2646 wxTextAttrEx
attr(style
);
2647 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2656 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2658 return GetBuffer().GetUncombinedStyle(position
, style
);
2661 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2663 return GetBuffer().GetUncombinedStyle(position
, style
);
2666 /// Set font, and also the buffer attributes
2667 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2669 wxControl::SetFont(font
);
2671 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2673 GetBuffer().SetBasicStyle(attr
);
2675 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2681 /// Transform logical to physical
2682 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2685 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2690 /// Transform physical to logical
2691 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2694 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2699 /// Position the caret
2700 void wxRichTextCtrl::PositionCaret()
2705 //wxLogDebug(wxT("PositionCaret"));
2708 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2710 wxPoint originalPt
= caretRect
.GetPosition();
2711 wxPoint pt
= GetPhysicalPoint(originalPt
);
2712 if (GetCaret()->GetPosition() != pt
)
2714 GetCaret()->Move(pt
);
2715 GetCaret()->SetSize(caretRect
.GetSize());
2720 /// Get the caret height and position for the given character position
2721 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2723 wxClientDC
dc(this);
2724 dc
.SetFont(GetFont());
2731 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2733 // Caret height can't be zero
2735 height
= dc
.GetCharHeight();
2737 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2744 /// Gets the line for the visible caret position. If the caret is
2745 /// shown at the very end of the line, it means the next character is actually
2746 /// on the following line. So let's get the line we're expecting to find
2747 /// if this is the case.
2748 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2750 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2751 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2754 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2755 if (caretPosition
== lineRange
.GetStart()-1 &&
2756 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2758 if (!m_caretAtLineStart
)
2759 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2766 /// Move the caret to the given character position
2767 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2769 if (GetBuffer().GetDirty())
2772 if (pos
<= GetBuffer().GetRange().GetEnd())
2774 SetCaretPosition(pos
, showAtLineStart
);
2784 /// Layout the buffer: which we must do before certain operations, such as
2785 /// setting the caret position.
2786 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2788 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2790 wxRect
availableSpace(GetClientSize());
2791 if (availableSpace
.width
== 0)
2792 availableSpace
.width
= 10;
2793 if (availableSpace
.height
== 0)
2794 availableSpace
.height
= 10;
2796 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2797 if (onlyVisibleRect
)
2799 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2800 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2803 wxClientDC
dc(this);
2804 dc
.SetFont(GetFont());
2808 GetBuffer().Defragment();
2809 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2810 GetBuffer().Layout(dc
, availableSpace
, flags
);
2811 GetBuffer().SetDirty(false);
2820 /// Is all of the selection bold?
2821 bool wxRichTextCtrl::IsSelectionBold()
2825 wxRichTextAttr attr
;
2826 wxRichTextRange range
= GetSelectionRange();
2827 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2828 attr
.SetFontWeight(wxBOLD
);
2830 return HasCharacterAttributes(range
, attr
);
2834 // If no selection, then we need to combine current style with default style
2835 // to see what the effect would be if we started typing.
2836 wxRichTextAttr attr
;
2837 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2839 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2840 if (GetStyle(pos
, attr
))
2842 if (IsDefaultStyleShowing())
2843 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2844 return attr
.GetFontWeight() == wxBOLD
;
2850 /// Is all of the selection italics?
2851 bool wxRichTextCtrl::IsSelectionItalics()
2855 wxRichTextRange range
= GetSelectionRange();
2856 wxRichTextAttr attr
;
2857 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2858 attr
.SetFontStyle(wxITALIC
);
2860 return HasCharacterAttributes(range
, attr
);
2864 // If no selection, then we need to combine current style with default style
2865 // to see what the effect would be if we started typing.
2866 wxRichTextAttr attr
;
2867 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2869 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2870 if (GetStyle(pos
, attr
))
2872 if (IsDefaultStyleShowing())
2873 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2874 return attr
.GetFontStyle() == wxITALIC
;
2880 /// Is all of the selection underlined?
2881 bool wxRichTextCtrl::IsSelectionUnderlined()
2885 wxRichTextRange range
= GetSelectionRange();
2886 wxRichTextAttr attr
;
2887 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2888 attr
.SetFontUnderlined(true);
2890 return HasCharacterAttributes(range
, attr
);
2894 // If no selection, then we need to combine current style with default style
2895 // to see what the effect would be if we started typing.
2896 wxRichTextAttr attr
;
2897 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2898 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2900 if (GetStyle(pos
, attr
))
2902 if (IsDefaultStyleShowing())
2903 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2904 return attr
.GetFontUnderlined();
2910 /// Apply bold to the selection
2911 bool wxRichTextCtrl::ApplyBoldToSelection()
2913 wxRichTextAttr attr
;
2914 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2915 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2918 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2920 SetAndShowDefaultStyle(attr
);
2924 /// Apply italic to the selection
2925 bool wxRichTextCtrl::ApplyItalicToSelection()
2927 wxRichTextAttr attr
;
2928 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2929 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2932 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2934 SetAndShowDefaultStyle(attr
);
2938 /// Apply underline to the selection
2939 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2941 wxRichTextAttr attr
;
2942 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2943 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2946 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2948 SetAndShowDefaultStyle(attr
);
2952 /// Is all of the selection aligned according to the specified flag?
2953 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2955 wxRichTextRange range
;
2957 range
= GetSelectionRange();
2959 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2961 wxRichTextAttr attr
;
2962 attr
.SetAlignment(alignment
);
2964 return HasParagraphAttributes(range
, attr
);
2967 /// Apply alignment to the selection
2968 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2970 wxRichTextAttr attr
;
2971 attr
.SetAlignment(alignment
);
2973 return SetStyle(GetSelectionRange(), attr
);
2976 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2978 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2983 /// Apply a named style to the selection
2984 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2986 // Flags are defined within each definition, so only certain
2987 // attributes are applied.
2988 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2990 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2992 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2994 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2996 wxRichTextRange range
;
2999 range
= GetSelectionRange();
3002 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3003 range
= wxRichTextRange(pos
, pos
+1);
3006 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3009 // Make sure the attr has the style name
3010 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3012 attr
.SetParagraphStyleName(def
->GetName());
3014 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3015 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3016 // to change its style independently.
3017 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3020 attr
.SetCharacterStyleName(def
->GetName());
3023 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3026 SetAndShowDefaultStyle(attr
);
3031 /// Apply the style sheet to the buffer, for example if the styles have changed.
3032 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3035 styleSheet
= GetBuffer().GetStyleSheet();
3039 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3041 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3049 /// Sets the default style to the style under the cursor
3050 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3053 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3055 // If at the start of a paragraph, use the next position.
3056 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3058 if (GetUncombinedStyle(pos
, attr
))
3060 SetDefaultStyle(attr
);
3067 /// Returns the first visible position in the current view
3068 long wxRichTextCtrl::GetFirstVisiblePosition() const
3070 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3072 return line
->GetAbsoluteRange().GetStart();
3077 /// Get the first visible point in the window
3078 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3081 int startXUnits
, startYUnits
;
3083 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3084 GetViewStart(& startXUnits
, & startYUnits
);
3086 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3089 /// The adjusted caret position is the character position adjusted to take
3090 /// into account whether we're at the start of a paragraph, in which case
3091 /// style information should be taken from the next position, not current one.
3092 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3094 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3096 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3101 /// Get/set the selection range in character positions. -1, -1 means no selection.
3102 /// The range is in API convention, i.e. a single character selection is denoted
3104 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3106 wxRichTextRange range
= GetInternalSelectionRange();
3107 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3108 range
.SetEnd(range
.GetEnd() + 1);
3112 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3114 wxRichTextRange
range1(range
);
3115 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3116 range1
.SetEnd(range1
.GetEnd() - 1);
3118 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3120 SetInternalSelectionRange(range1
);
3124 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3126 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3129 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3131 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3134 /// Clear list for given range
3135 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3137 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3140 /// Number/renumber any list elements in the given range
3141 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3143 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3146 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3148 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3151 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3152 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3154 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3157 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3159 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3162 /// Deletes the content in the given range
3163 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3165 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3168 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3170 if (sm_availableFontNames
.GetCount() == 0)
3172 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3173 sm_availableFontNames
.Sort();
3175 return sm_availableFontNames
;
3178 void wxRichTextCtrl::ClearAvailableFontNames()
3180 sm_availableFontNames
.Clear();