1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
52 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
54 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
56 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
57 EVT_PAINT(wxRichTextCtrl::OnPaint
)
58 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
59 EVT_IDLE(wxRichTextCtrl::OnIdle
)
60 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
61 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
62 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
63 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
64 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
65 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
66 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
67 EVT_CHAR(wxRichTextCtrl::OnChar
)
68 EVT_SIZE(wxRichTextCtrl::OnSize
)
69 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
70 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
71 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
73 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
74 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
76 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
77 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
79 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
80 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
82 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
83 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
85 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
86 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
88 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
89 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
91 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
92 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
99 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
101 wxRichTextCtrl::wxRichTextCtrl()
102 : wxScrollHelper(this)
107 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
109 const wxString
& value
,
113 : wxScrollHelper(this)
116 Create(parent
, id
, value
, pos
, size
, style
);
120 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
122 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
123 style
|wxFULL_REPAINT_ON_RESIZE
))
128 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
132 GetBuffer().SetRichTextCtrl(this);
134 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
137 if (style
& wxTE_READONLY
)
140 wxTextAttrEx attributes
;
141 attributes
.SetFont(GetFont());
142 attributes
.SetTextColour(*wxBLACK
);
143 attributes
.SetBackgroundColour(*wxWHITE
);
144 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
145 attributes
.SetLineSpacing(10);
146 attributes
.SetParagraphSpacingAfter(10);
147 attributes
.SetParagraphSpacingBefore(0);
149 SetBasicStyle(attributes
);
151 // The default attributes will be merged with base attributes, so
152 // can be empty to begin with
153 wxTextAttrEx defaultAttributes
;
154 SetDefaultStyle(defaultAttributes
);
156 SetBackgroundColour(*wxWHITE
);
157 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
159 // Tell the sizers to use the given or best size
160 SetBestFittingSize(size
);
162 #if wxRICHTEXT_BUFFERED_PAINTING
164 RecreateBuffer(size
);
167 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
168 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
170 SetCursor(m_textCursor
);
172 if (!value
.IsEmpty())
175 GetBuffer().AddEventHandler(this);
180 wxRichTextCtrl::~wxRichTextCtrl()
182 GetBuffer().RemoveEventHandler(this);
184 delete m_contextMenu
;
187 /// Member initialisation
188 void wxRichTextCtrl::Init()
191 m_contextMenu
= NULL
;
193 m_caretPosition
= -1;
194 m_selectionRange
.SetRange(-2, -2);
195 m_selectionAnchor
= -2;
197 m_caretAtLineStart
= false;
199 m_fullLayoutRequired
= false;
200 m_fullLayoutTime
= 0;
201 m_fullLayoutSavedPosition
= 0;
202 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
203 m_caretPositionForDefaultStyle
= -2;
206 /// Call Freeze to prevent refresh
207 void wxRichTextCtrl::Freeze()
212 /// Call Thaw to refresh
213 void wxRichTextCtrl::Thaw()
217 if (m_freezeCount
== 0)
225 void wxRichTextCtrl::Clear()
227 m_buffer
.ResetAndClearCommands();
228 m_buffer
.SetDirty(true);
229 m_caretPosition
= -1;
230 m_caretPositionForDefaultStyle
= -2;
231 m_caretAtLineStart
= false;
232 m_selectionRange
.SetRange(-2, -2);
234 SetScrollbars(0, 0, 0, 0, 0, 0);
236 if (m_freezeCount
== 0)
241 SendTextUpdatedEvent();
245 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
247 if (GetCaret() && GetCaret()->IsVisible())
251 #if wxRICHTEXT_BUFFERED_PAINTING
252 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
258 if (m_freezeCount
> 0)
261 dc
.SetFont(GetFont());
263 // Paint the background
266 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
268 wxRect
drawingArea(GetUpdateRegion().GetBox());
269 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
271 wxRect
availableSpace(GetClientSize());
272 if (GetBuffer().GetDirty())
274 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
275 GetBuffer().SetDirty(false);
279 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
282 if (GetCaret() && !GetCaret()->IsVisible())
288 // Empty implementation, to prevent flicker
289 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
293 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
297 if (!GetCaret()->IsVisible())
306 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
308 if (GetCaret() && GetCaret()->IsVisible())
316 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
322 dc
.SetFont(GetFont());
325 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
327 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
329 m_dragStart
= event
.GetLogicalPosition(dc
);
335 bool caretAtLineStart
= false;
337 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
339 // If we're at the start of a line (but not first in para)
340 // then we should keep the caret showing at the start of the line
341 // by showing the m_caretAtLineStart flag.
342 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
343 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
345 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
346 caretAtLineStart
= true;
350 MoveCaret(position
, caretAtLineStart
);
351 SetDefaultStyleToCursorStyle();
358 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
363 if (GetCapture() == this)
366 // See if we clicked on a URL
369 dc
.SetFont(GetFont());
372 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
373 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
375 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
378 if (GetStyle(position
, attr
))
380 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
382 wxString urlTarget
= attr
.GetURL();
383 if (!urlTarget
.IsEmpty())
385 wxMouseEvent
mouseEvent(event
);
387 long startPos
= 0, endPos
= 0;
388 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
391 startPos
= obj
->GetRange().GetStart();
392 endPos
= obj
->GetRange().GetEnd();
395 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
396 InitCommandEvent(urlEvent
);
398 urlEvent
.SetString(urlTarget
);
400 GetEventHandler()->ProcessEvent(urlEvent
);
409 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
413 dc
.SetFont(GetFont());
416 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
417 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
419 // See if we need to change the cursor
422 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
425 if (GetStyle(position
, attr
))
427 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
429 if (GetCursor() != m_urlCursor
)
430 SetCursor(m_urlCursor
);
432 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
434 if (GetCursor() != m_textCursor
)
435 SetCursor(m_textCursor
);
441 if (!event
.Dragging())
447 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
449 // TODO: test closeness
451 bool caretAtLineStart
= false;
453 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
455 // If we're at the start of a line (but not first in para)
456 // then we should keep the caret showing at the start of the line
457 // by showing the m_caretAtLineStart flag.
458 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
459 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
461 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
462 caretAtLineStart
= true;
466 if (m_caretPosition
!= position
)
468 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
470 MoveCaret(position
, caretAtLineStart
);
471 SetDefaultStyleToCursorStyle();
480 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
486 /// Left-double-click
487 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
489 SelectWord(GetCaretPosition()+1);
494 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
500 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
504 flags
|= wxRICHTEXT_CTRL_DOWN
;
505 if (event
.ShiftDown())
506 flags
|= wxRICHTEXT_SHIFT_DOWN
;
508 flags
|= wxRICHTEXT_ALT_DOWN
;
510 if (event
.GetKeyCode() == WXK_LEFT
||
511 event
.GetKeyCode() == WXK_RIGHT
||
512 event
.GetKeyCode() == WXK_UP
||
513 event
.GetKeyCode() == WXK_DOWN
||
514 event
.GetKeyCode() == WXK_HOME
||
515 event
.GetKeyCode() == WXK_PAGEUP
||
516 event
.GetKeyCode() == WXK_PAGEDOWN
||
517 event
.GetKeyCode() == WXK_END
||
519 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
520 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
521 event
.GetKeyCode() == WXK_NUMPAD_UP
||
522 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
523 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
524 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
525 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
526 event
.GetKeyCode() == WXK_NUMPAD_END
)
528 KeyboardNavigate(event
.GetKeyCode(), flags
);
532 // all the other keys modify the controls contents which shouldn't be
533 // possible if we're read-only
540 if (event
.GetKeyCode() == WXK_RETURN
)
542 BeginBatchUndo(_("Insert Text"));
544 long newPos
= m_caretPosition
;
546 DeleteSelectedContent(& newPos
);
548 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
550 SetDefaultStyleToCursorStyle();
552 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
554 wxRichTextEvent
cmdEvent(
555 wxEVT_COMMAND_RICHTEXT_RETURN
,
557 cmdEvent
.SetEventObject(this);
558 cmdEvent
.SetFlags(flags
);
559 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
561 // Generate conventional event
562 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
563 InitCommandEvent(textEvent
);
565 GetEventHandler()->ProcessEvent(textEvent
);
569 else if (event
.GetKeyCode() == WXK_BACK
)
571 BeginBatchUndo(_("Delete Text"));
573 // Submit range in character positions, which are greater than caret positions,
574 // so subtract 1 for deleted character and add 1 for conversion to character position.
575 if (m_caretPosition
> -1 && !HasSelection())
577 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
578 m_caretPosition
, // Current caret position
579 m_caretPosition
-1, // New caret position
583 DeleteSelectedContent();
587 if (GetLastPosition() == -1)
591 m_caretPosition
= -1;
593 SetDefaultStyleToCursorStyle();
596 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
599 else if (event
.GetKeyCode() == WXK_DELETE
)
601 BeginBatchUndo(_("Delete Text"));
603 // Submit range in character positions, which are greater than caret positions,
604 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
606 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
607 m_caretPosition
, // Current caret position
608 m_caretPosition
+1, // New caret position
612 DeleteSelectedContent();
616 if (GetLastPosition() == -1)
620 m_caretPosition
= -1;
622 SetDefaultStyleToCursorStyle();
628 long keycode
= event
.GetKeyCode();
702 case WXK_NUMPAD_SPACE
:
704 case WXK_NUMPAD_ENTER
:
709 case WXK_NUMPAD_HOME
:
710 case WXK_NUMPAD_LEFT
:
712 case WXK_NUMPAD_RIGHT
:
713 case WXK_NUMPAD_DOWN
:
714 case WXK_NUMPAD_PAGEUP
:
715 case WXK_NUMPAD_PAGEDOWN
:
717 case WXK_NUMPAD_BEGIN
:
718 case WXK_NUMPAD_INSERT
:
719 case WXK_NUMPAD_DELETE
:
720 case WXK_NUMPAD_EQUAL
:
721 case WXK_NUMPAD_MULTIPLY
:
723 case WXK_NUMPAD_SEPARATOR
:
724 case WXK_NUMPAD_SUBTRACT
:
725 case WXK_NUMPAD_DECIMAL
:
733 if (event
.CmdDown() || event
.AltDown())
739 if (keycode
== wxT('\t'))
741 // See if we need to promote or demote the selection or paragraph at the cursor
742 // position, instead of inserting a tab.
743 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
744 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
745 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
747 wxRichTextRange range
;
749 range
= GetSelectionRange();
751 range
= para
->GetRange().FromInternal();
753 int promoteBy
= event
.ShiftDown() ? 1 : -1;
755 PromoteList(promoteBy
, range
, NULL
);
761 BeginBatchUndo(_("Insert Text"));
763 long newPos
= m_caretPosition
;
764 DeleteSelectedContent(& newPos
);
766 wxString str
= (wxChar
) event
.GetKeyCode();
767 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
771 SetDefaultStyleToCursorStyle();
772 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
779 /// Delete content if there is a selection, e.g. when pressing a key.
780 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
784 long pos
= m_selectionRange
.GetStart();
785 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
786 m_caretPosition
, // Current caret position
787 pos
, // New caret position
789 m_selectionRange
.SetRange(-2, -2);
799 /// Keyboard navigation
803 Left: left one character
804 Right: right one character
807 Ctrl-Left: left one word
808 Ctrl-Right: right one word
809 Ctrl-Up: previous paragraph start
810 Ctrl-Down: next start of paragraph
813 Ctrl-Home: start of document
814 Ctrl-End: end of document
816 Page-Down: Down a screen
820 Ctrl-Alt-PgUp: Start of window
821 Ctrl-Alt-PgDn: End of window
822 F8: Start selection mode
823 Esc: End selection mode
825 Adding Shift does the above but starts/extends selection.
830 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
832 bool success
= false;
834 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
836 if (flags
& wxRICHTEXT_CTRL_DOWN
)
837 success
= WordRight(1, flags
);
839 success
= MoveRight(1, flags
);
841 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
843 if (flags
& wxRICHTEXT_CTRL_DOWN
)
844 success
= WordLeft(1, flags
);
846 success
= MoveLeft(1, flags
);
848 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
850 if (flags
& wxRICHTEXT_CTRL_DOWN
)
851 success
= MoveToParagraphStart(flags
);
853 success
= MoveUp(1, flags
);
855 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
857 if (flags
& wxRICHTEXT_CTRL_DOWN
)
858 success
= MoveToParagraphEnd(flags
);
860 success
= MoveDown(1, flags
);
862 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
864 success
= PageUp(1, flags
);
866 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
868 success
= PageDown(1, flags
);
870 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
872 if (flags
& wxRICHTEXT_CTRL_DOWN
)
873 success
= MoveHome(flags
);
875 success
= MoveToLineStart(flags
);
877 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
879 if (flags
& wxRICHTEXT_CTRL_DOWN
)
880 success
= MoveEnd(flags
);
882 success
= MoveToLineEnd(flags
);
887 ScrollIntoView(m_caretPosition
, keyCode
);
888 SetDefaultStyleToCursorStyle();
894 /// Extend the selection. Selections are in caret positions.
895 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
897 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
899 // If not currently selecting, start selecting
900 if (m_selectionRange
.GetStart() == -2)
902 m_selectionAnchor
= oldPos
;
905 m_selectionRange
.SetRange(newPos
+1, oldPos
);
907 m_selectionRange
.SetRange(oldPos
+1, newPos
);
911 // Always ensure that the selection range start is greater than
913 if (newPos
> m_selectionAnchor
)
914 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
916 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
919 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
921 wxLogDebug(wxT("Strange selection range"));
930 /// Scroll into view, returning true if we scrolled.
931 /// This takes a _caret_ position.
932 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
934 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
940 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
942 int startXUnits
, startYUnits
;
943 GetViewStart(& startXUnits
, & startYUnits
);
944 int startY
= startYUnits
* ppuY
;
947 GetVirtualSize(& sx
, & sy
);
953 wxRect rect
= line
->GetRect();
955 bool scrolled
= false;
957 wxSize clientSize
= GetClientSize();
960 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
961 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
962 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
963 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
965 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
967 // Make it scroll so this item is at the bottom
969 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
970 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
972 // If we're still off the screen, scroll another line down
973 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
976 if (startYUnits
!= yUnits
)
978 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
982 else if (rect
.y
< startY
)
984 // Make it scroll so this item is at the top
987 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
989 if (startYUnits
!= yUnits
)
991 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
997 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
998 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
999 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1000 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1002 if (rect
.y
< startY
)
1004 // Make it scroll so this item is at the top
1007 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1009 if (startYUnits
!= yUnits
)
1011 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1015 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1017 // Make it scroll so this item is at the bottom
1019 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1020 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1022 // If we're still off the screen, scroll another line down
1023 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1026 if (startYUnits
!= yUnits
)
1028 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1038 /// Is the given position visible on the screen?
1039 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1041 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1047 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1050 GetViewStart(& startX
, & startY
);
1052 startY
= startY
* ppuY
;
1055 GetVirtualSize(& sx
, & sy
);
1060 wxRect rect
= line
->GetRect();
1062 wxSize clientSize
= GetClientSize();
1064 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1067 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1069 m_caretPosition
= position
;
1070 m_caretAtLineStart
= showAtLineStart
;
1073 /// Move caret one visual step forward: this may mean setting a flag
1074 /// and keeping the same position if we're going from the end of one line
1075 /// to the start of the next, which may be the exact same caret position.
1076 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1078 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1080 // Only do the check if we're not at the end of the paragraph (where things work OK
1082 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1084 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1088 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1090 // We're at the end of a line. See whether we need to
1091 // stay at the same actual caret position but change visual
1092 // position, or not.
1093 if (oldPosition
== lineRange
.GetEnd())
1095 if (m_caretAtLineStart
)
1097 // We're already at the start of the line, so actually move on now.
1098 m_caretPosition
= oldPosition
+ 1;
1099 m_caretAtLineStart
= false;
1103 // We're showing at the end of the line, so keep to
1104 // the same position but indicate that we're to show
1105 // at the start of the next line.
1106 m_caretPosition
= oldPosition
;
1107 m_caretAtLineStart
= true;
1109 SetDefaultStyleToCursorStyle();
1115 SetDefaultStyleToCursorStyle();
1118 /// Move caret one visual step backward: this may mean setting a flag
1119 /// and keeping the same position if we're going from the end of one line
1120 /// to the start of the next, which may be the exact same caret position.
1121 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1123 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1125 // Only do the check if we're not at the start of the paragraph (where things work OK
1127 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1129 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1133 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1135 // We're at the start of a line. See whether we need to
1136 // stay at the same actual caret position but change visual
1137 // position, or not.
1138 if (oldPosition
== lineRange
.GetStart())
1140 m_caretPosition
= oldPosition
-1;
1141 m_caretAtLineStart
= true;
1144 else if (oldPosition
== lineRange
.GetEnd())
1146 if (m_caretAtLineStart
)
1148 // We're at the start of the line, so keep the same caret position
1149 // but clear the start-of-line flag.
1150 m_caretPosition
= oldPosition
;
1151 m_caretAtLineStart
= false;
1155 // We're showing at the end of the line, so go back
1156 // to the previous character position.
1157 m_caretPosition
= oldPosition
- 1;
1159 SetDefaultStyleToCursorStyle();
1165 SetDefaultStyleToCursorStyle();
1169 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1171 long endPos
= GetBuffer().GetRange().GetEnd();
1173 if (m_caretPosition
+ noPositions
< endPos
)
1175 long oldPos
= m_caretPosition
;
1176 long newPos
= m_caretPosition
+ noPositions
;
1178 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1182 // Determine by looking at oldPos and m_caretPosition whether
1183 // we moved from the end of a line to the start of the next line, in which case
1184 // we want to adjust the caret position such that it is positioned at the
1185 // start of the next line, rather than jumping past the first character of the
1187 if (noPositions
== 1 && !extendSel
)
1188 MoveCaretForward(oldPos
);
1190 SetCaretPosition(newPos
);
1193 SetDefaultStyleToCursorStyle();
1204 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1208 if (m_caretPosition
> startPos
- noPositions
+ 1)
1210 long oldPos
= m_caretPosition
;
1211 long newPos
= m_caretPosition
- noPositions
;
1212 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1216 if (noPositions
== 1 && !extendSel
)
1217 MoveCaretBack(oldPos
);
1219 SetCaretPosition(newPos
);
1222 SetDefaultStyleToCursorStyle();
1233 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1235 return MoveDown(- noLines
, flags
);
1239 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1244 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1245 wxPoint pt
= GetCaret()->GetPosition();
1246 long newLine
= lineNumber
+ noLines
;
1248 if (lineNumber
!= -1)
1252 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1254 if (newLine
> lastLine
)
1264 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1267 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1273 wxClientDC
dc(this);
1275 dc
.SetFont(GetFont());
1277 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1279 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1281 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1282 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1283 // so we view the caret at the start of the line.
1284 bool caretLineStart
= false;
1285 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1287 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1288 wxRichTextRange lineRange
;
1290 lineRange
= thisLine
->GetAbsoluteRange();
1292 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1295 caretLineStart
= true;
1299 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1300 if (para
&& para
->GetRange().GetStart() == newPos
)
1305 long newSelEnd
= newPos
;
1307 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1311 SetCaretPosition(newPos
, caretLineStart
);
1313 SetDefaultStyleToCursorStyle();
1323 /// Move to the end of the paragraph
1324 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1326 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1329 long newPos
= para
->GetRange().GetEnd() - 1;
1330 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1334 SetCaretPosition(newPos
);
1336 SetDefaultStyleToCursorStyle();
1346 /// Move to the start of the paragraph
1347 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1349 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1352 long newPos
= para
->GetRange().GetStart() - 1;
1353 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1357 SetCaretPosition(newPos
);
1359 SetDefaultStyleToCursorStyle();
1369 /// Move to the end of the line
1370 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1372 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1376 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1377 long newPos
= lineRange
.GetEnd();
1378 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1382 SetCaretPosition(newPos
);
1384 SetDefaultStyleToCursorStyle();
1394 /// Move to the start of the line
1395 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1397 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1400 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1401 long newPos
= lineRange
.GetStart()-1;
1403 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1407 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1409 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1411 SetDefaultStyleToCursorStyle();
1421 /// Move to the start of the buffer
1422 bool wxRichTextCtrl::MoveHome(int flags
)
1424 if (m_caretPosition
!= -1)
1426 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1430 SetCaretPosition(-1);
1432 SetDefaultStyleToCursorStyle();
1442 /// Move to the end of the buffer
1443 bool wxRichTextCtrl::MoveEnd(int flags
)
1445 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1447 if (m_caretPosition
!= endPos
)
1449 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1453 SetCaretPosition(endPos
);
1455 SetDefaultStyleToCursorStyle();
1465 /// Move noPages pages up
1466 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1468 return PageDown(- noPages
, flags
);
1471 /// Move noPages pages down
1472 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1474 // Calculate which line occurs noPages * screen height further down.
1475 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1478 wxSize clientSize
= GetClientSize();
1479 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1481 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1484 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1485 long pos
= lineRange
.GetStart()-1;
1486 if (pos
!= m_caretPosition
)
1488 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1490 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1494 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1496 SetDefaultStyleToCursorStyle();
1508 // Finds the caret position for the next word
1509 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1511 long endPos
= GetBuffer().GetRange().GetEnd();
1515 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1517 // First skip current text to space
1518 while (i
< endPos
&& i
> -1)
1520 // i is in character, not caret positions
1521 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1522 if (text
!= wxT(" ") && !text
.empty())
1529 while (i
< endPos
&& i
> -1)
1531 // i is in character, not caret positions
1532 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1533 if (text
.empty()) // End of paragraph, or maybe an image
1534 return wxMax(-1, i
- 1);
1535 else if (text
== wxT(" ") || text
.empty())
1539 // Convert to caret position
1540 return wxMax(-1, i
- 1);
1549 long i
= m_caretPosition
;
1551 // First skip white space
1552 while (i
< endPos
&& i
> -1)
1554 // i is in character, not caret positions
1555 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1556 if (text
.empty()) // End of paragraph, or maybe an image
1558 else if (text
== wxT(" ") || text
.empty())
1563 // Next skip current text to space
1564 while (i
< endPos
&& i
> -1)
1566 // i is in character, not caret positions
1567 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1568 if (text
!= wxT(" ") /* && !text.empty() */)
1581 /// Move n words left
1582 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1584 long pos
= FindNextWordPosition(-1);
1585 if (pos
!= m_caretPosition
)
1587 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1589 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1593 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1595 SetDefaultStyleToCursorStyle();
1605 /// Move n words right
1606 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1608 long pos
= FindNextWordPosition(1);
1609 if (pos
!= m_caretPosition
)
1611 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1613 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1617 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1619 SetDefaultStyleToCursorStyle();
1630 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1632 // Only do sizing optimization for large buffers
1633 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1635 m_fullLayoutRequired
= true;
1636 m_fullLayoutTime
= wxGetLocalTimeMillis();
1637 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1638 LayoutContent(true /* onlyVisibleRect */);
1641 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1643 #if wxRICHTEXT_BUFFERED_PAINTING
1651 /// Idle-time processing
1652 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1654 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1656 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1658 m_fullLayoutRequired
= false;
1659 m_fullLayoutTime
= 0;
1660 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1661 ShowPosition(m_fullLayoutSavedPosition
);
1665 if (m_caretPositionForDefaultStyle
!= -2)
1667 // If the caret position has changed, no longer reflect the default style
1669 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1670 m_caretPositionForDefaultStyle
= -2;
1677 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1683 /// Set up scrollbars, e.g. after a resize
1684 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1689 if (GetBuffer().IsEmpty())
1691 SetScrollbars(0, 0, 0, 0, 0, 0);
1695 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1696 // of pixels. See e.g. wxVScrolledWindow for ideas.
1697 int pixelsPerUnit
= 5;
1698 wxSize clientSize
= GetClientSize();
1700 int maxHeight
= GetBuffer().GetCachedSize().y
;
1702 // Round up so we have at least maxHeight pixels
1703 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1705 int startX
= 0, startY
= 0;
1707 GetViewStart(& startX
, & startY
);
1709 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1710 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1712 // Move to previous scroll position if
1714 SetScrollbars(0, pixelsPerUnit
,
1716 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1719 /// Paint the background
1720 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1722 wxColour backgroundColour
= GetBackgroundColour();
1723 if (!backgroundColour
.Ok())
1724 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1726 // Clear the background
1727 dc
.SetBrush(wxBrush(backgroundColour
));
1728 dc
.SetPen(*wxTRANSPARENT_PEN
);
1729 wxRect
windowRect(GetClientSize());
1730 windowRect
.x
-= 2; windowRect
.y
-= 2;
1731 windowRect
.width
+= 4; windowRect
.height
+= 4;
1733 // We need to shift the rectangle to take into account
1734 // scrolling. Converting device to logical coordinates.
1735 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1736 dc
.DrawRectangle(windowRect
);
1739 #if wxRICHTEXT_BUFFERED_PAINTING
1740 /// Recreate buffer bitmap if necessary
1741 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1744 if (sz
== wxDefaultSize
)
1745 sz
= GetClientSize();
1747 if (sz
.x
< 1 || sz
.y
< 1)
1750 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1751 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1752 return m_bufferBitmap
.Ok();
1756 // ----------------------------------------------------------------------------
1757 // file IO functions
1758 // ----------------------------------------------------------------------------
1760 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1762 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1764 m_filename
= filename
;
1767 SetInsertionPoint(0);
1770 SetupScrollbars(true);
1772 SendTextUpdatedEvent();
1778 wxLogError(_("File couldn't be loaded."));
1784 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1786 if (GetBuffer().SaveFile(filename
, fileType
))
1788 m_filename
= filename
;
1795 wxLogError(_("The text couldn't be saved."));
1800 // ----------------------------------------------------------------------------
1801 // wxRichTextCtrl specific functionality
1802 // ----------------------------------------------------------------------------
1804 /// Add a new paragraph of text to the end of the buffer
1805 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1807 return GetBuffer().AddParagraph(text
);
1811 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1813 return GetBuffer().AddImage(image
);
1816 // ----------------------------------------------------------------------------
1817 // selection and ranges
1818 // ----------------------------------------------------------------------------
1820 void wxRichTextCtrl::SelectAll()
1822 SetSelection(0, GetLastPosition()+1);
1823 m_selectionAnchor
= -1;
1827 void wxRichTextCtrl::SelectNone()
1829 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1830 SetSelection(-2, -2);
1831 m_selectionAnchor
= -2;
1834 static bool wxIsWordDelimiter(const wxString
& text
)
1836 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1839 /// Select the word at the given character position
1840 bool wxRichTextCtrl::SelectWord(long position
)
1842 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1845 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1849 long positionStart
= position
;
1850 long positionEnd
= position
;
1852 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1854 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1855 if (wxIsWordDelimiter(text
))
1861 if (positionStart
< para
->GetRange().GetStart())
1862 positionStart
= para
->GetRange().GetStart();
1864 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1866 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1867 if (wxIsWordDelimiter(text
))
1873 if (positionEnd
>= para
->GetRange().GetEnd())
1874 positionEnd
= para
->GetRange().GetEnd();
1876 SetSelection(positionStart
, positionEnd
+1);
1878 if (positionStart
>= 0)
1880 MoveCaret(positionStart
-1, true);
1881 SetDefaultStyleToCursorStyle();
1887 wxString
wxRichTextCtrl::GetStringSelection() const
1890 GetSelection(&from
, &to
);
1892 return GetRange(from
, to
);
1895 // ----------------------------------------------------------------------------
1897 // ----------------------------------------------------------------------------
1899 wxTextCtrlHitTestResult
1900 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1902 // implement in terms of the other overload as the native ports typically
1903 // can get the position and not (x, y) pair directly (although wxUniv
1904 // directly gets x and y -- and so overrides this method as well)
1906 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1908 if ( rc
!= wxTE_HT_UNKNOWN
)
1910 PositionToXY(pos
, x
, y
);
1916 wxTextCtrlHitTestResult
1917 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1920 wxClientDC
dc((wxRichTextCtrl
*) this);
1921 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1923 // Buffer uses logical position (relative to start of buffer)
1925 wxPoint pt2
= GetLogicalPoint(pt
);
1927 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1931 case wxRICHTEXT_HITTEST_BEFORE
:
1932 return wxTE_HT_BEFORE
;
1934 case wxRICHTEXT_HITTEST_AFTER
:
1935 return wxTE_HT_BEYOND
;
1937 case wxRICHTEXT_HITTEST_ON
:
1938 return wxTE_HT_ON_TEXT
;
1941 return wxTE_HT_UNKNOWN
;
1944 // ----------------------------------------------------------------------------
1945 // set/get the controls text
1946 // ----------------------------------------------------------------------------
1948 wxString
wxRichTextCtrl::GetValue() const
1950 return GetBuffer().GetText();
1953 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1955 // Public API for range is different from internals
1956 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1959 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1963 // if the text is long enough, it's faster to just set it instead of first
1964 // comparing it with the old one (chances are that it will be different
1965 // anyhow, this comparison is there to avoid flicker for small single-line
1966 // edit controls mostly)
1967 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1971 // for compatibility, don't move the cursor when doing SetValue()
1972 SetInsertionPoint(0);
1976 if ( flags
& SetValue_SendEvent
)
1978 // still send an event for consistency
1979 SendTextUpdatedEvent();
1983 // we should reset the modified flag even if the value didn't really change
1985 // mark the control as being not dirty - we changed its text, not the
1990 void wxRichTextCtrl::WriteText(const wxString
& value
)
1995 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1997 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1999 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
2001 if ( flags
& SetValue_SendEvent
)
2002 SendTextUpdatedEvent();
2005 void wxRichTextCtrl::AppendText(const wxString
& text
)
2007 SetInsertionPointEnd();
2012 /// Write an image at the current insertion point
2013 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2015 wxRichTextImageBlock imageBlock
;
2017 wxImage image2
= image
;
2018 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2019 return WriteImage(imageBlock
);
2024 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2026 wxRichTextImageBlock imageBlock
;
2029 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2030 return WriteImage(imageBlock
);
2035 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2037 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2040 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2044 wxRichTextImageBlock imageBlock
;
2046 wxImage image
= bitmap
.ConvertToImage();
2047 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2048 return WriteImage(imageBlock
);
2054 /// Insert a newline (actually paragraph) at the current insertion point.
2055 bool wxRichTextCtrl::Newline()
2057 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2061 // ----------------------------------------------------------------------------
2062 // Clipboard operations
2063 // ----------------------------------------------------------------------------
2065 void wxRichTextCtrl::Copy()
2069 wxRichTextRange range
= GetInternalSelectionRange();
2070 GetBuffer().CopyToClipboard(range
);
2074 void wxRichTextCtrl::Cut()
2078 wxRichTextRange range
= GetInternalSelectionRange();
2079 GetBuffer().CopyToClipboard(range
);
2081 DeleteSelectedContent();
2087 void wxRichTextCtrl::Paste()
2091 BeginBatchUndo(_("Paste"));
2093 long newPos
= m_caretPosition
;
2094 DeleteSelectedContent(& newPos
);
2096 GetBuffer().PasteFromClipboard(newPos
);
2102 void wxRichTextCtrl::DeleteSelection()
2104 if (CanDeleteSelection())
2106 DeleteSelectedContent();
2110 bool wxRichTextCtrl::HasSelection() const
2112 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2115 bool wxRichTextCtrl::CanCopy() const
2117 // Can copy if there's a selection
2118 return HasSelection();
2121 bool wxRichTextCtrl::CanCut() const
2123 return HasSelection() && IsEditable();
2126 bool wxRichTextCtrl::CanPaste() const
2128 if ( !IsEditable() )
2131 return GetBuffer().CanPasteFromClipboard();
2134 bool wxRichTextCtrl::CanDeleteSelection() const
2136 return HasSelection() && IsEditable();
2140 // ----------------------------------------------------------------------------
2142 // ----------------------------------------------------------------------------
2144 void wxRichTextCtrl::SetEditable(bool editable
)
2146 m_editable
= editable
;
2149 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2153 m_caretPosition
= pos
- 1;
2156 void wxRichTextCtrl::SetInsertionPointEnd()
2158 long pos
= GetLastPosition();
2159 SetInsertionPoint(pos
);
2162 long wxRichTextCtrl::GetInsertionPoint() const
2164 return m_caretPosition
+1;
2167 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2169 return GetBuffer().GetRange().GetEnd();
2172 // If the return values from and to are the same, there is no
2174 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2176 *from
= m_selectionRange
.GetStart();
2177 *to
= m_selectionRange
.GetEnd();
2178 if ((*to
) != -1 && (*to
) != -2)
2182 bool wxRichTextCtrl::IsEditable() const
2187 // ----------------------------------------------------------------------------
2189 // ----------------------------------------------------------------------------
2191 void wxRichTextCtrl::SetSelection(long from
, long to
)
2193 // if from and to are both -1, it means (in wxWidgets) that all text should
2195 if ( (from
== -1) && (to
== -1) )
2198 to
= GetLastPosition()+1;
2201 DoSetSelection(from
, to
);
2204 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2206 m_selectionAnchor
= from
;
2207 m_selectionRange
.SetRange(from
, to
-1);
2213 // ----------------------------------------------------------------------------
2215 // ----------------------------------------------------------------------------
2217 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2218 const wxString
& value
)
2220 BeginBatchUndo(_("Replace"));
2222 DeleteSelectedContent();
2224 DoWriteText(value
, SetValue_SelectionOnly
);
2229 void wxRichTextCtrl::Remove(long from
, long to
)
2233 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2234 m_caretPosition
, // Current caret position
2235 from
, // New caret position
2243 bool wxRichTextCtrl::IsModified() const
2245 return m_buffer
.IsModified();
2248 void wxRichTextCtrl::MarkDirty()
2250 m_buffer
.Modify(true);
2253 void wxRichTextCtrl::DiscardEdits()
2255 m_caretPositionForDefaultStyle
= -2;
2256 m_buffer
.Modify(false);
2257 m_buffer
.GetCommandProcessor()->ClearCommands();
2260 int wxRichTextCtrl::GetNumberOfLines() const
2262 return GetBuffer().GetParagraphCount();
2265 // ----------------------------------------------------------------------------
2266 // Positions <-> coords
2267 // ----------------------------------------------------------------------------
2269 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2271 return GetBuffer().XYToPosition(x
, y
);
2274 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2276 return GetBuffer().PositionToXY(pos
, x
, y
);
2279 // ----------------------------------------------------------------------------
2281 // ----------------------------------------------------------------------------
2283 void wxRichTextCtrl::ShowPosition(long pos
)
2285 if (!IsPositionVisible(pos
))
2286 ScrollIntoView(pos
-1, WXK_DOWN
);
2289 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2291 return GetBuffer().GetParagraphLength(lineNo
);
2294 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2296 return GetBuffer().GetParagraphText(lineNo
);
2299 // ----------------------------------------------------------------------------
2301 // ----------------------------------------------------------------------------
2303 void wxRichTextCtrl::Undo()
2307 GetCommandProcessor()->Undo();
2311 void wxRichTextCtrl::Redo()
2315 GetCommandProcessor()->Redo();
2319 bool wxRichTextCtrl::CanUndo() const
2321 return GetCommandProcessor()->CanUndo();
2324 bool wxRichTextCtrl::CanRedo() const
2326 return GetCommandProcessor()->CanRedo();
2329 // ----------------------------------------------------------------------------
2330 // implementation details
2331 // ----------------------------------------------------------------------------
2333 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2335 SetValue(event
.GetString());
2336 GetEventHandler()->ProcessEvent(event
);
2339 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2341 // By default, load the first file into the text window.
2342 if (event
.GetNumberOfFiles() > 0)
2344 LoadFile(event
.GetFiles()[0]);
2348 wxSize
wxRichTextCtrl::DoGetBestSize() const
2350 return wxSize(10, 10);
2353 // ----------------------------------------------------------------------------
2354 // standard handlers for standard edit menu events
2355 // ----------------------------------------------------------------------------
2357 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2362 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2367 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2372 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2377 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2382 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2387 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2389 event
.Enable( CanCut() );
2392 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2394 event
.Enable( CanCopy() );
2397 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2399 event
.Enable( CanDeleteSelection() );
2402 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2404 event
.Enable( CanPaste() );
2407 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2409 event
.Enable( CanUndo() );
2410 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2413 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2415 event
.Enable( CanRedo() );
2416 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2419 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2424 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2426 event
.Enable(GetLastPosition() > 0);
2429 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2433 m_contextMenu
= new wxMenu
;
2434 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2435 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2436 m_contextMenu
->AppendSeparator();
2437 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2438 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2439 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2440 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2441 m_contextMenu
->AppendSeparator();
2442 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2444 PopupMenu(m_contextMenu
);
2448 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2450 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2453 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2455 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2458 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2460 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2463 // extended style setting operation with flags including:
2464 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2465 // see richtextbuffer.h for more details.
2466 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2468 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2471 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2473 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2476 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2478 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2481 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2483 return GetBuffer().SetDefaultStyle(style
);
2486 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2488 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2491 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2493 return GetBuffer().GetDefaultStyle();
2496 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2498 return GetBuffer().GetDefaultStyle();
2501 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2503 wxTextAttrEx
attr(style
);
2504 if (GetBuffer().GetStyle(position
, attr
))
2513 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2515 return GetBuffer().GetStyle(position
, style
);
2518 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2520 return GetBuffer().GetStyle(position
, style
);
2523 /// Get the content (uncombined) attributes for this position.
2525 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2527 wxTextAttrEx
attr(style
);
2528 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2537 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2539 return GetBuffer().GetUncombinedStyle(position
, style
);
2542 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2544 return GetBuffer().GetUncombinedStyle(position
, style
);
2547 /// Set font, and also the buffer attributes
2548 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2550 wxControl::SetFont(font
);
2552 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2554 GetBuffer().SetBasicStyle(attr
);
2556 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2562 /// Transform logical to physical
2563 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2566 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2571 /// Transform physical to logical
2572 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2575 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2580 /// Position the caret
2581 void wxRichTextCtrl::PositionCaret()
2586 //wxLogDebug(wxT("PositionCaret"));
2589 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2591 wxPoint originalPt
= caretRect
.GetPosition();
2592 wxPoint pt
= GetPhysicalPoint(originalPt
);
2593 if (GetCaret()->GetPosition() != pt
)
2595 GetCaret()->Move(pt
);
2596 GetCaret()->SetSize(caretRect
.GetSize());
2601 /// Get the caret height and position for the given character position
2602 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2604 wxClientDC
dc(this);
2605 dc
.SetFont(GetFont());
2612 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2614 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2621 /// Gets the line for the visible caret position. If the caret is
2622 /// shown at the very end of the line, it means the next character is actually
2623 /// on the following line. So let's get the line we're expecting to find
2624 /// if this is the case.
2625 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2627 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2628 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2631 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2632 if (caretPosition
== lineRange
.GetStart()-1 &&
2633 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2635 if (!m_caretAtLineStart
)
2636 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2643 /// Move the caret to the given character position
2644 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2646 if (GetBuffer().GetDirty())
2649 if (pos
<= GetBuffer().GetRange().GetEnd())
2651 SetCaretPosition(pos
, showAtLineStart
);
2661 /// Layout the buffer: which we must do before certain operations, such as
2662 /// setting the caret position.
2663 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2665 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2667 wxRect
availableSpace(GetClientSize());
2668 if (availableSpace
.width
== 0)
2669 availableSpace
.width
= 10;
2670 if (availableSpace
.height
== 0)
2671 availableSpace
.height
= 10;
2673 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2674 if (onlyVisibleRect
)
2676 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2677 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2680 wxClientDC
dc(this);
2681 dc
.SetFont(GetFont());
2685 GetBuffer().Defragment();
2686 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2687 GetBuffer().Layout(dc
, availableSpace
, flags
);
2688 GetBuffer().SetDirty(false);
2697 /// Is all of the selection bold?
2698 bool wxRichTextCtrl::IsSelectionBold()
2702 wxRichTextAttr attr
;
2703 wxRichTextRange range
= GetInternalSelectionRange();
2704 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2705 attr
.SetFontWeight(wxBOLD
);
2707 return HasCharacterAttributes(range
, attr
);
2711 // If no selection, then we need to combine current style with default style
2712 // to see what the effect would be if we started typing.
2713 wxRichTextAttr attr
;
2714 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2716 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2717 if (GetStyle(pos
, attr
))
2719 if (IsDefaultStyleShowing())
2720 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2721 return attr
.GetFontWeight() == wxBOLD
;
2727 /// Is all of the selection italics?
2728 bool wxRichTextCtrl::IsSelectionItalics()
2732 wxRichTextRange range
= GetInternalSelectionRange();
2733 wxRichTextAttr attr
;
2734 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2735 attr
.SetFontStyle(wxITALIC
);
2737 return HasCharacterAttributes(range
, attr
);
2741 // If no selection, then we need to combine current style with default style
2742 // to see what the effect would be if we started typing.
2743 wxRichTextAttr attr
;
2744 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2746 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2747 if (GetStyle(pos
, attr
))
2749 if (IsDefaultStyleShowing())
2750 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2751 return attr
.GetFontStyle() == wxITALIC
;
2757 /// Is all of the selection underlined?
2758 bool wxRichTextCtrl::IsSelectionUnderlined()
2762 wxRichTextRange range
= GetInternalSelectionRange();
2763 wxRichTextAttr attr
;
2764 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2765 attr
.SetFontUnderlined(true);
2767 return HasCharacterAttributes(range
, attr
);
2771 // If no selection, then we need to combine current style with default style
2772 // to see what the effect would be if we started typing.
2773 wxRichTextAttr attr
;
2774 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2775 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2777 if (GetStyle(pos
, attr
))
2779 if (IsDefaultStyleShowing())
2780 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2781 return attr
.GetFontUnderlined();
2787 /// Apply bold to the selection
2788 bool wxRichTextCtrl::ApplyBoldToSelection()
2790 wxRichTextAttr attr
;
2791 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2792 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2795 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2797 SetAndShowDefaultStyle(attr
);
2801 /// Apply italic to the selection
2802 bool wxRichTextCtrl::ApplyItalicToSelection()
2804 wxRichTextAttr attr
;
2805 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2806 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2809 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2811 SetAndShowDefaultStyle(attr
);
2815 /// Apply underline to the selection
2816 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2818 wxRichTextAttr attr
;
2819 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2820 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2823 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2825 SetAndShowDefaultStyle(attr
);
2829 /// Is all of the selection aligned according to the specified flag?
2830 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2832 wxRichTextRange range
;
2834 range
= GetInternalSelectionRange();
2836 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2838 wxRichTextAttr attr
;
2839 attr
.SetAlignment(alignment
);
2841 return HasParagraphAttributes(range
, attr
);
2844 /// Apply alignment to the selection
2845 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2847 wxRichTextAttr attr
;
2848 attr
.SetAlignment(alignment
);
2850 return SetStyle(GetSelectionRange(), attr
);
2853 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2855 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2860 /// Apply a named style to the selection
2861 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2863 // Flags are defined within each definition, so only certain
2864 // attributes are applied.
2865 wxRichTextAttr
attr(def
->GetStyle());
2867 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2869 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2871 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2873 wxRichTextRange range
;
2876 range
= GetSelectionRange();
2879 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2880 range
= wxRichTextRange(pos
, pos
+1);
2883 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2886 // Make sure the attr has the style name
2887 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2889 attr
.SetParagraphStyleName(def
->GetName());
2891 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2892 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2893 // to change its style independently.
2894 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2897 attr
.SetCharacterStyleName(def
->GetName());
2900 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2903 SetAndShowDefaultStyle(attr
);
2908 /// Apply the style sheet to the buffer, for example if the styles have changed.
2909 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2912 styleSheet
= GetBuffer().GetStyleSheet();
2916 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2918 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2926 /// Sets the default style to the style under the cursor
2927 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2930 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2932 // If at the start of a paragraph, use the next position.
2933 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2935 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2936 if (GetUncombinedStyle(pos
, attr
))
2938 if (GetStyle(pos
, attr
))
2941 SetDefaultStyle(attr
);
2948 /// Returns the first visible position in the current view
2949 long wxRichTextCtrl::GetFirstVisiblePosition() const
2951 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2953 return line
->GetAbsoluteRange().GetStart();
2958 /// Get the first visible point in the window
2959 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
2962 int startXUnits
, startYUnits
;
2964 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
2965 GetViewStart(& startXUnits
, & startYUnits
);
2967 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
2970 /// The adjusted caret position is the character position adjusted to take
2971 /// into account whether we're at the start of a paragraph, in which case
2972 /// style information should be taken from the next position, not current one.
2973 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2975 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2977 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2982 /// Get/set the selection range in character positions. -1, -1 means no selection.
2983 /// The range is in API convention, i.e. a single character selection is denoted
2985 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2987 wxRichTextRange range
= GetInternalSelectionRange();
2988 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2989 range
.SetEnd(range
.GetEnd() + 1);
2993 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2995 wxRichTextRange
range1(range
);
2996 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2997 range1
.SetEnd(range1
.GetEnd() - 1);
2999 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3001 SetInternalSelectionRange(range1
);
3005 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3007 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3010 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3012 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3015 /// Clear list for given range
3016 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3018 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3021 /// Number/renumber any list elements in the given range
3022 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3024 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3027 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3029 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3032 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3033 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3035 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3038 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3040 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3043 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3045 if (sm_availableFontNames
.GetCount() == 0)
3047 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3048 sm_availableFontNames
.Sort();
3050 return sm_availableFontNames
;
3053 void wxRichTextCtrl::ClearAvailableFontNames()
3055 sm_availableFontNames
.Clear();