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
);
784 cmdEvent
.SetCharacter((wxChar
) keycode
);
785 cmdEvent
.SetPosition(m_caretPosition
+1);
787 if (keycode
== wxT('\t'))
789 // See if we need to promote or demote the selection or paragraph at the cursor
790 // position, instead of inserting a tab.
791 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
792 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
793 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
795 wxRichTextRange range
;
797 range
= GetSelectionRange();
799 range
= para
->GetRange().FromInternal();
801 int promoteBy
= event
.ShiftDown() ? 1 : -1;
803 PromoteList(promoteBy
, range
, NULL
);
805 GetEventHandler()->ProcessEvent(cmdEvent
);
811 BeginBatchUndo(_("Insert Text"));
813 long newPos
= m_caretPosition
;
814 DeleteSelectedContent(& newPos
);
816 wxString str
= (wxChar
) event
.GetKeyCode();
817 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
821 SetDefaultStyleToCursorStyle();
822 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
824 GetEventHandler()->ProcessEvent(cmdEvent
);
832 /// Delete content if there is a selection, e.g. when pressing a key.
833 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
837 long pos
= m_selectionRange
.GetStart();
838 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
839 m_selectionRange
.SetRange(-2, -2);
849 /// Keyboard navigation
853 Left: left one character
854 Right: right one character
857 Ctrl-Left: left one word
858 Ctrl-Right: right one word
859 Ctrl-Up: previous paragraph start
860 Ctrl-Down: next start of paragraph
863 Ctrl-Home: start of document
864 Ctrl-End: end of document
866 Page-Down: Down a screen
870 Ctrl-Alt-PgUp: Start of window
871 Ctrl-Alt-PgDn: End of window
872 F8: Start selection mode
873 Esc: End selection mode
875 Adding Shift does the above but starts/extends selection.
880 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
882 bool success
= false;
884 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
886 if (flags
& wxRICHTEXT_CTRL_DOWN
)
887 success
= WordRight(1, flags
);
889 success
= MoveRight(1, flags
);
891 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
893 if (flags
& wxRICHTEXT_CTRL_DOWN
)
894 success
= WordLeft(1, flags
);
896 success
= MoveLeft(1, flags
);
898 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
900 if (flags
& wxRICHTEXT_CTRL_DOWN
)
901 success
= MoveToParagraphStart(flags
);
903 success
= MoveUp(1, flags
);
905 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
907 if (flags
& wxRICHTEXT_CTRL_DOWN
)
908 success
= MoveToParagraphEnd(flags
);
910 success
= MoveDown(1, flags
);
912 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
914 success
= PageUp(1, flags
);
916 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
918 success
= PageDown(1, flags
);
920 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
922 if (flags
& wxRICHTEXT_CTRL_DOWN
)
923 success
= MoveHome(flags
);
925 success
= MoveToLineStart(flags
);
927 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
929 if (flags
& wxRICHTEXT_CTRL_DOWN
)
930 success
= MoveEnd(flags
);
932 success
= MoveToLineEnd(flags
);
937 ScrollIntoView(m_caretPosition
, keyCode
);
938 SetDefaultStyleToCursorStyle();
944 /// Extend the selection. Selections are in caret positions.
945 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
947 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
949 // If not currently selecting, start selecting
950 if (m_selectionRange
.GetStart() == -2)
952 m_selectionAnchor
= oldPos
;
955 m_selectionRange
.SetRange(newPos
+1, oldPos
);
957 m_selectionRange
.SetRange(oldPos
+1, newPos
);
961 // Always ensure that the selection range start is greater than
963 if (newPos
> m_selectionAnchor
)
964 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
966 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
969 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
971 wxLogDebug(wxT("Strange selection range"));
980 /// Scroll into view, returning true if we scrolled.
981 /// This takes a _caret_ position.
982 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
984 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
990 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
992 int startXUnits
, startYUnits
;
993 GetViewStart(& startXUnits
, & startYUnits
);
994 int startY
= startYUnits
* ppuY
;
997 GetVirtualSize(& sx
, & sy
);
1003 wxRect rect
= line
->GetRect();
1005 bool scrolled
= false;
1007 wxSize clientSize
= GetClientSize();
1010 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1011 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
1012 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1013 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1015 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1017 // Make it scroll so this item is at the bottom
1019 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1020 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1022 // If we're still off the screen, scroll another line down
1023 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1026 if (startYUnits
!= yUnits
)
1028 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1032 else if (rect
.y
< startY
)
1034 // Make it scroll so this item is at the top
1037 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1039 if (startYUnits
!= yUnits
)
1041 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1047 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1048 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1049 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1050 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1052 if (rect
.y
< startY
)
1054 // Make it scroll so this item is at the top
1057 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1059 if (startYUnits
!= yUnits
)
1061 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1065 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1067 // Make it scroll so this item is at the bottom
1069 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1070 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1072 // If we're still off the screen, scroll another line down
1073 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1076 if (startYUnits
!= yUnits
)
1078 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1088 /// Is the given position visible on the screen?
1089 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1091 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1097 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1100 GetViewStart(& startX
, & startY
);
1102 startY
= startY
* ppuY
;
1105 GetVirtualSize(& sx
, & sy
);
1110 wxRect rect
= line
->GetRect();
1112 wxSize clientSize
= GetClientSize();
1114 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1117 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1119 m_caretPosition
= position
;
1120 m_caretAtLineStart
= showAtLineStart
;
1123 /// Move caret one visual step forward: this may mean setting a flag
1124 /// and keeping the same position if we're going from the end of one line
1125 /// to the start of the next, which may be the exact same caret position.
1126 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1128 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1130 // Only do the check if we're not at the end of the paragraph (where things work OK
1132 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1134 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1138 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1140 // We're at the end of a line. See whether we need to
1141 // stay at the same actual caret position but change visual
1142 // position, or not.
1143 if (oldPosition
== lineRange
.GetEnd())
1145 if (m_caretAtLineStart
)
1147 // We're already at the start of the line, so actually move on now.
1148 m_caretPosition
= oldPosition
+ 1;
1149 m_caretAtLineStart
= false;
1153 // We're showing at the end of the line, so keep to
1154 // the same position but indicate that we're to show
1155 // at the start of the next line.
1156 m_caretPosition
= oldPosition
;
1157 m_caretAtLineStart
= true;
1159 SetDefaultStyleToCursorStyle();
1165 SetDefaultStyleToCursorStyle();
1168 /// Move caret one visual step backward: this may mean setting a flag
1169 /// and keeping the same position if we're going from the end of one line
1170 /// to the start of the next, which may be the exact same caret position.
1171 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1173 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1175 // Only do the check if we're not at the start of the paragraph (where things work OK
1177 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1179 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1183 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1185 // We're at the start of a line. See whether we need to
1186 // stay at the same actual caret position but change visual
1187 // position, or not.
1188 if (oldPosition
== lineRange
.GetStart())
1190 m_caretPosition
= oldPosition
-1;
1191 m_caretAtLineStart
= true;
1194 else if (oldPosition
== lineRange
.GetEnd())
1196 if (m_caretAtLineStart
)
1198 // We're at the start of the line, so keep the same caret position
1199 // but clear the start-of-line flag.
1200 m_caretPosition
= oldPosition
;
1201 m_caretAtLineStart
= false;
1205 // We're showing at the end of the line, so go back
1206 // to the previous character position.
1207 m_caretPosition
= oldPosition
- 1;
1209 SetDefaultStyleToCursorStyle();
1215 SetDefaultStyleToCursorStyle();
1219 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1221 long endPos
= GetBuffer().GetRange().GetEnd();
1223 if (m_caretPosition
+ noPositions
< endPos
)
1225 long oldPos
= m_caretPosition
;
1226 long newPos
= m_caretPosition
+ noPositions
;
1228 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1232 // Determine by looking at oldPos and m_caretPosition whether
1233 // we moved from the end of a line to the start of the next line, in which case
1234 // we want to adjust the caret position such that it is positioned at the
1235 // start of the next line, rather than jumping past the first character of the
1237 if (noPositions
== 1 && !extendSel
)
1238 MoveCaretForward(oldPos
);
1240 SetCaretPosition(newPos
);
1243 SetDefaultStyleToCursorStyle();
1254 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1258 if (m_caretPosition
> startPos
- noPositions
+ 1)
1260 long oldPos
= m_caretPosition
;
1261 long newPos
= m_caretPosition
- noPositions
;
1262 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1266 if (noPositions
== 1 && !extendSel
)
1267 MoveCaretBack(oldPos
);
1269 SetCaretPosition(newPos
);
1272 SetDefaultStyleToCursorStyle();
1283 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1285 return MoveDown(- noLines
, flags
);
1289 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1294 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1295 wxPoint pt
= GetCaret()->GetPosition();
1296 long newLine
= lineNumber
+ noLines
;
1298 if (lineNumber
!= -1)
1302 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1304 if (newLine
> lastLine
)
1314 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1317 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1323 wxClientDC
dc(this);
1325 dc
.SetFont(GetFont());
1327 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1329 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1331 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1332 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1333 // so we view the caret at the start of the line.
1334 bool caretLineStart
= false;
1335 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1337 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1338 wxRichTextRange lineRange
;
1340 lineRange
= thisLine
->GetAbsoluteRange();
1342 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1345 caretLineStart
= true;
1349 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1350 if (para
&& para
->GetRange().GetStart() == newPos
)
1355 long newSelEnd
= newPos
;
1357 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1361 SetCaretPosition(newPos
, caretLineStart
);
1363 SetDefaultStyleToCursorStyle();
1373 /// Move to the end of the paragraph
1374 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1376 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1379 long newPos
= para
->GetRange().GetEnd() - 1;
1380 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1384 SetCaretPosition(newPos
);
1386 SetDefaultStyleToCursorStyle();
1396 /// Move to the start of the paragraph
1397 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1399 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1402 long newPos
= para
->GetRange().GetStart() - 1;
1403 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1407 SetCaretPosition(newPos
);
1409 SetDefaultStyleToCursorStyle();
1419 /// Move to the end of the line
1420 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1422 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1426 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1427 long newPos
= lineRange
.GetEnd();
1428 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1432 SetCaretPosition(newPos
);
1434 SetDefaultStyleToCursorStyle();
1444 /// Move to the start of the line
1445 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1447 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1450 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1451 long newPos
= lineRange
.GetStart()-1;
1453 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1457 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1459 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1461 SetDefaultStyleToCursorStyle();
1471 /// Move to the start of the buffer
1472 bool wxRichTextCtrl::MoveHome(int flags
)
1474 if (m_caretPosition
!= -1)
1476 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1480 SetCaretPosition(-1);
1482 SetDefaultStyleToCursorStyle();
1492 /// Move to the end of the buffer
1493 bool wxRichTextCtrl::MoveEnd(int flags
)
1495 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1497 if (m_caretPosition
!= endPos
)
1499 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1503 SetCaretPosition(endPos
);
1505 SetDefaultStyleToCursorStyle();
1515 /// Move noPages pages up
1516 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1518 return PageDown(- noPages
, flags
);
1521 /// Move noPages pages down
1522 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1524 // Calculate which line occurs noPages * screen height further down.
1525 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1528 wxSize clientSize
= GetClientSize();
1529 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1531 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1534 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1535 long pos
= lineRange
.GetStart()-1;
1536 if (pos
!= m_caretPosition
)
1538 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1540 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1544 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1546 SetDefaultStyleToCursorStyle();
1558 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1560 return str
== wxT(" ") || str
== wxT("\t");
1563 // Finds the caret position for the next word
1564 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1566 long endPos
= GetBuffer().GetRange().GetEnd();
1570 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1572 // First skip current text to space
1573 while (i
< endPos
&& i
> -1)
1575 // i is in character, not caret positions
1576 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1577 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1578 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1582 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1589 while (i
< endPos
&& i
> -1)
1591 // i is in character, not caret positions
1592 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1593 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1594 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1595 return wxMax(-1, i
);
1597 if (text
.empty()) // End of paragraph, or maybe an image
1598 return wxMax(-1, i
- 1);
1599 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1603 // Convert to caret position
1604 return wxMax(-1, i
- 1);
1613 long i
= m_caretPosition
;
1615 // First skip white space
1616 while (i
< endPos
&& i
> -1)
1618 // i is in character, not caret positions
1619 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1620 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1622 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1624 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1629 // Next skip current text to space
1630 while (i
< endPos
&& i
> -1)
1632 // i is in character, not caret positions
1633 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1634 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1635 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1638 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1651 /// Move n words left
1652 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1654 long pos
= FindNextWordPosition(-1);
1655 if (pos
!= m_caretPosition
)
1657 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1659 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1663 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1665 SetDefaultStyleToCursorStyle();
1675 /// Move n words right
1676 bool wxRichTextCtrl::WordRight(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();
1700 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1702 // Only do sizing optimization for large buffers
1703 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1705 m_fullLayoutRequired
= true;
1706 m_fullLayoutTime
= wxGetLocalTimeMillis();
1707 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1708 LayoutContent(true /* onlyVisibleRect */);
1711 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1713 #if wxRICHTEXT_BUFFERED_PAINTING
1721 /// Idle-time processing
1722 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1724 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1726 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1728 m_fullLayoutRequired
= false;
1729 m_fullLayoutTime
= 0;
1730 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1731 ShowPosition(m_fullLayoutSavedPosition
);
1735 if (m_caretPositionForDefaultStyle
!= -2)
1737 // If the caret position has changed, no longer reflect the default style
1739 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1740 m_caretPositionForDefaultStyle
= -2;
1747 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1753 /// Set up scrollbars, e.g. after a resize
1754 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1759 if (GetBuffer().IsEmpty())
1761 SetScrollbars(0, 0, 0, 0, 0, 0);
1765 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1766 // of pixels. See e.g. wxVScrolledWindow for ideas.
1767 int pixelsPerUnit
= 5;
1768 wxSize clientSize
= GetClientSize();
1770 int maxHeight
= GetBuffer().GetCachedSize().y
;
1772 // Round up so we have at least maxHeight pixels
1773 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1775 int startX
= 0, startY
= 0;
1777 GetViewStart(& startX
, & startY
);
1779 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1780 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1782 // Move to previous scroll position if
1784 SetScrollbars(0, pixelsPerUnit
,
1786 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1789 /// Paint the background
1790 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1792 wxColour backgroundColour
= GetBackgroundColour();
1793 if (!backgroundColour
.Ok())
1794 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1796 // Clear the background
1797 dc
.SetBrush(wxBrush(backgroundColour
));
1798 dc
.SetPen(*wxTRANSPARENT_PEN
);
1799 wxRect
windowRect(GetClientSize());
1800 windowRect
.x
-= 2; windowRect
.y
-= 2;
1801 windowRect
.width
+= 4; windowRect
.height
+= 4;
1803 // We need to shift the rectangle to take into account
1804 // scrolling. Converting device to logical coordinates.
1805 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1806 dc
.DrawRectangle(windowRect
);
1809 #if wxRICHTEXT_BUFFERED_PAINTING
1810 /// Recreate buffer bitmap if necessary
1811 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1814 if (sz
== wxDefaultSize
)
1815 sz
= GetClientSize();
1817 if (sz
.x
< 1 || sz
.y
< 1)
1820 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1821 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1822 return m_bufferBitmap
.Ok();
1826 // ----------------------------------------------------------------------------
1827 // file IO functions
1828 // ----------------------------------------------------------------------------
1830 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1832 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1834 m_filename
= filename
;
1837 SetInsertionPoint(0);
1840 SetupScrollbars(true);
1842 SendTextUpdatedEvent();
1848 wxLogError(_("File couldn't be loaded."));
1854 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1856 if (GetBuffer().SaveFile(filename
, fileType
))
1858 m_filename
= filename
;
1865 wxLogError(_("The text couldn't be saved."));
1870 // ----------------------------------------------------------------------------
1871 // wxRichTextCtrl specific functionality
1872 // ----------------------------------------------------------------------------
1874 /// Add a new paragraph of text to the end of the buffer
1875 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1877 return GetBuffer().AddParagraph(text
);
1881 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1883 return GetBuffer().AddImage(image
);
1886 // ----------------------------------------------------------------------------
1887 // selection and ranges
1888 // ----------------------------------------------------------------------------
1890 void wxRichTextCtrl::SelectAll()
1892 SetSelection(0, GetLastPosition()+1);
1893 m_selectionAnchor
= -1;
1897 void wxRichTextCtrl::SelectNone()
1899 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1900 SetSelection(-2, -2);
1901 m_selectionAnchor
= -2;
1904 static bool wxIsWordDelimiter(const wxString
& text
)
1906 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1909 /// Select the word at the given character position
1910 bool wxRichTextCtrl::SelectWord(long position
)
1912 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1915 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1919 long positionStart
= position
;
1920 long positionEnd
= position
;
1922 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1924 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1925 if (wxIsWordDelimiter(text
))
1931 if (positionStart
< para
->GetRange().GetStart())
1932 positionStart
= para
->GetRange().GetStart();
1934 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1936 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1937 if (wxIsWordDelimiter(text
))
1943 if (positionEnd
>= para
->GetRange().GetEnd())
1944 positionEnd
= para
->GetRange().GetEnd();
1946 SetSelection(positionStart
, positionEnd
+1);
1948 if (positionStart
>= 0)
1950 MoveCaret(positionStart
-1, true);
1951 SetDefaultStyleToCursorStyle();
1957 wxString
wxRichTextCtrl::GetStringSelection() const
1960 GetSelection(&from
, &to
);
1962 return GetRange(from
, to
);
1965 // ----------------------------------------------------------------------------
1967 // ----------------------------------------------------------------------------
1969 wxTextCtrlHitTestResult
1970 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1972 // implement in terms of the other overload as the native ports typically
1973 // can get the position and not (x, y) pair directly (although wxUniv
1974 // directly gets x and y -- and so overrides this method as well)
1976 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1978 if ( rc
!= wxTE_HT_UNKNOWN
)
1980 PositionToXY(pos
, x
, y
);
1986 wxTextCtrlHitTestResult
1987 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1990 wxClientDC
dc((wxRichTextCtrl
*) this);
1991 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1993 // Buffer uses logical position (relative to start of buffer)
1995 wxPoint pt2
= GetLogicalPoint(pt
);
1997 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1999 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2000 return wxTE_HT_BEFORE
;
2001 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2002 return wxTE_HT_BEYOND
;
2003 else if (hit
& wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
)
2004 return wxTE_HT_ON_TEXT
;
2006 return wxTE_HT_UNKNOWN
;
2009 // ----------------------------------------------------------------------------
2010 // set/get the controls text
2011 // ----------------------------------------------------------------------------
2013 wxString
wxRichTextCtrl::GetValue() const
2015 return GetBuffer().GetText();
2018 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2020 // Public API for range is different from internals
2021 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2024 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2028 // if the text is long enough, it's faster to just set it instead of first
2029 // comparing it with the old one (chances are that it will be different
2030 // anyhow, this comparison is there to avoid flicker for small single-line
2031 // edit controls mostly)
2032 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
2036 // for compatibility, don't move the cursor when doing SetValue()
2037 SetInsertionPoint(0);
2041 if ( flags
& SetValue_SendEvent
)
2043 // still send an event for consistency
2044 SendTextUpdatedEvent();
2048 // we should reset the modified flag even if the value didn't really change
2050 // mark the control as being not dirty - we changed its text, not the
2055 void wxRichTextCtrl::WriteText(const wxString
& value
)
2060 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2062 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2064 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2066 if ( flags
& SetValue_SendEvent
)
2067 SendTextUpdatedEvent();
2070 void wxRichTextCtrl::AppendText(const wxString
& text
)
2072 SetInsertionPointEnd();
2077 /// Write an image at the current insertion point
2078 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2080 wxRichTextImageBlock imageBlock
;
2082 wxImage image2
= image
;
2083 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2084 return WriteImage(imageBlock
);
2089 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2091 wxRichTextImageBlock imageBlock
;
2094 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2095 return WriteImage(imageBlock
);
2100 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2102 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2105 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2109 wxRichTextImageBlock imageBlock
;
2111 wxImage image
= bitmap
.ConvertToImage();
2112 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2113 return WriteImage(imageBlock
);
2119 /// Insert a newline (actually paragraph) at the current insertion point.
2120 bool wxRichTextCtrl::Newline()
2122 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2125 /// Insert a line break at the current insertion point.
2126 bool wxRichTextCtrl::LineBreak()
2129 text
= wxRichTextLineBreakChar
;
2130 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2133 // ----------------------------------------------------------------------------
2134 // Clipboard operations
2135 // ----------------------------------------------------------------------------
2137 void wxRichTextCtrl::Copy()
2141 wxRichTextRange range
= GetInternalSelectionRange();
2142 GetBuffer().CopyToClipboard(range
);
2146 void wxRichTextCtrl::Cut()
2150 wxRichTextRange range
= GetInternalSelectionRange();
2151 GetBuffer().CopyToClipboard(range
);
2153 DeleteSelectedContent();
2159 void wxRichTextCtrl::Paste()
2163 BeginBatchUndo(_("Paste"));
2165 long newPos
= m_caretPosition
;
2166 DeleteSelectedContent(& newPos
);
2168 GetBuffer().PasteFromClipboard(newPos
);
2174 void wxRichTextCtrl::DeleteSelection()
2176 if (CanDeleteSelection())
2178 DeleteSelectedContent();
2182 bool wxRichTextCtrl::HasSelection() const
2184 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2187 bool wxRichTextCtrl::CanCopy() const
2189 // Can copy if there's a selection
2190 return HasSelection();
2193 bool wxRichTextCtrl::CanCut() const
2195 return HasSelection() && IsEditable();
2198 bool wxRichTextCtrl::CanPaste() const
2200 if ( !IsEditable() )
2203 return GetBuffer().CanPasteFromClipboard();
2206 bool wxRichTextCtrl::CanDeleteSelection() const
2208 return HasSelection() && IsEditable();
2212 // ----------------------------------------------------------------------------
2214 // ----------------------------------------------------------------------------
2216 void wxRichTextCtrl::SetEditable(bool editable
)
2218 m_editable
= editable
;
2221 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2225 m_caretPosition
= pos
- 1;
2228 void wxRichTextCtrl::SetInsertionPointEnd()
2230 long pos
= GetLastPosition();
2231 SetInsertionPoint(pos
);
2234 long wxRichTextCtrl::GetInsertionPoint() const
2236 return m_caretPosition
+1;
2239 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2241 return GetBuffer().GetRange().GetEnd();
2244 // If the return values from and to are the same, there is no
2246 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2248 *from
= m_selectionRange
.GetStart();
2249 *to
= m_selectionRange
.GetEnd();
2250 if ((*to
) != -1 && (*to
) != -2)
2254 bool wxRichTextCtrl::IsEditable() const
2259 // ----------------------------------------------------------------------------
2261 // ----------------------------------------------------------------------------
2263 void wxRichTextCtrl::SetSelection(long from
, long to
)
2265 // if from and to are both -1, it means (in wxWidgets) that all text should
2267 if ( (from
== -1) && (to
== -1) )
2270 to
= GetLastPosition()+1;
2273 DoSetSelection(from
, to
);
2276 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2278 m_selectionAnchor
= from
;
2279 m_selectionRange
.SetRange(from
, to
-1);
2281 m_caretPosition
= from
-1;
2287 // ----------------------------------------------------------------------------
2289 // ----------------------------------------------------------------------------
2291 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2292 const wxString
& value
)
2294 BeginBatchUndo(_("Replace"));
2296 DeleteSelectedContent();
2298 DoWriteText(value
, SetValue_SelectionOnly
);
2303 void wxRichTextCtrl::Remove(long from
, long to
)
2307 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2314 bool wxRichTextCtrl::IsModified() const
2316 return m_buffer
.IsModified();
2319 void wxRichTextCtrl::MarkDirty()
2321 m_buffer
.Modify(true);
2324 void wxRichTextCtrl::DiscardEdits()
2326 m_caretPositionForDefaultStyle
= -2;
2327 m_buffer
.Modify(false);
2328 m_buffer
.GetCommandProcessor()->ClearCommands();
2331 int wxRichTextCtrl::GetNumberOfLines() const
2333 return GetBuffer().GetParagraphCount();
2336 // ----------------------------------------------------------------------------
2337 // Positions <-> coords
2338 // ----------------------------------------------------------------------------
2340 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2342 return GetBuffer().XYToPosition(x
, y
);
2345 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2347 return GetBuffer().PositionToXY(pos
, x
, y
);
2350 // ----------------------------------------------------------------------------
2352 // ----------------------------------------------------------------------------
2354 void wxRichTextCtrl::ShowPosition(long pos
)
2356 if (!IsPositionVisible(pos
))
2357 ScrollIntoView(pos
-1, WXK_DOWN
);
2360 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2362 return GetBuffer().GetParagraphLength(lineNo
);
2365 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2367 return GetBuffer().GetParagraphText(lineNo
);
2370 // ----------------------------------------------------------------------------
2372 // ----------------------------------------------------------------------------
2374 void wxRichTextCtrl::Undo()
2378 GetCommandProcessor()->Undo();
2382 void wxRichTextCtrl::Redo()
2386 GetCommandProcessor()->Redo();
2390 bool wxRichTextCtrl::CanUndo() const
2392 return GetCommandProcessor()->CanUndo();
2395 bool wxRichTextCtrl::CanRedo() const
2397 return GetCommandProcessor()->CanRedo();
2400 // ----------------------------------------------------------------------------
2401 // implementation details
2402 // ----------------------------------------------------------------------------
2404 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2406 SetValue(event
.GetString());
2407 GetEventHandler()->ProcessEvent(event
);
2410 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2412 // By default, load the first file into the text window.
2413 if (event
.GetNumberOfFiles() > 0)
2415 LoadFile(event
.GetFiles()[0]);
2419 wxSize
wxRichTextCtrl::DoGetBestSize() const
2421 return wxSize(10, 10);
2424 // ----------------------------------------------------------------------------
2425 // standard handlers for standard edit menu events
2426 // ----------------------------------------------------------------------------
2428 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2433 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2438 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2443 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2448 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2453 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2458 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2460 event
.Enable( CanCut() );
2463 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2465 event
.Enable( CanCopy() );
2468 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2470 event
.Enable( CanDeleteSelection() );
2473 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2475 event
.Enable( CanPaste() );
2478 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2480 event
.Enable( CanUndo() );
2481 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2484 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2486 event
.Enable( CanRedo() );
2487 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2490 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2495 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2497 event
.Enable(GetLastPosition() > 0);
2500 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2504 m_contextMenu
= new wxMenu
;
2505 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2506 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2507 m_contextMenu
->AppendSeparator();
2508 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2509 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2510 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2511 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2512 m_contextMenu
->AppendSeparator();
2513 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2515 PopupMenu(m_contextMenu
);
2519 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2521 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2524 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2526 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2529 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2531 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2534 // extended style setting operation with flags including:
2535 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2536 // see richtextbuffer.h for more details.
2537 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2539 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2542 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2544 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2547 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2549 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2552 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2554 return GetBuffer().SetDefaultStyle(style
);
2557 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2559 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2562 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2564 return GetBuffer().GetDefaultStyle();
2567 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2569 return GetBuffer().GetDefaultStyle();
2572 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2574 wxTextAttrEx
attr(style
);
2575 if (GetBuffer().GetStyle(position
, attr
))
2584 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2586 return GetBuffer().GetStyle(position
, style
);
2589 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2591 return GetBuffer().GetStyle(position
, style
);
2594 // get the common set of styles for the range
2595 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2597 wxTextAttrEx styleEx
;
2598 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2607 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2609 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2612 /// Get the content (uncombined) attributes for this position.
2614 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2616 wxTextAttrEx
attr(style
);
2617 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2626 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2628 return GetBuffer().GetUncombinedStyle(position
, style
);
2631 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2633 return GetBuffer().GetUncombinedStyle(position
, style
);
2636 /// Set font, and also the buffer attributes
2637 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2639 wxControl::SetFont(font
);
2641 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2643 GetBuffer().SetBasicStyle(attr
);
2645 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2651 /// Transform logical to physical
2652 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2655 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2660 /// Transform physical to logical
2661 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2664 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2669 /// Position the caret
2670 void wxRichTextCtrl::PositionCaret()
2675 //wxLogDebug(wxT("PositionCaret"));
2678 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2680 wxPoint originalPt
= caretRect
.GetPosition();
2681 wxPoint pt
= GetPhysicalPoint(originalPt
);
2682 if (GetCaret()->GetPosition() != pt
)
2684 GetCaret()->Move(pt
);
2685 GetCaret()->SetSize(caretRect
.GetSize());
2690 /// Get the caret height and position for the given character position
2691 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2693 wxClientDC
dc(this);
2694 dc
.SetFont(GetFont());
2701 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2703 // Caret height can't be zero
2705 height
= dc
.GetCharHeight();
2707 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2714 /// Gets the line for the visible caret position. If the caret is
2715 /// shown at the very end of the line, it means the next character is actually
2716 /// on the following line. So let's get the line we're expecting to find
2717 /// if this is the case.
2718 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2720 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2721 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2724 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2725 if (caretPosition
== lineRange
.GetStart()-1 &&
2726 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2728 if (!m_caretAtLineStart
)
2729 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2736 /// Move the caret to the given character position
2737 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2739 if (GetBuffer().GetDirty())
2742 if (pos
<= GetBuffer().GetRange().GetEnd())
2744 SetCaretPosition(pos
, showAtLineStart
);
2754 /// Layout the buffer: which we must do before certain operations, such as
2755 /// setting the caret position.
2756 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2758 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2760 wxRect
availableSpace(GetClientSize());
2761 if (availableSpace
.width
== 0)
2762 availableSpace
.width
= 10;
2763 if (availableSpace
.height
== 0)
2764 availableSpace
.height
= 10;
2766 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2767 if (onlyVisibleRect
)
2769 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2770 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2773 wxClientDC
dc(this);
2774 dc
.SetFont(GetFont());
2778 GetBuffer().Defragment();
2779 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2780 GetBuffer().Layout(dc
, availableSpace
, flags
);
2781 GetBuffer().SetDirty(false);
2790 /// Is all of the selection bold?
2791 bool wxRichTextCtrl::IsSelectionBold()
2795 wxRichTextAttr attr
;
2796 wxRichTextRange range
= GetSelectionRange();
2797 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2798 attr
.SetFontWeight(wxBOLD
);
2800 return HasCharacterAttributes(range
, attr
);
2804 // If no selection, then we need to combine current style with default style
2805 // to see what the effect would be if we started typing.
2806 wxRichTextAttr attr
;
2807 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2809 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2810 if (GetStyle(pos
, attr
))
2812 if (IsDefaultStyleShowing())
2813 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2814 return attr
.GetFontWeight() == wxBOLD
;
2820 /// Is all of the selection italics?
2821 bool wxRichTextCtrl::IsSelectionItalics()
2825 wxRichTextRange range
= GetSelectionRange();
2826 wxRichTextAttr attr
;
2827 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2828 attr
.SetFontStyle(wxITALIC
);
2830 return HasCharacterAttributes(range
, attr
);
2834 // If no selection, then we need to combine current style with default style
2835 // to see what the effect would be if we started typing.
2836 wxRichTextAttr attr
;
2837 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2839 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2840 if (GetStyle(pos
, attr
))
2842 if (IsDefaultStyleShowing())
2843 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2844 return attr
.GetFontStyle() == wxITALIC
;
2850 /// Is all of the selection underlined?
2851 bool wxRichTextCtrl::IsSelectionUnderlined()
2855 wxRichTextRange range
= GetSelectionRange();
2856 wxRichTextAttr attr
;
2857 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2858 attr
.SetFontUnderlined(true);
2860 return HasCharacterAttributes(range
, attr
);
2864 // If no selection, then we need to combine current style with default style
2865 // to see what the effect would be if we started typing.
2866 wxRichTextAttr attr
;
2867 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2868 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2870 if (GetStyle(pos
, attr
))
2872 if (IsDefaultStyleShowing())
2873 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2874 return attr
.GetFontUnderlined();
2880 /// Apply bold to the selection
2881 bool wxRichTextCtrl::ApplyBoldToSelection()
2883 wxRichTextAttr attr
;
2884 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2885 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2888 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2890 SetAndShowDefaultStyle(attr
);
2894 /// Apply italic to the selection
2895 bool wxRichTextCtrl::ApplyItalicToSelection()
2897 wxRichTextAttr attr
;
2898 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2899 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2902 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2904 SetAndShowDefaultStyle(attr
);
2908 /// Apply underline to the selection
2909 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2911 wxRichTextAttr attr
;
2912 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2913 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2916 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2918 SetAndShowDefaultStyle(attr
);
2922 /// Is all of the selection aligned according to the specified flag?
2923 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2925 wxRichTextRange range
;
2927 range
= GetSelectionRange();
2929 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2931 wxRichTextAttr attr
;
2932 attr
.SetAlignment(alignment
);
2934 return HasParagraphAttributes(range
, attr
);
2937 /// Apply alignment to the selection
2938 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2940 wxRichTextAttr attr
;
2941 attr
.SetAlignment(alignment
);
2943 return SetStyle(GetSelectionRange(), attr
);
2946 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2948 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2953 /// Apply a named style to the selection
2954 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2956 // Flags are defined within each definition, so only certain
2957 // attributes are applied.
2958 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2960 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2962 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2964 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2966 wxRichTextRange range
;
2969 range
= GetSelectionRange();
2972 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2973 range
= wxRichTextRange(pos
, pos
+1);
2976 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2979 // Make sure the attr has the style name
2980 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2982 attr
.SetParagraphStyleName(def
->GetName());
2984 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2985 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2986 // to change its style independently.
2987 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2990 attr
.SetCharacterStyleName(def
->GetName());
2993 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2996 SetAndShowDefaultStyle(attr
);
3001 /// Apply the style sheet to the buffer, for example if the styles have changed.
3002 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3005 styleSheet
= GetBuffer().GetStyleSheet();
3009 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3011 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3019 /// Sets the default style to the style under the cursor
3020 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3023 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3025 // If at the start of a paragraph, use the next position.
3026 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3028 if (GetUncombinedStyle(pos
, attr
))
3030 SetDefaultStyle(attr
);
3037 /// Returns the first visible position in the current view
3038 long wxRichTextCtrl::GetFirstVisiblePosition() const
3040 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3042 return line
->GetAbsoluteRange().GetStart();
3047 /// Get the first visible point in the window
3048 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3051 int startXUnits
, startYUnits
;
3053 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3054 GetViewStart(& startXUnits
, & startYUnits
);
3056 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3059 /// The adjusted caret position is the character position adjusted to take
3060 /// into account whether we're at the start of a paragraph, in which case
3061 /// style information should be taken from the next position, not current one.
3062 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3064 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3066 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3071 /// Get/set the selection range in character positions. -1, -1 means no selection.
3072 /// The range is in API convention, i.e. a single character selection is denoted
3074 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3076 wxRichTextRange range
= GetInternalSelectionRange();
3077 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3078 range
.SetEnd(range
.GetEnd() + 1);
3082 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3084 wxRichTextRange
range1(range
);
3085 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3086 range1
.SetEnd(range1
.GetEnd() - 1);
3088 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3090 SetInternalSelectionRange(range1
);
3094 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3096 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3099 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3101 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3104 /// Clear list for given range
3105 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3107 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3110 /// Number/renumber any list elements in the given range
3111 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3113 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3116 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3118 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3121 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3122 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3124 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3127 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3129 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3132 /// Deletes the content in the given range
3133 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3135 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3138 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3140 if (sm_availableFontNames
.GetCount() == 0)
3142 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3143 sm_availableFontNames
.Sort();
3145 return sm_availableFontNames
;
3148 void wxRichTextCtrl::ClearAvailableFontNames()
3150 sm_availableFontNames
.Clear();