1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/richtext/richtextctrl.h"
21 #include "wx/richtext/richtextstyles.h"
25 #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"
37 #if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
38 #define wxHAVE_PRIMARY_SELECTION 1
40 #define wxHAVE_PRIMARY_SELECTION 0
43 #if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION
44 #include "wx/clipbrd.h"
47 // DLL options compatibility check:
49 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
51 wxDEFINE_EVENT( wxEVT_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
52 wxDEFINE_EVENT( wxEVT_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
54 wxDEFINE_EVENT( wxEVT_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_RICHTEXT_RETURN
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_RICHTEXT_CHARACTER
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_RICHTEXT_DELETE
, wxRichTextEvent
);
59 wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
60 wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
61 wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
62 wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
64 wxDEFINE_EVENT( wxEVT_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
65 wxDEFINE_EVENT( wxEVT_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
66 wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
67 wxDEFINE_EVENT( wxEVT_RICHTEXT_PROPERTIES_CHANGED
, wxRichTextEvent
);
68 wxDEFINE_EVENT( wxEVT_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
69 wxDEFINE_EVENT( wxEVT_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
70 wxDEFINE_EVENT( wxEVT_RICHTEXT_FOCUS_OBJECT_CHANGED
, wxRichTextEvent
);
72 #if wxRICHTEXT_USE_OWN_CARET
77 * This implements a non-flashing cursor in case there
78 * are platform-specific problems with the generic caret.
79 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
82 class wxRichTextCaret
;
83 class wxRichTextCaretTimer
: public wxTimer
86 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
90 virtual void Notify();
91 wxRichTextCaret
* m_caret
;
94 class wxRichTextCaret
: public wxCaret
99 // default - use Create()
100 wxRichTextCaret(): m_timer(this) { Init(); }
101 // creates a block caret associated with the given window
102 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
103 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
104 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
105 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
107 virtual ~wxRichTextCaret();
112 // called by wxWindow (not using the event tables)
113 virtual void OnSetFocus();
114 virtual void OnKillFocus();
116 // draw the caret on the given DC
117 void DoDraw(wxDC
*dc
);
119 // get the visible count
120 int GetVisibleCount() const { return m_countVisible
; }
122 // delay repositioning
123 bool GetNeedsUpdate() const { return m_needsUpdate
; }
124 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
128 bool GetRefreshEnabled() const { return m_refreshEnabled
; }
129 void EnableRefresh(bool b
) { m_refreshEnabled
= b
; }
132 virtual void DoShow();
133 virtual void DoHide();
134 virtual void DoMove();
135 virtual void DoSize();
145 bool m_hasFocus
; // true => our window has focus
146 bool m_needsUpdate
; // must be repositioned
148 wxRichTextCaretTimer m_timer
;
149 wxRichTextCtrl
* m_richTextCtrl
;
150 bool m_refreshEnabled
;
152 wxBrush m_caretBrush
;
156 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl
)
158 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
160 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
161 EVT_PAINT(wxRichTextCtrl::OnPaint
)
162 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
163 EVT_IDLE(wxRichTextCtrl::OnIdle
)
164 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
165 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
166 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
167 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
168 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
169 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
170 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
171 EVT_CHAR(wxRichTextCtrl::OnChar
)
172 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
173 EVT_SIZE(wxRichTextCtrl::OnSize
)
174 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
175 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
176 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
177 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
178 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
180 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
181 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
183 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
184 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
186 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
187 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
189 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
190 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
192 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
193 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
195 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
196 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
198 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
199 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
201 EVT_MENU(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnProperties
)
202 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnUpdateProperties
)
204 EVT_MENU(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnProperties
)
205 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnUpdateProperties
)
207 EVT_MENU(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnProperties
)
208 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnUpdateProperties
)
216 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
218 wxRichTextCtrl::wxRichTextCtrl()
219 : wxScrollHelper(this)
224 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
226 const wxString
& value
,
230 const wxValidator
& validator
,
231 const wxString
& name
)
232 : wxScrollHelper(this)
235 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
239 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
240 const wxValidator
& validator
, const wxString
& name
)
244 if (!wxControl::Create(parent
, id
, pos
, size
,
245 style
|wxFULL_REPAINT_ON_RESIZE
,
249 if (!GetFont().IsOk())
251 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
254 // No physical scrolling, so we can preserve margins
255 EnableScrolling(false, false);
257 if (style
& wxTE_READONLY
)
260 // The base attributes must all have default values
261 wxRichTextAttr attributes
;
262 attributes
.SetFont(GetFont());
263 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
264 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
265 attributes
.SetLineSpacing(10);
266 attributes
.SetParagraphSpacingAfter(10);
267 attributes
.SetParagraphSpacingBefore(0);
268 SetBasicStyle(attributes
);
271 SetMargins(margin
, margin
);
273 // The default attributes will be merged with base attributes, so
274 // can be empty to begin with
275 wxRichTextAttr defaultAttributes
;
276 SetDefaultStyle(defaultAttributes
);
278 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
279 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
282 GetBuffer().SetRichTextCtrl(this);
284 #if wxRICHTEXT_USE_OWN_CARET
285 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
287 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
290 // Tell the sizers to use the given or best size
291 SetInitialSize(size
);
293 #if wxRICHTEXT_BUFFERED_PAINTING
295 RecreateBuffer(size
);
298 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
299 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
301 SetCursor(m_textCursor
);
303 if (!value
.IsEmpty())
306 GetBuffer().AddEventHandler(this);
309 wxAcceleratorEntry entries
[6];
311 entries
[0].Set(wxACCEL_CTRL
, (int) 'C', wxID_COPY
);
312 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_CUT
);
313 entries
[2].Set(wxACCEL_CTRL
, (int) 'V', wxID_PASTE
);
314 entries
[3].Set(wxACCEL_CTRL
, (int) 'A', wxID_SELECTALL
);
315 entries
[4].Set(wxACCEL_CTRL
, (int) 'Z', wxID_UNDO
);
316 entries
[5].Set(wxACCEL_CTRL
, (int) 'Y', wxID_REDO
);
318 wxAcceleratorTable
accel(6, entries
);
319 SetAcceleratorTable(accel
);
321 m_contextMenu
= new wxMenu
;
322 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
323 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
324 m_contextMenu
->AppendSeparator();
325 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
326 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
327 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
328 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
329 m_contextMenu
->AppendSeparator();
330 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
331 m_contextMenu
->AppendSeparator();
332 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
334 #if wxUSE_DRAG_AND_DROP
335 SetDropTarget(new wxRichTextDropTarget(this));
341 wxRichTextCtrl::~wxRichTextCtrl()
343 SetFocusObject(& GetBuffer(), false);
344 GetBuffer().RemoveEventHandler(this);
346 delete m_contextMenu
;
349 /// Member initialisation
350 void wxRichTextCtrl::Init()
352 m_contextMenu
= NULL
;
354 m_caretPosition
= -1;
355 m_selectionAnchor
= -2;
356 m_selectionAnchorObject
= NULL
;
357 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
359 m_useVirtualAttributes
= false;
360 m_verticalScrollbarEnabled
= true;
361 m_caretAtLineStart
= false;
363 #if wxUSE_DRAG_AND_DROP
366 m_fullLayoutRequired
= false;
367 m_fullLayoutTime
= 0;
368 m_fullLayoutSavedPosition
= 0;
369 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
370 m_caretPositionForDefaultStyle
= -2;
371 m_focusObject
= & m_buffer
;
375 void wxRichTextCtrl::DoThaw()
377 if (GetBuffer().IsDirty())
386 void wxRichTextCtrl::Clear()
388 if (GetFocusObject() == & GetBuffer())
390 m_buffer
.ResetAndClearCommands();
391 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
395 GetFocusObject()->Reset();
398 m_caretPosition
= -1;
399 m_caretPositionForDefaultStyle
= -2;
400 m_caretAtLineStart
= false;
402 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
412 wxTextCtrl::SendTextUpdatedEvent(this);
416 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
418 #if !wxRICHTEXT_USE_OWN_CARET
419 if (GetCaret() && !IsFrozen())
422 // Stop the caret refreshing the control from within the
425 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(false);
429 #if wxRICHTEXT_BUFFERED_PAINTING
430 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
440 dc
.SetFont(GetFont());
442 wxRect
drawingArea(GetUpdateRegion().GetBox());
443 drawingArea
.SetPosition(GetUnscaledPoint(GetLogicalPoint(drawingArea
.GetPosition())));
444 drawingArea
.SetSize(GetUnscaledSize(drawingArea
.GetSize()));
446 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
447 wxRichTextDrawingContext
context(& GetBuffer());
448 if (GetBuffer().IsDirty())
450 dc
.SetUserScale(GetScale(), GetScale());
452 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
453 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
455 dc
.SetUserScale(1.0, 1.0);
460 // Paint the background
463 wxRect
clipRect(availableSpace
);
464 clipRect
.x
+= GetBuffer().GetLeftMargin();
465 clipRect
.y
+= GetBuffer().GetTopMargin();
466 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
467 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
469 clipRect
= GetScaledRect(clipRect
);
470 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
472 dc
.SetClippingRegion(clipRect
);
475 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
476 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
478 dc
.SetUserScale(GetScale(), GetScale());
480 GetBuffer().Draw(dc
, context
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
482 dc
.DestroyClippingRegion();
484 // Other user defined painting after everything else (i.e. all text) is painted
485 PaintAboveContent(dc
);
487 #if wxRICHTEXT_USE_OWN_CARET
488 if (GetCaret()->IsVisible())
491 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
495 dc
.SetUserScale(1.0, 1.0);
498 #if !wxRICHTEXT_USE_OWN_CARET
504 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(true);
508 // Empty implementation, to prevent flicker
509 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
513 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
517 #if !wxRICHTEXT_USE_OWN_CARET
523 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
524 // Work around dropouts when control is focused
532 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
537 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
538 // Work around dropouts when control is focused
546 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
551 // Set up the caret for the given position and container, after a mouse click
552 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
554 bool caretAtLineStart
= false;
556 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
558 // If we're at the start of a line (but not first in para)
559 // then we should keep the caret showing at the start of the line
560 // by showing the m_caretAtLineStart flag.
561 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
562 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
564 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
565 caretAtLineStart
= true;
569 if (extendSelection
&& (m_caretPosition
!= position
))
570 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
572 MoveCaret(position
, caretAtLineStart
);
573 SetDefaultStyleToCursorStyle();
579 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
585 dc
.SetFont(GetFont());
587 // TODO: detect change of focus object
589 wxRichTextObject
* hitObj
= NULL
;
590 wxRichTextObject
* contextObj
= NULL
;
591 wxRichTextDrawingContext
context(& GetBuffer());
592 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
594 #if wxUSE_DRAG_AND_DROP
595 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
596 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position
))
598 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
600 m_dragStartPoint
= event
.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
603 m_dragStartTime
= wxDateTime::UNow();
604 #endif // wxUSE_DATETIME
606 // Preserve behaviour of clicking on an object within the selection
607 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
610 return; // Don't skip the event, else the selection will be lost
612 #endif // wxUSE_DRAG_AND_DROP
614 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
616 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
617 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
618 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
620 SetFocusObject(container
, false /* don't set caret position yet */);
626 long oldCaretPos
= m_caretPosition
;
628 SetCaretPositionAfterClick(container
, position
, hit
);
630 // For now, don't handle shift-click when we're selecting multiple objects.
631 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
632 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
641 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
646 if (GetCapture() == this)
649 // See if we clicked on a URL
652 dc
.SetFont(GetFont());
655 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
656 wxRichTextObject
* hitObj
= NULL
;
657 wxRichTextObject
* contextObj
= NULL
;
658 wxRichTextDrawingContext
context(& GetBuffer());
659 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
660 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
662 #if wxUSE_DRAG_AND_DROP
665 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
666 m_preDrag
= false; // Tell DnD not to happen now: we are processing Left Up ourselves.
668 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
670 wxRichTextObject
* hitObj
= NULL
;
671 wxRichTextObject
* contextObj
= NULL
;
672 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
673 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
674 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
675 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
677 SetFocusObject(container
, false /* don't set caret position yet */);
680 long oldCaretPos
= m_caretPosition
;
682 SetCaretPositionAfterClick(container
, position
, hit
);
684 // For now, don't handle shift-click when we're selecting multiple objects.
685 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
686 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
692 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
694 wxRichTextEvent
cmdEvent(
695 wxEVT_RICHTEXT_LEFT_CLICK
,
697 cmdEvent
.SetEventObject(this);
698 cmdEvent
.SetPosition(position
);
700 cmdEvent
.SetContainer(hitObj
->GetContainer());
702 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
705 if (GetStyle(position
, attr
))
707 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
709 wxString urlTarget
= attr
.GetURL();
710 if (!urlTarget
.IsEmpty())
712 wxMouseEvent
mouseEvent(event
);
714 long startPos
= 0, endPos
= 0;
715 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
718 startPos
= obj
->GetRange().GetStart();
719 endPos
= obj
->GetRange().GetEnd();
722 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
723 InitCommandEvent(urlEvent
);
725 urlEvent
.SetString(urlTarget
);
727 GetEventHandler()->ProcessEvent(urlEvent
);
735 #if wxUSE_DRAG_AND_DROP
737 #endif // wxUSE_DRAG_AND_DROP
739 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
740 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
742 // Put the selection in PRIMARY, if it exists
743 wxTheClipboard
->UsePrimarySelection(true);
745 wxRichTextRange range
= GetInternalSelectionRange();
746 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
748 wxTheClipboard
->UsePrimarySelection(false);
754 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
756 if (!event
.Dragging() && m_dragging
)
758 // We may have accidentally lost a mouse-up event, especially on Linux
760 if (GetCapture() == this)
764 #if wxUSE_DRAG_AND_DROP
766 if (m_preDrag
|| m_dragging
)
768 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
769 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
770 distance
= abs(x
) + abs(y
);
773 // See if we're starting Drag'n'Drop
777 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
781 && (diff
.GetMilliseconds() > 100)
788 wxRichTextRange range
= GetInternalSelectionRange();
789 if (range
== wxRICHTEXT_NONE
)
791 // Don't try to drag an empty range
796 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
797 long oldPos
= GetCaretPosition();
798 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
800 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
801 wxString text
= GetFocusObject()->GetTextForRange(range
);
803 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
805 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
807 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
808 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
809 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
811 wxRichTextDropSource
source(*compositeObject
, this);
812 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
813 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
814 BeginBatchUndo(_("Drag"));
815 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
818 case wxDragCopy
: break;
821 wxLogError(wxT("An error occurred during drag and drop operation"));
824 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
825 SetCaretPosition(oldPos
);
826 SetFocusObject(oldFocus
, false);
835 #endif // wxUSE_DRAG_AND_DROP
839 dc
.SetFont(GetFont());
842 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
843 wxRichTextObject
* hitObj
= NULL
;
844 wxRichTextObject
* contextObj
= NULL
;
848 // If we're dragging, let's only consider positions at this level; otherwise
849 // selecting a range is not going to work.
850 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
853 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
854 container
= GetFocusObject();
856 wxRichTextDrawingContext
context(& GetBuffer());
857 int hit
= container
->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, flags
);
859 // See if we need to change the cursor
862 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
864 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
866 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
869 SetCursor(m_textCursor
);
872 if (!event
.Dragging())
879 #if wxUSE_DRAG_AND_DROP
885 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
886 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
887 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
889 // Check for dragging across multiple containers
891 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
892 int hit2
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position2
, & hitObj2
, & contextObj2
, 0);
893 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
895 // See if we can find a common ancestor
896 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
898 firstContainer
= GetFocusObject();
899 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
903 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
904 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
905 // is the common ancestor.
906 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
909 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
911 wxRichTextObject
* p
= hitObj2
;
914 if (p
->GetParent() == commonAncestor
)
916 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
923 if (commonAncestor
&& firstContainer
&& otherContainer
)
925 // We have now got a second container that shares a parent with the current or anchor object.
926 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
928 // Don't go into common-ancestor selection mode if we still have the same
930 if (otherContainer
!= firstContainer
)
932 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
933 m_selectionAnchorObject
= firstContainer
;
934 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
936 // The common ancestor, such as a table, returns the cell selection
937 // between the anchor and current position.
938 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
943 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
948 if (otherContainer
->AcceptsFocus())
949 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
950 MoveCaret(-1, false);
951 SetDefaultStyleToCursorStyle();
956 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
957 #if wxUSE_DRAG_AND_DROP
963 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
968 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
974 dc
.SetFont(GetFont());
977 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
978 wxRichTextObject
* hitObj
= NULL
;
979 wxRichTextObject
* contextObj
= NULL
;
980 wxRichTextDrawingContext
context(& GetBuffer());
981 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
983 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
985 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
986 if (actualContainer
&& actualContainer
->AcceptsFocus())
988 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
989 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
993 wxRichTextEvent
cmdEvent(
994 wxEVT_RICHTEXT_RIGHT_CLICK
,
996 cmdEvent
.SetEventObject(this);
997 cmdEvent
.SetPosition(position
);
999 cmdEvent
.SetContainer(hitObj
->GetContainer());
1001 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1005 /// Left-double-click
1006 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
1008 wxRichTextEvent
cmdEvent(
1009 wxEVT_RICHTEXT_LEFT_DCLICK
,
1011 cmdEvent
.SetEventObject(this);
1012 cmdEvent
.SetPosition(m_caretPosition
+1);
1013 cmdEvent
.SetContainer(GetFocusObject());
1015 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1017 SelectWord(GetCaretPosition()+1);
1022 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
1024 wxRichTextEvent
cmdEvent(
1025 wxEVT_RICHTEXT_MIDDLE_CLICK
,
1027 cmdEvent
.SetEventObject(this);
1028 cmdEvent
.SetPosition(m_caretPosition
+1);
1029 cmdEvent
.SetContainer(GetFocusObject());
1031 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1034 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1035 // Paste any PRIMARY selection, if it exists
1036 wxTheClipboard
->UsePrimarySelection(true);
1038 wxTheClipboard
->UsePrimarySelection(false);
1043 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1046 if (event
.CmdDown())
1047 flags
|= wxRICHTEXT_CTRL_DOWN
;
1048 if (event
.ShiftDown())
1049 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1050 if (event
.AltDown())
1051 flags
|= wxRICHTEXT_ALT_DOWN
;
1053 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1055 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1057 KeyboardNavigate(event
.GetKeyCode(), flags
);
1061 long keycode
= event
.GetKeyCode();
1121 case WXK_NUMPAD_HOME
:
1122 case WXK_NUMPAD_LEFT
:
1124 case WXK_NUMPAD_RIGHT
:
1125 case WXK_NUMPAD_DOWN
:
1126 case WXK_NUMPAD_PAGEUP
:
1127 case WXK_NUMPAD_PAGEDOWN
:
1128 case WXK_NUMPAD_END
:
1129 case WXK_NUMPAD_BEGIN
:
1130 case WXK_NUMPAD_INSERT
:
1131 case WXK_WINDOWS_LEFT
:
1140 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1141 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1143 if (!ProcessBackKey(event
, flags
))
1152 // all the other keys modify the controls contents which shouldn't be
1153 // possible if we're read-only
1154 if ( !IsEditable() )
1160 if (event
.GetKeyCode() == WXK_RETURN
)
1162 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1165 long newPos
= m_caretPosition
;
1167 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1172 BeginBatchUndo(_("Insert Text"));
1174 DeleteSelectedContent(& newPos
);
1176 if (event
.ShiftDown())
1179 text
= wxRichTextLineBreakChar
;
1180 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1181 m_caretAtLineStart
= true;
1185 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1187 // Automatically renumber list
1188 bool isNumberedList
= false;
1189 wxRichTextRange numberedListRange
= FindRangeForList(newPos
+1, isNumberedList
);
1190 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1192 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1196 SetDefaultStyleToCursorStyle();
1198 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1200 wxRichTextEvent
cmdEvent(
1201 wxEVT_RICHTEXT_RETURN
,
1203 cmdEvent
.SetEventObject(this);
1204 cmdEvent
.SetFlags(flags
);
1205 cmdEvent
.SetPosition(newPos
+1);
1206 cmdEvent
.SetContainer(GetFocusObject());
1208 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1210 // Generate conventional event
1211 wxCommandEvent
textEvent(wxEVT_TEXT_ENTER
, GetId());
1212 InitCommandEvent(textEvent
);
1214 GetEventHandler()->ProcessEvent(textEvent
);
1218 else if (event
.GetKeyCode() == WXK_BACK
)
1220 ProcessBackKey(event
, flags
);
1222 else if (event
.GetKeyCode() == WXK_DELETE
)
1224 long newPos
= m_caretPosition
;
1226 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1231 BeginBatchUndo(_("Delete Text"));
1233 bool processed
= DeleteSelectedContent(& newPos
);
1239 // Submit range in character positions, which are greater than caret positions,
1240 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1242 if (event
.CmdDown())
1244 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1245 if (pos
!= -1 && (pos
> newPos
))
1247 wxRichTextRange
range(newPos
+1, pos
);
1248 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1250 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1257 if (!processed
&& newPos
< (GetLastPosition()-1))
1259 wxRichTextRange
range(newPos
+1, newPos
+1);
1260 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1262 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1270 if (GetLastPosition() == -1)
1272 GetFocusObject()->Reset();
1274 m_caretPosition
= -1;
1276 SetDefaultStyleToCursorStyle();
1279 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1281 // Always send this event; wxEVT_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1283 wxRichTextEvent
cmdEvent(
1284 wxEVT_RICHTEXT_DELETE
,
1286 cmdEvent
.SetEventObject(this);
1287 cmdEvent
.SetFlags(flags
);
1288 cmdEvent
.SetPosition(m_caretPosition
+1);
1289 cmdEvent
.SetContainer(GetFocusObject());
1290 GetEventHandler()->ProcessEvent(cmdEvent
);
1297 long keycode
= event
.GetKeyCode();
1309 if (event
.CmdDown())
1311 // Fixes AltGr+key with European input languages on Windows
1312 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1319 wxRichTextEvent
cmdEvent(
1320 wxEVT_RICHTEXT_CHARACTER
,
1322 cmdEvent
.SetEventObject(this);
1323 cmdEvent
.SetFlags(flags
);
1325 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1327 cmdEvent
.SetCharacter((wxChar
) keycode
);
1329 cmdEvent
.SetPosition(m_caretPosition
+1);
1330 cmdEvent
.SetContainer(GetFocusObject());
1332 if (keycode
== wxT('\t'))
1334 // See if we need to promote or demote the selection or paragraph at the cursor
1335 // position, instead of inserting a tab.
1336 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1337 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1338 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1340 wxRichTextRange range
;
1342 range
= GetSelectionRange();
1344 range
= para
->GetRange().FromInternal();
1346 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1348 PromoteList(promoteBy
, range
, NULL
);
1350 GetEventHandler()->ProcessEvent(cmdEvent
);
1356 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1359 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1362 BeginBatchUndo(_("Insert Text"));
1364 long newPos
= m_caretPosition
;
1365 DeleteSelectedContent(& newPos
);
1368 wxString str
= event
.GetUnicodeKey();
1370 wxString str
= (wxChar
) event
.GetKeyCode();
1372 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1376 SetDefaultStyleToCursorStyle();
1377 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1379 cmdEvent
.SetPosition(m_caretPosition
);
1380 GetEventHandler()->ProcessEvent(cmdEvent
);
1388 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1390 wxRichTextAttr attr
;
1391 if (container
&& GetStyle(position
, attr
, container
))
1393 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1395 SetCursor(m_urlCursor
);
1397 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1399 SetCursor(m_textCursor
);
1407 // Processes the back key
1408 bool wxRichTextCtrl::ProcessBackKey(wxKeyEvent
& event
, int flags
)
1415 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1420 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1422 // If we're at the start of a list item with a bullet, let's 'delete' the bullet, i.e.
1423 // make it a continuation paragraph.
1424 if (!HasSelection() && para
&& ((m_caretPosition
+1) == para
->GetRange().GetStart()) &&
1425 para
->GetAttributes().HasBulletStyle() && (para
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
) == 0)
1427 wxRichTextParagraph
* newPara
= wxDynamicCast(para
->Clone(), wxRichTextParagraph
);
1428 newPara
->GetAttributes().SetBulletStyle(newPara
->GetAttributes().GetBulletStyle() | wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
);
1430 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Remove Bullet"), wxRICHTEXT_CHANGE_STYLE
, & GetBuffer(), GetFocusObject(), this);
1431 action
->SetRange(newPara
->GetRange());
1432 action
->SetPosition(GetCaretPosition());
1433 action
->GetNewParagraphs().AppendChild(newPara
);
1434 // Also store the old ones for Undo
1435 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1437 GetBuffer().Invalidate(para
->GetRange());
1438 GetBuffer().SubmitAction(action
);
1440 // Automatically renumber list
1441 bool isNumberedList
= false;
1442 wxRichTextRange numberedListRange
= FindRangeForList(m_caretPosition
, isNumberedList
);
1443 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1445 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1452 BeginBatchUndo(_("Delete Text"));
1454 long newPos
= m_caretPosition
;
1456 bool processed
= DeleteSelectedContent(& newPos
);
1462 // Submit range in character positions, which are greater than caret positions,
1463 // so subtract 1 for deleted character and add 1 for conversion to character position.
1466 if (event
.CmdDown())
1468 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1471 wxRichTextRange
range(pos
+1, newPos
);
1472 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1474 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1483 wxRichTextRange
range(newPos
, newPos
);
1484 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1486 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1494 if (GetLastPosition() == -1)
1496 GetFocusObject()->Reset();
1498 m_caretPosition
= -1;
1500 SetDefaultStyleToCursorStyle();
1503 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1505 // Always send this event; wxEVT_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1507 wxRichTextEvent
cmdEvent(
1508 wxEVT_RICHTEXT_DELETE
,
1510 cmdEvent
.SetEventObject(this);
1511 cmdEvent
.SetFlags(flags
);
1512 cmdEvent
.SetPosition(m_caretPosition
+1);
1513 cmdEvent
.SetContainer(GetFocusObject());
1514 GetEventHandler()->ProcessEvent(cmdEvent
);
1523 /// Delete content if there is a selection, e.g. when pressing a key.
1524 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1528 long pos
= m_selection
.GetRange().GetStart();
1529 wxRichTextRange range
= m_selection
.GetRange();
1531 // SelectAll causes more to be selected than doing it interactively,
1532 // and causes a new paragraph to be inserted. So for multiline buffers,
1533 // don't delete the final position.
1534 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1535 range
.SetEnd(range
.GetEnd()-1);
1537 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1538 m_selection
.Reset();
1539 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1549 /// Keyboard navigation
1553 Left: left one character
1554 Right: right one character
1557 Ctrl-Left: left one word
1558 Ctrl-Right: right one word
1559 Ctrl-Up: previous paragraph start
1560 Ctrl-Down: next start of paragraph
1563 Ctrl-Home: start of document
1564 Ctrl-End: end of document
1565 Page-Up: Up a screen
1566 Page-Down: Down a screen
1570 Ctrl-Alt-PgUp: Start of window
1571 Ctrl-Alt-PgDn: End of window
1572 F8: Start selection mode
1573 Esc: End selection mode
1575 Adding Shift does the above but starts/extends selection.
1580 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1582 bool success
= false;
1584 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1586 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1587 success
= WordRight(1, flags
);
1589 success
= MoveRight(1, flags
);
1591 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1593 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1594 success
= WordLeft(1, flags
);
1596 success
= MoveLeft(1, flags
);
1598 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1600 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1601 success
= MoveToParagraphStart(flags
);
1603 success
= MoveUp(1, flags
);
1605 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1607 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1608 success
= MoveToParagraphEnd(flags
);
1610 success
= MoveDown(1, flags
);
1612 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1614 success
= PageUp(1, flags
);
1616 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1618 success
= PageDown(1, flags
);
1620 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1622 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1623 success
= MoveHome(flags
);
1625 success
= MoveToLineStart(flags
);
1627 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1629 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1630 success
= MoveEnd(flags
);
1632 success
= MoveToLineEnd(flags
);
1637 ScrollIntoView(m_caretPosition
, keyCode
);
1638 SetDefaultStyleToCursorStyle();
1644 /// Extend the selection. Selections are in caret positions.
1645 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1647 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1649 if (oldPos
== newPos
)
1652 wxRichTextSelection oldSelection
= m_selection
;
1654 m_selection
.SetContainer(GetFocusObject());
1656 wxRichTextRange oldRange
;
1657 if (m_selection
.IsValid())
1658 oldRange
= m_selection
.GetRange();
1660 oldRange
= wxRICHTEXT_NO_SELECTION
;
1661 wxRichTextRange newRange
;
1663 // If not currently selecting, start selecting
1664 if (oldRange
.GetStart() == -2)
1666 m_selectionAnchor
= oldPos
;
1668 if (oldPos
> newPos
)
1669 newRange
.SetRange(newPos
+1, oldPos
);
1671 newRange
.SetRange(oldPos
+1, newPos
);
1675 // Always ensure that the selection range start is greater than
1677 if (newPos
> m_selectionAnchor
)
1678 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1679 else if (newPos
== m_selectionAnchor
)
1680 newRange
= wxRichTextRange(-2, -2);
1682 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1685 m_selection
.SetRange(newRange
);
1687 RefreshForSelectionChange(oldSelection
, m_selection
);
1689 if (newRange
.GetStart() > newRange
.GetEnd())
1691 wxLogDebug(wxT("Strange selection range"));
1700 /// Scroll into view, returning true if we scrolled.
1701 /// This takes a _caret_ position.
1702 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1704 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1710 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1712 int startXUnits
, startYUnits
;
1713 GetViewStart(& startXUnits
, & startYUnits
);
1714 int startY
= startYUnits
* ppuY
;
1717 GetVirtualSize(& sx
, & sy
);
1723 wxRect rect
= GetScaledRect(line
->GetRect());
1725 bool scrolled
= false;
1727 wxSize clientSize
= GetClientSize();
1729 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1732 wxClientDC
dc(this);
1733 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1734 topMargin
, bottomMargin
);
1736 clientSize
.y
-= (int) (0.5 + bottomMargin
* GetScale());
1738 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1740 int y
= rect
.y
- GetClientSize().y
/2;
1741 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1742 if (y
>= 0 && (y
+ clientSize
.y
) < (int) (0.5 + GetBuffer().GetCachedSize().y
* GetScale()))
1744 if (startYUnits
!= yUnits
)
1746 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1749 #if !wxRICHTEXT_USE_OWN_CARET
1759 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1760 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1761 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1762 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1764 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1766 // Make it scroll so this item is at the bottom
1768 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1769 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1771 // If we're still off the screen, scroll another line down
1772 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1775 if (startYUnits
!= yUnits
)
1777 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1781 else if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale())))
1783 // Make it scroll so this item is at the top
1785 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1786 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1788 if (startYUnits
!= yUnits
)
1790 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1796 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1797 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1798 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1799 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1801 if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale())))
1803 // Make it scroll so this item is at the top
1805 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1806 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1808 if (startYUnits
!= yUnits
)
1810 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1814 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1816 // Make it scroll so this item is at the bottom
1818 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1819 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1821 // If we're still off the screen, scroll another line down
1822 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1825 if (startYUnits
!= yUnits
)
1827 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1833 #if !wxRICHTEXT_USE_OWN_CARET
1841 /// Is the given position visible on the screen?
1842 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1844 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1850 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1853 GetViewStart(& startX
, & startY
);
1855 startY
= startY
* ppuY
;
1857 wxRect rect
= GetScaledRect(line
->GetRect());
1858 wxSize clientSize
= GetClientSize();
1859 clientSize
.y
-= (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale());
1861 return (rect
.GetTop() >= (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()))) &&
1862 (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1865 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1867 m_caretPosition
= position
;
1868 m_caretAtLineStart
= showAtLineStart
;
1871 /// Move caret one visual step forward: this may mean setting a flag
1872 /// and keeping the same position if we're going from the end of one line
1873 /// to the start of the next, which may be the exact same caret position.
1874 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1876 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1878 // Only do the check if we're not at the end of the paragraph (where things work OK
1880 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1882 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1886 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1888 // We're at the end of a line. See whether we need to
1889 // stay at the same actual caret position but change visual
1890 // position, or not.
1891 if (oldPosition
== lineRange
.GetEnd())
1893 if (m_caretAtLineStart
)
1895 // We're already at the start of the line, so actually move on now.
1896 m_caretPosition
= oldPosition
+ 1;
1897 m_caretAtLineStart
= false;
1901 // We're showing at the end of the line, so keep to
1902 // the same position but indicate that we're to show
1903 // at the start of the next line.
1904 m_caretPosition
= oldPosition
;
1905 m_caretAtLineStart
= true;
1907 SetDefaultStyleToCursorStyle();
1913 SetDefaultStyleToCursorStyle();
1916 /// Move caret one visual step backward: this may mean setting a flag
1917 /// and keeping the same position if we're going from the end of one line
1918 /// to the start of the next, which may be the exact same caret position.
1919 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1921 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1923 // Only do the check if we're not at the start of the paragraph (where things work OK
1925 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1927 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1931 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1933 // We're at the start of a line. See whether we need to
1934 // stay at the same actual caret position but change visual
1935 // position, or not.
1936 if (oldPosition
== lineRange
.GetStart())
1938 m_caretPosition
= oldPosition
-1;
1939 m_caretAtLineStart
= true;
1942 else if (oldPosition
== lineRange
.GetEnd())
1944 if (m_caretAtLineStart
)
1946 // We're at the start of the line, so keep the same caret position
1947 // but clear the start-of-line flag.
1948 m_caretPosition
= oldPosition
;
1949 m_caretAtLineStart
= false;
1953 // We're showing at the end of the line, so go back
1954 // to the previous character position.
1955 m_caretPosition
= oldPosition
- 1;
1957 SetDefaultStyleToCursorStyle();
1963 SetDefaultStyleToCursorStyle();
1967 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1969 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1971 if (m_caretPosition
+ noPositions
< endPos
)
1973 long oldPos
= m_caretPosition
;
1974 long newPos
= m_caretPosition
+ noPositions
;
1976 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1980 // Determine by looking at oldPos and m_caretPosition whether
1981 // we moved from the end of a line to the start of the next line, in which case
1982 // we want to adjust the caret position such that it is positioned at the
1983 // start of the next line, rather than jumping past the first character of the
1985 if (noPositions
== 1)
1986 MoveCaretForward(oldPos
);
1988 SetCaretPosition(newPos
);
1991 SetDefaultStyleToCursorStyle();
2000 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
2004 if (m_caretPosition
> startPos
- noPositions
+ 1)
2006 long oldPos
= m_caretPosition
;
2007 long newPos
= m_caretPosition
- noPositions
;
2008 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2012 if (noPositions
== 1)
2013 MoveCaretBack(oldPos
);
2015 SetCaretPosition(newPos
);
2018 SetDefaultStyleToCursorStyle();
2026 // Find the caret position for the combination of hit-test flags and character position.
2027 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2028 // since this is ambiguous (same position used for end of line and start of next).
2029 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2030 bool& caretLineStart
)
2032 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2033 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2034 // so we view the caret at the start of the line.
2035 caretLineStart
= false;
2036 long caretPosition
= position
;
2038 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2040 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2041 wxRichTextRange lineRange
;
2043 lineRange
= thisLine
->GetAbsoluteRange();
2045 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2048 caretLineStart
= true;
2052 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2053 if (para
&& para
->GetRange().GetStart() == position
)
2057 return caretPosition
;
2061 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2063 return MoveDown(- noLines
, flags
);
2067 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2072 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2073 wxPoint pt
= GetCaret()->GetPosition();
2074 long newLine
= lineNumber
+ noLines
;
2075 bool notInThisObject
= false;
2077 if (lineNumber
!= -1)
2081 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2082 if (newLine
> lastLine
)
2083 notInThisObject
= true;
2088 notInThisObject
= true;
2092 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2093 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
;
2095 bool lineIsEmpty
= false;
2096 if (notInThisObject
)
2098 // If we know we're navigating out of the current object,
2099 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2100 container
= & GetBuffer();
2101 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2103 if (noLines
> 0) // going down
2105 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2109 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2114 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2117 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2118 if (lineObj
->GetRange().GetStart() == lineObj
->GetRange().GetEnd())
2126 wxClientDC
dc(this);
2128 dc
.SetFont(GetFont());
2130 wxRichTextObject
* hitObj
= NULL
;
2131 wxRichTextObject
* contextObj
= NULL
;
2132 wxRichTextDrawingContext
context(& GetBuffer());
2133 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2136 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2137 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2140 if (notInThisObject
)
2142 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2143 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2145 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2147 container
= actualContainer
;
2151 bool caretLineStart
= true;
2153 // If the line is empty, there is only one possible position for the caret,
2154 // so force the 'before' state so FindCaretPositionForCharacterPosition doesn't
2155 // just return the same position.
2158 hitTest
&= ~wxRICHTEXT_HITTEST_AFTER
;
2159 hitTest
|= wxRICHTEXT_HITTEST_BEFORE
;
2161 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2162 long newSelEnd
= caretPosition
;
2165 if (notInThisObject
)
2168 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2173 SetCaretPosition(caretPosition
, caretLineStart
);
2175 SetDefaultStyleToCursorStyle();
2183 /// Move to the end of the paragraph
2184 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2186 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2189 long newPos
= para
->GetRange().GetEnd() - 1;
2190 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2194 SetCaretPosition(newPos
);
2196 SetDefaultStyleToCursorStyle();
2204 /// Move to the start of the paragraph
2205 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2207 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2210 long newPos
= para
->GetRange().GetStart() - 1;
2211 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2215 SetCaretPosition(newPos
, true);
2217 SetDefaultStyleToCursorStyle();
2225 /// Move to the end of the line
2226 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2228 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2232 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2233 long newPos
= lineRange
.GetEnd();
2234 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2238 SetCaretPosition(newPos
);
2240 SetDefaultStyleToCursorStyle();
2248 /// Move to the start of the line
2249 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2251 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2254 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2255 long newPos
= lineRange
.GetStart()-1;
2257 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2261 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2263 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2265 SetDefaultStyleToCursorStyle();
2273 /// Move to the start of the buffer
2274 bool wxRichTextCtrl::MoveHome(int flags
)
2276 if (m_caretPosition
!= -1)
2278 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2282 SetCaretPosition(-1);
2284 SetDefaultStyleToCursorStyle();
2292 /// Move to the end of the buffer
2293 bool wxRichTextCtrl::MoveEnd(int flags
)
2295 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2297 if (m_caretPosition
!= endPos
)
2299 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2303 SetCaretPosition(endPos
);
2305 SetDefaultStyleToCursorStyle();
2313 /// Move noPages pages up
2314 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2316 return PageDown(- noPages
, flags
);
2319 /// Move noPages pages down
2320 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2322 // Calculate which line occurs noPages * screen height further down.
2323 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2326 wxSize clientSize
= GetClientSize();
2327 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2329 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2332 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2333 long pos
= lineRange
.GetStart()-1;
2334 if (pos
!= m_caretPosition
)
2336 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2338 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2342 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2344 SetDefaultStyleToCursorStyle();
2354 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2356 return str
== wxT(" ") || str
== wxT("\t") || (!str
.empty() && (str
[0] == (wxChar
) 160));
2359 // Finds the caret position for the next word
2360 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2362 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2366 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2368 // First skip current text to space
2369 while (i
< endPos
&& i
> -1)
2371 // i is in character, not caret positions
2372 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2373 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2374 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2378 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2385 while (i
< endPos
&& i
> -1)
2387 // i is in character, not caret positions
2388 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2389 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2390 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2391 return wxMax(-1, i
);
2393 if (text
.empty()) // End of paragraph, or maybe an image
2394 return wxMax(-1, i
- 1);
2395 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2399 // Convert to caret position
2400 return wxMax(-1, i
- 1);
2409 long i
= m_caretPosition
;
2411 // First skip white space
2412 while (i
< endPos
&& i
> -1)
2414 // i is in character, not caret positions
2415 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2416 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2418 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2420 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2425 // Next skip current text to space
2426 while (i
< endPos
&& i
> -1)
2428 // i is in character, not caret positions
2429 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2430 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2431 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2434 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2447 /// Move n words left
2448 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2450 long pos
= FindNextWordPosition(-1);
2451 if (pos
!= m_caretPosition
)
2453 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2455 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2459 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2461 SetDefaultStyleToCursorStyle();
2469 /// Move n words right
2470 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2472 long pos
= FindNextWordPosition(1);
2473 if (pos
!= m_caretPosition
)
2475 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2477 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2481 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2483 SetDefaultStyleToCursorStyle();
2492 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2494 // Only do sizing optimization for large buffers
2495 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2497 m_fullLayoutRequired
= true;
2498 m_fullLayoutTime
= wxGetLocalTimeMillis();
2499 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2500 LayoutContent(true /* onlyVisibleRect */);
2503 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2505 #if wxRICHTEXT_BUFFERED_PAINTING
2512 // Force any pending layout due to large buffer
2513 void wxRichTextCtrl::ForceDelayedLayout()
2515 if (m_fullLayoutRequired
)
2517 m_fullLayoutRequired
= false;
2518 m_fullLayoutTime
= 0;
2519 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2520 ShowPosition(m_fullLayoutSavedPosition
);
2526 /// Idle-time processing
2527 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2529 #if wxRICHTEXT_USE_OWN_CARET
2530 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2532 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2538 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2540 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2542 m_fullLayoutRequired
= false;
2543 m_fullLayoutTime
= 0;
2544 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2545 ShowPosition(m_fullLayoutSavedPosition
);
2549 if (m_caretPositionForDefaultStyle
!= -2)
2551 // If the caret position has changed, no longer reflect the default style
2553 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2554 m_caretPositionForDefaultStyle
= -2;
2561 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2563 #if wxRICHTEXT_USE_OWN_CARET
2564 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2567 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2574 /// Set up scrollbars, e.g. after a resize
2575 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2580 if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled
)
2582 SetScrollbars(0, 0, 0, 0, 0, 0);
2586 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2587 // of pixels. See e.g. wxVScrolledWindow for ideas.
2588 int pixelsPerUnit
= 5;
2589 wxSize clientSize
= GetClientSize();
2591 int maxHeight
= (int) (0.5 + GetScale() * (GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin()));
2593 // Round up so we have at least maxHeight pixels
2594 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2596 int startX
= 0, startY
= 0;
2598 GetViewStart(& startX
, & startY
);
2600 int maxPositionX
= 0;
2601 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2603 int newStartX
= wxMin(maxPositionX
, startX
);
2604 int newStartY
= wxMin(maxPositionY
, startY
);
2606 int oldPPUX
, oldPPUY
;
2607 int oldStartX
, oldStartY
;
2608 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2609 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2610 GetViewStart(& oldStartX
, & oldStartY
);
2611 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2613 oldVirtualSizeY
/= oldPPUY
;
2615 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2618 // Don't set scrollbars if there were none before, and there will be none now.
2619 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2622 // Move to previous scroll position if
2624 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2627 /// Paint the background
2628 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2630 wxColour backgroundColour
= GetBackgroundColour();
2631 if (!backgroundColour
.IsOk())
2632 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2634 // Clear the background
2635 dc
.SetBrush(wxBrush(backgroundColour
));
2636 dc
.SetPen(*wxTRANSPARENT_PEN
);
2637 wxRect
windowRect(GetClientSize());
2638 windowRect
.x
-= 2; windowRect
.y
-= 2;
2639 windowRect
.width
+= 4; windowRect
.height
+= 4;
2641 // We need to shift the rectangle to take into account
2642 // scrolling. Converting device to logical coordinates.
2643 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2644 dc
.DrawRectangle(windowRect
);
2647 #if wxRICHTEXT_BUFFERED_PAINTING
2648 /// Recreate buffer bitmap if necessary
2649 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2652 if (sz
== wxDefaultSize
)
2653 sz
= GetClientSize();
2655 if (sz
.x
< 1 || sz
.y
< 1)
2658 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2659 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2660 return m_bufferBitmap
.IsOk();
2664 // ----------------------------------------------------------------------------
2665 // file IO functions
2666 // ----------------------------------------------------------------------------
2667 #if wxUSE_FFILE && wxUSE_STREAMS
2668 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2670 SetFocusObject(& GetBuffer(), true);
2672 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2674 m_filename
= filename
;
2677 SetInsertionPoint(0);
2680 SetupScrollbars(true);
2682 wxTextCtrl::SendTextUpdatedEvent(this);
2688 wxLogError(_("File couldn't be loaded."));
2694 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2696 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2698 m_filename
= filename
;
2705 wxLogError(_("The text couldn't be saved."));
2709 #endif // wxUSE_FFILE && wxUSE_STREAMS
2711 // ----------------------------------------------------------------------------
2712 // wxRichTextCtrl specific functionality
2713 // ----------------------------------------------------------------------------
2715 /// Add a new paragraph of text to the end of the buffer
2716 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2718 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2719 GetBuffer().Invalidate();
2725 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2727 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2728 GetBuffer().Invalidate();
2733 // ----------------------------------------------------------------------------
2734 // selection and ranges
2735 // ----------------------------------------------------------------------------
2738 void wxRichTextCtrl::SelectNone()
2740 if (m_selection
.IsValid())
2742 wxRichTextSelection oldSelection
= m_selection
;
2744 m_selection
.Reset();
2746 RefreshForSelectionChange(oldSelection
, m_selection
);
2748 m_selectionAnchor
= -2;
2749 m_selectionAnchorObject
= NULL
;
2750 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2753 static bool wxIsWordDelimiter(const wxString
& text
)
2755 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2758 /// Select the word at the given character position
2759 bool wxRichTextCtrl::SelectWord(long position
)
2761 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2764 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2768 if (position
== para
->GetRange().GetEnd())
2771 long positionStart
= position
;
2772 long positionEnd
= position
;
2774 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2776 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2777 if (wxIsWordDelimiter(text
))
2783 if (positionStart
< para
->GetRange().GetStart())
2784 positionStart
= para
->GetRange().GetStart();
2786 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2788 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2789 if (wxIsWordDelimiter(text
))
2795 if (positionEnd
>= para
->GetRange().GetEnd())
2796 positionEnd
= para
->GetRange().GetEnd();
2798 if (positionEnd
< positionStart
)
2801 SetSelection(positionStart
, positionEnd
+1);
2803 if (positionStart
>= 0)
2805 MoveCaret(positionStart
-1, true);
2806 SetDefaultStyleToCursorStyle();
2812 wxString
wxRichTextCtrl::GetStringSelection() const
2815 GetSelection(&from
, &to
);
2817 return GetRange(from
, to
);
2820 // ----------------------------------------------------------------------------
2822 // ----------------------------------------------------------------------------
2824 wxTextCtrlHitTestResult
2825 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2827 // implement in terms of the other overload as the native ports typically
2828 // can get the position and not (x, y) pair directly (although wxUniv
2829 // directly gets x and y -- and so overrides this method as well)
2831 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2833 if ( rc
!= wxTE_HT_UNKNOWN
)
2835 PositionToXY(pos
, x
, y
);
2841 wxTextCtrlHitTestResult
2842 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2845 wxClientDC
dc((wxRichTextCtrl
*) this);
2846 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2848 // Buffer uses logical position (relative to start of buffer)
2850 wxPoint pt2
= GetLogicalPoint(pt
);
2852 wxRichTextObject
* hitObj
= NULL
;
2853 wxRichTextObject
* contextObj
= NULL
;
2854 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2855 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2857 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2858 return wxTE_HT_BEFORE
;
2859 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2860 return wxTE_HT_BEYOND
;
2861 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2862 return wxTE_HT_ON_TEXT
;
2864 return wxTE_HT_UNKNOWN
;
2867 wxRichTextParagraphLayoutBox
*
2868 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2870 wxClientDC
dc(this);
2872 dc
.SetFont(GetFont());
2874 wxPoint logicalPt
= GetLogicalPoint(pt
);
2876 wxRichTextObject
* contextObj
= NULL
;
2877 wxRichTextDrawingContext
context(& GetBuffer());
2878 hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, &hitObj
, &contextObj
, flags
);
2879 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2885 // ----------------------------------------------------------------------------
2886 // set/get the controls text
2887 // ----------------------------------------------------------------------------
2889 wxString
wxRichTextCtrl::DoGetValue() const
2891 return GetBuffer().GetText();
2894 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2896 // Public API for range is different from internals
2897 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2900 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2902 // Don't call Clear here, since it always sends a text updated event
2903 m_buffer
.ResetAndClearCommands();
2904 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2905 m_caretPosition
= -1;
2906 m_caretPositionForDefaultStyle
= -2;
2907 m_caretAtLineStart
= false;
2908 m_selection
.Reset();
2909 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2919 if (!value
.IsEmpty())
2921 // Remove empty paragraph
2922 GetBuffer().Clear();
2923 DoWriteText(value
, flags
);
2925 // for compatibility, don't move the cursor when doing SetValue()
2926 SetInsertionPoint(0);
2930 // still send an event for consistency
2931 if (flags
& SetValue_SendEvent
)
2932 wxTextCtrl::SendTextUpdatedEvent(this);
2937 void wxRichTextCtrl::WriteText(const wxString
& value
)
2942 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2944 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2946 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2947 wxRichTextDrawingContext
context(& GetBuffer());
2948 GetBuffer().Defragment(context
);
2950 if ( flags
& SetValue_SendEvent
)
2951 wxTextCtrl::SendTextUpdatedEvent(this);
2954 void wxRichTextCtrl::AppendText(const wxString
& text
)
2956 SetInsertionPointEnd();
2961 /// Write an image at the current insertion point
2962 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2964 wxRichTextImageBlock imageBlock
;
2966 wxImage image2
= image
;
2967 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2968 return WriteImage(imageBlock
, textAttr
);
2973 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2975 wxRichTextImageBlock imageBlock
;
2978 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2979 return WriteImage(imageBlock
, textAttr
);
2984 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2986 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2989 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2993 wxRichTextImageBlock imageBlock
;
2995 wxImage image
= bitmap
.ConvertToImage();
2996 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2997 return WriteImage(imageBlock
, textAttr
);
3003 // Write a text box at the current insertion point.
3004 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
3006 wxRichTextBox
* textBox
= new wxRichTextBox
;
3007 textBox
->SetAttributes(textAttr
);
3008 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3009 textBox
->AddParagraph(wxEmptyString
);
3010 textBox
->SetParent(NULL
);
3012 // The object returned is the one actually inserted into the buffer,
3013 // while the original one is deleted.
3014 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3015 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
3019 wxRichTextField
* wxRichTextCtrl::WriteField(const wxString
& fieldType
, const wxRichTextProperties
& properties
,
3020 const wxRichTextAttr
& textAttr
)
3022 return GetFocusObject()->InsertFieldWithUndo(& GetBuffer(), m_caretPosition
+1, fieldType
, properties
,
3023 this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
, textAttr
);
3026 // Write a table at the current insertion point, returning the table.
3027 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3029 wxASSERT(rows
> 0 && cols
> 0);
3031 if (rows
<= 0 || cols
<= 0)
3034 wxRichTextTable
* table
= new wxRichTextTable
;
3035 table
->SetAttributes(tableAttr
);
3036 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3038 table
->CreateTable(rows
, cols
);
3040 table
->SetParent(NULL
);
3043 for (j
= 0; j
< rows
; j
++)
3045 for (i
= 0; i
< cols
; i
++)
3047 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
3051 // The object returned is the one actually inserted into the buffer,
3052 // while the original one is deleted.
3053 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3054 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3059 /// Insert a newline (actually paragraph) at the current insertion point.
3060 bool wxRichTextCtrl::Newline()
3062 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3065 /// Insert a line break at the current insertion point.
3066 bool wxRichTextCtrl::LineBreak()
3069 text
= wxRichTextLineBreakChar
;
3070 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3073 // ----------------------------------------------------------------------------
3074 // Clipboard operations
3075 // ----------------------------------------------------------------------------
3077 void wxRichTextCtrl::Copy()
3081 wxRichTextRange range
= GetInternalSelectionRange();
3082 GetBuffer().CopyToClipboard(range
);
3086 void wxRichTextCtrl::Cut()
3090 wxRichTextRange range
= GetInternalSelectionRange();
3091 GetBuffer().CopyToClipboard(range
);
3093 DeleteSelectedContent();
3099 void wxRichTextCtrl::Paste()
3103 BeginBatchUndo(_("Paste"));
3105 long newPos
= m_caretPosition
;
3106 DeleteSelectedContent(& newPos
);
3108 GetBuffer().PasteFromClipboard(newPos
);
3114 void wxRichTextCtrl::DeleteSelection()
3116 if (CanDeleteSelection())
3118 DeleteSelectedContent();
3122 bool wxRichTextCtrl::HasSelection() const
3124 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3127 bool wxRichTextCtrl::HasUnfocusedSelection() const
3129 return m_selection
.IsValid();
3132 bool wxRichTextCtrl::CanCopy() const
3134 // Can copy if there's a selection
3135 return HasSelection();
3138 bool wxRichTextCtrl::CanCut() const
3140 return CanDeleteSelection();
3143 bool wxRichTextCtrl::CanPaste() const
3145 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3148 return GetBuffer().CanPasteFromClipboard();
3151 bool wxRichTextCtrl::CanDeleteSelection() const
3153 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3157 // ----------------------------------------------------------------------------
3159 // ----------------------------------------------------------------------------
3161 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3163 if (m_contextMenu
&& m_contextMenu
!= menu
)
3164 delete m_contextMenu
;
3165 m_contextMenu
= menu
;
3168 void wxRichTextCtrl::SetEditable(bool editable
)
3170 m_editable
= editable
;
3173 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3177 m_caretPosition
= pos
- 1;
3178 m_caretAtLineStart
= true;
3182 SetDefaultStyleToCursorStyle();
3185 void wxRichTextCtrl::SetInsertionPointEnd()
3187 long pos
= GetLastPosition();
3188 SetInsertionPoint(pos
);
3191 long wxRichTextCtrl::GetInsertionPoint() const
3193 return m_caretPosition
+1;
3196 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3198 return GetFocusObject()->GetOwnRange().GetEnd();
3201 // If the return values from and to are the same, there is no
3203 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3205 if (m_selection
.IsValid())
3207 *from
= m_selection
.GetRange().GetStart();
3208 *to
= m_selection
.GetRange().GetEnd();
3218 bool wxRichTextCtrl::IsEditable() const
3223 // ----------------------------------------------------------------------------
3225 // ----------------------------------------------------------------------------
3227 void wxRichTextCtrl::SetSelection(long from
, long to
)
3229 // if from and to are both -1, it means (in wxWidgets) that all text should
3231 if ( (from
== -1) && (to
== -1) )
3234 to
= GetLastPosition()+1;
3243 wxRichTextSelection oldSelection
= m_selection
;
3245 m_selectionAnchor
= from
-1;
3246 m_selectionAnchorObject
= NULL
;
3247 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3249 m_caretPosition
= wxMax(-1, to
-1);
3251 RefreshForSelectionChange(oldSelection
, m_selection
);
3256 // ----------------------------------------------------------------------------
3258 // ----------------------------------------------------------------------------
3260 void wxRichTextCtrl::Replace(long from
, long to
,
3261 const wxString
& value
)
3263 BeginBatchUndo(_("Replace"));
3265 SetSelection(from
, to
);
3267 wxRichTextAttr
attr(GetDefaultStyle());
3269 DeleteSelectedContent();
3271 SetDefaultStyle(attr
);
3273 if (!value
.IsEmpty())
3274 DoWriteText(value
, SetValue_SelectionOnly
);
3279 void wxRichTextCtrl::Remove(long from
, long to
)
3283 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3290 bool wxRichTextCtrl::IsModified() const
3292 return m_buffer
.IsModified();
3295 void wxRichTextCtrl::MarkDirty()
3297 m_buffer
.Modify(true);
3300 void wxRichTextCtrl::DiscardEdits()
3302 m_caretPositionForDefaultStyle
= -2;
3303 m_buffer
.Modify(false);
3304 m_buffer
.GetCommandProcessor()->ClearCommands();
3307 int wxRichTextCtrl::GetNumberOfLines() const
3309 return GetFocusObject()->GetParagraphCount();
3312 // ----------------------------------------------------------------------------
3313 // Positions <-> coords
3314 // ----------------------------------------------------------------------------
3316 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3318 return GetFocusObject()->XYToPosition(x
, y
);
3321 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3323 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3326 // ----------------------------------------------------------------------------
3328 // ----------------------------------------------------------------------------
3330 void wxRichTextCtrl::ShowPosition(long pos
)
3332 if (!IsPositionVisible(pos
))
3333 ScrollIntoView(pos
-1, WXK_DOWN
);
3336 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3338 return GetFocusObject()->GetParagraphLength(lineNo
);
3341 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3343 return GetFocusObject()->GetParagraphText(lineNo
);
3346 // ----------------------------------------------------------------------------
3348 // ----------------------------------------------------------------------------
3350 void wxRichTextCtrl::Undo()
3354 GetCommandProcessor()->Undo();
3358 void wxRichTextCtrl::Redo()
3362 GetCommandProcessor()->Redo();
3366 bool wxRichTextCtrl::CanUndo() const
3368 return GetCommandProcessor()->CanUndo() && IsEditable();
3371 bool wxRichTextCtrl::CanRedo() const
3373 return GetCommandProcessor()->CanRedo() && IsEditable();
3376 // ----------------------------------------------------------------------------
3377 // implementation details
3378 // ----------------------------------------------------------------------------
3380 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3382 SetValue(event
.GetString());
3383 GetEventHandler()->ProcessEvent(event
);
3386 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3388 // By default, load the first file into the text window.
3389 if (event
.GetNumberOfFiles() > 0)
3391 LoadFile(event
.GetFiles()[0]);
3395 wxSize
wxRichTextCtrl::DoGetBestSize() const
3397 return wxSize(10, 10);
3400 // ----------------------------------------------------------------------------
3401 // standard handlers for standard edit menu events
3402 // ----------------------------------------------------------------------------
3404 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3409 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3414 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3419 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3424 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3429 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3434 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3436 event
.Enable( CanCut() );
3439 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3441 event
.Enable( CanCopy() );
3444 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3446 event
.Enable( CanDeleteSelection() );
3449 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3451 event
.Enable( CanPaste() );
3454 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3456 event
.Enable( CanUndo() );
3457 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3460 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3462 event
.Enable( CanRedo() );
3463 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3466 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3468 if (GetLastPosition() > 0)
3472 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3474 event
.Enable(GetLastPosition() > 0);
3477 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3479 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3480 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3482 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3483 if (obj
&& CanEditProperties(obj
))
3484 EditProperties(obj
, this);
3486 m_contextMenuPropertiesInfo
.Clear();
3490 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3492 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3493 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3496 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3498 if (event
.GetEventObject() != this)
3504 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3507 // Prepares the context menu, adding appropriate property-editing commands.
3508 // Returns the number of property commands added.
3509 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3511 wxClientDC
dc(this);
3513 dc
.SetFont(GetFont());
3515 m_contextMenuPropertiesInfo
.Clear();
3518 wxRichTextObject
* hitObj
= NULL
;
3519 wxRichTextObject
* contextObj
= NULL
;
3520 if (pt
!= wxDefaultPosition
)
3522 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3523 wxRichTextDrawingContext
context(& GetBuffer());
3524 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
);
3526 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3528 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3529 if (hitObj
&& actualContainer
)
3531 if (actualContainer
->AcceptsFocus())
3533 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3534 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3537 if (addPropertyCommands
)
3538 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3542 if (addPropertyCommands
)
3543 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3548 if (addPropertyCommands
)
3549 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3554 // Invoked from the keyboard, so don't set the caret position and don't use the event
3556 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3558 contextObj
= hitObj
->GetParentContainer();
3560 contextObj
= GetFocusObject();
3562 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3563 if (hitObj
&& actualContainer
)
3565 if (addPropertyCommands
)
3566 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3570 if (addPropertyCommands
)
3571 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3577 if (addPropertyCommands
)
3578 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3579 return m_contextMenuPropertiesInfo
.GetCount();
3585 // Shows the context menu, adding appropriate property-editing commands
3586 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3590 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3598 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3600 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3603 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3605 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3608 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3610 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3613 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3615 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3618 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
, int flags
)
3620 GetFocusObject()->SetStyle(obj
, textAttr
, flags
);
3623 // extended style setting operation with flags including:
3624 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3625 // see richtextbuffer.h for more details.
3627 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3629 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3632 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3634 return GetBuffer().SetDefaultStyle(style
);
3637 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3639 wxRichTextAttr
attr1(style
);
3640 attr1
.GetTextBoxAttr().Reset();
3641 return GetBuffer().SetDefaultStyle(attr1
);
3644 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3646 return GetBuffer().GetDefaultStyle();
3649 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3651 wxRichTextAttr attr
;
3652 if (GetFocusObject()->GetStyle(position
, attr
))
3661 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3663 return GetFocusObject()->GetStyle(position
, style
);
3666 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3668 wxRichTextAttr attr
;
3669 if (container
->GetStyle(position
, attr
))
3678 // get the common set of styles for the range
3679 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3681 wxRichTextAttr attr
;
3682 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3691 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3693 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3696 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3698 return container
->GetStyleForRange(range
.ToInternal(), style
);
3701 /// Get the content (uncombined) attributes for this position.
3702 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3704 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3707 /// Get the content (uncombined) attributes for this position.
3708 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3710 return container
->GetUncombinedStyle(position
, style
);
3713 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3715 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3718 /// Set font, and also the buffer attributes
3719 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3721 wxControl::SetFont(font
);
3723 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3725 GetBuffer().SetBasicStyle(attr
);
3727 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3733 /// Transform logical to physical
3734 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3737 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3742 /// Transform physical to logical
3743 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3746 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3751 /// Position the caret
3752 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3757 //wxLogDebug(wxT("PositionCaret"));
3760 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3762 #if !wxRICHTEXT_USE_OWN_CARET
3763 caretRect
= GetScaledRect(caretRect
);
3765 int topMargin
= (int) (0.5 + GetScale()*GetBuffer().GetTopMargin());
3766 int bottomMargin
= (int) (0.5 + GetScale()*GetBuffer().GetBottomMargin());
3767 wxPoint newPt
= caretRect
.GetPosition();
3768 wxSize newSz
= caretRect
.GetSize();
3769 wxPoint pt
= GetPhysicalPoint(newPt
);
3770 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3773 if (GetCaret()->GetSize() != newSz
)
3774 GetCaret()->SetSize(newSz
);
3776 // Adjust size so the caret size and position doesn't appear in the margins
3777 if (((pt
.y
+ newSz
.y
) <= topMargin
) || (pt
.y
>= (GetClientSize().y
- bottomMargin
)))
3782 else if (pt
.y
< topMargin
&& (pt
.y
+ newSz
.y
) > topMargin
)
3784 newSz
.y
-= (topMargin
- pt
.y
);
3788 GetCaret()->SetSize(newSz
);
3791 else if (pt
.y
< (GetClientSize().y
- bottomMargin
) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- bottomMargin
))
3793 newSz
.y
= GetClientSize().y
- bottomMargin
- pt
.y
;
3794 GetCaret()->SetSize(newSz
);
3797 GetCaret()->Move(pt
);
3803 /// Get the caret height and position for the given character position
3804 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3806 wxClientDC
dc(this);
3808 dc
.SetUserScale(GetScale(), GetScale());
3809 dc
.SetFont(GetFont());
3815 container
= GetFocusObject();
3817 wxRichTextDrawingContext
context(& GetBuffer());
3818 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3820 // Caret height can't be zero
3822 height
= dc
.GetCharHeight();
3824 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3831 /// Gets the line for the visible caret position. If the caret is
3832 /// shown at the very end of the line, it means the next character is actually
3833 /// on the following line. So let's get the line we're expecting to find
3834 /// if this is the case.
3835 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3837 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3838 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3841 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3842 if (caretPosition
== lineRange
.GetStart()-1 &&
3843 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3845 if (!m_caretAtLineStart
)
3846 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3853 /// Move the caret to the given character position
3854 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3856 if (GetBuffer().IsDirty())
3860 container
= GetFocusObject();
3862 if (pos
<= container
->GetOwnRange().GetEnd())
3864 SetCaretPosition(pos
, showAtLineStart
);
3866 PositionCaret(container
);
3874 /// Layout the buffer: which we must do before certain operations, such as
3875 /// setting the caret position.
3876 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3878 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3880 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
3881 if (availableSpace
.width
== 0)
3882 availableSpace
.width
= 10;
3883 if (availableSpace
.height
== 0)
3884 availableSpace
.height
= 10;
3886 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3887 if (onlyVisibleRect
)
3889 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3890 availableSpace
.SetPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))));
3893 wxClientDC
dc(this);
3896 dc
.SetFont(GetFont());
3897 dc
.SetUserScale(GetScale(), GetScale());
3899 wxRichTextDrawingContext
context(& GetBuffer());
3900 GetBuffer().Defragment(context
);
3901 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3902 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3903 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3905 dc
.SetUserScale(1.0, 1.0);
3907 if (!IsFrozen() && !onlyVisibleRect
)
3914 /// Is all of the selection, or the current caret position, bold?
3915 bool wxRichTextCtrl::IsSelectionBold()
3919 wxRichTextAttr attr
;
3920 wxRichTextRange range
= GetSelectionRange();
3921 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3922 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3924 return HasCharacterAttributes(range
, attr
);
3928 // If no selection, then we need to combine current style with default style
3929 // to see what the effect would be if we started typing.
3930 wxRichTextAttr attr
;
3931 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3933 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3934 if (GetStyle(pos
, attr
))
3936 if (IsDefaultStyleShowing())
3937 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3938 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3944 /// Is all of the selection, or the current caret position, italics?
3945 bool wxRichTextCtrl::IsSelectionItalics()
3949 wxRichTextRange range
= GetSelectionRange();
3950 wxRichTextAttr attr
;
3951 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3952 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3954 return HasCharacterAttributes(range
, attr
);
3958 // If no selection, then we need to combine current style with default style
3959 // to see what the effect would be if we started typing.
3960 wxRichTextAttr attr
;
3961 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3963 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3964 if (GetStyle(pos
, attr
))
3966 if (IsDefaultStyleShowing())
3967 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3968 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3974 /// Is all of the selection, or the current caret position, underlined?
3975 bool wxRichTextCtrl::IsSelectionUnderlined()
3979 wxRichTextRange range
= GetSelectionRange();
3980 wxRichTextAttr attr
;
3981 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3982 attr
.SetFontUnderlined(true);
3984 return HasCharacterAttributes(range
, attr
);
3988 // If no selection, then we need to combine current style with default style
3989 // to see what the effect would be if we started typing.
3990 wxRichTextAttr attr
;
3991 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3992 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3994 if (GetStyle(pos
, attr
))
3996 if (IsDefaultStyleShowing())
3997 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3998 return attr
.GetFontUnderlined();
4004 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
4005 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
4007 wxRichTextAttr attr
;
4008 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4009 attr
.SetTextEffectFlags(flag
);
4010 attr
.SetTextEffects(flag
);
4014 return HasCharacterAttributes(GetSelectionRange(), attr
);
4018 // If no selection, then we need to combine current style with default style
4019 // to see what the effect would be if we started typing.
4020 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4021 if (GetStyle(pos
, attr
))
4023 if (IsDefaultStyleShowing())
4024 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
4025 return (attr
.GetTextEffectFlags() & flag
) != 0;
4031 /// Apply bold to the selection
4032 bool wxRichTextCtrl::ApplyBoldToSelection()
4034 wxRichTextAttr attr
;
4035 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
4036 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4039 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4042 wxRichTextAttr current
= GetDefaultStyleEx();
4043 current
.Apply(attr
);
4044 SetAndShowDefaultStyle(current
);
4049 /// Apply italic to the selection
4050 bool wxRichTextCtrl::ApplyItalicToSelection()
4052 wxRichTextAttr attr
;
4053 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4054 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4057 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4060 wxRichTextAttr current
= GetDefaultStyleEx();
4061 current
.Apply(attr
);
4062 SetAndShowDefaultStyle(current
);
4067 /// Apply underline to the selection
4068 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4070 wxRichTextAttr attr
;
4071 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4072 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4075 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4078 wxRichTextAttr current
= GetDefaultStyleEx();
4079 current
.Apply(attr
);
4080 SetAndShowDefaultStyle(current
);
4085 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4086 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4088 wxRichTextAttr attr
;
4089 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4090 attr
.SetTextEffectFlags(flags
);
4091 if (!DoesSelectionHaveTextEffectFlag(flags
))
4092 attr
.SetTextEffects(flags
);
4094 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4097 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4100 wxRichTextAttr current
= GetDefaultStyleEx();
4101 current
.Apply(attr
);
4102 SetAndShowDefaultStyle(current
);
4107 /// Is all of the selection aligned according to the specified flag?
4108 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4110 wxRichTextRange range
;
4112 range
= GetSelectionRange();
4114 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4116 wxRichTextAttr attr
;
4117 attr
.SetAlignment(alignment
);
4119 return HasParagraphAttributes(range
, attr
);
4122 /// Apply alignment to the selection
4123 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4125 wxRichTextAttr attr
;
4126 attr
.SetAlignment(alignment
);
4128 return SetStyle(GetSelectionRange(), attr
);
4131 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4133 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4138 /// Apply a named style to the selection
4139 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4141 // Flags are defined within each definition, so only certain
4142 // attributes are applied.
4143 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4145 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4147 if (wxDynamicCast(def
, wxRichTextListStyleDefinition
))
4149 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4151 wxRichTextRange range
;
4154 range
= GetSelectionRange();
4157 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4158 range
= wxRichTextRange(pos
, pos
+1);
4161 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4164 bool isPara
= false;
4166 // Make sure the attr has the style name
4167 if (wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
))
4170 attr
.SetParagraphStyleName(def
->GetName());
4172 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4173 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4174 // to change its style independently.
4175 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4177 else if (wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
))
4178 attr
.SetCharacterStyleName(def
->GetName());
4179 else if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4180 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4182 if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4184 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4186 SetStyle(GetFocusObject(), attr
);
4192 else if (HasSelection())
4193 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4196 wxRichTextAttr current
= GetDefaultStyleEx();
4197 wxRichTextAttr
defaultStyle(attr
);
4200 // Don't apply extra character styles since they are already implied
4201 // in the paragraph style
4202 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4204 current
.Apply(defaultStyle
);
4205 SetAndShowDefaultStyle(current
);
4207 // If it's a paragraph style, we want to apply the style to the
4208 // current paragraph even if we didn't select any text.
4211 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4212 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4215 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4222 /// Apply the style sheet to the buffer, for example if the styles have changed.
4223 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4226 styleSheet
= GetBuffer().GetStyleSheet();
4230 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4232 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4240 /// Sets the default style to the style under the cursor
4241 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4243 wxRichTextAttr attr
;
4244 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4246 // If at the start of a paragraph, use the next position.
4247 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4249 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4250 if (obj
&& obj
->IsTopLevel())
4252 // Don't use the attributes of a top-level object, since they might apply
4253 // to content of the object, e.g. background colour.
4254 SetDefaultStyle(wxRichTextAttr());
4257 else if (GetUncombinedStyle(pos
, attr
))
4259 SetDefaultStyle(attr
);
4266 /// Returns the first visible position in the current view
4267 long wxRichTextCtrl::GetFirstVisiblePosition() const
4269 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))).y
);
4271 return line
->GetAbsoluteRange().GetStart();
4276 /// Get the first visible point in the window
4277 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4280 int startXUnits
, startYUnits
;
4282 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4283 GetViewStart(& startXUnits
, & startYUnits
);
4285 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4288 /// The adjusted caret position is the character position adjusted to take
4289 /// into account whether we're at the start of a paragraph, in which case
4290 /// style information should be taken from the next position, not current one.
4291 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4293 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4295 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4300 /// Get/set the selection range in character positions. -1, -1 means no selection.
4301 /// The range is in API convention, i.e. a single character selection is denoted
4303 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4305 wxRichTextRange range
= GetInternalSelectionRange();
4306 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4307 range
.SetEnd(range
.GetEnd() + 1);
4311 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4313 SetSelection(range
.GetStart(), range
.GetEnd());
4317 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4319 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4322 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4324 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4327 /// Clear list for given range
4328 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4330 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4333 /// Number/renumber any list elements in the given range
4334 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4336 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4339 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4341 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4344 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4345 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4347 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4350 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4352 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4355 // Given a character position at which there is a list style, find the range
4356 // encompassing the same list style by looking backwards and forwards.
4357 wxRichTextRange
wxRichTextCtrl::FindRangeForList(long pos
, bool& isNumberedList
)
4359 wxRichTextParagraphLayoutBox
* focusObject
= GetFocusObject();
4360 wxRichTextRange range
= wxRichTextRange(-1, -1);
4361 wxRichTextParagraph
* para
= focusObject
->GetParagraphAtPosition(pos
);
4362 if (!para
|| !para
->GetAttributes().HasListStyleName())
4366 wxString listStyle
= para
->GetAttributes().GetListStyleName();
4367 range
= para
->GetRange();
4369 isNumberedList
= para
->GetAttributes().HasBulletNumber();
4372 wxRichTextObjectList::compatibility_iterator initialNode
= focusObject
->GetChildren().Find(para
);
4375 wxRichTextObjectList::compatibility_iterator startNode
= initialNode
->GetPrevious();
4378 wxRichTextParagraph
* p
= wxDynamicCast(startNode
->GetData(), wxRichTextParagraph
);
4381 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4384 range
.SetStart(p
->GetRange().GetStart());
4387 startNode
= startNode
->GetPrevious();
4391 wxRichTextObjectList::compatibility_iterator endNode
= initialNode
->GetNext();
4394 wxRichTextParagraph
* p
= wxDynamicCast(endNode
->GetData(), wxRichTextParagraph
);
4397 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4400 range
.SetEnd(p
->GetRange().GetEnd());
4403 endNode
= endNode
->GetNext();
4410 /// Deletes the content in the given range
4411 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4413 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4416 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4418 if (sm_availableFontNames
.GetCount() == 0)
4420 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4421 sm_availableFontNames
.Sort();
4423 return sm_availableFontNames
;
4426 void wxRichTextCtrl::ClearAvailableFontNames()
4428 sm_availableFontNames
.Clear();
4431 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4433 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4435 wxTextAttrEx basicStyle
= GetBasicStyle();
4436 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4437 SetBasicStyle(basicStyle
);
4438 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4443 // Refresh the area affected by a selection change
4444 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4446 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4447 // the selection contains whole containers rather than just text, so refresh everything
4448 // for now as it would be hard to compute the rectangle bounding all selections.
4449 // TODO: improve on this.
4450 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4451 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4457 wxRichTextRange oldRange
, newRange
;
4458 if (oldSelection
.IsValid())
4459 oldRange
= oldSelection
.GetRange();
4461 oldRange
= wxRICHTEXT_NO_SELECTION
;
4462 if (newSelection
.IsValid())
4463 newRange
= newSelection
.GetRange();
4465 newRange
= wxRICHTEXT_NO_SELECTION
;
4467 // Calculate the refresh rectangle - just the affected lines
4468 long firstPos
, lastPos
;
4469 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4471 firstPos
= newRange
.GetStart();
4472 lastPos
= newRange
.GetEnd();
4474 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4476 firstPos
= oldRange
.GetStart();
4477 lastPos
= oldRange
.GetEnd();
4479 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4485 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4486 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4489 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4490 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4492 if (firstLine
&& lastLine
)
4494 wxSize clientSize
= GetClientSize();
4495 wxPoint pt1
= GetPhysicalPoint(GetScaledPoint(firstLine
->GetAbsolutePosition()));
4496 wxPoint pt2
= GetPhysicalPoint(GetScaledPoint(lastLine
->GetAbsolutePosition())) + wxPoint(0, (int) (0.5 + lastLine
->GetSize().y
* GetScale()));
4499 pt1
.y
= wxMax(0, pt1
.y
);
4501 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4503 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4504 RefreshRect(rect
, false);
4512 // margins functions
4513 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4515 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4516 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4517 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4518 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4523 wxPoint
wxRichTextCtrl::DoGetMargins() const
4525 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4526 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4529 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4531 if (obj
&& !obj
->AcceptsFocus())
4534 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4535 bool changingContainer
= (m_focusObject
!= obj
);
4537 if (changingContainer
&& HasSelection())
4540 m_focusObject
= obj
;
4543 m_focusObject
= & m_buffer
;
4545 if (setCaretPosition
&& changingContainer
)
4547 m_selection
.Reset();
4548 m_selectionAnchor
= -2;
4549 m_selectionAnchorObject
= NULL
;
4550 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4554 m_caretAtLineStart
= false;
4555 MoveCaret(pos
, m_caretAtLineStart
);
4556 SetDefaultStyleToCursorStyle();
4558 wxRichTextEvent
cmdEvent(
4559 wxEVT_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4561 cmdEvent
.SetEventObject(this);
4562 cmdEvent
.SetPosition(m_caretPosition
+1);
4563 cmdEvent
.SetOldContainer(oldContainer
);
4564 cmdEvent
.SetContainer(m_focusObject
);
4566 GetEventHandler()->ProcessEvent(cmdEvent
);
4571 #if wxUSE_DRAG_AND_DROP
4572 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4576 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4581 if (!GetSelection().IsValid())
4586 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4587 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4589 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4592 long position
= GetCaretPosition();
4593 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4594 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4596 // It doesn't make sense to move onto itself
4600 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4601 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4602 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4603 if ((def
== wxDragMove
) && !DeleteAfter
)
4605 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4606 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4609 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4610 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4612 delete richTextBuffer
;
4616 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4617 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4624 #endif // wxUSE_DRAG_AND_DROP
4627 #if wxUSE_DRAG_AND_DROP
4628 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4630 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4634 wxRichTextObject
* hitObj
= NULL
;
4635 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->GetUnscaledPoint(m_rtc
->ScreenToClient(wxGetMousePosition())), position
, hit
, hitObj
);
4637 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4639 m_rtc
->StoreFocusObject(container
);
4640 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4643 return false; // so that the base-class sets a cursor
4645 #endif // wxUSE_DRAG_AND_DROP
4647 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4652 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4657 void wxRichTextCtrl::EnableVerticalScrollbar(bool enable
)
4659 m_verticalScrollbarEnabled
= enable
;
4663 void wxRichTextCtrl::SetFontScale(double fontScale
, bool refresh
)
4665 GetBuffer().SetFontScale(fontScale
);
4668 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4673 void wxRichTextCtrl::SetDimensionScale(double dimScale
, bool refresh
)
4675 GetBuffer().SetDimensionScale(dimScale
);
4678 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4683 // Sets an overall scale factor for displaying and editing the content.
4684 void wxRichTextCtrl::SetScale(double scale
, bool refresh
)
4689 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4694 // Get an unscaled point
4695 wxPoint
wxRichTextCtrl::GetUnscaledPoint(const wxPoint
& pt
) const
4697 if (GetScale() == 1.0)
4700 return wxPoint((int) (0.5 + double(pt
.x
) / GetScale()), (int) (0.5 + double(pt
.y
) / GetScale()));
4703 // Get a scaled point
4704 wxPoint
wxRichTextCtrl::GetScaledPoint(const wxPoint
& pt
) const
4706 if (GetScale() == 1.0)
4709 return wxPoint((int) (0.5 + double(pt
.x
) * GetScale()), (int) (0.5 + double(pt
.y
) * GetScale()));
4712 // Get an unscaled size
4713 wxSize
wxRichTextCtrl::GetUnscaledSize(const wxSize
& sz
) const
4715 if (GetScale() == 1.0)
4718 return wxSize((int) (0.5 + double(sz
.x
) / GetScale()), (int) (0.5 + double(sz
.y
) / GetScale()));
4721 // Get a scaled size
4722 wxSize
wxRichTextCtrl::GetScaledSize(const wxSize
& sz
) const
4724 if (GetScale() == 1.0)
4727 return wxSize((int) (0.5 + double(sz
.x
) * GetScale()), (int) (0.5 + double(sz
.y
) * GetScale()));
4730 // Get an unscaled rect
4731 wxRect
wxRichTextCtrl::GetUnscaledRect(const wxRect
& rect
) const
4733 if (GetScale() == 1.0)
4736 return wxRect((int) (0.5 + double(rect
.x
) / GetScale()), (int) (0.5 + double(rect
.y
) / GetScale()),
4737 (int) (0.5 + double(rect
.width
) / GetScale()), (int) (0.5 + double(rect
.height
) / GetScale()));
4740 // Get a scaled rect
4741 wxRect
wxRichTextCtrl::GetScaledRect(const wxRect
& rect
) const
4743 if (GetScale() == 1.0)
4746 return wxRect((int) (0.5 + double(rect
.x
) * GetScale()), (int) (0.5 + double(rect
.y
) * GetScale()),
4747 (int) (0.5 + double(rect
.width
) * GetScale()), (int) (0.5 + double(rect
.height
) * GetScale()));
4750 #if wxRICHTEXT_USE_OWN_CARET
4752 // ----------------------------------------------------------------------------
4753 // initialization and destruction
4754 // ----------------------------------------------------------------------------
4756 void wxRichTextCaret::Init()
4759 m_refreshEnabled
= true;
4763 m_richTextCtrl
= NULL
;
4764 m_needsUpdate
= false;
4768 wxRichTextCaret::~wxRichTextCaret()
4770 if (m_timer
.IsRunning())
4774 // ----------------------------------------------------------------------------
4775 // showing/hiding/moving the caret (base class interface)
4776 // ----------------------------------------------------------------------------
4778 void wxRichTextCaret::DoShow()
4782 if (!m_timer
.IsRunning() && GetBlinkTime() > 0)
4783 m_timer
.Start(GetBlinkTime());
4788 void wxRichTextCaret::DoHide()
4790 if (m_timer
.IsRunning())
4796 void wxRichTextCaret::DoMove()
4802 if (m_xOld
!= -1 && m_yOld
!= -1)
4804 if (m_richTextCtrl
&& m_refreshEnabled
)
4806 wxRect
rect(wxPoint(m_xOld
, m_yOld
), GetSize());
4807 wxRect scaledRect
= m_richTextCtrl
->GetScaledRect(rect
);
4808 m_richTextCtrl
->RefreshRect(scaledRect
, false);
4817 void wxRichTextCaret::DoSize()
4819 int countVisible
= m_countVisible
;
4820 if (countVisible
> 0)
4826 if (countVisible
> 0)
4828 m_countVisible
= countVisible
;
4833 // ----------------------------------------------------------------------------
4834 // handling the focus
4835 // ----------------------------------------------------------------------------
4837 void wxRichTextCaret::OnSetFocus()
4845 void wxRichTextCaret::OnKillFocus()
4850 // ----------------------------------------------------------------------------
4851 // drawing the caret
4852 // ----------------------------------------------------------------------------
4854 void wxRichTextCaret::Refresh()
4856 if (m_richTextCtrl
&& m_refreshEnabled
)
4858 wxRect
rect(GetPosition(), GetSize());
4859 wxRect rectScaled
= m_richTextCtrl
->GetScaledRect(rect
);
4860 m_richTextCtrl
->RefreshRect(rectScaled
, false);
4864 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4866 wxBrush
brush(m_caretBrush
);
4867 wxPen
pen(m_caretPen
);
4868 if (m_richTextCtrl
&& m_richTextCtrl
->GetBasicStyle().HasTextColour())
4870 brush
= wxBrush(m_richTextCtrl
->GetBasicStyle().GetTextColour());
4871 pen
= wxPen(m_richTextCtrl
->GetBasicStyle().GetTextColour());
4873 dc
->SetBrush((m_hasFocus
? brush
: *wxTRANSPARENT_BRUSH
));
4876 wxPoint
pt(m_x
, m_y
);
4880 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4882 if (IsVisible() && m_flashOn
)
4883 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4886 void wxRichTextCaret::Notify()
4888 m_flashOn
= !m_flashOn
;
4892 void wxRichTextCaretTimer::Notify()
4897 // wxRICHTEXT_USE_OWN_CARET
4900 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4904 m_labels
.Add(label
);
4912 // Returns number of menu items were added.
4913 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4915 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4916 // If none of the standard properties identifiers are in the menu, add them if necessary.
4917 // If no items to add, just set the text to something generic
4918 if (GetCount() == 0)
4922 menu
->SetLabel(startCmd
, _("&Properties"));
4924 // Delete the others if necessary
4926 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4928 if (menu
->FindItem(i
))
4939 // Find the position of the first properties item
4940 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4942 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4943 if (item
&& item
->GetId() == startCmd
)
4952 int insertBefore
= pos
+1;
4953 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4955 if (menu
->FindItem(i
))
4957 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4961 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4962 menu
->Append(i
, m_labels
[i
- startCmd
]);
4964 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4969 // Delete any old items still left on the menu
4970 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4972 if (menu
->FindItem(i
))
4980 // No existing property identifiers were found, so append to the end of the menu.
4981 menu
->AppendSeparator();
4982 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4984 menu
->Append(i
, m_labels
[i
- startCmd
]);
4992 // Add appropriate menu items for the current container and clicked on object
4993 // (and container's parent, if appropriate).
4994 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4997 if (obj
&& ctrl
->CanEditProperties(obj
))
4998 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
5000 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
5001 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
5003 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
5004 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());