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 if (style
& wxTE_READONLY
)
137 wxTextAttrEx attributes
;
138 attributes
.SetFont(GetFont());
139 attributes
.SetTextColour(*wxBLACK
);
140 attributes
.SetBackgroundColour(*wxWHITE
);
141 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
142 attributes
.SetLineSpacing(10);
143 attributes
.SetParagraphSpacingAfter(10);
144 attributes
.SetParagraphSpacingBefore(0);
146 SetBasicStyle(attributes
);
148 // The default attributes will be merged with base attributes, so
149 // can be empty to begin with
150 wxTextAttrEx defaultAttributes
;
151 SetDefaultStyle(defaultAttributes
);
153 SetBackgroundColour(*wxWHITE
);
154 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
156 // Tell the sizers to use the given or best size
157 SetBestFittingSize(size
);
159 #if wxRICHTEXT_BUFFERED_PAINTING
161 RecreateBuffer(size
);
164 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
165 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
167 SetCursor(m_textCursor
);
169 if (!value
.IsEmpty())
172 GetBuffer().AddEventHandler(this);
177 wxRichTextCtrl::~wxRichTextCtrl()
179 GetBuffer().RemoveEventHandler(this);
181 delete m_contextMenu
;
184 /// Member initialisation
185 void wxRichTextCtrl::Init()
188 m_contextMenu
= NULL
;
190 m_caretPosition
= -1;
191 m_selectionRange
.SetRange(-2, -2);
192 m_selectionAnchor
= -2;
194 m_caretAtLineStart
= false;
196 m_fullLayoutRequired
= false;
197 m_fullLayoutTime
= 0;
198 m_fullLayoutSavedPosition
= 0;
199 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
200 m_caretPositionForDefaultStyle
= -2;
203 /// Call Freeze to prevent refresh
204 void wxRichTextCtrl::Freeze()
209 /// Call Thaw to refresh
210 void wxRichTextCtrl::Thaw()
214 if (m_freezeCount
== 0)
222 void wxRichTextCtrl::Clear()
224 m_buffer
.ResetAndClearCommands();
225 m_buffer
.SetDirty(true);
226 m_caretPosition
= -1;
227 m_caretPositionForDefaultStyle
= -2;
228 m_caretAtLineStart
= false;
229 m_selectionRange
.SetRange(-2, -2);
231 SetScrollbars(0, 0, 0, 0, 0, 0);
233 if (m_freezeCount
== 0)
238 SendTextUpdatedEvent();
242 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
248 #if wxRICHTEXT_BUFFERED_PAINTING
249 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
255 if (m_freezeCount
> 0)
258 dc
.SetFont(GetFont());
260 // Paint the background
263 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
264 wxRect
availableSpace(GetClientSize());
265 if (GetBuffer().GetDirty())
267 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
268 GetBuffer().SetDirty(false);
272 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
281 // Empty implementation, to prevent flicker
282 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
286 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
288 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
297 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
306 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
312 dc
.SetFont(GetFont());
315 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
317 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
319 m_dragStart
= event
.GetLogicalPosition(dc
);
325 bool caretAtLineStart
= false;
327 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
329 // If we're at the start of a line (but not first in para)
330 // then we should keep the caret showing at the start of the line
331 // by showing the m_caretAtLineStart flag.
332 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
333 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
335 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
336 caretAtLineStart
= true;
340 MoveCaret(position
, caretAtLineStart
);
341 SetDefaultStyleToCursorStyle();
348 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
353 if (GetCapture() == this)
356 // See if we clicked on a URL
359 dc
.SetFont(GetFont());
362 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
363 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
365 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
368 if (GetStyle(position
, attr
))
370 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
372 wxString urlTarget
= attr
.GetURL();
373 if (!urlTarget
.IsEmpty())
375 wxMouseEvent
mouseEvent(event
);
377 long startPos
= 0, endPos
= 0;
378 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
381 startPos
= obj
->GetRange().GetStart();
382 endPos
= obj
->GetRange().GetEnd();
385 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
386 InitCommandEvent(urlEvent
);
388 urlEvent
.SetString(urlTarget
);
390 GetEventHandler()->ProcessEvent(urlEvent
);
399 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
403 dc
.SetFont(GetFont());
406 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
407 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
409 // See if we need to change the cursor
412 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
415 if (GetStyle(position
, attr
))
417 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
419 if (GetCursor() != m_urlCursor
)
420 SetCursor(m_urlCursor
);
422 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
424 if (GetCursor() != m_textCursor
)
425 SetCursor(m_textCursor
);
431 if (!event
.Dragging())
437 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
439 // TODO: test closeness
441 bool caretAtLineStart
= false;
443 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
445 // If we're at the start of a line (but not first in para)
446 // then we should keep the caret showing at the start of the line
447 // by showing the m_caretAtLineStart flag.
448 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
449 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
451 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
452 caretAtLineStart
= true;
456 if (m_caretPosition
!= position
)
458 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
460 MoveCaret(position
, caretAtLineStart
);
461 SetDefaultStyleToCursorStyle();
470 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
476 /// Left-double-click
477 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
479 SelectWord(GetCaretPosition()+1);
484 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
490 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
494 flags
|= wxRICHTEXT_CTRL_DOWN
;
495 if (event
.ShiftDown())
496 flags
|= wxRICHTEXT_SHIFT_DOWN
;
498 flags
|= wxRICHTEXT_ALT_DOWN
;
500 if (event
.GetKeyCode() == WXK_LEFT
||
501 event
.GetKeyCode() == WXK_RIGHT
||
502 event
.GetKeyCode() == WXK_UP
||
503 event
.GetKeyCode() == WXK_DOWN
||
504 event
.GetKeyCode() == WXK_HOME
||
505 event
.GetKeyCode() == WXK_PAGEUP
||
506 event
.GetKeyCode() == WXK_PAGEDOWN
||
507 event
.GetKeyCode() == WXK_END
||
509 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
510 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
511 event
.GetKeyCode() == WXK_NUMPAD_UP
||
512 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
513 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
514 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
515 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
516 event
.GetKeyCode() == WXK_NUMPAD_END
)
518 KeyboardNavigate(event
.GetKeyCode(), flags
);
522 // all the other keys modify the controls contents which shouldn't be
523 // possible if we're read-only
530 if (event
.GetKeyCode() == WXK_RETURN
)
532 BeginBatchUndo(_("Insert Text"));
534 long newPos
= m_caretPosition
;
536 DeleteSelectedContent(& newPos
);
538 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
540 SetDefaultStyleToCursorStyle();
542 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
544 wxRichTextEvent
cmdEvent(
545 wxEVT_COMMAND_RICHTEXT_RETURN
,
547 cmdEvent
.SetEventObject(this);
548 cmdEvent
.SetFlags(flags
);
549 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
551 // Generate conventional event
552 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
553 InitCommandEvent(textEvent
);
555 GetEventHandler()->ProcessEvent(textEvent
);
558 else if (event
.GetKeyCode() == WXK_BACK
)
560 BeginBatchUndo(_("Delete Text"));
562 // Submit range in character positions, which are greater than caret positions,
563 // so subtract 1 for deleted character and add 1 for conversion to character position.
564 if (m_caretPosition
> -1 && !HasSelection())
566 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
567 m_caretPosition
, // Current caret position
568 m_caretPosition
-1, // New caret position
572 DeleteSelectedContent();
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 if (GetLastPosition() == -1)
608 m_caretPosition
= -1;
610 SetDefaultStyleToCursorStyle();
615 long keycode
= event
.GetKeyCode();
689 case WXK_NUMPAD_SPACE
:
691 case WXK_NUMPAD_ENTER
:
696 case WXK_NUMPAD_HOME
:
697 case WXK_NUMPAD_LEFT
:
699 case WXK_NUMPAD_RIGHT
:
700 case WXK_NUMPAD_DOWN
:
701 case WXK_NUMPAD_PAGEUP
:
702 case WXK_NUMPAD_PAGEDOWN
:
704 case WXK_NUMPAD_BEGIN
:
705 case WXK_NUMPAD_INSERT
:
706 case WXK_NUMPAD_DELETE
:
707 case WXK_NUMPAD_EQUAL
:
708 case WXK_NUMPAD_MULTIPLY
:
710 case WXK_NUMPAD_SEPARATOR
:
711 case WXK_NUMPAD_SUBTRACT
:
712 case WXK_NUMPAD_DECIMAL
:
720 if (event
.CmdDown() || event
.AltDown())
726 if (keycode
== wxT('\t'))
728 // See if we need to promote or demote the selection or paragraph at the cursor
729 // position, instead of inserting a tab.
730 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
731 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
732 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
734 wxRichTextRange range
;
736 range
= GetSelectionRange();
738 range
= para
->GetRange().FromInternal();
740 int promoteBy
= event
.ShiftDown() ? 1 : -1;
742 PromoteList(promoteBy
, range
, NULL
);
748 BeginBatchUndo(_("Insert Text"));
750 long newPos
= m_caretPosition
;
751 DeleteSelectedContent(& newPos
);
753 wxString str
= (wxChar
) event
.GetKeyCode();
754 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
758 SetDefaultStyleToCursorStyle();
759 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
765 /// Delete content if there is a selection, e.g. when pressing a key.
766 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
770 long pos
= m_selectionRange
.GetStart();
771 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
772 m_caretPosition
, // Current caret position
773 pos
, // New caret position
775 m_selectionRange
.SetRange(-2, -2);
785 /// Keyboard navigation
789 Left: left one character
790 Right: right one character
793 Ctrl-Left: left one word
794 Ctrl-Right: right one word
795 Ctrl-Up: previous paragraph start
796 Ctrl-Down: next start of paragraph
799 Ctrl-Home: start of document
800 Ctrl-End: end of document
802 Page-Down: Down a screen
806 Ctrl-Alt-PgUp: Start of window
807 Ctrl-Alt-PgDn: End of window
808 F8: Start selection mode
809 Esc: End selection mode
811 Adding Shift does the above but starts/extends selection.
816 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
818 bool success
= false;
820 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
822 if (flags
& wxRICHTEXT_CTRL_DOWN
)
823 success
= WordRight(1, flags
);
825 success
= MoveRight(1, flags
);
827 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
829 if (flags
& wxRICHTEXT_CTRL_DOWN
)
830 success
= WordLeft(1, flags
);
832 success
= MoveLeft(1, flags
);
834 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
836 if (flags
& wxRICHTEXT_CTRL_DOWN
)
837 success
= MoveToParagraphStart(flags
);
839 success
= MoveUp(1, flags
);
841 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
843 if (flags
& wxRICHTEXT_CTRL_DOWN
)
844 success
= MoveToParagraphEnd(flags
);
846 success
= MoveDown(1, flags
);
848 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
850 success
= PageUp(1, flags
);
852 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
854 success
= PageDown(1, flags
);
856 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
858 if (flags
& wxRICHTEXT_CTRL_DOWN
)
859 success
= MoveHome(flags
);
861 success
= MoveToLineStart(flags
);
863 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
865 if (flags
& wxRICHTEXT_CTRL_DOWN
)
866 success
= MoveEnd(flags
);
868 success
= MoveToLineEnd(flags
);
873 ScrollIntoView(m_caretPosition
, keyCode
);
874 SetDefaultStyleToCursorStyle();
880 /// Extend the selection. Selections are in caret positions.
881 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
883 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
885 // If not currently selecting, start selecting
886 if (m_selectionRange
.GetStart() == -2)
888 m_selectionAnchor
= oldPos
;
891 m_selectionRange
.SetRange(newPos
+1, oldPos
);
893 m_selectionRange
.SetRange(oldPos
+1, newPos
);
897 // Always ensure that the selection range start is greater than
899 if (newPos
> m_selectionAnchor
)
900 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
902 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
905 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
907 wxLogDebug(wxT("Strange selection range"));
916 /// Scroll into view, returning true if we scrolled.
917 /// This takes a _caret_ position.
918 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
920 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
926 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
928 int startXUnits
, startYUnits
;
929 GetViewStart(& startXUnits
, & startYUnits
);
930 int startY
= startYUnits
* ppuY
;
933 GetVirtualSize(& sx
, & sy
);
939 wxRect rect
= line
->GetRect();
941 bool scrolled
= false;
943 wxSize clientSize
= GetClientSize();
946 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
947 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
948 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
949 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
951 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
953 // Make it scroll so this item is at the bottom
955 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
956 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
958 // If we're still off the screen, scroll another line down
959 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
962 if (startYUnits
!= yUnits
)
964 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
968 else if (rect
.y
< startY
)
970 // Make it scroll so this item is at the top
973 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
975 if (startYUnits
!= yUnits
)
977 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
983 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
984 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
985 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
986 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
990 // Make it scroll so this item is at the top
993 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
995 if (startYUnits
!= yUnits
)
997 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1001 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1003 // Make it scroll so this item is at the bottom
1005 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1006 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1008 // If we're still off the screen, scroll another line down
1009 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1012 if (startYUnits
!= yUnits
)
1014 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1024 /// Is the given position visible on the screen?
1025 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1027 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1033 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1036 GetViewStart(& startX
, & startY
);
1038 startY
= startY
* ppuY
;
1041 GetVirtualSize(& sx
, & sy
);
1046 wxRect rect
= line
->GetRect();
1048 wxSize clientSize
= GetClientSize();
1050 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1053 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1055 m_caretPosition
= position
;
1056 m_caretAtLineStart
= showAtLineStart
;
1059 /// Move caret one visual step forward: this may mean setting a flag
1060 /// and keeping the same position if we're going from the end of one line
1061 /// to the start of the next, which may be the exact same caret position.
1062 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1064 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1066 // Only do the check if we're not at the end of the paragraph (where things work OK
1068 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1070 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1074 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1076 // We're at the end of a line. See whether we need to
1077 // stay at the same actual caret position but change visual
1078 // position, or not.
1079 if (oldPosition
== lineRange
.GetEnd())
1081 if (m_caretAtLineStart
)
1083 // We're already at the start of the line, so actually move on now.
1084 m_caretPosition
= oldPosition
+ 1;
1085 m_caretAtLineStart
= false;
1089 // We're showing at the end of the line, so keep to
1090 // the same position but indicate that we're to show
1091 // at the start of the next line.
1092 m_caretPosition
= oldPosition
;
1093 m_caretAtLineStart
= true;
1095 SetDefaultStyleToCursorStyle();
1101 SetDefaultStyleToCursorStyle();
1104 /// Move caret one visual step backward: this may mean setting a flag
1105 /// and keeping the same position if we're going from the end of one line
1106 /// to the start of the next, which may be the exact same caret position.
1107 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1109 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1111 // Only do the check if we're not at the start of the paragraph (where things work OK
1113 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1115 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1119 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1121 // We're at the start of a line. See whether we need to
1122 // stay at the same actual caret position but change visual
1123 // position, or not.
1124 if (oldPosition
== lineRange
.GetStart())
1126 m_caretPosition
= oldPosition
-1;
1127 m_caretAtLineStart
= true;
1130 else if (oldPosition
== lineRange
.GetEnd())
1132 if (m_caretAtLineStart
)
1134 // We're at the start of the line, so keep the same caret position
1135 // but clear the start-of-line flag.
1136 m_caretPosition
= oldPosition
;
1137 m_caretAtLineStart
= false;
1141 // We're showing at the end of the line, so go back
1142 // to the previous character position.
1143 m_caretPosition
= oldPosition
- 1;
1145 SetDefaultStyleToCursorStyle();
1151 SetDefaultStyleToCursorStyle();
1155 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1157 long endPos
= GetBuffer().GetRange().GetEnd();
1159 if (m_caretPosition
+ noPositions
< endPos
)
1161 long oldPos
= m_caretPosition
;
1162 long newPos
= m_caretPosition
+ noPositions
;
1164 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1168 // Determine by looking at oldPos and m_caretPosition whether
1169 // we moved from the end of a line to the start of the next line, in which case
1170 // we want to adjust the caret position such that it is positioned at the
1171 // start of the next line, rather than jumping past the first character of the
1173 if (noPositions
== 1 && !extendSel
)
1174 MoveCaretForward(oldPos
);
1176 SetCaretPosition(newPos
);
1179 SetDefaultStyleToCursorStyle();
1190 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1194 if (m_caretPosition
> startPos
- noPositions
+ 1)
1196 long oldPos
= m_caretPosition
;
1197 long newPos
= m_caretPosition
- noPositions
;
1198 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1202 if (noPositions
== 1 && !extendSel
)
1203 MoveCaretBack(oldPos
);
1205 SetCaretPosition(newPos
);
1208 SetDefaultStyleToCursorStyle();
1219 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1221 return MoveDown(- noLines
, flags
);
1225 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1230 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1231 wxPoint pt
= GetCaret()->GetPosition();
1232 long newLine
= lineNumber
+ noLines
;
1234 if (lineNumber
!= -1)
1238 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1240 if (newLine
> lastLine
)
1250 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1253 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1259 wxClientDC
dc(this);
1261 dc
.SetFont(GetFont());
1263 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1265 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1267 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1268 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1269 // so we view the caret at the start of the line.
1270 bool caretLineStart
= false;
1271 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1273 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1274 wxRichTextRange lineRange
;
1276 lineRange
= thisLine
->GetAbsoluteRange();
1278 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1281 caretLineStart
= true;
1285 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1286 if (para
&& para
->GetRange().GetStart() == newPos
)
1291 long newSelEnd
= newPos
;
1293 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1297 SetCaretPosition(newPos
, caretLineStart
);
1299 SetDefaultStyleToCursorStyle();
1309 /// Move to the end of the paragraph
1310 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1312 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1315 long newPos
= para
->GetRange().GetEnd() - 1;
1316 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1320 SetCaretPosition(newPos
);
1322 SetDefaultStyleToCursorStyle();
1332 /// Move to the start of the paragraph
1333 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1335 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1338 long newPos
= para
->GetRange().GetStart() - 1;
1339 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1343 SetCaretPosition(newPos
);
1345 SetDefaultStyleToCursorStyle();
1355 /// Move to the end of the line
1356 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1358 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1362 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1363 long newPos
= lineRange
.GetEnd();
1364 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1368 SetCaretPosition(newPos
);
1370 SetDefaultStyleToCursorStyle();
1380 /// Move to the start of the line
1381 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1383 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1386 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1387 long newPos
= lineRange
.GetStart()-1;
1389 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1393 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1395 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1397 SetDefaultStyleToCursorStyle();
1407 /// Move to the start of the buffer
1408 bool wxRichTextCtrl::MoveHome(int flags
)
1410 if (m_caretPosition
!= -1)
1412 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1416 SetCaretPosition(-1);
1418 SetDefaultStyleToCursorStyle();
1428 /// Move to the end of the buffer
1429 bool wxRichTextCtrl::MoveEnd(int flags
)
1431 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1433 if (m_caretPosition
!= endPos
)
1435 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1439 SetCaretPosition(endPos
);
1441 SetDefaultStyleToCursorStyle();
1451 /// Move noPages pages up
1452 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1454 return PageDown(- noPages
, flags
);
1457 /// Move noPages pages down
1458 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1460 // Calculate which line occurs noPages * screen height further down.
1461 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1464 wxSize clientSize
= GetClientSize();
1465 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1467 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1470 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1471 long pos
= lineRange
.GetStart()-1;
1472 if (pos
!= m_caretPosition
)
1474 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1476 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1480 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1482 SetDefaultStyleToCursorStyle();
1494 // Finds the caret position for the next word
1495 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1497 long endPos
= GetBuffer().GetRange().GetEnd();
1501 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1503 // First skip current text to space
1504 while (i
< endPos
&& i
> -1)
1506 // i is in character, not caret positions
1507 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1508 if (text
!= wxT(" ") && !text
.empty())
1515 while (i
< endPos
&& i
> -1)
1517 // i is in character, not caret positions
1518 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1519 if (text
.empty()) // End of paragraph, or maybe an image
1520 return wxMax(-1, i
- 1);
1521 else if (text
== wxT(" ") || text
.empty())
1525 // Convert to caret position
1526 return wxMax(-1, i
- 1);
1535 long i
= m_caretPosition
;
1537 // First skip white space
1538 while (i
< endPos
&& i
> -1)
1540 // i is in character, not caret positions
1541 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1542 if (text
.empty()) // End of paragraph, or maybe an image
1544 else if (text
== wxT(" ") || text
.empty())
1549 // Next skip current text to space
1550 while (i
< endPos
&& i
> -1)
1552 // i is in character, not caret positions
1553 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1554 if (text
!= wxT(" ") /* && !text.empty() */)
1567 /// Move n words left
1568 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1570 long pos
= FindNextWordPosition(-1);
1571 if (pos
!= m_caretPosition
)
1573 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1575 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1579 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1581 SetDefaultStyleToCursorStyle();
1591 /// Move n words right
1592 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1594 long pos
= FindNextWordPosition(1);
1595 if (pos
!= m_caretPosition
)
1597 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1599 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1603 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1605 SetDefaultStyleToCursorStyle();
1616 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1618 // Only do sizing optimization for large buffers
1619 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1621 m_fullLayoutRequired
= true;
1622 m_fullLayoutTime
= wxGetLocalTimeMillis();
1623 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1624 LayoutContent(true /* onlyVisibleRect */);
1627 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1629 #if wxRICHTEXT_BUFFERED_PAINTING
1637 /// Idle-time processing
1638 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1640 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1642 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1644 m_fullLayoutRequired
= false;
1645 m_fullLayoutTime
= 0;
1646 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1647 ShowPosition(m_fullLayoutSavedPosition
);
1651 if (m_caretPositionForDefaultStyle
!= -2)
1653 // If the caret position has changed, no longer reflect the default style
1655 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1656 m_caretPositionForDefaultStyle
= -2;
1663 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1669 /// Set up scrollbars, e.g. after a resize
1670 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1675 if (GetBuffer().IsEmpty())
1677 SetScrollbars(0, 0, 0, 0, 0, 0);
1681 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1682 // of pixels. See e.g. wxVScrolledWindow for ideas.
1683 int pixelsPerUnit
= 5;
1684 wxSize clientSize
= GetClientSize();
1686 int maxHeight
= GetBuffer().GetCachedSize().y
;
1688 // Round up so we have at least maxHeight pixels
1689 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1691 int startX
= 0, startY
= 0;
1693 GetViewStart(& startX
, & startY
);
1695 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1696 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1698 // Move to previous scroll position if
1700 SetScrollbars(0, pixelsPerUnit
,
1702 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1705 /// Paint the background
1706 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1708 wxColour backgroundColour
= GetBackgroundColour();
1709 if (!backgroundColour
.Ok())
1710 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1712 // Clear the background
1713 dc
.SetBrush(wxBrush(backgroundColour
));
1714 dc
.SetPen(*wxTRANSPARENT_PEN
);
1715 wxRect
windowRect(GetClientSize());
1716 windowRect
.x
-= 2; windowRect
.y
-= 2;
1717 windowRect
.width
+= 4; windowRect
.height
+= 4;
1719 // We need to shift the rectangle to take into account
1720 // scrolling. Converting device to logical coordinates.
1721 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1722 dc
.DrawRectangle(windowRect
);
1725 #if wxRICHTEXT_BUFFERED_PAINTING
1726 /// Recreate buffer bitmap if necessary
1727 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1730 if (sz
== wxDefaultSize
)
1731 sz
= GetClientSize();
1733 if (sz
.x
< 1 || sz
.y
< 1)
1736 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1737 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1738 return m_bufferBitmap
.Ok();
1742 // ----------------------------------------------------------------------------
1743 // file IO functions
1744 // ----------------------------------------------------------------------------
1746 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1748 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1750 m_filename
= filename
;
1753 SetInsertionPoint(0);
1756 SetupScrollbars(true);
1758 SendTextUpdatedEvent();
1764 wxLogError(_("File couldn't be loaded."));
1770 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1772 if (GetBuffer().SaveFile(filename
, fileType
))
1774 m_filename
= filename
;
1781 wxLogError(_("The text couldn't be saved."));
1786 // ----------------------------------------------------------------------------
1787 // wxRichTextCtrl specific functionality
1788 // ----------------------------------------------------------------------------
1790 /// Add a new paragraph of text to the end of the buffer
1791 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1793 return GetBuffer().AddParagraph(text
);
1797 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1799 return GetBuffer().AddImage(image
);
1802 // ----------------------------------------------------------------------------
1803 // selection and ranges
1804 // ----------------------------------------------------------------------------
1806 void wxRichTextCtrl::SelectAll()
1808 SetSelection(0, GetLastPosition()+1);
1809 m_selectionAnchor
= -1;
1813 void wxRichTextCtrl::SelectNone()
1815 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1816 SetSelection(-2, -2);
1817 m_selectionAnchor
= -2;
1820 static bool wxIsWordDelimiter(const wxString
& text
)
1822 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1825 /// Select the word at the given character position
1826 bool wxRichTextCtrl::SelectWord(long position
)
1828 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1831 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1835 long positionStart
= position
;
1836 long positionEnd
= position
;
1838 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1840 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1841 if (wxIsWordDelimiter(text
))
1847 if (positionStart
< para
->GetRange().GetStart())
1848 positionStart
= para
->GetRange().GetStart();
1850 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1852 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1853 if (wxIsWordDelimiter(text
))
1859 if (positionEnd
>= para
->GetRange().GetEnd())
1860 positionEnd
= para
->GetRange().GetEnd();
1862 SetSelection(positionStart
, positionEnd
+1);
1864 if (positionStart
>= 0)
1866 MoveCaret(positionStart
-1, true);
1867 SetDefaultStyleToCursorStyle();
1873 wxString
wxRichTextCtrl::GetStringSelection() const
1876 GetSelection(&from
, &to
);
1878 return GetRange(from
, to
);
1881 // ----------------------------------------------------------------------------
1883 // ----------------------------------------------------------------------------
1885 wxTextCtrlHitTestResult
1886 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1888 // implement in terms of the other overload as the native ports typically
1889 // can get the position and not (x, y) pair directly (although wxUniv
1890 // directly gets x and y -- and so overrides this method as well)
1892 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1894 if ( rc
!= wxTE_HT_UNKNOWN
)
1896 PositionToXY(pos
, x
, y
);
1902 wxTextCtrlHitTestResult
1903 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1906 wxClientDC
dc((wxRichTextCtrl
*) this);
1907 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1909 // Buffer uses logical position (relative to start of buffer)
1911 wxPoint pt2
= GetLogicalPoint(pt
);
1913 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1917 case wxRICHTEXT_HITTEST_BEFORE
:
1918 return wxTE_HT_BEFORE
;
1920 case wxRICHTEXT_HITTEST_AFTER
:
1921 return wxTE_HT_BEYOND
;
1923 case wxRICHTEXT_HITTEST_ON
:
1924 return wxTE_HT_ON_TEXT
;
1927 return wxTE_HT_UNKNOWN
;
1930 // ----------------------------------------------------------------------------
1931 // set/get the controls text
1932 // ----------------------------------------------------------------------------
1934 wxString
wxRichTextCtrl::GetValue() const
1936 return GetBuffer().GetText();
1939 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1941 // Public API for range is different from internals
1942 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1945 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1949 // if the text is long enough, it's faster to just set it instead of first
1950 // comparing it with the old one (chances are that it will be different
1951 // anyhow, this comparison is there to avoid flicker for small single-line
1952 // edit controls mostly)
1953 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1957 // for compatibility, don't move the cursor when doing SetValue()
1958 SetInsertionPoint(0);
1962 if ( flags
& SetValue_SendEvent
)
1964 // still send an event for consistency
1965 SendTextUpdatedEvent();
1969 // we should reset the modified flag even if the value didn't really change
1971 // mark the control as being not dirty - we changed its text, not the
1976 void wxRichTextCtrl::WriteText(const wxString
& value
)
1981 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1983 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1985 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1987 if ( flags
& SetValue_SendEvent
)
1988 SendTextUpdatedEvent();
1991 void wxRichTextCtrl::AppendText(const wxString
& text
)
1993 SetInsertionPointEnd();
1998 /// Write an image at the current insertion point
1999 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2001 wxRichTextImageBlock imageBlock
;
2003 wxImage image2
= image
;
2004 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2005 return WriteImage(imageBlock
);
2010 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2012 wxRichTextImageBlock imageBlock
;
2015 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2016 return WriteImage(imageBlock
);
2021 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2023 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2026 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2030 wxRichTextImageBlock imageBlock
;
2032 wxImage image
= bitmap
.ConvertToImage();
2033 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2034 return WriteImage(imageBlock
);
2040 /// Insert a newline (actually paragraph) at the current insertion point.
2041 bool wxRichTextCtrl::Newline()
2043 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2047 // ----------------------------------------------------------------------------
2048 // Clipboard operations
2049 // ----------------------------------------------------------------------------
2051 void wxRichTextCtrl::Copy()
2055 wxRichTextRange range
= GetInternalSelectionRange();
2056 GetBuffer().CopyToClipboard(range
);
2060 void wxRichTextCtrl::Cut()
2064 wxRichTextRange range
= GetInternalSelectionRange();
2065 GetBuffer().CopyToClipboard(range
);
2067 DeleteSelectedContent();
2073 void wxRichTextCtrl::Paste()
2077 BeginBatchUndo(_("Paste"));
2079 long newPos
= m_caretPosition
;
2080 DeleteSelectedContent(& newPos
);
2082 GetBuffer().PasteFromClipboard(newPos
);
2088 void wxRichTextCtrl::DeleteSelection()
2090 if (CanDeleteSelection())
2092 DeleteSelectedContent();
2096 bool wxRichTextCtrl::HasSelection() const
2098 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2101 bool wxRichTextCtrl::CanCopy() const
2103 // Can copy if there's a selection
2104 return HasSelection();
2107 bool wxRichTextCtrl::CanCut() const
2109 return HasSelection() && IsEditable();
2112 bool wxRichTextCtrl::CanPaste() const
2114 if ( !IsEditable() )
2117 return GetBuffer().CanPasteFromClipboard();
2120 bool wxRichTextCtrl::CanDeleteSelection() const
2122 return HasSelection() && IsEditable();
2126 // ----------------------------------------------------------------------------
2128 // ----------------------------------------------------------------------------
2130 void wxRichTextCtrl::SetEditable(bool editable
)
2132 m_editable
= editable
;
2135 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2139 m_caretPosition
= pos
- 1;
2142 void wxRichTextCtrl::SetInsertionPointEnd()
2144 long pos
= GetLastPosition();
2145 SetInsertionPoint(pos
);
2148 long wxRichTextCtrl::GetInsertionPoint() const
2150 return m_caretPosition
+1;
2153 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2155 return GetBuffer().GetRange().GetEnd();
2158 // If the return values from and to are the same, there is no
2160 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2162 *from
= m_selectionRange
.GetStart();
2163 *to
= m_selectionRange
.GetEnd();
2164 if ((*to
) != -1 && (*to
) != -2)
2168 bool wxRichTextCtrl::IsEditable() const
2173 // ----------------------------------------------------------------------------
2175 // ----------------------------------------------------------------------------
2177 void wxRichTextCtrl::SetSelection(long from
, long to
)
2179 // if from and to are both -1, it means (in wxWidgets) that all text should
2181 if ( (from
== -1) && (to
== -1) )
2184 to
= GetLastPosition()+1;
2187 DoSetSelection(from
, to
);
2190 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2192 m_selectionAnchor
= from
;
2193 m_selectionRange
.SetRange(from
, to
-1);
2199 // ----------------------------------------------------------------------------
2201 // ----------------------------------------------------------------------------
2203 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2204 const wxString
& value
)
2206 BeginBatchUndo(_("Replace"));
2208 DeleteSelectedContent();
2210 DoWriteText(value
, SetValue_SelectionOnly
);
2215 void wxRichTextCtrl::Remove(long from
, long to
)
2219 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2220 m_caretPosition
, // Current caret position
2221 from
, // New caret position
2229 bool wxRichTextCtrl::IsModified() const
2231 return m_buffer
.IsModified();
2234 void wxRichTextCtrl::MarkDirty()
2236 m_buffer
.Modify(true);
2239 void wxRichTextCtrl::DiscardEdits()
2241 m_caretPositionForDefaultStyle
= -2;
2242 m_buffer
.Modify(false);
2243 m_buffer
.GetCommandProcessor()->ClearCommands();
2246 int wxRichTextCtrl::GetNumberOfLines() const
2248 return GetBuffer().GetParagraphCount();
2251 // ----------------------------------------------------------------------------
2252 // Positions <-> coords
2253 // ----------------------------------------------------------------------------
2255 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2257 return GetBuffer().XYToPosition(x
, y
);
2260 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2262 return GetBuffer().PositionToXY(pos
, x
, y
);
2265 // ----------------------------------------------------------------------------
2267 // ----------------------------------------------------------------------------
2269 void wxRichTextCtrl::ShowPosition(long pos
)
2271 if (!IsPositionVisible(pos
))
2272 ScrollIntoView(pos
-1, WXK_DOWN
);
2275 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2277 return GetBuffer().GetParagraphLength(lineNo
);
2280 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2282 return GetBuffer().GetParagraphText(lineNo
);
2285 // ----------------------------------------------------------------------------
2287 // ----------------------------------------------------------------------------
2289 void wxRichTextCtrl::Undo()
2293 GetCommandProcessor()->Undo();
2297 void wxRichTextCtrl::Redo()
2301 GetCommandProcessor()->Redo();
2305 bool wxRichTextCtrl::CanUndo() const
2307 return GetCommandProcessor()->CanUndo();
2310 bool wxRichTextCtrl::CanRedo() const
2312 return GetCommandProcessor()->CanRedo();
2315 // ----------------------------------------------------------------------------
2316 // implementation details
2317 // ----------------------------------------------------------------------------
2319 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2321 SetValue(event
.GetString());
2322 GetEventHandler()->ProcessEvent(event
);
2325 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2327 // By default, load the first file into the text window.
2328 if (event
.GetNumberOfFiles() > 0)
2330 LoadFile(event
.GetFiles()[0]);
2334 wxSize
wxRichTextCtrl::DoGetBestSize() const
2336 return wxSize(10, 10);
2339 // ----------------------------------------------------------------------------
2340 // standard handlers for standard edit menu events
2341 // ----------------------------------------------------------------------------
2343 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2348 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2353 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2358 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2363 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2368 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2373 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2375 event
.Enable( CanCut() );
2378 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2380 event
.Enable( CanCopy() );
2383 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2385 event
.Enable( CanDeleteSelection() );
2388 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2390 event
.Enable( CanPaste() );
2393 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2395 event
.Enable( CanUndo() );
2396 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2399 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2401 event
.Enable( CanRedo() );
2402 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2405 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2410 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2412 event
.Enable(GetLastPosition() > 0);
2415 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2419 m_contextMenu
= new wxMenu
;
2420 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2421 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2422 m_contextMenu
->AppendSeparator();
2423 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2424 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2425 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2426 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2427 m_contextMenu
->AppendSeparator();
2428 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2430 PopupMenu(m_contextMenu
);
2434 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2436 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2439 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2441 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2444 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2446 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2449 // extended style setting operation with flags including:
2450 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2451 // see richtextbuffer.h for more details.
2452 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2454 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2457 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2459 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2462 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2464 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2467 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2469 return GetBuffer().SetDefaultStyle(style
);
2472 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2474 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2477 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2479 return GetBuffer().GetDefaultStyle();
2482 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2484 return GetBuffer().GetDefaultStyle();
2487 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2489 wxTextAttrEx
attr(style
);
2490 if (GetBuffer().GetStyle(position
, attr
))
2499 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2501 return GetBuffer().GetStyle(position
, style
);
2504 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2506 return GetBuffer().GetStyle(position
, style
);
2509 /// Get the content (uncombined) attributes for this position.
2511 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2513 wxTextAttrEx
attr(style
);
2514 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2523 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2525 return GetBuffer().GetUncombinedStyle(position
, style
);
2528 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2530 return GetBuffer().GetUncombinedStyle(position
, style
);
2533 /// Set font, and also the buffer attributes
2534 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2536 wxControl::SetFont(font
);
2538 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2540 GetBuffer().SetBasicStyle(attr
);
2542 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2548 /// Transform logical to physical
2549 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2552 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2557 /// Transform physical to logical
2558 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2561 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2566 /// Position the caret
2567 void wxRichTextCtrl::PositionCaret()
2572 //wxLogDebug(wxT("PositionCaret"));
2575 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2577 wxPoint originalPt
= caretRect
.GetPosition();
2578 wxPoint pt
= GetPhysicalPoint(originalPt
);
2579 if (GetCaret()->GetPosition() != pt
)
2581 GetCaret()->Move(pt
);
2582 GetCaret()->SetSize(caretRect
.GetSize());
2587 /// Get the caret height and position for the given character position
2588 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2590 wxClientDC
dc(this);
2591 dc
.SetFont(GetFont());
2598 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2600 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2607 /// Gets the line for the visible caret position. If the caret is
2608 /// shown at the very end of the line, it means the next character is actually
2609 /// on the following line. So let's get the line we're expecting to find
2610 /// if this is the case.
2611 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2613 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2614 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2617 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2618 if (caretPosition
== lineRange
.GetStart()-1 &&
2619 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2621 if (!m_caretAtLineStart
)
2622 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2629 /// Move the caret to the given character position
2630 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2632 if (GetBuffer().GetDirty())
2635 if (pos
<= GetBuffer().GetRange().GetEnd())
2637 SetCaretPosition(pos
, showAtLineStart
);
2647 /// Layout the buffer: which we must do before certain operations, such as
2648 /// setting the caret position.
2649 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2651 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2653 wxRect
availableSpace(GetClientSize());
2654 if (availableSpace
.width
== 0)
2655 availableSpace
.width
= 10;
2656 if (availableSpace
.height
== 0)
2657 availableSpace
.height
= 10;
2659 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2660 if (onlyVisibleRect
)
2662 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2663 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2666 wxClientDC
dc(this);
2667 dc
.SetFont(GetFont());
2671 GetBuffer().Defragment();
2672 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2673 GetBuffer().Layout(dc
, availableSpace
, flags
);
2674 GetBuffer().SetDirty(false);
2683 /// Is all of the selection bold?
2684 bool wxRichTextCtrl::IsSelectionBold()
2688 wxRichTextAttr attr
;
2689 wxRichTextRange range
= GetInternalSelectionRange();
2690 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2691 attr
.SetFontWeight(wxBOLD
);
2693 return HasCharacterAttributes(range
, attr
);
2697 // If no selection, then we need to combine current style with default style
2698 // to see what the effect would be if we started typing.
2699 wxRichTextAttr attr
;
2700 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2702 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2703 if (GetStyle(pos
, attr
))
2705 if (IsDefaultStyleShowing())
2706 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2707 return attr
.GetFontWeight() == wxBOLD
;
2713 /// Is all of the selection italics?
2714 bool wxRichTextCtrl::IsSelectionItalics()
2718 wxRichTextRange range
= GetInternalSelectionRange();
2719 wxRichTextAttr attr
;
2720 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2721 attr
.SetFontStyle(wxITALIC
);
2723 return HasCharacterAttributes(range
, attr
);
2727 // If no selection, then we need to combine current style with default style
2728 // to see what the effect would be if we started typing.
2729 wxRichTextAttr attr
;
2730 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2732 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2733 if (GetStyle(pos
, attr
))
2735 if (IsDefaultStyleShowing())
2736 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2737 return attr
.GetFontStyle() == wxITALIC
;
2743 /// Is all of the selection underlined?
2744 bool wxRichTextCtrl::IsSelectionUnderlined()
2748 wxRichTextRange range
= GetInternalSelectionRange();
2749 wxRichTextAttr attr
;
2750 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2751 attr
.SetFontUnderlined(true);
2753 return HasCharacterAttributes(range
, attr
);
2757 // If no selection, then we need to combine current style with default style
2758 // to see what the effect would be if we started typing.
2759 wxRichTextAttr attr
;
2760 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2761 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2763 if (GetStyle(pos
, attr
))
2765 if (IsDefaultStyleShowing())
2766 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2767 return attr
.GetFontUnderlined();
2773 /// Apply bold to the selection
2774 bool wxRichTextCtrl::ApplyBoldToSelection()
2776 wxRichTextAttr attr
;
2777 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2778 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2781 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2783 SetAndShowDefaultStyle(attr
);
2787 /// Apply italic to the selection
2788 bool wxRichTextCtrl::ApplyItalicToSelection()
2790 wxRichTextAttr attr
;
2791 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2792 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2795 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2797 SetAndShowDefaultStyle(attr
);
2801 /// Apply underline to the selection
2802 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2804 wxRichTextAttr attr
;
2805 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2806 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2809 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2811 SetAndShowDefaultStyle(attr
);
2815 /// Is all of the selection aligned according to the specified flag?
2816 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2818 wxRichTextRange range
;
2820 range
= GetInternalSelectionRange();
2822 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2824 wxRichTextAttr attr
;
2825 attr
.SetAlignment(alignment
);
2827 return HasParagraphAttributes(range
, attr
);
2830 /// Apply alignment to the selection
2831 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2833 wxRichTextAttr attr
;
2834 attr
.SetAlignment(alignment
);
2836 return SetStyle(GetSelectionRange(), attr
);
2839 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2841 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2846 /// Apply a named style to the selection
2847 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2849 // Flags are defined within each definition, so only certain
2850 // attributes are applied.
2851 wxRichTextAttr
attr(def
->GetStyle());
2853 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2855 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2857 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2859 wxRichTextRange range
;
2862 range
= GetSelectionRange();
2865 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2866 range
= wxRichTextRange(pos
, pos
+1);
2869 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2872 // Make sure the attr has the style name
2873 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2875 attr
.SetParagraphStyleName(def
->GetName());
2877 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2878 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2879 // to change its style independently.
2880 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2883 attr
.SetCharacterStyleName(def
->GetName());
2886 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2889 SetAndShowDefaultStyle(attr
);
2894 /// Apply the style sheet to the buffer, for example if the styles have changed.
2895 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2898 styleSheet
= GetBuffer().GetStyleSheet();
2902 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2904 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2912 /// Sets the default style to the style under the cursor
2913 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2916 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2918 // If at the start of a paragraph, use the next position.
2919 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2921 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2922 if (GetUncombinedStyle(pos
, attr
))
2924 if (GetStyle(pos
, attr
))
2927 SetDefaultStyle(attr
);
2934 /// Returns the first visible position in the current view
2935 long wxRichTextCtrl::GetFirstVisiblePosition() const
2937 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2939 return line
->GetAbsoluteRange().GetStart();
2944 /// The adjusted caret position is the character position adjusted to take
2945 /// into account whether we're at the start of a paragraph, in which case
2946 /// style information should be taken from the next position, not current one.
2947 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2949 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2951 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2956 /// Get/set the selection range in character positions. -1, -1 means no selection.
2957 /// The range is in API convention, i.e. a single character selection is denoted
2959 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2961 wxRichTextRange range
= GetInternalSelectionRange();
2962 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2963 range
.SetEnd(range
.GetEnd() + 1);
2967 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2969 wxRichTextRange
range1(range
);
2970 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2971 range1
.SetEnd(range1
.GetEnd() - 1);
2973 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2975 SetInternalSelectionRange(range1
);
2979 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2981 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
2984 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2986 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
2989 /// Clear list for given range
2990 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2992 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
2995 /// Number/renumber any list elements in the given range
2996 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2998 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3001 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3003 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3006 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3007 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3009 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3012 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3014 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3017 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3019 if (sm_availableFontNames
.GetCount() == 0)
3021 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3022 sm_availableFontNames
.Sort();
3024 return sm_availableFontNames
;
3027 void wxRichTextCtrl::ClearAvailableFontNames()
3029 sm_availableFontNames
.Clear();