1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
)
58 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
60 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
62 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
63 EVT_PAINT(wxRichTextCtrl::OnPaint
)
64 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
65 EVT_IDLE(wxRichTextCtrl::OnIdle
)
66 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
67 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
68 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
69 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
70 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
71 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
72 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
73 EVT_CHAR(wxRichTextCtrl::OnChar
)
74 EVT_SIZE(wxRichTextCtrl::OnSize
)
75 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
76 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
77 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
78 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
80 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
81 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
83 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
84 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
86 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
87 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
89 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
90 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
92 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
93 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
95 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
96 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
98 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
99 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
106 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
108 wxRichTextCtrl::wxRichTextCtrl()
109 : wxScrollHelper(this)
114 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
116 const wxString
& value
,
120 const wxValidator
& validator
,
121 const wxString
& name
)
122 : wxScrollHelper(this)
125 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
129 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
130 const wxValidator
& validator
, const wxString
& name
)
132 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
133 style
|wxFULL_REPAINT_ON_RESIZE
,
139 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
143 GetBuffer().SetRichTextCtrl(this);
145 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
148 if (style
& wxTE_READONLY
)
151 // The base attributes must all have default values
152 wxTextAttrEx attributes
;
153 attributes
.SetFont(GetFont());
154 attributes
.SetTextColour(*wxBLACK
);
155 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
156 attributes
.SetLineSpacing(10);
157 attributes
.SetParagraphSpacingAfter(10);
158 attributes
.SetParagraphSpacingBefore(0);
159 attributes
.SetTextEffects(0);
160 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
162 SetBasicStyle(attributes
);
164 // The default attributes will be merged with base attributes, so
165 // can be empty to begin with
166 wxTextAttrEx defaultAttributes
;
167 SetDefaultStyle(defaultAttributes
);
169 SetBackgroundColour(*wxWHITE
);
170 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
172 // Tell the sizers to use the given or best size
173 SetInitialSize(size
);
175 #if wxRICHTEXT_BUFFERED_PAINTING
177 RecreateBuffer(size
);
180 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
181 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
183 SetCursor(m_textCursor
);
185 if (!value
.IsEmpty())
188 GetBuffer().AddEventHandler(this);
193 wxRichTextCtrl::~wxRichTextCtrl()
195 GetBuffer().RemoveEventHandler(this);
197 delete m_contextMenu
;
200 /// Member initialisation
201 void wxRichTextCtrl::Init()
204 m_contextMenu
= NULL
;
206 m_caretPosition
= -1;
207 m_selectionRange
.SetRange(-2, -2);
208 m_selectionAnchor
= -2;
210 m_caretAtLineStart
= false;
212 m_fullLayoutRequired
= false;
213 m_fullLayoutTime
= 0;
214 m_fullLayoutSavedPosition
= 0;
215 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
216 m_caretPositionForDefaultStyle
= -2;
219 /// Call Freeze to prevent refresh
220 void wxRichTextCtrl::Freeze()
225 /// Call Thaw to refresh
226 void wxRichTextCtrl::Thaw()
230 if (m_freezeCount
== 0)
238 void wxRichTextCtrl::Clear()
240 m_buffer
.ResetAndClearCommands();
241 m_buffer
.SetDirty(true);
242 m_caretPosition
= -1;
243 m_caretPositionForDefaultStyle
= -2;
244 m_caretAtLineStart
= false;
245 m_selectionRange
.SetRange(-2, -2);
247 SetScrollbars(0, 0, 0, 0, 0, 0);
249 if (m_freezeCount
== 0)
254 SendTextUpdatedEvent();
258 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
260 if (GetCaret() && GetCaret()->IsVisible())
264 #if wxRICHTEXT_BUFFERED_PAINTING
265 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
271 if (m_freezeCount
> 0)
274 dc
.SetFont(GetFont());
276 // Paint the background
279 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
281 wxRect
drawingArea(GetUpdateRegion().GetBox());
282 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
284 wxRect
availableSpace(GetClientSize());
285 if (GetBuffer().GetDirty())
287 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
288 GetBuffer().SetDirty(false);
292 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
295 if (GetCaret() && !GetCaret()->IsVisible())
301 // Empty implementation, to prevent flicker
302 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
306 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
310 if (!GetCaret()->IsVisible())
319 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
321 if (GetCaret() && GetCaret()->IsVisible())
328 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
334 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
340 dc
.SetFont(GetFont());
343 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
345 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
347 m_dragStart
= event
.GetLogicalPosition(dc
);
353 bool caretAtLineStart
= false;
355 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
357 // If we're at the start of a line (but not first in para)
358 // then we should keep the caret showing at the start of the line
359 // by showing the m_caretAtLineStart flag.
360 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
361 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
363 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
364 caretAtLineStart
= true;
368 MoveCaret(position
, caretAtLineStart
);
369 SetDefaultStyleToCursorStyle();
376 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
381 if (GetCapture() == this)
384 // See if we clicked on a URL
387 dc
.SetFont(GetFont());
390 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
391 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
393 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
396 if (GetStyle(position
, attr
))
398 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
400 wxString urlTarget
= attr
.GetURL();
401 if (!urlTarget
.IsEmpty())
403 wxMouseEvent
mouseEvent(event
);
405 long startPos
= 0, endPos
= 0;
406 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
409 startPos
= obj
->GetRange().GetStart();
410 endPos
= obj
->GetRange().GetEnd();
413 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
414 InitCommandEvent(urlEvent
);
416 urlEvent
.SetString(urlTarget
);
418 GetEventHandler()->ProcessEvent(urlEvent
);
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
)
504 /// Left-double-click
505 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
507 SelectWord(GetCaretPosition()+1);
512 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
518 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
522 flags
|= wxRICHTEXT_CTRL_DOWN
;
523 if (event
.ShiftDown())
524 flags
|= wxRICHTEXT_SHIFT_DOWN
;
526 flags
|= wxRICHTEXT_ALT_DOWN
;
528 if (event
.GetKeyCode() == WXK_LEFT
||
529 event
.GetKeyCode() == WXK_RIGHT
||
530 event
.GetKeyCode() == WXK_UP
||
531 event
.GetKeyCode() == WXK_DOWN
||
532 event
.GetKeyCode() == WXK_HOME
||
533 event
.GetKeyCode() == WXK_PAGEUP
||
534 event
.GetKeyCode() == WXK_PAGEDOWN
||
535 event
.GetKeyCode() == WXK_END
||
537 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
538 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
539 event
.GetKeyCode() == WXK_NUMPAD_UP
||
540 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
541 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
542 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
543 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
544 event
.GetKeyCode() == WXK_NUMPAD_END
)
546 KeyboardNavigate(event
.GetKeyCode(), flags
);
550 // all the other keys modify the controls contents which shouldn't be
551 // possible if we're read-only
558 if (event
.GetKeyCode() == WXK_RETURN
)
560 BeginBatchUndo(_("Insert Text"));
562 long newPos
= m_caretPosition
;
564 DeleteSelectedContent(& newPos
);
566 if (event
.ShiftDown())
569 text
= wxRichTextLineBreakChar
;
570 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
573 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
576 SetDefaultStyleToCursorStyle();
578 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
580 wxRichTextEvent
cmdEvent(
581 wxEVT_COMMAND_RICHTEXT_RETURN
,
583 cmdEvent
.SetEventObject(this);
584 cmdEvent
.SetFlags(flags
);
585 cmdEvent
.SetPosition(newPos
+1);
587 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
589 // Generate conventional event
590 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
591 InitCommandEvent(textEvent
);
593 GetEventHandler()->ProcessEvent(textEvent
);
597 else if (event
.GetKeyCode() == WXK_BACK
)
599 BeginBatchUndo(_("Delete Text"));
601 // Submit range in character positions, which are greater than caret positions,
602 // so subtract 1 for deleted character and add 1 for conversion to character position.
603 if (m_caretPosition
> -1 && !HasSelection())
605 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
608 DeleteSelectedContent();
612 if (GetLastPosition() == -1)
616 m_caretPosition
= -1;
618 SetDefaultStyleToCursorStyle();
621 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
623 wxRichTextEvent
cmdEvent(
624 wxEVT_COMMAND_RICHTEXT_DELETE
,
626 cmdEvent
.SetEventObject(this);
627 cmdEvent
.SetFlags(flags
);
628 cmdEvent
.SetPosition(m_caretPosition
+1);
629 GetEventHandler()->ProcessEvent(cmdEvent
);
633 else if (event
.GetKeyCode() == WXK_DELETE
)
635 BeginBatchUndo(_("Delete Text"));
637 // Submit range in character positions, which are greater than caret positions,
638 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
640 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
643 DeleteSelectedContent();
647 if (GetLastPosition() == -1)
651 m_caretPosition
= -1;
653 SetDefaultStyleToCursorStyle();
656 wxRichTextEvent
cmdEvent(
657 wxEVT_COMMAND_RICHTEXT_DELETE
,
659 cmdEvent
.SetEventObject(this);
660 cmdEvent
.SetFlags(flags
);
661 cmdEvent
.SetPosition(m_caretPosition
+1);
662 GetEventHandler()->ProcessEvent(cmdEvent
);
668 long keycode
= event
.GetKeyCode();
741 case WXK_NUMPAD_SPACE
:
743 case WXK_NUMPAD_ENTER
:
748 case WXK_NUMPAD_HOME
:
749 case WXK_NUMPAD_LEFT
:
751 case WXK_NUMPAD_RIGHT
:
752 case WXK_NUMPAD_DOWN
:
753 case WXK_NUMPAD_PAGEUP
:
754 case WXK_NUMPAD_PAGEDOWN
:
756 case WXK_NUMPAD_BEGIN
:
757 case WXK_NUMPAD_INSERT
:
758 case WXK_NUMPAD_DELETE
:
759 case WXK_NUMPAD_EQUAL
:
760 case WXK_NUMPAD_MULTIPLY
:
762 case WXK_NUMPAD_SEPARATOR
:
763 case WXK_NUMPAD_SUBTRACT
:
764 case WXK_NUMPAD_DECIMAL
:
765 case WXK_WINDOWS_LEFT
:
773 if (event
.CmdDown() || event
.AltDown())
779 wxRichTextEvent
cmdEvent(
780 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
782 cmdEvent
.SetEventObject(this);
783 cmdEvent
.SetFlags(flags
);
785 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
787 cmdEvent
.SetCharacter((wxChar
) keycode
);
789 cmdEvent
.SetPosition(m_caretPosition
+1);
791 if (keycode
== wxT('\t'))
793 // See if we need to promote or demote the selection or paragraph at the cursor
794 // position, instead of inserting a tab.
795 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
796 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
797 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
799 wxRichTextRange range
;
801 range
= GetSelectionRange();
803 range
= para
->GetRange().FromInternal();
805 int promoteBy
= event
.ShiftDown() ? 1 : -1;
807 PromoteList(promoteBy
, range
, NULL
);
809 GetEventHandler()->ProcessEvent(cmdEvent
);
815 BeginBatchUndo(_("Insert Text"));
817 long newPos
= m_caretPosition
;
818 DeleteSelectedContent(& newPos
);
821 wxString str
= event
.GetUnicodeKey();
823 wxString str
= (wxChar
) event
.GetKeyCode();
825 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
829 SetDefaultStyleToCursorStyle();
830 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
832 GetEventHandler()->ProcessEvent(cmdEvent
);
840 /// Delete content if there is a selection, e.g. when pressing a key.
841 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
845 long pos
= m_selectionRange
.GetStart();
846 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
847 m_selectionRange
.SetRange(-2, -2);
857 /// Keyboard navigation
861 Left: left one character
862 Right: right one character
865 Ctrl-Left: left one word
866 Ctrl-Right: right one word
867 Ctrl-Up: previous paragraph start
868 Ctrl-Down: next start of paragraph
871 Ctrl-Home: start of document
872 Ctrl-End: end of document
874 Page-Down: Down a screen
878 Ctrl-Alt-PgUp: Start of window
879 Ctrl-Alt-PgDn: End of window
880 F8: Start selection mode
881 Esc: End selection mode
883 Adding Shift does the above but starts/extends selection.
888 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
890 bool success
= false;
892 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
894 if (flags
& wxRICHTEXT_CTRL_DOWN
)
895 success
= WordRight(1, flags
);
897 success
= MoveRight(1, flags
);
899 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
901 if (flags
& wxRICHTEXT_CTRL_DOWN
)
902 success
= WordLeft(1, flags
);
904 success
= MoveLeft(1, flags
);
906 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
908 if (flags
& wxRICHTEXT_CTRL_DOWN
)
909 success
= MoveToParagraphStart(flags
);
911 success
= MoveUp(1, flags
);
913 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
915 if (flags
& wxRICHTEXT_CTRL_DOWN
)
916 success
= MoveToParagraphEnd(flags
);
918 success
= MoveDown(1, flags
);
920 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
922 success
= PageUp(1, flags
);
924 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
926 success
= PageDown(1, flags
);
928 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
930 if (flags
& wxRICHTEXT_CTRL_DOWN
)
931 success
= MoveHome(flags
);
933 success
= MoveToLineStart(flags
);
935 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
937 if (flags
& wxRICHTEXT_CTRL_DOWN
)
938 success
= MoveEnd(flags
);
940 success
= MoveToLineEnd(flags
);
945 ScrollIntoView(m_caretPosition
, keyCode
);
946 SetDefaultStyleToCursorStyle();
952 /// Extend the selection. Selections are in caret positions.
953 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
955 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
957 // If not currently selecting, start selecting
958 if (m_selectionRange
.GetStart() == -2)
960 m_selectionAnchor
= oldPos
;
963 m_selectionRange
.SetRange(newPos
+1, oldPos
);
965 m_selectionRange
.SetRange(oldPos
+1, newPos
);
969 // Always ensure that the selection range start is greater than
971 if (newPos
> m_selectionAnchor
)
972 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
974 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
977 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
979 wxLogDebug(wxT("Strange selection range"));
988 /// Scroll into view, returning true if we scrolled.
989 /// This takes a _caret_ position.
990 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
992 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
998 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1000 int startXUnits
, startYUnits
;
1001 GetViewStart(& startXUnits
, & startYUnits
);
1002 int startY
= startYUnits
* ppuY
;
1005 GetVirtualSize(& sx
, & sy
);
1011 wxRect rect
= line
->GetRect();
1013 bool scrolled
= false;
1015 wxSize clientSize
= GetClientSize();
1018 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1019 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
1020 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1021 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1023 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1025 // Make it scroll so this item is at the bottom
1027 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1028 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1030 // If we're still off the screen, scroll another line down
1031 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1034 if (startYUnits
!= yUnits
)
1036 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1040 else if (rect
.y
< startY
)
1042 // Make it scroll so this item is at the top
1045 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1047 if (startYUnits
!= yUnits
)
1049 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1055 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1056 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1057 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1058 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1060 if (rect
.y
< startY
)
1062 // Make it scroll so this item is at the top
1065 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1067 if (startYUnits
!= yUnits
)
1069 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1073 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1075 // Make it scroll so this item is at the bottom
1077 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1078 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1080 // If we're still off the screen, scroll another line down
1081 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1084 if (startYUnits
!= yUnits
)
1086 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1096 /// Is the given position visible on the screen?
1097 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1099 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1105 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1108 GetViewStart(& startX
, & startY
);
1110 startY
= startY
* ppuY
;
1113 GetVirtualSize(& sx
, & sy
);
1118 wxRect rect
= line
->GetRect();
1120 wxSize clientSize
= GetClientSize();
1122 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1125 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1127 m_caretPosition
= position
;
1128 m_caretAtLineStart
= showAtLineStart
;
1131 /// Move caret one visual step forward: this may mean setting a flag
1132 /// and keeping the same position if we're going from the end of one line
1133 /// to the start of the next, which may be the exact same caret position.
1134 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1136 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1138 // Only do the check if we're not at the end of the paragraph (where things work OK
1140 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1142 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1146 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1148 // We're at the end of a line. See whether we need to
1149 // stay at the same actual caret position but change visual
1150 // position, or not.
1151 if (oldPosition
== lineRange
.GetEnd())
1153 if (m_caretAtLineStart
)
1155 // We're already at the start of the line, so actually move on now.
1156 m_caretPosition
= oldPosition
+ 1;
1157 m_caretAtLineStart
= false;
1161 // We're showing at the end of the line, so keep to
1162 // the same position but indicate that we're to show
1163 // at the start of the next line.
1164 m_caretPosition
= oldPosition
;
1165 m_caretAtLineStart
= true;
1167 SetDefaultStyleToCursorStyle();
1173 SetDefaultStyleToCursorStyle();
1176 /// Move caret one visual step backward: this may mean setting a flag
1177 /// and keeping the same position if we're going from the end of one line
1178 /// to the start of the next, which may be the exact same caret position.
1179 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1181 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1183 // Only do the check if we're not at the start of the paragraph (where things work OK
1185 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1187 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1191 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1193 // We're at the start of a line. See whether we need to
1194 // stay at the same actual caret position but change visual
1195 // position, or not.
1196 if (oldPosition
== lineRange
.GetStart())
1198 m_caretPosition
= oldPosition
-1;
1199 m_caretAtLineStart
= true;
1202 else if (oldPosition
== lineRange
.GetEnd())
1204 if (m_caretAtLineStart
)
1206 // We're at the start of the line, so keep the same caret position
1207 // but clear the start-of-line flag.
1208 m_caretPosition
= oldPosition
;
1209 m_caretAtLineStart
= false;
1213 // We're showing at the end of the line, so go back
1214 // to the previous character position.
1215 m_caretPosition
= oldPosition
- 1;
1217 SetDefaultStyleToCursorStyle();
1223 SetDefaultStyleToCursorStyle();
1227 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1229 long endPos
= GetBuffer().GetRange().GetEnd();
1231 if (m_caretPosition
+ noPositions
< endPos
)
1233 long oldPos
= m_caretPosition
;
1234 long newPos
= m_caretPosition
+ noPositions
;
1236 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1240 // Determine by looking at oldPos and m_caretPosition whether
1241 // we moved from the end of a line to the start of the next line, in which case
1242 // we want to adjust the caret position such that it is positioned at the
1243 // start of the next line, rather than jumping past the first character of the
1245 if (noPositions
== 1 && !extendSel
)
1246 MoveCaretForward(oldPos
);
1248 SetCaretPosition(newPos
);
1251 SetDefaultStyleToCursorStyle();
1262 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1266 if (m_caretPosition
> startPos
- noPositions
+ 1)
1268 long oldPos
= m_caretPosition
;
1269 long newPos
= m_caretPosition
- noPositions
;
1270 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1274 if (noPositions
== 1 && !extendSel
)
1275 MoveCaretBack(oldPos
);
1277 SetCaretPosition(newPos
);
1280 SetDefaultStyleToCursorStyle();
1291 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1293 return MoveDown(- noLines
, flags
);
1297 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1302 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1303 wxPoint pt
= GetCaret()->GetPosition();
1304 long newLine
= lineNumber
+ noLines
;
1306 if (lineNumber
!= -1)
1310 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1312 if (newLine
> lastLine
)
1322 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1325 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1331 wxClientDC
dc(this);
1333 dc
.SetFont(GetFont());
1335 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1337 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1339 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1340 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1341 // so we view the caret at the start of the line.
1342 bool caretLineStart
= false;
1343 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1345 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1346 wxRichTextRange lineRange
;
1348 lineRange
= thisLine
->GetAbsoluteRange();
1350 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1353 caretLineStart
= true;
1357 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1358 if (para
&& para
->GetRange().GetStart() == newPos
)
1363 long newSelEnd
= newPos
;
1365 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1369 SetCaretPosition(newPos
, caretLineStart
);
1371 SetDefaultStyleToCursorStyle();
1381 /// Move to the end of the paragraph
1382 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1384 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1387 long newPos
= para
->GetRange().GetEnd() - 1;
1388 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1392 SetCaretPosition(newPos
);
1394 SetDefaultStyleToCursorStyle();
1404 /// Move to the start of the paragraph
1405 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1407 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1410 long newPos
= para
->GetRange().GetStart() - 1;
1411 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1415 SetCaretPosition(newPos
);
1417 SetDefaultStyleToCursorStyle();
1427 /// Move to the end of the line
1428 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1430 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1434 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1435 long newPos
= lineRange
.GetEnd();
1436 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1440 SetCaretPosition(newPos
);
1442 SetDefaultStyleToCursorStyle();
1452 /// Move to the start of the line
1453 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1455 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1458 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1459 long newPos
= lineRange
.GetStart()-1;
1461 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1465 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1467 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1469 SetDefaultStyleToCursorStyle();
1479 /// Move to the start of the buffer
1480 bool wxRichTextCtrl::MoveHome(int flags
)
1482 if (m_caretPosition
!= -1)
1484 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1488 SetCaretPosition(-1);
1490 SetDefaultStyleToCursorStyle();
1500 /// Move to the end of the buffer
1501 bool wxRichTextCtrl::MoveEnd(int flags
)
1503 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1505 if (m_caretPosition
!= endPos
)
1507 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1511 SetCaretPosition(endPos
);
1513 SetDefaultStyleToCursorStyle();
1523 /// Move noPages pages up
1524 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1526 return PageDown(- noPages
, flags
);
1529 /// Move noPages pages down
1530 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1532 // Calculate which line occurs noPages * screen height further down.
1533 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1536 wxSize clientSize
= GetClientSize();
1537 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1539 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1542 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1543 long pos
= lineRange
.GetStart()-1;
1544 if (pos
!= m_caretPosition
)
1546 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1548 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1552 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1554 SetDefaultStyleToCursorStyle();
1566 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1568 return str
== wxT(" ") || str
== wxT("\t");
1571 // Finds the caret position for the next word
1572 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1574 long endPos
= GetBuffer().GetRange().GetEnd();
1578 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1580 // First skip current text to space
1581 while (i
< endPos
&& i
> -1)
1583 // i is in character, not caret positions
1584 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1585 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1586 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1590 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
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()))
1603 return wxMax(-1, i
);
1605 if (text
.empty()) // End of paragraph, or maybe an image
1606 return wxMax(-1, i
- 1);
1607 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1611 // Convert to caret position
1612 return wxMax(-1, i
- 1);
1621 long i
= m_caretPosition
;
1623 // First skip white space
1624 while (i
< endPos
&& i
> -1)
1626 // i is in character, not caret positions
1627 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1628 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1630 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1632 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1637 // Next skip current text to space
1638 while (i
< endPos
&& i
> -1)
1640 // i is in character, not caret positions
1641 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1642 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1643 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1646 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1659 /// Move n words left
1660 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1662 long pos
= FindNextWordPosition(-1);
1663 if (pos
!= m_caretPosition
)
1665 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1667 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1671 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1673 SetDefaultStyleToCursorStyle();
1683 /// Move n words right
1684 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1686 long pos
= FindNextWordPosition(1);
1687 if (pos
!= m_caretPosition
)
1689 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1691 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1695 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1697 SetDefaultStyleToCursorStyle();
1708 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1710 // Only do sizing optimization for large buffers
1711 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1713 m_fullLayoutRequired
= true;
1714 m_fullLayoutTime
= wxGetLocalTimeMillis();
1715 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1716 LayoutContent(true /* onlyVisibleRect */);
1719 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1721 #if wxRICHTEXT_BUFFERED_PAINTING
1729 /// Idle-time processing
1730 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1732 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1734 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1736 m_fullLayoutRequired
= false;
1737 m_fullLayoutTime
= 0;
1738 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1739 ShowPosition(m_fullLayoutSavedPosition
);
1743 if (m_caretPositionForDefaultStyle
!= -2)
1745 // If the caret position has changed, no longer reflect the default style
1747 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1748 m_caretPositionForDefaultStyle
= -2;
1755 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1761 /// Set up scrollbars, e.g. after a resize
1762 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1767 if (GetBuffer().IsEmpty())
1769 SetScrollbars(0, 0, 0, 0, 0, 0);
1773 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1774 // of pixels. See e.g. wxVScrolledWindow for ideas.
1775 int pixelsPerUnit
= 5;
1776 wxSize clientSize
= GetClientSize();
1778 int maxHeight
= GetBuffer().GetCachedSize().y
;
1780 // Round up so we have at least maxHeight pixels
1781 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1783 int startX
= 0, startY
= 0;
1785 GetViewStart(& startX
, & startY
);
1787 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1788 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1790 // Move to previous scroll position if
1792 SetScrollbars(0, pixelsPerUnit
,
1794 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1797 /// Paint the background
1798 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1800 wxColour backgroundColour
= GetBackgroundColour();
1801 if (!backgroundColour
.Ok())
1802 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1804 // Clear the background
1805 dc
.SetBrush(wxBrush(backgroundColour
));
1806 dc
.SetPen(*wxTRANSPARENT_PEN
);
1807 wxRect
windowRect(GetClientSize());
1808 windowRect
.x
-= 2; windowRect
.y
-= 2;
1809 windowRect
.width
+= 4; windowRect
.height
+= 4;
1811 // We need to shift the rectangle to take into account
1812 // scrolling. Converting device to logical coordinates.
1813 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1814 dc
.DrawRectangle(windowRect
);
1817 #if wxRICHTEXT_BUFFERED_PAINTING
1818 /// Recreate buffer bitmap if necessary
1819 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1822 if (sz
== wxDefaultSize
)
1823 sz
= GetClientSize();
1825 if (sz
.x
< 1 || sz
.y
< 1)
1828 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1829 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1830 return m_bufferBitmap
.Ok();
1834 // ----------------------------------------------------------------------------
1835 // file IO functions
1836 // ----------------------------------------------------------------------------
1838 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1840 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1842 m_filename
= filename
;
1845 SetInsertionPoint(0);
1848 SetupScrollbars(true);
1850 SendTextUpdatedEvent();
1856 wxLogError(_("File couldn't be loaded."));
1862 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1864 if (GetBuffer().SaveFile(filename
, fileType
))
1866 m_filename
= filename
;
1873 wxLogError(_("The text couldn't be saved."));
1878 // ----------------------------------------------------------------------------
1879 // wxRichTextCtrl specific functionality
1880 // ----------------------------------------------------------------------------
1882 /// Add a new paragraph of text to the end of the buffer
1883 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1885 return GetBuffer().AddParagraph(text
);
1889 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1891 return GetBuffer().AddImage(image
);
1894 // ----------------------------------------------------------------------------
1895 // selection and ranges
1896 // ----------------------------------------------------------------------------
1898 void wxRichTextCtrl::SelectAll()
1900 SetSelection(0, GetLastPosition()+1);
1901 m_selectionAnchor
= -1;
1905 void wxRichTextCtrl::SelectNone()
1907 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1908 SetSelection(-2, -2);
1909 m_selectionAnchor
= -2;
1912 static bool wxIsWordDelimiter(const wxString
& text
)
1914 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1917 /// Select the word at the given character position
1918 bool wxRichTextCtrl::SelectWord(long position
)
1920 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1923 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1927 long positionStart
= position
;
1928 long positionEnd
= position
;
1930 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1932 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1933 if (wxIsWordDelimiter(text
))
1939 if (positionStart
< para
->GetRange().GetStart())
1940 positionStart
= para
->GetRange().GetStart();
1942 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1944 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1945 if (wxIsWordDelimiter(text
))
1951 if (positionEnd
>= para
->GetRange().GetEnd())
1952 positionEnd
= para
->GetRange().GetEnd();
1954 SetSelection(positionStart
, positionEnd
+1);
1956 if (positionStart
>= 0)
1958 MoveCaret(positionStart
-1, true);
1959 SetDefaultStyleToCursorStyle();
1965 wxString
wxRichTextCtrl::GetStringSelection() const
1968 GetSelection(&from
, &to
);
1970 return GetRange(from
, to
);
1973 // ----------------------------------------------------------------------------
1975 // ----------------------------------------------------------------------------
1977 wxTextCtrlHitTestResult
1978 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1980 // implement in terms of the other overload as the native ports typically
1981 // can get the position and not (x, y) pair directly (although wxUniv
1982 // directly gets x and y -- and so overrides this method as well)
1984 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1986 if ( rc
!= wxTE_HT_UNKNOWN
)
1988 PositionToXY(pos
, x
, y
);
1994 wxTextCtrlHitTestResult
1995 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1998 wxClientDC
dc((wxRichTextCtrl
*) this);
1999 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2001 // Buffer uses logical position (relative to start of buffer)
2003 wxPoint pt2
= GetLogicalPoint(pt
);
2005 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2007 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2008 return wxTE_HT_BEFORE
;
2009 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2010 return wxTE_HT_BEYOND
;
2011 else if (hit
& wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
)
2012 return wxTE_HT_ON_TEXT
;
2014 return wxTE_HT_UNKNOWN
;
2017 // ----------------------------------------------------------------------------
2018 // set/get the controls text
2019 // ----------------------------------------------------------------------------
2021 wxString
wxRichTextCtrl::GetValue() const
2023 return GetBuffer().GetText();
2026 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2028 // Public API for range is different from internals
2029 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2032 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2036 // if the text is long enough, it's faster to just set it instead of first
2037 // comparing it with the old one (chances are that it will be different
2038 // anyhow, this comparison is there to avoid flicker for small single-line
2039 // edit controls mostly)
2040 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
2044 // for compatibility, don't move the cursor when doing SetValue()
2045 SetInsertionPoint(0);
2049 if ( flags
& SetValue_SendEvent
)
2051 // still send an event for consistency
2052 SendTextUpdatedEvent();
2056 // we should reset the modified flag even if the value didn't really change
2058 // mark the control as being not dirty - we changed its text, not the
2063 void wxRichTextCtrl::WriteText(const wxString
& value
)
2068 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2070 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2072 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2074 if ( flags
& SetValue_SendEvent
)
2075 SendTextUpdatedEvent();
2078 void wxRichTextCtrl::AppendText(const wxString
& text
)
2080 SetInsertionPointEnd();
2085 /// Write an image at the current insertion point
2086 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2088 wxRichTextImageBlock imageBlock
;
2090 wxImage image2
= image
;
2091 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2092 return WriteImage(imageBlock
);
2097 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2099 wxRichTextImageBlock imageBlock
;
2102 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2103 return WriteImage(imageBlock
);
2108 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2110 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2113 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2117 wxRichTextImageBlock imageBlock
;
2119 wxImage image
= bitmap
.ConvertToImage();
2120 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2121 return WriteImage(imageBlock
);
2127 /// Insert a newline (actually paragraph) at the current insertion point.
2128 bool wxRichTextCtrl::Newline()
2130 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2133 /// Insert a line break at the current insertion point.
2134 bool wxRichTextCtrl::LineBreak()
2137 text
= wxRichTextLineBreakChar
;
2138 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2141 // ----------------------------------------------------------------------------
2142 // Clipboard operations
2143 // ----------------------------------------------------------------------------
2145 void wxRichTextCtrl::Copy()
2149 wxRichTextRange range
= GetInternalSelectionRange();
2150 GetBuffer().CopyToClipboard(range
);
2154 void wxRichTextCtrl::Cut()
2158 wxRichTextRange range
= GetInternalSelectionRange();
2159 GetBuffer().CopyToClipboard(range
);
2161 DeleteSelectedContent();
2167 void wxRichTextCtrl::Paste()
2171 BeginBatchUndo(_("Paste"));
2173 long newPos
= m_caretPosition
;
2174 DeleteSelectedContent(& newPos
);
2176 GetBuffer().PasteFromClipboard(newPos
);
2182 void wxRichTextCtrl::DeleteSelection()
2184 if (CanDeleteSelection())
2186 DeleteSelectedContent();
2190 bool wxRichTextCtrl::HasSelection() const
2192 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2195 bool wxRichTextCtrl::CanCopy() const
2197 // Can copy if there's a selection
2198 return HasSelection();
2201 bool wxRichTextCtrl::CanCut() const
2203 return HasSelection() && IsEditable();
2206 bool wxRichTextCtrl::CanPaste() const
2208 if ( !IsEditable() )
2211 return GetBuffer().CanPasteFromClipboard();
2214 bool wxRichTextCtrl::CanDeleteSelection() const
2216 return HasSelection() && IsEditable();
2220 // ----------------------------------------------------------------------------
2222 // ----------------------------------------------------------------------------
2224 void wxRichTextCtrl::SetEditable(bool editable
)
2226 m_editable
= editable
;
2229 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2233 m_caretPosition
= pos
- 1;
2236 void wxRichTextCtrl::SetInsertionPointEnd()
2238 long pos
= GetLastPosition();
2239 SetInsertionPoint(pos
);
2242 long wxRichTextCtrl::GetInsertionPoint() const
2244 return m_caretPosition
+1;
2247 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2249 return GetBuffer().GetRange().GetEnd();
2252 // If the return values from and to are the same, there is no
2254 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2256 *from
= m_selectionRange
.GetStart();
2257 *to
= m_selectionRange
.GetEnd();
2258 if ((*to
) != -1 && (*to
) != -2)
2262 bool wxRichTextCtrl::IsEditable() const
2267 // ----------------------------------------------------------------------------
2269 // ----------------------------------------------------------------------------
2271 void wxRichTextCtrl::SetSelection(long from
, long to
)
2273 // if from and to are both -1, it means (in wxWidgets) that all text should
2275 if ( (from
== -1) && (to
== -1) )
2278 to
= GetLastPosition()+1;
2281 DoSetSelection(from
, to
);
2284 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2286 m_selectionAnchor
= from
;
2287 m_selectionRange
.SetRange(from
, to
-1);
2289 m_caretPosition
= from
-1;
2295 // ----------------------------------------------------------------------------
2297 // ----------------------------------------------------------------------------
2299 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2300 const wxString
& value
)
2302 BeginBatchUndo(_("Replace"));
2304 DeleteSelectedContent();
2306 DoWriteText(value
, SetValue_SelectionOnly
);
2311 void wxRichTextCtrl::Remove(long from
, long to
)
2315 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2322 bool wxRichTextCtrl::IsModified() const
2324 return m_buffer
.IsModified();
2327 void wxRichTextCtrl::MarkDirty()
2329 m_buffer
.Modify(true);
2332 void wxRichTextCtrl::DiscardEdits()
2334 m_caretPositionForDefaultStyle
= -2;
2335 m_buffer
.Modify(false);
2336 m_buffer
.GetCommandProcessor()->ClearCommands();
2339 int wxRichTextCtrl::GetNumberOfLines() const
2341 return GetBuffer().GetParagraphCount();
2344 // ----------------------------------------------------------------------------
2345 // Positions <-> coords
2346 // ----------------------------------------------------------------------------
2348 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2350 return GetBuffer().XYToPosition(x
, y
);
2353 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2355 return GetBuffer().PositionToXY(pos
, x
, y
);
2358 // ----------------------------------------------------------------------------
2360 // ----------------------------------------------------------------------------
2362 void wxRichTextCtrl::ShowPosition(long pos
)
2364 if (!IsPositionVisible(pos
))
2365 ScrollIntoView(pos
-1, WXK_DOWN
);
2368 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2370 return GetBuffer().GetParagraphLength(lineNo
);
2373 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2375 return GetBuffer().GetParagraphText(lineNo
);
2378 // ----------------------------------------------------------------------------
2380 // ----------------------------------------------------------------------------
2382 void wxRichTextCtrl::Undo()
2386 GetCommandProcessor()->Undo();
2390 void wxRichTextCtrl::Redo()
2394 GetCommandProcessor()->Redo();
2398 bool wxRichTextCtrl::CanUndo() const
2400 return GetCommandProcessor()->CanUndo();
2403 bool wxRichTextCtrl::CanRedo() const
2405 return GetCommandProcessor()->CanRedo();
2408 // ----------------------------------------------------------------------------
2409 // implementation details
2410 // ----------------------------------------------------------------------------
2412 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2414 SetValue(event
.GetString());
2415 GetEventHandler()->ProcessEvent(event
);
2418 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2420 // By default, load the first file into the text window.
2421 if (event
.GetNumberOfFiles() > 0)
2423 LoadFile(event
.GetFiles()[0]);
2427 wxSize
wxRichTextCtrl::DoGetBestSize() const
2429 return wxSize(10, 10);
2432 // ----------------------------------------------------------------------------
2433 // standard handlers for standard edit menu events
2434 // ----------------------------------------------------------------------------
2436 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2441 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2446 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2451 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2456 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2461 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2466 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2468 event
.Enable( CanCut() );
2471 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2473 event
.Enable( CanCopy() );
2476 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2478 event
.Enable( CanDeleteSelection() );
2481 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2483 event
.Enable( CanPaste() );
2486 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2488 event
.Enable( CanUndo() );
2489 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2492 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2494 event
.Enable( CanRedo() );
2495 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2498 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2503 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2505 event
.Enable(GetLastPosition() > 0);
2508 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2512 m_contextMenu
= new wxMenu
;
2513 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2514 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2515 m_contextMenu
->AppendSeparator();
2516 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2517 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2518 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2519 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2520 m_contextMenu
->AppendSeparator();
2521 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2523 PopupMenu(m_contextMenu
);
2527 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2529 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2532 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2534 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2537 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2539 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2542 // extended style setting operation with flags including:
2543 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2544 // see richtextbuffer.h for more details.
2545 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2547 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2550 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2552 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2555 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2557 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2560 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2562 return GetBuffer().SetDefaultStyle(style
);
2565 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2567 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2570 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2572 return GetBuffer().GetDefaultStyle();
2575 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2577 return GetBuffer().GetDefaultStyle();
2580 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2582 wxTextAttrEx
attr(style
);
2583 if (GetBuffer().GetStyle(position
, attr
))
2592 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2594 return GetBuffer().GetStyle(position
, style
);
2597 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2599 return GetBuffer().GetStyle(position
, style
);
2602 // get the common set of styles for the range
2603 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2605 wxTextAttrEx styleEx
;
2606 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2615 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2617 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2620 /// Get the content (uncombined) attributes for this position.
2622 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2624 wxTextAttrEx
attr(style
);
2625 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2634 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2636 return GetBuffer().GetUncombinedStyle(position
, style
);
2639 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2641 return GetBuffer().GetUncombinedStyle(position
, style
);
2644 /// Set font, and also the buffer attributes
2645 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2647 wxControl::SetFont(font
);
2649 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2651 GetBuffer().SetBasicStyle(attr
);
2653 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2659 /// Transform logical to physical
2660 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2663 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2668 /// Transform physical to logical
2669 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2672 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2677 /// Position the caret
2678 void wxRichTextCtrl::PositionCaret()
2683 //wxLogDebug(wxT("PositionCaret"));
2686 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2688 wxPoint originalPt
= caretRect
.GetPosition();
2689 wxPoint pt
= GetPhysicalPoint(originalPt
);
2690 if (GetCaret()->GetPosition() != pt
)
2692 GetCaret()->Move(pt
);
2693 GetCaret()->SetSize(caretRect
.GetSize());
2698 /// Get the caret height and position for the given character position
2699 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2701 wxClientDC
dc(this);
2702 dc
.SetFont(GetFont());
2709 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2711 // Caret height can't be zero
2713 height
= dc
.GetCharHeight();
2715 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2722 /// Gets the line for the visible caret position. If the caret is
2723 /// shown at the very end of the line, it means the next character is actually
2724 /// on the following line. So let's get the line we're expecting to find
2725 /// if this is the case.
2726 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2728 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2729 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2732 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2733 if (caretPosition
== lineRange
.GetStart()-1 &&
2734 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2736 if (!m_caretAtLineStart
)
2737 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2744 /// Move the caret to the given character position
2745 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2747 if (GetBuffer().GetDirty())
2750 if (pos
<= GetBuffer().GetRange().GetEnd())
2752 SetCaretPosition(pos
, showAtLineStart
);
2762 /// Layout the buffer: which we must do before certain operations, such as
2763 /// setting the caret position.
2764 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2766 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2768 wxRect
availableSpace(GetClientSize());
2769 if (availableSpace
.width
== 0)
2770 availableSpace
.width
= 10;
2771 if (availableSpace
.height
== 0)
2772 availableSpace
.height
= 10;
2774 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2775 if (onlyVisibleRect
)
2777 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2778 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2781 wxClientDC
dc(this);
2782 dc
.SetFont(GetFont());
2786 GetBuffer().Defragment();
2787 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2788 GetBuffer().Layout(dc
, availableSpace
, flags
);
2789 GetBuffer().SetDirty(false);
2798 /// Is all of the selection bold?
2799 bool wxRichTextCtrl::IsSelectionBold()
2803 wxRichTextAttr attr
;
2804 wxRichTextRange range
= GetSelectionRange();
2805 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2806 attr
.SetFontWeight(wxBOLD
);
2808 return HasCharacterAttributes(range
, attr
);
2812 // If no selection, then we need to combine current style with default style
2813 // to see what the effect would be if we started typing.
2814 wxRichTextAttr attr
;
2815 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2817 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2818 if (GetStyle(pos
, attr
))
2820 if (IsDefaultStyleShowing())
2821 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2822 return attr
.GetFontWeight() == wxBOLD
;
2828 /// Is all of the selection italics?
2829 bool wxRichTextCtrl::IsSelectionItalics()
2833 wxRichTextRange range
= GetSelectionRange();
2834 wxRichTextAttr attr
;
2835 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2836 attr
.SetFontStyle(wxITALIC
);
2838 return HasCharacterAttributes(range
, attr
);
2842 // If no selection, then we need to combine current style with default style
2843 // to see what the effect would be if we started typing.
2844 wxRichTextAttr attr
;
2845 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2847 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2848 if (GetStyle(pos
, attr
))
2850 if (IsDefaultStyleShowing())
2851 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2852 return attr
.GetFontStyle() == wxITALIC
;
2858 /// Is all of the selection underlined?
2859 bool wxRichTextCtrl::IsSelectionUnderlined()
2863 wxRichTextRange range
= GetSelectionRange();
2864 wxRichTextAttr attr
;
2865 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2866 attr
.SetFontUnderlined(true);
2868 return HasCharacterAttributes(range
, attr
);
2872 // If no selection, then we need to combine current style with default style
2873 // to see what the effect would be if we started typing.
2874 wxRichTextAttr attr
;
2875 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2876 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2878 if (GetStyle(pos
, attr
))
2880 if (IsDefaultStyleShowing())
2881 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2882 return attr
.GetFontUnderlined();
2888 /// Apply bold to the selection
2889 bool wxRichTextCtrl::ApplyBoldToSelection()
2891 wxRichTextAttr attr
;
2892 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2893 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2896 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2898 SetAndShowDefaultStyle(attr
);
2902 /// Apply italic to the selection
2903 bool wxRichTextCtrl::ApplyItalicToSelection()
2905 wxRichTextAttr attr
;
2906 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2907 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2910 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2912 SetAndShowDefaultStyle(attr
);
2916 /// Apply underline to the selection
2917 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2919 wxRichTextAttr attr
;
2920 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2921 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2924 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2926 SetAndShowDefaultStyle(attr
);
2930 /// Is all of the selection aligned according to the specified flag?
2931 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2933 wxRichTextRange range
;
2935 range
= GetSelectionRange();
2937 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2939 wxRichTextAttr attr
;
2940 attr
.SetAlignment(alignment
);
2942 return HasParagraphAttributes(range
, attr
);
2945 /// Apply alignment to the selection
2946 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2948 wxRichTextAttr attr
;
2949 attr
.SetAlignment(alignment
);
2951 return SetStyle(GetSelectionRange(), attr
);
2954 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2956 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2961 /// Apply a named style to the selection
2962 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2964 // Flags are defined within each definition, so only certain
2965 // attributes are applied.
2966 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2968 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2970 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2972 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2974 wxRichTextRange range
;
2977 range
= GetSelectionRange();
2980 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2981 range
= wxRichTextRange(pos
, pos
+1);
2984 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2987 // Make sure the attr has the style name
2988 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2990 attr
.SetParagraphStyleName(def
->GetName());
2992 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2993 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2994 // to change its style independently.
2995 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2998 attr
.SetCharacterStyleName(def
->GetName());
3001 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3004 SetAndShowDefaultStyle(attr
);
3009 /// Apply the style sheet to the buffer, for example if the styles have changed.
3010 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3013 styleSheet
= GetBuffer().GetStyleSheet();
3017 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3019 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3027 /// Sets the default style to the style under the cursor
3028 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3031 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3033 // If at the start of a paragraph, use the next position.
3034 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3036 if (GetUncombinedStyle(pos
, attr
))
3038 SetDefaultStyle(attr
);
3045 /// Returns the first visible position in the current view
3046 long wxRichTextCtrl::GetFirstVisiblePosition() const
3048 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3050 return line
->GetAbsoluteRange().GetStart();
3055 /// Get the first visible point in the window
3056 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3059 int startXUnits
, startYUnits
;
3061 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3062 GetViewStart(& startXUnits
, & startYUnits
);
3064 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3067 /// The adjusted caret position is the character position adjusted to take
3068 /// into account whether we're at the start of a paragraph, in which case
3069 /// style information should be taken from the next position, not current one.
3070 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3072 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3074 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3079 /// Get/set the selection range in character positions. -1, -1 means no selection.
3080 /// The range is in API convention, i.e. a single character selection is denoted
3082 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3084 wxRichTextRange range
= GetInternalSelectionRange();
3085 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3086 range
.SetEnd(range
.GetEnd() + 1);
3090 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3092 wxRichTextRange
range1(range
);
3093 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3094 range1
.SetEnd(range1
.GetEnd() - 1);
3096 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3098 SetInternalSelectionRange(range1
);
3102 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3104 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3107 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3109 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3112 /// Clear list for given range
3113 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3115 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3118 /// Number/renumber any list elements in the given range
3119 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3121 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3124 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3126 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3129 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3130 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3132 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3135 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3137 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3140 /// Deletes the content in the given range
3141 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3143 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3146 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3148 if (sm_availableFontNames
.GetCount() == 0)
3150 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3151 sm_availableFontNames
.Sort();
3153 return sm_availableFontNames
;
3156 void wxRichTextCtrl::ClearAvailableFontNames()
3158 sm_availableFontNames
.Clear();