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_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
79 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
80 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
82 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
83 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
85 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
86 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
88 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
89 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
91 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
92 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
94 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
95 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
97 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
98 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
105 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
107 wxRichTextCtrl::wxRichTextCtrl()
108 : wxScrollHelper(this)
113 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
115 const wxString
& value
,
119 const wxValidator
& validator
,
120 const wxString
& name
)
121 : wxScrollHelper(this)
124 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
128 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
129 const wxValidator
& validator
, const wxString
& name
)
131 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
132 style
|wxFULL_REPAINT_ON_RESIZE
,
138 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
142 GetBuffer().SetRichTextCtrl(this);
144 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
147 if (style
& wxTE_READONLY
)
150 // The base attributes must all have default values
151 wxTextAttrEx attributes
;
152 attributes
.SetFont(GetFont());
153 attributes
.SetTextColour(*wxBLACK
);
154 attributes
.SetBackgroundColour(*wxWHITE
);
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())
329 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
335 dc
.SetFont(GetFont());
338 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
340 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
342 m_dragStart
= event
.GetLogicalPosition(dc
);
348 bool caretAtLineStart
= false;
350 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
352 // If we're at the start of a line (but not first in para)
353 // then we should keep the caret showing at the start of the line
354 // by showing the m_caretAtLineStart flag.
355 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
356 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
358 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
359 caretAtLineStart
= true;
363 MoveCaret(position
, caretAtLineStart
);
364 SetDefaultStyleToCursorStyle();
371 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
376 if (GetCapture() == this)
379 // See if we clicked on a URL
382 dc
.SetFont(GetFont());
385 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
386 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
388 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
391 if (GetStyle(position
, attr
))
393 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
395 wxString urlTarget
= attr
.GetURL();
396 if (!urlTarget
.IsEmpty())
398 wxMouseEvent
mouseEvent(event
);
400 long startPos
= 0, endPos
= 0;
401 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
404 startPos
= obj
->GetRange().GetStart();
405 endPos
= obj
->GetRange().GetEnd();
408 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
409 InitCommandEvent(urlEvent
);
411 urlEvent
.SetString(urlTarget
);
413 GetEventHandler()->ProcessEvent(urlEvent
);
422 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
426 dc
.SetFont(GetFont());
429 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
430 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
432 // See if we need to change the cursor
435 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
438 if (GetStyle(position
, attr
))
440 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
442 SetCursor(m_urlCursor
);
444 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
446 SetCursor(m_textCursor
);
452 if (!event
.Dragging())
458 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
460 // TODO: test closeness
462 bool caretAtLineStart
= false;
464 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
466 // If we're at the start of a line (but not first in para)
467 // then we should keep the caret showing at the start of the line
468 // by showing the m_caretAtLineStart flag.
469 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
470 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
472 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
473 caretAtLineStart
= true;
477 if (m_caretPosition
!= position
)
479 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
481 MoveCaret(position
, caretAtLineStart
);
482 SetDefaultStyleToCursorStyle();
491 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
497 /// Left-double-click
498 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
500 SelectWord(GetCaretPosition()+1);
505 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
511 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
515 flags
|= wxRICHTEXT_CTRL_DOWN
;
516 if (event
.ShiftDown())
517 flags
|= wxRICHTEXT_SHIFT_DOWN
;
519 flags
|= wxRICHTEXT_ALT_DOWN
;
521 if (event
.GetKeyCode() == WXK_LEFT
||
522 event
.GetKeyCode() == WXK_RIGHT
||
523 event
.GetKeyCode() == WXK_UP
||
524 event
.GetKeyCode() == WXK_DOWN
||
525 event
.GetKeyCode() == WXK_HOME
||
526 event
.GetKeyCode() == WXK_PAGEUP
||
527 event
.GetKeyCode() == WXK_PAGEDOWN
||
528 event
.GetKeyCode() == WXK_END
||
530 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
531 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
532 event
.GetKeyCode() == WXK_NUMPAD_UP
||
533 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
534 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
535 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
536 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
537 event
.GetKeyCode() == WXK_NUMPAD_END
)
539 KeyboardNavigate(event
.GetKeyCode(), flags
);
543 // all the other keys modify the controls contents which shouldn't be
544 // possible if we're read-only
551 if (event
.GetKeyCode() == WXK_RETURN
)
553 BeginBatchUndo(_("Insert Text"));
555 long newPos
= m_caretPosition
;
557 DeleteSelectedContent(& newPos
);
559 if (event
.ShiftDown())
562 text
= wxRichTextLineBreakChar
;
563 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
566 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
569 SetDefaultStyleToCursorStyle();
571 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
573 wxRichTextEvent
cmdEvent(
574 wxEVT_COMMAND_RICHTEXT_RETURN
,
576 cmdEvent
.SetEventObject(this);
577 cmdEvent
.SetFlags(flags
);
578 cmdEvent
.SetPosition(newPos
+1);
580 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
582 // Generate conventional event
583 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
584 InitCommandEvent(textEvent
);
586 GetEventHandler()->ProcessEvent(textEvent
);
590 else if (event
.GetKeyCode() == WXK_BACK
)
592 BeginBatchUndo(_("Delete Text"));
594 // Submit range in character positions, which are greater than caret positions,
595 // so subtract 1 for deleted character and add 1 for conversion to character position.
596 if (m_caretPosition
> -1 && !HasSelection())
598 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
601 DeleteSelectedContent();
605 if (GetLastPosition() == -1)
609 m_caretPosition
= -1;
611 SetDefaultStyleToCursorStyle();
614 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
616 wxRichTextEvent
cmdEvent(
617 wxEVT_COMMAND_RICHTEXT_DELETE
,
619 cmdEvent
.SetEventObject(this);
620 cmdEvent
.SetFlags(flags
);
621 cmdEvent
.SetPosition(m_caretPosition
+1);
622 GetEventHandler()->ProcessEvent(cmdEvent
);
626 else if (event
.GetKeyCode() == WXK_DELETE
)
628 BeginBatchUndo(_("Delete Text"));
630 // Submit range in character positions, which are greater than caret positions,
631 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
633 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
636 DeleteSelectedContent();
640 if (GetLastPosition() == -1)
644 m_caretPosition
= -1;
646 SetDefaultStyleToCursorStyle();
649 wxRichTextEvent
cmdEvent(
650 wxEVT_COMMAND_RICHTEXT_DELETE
,
652 cmdEvent
.SetEventObject(this);
653 cmdEvent
.SetFlags(flags
);
654 cmdEvent
.SetPosition(m_caretPosition
+1);
655 GetEventHandler()->ProcessEvent(cmdEvent
);
661 long keycode
= event
.GetKeyCode();
735 case WXK_NUMPAD_SPACE
:
737 case WXK_NUMPAD_ENTER
:
742 case WXK_NUMPAD_HOME
:
743 case WXK_NUMPAD_LEFT
:
745 case WXK_NUMPAD_RIGHT
:
746 case WXK_NUMPAD_DOWN
:
747 case WXK_NUMPAD_PAGEUP
:
748 case WXK_NUMPAD_PAGEDOWN
:
750 case WXK_NUMPAD_BEGIN
:
751 case WXK_NUMPAD_INSERT
:
752 case WXK_NUMPAD_DELETE
:
753 case WXK_NUMPAD_EQUAL
:
754 case WXK_NUMPAD_MULTIPLY
:
756 case WXK_NUMPAD_SEPARATOR
:
757 case WXK_NUMPAD_SUBTRACT
:
758 case WXK_NUMPAD_DECIMAL
:
766 if (event
.CmdDown() || event
.AltDown())
772 wxRichTextEvent
cmdEvent(
773 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
775 cmdEvent
.SetEventObject(this);
776 cmdEvent
.SetFlags(flags
);
777 cmdEvent
.SetCharacter((wxChar
) keycode
);
778 cmdEvent
.SetPosition(m_caretPosition
+1);
780 if (keycode
== wxT('\t'))
782 // See if we need to promote or demote the selection or paragraph at the cursor
783 // position, instead of inserting a tab.
784 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
785 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
786 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
788 wxRichTextRange range
;
790 range
= GetSelectionRange();
792 range
= para
->GetRange().FromInternal();
794 int promoteBy
= event
.ShiftDown() ? 1 : -1;
796 PromoteList(promoteBy
, range
, NULL
);
798 GetEventHandler()->ProcessEvent(cmdEvent
);
804 BeginBatchUndo(_("Insert Text"));
806 long newPos
= m_caretPosition
;
807 DeleteSelectedContent(& newPos
);
809 wxString str
= (wxChar
) event
.GetKeyCode();
810 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
814 SetDefaultStyleToCursorStyle();
815 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
817 GetEventHandler()->ProcessEvent(cmdEvent
);
825 /// Delete content if there is a selection, e.g. when pressing a key.
826 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
830 long pos
= m_selectionRange
.GetStart();
831 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
832 m_selectionRange
.SetRange(-2, -2);
842 /// Keyboard navigation
846 Left: left one character
847 Right: right one character
850 Ctrl-Left: left one word
851 Ctrl-Right: right one word
852 Ctrl-Up: previous paragraph start
853 Ctrl-Down: next start of paragraph
856 Ctrl-Home: start of document
857 Ctrl-End: end of document
859 Page-Down: Down a screen
863 Ctrl-Alt-PgUp: Start of window
864 Ctrl-Alt-PgDn: End of window
865 F8: Start selection mode
866 Esc: End selection mode
868 Adding Shift does the above but starts/extends selection.
873 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
875 bool success
= false;
877 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
879 if (flags
& wxRICHTEXT_CTRL_DOWN
)
880 success
= WordRight(1, flags
);
882 success
= MoveRight(1, flags
);
884 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
886 if (flags
& wxRICHTEXT_CTRL_DOWN
)
887 success
= WordLeft(1, flags
);
889 success
= MoveLeft(1, flags
);
891 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
893 if (flags
& wxRICHTEXT_CTRL_DOWN
)
894 success
= MoveToParagraphStart(flags
);
896 success
= MoveUp(1, flags
);
898 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
900 if (flags
& wxRICHTEXT_CTRL_DOWN
)
901 success
= MoveToParagraphEnd(flags
);
903 success
= MoveDown(1, flags
);
905 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
907 success
= PageUp(1, flags
);
909 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
911 success
= PageDown(1, flags
);
913 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
915 if (flags
& wxRICHTEXT_CTRL_DOWN
)
916 success
= MoveHome(flags
);
918 success
= MoveToLineStart(flags
);
920 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
922 if (flags
& wxRICHTEXT_CTRL_DOWN
)
923 success
= MoveEnd(flags
);
925 success
= MoveToLineEnd(flags
);
930 ScrollIntoView(m_caretPosition
, keyCode
);
931 SetDefaultStyleToCursorStyle();
937 /// Extend the selection. Selections are in caret positions.
938 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
940 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
942 // If not currently selecting, start selecting
943 if (m_selectionRange
.GetStart() == -2)
945 m_selectionAnchor
= oldPos
;
948 m_selectionRange
.SetRange(newPos
+1, oldPos
);
950 m_selectionRange
.SetRange(oldPos
+1, newPos
);
954 // Always ensure that the selection range start is greater than
956 if (newPos
> m_selectionAnchor
)
957 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
959 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
962 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
964 wxLogDebug(wxT("Strange selection range"));
973 /// Scroll into view, returning true if we scrolled.
974 /// This takes a _caret_ position.
975 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
977 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
983 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
985 int startXUnits
, startYUnits
;
986 GetViewStart(& startXUnits
, & startYUnits
);
987 int startY
= startYUnits
* ppuY
;
990 GetVirtualSize(& sx
, & sy
);
996 wxRect rect
= line
->GetRect();
998 bool scrolled
= false;
1000 wxSize clientSize
= GetClientSize();
1003 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1004 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
1005 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1006 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1008 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1010 // Make it scroll so this item is at the bottom
1012 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1013 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1015 // If we're still off the screen, scroll another line down
1016 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1019 if (startYUnits
!= yUnits
)
1021 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1025 else if (rect
.y
< startY
)
1027 // Make it scroll so this item is at the top
1030 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1032 if (startYUnits
!= yUnits
)
1034 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1040 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1041 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1042 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1043 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1045 if (rect
.y
< startY
)
1047 // Make it scroll so this item is at the top
1050 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1052 if (startYUnits
!= yUnits
)
1054 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1058 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1060 // Make it scroll so this item is at the bottom
1062 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1063 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1065 // If we're still off the screen, scroll another line down
1066 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1069 if (startYUnits
!= yUnits
)
1071 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1081 /// Is the given position visible on the screen?
1082 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1084 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1090 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1093 GetViewStart(& startX
, & startY
);
1095 startY
= startY
* ppuY
;
1098 GetVirtualSize(& sx
, & sy
);
1103 wxRect rect
= line
->GetRect();
1105 wxSize clientSize
= GetClientSize();
1107 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1110 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1112 m_caretPosition
= position
;
1113 m_caretAtLineStart
= showAtLineStart
;
1116 /// Move caret one visual step forward: this may mean setting a flag
1117 /// and keeping the same position if we're going from the end of one line
1118 /// to the start of the next, which may be the exact same caret position.
1119 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1121 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1123 // Only do the check if we're not at the end of the paragraph (where things work OK
1125 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1127 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1131 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1133 // We're at the end of a line. See whether we need to
1134 // stay at the same actual caret position but change visual
1135 // position, or not.
1136 if (oldPosition
== lineRange
.GetEnd())
1138 if (m_caretAtLineStart
)
1140 // We're already at the start of the line, so actually move on now.
1141 m_caretPosition
= oldPosition
+ 1;
1142 m_caretAtLineStart
= false;
1146 // We're showing at the end of the line, so keep to
1147 // the same position but indicate that we're to show
1148 // at the start of the next line.
1149 m_caretPosition
= oldPosition
;
1150 m_caretAtLineStart
= true;
1152 SetDefaultStyleToCursorStyle();
1158 SetDefaultStyleToCursorStyle();
1161 /// Move caret one visual step backward: this may mean setting a flag
1162 /// and keeping the same position if we're going from the end of one line
1163 /// to the start of the next, which may be the exact same caret position.
1164 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1166 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1168 // Only do the check if we're not at the start of the paragraph (where things work OK
1170 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1172 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1176 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1178 // We're at the start of a line. See whether we need to
1179 // stay at the same actual caret position but change visual
1180 // position, or not.
1181 if (oldPosition
== lineRange
.GetStart())
1183 m_caretPosition
= oldPosition
-1;
1184 m_caretAtLineStart
= true;
1187 else if (oldPosition
== lineRange
.GetEnd())
1189 if (m_caretAtLineStart
)
1191 // We're at the start of the line, so keep the same caret position
1192 // but clear the start-of-line flag.
1193 m_caretPosition
= oldPosition
;
1194 m_caretAtLineStart
= false;
1198 // We're showing at the end of the line, so go back
1199 // to the previous character position.
1200 m_caretPosition
= oldPosition
- 1;
1202 SetDefaultStyleToCursorStyle();
1208 SetDefaultStyleToCursorStyle();
1212 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1214 long endPos
= GetBuffer().GetRange().GetEnd();
1216 if (m_caretPosition
+ noPositions
< endPos
)
1218 long oldPos
= m_caretPosition
;
1219 long newPos
= m_caretPosition
+ noPositions
;
1221 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1225 // Determine by looking at oldPos and m_caretPosition whether
1226 // we moved from the end of a line to the start of the next line, in which case
1227 // we want to adjust the caret position such that it is positioned at the
1228 // start of the next line, rather than jumping past the first character of the
1230 if (noPositions
== 1 && !extendSel
)
1231 MoveCaretForward(oldPos
);
1233 SetCaretPosition(newPos
);
1236 SetDefaultStyleToCursorStyle();
1247 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1251 if (m_caretPosition
> startPos
- noPositions
+ 1)
1253 long oldPos
= m_caretPosition
;
1254 long newPos
= m_caretPosition
- noPositions
;
1255 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1259 if (noPositions
== 1 && !extendSel
)
1260 MoveCaretBack(oldPos
);
1262 SetCaretPosition(newPos
);
1265 SetDefaultStyleToCursorStyle();
1276 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1278 return MoveDown(- noLines
, flags
);
1282 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1287 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1288 wxPoint pt
= GetCaret()->GetPosition();
1289 long newLine
= lineNumber
+ noLines
;
1291 if (lineNumber
!= -1)
1295 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1297 if (newLine
> lastLine
)
1307 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1310 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1316 wxClientDC
dc(this);
1318 dc
.SetFont(GetFont());
1320 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1322 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1324 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1325 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1326 // so we view the caret at the start of the line.
1327 bool caretLineStart
= false;
1328 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1330 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1331 wxRichTextRange lineRange
;
1333 lineRange
= thisLine
->GetAbsoluteRange();
1335 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1338 caretLineStart
= true;
1342 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1343 if (para
&& para
->GetRange().GetStart() == newPos
)
1348 long newSelEnd
= newPos
;
1350 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1354 SetCaretPosition(newPos
, caretLineStart
);
1356 SetDefaultStyleToCursorStyle();
1366 /// Move to the end of the paragraph
1367 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1369 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1372 long newPos
= para
->GetRange().GetEnd() - 1;
1373 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1377 SetCaretPosition(newPos
);
1379 SetDefaultStyleToCursorStyle();
1389 /// Move to the start of the paragraph
1390 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1392 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1395 long newPos
= para
->GetRange().GetStart() - 1;
1396 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1400 SetCaretPosition(newPos
);
1402 SetDefaultStyleToCursorStyle();
1412 /// Move to the end of the line
1413 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1415 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1419 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1420 long newPos
= lineRange
.GetEnd();
1421 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1425 SetCaretPosition(newPos
);
1427 SetDefaultStyleToCursorStyle();
1437 /// Move to the start of the line
1438 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1440 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1443 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1444 long newPos
= lineRange
.GetStart()-1;
1446 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1450 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1452 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1454 SetDefaultStyleToCursorStyle();
1464 /// Move to the start of the buffer
1465 bool wxRichTextCtrl::MoveHome(int flags
)
1467 if (m_caretPosition
!= -1)
1469 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1473 SetCaretPosition(-1);
1475 SetDefaultStyleToCursorStyle();
1485 /// Move to the end of the buffer
1486 bool wxRichTextCtrl::MoveEnd(int flags
)
1488 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1490 if (m_caretPosition
!= endPos
)
1492 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1496 SetCaretPosition(endPos
);
1498 SetDefaultStyleToCursorStyle();
1508 /// Move noPages pages up
1509 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1511 return PageDown(- noPages
, flags
);
1514 /// Move noPages pages down
1515 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1517 // Calculate which line occurs noPages * screen height further down.
1518 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1521 wxSize clientSize
= GetClientSize();
1522 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1524 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1527 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1528 long pos
= lineRange
.GetStart()-1;
1529 if (pos
!= m_caretPosition
)
1531 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1533 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1537 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1539 SetDefaultStyleToCursorStyle();
1551 // Finds the caret position for the next word
1552 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1554 long endPos
= GetBuffer().GetRange().GetEnd();
1558 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1560 // First skip current text to space
1561 while (i
< endPos
&& i
> -1)
1563 // i is in character, not caret positions
1564 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1565 if (text
!= wxT(" ") && !text
.empty())
1572 while (i
< endPos
&& i
> -1)
1574 // i is in character, not caret positions
1575 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1576 if (text
.empty()) // End of paragraph, or maybe an image
1577 return wxMax(-1, i
- 1);
1578 else if (text
== wxT(" ") || text
.empty())
1582 // Convert to caret position
1583 return wxMax(-1, i
- 1);
1592 long i
= m_caretPosition
;
1594 // First skip white space
1595 while (i
< endPos
&& i
> -1)
1597 // i is in character, not caret positions
1598 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1599 if (text
.empty()) // End of paragraph, or maybe an image
1601 else if (text
== wxT(" ") || text
.empty())
1606 // Next skip current text to space
1607 while (i
< endPos
&& i
> -1)
1609 // i is in character, not caret positions
1610 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1611 if (text
!= wxT(" ") /* && !text.empty() */)
1624 /// Move n words left
1625 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1627 long pos
= FindNextWordPosition(-1);
1628 if (pos
!= m_caretPosition
)
1630 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1632 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1636 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1638 SetDefaultStyleToCursorStyle();
1648 /// Move n words right
1649 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1651 long pos
= FindNextWordPosition(1);
1652 if (pos
!= m_caretPosition
)
1654 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1656 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1660 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1662 SetDefaultStyleToCursorStyle();
1673 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1675 // Only do sizing optimization for large buffers
1676 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1678 m_fullLayoutRequired
= true;
1679 m_fullLayoutTime
= wxGetLocalTimeMillis();
1680 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1681 LayoutContent(true /* onlyVisibleRect */);
1684 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1686 #if wxRICHTEXT_BUFFERED_PAINTING
1694 /// Idle-time processing
1695 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1697 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1699 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1701 m_fullLayoutRequired
= false;
1702 m_fullLayoutTime
= 0;
1703 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1704 ShowPosition(m_fullLayoutSavedPosition
);
1708 if (m_caretPositionForDefaultStyle
!= -2)
1710 // If the caret position has changed, no longer reflect the default style
1712 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1713 m_caretPositionForDefaultStyle
= -2;
1720 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1726 /// Set up scrollbars, e.g. after a resize
1727 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1732 if (GetBuffer().IsEmpty())
1734 SetScrollbars(0, 0, 0, 0, 0, 0);
1738 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1739 // of pixels. See e.g. wxVScrolledWindow for ideas.
1740 int pixelsPerUnit
= 5;
1741 wxSize clientSize
= GetClientSize();
1743 int maxHeight
= GetBuffer().GetCachedSize().y
;
1745 // Round up so we have at least maxHeight pixels
1746 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1748 int startX
= 0, startY
= 0;
1750 GetViewStart(& startX
, & startY
);
1752 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1753 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1755 // Move to previous scroll position if
1757 SetScrollbars(0, pixelsPerUnit
,
1759 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1762 /// Paint the background
1763 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1765 wxColour backgroundColour
= GetBackgroundColour();
1766 if (!backgroundColour
.Ok())
1767 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1769 // Clear the background
1770 dc
.SetBrush(wxBrush(backgroundColour
));
1771 dc
.SetPen(*wxTRANSPARENT_PEN
);
1772 wxRect
windowRect(GetClientSize());
1773 windowRect
.x
-= 2; windowRect
.y
-= 2;
1774 windowRect
.width
+= 4; windowRect
.height
+= 4;
1776 // We need to shift the rectangle to take into account
1777 // scrolling. Converting device to logical coordinates.
1778 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1779 dc
.DrawRectangle(windowRect
);
1782 #if wxRICHTEXT_BUFFERED_PAINTING
1783 /// Recreate buffer bitmap if necessary
1784 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1787 if (sz
== wxDefaultSize
)
1788 sz
= GetClientSize();
1790 if (sz
.x
< 1 || sz
.y
< 1)
1793 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1794 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1795 return m_bufferBitmap
.Ok();
1799 // ----------------------------------------------------------------------------
1800 // file IO functions
1801 // ----------------------------------------------------------------------------
1803 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1805 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1807 m_filename
= filename
;
1810 SetInsertionPoint(0);
1813 SetupScrollbars(true);
1815 SendTextUpdatedEvent();
1821 wxLogError(_("File couldn't be loaded."));
1827 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1829 if (GetBuffer().SaveFile(filename
, fileType
))
1831 m_filename
= filename
;
1838 wxLogError(_("The text couldn't be saved."));
1843 // ----------------------------------------------------------------------------
1844 // wxRichTextCtrl specific functionality
1845 // ----------------------------------------------------------------------------
1847 /// Add a new paragraph of text to the end of the buffer
1848 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1850 return GetBuffer().AddParagraph(text
);
1854 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1856 return GetBuffer().AddImage(image
);
1859 // ----------------------------------------------------------------------------
1860 // selection and ranges
1861 // ----------------------------------------------------------------------------
1863 void wxRichTextCtrl::SelectAll()
1865 SetSelection(0, GetLastPosition()+1);
1866 m_selectionAnchor
= -1;
1870 void wxRichTextCtrl::SelectNone()
1872 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1873 SetSelection(-2, -2);
1874 m_selectionAnchor
= -2;
1877 static bool wxIsWordDelimiter(const wxString
& text
)
1879 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1882 /// Select the word at the given character position
1883 bool wxRichTextCtrl::SelectWord(long position
)
1885 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1888 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1892 long positionStart
= position
;
1893 long positionEnd
= position
;
1895 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1897 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1898 if (wxIsWordDelimiter(text
))
1904 if (positionStart
< para
->GetRange().GetStart())
1905 positionStart
= para
->GetRange().GetStart();
1907 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1909 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1910 if (wxIsWordDelimiter(text
))
1916 if (positionEnd
>= para
->GetRange().GetEnd())
1917 positionEnd
= para
->GetRange().GetEnd();
1919 SetSelection(positionStart
, positionEnd
+1);
1921 if (positionStart
>= 0)
1923 MoveCaret(positionStart
-1, true);
1924 SetDefaultStyleToCursorStyle();
1930 wxString
wxRichTextCtrl::GetStringSelection() const
1933 GetSelection(&from
, &to
);
1935 return GetRange(from
, to
);
1938 // ----------------------------------------------------------------------------
1940 // ----------------------------------------------------------------------------
1942 wxTextCtrlHitTestResult
1943 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1945 // implement in terms of the other overload as the native ports typically
1946 // can get the position and not (x, y) pair directly (although wxUniv
1947 // directly gets x and y -- and so overrides this method as well)
1949 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1951 if ( rc
!= wxTE_HT_UNKNOWN
)
1953 PositionToXY(pos
, x
, y
);
1959 wxTextCtrlHitTestResult
1960 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1963 wxClientDC
dc((wxRichTextCtrl
*) this);
1964 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1966 // Buffer uses logical position (relative to start of buffer)
1968 wxPoint pt2
= GetLogicalPoint(pt
);
1970 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1974 case wxRICHTEXT_HITTEST_BEFORE
:
1975 return wxTE_HT_BEFORE
;
1977 case wxRICHTEXT_HITTEST_AFTER
:
1978 return wxTE_HT_BEYOND
;
1980 case wxRICHTEXT_HITTEST_ON
:
1981 return wxTE_HT_ON_TEXT
;
1984 return wxTE_HT_UNKNOWN
;
1987 // ----------------------------------------------------------------------------
1988 // set/get the controls text
1989 // ----------------------------------------------------------------------------
1991 wxString
wxRichTextCtrl::GetValue() const
1993 return GetBuffer().GetText();
1996 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1998 // Public API for range is different from internals
1999 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2002 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2006 // if the text is long enough, it's faster to just set it instead of first
2007 // comparing it with the old one (chances are that it will be different
2008 // anyhow, this comparison is there to avoid flicker for small single-line
2009 // edit controls mostly)
2010 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
2014 // for compatibility, don't move the cursor when doing SetValue()
2015 SetInsertionPoint(0);
2019 if ( flags
& SetValue_SendEvent
)
2021 // still send an event for consistency
2022 SendTextUpdatedEvent();
2026 // we should reset the modified flag even if the value didn't really change
2028 // mark the control as being not dirty - we changed its text, not the
2033 void wxRichTextCtrl::WriteText(const wxString
& value
)
2038 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2040 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2042 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2044 if ( flags
& SetValue_SendEvent
)
2045 SendTextUpdatedEvent();
2048 void wxRichTextCtrl::AppendText(const wxString
& text
)
2050 SetInsertionPointEnd();
2055 /// Write an image at the current insertion point
2056 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2058 wxRichTextImageBlock imageBlock
;
2060 wxImage image2
= image
;
2061 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2062 return WriteImage(imageBlock
);
2067 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2069 wxRichTextImageBlock imageBlock
;
2072 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2073 return WriteImage(imageBlock
);
2078 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2080 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2083 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2087 wxRichTextImageBlock imageBlock
;
2089 wxImage image
= bitmap
.ConvertToImage();
2090 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2091 return WriteImage(imageBlock
);
2097 /// Insert a newline (actually paragraph) at the current insertion point.
2098 bool wxRichTextCtrl::Newline()
2100 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2103 /// Insert a line break at the current insertion point.
2104 bool wxRichTextCtrl::LineBreak()
2107 text
= wxRichTextLineBreakChar
;
2108 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2111 // ----------------------------------------------------------------------------
2112 // Clipboard operations
2113 // ----------------------------------------------------------------------------
2115 void wxRichTextCtrl::Copy()
2119 wxRichTextRange range
= GetInternalSelectionRange();
2120 GetBuffer().CopyToClipboard(range
);
2124 void wxRichTextCtrl::Cut()
2128 wxRichTextRange range
= GetInternalSelectionRange();
2129 GetBuffer().CopyToClipboard(range
);
2131 DeleteSelectedContent();
2137 void wxRichTextCtrl::Paste()
2141 BeginBatchUndo(_("Paste"));
2143 long newPos
= m_caretPosition
;
2144 DeleteSelectedContent(& newPos
);
2146 GetBuffer().PasteFromClipboard(newPos
);
2152 void wxRichTextCtrl::DeleteSelection()
2154 if (CanDeleteSelection())
2156 DeleteSelectedContent();
2160 bool wxRichTextCtrl::HasSelection() const
2162 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2165 bool wxRichTextCtrl::CanCopy() const
2167 // Can copy if there's a selection
2168 return HasSelection();
2171 bool wxRichTextCtrl::CanCut() const
2173 return HasSelection() && IsEditable();
2176 bool wxRichTextCtrl::CanPaste() const
2178 if ( !IsEditable() )
2181 return GetBuffer().CanPasteFromClipboard();
2184 bool wxRichTextCtrl::CanDeleteSelection() const
2186 return HasSelection() && IsEditable();
2190 // ----------------------------------------------------------------------------
2192 // ----------------------------------------------------------------------------
2194 void wxRichTextCtrl::SetEditable(bool editable
)
2196 m_editable
= editable
;
2199 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2203 m_caretPosition
= pos
- 1;
2206 void wxRichTextCtrl::SetInsertionPointEnd()
2208 long pos
= GetLastPosition();
2209 SetInsertionPoint(pos
);
2212 long wxRichTextCtrl::GetInsertionPoint() const
2214 return m_caretPosition
+1;
2217 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2219 return GetBuffer().GetRange().GetEnd();
2222 // If the return values from and to are the same, there is no
2224 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2226 *from
= m_selectionRange
.GetStart();
2227 *to
= m_selectionRange
.GetEnd();
2228 if ((*to
) != -1 && (*to
) != -2)
2232 bool wxRichTextCtrl::IsEditable() const
2237 // ----------------------------------------------------------------------------
2239 // ----------------------------------------------------------------------------
2241 void wxRichTextCtrl::SetSelection(long from
, long to
)
2243 // if from and to are both -1, it means (in wxWidgets) that all text should
2245 if ( (from
== -1) && (to
== -1) )
2248 to
= GetLastPosition()+1;
2251 DoSetSelection(from
, to
);
2254 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2256 m_selectionAnchor
= from
;
2257 m_selectionRange
.SetRange(from
, to
-1);
2259 m_caretPosition
= from
-1;
2265 // ----------------------------------------------------------------------------
2267 // ----------------------------------------------------------------------------
2269 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2270 const wxString
& value
)
2272 BeginBatchUndo(_("Replace"));
2274 DeleteSelectedContent();
2276 DoWriteText(value
, SetValue_SelectionOnly
);
2281 void wxRichTextCtrl::Remove(long from
, long to
)
2285 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2292 bool wxRichTextCtrl::IsModified() const
2294 return m_buffer
.IsModified();
2297 void wxRichTextCtrl::MarkDirty()
2299 m_buffer
.Modify(true);
2302 void wxRichTextCtrl::DiscardEdits()
2304 m_caretPositionForDefaultStyle
= -2;
2305 m_buffer
.Modify(false);
2306 m_buffer
.GetCommandProcessor()->ClearCommands();
2309 int wxRichTextCtrl::GetNumberOfLines() const
2311 return GetBuffer().GetParagraphCount();
2314 // ----------------------------------------------------------------------------
2315 // Positions <-> coords
2316 // ----------------------------------------------------------------------------
2318 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2320 return GetBuffer().XYToPosition(x
, y
);
2323 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2325 return GetBuffer().PositionToXY(pos
, x
, y
);
2328 // ----------------------------------------------------------------------------
2330 // ----------------------------------------------------------------------------
2332 void wxRichTextCtrl::ShowPosition(long pos
)
2334 if (!IsPositionVisible(pos
))
2335 ScrollIntoView(pos
-1, WXK_DOWN
);
2338 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2340 return GetBuffer().GetParagraphLength(lineNo
);
2343 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2345 return GetBuffer().GetParagraphText(lineNo
);
2348 // ----------------------------------------------------------------------------
2350 // ----------------------------------------------------------------------------
2352 void wxRichTextCtrl::Undo()
2356 GetCommandProcessor()->Undo();
2360 void wxRichTextCtrl::Redo()
2364 GetCommandProcessor()->Redo();
2368 bool wxRichTextCtrl::CanUndo() const
2370 return GetCommandProcessor()->CanUndo();
2373 bool wxRichTextCtrl::CanRedo() const
2375 return GetCommandProcessor()->CanRedo();
2378 // ----------------------------------------------------------------------------
2379 // implementation details
2380 // ----------------------------------------------------------------------------
2382 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2384 SetValue(event
.GetString());
2385 GetEventHandler()->ProcessEvent(event
);
2388 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2390 // By default, load the first file into the text window.
2391 if (event
.GetNumberOfFiles() > 0)
2393 LoadFile(event
.GetFiles()[0]);
2397 wxSize
wxRichTextCtrl::DoGetBestSize() const
2399 return wxSize(10, 10);
2402 // ----------------------------------------------------------------------------
2403 // standard handlers for standard edit menu events
2404 // ----------------------------------------------------------------------------
2406 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2411 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2416 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2421 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2426 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2431 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2436 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2438 event
.Enable( CanCut() );
2441 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2443 event
.Enable( CanCopy() );
2446 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2448 event
.Enable( CanDeleteSelection() );
2451 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2453 event
.Enable( CanPaste() );
2456 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2458 event
.Enable( CanUndo() );
2459 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2462 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2464 event
.Enable( CanRedo() );
2465 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2468 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2473 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2475 event
.Enable(GetLastPosition() > 0);
2478 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2482 m_contextMenu
= new wxMenu
;
2483 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2484 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2485 m_contextMenu
->AppendSeparator();
2486 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2487 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2488 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2489 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2490 m_contextMenu
->AppendSeparator();
2491 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2493 PopupMenu(m_contextMenu
);
2497 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2499 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2502 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2504 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2507 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2509 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2512 // extended style setting operation with flags including:
2513 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2514 // see richtextbuffer.h for more details.
2515 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2517 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2520 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2522 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2525 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2527 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2530 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2532 return GetBuffer().SetDefaultStyle(style
);
2535 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2537 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2540 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2542 return GetBuffer().GetDefaultStyle();
2545 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2547 return GetBuffer().GetDefaultStyle();
2550 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2552 wxTextAttrEx
attr(style
);
2553 if (GetBuffer().GetStyle(position
, attr
))
2562 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2564 return GetBuffer().GetStyle(position
, style
);
2567 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2569 return GetBuffer().GetStyle(position
, style
);
2572 // get the common set of styles for the range
2573 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2575 wxTextAttrEx styleEx
;
2576 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2585 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2587 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2590 /// Get the content (uncombined) attributes for this position.
2592 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2594 wxTextAttrEx
attr(style
);
2595 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2604 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2606 return GetBuffer().GetUncombinedStyle(position
, style
);
2609 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2611 return GetBuffer().GetUncombinedStyle(position
, style
);
2614 /// Set font, and also the buffer attributes
2615 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2617 wxControl::SetFont(font
);
2619 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2621 GetBuffer().SetBasicStyle(attr
);
2623 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2629 /// Transform logical to physical
2630 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2633 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2638 /// Transform physical to logical
2639 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2642 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2647 /// Position the caret
2648 void wxRichTextCtrl::PositionCaret()
2653 //wxLogDebug(wxT("PositionCaret"));
2656 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2658 wxPoint originalPt
= caretRect
.GetPosition();
2659 wxPoint pt
= GetPhysicalPoint(originalPt
);
2660 if (GetCaret()->GetPosition() != pt
)
2662 GetCaret()->Move(pt
);
2663 GetCaret()->SetSize(caretRect
.GetSize());
2668 /// Get the caret height and position for the given character position
2669 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2671 wxClientDC
dc(this);
2672 dc
.SetFont(GetFont());
2679 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2681 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2688 /// Gets the line for the visible caret position. If the caret is
2689 /// shown at the very end of the line, it means the next character is actually
2690 /// on the following line. So let's get the line we're expecting to find
2691 /// if this is the case.
2692 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2694 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2695 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2698 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2699 if (caretPosition
== lineRange
.GetStart()-1 &&
2700 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2702 if (!m_caretAtLineStart
)
2703 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2710 /// Move the caret to the given character position
2711 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2713 if (GetBuffer().GetDirty())
2716 if (pos
<= GetBuffer().GetRange().GetEnd())
2718 SetCaretPosition(pos
, showAtLineStart
);
2728 /// Layout the buffer: which we must do before certain operations, such as
2729 /// setting the caret position.
2730 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2732 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2734 wxRect
availableSpace(GetClientSize());
2735 if (availableSpace
.width
== 0)
2736 availableSpace
.width
= 10;
2737 if (availableSpace
.height
== 0)
2738 availableSpace
.height
= 10;
2740 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2741 if (onlyVisibleRect
)
2743 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2744 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2747 wxClientDC
dc(this);
2748 dc
.SetFont(GetFont());
2752 GetBuffer().Defragment();
2753 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2754 GetBuffer().Layout(dc
, availableSpace
, flags
);
2755 GetBuffer().SetDirty(false);
2764 /// Is all of the selection bold?
2765 bool wxRichTextCtrl::IsSelectionBold()
2769 wxRichTextAttr attr
;
2770 wxRichTextRange range
= GetSelectionRange();
2771 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2772 attr
.SetFontWeight(wxBOLD
);
2774 return HasCharacterAttributes(range
, attr
);
2778 // If no selection, then we need to combine current style with default style
2779 // to see what the effect would be if we started typing.
2780 wxRichTextAttr attr
;
2781 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2783 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2784 if (GetStyle(pos
, attr
))
2786 if (IsDefaultStyleShowing())
2787 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2788 return attr
.GetFontWeight() == wxBOLD
;
2794 /// Is all of the selection italics?
2795 bool wxRichTextCtrl::IsSelectionItalics()
2799 wxRichTextRange range
= GetSelectionRange();
2800 wxRichTextAttr attr
;
2801 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2802 attr
.SetFontStyle(wxITALIC
);
2804 return HasCharacterAttributes(range
, attr
);
2808 // If no selection, then we need to combine current style with default style
2809 // to see what the effect would be if we started typing.
2810 wxRichTextAttr attr
;
2811 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2813 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2814 if (GetStyle(pos
, attr
))
2816 if (IsDefaultStyleShowing())
2817 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2818 return attr
.GetFontStyle() == wxITALIC
;
2824 /// Is all of the selection underlined?
2825 bool wxRichTextCtrl::IsSelectionUnderlined()
2829 wxRichTextRange range
= GetSelectionRange();
2830 wxRichTextAttr attr
;
2831 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2832 attr
.SetFontUnderlined(true);
2834 return HasCharacterAttributes(range
, attr
);
2838 // If no selection, then we need to combine current style with default style
2839 // to see what the effect would be if we started typing.
2840 wxRichTextAttr attr
;
2841 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2842 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2844 if (GetStyle(pos
, attr
))
2846 if (IsDefaultStyleShowing())
2847 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2848 return attr
.GetFontUnderlined();
2854 /// Apply bold to the selection
2855 bool wxRichTextCtrl::ApplyBoldToSelection()
2857 wxRichTextAttr attr
;
2858 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2859 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2862 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2864 SetAndShowDefaultStyle(attr
);
2868 /// Apply italic to the selection
2869 bool wxRichTextCtrl::ApplyItalicToSelection()
2871 wxRichTextAttr attr
;
2872 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2873 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2876 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2878 SetAndShowDefaultStyle(attr
);
2882 /// Apply underline to the selection
2883 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2885 wxRichTextAttr attr
;
2886 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2887 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2890 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2892 SetAndShowDefaultStyle(attr
);
2896 /// Is all of the selection aligned according to the specified flag?
2897 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2899 wxRichTextRange range
;
2901 range
= GetSelectionRange();
2903 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2905 wxRichTextAttr attr
;
2906 attr
.SetAlignment(alignment
);
2908 return HasParagraphAttributes(range
, attr
);
2911 /// Apply alignment to the selection
2912 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2914 wxRichTextAttr attr
;
2915 attr
.SetAlignment(alignment
);
2917 return SetStyle(GetSelectionRange(), attr
);
2920 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2922 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2927 /// Apply a named style to the selection
2928 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2930 // Flags are defined within each definition, so only certain
2931 // attributes are applied.
2932 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2934 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2936 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2938 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2940 wxRichTextRange range
;
2943 range
= GetSelectionRange();
2946 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2947 range
= wxRichTextRange(pos
, pos
+1);
2950 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2953 // Make sure the attr has the style name
2954 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2956 attr
.SetParagraphStyleName(def
->GetName());
2958 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2959 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2960 // to change its style independently.
2961 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2964 attr
.SetCharacterStyleName(def
->GetName());
2967 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2970 SetAndShowDefaultStyle(attr
);
2975 /// Apply the style sheet to the buffer, for example if the styles have changed.
2976 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2979 styleSheet
= GetBuffer().GetStyleSheet();
2983 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2985 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2993 /// Sets the default style to the style under the cursor
2994 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2997 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2999 // If at the start of a paragraph, use the next position.
3000 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3002 if (GetUncombinedStyle(pos
, attr
))
3004 SetDefaultStyle(attr
);
3011 /// Returns the first visible position in the current view
3012 long wxRichTextCtrl::GetFirstVisiblePosition() const
3014 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3016 return line
->GetAbsoluteRange().GetStart();
3021 /// Get the first visible point in the window
3022 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3025 int startXUnits
, startYUnits
;
3027 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3028 GetViewStart(& startXUnits
, & startYUnits
);
3030 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3033 /// The adjusted caret position is the character position adjusted to take
3034 /// into account whether we're at the start of a paragraph, in which case
3035 /// style information should be taken from the next position, not current one.
3036 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3038 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3040 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3045 /// Get/set the selection range in character positions. -1, -1 means no selection.
3046 /// The range is in API convention, i.e. a single character selection is denoted
3048 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3050 wxRichTextRange range
= GetInternalSelectionRange();
3051 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3052 range
.SetEnd(range
.GetEnd() + 1);
3056 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3058 wxRichTextRange
range1(range
);
3059 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3060 range1
.SetEnd(range1
.GetEnd() - 1);
3062 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3064 SetInternalSelectionRange(range1
);
3068 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3070 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3073 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3075 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3078 /// Clear list for given range
3079 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3081 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3084 /// Number/renumber any list elements in the given range
3085 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3087 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3090 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3092 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3095 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3096 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3098 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3101 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3103 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3106 /// Deletes the content in the given range
3107 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3109 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3112 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3114 if (sm_availableFontNames
.GetCount() == 0)
3116 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3117 sm_availableFontNames
.Sort();
3119 return sm_availableFontNames
;
3122 void wxRichTextCtrl::ClearAvailableFontNames()
3124 sm_availableFontNames
.Clear();