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
)
79 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
81 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
82 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
84 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
85 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
87 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
88 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
90 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
91 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
93 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
94 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
96 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
97 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
99 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
100 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
107 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
109 wxRichTextCtrl::wxRichTextCtrl()
110 : wxScrollHelper(this)
115 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
117 const wxString
& value
,
121 const wxValidator
& validator
,
122 const wxString
& name
)
123 : wxScrollHelper(this)
126 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
130 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
131 const wxValidator
& validator
, const wxString
& name
)
133 if (!wxControl::Create(parent
, id
, pos
, size
,
134 style
|wxFULL_REPAINT_ON_RESIZE
,
140 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
144 GetBuffer().SetRichTextCtrl(this);
146 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
149 if (style
& wxTE_READONLY
)
152 // The base attributes must all have default values
153 wxTextAttr attributes
;
154 attributes
.SetFont(GetFont());
155 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
156 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
157 attributes
.SetLineSpacing(10);
158 attributes
.SetParagraphSpacingAfter(10);
159 attributes
.SetParagraphSpacingBefore(0);
160 attributes
.SetTextEffects(0);
161 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
163 SetBasicStyle(attributes
);
165 // The default attributes will be merged with base attributes, so
166 // can be empty to begin with
167 wxTextAttr defaultAttributes
;
168 SetDefaultStyle(defaultAttributes
);
170 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
171 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
173 // Tell the sizers to use the given or best size
174 SetInitialSize(size
);
176 #if wxRICHTEXT_BUFFERED_PAINTING
178 RecreateBuffer(size
);
181 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
182 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
184 SetCursor(m_textCursor
);
186 if (!value
.IsEmpty())
189 GetBuffer().AddEventHandler(this);
194 wxRichTextCtrl::~wxRichTextCtrl()
196 GetBuffer().RemoveEventHandler(this);
198 delete m_contextMenu
;
201 /// Member initialisation
202 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 void wxRichTextCtrl::DoThaw()
221 if (GetBuffer().GetDirty())
229 void wxRichTextCtrl::Clear()
231 m_buffer
.ResetAndClearCommands();
232 m_buffer
.SetDirty(true);
233 m_caretPosition
= -1;
234 m_caretPositionForDefaultStyle
= -2;
235 m_caretAtLineStart
= false;
236 m_selectionRange
.SetRange(-2, -2);
244 wxTextCtrl::SendTextUpdatedEvent(this);
248 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
250 if (GetCaret() && GetCaret()->IsVisible())
254 #if wxRICHTEXT_BUFFERED_PAINTING
255 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
265 dc
.SetFont(GetFont());
267 // Paint the background
270 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
272 wxRect
drawingArea(GetUpdateRegion().GetBox());
273 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
275 wxRect
availableSpace(GetClientSize());
276 if (GetBuffer().GetDirty())
278 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
279 GetBuffer().SetDirty(false);
283 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
286 if (GetCaret() && !GetCaret()->IsVisible())
292 // Empty implementation, to prevent flicker
293 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
297 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
301 if (!GetCaret()->IsVisible())
310 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
312 if (GetCaret() && GetCaret()->IsVisible())
319 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
325 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
331 dc
.SetFont(GetFont());
334 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
336 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
338 m_dragStart
= event
.GetLogicalPosition(dc
);
344 bool caretAtLineStart
= false;
346 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
348 // If we're at the start of a line (but not first in para)
349 // then we should keep the caret showing at the start of the line
350 // by showing the m_caretAtLineStart flag.
351 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
352 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
354 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
355 caretAtLineStart
= true;
359 MoveCaret(position
, caretAtLineStart
);
360 SetDefaultStyleToCursorStyle();
367 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
372 if (GetCapture() == this)
375 // See if we clicked on a URL
378 dc
.SetFont(GetFont());
381 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
382 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
384 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
386 wxRichTextEvent
cmdEvent(
387 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
389 cmdEvent
.SetEventObject(this);
390 cmdEvent
.SetPosition(m_caretPosition
+1);
392 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
395 if (GetStyle(position
, attr
))
397 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
399 wxString urlTarget
= attr
.GetURL();
400 if (!urlTarget
.IsEmpty())
402 wxMouseEvent
mouseEvent(event
);
404 long startPos
= 0, endPos
= 0;
405 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
408 startPos
= obj
->GetRange().GetStart();
409 endPos
= obj
->GetRange().GetEnd();
412 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
413 InitCommandEvent(urlEvent
);
415 urlEvent
.SetString(urlTarget
);
417 GetEventHandler()->ProcessEvent(urlEvent
);
427 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
431 dc
.SetFont(GetFont());
434 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
435 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
437 // See if we need to change the cursor
440 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
443 if (GetStyle(position
, attr
))
445 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
447 SetCursor(m_urlCursor
);
449 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
451 SetCursor(m_textCursor
);
456 SetCursor(m_textCursor
);
459 if (!event
.Dragging())
465 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
467 // TODO: test closeness
469 bool caretAtLineStart
= false;
471 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
473 // If we're at the start of a line (but not first in para)
474 // then we should keep the caret showing at the start of the line
475 // by showing the m_caretAtLineStart flag.
476 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
477 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
479 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
480 caretAtLineStart
= true;
484 if (m_caretPosition
!= position
)
486 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
488 MoveCaret(position
, caretAtLineStart
);
489 SetDefaultStyleToCursorStyle();
498 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
502 wxRichTextEvent
cmdEvent(
503 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
505 cmdEvent
.SetEventObject(this);
506 cmdEvent
.SetPosition(m_caretPosition
+1);
508 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
512 /// Left-double-click
513 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
515 wxRichTextEvent
cmdEvent(
516 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
518 cmdEvent
.SetEventObject(this);
519 cmdEvent
.SetPosition(m_caretPosition
+1);
521 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
523 SelectWord(GetCaretPosition()+1);
528 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
530 wxRichTextEvent
cmdEvent(
531 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
533 cmdEvent
.SetEventObject(this);
534 cmdEvent
.SetPosition(m_caretPosition
+1);
536 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
541 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
545 flags
|= wxRICHTEXT_CTRL_DOWN
;
546 if (event
.ShiftDown())
547 flags
|= wxRICHTEXT_SHIFT_DOWN
;
549 flags
|= wxRICHTEXT_ALT_DOWN
;
551 if (event
.GetKeyCode() == WXK_LEFT
||
552 event
.GetKeyCode() == WXK_RIGHT
||
553 event
.GetKeyCode() == WXK_UP
||
554 event
.GetKeyCode() == WXK_DOWN
||
555 event
.GetKeyCode() == WXK_HOME
||
556 event
.GetKeyCode() == WXK_PAGEUP
||
557 event
.GetKeyCode() == WXK_PAGEDOWN
||
558 event
.GetKeyCode() == WXK_END
||
560 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
561 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
562 event
.GetKeyCode() == WXK_NUMPAD_UP
||
563 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
564 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
565 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
566 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
567 event
.GetKeyCode() == WXK_NUMPAD_END
)
569 KeyboardNavigate(event
.GetKeyCode(), flags
);
573 // all the other keys modify the controls contents which shouldn't be
574 // possible if we're read-only
581 if (event
.GetKeyCode() == WXK_RETURN
)
583 BeginBatchUndo(_("Insert Text"));
585 long newPos
= m_caretPosition
;
587 DeleteSelectedContent(& newPos
);
589 if (event
.ShiftDown())
592 text
= wxRichTextLineBreakChar
;
593 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
596 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
599 SetDefaultStyleToCursorStyle();
601 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
603 wxRichTextEvent
cmdEvent(
604 wxEVT_COMMAND_RICHTEXT_RETURN
,
606 cmdEvent
.SetEventObject(this);
607 cmdEvent
.SetFlags(flags
);
608 cmdEvent
.SetPosition(newPos
+1);
610 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
612 // Generate conventional event
613 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
614 InitCommandEvent(textEvent
);
616 GetEventHandler()->ProcessEvent(textEvent
);
620 else if (event
.GetKeyCode() == WXK_BACK
)
622 BeginBatchUndo(_("Delete Text"));
624 // Submit range in character positions, which are greater than caret positions,
625 // so subtract 1 for deleted character and add 1 for conversion to character position.
626 if (m_caretPosition
> -1 && !HasSelection())
628 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
631 DeleteSelectedContent();
635 if (GetLastPosition() == -1)
639 m_caretPosition
= -1;
641 SetDefaultStyleToCursorStyle();
644 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
646 wxRichTextEvent
cmdEvent(
647 wxEVT_COMMAND_RICHTEXT_DELETE
,
649 cmdEvent
.SetEventObject(this);
650 cmdEvent
.SetFlags(flags
);
651 cmdEvent
.SetPosition(m_caretPosition
+1);
652 GetEventHandler()->ProcessEvent(cmdEvent
);
656 else if (event
.GetKeyCode() == WXK_DELETE
)
658 BeginBatchUndo(_("Delete Text"));
660 // Submit range in character positions, which are greater than caret positions,
661 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
663 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
666 DeleteSelectedContent();
670 if (GetLastPosition() == -1)
674 m_caretPosition
= -1;
676 SetDefaultStyleToCursorStyle();
679 wxRichTextEvent
cmdEvent(
680 wxEVT_COMMAND_RICHTEXT_DELETE
,
682 cmdEvent
.SetEventObject(this);
683 cmdEvent
.SetFlags(flags
);
684 cmdEvent
.SetPosition(m_caretPosition
+1);
685 GetEventHandler()->ProcessEvent(cmdEvent
);
691 long keycode
= event
.GetKeyCode();
764 case WXK_NUMPAD_SPACE
:
766 case WXK_NUMPAD_ENTER
:
771 case WXK_NUMPAD_HOME
:
772 case WXK_NUMPAD_LEFT
:
774 case WXK_NUMPAD_RIGHT
:
775 case WXK_NUMPAD_DOWN
:
776 case WXK_NUMPAD_PAGEUP
:
777 case WXK_NUMPAD_PAGEDOWN
:
779 case WXK_NUMPAD_BEGIN
:
780 case WXK_NUMPAD_INSERT
:
781 case WXK_NUMPAD_DELETE
:
782 case WXK_NUMPAD_EQUAL
:
783 case WXK_NUMPAD_MULTIPLY
:
785 case WXK_NUMPAD_SEPARATOR
:
786 case WXK_NUMPAD_SUBTRACT
:
787 case WXK_NUMPAD_DECIMAL
:
788 case WXK_WINDOWS_LEFT
:
796 if (event
.CmdDown() || event
.AltDown())
802 wxRichTextEvent
cmdEvent(
803 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
805 cmdEvent
.SetEventObject(this);
806 cmdEvent
.SetFlags(flags
);
808 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
810 cmdEvent
.SetCharacter((wxChar
) keycode
);
812 cmdEvent
.SetPosition(m_caretPosition
+1);
814 if (keycode
== wxT('\t'))
816 // See if we need to promote or demote the selection or paragraph at the cursor
817 // position, instead of inserting a tab.
818 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
819 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
820 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
822 wxRichTextRange range
;
824 range
= GetSelectionRange();
826 range
= para
->GetRange().FromInternal();
828 int promoteBy
= event
.ShiftDown() ? 1 : -1;
830 PromoteList(promoteBy
, range
, NULL
);
832 GetEventHandler()->ProcessEvent(cmdEvent
);
838 BeginBatchUndo(_("Insert Text"));
840 long newPos
= m_caretPosition
;
841 DeleteSelectedContent(& newPos
);
844 wxString str
= event
.GetUnicodeKey();
846 wxString str
= (wxChar
) event
.GetKeyCode();
848 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
852 SetDefaultStyleToCursorStyle();
853 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
855 GetEventHandler()->ProcessEvent(cmdEvent
);
863 /// Delete content if there is a selection, e.g. when pressing a key.
864 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
868 long pos
= m_selectionRange
.GetStart();
869 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
870 m_selectionRange
.SetRange(-2, -2);
880 /// Keyboard navigation
884 Left: left one character
885 Right: right one character
888 Ctrl-Left: left one word
889 Ctrl-Right: right one word
890 Ctrl-Up: previous paragraph start
891 Ctrl-Down: next start of paragraph
894 Ctrl-Home: start of document
895 Ctrl-End: end of document
897 Page-Down: Down a screen
901 Ctrl-Alt-PgUp: Start of window
902 Ctrl-Alt-PgDn: End of window
903 F8: Start selection mode
904 Esc: End selection mode
906 Adding Shift does the above but starts/extends selection.
911 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
913 bool success
= false;
915 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
917 if (flags
& wxRICHTEXT_CTRL_DOWN
)
918 success
= WordRight(1, flags
);
920 success
= MoveRight(1, flags
);
922 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
924 if (flags
& wxRICHTEXT_CTRL_DOWN
)
925 success
= WordLeft(1, flags
);
927 success
= MoveLeft(1, flags
);
929 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
931 if (flags
& wxRICHTEXT_CTRL_DOWN
)
932 success
= MoveToParagraphStart(flags
);
934 success
= MoveUp(1, flags
);
936 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
938 if (flags
& wxRICHTEXT_CTRL_DOWN
)
939 success
= MoveToParagraphEnd(flags
);
941 success
= MoveDown(1, flags
);
943 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
945 success
= PageUp(1, flags
);
947 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
949 success
= PageDown(1, flags
);
951 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
953 if (flags
& wxRICHTEXT_CTRL_DOWN
)
954 success
= MoveHome(flags
);
956 success
= MoveToLineStart(flags
);
958 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
960 if (flags
& wxRICHTEXT_CTRL_DOWN
)
961 success
= MoveEnd(flags
);
963 success
= MoveToLineEnd(flags
);
968 ScrollIntoView(m_caretPosition
, keyCode
);
969 SetDefaultStyleToCursorStyle();
975 /// Extend the selection. Selections are in caret positions.
976 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
978 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
980 // If not currently selecting, start selecting
981 if (m_selectionRange
.GetStart() == -2)
983 m_selectionAnchor
= oldPos
;
986 m_selectionRange
.SetRange(newPos
+1, oldPos
);
988 m_selectionRange
.SetRange(oldPos
+1, newPos
);
992 // Always ensure that the selection range start is greater than
994 if (newPos
> m_selectionAnchor
)
995 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
997 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1000 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1002 wxLogDebug(wxT("Strange selection range"));
1011 /// Scroll into view, returning true if we scrolled.
1012 /// This takes a _caret_ position.
1013 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1015 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1021 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1023 int startXUnits
, startYUnits
;
1024 GetViewStart(& startXUnits
, & startYUnits
);
1025 int startY
= startYUnits
* ppuY
;
1028 GetVirtualSize(& sx
, & sy
);
1034 wxRect rect
= line
->GetRect();
1036 bool scrolled
= false;
1038 wxSize clientSize
= GetClientSize();
1041 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1042 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1043 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1044 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1046 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1048 // Make it scroll so this item is at the bottom
1050 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1051 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1053 // If we're still off the screen, scroll another line down
1054 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1057 if (startYUnits
!= yUnits
)
1059 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1063 else if (rect
.y
< startY
)
1065 // Make it scroll so this item is at the top
1068 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1070 if (startYUnits
!= yUnits
)
1072 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1078 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1079 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1080 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1081 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1083 if (rect
.y
< startY
)
1085 // Make it scroll so this item is at the top
1088 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1090 if (startYUnits
!= yUnits
)
1092 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1096 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1098 // Make it scroll so this item is at the bottom
1100 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1101 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1103 // If we're still off the screen, scroll another line down
1104 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1107 if (startYUnits
!= yUnits
)
1109 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1119 /// Is the given position visible on the screen?
1120 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1122 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1128 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1131 GetViewStart(& startX
, & startY
);
1133 startY
= startY
* ppuY
;
1135 wxRect rect
= line
->GetRect();
1136 wxSize clientSize
= GetClientSize();
1138 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1141 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1143 m_caretPosition
= position
;
1144 m_caretAtLineStart
= showAtLineStart
;
1147 /// Move caret one visual step forward: this may mean setting a flag
1148 /// and keeping the same position if we're going from the end of one line
1149 /// to the start of the next, which may be the exact same caret position.
1150 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1152 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1154 // Only do the check if we're not at the end of the paragraph (where things work OK
1156 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1158 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1162 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1164 // We're at the end of a line. See whether we need to
1165 // stay at the same actual caret position but change visual
1166 // position, or not.
1167 if (oldPosition
== lineRange
.GetEnd())
1169 if (m_caretAtLineStart
)
1171 // We're already at the start of the line, so actually move on now.
1172 m_caretPosition
= oldPosition
+ 1;
1173 m_caretAtLineStart
= false;
1177 // We're showing at the end of the line, so keep to
1178 // the same position but indicate that we're to show
1179 // at the start of the next line.
1180 m_caretPosition
= oldPosition
;
1181 m_caretAtLineStart
= true;
1183 SetDefaultStyleToCursorStyle();
1189 SetDefaultStyleToCursorStyle();
1192 /// Move caret one visual step backward: this may mean setting a flag
1193 /// and keeping the same position if we're going from the end of one line
1194 /// to the start of the next, which may be the exact same caret position.
1195 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1197 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1199 // Only do the check if we're not at the start of the paragraph (where things work OK
1201 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1203 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1207 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1209 // We're at the start of a line. See whether we need to
1210 // stay at the same actual caret position but change visual
1211 // position, or not.
1212 if (oldPosition
== lineRange
.GetStart())
1214 m_caretPosition
= oldPosition
-1;
1215 m_caretAtLineStart
= true;
1218 else if (oldPosition
== lineRange
.GetEnd())
1220 if (m_caretAtLineStart
)
1222 // We're at the start of the line, so keep the same caret position
1223 // but clear the start-of-line flag.
1224 m_caretPosition
= oldPosition
;
1225 m_caretAtLineStart
= false;
1229 // We're showing at the end of the line, so go back
1230 // to the previous character position.
1231 m_caretPosition
= oldPosition
- 1;
1233 SetDefaultStyleToCursorStyle();
1239 SetDefaultStyleToCursorStyle();
1243 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1245 long endPos
= GetBuffer().GetRange().GetEnd();
1247 if (m_caretPosition
+ noPositions
< endPos
)
1249 long oldPos
= m_caretPosition
;
1250 long newPos
= m_caretPosition
+ noPositions
;
1252 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1256 // Determine by looking at oldPos and m_caretPosition whether
1257 // we moved from the end of a line to the start of the next line, in which case
1258 // we want to adjust the caret position such that it is positioned at the
1259 // start of the next line, rather than jumping past the first character of the
1261 if (noPositions
== 1 && !extendSel
)
1262 MoveCaretForward(oldPos
);
1264 SetCaretPosition(newPos
);
1267 SetDefaultStyleToCursorStyle();
1278 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1282 if (m_caretPosition
> startPos
- noPositions
+ 1)
1284 long oldPos
= m_caretPosition
;
1285 long newPos
= m_caretPosition
- noPositions
;
1286 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1290 if (noPositions
== 1 && !extendSel
)
1291 MoveCaretBack(oldPos
);
1293 SetCaretPosition(newPos
);
1296 SetDefaultStyleToCursorStyle();
1307 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1309 return MoveDown(- noLines
, flags
);
1313 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1318 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1319 wxPoint pt
= GetCaret()->GetPosition();
1320 long newLine
= lineNumber
+ noLines
;
1322 if (lineNumber
!= -1)
1326 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1328 if (newLine
> lastLine
)
1338 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1341 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1347 wxClientDC
dc(this);
1349 dc
.SetFont(GetFont());
1351 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1353 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1355 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1356 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1357 // so we view the caret at the start of the line.
1358 bool caretLineStart
= false;
1359 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1361 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1362 wxRichTextRange lineRange
;
1364 lineRange
= thisLine
->GetAbsoluteRange();
1366 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1369 caretLineStart
= true;
1373 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1374 if (para
&& para
->GetRange().GetStart() == newPos
)
1379 long newSelEnd
= newPos
;
1381 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1385 SetCaretPosition(newPos
, caretLineStart
);
1387 SetDefaultStyleToCursorStyle();
1397 /// Move to the end of the paragraph
1398 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1400 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1403 long newPos
= para
->GetRange().GetEnd() - 1;
1404 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1408 SetCaretPosition(newPos
);
1410 SetDefaultStyleToCursorStyle();
1420 /// Move to the start of the paragraph
1421 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1423 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1426 long newPos
= para
->GetRange().GetStart() - 1;
1427 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1431 SetCaretPosition(newPos
);
1433 SetDefaultStyleToCursorStyle();
1443 /// Move to the end of the line
1444 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1446 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1450 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1451 long newPos
= lineRange
.GetEnd();
1452 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1456 SetCaretPosition(newPos
);
1458 SetDefaultStyleToCursorStyle();
1468 /// Move to the start of the line
1469 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1471 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1474 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1475 long newPos
= lineRange
.GetStart()-1;
1477 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1481 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1483 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1485 SetDefaultStyleToCursorStyle();
1495 /// Move to the start of the buffer
1496 bool wxRichTextCtrl::MoveHome(int flags
)
1498 if (m_caretPosition
!= -1)
1500 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1504 SetCaretPosition(-1);
1506 SetDefaultStyleToCursorStyle();
1516 /// Move to the end of the buffer
1517 bool wxRichTextCtrl::MoveEnd(int flags
)
1519 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1521 if (m_caretPosition
!= endPos
)
1523 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1527 SetCaretPosition(endPos
);
1529 SetDefaultStyleToCursorStyle();
1539 /// Move noPages pages up
1540 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1542 return PageDown(- noPages
, flags
);
1545 /// Move noPages pages down
1546 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1548 // Calculate which line occurs noPages * screen height further down.
1549 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1552 wxSize clientSize
= GetClientSize();
1553 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1555 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1558 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1559 long pos
= lineRange
.GetStart()-1;
1560 if (pos
!= m_caretPosition
)
1562 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1564 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1568 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1570 SetDefaultStyleToCursorStyle();
1582 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1584 return str
== wxT(" ") || str
== wxT("\t");
1587 // Finds the caret position for the next word
1588 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1590 long endPos
= GetBuffer().GetRange().GetEnd();
1594 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1596 // First skip current text to space
1597 while (i
< endPos
&& i
> -1)
1599 // i is in character, not caret positions
1600 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1601 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1602 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1606 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1613 while (i
< endPos
&& i
> -1)
1615 // i is in character, not caret positions
1616 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1617 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1618 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1619 return wxMax(-1, i
);
1621 if (text
.empty()) // End of paragraph, or maybe an image
1622 return wxMax(-1, i
- 1);
1623 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1627 // Convert to caret position
1628 return wxMax(-1, i
- 1);
1637 long i
= m_caretPosition
;
1639 // First skip white space
1640 while (i
< endPos
&& i
> -1)
1642 // i is in character, not caret positions
1643 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1644 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1646 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1648 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1653 // Next skip current text to space
1654 while (i
< endPos
&& i
> -1)
1656 // i is in character, not caret positions
1657 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1658 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1659 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1662 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1675 /// Move n words left
1676 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1678 long pos
= FindNextWordPosition(-1);
1679 if (pos
!= m_caretPosition
)
1681 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1683 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1687 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1689 SetDefaultStyleToCursorStyle();
1699 /// Move n words right
1700 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1702 long pos
= FindNextWordPosition(1);
1703 if (pos
!= m_caretPosition
)
1705 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1707 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1711 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1713 SetDefaultStyleToCursorStyle();
1724 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1726 // Only do sizing optimization for large buffers
1727 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1729 m_fullLayoutRequired
= true;
1730 m_fullLayoutTime
= wxGetLocalTimeMillis();
1731 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1732 LayoutContent(true /* onlyVisibleRect */);
1735 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1737 #if wxRICHTEXT_BUFFERED_PAINTING
1745 /// Idle-time processing
1746 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1748 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1750 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1752 m_fullLayoutRequired
= false;
1753 m_fullLayoutTime
= 0;
1754 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1755 ShowPosition(m_fullLayoutSavedPosition
);
1759 if (m_caretPositionForDefaultStyle
!= -2)
1761 // If the caret position has changed, no longer reflect the default style
1763 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1764 m_caretPositionForDefaultStyle
= -2;
1771 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1777 /// Set up scrollbars, e.g. after a resize
1778 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1783 if (GetBuffer().IsEmpty())
1785 SetScrollbars(0, 0, 0, 0, 0, 0);
1789 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1790 // of pixels. See e.g. wxVScrolledWindow for ideas.
1791 int pixelsPerUnit
= 5;
1792 wxSize clientSize
= GetClientSize();
1794 int maxHeight
= GetBuffer().GetCachedSize().y
;
1796 // Round up so we have at least maxHeight pixels
1797 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1799 int startX
= 0, startY
= 0;
1801 GetViewStart(& startX
, & startY
);
1803 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1804 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1806 // Move to previous scroll position if
1808 SetScrollbars(0, pixelsPerUnit
,
1810 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1813 /// Paint the background
1814 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1816 wxColour backgroundColour
= GetBackgroundColour();
1817 if (!backgroundColour
.Ok())
1818 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1820 // Clear the background
1821 dc
.SetBrush(wxBrush(backgroundColour
));
1822 dc
.SetPen(*wxTRANSPARENT_PEN
);
1823 wxRect
windowRect(GetClientSize());
1824 windowRect
.x
-= 2; windowRect
.y
-= 2;
1825 windowRect
.width
+= 4; windowRect
.height
+= 4;
1827 // We need to shift the rectangle to take into account
1828 // scrolling. Converting device to logical coordinates.
1829 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1830 dc
.DrawRectangle(windowRect
);
1833 #if wxRICHTEXT_BUFFERED_PAINTING
1834 /// Recreate buffer bitmap if necessary
1835 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1838 if (sz
== wxDefaultSize
)
1839 sz
= GetClientSize();
1841 if (sz
.x
< 1 || sz
.y
< 1)
1844 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1845 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1846 return m_bufferBitmap
.Ok();
1850 // ----------------------------------------------------------------------------
1851 // file IO functions
1852 // ----------------------------------------------------------------------------
1854 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1856 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1858 m_filename
= filename
;
1861 SetInsertionPoint(0);
1864 SetupScrollbars(true);
1866 wxTextCtrl::SendTextUpdatedEvent(this);
1872 wxLogError(_("File couldn't be loaded."));
1878 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1880 if (GetBuffer().SaveFile(filename
, fileType
))
1882 m_filename
= filename
;
1889 wxLogError(_("The text couldn't be saved."));
1894 // ----------------------------------------------------------------------------
1895 // wxRichTextCtrl specific functionality
1896 // ----------------------------------------------------------------------------
1898 /// Add a new paragraph of text to the end of the buffer
1899 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1901 return GetBuffer().AddParagraph(text
);
1905 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1907 return GetBuffer().AddImage(image
);
1910 // ----------------------------------------------------------------------------
1911 // selection and ranges
1912 // ----------------------------------------------------------------------------
1914 void wxRichTextCtrl::SelectAll()
1916 SetSelection(0, GetLastPosition()+1);
1917 m_selectionAnchor
= -1;
1921 void wxRichTextCtrl::SelectNone()
1923 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1924 SetSelection(-2, -2);
1925 m_selectionAnchor
= -2;
1928 static bool wxIsWordDelimiter(const wxString
& text
)
1930 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1933 /// Select the word at the given character position
1934 bool wxRichTextCtrl::SelectWord(long position
)
1936 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1939 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1943 long positionStart
= position
;
1944 long positionEnd
= position
;
1946 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1948 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1949 if (wxIsWordDelimiter(text
))
1955 if (positionStart
< para
->GetRange().GetStart())
1956 positionStart
= para
->GetRange().GetStart();
1958 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1960 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1961 if (wxIsWordDelimiter(text
))
1967 if (positionEnd
>= para
->GetRange().GetEnd())
1968 positionEnd
= para
->GetRange().GetEnd();
1970 SetSelection(positionStart
, positionEnd
+1);
1972 if (positionStart
>= 0)
1974 MoveCaret(positionStart
-1, true);
1975 SetDefaultStyleToCursorStyle();
1981 wxString
wxRichTextCtrl::GetStringSelection() const
1984 GetSelection(&from
, &to
);
1986 return GetRange(from
, to
);
1989 // ----------------------------------------------------------------------------
1991 // ----------------------------------------------------------------------------
1993 wxTextCtrlHitTestResult
1994 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1996 // implement in terms of the other overload as the native ports typically
1997 // can get the position and not (x, y) pair directly (although wxUniv
1998 // directly gets x and y -- and so overrides this method as well)
2000 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2002 if ( rc
!= wxTE_HT_UNKNOWN
)
2004 PositionToXY(pos
, x
, y
);
2010 wxTextCtrlHitTestResult
2011 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2014 wxClientDC
dc((wxRichTextCtrl
*) this);
2015 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2017 // Buffer uses logical position (relative to start of buffer)
2019 wxPoint pt2
= GetLogicalPoint(pt
);
2021 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2023 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2024 return wxTE_HT_BEFORE
;
2025 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2026 return wxTE_HT_BEYOND
;
2027 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2028 return wxTE_HT_ON_TEXT
;
2030 return wxTE_HT_UNKNOWN
;
2033 // ----------------------------------------------------------------------------
2034 // set/get the controls text
2035 // ----------------------------------------------------------------------------
2037 wxString
wxRichTextCtrl::GetValue() const
2039 return GetBuffer().GetText();
2042 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2044 // Public API for range is different from internals
2045 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2048 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2052 if (!value
.IsEmpty())
2054 // Remove empty paragraph
2055 GetBuffer().Clear();
2058 // for compatibility, don't move the cursor when doing SetValue()
2059 SetInsertionPoint(0);
2063 // still send an event for consistency
2064 if (flags
& SetValue_SendEvent
)
2065 wxTextCtrl::SendTextUpdatedEvent(this);
2070 void wxRichTextCtrl::WriteText(const wxString
& value
)
2075 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2077 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2079 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2081 if ( flags
& SetValue_SendEvent
)
2082 wxTextCtrl::SendTextUpdatedEvent(this);
2085 void wxRichTextCtrl::AppendText(const wxString
& text
)
2087 SetInsertionPointEnd();
2092 /// Write an image at the current insertion point
2093 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2095 wxRichTextImageBlock imageBlock
;
2097 wxImage image2
= image
;
2098 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2099 return WriteImage(imageBlock
);
2104 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2106 wxRichTextImageBlock imageBlock
;
2109 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2110 return WriteImage(imageBlock
);
2115 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2117 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2120 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2124 wxRichTextImageBlock imageBlock
;
2126 wxImage image
= bitmap
.ConvertToImage();
2127 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2128 return WriteImage(imageBlock
);
2134 /// Insert a newline (actually paragraph) at the current insertion point.
2135 bool wxRichTextCtrl::Newline()
2137 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2140 /// Insert a line break at the current insertion point.
2141 bool wxRichTextCtrl::LineBreak()
2144 text
= wxRichTextLineBreakChar
;
2145 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2148 // ----------------------------------------------------------------------------
2149 // Clipboard operations
2150 // ----------------------------------------------------------------------------
2152 void wxRichTextCtrl::Copy()
2156 wxRichTextRange range
= GetInternalSelectionRange();
2157 GetBuffer().CopyToClipboard(range
);
2161 void wxRichTextCtrl::Cut()
2165 wxRichTextRange range
= GetInternalSelectionRange();
2166 GetBuffer().CopyToClipboard(range
);
2168 DeleteSelectedContent();
2174 void wxRichTextCtrl::Paste()
2178 BeginBatchUndo(_("Paste"));
2180 long newPos
= m_caretPosition
;
2181 DeleteSelectedContent(& newPos
);
2183 GetBuffer().PasteFromClipboard(newPos
);
2189 void wxRichTextCtrl::DeleteSelection()
2191 if (CanDeleteSelection())
2193 DeleteSelectedContent();
2197 bool wxRichTextCtrl::HasSelection() const
2199 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2202 bool wxRichTextCtrl::CanCopy() const
2204 // Can copy if there's a selection
2205 return HasSelection();
2208 bool wxRichTextCtrl::CanCut() const
2210 return HasSelection() && IsEditable();
2213 bool wxRichTextCtrl::CanPaste() const
2215 if ( !IsEditable() )
2218 return GetBuffer().CanPasteFromClipboard();
2221 bool wxRichTextCtrl::CanDeleteSelection() const
2223 return HasSelection() && IsEditable();
2227 // ----------------------------------------------------------------------------
2229 // ----------------------------------------------------------------------------
2231 void wxRichTextCtrl::SetEditable(bool editable
)
2233 m_editable
= editable
;
2236 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2240 m_caretPosition
= pos
- 1;
2243 void wxRichTextCtrl::SetInsertionPointEnd()
2245 long pos
= GetLastPosition();
2246 SetInsertionPoint(pos
);
2249 long wxRichTextCtrl::GetInsertionPoint() const
2251 return m_caretPosition
+1;
2254 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2256 return GetBuffer().GetRange().GetEnd();
2259 // If the return values from and to are the same, there is no
2261 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2263 *from
= m_selectionRange
.GetStart();
2264 *to
= m_selectionRange
.GetEnd();
2265 if ((*to
) != -1 && (*to
) != -2)
2269 bool wxRichTextCtrl::IsEditable() const
2274 // ----------------------------------------------------------------------------
2276 // ----------------------------------------------------------------------------
2278 void wxRichTextCtrl::SetSelection(long from
, long to
)
2280 // if from and to are both -1, it means (in wxWidgets) that all text should
2282 if ( (from
== -1) && (to
== -1) )
2285 to
= GetLastPosition()+1;
2288 DoSetSelection(from
, to
);
2291 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2293 m_selectionAnchor
= from
;
2294 m_selectionRange
.SetRange(from
, to
-1);
2296 m_caretPosition
= from
-1;
2302 // ----------------------------------------------------------------------------
2304 // ----------------------------------------------------------------------------
2306 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2307 const wxString
& value
)
2309 BeginBatchUndo(_("Replace"));
2311 DeleteSelectedContent();
2313 DoWriteText(value
, SetValue_SelectionOnly
);
2318 void wxRichTextCtrl::Remove(long from
, long to
)
2322 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2329 bool wxRichTextCtrl::IsModified() const
2331 return m_buffer
.IsModified();
2334 void wxRichTextCtrl::MarkDirty()
2336 m_buffer
.Modify(true);
2339 void wxRichTextCtrl::DiscardEdits()
2341 m_caretPositionForDefaultStyle
= -2;
2342 m_buffer
.Modify(false);
2343 m_buffer
.GetCommandProcessor()->ClearCommands();
2346 int wxRichTextCtrl::GetNumberOfLines() const
2348 return GetBuffer().GetParagraphCount();
2351 // ----------------------------------------------------------------------------
2352 // Positions <-> coords
2353 // ----------------------------------------------------------------------------
2355 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2357 return GetBuffer().XYToPosition(x
, y
);
2360 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2362 return GetBuffer().PositionToXY(pos
, x
, y
);
2365 // ----------------------------------------------------------------------------
2367 // ----------------------------------------------------------------------------
2369 void wxRichTextCtrl::ShowPosition(long pos
)
2371 if (!IsPositionVisible(pos
))
2372 ScrollIntoView(pos
-1, WXK_DOWN
);
2375 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2377 return GetBuffer().GetParagraphLength(lineNo
);
2380 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2382 return GetBuffer().GetParagraphText(lineNo
);
2385 // ----------------------------------------------------------------------------
2387 // ----------------------------------------------------------------------------
2389 void wxRichTextCtrl::Undo()
2393 GetCommandProcessor()->Undo();
2397 void wxRichTextCtrl::Redo()
2401 GetCommandProcessor()->Redo();
2405 bool wxRichTextCtrl::CanUndo() const
2407 return GetCommandProcessor()->CanUndo();
2410 bool wxRichTextCtrl::CanRedo() const
2412 return GetCommandProcessor()->CanRedo();
2415 // ----------------------------------------------------------------------------
2416 // implementation details
2417 // ----------------------------------------------------------------------------
2419 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2421 SetValue(event
.GetString());
2422 GetEventHandler()->ProcessEvent(event
);
2425 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2427 // By default, load the first file into the text window.
2428 if (event
.GetNumberOfFiles() > 0)
2430 LoadFile(event
.GetFiles()[0]);
2434 wxSize
wxRichTextCtrl::DoGetBestSize() const
2436 return wxSize(10, 10);
2439 // ----------------------------------------------------------------------------
2440 // standard handlers for standard edit menu events
2441 // ----------------------------------------------------------------------------
2443 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2448 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2453 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2458 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2463 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2468 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2473 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2475 event
.Enable( CanCut() );
2478 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2480 event
.Enable( CanCopy() );
2483 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2485 event
.Enable( CanDeleteSelection() );
2488 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2490 event
.Enable( CanPaste() );
2493 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2495 event
.Enable( CanUndo() );
2496 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2499 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2501 event
.Enable( CanRedo() );
2502 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2505 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2510 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2512 event
.Enable(GetLastPosition() > 0);
2515 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2517 if (event
.GetEventObject() != this)
2525 m_contextMenu
= new wxMenu
;
2526 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2527 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2528 m_contextMenu
->AppendSeparator();
2529 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2530 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2531 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2532 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2533 m_contextMenu
->AppendSeparator();
2534 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2536 PopupMenu(m_contextMenu
);
2540 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2542 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2545 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2547 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2550 // extended style setting operation with flags including:
2551 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2552 // see richtextbuffer.h for more details.
2554 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2556 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2559 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2561 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2564 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2566 return GetBuffer().GetDefaultStyle();
2569 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2571 return GetBuffer().GetStyle(position
, style
);
2574 // get the common set of styles for the range
2575 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2577 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2580 /// Get the content (uncombined) attributes for this position.
2581 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2583 return GetBuffer().GetUncombinedStyle(position
, style
);
2586 /// Set font, and also the buffer attributes
2587 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2589 wxControl::SetFont(font
);
2591 wxTextAttr attr
= GetBuffer().GetAttributes();
2593 GetBuffer().SetBasicStyle(attr
);
2595 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2601 /// Transform logical to physical
2602 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2605 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2610 /// Transform physical to logical
2611 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2614 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2619 /// Position the caret
2620 void wxRichTextCtrl::PositionCaret()
2625 //wxLogDebug(wxT("PositionCaret"));
2628 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2630 wxPoint newPt
= caretRect
.GetPosition();
2631 wxSize newSz
= caretRect
.GetSize();
2632 wxPoint pt
= GetPhysicalPoint(newPt
);
2633 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2635 GetCaret()->Move(pt
);
2636 GetCaret()->SetSize(newSz
);
2641 /// Get the caret height and position for the given character position
2642 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2644 wxClientDC
dc(this);
2645 dc
.SetFont(GetFont());
2652 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2654 // Caret height can't be zero
2656 height
= dc
.GetCharHeight();
2658 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2665 /// Gets the line for the visible caret position. If the caret is
2666 /// shown at the very end of the line, it means the next character is actually
2667 /// on the following line. So let's get the line we're expecting to find
2668 /// if this is the case.
2669 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2671 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2672 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2675 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2676 if (caretPosition
== lineRange
.GetStart()-1 &&
2677 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2679 if (!m_caretAtLineStart
)
2680 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2687 /// Move the caret to the given character position
2688 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2690 if (GetBuffer().GetDirty())
2693 if (pos
<= GetBuffer().GetRange().GetEnd())
2695 SetCaretPosition(pos
, showAtLineStart
);
2705 /// Layout the buffer: which we must do before certain operations, such as
2706 /// setting the caret position.
2707 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2709 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2711 wxRect
availableSpace(GetClientSize());
2712 if (availableSpace
.width
== 0)
2713 availableSpace
.width
= 10;
2714 if (availableSpace
.height
== 0)
2715 availableSpace
.height
= 10;
2717 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2718 if (onlyVisibleRect
)
2720 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2721 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2724 wxClientDC
dc(this);
2725 dc
.SetFont(GetFont());
2729 GetBuffer().Defragment();
2730 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2731 GetBuffer().Layout(dc
, availableSpace
, flags
);
2732 GetBuffer().SetDirty(false);
2741 /// Is all of the selection bold?
2742 bool wxRichTextCtrl::IsSelectionBold()
2747 wxRichTextRange range
= GetSelectionRange();
2748 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2749 attr
.SetFontWeight(wxBOLD
);
2751 return HasCharacterAttributes(range
, attr
);
2755 // If no selection, then we need to combine current style with default style
2756 // to see what the effect would be if we started typing.
2758 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2760 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2761 if (GetStyle(pos
, attr
))
2763 if (IsDefaultStyleShowing())
2764 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2765 return attr
.GetFontWeight() == wxBOLD
;
2771 /// Is all of the selection italics?
2772 bool wxRichTextCtrl::IsSelectionItalics()
2776 wxRichTextRange range
= GetSelectionRange();
2778 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2779 attr
.SetFontStyle(wxITALIC
);
2781 return HasCharacterAttributes(range
, attr
);
2785 // If no selection, then we need to combine current style with default style
2786 // to see what the effect would be if we started typing.
2788 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2790 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2791 if (GetStyle(pos
, attr
))
2793 if (IsDefaultStyleShowing())
2794 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2795 return attr
.GetFontStyle() == wxITALIC
;
2801 /// Is all of the selection underlined?
2802 bool wxRichTextCtrl::IsSelectionUnderlined()
2806 wxRichTextRange range
= GetSelectionRange();
2808 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2809 attr
.SetFontUnderlined(true);
2811 return HasCharacterAttributes(range
, attr
);
2815 // If no selection, then we need to combine current style with default style
2816 // to see what the effect would be if we started typing.
2818 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2819 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2821 if (GetStyle(pos
, attr
))
2823 if (IsDefaultStyleShowing())
2824 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2825 return attr
.GetFontUnderlined();
2831 /// Apply bold to the selection
2832 bool wxRichTextCtrl::ApplyBoldToSelection()
2835 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2836 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2839 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2841 SetAndShowDefaultStyle(attr
);
2845 /// Apply italic to the selection
2846 bool wxRichTextCtrl::ApplyItalicToSelection()
2849 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2850 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2853 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2855 SetAndShowDefaultStyle(attr
);
2859 /// Apply underline to the selection
2860 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2863 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2864 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2867 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2869 SetAndShowDefaultStyle(attr
);
2873 /// Is all of the selection aligned according to the specified flag?
2874 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2876 wxRichTextRange range
;
2878 range
= GetSelectionRange();
2880 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2883 attr
.SetAlignment(alignment
);
2885 return HasParagraphAttributes(range
, attr
);
2888 /// Apply alignment to the selection
2889 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2892 attr
.SetAlignment(alignment
);
2894 return SetStyle(GetSelectionRange(), attr
);
2897 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2899 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2904 /// Apply a named style to the selection
2905 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2907 // Flags are defined within each definition, so only certain
2908 // attributes are applied.
2909 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2911 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2913 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2915 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2917 wxRichTextRange range
;
2920 range
= GetSelectionRange();
2923 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2924 range
= wxRichTextRange(pos
, pos
+1);
2927 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2930 // Make sure the attr has the style name
2931 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2933 attr
.SetParagraphStyleName(def
->GetName());
2935 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2936 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2937 // to change its style independently.
2938 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2941 attr
.SetCharacterStyleName(def
->GetName());
2944 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2947 SetAndShowDefaultStyle(attr
);
2952 /// Apply the style sheet to the buffer, for example if the styles have changed.
2953 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2956 styleSheet
= GetBuffer().GetStyleSheet();
2960 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2962 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2970 /// Sets the default style to the style under the cursor
2971 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2974 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2976 // If at the start of a paragraph, use the next position.
2977 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2979 if (GetUncombinedStyle(pos
, attr
))
2981 SetDefaultStyle(attr
);
2988 /// Returns the first visible position in the current view
2989 long wxRichTextCtrl::GetFirstVisiblePosition() const
2991 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2993 return line
->GetAbsoluteRange().GetStart();
2998 /// Get the first visible point in the window
2999 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3002 int startXUnits
, startYUnits
;
3004 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3005 GetViewStart(& startXUnits
, & startYUnits
);
3007 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3010 /// The adjusted caret position is the character position adjusted to take
3011 /// into account whether we're at the start of a paragraph, in which case
3012 /// style information should be taken from the next position, not current one.
3013 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3015 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3017 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3022 /// Get/set the selection range in character positions. -1, -1 means no selection.
3023 /// The range is in API convention, i.e. a single character selection is denoted
3025 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3027 wxRichTextRange range
= GetInternalSelectionRange();
3028 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3029 range
.SetEnd(range
.GetEnd() + 1);
3033 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3035 wxRichTextRange
range1(range
);
3036 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3037 range1
.SetEnd(range1
.GetEnd() - 1);
3039 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3041 SetInternalSelectionRange(range1
);
3045 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3047 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3050 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3052 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3055 /// Clear list for given range
3056 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3058 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3061 /// Number/renumber any list elements in the given range
3062 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3064 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3067 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3069 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3072 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3073 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3075 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3078 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3080 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3083 /// Deletes the content in the given range
3084 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3086 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3089 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3091 if (sm_availableFontNames
.GetCount() == 0)
3093 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3094 sm_availableFontNames
.Sort();
3096 return sm_availableFontNames
;
3099 void wxRichTextCtrl::ClearAvailableFontNames()
3101 sm_availableFontNames
.Clear();
3104 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3106 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3108 wxTextAttrEx basicStyle
= GetBasicStyle();
3109 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3110 SetBasicStyle(basicStyle
);
3111 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));