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);
247 wxTextCtrl::SendTextUpdatedEvent(this);
251 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
253 if (GetCaret() && GetCaret()->IsVisible())
257 #if wxRICHTEXT_BUFFERED_PAINTING
258 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
268 dc
.SetFont(GetFont());
270 // Paint the background
273 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
275 wxRect
drawingArea(GetUpdateRegion().GetBox());
276 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
278 wxRect
availableSpace(GetClientSize());
279 if (GetBuffer().GetDirty())
281 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
282 GetBuffer().SetDirty(false);
286 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
289 if (GetCaret() && !GetCaret()->IsVisible())
295 // Empty implementation, to prevent flicker
296 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
300 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
304 if (!GetCaret()->IsVisible())
313 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
315 if (GetCaret() && GetCaret()->IsVisible())
322 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
328 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
334 dc
.SetFont(GetFont());
337 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
339 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
341 m_dragStart
= event
.GetLogicalPosition(dc
);
347 bool caretAtLineStart
= false;
349 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
351 // If we're at the start of a line (but not first in para)
352 // then we should keep the caret showing at the start of the line
353 // by showing the m_caretAtLineStart flag.
354 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
355 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
357 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
358 caretAtLineStart
= true;
362 MoveCaret(position
, caretAtLineStart
);
363 SetDefaultStyleToCursorStyle();
370 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
375 if (GetCapture() == this)
378 // See if we clicked on a URL
381 dc
.SetFont(GetFont());
384 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
385 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
387 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
389 wxRichTextEvent
cmdEvent(
390 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
392 cmdEvent
.SetEventObject(this);
393 cmdEvent
.SetPosition(m_caretPosition
+1);
395 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
398 if (GetStyle(position
, attr
))
400 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
402 wxString urlTarget
= attr
.GetURL();
403 if (!urlTarget
.IsEmpty())
405 wxMouseEvent
mouseEvent(event
);
407 long startPos
= 0, endPos
= 0;
408 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
411 startPos
= obj
->GetRange().GetStart();
412 endPos
= obj
->GetRange().GetEnd();
415 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
416 InitCommandEvent(urlEvent
);
418 urlEvent
.SetString(urlTarget
);
420 GetEventHandler()->ProcessEvent(urlEvent
);
430 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
434 dc
.SetFont(GetFont());
437 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
438 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
440 // See if we need to change the cursor
443 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
446 if (GetStyle(position
, attr
))
448 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
450 SetCursor(m_urlCursor
);
452 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
454 SetCursor(m_textCursor
);
459 SetCursor(m_textCursor
);
462 if (!event
.Dragging())
468 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
470 // TODO: test closeness
472 bool caretAtLineStart
= false;
474 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
476 // If we're at the start of a line (but not first in para)
477 // then we should keep the caret showing at the start of the line
478 // by showing the m_caretAtLineStart flag.
479 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
480 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
482 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
483 caretAtLineStart
= true;
487 if (m_caretPosition
!= position
)
489 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
491 MoveCaret(position
, caretAtLineStart
);
492 SetDefaultStyleToCursorStyle();
501 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
505 wxRichTextEvent
cmdEvent(
506 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
508 cmdEvent
.SetEventObject(this);
509 cmdEvent
.SetPosition(m_caretPosition
+1);
511 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
515 /// Left-double-click
516 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
518 wxRichTextEvent
cmdEvent(
519 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
521 cmdEvent
.SetEventObject(this);
522 cmdEvent
.SetPosition(m_caretPosition
+1);
524 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
526 SelectWord(GetCaretPosition()+1);
531 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
533 wxRichTextEvent
cmdEvent(
534 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
536 cmdEvent
.SetEventObject(this);
537 cmdEvent
.SetPosition(m_caretPosition
+1);
539 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
544 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
548 flags
|= wxRICHTEXT_CTRL_DOWN
;
549 if (event
.ShiftDown())
550 flags
|= wxRICHTEXT_SHIFT_DOWN
;
552 flags
|= wxRICHTEXT_ALT_DOWN
;
554 if (event
.GetKeyCode() == WXK_LEFT
||
555 event
.GetKeyCode() == WXK_RIGHT
||
556 event
.GetKeyCode() == WXK_UP
||
557 event
.GetKeyCode() == WXK_DOWN
||
558 event
.GetKeyCode() == WXK_HOME
||
559 event
.GetKeyCode() == WXK_PAGEUP
||
560 event
.GetKeyCode() == WXK_PAGEDOWN
||
561 event
.GetKeyCode() == WXK_END
||
563 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
564 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
565 event
.GetKeyCode() == WXK_NUMPAD_UP
||
566 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
567 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
568 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
569 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
570 event
.GetKeyCode() == WXK_NUMPAD_END
)
572 KeyboardNavigate(event
.GetKeyCode(), flags
);
576 // all the other keys modify the controls contents which shouldn't be
577 // possible if we're read-only
584 if (event
.GetKeyCode() == WXK_RETURN
)
586 BeginBatchUndo(_("Insert Text"));
588 long newPos
= m_caretPosition
;
590 DeleteSelectedContent(& newPos
);
592 if (event
.ShiftDown())
595 text
= wxRichTextLineBreakChar
;
596 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
599 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
602 SetDefaultStyleToCursorStyle();
604 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
606 wxRichTextEvent
cmdEvent(
607 wxEVT_COMMAND_RICHTEXT_RETURN
,
609 cmdEvent
.SetEventObject(this);
610 cmdEvent
.SetFlags(flags
);
611 cmdEvent
.SetPosition(newPos
+1);
613 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
615 // Generate conventional event
616 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
617 InitCommandEvent(textEvent
);
619 GetEventHandler()->ProcessEvent(textEvent
);
623 else if (event
.GetKeyCode() == WXK_BACK
)
625 BeginBatchUndo(_("Delete Text"));
627 // Submit range in character positions, which are greater than caret positions,
628 // so subtract 1 for deleted character and add 1 for conversion to character position.
629 if (m_caretPosition
> -1 && !HasSelection())
631 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
634 DeleteSelectedContent();
638 if (GetLastPosition() == -1)
642 m_caretPosition
= -1;
644 SetDefaultStyleToCursorStyle();
647 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
649 wxRichTextEvent
cmdEvent(
650 wxEVT_COMMAND_RICHTEXT_DELETE
,
652 cmdEvent
.SetEventObject(this);
653 cmdEvent
.SetFlags(flags
);
654 cmdEvent
.SetPosition(m_caretPosition
+1);
655 GetEventHandler()->ProcessEvent(cmdEvent
);
659 else if (event
.GetKeyCode() == WXK_DELETE
)
661 BeginBatchUndo(_("Delete Text"));
663 // Submit range in character positions, which are greater than caret positions,
664 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
666 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
669 DeleteSelectedContent();
673 if (GetLastPosition() == -1)
677 m_caretPosition
= -1;
679 SetDefaultStyleToCursorStyle();
682 wxRichTextEvent
cmdEvent(
683 wxEVT_COMMAND_RICHTEXT_DELETE
,
685 cmdEvent
.SetEventObject(this);
686 cmdEvent
.SetFlags(flags
);
687 cmdEvent
.SetPosition(m_caretPosition
+1);
688 GetEventHandler()->ProcessEvent(cmdEvent
);
694 long keycode
= event
.GetKeyCode();
767 case WXK_NUMPAD_SPACE
:
769 case WXK_NUMPAD_ENTER
:
774 case WXK_NUMPAD_HOME
:
775 case WXK_NUMPAD_LEFT
:
777 case WXK_NUMPAD_RIGHT
:
778 case WXK_NUMPAD_DOWN
:
779 case WXK_NUMPAD_PAGEUP
:
780 case WXK_NUMPAD_PAGEDOWN
:
782 case WXK_NUMPAD_BEGIN
:
783 case WXK_NUMPAD_INSERT
:
784 case WXK_NUMPAD_DELETE
:
785 case WXK_NUMPAD_EQUAL
:
786 case WXK_NUMPAD_MULTIPLY
:
788 case WXK_NUMPAD_SEPARATOR
:
789 case WXK_NUMPAD_SUBTRACT
:
790 case WXK_NUMPAD_DECIMAL
:
791 case WXK_WINDOWS_LEFT
:
799 if (event
.CmdDown() || event
.AltDown())
805 wxRichTextEvent
cmdEvent(
806 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
808 cmdEvent
.SetEventObject(this);
809 cmdEvent
.SetFlags(flags
);
811 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
813 cmdEvent
.SetCharacter((wxChar
) keycode
);
815 cmdEvent
.SetPosition(m_caretPosition
+1);
817 if (keycode
== wxT('\t'))
819 // See if we need to promote or demote the selection or paragraph at the cursor
820 // position, instead of inserting a tab.
821 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
822 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
823 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
825 wxRichTextRange range
;
827 range
= GetSelectionRange();
829 range
= para
->GetRange().FromInternal();
831 int promoteBy
= event
.ShiftDown() ? 1 : -1;
833 PromoteList(promoteBy
, range
, NULL
);
835 GetEventHandler()->ProcessEvent(cmdEvent
);
841 BeginBatchUndo(_("Insert Text"));
843 long newPos
= m_caretPosition
;
844 DeleteSelectedContent(& newPos
);
847 wxString str
= event
.GetUnicodeKey();
849 wxString str
= (wxChar
) event
.GetKeyCode();
851 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
855 SetDefaultStyleToCursorStyle();
856 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
858 GetEventHandler()->ProcessEvent(cmdEvent
);
866 /// Delete content if there is a selection, e.g. when pressing a key.
867 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
871 long pos
= m_selectionRange
.GetStart();
872 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
873 m_selectionRange
.SetRange(-2, -2);
883 /// Keyboard navigation
887 Left: left one character
888 Right: right one character
891 Ctrl-Left: left one word
892 Ctrl-Right: right one word
893 Ctrl-Up: previous paragraph start
894 Ctrl-Down: next start of paragraph
897 Ctrl-Home: start of document
898 Ctrl-End: end of document
900 Page-Down: Down a screen
904 Ctrl-Alt-PgUp: Start of window
905 Ctrl-Alt-PgDn: End of window
906 F8: Start selection mode
907 Esc: End selection mode
909 Adding Shift does the above but starts/extends selection.
914 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
916 bool success
= false;
918 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
920 if (flags
& wxRICHTEXT_CTRL_DOWN
)
921 success
= WordRight(1, flags
);
923 success
= MoveRight(1, flags
);
925 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
927 if (flags
& wxRICHTEXT_CTRL_DOWN
)
928 success
= WordLeft(1, flags
);
930 success
= MoveLeft(1, flags
);
932 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
934 if (flags
& wxRICHTEXT_CTRL_DOWN
)
935 success
= MoveToParagraphStart(flags
);
937 success
= MoveUp(1, flags
);
939 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
941 if (flags
& wxRICHTEXT_CTRL_DOWN
)
942 success
= MoveToParagraphEnd(flags
);
944 success
= MoveDown(1, flags
);
946 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
948 success
= PageUp(1, flags
);
950 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
952 success
= PageDown(1, flags
);
954 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
956 if (flags
& wxRICHTEXT_CTRL_DOWN
)
957 success
= MoveHome(flags
);
959 success
= MoveToLineStart(flags
);
961 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
963 if (flags
& wxRICHTEXT_CTRL_DOWN
)
964 success
= MoveEnd(flags
);
966 success
= MoveToLineEnd(flags
);
971 ScrollIntoView(m_caretPosition
, keyCode
);
972 SetDefaultStyleToCursorStyle();
978 /// Extend the selection. Selections are in caret positions.
979 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
981 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
983 // If not currently selecting, start selecting
984 if (m_selectionRange
.GetStart() == -2)
986 m_selectionAnchor
= oldPos
;
989 m_selectionRange
.SetRange(newPos
+1, oldPos
);
991 m_selectionRange
.SetRange(oldPos
+1, newPos
);
995 // Always ensure that the selection range start is greater than
997 if (newPos
> m_selectionAnchor
)
998 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1000 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1003 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1005 wxLogDebug(wxT("Strange selection range"));
1014 /// Scroll into view, returning true if we scrolled.
1015 /// This takes a _caret_ position.
1016 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1018 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1024 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1026 int startXUnits
, startYUnits
;
1027 GetViewStart(& startXUnits
, & startYUnits
);
1028 int startY
= startYUnits
* ppuY
;
1031 GetVirtualSize(& sx
, & sy
);
1037 wxRect rect
= line
->GetRect();
1039 bool scrolled
= false;
1041 wxSize clientSize
= GetClientSize();
1044 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1045 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1046 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1047 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1049 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1051 // Make it scroll so this item is at the bottom
1053 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1054 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1056 // If we're still off the screen, scroll another line down
1057 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1060 if (startYUnits
!= yUnits
)
1062 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1066 else if (rect
.y
< startY
)
1068 // Make it scroll so this item is at the top
1071 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1073 if (startYUnits
!= yUnits
)
1075 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1081 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1082 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1083 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1084 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1086 if (rect
.y
< startY
)
1088 // Make it scroll so this item is at the top
1091 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1093 if (startYUnits
!= yUnits
)
1095 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1099 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1101 // Make it scroll so this item is at the bottom
1103 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1104 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1106 // If we're still off the screen, scroll another line down
1107 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1110 if (startYUnits
!= yUnits
)
1112 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1122 /// Is the given position visible on the screen?
1123 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1125 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1131 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1134 GetViewStart(& startX
, & startY
);
1136 startY
= startY
* ppuY
;
1138 wxRect rect
= line
->GetRect();
1139 wxSize clientSize
= GetClientSize();
1141 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1144 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1146 m_caretPosition
= position
;
1147 m_caretAtLineStart
= showAtLineStart
;
1150 /// Move caret one visual step forward: this may mean setting a flag
1151 /// and keeping the same position if we're going from the end of one line
1152 /// to the start of the next, which may be the exact same caret position.
1153 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1155 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1157 // Only do the check if we're not at the end of the paragraph (where things work OK
1159 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1161 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1165 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1167 // We're at the end of a line. See whether we need to
1168 // stay at the same actual caret position but change visual
1169 // position, or not.
1170 if (oldPosition
== lineRange
.GetEnd())
1172 if (m_caretAtLineStart
)
1174 // We're already at the start of the line, so actually move on now.
1175 m_caretPosition
= oldPosition
+ 1;
1176 m_caretAtLineStart
= false;
1180 // We're showing at the end of the line, so keep to
1181 // the same position but indicate that we're to show
1182 // at the start of the next line.
1183 m_caretPosition
= oldPosition
;
1184 m_caretAtLineStart
= true;
1186 SetDefaultStyleToCursorStyle();
1192 SetDefaultStyleToCursorStyle();
1195 /// Move caret one visual step backward: this may mean setting a flag
1196 /// and keeping the same position if we're going from the end of one line
1197 /// to the start of the next, which may be the exact same caret position.
1198 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1200 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1202 // Only do the check if we're not at the start of the paragraph (where things work OK
1204 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1206 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1210 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1212 // We're at the start of a line. See whether we need to
1213 // stay at the same actual caret position but change visual
1214 // position, or not.
1215 if (oldPosition
== lineRange
.GetStart())
1217 m_caretPosition
= oldPosition
-1;
1218 m_caretAtLineStart
= true;
1221 else if (oldPosition
== lineRange
.GetEnd())
1223 if (m_caretAtLineStart
)
1225 // We're at the start of the line, so keep the same caret position
1226 // but clear the start-of-line flag.
1227 m_caretPosition
= oldPosition
;
1228 m_caretAtLineStart
= false;
1232 // We're showing at the end of the line, so go back
1233 // to the previous character position.
1234 m_caretPosition
= oldPosition
- 1;
1236 SetDefaultStyleToCursorStyle();
1242 SetDefaultStyleToCursorStyle();
1246 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1248 long endPos
= GetBuffer().GetRange().GetEnd();
1250 if (m_caretPosition
+ noPositions
< endPos
)
1252 long oldPos
= m_caretPosition
;
1253 long newPos
= m_caretPosition
+ noPositions
;
1255 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1259 // Determine by looking at oldPos and m_caretPosition whether
1260 // we moved from the end of a line to the start of the next line, in which case
1261 // we want to adjust the caret position such that it is positioned at the
1262 // start of the next line, rather than jumping past the first character of the
1264 if (noPositions
== 1 && !extendSel
)
1265 MoveCaretForward(oldPos
);
1267 SetCaretPosition(newPos
);
1270 SetDefaultStyleToCursorStyle();
1281 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1285 if (m_caretPosition
> startPos
- noPositions
+ 1)
1287 long oldPos
= m_caretPosition
;
1288 long newPos
= m_caretPosition
- noPositions
;
1289 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1293 if (noPositions
== 1 && !extendSel
)
1294 MoveCaretBack(oldPos
);
1296 SetCaretPosition(newPos
);
1299 SetDefaultStyleToCursorStyle();
1310 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1312 return MoveDown(- noLines
, flags
);
1316 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1321 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1322 wxPoint pt
= GetCaret()->GetPosition();
1323 long newLine
= lineNumber
+ noLines
;
1325 if (lineNumber
!= -1)
1329 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1331 if (newLine
> lastLine
)
1341 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1344 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1350 wxClientDC
dc(this);
1352 dc
.SetFont(GetFont());
1354 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1356 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1358 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1359 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1360 // so we view the caret at the start of the line.
1361 bool caretLineStart
= false;
1362 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1364 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1365 wxRichTextRange lineRange
;
1367 lineRange
= thisLine
->GetAbsoluteRange();
1369 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1372 caretLineStart
= true;
1376 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1377 if (para
&& para
->GetRange().GetStart() == newPos
)
1382 long newSelEnd
= newPos
;
1384 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1388 SetCaretPosition(newPos
, caretLineStart
);
1390 SetDefaultStyleToCursorStyle();
1400 /// Move to the end of the paragraph
1401 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1403 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1406 long newPos
= para
->GetRange().GetEnd() - 1;
1407 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1411 SetCaretPosition(newPos
);
1413 SetDefaultStyleToCursorStyle();
1423 /// Move to the start of the paragraph
1424 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1426 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1429 long newPos
= para
->GetRange().GetStart() - 1;
1430 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1434 SetCaretPosition(newPos
);
1436 SetDefaultStyleToCursorStyle();
1446 /// Move to the end of the line
1447 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1449 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1453 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1454 long newPos
= lineRange
.GetEnd();
1455 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1459 SetCaretPosition(newPos
);
1461 SetDefaultStyleToCursorStyle();
1471 /// Move to the start of the line
1472 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1474 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1477 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1478 long newPos
= lineRange
.GetStart()-1;
1480 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1484 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1486 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1488 SetDefaultStyleToCursorStyle();
1498 /// Move to the start of the buffer
1499 bool wxRichTextCtrl::MoveHome(int flags
)
1501 if (m_caretPosition
!= -1)
1503 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1507 SetCaretPosition(-1);
1509 SetDefaultStyleToCursorStyle();
1519 /// Move to the end of the buffer
1520 bool wxRichTextCtrl::MoveEnd(int flags
)
1522 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1524 if (m_caretPosition
!= endPos
)
1526 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1530 SetCaretPosition(endPos
);
1532 SetDefaultStyleToCursorStyle();
1542 /// Move noPages pages up
1543 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1545 return PageDown(- noPages
, flags
);
1548 /// Move noPages pages down
1549 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1551 // Calculate which line occurs noPages * screen height further down.
1552 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1555 wxSize clientSize
= GetClientSize();
1556 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1558 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1561 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1562 long pos
= lineRange
.GetStart()-1;
1563 if (pos
!= m_caretPosition
)
1565 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1567 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1571 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1573 SetDefaultStyleToCursorStyle();
1585 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1587 return str
== wxT(" ") || str
== wxT("\t");
1590 // Finds the caret position for the next word
1591 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1593 long endPos
= GetBuffer().GetRange().GetEnd();
1597 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1599 // First skip current text to space
1600 while (i
< endPos
&& i
> -1)
1602 // i is in character, not caret positions
1603 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1604 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1605 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1609 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1616 while (i
< endPos
&& i
> -1)
1618 // i is in character, not caret positions
1619 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1620 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1621 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1622 return wxMax(-1, i
);
1624 if (text
.empty()) // End of paragraph, or maybe an image
1625 return wxMax(-1, i
- 1);
1626 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1630 // Convert to caret position
1631 return wxMax(-1, i
- 1);
1640 long i
= m_caretPosition
;
1642 // First skip white space
1643 while (i
< endPos
&& i
> -1)
1645 // i is in character, not caret positions
1646 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1647 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1649 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1651 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1656 // Next skip current text to space
1657 while (i
< endPos
&& i
> -1)
1659 // i is in character, not caret positions
1660 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1661 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1662 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1665 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1678 /// Move n words left
1679 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1681 long pos
= FindNextWordPosition(-1);
1682 if (pos
!= m_caretPosition
)
1684 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1686 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1690 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1692 SetDefaultStyleToCursorStyle();
1702 /// Move n words right
1703 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1705 long pos
= FindNextWordPosition(1);
1706 if (pos
!= m_caretPosition
)
1708 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1710 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1714 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1716 SetDefaultStyleToCursorStyle();
1727 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1729 // Only do sizing optimization for large buffers
1730 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1732 m_fullLayoutRequired
= true;
1733 m_fullLayoutTime
= wxGetLocalTimeMillis();
1734 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1735 LayoutContent(true /* onlyVisibleRect */);
1738 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1740 #if wxRICHTEXT_BUFFERED_PAINTING
1748 /// Idle-time processing
1749 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1751 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1753 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1755 m_fullLayoutRequired
= false;
1756 m_fullLayoutTime
= 0;
1757 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1758 ShowPosition(m_fullLayoutSavedPosition
);
1762 if (m_caretPositionForDefaultStyle
!= -2)
1764 // If the caret position has changed, no longer reflect the default style
1766 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1767 m_caretPositionForDefaultStyle
= -2;
1774 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1780 /// Set up scrollbars, e.g. after a resize
1781 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1786 if (GetBuffer().IsEmpty())
1788 SetScrollbars(0, 0, 0, 0, 0, 0);
1792 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1793 // of pixels. See e.g. wxVScrolledWindow for ideas.
1794 int pixelsPerUnit
= 5;
1795 wxSize clientSize
= GetClientSize();
1797 int maxHeight
= GetBuffer().GetCachedSize().y
;
1799 // Round up so we have at least maxHeight pixels
1800 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1802 int startX
= 0, startY
= 0;
1804 GetViewStart(& startX
, & startY
);
1806 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1807 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1809 // Move to previous scroll position if
1811 SetScrollbars(0, pixelsPerUnit
,
1813 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1816 /// Paint the background
1817 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1819 wxColour backgroundColour
= GetBackgroundColour();
1820 if (!backgroundColour
.Ok())
1821 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1823 // Clear the background
1824 dc
.SetBrush(wxBrush(backgroundColour
));
1825 dc
.SetPen(*wxTRANSPARENT_PEN
);
1826 wxRect
windowRect(GetClientSize());
1827 windowRect
.x
-= 2; windowRect
.y
-= 2;
1828 windowRect
.width
+= 4; windowRect
.height
+= 4;
1830 // We need to shift the rectangle to take into account
1831 // scrolling. Converting device to logical coordinates.
1832 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1833 dc
.DrawRectangle(windowRect
);
1836 #if wxRICHTEXT_BUFFERED_PAINTING
1837 /// Recreate buffer bitmap if necessary
1838 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1841 if (sz
== wxDefaultSize
)
1842 sz
= GetClientSize();
1844 if (sz
.x
< 1 || sz
.y
< 1)
1847 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1848 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1849 return m_bufferBitmap
.Ok();
1853 // ----------------------------------------------------------------------------
1854 // file IO functions
1855 // ----------------------------------------------------------------------------
1857 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1859 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1861 m_filename
= filename
;
1864 SetInsertionPoint(0);
1867 SetupScrollbars(true);
1869 wxTextCtrl::SendTextUpdatedEvent(this);
1875 wxLogError(_("File couldn't be loaded."));
1881 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1883 if (GetBuffer().SaveFile(filename
, fileType
))
1885 m_filename
= filename
;
1892 wxLogError(_("The text couldn't be saved."));
1897 // ----------------------------------------------------------------------------
1898 // wxRichTextCtrl specific functionality
1899 // ----------------------------------------------------------------------------
1901 /// Add a new paragraph of text to the end of the buffer
1902 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1904 return GetBuffer().AddParagraph(text
);
1908 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1910 return GetBuffer().AddImage(image
);
1913 // ----------------------------------------------------------------------------
1914 // selection and ranges
1915 // ----------------------------------------------------------------------------
1917 void wxRichTextCtrl::SelectAll()
1919 SetSelection(0, GetLastPosition()+1);
1920 m_selectionAnchor
= -1;
1924 void wxRichTextCtrl::SelectNone()
1926 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1927 SetSelection(-2, -2);
1928 m_selectionAnchor
= -2;
1931 static bool wxIsWordDelimiter(const wxString
& text
)
1933 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1936 /// Select the word at the given character position
1937 bool wxRichTextCtrl::SelectWord(long position
)
1939 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1942 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1946 long positionStart
= position
;
1947 long positionEnd
= position
;
1949 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1951 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1952 if (wxIsWordDelimiter(text
))
1958 if (positionStart
< para
->GetRange().GetStart())
1959 positionStart
= para
->GetRange().GetStart();
1961 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1963 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1964 if (wxIsWordDelimiter(text
))
1970 if (positionEnd
>= para
->GetRange().GetEnd())
1971 positionEnd
= para
->GetRange().GetEnd();
1973 SetSelection(positionStart
, positionEnd
+1);
1975 if (positionStart
>= 0)
1977 MoveCaret(positionStart
-1, true);
1978 SetDefaultStyleToCursorStyle();
1984 wxString
wxRichTextCtrl::GetStringSelection() const
1987 GetSelection(&from
, &to
);
1989 return GetRange(from
, to
);
1992 // ----------------------------------------------------------------------------
1994 // ----------------------------------------------------------------------------
1996 wxTextCtrlHitTestResult
1997 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1999 // implement in terms of the other overload as the native ports typically
2000 // can get the position and not (x, y) pair directly (although wxUniv
2001 // directly gets x and y -- and so overrides this method as well)
2003 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2005 if ( rc
!= wxTE_HT_UNKNOWN
)
2007 PositionToXY(pos
, x
, y
);
2013 wxTextCtrlHitTestResult
2014 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2017 wxClientDC
dc((wxRichTextCtrl
*) this);
2018 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2020 // Buffer uses logical position (relative to start of buffer)
2022 wxPoint pt2
= GetLogicalPoint(pt
);
2024 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2026 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2027 return wxTE_HT_BEFORE
;
2028 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2029 return wxTE_HT_BEYOND
;
2030 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2031 return wxTE_HT_ON_TEXT
;
2033 return wxTE_HT_UNKNOWN
;
2036 // ----------------------------------------------------------------------------
2037 // set/get the controls text
2038 // ----------------------------------------------------------------------------
2040 wxString
wxRichTextCtrl::GetValue() const
2042 return GetBuffer().GetText();
2045 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2047 // Public API for range is different from internals
2048 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2051 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2055 if (!value
.IsEmpty())
2057 // Remove empty paragraph
2058 GetBuffer().Clear();
2061 // for compatibility, don't move the cursor when doing SetValue()
2062 SetInsertionPoint(0);
2066 // still send an event for consistency
2067 if (flags
& SetValue_SendEvent
)
2068 wxTextCtrl::SendTextUpdatedEvent(this);
2073 void wxRichTextCtrl::WriteText(const wxString
& value
)
2078 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2080 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2082 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2084 if ( flags
& SetValue_SendEvent
)
2085 wxTextCtrl::SendTextUpdatedEvent(this);
2088 void wxRichTextCtrl::AppendText(const wxString
& text
)
2090 SetInsertionPointEnd();
2095 /// Write an image at the current insertion point
2096 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2098 wxRichTextImageBlock imageBlock
;
2100 wxImage image2
= image
;
2101 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2102 return WriteImage(imageBlock
);
2107 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2109 wxRichTextImageBlock imageBlock
;
2112 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2113 return WriteImage(imageBlock
);
2118 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2120 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2123 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2127 wxRichTextImageBlock imageBlock
;
2129 wxImage image
= bitmap
.ConvertToImage();
2130 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2131 return WriteImage(imageBlock
);
2137 /// Insert a newline (actually paragraph) at the current insertion point.
2138 bool wxRichTextCtrl::Newline()
2140 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2143 /// Insert a line break at the current insertion point.
2144 bool wxRichTextCtrl::LineBreak()
2147 text
= wxRichTextLineBreakChar
;
2148 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2151 // ----------------------------------------------------------------------------
2152 // Clipboard operations
2153 // ----------------------------------------------------------------------------
2155 void wxRichTextCtrl::Copy()
2159 wxRichTextRange range
= GetInternalSelectionRange();
2160 GetBuffer().CopyToClipboard(range
);
2164 void wxRichTextCtrl::Cut()
2168 wxRichTextRange range
= GetInternalSelectionRange();
2169 GetBuffer().CopyToClipboard(range
);
2171 DeleteSelectedContent();
2177 void wxRichTextCtrl::Paste()
2181 BeginBatchUndo(_("Paste"));
2183 long newPos
= m_caretPosition
;
2184 DeleteSelectedContent(& newPos
);
2186 GetBuffer().PasteFromClipboard(newPos
);
2192 void wxRichTextCtrl::DeleteSelection()
2194 if (CanDeleteSelection())
2196 DeleteSelectedContent();
2200 bool wxRichTextCtrl::HasSelection() const
2202 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2205 bool wxRichTextCtrl::CanCopy() const
2207 // Can copy if there's a selection
2208 return HasSelection();
2211 bool wxRichTextCtrl::CanCut() const
2213 return HasSelection() && IsEditable();
2216 bool wxRichTextCtrl::CanPaste() const
2218 if ( !IsEditable() )
2221 return GetBuffer().CanPasteFromClipboard();
2224 bool wxRichTextCtrl::CanDeleteSelection() const
2226 return HasSelection() && IsEditable();
2230 // ----------------------------------------------------------------------------
2232 // ----------------------------------------------------------------------------
2234 void wxRichTextCtrl::SetEditable(bool editable
)
2236 m_editable
= editable
;
2239 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2243 m_caretPosition
= pos
- 1;
2248 void wxRichTextCtrl::SetInsertionPointEnd()
2250 long pos
= GetLastPosition();
2251 SetInsertionPoint(pos
);
2254 long wxRichTextCtrl::GetInsertionPoint() const
2256 return m_caretPosition
+1;
2259 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2261 return GetBuffer().GetRange().GetEnd();
2264 // If the return values from and to are the same, there is no
2266 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2268 *from
= m_selectionRange
.GetStart();
2269 *to
= m_selectionRange
.GetEnd();
2270 if ((*to
) != -1 && (*to
) != -2)
2274 bool wxRichTextCtrl::IsEditable() const
2279 // ----------------------------------------------------------------------------
2281 // ----------------------------------------------------------------------------
2283 void wxRichTextCtrl::SetSelection(long from
, long to
)
2285 // if from and to are both -1, it means (in wxWidgets) that all text should
2287 if ( (from
== -1) && (to
== -1) )
2290 to
= GetLastPosition()+1;
2293 DoSetSelection(from
, to
);
2296 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2298 m_selectionAnchor
= from
;
2299 m_selectionRange
.SetRange(from
, to
-1);
2301 m_caretPosition
= from
-1;
2307 // ----------------------------------------------------------------------------
2309 // ----------------------------------------------------------------------------
2311 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2312 const wxString
& value
)
2314 BeginBatchUndo(_("Replace"));
2316 DeleteSelectedContent();
2318 DoWriteText(value
, SetValue_SelectionOnly
);
2323 void wxRichTextCtrl::Remove(long from
, long to
)
2327 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2334 bool wxRichTextCtrl::IsModified() const
2336 return m_buffer
.IsModified();
2339 void wxRichTextCtrl::MarkDirty()
2341 m_buffer
.Modify(true);
2344 void wxRichTextCtrl::DiscardEdits()
2346 m_caretPositionForDefaultStyle
= -2;
2347 m_buffer
.Modify(false);
2348 m_buffer
.GetCommandProcessor()->ClearCommands();
2351 int wxRichTextCtrl::GetNumberOfLines() const
2353 return GetBuffer().GetParagraphCount();
2356 // ----------------------------------------------------------------------------
2357 // Positions <-> coords
2358 // ----------------------------------------------------------------------------
2360 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2362 return GetBuffer().XYToPosition(x
, y
);
2365 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2367 return GetBuffer().PositionToXY(pos
, x
, y
);
2370 // ----------------------------------------------------------------------------
2372 // ----------------------------------------------------------------------------
2374 void wxRichTextCtrl::ShowPosition(long pos
)
2376 if (!IsPositionVisible(pos
))
2377 ScrollIntoView(pos
-1, WXK_DOWN
);
2380 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2382 return GetBuffer().GetParagraphLength(lineNo
);
2385 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2387 return GetBuffer().GetParagraphText(lineNo
);
2390 // ----------------------------------------------------------------------------
2392 // ----------------------------------------------------------------------------
2394 void wxRichTextCtrl::Undo()
2398 GetCommandProcessor()->Undo();
2402 void wxRichTextCtrl::Redo()
2406 GetCommandProcessor()->Redo();
2410 bool wxRichTextCtrl::CanUndo() const
2412 return GetCommandProcessor()->CanUndo();
2415 bool wxRichTextCtrl::CanRedo() const
2417 return GetCommandProcessor()->CanRedo();
2420 // ----------------------------------------------------------------------------
2421 // implementation details
2422 // ----------------------------------------------------------------------------
2424 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2426 SetValue(event
.GetString());
2427 GetEventHandler()->ProcessEvent(event
);
2430 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2432 // By default, load the first file into the text window.
2433 if (event
.GetNumberOfFiles() > 0)
2435 LoadFile(event
.GetFiles()[0]);
2439 wxSize
wxRichTextCtrl::DoGetBestSize() const
2441 return wxSize(10, 10);
2444 // ----------------------------------------------------------------------------
2445 // standard handlers for standard edit menu events
2446 // ----------------------------------------------------------------------------
2448 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2453 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2458 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2463 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2468 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2473 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2478 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2480 event
.Enable( CanCut() );
2483 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2485 event
.Enable( CanCopy() );
2488 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2490 event
.Enable( CanDeleteSelection() );
2493 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2495 event
.Enable( CanPaste() );
2498 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2500 event
.Enable( CanUndo() );
2501 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2504 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2506 event
.Enable( CanRedo() );
2507 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2510 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2515 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2517 event
.Enable(GetLastPosition() > 0);
2520 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2522 if (event
.GetEventObject() != this)
2530 m_contextMenu
= new wxMenu
;
2531 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2532 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2533 m_contextMenu
->AppendSeparator();
2534 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2535 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2536 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2537 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2538 m_contextMenu
->AppendSeparator();
2539 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2541 PopupMenu(m_contextMenu
);
2545 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2547 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2550 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2552 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2555 // extended style setting operation with flags including:
2556 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2557 // see richtextbuffer.h for more details.
2559 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2561 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2564 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2566 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2569 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2571 return GetBuffer().GetDefaultStyle();
2574 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2576 return GetBuffer().GetStyle(position
, style
);
2579 // get the common set of styles for the range
2580 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2582 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2585 /// Get the content (uncombined) attributes for this position.
2586 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2588 return GetBuffer().GetUncombinedStyle(position
, style
);
2591 /// Set font, and also the buffer attributes
2592 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2594 wxControl::SetFont(font
);
2596 wxTextAttr attr
= GetBuffer().GetAttributes();
2598 GetBuffer().SetBasicStyle(attr
);
2600 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2606 /// Transform logical to physical
2607 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2610 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2615 /// Transform physical to logical
2616 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2619 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2624 /// Position the caret
2625 void wxRichTextCtrl::PositionCaret()
2630 //wxLogDebug(wxT("PositionCaret"));
2633 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2635 wxPoint newPt
= caretRect
.GetPosition();
2636 wxSize newSz
= caretRect
.GetSize();
2637 wxPoint pt
= GetPhysicalPoint(newPt
);
2638 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2640 GetCaret()->Move(pt
);
2641 GetCaret()->SetSize(newSz
);
2646 /// Get the caret height and position for the given character position
2647 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2649 wxClientDC
dc(this);
2650 dc
.SetFont(GetFont());
2657 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2659 // Caret height can't be zero
2661 height
= dc
.GetCharHeight();
2663 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2670 /// Gets the line for the visible caret position. If the caret is
2671 /// shown at the very end of the line, it means the next character is actually
2672 /// on the following line. So let's get the line we're expecting to find
2673 /// if this is the case.
2674 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2676 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2677 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2680 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2681 if (caretPosition
== lineRange
.GetStart()-1 &&
2682 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2684 if (!m_caretAtLineStart
)
2685 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2692 /// Move the caret to the given character position
2693 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2695 if (GetBuffer().GetDirty())
2698 if (pos
<= GetBuffer().GetRange().GetEnd())
2700 SetCaretPosition(pos
, showAtLineStart
);
2710 /// Layout the buffer: which we must do before certain operations, such as
2711 /// setting the caret position.
2712 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2714 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2716 wxRect
availableSpace(GetClientSize());
2717 if (availableSpace
.width
== 0)
2718 availableSpace
.width
= 10;
2719 if (availableSpace
.height
== 0)
2720 availableSpace
.height
= 10;
2722 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2723 if (onlyVisibleRect
)
2725 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2726 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2729 wxClientDC
dc(this);
2730 dc
.SetFont(GetFont());
2734 GetBuffer().Defragment();
2735 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2736 GetBuffer().Layout(dc
, availableSpace
, flags
);
2737 GetBuffer().SetDirty(false);
2746 /// Is all of the selection bold?
2747 bool wxRichTextCtrl::IsSelectionBold()
2752 wxRichTextRange range
= GetSelectionRange();
2753 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2754 attr
.SetFontWeight(wxBOLD
);
2756 return HasCharacterAttributes(range
, attr
);
2760 // If no selection, then we need to combine current style with default style
2761 // to see what the effect would be if we started typing.
2763 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2765 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2766 if (GetStyle(pos
, attr
))
2768 if (IsDefaultStyleShowing())
2769 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2770 return attr
.GetFontWeight() == wxBOLD
;
2776 /// Is all of the selection italics?
2777 bool wxRichTextCtrl::IsSelectionItalics()
2781 wxRichTextRange range
= GetSelectionRange();
2783 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2784 attr
.SetFontStyle(wxITALIC
);
2786 return HasCharacterAttributes(range
, attr
);
2790 // If no selection, then we need to combine current style with default style
2791 // to see what the effect would be if we started typing.
2793 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2795 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2796 if (GetStyle(pos
, attr
))
2798 if (IsDefaultStyleShowing())
2799 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2800 return attr
.GetFontStyle() == wxITALIC
;
2806 /// Is all of the selection underlined?
2807 bool wxRichTextCtrl::IsSelectionUnderlined()
2811 wxRichTextRange range
= GetSelectionRange();
2813 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2814 attr
.SetFontUnderlined(true);
2816 return HasCharacterAttributes(range
, attr
);
2820 // If no selection, then we need to combine current style with default style
2821 // to see what the effect would be if we started typing.
2823 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2824 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2826 if (GetStyle(pos
, attr
))
2828 if (IsDefaultStyleShowing())
2829 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2830 return attr
.GetFontUnderlined();
2836 /// Apply bold to the selection
2837 bool wxRichTextCtrl::ApplyBoldToSelection()
2840 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2841 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2844 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2846 SetAndShowDefaultStyle(attr
);
2850 /// Apply italic to the selection
2851 bool wxRichTextCtrl::ApplyItalicToSelection()
2854 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2855 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2858 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2860 SetAndShowDefaultStyle(attr
);
2864 /// Apply underline to the selection
2865 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2868 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2869 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2872 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2874 SetAndShowDefaultStyle(attr
);
2878 /// Is all of the selection aligned according to the specified flag?
2879 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2881 wxRichTextRange range
;
2883 range
= GetSelectionRange();
2885 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2888 attr
.SetAlignment(alignment
);
2890 return HasParagraphAttributes(range
, attr
);
2893 /// Apply alignment to the selection
2894 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2897 attr
.SetAlignment(alignment
);
2899 return SetStyle(GetSelectionRange(), attr
);
2902 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2904 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2909 /// Apply a named style to the selection
2910 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2912 // Flags are defined within each definition, so only certain
2913 // attributes are applied.
2914 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2916 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2918 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2920 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2922 wxRichTextRange range
;
2925 range
= GetSelectionRange();
2928 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2929 range
= wxRichTextRange(pos
, pos
+1);
2932 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2935 // Make sure the attr has the style name
2936 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2938 attr
.SetParagraphStyleName(def
->GetName());
2940 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2941 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2942 // to change its style independently.
2943 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2946 attr
.SetCharacterStyleName(def
->GetName());
2949 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2952 SetAndShowDefaultStyle(attr
);
2957 /// Apply the style sheet to the buffer, for example if the styles have changed.
2958 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2961 styleSheet
= GetBuffer().GetStyleSheet();
2965 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2967 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2975 /// Sets the default style to the style under the cursor
2976 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2979 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2981 // If at the start of a paragraph, use the next position.
2982 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2984 if (GetUncombinedStyle(pos
, attr
))
2986 SetDefaultStyle(attr
);
2993 /// Returns the first visible position in the current view
2994 long wxRichTextCtrl::GetFirstVisiblePosition() const
2996 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2998 return line
->GetAbsoluteRange().GetStart();
3003 /// Get the first visible point in the window
3004 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3007 int startXUnits
, startYUnits
;
3009 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3010 GetViewStart(& startXUnits
, & startYUnits
);
3012 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3015 /// The adjusted caret position is the character position adjusted to take
3016 /// into account whether we're at the start of a paragraph, in which case
3017 /// style information should be taken from the next position, not current one.
3018 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3020 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3022 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3027 /// Get/set the selection range in character positions. -1, -1 means no selection.
3028 /// The range is in API convention, i.e. a single character selection is denoted
3030 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3032 wxRichTextRange range
= GetInternalSelectionRange();
3033 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3034 range
.SetEnd(range
.GetEnd() + 1);
3038 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3040 wxRichTextRange
range1(range
);
3041 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3042 range1
.SetEnd(range1
.GetEnd() - 1);
3044 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3046 SetInternalSelectionRange(range1
);
3050 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3052 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3055 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3057 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3060 /// Clear list for given range
3061 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3063 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3066 /// Number/renumber any list elements in the given range
3067 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3069 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3072 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3074 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3077 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3078 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3080 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3083 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3085 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3088 /// Deletes the content in the given range
3089 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3091 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3094 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3096 if (sm_availableFontNames
.GetCount() == 0)
3098 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3099 sm_availableFontNames
.Sort();
3101 return sm_availableFontNames
;
3104 void wxRichTextCtrl::ClearAvailableFontNames()
3106 sm_availableFontNames
.Clear();
3109 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3111 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3113 wxTextAttrEx basicStyle
= GetBasicStyle();
3114 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3115 SetBasicStyle(basicStyle
);
3116 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));