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
)
48 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
50 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
52 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
53 EVT_PAINT(wxRichTextCtrl::OnPaint
)
54 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
55 EVT_IDLE(wxRichTextCtrl::OnIdle
)
56 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
57 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
58 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
59 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
60 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
61 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
62 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
63 EVT_CHAR(wxRichTextCtrl::OnChar
)
64 EVT_SIZE(wxRichTextCtrl::OnSize
)
65 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
66 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
67 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
69 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
70 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
72 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
73 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
75 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
76 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
78 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
79 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
81 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
82 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
84 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
85 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
87 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
88 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
95 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
97 wxRichTextCtrl::wxRichTextCtrl()
98 : wxScrollHelper(this)
103 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
105 const wxString
& value
,
109 : wxScrollHelper(this)
112 Create(parent
, id
, value
, pos
, size
, style
);
116 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
118 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
119 style
|wxFULL_REPAINT_ON_RESIZE
))
124 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
127 GetBuffer().SetRichTextCtrl(this);
129 if (style
& wxTE_READONLY
)
132 wxTextAttrEx attributes
;
133 attributes
.SetFont(GetFont());
134 attributes
.SetTextColour(*wxBLACK
);
135 attributes
.SetBackgroundColour(*wxWHITE
);
136 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
137 attributes
.SetLineSpacing(10);
138 attributes
.SetParagraphSpacingAfter(10);
139 attributes
.SetParagraphSpacingBefore(0);
140 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
141 SetBasicStyle(attributes
);
143 // The default attributes will be merged with base attributes, so
144 // can be empty to begin with
145 wxTextAttrEx defaultAttributes
;
146 SetDefaultStyle(defaultAttributes
);
148 SetBackgroundColour(*wxWHITE
);
149 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
151 // Tell the sizers to use the given or best size
152 SetBestFittingSize(size
);
154 #if wxRICHTEXT_BUFFERED_PAINTING
156 RecreateBuffer(size
);
159 SetCursor(wxCursor(wxCURSOR_IBEAM
));
161 if (!value
.IsEmpty())
167 wxRichTextCtrl::~wxRichTextCtrl()
169 delete m_contextMenu
;
172 /// Member initialisation
173 void wxRichTextCtrl::Init()
176 m_contextMenu
= NULL
;
178 m_caretPosition
= -1;
179 m_selectionRange
.SetRange(-2, -2);
180 m_selectionAnchor
= -2;
182 m_caretAtLineStart
= false;
184 m_fullLayoutRequired
= false;
185 m_fullLayoutTime
= 0;
186 m_fullLayoutSavedPosition
= 0;
187 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
188 m_caretPositionForDefaultStyle
= -2;
191 /// Call Freeze to prevent refresh
192 void wxRichTextCtrl::Freeze()
197 /// Call Thaw to refresh
198 void wxRichTextCtrl::Thaw()
202 if (m_freezeCount
== 0)
210 void wxRichTextCtrl::Clear()
213 m_buffer
.SetDirty(true);
214 m_caretPosition
= -1;
215 m_caretPositionForDefaultStyle
= -2;
216 m_caretAtLineStart
= false;
217 m_selectionRange
.SetRange(-2, -2);
219 SetScrollbars(0, 0, 0, 0, 0, 0);
221 if (m_freezeCount
== 0)
226 SendTextUpdatedEvent();
230 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
236 #if wxRICHTEXT_BUFFERED_PAINTING
237 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
243 if (m_freezeCount
> 0)
246 dc
.SetFont(GetFont());
248 // Paint the background
251 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
252 wxRect
availableSpace(GetClientSize());
253 if (GetBuffer().GetDirty())
255 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
256 GetBuffer().SetDirty(false);
260 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
269 // Empty implementation, to prevent flicker
270 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
274 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
276 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
285 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
294 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
300 dc
.SetFont(GetFont());
303 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
305 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
307 m_dragStart
= event
.GetLogicalPosition(dc
);
313 bool caretAtLineStart
= false;
315 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
317 // If we're at the start of a line (but not first in para)
318 // then we should keep the caret showing at the start of the line
319 // by showing the m_caretAtLineStart flag.
320 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
321 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
323 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
324 caretAtLineStart
= true;
328 MoveCaret(position
, caretAtLineStart
);
329 SetDefaultStyleToCursorStyle();
336 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
341 if (GetCapture() == this)
347 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
349 if (!event
.Dragging())
357 dc
.SetFont(GetFont());
360 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
361 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
363 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
365 // TODO: test closeness
367 bool caretAtLineStart
= false;
369 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
371 // If we're at the start of a line (but not first in para)
372 // then we should keep the caret showing at the start of the line
373 // by showing the m_caretAtLineStart flag.
374 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
375 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
377 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
378 caretAtLineStart
= true;
382 if (m_caretPosition
!= position
)
384 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
386 MoveCaret(position
, caretAtLineStart
);
387 SetDefaultStyleToCursorStyle();
396 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
402 /// Left-double-click
403 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
405 SelectWord(GetCaretPosition()+1);
410 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
416 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
420 flags
|= wxRICHTEXT_CTRL_DOWN
;
421 if (event
.ShiftDown())
422 flags
|= wxRICHTEXT_SHIFT_DOWN
;
424 flags
|= wxRICHTEXT_ALT_DOWN
;
426 if (event
.GetKeyCode() == WXK_LEFT
||
427 event
.GetKeyCode() == WXK_RIGHT
||
428 event
.GetKeyCode() == WXK_UP
||
429 event
.GetKeyCode() == WXK_DOWN
||
430 event
.GetKeyCode() == WXK_HOME
||
431 event
.GetKeyCode() == WXK_PAGEUP
||
432 event
.GetKeyCode() == WXK_PAGEDOWN
||
433 event
.GetKeyCode() == WXK_END
||
435 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
436 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
437 event
.GetKeyCode() == WXK_NUMPAD_UP
||
438 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
439 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
440 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
441 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
442 event
.GetKeyCode() == WXK_NUMPAD_END
)
444 KeyboardNavigate(event
.GetKeyCode(), flags
);
448 // all the other keys modify the controls contents which shouldn't be
449 // possible if we're read-only
456 if (event
.GetKeyCode() == WXK_RETURN
)
458 BeginBatchUndo(_("Insert Text"));
460 long newPos
= m_caretPosition
;
462 DeleteSelectedContent(& newPos
);
464 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
466 wxRichTextEvent
cmdEvent(
467 wxEVT_COMMAND_RICHTEXT_RETURN
,
469 cmdEvent
.SetEventObject(this);
470 cmdEvent
.SetFlags(flags
);
471 GetEventHandler()->ProcessEvent(cmdEvent
);
474 SetDefaultStyleToCursorStyle();
476 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
478 else if (event
.GetKeyCode() == WXK_BACK
)
480 BeginBatchUndo(_("Delete Text"));
482 // Submit range in character positions, which are greater than caret positions,
483 // so subtract 1 for deleted character and add 1 for conversion to character position.
484 if (m_caretPosition
> -1 && !HasSelection())
486 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
487 m_caretPosition
, // Current caret position
488 m_caretPosition
-1, // New caret position
492 DeleteSelectedContent();
496 // Shouldn't this be in Do()?
497 if (GetLastPosition() == -1)
501 m_caretPosition
= -1;
503 SetDefaultStyleToCursorStyle();
506 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
508 else if (event
.GetKeyCode() == WXK_DELETE
)
510 BeginBatchUndo(_("Delete Text"));
512 // Submit range in character positions, which are greater than caret positions,
513 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
515 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
516 m_caretPosition
, // Current caret position
517 m_caretPosition
+1, // New caret position
521 DeleteSelectedContent();
525 // Shouldn't this be in Do()?
526 if (GetLastPosition() == -1)
530 m_caretPosition
= -1;
532 SetDefaultStyleToCursorStyle();
537 long keycode
= event
.GetKeyCode();
611 case WXK_NUMPAD_SPACE
:
613 case WXK_NUMPAD_ENTER
:
618 case WXK_NUMPAD_HOME
:
619 case WXK_NUMPAD_LEFT
:
621 case WXK_NUMPAD_RIGHT
:
622 case WXK_NUMPAD_DOWN
:
623 case WXK_NUMPAD_PAGEUP
:
624 case WXK_NUMPAD_PAGEDOWN
:
626 case WXK_NUMPAD_BEGIN
:
627 case WXK_NUMPAD_INSERT
:
628 case WXK_NUMPAD_DELETE
:
629 case WXK_NUMPAD_EQUAL
:
630 case WXK_NUMPAD_MULTIPLY
:
632 case WXK_NUMPAD_SEPARATOR
:
633 case WXK_NUMPAD_SUBTRACT
:
634 case WXK_NUMPAD_DECIMAL
:
642 if (event
.CmdDown() || event
.AltDown())
648 if (keycode
== wxT('\t'))
650 // See if we need to promote or demote the selection or paragraph at the cursor
651 // position, instead of inserting a tab.
652 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
653 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
654 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
656 wxRichTextRange range
;
658 range
= GetSelectionRange();
660 range
= para
->GetRange().FromInternal();
662 int promoteBy
= event
.ShiftDown() ? 1 : -1;
664 PromoteList(promoteBy
, range
, NULL
);
669 BeginBatchUndo(_("Insert Text"));
671 long newPos
= m_caretPosition
;
672 DeleteSelectedContent(& newPos
);
674 wxString str
= (wxChar
) event
.GetKeyCode();
675 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
679 SetDefaultStyleToCursorStyle();
680 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
686 /// Delete content if there is a selection, e.g. when pressing a key.
687 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
691 long pos
= m_selectionRange
.GetStart();
692 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
693 m_caretPosition
, // Current caret position
694 pos
, // New caret position
696 m_selectionRange
.SetRange(-2, -2);
706 /// Keyboard navigation
710 Left: left one character
711 Right: right one character
714 Ctrl-Left: left one word
715 Ctrl-Right: right one word
716 Ctrl-Up: previous paragraph start
717 Ctrl-Down: next start of paragraph
720 Ctrl-Home: start of document
721 Ctrl-End: end of document
723 Page-Down: Down a screen
727 Ctrl-Alt-PgUp: Start of window
728 Ctrl-Alt-PgDn: End of window
729 F8: Start selection mode
730 Esc: End selection mode
732 Adding Shift does the above but starts/extends selection.
737 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
739 bool success
= false;
741 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
743 if (flags
& wxRICHTEXT_CTRL_DOWN
)
744 success
= WordRight(1, flags
);
746 success
= MoveRight(1, flags
);
748 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
750 if (flags
& wxRICHTEXT_CTRL_DOWN
)
751 success
= WordLeft(1, flags
);
753 success
= MoveLeft(1, flags
);
755 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
757 if (flags
& wxRICHTEXT_CTRL_DOWN
)
758 success
= MoveToParagraphStart(flags
);
760 success
= MoveUp(1, flags
);
762 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
764 if (flags
& wxRICHTEXT_CTRL_DOWN
)
765 success
= MoveToParagraphEnd(flags
);
767 success
= MoveDown(1, flags
);
769 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
771 success
= PageUp(1, flags
);
773 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
775 success
= PageDown(1, flags
);
777 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
779 if (flags
& wxRICHTEXT_CTRL_DOWN
)
780 success
= MoveHome(flags
);
782 success
= MoveToLineStart(flags
);
784 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
786 if (flags
& wxRICHTEXT_CTRL_DOWN
)
787 success
= MoveEnd(flags
);
789 success
= MoveToLineEnd(flags
);
794 ScrollIntoView(m_caretPosition
, keyCode
);
795 SetDefaultStyleToCursorStyle();
801 /// Extend the selection. Selections are in caret positions.
802 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
804 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
806 // If not currently selecting, start selecting
807 if (m_selectionRange
.GetStart() == -2)
809 m_selectionAnchor
= oldPos
;
812 m_selectionRange
.SetRange(newPos
+1, oldPos
);
814 m_selectionRange
.SetRange(oldPos
+1, newPos
);
818 // Always ensure that the selection range start is greater than
820 if (newPos
> m_selectionAnchor
)
821 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
823 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
826 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
828 wxLogDebug(wxT("Strange selection range"));
837 /// Scroll into view, returning true if we scrolled.
838 /// This takes a _caret_ position.
839 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
841 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
847 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
849 int startXUnits
, startYUnits
;
850 GetViewStart(& startXUnits
, & startYUnits
);
851 int startY
= startYUnits
* ppuY
;
854 GetVirtualSize(& sx
, & sy
);
860 wxRect rect
= line
->GetRect();
862 bool scrolled
= false;
864 wxSize clientSize
= GetClientSize();
867 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
868 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
869 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
870 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
872 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
874 // Make it scroll so this item is at the bottom
876 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
877 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
879 // If we're still off the screen, scroll another line down
880 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
883 if (startYUnits
!= yUnits
)
885 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
889 else if (rect
.y
< startY
)
891 // Make it scroll so this item is at the top
894 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
896 if (startYUnits
!= yUnits
)
898 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
904 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
905 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
906 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
907 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
911 // Make it scroll so this item is at the top
914 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
916 if (startYUnits
!= yUnits
)
918 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
922 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
924 // Make it scroll so this item is at the bottom
926 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
927 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
929 // If we're still off the screen, scroll another line down
930 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
933 if (startYUnits
!= yUnits
)
935 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
945 /// Is the given position visible on the screen?
946 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
948 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
954 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
957 GetViewStart(& startX
, & startY
);
959 startY
= startY
* ppuY
;
962 GetVirtualSize(& sx
, & sy
);
967 wxRect rect
= line
->GetRect();
969 wxSize clientSize
= GetClientSize();
971 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
974 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
976 m_caretPosition
= position
;
977 m_caretAtLineStart
= showAtLineStart
;
980 /// Move caret one visual step forward: this may mean setting a flag
981 /// and keeping the same position if we're going from the end of one line
982 /// to the start of the next, which may be the exact same caret position.
983 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
985 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
987 // Only do the check if we're not at the end of the paragraph (where things work OK
989 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
991 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
995 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
997 // We're at the end of a line. See whether we need to
998 // stay at the same actual caret position but change visual
1000 if (oldPosition
== lineRange
.GetEnd())
1002 if (m_caretAtLineStart
)
1004 // We're already at the start of the line, so actually move on now.
1005 m_caretPosition
= oldPosition
+ 1;
1006 m_caretAtLineStart
= false;
1010 // We're showing at the end of the line, so keep to
1011 // the same position but indicate that we're to show
1012 // at the start of the next line.
1013 m_caretPosition
= oldPosition
;
1014 m_caretAtLineStart
= true;
1016 SetDefaultStyleToCursorStyle();
1022 SetDefaultStyleToCursorStyle();
1025 /// Move caret one visual step backward: this may mean setting a flag
1026 /// and keeping the same position if we're going from the end of one line
1027 /// to the start of the next, which may be the exact same caret position.
1028 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1030 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1032 // Only do the check if we're not at the start of the paragraph (where things work OK
1034 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1036 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1040 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1042 // We're at the start of a line. See whether we need to
1043 // stay at the same actual caret position but change visual
1044 // position, or not.
1045 if (oldPosition
== lineRange
.GetStart())
1047 m_caretPosition
= oldPosition
-1;
1048 m_caretAtLineStart
= true;
1051 else if (oldPosition
== lineRange
.GetEnd())
1053 if (m_caretAtLineStart
)
1055 // We're at the start of the line, so keep the same caret position
1056 // but clear the start-of-line flag.
1057 m_caretPosition
= oldPosition
;
1058 m_caretAtLineStart
= false;
1062 // We're showing at the end of the line, so go back
1063 // to the previous character position.
1064 m_caretPosition
= oldPosition
- 1;
1066 SetDefaultStyleToCursorStyle();
1072 SetDefaultStyleToCursorStyle();
1076 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1078 long endPos
= GetBuffer().GetRange().GetEnd();
1080 if (m_caretPosition
+ noPositions
< endPos
)
1082 long oldPos
= m_caretPosition
;
1083 long newPos
= m_caretPosition
+ noPositions
;
1085 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1089 // Determine by looking at oldPos and m_caretPosition whether
1090 // we moved from the end of a line to the start of the next line, in which case
1091 // we want to adjust the caret position such that it is positioned at the
1092 // start of the next line, rather than jumping past the first character of the
1094 if (noPositions
== 1 && !extendSel
)
1095 MoveCaretForward(oldPos
);
1097 SetCaretPosition(newPos
);
1100 SetDefaultStyleToCursorStyle();
1111 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1115 if (m_caretPosition
> startPos
- noPositions
+ 1)
1117 long oldPos
= m_caretPosition
;
1118 long newPos
= m_caretPosition
- noPositions
;
1119 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1123 if (noPositions
== 1 && !extendSel
)
1124 MoveCaretBack(oldPos
);
1126 SetCaretPosition(newPos
);
1129 SetDefaultStyleToCursorStyle();
1140 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1142 return MoveDown(- noLines
, flags
);
1146 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1151 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1152 wxPoint pt
= GetCaret()->GetPosition();
1153 long newLine
= lineNumber
+ noLines
;
1155 if (lineNumber
!= -1)
1159 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1161 if (newLine
> lastLine
)
1171 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1174 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1180 wxClientDC
dc(this);
1182 dc
.SetFont(GetFont());
1184 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1186 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1188 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1189 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1190 // so we view the caret at the start of the line.
1191 bool caretLineStart
= false;
1192 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1194 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1195 wxRichTextRange lineRange
;
1197 lineRange
= thisLine
->GetAbsoluteRange();
1199 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1202 caretLineStart
= true;
1206 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1207 if (para
&& para
->GetRange().GetStart() == newPos
)
1212 long newSelEnd
= newPos
;
1214 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1218 SetCaretPosition(newPos
, caretLineStart
);
1220 SetDefaultStyleToCursorStyle();
1230 /// Move to the end of the paragraph
1231 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1233 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1236 long newPos
= para
->GetRange().GetEnd() - 1;
1237 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1241 SetCaretPosition(newPos
);
1243 SetDefaultStyleToCursorStyle();
1253 /// Move to the start of the paragraph
1254 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1256 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1259 long newPos
= para
->GetRange().GetStart() - 1;
1260 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1264 SetCaretPosition(newPos
);
1266 SetDefaultStyleToCursorStyle();
1276 /// Move to the end of the line
1277 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1279 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1283 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1284 long newPos
= lineRange
.GetEnd();
1285 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1289 SetCaretPosition(newPos
);
1291 SetDefaultStyleToCursorStyle();
1301 /// Move to the start of the line
1302 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1304 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1307 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1308 long newPos
= lineRange
.GetStart()-1;
1310 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1314 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1316 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1318 SetDefaultStyleToCursorStyle();
1328 /// Move to the start of the buffer
1329 bool wxRichTextCtrl::MoveHome(int flags
)
1331 if (m_caretPosition
!= -1)
1333 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1337 SetCaretPosition(-1);
1339 SetDefaultStyleToCursorStyle();
1349 /// Move to the end of the buffer
1350 bool wxRichTextCtrl::MoveEnd(int flags
)
1352 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1354 if (m_caretPosition
!= endPos
)
1356 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1360 SetCaretPosition(endPos
);
1362 SetDefaultStyleToCursorStyle();
1372 /// Move noPages pages up
1373 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1375 return PageDown(- noPages
, flags
);
1378 /// Move noPages pages down
1379 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1381 // Calculate which line occurs noPages * screen height further down.
1382 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1385 wxSize clientSize
= GetClientSize();
1386 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1388 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1391 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1392 long pos
= lineRange
.GetStart()-1;
1393 if (pos
!= m_caretPosition
)
1395 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1397 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1401 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1403 SetDefaultStyleToCursorStyle();
1415 // Finds the caret position for the next word
1416 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1418 long endPos
= GetBuffer().GetRange().GetEnd();
1422 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1424 // First skip current text to space
1425 while (i
< endPos
&& i
> -1)
1427 // i is in character, not caret positions
1428 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1429 if (text
!= wxT(" ") && !text
.empty())
1436 while (i
< endPos
&& i
> -1)
1438 // i is in character, not caret positions
1439 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1440 if (text
.empty()) // End of paragraph, or maybe an image
1441 return wxMax(-1, i
- 1);
1442 else if (text
== wxT(" ") || text
.empty())
1446 // Convert to caret position
1447 return wxMax(-1, i
- 1);
1456 long i
= m_caretPosition
;
1458 // First skip white space
1459 while (i
< endPos
&& i
> -1)
1461 // i is in character, not caret positions
1462 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1463 if (text
.empty()) // End of paragraph, or maybe an image
1465 else if (text
== wxT(" ") || text
.empty())
1470 // Next skip current text to space
1471 while (i
< endPos
&& i
> -1)
1473 // i is in character, not caret positions
1474 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1475 if (text
!= wxT(" ") /* && !text.empty() */)
1488 /// Move n words left
1489 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1491 long pos
= FindNextWordPosition(-1);
1492 if (pos
!= m_caretPosition
)
1494 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1496 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1500 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1502 SetDefaultStyleToCursorStyle();
1512 /// Move n words right
1513 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1515 long pos
= FindNextWordPosition(1);
1516 if (pos
!= m_caretPosition
)
1518 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1520 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1524 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1526 SetDefaultStyleToCursorStyle();
1537 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1539 // Only do sizing optimization for large buffers
1540 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1542 m_fullLayoutRequired
= true;
1543 m_fullLayoutTime
= wxGetLocalTimeMillis();
1544 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1545 LayoutContent(true /* onlyVisibleRect */);
1548 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1550 #if wxRICHTEXT_BUFFERED_PAINTING
1558 /// Idle-time processing
1559 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1561 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1563 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1565 m_fullLayoutRequired
= false;
1566 m_fullLayoutTime
= 0;
1567 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1568 ShowPosition(m_fullLayoutSavedPosition
);
1572 if (m_caretPositionForDefaultStyle
!= -2)
1574 // If the caret position has changed, no longer reflect the default style
1576 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1577 m_caretPositionForDefaultStyle
= -2;
1584 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1590 /// Set up scrollbars, e.g. after a resize
1591 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1596 if (GetBuffer().IsEmpty())
1598 SetScrollbars(0, 0, 0, 0, 0, 0);
1602 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1603 // of pixels. See e.g. wxVScrolledWindow for ideas.
1604 int pixelsPerUnit
= 5;
1605 wxSize clientSize
= GetClientSize();
1607 int maxHeight
= GetBuffer().GetCachedSize().y
;
1609 // Round up so we have at least maxHeight pixels
1610 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1612 int startX
= 0, startY
= 0;
1614 GetViewStart(& startX
, & startY
);
1616 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1617 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1619 // Move to previous scroll position if
1621 SetScrollbars(0, pixelsPerUnit
,
1623 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1626 /// Paint the background
1627 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1629 wxColour backgroundColour
= GetBackgroundColour();
1630 if (!backgroundColour
.Ok())
1631 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1633 // Clear the background
1634 dc
.SetBrush(wxBrush(backgroundColour
));
1635 dc
.SetPen(*wxTRANSPARENT_PEN
);
1636 wxRect
windowRect(GetClientSize());
1637 windowRect
.x
-= 2; windowRect
.y
-= 2;
1638 windowRect
.width
+= 4; windowRect
.height
+= 4;
1640 // We need to shift the rectangle to take into account
1641 // scrolling. Converting device to logical coordinates.
1642 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1643 dc
.DrawRectangle(windowRect
);
1646 #if wxRICHTEXT_BUFFERED_PAINTING
1647 /// Recreate buffer bitmap if necessary
1648 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1651 if (sz
== wxDefaultSize
)
1652 sz
= GetClientSize();
1654 if (sz
.x
< 1 || sz
.y
< 1)
1657 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1658 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1659 return m_bufferBitmap
.Ok();
1663 // ----------------------------------------------------------------------------
1664 // file IO functions
1665 // ----------------------------------------------------------------------------
1667 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1669 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1671 m_filename
= filename
;
1674 SetInsertionPoint(0);
1677 SetupScrollbars(true);
1679 SendTextUpdatedEvent();
1685 wxLogError(_("File couldn't be loaded."));
1691 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1693 if (GetBuffer().SaveFile(filename
, fileType
))
1695 m_filename
= filename
;
1702 wxLogError(_("The text couldn't be saved."));
1707 // ----------------------------------------------------------------------------
1708 // wxRichTextCtrl specific functionality
1709 // ----------------------------------------------------------------------------
1711 /// Add a new paragraph of text to the end of the buffer
1712 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1714 return GetBuffer().AddParagraph(text
);
1718 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1720 return GetBuffer().AddImage(image
);
1723 // ----------------------------------------------------------------------------
1724 // selection and ranges
1725 // ----------------------------------------------------------------------------
1727 void wxRichTextCtrl::SelectAll()
1729 SetSelection(0, GetLastPosition()+1);
1730 m_selectionAnchor
= -1;
1734 void wxRichTextCtrl::SelectNone()
1736 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1737 SetSelection(-2, -2);
1738 m_selectionAnchor
= -2;
1741 static bool wxIsWordDelimiter(const wxString
& text
)
1743 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1746 /// Select the word at the given character position
1747 bool wxRichTextCtrl::SelectWord(long position
)
1749 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1752 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1756 long positionStart
= position
;
1757 long positionEnd
= position
;
1759 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1761 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1762 if (wxIsWordDelimiter(text
))
1768 if (positionStart
< para
->GetRange().GetStart())
1769 positionStart
= para
->GetRange().GetStart();
1771 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1773 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1774 if (wxIsWordDelimiter(text
))
1780 if (positionEnd
>= para
->GetRange().GetEnd())
1781 positionEnd
= para
->GetRange().GetEnd();
1783 SetSelection(positionStart
, positionEnd
+1);
1785 if (positionStart
>= 0)
1787 MoveCaret(positionStart
-1, true);
1788 SetDefaultStyleToCursorStyle();
1794 wxString
wxRichTextCtrl::GetStringSelection() const
1797 GetSelection(&from
, &to
);
1799 return GetRange(from
, to
);
1802 // ----------------------------------------------------------------------------
1804 // ----------------------------------------------------------------------------
1806 wxTextCtrlHitTestResult
1807 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1809 // implement in terms of the other overload as the native ports typically
1810 // can get the position and not (x, y) pair directly (although wxUniv
1811 // directly gets x and y -- and so overrides this method as well)
1813 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1815 if ( rc
!= wxTE_HT_UNKNOWN
)
1817 PositionToXY(pos
, x
, y
);
1823 wxTextCtrlHitTestResult
1824 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1827 wxClientDC
dc((wxRichTextCtrl
*) this);
1828 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1830 // Buffer uses logical position (relative to start of buffer)
1832 wxPoint pt2
= GetLogicalPoint(pt
);
1834 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1838 case wxRICHTEXT_HITTEST_BEFORE
:
1839 return wxTE_HT_BEFORE
;
1841 case wxRICHTEXT_HITTEST_AFTER
:
1842 return wxTE_HT_BEYOND
;
1844 case wxRICHTEXT_HITTEST_ON
:
1845 return wxTE_HT_ON_TEXT
;
1848 return wxTE_HT_UNKNOWN
;
1851 // ----------------------------------------------------------------------------
1852 // set/get the controls text
1853 // ----------------------------------------------------------------------------
1855 wxString
wxRichTextCtrl::GetValue() const
1857 return GetBuffer().GetText();
1860 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1862 // Public API for range is different from internals
1863 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1866 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1870 // if the text is long enough, it's faster to just set it instead of first
1871 // comparing it with the old one (chances are that it will be different
1872 // anyhow, this comparison is there to avoid flicker for small single-line
1873 // edit controls mostly)
1874 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1878 // for compatibility, don't move the cursor when doing SetValue()
1879 SetInsertionPoint(0);
1883 if ( flags
& SetValue_SendEvent
)
1885 // still send an event for consistency
1886 SendTextUpdatedEvent();
1890 // we should reset the modified flag even if the value didn't really change
1892 // mark the control as being not dirty - we changed its text, not the
1897 void wxRichTextCtrl::WriteText(const wxString
& value
)
1902 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1904 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1906 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1908 if ( flags
& SetValue_SendEvent
)
1909 SendTextUpdatedEvent();
1912 void wxRichTextCtrl::AppendText(const wxString
& text
)
1914 SetInsertionPointEnd();
1919 /// Write an image at the current insertion point
1920 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1922 wxRichTextImageBlock imageBlock
;
1924 wxImage image2
= image
;
1925 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1926 return WriteImage(imageBlock
);
1931 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1933 wxRichTextImageBlock imageBlock
;
1936 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1937 return WriteImage(imageBlock
);
1942 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1944 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1947 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1951 wxRichTextImageBlock imageBlock
;
1953 wxImage image
= bitmap
.ConvertToImage();
1954 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1955 return WriteImage(imageBlock
);
1961 /// Insert a newline (actually paragraph) at the current insertion point.
1962 bool wxRichTextCtrl::Newline()
1964 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1968 // ----------------------------------------------------------------------------
1969 // Clipboard operations
1970 // ----------------------------------------------------------------------------
1972 void wxRichTextCtrl::Copy()
1976 wxRichTextRange range
= GetInternalSelectionRange();
1977 GetBuffer().CopyToClipboard(range
);
1981 void wxRichTextCtrl::Cut()
1985 wxRichTextRange range
= GetInternalSelectionRange();
1986 GetBuffer().CopyToClipboard(range
);
1988 DeleteSelectedContent();
1994 void wxRichTextCtrl::Paste()
1998 BeginBatchUndo(_("Paste"));
2000 long newPos
= m_caretPosition
;
2001 DeleteSelectedContent(& newPos
);
2003 GetBuffer().PasteFromClipboard(newPos
);
2009 void wxRichTextCtrl::DeleteSelection()
2011 if (CanDeleteSelection())
2013 DeleteSelectedContent();
2017 bool wxRichTextCtrl::HasSelection() const
2019 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2022 bool wxRichTextCtrl::CanCopy() const
2024 // Can copy if there's a selection
2025 return HasSelection();
2028 bool wxRichTextCtrl::CanCut() const
2030 return HasSelection() && IsEditable();
2033 bool wxRichTextCtrl::CanPaste() const
2035 if ( !IsEditable() )
2038 return GetBuffer().CanPasteFromClipboard();
2041 bool wxRichTextCtrl::CanDeleteSelection() const
2043 return HasSelection() && IsEditable();
2047 // ----------------------------------------------------------------------------
2049 // ----------------------------------------------------------------------------
2051 void wxRichTextCtrl::SetEditable(bool editable
)
2053 m_editable
= editable
;
2056 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2060 m_caretPosition
= pos
- 1;
2063 void wxRichTextCtrl::SetInsertionPointEnd()
2065 long pos
= GetLastPosition();
2066 SetInsertionPoint(pos
);
2069 long wxRichTextCtrl::GetInsertionPoint() const
2071 return m_caretPosition
+1;
2074 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2076 return GetBuffer().GetRange().GetEnd();
2079 // If the return values from and to are the same, there is no
2081 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2083 *from
= m_selectionRange
.GetStart();
2084 *to
= m_selectionRange
.GetEnd();
2085 if ((*to
) != -1 && (*to
) != -2)
2089 bool wxRichTextCtrl::IsEditable() const
2094 // ----------------------------------------------------------------------------
2096 // ----------------------------------------------------------------------------
2098 void wxRichTextCtrl::SetSelection(long from
, long to
)
2100 // if from and to are both -1, it means (in wxWidgets) that all text should
2102 if ( (from
== -1) && (to
== -1) )
2105 to
= GetLastPosition()+1;
2108 DoSetSelection(from
, to
);
2111 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2113 m_selectionAnchor
= from
;
2114 m_selectionRange
.SetRange(from
, to
-1);
2120 // ----------------------------------------------------------------------------
2122 // ----------------------------------------------------------------------------
2124 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2125 const wxString
& value
)
2127 BeginBatchUndo(_("Replace"));
2129 DeleteSelectedContent();
2131 DoWriteText(value
, SetValue_SelectionOnly
);
2136 void wxRichTextCtrl::Remove(long from
, long to
)
2140 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2141 m_caretPosition
, // Current caret position
2142 from
, // New caret position
2150 bool wxRichTextCtrl::IsModified() const
2152 return m_buffer
.IsModified();
2155 void wxRichTextCtrl::MarkDirty()
2157 m_buffer
.Modify(true);
2160 void wxRichTextCtrl::DiscardEdits()
2162 m_caretPositionForDefaultStyle
= -2;
2163 m_buffer
.Modify(false);
2164 m_buffer
.GetCommandProcessor()->ClearCommands();
2167 int wxRichTextCtrl::GetNumberOfLines() const
2169 return GetBuffer().GetParagraphCount();
2172 // ----------------------------------------------------------------------------
2173 // Positions <-> coords
2174 // ----------------------------------------------------------------------------
2176 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2178 return GetBuffer().XYToPosition(x
, y
);
2181 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2183 return GetBuffer().PositionToXY(pos
, x
, y
);
2186 // ----------------------------------------------------------------------------
2188 // ----------------------------------------------------------------------------
2190 void wxRichTextCtrl::ShowPosition(long pos
)
2192 if (!IsPositionVisible(pos
))
2193 ScrollIntoView(pos
-1, WXK_DOWN
);
2196 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2198 return GetBuffer().GetParagraphLength(lineNo
);
2201 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2203 return GetBuffer().GetParagraphText(lineNo
);
2206 // ----------------------------------------------------------------------------
2208 // ----------------------------------------------------------------------------
2210 void wxRichTextCtrl::Undo()
2214 GetCommandProcessor()->Undo();
2218 void wxRichTextCtrl::Redo()
2222 GetCommandProcessor()->Redo();
2226 bool wxRichTextCtrl::CanUndo() const
2228 return GetCommandProcessor()->CanUndo();
2231 bool wxRichTextCtrl::CanRedo() const
2233 return GetCommandProcessor()->CanRedo();
2236 // ----------------------------------------------------------------------------
2237 // implementation details
2238 // ----------------------------------------------------------------------------
2240 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2242 SetValue(event
.GetString());
2243 GetEventHandler()->ProcessEvent(event
);
2246 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2248 // By default, load the first file into the text window.
2249 if (event
.GetNumberOfFiles() > 0)
2251 LoadFile(event
.GetFiles()[0]);
2255 wxSize
wxRichTextCtrl::DoGetBestSize() const
2257 return wxSize(10, 10);
2260 // ----------------------------------------------------------------------------
2261 // standard handlers for standard edit menu events
2262 // ----------------------------------------------------------------------------
2264 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2269 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2274 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2279 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2284 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2289 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2294 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2296 event
.Enable( CanCut() );
2299 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2301 event
.Enable( CanCopy() );
2304 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2306 event
.Enable( CanDeleteSelection() );
2309 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2311 event
.Enable( CanPaste() );
2314 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2316 event
.Enable( CanUndo() );
2317 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2320 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2322 event
.Enable( CanRedo() );
2323 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2326 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2331 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2333 event
.Enable(GetLastPosition() > 0);
2336 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2340 m_contextMenu
= new wxMenu
;
2341 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2342 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2343 m_contextMenu
->AppendSeparator();
2344 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2345 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2346 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2347 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2348 m_contextMenu
->AppendSeparator();
2349 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2351 PopupMenu(m_contextMenu
);
2355 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2357 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2360 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2362 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2365 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2367 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2370 // extended style setting operation with flags including:
2371 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2372 // see richtextbuffer.h for more details.
2373 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2375 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2378 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2380 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2383 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2385 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2388 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2390 return GetBuffer().SetDefaultStyle(style
);
2393 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2395 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2398 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2400 return GetBuffer().GetDefaultStyle();
2403 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2405 return GetBuffer().GetDefaultStyle();
2408 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2410 wxTextAttrEx
attr(style
);
2411 if (GetBuffer().GetStyle(position
, attr
))
2420 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2422 return GetBuffer().GetStyle(position
, style
);
2425 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2427 return GetBuffer().GetStyle(position
, style
);
2430 /// Get the content (uncombined) attributes for this position.
2432 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2434 wxTextAttrEx
attr(style
);
2435 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2444 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2446 return GetBuffer().GetUncombinedStyle(position
, style
);
2449 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2451 return GetBuffer().GetUncombinedStyle(position
, style
);
2454 /// Set font, and also the buffer attributes
2455 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2457 wxControl::SetFont(font
);
2459 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2461 GetBuffer().SetBasicStyle(attr
);
2463 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2469 /// Transform logical to physical
2470 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2473 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2478 /// Transform physical to logical
2479 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2482 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2487 /// Position the caret
2488 void wxRichTextCtrl::PositionCaret()
2493 //wxLogDebug(wxT("PositionCaret"));
2496 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2498 wxPoint originalPt
= caretRect
.GetPosition();
2499 wxPoint pt
= GetPhysicalPoint(originalPt
);
2500 if (GetCaret()->GetPosition() != pt
)
2502 GetCaret()->Move(pt
);
2503 GetCaret()->SetSize(caretRect
.GetSize());
2508 /// Get the caret height and position for the given character position
2509 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2511 wxClientDC
dc(this);
2512 dc
.SetFont(GetFont());
2519 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2521 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2528 /// Gets the line for the visible caret position. If the caret is
2529 /// shown at the very end of the line, it means the next character is actually
2530 /// on the following line. So let's get the line we're expecting to find
2531 /// if this is the case.
2532 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2534 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2535 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2538 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2539 if (caretPosition
== lineRange
.GetStart()-1 &&
2540 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2542 if (!m_caretAtLineStart
)
2543 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2550 /// Move the caret to the given character position
2551 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2553 if (GetBuffer().GetDirty())
2556 if (pos
<= GetBuffer().GetRange().GetEnd())
2558 SetCaretPosition(pos
, showAtLineStart
);
2568 /// Layout the buffer: which we must do before certain operations, such as
2569 /// setting the caret position.
2570 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2572 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2574 wxRect
availableSpace(GetClientSize());
2575 if (availableSpace
.width
== 0)
2576 availableSpace
.width
= 10;
2577 if (availableSpace
.height
== 0)
2578 availableSpace
.height
= 10;
2580 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2581 if (onlyVisibleRect
)
2583 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2584 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2587 wxClientDC
dc(this);
2588 dc
.SetFont(GetFont());
2592 GetBuffer().Defragment();
2593 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2594 GetBuffer().Layout(dc
, availableSpace
, flags
);
2595 GetBuffer().SetDirty(false);
2604 /// Is all of the selection bold?
2605 bool wxRichTextCtrl::IsSelectionBold()
2609 wxRichTextAttr attr
;
2610 wxRichTextRange range
= GetInternalSelectionRange();
2611 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2612 attr
.SetFontWeight(wxBOLD
);
2614 return HasCharacterAttributes(range
, attr
);
2618 // If no selection, then we need to combine current style with default style
2619 // to see what the effect would be if we started typing.
2620 wxRichTextAttr attr
;
2621 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2623 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2624 if (GetStyle(pos
, attr
))
2626 if (IsDefaultStyleShowing())
2627 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2628 return attr
.GetFontWeight() == wxBOLD
;
2634 /// Is all of the selection italics?
2635 bool wxRichTextCtrl::IsSelectionItalics()
2639 wxRichTextRange range
= GetInternalSelectionRange();
2640 wxRichTextAttr attr
;
2641 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2642 attr
.SetFontStyle(wxITALIC
);
2644 return HasCharacterAttributes(range
, attr
);
2648 // If no selection, then we need to combine current style with default style
2649 // to see what the effect would be if we started typing.
2650 wxRichTextAttr attr
;
2651 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2653 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2654 if (GetStyle(pos
, attr
))
2656 if (IsDefaultStyleShowing())
2657 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2658 return attr
.GetFontStyle() == wxITALIC
;
2664 /// Is all of the selection underlined?
2665 bool wxRichTextCtrl::IsSelectionUnderlined()
2669 wxRichTextRange range
= GetInternalSelectionRange();
2670 wxRichTextAttr attr
;
2671 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2672 attr
.SetFontUnderlined(true);
2674 return HasCharacterAttributes(range
, attr
);
2678 // If no selection, then we need to combine current style with default style
2679 // to see what the effect would be if we started typing.
2680 wxRichTextAttr attr
;
2681 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2682 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2684 if (GetStyle(pos
, attr
))
2686 if (IsDefaultStyleShowing())
2687 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2688 return attr
.GetFontUnderlined();
2694 /// Apply bold to the selection
2695 bool wxRichTextCtrl::ApplyBoldToSelection()
2697 wxRichTextAttr attr
;
2698 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2699 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2702 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2704 SetAndShowDefaultStyle(attr
);
2708 /// Apply italic to the selection
2709 bool wxRichTextCtrl::ApplyItalicToSelection()
2711 wxRichTextAttr attr
;
2712 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2713 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2716 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2718 SetAndShowDefaultStyle(attr
);
2722 /// Apply underline to the selection
2723 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2725 wxRichTextAttr attr
;
2726 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2727 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2730 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2732 SetAndShowDefaultStyle(attr
);
2736 /// Is all of the selection aligned according to the specified flag?
2737 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2739 wxRichTextRange range
;
2741 range
= GetInternalSelectionRange();
2743 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2745 wxRichTextAttr attr
;
2746 attr
.SetAlignment(alignment
);
2748 return HasParagraphAttributes(range
, attr
);
2751 /// Apply alignment to the selection
2752 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2754 wxRichTextAttr attr
;
2755 attr
.SetAlignment(alignment
);
2757 return SetStyle(GetSelectionRange(), attr
);
2760 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2762 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2767 /// Apply a named style to the selection
2768 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2770 // Flags are defined within each definition, so only certain
2771 // attributes are applied.
2772 wxRichTextAttr
attr(def
->GetStyle());
2774 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2776 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2778 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2780 wxRichTextRange range
;
2783 range
= GetSelectionRange();
2786 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2787 range
= wxRichTextRange(pos
, pos
+1);
2790 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2793 // Make sure the attr has the style name
2794 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2796 attr
.SetParagraphStyleName(def
->GetName());
2798 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2799 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2800 // to change its style independently.
2801 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2804 attr
.SetCharacterStyleName(def
->GetName());
2807 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2810 SetAndShowDefaultStyle(attr
);
2815 /// Apply the style sheet to the buffer, for example if the styles have changed.
2816 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2819 styleSheet
= GetBuffer().GetStyleSheet();
2823 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2825 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2833 /// Sets the default style to the style under the cursor
2834 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2837 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2839 // If at the start of a paragraph, use the next position.
2840 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2842 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2843 if (GetUncombinedStyle(pos
, attr
))
2845 if (GetStyle(pos
, attr
))
2848 SetDefaultStyle(attr
);
2855 /// Returns the first visible position in the current view
2856 long wxRichTextCtrl::GetFirstVisiblePosition() const
2858 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2860 return line
->GetAbsoluteRange().GetStart();
2865 /// The adjusted caret position is the character position adjusted to take
2866 /// into account whether we're at the start of a paragraph, in which case
2867 /// style information should be taken from the next position, not current one.
2868 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2870 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2872 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2877 /// Get/set the selection range in character positions. -1, -1 means no selection.
2878 /// The range is in API convention, i.e. a single character selection is denoted
2880 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2882 wxRichTextRange range
= GetInternalSelectionRange();
2883 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2884 range
.SetEnd(range
.GetEnd() + 1);
2888 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2890 wxRichTextRange
range1(range
);
2891 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2892 range1
.SetEnd(range1
.GetEnd() - 1);
2894 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2896 SetInternalSelectionRange(range1
);
2900 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2902 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
2905 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2907 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
2910 /// Clear list for given range
2911 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2913 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
2916 /// Number/renumber any list elements in the given range
2917 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2919 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
2922 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2924 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
2927 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2928 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2930 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
2933 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2935 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
2938 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
2940 if (sm_availableFontNames
.GetCount() == 0)
2942 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
2943 sm_availableFontNames
.Sort();
2945 return sm_availableFontNames
;
2948 void wxRichTextCtrl::ClearAvailableFontNames()
2950 sm_availableFontNames
.Clear();