1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
30 #include "wx/textfile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/fontenum.h"
38 #if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
39 #define wxHAVE_PRIMARY_SELECTION 1
41 #define wxHAVE_PRIMARY_SELECTION 0
44 #if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION
45 #include "wx/clipbrd.h"
48 // DLL options compatibility check:
50 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
54 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
60 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
61 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
62 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
63 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
65 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
66 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
67 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
68 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_PROPERTIES_CHANGED
, wxRichTextEvent
);
69 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
70 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
71 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
, wxRichTextEvent
);
73 #if wxRICHTEXT_USE_OWN_CARET
78 * This implements a non-flashing cursor in case there
79 * are platform-specific problems with the generic caret.
80 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
83 class wxRichTextCaret
;
84 class wxRichTextCaretTimer
: public wxTimer
87 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
91 virtual void Notify();
92 wxRichTextCaret
* m_caret
;
95 class wxRichTextCaret
: public wxCaret
100 // default - use Create()
101 wxRichTextCaret(): m_timer(this) { Init(); }
102 // creates a block caret associated with the given window
103 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
104 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
105 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
106 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
108 virtual ~wxRichTextCaret();
113 // called by wxWindow (not using the event tables)
114 virtual void OnSetFocus();
115 virtual void OnKillFocus();
117 // draw the caret on the given DC
118 void DoDraw(wxDC
*dc
);
120 // get the visible count
121 int GetVisibleCount() const { return m_countVisible
; }
123 // delay repositioning
124 bool GetNeedsUpdate() const { return m_needsUpdate
; }
125 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
129 bool GetRefreshEnabled() const { return m_refreshEnabled
; }
130 void EnableRefresh(bool b
) { m_refreshEnabled
= b
; }
133 virtual void DoShow();
134 virtual void DoHide();
135 virtual void DoMove();
136 virtual void DoSize();
146 bool m_hasFocus
; // true => our window has focus
147 bool m_needsUpdate
; // must be repositioned
149 wxRichTextCaretTimer m_timer
;
150 wxRichTextCtrl
* m_richTextCtrl
;
151 bool m_refreshEnabled
;
155 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl
)
157 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
159 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
160 EVT_PAINT(wxRichTextCtrl::OnPaint
)
161 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
162 EVT_IDLE(wxRichTextCtrl::OnIdle
)
163 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
164 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
165 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
166 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
167 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
168 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
169 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
170 EVT_CHAR(wxRichTextCtrl::OnChar
)
171 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
172 EVT_SIZE(wxRichTextCtrl::OnSize
)
173 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
174 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
175 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
176 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
177 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
179 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
180 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
182 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
183 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
185 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
186 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
188 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
189 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
191 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
192 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
194 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
195 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
197 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
198 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
200 EVT_MENU(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnProperties
)
201 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnUpdateProperties
)
203 EVT_MENU(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnProperties
)
204 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnUpdateProperties
)
206 EVT_MENU(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnProperties
)
207 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnUpdateProperties
)
215 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
217 wxRichTextCtrl::wxRichTextCtrl()
218 : wxScrollHelper(this)
223 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
225 const wxString
& value
,
229 const wxValidator
& validator
,
230 const wxString
& name
)
231 : wxScrollHelper(this)
234 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
238 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
239 const wxValidator
& validator
, const wxString
& name
)
243 if (!wxControl::Create(parent
, id
, pos
, size
,
244 style
|wxFULL_REPAINT_ON_RESIZE
,
248 if (!GetFont().IsOk())
250 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
253 // No physical scrolling, so we can preserve margins
254 EnableScrolling(false, false);
256 if (style
& wxTE_READONLY
)
259 // The base attributes must all have default values
260 wxRichTextAttr attributes
;
261 attributes
.SetFont(GetFont());
262 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
263 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
264 attributes
.SetLineSpacing(10);
265 attributes
.SetParagraphSpacingAfter(10);
266 attributes
.SetParagraphSpacingBefore(0);
267 SetBasicStyle(attributes
);
270 SetMargins(margin
, margin
);
272 // The default attributes will be merged with base attributes, so
273 // can be empty to begin with
274 wxRichTextAttr defaultAttributes
;
275 SetDefaultStyle(defaultAttributes
);
277 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
278 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
281 GetBuffer().SetRichTextCtrl(this);
283 #if wxRICHTEXT_USE_OWN_CARET
284 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
286 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
289 // Tell the sizers to use the given or best size
290 SetInitialSize(size
);
292 #if wxRICHTEXT_BUFFERED_PAINTING
294 RecreateBuffer(size
);
297 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
298 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
300 SetCursor(m_textCursor
);
302 if (!value
.IsEmpty())
305 GetBuffer().AddEventHandler(this);
308 wxAcceleratorEntry entries
[6];
310 entries
[0].Set(wxACCEL_CTRL
, (int) 'C', wxID_COPY
);
311 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_CUT
);
312 entries
[2].Set(wxACCEL_CTRL
, (int) 'V', wxID_PASTE
);
313 entries
[3].Set(wxACCEL_CTRL
, (int) 'A', wxID_SELECTALL
);
314 entries
[4].Set(wxACCEL_CTRL
, (int) 'Z', wxID_UNDO
);
315 entries
[5].Set(wxACCEL_CTRL
, (int) 'Y', wxID_REDO
);
317 wxAcceleratorTable
accel(6, entries
);
318 SetAcceleratorTable(accel
);
320 m_contextMenu
= new wxMenu
;
321 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
322 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
323 m_contextMenu
->AppendSeparator();
324 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
325 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
326 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
327 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
328 m_contextMenu
->AppendSeparator();
329 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
330 m_contextMenu
->AppendSeparator();
331 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
333 #if wxUSE_DRAG_AND_DROP
334 SetDropTarget(new wxRichTextDropTarget(this));
340 wxRichTextCtrl::~wxRichTextCtrl()
342 SetFocusObject(& GetBuffer(), false);
343 GetBuffer().RemoveEventHandler(this);
345 delete m_contextMenu
;
348 /// Member initialisation
349 void wxRichTextCtrl::Init()
351 m_contextMenu
= NULL
;
353 m_caretPosition
= -1;
354 m_selectionAnchor
= -2;
355 m_selectionAnchorObject
= NULL
;
356 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
358 m_useVirtualAttributes
= false;
359 m_verticalScrollbarEnabled
= true;
360 m_caretAtLineStart
= false;
362 #if wxUSE_DRAG_AND_DROP
365 m_fullLayoutRequired
= false;
366 m_fullLayoutTime
= 0;
367 m_fullLayoutSavedPosition
= 0;
368 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
369 m_caretPositionForDefaultStyle
= -2;
370 m_focusObject
= & m_buffer
;
374 void wxRichTextCtrl::DoThaw()
376 if (GetBuffer().IsDirty())
385 void wxRichTextCtrl::Clear()
387 if (GetFocusObject() == & GetBuffer())
389 m_buffer
.ResetAndClearCommands();
390 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
394 GetFocusObject()->Reset();
397 m_caretPosition
= -1;
398 m_caretPositionForDefaultStyle
= -2;
399 m_caretAtLineStart
= false;
401 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
411 wxTextCtrl::SendTextUpdatedEvent(this);
415 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
417 #if !wxRICHTEXT_USE_OWN_CARET
418 if (GetCaret() && !IsFrozen())
421 // Stop the caret refreshing the control from within the
424 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(false);
428 #if wxRICHTEXT_BUFFERED_PAINTING
429 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
439 dc
.SetFont(GetFont());
441 wxRect
drawingArea(GetUpdateRegion().GetBox());
442 drawingArea
.SetPosition(GetUnscaledPoint(GetLogicalPoint(drawingArea
.GetPosition())));
443 drawingArea
.SetSize(GetUnscaledSize(drawingArea
.GetSize()));
445 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
446 wxRichTextDrawingContext
context(& GetBuffer());
447 if (GetBuffer().IsDirty())
449 dc
.SetUserScale(GetScale(), GetScale());
451 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
452 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
454 dc
.SetUserScale(1.0, 1.0);
459 // Paint the background
462 wxRect
clipRect(availableSpace
);
463 clipRect
.x
+= GetBuffer().GetLeftMargin();
464 clipRect
.y
+= GetBuffer().GetTopMargin();
465 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
466 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
468 clipRect
= GetScaledRect(clipRect
);
469 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
471 dc
.SetClippingRegion(clipRect
);
474 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
475 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
477 dc
.SetUserScale(GetScale(), GetScale());
479 GetBuffer().Draw(dc
, context
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
481 dc
.DestroyClippingRegion();
483 // Other user defined painting after everything else (i.e. all text) is painted
484 PaintAboveContent(dc
);
486 #if wxRICHTEXT_USE_OWN_CARET
487 if (GetCaret()->IsVisible())
490 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
494 dc
.SetUserScale(1.0, 1.0);
497 #if !wxRICHTEXT_USE_OWN_CARET
503 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(true);
507 // Empty implementation, to prevent flicker
508 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
512 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
516 #if !wxRICHTEXT_USE_OWN_CARET
522 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
523 // Work around dropouts when control is focused
531 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
536 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
537 // Work around dropouts when control is focused
545 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
550 // Set up the caret for the given position and container, after a mouse click
551 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
553 bool caretAtLineStart
= false;
555 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
557 // If we're at the start of a line (but not first in para)
558 // then we should keep the caret showing at the start of the line
559 // by showing the m_caretAtLineStart flag.
560 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
561 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
563 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
564 caretAtLineStart
= true;
568 if (extendSelection
&& (m_caretPosition
!= position
))
569 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
571 MoveCaret(position
, caretAtLineStart
);
572 SetDefaultStyleToCursorStyle();
578 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
584 dc
.SetFont(GetFont());
586 // TODO: detect change of focus object
588 wxRichTextObject
* hitObj
= NULL
;
589 wxRichTextObject
* contextObj
= NULL
;
590 wxRichTextDrawingContext
context(& GetBuffer());
591 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
593 #if wxUSE_DRAG_AND_DROP
594 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
595 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position
))
597 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
599 m_dragStartPoint
= event
.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
602 m_dragStartTime
= wxDateTime::UNow();
603 #endif // wxUSE_DATETIME
605 // Preserve behaviour of clicking on an object within the selection
606 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
609 return; // Don't skip the event, else the selection will be lost
611 #endif // wxUSE_DRAG_AND_DROP
613 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
615 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
616 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
617 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
619 SetFocusObject(container
, false /* don't set caret position yet */);
625 long oldCaretPos
= m_caretPosition
;
627 SetCaretPositionAfterClick(container
, position
, hit
);
629 // For now, don't handle shift-click when we're selecting multiple objects.
630 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
631 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
640 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
645 if (GetCapture() == this)
648 // See if we clicked on a URL
651 dc
.SetFont(GetFont());
654 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
655 wxRichTextObject
* hitObj
= NULL
;
656 wxRichTextObject
* contextObj
= NULL
;
657 wxRichTextDrawingContext
context(& GetBuffer());
658 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
659 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
661 #if wxUSE_DRAG_AND_DROP
664 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
665 m_preDrag
= false; // Tell DnD not to happen now: we are processing Left Up ourselves.
667 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
669 wxRichTextObject
* hitObj
= NULL
;
670 wxRichTextObject
* contextObj
= NULL
;
671 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
672 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
673 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
674 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
676 SetFocusObject(container
, false /* don't set caret position yet */);
679 long oldCaretPos
= m_caretPosition
;
681 SetCaretPositionAfterClick(container
, position
, hit
);
683 // For now, don't handle shift-click when we're selecting multiple objects.
684 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
685 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
691 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
693 wxRichTextEvent
cmdEvent(
694 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
696 cmdEvent
.SetEventObject(this);
697 cmdEvent
.SetPosition(position
);
699 cmdEvent
.SetContainer(hitObj
->GetContainer());
701 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
704 if (GetStyle(position
, attr
))
706 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
708 wxString urlTarget
= attr
.GetURL();
709 if (!urlTarget
.IsEmpty())
711 wxMouseEvent
mouseEvent(event
);
713 long startPos
= 0, endPos
= 0;
714 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
717 startPos
= obj
->GetRange().GetStart();
718 endPos
= obj
->GetRange().GetEnd();
721 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
722 InitCommandEvent(urlEvent
);
724 urlEvent
.SetString(urlTarget
);
726 GetEventHandler()->ProcessEvent(urlEvent
);
734 #if wxUSE_DRAG_AND_DROP
736 #endif // wxUSE_DRAG_AND_DROP
738 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
739 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
741 // Put the selection in PRIMARY, if it exists
742 wxTheClipboard
->UsePrimarySelection(true);
744 wxRichTextRange range
= GetInternalSelectionRange();
745 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
747 wxTheClipboard
->UsePrimarySelection(false);
753 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
755 if (!event
.Dragging() && m_dragging
)
757 // We may have accidentally lost a mouse-up event, especially on Linux
759 if (GetCapture() == this)
763 #if wxUSE_DRAG_AND_DROP
765 if (m_preDrag
|| m_dragging
)
767 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
768 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
769 distance
= abs(x
) + abs(y
);
772 // See if we're starting Drag'n'Drop
776 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
780 && (diff
.GetMilliseconds() > 100)
787 wxRichTextRange range
= GetInternalSelectionRange();
788 if (range
== wxRICHTEXT_NONE
)
790 // Don't try to drag an empty range
795 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
796 long oldPos
= GetCaretPosition();
797 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
799 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
800 wxString text
= GetFocusObject()->GetTextForRange(range
);
802 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
804 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
806 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
807 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
808 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
810 wxRichTextDropSource
source(*compositeObject
, this);
811 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
812 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
813 BeginBatchUndo(_("Drag"));
814 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
817 case wxDragCopy
: break;
820 wxLogError(wxT("An error occurred during drag and drop operation"));
823 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
824 SetCaretPosition(oldPos
);
825 SetFocusObject(oldFocus
, false);
834 #endif // wxUSE_DRAG_AND_DROP
838 dc
.SetFont(GetFont());
841 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
842 wxRichTextObject
* hitObj
= NULL
;
843 wxRichTextObject
* contextObj
= NULL
;
847 // If we're dragging, let's only consider positions at this level; otherwise
848 // selecting a range is not going to work.
849 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
852 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
853 container
= GetFocusObject();
855 wxRichTextDrawingContext
context(& GetBuffer());
856 int hit
= container
->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, flags
);
858 // See if we need to change the cursor
861 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
863 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
865 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
868 SetCursor(m_textCursor
);
871 if (!event
.Dragging())
878 #if wxUSE_DRAG_AND_DROP
884 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
885 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
886 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
888 // Check for dragging across multiple containers
890 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
891 int hit2
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position2
, & hitObj2
, & contextObj2
, 0);
892 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
894 // See if we can find a common ancestor
895 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
897 firstContainer
= GetFocusObject();
898 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
902 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
903 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
904 // is the common ancestor.
905 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
908 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
910 wxRichTextObject
* p
= hitObj2
;
913 if (p
->GetParent() == commonAncestor
)
915 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
922 if (commonAncestor
&& firstContainer
&& otherContainer
)
924 // We have now got a second container that shares a parent with the current or anchor object.
925 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
927 // Don't go into common-ancestor selection mode if we still have the same
929 if (otherContainer
!= firstContainer
)
931 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
932 m_selectionAnchorObject
= firstContainer
;
933 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
935 // The common ancestor, such as a table, returns the cell selection
936 // between the anchor and current position.
937 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
942 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
947 if (otherContainer
->AcceptsFocus())
948 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
949 MoveCaret(-1, false);
950 SetDefaultStyleToCursorStyle();
955 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
956 #if wxUSE_DRAG_AND_DROP
962 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
967 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
973 dc
.SetFont(GetFont());
976 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
977 wxRichTextObject
* hitObj
= NULL
;
978 wxRichTextObject
* contextObj
= NULL
;
979 wxRichTextDrawingContext
context(& GetBuffer());
980 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
982 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
984 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
985 if (actualContainer
&& actualContainer
->AcceptsFocus())
987 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
988 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
992 wxRichTextEvent
cmdEvent(
993 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
995 cmdEvent
.SetEventObject(this);
996 cmdEvent
.SetPosition(position
);
998 cmdEvent
.SetContainer(hitObj
->GetContainer());
1000 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1004 /// Left-double-click
1005 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
1007 wxRichTextEvent
cmdEvent(
1008 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
1010 cmdEvent
.SetEventObject(this);
1011 cmdEvent
.SetPosition(m_caretPosition
+1);
1012 cmdEvent
.SetContainer(GetFocusObject());
1014 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1016 SelectWord(GetCaretPosition()+1);
1021 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
1023 wxRichTextEvent
cmdEvent(
1024 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
1026 cmdEvent
.SetEventObject(this);
1027 cmdEvent
.SetPosition(m_caretPosition
+1);
1028 cmdEvent
.SetContainer(GetFocusObject());
1030 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1033 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1034 // Paste any PRIMARY selection, if it exists
1035 wxTheClipboard
->UsePrimarySelection(true);
1037 wxTheClipboard
->UsePrimarySelection(false);
1042 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1045 if (event
.CmdDown())
1046 flags
|= wxRICHTEXT_CTRL_DOWN
;
1047 if (event
.ShiftDown())
1048 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1049 if (event
.AltDown())
1050 flags
|= wxRICHTEXT_ALT_DOWN
;
1052 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1054 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1056 KeyboardNavigate(event
.GetKeyCode(), flags
);
1060 long keycode
= event
.GetKeyCode();
1120 case WXK_NUMPAD_HOME
:
1121 case WXK_NUMPAD_LEFT
:
1123 case WXK_NUMPAD_RIGHT
:
1124 case WXK_NUMPAD_DOWN
:
1125 case WXK_NUMPAD_PAGEUP
:
1126 case WXK_NUMPAD_PAGEDOWN
:
1127 case WXK_NUMPAD_END
:
1128 case WXK_NUMPAD_BEGIN
:
1129 case WXK_NUMPAD_INSERT
:
1130 case WXK_WINDOWS_LEFT
:
1139 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1140 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1142 if (!ProcessBackKey(event
, flags
))
1151 // all the other keys modify the controls contents which shouldn't be
1152 // possible if we're read-only
1153 if ( !IsEditable() )
1159 if (event
.GetKeyCode() == WXK_RETURN
)
1161 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1164 long newPos
= m_caretPosition
;
1166 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1171 BeginBatchUndo(_("Insert Text"));
1173 DeleteSelectedContent(& newPos
);
1175 if (event
.ShiftDown())
1178 text
= wxRichTextLineBreakChar
;
1179 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1180 m_caretAtLineStart
= true;
1184 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1186 // Automatically renumber list
1187 bool isNumberedList
= false;
1188 wxRichTextRange numberedListRange
= FindRangeForList(newPos
+1, isNumberedList
);
1189 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1191 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1195 SetDefaultStyleToCursorStyle();
1197 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1199 wxRichTextEvent
cmdEvent(
1200 wxEVT_COMMAND_RICHTEXT_RETURN
,
1202 cmdEvent
.SetEventObject(this);
1203 cmdEvent
.SetFlags(flags
);
1204 cmdEvent
.SetPosition(newPos
+1);
1205 cmdEvent
.SetContainer(GetFocusObject());
1207 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1209 // Generate conventional event
1210 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1211 InitCommandEvent(textEvent
);
1213 GetEventHandler()->ProcessEvent(textEvent
);
1217 else if (event
.GetKeyCode() == WXK_BACK
)
1219 ProcessBackKey(event
, flags
);
1221 else if (event
.GetKeyCode() == WXK_DELETE
)
1223 long newPos
= m_caretPosition
;
1225 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1230 BeginBatchUndo(_("Delete Text"));
1232 bool processed
= DeleteSelectedContent(& newPos
);
1238 // Submit range in character positions, which are greater than caret positions,
1239 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1241 if (event
.CmdDown())
1243 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1244 if (pos
!= -1 && (pos
> newPos
))
1246 wxRichTextRange
range(newPos
+1, pos
);
1247 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1249 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1256 if (!processed
&& newPos
< (GetLastPosition()-1))
1258 wxRichTextRange
range(newPos
+1, newPos
+1);
1259 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1261 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1269 if (GetLastPosition() == -1)
1271 GetFocusObject()->Reset();
1273 m_caretPosition
= -1;
1275 SetDefaultStyleToCursorStyle();
1278 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1280 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1282 wxRichTextEvent
cmdEvent(
1283 wxEVT_COMMAND_RICHTEXT_DELETE
,
1285 cmdEvent
.SetEventObject(this);
1286 cmdEvent
.SetFlags(flags
);
1287 cmdEvent
.SetPosition(m_caretPosition
+1);
1288 cmdEvent
.SetContainer(GetFocusObject());
1289 GetEventHandler()->ProcessEvent(cmdEvent
);
1296 long keycode
= event
.GetKeyCode();
1308 if (event
.CmdDown())
1310 // Fixes AltGr+key with European input languages on Windows
1311 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1318 wxRichTextEvent
cmdEvent(
1319 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1321 cmdEvent
.SetEventObject(this);
1322 cmdEvent
.SetFlags(flags
);
1324 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1326 cmdEvent
.SetCharacter((wxChar
) keycode
);
1328 cmdEvent
.SetPosition(m_caretPosition
+1);
1329 cmdEvent
.SetContainer(GetFocusObject());
1331 if (keycode
== wxT('\t'))
1333 // See if we need to promote or demote the selection or paragraph at the cursor
1334 // position, instead of inserting a tab.
1335 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1336 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1337 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1339 wxRichTextRange range
;
1341 range
= GetSelectionRange();
1343 range
= para
->GetRange().FromInternal();
1345 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1347 PromoteList(promoteBy
, range
, NULL
);
1349 GetEventHandler()->ProcessEvent(cmdEvent
);
1355 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1358 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1361 BeginBatchUndo(_("Insert Text"));
1363 long newPos
= m_caretPosition
;
1364 DeleteSelectedContent(& newPos
);
1367 wxString str
= event
.GetUnicodeKey();
1369 wxString str
= (wxChar
) event
.GetKeyCode();
1371 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1375 SetDefaultStyleToCursorStyle();
1376 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1378 cmdEvent
.SetPosition(m_caretPosition
);
1379 GetEventHandler()->ProcessEvent(cmdEvent
);
1387 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1389 wxRichTextAttr attr
;
1390 if (container
&& GetStyle(position
, attr
, container
))
1392 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1394 SetCursor(m_urlCursor
);
1396 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1398 SetCursor(m_textCursor
);
1406 // Processes the back key
1407 bool wxRichTextCtrl::ProcessBackKey(wxKeyEvent
& event
, int flags
)
1414 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1419 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1421 // If we're at the start of a list item with a bullet, let's 'delete' the bullet, i.e.
1422 // make it a continuation paragraph.
1423 if (!HasSelection() && para
&& ((m_caretPosition
+1) == para
->GetRange().GetStart()) &&
1424 para
->GetAttributes().HasBulletStyle() && (para
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
) == 0)
1426 wxRichTextParagraph
* newPara
= wxDynamicCast(para
->Clone(), wxRichTextParagraph
);
1427 newPara
->GetAttributes().SetBulletStyle(newPara
->GetAttributes().GetBulletStyle() | wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
);
1429 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Remove Bullet"), wxRICHTEXT_CHANGE_STYLE
, & GetBuffer(), GetFocusObject(), this);
1430 action
->SetRange(newPara
->GetRange());
1431 action
->SetPosition(GetCaretPosition());
1432 action
->GetNewParagraphs().AppendChild(newPara
);
1433 // Also store the old ones for Undo
1434 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1436 GetBuffer().Invalidate(para
->GetRange());
1437 GetBuffer().SubmitAction(action
);
1439 // Automatically renumber list
1440 bool isNumberedList
= false;
1441 wxRichTextRange numberedListRange
= FindRangeForList(m_caretPosition
, isNumberedList
);
1442 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1444 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1451 BeginBatchUndo(_("Delete Text"));
1453 long newPos
= m_caretPosition
;
1455 bool processed
= DeleteSelectedContent(& newPos
);
1461 // Submit range in character positions, which are greater than caret positions,
1462 // so subtract 1 for deleted character and add 1 for conversion to character position.
1465 if (event
.CmdDown())
1467 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1470 wxRichTextRange
range(pos
+1, newPos
);
1471 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1473 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1482 wxRichTextRange
range(newPos
, newPos
);
1483 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1485 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1493 if (GetLastPosition() == -1)
1495 GetFocusObject()->Reset();
1497 m_caretPosition
= -1;
1499 SetDefaultStyleToCursorStyle();
1502 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1504 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1506 wxRichTextEvent
cmdEvent(
1507 wxEVT_COMMAND_RICHTEXT_DELETE
,
1509 cmdEvent
.SetEventObject(this);
1510 cmdEvent
.SetFlags(flags
);
1511 cmdEvent
.SetPosition(m_caretPosition
+1);
1512 cmdEvent
.SetContainer(GetFocusObject());
1513 GetEventHandler()->ProcessEvent(cmdEvent
);
1522 /// Delete content if there is a selection, e.g. when pressing a key.
1523 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1527 long pos
= m_selection
.GetRange().GetStart();
1528 wxRichTextRange range
= m_selection
.GetRange();
1530 // SelectAll causes more to be selected than doing it interactively,
1531 // and causes a new paragraph to be inserted. So for multiline buffers,
1532 // don't delete the final position.
1533 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1534 range
.SetEnd(range
.GetEnd()-1);
1536 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1537 m_selection
.Reset();
1538 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1548 /// Keyboard navigation
1552 Left: left one character
1553 Right: right one character
1556 Ctrl-Left: left one word
1557 Ctrl-Right: right one word
1558 Ctrl-Up: previous paragraph start
1559 Ctrl-Down: next start of paragraph
1562 Ctrl-Home: start of document
1563 Ctrl-End: end of document
1564 Page-Up: Up a screen
1565 Page-Down: Down a screen
1569 Ctrl-Alt-PgUp: Start of window
1570 Ctrl-Alt-PgDn: End of window
1571 F8: Start selection mode
1572 Esc: End selection mode
1574 Adding Shift does the above but starts/extends selection.
1579 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1581 bool success
= false;
1583 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1585 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1586 success
= WordRight(1, flags
);
1588 success
= MoveRight(1, flags
);
1590 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1592 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1593 success
= WordLeft(1, flags
);
1595 success
= MoveLeft(1, flags
);
1597 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1599 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1600 success
= MoveToParagraphStart(flags
);
1602 success
= MoveUp(1, flags
);
1604 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1606 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1607 success
= MoveToParagraphEnd(flags
);
1609 success
= MoveDown(1, flags
);
1611 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1613 success
= PageUp(1, flags
);
1615 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1617 success
= PageDown(1, flags
);
1619 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1621 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1622 success
= MoveHome(flags
);
1624 success
= MoveToLineStart(flags
);
1626 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1628 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1629 success
= MoveEnd(flags
);
1631 success
= MoveToLineEnd(flags
);
1636 ScrollIntoView(m_caretPosition
, keyCode
);
1637 SetDefaultStyleToCursorStyle();
1643 /// Extend the selection. Selections are in caret positions.
1644 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1646 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1648 if (oldPos
== newPos
)
1651 wxRichTextSelection oldSelection
= m_selection
;
1653 m_selection
.SetContainer(GetFocusObject());
1655 wxRichTextRange oldRange
;
1656 if (m_selection
.IsValid())
1657 oldRange
= m_selection
.GetRange();
1659 oldRange
= wxRICHTEXT_NO_SELECTION
;
1660 wxRichTextRange newRange
;
1662 // If not currently selecting, start selecting
1663 if (oldRange
.GetStart() == -2)
1665 m_selectionAnchor
= oldPos
;
1667 if (oldPos
> newPos
)
1668 newRange
.SetRange(newPos
+1, oldPos
);
1670 newRange
.SetRange(oldPos
+1, newPos
);
1674 // Always ensure that the selection range start is greater than
1676 if (newPos
> m_selectionAnchor
)
1677 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1678 else if (newPos
== m_selectionAnchor
)
1679 newRange
= wxRichTextRange(-2, -2);
1681 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1684 m_selection
.SetRange(newRange
);
1686 RefreshForSelectionChange(oldSelection
, m_selection
);
1688 if (newRange
.GetStart() > newRange
.GetEnd())
1690 wxLogDebug(wxT("Strange selection range"));
1699 /// Scroll into view, returning true if we scrolled.
1700 /// This takes a _caret_ position.
1701 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1703 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1709 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1711 int startXUnits
, startYUnits
;
1712 GetViewStart(& startXUnits
, & startYUnits
);
1713 int startY
= startYUnits
* ppuY
;
1716 GetVirtualSize(& sx
, & sy
);
1722 wxRect rect
= GetScaledRect(line
->GetRect());
1724 bool scrolled
= false;
1726 wxSize clientSize
= GetClientSize();
1728 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1731 wxClientDC
dc(this);
1732 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1733 topMargin
, bottomMargin
);
1735 clientSize
.y
-= (int) (0.5 + bottomMargin
* GetScale());
1737 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1739 int y
= rect
.y
- GetClientSize().y
/2;
1740 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1741 if (y
>= 0 && (y
+ clientSize
.y
) < (int) (0.5 + GetBuffer().GetCachedSize().y
* GetScale()))
1743 if (startYUnits
!= yUnits
)
1745 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1748 #if !wxRICHTEXT_USE_OWN_CARET
1758 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1759 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1760 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1761 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1763 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1765 // Make it scroll so this item is at the bottom
1767 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1768 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1770 // If we're still off the screen, scroll another line down
1771 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1774 if (startYUnits
!= yUnits
)
1776 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1780 else if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale())))
1782 // Make it scroll so this item is at the top
1784 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1785 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1787 if (startYUnits
!= yUnits
)
1789 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1795 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1796 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1797 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1798 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1800 if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale())))
1802 // Make it scroll so this item is at the top
1804 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1805 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1807 if (startYUnits
!= yUnits
)
1809 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1813 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1815 // Make it scroll so this item is at the bottom
1817 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1818 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1820 // If we're still off the screen, scroll another line down
1821 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1824 if (startYUnits
!= yUnits
)
1826 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1832 #if !wxRICHTEXT_USE_OWN_CARET
1840 /// Is the given position visible on the screen?
1841 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1843 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1849 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1852 GetViewStart(& startX
, & startY
);
1854 startY
= startY
* ppuY
;
1856 wxRect rect
= GetScaledRect(line
->GetRect());
1857 wxSize clientSize
= GetClientSize();
1858 clientSize
.y
-= (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale());
1860 return (rect
.GetTop() >= (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()))) &&
1861 (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1864 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1866 m_caretPosition
= position
;
1867 m_caretAtLineStart
= showAtLineStart
;
1870 /// Move caret one visual step forward: this may mean setting a flag
1871 /// and keeping the same position if we're going from the end of one line
1872 /// to the start of the next, which may be the exact same caret position.
1873 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1875 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1877 // Only do the check if we're not at the end of the paragraph (where things work OK
1879 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1881 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1885 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1887 // We're at the end of a line. See whether we need to
1888 // stay at the same actual caret position but change visual
1889 // position, or not.
1890 if (oldPosition
== lineRange
.GetEnd())
1892 if (m_caretAtLineStart
)
1894 // We're already at the start of the line, so actually move on now.
1895 m_caretPosition
= oldPosition
+ 1;
1896 m_caretAtLineStart
= false;
1900 // We're showing at the end of the line, so keep to
1901 // the same position but indicate that we're to show
1902 // at the start of the next line.
1903 m_caretPosition
= oldPosition
;
1904 m_caretAtLineStart
= true;
1906 SetDefaultStyleToCursorStyle();
1912 SetDefaultStyleToCursorStyle();
1915 /// Move caret one visual step backward: this may mean setting a flag
1916 /// and keeping the same position if we're going from the end of one line
1917 /// to the start of the next, which may be the exact same caret position.
1918 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1920 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1922 // Only do the check if we're not at the start of the paragraph (where things work OK
1924 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1926 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1930 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1932 // We're at the start of a line. See whether we need to
1933 // stay at the same actual caret position but change visual
1934 // position, or not.
1935 if (oldPosition
== lineRange
.GetStart())
1937 m_caretPosition
= oldPosition
-1;
1938 m_caretAtLineStart
= true;
1941 else if (oldPosition
== lineRange
.GetEnd())
1943 if (m_caretAtLineStart
)
1945 // We're at the start of the line, so keep the same caret position
1946 // but clear the start-of-line flag.
1947 m_caretPosition
= oldPosition
;
1948 m_caretAtLineStart
= false;
1952 // We're showing at the end of the line, so go back
1953 // to the previous character position.
1954 m_caretPosition
= oldPosition
- 1;
1956 SetDefaultStyleToCursorStyle();
1962 SetDefaultStyleToCursorStyle();
1966 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1968 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1970 if (m_caretPosition
+ noPositions
< endPos
)
1972 long oldPos
= m_caretPosition
;
1973 long newPos
= m_caretPosition
+ noPositions
;
1975 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1979 // Determine by looking at oldPos and m_caretPosition whether
1980 // we moved from the end of a line to the start of the next line, in which case
1981 // we want to adjust the caret position such that it is positioned at the
1982 // start of the next line, rather than jumping past the first character of the
1984 if (noPositions
== 1)
1985 MoveCaretForward(oldPos
);
1987 SetCaretPosition(newPos
);
1990 SetDefaultStyleToCursorStyle();
1999 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
2003 if (m_caretPosition
> startPos
- noPositions
+ 1)
2005 long oldPos
= m_caretPosition
;
2006 long newPos
= m_caretPosition
- noPositions
;
2007 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2011 if (noPositions
== 1)
2012 MoveCaretBack(oldPos
);
2014 SetCaretPosition(newPos
);
2017 SetDefaultStyleToCursorStyle();
2025 // Find the caret position for the combination of hit-test flags and character position.
2026 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2027 // since this is ambiguous (same position used for end of line and start of next).
2028 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2029 bool& caretLineStart
)
2031 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2032 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2033 // so we view the caret at the start of the line.
2034 caretLineStart
= false;
2035 long caretPosition
= position
;
2037 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2039 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2040 wxRichTextRange lineRange
;
2042 lineRange
= thisLine
->GetAbsoluteRange();
2044 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2047 caretLineStart
= true;
2051 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2052 if (para
&& para
->GetRange().GetStart() == position
)
2056 return caretPosition
;
2060 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2062 return MoveDown(- noLines
, flags
);
2066 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2071 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2072 wxPoint pt
= GetCaret()->GetPosition();
2073 long newLine
= lineNumber
+ noLines
;
2074 bool notInThisObject
= false;
2076 if (lineNumber
!= -1)
2080 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2081 if (newLine
> lastLine
)
2082 notInThisObject
= true;
2087 notInThisObject
= true;
2091 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2092 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
;
2094 bool lineIsEmpty
= false;
2095 if (notInThisObject
)
2097 // If we know we're navigating out of the current object,
2098 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2099 container
= & GetBuffer();
2100 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2102 if (noLines
> 0) // going down
2104 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2108 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2113 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2116 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2117 if (lineObj
->GetRange().GetStart() == lineObj
->GetRange().GetEnd())
2125 wxClientDC
dc(this);
2127 dc
.SetFont(GetFont());
2129 wxRichTextObject
* hitObj
= NULL
;
2130 wxRichTextObject
* contextObj
= NULL
;
2131 wxRichTextDrawingContext
context(& GetBuffer());
2132 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2135 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2136 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2139 if (notInThisObject
)
2141 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2142 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2144 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2146 container
= actualContainer
;
2150 bool caretLineStart
= true;
2152 // If the line is empty, there is only one possible position for the caret,
2153 // so force the 'before' state so FindCaretPositionForCharacterPosition doesn't
2154 // just return the same position.
2157 hitTest
&= ~wxRICHTEXT_HITTEST_AFTER
;
2158 hitTest
|= wxRICHTEXT_HITTEST_BEFORE
;
2160 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2161 long newSelEnd
= caretPosition
;
2164 if (notInThisObject
)
2167 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2172 SetCaretPosition(caretPosition
, caretLineStart
);
2174 SetDefaultStyleToCursorStyle();
2182 /// Move to the end of the paragraph
2183 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2185 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2188 long newPos
= para
->GetRange().GetEnd() - 1;
2189 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2193 SetCaretPosition(newPos
);
2195 SetDefaultStyleToCursorStyle();
2203 /// Move to the start of the paragraph
2204 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2206 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2209 long newPos
= para
->GetRange().GetStart() - 1;
2210 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2214 SetCaretPosition(newPos
, true);
2216 SetDefaultStyleToCursorStyle();
2224 /// Move to the end of the line
2225 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2227 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2231 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2232 long newPos
= lineRange
.GetEnd();
2233 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2237 SetCaretPosition(newPos
);
2239 SetDefaultStyleToCursorStyle();
2247 /// Move to the start of the line
2248 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2250 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2253 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2254 long newPos
= lineRange
.GetStart()-1;
2256 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2260 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2262 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2264 SetDefaultStyleToCursorStyle();
2272 /// Move to the start of the buffer
2273 bool wxRichTextCtrl::MoveHome(int flags
)
2275 if (m_caretPosition
!= -1)
2277 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2281 SetCaretPosition(-1);
2283 SetDefaultStyleToCursorStyle();
2291 /// Move to the end of the buffer
2292 bool wxRichTextCtrl::MoveEnd(int flags
)
2294 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2296 if (m_caretPosition
!= endPos
)
2298 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2302 SetCaretPosition(endPos
);
2304 SetDefaultStyleToCursorStyle();
2312 /// Move noPages pages up
2313 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2315 return PageDown(- noPages
, flags
);
2318 /// Move noPages pages down
2319 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2321 // Calculate which line occurs noPages * screen height further down.
2322 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2325 wxSize clientSize
= GetClientSize();
2326 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2328 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2331 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2332 long pos
= lineRange
.GetStart()-1;
2333 if (pos
!= m_caretPosition
)
2335 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2337 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2341 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2343 SetDefaultStyleToCursorStyle();
2353 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2355 return str
== wxT(" ") || str
== wxT("\t") || (!str
.empty() && (str
[0] == (wxChar
) 160));
2358 // Finds the caret position for the next word
2359 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2361 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2365 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2367 // First skip current text to space
2368 while (i
< endPos
&& i
> -1)
2370 // i is in character, not caret positions
2371 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2372 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2373 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2377 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2384 while (i
< endPos
&& i
> -1)
2386 // i is in character, not caret positions
2387 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2388 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2389 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2390 return wxMax(-1, i
);
2392 if (text
.empty()) // End of paragraph, or maybe an image
2393 return wxMax(-1, i
- 1);
2394 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2398 // Convert to caret position
2399 return wxMax(-1, i
- 1);
2408 long i
= m_caretPosition
;
2410 // First skip white space
2411 while (i
< endPos
&& i
> -1)
2413 // i is in character, not caret positions
2414 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2415 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2417 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2419 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2424 // Next skip current text to space
2425 while (i
< endPos
&& i
> -1)
2427 // i is in character, not caret positions
2428 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2429 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2430 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2433 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2446 /// Move n words left
2447 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2449 long pos
= FindNextWordPosition(-1);
2450 if (pos
!= m_caretPosition
)
2452 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2454 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2458 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2460 SetDefaultStyleToCursorStyle();
2468 /// Move n words right
2469 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2471 long pos
= FindNextWordPosition(1);
2472 if (pos
!= m_caretPosition
)
2474 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2476 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2480 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2482 SetDefaultStyleToCursorStyle();
2491 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2493 // Only do sizing optimization for large buffers
2494 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2496 m_fullLayoutRequired
= true;
2497 m_fullLayoutTime
= wxGetLocalTimeMillis();
2498 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2499 LayoutContent(true /* onlyVisibleRect */);
2502 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2504 #if wxRICHTEXT_BUFFERED_PAINTING
2511 // Force any pending layout due to large buffer
2512 void wxRichTextCtrl::ForceDelayedLayout()
2514 if (m_fullLayoutRequired
)
2516 m_fullLayoutRequired
= false;
2517 m_fullLayoutTime
= 0;
2518 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2519 ShowPosition(m_fullLayoutSavedPosition
);
2525 /// Idle-time processing
2526 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2528 #if wxRICHTEXT_USE_OWN_CARET
2529 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2531 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2537 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2539 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2541 m_fullLayoutRequired
= false;
2542 m_fullLayoutTime
= 0;
2543 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2544 ShowPosition(m_fullLayoutSavedPosition
);
2548 if (m_caretPositionForDefaultStyle
!= -2)
2550 // If the caret position has changed, no longer reflect the default style
2552 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2553 m_caretPositionForDefaultStyle
= -2;
2560 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2562 #if wxRICHTEXT_USE_OWN_CARET
2563 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2566 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2573 /// Set up scrollbars, e.g. after a resize
2574 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2579 if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled
)
2581 SetScrollbars(0, 0, 0, 0, 0, 0);
2585 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2586 // of pixels. See e.g. wxVScrolledWindow for ideas.
2587 int pixelsPerUnit
= 5;
2588 wxSize clientSize
= GetClientSize();
2590 int maxHeight
= (int) (0.5 + GetScale() * (GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin()));
2592 // Round up so we have at least maxHeight pixels
2593 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2595 int startX
= 0, startY
= 0;
2597 GetViewStart(& startX
, & startY
);
2599 int maxPositionX
= 0;
2600 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2602 int newStartX
= wxMin(maxPositionX
, startX
);
2603 int newStartY
= wxMin(maxPositionY
, startY
);
2605 int oldPPUX
, oldPPUY
;
2606 int oldStartX
, oldStartY
;
2607 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2608 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2609 GetViewStart(& oldStartX
, & oldStartY
);
2610 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2612 oldVirtualSizeY
/= oldPPUY
;
2614 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2617 // Don't set scrollbars if there were none before, and there will be none now.
2618 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2621 // Move to previous scroll position if
2623 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2626 /// Paint the background
2627 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2629 wxColour backgroundColour
= GetBackgroundColour();
2630 if (!backgroundColour
.IsOk())
2631 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2633 // Clear the background
2634 dc
.SetBrush(wxBrush(backgroundColour
));
2635 dc
.SetPen(*wxTRANSPARENT_PEN
);
2636 wxRect
windowRect(GetClientSize());
2637 windowRect
.x
-= 2; windowRect
.y
-= 2;
2638 windowRect
.width
+= 4; windowRect
.height
+= 4;
2640 // We need to shift the rectangle to take into account
2641 // scrolling. Converting device to logical coordinates.
2642 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2643 dc
.DrawRectangle(windowRect
);
2646 #if wxRICHTEXT_BUFFERED_PAINTING
2647 /// Recreate buffer bitmap if necessary
2648 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2651 if (sz
== wxDefaultSize
)
2652 sz
= GetClientSize();
2654 if (sz
.x
< 1 || sz
.y
< 1)
2657 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2658 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2659 return m_bufferBitmap
.IsOk();
2663 // ----------------------------------------------------------------------------
2664 // file IO functions
2665 // ----------------------------------------------------------------------------
2667 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2669 SetFocusObject(& GetBuffer(), true);
2671 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2673 m_filename
= filename
;
2676 SetInsertionPoint(0);
2679 SetupScrollbars(true);
2681 wxTextCtrl::SendTextUpdatedEvent(this);
2687 wxLogError(_("File couldn't be loaded."));
2693 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2695 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2697 m_filename
= filename
;
2704 wxLogError(_("The text couldn't be saved."));
2709 // ----------------------------------------------------------------------------
2710 // wxRichTextCtrl specific functionality
2711 // ----------------------------------------------------------------------------
2713 /// Add a new paragraph of text to the end of the buffer
2714 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2716 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2717 GetBuffer().Invalidate();
2723 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2725 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2726 GetBuffer().Invalidate();
2731 // ----------------------------------------------------------------------------
2732 // selection and ranges
2733 // ----------------------------------------------------------------------------
2736 void wxRichTextCtrl::SelectNone()
2738 if (m_selection
.IsValid())
2740 wxRichTextSelection oldSelection
= m_selection
;
2742 m_selection
.Reset();
2744 RefreshForSelectionChange(oldSelection
, m_selection
);
2746 m_selectionAnchor
= -2;
2747 m_selectionAnchorObject
= NULL
;
2748 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2751 static bool wxIsWordDelimiter(const wxString
& text
)
2753 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2756 /// Select the word at the given character position
2757 bool wxRichTextCtrl::SelectWord(long position
)
2759 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2762 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2766 if (position
== para
->GetRange().GetEnd())
2769 long positionStart
= position
;
2770 long positionEnd
= position
;
2772 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2774 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2775 if (wxIsWordDelimiter(text
))
2781 if (positionStart
< para
->GetRange().GetStart())
2782 positionStart
= para
->GetRange().GetStart();
2784 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2786 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2787 if (wxIsWordDelimiter(text
))
2793 if (positionEnd
>= para
->GetRange().GetEnd())
2794 positionEnd
= para
->GetRange().GetEnd();
2796 if (positionEnd
< positionStart
)
2799 SetSelection(positionStart
, positionEnd
+1);
2801 if (positionStart
>= 0)
2803 MoveCaret(positionStart
-1, true);
2804 SetDefaultStyleToCursorStyle();
2810 wxString
wxRichTextCtrl::GetStringSelection() const
2813 GetSelection(&from
, &to
);
2815 return GetRange(from
, to
);
2818 // ----------------------------------------------------------------------------
2820 // ----------------------------------------------------------------------------
2822 wxTextCtrlHitTestResult
2823 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2825 // implement in terms of the other overload as the native ports typically
2826 // can get the position and not (x, y) pair directly (although wxUniv
2827 // directly gets x and y -- and so overrides this method as well)
2829 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2831 if ( rc
!= wxTE_HT_UNKNOWN
)
2833 PositionToXY(pos
, x
, y
);
2839 wxTextCtrlHitTestResult
2840 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2843 wxClientDC
dc((wxRichTextCtrl
*) this);
2844 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2846 // Buffer uses logical position (relative to start of buffer)
2848 wxPoint pt2
= GetLogicalPoint(pt
);
2850 wxRichTextObject
* hitObj
= NULL
;
2851 wxRichTextObject
* contextObj
= NULL
;
2852 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2853 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2855 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2856 return wxTE_HT_BEFORE
;
2857 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2858 return wxTE_HT_BEYOND
;
2859 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2860 return wxTE_HT_ON_TEXT
;
2862 return wxTE_HT_UNKNOWN
;
2865 wxRichTextParagraphLayoutBox
*
2866 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2868 wxClientDC
dc(this);
2870 dc
.SetFont(GetFont());
2872 wxPoint logicalPt
= GetLogicalPoint(pt
);
2874 wxRichTextObject
* contextObj
= NULL
;
2875 wxRichTextDrawingContext
context(& GetBuffer());
2876 hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, &hitObj
, &contextObj
, flags
);
2877 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2883 // ----------------------------------------------------------------------------
2884 // set/get the controls text
2885 // ----------------------------------------------------------------------------
2887 wxString
wxRichTextCtrl::DoGetValue() const
2889 return GetBuffer().GetText();
2892 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2894 // Public API for range is different from internals
2895 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2898 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2900 // Don't call Clear here, since it always sends a text updated event
2901 m_buffer
.ResetAndClearCommands();
2902 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2903 m_caretPosition
= -1;
2904 m_caretPositionForDefaultStyle
= -2;
2905 m_caretAtLineStart
= false;
2906 m_selection
.Reset();
2907 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2917 if (!value
.IsEmpty())
2919 // Remove empty paragraph
2920 GetBuffer().Clear();
2921 DoWriteText(value
, flags
);
2923 // for compatibility, don't move the cursor when doing SetValue()
2924 SetInsertionPoint(0);
2928 // still send an event for consistency
2929 if (flags
& SetValue_SendEvent
)
2930 wxTextCtrl::SendTextUpdatedEvent(this);
2935 void wxRichTextCtrl::WriteText(const wxString
& value
)
2940 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2942 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2944 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2945 wxRichTextDrawingContext
context(& GetBuffer());
2946 GetBuffer().Defragment(context
);
2948 if ( flags
& SetValue_SendEvent
)
2949 wxTextCtrl::SendTextUpdatedEvent(this);
2952 void wxRichTextCtrl::AppendText(const wxString
& text
)
2954 SetInsertionPointEnd();
2959 /// Write an image at the current insertion point
2960 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2962 wxRichTextImageBlock imageBlock
;
2964 wxImage image2
= image
;
2965 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2966 return WriteImage(imageBlock
, textAttr
);
2971 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2973 wxRichTextImageBlock imageBlock
;
2976 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2977 return WriteImage(imageBlock
, textAttr
);
2982 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2984 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2987 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2991 wxRichTextImageBlock imageBlock
;
2993 wxImage image
= bitmap
.ConvertToImage();
2994 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2995 return WriteImage(imageBlock
, textAttr
);
3001 // Write a text box at the current insertion point.
3002 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
3004 wxRichTextBox
* textBox
= new wxRichTextBox
;
3005 textBox
->SetAttributes(textAttr
);
3006 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3007 textBox
->AddParagraph(wxEmptyString
);
3008 textBox
->SetParent(NULL
);
3010 // The object returned is the one actually inserted into the buffer,
3011 // while the original one is deleted.
3012 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3013 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
3017 wxRichTextField
* wxRichTextCtrl::WriteField(const wxString
& fieldType
, const wxRichTextProperties
& properties
,
3018 const wxRichTextAttr
& textAttr
)
3020 return GetFocusObject()->InsertFieldWithUndo(& GetBuffer(), m_caretPosition
+1, fieldType
, properties
,
3021 this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
, textAttr
);
3024 // Write a table at the current insertion point, returning the table.
3025 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3027 wxASSERT(rows
> 0 && cols
> 0);
3029 if (rows
<= 0 || cols
<= 0)
3032 wxRichTextTable
* table
= new wxRichTextTable
;
3033 table
->SetAttributes(tableAttr
);
3034 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3036 table
->CreateTable(rows
, cols
);
3038 table
->SetParent(NULL
);
3041 for (j
= 0; j
< rows
; j
++)
3043 for (i
= 0; i
< cols
; i
++)
3045 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
3049 // The object returned is the one actually inserted into the buffer,
3050 // while the original one is deleted.
3051 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3052 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3057 /// Insert a newline (actually paragraph) at the current insertion point.
3058 bool wxRichTextCtrl::Newline()
3060 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3063 /// Insert a line break at the current insertion point.
3064 bool wxRichTextCtrl::LineBreak()
3067 text
= wxRichTextLineBreakChar
;
3068 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3071 // ----------------------------------------------------------------------------
3072 // Clipboard operations
3073 // ----------------------------------------------------------------------------
3075 void wxRichTextCtrl::Copy()
3079 wxRichTextRange range
= GetInternalSelectionRange();
3080 GetBuffer().CopyToClipboard(range
);
3084 void wxRichTextCtrl::Cut()
3088 wxRichTextRange range
= GetInternalSelectionRange();
3089 GetBuffer().CopyToClipboard(range
);
3091 DeleteSelectedContent();
3097 void wxRichTextCtrl::Paste()
3101 BeginBatchUndo(_("Paste"));
3103 long newPos
= m_caretPosition
;
3104 DeleteSelectedContent(& newPos
);
3106 GetBuffer().PasteFromClipboard(newPos
);
3112 void wxRichTextCtrl::DeleteSelection()
3114 if (CanDeleteSelection())
3116 DeleteSelectedContent();
3120 bool wxRichTextCtrl::HasSelection() const
3122 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3125 bool wxRichTextCtrl::HasUnfocusedSelection() const
3127 return m_selection
.IsValid();
3130 bool wxRichTextCtrl::CanCopy() const
3132 // Can copy if there's a selection
3133 return HasSelection();
3136 bool wxRichTextCtrl::CanCut() const
3138 return CanDeleteSelection();
3141 bool wxRichTextCtrl::CanPaste() const
3143 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3146 return GetBuffer().CanPasteFromClipboard();
3149 bool wxRichTextCtrl::CanDeleteSelection() const
3151 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3155 // ----------------------------------------------------------------------------
3157 // ----------------------------------------------------------------------------
3159 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3161 if (m_contextMenu
&& m_contextMenu
!= menu
)
3162 delete m_contextMenu
;
3163 m_contextMenu
= menu
;
3166 void wxRichTextCtrl::SetEditable(bool editable
)
3168 m_editable
= editable
;
3171 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3175 m_caretPosition
= pos
- 1;
3176 m_caretAtLineStart
= true;
3180 SetDefaultStyleToCursorStyle();
3183 void wxRichTextCtrl::SetInsertionPointEnd()
3185 long pos
= GetLastPosition();
3186 SetInsertionPoint(pos
);
3189 long wxRichTextCtrl::GetInsertionPoint() const
3191 return m_caretPosition
+1;
3194 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3196 return GetFocusObject()->GetOwnRange().GetEnd();
3199 // If the return values from and to are the same, there is no
3201 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3203 if (m_selection
.IsValid())
3205 *from
= m_selection
.GetRange().GetStart();
3206 *to
= m_selection
.GetRange().GetEnd();
3216 bool wxRichTextCtrl::IsEditable() const
3221 // ----------------------------------------------------------------------------
3223 // ----------------------------------------------------------------------------
3225 void wxRichTextCtrl::SetSelection(long from
, long to
)
3227 // if from and to are both -1, it means (in wxWidgets) that all text should
3229 if ( (from
== -1) && (to
== -1) )
3232 to
= GetLastPosition()+1;
3241 wxRichTextSelection oldSelection
= m_selection
;
3243 m_selectionAnchor
= from
-1;
3244 m_selectionAnchorObject
= NULL
;
3245 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3247 m_caretPosition
= wxMax(-1, to
-1);
3249 RefreshForSelectionChange(oldSelection
, m_selection
);
3254 // ----------------------------------------------------------------------------
3256 // ----------------------------------------------------------------------------
3258 void wxRichTextCtrl::Replace(long from
, long to
,
3259 const wxString
& value
)
3261 BeginBatchUndo(_("Replace"));
3263 SetSelection(from
, to
);
3265 wxRichTextAttr
attr(GetDefaultStyle());
3267 DeleteSelectedContent();
3269 SetDefaultStyle(attr
);
3271 if (!value
.IsEmpty())
3272 DoWriteText(value
, SetValue_SelectionOnly
);
3277 void wxRichTextCtrl::Remove(long from
, long to
)
3281 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3288 bool wxRichTextCtrl::IsModified() const
3290 return m_buffer
.IsModified();
3293 void wxRichTextCtrl::MarkDirty()
3295 m_buffer
.Modify(true);
3298 void wxRichTextCtrl::DiscardEdits()
3300 m_caretPositionForDefaultStyle
= -2;
3301 m_buffer
.Modify(false);
3302 m_buffer
.GetCommandProcessor()->ClearCommands();
3305 int wxRichTextCtrl::GetNumberOfLines() const
3307 return GetFocusObject()->GetParagraphCount();
3310 // ----------------------------------------------------------------------------
3311 // Positions <-> coords
3312 // ----------------------------------------------------------------------------
3314 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3316 return GetFocusObject()->XYToPosition(x
, y
);
3319 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3321 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3324 // ----------------------------------------------------------------------------
3326 // ----------------------------------------------------------------------------
3328 void wxRichTextCtrl::ShowPosition(long pos
)
3330 if (!IsPositionVisible(pos
))
3331 ScrollIntoView(pos
-1, WXK_DOWN
);
3334 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3336 return GetFocusObject()->GetParagraphLength(lineNo
);
3339 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3341 return GetFocusObject()->GetParagraphText(lineNo
);
3344 // ----------------------------------------------------------------------------
3346 // ----------------------------------------------------------------------------
3348 void wxRichTextCtrl::Undo()
3352 GetCommandProcessor()->Undo();
3356 void wxRichTextCtrl::Redo()
3360 GetCommandProcessor()->Redo();
3364 bool wxRichTextCtrl::CanUndo() const
3366 return GetCommandProcessor()->CanUndo() && IsEditable();
3369 bool wxRichTextCtrl::CanRedo() const
3371 return GetCommandProcessor()->CanRedo() && IsEditable();
3374 // ----------------------------------------------------------------------------
3375 // implementation details
3376 // ----------------------------------------------------------------------------
3378 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3380 SetValue(event
.GetString());
3381 GetEventHandler()->ProcessEvent(event
);
3384 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3386 // By default, load the first file into the text window.
3387 if (event
.GetNumberOfFiles() > 0)
3389 LoadFile(event
.GetFiles()[0]);
3393 wxSize
wxRichTextCtrl::DoGetBestSize() const
3395 return wxSize(10, 10);
3398 // ----------------------------------------------------------------------------
3399 // standard handlers for standard edit menu events
3400 // ----------------------------------------------------------------------------
3402 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3407 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3412 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3417 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3422 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3427 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3432 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3434 event
.Enable( CanCut() );
3437 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3439 event
.Enable( CanCopy() );
3442 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3444 event
.Enable( CanDeleteSelection() );
3447 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3449 event
.Enable( CanPaste() );
3452 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3454 event
.Enable( CanUndo() );
3455 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3458 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3460 event
.Enable( CanRedo() );
3461 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3464 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3466 if (GetLastPosition() > 0)
3470 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3472 event
.Enable(GetLastPosition() > 0);
3475 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3477 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3478 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3480 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3481 if (obj
&& CanEditProperties(obj
))
3482 EditProperties(obj
, this);
3484 m_contextMenuPropertiesInfo
.Clear();
3488 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3490 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3491 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3494 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3496 if (event
.GetEventObject() != this)
3502 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3505 // Prepares the context menu, adding appropriate property-editing commands.
3506 // Returns the number of property commands added.
3507 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3509 wxClientDC
dc(this);
3511 dc
.SetFont(GetFont());
3513 m_contextMenuPropertiesInfo
.Clear();
3516 wxRichTextObject
* hitObj
= NULL
;
3517 wxRichTextObject
* contextObj
= NULL
;
3518 if (pt
!= wxDefaultPosition
)
3520 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3521 wxRichTextDrawingContext
context(& GetBuffer());
3522 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
);
3524 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3526 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3527 if (hitObj
&& actualContainer
)
3529 if (actualContainer
->AcceptsFocus())
3531 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3532 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3535 if (addPropertyCommands
)
3536 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3540 if (addPropertyCommands
)
3541 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3546 if (addPropertyCommands
)
3547 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3552 // Invoked from the keyboard, so don't set the caret position and don't use the event
3554 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3556 contextObj
= hitObj
->GetParentContainer();
3558 contextObj
= GetFocusObject();
3560 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3561 if (hitObj
&& actualContainer
)
3563 if (addPropertyCommands
)
3564 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3568 if (addPropertyCommands
)
3569 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3575 if (addPropertyCommands
)
3576 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3577 return m_contextMenuPropertiesInfo
.GetCount();
3583 // Shows the context menu, adding appropriate property-editing commands
3584 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3588 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3596 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3598 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3601 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3603 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3606 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3608 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3611 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3613 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3616 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
, int flags
)
3618 GetFocusObject()->SetStyle(obj
, textAttr
, flags
);
3621 // extended style setting operation with flags including:
3622 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3623 // see richtextbuffer.h for more details.
3625 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3627 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3630 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3632 return GetBuffer().SetDefaultStyle(style
);
3635 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3637 wxRichTextAttr
attr1(style
);
3638 attr1
.GetTextBoxAttr().Reset();
3639 return GetBuffer().SetDefaultStyle(attr1
);
3642 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3644 return GetBuffer().GetDefaultStyle();
3647 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3649 wxRichTextAttr attr
;
3650 if (GetFocusObject()->GetStyle(position
, attr
))
3659 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3661 return GetFocusObject()->GetStyle(position
, style
);
3664 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3666 wxRichTextAttr attr
;
3667 if (container
->GetStyle(position
, attr
))
3676 // get the common set of styles for the range
3677 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3679 wxRichTextAttr attr
;
3680 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3689 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3691 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3694 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3696 return container
->GetStyleForRange(range
.ToInternal(), style
);
3699 /// Get the content (uncombined) attributes for this position.
3700 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3702 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3705 /// Get the content (uncombined) attributes for this position.
3706 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3708 return container
->GetUncombinedStyle(position
, style
);
3711 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3713 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3716 /// Set font, and also the buffer attributes
3717 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3719 wxControl::SetFont(font
);
3721 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3723 GetBuffer().SetBasicStyle(attr
);
3725 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3731 /// Transform logical to physical
3732 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3735 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3740 /// Transform physical to logical
3741 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3744 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3749 /// Position the caret
3750 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3755 //wxLogDebug(wxT("PositionCaret"));
3758 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3760 caretRect
= GetScaledRect(caretRect
);
3761 int topMargin
= (int) (0.5 + GetScale()*GetBuffer().GetTopMargin());
3762 int bottomMargin
= (int) (0.5 + GetScale()*GetBuffer().GetBottomMargin());
3763 wxPoint newPt
= caretRect
.GetPosition();
3764 wxSize newSz
= caretRect
.GetSize();
3765 wxPoint pt
= GetPhysicalPoint(newPt
);
3766 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3769 if (GetCaret()->GetSize() != newSz
)
3770 GetCaret()->SetSize(newSz
);
3772 // Adjust size so the caret size and position doesn't appear in the margins
3773 if (((pt
.y
+ newSz
.y
) <= topMargin
) || (pt
.y
>= (GetClientSize().y
- bottomMargin
)))
3778 else if (pt
.y
< topMargin
&& (pt
.y
+ newSz
.y
) > topMargin
)
3780 newSz
.y
-= (topMargin
- pt
.y
);
3784 GetCaret()->SetSize(newSz
);
3787 else if (pt
.y
< (GetClientSize().y
- bottomMargin
) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- bottomMargin
))
3789 newSz
.y
= GetClientSize().y
- bottomMargin
- pt
.y
;
3790 GetCaret()->SetSize(newSz
);
3793 GetCaret()->Move(pt
);
3799 /// Get the caret height and position for the given character position
3800 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3802 wxClientDC
dc(this);
3804 dc
.SetUserScale(GetScale(), GetScale());
3805 dc
.SetFont(GetFont());
3811 container
= GetFocusObject();
3813 wxRichTextDrawingContext
context(& GetBuffer());
3814 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3816 // Caret height can't be zero
3818 height
= dc
.GetCharHeight();
3820 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3827 /// Gets the line for the visible caret position. If the caret is
3828 /// shown at the very end of the line, it means the next character is actually
3829 /// on the following line. So let's get the line we're expecting to find
3830 /// if this is the case.
3831 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3833 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3834 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3837 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3838 if (caretPosition
== lineRange
.GetStart()-1 &&
3839 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3841 if (!m_caretAtLineStart
)
3842 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3849 /// Move the caret to the given character position
3850 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3852 if (GetBuffer().IsDirty())
3856 container
= GetFocusObject();
3858 if (pos
<= container
->GetOwnRange().GetEnd())
3860 SetCaretPosition(pos
, showAtLineStart
);
3862 PositionCaret(container
);
3870 /// Layout the buffer: which we must do before certain operations, such as
3871 /// setting the caret position.
3872 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3874 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3876 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
3877 if (availableSpace
.width
== 0)
3878 availableSpace
.width
= 10;
3879 if (availableSpace
.height
== 0)
3880 availableSpace
.height
= 10;
3882 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3883 if (onlyVisibleRect
)
3885 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3886 availableSpace
.SetPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))));
3889 wxClientDC
dc(this);
3892 dc
.SetFont(GetFont());
3893 dc
.SetUserScale(GetScale(), GetScale());
3895 wxRichTextDrawingContext
context(& GetBuffer());
3896 GetBuffer().Defragment(context
);
3897 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3898 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3899 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3901 dc
.SetUserScale(1.0, 1.0);
3903 if (!IsFrozen() && !onlyVisibleRect
)
3910 /// Is all of the selection, or the current caret position, bold?
3911 bool wxRichTextCtrl::IsSelectionBold()
3915 wxRichTextAttr attr
;
3916 wxRichTextRange range
= GetSelectionRange();
3917 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3918 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3920 return HasCharacterAttributes(range
, attr
);
3924 // If no selection, then we need to combine current style with default style
3925 // to see what the effect would be if we started typing.
3926 wxRichTextAttr attr
;
3927 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3929 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3930 if (GetStyle(pos
, attr
))
3932 if (IsDefaultStyleShowing())
3933 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3934 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3940 /// Is all of the selection, or the current caret position, italics?
3941 bool wxRichTextCtrl::IsSelectionItalics()
3945 wxRichTextRange range
= GetSelectionRange();
3946 wxRichTextAttr attr
;
3947 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3948 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3950 return HasCharacterAttributes(range
, attr
);
3954 // If no selection, then we need to combine current style with default style
3955 // to see what the effect would be if we started typing.
3956 wxRichTextAttr attr
;
3957 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3959 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3960 if (GetStyle(pos
, attr
))
3962 if (IsDefaultStyleShowing())
3963 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3964 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3970 /// Is all of the selection, or the current caret position, underlined?
3971 bool wxRichTextCtrl::IsSelectionUnderlined()
3975 wxRichTextRange range
= GetSelectionRange();
3976 wxRichTextAttr attr
;
3977 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3978 attr
.SetFontUnderlined(true);
3980 return HasCharacterAttributes(range
, attr
);
3984 // If no selection, then we need to combine current style with default style
3985 // to see what the effect would be if we started typing.
3986 wxRichTextAttr attr
;
3987 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3988 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3990 if (GetStyle(pos
, attr
))
3992 if (IsDefaultStyleShowing())
3993 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3994 return attr
.GetFontUnderlined();
4000 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
4001 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
4003 wxRichTextAttr attr
;
4004 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4005 attr
.SetTextEffectFlags(flag
);
4006 attr
.SetTextEffects(flag
);
4010 return HasCharacterAttributes(GetSelectionRange(), attr
);
4014 // If no selection, then we need to combine current style with default style
4015 // to see what the effect would be if we started typing.
4016 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4017 if (GetStyle(pos
, attr
))
4019 if (IsDefaultStyleShowing())
4020 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
4021 return (attr
.GetTextEffectFlags() & flag
) != 0;
4027 /// Apply bold to the selection
4028 bool wxRichTextCtrl::ApplyBoldToSelection()
4030 wxRichTextAttr attr
;
4031 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
4032 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4035 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4038 wxRichTextAttr current
= GetDefaultStyleEx();
4039 current
.Apply(attr
);
4040 SetAndShowDefaultStyle(current
);
4045 /// Apply italic to the selection
4046 bool wxRichTextCtrl::ApplyItalicToSelection()
4048 wxRichTextAttr attr
;
4049 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4050 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4053 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4056 wxRichTextAttr current
= GetDefaultStyleEx();
4057 current
.Apply(attr
);
4058 SetAndShowDefaultStyle(current
);
4063 /// Apply underline to the selection
4064 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4066 wxRichTextAttr attr
;
4067 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4068 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4071 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4074 wxRichTextAttr current
= GetDefaultStyleEx();
4075 current
.Apply(attr
);
4076 SetAndShowDefaultStyle(current
);
4081 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4082 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4084 wxRichTextAttr attr
;
4085 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4086 attr
.SetTextEffectFlags(flags
);
4087 if (!DoesSelectionHaveTextEffectFlag(flags
))
4088 attr
.SetTextEffects(flags
);
4090 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4093 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4096 wxRichTextAttr current
= GetDefaultStyleEx();
4097 current
.Apply(attr
);
4098 SetAndShowDefaultStyle(current
);
4103 /// Is all of the selection aligned according to the specified flag?
4104 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4106 wxRichTextRange range
;
4108 range
= GetSelectionRange();
4110 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4112 wxRichTextAttr attr
;
4113 attr
.SetAlignment(alignment
);
4115 return HasParagraphAttributes(range
, attr
);
4118 /// Apply alignment to the selection
4119 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4121 wxRichTextAttr attr
;
4122 attr
.SetAlignment(alignment
);
4124 return SetStyle(GetSelectionRange(), attr
);
4127 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4129 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4134 /// Apply a named style to the selection
4135 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4137 // Flags are defined within each definition, so only certain
4138 // attributes are applied.
4139 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4141 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4143 if (wxDynamicCast(def
, wxRichTextListStyleDefinition
))
4145 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4147 wxRichTextRange range
;
4150 range
= GetSelectionRange();
4153 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4154 range
= wxRichTextRange(pos
, pos
+1);
4157 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4160 bool isPara
= false;
4162 // Make sure the attr has the style name
4163 if (wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
))
4166 attr
.SetParagraphStyleName(def
->GetName());
4168 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4169 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4170 // to change its style independently.
4171 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4173 else if (wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
))
4174 attr
.SetCharacterStyleName(def
->GetName());
4175 else if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4176 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4178 if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4180 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4182 SetStyle(GetFocusObject(), attr
);
4188 else if (HasSelection())
4189 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4192 wxRichTextAttr current
= GetDefaultStyleEx();
4193 wxRichTextAttr
defaultStyle(attr
);
4196 // Don't apply extra character styles since they are already implied
4197 // in the paragraph style
4198 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4200 current
.Apply(defaultStyle
);
4201 SetAndShowDefaultStyle(current
);
4203 // If it's a paragraph style, we want to apply the style to the
4204 // current paragraph even if we didn't select any text.
4207 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4208 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4211 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4218 /// Apply the style sheet to the buffer, for example if the styles have changed.
4219 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4222 styleSheet
= GetBuffer().GetStyleSheet();
4226 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4228 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4236 /// Sets the default style to the style under the cursor
4237 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4239 wxRichTextAttr attr
;
4240 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4242 // If at the start of a paragraph, use the next position.
4243 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4245 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4246 if (obj
&& obj
->IsTopLevel())
4248 // Don't use the attributes of a top-level object, since they might apply
4249 // to content of the object, e.g. background colour.
4250 SetDefaultStyle(wxRichTextAttr());
4253 else if (GetUncombinedStyle(pos
, attr
))
4255 SetDefaultStyle(attr
);
4262 /// Returns the first visible position in the current view
4263 long wxRichTextCtrl::GetFirstVisiblePosition() const
4265 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))).y
);
4267 return line
->GetAbsoluteRange().GetStart();
4272 /// Get the first visible point in the window
4273 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4276 int startXUnits
, startYUnits
;
4278 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4279 GetViewStart(& startXUnits
, & startYUnits
);
4281 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4284 /// The adjusted caret position is the character position adjusted to take
4285 /// into account whether we're at the start of a paragraph, in which case
4286 /// style information should be taken from the next position, not current one.
4287 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4289 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4291 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4296 /// Get/set the selection range in character positions. -1, -1 means no selection.
4297 /// The range is in API convention, i.e. a single character selection is denoted
4299 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4301 wxRichTextRange range
= GetInternalSelectionRange();
4302 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4303 range
.SetEnd(range
.GetEnd() + 1);
4307 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4309 SetSelection(range
.GetStart(), range
.GetEnd());
4313 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4315 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4318 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4320 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4323 /// Clear list for given range
4324 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4326 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4329 /// Number/renumber any list elements in the given range
4330 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4332 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4335 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4337 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4340 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4341 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4343 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4346 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4348 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4351 // Given a character position at which there is a list style, find the range
4352 // encompassing the same list style by looking backwards and forwards.
4353 wxRichTextRange
wxRichTextCtrl::FindRangeForList(long pos
, bool& isNumberedList
)
4355 wxRichTextParagraphLayoutBox
* focusObject
= GetFocusObject();
4356 wxRichTextRange range
= wxRichTextRange(-1, -1);
4357 wxRichTextParagraph
* para
= focusObject
->GetParagraphAtPosition(pos
);
4358 if (!para
|| !para
->GetAttributes().HasListStyleName())
4362 wxString listStyle
= para
->GetAttributes().GetListStyleName();
4363 range
= para
->GetRange();
4365 isNumberedList
= para
->GetAttributes().HasBulletNumber();
4368 wxRichTextObjectList::compatibility_iterator initialNode
= focusObject
->GetChildren().Find(para
);
4371 wxRichTextObjectList::compatibility_iterator startNode
= initialNode
->GetPrevious();
4374 wxRichTextParagraph
* p
= wxDynamicCast(startNode
->GetData(), wxRichTextParagraph
);
4377 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4380 range
.SetStart(p
->GetRange().GetStart());
4383 startNode
= startNode
->GetPrevious();
4387 wxRichTextObjectList::compatibility_iterator endNode
= initialNode
->GetNext();
4390 wxRichTextParagraph
* p
= wxDynamicCast(endNode
->GetData(), wxRichTextParagraph
);
4393 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4396 range
.SetEnd(p
->GetRange().GetEnd());
4399 endNode
= endNode
->GetNext();
4406 /// Deletes the content in the given range
4407 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4409 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4412 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4414 if (sm_availableFontNames
.GetCount() == 0)
4416 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4417 sm_availableFontNames
.Sort();
4419 return sm_availableFontNames
;
4422 void wxRichTextCtrl::ClearAvailableFontNames()
4424 sm_availableFontNames
.Clear();
4427 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4429 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4431 wxTextAttrEx basicStyle
= GetBasicStyle();
4432 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4433 SetBasicStyle(basicStyle
);
4434 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4439 // Refresh the area affected by a selection change
4440 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4442 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4443 // the selection contains whole containers rather than just text, so refresh everything
4444 // for now as it would be hard to compute the rectangle bounding all selections.
4445 // TODO: improve on this.
4446 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4447 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4453 wxRichTextRange oldRange
, newRange
;
4454 if (oldSelection
.IsValid())
4455 oldRange
= oldSelection
.GetRange();
4457 oldRange
= wxRICHTEXT_NO_SELECTION
;
4458 if (newSelection
.IsValid())
4459 newRange
= newSelection
.GetRange();
4461 newRange
= wxRICHTEXT_NO_SELECTION
;
4463 // Calculate the refresh rectangle - just the affected lines
4464 long firstPos
, lastPos
;
4465 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4467 firstPos
= newRange
.GetStart();
4468 lastPos
= newRange
.GetEnd();
4470 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4472 firstPos
= oldRange
.GetStart();
4473 lastPos
= oldRange
.GetEnd();
4475 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4481 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4482 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4485 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4486 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4488 if (firstLine
&& lastLine
)
4490 wxSize clientSize
= GetClientSize();
4491 wxPoint pt1
= GetPhysicalPoint(GetScaledPoint(firstLine
->GetAbsolutePosition()));
4492 wxPoint pt2
= GetPhysicalPoint(GetScaledPoint(lastLine
->GetAbsolutePosition())) + wxPoint(0, (int) (0.5 + lastLine
->GetSize().y
* GetScale()));
4495 pt1
.y
= wxMax(0, pt1
.y
);
4497 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4499 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4500 RefreshRect(rect
, false);
4508 // margins functions
4509 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4511 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4512 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4513 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4514 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4519 wxPoint
wxRichTextCtrl::DoGetMargins() const
4521 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4522 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4525 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4527 if (obj
&& !obj
->AcceptsFocus())
4530 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4531 bool changingContainer
= (m_focusObject
!= obj
);
4533 if (changingContainer
&& HasSelection())
4536 m_focusObject
= obj
;
4539 m_focusObject
= & m_buffer
;
4541 if (setCaretPosition
&& changingContainer
)
4543 m_selection
.Reset();
4544 m_selectionAnchor
= -2;
4545 m_selectionAnchorObject
= NULL
;
4546 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4550 m_caretAtLineStart
= false;
4551 MoveCaret(pos
, m_caretAtLineStart
);
4552 SetDefaultStyleToCursorStyle();
4554 wxRichTextEvent
cmdEvent(
4555 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4557 cmdEvent
.SetEventObject(this);
4558 cmdEvent
.SetPosition(m_caretPosition
+1);
4559 cmdEvent
.SetOldContainer(oldContainer
);
4560 cmdEvent
.SetContainer(m_focusObject
);
4562 GetEventHandler()->ProcessEvent(cmdEvent
);
4567 #if wxUSE_DRAG_AND_DROP
4568 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4572 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4577 if (!GetSelection().IsValid())
4582 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4583 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4585 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4588 long position
= GetCaretPosition();
4589 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4590 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4592 // It doesn't make sense to move onto itself
4596 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4597 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4598 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4599 if ((def
== wxDragMove
) && !DeleteAfter
)
4601 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4602 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4605 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4606 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4608 delete richTextBuffer
;
4612 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4613 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4620 #endif // wxUSE_DRAG_AND_DROP
4623 #if wxUSE_DRAG_AND_DROP
4624 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4626 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4630 wxRichTextObject
* hitObj
= NULL
;
4631 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->GetUnscaledPoint(m_rtc
->ScreenToClient(wxGetMousePosition())), position
, hit
, hitObj
);
4633 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4635 m_rtc
->StoreFocusObject(container
);
4636 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4639 return false; // so that the base-class sets a cursor
4641 #endif // wxUSE_DRAG_AND_DROP
4643 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4648 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4653 void wxRichTextCtrl::EnableVerticalScrollbar(bool enable
)
4655 m_verticalScrollbarEnabled
= enable
;
4659 void wxRichTextCtrl::SetFontScale(double fontScale
, bool refresh
)
4661 GetBuffer().SetFontScale(fontScale
);
4664 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4669 void wxRichTextCtrl::SetDimensionScale(double dimScale
, bool refresh
)
4671 GetBuffer().SetDimensionScale(dimScale
);
4674 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4679 // Sets an overall scale factor for displaying and editing the content.
4680 void wxRichTextCtrl::SetScale(double scale
, bool refresh
)
4685 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4690 // Get an unscaled point
4691 wxPoint
wxRichTextCtrl::GetUnscaledPoint(const wxPoint
& pt
) const
4693 if (GetScale() == 1.0)
4696 return wxPoint((int) (0.5 + double(pt
.x
) / GetScale()), (int) (0.5 + double(pt
.y
) / GetScale()));
4699 // Get a scaled point
4700 wxPoint
wxRichTextCtrl::GetScaledPoint(const wxPoint
& pt
) const
4702 if (GetScale() == 1.0)
4705 return wxPoint((int) (0.5 + double(pt
.x
) * GetScale()), (int) (0.5 + double(pt
.y
) * GetScale()));
4708 // Get an unscaled size
4709 wxSize
wxRichTextCtrl::GetUnscaledSize(const wxSize
& sz
) const
4711 if (GetScale() == 1.0)
4714 return wxSize((int) (0.5 + double(sz
.x
) / GetScale()), (int) (0.5 + double(sz
.y
) / GetScale()));
4717 // Get a scaled size
4718 wxSize
wxRichTextCtrl::GetScaledSize(const wxSize
& sz
) const
4720 if (GetScale() == 1.0)
4723 return wxSize((int) (0.5 + double(sz
.x
) * GetScale()), (int) (0.5 + double(sz
.y
) * GetScale()));
4726 // Get an unscaled rect
4727 wxRect
wxRichTextCtrl::GetUnscaledRect(const wxRect
& rect
) const
4729 if (GetScale() == 1.0)
4732 return wxRect((int) (0.5 + double(rect
.x
) / GetScale()), (int) (0.5 + double(rect
.y
) / GetScale()),
4733 (int) (0.5 + double(rect
.width
) / GetScale()), (int) (0.5 + double(rect
.height
) / GetScale()));
4736 // Get a scaled rect
4737 wxRect
wxRichTextCtrl::GetScaledRect(const wxRect
& rect
) const
4739 if (GetScale() == 1.0)
4742 return wxRect((int) (0.5 + double(rect
.x
) * GetScale()), (int) (0.5 + double(rect
.y
) * GetScale()),
4743 (int) (0.5 + double(rect
.width
) * GetScale()), (int) (0.5 + double(rect
.height
) * GetScale()));
4746 #if wxRICHTEXT_USE_OWN_CARET
4748 // ----------------------------------------------------------------------------
4749 // initialization and destruction
4750 // ----------------------------------------------------------------------------
4752 void wxRichTextCaret::Init()
4755 m_refreshEnabled
= true;
4759 m_richTextCtrl
= NULL
;
4760 m_needsUpdate
= false;
4764 wxRichTextCaret::~wxRichTextCaret()
4766 if (m_timer
.IsRunning())
4770 // ----------------------------------------------------------------------------
4771 // showing/hiding/moving the caret (base class interface)
4772 // ----------------------------------------------------------------------------
4774 void wxRichTextCaret::DoShow()
4778 if (!m_timer
.IsRunning())
4779 m_timer
.Start(GetBlinkTime());
4784 void wxRichTextCaret::DoHide()
4786 if (m_timer
.IsRunning())
4792 void wxRichTextCaret::DoMove()
4798 if (m_xOld
!= -1 && m_yOld
!= -1)
4800 if (m_richTextCtrl
&& m_refreshEnabled
)
4802 wxRect
rect(GetPosition(), GetSize());
4803 m_richTextCtrl
->RefreshRect(rect
, false);
4812 void wxRichTextCaret::DoSize()
4814 int countVisible
= m_countVisible
;
4815 if (countVisible
> 0)
4821 if (countVisible
> 0)
4823 m_countVisible
= countVisible
;
4828 // ----------------------------------------------------------------------------
4829 // handling the focus
4830 // ----------------------------------------------------------------------------
4832 void wxRichTextCaret::OnSetFocus()
4840 void wxRichTextCaret::OnKillFocus()
4845 // ----------------------------------------------------------------------------
4846 // drawing the caret
4847 // ----------------------------------------------------------------------------
4849 void wxRichTextCaret::Refresh()
4851 if (m_richTextCtrl
&& m_refreshEnabled
)
4853 wxRect
rect(GetPosition(), GetSize());
4854 m_richTextCtrl
->RefreshRect(rect
, false);
4858 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4860 dc
->SetPen( *wxBLACK_PEN
);
4862 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4863 dc
->SetPen(*wxBLACK_PEN
);
4865 wxPoint
pt(m_x
, m_y
);
4869 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4871 if (IsVisible() && m_flashOn
)
4872 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4875 void wxRichTextCaret::Notify()
4877 m_flashOn
= !m_flashOn
;
4881 void wxRichTextCaretTimer::Notify()
4886 // wxRICHTEXT_USE_OWN_CARET
4889 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4893 m_labels
.Add(label
);
4901 // Returns number of menu items were added.
4902 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4904 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4905 // If none of the standard properties identifiers are in the menu, add them if necessary.
4906 // If no items to add, just set the text to something generic
4907 if (GetCount() == 0)
4911 menu
->SetLabel(startCmd
, _("&Properties"));
4913 // Delete the others if necessary
4915 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4917 if (menu
->FindItem(i
))
4928 // Find the position of the first properties item
4929 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4931 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4932 if (item
&& item
->GetId() == startCmd
)
4941 int insertBefore
= pos
+1;
4942 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4944 if (menu
->FindItem(i
))
4946 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4950 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4951 menu
->Append(i
, m_labels
[i
- startCmd
]);
4953 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4958 // Delete any old items still left on the menu
4959 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4961 if (menu
->FindItem(i
))
4969 // No existing property identifiers were found, so append to the end of the menu.
4970 menu
->AppendSeparator();
4971 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4973 menu
->Append(i
, m_labels
[i
- startCmd
]);
4981 // Add appropriate menu items for the current container and clicked on object
4982 // (and container's parent, if appropriate).
4983 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4986 if (obj
&& ctrl
->CanEditProperties(obj
))
4987 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
4989 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
4990 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
4992 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
4993 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());