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 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
561 SetDefaultStyleToCursorStyle();
563 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
565 wxRichTextEvent
cmdEvent(
566 wxEVT_COMMAND_RICHTEXT_RETURN
,
568 cmdEvent
.SetEventObject(this);
569 cmdEvent
.SetFlags(flags
);
570 cmdEvent
.SetPosition(newPos
+1);
572 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
574 // Generate conventional event
575 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
576 InitCommandEvent(textEvent
);
578 GetEventHandler()->ProcessEvent(textEvent
);
582 else if (event
.GetKeyCode() == WXK_BACK
)
584 BeginBatchUndo(_("Delete Text"));
586 // Submit range in character positions, which are greater than caret positions,
587 // so subtract 1 for deleted character and add 1 for conversion to character position.
588 if (m_caretPosition
> -1 && !HasSelection())
590 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
593 DeleteSelectedContent();
597 if (GetLastPosition() == -1)
601 m_caretPosition
= -1;
603 SetDefaultStyleToCursorStyle();
606 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
608 wxRichTextEvent
cmdEvent(
609 wxEVT_COMMAND_RICHTEXT_DELETE
,
611 cmdEvent
.SetEventObject(this);
612 cmdEvent
.SetFlags(flags
);
613 cmdEvent
.SetPosition(m_caretPosition
+1);
614 GetEventHandler()->ProcessEvent(cmdEvent
);
618 else if (event
.GetKeyCode() == WXK_DELETE
)
620 BeginBatchUndo(_("Delete Text"));
622 // Submit range in character positions, which are greater than caret positions,
623 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
625 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
628 DeleteSelectedContent();
632 if (GetLastPosition() == -1)
636 m_caretPosition
= -1;
638 SetDefaultStyleToCursorStyle();
641 wxRichTextEvent
cmdEvent(
642 wxEVT_COMMAND_RICHTEXT_DELETE
,
644 cmdEvent
.SetEventObject(this);
645 cmdEvent
.SetFlags(flags
);
646 cmdEvent
.SetPosition(m_caretPosition
+1);
647 GetEventHandler()->ProcessEvent(cmdEvent
);
653 long keycode
= event
.GetKeyCode();
727 case WXK_NUMPAD_SPACE
:
729 case WXK_NUMPAD_ENTER
:
734 case WXK_NUMPAD_HOME
:
735 case WXK_NUMPAD_LEFT
:
737 case WXK_NUMPAD_RIGHT
:
738 case WXK_NUMPAD_DOWN
:
739 case WXK_NUMPAD_PAGEUP
:
740 case WXK_NUMPAD_PAGEDOWN
:
742 case WXK_NUMPAD_BEGIN
:
743 case WXK_NUMPAD_INSERT
:
744 case WXK_NUMPAD_DELETE
:
745 case WXK_NUMPAD_EQUAL
:
746 case WXK_NUMPAD_MULTIPLY
:
748 case WXK_NUMPAD_SEPARATOR
:
749 case WXK_NUMPAD_SUBTRACT
:
750 case WXK_NUMPAD_DECIMAL
:
758 if (event
.CmdDown() || event
.AltDown())
764 wxRichTextEvent
cmdEvent(
765 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
767 cmdEvent
.SetEventObject(this);
768 cmdEvent
.SetFlags(flags
);
769 cmdEvent
.SetCharacter((wxChar
) keycode
);
770 cmdEvent
.SetPosition(m_caretPosition
+1);
772 if (keycode
== wxT('\t'))
774 // See if we need to promote or demote the selection or paragraph at the cursor
775 // position, instead of inserting a tab.
776 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
777 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
778 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
780 wxRichTextRange range
;
782 range
= GetSelectionRange();
784 range
= para
->GetRange().FromInternal();
786 int promoteBy
= event
.ShiftDown() ? 1 : -1;
788 PromoteList(promoteBy
, range
, NULL
);
790 GetEventHandler()->ProcessEvent(cmdEvent
);
796 BeginBatchUndo(_("Insert Text"));
798 long newPos
= m_caretPosition
;
799 DeleteSelectedContent(& newPos
);
801 wxString str
= (wxChar
) event
.GetKeyCode();
802 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
806 SetDefaultStyleToCursorStyle();
807 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
809 GetEventHandler()->ProcessEvent(cmdEvent
);
817 /// Delete content if there is a selection, e.g. when pressing a key.
818 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
822 long pos
= m_selectionRange
.GetStart();
823 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
824 m_selectionRange
.SetRange(-2, -2);
834 /// Keyboard navigation
838 Left: left one character
839 Right: right one character
842 Ctrl-Left: left one word
843 Ctrl-Right: right one word
844 Ctrl-Up: previous paragraph start
845 Ctrl-Down: next start of paragraph
848 Ctrl-Home: start of document
849 Ctrl-End: end of document
851 Page-Down: Down a screen
855 Ctrl-Alt-PgUp: Start of window
856 Ctrl-Alt-PgDn: End of window
857 F8: Start selection mode
858 Esc: End selection mode
860 Adding Shift does the above but starts/extends selection.
865 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
867 bool success
= false;
869 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
871 if (flags
& wxRICHTEXT_CTRL_DOWN
)
872 success
= WordRight(1, flags
);
874 success
= MoveRight(1, flags
);
876 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
878 if (flags
& wxRICHTEXT_CTRL_DOWN
)
879 success
= WordLeft(1, flags
);
881 success
= MoveLeft(1, flags
);
883 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
885 if (flags
& wxRICHTEXT_CTRL_DOWN
)
886 success
= MoveToParagraphStart(flags
);
888 success
= MoveUp(1, flags
);
890 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
892 if (flags
& wxRICHTEXT_CTRL_DOWN
)
893 success
= MoveToParagraphEnd(flags
);
895 success
= MoveDown(1, flags
);
897 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
899 success
= PageUp(1, flags
);
901 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
903 success
= PageDown(1, flags
);
905 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
907 if (flags
& wxRICHTEXT_CTRL_DOWN
)
908 success
= MoveHome(flags
);
910 success
= MoveToLineStart(flags
);
912 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
914 if (flags
& wxRICHTEXT_CTRL_DOWN
)
915 success
= MoveEnd(flags
);
917 success
= MoveToLineEnd(flags
);
922 ScrollIntoView(m_caretPosition
, keyCode
);
923 SetDefaultStyleToCursorStyle();
929 /// Extend the selection. Selections are in caret positions.
930 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
932 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
934 // If not currently selecting, start selecting
935 if (m_selectionRange
.GetStart() == -2)
937 m_selectionAnchor
= oldPos
;
940 m_selectionRange
.SetRange(newPos
+1, oldPos
);
942 m_selectionRange
.SetRange(oldPos
+1, newPos
);
946 // Always ensure that the selection range start is greater than
948 if (newPos
> m_selectionAnchor
)
949 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
951 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
954 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
956 wxLogDebug(wxT("Strange selection range"));
965 /// Scroll into view, returning true if we scrolled.
966 /// This takes a _caret_ position.
967 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
969 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
975 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
977 int startXUnits
, startYUnits
;
978 GetViewStart(& startXUnits
, & startYUnits
);
979 int startY
= startYUnits
* ppuY
;
982 GetVirtualSize(& sx
, & sy
);
988 wxRect rect
= line
->GetRect();
990 bool scrolled
= false;
992 wxSize clientSize
= GetClientSize();
995 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
996 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
997 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
998 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1000 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1002 // Make it scroll so this item is at the bottom
1004 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1005 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1007 // If we're still off the screen, scroll another line down
1008 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1011 if (startYUnits
!= yUnits
)
1013 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1017 else if (rect
.y
< startY
)
1019 // Make it scroll so this item is at the top
1022 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1024 if (startYUnits
!= yUnits
)
1026 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1032 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1033 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1034 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1035 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1037 if (rect
.y
< startY
)
1039 // Make it scroll so this item is at the top
1042 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1044 if (startYUnits
!= yUnits
)
1046 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1050 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1052 // Make it scroll so this item is at the bottom
1054 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1055 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1057 // If we're still off the screen, scroll another line down
1058 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1061 if (startYUnits
!= yUnits
)
1063 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1073 /// Is the given position visible on the screen?
1074 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1076 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1082 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1085 GetViewStart(& startX
, & startY
);
1087 startY
= startY
* ppuY
;
1090 GetVirtualSize(& sx
, & sy
);
1095 wxRect rect
= line
->GetRect();
1097 wxSize clientSize
= GetClientSize();
1099 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1102 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1104 m_caretPosition
= position
;
1105 m_caretAtLineStart
= showAtLineStart
;
1108 /// Move caret one visual step forward: this may mean setting a flag
1109 /// and keeping the same position if we're going from the end of one line
1110 /// to the start of the next, which may be the exact same caret position.
1111 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1113 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1115 // Only do the check if we're not at the end of the paragraph (where things work OK
1117 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1119 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1123 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1125 // We're at the end of a line. See whether we need to
1126 // stay at the same actual caret position but change visual
1127 // position, or not.
1128 if (oldPosition
== lineRange
.GetEnd())
1130 if (m_caretAtLineStart
)
1132 // We're already at the start of the line, so actually move on now.
1133 m_caretPosition
= oldPosition
+ 1;
1134 m_caretAtLineStart
= false;
1138 // We're showing at the end of the line, so keep to
1139 // the same position but indicate that we're to show
1140 // at the start of the next line.
1141 m_caretPosition
= oldPosition
;
1142 m_caretAtLineStart
= true;
1144 SetDefaultStyleToCursorStyle();
1150 SetDefaultStyleToCursorStyle();
1153 /// Move caret one visual step backward: this may mean setting a flag
1154 /// and keeping the same position if we're going from the end of one line
1155 /// to the start of the next, which may be the exact same caret position.
1156 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1158 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1160 // Only do the check if we're not at the start of the paragraph (where things work OK
1162 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1164 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1168 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1170 // We're at the start of a line. See whether we need to
1171 // stay at the same actual caret position but change visual
1172 // position, or not.
1173 if (oldPosition
== lineRange
.GetStart())
1175 m_caretPosition
= oldPosition
-1;
1176 m_caretAtLineStart
= true;
1179 else if (oldPosition
== lineRange
.GetEnd())
1181 if (m_caretAtLineStart
)
1183 // We're at the start of the line, so keep the same caret position
1184 // but clear the start-of-line flag.
1185 m_caretPosition
= oldPosition
;
1186 m_caretAtLineStart
= false;
1190 // We're showing at the end of the line, so go back
1191 // to the previous character position.
1192 m_caretPosition
= oldPosition
- 1;
1194 SetDefaultStyleToCursorStyle();
1200 SetDefaultStyleToCursorStyle();
1204 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1206 long endPos
= GetBuffer().GetRange().GetEnd();
1208 if (m_caretPosition
+ noPositions
< endPos
)
1210 long oldPos
= m_caretPosition
;
1211 long newPos
= m_caretPosition
+ noPositions
;
1213 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1217 // Determine by looking at oldPos and m_caretPosition whether
1218 // we moved from the end of a line to the start of the next line, in which case
1219 // we want to adjust the caret position such that it is positioned at the
1220 // start of the next line, rather than jumping past the first character of the
1222 if (noPositions
== 1 && !extendSel
)
1223 MoveCaretForward(oldPos
);
1225 SetCaretPosition(newPos
);
1228 SetDefaultStyleToCursorStyle();
1239 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1243 if (m_caretPosition
> startPos
- noPositions
+ 1)
1245 long oldPos
= m_caretPosition
;
1246 long newPos
= m_caretPosition
- noPositions
;
1247 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1251 if (noPositions
== 1 && !extendSel
)
1252 MoveCaretBack(oldPos
);
1254 SetCaretPosition(newPos
);
1257 SetDefaultStyleToCursorStyle();
1268 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1270 return MoveDown(- noLines
, flags
);
1274 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1279 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1280 wxPoint pt
= GetCaret()->GetPosition();
1281 long newLine
= lineNumber
+ noLines
;
1283 if (lineNumber
!= -1)
1287 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1289 if (newLine
> lastLine
)
1299 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1302 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1308 wxClientDC
dc(this);
1310 dc
.SetFont(GetFont());
1312 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1314 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1316 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1317 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1318 // so we view the caret at the start of the line.
1319 bool caretLineStart
= false;
1320 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1322 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1323 wxRichTextRange lineRange
;
1325 lineRange
= thisLine
->GetAbsoluteRange();
1327 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1330 caretLineStart
= true;
1334 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1335 if (para
&& para
->GetRange().GetStart() == newPos
)
1340 long newSelEnd
= newPos
;
1342 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1346 SetCaretPosition(newPos
, caretLineStart
);
1348 SetDefaultStyleToCursorStyle();
1358 /// Move to the end of the paragraph
1359 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1361 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1364 long newPos
= para
->GetRange().GetEnd() - 1;
1365 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1369 SetCaretPosition(newPos
);
1371 SetDefaultStyleToCursorStyle();
1381 /// Move to the start of the paragraph
1382 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1384 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1387 long newPos
= para
->GetRange().GetStart() - 1;
1388 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1392 SetCaretPosition(newPos
);
1394 SetDefaultStyleToCursorStyle();
1404 /// Move to the end of the line
1405 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1407 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1411 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1412 long newPos
= lineRange
.GetEnd();
1413 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1417 SetCaretPosition(newPos
);
1419 SetDefaultStyleToCursorStyle();
1429 /// Move to the start of the line
1430 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1432 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1435 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1436 long newPos
= lineRange
.GetStart()-1;
1438 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1442 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1444 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1446 SetDefaultStyleToCursorStyle();
1456 /// Move to the start of the buffer
1457 bool wxRichTextCtrl::MoveHome(int flags
)
1459 if (m_caretPosition
!= -1)
1461 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1465 SetCaretPosition(-1);
1467 SetDefaultStyleToCursorStyle();
1477 /// Move to the end of the buffer
1478 bool wxRichTextCtrl::MoveEnd(int flags
)
1480 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1482 if (m_caretPosition
!= endPos
)
1484 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1488 SetCaretPosition(endPos
);
1490 SetDefaultStyleToCursorStyle();
1500 /// Move noPages pages up
1501 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1503 return PageDown(- noPages
, flags
);
1506 /// Move noPages pages down
1507 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1509 // Calculate which line occurs noPages * screen height further down.
1510 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1513 wxSize clientSize
= GetClientSize();
1514 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1516 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1519 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1520 long pos
= lineRange
.GetStart()-1;
1521 if (pos
!= m_caretPosition
)
1523 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1525 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1529 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1531 SetDefaultStyleToCursorStyle();
1543 // Finds the caret position for the next word
1544 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1546 long endPos
= GetBuffer().GetRange().GetEnd();
1550 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1552 // First skip current text to space
1553 while (i
< endPos
&& i
> -1)
1555 // i is in character, not caret positions
1556 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1557 if (text
!= wxT(" ") && !text
.empty())
1564 while (i
< endPos
&& i
> -1)
1566 // i is in character, not caret positions
1567 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1568 if (text
.empty()) // End of paragraph, or maybe an image
1569 return wxMax(-1, i
- 1);
1570 else if (text
== wxT(" ") || text
.empty())
1574 // Convert to caret position
1575 return wxMax(-1, i
- 1);
1584 long i
= m_caretPosition
;
1586 // First skip white space
1587 while (i
< endPos
&& i
> -1)
1589 // i is in character, not caret positions
1590 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1591 if (text
.empty()) // End of paragraph, or maybe an image
1593 else if (text
== wxT(" ") || text
.empty())
1598 // Next skip current text to space
1599 while (i
< endPos
&& i
> -1)
1601 // i is in character, not caret positions
1602 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1603 if (text
!= wxT(" ") /* && !text.empty() */)
1616 /// Move n words left
1617 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1619 long pos
= FindNextWordPosition(-1);
1620 if (pos
!= m_caretPosition
)
1622 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1624 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1628 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1630 SetDefaultStyleToCursorStyle();
1640 /// Move n words right
1641 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1643 long pos
= FindNextWordPosition(1);
1644 if (pos
!= m_caretPosition
)
1646 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1648 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1652 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1654 SetDefaultStyleToCursorStyle();
1665 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1667 // Only do sizing optimization for large buffers
1668 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1670 m_fullLayoutRequired
= true;
1671 m_fullLayoutTime
= wxGetLocalTimeMillis();
1672 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1673 LayoutContent(true /* onlyVisibleRect */);
1676 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1678 #if wxRICHTEXT_BUFFERED_PAINTING
1686 /// Idle-time processing
1687 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1689 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1691 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1693 m_fullLayoutRequired
= false;
1694 m_fullLayoutTime
= 0;
1695 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1696 ShowPosition(m_fullLayoutSavedPosition
);
1700 if (m_caretPositionForDefaultStyle
!= -2)
1702 // If the caret position has changed, no longer reflect the default style
1704 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1705 m_caretPositionForDefaultStyle
= -2;
1712 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1718 /// Set up scrollbars, e.g. after a resize
1719 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1724 if (GetBuffer().IsEmpty())
1726 SetScrollbars(0, 0, 0, 0, 0, 0);
1730 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1731 // of pixels. See e.g. wxVScrolledWindow for ideas.
1732 int pixelsPerUnit
= 5;
1733 wxSize clientSize
= GetClientSize();
1735 int maxHeight
= GetBuffer().GetCachedSize().y
;
1737 // Round up so we have at least maxHeight pixels
1738 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1740 int startX
= 0, startY
= 0;
1742 GetViewStart(& startX
, & startY
);
1744 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1745 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1747 // Move to previous scroll position if
1749 SetScrollbars(0, pixelsPerUnit
,
1751 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1754 /// Paint the background
1755 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1757 wxColour backgroundColour
= GetBackgroundColour();
1758 if (!backgroundColour
.Ok())
1759 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1761 // Clear the background
1762 dc
.SetBrush(wxBrush(backgroundColour
));
1763 dc
.SetPen(*wxTRANSPARENT_PEN
);
1764 wxRect
windowRect(GetClientSize());
1765 windowRect
.x
-= 2; windowRect
.y
-= 2;
1766 windowRect
.width
+= 4; windowRect
.height
+= 4;
1768 // We need to shift the rectangle to take into account
1769 // scrolling. Converting device to logical coordinates.
1770 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1771 dc
.DrawRectangle(windowRect
);
1774 #if wxRICHTEXT_BUFFERED_PAINTING
1775 /// Recreate buffer bitmap if necessary
1776 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1779 if (sz
== wxDefaultSize
)
1780 sz
= GetClientSize();
1782 if (sz
.x
< 1 || sz
.y
< 1)
1785 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1786 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1787 return m_bufferBitmap
.Ok();
1791 // ----------------------------------------------------------------------------
1792 // file IO functions
1793 // ----------------------------------------------------------------------------
1795 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1797 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1799 m_filename
= filename
;
1802 SetInsertionPoint(0);
1805 SetupScrollbars(true);
1807 SendTextUpdatedEvent();
1813 wxLogError(_("File couldn't be loaded."));
1819 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1821 if (GetBuffer().SaveFile(filename
, fileType
))
1823 m_filename
= filename
;
1830 wxLogError(_("The text couldn't be saved."));
1835 // ----------------------------------------------------------------------------
1836 // wxRichTextCtrl specific functionality
1837 // ----------------------------------------------------------------------------
1839 /// Add a new paragraph of text to the end of the buffer
1840 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1842 return GetBuffer().AddParagraph(text
);
1846 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1848 return GetBuffer().AddImage(image
);
1851 // ----------------------------------------------------------------------------
1852 // selection and ranges
1853 // ----------------------------------------------------------------------------
1855 void wxRichTextCtrl::SelectAll()
1857 SetSelection(0, GetLastPosition()+1);
1858 m_selectionAnchor
= -1;
1862 void wxRichTextCtrl::SelectNone()
1864 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1865 SetSelection(-2, -2);
1866 m_selectionAnchor
= -2;
1869 static bool wxIsWordDelimiter(const wxString
& text
)
1871 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1874 /// Select the word at the given character position
1875 bool wxRichTextCtrl::SelectWord(long position
)
1877 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1880 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1884 long positionStart
= position
;
1885 long positionEnd
= position
;
1887 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1889 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1890 if (wxIsWordDelimiter(text
))
1896 if (positionStart
< para
->GetRange().GetStart())
1897 positionStart
= para
->GetRange().GetStart();
1899 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1901 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1902 if (wxIsWordDelimiter(text
))
1908 if (positionEnd
>= para
->GetRange().GetEnd())
1909 positionEnd
= para
->GetRange().GetEnd();
1911 SetSelection(positionStart
, positionEnd
+1);
1913 if (positionStart
>= 0)
1915 MoveCaret(positionStart
-1, true);
1916 SetDefaultStyleToCursorStyle();
1922 wxString
wxRichTextCtrl::GetStringSelection() const
1925 GetSelection(&from
, &to
);
1927 return GetRange(from
, to
);
1930 // ----------------------------------------------------------------------------
1932 // ----------------------------------------------------------------------------
1934 wxTextCtrlHitTestResult
1935 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1937 // implement in terms of the other overload as the native ports typically
1938 // can get the position and not (x, y) pair directly (although wxUniv
1939 // directly gets x and y -- and so overrides this method as well)
1941 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1943 if ( rc
!= wxTE_HT_UNKNOWN
)
1945 PositionToXY(pos
, x
, y
);
1951 wxTextCtrlHitTestResult
1952 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1955 wxClientDC
dc((wxRichTextCtrl
*) this);
1956 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1958 // Buffer uses logical position (relative to start of buffer)
1960 wxPoint pt2
= GetLogicalPoint(pt
);
1962 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1966 case wxRICHTEXT_HITTEST_BEFORE
:
1967 return wxTE_HT_BEFORE
;
1969 case wxRICHTEXT_HITTEST_AFTER
:
1970 return wxTE_HT_BEYOND
;
1972 case wxRICHTEXT_HITTEST_ON
:
1973 return wxTE_HT_ON_TEXT
;
1976 return wxTE_HT_UNKNOWN
;
1979 // ----------------------------------------------------------------------------
1980 // set/get the controls text
1981 // ----------------------------------------------------------------------------
1983 wxString
wxRichTextCtrl::GetValue() const
1985 return GetBuffer().GetText();
1988 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1990 // Public API for range is different from internals
1991 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1994 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1998 // if the text is long enough, it's faster to just set it instead of first
1999 // comparing it with the old one (chances are that it will be different
2000 // anyhow, this comparison is there to avoid flicker for small single-line
2001 // edit controls mostly)
2002 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
2006 // for compatibility, don't move the cursor when doing SetValue()
2007 SetInsertionPoint(0);
2011 if ( flags
& SetValue_SendEvent
)
2013 // still send an event for consistency
2014 SendTextUpdatedEvent();
2018 // we should reset the modified flag even if the value didn't really change
2020 // mark the control as being not dirty - we changed its text, not the
2025 void wxRichTextCtrl::WriteText(const wxString
& value
)
2030 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2032 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2034 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
2036 if ( flags
& SetValue_SendEvent
)
2037 SendTextUpdatedEvent();
2040 void wxRichTextCtrl::AppendText(const wxString
& text
)
2042 SetInsertionPointEnd();
2047 /// Write an image at the current insertion point
2048 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2050 wxRichTextImageBlock imageBlock
;
2052 wxImage image2
= image
;
2053 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2054 return WriteImage(imageBlock
);
2059 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2061 wxRichTextImageBlock imageBlock
;
2064 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2065 return WriteImage(imageBlock
);
2070 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2072 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2075 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2079 wxRichTextImageBlock imageBlock
;
2081 wxImage image
= bitmap
.ConvertToImage();
2082 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2083 return WriteImage(imageBlock
);
2089 /// Insert a newline (actually paragraph) at the current insertion point.
2090 bool wxRichTextCtrl::Newline()
2092 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2096 // ----------------------------------------------------------------------------
2097 // Clipboard operations
2098 // ----------------------------------------------------------------------------
2100 void wxRichTextCtrl::Copy()
2104 wxRichTextRange range
= GetInternalSelectionRange();
2105 GetBuffer().CopyToClipboard(range
);
2109 void wxRichTextCtrl::Cut()
2113 wxRichTextRange range
= GetInternalSelectionRange();
2114 GetBuffer().CopyToClipboard(range
);
2116 DeleteSelectedContent();
2122 void wxRichTextCtrl::Paste()
2126 BeginBatchUndo(_("Paste"));
2128 long newPos
= m_caretPosition
;
2129 DeleteSelectedContent(& newPos
);
2131 GetBuffer().PasteFromClipboard(newPos
);
2137 void wxRichTextCtrl::DeleteSelection()
2139 if (CanDeleteSelection())
2141 DeleteSelectedContent();
2145 bool wxRichTextCtrl::HasSelection() const
2147 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2150 bool wxRichTextCtrl::CanCopy() const
2152 // Can copy if there's a selection
2153 return HasSelection();
2156 bool wxRichTextCtrl::CanCut() const
2158 return HasSelection() && IsEditable();
2161 bool wxRichTextCtrl::CanPaste() const
2163 if ( !IsEditable() )
2166 return GetBuffer().CanPasteFromClipboard();
2169 bool wxRichTextCtrl::CanDeleteSelection() const
2171 return HasSelection() && IsEditable();
2175 // ----------------------------------------------------------------------------
2177 // ----------------------------------------------------------------------------
2179 void wxRichTextCtrl::SetEditable(bool editable
)
2181 m_editable
= editable
;
2184 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2188 m_caretPosition
= pos
- 1;
2191 void wxRichTextCtrl::SetInsertionPointEnd()
2193 long pos
= GetLastPosition();
2194 SetInsertionPoint(pos
);
2197 long wxRichTextCtrl::GetInsertionPoint() const
2199 return m_caretPosition
+1;
2202 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2204 return GetBuffer().GetRange().GetEnd();
2207 // If the return values from and to are the same, there is no
2209 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2211 *from
= m_selectionRange
.GetStart();
2212 *to
= m_selectionRange
.GetEnd();
2213 if ((*to
) != -1 && (*to
) != -2)
2217 bool wxRichTextCtrl::IsEditable() const
2222 // ----------------------------------------------------------------------------
2224 // ----------------------------------------------------------------------------
2226 void wxRichTextCtrl::SetSelection(long from
, long to
)
2228 // if from and to are both -1, it means (in wxWidgets) that all text should
2230 if ( (from
== -1) && (to
== -1) )
2233 to
= GetLastPosition()+1;
2236 DoSetSelection(from
, to
);
2239 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2241 m_selectionAnchor
= from
;
2242 m_selectionRange
.SetRange(from
, to
-1);
2243 m_caretPosition
= from
-1;
2249 // ----------------------------------------------------------------------------
2251 // ----------------------------------------------------------------------------
2253 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2254 const wxString
& value
)
2256 BeginBatchUndo(_("Replace"));
2258 DeleteSelectedContent();
2260 DoWriteText(value
, SetValue_SelectionOnly
);
2265 void wxRichTextCtrl::Remove(long from
, long to
)
2269 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2276 bool wxRichTextCtrl::IsModified() const
2278 return m_buffer
.IsModified();
2281 void wxRichTextCtrl::MarkDirty()
2283 m_buffer
.Modify(true);
2286 void wxRichTextCtrl::DiscardEdits()
2288 m_caretPositionForDefaultStyle
= -2;
2289 m_buffer
.Modify(false);
2290 m_buffer
.GetCommandProcessor()->ClearCommands();
2293 int wxRichTextCtrl::GetNumberOfLines() const
2295 return GetBuffer().GetParagraphCount();
2298 // ----------------------------------------------------------------------------
2299 // Positions <-> coords
2300 // ----------------------------------------------------------------------------
2302 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2304 return GetBuffer().XYToPosition(x
, y
);
2307 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2309 return GetBuffer().PositionToXY(pos
, x
, y
);
2312 // ----------------------------------------------------------------------------
2314 // ----------------------------------------------------------------------------
2316 void wxRichTextCtrl::ShowPosition(long pos
)
2318 if (!IsPositionVisible(pos
))
2319 ScrollIntoView(pos
-1, WXK_DOWN
);
2322 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2324 return GetBuffer().GetParagraphLength(lineNo
);
2327 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2329 return GetBuffer().GetParagraphText(lineNo
);
2332 // ----------------------------------------------------------------------------
2334 // ----------------------------------------------------------------------------
2336 void wxRichTextCtrl::Undo()
2340 GetCommandProcessor()->Undo();
2344 void wxRichTextCtrl::Redo()
2348 GetCommandProcessor()->Redo();
2352 bool wxRichTextCtrl::CanUndo() const
2354 return GetCommandProcessor()->CanUndo();
2357 bool wxRichTextCtrl::CanRedo() const
2359 return GetCommandProcessor()->CanRedo();
2362 // ----------------------------------------------------------------------------
2363 // implementation details
2364 // ----------------------------------------------------------------------------
2366 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2368 SetValue(event
.GetString());
2369 GetEventHandler()->ProcessEvent(event
);
2372 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2374 // By default, load the first file into the text window.
2375 if (event
.GetNumberOfFiles() > 0)
2377 LoadFile(event
.GetFiles()[0]);
2381 wxSize
wxRichTextCtrl::DoGetBestSize() const
2383 return wxSize(10, 10);
2386 // ----------------------------------------------------------------------------
2387 // standard handlers for standard edit menu events
2388 // ----------------------------------------------------------------------------
2390 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2395 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2400 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2405 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2410 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2415 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2420 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2422 event
.Enable( CanCut() );
2425 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2427 event
.Enable( CanCopy() );
2430 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2432 event
.Enable( CanDeleteSelection() );
2435 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2437 event
.Enable( CanPaste() );
2440 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2442 event
.Enable( CanUndo() );
2443 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2446 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2448 event
.Enable( CanRedo() );
2449 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2452 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2457 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2459 event
.Enable(GetLastPosition() > 0);
2462 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2466 m_contextMenu
= new wxMenu
;
2467 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2468 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2469 m_contextMenu
->AppendSeparator();
2470 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2471 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2472 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2473 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2474 m_contextMenu
->AppendSeparator();
2475 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2477 PopupMenu(m_contextMenu
);
2481 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2483 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2486 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2488 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2491 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2493 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2496 // extended style setting operation with flags including:
2497 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2498 // see richtextbuffer.h for more details.
2499 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2501 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2504 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2506 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2509 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2511 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2514 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2516 return GetBuffer().SetDefaultStyle(style
);
2519 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2521 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2524 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2526 return GetBuffer().GetDefaultStyle();
2529 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2531 return GetBuffer().GetDefaultStyle();
2534 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2536 wxTextAttrEx
attr(style
);
2537 if (GetBuffer().GetStyle(position
, attr
))
2546 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2548 return GetBuffer().GetStyle(position
, style
);
2551 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2553 return GetBuffer().GetStyle(position
, style
);
2556 // get the common set of styles for the range
2557 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2559 wxTextAttrEx styleEx
;
2560 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2569 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2571 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2574 /// Get the content (uncombined) attributes for this position.
2576 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2578 wxTextAttrEx
attr(style
);
2579 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2588 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2590 return GetBuffer().GetUncombinedStyle(position
, style
);
2593 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2595 return GetBuffer().GetUncombinedStyle(position
, style
);
2598 /// Set font, and also the buffer attributes
2599 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2601 wxControl::SetFont(font
);
2603 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2605 GetBuffer().SetBasicStyle(attr
);
2607 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2613 /// Transform logical to physical
2614 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2617 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2622 /// Transform physical to logical
2623 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2626 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2631 /// Position the caret
2632 void wxRichTextCtrl::PositionCaret()
2637 //wxLogDebug(wxT("PositionCaret"));
2640 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2642 wxPoint originalPt
= caretRect
.GetPosition();
2643 wxPoint pt
= GetPhysicalPoint(originalPt
);
2644 if (GetCaret()->GetPosition() != pt
)
2646 GetCaret()->Move(pt
);
2647 GetCaret()->SetSize(caretRect
.GetSize());
2652 /// Get the caret height and position for the given character position
2653 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2655 wxClientDC
dc(this);
2656 dc
.SetFont(GetFont());
2663 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2665 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2672 /// Gets the line for the visible caret position. If the caret is
2673 /// shown at the very end of the line, it means the next character is actually
2674 /// on the following line. So let's get the line we're expecting to find
2675 /// if this is the case.
2676 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2678 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2679 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2682 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2683 if (caretPosition
== lineRange
.GetStart()-1 &&
2684 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2686 if (!m_caretAtLineStart
)
2687 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2694 /// Move the caret to the given character position
2695 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2697 if (GetBuffer().GetDirty())
2700 if (pos
<= GetBuffer().GetRange().GetEnd())
2702 SetCaretPosition(pos
, showAtLineStart
);
2712 /// Layout the buffer: which we must do before certain operations, such as
2713 /// setting the caret position.
2714 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2716 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2718 wxRect
availableSpace(GetClientSize());
2719 if (availableSpace
.width
== 0)
2720 availableSpace
.width
= 10;
2721 if (availableSpace
.height
== 0)
2722 availableSpace
.height
= 10;
2724 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2725 if (onlyVisibleRect
)
2727 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2728 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2731 wxClientDC
dc(this);
2732 dc
.SetFont(GetFont());
2736 GetBuffer().Defragment();
2737 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2738 GetBuffer().Layout(dc
, availableSpace
, flags
);
2739 GetBuffer().SetDirty(false);
2748 /// Is all of the selection bold?
2749 bool wxRichTextCtrl::IsSelectionBold()
2753 wxRichTextAttr attr
;
2754 wxRichTextRange range
= GetSelectionRange();
2755 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2756 attr
.SetFontWeight(wxBOLD
);
2758 return HasCharacterAttributes(range
, attr
);
2762 // If no selection, then we need to combine current style with default style
2763 // to see what the effect would be if we started typing.
2764 wxRichTextAttr attr
;
2765 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2767 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2768 if (GetStyle(pos
, attr
))
2770 if (IsDefaultStyleShowing())
2771 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2772 return attr
.GetFontWeight() == wxBOLD
;
2778 /// Is all of the selection italics?
2779 bool wxRichTextCtrl::IsSelectionItalics()
2783 wxRichTextRange range
= GetSelectionRange();
2784 wxRichTextAttr attr
;
2785 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2786 attr
.SetFontStyle(wxITALIC
);
2788 return HasCharacterAttributes(range
, attr
);
2792 // If no selection, then we need to combine current style with default style
2793 // to see what the effect would be if we started typing.
2794 wxRichTextAttr attr
;
2795 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2797 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2798 if (GetStyle(pos
, attr
))
2800 if (IsDefaultStyleShowing())
2801 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2802 return attr
.GetFontStyle() == wxITALIC
;
2808 /// Is all of the selection underlined?
2809 bool wxRichTextCtrl::IsSelectionUnderlined()
2813 wxRichTextRange range
= GetSelectionRange();
2814 wxRichTextAttr attr
;
2815 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2816 attr
.SetFontUnderlined(true);
2818 return HasCharacterAttributes(range
, attr
);
2822 // If no selection, then we need to combine current style with default style
2823 // to see what the effect would be if we started typing.
2824 wxRichTextAttr attr
;
2825 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2826 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2828 if (GetStyle(pos
, attr
))
2830 if (IsDefaultStyleShowing())
2831 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2832 return attr
.GetFontUnderlined();
2838 /// Apply bold to the selection
2839 bool wxRichTextCtrl::ApplyBoldToSelection()
2841 wxRichTextAttr attr
;
2842 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2843 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2846 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2848 SetAndShowDefaultStyle(attr
);
2852 /// Apply italic to the selection
2853 bool wxRichTextCtrl::ApplyItalicToSelection()
2855 wxRichTextAttr attr
;
2856 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2857 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2860 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2862 SetAndShowDefaultStyle(attr
);
2866 /// Apply underline to the selection
2867 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2869 wxRichTextAttr attr
;
2870 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2871 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2874 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2876 SetAndShowDefaultStyle(attr
);
2880 /// Is all of the selection aligned according to the specified flag?
2881 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2883 wxRichTextRange range
;
2885 range
= GetSelectionRange();
2887 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2889 wxRichTextAttr attr
;
2890 attr
.SetAlignment(alignment
);
2892 return HasParagraphAttributes(range
, attr
);
2895 /// Apply alignment to the selection
2896 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2898 wxRichTextAttr attr
;
2899 attr
.SetAlignment(alignment
);
2901 return SetStyle(GetSelectionRange(), attr
);
2904 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2906 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2911 /// Apply a named style to the selection
2912 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2914 // Flags are defined within each definition, so only certain
2915 // attributes are applied.
2916 wxRichTextAttr
attr(def
->GetStyle());
2918 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2920 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2922 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2924 wxRichTextRange range
;
2927 range
= GetSelectionRange();
2930 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2931 range
= wxRichTextRange(pos
, pos
+1);
2934 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2937 // Make sure the attr has the style name
2938 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2940 attr
.SetParagraphStyleName(def
->GetName());
2942 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2943 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2944 // to change its style independently.
2945 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2948 attr
.SetCharacterStyleName(def
->GetName());
2951 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2954 SetAndShowDefaultStyle(attr
);
2959 /// Apply the style sheet to the buffer, for example if the styles have changed.
2960 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2963 styleSheet
= GetBuffer().GetStyleSheet();
2967 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2969 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2977 /// Sets the default style to the style under the cursor
2978 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2981 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2983 // If at the start of a paragraph, use the next position.
2984 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2986 if (GetUncombinedStyle(pos
, attr
))
2988 SetDefaultStyle(attr
);
2995 /// Returns the first visible position in the current view
2996 long wxRichTextCtrl::GetFirstVisiblePosition() const
2998 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3000 return line
->GetAbsoluteRange().GetStart();
3005 /// Get the first visible point in the window
3006 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3009 int startXUnits
, startYUnits
;
3011 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3012 GetViewStart(& startXUnits
, & startYUnits
);
3014 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3017 /// The adjusted caret position is the character position adjusted to take
3018 /// into account whether we're at the start of a paragraph, in which case
3019 /// style information should be taken from the next position, not current one.
3020 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3022 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3024 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3029 /// Get/set the selection range in character positions. -1, -1 means no selection.
3030 /// The range is in API convention, i.e. a single character selection is denoted
3032 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3034 wxRichTextRange range
= GetInternalSelectionRange();
3035 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3036 range
.SetEnd(range
.GetEnd() + 1);
3040 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3042 wxRichTextRange
range1(range
);
3043 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3044 range1
.SetEnd(range1
.GetEnd() - 1);
3046 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3048 SetInternalSelectionRange(range1
);
3052 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3054 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3057 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3059 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3062 /// Clear list for given range
3063 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3065 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3068 /// Number/renumber any list elements in the given range
3069 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3071 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3074 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3076 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3079 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3080 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3082 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3085 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3087 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3090 /// Deletes the content in the given range
3091 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3093 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3096 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3098 if (sm_availableFontNames
.GetCount() == 0)
3100 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3101 sm_availableFontNames
.Sort();
3103 return sm_availableFontNames
;
3106 void wxRichTextCtrl::ClearAvailableFontNames()
3108 sm_availableFontNames
.Clear();