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 read-only, the programmer probably wants to retain dialog keyboard navigation.
245 // If you don't, then pass wxWANTS_CHARS explicitly.
246 if ((style
& wxTE_READONLY
) == 0)
247 style
|= wxWANTS_CHARS
;
249 if (!wxControl::Create(parent
, id
, pos
, size
,
250 style
|wxFULL_REPAINT_ON_RESIZE
,
254 if (!GetFont().IsOk())
256 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
259 // No physical scrolling, so we can preserve margins
260 EnableScrolling(false, false);
262 if (style
& wxTE_READONLY
)
265 // The base attributes must all have default values
266 wxRichTextAttr attributes
;
267 attributes
.SetFont(GetFont());
268 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
269 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
270 attributes
.SetLineSpacing(10);
271 attributes
.SetParagraphSpacingAfter(10);
272 attributes
.SetParagraphSpacingBefore(0);
273 SetBasicStyle(attributes
);
276 SetMargins(margin
, margin
);
278 // The default attributes will be merged with base attributes, so
279 // can be empty to begin with
280 wxRichTextAttr defaultAttributes
;
281 SetDefaultStyle(defaultAttributes
);
283 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
284 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
287 GetBuffer().SetRichTextCtrl(this);
289 #if wxRICHTEXT_USE_OWN_CARET
290 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
292 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
295 // Tell the sizers to use the given or best size
296 SetInitialSize(size
);
298 #if wxRICHTEXT_BUFFERED_PAINTING
300 RecreateBuffer(size
);
303 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
304 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
306 SetCursor(m_textCursor
);
308 if (!value
.IsEmpty())
311 GetBuffer().AddEventHandler(this);
314 wxAcceleratorEntry entries
[6];
316 entries
[0].Set(wxACCEL_CTRL
, (int) 'C', wxID_COPY
);
317 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_CUT
);
318 entries
[2].Set(wxACCEL_CTRL
, (int) 'V', wxID_PASTE
);
319 entries
[3].Set(wxACCEL_CTRL
, (int) 'A', wxID_SELECTALL
);
320 entries
[4].Set(wxACCEL_CTRL
, (int) 'Z', wxID_UNDO
);
321 entries
[5].Set(wxACCEL_CTRL
, (int) 'Y', wxID_REDO
);
323 wxAcceleratorTable
accel(6, entries
);
324 SetAcceleratorTable(accel
);
326 m_contextMenu
= new wxMenu
;
327 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
328 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
329 m_contextMenu
->AppendSeparator();
330 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
331 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
332 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
333 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
334 m_contextMenu
->AppendSeparator();
335 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
336 m_contextMenu
->AppendSeparator();
337 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
339 #if wxUSE_DRAG_AND_DROP
340 SetDropTarget(new wxRichTextDropTarget(this));
346 wxRichTextCtrl::~wxRichTextCtrl()
348 SetFocusObject(& GetBuffer(), false);
349 GetBuffer().RemoveEventHandler(this);
351 delete m_contextMenu
;
354 /// Member initialisation
355 void wxRichTextCtrl::Init()
357 m_contextMenu
= NULL
;
359 m_caretPosition
= -1;
360 m_selectionAnchor
= -2;
361 m_selectionAnchorObject
= NULL
;
362 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
364 m_useVirtualAttributes
= false;
365 m_verticalScrollbarEnabled
= true;
366 m_caretAtLineStart
= false;
368 #if wxUSE_DRAG_AND_DROP
371 m_fullLayoutRequired
= false;
372 m_fullLayoutTime
= 0;
373 m_fullLayoutSavedPosition
= 0;
374 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
375 m_caretPositionForDefaultStyle
= -2;
376 m_focusObject
= & m_buffer
;
380 void wxRichTextCtrl::DoThaw()
382 if (GetBuffer().IsDirty())
391 void wxRichTextCtrl::Clear()
393 if (GetFocusObject() == & GetBuffer())
395 m_buffer
.ResetAndClearCommands();
396 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
400 GetFocusObject()->Reset();
403 m_caretPosition
= -1;
404 m_caretPositionForDefaultStyle
= -2;
405 m_caretAtLineStart
= false;
407 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
417 wxTextCtrl::SendTextUpdatedEvent(this);
421 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
423 #if !wxRICHTEXT_USE_OWN_CARET
424 if (GetCaret() && !IsFrozen())
427 // Stop the caret refreshing the control from within the
430 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(false);
434 #if wxRICHTEXT_BUFFERED_PAINTING
435 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
445 dc
.SetFont(GetFont());
447 wxRect
drawingArea(GetUpdateRegion().GetBox());
448 drawingArea
.SetPosition(GetUnscaledPoint(GetLogicalPoint(drawingArea
.GetPosition())));
449 drawingArea
.SetSize(GetUnscaledSize(drawingArea
.GetSize()));
451 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
452 wxRichTextDrawingContext
context(& GetBuffer());
453 if (GetBuffer().IsDirty())
455 dc
.SetUserScale(GetScale(), GetScale());
457 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
458 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
460 dc
.SetUserScale(1.0, 1.0);
465 // Paint the background
468 wxRect
clipRect(availableSpace
);
469 clipRect
.x
+= GetBuffer().GetLeftMargin();
470 clipRect
.y
+= GetBuffer().GetTopMargin();
471 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
472 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
474 clipRect
= GetScaledRect(clipRect
);
475 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
477 dc
.SetClippingRegion(clipRect
);
480 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
481 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
483 dc
.SetUserScale(GetScale(), GetScale());
485 GetBuffer().Draw(dc
, context
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
487 dc
.DestroyClippingRegion();
489 // Other user defined painting after everything else (i.e. all text) is painted
490 PaintAboveContent(dc
);
492 #if wxRICHTEXT_USE_OWN_CARET
493 if (GetCaret()->IsVisible())
496 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
500 dc
.SetUserScale(1.0, 1.0);
503 #if !wxRICHTEXT_USE_OWN_CARET
509 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(true);
513 // Empty implementation, to prevent flicker
514 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
518 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
522 #if !wxRICHTEXT_USE_OWN_CARET
528 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
529 // Work around dropouts when control is focused
537 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
542 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
543 // Work around dropouts when control is focused
551 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
556 // Set up the caret for the given position and container, after a mouse click
557 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
559 bool caretAtLineStart
= false;
561 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
563 // If we're at the start of a line (but not first in para)
564 // then we should keep the caret showing at the start of the line
565 // by showing the m_caretAtLineStart flag.
566 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
567 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
569 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
570 caretAtLineStart
= true;
574 if (extendSelection
&& (m_caretPosition
!= position
))
575 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
577 MoveCaret(position
, caretAtLineStart
);
578 SetDefaultStyleToCursorStyle();
584 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
590 dc
.SetFont(GetFont());
592 // TODO: detect change of focus object
594 wxRichTextObject
* hitObj
= NULL
;
595 wxRichTextObject
* contextObj
= NULL
;
596 wxRichTextDrawingContext
context(& GetBuffer());
597 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
599 #if wxUSE_DRAG_AND_DROP
600 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
601 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position
))
603 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
605 m_dragStartPoint
= event
.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
608 m_dragStartTime
= wxDateTime::UNow();
609 #endif // wxUSE_DATETIME
611 // Preserve behaviour of clicking on an object within the selection
612 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
615 return; // Don't skip the event, else the selection will be lost
617 #endif // wxUSE_DRAG_AND_DROP
619 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
621 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
622 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
623 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
625 SetFocusObject(container
, false /* don't set caret position yet */);
631 long oldCaretPos
= m_caretPosition
;
633 SetCaretPositionAfterClick(container
, position
, hit
);
635 // For now, don't handle shift-click when we're selecting multiple objects.
636 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
637 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
646 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
651 if (GetCapture() == this)
654 // See if we clicked on a URL
657 dc
.SetFont(GetFont());
660 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
661 wxRichTextObject
* hitObj
= NULL
;
662 wxRichTextObject
* contextObj
= NULL
;
663 wxRichTextDrawingContext
context(& GetBuffer());
664 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
665 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
667 #if wxUSE_DRAG_AND_DROP
670 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
671 m_preDrag
= false; // Tell DnD not to happen now: we are processing Left Up ourselves.
673 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
675 wxRichTextObject
* hitObj
= NULL
;
676 wxRichTextObject
* contextObj
= NULL
;
677 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
678 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
679 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
680 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
682 SetFocusObject(container
, false /* don't set caret position yet */);
685 long oldCaretPos
= m_caretPosition
;
687 SetCaretPositionAfterClick(container
, position
, hit
);
689 // For now, don't handle shift-click when we're selecting multiple objects.
690 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
691 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
697 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
699 wxRichTextEvent
cmdEvent(
700 wxEVT_RICHTEXT_LEFT_CLICK
,
702 cmdEvent
.SetEventObject(this);
703 cmdEvent
.SetPosition(position
);
705 cmdEvent
.SetContainer(hitObj
->GetContainer());
707 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
710 if (GetStyle(position
, attr
))
712 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
714 wxString urlTarget
= attr
.GetURL();
715 if (!urlTarget
.IsEmpty())
717 wxMouseEvent
mouseEvent(event
);
719 long startPos
= 0, endPos
= 0;
720 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
723 startPos
= obj
->GetRange().GetStart();
724 endPos
= obj
->GetRange().GetEnd();
727 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
728 InitCommandEvent(urlEvent
);
730 urlEvent
.SetString(urlTarget
);
732 GetEventHandler()->ProcessEvent(urlEvent
);
740 #if wxUSE_DRAG_AND_DROP
742 #endif // wxUSE_DRAG_AND_DROP
744 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
745 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
747 // Put the selection in PRIMARY, if it exists
748 wxTheClipboard
->UsePrimarySelection(true);
750 wxRichTextRange range
= GetInternalSelectionRange();
751 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
753 wxTheClipboard
->UsePrimarySelection(false);
759 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
761 if (!event
.Dragging() && m_dragging
)
763 // We may have accidentally lost a mouse-up event, especially on Linux
765 if (GetCapture() == this)
769 #if wxUSE_DRAG_AND_DROP
771 if (m_preDrag
|| m_dragging
)
773 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
774 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
775 distance
= abs(x
) + abs(y
);
778 // See if we're starting Drag'n'Drop
782 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
786 && (diff
.GetMilliseconds() > 100)
793 wxRichTextRange range
= GetInternalSelectionRange();
794 if (range
== wxRICHTEXT_NONE
)
796 // Don't try to drag an empty range
801 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
802 long oldPos
= GetCaretPosition();
803 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
805 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
806 wxString text
= GetFocusObject()->GetTextForRange(range
);
808 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
810 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
812 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
813 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
814 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
816 wxRichTextDropSource
source(*compositeObject
, this);
817 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
818 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
819 BeginBatchUndo(_("Drag"));
820 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
823 case wxDragCopy
: break;
826 wxLogError(wxT("An error occurred during drag and drop operation"));
829 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
830 SetCaretPosition(oldPos
);
831 SetFocusObject(oldFocus
, false);
840 #endif // wxUSE_DRAG_AND_DROP
844 dc
.SetFont(GetFont());
847 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
848 wxRichTextObject
* hitObj
= NULL
;
849 wxRichTextObject
* contextObj
= NULL
;
853 // If we're dragging, let's only consider positions at this level; otherwise
854 // selecting a range is not going to work.
855 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
858 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
859 container
= GetFocusObject();
861 wxRichTextDrawingContext
context(& GetBuffer());
862 int hit
= container
->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, flags
);
864 // See if we need to change the cursor
867 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
869 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
871 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
874 SetCursor(m_textCursor
);
877 if (!event
.Dragging())
884 #if wxUSE_DRAG_AND_DROP
890 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
891 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
892 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
894 // Check for dragging across multiple containers
896 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
897 int hit2
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position2
, & hitObj2
, & contextObj2
, 0);
898 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
900 // See if we can find a common ancestor
901 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
903 firstContainer
= GetFocusObject();
904 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
908 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
909 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
910 // is the common ancestor.
911 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
914 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
916 wxRichTextObject
* p
= hitObj2
;
919 if (p
->GetParent() == commonAncestor
)
921 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
928 if (commonAncestor
&& firstContainer
&& otherContainer
)
930 // We have now got a second container that shares a parent with the current or anchor object.
931 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
933 // Don't go into common-ancestor selection mode if we still have the same
935 if (otherContainer
!= firstContainer
)
937 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
938 m_selectionAnchorObject
= firstContainer
;
939 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
941 // The common ancestor, such as a table, returns the cell selection
942 // between the anchor and current position.
943 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
948 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
953 if (otherContainer
->AcceptsFocus())
954 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
955 MoveCaret(-1, false);
956 SetDefaultStyleToCursorStyle();
961 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
962 #if wxUSE_DRAG_AND_DROP
968 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
973 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
979 dc
.SetFont(GetFont());
982 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
983 wxRichTextObject
* hitObj
= NULL
;
984 wxRichTextObject
* contextObj
= NULL
;
985 wxRichTextDrawingContext
context(& GetBuffer());
986 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
988 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
990 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
991 if (actualContainer
&& actualContainer
->AcceptsFocus())
993 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
994 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
998 wxRichTextEvent
cmdEvent(
999 wxEVT_RICHTEXT_RIGHT_CLICK
,
1001 cmdEvent
.SetEventObject(this);
1002 cmdEvent
.SetPosition(position
);
1004 cmdEvent
.SetContainer(hitObj
->GetContainer());
1006 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1010 /// Left-double-click
1011 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
1013 wxRichTextEvent
cmdEvent(
1014 wxEVT_RICHTEXT_LEFT_DCLICK
,
1016 cmdEvent
.SetEventObject(this);
1017 cmdEvent
.SetPosition(m_caretPosition
+1);
1018 cmdEvent
.SetContainer(GetFocusObject());
1020 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1022 SelectWord(GetCaretPosition()+1);
1027 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
1029 wxRichTextEvent
cmdEvent(
1030 wxEVT_RICHTEXT_MIDDLE_CLICK
,
1032 cmdEvent
.SetEventObject(this);
1033 cmdEvent
.SetPosition(m_caretPosition
+1);
1034 cmdEvent
.SetContainer(GetFocusObject());
1036 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1039 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1040 // Paste any PRIMARY selection, if it exists
1041 wxTheClipboard
->UsePrimarySelection(true);
1043 wxTheClipboard
->UsePrimarySelection(false);
1048 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1051 if (event
.CmdDown())
1052 flags
|= wxRICHTEXT_CTRL_DOWN
;
1053 if (event
.ShiftDown())
1054 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1055 if (event
.AltDown())
1056 flags
|= wxRICHTEXT_ALT_DOWN
;
1058 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1060 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1062 KeyboardNavigate(event
.GetKeyCode(), flags
);
1066 long keycode
= event
.GetKeyCode();
1126 case WXK_NUMPAD_HOME
:
1127 case WXK_NUMPAD_LEFT
:
1129 case WXK_NUMPAD_RIGHT
:
1130 case WXK_NUMPAD_DOWN
:
1131 case WXK_NUMPAD_PAGEUP
:
1132 case WXK_NUMPAD_PAGEDOWN
:
1133 case WXK_NUMPAD_END
:
1134 case WXK_NUMPAD_BEGIN
:
1135 case WXK_NUMPAD_INSERT
:
1136 case WXK_WINDOWS_LEFT
:
1145 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1146 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1148 if (!ProcessBackKey(event
, flags
))
1157 // all the other keys modify the controls contents which shouldn't be
1158 // possible if we're read-only
1159 if ( !IsEditable() )
1165 if (event
.GetKeyCode() == WXK_RETURN
)
1167 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1170 long newPos
= m_caretPosition
;
1172 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1177 BeginBatchUndo(_("Insert Text"));
1179 DeleteSelectedContent(& newPos
);
1181 if (event
.ShiftDown())
1184 text
= wxRichTextLineBreakChar
;
1185 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1186 m_caretAtLineStart
= true;
1190 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1192 // Automatically renumber list
1193 bool isNumberedList
= false;
1194 wxRichTextRange numberedListRange
= FindRangeForList(newPos
+1, isNumberedList
);
1195 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1197 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1201 SetDefaultStyleToCursorStyle();
1203 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1205 wxRichTextEvent
cmdEvent(
1206 wxEVT_RICHTEXT_RETURN
,
1208 cmdEvent
.SetEventObject(this);
1209 cmdEvent
.SetFlags(flags
);
1210 cmdEvent
.SetPosition(newPos
+1);
1211 cmdEvent
.SetContainer(GetFocusObject());
1213 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1215 // Generate conventional event
1216 wxCommandEvent
textEvent(wxEVT_TEXT_ENTER
, GetId());
1217 InitCommandEvent(textEvent
);
1219 GetEventHandler()->ProcessEvent(textEvent
);
1223 else if (event
.GetKeyCode() == WXK_BACK
)
1225 ProcessBackKey(event
, flags
);
1227 else if (event
.GetKeyCode() == WXK_DELETE
)
1229 long newPos
= m_caretPosition
;
1231 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1236 BeginBatchUndo(_("Delete Text"));
1238 bool processed
= DeleteSelectedContent(& newPos
);
1244 // Submit range in character positions, which are greater than caret positions,
1245 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1247 if (event
.CmdDown())
1249 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1250 if (pos
!= -1 && (pos
> newPos
))
1252 wxRichTextRange
range(newPos
+1, pos
);
1253 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1255 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1262 if (!processed
&& newPos
< (GetLastPosition()-1))
1264 wxRichTextRange
range(newPos
+1, newPos
+1);
1265 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1267 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1275 if (GetLastPosition() == -1)
1277 GetFocusObject()->Reset();
1279 m_caretPosition
= -1;
1281 SetDefaultStyleToCursorStyle();
1284 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1286 // Always send this event; wxEVT_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1288 wxRichTextEvent
cmdEvent(
1289 wxEVT_RICHTEXT_DELETE
,
1291 cmdEvent
.SetEventObject(this);
1292 cmdEvent
.SetFlags(flags
);
1293 cmdEvent
.SetPosition(m_caretPosition
+1);
1294 cmdEvent
.SetContainer(GetFocusObject());
1295 GetEventHandler()->ProcessEvent(cmdEvent
);
1302 long keycode
= event
.GetKeyCode();
1314 if (event
.CmdDown())
1316 // Fixes AltGr+key with European input languages on Windows
1317 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1324 wxRichTextEvent
cmdEvent(
1325 wxEVT_RICHTEXT_CHARACTER
,
1327 cmdEvent
.SetEventObject(this);
1328 cmdEvent
.SetFlags(flags
);
1330 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1332 cmdEvent
.SetCharacter((wxChar
) keycode
);
1334 cmdEvent
.SetPosition(m_caretPosition
+1);
1335 cmdEvent
.SetContainer(GetFocusObject());
1337 if (keycode
== wxT('\t'))
1339 // See if we need to promote or demote the selection or paragraph at the cursor
1340 // position, instead of inserting a tab.
1341 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1342 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1343 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1345 wxRichTextRange range
;
1347 range
= GetSelectionRange();
1349 range
= para
->GetRange().FromInternal();
1351 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1353 PromoteList(promoteBy
, range
, NULL
);
1355 GetEventHandler()->ProcessEvent(cmdEvent
);
1361 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1364 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1367 BeginBatchUndo(_("Insert Text"));
1369 long newPos
= m_caretPosition
;
1370 DeleteSelectedContent(& newPos
);
1373 wxString str
= event
.GetUnicodeKey();
1375 wxString str
= (wxChar
) event
.GetKeyCode();
1377 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1381 SetDefaultStyleToCursorStyle();
1382 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1384 cmdEvent
.SetPosition(m_caretPosition
);
1385 GetEventHandler()->ProcessEvent(cmdEvent
);
1393 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1395 wxRichTextAttr attr
;
1396 if (container
&& GetStyle(position
, attr
, container
))
1398 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1400 SetCursor(m_urlCursor
);
1402 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1404 SetCursor(m_textCursor
);
1412 // Processes the back key
1413 bool wxRichTextCtrl::ProcessBackKey(wxKeyEvent
& event
, int flags
)
1420 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1425 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1427 // If we're at the start of a list item with a bullet, let's 'delete' the bullet, i.e.
1428 // make it a continuation paragraph.
1429 if (!HasSelection() && para
&& ((m_caretPosition
+1) == para
->GetRange().GetStart()) &&
1430 para
->GetAttributes().HasBulletStyle() && (para
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
) == 0)
1432 wxRichTextParagraph
* newPara
= wxDynamicCast(para
->Clone(), wxRichTextParagraph
);
1433 newPara
->GetAttributes().SetBulletStyle(newPara
->GetAttributes().GetBulletStyle() | wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
);
1435 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Remove Bullet"), wxRICHTEXT_CHANGE_STYLE
, & GetBuffer(), GetFocusObject(), this);
1436 action
->SetRange(newPara
->GetRange());
1437 action
->SetPosition(GetCaretPosition());
1438 action
->GetNewParagraphs().AppendChild(newPara
);
1439 // Also store the old ones for Undo
1440 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1442 GetBuffer().Invalidate(para
->GetRange());
1443 GetBuffer().SubmitAction(action
);
1445 // Automatically renumber list
1446 bool isNumberedList
= false;
1447 wxRichTextRange numberedListRange
= FindRangeForList(m_caretPosition
, isNumberedList
);
1448 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1450 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1457 BeginBatchUndo(_("Delete Text"));
1459 long newPos
= m_caretPosition
;
1461 bool processed
= DeleteSelectedContent(& newPos
);
1467 // Submit range in character positions, which are greater than caret positions,
1468 // so subtract 1 for deleted character and add 1 for conversion to character position.
1471 if (event
.CmdDown())
1473 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1476 wxRichTextRange
range(pos
+1, newPos
);
1477 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1479 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1488 wxRichTextRange
range(newPos
, newPos
);
1489 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1491 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1499 if (GetLastPosition() == -1)
1501 GetFocusObject()->Reset();
1503 m_caretPosition
= -1;
1505 SetDefaultStyleToCursorStyle();
1508 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1510 // Always send this event; wxEVT_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1512 wxRichTextEvent
cmdEvent(
1513 wxEVT_RICHTEXT_DELETE
,
1515 cmdEvent
.SetEventObject(this);
1516 cmdEvent
.SetFlags(flags
);
1517 cmdEvent
.SetPosition(m_caretPosition
+1);
1518 cmdEvent
.SetContainer(GetFocusObject());
1519 GetEventHandler()->ProcessEvent(cmdEvent
);
1528 /// Delete content if there is a selection, e.g. when pressing a key.
1529 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1533 long pos
= m_selection
.GetRange().GetStart();
1534 wxRichTextRange range
= m_selection
.GetRange();
1536 // SelectAll causes more to be selected than doing it interactively,
1537 // and causes a new paragraph to be inserted. So for multiline buffers,
1538 // don't delete the final position.
1539 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1540 range
.SetEnd(range
.GetEnd()-1);
1542 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1543 m_selection
.Reset();
1544 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1554 /// Keyboard navigation
1558 Left: left one character
1559 Right: right one character
1562 Ctrl-Left: left one word
1563 Ctrl-Right: right one word
1564 Ctrl-Up: previous paragraph start
1565 Ctrl-Down: next start of paragraph
1568 Ctrl-Home: start of document
1569 Ctrl-End: end of document
1570 Page-Up: Up a screen
1571 Page-Down: Down a screen
1575 Ctrl-Alt-PgUp: Start of window
1576 Ctrl-Alt-PgDn: End of window
1577 F8: Start selection mode
1578 Esc: End selection mode
1580 Adding Shift does the above but starts/extends selection.
1585 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1587 bool success
= false;
1589 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1591 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1592 success
= WordRight(1, flags
);
1594 success
= MoveRight(1, flags
);
1596 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1598 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1599 success
= WordLeft(1, flags
);
1601 success
= MoveLeft(1, flags
);
1603 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1605 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1606 success
= MoveToParagraphStart(flags
);
1608 success
= MoveUp(1, flags
);
1610 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1612 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1613 success
= MoveToParagraphEnd(flags
);
1615 success
= MoveDown(1, flags
);
1617 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1619 success
= PageUp(1, flags
);
1621 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1623 success
= PageDown(1, flags
);
1625 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1627 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1628 success
= MoveHome(flags
);
1630 success
= MoveToLineStart(flags
);
1632 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1634 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1635 success
= MoveEnd(flags
);
1637 success
= MoveToLineEnd(flags
);
1642 ScrollIntoView(m_caretPosition
, keyCode
);
1643 SetDefaultStyleToCursorStyle();
1649 /// Extend the selection. Selections are in caret positions.
1650 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1652 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1654 if (oldPos
== newPos
)
1657 wxRichTextSelection oldSelection
= m_selection
;
1659 m_selection
.SetContainer(GetFocusObject());
1661 wxRichTextRange oldRange
;
1662 if (m_selection
.IsValid())
1663 oldRange
= m_selection
.GetRange();
1665 oldRange
= wxRICHTEXT_NO_SELECTION
;
1666 wxRichTextRange newRange
;
1668 // If not currently selecting, start selecting
1669 if (oldRange
.GetStart() == -2)
1671 m_selectionAnchor
= oldPos
;
1673 if (oldPos
> newPos
)
1674 newRange
.SetRange(newPos
+1, oldPos
);
1676 newRange
.SetRange(oldPos
+1, newPos
);
1680 // Always ensure that the selection range start is greater than
1682 if (newPos
> m_selectionAnchor
)
1683 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1684 else if (newPos
== m_selectionAnchor
)
1685 newRange
= wxRichTextRange(-2, -2);
1687 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1690 m_selection
.SetRange(newRange
);
1692 RefreshForSelectionChange(oldSelection
, m_selection
);
1694 if (newRange
.GetStart() > newRange
.GetEnd())
1696 wxLogDebug(wxT("Strange selection range"));
1705 /// Scroll into view, returning true if we scrolled.
1706 /// This takes a _caret_ position.
1707 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1709 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1715 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1717 int startXUnits
, startYUnits
;
1718 GetViewStart(& startXUnits
, & startYUnits
);
1719 int startY
= startYUnits
* ppuY
;
1722 GetVirtualSize(& sx
, & sy
);
1728 wxRect rect
= GetScaledRect(line
->GetRect());
1730 bool scrolled
= false;
1732 wxSize clientSize
= GetClientSize();
1734 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1737 wxClientDC
dc(this);
1738 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1739 topMargin
, bottomMargin
);
1741 clientSize
.y
-= (int) (0.5 + bottomMargin
* GetScale());
1743 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1745 int y
= rect
.y
- GetClientSize().y
/2;
1746 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1747 if (y
>= 0 && (y
+ clientSize
.y
) < (int) (0.5 + GetBuffer().GetCachedSize().y
* GetScale()))
1749 if (startYUnits
!= yUnits
)
1751 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1754 #if !wxRICHTEXT_USE_OWN_CARET
1764 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1765 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1766 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1767 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1769 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1771 // Make it scroll so this item is at the bottom
1773 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1774 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1776 // If we're still off the screen, scroll another line down
1777 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1780 if (startYUnits
!= yUnits
)
1782 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1786 else if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale())))
1788 // Make it scroll so this item is at the top
1790 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1791 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1793 if (startYUnits
!= yUnits
)
1795 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1801 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1802 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1803 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1804 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1806 if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale())))
1808 // Make it scroll so this item is at the top
1810 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1811 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1813 if (startYUnits
!= yUnits
)
1815 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1819 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1821 // Make it scroll so this item is at the bottom
1823 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1824 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1826 // If we're still off the screen, scroll another line down
1827 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1830 if (startYUnits
!= yUnits
)
1832 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1838 #if !wxRICHTEXT_USE_OWN_CARET
1846 /// Is the given position visible on the screen?
1847 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1849 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1855 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1858 GetViewStart(& startX
, & startY
);
1860 startY
= startY
* ppuY
;
1862 wxRect rect
= GetScaledRect(line
->GetRect());
1863 wxSize clientSize
= GetClientSize();
1864 clientSize
.y
-= (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale());
1866 return (rect
.GetTop() >= (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()))) &&
1867 (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1870 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1872 m_caretPosition
= position
;
1873 m_caretAtLineStart
= showAtLineStart
;
1876 /// Move caret one visual step forward: this may mean setting a flag
1877 /// and keeping the same position if we're going from the end of one line
1878 /// to the start of the next, which may be the exact same caret position.
1879 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1881 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1883 // Only do the check if we're not at the end of the paragraph (where things work OK
1885 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1887 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1891 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1893 // We're at the end of a line. See whether we need to
1894 // stay at the same actual caret position but change visual
1895 // position, or not.
1896 if (oldPosition
== lineRange
.GetEnd())
1898 if (m_caretAtLineStart
)
1900 // We're already at the start of the line, so actually move on now.
1901 m_caretPosition
= oldPosition
+ 1;
1902 m_caretAtLineStart
= false;
1906 // We're showing at the end of the line, so keep to
1907 // the same position but indicate that we're to show
1908 // at the start of the next line.
1909 m_caretPosition
= oldPosition
;
1910 m_caretAtLineStart
= true;
1912 SetDefaultStyleToCursorStyle();
1918 SetDefaultStyleToCursorStyle();
1921 /// Move caret one visual step backward: this may mean setting a flag
1922 /// and keeping the same position if we're going from the end of one line
1923 /// to the start of the next, which may be the exact same caret position.
1924 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1926 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1928 // Only do the check if we're not at the start of the paragraph (where things work OK
1930 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1932 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1936 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1938 // We're at the start of a line. See whether we need to
1939 // stay at the same actual caret position but change visual
1940 // position, or not.
1941 if (oldPosition
== lineRange
.GetStart())
1943 m_caretPosition
= oldPosition
-1;
1944 m_caretAtLineStart
= true;
1947 else if (oldPosition
== lineRange
.GetEnd())
1949 if (m_caretAtLineStart
)
1951 // We're at the start of the line, so keep the same caret position
1952 // but clear the start-of-line flag.
1953 m_caretPosition
= oldPosition
;
1954 m_caretAtLineStart
= false;
1958 // We're showing at the end of the line, so go back
1959 // to the previous character position.
1960 m_caretPosition
= oldPosition
- 1;
1962 SetDefaultStyleToCursorStyle();
1968 SetDefaultStyleToCursorStyle();
1972 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1974 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1976 if (m_caretPosition
+ noPositions
< endPos
)
1978 long oldPos
= m_caretPosition
;
1979 long newPos
= m_caretPosition
+ noPositions
;
1981 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1985 // Determine by looking at oldPos and m_caretPosition whether
1986 // we moved from the end of a line to the start of the next line, in which case
1987 // we want to adjust the caret position such that it is positioned at the
1988 // start of the next line, rather than jumping past the first character of the
1990 if (noPositions
== 1)
1991 MoveCaretForward(oldPos
);
1993 SetCaretPosition(newPos
);
1996 SetDefaultStyleToCursorStyle();
2005 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
2009 if (m_caretPosition
> startPos
- noPositions
+ 1)
2011 long oldPos
= m_caretPosition
;
2012 long newPos
= m_caretPosition
- noPositions
;
2013 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2017 if (noPositions
== 1)
2018 MoveCaretBack(oldPos
);
2020 SetCaretPosition(newPos
);
2023 SetDefaultStyleToCursorStyle();
2031 // Find the caret position for the combination of hit-test flags and character position.
2032 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2033 // since this is ambiguous (same position used for end of line and start of next).
2034 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2035 bool& caretLineStart
)
2037 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2038 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2039 // so we view the caret at the start of the line.
2040 caretLineStart
= false;
2041 long caretPosition
= position
;
2043 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2045 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2046 wxRichTextRange lineRange
;
2048 lineRange
= thisLine
->GetAbsoluteRange();
2050 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2053 caretLineStart
= true;
2057 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2058 if (para
&& para
->GetRange().GetStart() == position
)
2062 return caretPosition
;
2066 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2068 return MoveDown(- noLines
, flags
);
2072 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2077 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2078 wxPoint pt
= GetCaret()->GetPosition();
2079 long newLine
= lineNumber
+ noLines
;
2080 bool notInThisObject
= false;
2082 if (lineNumber
!= -1)
2086 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2087 if (newLine
> lastLine
)
2088 notInThisObject
= true;
2093 notInThisObject
= true;
2097 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2098 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
;
2100 bool lineIsEmpty
= false;
2101 if (notInThisObject
)
2103 // If we know we're navigating out of the current object,
2104 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2105 container
= & GetBuffer();
2106 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2108 if (noLines
> 0) // going down
2110 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2114 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2119 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2122 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2123 if (lineObj
->GetRange().GetStart() == lineObj
->GetRange().GetEnd())
2131 wxClientDC
dc(this);
2133 dc
.SetFont(GetFont());
2135 wxRichTextObject
* hitObj
= NULL
;
2136 wxRichTextObject
* contextObj
= NULL
;
2137 wxRichTextDrawingContext
context(& GetBuffer());
2138 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2141 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2142 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2145 if (notInThisObject
)
2147 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2148 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2150 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2152 container
= actualContainer
;
2156 bool caretLineStart
= true;
2158 // If the line is empty, there is only one possible position for the caret,
2159 // so force the 'before' state so FindCaretPositionForCharacterPosition doesn't
2160 // just return the same position.
2163 hitTest
&= ~wxRICHTEXT_HITTEST_AFTER
;
2164 hitTest
|= wxRICHTEXT_HITTEST_BEFORE
;
2166 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2167 long newSelEnd
= caretPosition
;
2170 if (notInThisObject
)
2173 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2178 SetCaretPosition(caretPosition
, caretLineStart
);
2180 SetDefaultStyleToCursorStyle();
2188 /// Move to the end of the paragraph
2189 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2191 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2194 long newPos
= para
->GetRange().GetEnd() - 1;
2195 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2199 SetCaretPosition(newPos
);
2201 SetDefaultStyleToCursorStyle();
2209 /// Move to the start of the paragraph
2210 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2212 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2215 long newPos
= para
->GetRange().GetStart() - 1;
2216 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2220 SetCaretPosition(newPos
, true);
2222 SetDefaultStyleToCursorStyle();
2230 /// Move to the end of the line
2231 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2233 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2237 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2238 long newPos
= lineRange
.GetEnd();
2239 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2243 SetCaretPosition(newPos
);
2245 SetDefaultStyleToCursorStyle();
2253 /// Move to the start of the line
2254 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2256 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2259 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2260 long newPos
= lineRange
.GetStart()-1;
2262 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2266 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2268 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2270 SetDefaultStyleToCursorStyle();
2278 /// Move to the start of the buffer
2279 bool wxRichTextCtrl::MoveHome(int flags
)
2281 if (m_caretPosition
!= -1)
2283 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2287 SetCaretPosition(-1);
2289 SetDefaultStyleToCursorStyle();
2297 /// Move to the end of the buffer
2298 bool wxRichTextCtrl::MoveEnd(int flags
)
2300 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2302 if (m_caretPosition
!= endPos
)
2304 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2308 SetCaretPosition(endPos
);
2310 SetDefaultStyleToCursorStyle();
2318 /// Move noPages pages up
2319 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2321 return PageDown(- noPages
, flags
);
2324 /// Move noPages pages down
2325 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2327 // Calculate which line occurs noPages * screen height further down.
2328 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2331 wxSize clientSize
= GetClientSize();
2332 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2334 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2337 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2338 long pos
= lineRange
.GetStart()-1;
2339 if (pos
!= m_caretPosition
)
2341 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2343 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2347 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2349 SetDefaultStyleToCursorStyle();
2359 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2361 return str
== wxT(" ") || str
== wxT("\t") || (!str
.empty() && (str
[0] == (wxChar
) 160));
2364 // Finds the caret position for the next word
2365 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2367 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2371 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2373 // First skip current text to space
2374 while (i
< endPos
&& i
> -1)
2376 // i is in character, not caret positions
2377 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2378 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2379 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2383 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2390 while (i
< endPos
&& i
> -1)
2392 // i is in character, not caret positions
2393 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2394 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2395 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2396 return wxMax(-1, i
);
2398 if (text
.empty()) // End of paragraph, or maybe an image
2399 return wxMax(-1, i
- 1);
2400 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2404 // Convert to caret position
2405 return wxMax(-1, i
- 1);
2414 long i
= m_caretPosition
;
2416 // First skip white space
2417 while (i
< endPos
&& i
> -1)
2419 // i is in character, not caret positions
2420 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2421 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2423 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2425 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2430 // Next skip current text to space
2431 while (i
< endPos
&& i
> -1)
2433 // i is in character, not caret positions
2434 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2435 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2436 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2439 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2452 /// Move n words left
2453 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2455 long pos
= FindNextWordPosition(-1);
2456 if (pos
!= m_caretPosition
)
2458 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2460 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2464 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2466 SetDefaultStyleToCursorStyle();
2474 /// Move n words right
2475 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2477 long pos
= FindNextWordPosition(1);
2478 if (pos
!= m_caretPosition
)
2480 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2482 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2486 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2488 SetDefaultStyleToCursorStyle();
2497 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2499 // Only do sizing optimization for large buffers
2500 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2502 m_fullLayoutRequired
= true;
2503 m_fullLayoutTime
= wxGetLocalTimeMillis();
2504 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2505 LayoutContent(true /* onlyVisibleRect */);
2508 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2510 #if wxRICHTEXT_BUFFERED_PAINTING
2517 // Force any pending layout due to large buffer
2518 void wxRichTextCtrl::ForceDelayedLayout()
2520 if (m_fullLayoutRequired
)
2522 m_fullLayoutRequired
= false;
2523 m_fullLayoutTime
= 0;
2524 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2525 ShowPosition(m_fullLayoutSavedPosition
);
2531 /// Idle-time processing
2532 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2534 #if wxRICHTEXT_USE_OWN_CARET
2535 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2537 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2543 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2545 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2547 m_fullLayoutRequired
= false;
2548 m_fullLayoutTime
= 0;
2549 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2550 ShowPosition(m_fullLayoutSavedPosition
);
2554 if (m_caretPositionForDefaultStyle
!= -2)
2556 // If the caret position has changed, no longer reflect the default style
2558 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2559 m_caretPositionForDefaultStyle
= -2;
2566 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2568 #if wxRICHTEXT_USE_OWN_CARET
2569 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2572 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2579 /// Set up scrollbars, e.g. after a resize
2580 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2585 if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled
)
2587 SetScrollbars(0, 0, 0, 0, 0, 0);
2591 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2592 // of pixels. See e.g. wxVScrolledWindow for ideas.
2593 int pixelsPerUnit
= 5;
2594 wxSize clientSize
= GetClientSize();
2596 int maxHeight
= (int) (0.5 + GetScale() * (GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin()));
2598 // Round up so we have at least maxHeight pixels
2599 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2601 int startX
= 0, startY
= 0;
2603 GetViewStart(& startX
, & startY
);
2605 int maxPositionX
= 0;
2606 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2608 int newStartX
= wxMin(maxPositionX
, startX
);
2609 int newStartY
= wxMin(maxPositionY
, startY
);
2611 int oldPPUX
, oldPPUY
;
2612 int oldStartX
, oldStartY
;
2613 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2614 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2615 GetViewStart(& oldStartX
, & oldStartY
);
2616 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2618 oldVirtualSizeY
/= oldPPUY
;
2620 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2623 // Don't set scrollbars if there were none before, and there will be none now.
2624 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2627 // Move to previous scroll position if
2629 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2632 /// Paint the background
2633 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2635 wxColour backgroundColour
= GetBackgroundColour();
2636 if (!backgroundColour
.IsOk())
2637 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2639 // Clear the background
2640 dc
.SetBrush(wxBrush(backgroundColour
));
2641 dc
.SetPen(*wxTRANSPARENT_PEN
);
2642 wxRect
windowRect(GetClientSize());
2643 windowRect
.x
-= 2; windowRect
.y
-= 2;
2644 windowRect
.width
+= 4; windowRect
.height
+= 4;
2646 // We need to shift the rectangle to take into account
2647 // scrolling. Converting device to logical coordinates.
2648 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2649 dc
.DrawRectangle(windowRect
);
2652 #if wxRICHTEXT_BUFFERED_PAINTING
2653 /// Recreate buffer bitmap if necessary
2654 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2657 if (sz
== wxDefaultSize
)
2658 sz
= GetClientSize();
2660 if (sz
.x
< 1 || sz
.y
< 1)
2663 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2664 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2665 return m_bufferBitmap
.IsOk();
2669 // ----------------------------------------------------------------------------
2670 // file IO functions
2671 // ----------------------------------------------------------------------------
2672 #if wxUSE_FFILE && wxUSE_STREAMS
2673 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2675 SetFocusObject(& GetBuffer(), true);
2677 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2679 m_filename
= filename
;
2682 SetInsertionPoint(0);
2685 SetupScrollbars(true);
2687 wxTextCtrl::SendTextUpdatedEvent(this);
2693 wxLogError(_("File couldn't be loaded."));
2699 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2701 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2703 m_filename
= filename
;
2710 wxLogError(_("The text couldn't be saved."));
2714 #endif // wxUSE_FFILE && wxUSE_STREAMS
2716 // ----------------------------------------------------------------------------
2717 // wxRichTextCtrl specific functionality
2718 // ----------------------------------------------------------------------------
2720 /// Add a new paragraph of text to the end of the buffer
2721 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2723 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2724 GetBuffer().Invalidate();
2730 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2732 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2733 GetBuffer().Invalidate();
2738 // ----------------------------------------------------------------------------
2739 // selection and ranges
2740 // ----------------------------------------------------------------------------
2743 void wxRichTextCtrl::SelectNone()
2745 if (m_selection
.IsValid())
2747 wxRichTextSelection oldSelection
= m_selection
;
2749 m_selection
.Reset();
2751 RefreshForSelectionChange(oldSelection
, m_selection
);
2753 m_selectionAnchor
= -2;
2754 m_selectionAnchorObject
= NULL
;
2755 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2758 static bool wxIsWordDelimiter(const wxString
& text
)
2760 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2763 /// Select the word at the given character position
2764 bool wxRichTextCtrl::SelectWord(long position
)
2766 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2769 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2773 if (position
== para
->GetRange().GetEnd())
2776 long positionStart
= position
;
2777 long positionEnd
= position
;
2779 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2781 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2782 if (wxIsWordDelimiter(text
))
2788 if (positionStart
< para
->GetRange().GetStart())
2789 positionStart
= para
->GetRange().GetStart();
2791 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2793 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2794 if (wxIsWordDelimiter(text
))
2800 if (positionEnd
>= para
->GetRange().GetEnd())
2801 positionEnd
= para
->GetRange().GetEnd();
2803 if (positionEnd
< positionStart
)
2806 SetSelection(positionStart
, positionEnd
+1);
2808 if (positionStart
>= 0)
2810 MoveCaret(positionStart
-1, true);
2811 SetDefaultStyleToCursorStyle();
2817 wxString
wxRichTextCtrl::GetStringSelection() const
2820 GetSelection(&from
, &to
);
2822 return GetRange(from
, to
);
2825 // ----------------------------------------------------------------------------
2827 // ----------------------------------------------------------------------------
2829 wxTextCtrlHitTestResult
2830 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2832 // implement in terms of the other overload as the native ports typically
2833 // can get the position and not (x, y) pair directly (although wxUniv
2834 // directly gets x and y -- and so overrides this method as well)
2836 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2838 if ( rc
!= wxTE_HT_UNKNOWN
)
2840 PositionToXY(pos
, x
, y
);
2846 wxTextCtrlHitTestResult
2847 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2850 wxClientDC
dc((wxRichTextCtrl
*) this);
2851 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2853 // Buffer uses logical position (relative to start of buffer)
2855 wxPoint pt2
= GetLogicalPoint(pt
);
2857 wxRichTextObject
* hitObj
= NULL
;
2858 wxRichTextObject
* contextObj
= NULL
;
2859 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2860 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2862 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2863 return wxTE_HT_BEFORE
;
2864 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2865 return wxTE_HT_BEYOND
;
2866 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2867 return wxTE_HT_ON_TEXT
;
2869 return wxTE_HT_UNKNOWN
;
2872 wxRichTextParagraphLayoutBox
*
2873 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2875 wxClientDC
dc(this);
2877 dc
.SetFont(GetFont());
2879 wxPoint logicalPt
= GetLogicalPoint(pt
);
2881 wxRichTextObject
* contextObj
= NULL
;
2882 wxRichTextDrawingContext
context(& GetBuffer());
2883 hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, &hitObj
, &contextObj
, flags
);
2884 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2890 // ----------------------------------------------------------------------------
2891 // set/get the controls text
2892 // ----------------------------------------------------------------------------
2894 wxString
wxRichTextCtrl::DoGetValue() const
2896 return GetBuffer().GetText();
2899 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2901 // Public API for range is different from internals
2902 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2905 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2907 // Don't call Clear here, since it always sends a text updated event
2908 m_buffer
.ResetAndClearCommands();
2909 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2910 m_caretPosition
= -1;
2911 m_caretPositionForDefaultStyle
= -2;
2912 m_caretAtLineStart
= false;
2913 m_selection
.Reset();
2914 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2924 if (!value
.IsEmpty())
2926 // Remove empty paragraph
2927 GetBuffer().Clear();
2928 DoWriteText(value
, flags
);
2930 // for compatibility, don't move the cursor when doing SetValue()
2931 SetInsertionPoint(0);
2935 // still send an event for consistency
2936 if (flags
& SetValue_SendEvent
)
2937 wxTextCtrl::SendTextUpdatedEvent(this);
2942 void wxRichTextCtrl::WriteText(const wxString
& value
)
2947 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2949 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2951 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2952 wxRichTextDrawingContext
context(& GetBuffer());
2953 GetBuffer().Defragment(context
);
2955 if ( flags
& SetValue_SendEvent
)
2956 wxTextCtrl::SendTextUpdatedEvent(this);
2959 void wxRichTextCtrl::AppendText(const wxString
& text
)
2961 SetInsertionPointEnd();
2966 /// Write an image at the current insertion point
2967 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2969 wxRichTextImageBlock imageBlock
;
2971 wxImage image2
= image
;
2972 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2973 return WriteImage(imageBlock
, textAttr
);
2978 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2980 wxRichTextImageBlock imageBlock
;
2983 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2984 return WriteImage(imageBlock
, textAttr
);
2989 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2991 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2994 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2998 wxRichTextImageBlock imageBlock
;
3000 wxImage image
= bitmap
.ConvertToImage();
3001 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
3002 return WriteImage(imageBlock
, textAttr
);
3008 // Write a text box at the current insertion point.
3009 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
3011 wxRichTextBox
* textBox
= new wxRichTextBox
;
3012 textBox
->SetAttributes(textAttr
);
3013 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3014 textBox
->AddParagraph(wxEmptyString
);
3015 textBox
->SetParent(NULL
);
3017 // If the box has an invalid foreground colour, its text will mimic any upstream value (see #15224)
3018 if (!textBox
->GetAttributes().GetTextColour().IsOk())
3020 textBox
->GetAttributes().SetTextColour(GetBasicStyle().GetTextColour());
3023 // The object returned is the one actually inserted into the buffer,
3024 // while the original one is deleted.
3025 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3026 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
3030 wxRichTextField
* wxRichTextCtrl::WriteField(const wxString
& fieldType
, const wxRichTextProperties
& properties
,
3031 const wxRichTextAttr
& textAttr
)
3033 return GetFocusObject()->InsertFieldWithUndo(& GetBuffer(), m_caretPosition
+1, fieldType
, properties
,
3034 this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
, textAttr
);
3037 // Write a table at the current insertion point, returning the table.
3038 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3040 wxASSERT(rows
> 0 && cols
> 0);
3042 if (rows
<= 0 || cols
<= 0)
3045 wxRichTextTable
* table
= new wxRichTextTable
;
3046 table
->SetAttributes(tableAttr
);
3047 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3048 table
->SetBasicStyle(GetBasicStyle());
3050 table
->CreateTable(rows
, cols
);
3052 table
->SetParent(NULL
);
3054 // If cells have an invalid foreground colour, their text will mimic any upstream value (see #15224)
3055 wxRichTextAttr attr
= cellAttr
;
3056 if (!attr
.GetTextColour().IsOk())
3058 attr
.SetTextColour(GetBasicStyle().GetTextColour());
3062 for (j
= 0; j
< rows
; j
++)
3064 for (i
= 0; i
< cols
; i
++)
3066 table
->GetCell(j
, i
)->GetAttributes() = attr
;
3070 // The object returned is the one actually inserted into the buffer,
3071 // while the original one is deleted.
3072 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3073 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3078 /// Insert a newline (actually paragraph) at the current insertion point.
3079 bool wxRichTextCtrl::Newline()
3081 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3084 /// Insert a line break at the current insertion point.
3085 bool wxRichTextCtrl::LineBreak()
3088 text
= wxRichTextLineBreakChar
;
3089 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3092 // ----------------------------------------------------------------------------
3093 // Clipboard operations
3094 // ----------------------------------------------------------------------------
3096 void wxRichTextCtrl::Copy()
3100 wxRichTextRange range
= GetInternalSelectionRange();
3101 GetBuffer().CopyToClipboard(range
);
3105 void wxRichTextCtrl::Cut()
3109 wxRichTextRange range
= GetInternalSelectionRange();
3110 GetBuffer().CopyToClipboard(range
);
3112 DeleteSelectedContent();
3118 void wxRichTextCtrl::Paste()
3122 BeginBatchUndo(_("Paste"));
3124 long newPos
= m_caretPosition
;
3125 DeleteSelectedContent(& newPos
);
3127 GetBuffer().PasteFromClipboard(newPos
);
3133 void wxRichTextCtrl::DeleteSelection()
3135 if (CanDeleteSelection())
3137 DeleteSelectedContent();
3141 bool wxRichTextCtrl::HasSelection() const
3143 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3146 bool wxRichTextCtrl::HasUnfocusedSelection() const
3148 return m_selection
.IsValid();
3151 bool wxRichTextCtrl::CanCopy() const
3153 // Can copy if there's a selection
3154 return HasSelection();
3157 bool wxRichTextCtrl::CanCut() const
3159 return CanDeleteSelection();
3162 bool wxRichTextCtrl::CanPaste() const
3164 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3167 return GetBuffer().CanPasteFromClipboard();
3170 bool wxRichTextCtrl::CanDeleteSelection() const
3172 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3176 // ----------------------------------------------------------------------------
3178 // ----------------------------------------------------------------------------
3180 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3182 if (m_contextMenu
&& m_contextMenu
!= menu
)
3183 delete m_contextMenu
;
3184 m_contextMenu
= menu
;
3187 void wxRichTextCtrl::SetEditable(bool editable
)
3189 m_editable
= editable
;
3192 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3196 m_caretPosition
= pos
- 1;
3197 m_caretAtLineStart
= true;
3201 SetDefaultStyleToCursorStyle();
3204 void wxRichTextCtrl::SetInsertionPointEnd()
3206 long pos
= GetLastPosition();
3207 SetInsertionPoint(pos
);
3210 long wxRichTextCtrl::GetInsertionPoint() const
3212 return m_caretPosition
+1;
3215 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3217 return GetFocusObject()->GetOwnRange().GetEnd();
3220 // If the return values from and to are the same, there is no
3222 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3224 if (m_selection
.IsValid())
3226 *from
= m_selection
.GetRange().GetStart();
3227 *to
= m_selection
.GetRange().GetEnd();
3237 bool wxRichTextCtrl::IsEditable() const
3242 // ----------------------------------------------------------------------------
3244 // ----------------------------------------------------------------------------
3246 void wxRichTextCtrl::SetSelection(long from
, long to
)
3248 // if from and to are both -1, it means (in wxWidgets) that all text should
3250 if ( (from
== -1) && (to
== -1) )
3253 to
= GetLastPosition()+1;
3262 wxRichTextSelection oldSelection
= m_selection
;
3264 m_selectionAnchor
= from
-1;
3265 m_selectionAnchorObject
= NULL
;
3266 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3268 m_caretPosition
= wxMax(-1, to
-1);
3270 RefreshForSelectionChange(oldSelection
, m_selection
);
3275 // ----------------------------------------------------------------------------
3277 // ----------------------------------------------------------------------------
3279 void wxRichTextCtrl::Replace(long from
, long to
,
3280 const wxString
& value
)
3282 BeginBatchUndo(_("Replace"));
3284 SetSelection(from
, to
);
3286 wxRichTextAttr
attr(GetDefaultStyle());
3288 DeleteSelectedContent();
3290 SetDefaultStyle(attr
);
3292 if (!value
.IsEmpty())
3293 DoWriteText(value
, SetValue_SelectionOnly
);
3298 void wxRichTextCtrl::Remove(long from
, long to
)
3302 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3309 bool wxRichTextCtrl::IsModified() const
3311 return m_buffer
.IsModified();
3314 void wxRichTextCtrl::MarkDirty()
3316 m_buffer
.Modify(true);
3319 void wxRichTextCtrl::DiscardEdits()
3321 m_caretPositionForDefaultStyle
= -2;
3322 m_buffer
.Modify(false);
3323 m_buffer
.GetCommandProcessor()->ClearCommands();
3326 int wxRichTextCtrl::GetNumberOfLines() const
3328 return GetFocusObject()->GetParagraphCount();
3331 // ----------------------------------------------------------------------------
3332 // Positions <-> coords
3333 // ----------------------------------------------------------------------------
3335 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3337 return GetFocusObject()->XYToPosition(x
, y
);
3340 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3342 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3345 // ----------------------------------------------------------------------------
3347 // ----------------------------------------------------------------------------
3349 void wxRichTextCtrl::ShowPosition(long pos
)
3351 if (!IsPositionVisible(pos
))
3352 ScrollIntoView(pos
-1, WXK_DOWN
);
3355 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3357 return GetFocusObject()->GetParagraphLength(lineNo
);
3360 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3362 return GetFocusObject()->GetParagraphText(lineNo
);
3365 // ----------------------------------------------------------------------------
3367 // ----------------------------------------------------------------------------
3369 void wxRichTextCtrl::Undo()
3373 GetCommandProcessor()->Undo();
3377 void wxRichTextCtrl::Redo()
3381 GetCommandProcessor()->Redo();
3385 bool wxRichTextCtrl::CanUndo() const
3387 return GetCommandProcessor()->CanUndo() && IsEditable();
3390 bool wxRichTextCtrl::CanRedo() const
3392 return GetCommandProcessor()->CanRedo() && IsEditable();
3395 // ----------------------------------------------------------------------------
3396 // implementation details
3397 // ----------------------------------------------------------------------------
3399 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3401 SetValue(event
.GetString());
3402 GetEventHandler()->ProcessEvent(event
);
3405 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3407 // By default, load the first file into the text window.
3408 if (event
.GetNumberOfFiles() > 0)
3410 LoadFile(event
.GetFiles()[0]);
3414 wxSize
wxRichTextCtrl::DoGetBestSize() const
3416 return wxSize(10, 10);
3419 // ----------------------------------------------------------------------------
3420 // standard handlers for standard edit menu events
3421 // ----------------------------------------------------------------------------
3423 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3428 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3433 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3438 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3443 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3448 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3453 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3455 event
.Enable( CanCut() );
3458 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3460 event
.Enable( CanCopy() );
3463 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3465 event
.Enable( CanDeleteSelection() );
3468 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3470 event
.Enable( CanPaste() );
3473 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3475 event
.Enable( CanUndo() );
3476 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3479 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3481 event
.Enable( CanRedo() );
3482 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3485 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3487 if (GetLastPosition() > 0)
3491 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3493 event
.Enable(GetLastPosition() > 0);
3496 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3498 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3499 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3501 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3502 if (obj
&& CanEditProperties(obj
))
3503 EditProperties(obj
, this);
3505 m_contextMenuPropertiesInfo
.Clear();
3509 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3511 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3512 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3515 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3517 if (event
.GetEventObject() != this)
3523 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3526 // Prepares the context menu, adding appropriate property-editing commands.
3527 // Returns the number of property commands added.
3528 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3530 wxClientDC
dc(this);
3532 dc
.SetFont(GetFont());
3534 m_contextMenuPropertiesInfo
.Clear();
3537 wxRichTextObject
* hitObj
= NULL
;
3538 wxRichTextObject
* contextObj
= NULL
;
3539 if (pt
!= wxDefaultPosition
)
3541 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3542 wxRichTextDrawingContext
context(& GetBuffer());
3543 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
);
3545 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3547 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3548 if (hitObj
&& actualContainer
)
3550 if (actualContainer
->AcceptsFocus())
3552 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3553 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3556 if (addPropertyCommands
)
3557 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3561 if (addPropertyCommands
)
3562 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3567 if (addPropertyCommands
)
3568 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3573 // Invoked from the keyboard, so don't set the caret position and don't use the event
3575 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3577 contextObj
= hitObj
->GetParentContainer();
3579 contextObj
= GetFocusObject();
3581 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3582 if (hitObj
&& actualContainer
)
3584 if (addPropertyCommands
)
3585 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3589 if (addPropertyCommands
)
3590 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3596 if (addPropertyCommands
)
3597 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3598 return m_contextMenuPropertiesInfo
.GetCount();
3604 // Shows the context menu, adding appropriate property-editing commands
3605 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3609 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3617 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3619 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3622 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3624 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3627 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3629 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3632 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3634 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3637 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
, int flags
)
3639 GetFocusObject()->SetStyle(obj
, textAttr
, flags
);
3642 // extended style setting operation with flags including:
3643 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3644 // see richtextbuffer.h for more details.
3646 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3648 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3651 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3653 return GetBuffer().SetDefaultStyle(style
);
3656 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3658 wxRichTextAttr
attr1(style
);
3659 attr1
.GetTextBoxAttr().Reset();
3660 return GetBuffer().SetDefaultStyle(attr1
);
3663 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3665 return GetBuffer().GetDefaultStyle();
3668 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3670 wxRichTextAttr attr
;
3671 if (GetFocusObject()->GetStyle(position
, attr
))
3680 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3682 return GetFocusObject()->GetStyle(position
, style
);
3685 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3687 wxRichTextAttr attr
;
3688 if (container
->GetStyle(position
, attr
))
3697 // get the common set of styles for the range
3698 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3700 wxRichTextAttr attr
;
3701 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3710 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3712 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3715 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3717 return container
->GetStyleForRange(range
.ToInternal(), style
);
3720 /// Get the content (uncombined) attributes for this position.
3721 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3723 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3726 /// Get the content (uncombined) attributes for this position.
3727 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3729 return container
->GetUncombinedStyle(position
, style
);
3732 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3734 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3737 /// Set font, and also the buffer attributes
3738 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3740 wxControl::SetFont(font
);
3742 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3744 GetBuffer().SetBasicStyle(attr
);
3746 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3752 /// Transform logical to physical
3753 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3756 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3761 /// Transform physical to logical
3762 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3765 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3770 /// Position the caret
3771 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3776 //wxLogDebug(wxT("PositionCaret"));
3779 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3781 #if !wxRICHTEXT_USE_OWN_CARET
3782 caretRect
= GetScaledRect(caretRect
);
3784 int topMargin
= (int) (0.5 + GetScale()*GetBuffer().GetTopMargin());
3785 int bottomMargin
= (int) (0.5 + GetScale()*GetBuffer().GetBottomMargin());
3786 wxPoint newPt
= caretRect
.GetPosition();
3787 wxSize newSz
= caretRect
.GetSize();
3788 wxPoint pt
= GetPhysicalPoint(newPt
);
3789 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3792 if (GetCaret()->GetSize() != newSz
)
3793 GetCaret()->SetSize(newSz
);
3795 // Adjust size so the caret size and position doesn't appear in the margins
3796 if (((pt
.y
+ newSz
.y
) <= topMargin
) || (pt
.y
>= (GetClientSize().y
- bottomMargin
)))
3801 else if (pt
.y
< topMargin
&& (pt
.y
+ newSz
.y
) > topMargin
)
3803 newSz
.y
-= (topMargin
- pt
.y
);
3807 GetCaret()->SetSize(newSz
);
3810 else if (pt
.y
< (GetClientSize().y
- bottomMargin
) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- bottomMargin
))
3812 newSz
.y
= GetClientSize().y
- bottomMargin
- pt
.y
;
3813 GetCaret()->SetSize(newSz
);
3816 GetCaret()->Move(pt
);
3822 /// Get the caret height and position for the given character position
3823 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3825 wxClientDC
dc(this);
3827 dc
.SetUserScale(GetScale(), GetScale());
3828 dc
.SetFont(GetFont());
3834 container
= GetFocusObject();
3836 wxRichTextDrawingContext
context(& GetBuffer());
3837 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3839 // Caret height can't be zero
3841 height
= dc
.GetCharHeight();
3843 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3850 /// Gets the line for the visible caret position. If the caret is
3851 /// shown at the very end of the line, it means the next character is actually
3852 /// on the following line. So let's get the line we're expecting to find
3853 /// if this is the case.
3854 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3856 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3857 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3860 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3861 if (caretPosition
== lineRange
.GetStart()-1 &&
3862 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3864 if (!m_caretAtLineStart
)
3865 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3872 /// Move the caret to the given character position
3873 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3875 if (GetBuffer().IsDirty())
3879 container
= GetFocusObject();
3881 if (pos
<= container
->GetOwnRange().GetEnd())
3883 SetCaretPosition(pos
, showAtLineStart
);
3885 PositionCaret(container
);
3893 /// Layout the buffer: which we must do before certain operations, such as
3894 /// setting the caret position.
3895 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3897 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3899 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
3900 if (availableSpace
.width
== 0)
3901 availableSpace
.width
= 10;
3902 if (availableSpace
.height
== 0)
3903 availableSpace
.height
= 10;
3905 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3906 if (onlyVisibleRect
)
3908 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3909 availableSpace
.SetPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))));
3912 wxClientDC
dc(this);
3915 dc
.SetFont(GetFont());
3916 dc
.SetUserScale(GetScale(), GetScale());
3918 wxRichTextDrawingContext
context(& GetBuffer());
3919 GetBuffer().Defragment(context
);
3920 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3921 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3922 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3924 dc
.SetUserScale(1.0, 1.0);
3926 if (!IsFrozen() && !onlyVisibleRect
)
3933 /// Is all of the selection, or the current caret position, bold?
3934 bool wxRichTextCtrl::IsSelectionBold()
3938 wxRichTextAttr attr
;
3939 wxRichTextRange range
= GetSelectionRange();
3940 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3941 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3943 return HasCharacterAttributes(range
, attr
);
3947 // If no selection, then we need to combine current style with default style
3948 // to see what the effect would be if we started typing.
3949 wxRichTextAttr attr
;
3950 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3952 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3953 if (GetStyle(pos
, attr
))
3955 if (IsDefaultStyleShowing())
3956 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3957 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3963 /// Is all of the selection, or the current caret position, italics?
3964 bool wxRichTextCtrl::IsSelectionItalics()
3968 wxRichTextRange range
= GetSelectionRange();
3969 wxRichTextAttr attr
;
3970 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3971 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3973 return HasCharacterAttributes(range
, attr
);
3977 // If no selection, then we need to combine current style with default style
3978 // to see what the effect would be if we started typing.
3979 wxRichTextAttr attr
;
3980 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3982 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3983 if (GetStyle(pos
, attr
))
3985 if (IsDefaultStyleShowing())
3986 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3987 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3993 /// Is all of the selection, or the current caret position, underlined?
3994 bool wxRichTextCtrl::IsSelectionUnderlined()
3998 wxRichTextRange range
= GetSelectionRange();
3999 wxRichTextAttr attr
;
4000 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4001 attr
.SetFontUnderlined(true);
4003 return HasCharacterAttributes(range
, attr
);
4007 // If no selection, then we need to combine current style with default style
4008 // to see what the effect would be if we started typing.
4009 wxRichTextAttr attr
;
4010 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4011 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4013 if (GetStyle(pos
, attr
))
4015 if (IsDefaultStyleShowing())
4016 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
4017 return attr
.GetFontUnderlined();
4023 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
4024 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
4026 wxRichTextAttr attr
;
4027 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4028 attr
.SetTextEffectFlags(flag
);
4029 attr
.SetTextEffects(flag
);
4033 return HasCharacterAttributes(GetSelectionRange(), attr
);
4037 // If no selection, then we need to combine current style with default style
4038 // to see what the effect would be if we started typing.
4039 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4040 if (GetStyle(pos
, attr
))
4042 if (IsDefaultStyleShowing())
4043 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
4044 return (attr
.GetTextEffectFlags() & flag
) != 0;
4050 /// Apply bold to the selection
4051 bool wxRichTextCtrl::ApplyBoldToSelection()
4053 wxRichTextAttr attr
;
4054 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
4055 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4058 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4061 wxRichTextAttr current
= GetDefaultStyleEx();
4062 current
.Apply(attr
);
4063 SetAndShowDefaultStyle(current
);
4068 /// Apply italic to the selection
4069 bool wxRichTextCtrl::ApplyItalicToSelection()
4071 wxRichTextAttr attr
;
4072 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4073 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4076 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4079 wxRichTextAttr current
= GetDefaultStyleEx();
4080 current
.Apply(attr
);
4081 SetAndShowDefaultStyle(current
);
4086 /// Apply underline to the selection
4087 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4089 wxRichTextAttr attr
;
4090 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4091 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4094 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4097 wxRichTextAttr current
= GetDefaultStyleEx();
4098 current
.Apply(attr
);
4099 SetAndShowDefaultStyle(current
);
4104 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4105 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4107 wxRichTextAttr attr
;
4108 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4109 attr
.SetTextEffectFlags(flags
);
4110 if (!DoesSelectionHaveTextEffectFlag(flags
))
4111 attr
.SetTextEffects(flags
);
4113 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4116 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4119 wxRichTextAttr current
= GetDefaultStyleEx();
4120 current
.Apply(attr
);
4121 SetAndShowDefaultStyle(current
);
4126 /// Is all of the selection aligned according to the specified flag?
4127 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4129 wxRichTextRange range
;
4131 range
= GetSelectionRange();
4133 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4135 wxRichTextAttr attr
;
4136 attr
.SetAlignment(alignment
);
4138 return HasParagraphAttributes(range
, attr
);
4141 /// Apply alignment to the selection
4142 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4144 wxRichTextAttr attr
;
4145 attr
.SetAlignment(alignment
);
4147 return SetStyle(GetSelectionRange(), attr
);
4150 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4152 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4157 /// Apply a named style to the selection
4158 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4160 // Flags are defined within each definition, so only certain
4161 // attributes are applied.
4162 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4164 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4166 if (wxDynamicCast(def
, wxRichTextListStyleDefinition
))
4168 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4170 wxRichTextRange range
;
4173 range
= GetSelectionRange();
4176 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4177 range
= wxRichTextRange(pos
, pos
+1);
4180 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4183 bool isPara
= false;
4185 // Make sure the attr has the style name
4186 if (wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
))
4189 attr
.SetParagraphStyleName(def
->GetName());
4191 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4192 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4193 // to change its style independently.
4194 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4196 else if (wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
))
4197 attr
.SetCharacterStyleName(def
->GetName());
4198 else if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4199 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4201 if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4203 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4205 SetStyle(GetFocusObject(), attr
);
4211 else if (HasSelection())
4212 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4215 wxRichTextAttr current
= GetDefaultStyleEx();
4216 wxRichTextAttr
defaultStyle(attr
);
4219 // Don't apply extra character styles since they are already implied
4220 // in the paragraph style
4221 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4223 current
.Apply(defaultStyle
);
4224 SetAndShowDefaultStyle(current
);
4226 // If it's a paragraph style, we want to apply the style to the
4227 // current paragraph even if we didn't select any text.
4230 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4231 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4234 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4241 /// Apply the style sheet to the buffer, for example if the styles have changed.
4242 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4245 styleSheet
= GetBuffer().GetStyleSheet();
4249 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4251 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4259 /// Sets the default style to the style under the cursor
4260 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4262 wxRichTextAttr attr
;
4263 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4265 // If at the start of a paragraph, use the next position.
4266 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4268 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4269 if (obj
&& obj
->IsTopLevel())
4271 // Don't use the attributes of a top-level object, since they might apply
4272 // to content of the object, e.g. background colour.
4273 SetDefaultStyle(wxRichTextAttr());
4276 else if (GetUncombinedStyle(pos
, attr
))
4278 SetDefaultStyle(attr
);
4285 /// Returns the first visible position in the current view
4286 long wxRichTextCtrl::GetFirstVisiblePosition() const
4288 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))).y
);
4290 return line
->GetAbsoluteRange().GetStart();
4295 /// Get the first visible point in the window
4296 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4299 int startXUnits
, startYUnits
;
4301 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4302 GetViewStart(& startXUnits
, & startYUnits
);
4304 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4307 /// The adjusted caret position is the character position adjusted to take
4308 /// into account whether we're at the start of a paragraph, in which case
4309 /// style information should be taken from the next position, not current one.
4310 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4312 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4314 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4319 /// Get/set the selection range in character positions. -1, -1 means no selection.
4320 /// The range is in API convention, i.e. a single character selection is denoted
4322 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4324 wxRichTextRange range
= GetInternalSelectionRange();
4325 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4326 range
.SetEnd(range
.GetEnd() + 1);
4330 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4332 SetSelection(range
.GetStart(), range
.GetEnd());
4336 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4338 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4341 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4343 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4346 /// Clear list for given range
4347 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4349 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4352 /// Number/renumber any list elements in the given range
4353 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4355 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4358 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4360 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4363 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4364 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4366 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4369 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4371 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4374 // Given a character position at which there is a list style, find the range
4375 // encompassing the same list style by looking backwards and forwards.
4376 wxRichTextRange
wxRichTextCtrl::FindRangeForList(long pos
, bool& isNumberedList
)
4378 wxRichTextParagraphLayoutBox
* focusObject
= GetFocusObject();
4379 wxRichTextRange range
= wxRichTextRange(-1, -1);
4380 wxRichTextParagraph
* para
= focusObject
->GetParagraphAtPosition(pos
);
4381 if (!para
|| !para
->GetAttributes().HasListStyleName())
4385 wxString listStyle
= para
->GetAttributes().GetListStyleName();
4386 range
= para
->GetRange();
4388 isNumberedList
= para
->GetAttributes().HasBulletNumber();
4391 wxRichTextObjectList::compatibility_iterator initialNode
= focusObject
->GetChildren().Find(para
);
4394 wxRichTextObjectList::compatibility_iterator startNode
= initialNode
->GetPrevious();
4397 wxRichTextParagraph
* p
= wxDynamicCast(startNode
->GetData(), wxRichTextParagraph
);
4400 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4403 range
.SetStart(p
->GetRange().GetStart());
4406 startNode
= startNode
->GetPrevious();
4410 wxRichTextObjectList::compatibility_iterator endNode
= initialNode
->GetNext();
4413 wxRichTextParagraph
* p
= wxDynamicCast(endNode
->GetData(), wxRichTextParagraph
);
4416 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4419 range
.SetEnd(p
->GetRange().GetEnd());
4422 endNode
= endNode
->GetNext();
4429 /// Deletes the content in the given range
4430 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4432 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4435 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4437 if (sm_availableFontNames
.GetCount() == 0)
4439 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4440 sm_availableFontNames
.Sort();
4442 return sm_availableFontNames
;
4445 void wxRichTextCtrl::ClearAvailableFontNames()
4447 sm_availableFontNames
.Clear();
4450 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4452 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4454 wxTextAttrEx basicStyle
= GetBasicStyle();
4455 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4456 SetBasicStyle(basicStyle
);
4457 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4462 // Refresh the area affected by a selection change
4463 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4465 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4466 // the selection contains whole containers rather than just text, so refresh everything
4467 // for now as it would be hard to compute the rectangle bounding all selections.
4468 // TODO: improve on this.
4469 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4470 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4476 wxRichTextRange oldRange
, newRange
;
4477 if (oldSelection
.IsValid())
4478 oldRange
= oldSelection
.GetRange();
4480 oldRange
= wxRICHTEXT_NO_SELECTION
;
4481 if (newSelection
.IsValid())
4482 newRange
= newSelection
.GetRange();
4484 newRange
= wxRICHTEXT_NO_SELECTION
;
4486 // Calculate the refresh rectangle - just the affected lines
4487 long firstPos
, lastPos
;
4488 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4490 firstPos
= newRange
.GetStart();
4491 lastPos
= newRange
.GetEnd();
4493 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4495 firstPos
= oldRange
.GetStart();
4496 lastPos
= oldRange
.GetEnd();
4498 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4504 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4505 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4508 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4509 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4511 if (firstLine
&& lastLine
)
4513 wxSize clientSize
= GetClientSize();
4514 wxPoint pt1
= GetPhysicalPoint(GetScaledPoint(firstLine
->GetAbsolutePosition()));
4515 wxPoint pt2
= GetPhysicalPoint(GetScaledPoint(lastLine
->GetAbsolutePosition())) + wxPoint(0, (int) (0.5 + lastLine
->GetSize().y
* GetScale()));
4518 pt1
.y
= wxMax(0, pt1
.y
);
4520 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4522 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4523 RefreshRect(rect
, false);
4531 // margins functions
4532 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4534 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4535 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4536 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4537 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4542 wxPoint
wxRichTextCtrl::DoGetMargins() const
4544 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4545 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4548 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4550 if (obj
&& !obj
->AcceptsFocus())
4553 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4554 bool changingContainer
= (m_focusObject
!= obj
);
4556 if (changingContainer
&& HasSelection())
4559 m_focusObject
= obj
;
4562 m_focusObject
= & m_buffer
;
4564 if (setCaretPosition
&& changingContainer
)
4566 m_selection
.Reset();
4567 m_selectionAnchor
= -2;
4568 m_selectionAnchorObject
= NULL
;
4569 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4573 m_caretAtLineStart
= false;
4574 MoveCaret(pos
, m_caretAtLineStart
);
4575 SetDefaultStyleToCursorStyle();
4577 wxRichTextEvent
cmdEvent(
4578 wxEVT_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4580 cmdEvent
.SetEventObject(this);
4581 cmdEvent
.SetPosition(m_caretPosition
+1);
4582 cmdEvent
.SetOldContainer(oldContainer
);
4583 cmdEvent
.SetContainer(m_focusObject
);
4585 GetEventHandler()->ProcessEvent(cmdEvent
);
4590 #if wxUSE_DRAG_AND_DROP
4591 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4595 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4600 if (!GetSelection().IsValid())
4605 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4606 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4608 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4611 long position
= GetCaretPosition();
4612 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4613 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4615 // It doesn't make sense to move onto itself
4619 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4620 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4621 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4622 if ((def
== wxDragMove
) && !DeleteAfter
)
4624 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4625 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4628 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4629 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4631 delete richTextBuffer
;
4635 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4636 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4643 #endif // wxUSE_DRAG_AND_DROP
4646 #if wxUSE_DRAG_AND_DROP
4647 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4649 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4653 wxRichTextObject
* hitObj
= NULL
;
4654 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->GetUnscaledPoint(m_rtc
->ScreenToClient(wxGetMousePosition())), position
, hit
, hitObj
);
4656 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4658 m_rtc
->StoreFocusObject(container
);
4659 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4662 return false; // so that the base-class sets a cursor
4664 #endif // wxUSE_DRAG_AND_DROP
4666 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4671 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4676 void wxRichTextCtrl::EnableVerticalScrollbar(bool enable
)
4678 m_verticalScrollbarEnabled
= enable
;
4682 void wxRichTextCtrl::SetFontScale(double fontScale
, bool refresh
)
4684 GetBuffer().SetFontScale(fontScale
);
4687 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4692 void wxRichTextCtrl::SetDimensionScale(double dimScale
, bool refresh
)
4694 GetBuffer().SetDimensionScale(dimScale
);
4697 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4702 // Sets an overall scale factor for displaying and editing the content.
4703 void wxRichTextCtrl::SetScale(double scale
, bool refresh
)
4708 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4713 // Get an unscaled point
4714 wxPoint
wxRichTextCtrl::GetUnscaledPoint(const wxPoint
& pt
) const
4716 if (GetScale() == 1.0)
4719 return wxPoint((int) (0.5 + double(pt
.x
) / GetScale()), (int) (0.5 + double(pt
.y
) / GetScale()));
4722 // Get a scaled point
4723 wxPoint
wxRichTextCtrl::GetScaledPoint(const wxPoint
& pt
) const
4725 if (GetScale() == 1.0)
4728 return wxPoint((int) (0.5 + double(pt
.x
) * GetScale()), (int) (0.5 + double(pt
.y
) * GetScale()));
4731 // Get an unscaled size
4732 wxSize
wxRichTextCtrl::GetUnscaledSize(const wxSize
& sz
) const
4734 if (GetScale() == 1.0)
4737 return wxSize((int) (0.5 + double(sz
.x
) / GetScale()), (int) (0.5 + double(sz
.y
) / GetScale()));
4740 // Get a scaled size
4741 wxSize
wxRichTextCtrl::GetScaledSize(const wxSize
& sz
) const
4743 if (GetScale() == 1.0)
4746 return wxSize((int) (0.5 + double(sz
.x
) * GetScale()), (int) (0.5 + double(sz
.y
) * GetScale()));
4749 // Get an unscaled rect
4750 wxRect
wxRichTextCtrl::GetUnscaledRect(const wxRect
& rect
) const
4752 if (GetScale() == 1.0)
4755 return wxRect((int) (0.5 + double(rect
.x
) / GetScale()), (int) (0.5 + double(rect
.y
) / GetScale()),
4756 (int) (0.5 + double(rect
.width
) / GetScale()), (int) (0.5 + double(rect
.height
) / GetScale()));
4759 // Get a scaled rect
4760 wxRect
wxRichTextCtrl::GetScaledRect(const wxRect
& rect
) const
4762 if (GetScale() == 1.0)
4765 return wxRect((int) (0.5 + double(rect
.x
) * GetScale()), (int) (0.5 + double(rect
.y
) * GetScale()),
4766 (int) (0.5 + double(rect
.width
) * GetScale()), (int) (0.5 + double(rect
.height
) * GetScale()));
4769 #if wxRICHTEXT_USE_OWN_CARET
4771 // ----------------------------------------------------------------------------
4772 // initialization and destruction
4773 // ----------------------------------------------------------------------------
4775 void wxRichTextCaret::Init()
4778 m_refreshEnabled
= true;
4782 m_richTextCtrl
= NULL
;
4783 m_needsUpdate
= false;
4787 wxRichTextCaret::~wxRichTextCaret()
4789 if (m_timer
.IsRunning())
4793 // ----------------------------------------------------------------------------
4794 // showing/hiding/moving the caret (base class interface)
4795 // ----------------------------------------------------------------------------
4797 void wxRichTextCaret::DoShow()
4801 if (!m_timer
.IsRunning() && GetBlinkTime() > 0)
4802 m_timer
.Start(GetBlinkTime());
4807 void wxRichTextCaret::DoHide()
4809 if (m_timer
.IsRunning())
4815 void wxRichTextCaret::DoMove()
4821 if (m_xOld
!= -1 && m_yOld
!= -1)
4823 if (m_richTextCtrl
&& m_refreshEnabled
)
4825 wxRect
rect(wxPoint(m_xOld
, m_yOld
), GetSize());
4826 wxRect scaledRect
= m_richTextCtrl
->GetScaledRect(rect
);
4827 m_richTextCtrl
->RefreshRect(scaledRect
, false);
4836 void wxRichTextCaret::DoSize()
4838 int countVisible
= m_countVisible
;
4839 if (countVisible
> 0)
4845 if (countVisible
> 0)
4847 m_countVisible
= countVisible
;
4852 // ----------------------------------------------------------------------------
4853 // handling the focus
4854 // ----------------------------------------------------------------------------
4856 void wxRichTextCaret::OnSetFocus()
4864 void wxRichTextCaret::OnKillFocus()
4869 // ----------------------------------------------------------------------------
4870 // drawing the caret
4871 // ----------------------------------------------------------------------------
4873 void wxRichTextCaret::Refresh()
4875 if (m_richTextCtrl
&& m_refreshEnabled
)
4877 wxRect
rect(GetPosition(), GetSize());
4878 wxRect rectScaled
= m_richTextCtrl
->GetScaledRect(rect
);
4879 m_richTextCtrl
->RefreshRect(rectScaled
, false);
4883 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4885 wxBrush
brush(m_caretBrush
);
4886 wxPen
pen(m_caretPen
);
4887 if (m_richTextCtrl
&& m_richTextCtrl
->GetBasicStyle().HasTextColour())
4889 brush
= wxBrush(m_richTextCtrl
->GetBasicStyle().GetTextColour());
4890 pen
= wxPen(m_richTextCtrl
->GetBasicStyle().GetTextColour());
4892 dc
->SetBrush((m_hasFocus
? brush
: *wxTRANSPARENT_BRUSH
));
4895 wxPoint
pt(m_x
, m_y
);
4899 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4901 if (IsVisible() && m_flashOn
)
4902 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4905 void wxRichTextCaret::Notify()
4908 // Workaround for lack of kill focus event in wxOSX
4909 if (m_richTextCtrl
&& !m_richTextCtrl
->HasFocus())
4916 m_flashOn
= !m_flashOn
;
4920 void wxRichTextCaretTimer::Notify()
4925 // wxRICHTEXT_USE_OWN_CARET
4928 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4932 m_labels
.Add(label
);
4940 // Returns number of menu items were added.
4941 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4943 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4944 // If none of the standard properties identifiers are in the menu, add them if necessary.
4945 // If no items to add, just set the text to something generic
4946 if (GetCount() == 0)
4950 menu
->SetLabel(startCmd
, _("&Properties"));
4952 // Delete the others if necessary
4954 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4956 if (menu
->FindItem(i
))
4967 // Find the position of the first properties item
4968 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4970 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4971 if (item
&& item
->GetId() == startCmd
)
4980 int insertBefore
= pos
+1;
4981 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4983 if (menu
->FindItem(i
))
4985 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4989 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4990 menu
->Append(i
, m_labels
[i
- startCmd
]);
4992 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4997 // Delete any old items still left on the menu
4998 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
5000 if (menu
->FindItem(i
))
5008 // No existing property identifiers were found, so append to the end of the menu.
5009 menu
->AppendSeparator();
5010 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
5012 menu
->Append(i
, m_labels
[i
- startCmd
]);
5020 // Add appropriate menu items for the current container and clicked on object
5021 // (and container's parent, if appropriate).
5022 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
5025 if (obj
&& ctrl
->CanEditProperties(obj
))
5026 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
5028 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
5029 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
5031 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
5032 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());