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_ITEM_SELECTED
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
52 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
54 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
56 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
57 EVT_PAINT(wxRichTextCtrl::OnPaint
)
58 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
59 EVT_IDLE(wxRichTextCtrl::OnIdle
)
60 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
61 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
62 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
63 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
64 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
65 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
66 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
67 EVT_CHAR(wxRichTextCtrl::OnChar
)
68 EVT_SIZE(wxRichTextCtrl::OnSize
)
69 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
70 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
71 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
73 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
74 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
76 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
77 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
79 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
80 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
82 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
83 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
85 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
86 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
88 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
89 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
91 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
92 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
99 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
101 wxRichTextCtrl::wxRichTextCtrl()
102 : wxScrollHelper(this)
107 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
109 const wxString
& value
,
113 : wxScrollHelper(this)
116 Create(parent
, id
, value
, pos
, size
, style
);
120 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
122 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
123 style
|wxFULL_REPAINT_ON_RESIZE
))
128 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
132 GetBuffer().SetRichTextCtrl(this);
134 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
137 if (style
& wxTE_READONLY
)
140 // The base attributes must all have default values
141 wxTextAttrEx attributes
;
142 attributes
.SetFont(GetFont());
143 attributes
.SetTextColour(*wxBLACK
);
144 attributes
.SetBackgroundColour(*wxWHITE
);
145 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
146 attributes
.SetLineSpacing(10);
147 attributes
.SetParagraphSpacingAfter(10);
148 attributes
.SetParagraphSpacingBefore(0);
149 attributes
.SetTextEffects(0);
150 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
152 SetBasicStyle(attributes
);
154 // The default attributes will be merged with base attributes, so
155 // can be empty to begin with
156 wxTextAttrEx defaultAttributes
;
157 SetDefaultStyle(defaultAttributes
);
159 SetBackgroundColour(*wxWHITE
);
160 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
162 // Tell the sizers to use the given or best size
163 SetInitialSize(size
);
165 #if wxRICHTEXT_BUFFERED_PAINTING
167 RecreateBuffer(size
);
170 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
171 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
173 SetCursor(m_textCursor
);
175 if (!value
.IsEmpty())
178 GetBuffer().AddEventHandler(this);
183 wxRichTextCtrl::~wxRichTextCtrl()
185 GetBuffer().RemoveEventHandler(this);
187 delete m_contextMenu
;
190 /// Member initialisation
191 void wxRichTextCtrl::Init()
194 m_contextMenu
= NULL
;
196 m_caretPosition
= -1;
197 m_selectionRange
.SetRange(-2, -2);
198 m_selectionAnchor
= -2;
200 m_caretAtLineStart
= false;
202 m_fullLayoutRequired
= false;
203 m_fullLayoutTime
= 0;
204 m_fullLayoutSavedPosition
= 0;
205 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
206 m_caretPositionForDefaultStyle
= -2;
209 /// Call Freeze to prevent refresh
210 void wxRichTextCtrl::Freeze()
215 /// Call Thaw to refresh
216 void wxRichTextCtrl::Thaw()
220 if (m_freezeCount
== 0)
228 void wxRichTextCtrl::Clear()
230 m_buffer
.ResetAndClearCommands();
231 m_buffer
.SetDirty(true);
232 m_caretPosition
= -1;
233 m_caretPositionForDefaultStyle
= -2;
234 m_caretAtLineStart
= false;
235 m_selectionRange
.SetRange(-2, -2);
237 SetScrollbars(0, 0, 0, 0, 0, 0);
239 if (m_freezeCount
== 0)
244 SendTextUpdatedEvent();
248 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
250 if (GetCaret() && GetCaret()->IsVisible())
254 #if wxRICHTEXT_BUFFERED_PAINTING
255 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
261 if (m_freezeCount
> 0)
264 dc
.SetFont(GetFont());
266 // Paint the background
269 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
271 wxRect
drawingArea(GetUpdateRegion().GetBox());
272 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
274 wxRect
availableSpace(GetClientSize());
275 if (GetBuffer().GetDirty())
277 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
278 GetBuffer().SetDirty(false);
282 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
285 if (GetCaret() && !GetCaret()->IsVisible())
291 // Empty implementation, to prevent flicker
292 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
296 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
300 if (!GetCaret()->IsVisible())
309 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
311 if (GetCaret() && GetCaret()->IsVisible())
319 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
325 dc
.SetFont(GetFont());
328 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
330 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
332 m_dragStart
= event
.GetLogicalPosition(dc
);
338 bool caretAtLineStart
= false;
340 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
342 // If we're at the start of a line (but not first in para)
343 // then we should keep the caret showing at the start of the line
344 // by showing the m_caretAtLineStart flag.
345 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
346 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
348 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
349 caretAtLineStart
= true;
353 MoveCaret(position
, caretAtLineStart
);
354 SetDefaultStyleToCursorStyle();
361 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
366 if (GetCapture() == this)
369 // See if we clicked on a URL
372 dc
.SetFont(GetFont());
375 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
376 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
378 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
381 if (GetStyle(position
, attr
))
383 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
385 wxString urlTarget
= attr
.GetURL();
386 if (!urlTarget
.IsEmpty())
388 wxMouseEvent
mouseEvent(event
);
390 long startPos
= 0, endPos
= 0;
391 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
394 startPos
= obj
->GetRange().GetStart();
395 endPos
= obj
->GetRange().GetEnd();
398 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
399 InitCommandEvent(urlEvent
);
401 urlEvent
.SetString(urlTarget
);
403 GetEventHandler()->ProcessEvent(urlEvent
);
412 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
416 dc
.SetFont(GetFont());
419 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
420 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
422 // See if we need to change the cursor
425 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
428 if (GetStyle(position
, attr
))
430 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
432 SetCursor(m_urlCursor
);
434 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
436 SetCursor(m_textCursor
);
442 if (!event
.Dragging())
448 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
450 // TODO: test closeness
452 bool caretAtLineStart
= false;
454 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
456 // If we're at the start of a line (but not first in para)
457 // then we should keep the caret showing at the start of the line
458 // by showing the m_caretAtLineStart flag.
459 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
460 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
462 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
463 caretAtLineStart
= true;
467 if (m_caretPosition
!= position
)
469 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
471 MoveCaret(position
, caretAtLineStart
);
472 SetDefaultStyleToCursorStyle();
481 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
487 /// Left-double-click
488 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
490 SelectWord(GetCaretPosition()+1);
495 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
501 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
505 flags
|= wxRICHTEXT_CTRL_DOWN
;
506 if (event
.ShiftDown())
507 flags
|= wxRICHTEXT_SHIFT_DOWN
;
509 flags
|= wxRICHTEXT_ALT_DOWN
;
511 if (event
.GetKeyCode() == WXK_LEFT
||
512 event
.GetKeyCode() == WXK_RIGHT
||
513 event
.GetKeyCode() == WXK_UP
||
514 event
.GetKeyCode() == WXK_DOWN
||
515 event
.GetKeyCode() == WXK_HOME
||
516 event
.GetKeyCode() == WXK_PAGEUP
||
517 event
.GetKeyCode() == WXK_PAGEDOWN
||
518 event
.GetKeyCode() == WXK_END
||
520 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
521 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
522 event
.GetKeyCode() == WXK_NUMPAD_UP
||
523 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
524 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
525 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
526 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
527 event
.GetKeyCode() == WXK_NUMPAD_END
)
529 KeyboardNavigate(event
.GetKeyCode(), flags
);
533 // all the other keys modify the controls contents which shouldn't be
534 // possible if we're read-only
541 if (event
.GetKeyCode() == WXK_RETURN
)
543 BeginBatchUndo(_("Insert Text"));
545 long newPos
= m_caretPosition
;
547 DeleteSelectedContent(& newPos
);
549 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
551 SetDefaultStyleToCursorStyle();
553 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
555 wxRichTextEvent
cmdEvent(
556 wxEVT_COMMAND_RICHTEXT_RETURN
,
558 cmdEvent
.SetEventObject(this);
559 cmdEvent
.SetFlags(flags
);
560 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
562 // Generate conventional event
563 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
564 InitCommandEvent(textEvent
);
566 GetEventHandler()->ProcessEvent(textEvent
);
570 else if (event
.GetKeyCode() == WXK_BACK
)
572 BeginBatchUndo(_("Delete Text"));
574 // Submit range in character positions, which are greater than caret positions,
575 // so subtract 1 for deleted character and add 1 for conversion to character position.
576 if (m_caretPosition
> -1 && !HasSelection())
578 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
579 m_caretPosition
, // Current caret position
580 m_caretPosition
-1, // New caret position
584 DeleteSelectedContent();
588 if (GetLastPosition() == -1)
592 m_caretPosition
= -1;
594 SetDefaultStyleToCursorStyle();
597 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
600 else if (event
.GetKeyCode() == WXK_DELETE
)
602 BeginBatchUndo(_("Delete Text"));
604 // Submit range in character positions, which are greater than caret positions,
605 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
607 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
608 m_caretPosition
, // Current caret position
609 m_caretPosition
+1, // New caret position
613 DeleteSelectedContent();
617 if (GetLastPosition() == -1)
621 m_caretPosition
= -1;
623 SetDefaultStyleToCursorStyle();
629 long keycode
= event
.GetKeyCode();
703 case WXK_NUMPAD_SPACE
:
705 case WXK_NUMPAD_ENTER
:
710 case WXK_NUMPAD_HOME
:
711 case WXK_NUMPAD_LEFT
:
713 case WXK_NUMPAD_RIGHT
:
714 case WXK_NUMPAD_DOWN
:
715 case WXK_NUMPAD_PAGEUP
:
716 case WXK_NUMPAD_PAGEDOWN
:
718 case WXK_NUMPAD_BEGIN
:
719 case WXK_NUMPAD_INSERT
:
720 case WXK_NUMPAD_DELETE
:
721 case WXK_NUMPAD_EQUAL
:
722 case WXK_NUMPAD_MULTIPLY
:
724 case WXK_NUMPAD_SEPARATOR
:
725 case WXK_NUMPAD_SUBTRACT
:
726 case WXK_NUMPAD_DECIMAL
:
734 if (event
.CmdDown() || event
.AltDown())
740 if (keycode
== wxT('\t'))
742 // See if we need to promote or demote the selection or paragraph at the cursor
743 // position, instead of inserting a tab.
744 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
745 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
746 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
748 wxRichTextRange range
;
750 range
= GetSelectionRange();
752 range
= para
->GetRange().FromInternal();
754 int promoteBy
= event
.ShiftDown() ? 1 : -1;
756 PromoteList(promoteBy
, range
, NULL
);
762 BeginBatchUndo(_("Insert Text"));
764 long newPos
= m_caretPosition
;
765 DeleteSelectedContent(& newPos
);
767 wxString str
= (wxChar
) event
.GetKeyCode();
768 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
772 SetDefaultStyleToCursorStyle();
773 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
780 /// Delete content if there is a selection, e.g. when pressing a key.
781 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
785 long pos
= m_selectionRange
.GetStart();
786 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
787 m_caretPosition
, // Current caret position
788 pos
, // New caret position
790 m_selectionRange
.SetRange(-2, -2);
800 /// Keyboard navigation
804 Left: left one character
805 Right: right one character
808 Ctrl-Left: left one word
809 Ctrl-Right: right one word
810 Ctrl-Up: previous paragraph start
811 Ctrl-Down: next start of paragraph
814 Ctrl-Home: start of document
815 Ctrl-End: end of document
817 Page-Down: Down a screen
821 Ctrl-Alt-PgUp: Start of window
822 Ctrl-Alt-PgDn: End of window
823 F8: Start selection mode
824 Esc: End selection mode
826 Adding Shift does the above but starts/extends selection.
831 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
833 bool success
= false;
835 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
837 if (flags
& wxRICHTEXT_CTRL_DOWN
)
838 success
= WordRight(1, flags
);
840 success
= MoveRight(1, flags
);
842 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
844 if (flags
& wxRICHTEXT_CTRL_DOWN
)
845 success
= WordLeft(1, flags
);
847 success
= MoveLeft(1, flags
);
849 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
851 if (flags
& wxRICHTEXT_CTRL_DOWN
)
852 success
= MoveToParagraphStart(flags
);
854 success
= MoveUp(1, flags
);
856 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
858 if (flags
& wxRICHTEXT_CTRL_DOWN
)
859 success
= MoveToParagraphEnd(flags
);
861 success
= MoveDown(1, flags
);
863 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
865 success
= PageUp(1, flags
);
867 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
869 success
= PageDown(1, flags
);
871 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
873 if (flags
& wxRICHTEXT_CTRL_DOWN
)
874 success
= MoveHome(flags
);
876 success
= MoveToLineStart(flags
);
878 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
880 if (flags
& wxRICHTEXT_CTRL_DOWN
)
881 success
= MoveEnd(flags
);
883 success
= MoveToLineEnd(flags
);
888 ScrollIntoView(m_caretPosition
, keyCode
);
889 SetDefaultStyleToCursorStyle();
895 /// Extend the selection. Selections are in caret positions.
896 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
898 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
900 // If not currently selecting, start selecting
901 if (m_selectionRange
.GetStart() == -2)
903 m_selectionAnchor
= oldPos
;
906 m_selectionRange
.SetRange(newPos
+1, oldPos
);
908 m_selectionRange
.SetRange(oldPos
+1, newPos
);
912 // Always ensure that the selection range start is greater than
914 if (newPos
> m_selectionAnchor
)
915 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
917 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
920 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
922 wxLogDebug(wxT("Strange selection range"));
931 /// Scroll into view, returning true if we scrolled.
932 /// This takes a _caret_ position.
933 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
935 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
941 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
943 int startXUnits
, startYUnits
;
944 GetViewStart(& startXUnits
, & startYUnits
);
945 int startY
= startYUnits
* ppuY
;
948 GetVirtualSize(& sx
, & sy
);
954 wxRect rect
= line
->GetRect();
956 bool scrolled
= false;
958 wxSize clientSize
= GetClientSize();
961 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
962 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
963 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
964 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
966 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
968 // Make it scroll so this item is at the bottom
970 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
971 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
973 // If we're still off the screen, scroll another line down
974 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
977 if (startYUnits
!= yUnits
)
979 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
983 else if (rect
.y
< startY
)
985 // Make it scroll so this item is at the top
988 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
990 if (startYUnits
!= yUnits
)
992 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
998 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
999 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1000 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1001 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1003 if (rect
.y
< startY
)
1005 // Make it scroll so this item is at the top
1008 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1010 if (startYUnits
!= yUnits
)
1012 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1016 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1018 // Make it scroll so this item is at the bottom
1020 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1021 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1023 // If we're still off the screen, scroll another line down
1024 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1027 if (startYUnits
!= yUnits
)
1029 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1039 /// Is the given position visible on the screen?
1040 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1042 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1048 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1051 GetViewStart(& startX
, & startY
);
1053 startY
= startY
* ppuY
;
1056 GetVirtualSize(& sx
, & sy
);
1061 wxRect rect
= line
->GetRect();
1063 wxSize clientSize
= GetClientSize();
1065 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1068 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1070 m_caretPosition
= position
;
1071 m_caretAtLineStart
= showAtLineStart
;
1074 /// Move caret one visual step forward: this may mean setting a flag
1075 /// and keeping the same position if we're going from the end of one line
1076 /// to the start of the next, which may be the exact same caret position.
1077 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1079 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1081 // Only do the check if we're not at the end of the paragraph (where things work OK
1083 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1085 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1089 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1091 // We're at the end of a line. See whether we need to
1092 // stay at the same actual caret position but change visual
1093 // position, or not.
1094 if (oldPosition
== lineRange
.GetEnd())
1096 if (m_caretAtLineStart
)
1098 // We're already at the start of the line, so actually move on now.
1099 m_caretPosition
= oldPosition
+ 1;
1100 m_caretAtLineStart
= false;
1104 // We're showing at the end of the line, so keep to
1105 // the same position but indicate that we're to show
1106 // at the start of the next line.
1107 m_caretPosition
= oldPosition
;
1108 m_caretAtLineStart
= true;
1110 SetDefaultStyleToCursorStyle();
1116 SetDefaultStyleToCursorStyle();
1119 /// Move caret one visual step backward: this may mean setting a flag
1120 /// and keeping the same position if we're going from the end of one line
1121 /// to the start of the next, which may be the exact same caret position.
1122 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1124 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1126 // Only do the check if we're not at the start of the paragraph (where things work OK
1128 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1130 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1134 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1136 // We're at the start of a line. See whether we need to
1137 // stay at the same actual caret position but change visual
1138 // position, or not.
1139 if (oldPosition
== lineRange
.GetStart())
1141 m_caretPosition
= oldPosition
-1;
1142 m_caretAtLineStart
= true;
1145 else if (oldPosition
== lineRange
.GetEnd())
1147 if (m_caretAtLineStart
)
1149 // We're at the start of the line, so keep the same caret position
1150 // but clear the start-of-line flag.
1151 m_caretPosition
= oldPosition
;
1152 m_caretAtLineStart
= false;
1156 // We're showing at the end of the line, so go back
1157 // to the previous character position.
1158 m_caretPosition
= oldPosition
- 1;
1160 SetDefaultStyleToCursorStyle();
1166 SetDefaultStyleToCursorStyle();
1170 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1172 long endPos
= GetBuffer().GetRange().GetEnd();
1174 if (m_caretPosition
+ noPositions
< endPos
)
1176 long oldPos
= m_caretPosition
;
1177 long newPos
= m_caretPosition
+ noPositions
;
1179 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1183 // Determine by looking at oldPos and m_caretPosition whether
1184 // we moved from the end of a line to the start of the next line, in which case
1185 // we want to adjust the caret position such that it is positioned at the
1186 // start of the next line, rather than jumping past the first character of the
1188 if (noPositions
== 1 && !extendSel
)
1189 MoveCaretForward(oldPos
);
1191 SetCaretPosition(newPos
);
1194 SetDefaultStyleToCursorStyle();
1205 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1209 if (m_caretPosition
> startPos
- noPositions
+ 1)
1211 long oldPos
= m_caretPosition
;
1212 long newPos
= m_caretPosition
- noPositions
;
1213 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1217 if (noPositions
== 1 && !extendSel
)
1218 MoveCaretBack(oldPos
);
1220 SetCaretPosition(newPos
);
1223 SetDefaultStyleToCursorStyle();
1234 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1236 return MoveDown(- noLines
, flags
);
1240 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1245 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1246 wxPoint pt
= GetCaret()->GetPosition();
1247 long newLine
= lineNumber
+ noLines
;
1249 if (lineNumber
!= -1)
1253 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1255 if (newLine
> lastLine
)
1265 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1268 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1274 wxClientDC
dc(this);
1276 dc
.SetFont(GetFont());
1278 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1280 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1282 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1283 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1284 // so we view the caret at the start of the line.
1285 bool caretLineStart
= false;
1286 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1288 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1289 wxRichTextRange lineRange
;
1291 lineRange
= thisLine
->GetAbsoluteRange();
1293 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1296 caretLineStart
= true;
1300 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1301 if (para
&& para
->GetRange().GetStart() == newPos
)
1306 long newSelEnd
= newPos
;
1308 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1312 SetCaretPosition(newPos
, caretLineStart
);
1314 SetDefaultStyleToCursorStyle();
1324 /// Move to the end of the paragraph
1325 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1327 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1330 long newPos
= para
->GetRange().GetEnd() - 1;
1331 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1335 SetCaretPosition(newPos
);
1337 SetDefaultStyleToCursorStyle();
1347 /// Move to the start of the paragraph
1348 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1350 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1353 long newPos
= para
->GetRange().GetStart() - 1;
1354 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1358 SetCaretPosition(newPos
);
1360 SetDefaultStyleToCursorStyle();
1370 /// Move to the end of the line
1371 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1373 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1377 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1378 long newPos
= lineRange
.GetEnd();
1379 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1383 SetCaretPosition(newPos
);
1385 SetDefaultStyleToCursorStyle();
1395 /// Move to the start of the line
1396 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1398 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1401 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1402 long newPos
= lineRange
.GetStart()-1;
1404 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1408 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1410 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1412 SetDefaultStyleToCursorStyle();
1422 /// Move to the start of the buffer
1423 bool wxRichTextCtrl::MoveHome(int flags
)
1425 if (m_caretPosition
!= -1)
1427 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1431 SetCaretPosition(-1);
1433 SetDefaultStyleToCursorStyle();
1443 /// Move to the end of the buffer
1444 bool wxRichTextCtrl::MoveEnd(int flags
)
1446 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1448 if (m_caretPosition
!= endPos
)
1450 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1454 SetCaretPosition(endPos
);
1456 SetDefaultStyleToCursorStyle();
1466 /// Move noPages pages up
1467 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1469 return PageDown(- noPages
, flags
);
1472 /// Move noPages pages down
1473 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1475 // Calculate which line occurs noPages * screen height further down.
1476 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1479 wxSize clientSize
= GetClientSize();
1480 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1482 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1485 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1486 long pos
= lineRange
.GetStart()-1;
1487 if (pos
!= m_caretPosition
)
1489 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1491 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1495 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1497 SetDefaultStyleToCursorStyle();
1509 // Finds the caret position for the next word
1510 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1512 long endPos
= GetBuffer().GetRange().GetEnd();
1516 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1518 // First skip current text to space
1519 while (i
< endPos
&& i
> -1)
1521 // i is in character, not caret positions
1522 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1523 if (text
!= wxT(" ") && !text
.empty())
1530 while (i
< endPos
&& i
> -1)
1532 // i is in character, not caret positions
1533 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1534 if (text
.empty()) // End of paragraph, or maybe an image
1535 return wxMax(-1, i
- 1);
1536 else if (text
== wxT(" ") || text
.empty())
1540 // Convert to caret position
1541 return wxMax(-1, i
- 1);
1550 long i
= m_caretPosition
;
1552 // First skip white 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
.empty()) // End of paragraph, or maybe an image
1559 else if (text
== wxT(" ") || text
.empty())
1564 // Next skip current text to space
1565 while (i
< endPos
&& i
> -1)
1567 // i is in character, not caret positions
1568 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1569 if (text
!= wxT(" ") /* && !text.empty() */)
1582 /// Move n words left
1583 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1585 long pos
= FindNextWordPosition(-1);
1586 if (pos
!= m_caretPosition
)
1588 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1590 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1594 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1596 SetDefaultStyleToCursorStyle();
1606 /// Move n words right
1607 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1609 long pos
= FindNextWordPosition(1);
1610 if (pos
!= m_caretPosition
)
1612 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1614 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1618 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1620 SetDefaultStyleToCursorStyle();
1631 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1633 // Only do sizing optimization for large buffers
1634 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1636 m_fullLayoutRequired
= true;
1637 m_fullLayoutTime
= wxGetLocalTimeMillis();
1638 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1639 LayoutContent(true /* onlyVisibleRect */);
1642 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1644 #if wxRICHTEXT_BUFFERED_PAINTING
1652 /// Idle-time processing
1653 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1655 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1657 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1659 m_fullLayoutRequired
= false;
1660 m_fullLayoutTime
= 0;
1661 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1662 ShowPosition(m_fullLayoutSavedPosition
);
1666 if (m_caretPositionForDefaultStyle
!= -2)
1668 // If the caret position has changed, no longer reflect the default style
1670 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1671 m_caretPositionForDefaultStyle
= -2;
1678 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1684 /// Set up scrollbars, e.g. after a resize
1685 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1690 if (GetBuffer().IsEmpty())
1692 SetScrollbars(0, 0, 0, 0, 0, 0);
1696 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1697 // of pixels. See e.g. wxVScrolledWindow for ideas.
1698 int pixelsPerUnit
= 5;
1699 wxSize clientSize
= GetClientSize();
1701 int maxHeight
= GetBuffer().GetCachedSize().y
;
1703 // Round up so we have at least maxHeight pixels
1704 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1706 int startX
= 0, startY
= 0;
1708 GetViewStart(& startX
, & startY
);
1710 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1711 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1713 // Move to previous scroll position if
1715 SetScrollbars(0, pixelsPerUnit
,
1717 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1720 /// Paint the background
1721 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1723 wxColour backgroundColour
= GetBackgroundColour();
1724 if (!backgroundColour
.Ok())
1725 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1727 // Clear the background
1728 dc
.SetBrush(wxBrush(backgroundColour
));
1729 dc
.SetPen(*wxTRANSPARENT_PEN
);
1730 wxRect
windowRect(GetClientSize());
1731 windowRect
.x
-= 2; windowRect
.y
-= 2;
1732 windowRect
.width
+= 4; windowRect
.height
+= 4;
1734 // We need to shift the rectangle to take into account
1735 // scrolling. Converting device to logical coordinates.
1736 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1737 dc
.DrawRectangle(windowRect
);
1740 #if wxRICHTEXT_BUFFERED_PAINTING
1741 /// Recreate buffer bitmap if necessary
1742 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1745 if (sz
== wxDefaultSize
)
1746 sz
= GetClientSize();
1748 if (sz
.x
< 1 || sz
.y
< 1)
1751 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1752 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1753 return m_bufferBitmap
.Ok();
1757 // ----------------------------------------------------------------------------
1758 // file IO functions
1759 // ----------------------------------------------------------------------------
1761 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1763 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1765 m_filename
= filename
;
1768 SetInsertionPoint(0);
1771 SetupScrollbars(true);
1773 SendTextUpdatedEvent();
1779 wxLogError(_("File couldn't be loaded."));
1785 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1787 if (GetBuffer().SaveFile(filename
, fileType
))
1789 m_filename
= filename
;
1796 wxLogError(_("The text couldn't be saved."));
1801 // ----------------------------------------------------------------------------
1802 // wxRichTextCtrl specific functionality
1803 // ----------------------------------------------------------------------------
1805 /// Add a new paragraph of text to the end of the buffer
1806 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1808 return GetBuffer().AddParagraph(text
);
1812 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1814 return GetBuffer().AddImage(image
);
1817 // ----------------------------------------------------------------------------
1818 // selection and ranges
1819 // ----------------------------------------------------------------------------
1821 void wxRichTextCtrl::SelectAll()
1823 SetSelection(0, GetLastPosition()+1);
1824 m_selectionAnchor
= -1;
1828 void wxRichTextCtrl::SelectNone()
1830 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1831 SetSelection(-2, -2);
1832 m_selectionAnchor
= -2;
1835 static bool wxIsWordDelimiter(const wxString
& text
)
1837 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1840 /// Select the word at the given character position
1841 bool wxRichTextCtrl::SelectWord(long position
)
1843 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1846 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1850 long positionStart
= position
;
1851 long positionEnd
= position
;
1853 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1855 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1856 if (wxIsWordDelimiter(text
))
1862 if (positionStart
< para
->GetRange().GetStart())
1863 positionStart
= para
->GetRange().GetStart();
1865 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1867 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1868 if (wxIsWordDelimiter(text
))
1874 if (positionEnd
>= para
->GetRange().GetEnd())
1875 positionEnd
= para
->GetRange().GetEnd();
1877 SetSelection(positionStart
, positionEnd
+1);
1879 if (positionStart
>= 0)
1881 MoveCaret(positionStart
-1, true);
1882 SetDefaultStyleToCursorStyle();
1888 wxString
wxRichTextCtrl::GetStringSelection() const
1891 GetSelection(&from
, &to
);
1893 return GetRange(from
, to
);
1896 // ----------------------------------------------------------------------------
1898 // ----------------------------------------------------------------------------
1900 wxTextCtrlHitTestResult
1901 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1903 // implement in terms of the other overload as the native ports typically
1904 // can get the position and not (x, y) pair directly (although wxUniv
1905 // directly gets x and y -- and so overrides this method as well)
1907 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1909 if ( rc
!= wxTE_HT_UNKNOWN
)
1911 PositionToXY(pos
, x
, y
);
1917 wxTextCtrlHitTestResult
1918 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1921 wxClientDC
dc((wxRichTextCtrl
*) this);
1922 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1924 // Buffer uses logical position (relative to start of buffer)
1926 wxPoint pt2
= GetLogicalPoint(pt
);
1928 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1932 case wxRICHTEXT_HITTEST_BEFORE
:
1933 return wxTE_HT_BEFORE
;
1935 case wxRICHTEXT_HITTEST_AFTER
:
1936 return wxTE_HT_BEYOND
;
1938 case wxRICHTEXT_HITTEST_ON
:
1939 return wxTE_HT_ON_TEXT
;
1942 return wxTE_HT_UNKNOWN
;
1945 // ----------------------------------------------------------------------------
1946 // set/get the controls text
1947 // ----------------------------------------------------------------------------
1949 wxString
wxRichTextCtrl::GetValue() const
1951 return GetBuffer().GetText();
1954 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1956 // Public API for range is different from internals
1957 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1960 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1964 // if the text is long enough, it's faster to just set it instead of first
1965 // comparing it with the old one (chances are that it will be different
1966 // anyhow, this comparison is there to avoid flicker for small single-line
1967 // edit controls mostly)
1968 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1972 // for compatibility, don't move the cursor when doing SetValue()
1973 SetInsertionPoint(0);
1977 if ( flags
& SetValue_SendEvent
)
1979 // still send an event for consistency
1980 SendTextUpdatedEvent();
1984 // we should reset the modified flag even if the value didn't really change
1986 // mark the control as being not dirty - we changed its text, not the
1991 void wxRichTextCtrl::WriteText(const wxString
& value
)
1996 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1998 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2000 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
2002 if ( flags
& SetValue_SendEvent
)
2003 SendTextUpdatedEvent();
2006 void wxRichTextCtrl::AppendText(const wxString
& text
)
2008 SetInsertionPointEnd();
2013 /// Write an image at the current insertion point
2014 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2016 wxRichTextImageBlock imageBlock
;
2018 wxImage image2
= image
;
2019 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2020 return WriteImage(imageBlock
);
2025 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2027 wxRichTextImageBlock imageBlock
;
2030 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2031 return WriteImage(imageBlock
);
2036 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2038 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2041 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2045 wxRichTextImageBlock imageBlock
;
2047 wxImage image
= bitmap
.ConvertToImage();
2048 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2049 return WriteImage(imageBlock
);
2055 /// Insert a newline (actually paragraph) at the current insertion point.
2056 bool wxRichTextCtrl::Newline()
2058 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2062 // ----------------------------------------------------------------------------
2063 // Clipboard operations
2064 // ----------------------------------------------------------------------------
2066 void wxRichTextCtrl::Copy()
2070 wxRichTextRange range
= GetInternalSelectionRange();
2071 GetBuffer().CopyToClipboard(range
);
2075 void wxRichTextCtrl::Cut()
2079 wxRichTextRange range
= GetInternalSelectionRange();
2080 GetBuffer().CopyToClipboard(range
);
2082 DeleteSelectedContent();
2088 void wxRichTextCtrl::Paste()
2092 BeginBatchUndo(_("Paste"));
2094 long newPos
= m_caretPosition
;
2095 DeleteSelectedContent(& newPos
);
2097 GetBuffer().PasteFromClipboard(newPos
);
2103 void wxRichTextCtrl::DeleteSelection()
2105 if (CanDeleteSelection())
2107 DeleteSelectedContent();
2111 bool wxRichTextCtrl::HasSelection() const
2113 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2116 bool wxRichTextCtrl::CanCopy() const
2118 // Can copy if there's a selection
2119 return HasSelection();
2122 bool wxRichTextCtrl::CanCut() const
2124 return HasSelection() && IsEditable();
2127 bool wxRichTextCtrl::CanPaste() const
2129 if ( !IsEditable() )
2132 return GetBuffer().CanPasteFromClipboard();
2135 bool wxRichTextCtrl::CanDeleteSelection() const
2137 return HasSelection() && IsEditable();
2141 // ----------------------------------------------------------------------------
2143 // ----------------------------------------------------------------------------
2145 void wxRichTextCtrl::SetEditable(bool editable
)
2147 m_editable
= editable
;
2150 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2154 m_caretPosition
= pos
- 1;
2157 void wxRichTextCtrl::SetInsertionPointEnd()
2159 long pos
= GetLastPosition();
2160 SetInsertionPoint(pos
);
2163 long wxRichTextCtrl::GetInsertionPoint() const
2165 return m_caretPosition
+1;
2168 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2170 return GetBuffer().GetRange().GetEnd();
2173 // If the return values from and to are the same, there is no
2175 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2177 *from
= m_selectionRange
.GetStart();
2178 *to
= m_selectionRange
.GetEnd();
2179 if ((*to
) != -1 && (*to
) != -2)
2183 bool wxRichTextCtrl::IsEditable() const
2188 // ----------------------------------------------------------------------------
2190 // ----------------------------------------------------------------------------
2192 void wxRichTextCtrl::SetSelection(long from
, long to
)
2194 // if from and to are both -1, it means (in wxWidgets) that all text should
2196 if ( (from
== -1) && (to
== -1) )
2199 to
= GetLastPosition()+1;
2202 DoSetSelection(from
, to
);
2205 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2207 m_selectionAnchor
= from
;
2208 m_selectionRange
.SetRange(from
, to
-1);
2214 // ----------------------------------------------------------------------------
2216 // ----------------------------------------------------------------------------
2218 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2219 const wxString
& value
)
2221 BeginBatchUndo(_("Replace"));
2223 DeleteSelectedContent();
2225 DoWriteText(value
, SetValue_SelectionOnly
);
2230 void wxRichTextCtrl::Remove(long from
, long to
)
2234 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2235 m_caretPosition
, // Current caret position
2236 from
, // New caret position
2244 bool wxRichTextCtrl::IsModified() const
2246 return m_buffer
.IsModified();
2249 void wxRichTextCtrl::MarkDirty()
2251 m_buffer
.Modify(true);
2254 void wxRichTextCtrl::DiscardEdits()
2256 m_caretPositionForDefaultStyle
= -2;
2257 m_buffer
.Modify(false);
2258 m_buffer
.GetCommandProcessor()->ClearCommands();
2261 int wxRichTextCtrl::GetNumberOfLines() const
2263 return GetBuffer().GetParagraphCount();
2266 // ----------------------------------------------------------------------------
2267 // Positions <-> coords
2268 // ----------------------------------------------------------------------------
2270 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2272 return GetBuffer().XYToPosition(x
, y
);
2275 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2277 return GetBuffer().PositionToXY(pos
, x
, y
);
2280 // ----------------------------------------------------------------------------
2282 // ----------------------------------------------------------------------------
2284 void wxRichTextCtrl::ShowPosition(long pos
)
2286 if (!IsPositionVisible(pos
))
2287 ScrollIntoView(pos
-1, WXK_DOWN
);
2290 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2292 return GetBuffer().GetParagraphLength(lineNo
);
2295 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2297 return GetBuffer().GetParagraphText(lineNo
);
2300 // ----------------------------------------------------------------------------
2302 // ----------------------------------------------------------------------------
2304 void wxRichTextCtrl::Undo()
2308 GetCommandProcessor()->Undo();
2312 void wxRichTextCtrl::Redo()
2316 GetCommandProcessor()->Redo();
2320 bool wxRichTextCtrl::CanUndo() const
2322 return GetCommandProcessor()->CanUndo();
2325 bool wxRichTextCtrl::CanRedo() const
2327 return GetCommandProcessor()->CanRedo();
2330 // ----------------------------------------------------------------------------
2331 // implementation details
2332 // ----------------------------------------------------------------------------
2334 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2336 SetValue(event
.GetString());
2337 GetEventHandler()->ProcessEvent(event
);
2340 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2342 // By default, load the first file into the text window.
2343 if (event
.GetNumberOfFiles() > 0)
2345 LoadFile(event
.GetFiles()[0]);
2349 wxSize
wxRichTextCtrl::DoGetBestSize() const
2351 return wxSize(10, 10);
2354 // ----------------------------------------------------------------------------
2355 // standard handlers for standard edit menu events
2356 // ----------------------------------------------------------------------------
2358 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2363 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2368 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2373 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2378 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2383 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2388 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2390 event
.Enable( CanCut() );
2393 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2395 event
.Enable( CanCopy() );
2398 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2400 event
.Enable( CanDeleteSelection() );
2403 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2405 event
.Enable( CanPaste() );
2408 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2410 event
.Enable( CanUndo() );
2411 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2414 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2416 event
.Enable( CanRedo() );
2417 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2420 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2425 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2427 event
.Enable(GetLastPosition() > 0);
2430 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2434 m_contextMenu
= new wxMenu
;
2435 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2436 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2437 m_contextMenu
->AppendSeparator();
2438 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2439 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2440 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2441 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2442 m_contextMenu
->AppendSeparator();
2443 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2445 PopupMenu(m_contextMenu
);
2449 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2451 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2454 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2456 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2459 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2461 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2464 // extended style setting operation with flags including:
2465 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2466 // see richtextbuffer.h for more details.
2467 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2469 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2472 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2474 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2477 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2479 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2482 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2484 return GetBuffer().SetDefaultStyle(style
);
2487 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2489 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2492 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2494 return GetBuffer().GetDefaultStyle();
2497 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2499 return GetBuffer().GetDefaultStyle();
2502 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2504 wxTextAttrEx
attr(style
);
2505 if (GetBuffer().GetStyle(position
, attr
))
2514 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2516 return GetBuffer().GetStyle(position
, style
);
2519 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2521 return GetBuffer().GetStyle(position
, style
);
2524 /// Get the content (uncombined) attributes for this position.
2526 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2528 wxTextAttrEx
attr(style
);
2529 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2538 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2540 return GetBuffer().GetUncombinedStyle(position
, style
);
2543 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2545 return GetBuffer().GetUncombinedStyle(position
, style
);
2548 /// Set font, and also the buffer attributes
2549 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2551 wxControl::SetFont(font
);
2553 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2555 GetBuffer().SetBasicStyle(attr
);
2557 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2563 /// Transform logical to physical
2564 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2567 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2572 /// Transform physical to logical
2573 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2576 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2581 /// Position the caret
2582 void wxRichTextCtrl::PositionCaret()
2587 //wxLogDebug(wxT("PositionCaret"));
2590 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2592 wxPoint originalPt
= caretRect
.GetPosition();
2593 wxPoint pt
= GetPhysicalPoint(originalPt
);
2594 if (GetCaret()->GetPosition() != pt
)
2596 GetCaret()->Move(pt
);
2597 GetCaret()->SetSize(caretRect
.GetSize());
2602 /// Get the caret height and position for the given character position
2603 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2605 wxClientDC
dc(this);
2606 dc
.SetFont(GetFont());
2613 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2615 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2622 /// Gets the line for the visible caret position. If the caret is
2623 /// shown at the very end of the line, it means the next character is actually
2624 /// on the following line. So let's get the line we're expecting to find
2625 /// if this is the case.
2626 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2628 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2629 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2632 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2633 if (caretPosition
== lineRange
.GetStart()-1 &&
2634 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2636 if (!m_caretAtLineStart
)
2637 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2644 /// Move the caret to the given character position
2645 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2647 if (GetBuffer().GetDirty())
2650 if (pos
<= GetBuffer().GetRange().GetEnd())
2652 SetCaretPosition(pos
, showAtLineStart
);
2662 /// Layout the buffer: which we must do before certain operations, such as
2663 /// setting the caret position.
2664 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2666 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2668 wxRect
availableSpace(GetClientSize());
2669 if (availableSpace
.width
== 0)
2670 availableSpace
.width
= 10;
2671 if (availableSpace
.height
== 0)
2672 availableSpace
.height
= 10;
2674 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2675 if (onlyVisibleRect
)
2677 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2678 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2681 wxClientDC
dc(this);
2682 dc
.SetFont(GetFont());
2686 GetBuffer().Defragment();
2687 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2688 GetBuffer().Layout(dc
, availableSpace
, flags
);
2689 GetBuffer().SetDirty(false);
2698 /// Is all of the selection bold?
2699 bool wxRichTextCtrl::IsSelectionBold()
2703 wxRichTextAttr attr
;
2704 wxRichTextRange range
= GetInternalSelectionRange();
2705 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2706 attr
.SetFontWeight(wxBOLD
);
2708 return HasCharacterAttributes(range
, attr
);
2712 // If no selection, then we need to combine current style with default style
2713 // to see what the effect would be if we started typing.
2714 wxRichTextAttr attr
;
2715 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2717 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2718 if (GetStyle(pos
, attr
))
2720 if (IsDefaultStyleShowing())
2721 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2722 return attr
.GetFontWeight() == wxBOLD
;
2728 /// Is all of the selection italics?
2729 bool wxRichTextCtrl::IsSelectionItalics()
2733 wxRichTextRange range
= GetInternalSelectionRange();
2734 wxRichTextAttr attr
;
2735 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2736 attr
.SetFontStyle(wxITALIC
);
2738 return HasCharacterAttributes(range
, attr
);
2742 // If no selection, then we need to combine current style with default style
2743 // to see what the effect would be if we started typing.
2744 wxRichTextAttr attr
;
2745 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2747 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2748 if (GetStyle(pos
, attr
))
2750 if (IsDefaultStyleShowing())
2751 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2752 return attr
.GetFontStyle() == wxITALIC
;
2758 /// Is all of the selection underlined?
2759 bool wxRichTextCtrl::IsSelectionUnderlined()
2763 wxRichTextRange range
= GetInternalSelectionRange();
2764 wxRichTextAttr attr
;
2765 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2766 attr
.SetFontUnderlined(true);
2768 return HasCharacterAttributes(range
, attr
);
2772 // If no selection, then we need to combine current style with default style
2773 // to see what the effect would be if we started typing.
2774 wxRichTextAttr attr
;
2775 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2776 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2778 if (GetStyle(pos
, attr
))
2780 if (IsDefaultStyleShowing())
2781 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2782 return attr
.GetFontUnderlined();
2788 /// Apply bold to the selection
2789 bool wxRichTextCtrl::ApplyBoldToSelection()
2791 wxRichTextAttr attr
;
2792 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2793 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2796 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2798 SetAndShowDefaultStyle(attr
);
2802 /// Apply italic to the selection
2803 bool wxRichTextCtrl::ApplyItalicToSelection()
2805 wxRichTextAttr attr
;
2806 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2807 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2810 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2812 SetAndShowDefaultStyle(attr
);
2816 /// Apply underline to the selection
2817 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2819 wxRichTextAttr attr
;
2820 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2821 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2824 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2826 SetAndShowDefaultStyle(attr
);
2830 /// Is all of the selection aligned according to the specified flag?
2831 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2833 wxRichTextRange range
;
2835 range
= GetInternalSelectionRange();
2837 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2839 wxRichTextAttr attr
;
2840 attr
.SetAlignment(alignment
);
2842 return HasParagraphAttributes(range
, attr
);
2845 /// Apply alignment to the selection
2846 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2848 wxRichTextAttr attr
;
2849 attr
.SetAlignment(alignment
);
2851 return SetStyle(GetSelectionRange(), attr
);
2854 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2856 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2861 /// Apply a named style to the selection
2862 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2864 // Flags are defined within each definition, so only certain
2865 // attributes are applied.
2866 wxRichTextAttr
attr(def
->GetStyle());
2868 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2870 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2872 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2874 wxRichTextRange range
;
2877 range
= GetSelectionRange();
2880 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2881 range
= wxRichTextRange(pos
, pos
+1);
2884 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2887 // Make sure the attr has the style name
2888 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2890 attr
.SetParagraphStyleName(def
->GetName());
2892 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2893 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2894 // to change its style independently.
2895 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2898 attr
.SetCharacterStyleName(def
->GetName());
2901 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2904 SetAndShowDefaultStyle(attr
);
2909 /// Apply the style sheet to the buffer, for example if the styles have changed.
2910 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2913 styleSheet
= GetBuffer().GetStyleSheet();
2917 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2919 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2927 /// Sets the default style to the style under the cursor
2928 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2931 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2933 // If at the start of a paragraph, use the next position.
2934 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2936 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2937 if (GetUncombinedStyle(pos
, attr
))
2939 if (GetStyle(pos
, attr
))
2942 SetDefaultStyle(attr
);
2949 /// Returns the first visible position in the current view
2950 long wxRichTextCtrl::GetFirstVisiblePosition() const
2952 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2954 return line
->GetAbsoluteRange().GetStart();
2959 /// Get the first visible point in the window
2960 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
2963 int startXUnits
, startYUnits
;
2965 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
2966 GetViewStart(& startXUnits
, & startYUnits
);
2968 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
2971 /// The adjusted caret position is the character position adjusted to take
2972 /// into account whether we're at the start of a paragraph, in which case
2973 /// style information should be taken from the next position, not current one.
2974 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2976 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2978 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2983 /// Get/set the selection range in character positions. -1, -1 means no selection.
2984 /// The range is in API convention, i.e. a single character selection is denoted
2986 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2988 wxRichTextRange range
= GetInternalSelectionRange();
2989 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2990 range
.SetEnd(range
.GetEnd() + 1);
2994 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2996 wxRichTextRange
range1(range
);
2997 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2998 range1
.SetEnd(range1
.GetEnd() - 1);
3000 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3002 SetInternalSelectionRange(range1
);
3006 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3008 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3011 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3013 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3016 /// Clear list for given range
3017 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3019 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3022 /// Number/renumber any list elements in the given range
3023 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3025 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3028 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3030 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3033 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3034 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3036 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3039 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3041 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3044 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3046 if (sm_availableFontNames
.GetCount() == 0)
3048 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3049 sm_availableFontNames
.Sort();
3051 return sm_availableFontNames
;
3054 void wxRichTextCtrl::ClearAvailableFontNames()
3056 sm_availableFontNames
.Clear();