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
));
131 GetBuffer().SetRichTextCtrl(this);
133 if (style
& wxTE_READONLY
)
136 wxTextAttrEx attributes
;
137 attributes
.SetFont(GetFont());
138 attributes
.SetTextColour(*wxBLACK
);
139 attributes
.SetBackgroundColour(*wxWHITE
);
140 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
141 attributes
.SetLineSpacing(10);
142 attributes
.SetParagraphSpacingAfter(10);
143 attributes
.SetParagraphSpacingBefore(0);
145 SetBasicStyle(attributes
);
147 // The default attributes will be merged with base attributes, so
148 // can be empty to begin with
149 wxTextAttrEx defaultAttributes
;
150 SetDefaultStyle(defaultAttributes
);
152 SetBackgroundColour(*wxWHITE
);
153 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
155 // Tell the sizers to use the given or best size
156 SetBestFittingSize(size
);
158 #if wxRICHTEXT_BUFFERED_PAINTING
160 RecreateBuffer(size
);
163 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
164 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
166 SetCursor(m_textCursor
);
168 if (!value
.IsEmpty())
171 GetBuffer().AddEventHandler(this);
176 wxRichTextCtrl::~wxRichTextCtrl()
178 GetBuffer().RemoveEventHandler(this);
180 delete m_contextMenu
;
183 /// Member initialisation
184 void wxRichTextCtrl::Init()
187 m_contextMenu
= NULL
;
189 m_caretPosition
= -1;
190 m_selectionRange
.SetRange(-2, -2);
191 m_selectionAnchor
= -2;
193 m_caretAtLineStart
= false;
195 m_fullLayoutRequired
= false;
196 m_fullLayoutTime
= 0;
197 m_fullLayoutSavedPosition
= 0;
198 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
199 m_caretPositionForDefaultStyle
= -2;
202 /// Call Freeze to prevent refresh
203 void wxRichTextCtrl::Freeze()
208 /// Call Thaw to refresh
209 void wxRichTextCtrl::Thaw()
213 if (m_freezeCount
== 0)
221 void wxRichTextCtrl::Clear()
224 m_buffer
.SetDirty(true);
225 m_caretPosition
= -1;
226 m_caretPositionForDefaultStyle
= -2;
227 m_caretAtLineStart
= false;
228 m_selectionRange
.SetRange(-2, -2);
230 SetScrollbars(0, 0, 0, 0, 0, 0);
232 if (m_freezeCount
== 0)
237 SendTextUpdatedEvent();
241 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
247 #if wxRICHTEXT_BUFFERED_PAINTING
248 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
254 if (m_freezeCount
> 0)
257 dc
.SetFont(GetFont());
259 // Paint the background
262 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
263 wxRect
availableSpace(GetClientSize());
264 if (GetBuffer().GetDirty())
266 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
267 GetBuffer().SetDirty(false);
271 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
280 // Empty implementation, to prevent flicker
281 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
285 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
287 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
296 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
305 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
311 dc
.SetFont(GetFont());
314 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
316 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
318 m_dragStart
= event
.GetLogicalPosition(dc
);
324 bool caretAtLineStart
= false;
326 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
328 // If we're at the start of a line (but not first in para)
329 // then we should keep the caret showing at the start of the line
330 // by showing the m_caretAtLineStart flag.
331 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
332 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
334 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
335 caretAtLineStart
= true;
339 MoveCaret(position
, caretAtLineStart
);
340 SetDefaultStyleToCursorStyle();
347 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
352 if (GetCapture() == this)
355 // See if we clicked on a URL
358 dc
.SetFont(GetFont());
361 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
362 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
364 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
367 if (GetStyle(position
, attr
))
369 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
371 wxString urlTarget
= attr
.GetURL();
372 if (!urlTarget
.IsEmpty())
374 wxMouseEvent
mouseEvent(event
);
376 long startPos
= 0, endPos
= 0;
377 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
380 startPos
= obj
->GetRange().GetStart();
381 endPos
= obj
->GetRange().GetEnd();
384 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
385 InitCommandEvent(urlEvent
);
387 urlEvent
.SetString(urlTarget
);
389 GetEventHandler()->ProcessEvent(urlEvent
);
398 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
402 dc
.SetFont(GetFont());
405 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
406 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
408 // See if we need to change the cursor
411 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
414 if (GetStyle(position
, attr
))
416 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
418 if (GetCursor() != m_urlCursor
)
419 SetCursor(m_urlCursor
);
421 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
423 if (GetCursor() != m_textCursor
)
424 SetCursor(m_textCursor
);
430 if (!event
.Dragging())
436 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
438 // TODO: test closeness
440 bool caretAtLineStart
= false;
442 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
444 // If we're at the start of a line (but not first in para)
445 // then we should keep the caret showing at the start of the line
446 // by showing the m_caretAtLineStart flag.
447 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
448 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
450 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
451 caretAtLineStart
= true;
455 if (m_caretPosition
!= position
)
457 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
459 MoveCaret(position
, caretAtLineStart
);
460 SetDefaultStyleToCursorStyle();
469 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
475 /// Left-double-click
476 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
478 SelectWord(GetCaretPosition()+1);
483 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
489 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
493 flags
|= wxRICHTEXT_CTRL_DOWN
;
494 if (event
.ShiftDown())
495 flags
|= wxRICHTEXT_SHIFT_DOWN
;
497 flags
|= wxRICHTEXT_ALT_DOWN
;
499 if (event
.GetKeyCode() == WXK_LEFT
||
500 event
.GetKeyCode() == WXK_RIGHT
||
501 event
.GetKeyCode() == WXK_UP
||
502 event
.GetKeyCode() == WXK_DOWN
||
503 event
.GetKeyCode() == WXK_HOME
||
504 event
.GetKeyCode() == WXK_PAGEUP
||
505 event
.GetKeyCode() == WXK_PAGEDOWN
||
506 event
.GetKeyCode() == WXK_END
||
508 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
509 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
510 event
.GetKeyCode() == WXK_NUMPAD_UP
||
511 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
512 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
513 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
514 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
515 event
.GetKeyCode() == WXK_NUMPAD_END
)
517 KeyboardNavigate(event
.GetKeyCode(), flags
);
521 // all the other keys modify the controls contents which shouldn't be
522 // possible if we're read-only
529 if (event
.GetKeyCode() == WXK_RETURN
)
531 BeginBatchUndo(_("Insert Text"));
533 long newPos
= m_caretPosition
;
535 DeleteSelectedContent(& newPos
);
537 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
539 SetDefaultStyleToCursorStyle();
541 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
543 wxRichTextEvent
cmdEvent(
544 wxEVT_COMMAND_RICHTEXT_RETURN
,
546 cmdEvent
.SetEventObject(this);
547 cmdEvent
.SetFlags(flags
);
548 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
550 // Generate conventional event
551 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
552 InitCommandEvent(textEvent
);
554 GetEventHandler()->ProcessEvent(textEvent
);
557 else if (event
.GetKeyCode() == WXK_BACK
)
559 BeginBatchUndo(_("Delete Text"));
561 // Submit range in character positions, which are greater than caret positions,
562 // so subtract 1 for deleted character and add 1 for conversion to character position.
563 if (m_caretPosition
> -1 && !HasSelection())
565 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
566 m_caretPosition
, // Current caret position
567 m_caretPosition
-1, // New caret position
571 DeleteSelectedContent();
575 // Shouldn't this be in Do()?
576 if (GetLastPosition() == -1)
580 m_caretPosition
= -1;
582 SetDefaultStyleToCursorStyle();
585 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
587 else if (event
.GetKeyCode() == WXK_DELETE
)
589 BeginBatchUndo(_("Delete Text"));
591 // Submit range in character positions, which are greater than caret positions,
592 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
594 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
595 m_caretPosition
, // Current caret position
596 m_caretPosition
+1, // New caret position
600 DeleteSelectedContent();
604 // Shouldn't this be in Do()?
605 if (GetLastPosition() == -1)
609 m_caretPosition
= -1;
611 SetDefaultStyleToCursorStyle();
616 long keycode
= event
.GetKeyCode();
690 case WXK_NUMPAD_SPACE
:
692 case WXK_NUMPAD_ENTER
:
697 case WXK_NUMPAD_HOME
:
698 case WXK_NUMPAD_LEFT
:
700 case WXK_NUMPAD_RIGHT
:
701 case WXK_NUMPAD_DOWN
:
702 case WXK_NUMPAD_PAGEUP
:
703 case WXK_NUMPAD_PAGEDOWN
:
705 case WXK_NUMPAD_BEGIN
:
706 case WXK_NUMPAD_INSERT
:
707 case WXK_NUMPAD_DELETE
:
708 case WXK_NUMPAD_EQUAL
:
709 case WXK_NUMPAD_MULTIPLY
:
711 case WXK_NUMPAD_SEPARATOR
:
712 case WXK_NUMPAD_SUBTRACT
:
713 case WXK_NUMPAD_DECIMAL
:
721 if (event
.CmdDown() || event
.AltDown())
727 if (keycode
== wxT('\t'))
729 // See if we need to promote or demote the selection or paragraph at the cursor
730 // position, instead of inserting a tab.
731 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
732 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
733 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
735 wxRichTextRange range
;
737 range
= GetSelectionRange();
739 range
= para
->GetRange().FromInternal();
741 int promoteBy
= event
.ShiftDown() ? 1 : -1;
743 PromoteList(promoteBy
, range
, NULL
);
749 BeginBatchUndo(_("Insert Text"));
751 long newPos
= m_caretPosition
;
752 DeleteSelectedContent(& newPos
);
754 wxString str
= (wxChar
) event
.GetKeyCode();
755 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
759 SetDefaultStyleToCursorStyle();
760 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
766 /// Delete content if there is a selection, e.g. when pressing a key.
767 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
771 long pos
= m_selectionRange
.GetStart();
772 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
773 m_caretPosition
, // Current caret position
774 pos
, // New caret position
776 m_selectionRange
.SetRange(-2, -2);
786 /// Keyboard navigation
790 Left: left one character
791 Right: right one character
794 Ctrl-Left: left one word
795 Ctrl-Right: right one word
796 Ctrl-Up: previous paragraph start
797 Ctrl-Down: next start of paragraph
800 Ctrl-Home: start of document
801 Ctrl-End: end of document
803 Page-Down: Down a screen
807 Ctrl-Alt-PgUp: Start of window
808 Ctrl-Alt-PgDn: End of window
809 F8: Start selection mode
810 Esc: End selection mode
812 Adding Shift does the above but starts/extends selection.
817 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
819 bool success
= false;
821 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
823 if (flags
& wxRICHTEXT_CTRL_DOWN
)
824 success
= WordRight(1, flags
);
826 success
= MoveRight(1, flags
);
828 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
830 if (flags
& wxRICHTEXT_CTRL_DOWN
)
831 success
= WordLeft(1, flags
);
833 success
= MoveLeft(1, flags
);
835 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
837 if (flags
& wxRICHTEXT_CTRL_DOWN
)
838 success
= MoveToParagraphStart(flags
);
840 success
= MoveUp(1, flags
);
842 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
844 if (flags
& wxRICHTEXT_CTRL_DOWN
)
845 success
= MoveToParagraphEnd(flags
);
847 success
= MoveDown(1, flags
);
849 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
851 success
= PageUp(1, flags
);
853 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
855 success
= PageDown(1, flags
);
857 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
859 if (flags
& wxRICHTEXT_CTRL_DOWN
)
860 success
= MoveHome(flags
);
862 success
= MoveToLineStart(flags
);
864 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
866 if (flags
& wxRICHTEXT_CTRL_DOWN
)
867 success
= MoveEnd(flags
);
869 success
= MoveToLineEnd(flags
);
874 ScrollIntoView(m_caretPosition
, keyCode
);
875 SetDefaultStyleToCursorStyle();
881 /// Extend the selection. Selections are in caret positions.
882 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
884 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
886 // If not currently selecting, start selecting
887 if (m_selectionRange
.GetStart() == -2)
889 m_selectionAnchor
= oldPos
;
892 m_selectionRange
.SetRange(newPos
+1, oldPos
);
894 m_selectionRange
.SetRange(oldPos
+1, newPos
);
898 // Always ensure that the selection range start is greater than
900 if (newPos
> m_selectionAnchor
)
901 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
903 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
906 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
908 wxLogDebug(wxT("Strange selection range"));
917 /// Scroll into view, returning true if we scrolled.
918 /// This takes a _caret_ position.
919 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
921 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
927 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
929 int startXUnits
, startYUnits
;
930 GetViewStart(& startXUnits
, & startYUnits
);
931 int startY
= startYUnits
* ppuY
;
934 GetVirtualSize(& sx
, & sy
);
940 wxRect rect
= line
->GetRect();
942 bool scrolled
= false;
944 wxSize clientSize
= GetClientSize();
947 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
948 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
949 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
950 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
952 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
954 // Make it scroll so this item is at the bottom
956 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
957 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
959 // If we're still off the screen, scroll another line down
960 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
963 if (startYUnits
!= yUnits
)
965 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
969 else if (rect
.y
< startY
)
971 // Make it scroll so this item is at the top
974 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
976 if (startYUnits
!= yUnits
)
978 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
984 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
985 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
986 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
987 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
991 // Make it scroll so this item is at the top
994 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
996 if (startYUnits
!= yUnits
)
998 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1002 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1004 // Make it scroll so this item is at the bottom
1006 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1007 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1009 // If we're still off the screen, scroll another line down
1010 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1013 if (startYUnits
!= yUnits
)
1015 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1025 /// Is the given position visible on the screen?
1026 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1028 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1034 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1037 GetViewStart(& startX
, & startY
);
1039 startY
= startY
* ppuY
;
1042 GetVirtualSize(& sx
, & sy
);
1047 wxRect rect
= line
->GetRect();
1049 wxSize clientSize
= GetClientSize();
1051 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1054 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1056 m_caretPosition
= position
;
1057 m_caretAtLineStart
= showAtLineStart
;
1060 /// Move caret one visual step forward: this may mean setting a flag
1061 /// and keeping the same position if we're going from the end of one line
1062 /// to the start of the next, which may be the exact same caret position.
1063 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1065 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1067 // Only do the check if we're not at the end of the paragraph (where things work OK
1069 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1071 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1075 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1077 // We're at the end of a line. See whether we need to
1078 // stay at the same actual caret position but change visual
1079 // position, or not.
1080 if (oldPosition
== lineRange
.GetEnd())
1082 if (m_caretAtLineStart
)
1084 // We're already at the start of the line, so actually move on now.
1085 m_caretPosition
= oldPosition
+ 1;
1086 m_caretAtLineStart
= false;
1090 // We're showing at the end of the line, so keep to
1091 // the same position but indicate that we're to show
1092 // at the start of the next line.
1093 m_caretPosition
= oldPosition
;
1094 m_caretAtLineStart
= true;
1096 SetDefaultStyleToCursorStyle();
1102 SetDefaultStyleToCursorStyle();
1105 /// Move caret one visual step backward: this may mean setting a flag
1106 /// and keeping the same position if we're going from the end of one line
1107 /// to the start of the next, which may be the exact same caret position.
1108 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1110 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1112 // Only do the check if we're not at the start of the paragraph (where things work OK
1114 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1116 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1120 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1122 // We're at the start of a line. See whether we need to
1123 // stay at the same actual caret position but change visual
1124 // position, or not.
1125 if (oldPosition
== lineRange
.GetStart())
1127 m_caretPosition
= oldPosition
-1;
1128 m_caretAtLineStart
= true;
1131 else if (oldPosition
== lineRange
.GetEnd())
1133 if (m_caretAtLineStart
)
1135 // We're at the start of the line, so keep the same caret position
1136 // but clear the start-of-line flag.
1137 m_caretPosition
= oldPosition
;
1138 m_caretAtLineStart
= false;
1142 // We're showing at the end of the line, so go back
1143 // to the previous character position.
1144 m_caretPosition
= oldPosition
- 1;
1146 SetDefaultStyleToCursorStyle();
1152 SetDefaultStyleToCursorStyle();
1156 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1158 long endPos
= GetBuffer().GetRange().GetEnd();
1160 if (m_caretPosition
+ noPositions
< endPos
)
1162 long oldPos
= m_caretPosition
;
1163 long newPos
= m_caretPosition
+ noPositions
;
1165 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1169 // Determine by looking at oldPos and m_caretPosition whether
1170 // we moved from the end of a line to the start of the next line, in which case
1171 // we want to adjust the caret position such that it is positioned at the
1172 // start of the next line, rather than jumping past the first character of the
1174 if (noPositions
== 1 && !extendSel
)
1175 MoveCaretForward(oldPos
);
1177 SetCaretPosition(newPos
);
1180 SetDefaultStyleToCursorStyle();
1191 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1195 if (m_caretPosition
> startPos
- noPositions
+ 1)
1197 long oldPos
= m_caretPosition
;
1198 long newPos
= m_caretPosition
- noPositions
;
1199 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1203 if (noPositions
== 1 && !extendSel
)
1204 MoveCaretBack(oldPos
);
1206 SetCaretPosition(newPos
);
1209 SetDefaultStyleToCursorStyle();
1220 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1222 return MoveDown(- noLines
, flags
);
1226 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1231 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1232 wxPoint pt
= GetCaret()->GetPosition();
1233 long newLine
= lineNumber
+ noLines
;
1235 if (lineNumber
!= -1)
1239 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1241 if (newLine
> lastLine
)
1251 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1254 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1260 wxClientDC
dc(this);
1262 dc
.SetFont(GetFont());
1264 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1266 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1268 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1269 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1270 // so we view the caret at the start of the line.
1271 bool caretLineStart
= false;
1272 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1274 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1275 wxRichTextRange lineRange
;
1277 lineRange
= thisLine
->GetAbsoluteRange();
1279 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1282 caretLineStart
= true;
1286 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1287 if (para
&& para
->GetRange().GetStart() == newPos
)
1292 long newSelEnd
= newPos
;
1294 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1298 SetCaretPosition(newPos
, caretLineStart
);
1300 SetDefaultStyleToCursorStyle();
1310 /// Move to the end of the paragraph
1311 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1313 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1316 long newPos
= para
->GetRange().GetEnd() - 1;
1317 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1321 SetCaretPosition(newPos
);
1323 SetDefaultStyleToCursorStyle();
1333 /// Move to the start of the paragraph
1334 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1336 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1339 long newPos
= para
->GetRange().GetStart() - 1;
1340 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1344 SetCaretPosition(newPos
);
1346 SetDefaultStyleToCursorStyle();
1356 /// Move to the end of the line
1357 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1359 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1363 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1364 long newPos
= lineRange
.GetEnd();
1365 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1369 SetCaretPosition(newPos
);
1371 SetDefaultStyleToCursorStyle();
1381 /// Move to the start of the line
1382 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1384 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1387 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1388 long newPos
= lineRange
.GetStart()-1;
1390 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1394 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1396 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1398 SetDefaultStyleToCursorStyle();
1408 /// Move to the start of the buffer
1409 bool wxRichTextCtrl::MoveHome(int flags
)
1411 if (m_caretPosition
!= -1)
1413 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1417 SetCaretPosition(-1);
1419 SetDefaultStyleToCursorStyle();
1429 /// Move to the end of the buffer
1430 bool wxRichTextCtrl::MoveEnd(int flags
)
1432 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1434 if (m_caretPosition
!= endPos
)
1436 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1440 SetCaretPosition(endPos
);
1442 SetDefaultStyleToCursorStyle();
1452 /// Move noPages pages up
1453 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1455 return PageDown(- noPages
, flags
);
1458 /// Move noPages pages down
1459 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1461 // Calculate which line occurs noPages * screen height further down.
1462 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1465 wxSize clientSize
= GetClientSize();
1466 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1468 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1471 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1472 long pos
= lineRange
.GetStart()-1;
1473 if (pos
!= m_caretPosition
)
1475 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1477 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1481 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1483 SetDefaultStyleToCursorStyle();
1495 // Finds the caret position for the next word
1496 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1498 long endPos
= GetBuffer().GetRange().GetEnd();
1502 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1504 // First skip current text to space
1505 while (i
< endPos
&& i
> -1)
1507 // i is in character, not caret positions
1508 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1509 if (text
!= wxT(" ") && !text
.empty())
1516 while (i
< endPos
&& i
> -1)
1518 // i is in character, not caret positions
1519 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1520 if (text
.empty()) // End of paragraph, or maybe an image
1521 return wxMax(-1, i
- 1);
1522 else if (text
== wxT(" ") || text
.empty())
1526 // Convert to caret position
1527 return wxMax(-1, i
- 1);
1536 long i
= m_caretPosition
;
1538 // First skip white space
1539 while (i
< endPos
&& i
> -1)
1541 // i is in character, not caret positions
1542 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1543 if (text
.empty()) // End of paragraph, or maybe an image
1545 else if (text
== wxT(" ") || text
.empty())
1550 // Next skip current text to space
1551 while (i
< endPos
&& i
> -1)
1553 // i is in character, not caret positions
1554 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1555 if (text
!= wxT(" ") /* && !text.empty() */)
1568 /// Move n words left
1569 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1571 long pos
= FindNextWordPosition(-1);
1572 if (pos
!= m_caretPosition
)
1574 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1576 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1580 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1582 SetDefaultStyleToCursorStyle();
1592 /// Move n words right
1593 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1595 long pos
= FindNextWordPosition(1);
1596 if (pos
!= m_caretPosition
)
1598 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1600 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1604 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1606 SetDefaultStyleToCursorStyle();
1617 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1619 // Only do sizing optimization for large buffers
1620 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1622 m_fullLayoutRequired
= true;
1623 m_fullLayoutTime
= wxGetLocalTimeMillis();
1624 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1625 LayoutContent(true /* onlyVisibleRect */);
1628 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1630 #if wxRICHTEXT_BUFFERED_PAINTING
1638 /// Idle-time processing
1639 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1641 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1643 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1645 m_fullLayoutRequired
= false;
1646 m_fullLayoutTime
= 0;
1647 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1648 ShowPosition(m_fullLayoutSavedPosition
);
1652 if (m_caretPositionForDefaultStyle
!= -2)
1654 // If the caret position has changed, no longer reflect the default style
1656 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1657 m_caretPositionForDefaultStyle
= -2;
1664 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1670 /// Set up scrollbars, e.g. after a resize
1671 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1676 if (GetBuffer().IsEmpty())
1678 SetScrollbars(0, 0, 0, 0, 0, 0);
1682 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1683 // of pixels. See e.g. wxVScrolledWindow for ideas.
1684 int pixelsPerUnit
= 5;
1685 wxSize clientSize
= GetClientSize();
1687 int maxHeight
= GetBuffer().GetCachedSize().y
;
1689 // Round up so we have at least maxHeight pixels
1690 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1692 int startX
= 0, startY
= 0;
1694 GetViewStart(& startX
, & startY
);
1696 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1697 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1699 // Move to previous scroll position if
1701 SetScrollbars(0, pixelsPerUnit
,
1703 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1706 /// Paint the background
1707 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1709 wxColour backgroundColour
= GetBackgroundColour();
1710 if (!backgroundColour
.Ok())
1711 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1713 // Clear the background
1714 dc
.SetBrush(wxBrush(backgroundColour
));
1715 dc
.SetPen(*wxTRANSPARENT_PEN
);
1716 wxRect
windowRect(GetClientSize());
1717 windowRect
.x
-= 2; windowRect
.y
-= 2;
1718 windowRect
.width
+= 4; windowRect
.height
+= 4;
1720 // We need to shift the rectangle to take into account
1721 // scrolling. Converting device to logical coordinates.
1722 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1723 dc
.DrawRectangle(windowRect
);
1726 #if wxRICHTEXT_BUFFERED_PAINTING
1727 /// Recreate buffer bitmap if necessary
1728 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1731 if (sz
== wxDefaultSize
)
1732 sz
= GetClientSize();
1734 if (sz
.x
< 1 || sz
.y
< 1)
1737 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1738 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1739 return m_bufferBitmap
.Ok();
1743 // ----------------------------------------------------------------------------
1744 // file IO functions
1745 // ----------------------------------------------------------------------------
1747 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1749 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1751 m_filename
= filename
;
1754 SetInsertionPoint(0);
1757 SetupScrollbars(true);
1759 SendTextUpdatedEvent();
1765 wxLogError(_("File couldn't be loaded."));
1771 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1773 if (GetBuffer().SaveFile(filename
, fileType
))
1775 m_filename
= filename
;
1782 wxLogError(_("The text couldn't be saved."));
1787 // ----------------------------------------------------------------------------
1788 // wxRichTextCtrl specific functionality
1789 // ----------------------------------------------------------------------------
1791 /// Add a new paragraph of text to the end of the buffer
1792 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1794 return GetBuffer().AddParagraph(text
);
1798 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1800 return GetBuffer().AddImage(image
);
1803 // ----------------------------------------------------------------------------
1804 // selection and ranges
1805 // ----------------------------------------------------------------------------
1807 void wxRichTextCtrl::SelectAll()
1809 SetSelection(0, GetLastPosition()+1);
1810 m_selectionAnchor
= -1;
1814 void wxRichTextCtrl::SelectNone()
1816 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1817 SetSelection(-2, -2);
1818 m_selectionAnchor
= -2;
1821 static bool wxIsWordDelimiter(const wxString
& text
)
1823 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1826 /// Select the word at the given character position
1827 bool wxRichTextCtrl::SelectWord(long position
)
1829 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1832 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1836 long positionStart
= position
;
1837 long positionEnd
= position
;
1839 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1841 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1842 if (wxIsWordDelimiter(text
))
1848 if (positionStart
< para
->GetRange().GetStart())
1849 positionStart
= para
->GetRange().GetStart();
1851 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1853 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1854 if (wxIsWordDelimiter(text
))
1860 if (positionEnd
>= para
->GetRange().GetEnd())
1861 positionEnd
= para
->GetRange().GetEnd();
1863 SetSelection(positionStart
, positionEnd
+1);
1865 if (positionStart
>= 0)
1867 MoveCaret(positionStart
-1, true);
1868 SetDefaultStyleToCursorStyle();
1874 wxString
wxRichTextCtrl::GetStringSelection() const
1877 GetSelection(&from
, &to
);
1879 return GetRange(from
, to
);
1882 // ----------------------------------------------------------------------------
1884 // ----------------------------------------------------------------------------
1886 wxTextCtrlHitTestResult
1887 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1889 // implement in terms of the other overload as the native ports typically
1890 // can get the position and not (x, y) pair directly (although wxUniv
1891 // directly gets x and y -- and so overrides this method as well)
1893 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1895 if ( rc
!= wxTE_HT_UNKNOWN
)
1897 PositionToXY(pos
, x
, y
);
1903 wxTextCtrlHitTestResult
1904 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1907 wxClientDC
dc((wxRichTextCtrl
*) this);
1908 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1910 // Buffer uses logical position (relative to start of buffer)
1912 wxPoint pt2
= GetLogicalPoint(pt
);
1914 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1918 case wxRICHTEXT_HITTEST_BEFORE
:
1919 return wxTE_HT_BEFORE
;
1921 case wxRICHTEXT_HITTEST_AFTER
:
1922 return wxTE_HT_BEYOND
;
1924 case wxRICHTEXT_HITTEST_ON
:
1925 return wxTE_HT_ON_TEXT
;
1928 return wxTE_HT_UNKNOWN
;
1931 // ----------------------------------------------------------------------------
1932 // set/get the controls text
1933 // ----------------------------------------------------------------------------
1935 wxString
wxRichTextCtrl::GetValue() const
1937 return GetBuffer().GetText();
1940 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1942 // Public API for range is different from internals
1943 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1946 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1950 // if the text is long enough, it's faster to just set it instead of first
1951 // comparing it with the old one (chances are that it will be different
1952 // anyhow, this comparison is there to avoid flicker for small single-line
1953 // edit controls mostly)
1954 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1958 // for compatibility, don't move the cursor when doing SetValue()
1959 SetInsertionPoint(0);
1963 if ( flags
& SetValue_SendEvent
)
1965 // still send an event for consistency
1966 SendTextUpdatedEvent();
1970 // we should reset the modified flag even if the value didn't really change
1972 // mark the control as being not dirty - we changed its text, not the
1977 void wxRichTextCtrl::WriteText(const wxString
& value
)
1982 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1984 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1986 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1988 if ( flags
& SetValue_SendEvent
)
1989 SendTextUpdatedEvent();
1992 void wxRichTextCtrl::AppendText(const wxString
& text
)
1994 SetInsertionPointEnd();
1999 /// Write an image at the current insertion point
2000 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2002 wxRichTextImageBlock imageBlock
;
2004 wxImage image2
= image
;
2005 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2006 return WriteImage(imageBlock
);
2011 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2013 wxRichTextImageBlock imageBlock
;
2016 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2017 return WriteImage(imageBlock
);
2022 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2024 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2027 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2031 wxRichTextImageBlock imageBlock
;
2033 wxImage image
= bitmap
.ConvertToImage();
2034 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2035 return WriteImage(imageBlock
);
2041 /// Insert a newline (actually paragraph) at the current insertion point.
2042 bool wxRichTextCtrl::Newline()
2044 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2048 // ----------------------------------------------------------------------------
2049 // Clipboard operations
2050 // ----------------------------------------------------------------------------
2052 void wxRichTextCtrl::Copy()
2056 wxRichTextRange range
= GetInternalSelectionRange();
2057 GetBuffer().CopyToClipboard(range
);
2061 void wxRichTextCtrl::Cut()
2065 wxRichTextRange range
= GetInternalSelectionRange();
2066 GetBuffer().CopyToClipboard(range
);
2068 DeleteSelectedContent();
2074 void wxRichTextCtrl::Paste()
2078 BeginBatchUndo(_("Paste"));
2080 long newPos
= m_caretPosition
;
2081 DeleteSelectedContent(& newPos
);
2083 GetBuffer().PasteFromClipboard(newPos
);
2089 void wxRichTextCtrl::DeleteSelection()
2091 if (CanDeleteSelection())
2093 DeleteSelectedContent();
2097 bool wxRichTextCtrl::HasSelection() const
2099 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2102 bool wxRichTextCtrl::CanCopy() const
2104 // Can copy if there's a selection
2105 return HasSelection();
2108 bool wxRichTextCtrl::CanCut() const
2110 return HasSelection() && IsEditable();
2113 bool wxRichTextCtrl::CanPaste() const
2115 if ( !IsEditable() )
2118 return GetBuffer().CanPasteFromClipboard();
2121 bool wxRichTextCtrl::CanDeleteSelection() const
2123 return HasSelection() && IsEditable();
2127 // ----------------------------------------------------------------------------
2129 // ----------------------------------------------------------------------------
2131 void wxRichTextCtrl::SetEditable(bool editable
)
2133 m_editable
= editable
;
2136 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2140 m_caretPosition
= pos
- 1;
2143 void wxRichTextCtrl::SetInsertionPointEnd()
2145 long pos
= GetLastPosition();
2146 SetInsertionPoint(pos
);
2149 long wxRichTextCtrl::GetInsertionPoint() const
2151 return m_caretPosition
+1;
2154 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2156 return GetBuffer().GetRange().GetEnd();
2159 // If the return values from and to are the same, there is no
2161 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2163 *from
= m_selectionRange
.GetStart();
2164 *to
= m_selectionRange
.GetEnd();
2165 if ((*to
) != -1 && (*to
) != -2)
2169 bool wxRichTextCtrl::IsEditable() const
2174 // ----------------------------------------------------------------------------
2176 // ----------------------------------------------------------------------------
2178 void wxRichTextCtrl::SetSelection(long from
, long to
)
2180 // if from and to are both -1, it means (in wxWidgets) that all text should
2182 if ( (from
== -1) && (to
== -1) )
2185 to
= GetLastPosition()+1;
2188 DoSetSelection(from
, to
);
2191 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2193 m_selectionAnchor
= from
;
2194 m_selectionRange
.SetRange(from
, to
-1);
2200 // ----------------------------------------------------------------------------
2202 // ----------------------------------------------------------------------------
2204 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2205 const wxString
& value
)
2207 BeginBatchUndo(_("Replace"));
2209 DeleteSelectedContent();
2211 DoWriteText(value
, SetValue_SelectionOnly
);
2216 void wxRichTextCtrl::Remove(long from
, long to
)
2220 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2221 m_caretPosition
, // Current caret position
2222 from
, // New caret position
2230 bool wxRichTextCtrl::IsModified() const
2232 return m_buffer
.IsModified();
2235 void wxRichTextCtrl::MarkDirty()
2237 m_buffer
.Modify(true);
2240 void wxRichTextCtrl::DiscardEdits()
2242 m_caretPositionForDefaultStyle
= -2;
2243 m_buffer
.Modify(false);
2244 m_buffer
.GetCommandProcessor()->ClearCommands();
2247 int wxRichTextCtrl::GetNumberOfLines() const
2249 return GetBuffer().GetParagraphCount();
2252 // ----------------------------------------------------------------------------
2253 // Positions <-> coords
2254 // ----------------------------------------------------------------------------
2256 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2258 return GetBuffer().XYToPosition(x
, y
);
2261 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2263 return GetBuffer().PositionToXY(pos
, x
, y
);
2266 // ----------------------------------------------------------------------------
2268 // ----------------------------------------------------------------------------
2270 void wxRichTextCtrl::ShowPosition(long pos
)
2272 if (!IsPositionVisible(pos
))
2273 ScrollIntoView(pos
-1, WXK_DOWN
);
2276 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2278 return GetBuffer().GetParagraphLength(lineNo
);
2281 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2283 return GetBuffer().GetParagraphText(lineNo
);
2286 // ----------------------------------------------------------------------------
2288 // ----------------------------------------------------------------------------
2290 void wxRichTextCtrl::Undo()
2294 GetCommandProcessor()->Undo();
2298 void wxRichTextCtrl::Redo()
2302 GetCommandProcessor()->Redo();
2306 bool wxRichTextCtrl::CanUndo() const
2308 return GetCommandProcessor()->CanUndo();
2311 bool wxRichTextCtrl::CanRedo() const
2313 return GetCommandProcessor()->CanRedo();
2316 // ----------------------------------------------------------------------------
2317 // implementation details
2318 // ----------------------------------------------------------------------------
2320 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2322 SetValue(event
.GetString());
2323 GetEventHandler()->ProcessEvent(event
);
2326 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2328 // By default, load the first file into the text window.
2329 if (event
.GetNumberOfFiles() > 0)
2331 LoadFile(event
.GetFiles()[0]);
2335 wxSize
wxRichTextCtrl::DoGetBestSize() const
2337 return wxSize(10, 10);
2340 // ----------------------------------------------------------------------------
2341 // standard handlers for standard edit menu events
2342 // ----------------------------------------------------------------------------
2344 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2349 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2354 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2359 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2364 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2369 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2374 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2376 event
.Enable( CanCut() );
2379 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2381 event
.Enable( CanCopy() );
2384 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2386 event
.Enable( CanDeleteSelection() );
2389 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2391 event
.Enable( CanPaste() );
2394 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2396 event
.Enable( CanUndo() );
2397 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2400 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2402 event
.Enable( CanRedo() );
2403 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2406 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2411 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2413 event
.Enable(GetLastPosition() > 0);
2416 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2420 m_contextMenu
= new wxMenu
;
2421 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2422 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2423 m_contextMenu
->AppendSeparator();
2424 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2425 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2426 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2427 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2428 m_contextMenu
->AppendSeparator();
2429 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2431 PopupMenu(m_contextMenu
);
2435 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2437 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2440 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2442 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2445 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2447 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2450 // extended style setting operation with flags including:
2451 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2452 // see richtextbuffer.h for more details.
2453 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2455 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2458 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2460 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2463 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2465 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2468 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2470 return GetBuffer().SetDefaultStyle(style
);
2473 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2475 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2478 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2480 return GetBuffer().GetDefaultStyle();
2483 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2485 return GetBuffer().GetDefaultStyle();
2488 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2490 wxTextAttrEx
attr(style
);
2491 if (GetBuffer().GetStyle(position
, attr
))
2500 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2502 return GetBuffer().GetStyle(position
, style
);
2505 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2507 return GetBuffer().GetStyle(position
, style
);
2510 /// Get the content (uncombined) attributes for this position.
2512 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2514 wxTextAttrEx
attr(style
);
2515 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2524 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2526 return GetBuffer().GetUncombinedStyle(position
, style
);
2529 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2531 return GetBuffer().GetUncombinedStyle(position
, style
);
2534 /// Set font, and also the buffer attributes
2535 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2537 wxControl::SetFont(font
);
2539 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2541 GetBuffer().SetBasicStyle(attr
);
2543 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2549 /// Transform logical to physical
2550 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2553 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2558 /// Transform physical to logical
2559 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2562 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2567 /// Position the caret
2568 void wxRichTextCtrl::PositionCaret()
2573 //wxLogDebug(wxT("PositionCaret"));
2576 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2578 wxPoint originalPt
= caretRect
.GetPosition();
2579 wxPoint pt
= GetPhysicalPoint(originalPt
);
2580 if (GetCaret()->GetPosition() != pt
)
2582 GetCaret()->Move(pt
);
2583 GetCaret()->SetSize(caretRect
.GetSize());
2588 /// Get the caret height and position for the given character position
2589 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2591 wxClientDC
dc(this);
2592 dc
.SetFont(GetFont());
2599 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2601 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2608 /// Gets the line for the visible caret position. If the caret is
2609 /// shown at the very end of the line, it means the next character is actually
2610 /// on the following line. So let's get the line we're expecting to find
2611 /// if this is the case.
2612 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2614 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2615 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2618 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2619 if (caretPosition
== lineRange
.GetStart()-1 &&
2620 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2622 if (!m_caretAtLineStart
)
2623 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2630 /// Move the caret to the given character position
2631 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2633 if (GetBuffer().GetDirty())
2636 if (pos
<= GetBuffer().GetRange().GetEnd())
2638 SetCaretPosition(pos
, showAtLineStart
);
2648 /// Layout the buffer: which we must do before certain operations, such as
2649 /// setting the caret position.
2650 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2652 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2654 wxRect
availableSpace(GetClientSize());
2655 if (availableSpace
.width
== 0)
2656 availableSpace
.width
= 10;
2657 if (availableSpace
.height
== 0)
2658 availableSpace
.height
= 10;
2660 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2661 if (onlyVisibleRect
)
2663 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2664 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2667 wxClientDC
dc(this);
2668 dc
.SetFont(GetFont());
2672 GetBuffer().Defragment();
2673 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2674 GetBuffer().Layout(dc
, availableSpace
, flags
);
2675 GetBuffer().SetDirty(false);
2684 /// Is all of the selection bold?
2685 bool wxRichTextCtrl::IsSelectionBold()
2689 wxRichTextAttr attr
;
2690 wxRichTextRange range
= GetInternalSelectionRange();
2691 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2692 attr
.SetFontWeight(wxBOLD
);
2694 return HasCharacterAttributes(range
, attr
);
2698 // If no selection, then we need to combine current style with default style
2699 // to see what the effect would be if we started typing.
2700 wxRichTextAttr attr
;
2701 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2703 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2704 if (GetStyle(pos
, attr
))
2706 if (IsDefaultStyleShowing())
2707 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2708 return attr
.GetFontWeight() == wxBOLD
;
2714 /// Is all of the selection italics?
2715 bool wxRichTextCtrl::IsSelectionItalics()
2719 wxRichTextRange range
= GetInternalSelectionRange();
2720 wxRichTextAttr attr
;
2721 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2722 attr
.SetFontStyle(wxITALIC
);
2724 return HasCharacterAttributes(range
, attr
);
2728 // If no selection, then we need to combine current style with default style
2729 // to see what the effect would be if we started typing.
2730 wxRichTextAttr attr
;
2731 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2733 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2734 if (GetStyle(pos
, attr
))
2736 if (IsDefaultStyleShowing())
2737 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2738 return attr
.GetFontStyle() == wxITALIC
;
2744 /// Is all of the selection underlined?
2745 bool wxRichTextCtrl::IsSelectionUnderlined()
2749 wxRichTextRange range
= GetInternalSelectionRange();
2750 wxRichTextAttr attr
;
2751 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2752 attr
.SetFontUnderlined(true);
2754 return HasCharacterAttributes(range
, attr
);
2758 // If no selection, then we need to combine current style with default style
2759 // to see what the effect would be if we started typing.
2760 wxRichTextAttr attr
;
2761 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2762 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2764 if (GetStyle(pos
, attr
))
2766 if (IsDefaultStyleShowing())
2767 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2768 return attr
.GetFontUnderlined();
2774 /// Apply bold to the selection
2775 bool wxRichTextCtrl::ApplyBoldToSelection()
2777 wxRichTextAttr attr
;
2778 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2779 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2782 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2784 SetAndShowDefaultStyle(attr
);
2788 /// Apply italic to the selection
2789 bool wxRichTextCtrl::ApplyItalicToSelection()
2791 wxRichTextAttr attr
;
2792 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2793 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2796 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2798 SetAndShowDefaultStyle(attr
);
2802 /// Apply underline to the selection
2803 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2805 wxRichTextAttr attr
;
2806 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2807 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2810 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2812 SetAndShowDefaultStyle(attr
);
2816 /// Is all of the selection aligned according to the specified flag?
2817 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2819 wxRichTextRange range
;
2821 range
= GetInternalSelectionRange();
2823 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2825 wxRichTextAttr attr
;
2826 attr
.SetAlignment(alignment
);
2828 return HasParagraphAttributes(range
, attr
);
2831 /// Apply alignment to the selection
2832 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2834 wxRichTextAttr attr
;
2835 attr
.SetAlignment(alignment
);
2837 return SetStyle(GetSelectionRange(), attr
);
2840 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2842 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2847 /// Apply a named style to the selection
2848 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2850 // Flags are defined within each definition, so only certain
2851 // attributes are applied.
2852 wxRichTextAttr
attr(def
->GetStyle());
2854 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2856 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2858 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2860 wxRichTextRange range
;
2863 range
= GetSelectionRange();
2866 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2867 range
= wxRichTextRange(pos
, pos
+1);
2870 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2873 // Make sure the attr has the style name
2874 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2876 attr
.SetParagraphStyleName(def
->GetName());
2878 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2879 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2880 // to change its style independently.
2881 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2884 attr
.SetCharacterStyleName(def
->GetName());
2887 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2890 SetAndShowDefaultStyle(attr
);
2895 /// Apply the style sheet to the buffer, for example if the styles have changed.
2896 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2899 styleSheet
= GetBuffer().GetStyleSheet();
2903 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2905 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2913 /// Sets the default style to the style under the cursor
2914 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2917 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2919 // If at the start of a paragraph, use the next position.
2920 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2922 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2923 if (GetUncombinedStyle(pos
, attr
))
2925 if (GetStyle(pos
, attr
))
2928 SetDefaultStyle(attr
);
2935 /// Returns the first visible position in the current view
2936 long wxRichTextCtrl::GetFirstVisiblePosition() const
2938 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2940 return line
->GetAbsoluteRange().GetStart();
2945 /// The adjusted caret position is the character position adjusted to take
2946 /// into account whether we're at the start of a paragraph, in which case
2947 /// style information should be taken from the next position, not current one.
2948 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2950 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2952 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2957 /// Get/set the selection range in character positions. -1, -1 means no selection.
2958 /// The range is in API convention, i.e. a single character selection is denoted
2960 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2962 wxRichTextRange range
= GetInternalSelectionRange();
2963 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2964 range
.SetEnd(range
.GetEnd() + 1);
2968 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2970 wxRichTextRange
range1(range
);
2971 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2972 range1
.SetEnd(range1
.GetEnd() - 1);
2974 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2976 SetInternalSelectionRange(range1
);
2980 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2982 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
2985 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2987 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
2990 /// Clear list for given range
2991 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2993 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
2996 /// Number/renumber any list elements in the given range
2997 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2999 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3002 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3004 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3007 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3008 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3010 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3013 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3015 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3018 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3020 if (sm_availableFontNames
.GetCount() == 0)
3022 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3023 sm_availableFontNames
.Sort();
3025 return sm_availableFontNames
;
3028 void wxRichTextCtrl::ClearAvailableFontNames()
3030 sm_availableFontNames
.Clear();