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_verticalScrollbarEnabled
= true;
359 m_caretAtLineStart
= false;
361 #if wxUSE_DRAG_AND_DROP
364 m_fullLayoutRequired
= false;
365 m_fullLayoutTime
= 0;
366 m_fullLayoutSavedPosition
= 0;
367 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
368 m_caretPositionForDefaultStyle
= -2;
369 m_focusObject
= & m_buffer
;
373 void wxRichTextCtrl::DoThaw()
375 if (GetBuffer().IsDirty())
384 void wxRichTextCtrl::Clear()
386 if (GetFocusObject() == & GetBuffer())
388 m_buffer
.ResetAndClearCommands();
389 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
393 GetFocusObject()->Reset();
396 m_caretPosition
= -1;
397 m_caretPositionForDefaultStyle
= -2;
398 m_caretAtLineStart
= false;
400 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
410 wxTextCtrl::SendTextUpdatedEvent(this);
414 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
416 #if !wxRICHTEXT_USE_OWN_CARET
417 if (GetCaret() && !IsFrozen())
420 // Stop the caret refreshing the control from within the
423 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(false);
427 #if wxRICHTEXT_BUFFERED_PAINTING
428 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
438 dc
.SetFont(GetFont());
440 // Paint the background
443 wxRect
drawingArea(GetUpdateRegion().GetBox());
444 drawingArea
.SetPosition(GetUnscaledPoint(GetLogicalPoint(drawingArea
.GetPosition())));
445 drawingArea
.SetSize(GetUnscaledSize(drawingArea
.GetSize()));
447 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
448 wxRichTextDrawingContext
context(& GetBuffer());
449 if (GetBuffer().IsDirty())
451 dc
.SetUserScale(GetScale(), GetScale());
453 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
454 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
456 dc
.SetUserScale(1.0, 1.0);
461 wxRect
clipRect(availableSpace
);
462 clipRect
.x
+= GetBuffer().GetLeftMargin();
463 clipRect
.y
+= GetBuffer().GetTopMargin();
464 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
465 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
467 clipRect
= GetScaledRect(clipRect
);
468 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
470 dc
.SetClippingRegion(clipRect
);
473 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
474 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
476 dc
.SetUserScale(GetScale(), GetScale());
478 GetBuffer().Draw(dc
, context
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
480 dc
.DestroyClippingRegion();
482 // Other user defined painting after everything else (i.e. all text) is painted
483 PaintAboveContent(dc
);
485 #if wxRICHTEXT_USE_OWN_CARET
486 if (GetCaret()->IsVisible())
489 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
493 dc
.SetUserScale(1.0, 1.0);
496 #if !wxRICHTEXT_USE_OWN_CARET
502 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(true);
506 // Empty implementation, to prevent flicker
507 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
511 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
515 #if !wxRICHTEXT_USE_OWN_CARET
521 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
522 // Work around dropouts when control is focused
530 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
535 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
536 // Work around dropouts when control is focused
544 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
549 // Set up the caret for the given position and container, after a mouse click
550 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
552 bool caretAtLineStart
= false;
554 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
556 // If we're at the start of a line (but not first in para)
557 // then we should keep the caret showing at the start of the line
558 // by showing the m_caretAtLineStart flag.
559 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
560 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
562 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
563 caretAtLineStart
= true;
567 if (extendSelection
&& (m_caretPosition
!= position
))
568 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
570 MoveCaret(position
, caretAtLineStart
);
571 SetDefaultStyleToCursorStyle();
577 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
583 dc
.SetFont(GetFont());
585 // TODO: detect change of focus object
587 wxRichTextObject
* hitObj
= NULL
;
588 wxRichTextObject
* contextObj
= NULL
;
589 wxRichTextDrawingContext
context(& GetBuffer());
590 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
592 #if wxUSE_DRAG_AND_DROP
593 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
594 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position
))
596 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
598 m_dragStartPoint
= event
.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
601 m_dragStartTime
= wxDateTime::UNow();
602 #endif // wxUSE_DATETIME
604 // Preserve behaviour of clicking on an object within the selection
605 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
608 return; // Don't skip the event, else the selection will be lost
610 #endif // wxUSE_DRAG_AND_DROP
612 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
614 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
615 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
616 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
618 SetFocusObject(container
, false /* don't set caret position yet */);
624 long oldCaretPos
= m_caretPosition
;
626 SetCaretPositionAfterClick(container
, position
, hit
);
628 // For now, don't handle shift-click when we're selecting multiple objects.
629 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
630 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
639 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
644 if (GetCapture() == this)
647 // See if we clicked on a URL
650 dc
.SetFont(GetFont());
653 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
654 wxRichTextObject
* hitObj
= NULL
;
655 wxRichTextObject
* contextObj
= NULL
;
656 wxRichTextDrawingContext
context(& GetBuffer());
657 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
658 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
660 #if wxUSE_DRAG_AND_DROP
663 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
664 m_preDrag
= false; // Tell DnD not to happen now: we are processing Left Up ourselves.
666 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
668 wxRichTextObject
* hitObj
= NULL
;
669 wxRichTextObject
* contextObj
= NULL
;
670 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(event
.GetLogicalPosition(dc
)), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
671 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
672 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
673 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
675 SetFocusObject(container
, false /* don't set caret position yet */);
678 long oldCaretPos
= m_caretPosition
;
680 SetCaretPositionAfterClick(container
, position
, hit
);
682 // For now, don't handle shift-click when we're selecting multiple objects.
683 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
684 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
690 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
692 wxRichTextEvent
cmdEvent(
693 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
695 cmdEvent
.SetEventObject(this);
696 cmdEvent
.SetPosition(position
);
698 cmdEvent
.SetContainer(hitObj
->GetContainer());
700 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
703 if (GetStyle(position
, attr
))
705 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
707 wxString urlTarget
= attr
.GetURL();
708 if (!urlTarget
.IsEmpty())
710 wxMouseEvent
mouseEvent(event
);
712 long startPos
= 0, endPos
= 0;
713 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
716 startPos
= obj
->GetRange().GetStart();
717 endPos
= obj
->GetRange().GetEnd();
720 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
721 InitCommandEvent(urlEvent
);
723 urlEvent
.SetString(urlTarget
);
725 GetEventHandler()->ProcessEvent(urlEvent
);
733 #if wxUSE_DRAG_AND_DROP
735 #endif // wxUSE_DRAG_AND_DROP
737 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
738 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
740 // Put the selection in PRIMARY, if it exists
741 wxTheClipboard
->UsePrimarySelection(true);
743 wxRichTextRange range
= GetInternalSelectionRange();
744 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
746 wxTheClipboard
->UsePrimarySelection(false);
752 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
754 #if wxUSE_DRAG_AND_DROP
756 if (m_preDrag
|| m_dragging
)
758 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
759 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
760 distance
= abs(x
) + abs(y
);
763 // See if we're starting Drag'n'Drop
767 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
771 && (diff
.GetMilliseconds() > 100)
778 wxRichTextRange range
= GetInternalSelectionRange();
779 if (range
== wxRICHTEXT_NONE
)
781 // Don't try to drag an empty range
786 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
787 long oldPos
= GetCaretPosition();
788 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
790 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
791 wxString text
= GetFocusObject()->GetTextForRange(range
);
793 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
795 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
797 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
798 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
799 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
801 wxRichTextDropSource
source(*compositeObject
, this);
802 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
803 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
804 BeginBatchUndo(_("Drag"));
805 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
808 case wxDragCopy
: break;
811 wxLogError(wxT("An error occurred during drag and drop operation"));
814 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
815 SetCaretPosition(oldPos
);
816 SetFocusObject(oldFocus
, false);
825 #endif // wxUSE_DRAG_AND_DROP
829 dc
.SetFont(GetFont());
832 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
833 wxRichTextObject
* hitObj
= NULL
;
834 wxRichTextObject
* contextObj
= NULL
;
838 // If we're dragging, let's only consider positions at this level; otherwise
839 // selecting a range is not going to work.
840 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
843 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
844 container
= GetFocusObject();
846 wxRichTextDrawingContext
context(& GetBuffer());
847 int hit
= container
->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, flags
);
849 // See if we need to change the cursor
852 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
854 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
856 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
859 SetCursor(m_textCursor
);
862 if (!event
.Dragging())
869 #if wxUSE_DRAG_AND_DROP
875 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
876 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
877 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
879 // Check for dragging across multiple containers
881 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
882 int hit2
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position2
, & hitObj2
, & contextObj2
, 0);
883 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
885 // See if we can find a common ancestor
886 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
888 firstContainer
= GetFocusObject();
889 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
893 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
894 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
895 // is the common ancestor.
896 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
899 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
901 wxRichTextObject
* p
= hitObj2
;
904 if (p
->GetParent() == commonAncestor
)
906 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
913 if (commonAncestor
&& firstContainer
&& otherContainer
)
915 // We have now got a second container that shares a parent with the current or anchor object.
916 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
918 // Don't go into common-ancestor selection mode if we still have the same
920 if (otherContainer
!= firstContainer
)
922 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
923 m_selectionAnchorObject
= firstContainer
;
924 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
926 // The common ancestor, such as a table, returns the cell selection
927 // between the anchor and current position.
928 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
933 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
938 if (otherContainer
->AcceptsFocus())
939 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
940 MoveCaret(-1, false);
941 SetDefaultStyleToCursorStyle();
946 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
947 #if wxUSE_DRAG_AND_DROP
953 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
958 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
964 dc
.SetFont(GetFont());
967 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
968 wxRichTextObject
* hitObj
= NULL
;
969 wxRichTextObject
* contextObj
= NULL
;
970 wxRichTextDrawingContext
context(& GetBuffer());
971 int hit
= GetFocusObject()->HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
973 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
975 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
976 if (actualContainer
&& actualContainer
->AcceptsFocus())
978 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
979 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
983 wxRichTextEvent
cmdEvent(
984 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
986 cmdEvent
.SetEventObject(this);
987 cmdEvent
.SetPosition(position
);
989 cmdEvent
.SetContainer(hitObj
->GetContainer());
991 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
995 /// Left-double-click
996 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
998 wxRichTextEvent
cmdEvent(
999 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
1001 cmdEvent
.SetEventObject(this);
1002 cmdEvent
.SetPosition(m_caretPosition
+1);
1003 cmdEvent
.SetContainer(GetFocusObject());
1005 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1007 SelectWord(GetCaretPosition()+1);
1012 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
1014 wxRichTextEvent
cmdEvent(
1015 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
1017 cmdEvent
.SetEventObject(this);
1018 cmdEvent
.SetPosition(m_caretPosition
+1);
1019 cmdEvent
.SetContainer(GetFocusObject());
1021 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1024 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1025 // Paste any PRIMARY selection, if it exists
1026 wxTheClipboard
->UsePrimarySelection(true);
1028 wxTheClipboard
->UsePrimarySelection(false);
1033 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1036 if (event
.CmdDown())
1037 flags
|= wxRICHTEXT_CTRL_DOWN
;
1038 if (event
.ShiftDown())
1039 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1040 if (event
.AltDown())
1041 flags
|= wxRICHTEXT_ALT_DOWN
;
1043 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1045 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1047 KeyboardNavigate(event
.GetKeyCode(), flags
);
1051 long keycode
= event
.GetKeyCode();
1111 case WXK_NUMPAD_HOME
:
1112 case WXK_NUMPAD_LEFT
:
1114 case WXK_NUMPAD_RIGHT
:
1115 case WXK_NUMPAD_DOWN
:
1116 case WXK_NUMPAD_PAGEUP
:
1117 case WXK_NUMPAD_PAGEDOWN
:
1118 case WXK_NUMPAD_END
:
1119 case WXK_NUMPAD_BEGIN
:
1120 case WXK_NUMPAD_INSERT
:
1121 case WXK_WINDOWS_LEFT
:
1130 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1131 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1133 if (!ProcessBackKey(event
, flags
))
1142 // all the other keys modify the controls contents which shouldn't be
1143 // possible if we're read-only
1144 if ( !IsEditable() )
1150 if (event
.GetKeyCode() == WXK_RETURN
)
1152 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1155 long newPos
= m_caretPosition
;
1157 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1162 BeginBatchUndo(_("Insert Text"));
1164 DeleteSelectedContent(& newPos
);
1166 if (event
.ShiftDown())
1169 text
= wxRichTextLineBreakChar
;
1170 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1171 m_caretAtLineStart
= true;
1175 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1177 // Automatically renumber list
1178 bool isNumberedList
= false;
1179 wxRichTextRange numberedListRange
= FindRangeForList(newPos
+1, isNumberedList
);
1180 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1182 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1186 SetDefaultStyleToCursorStyle();
1188 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1190 wxRichTextEvent
cmdEvent(
1191 wxEVT_COMMAND_RICHTEXT_RETURN
,
1193 cmdEvent
.SetEventObject(this);
1194 cmdEvent
.SetFlags(flags
);
1195 cmdEvent
.SetPosition(newPos
+1);
1196 cmdEvent
.SetContainer(GetFocusObject());
1198 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1200 // Generate conventional event
1201 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1202 InitCommandEvent(textEvent
);
1204 GetEventHandler()->ProcessEvent(textEvent
);
1208 else if (event
.GetKeyCode() == WXK_BACK
)
1210 ProcessBackKey(event
, flags
);
1212 else if (event
.GetKeyCode() == WXK_DELETE
)
1214 long newPos
= m_caretPosition
;
1216 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1221 BeginBatchUndo(_("Delete Text"));
1223 bool processed
= DeleteSelectedContent(& newPos
);
1229 // Submit range in character positions, which are greater than caret positions,
1230 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1232 if (event
.CmdDown())
1234 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1235 if (pos
!= -1 && (pos
> newPos
))
1237 wxRichTextRange
range(newPos
+1, pos
);
1238 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1240 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1247 if (!processed
&& newPos
< (GetLastPosition()-1))
1249 wxRichTextRange
range(newPos
+1, newPos
+1);
1250 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1252 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1260 if (GetLastPosition() == -1)
1262 GetFocusObject()->Reset();
1264 m_caretPosition
= -1;
1266 SetDefaultStyleToCursorStyle();
1269 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1271 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1273 wxRichTextEvent
cmdEvent(
1274 wxEVT_COMMAND_RICHTEXT_DELETE
,
1276 cmdEvent
.SetEventObject(this);
1277 cmdEvent
.SetFlags(flags
);
1278 cmdEvent
.SetPosition(m_caretPosition
+1);
1279 cmdEvent
.SetContainer(GetFocusObject());
1280 GetEventHandler()->ProcessEvent(cmdEvent
);
1287 long keycode
= event
.GetKeyCode();
1299 if (event
.CmdDown())
1301 // Fixes AltGr+key with European input languages on Windows
1302 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1309 wxRichTextEvent
cmdEvent(
1310 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1312 cmdEvent
.SetEventObject(this);
1313 cmdEvent
.SetFlags(flags
);
1315 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1317 cmdEvent
.SetCharacter((wxChar
) keycode
);
1319 cmdEvent
.SetPosition(m_caretPosition
+1);
1320 cmdEvent
.SetContainer(GetFocusObject());
1322 if (keycode
== wxT('\t'))
1324 // See if we need to promote or demote the selection or paragraph at the cursor
1325 // position, instead of inserting a tab.
1326 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1327 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1328 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1330 wxRichTextRange range
;
1332 range
= GetSelectionRange();
1334 range
= para
->GetRange().FromInternal();
1336 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1338 PromoteList(promoteBy
, range
, NULL
);
1340 GetEventHandler()->ProcessEvent(cmdEvent
);
1346 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1349 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1352 BeginBatchUndo(_("Insert Text"));
1354 long newPos
= m_caretPosition
;
1355 DeleteSelectedContent(& newPos
);
1358 wxString str
= event
.GetUnicodeKey();
1360 wxString str
= (wxChar
) event
.GetKeyCode();
1362 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1366 SetDefaultStyleToCursorStyle();
1367 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1369 cmdEvent
.SetPosition(m_caretPosition
);
1370 GetEventHandler()->ProcessEvent(cmdEvent
);
1378 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1380 wxRichTextAttr attr
;
1381 if (container
&& GetStyle(position
, attr
, container
))
1383 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1385 SetCursor(m_urlCursor
);
1387 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1389 SetCursor(m_textCursor
);
1397 // Processes the back key
1398 bool wxRichTextCtrl::ProcessBackKey(wxKeyEvent
& event
, int flags
)
1405 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1410 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1412 // If we're at the start of a list item with a bullet, let's 'delete' the bullet, i.e.
1413 // make it a continuation paragraph.
1414 if (!HasSelection() && para
&& ((m_caretPosition
+1) == para
->GetRange().GetStart()) &&
1415 para
->GetAttributes().HasBulletStyle() && (para
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
) == 0)
1417 wxRichTextParagraph
* newPara
= wxDynamicCast(para
->Clone(), wxRichTextParagraph
);
1418 newPara
->GetAttributes().SetBulletStyle(newPara
->GetAttributes().GetBulletStyle() | wxTEXT_ATTR_BULLET_STYLE_CONTINUATION
);
1420 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Remove Bullet"), wxRICHTEXT_CHANGE_STYLE
, & GetBuffer(), GetFocusObject(), this);
1421 action
->SetRange(newPara
->GetRange());
1422 action
->SetPosition(GetCaretPosition());
1423 action
->GetNewParagraphs().AppendChild(newPara
);
1424 // Also store the old ones for Undo
1425 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1427 GetBuffer().Invalidate(para
->GetRange());
1428 GetBuffer().SubmitAction(action
);
1430 // Automatically renumber list
1431 bool isNumberedList
= false;
1432 wxRichTextRange numberedListRange
= FindRangeForList(m_caretPosition
, isNumberedList
);
1433 if (isNumberedList
&& numberedListRange
!= wxRichTextRange(-1, -1))
1435 NumberList(numberedListRange
, NULL
, wxRICHTEXT_SETSTYLE_RENUMBER
|wxRICHTEXT_SETSTYLE_WITH_UNDO
);
1442 BeginBatchUndo(_("Delete Text"));
1444 long newPos
= m_caretPosition
;
1446 bool processed
= DeleteSelectedContent(& newPos
);
1452 // Submit range in character positions, which are greater than caret positions,
1453 // so subtract 1 for deleted character and add 1 for conversion to character position.
1456 if (event
.CmdDown())
1458 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1461 wxRichTextRange
range(pos
+1, newPos
);
1462 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1464 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1473 wxRichTextRange
range(newPos
, newPos
);
1474 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1476 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1484 if (GetLastPosition() == -1)
1486 GetFocusObject()->Reset();
1488 m_caretPosition
= -1;
1490 SetDefaultStyleToCursorStyle();
1493 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1495 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1497 wxRichTextEvent
cmdEvent(
1498 wxEVT_COMMAND_RICHTEXT_DELETE
,
1500 cmdEvent
.SetEventObject(this);
1501 cmdEvent
.SetFlags(flags
);
1502 cmdEvent
.SetPosition(m_caretPosition
+1);
1503 cmdEvent
.SetContainer(GetFocusObject());
1504 GetEventHandler()->ProcessEvent(cmdEvent
);
1513 /// Delete content if there is a selection, e.g. when pressing a key.
1514 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1518 long pos
= m_selection
.GetRange().GetStart();
1519 wxRichTextRange range
= m_selection
.GetRange();
1521 // SelectAll causes more to be selected than doing it interactively,
1522 // and causes a new paragraph to be inserted. So for multiline buffers,
1523 // don't delete the final position.
1524 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1525 range
.SetEnd(range
.GetEnd()-1);
1527 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1528 m_selection
.Reset();
1529 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1539 /// Keyboard navigation
1543 Left: left one character
1544 Right: right one character
1547 Ctrl-Left: left one word
1548 Ctrl-Right: right one word
1549 Ctrl-Up: previous paragraph start
1550 Ctrl-Down: next start of paragraph
1553 Ctrl-Home: start of document
1554 Ctrl-End: end of document
1555 Page-Up: Up a screen
1556 Page-Down: Down a screen
1560 Ctrl-Alt-PgUp: Start of window
1561 Ctrl-Alt-PgDn: End of window
1562 F8: Start selection mode
1563 Esc: End selection mode
1565 Adding Shift does the above but starts/extends selection.
1570 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1572 bool success
= false;
1574 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1576 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1577 success
= WordRight(1, flags
);
1579 success
= MoveRight(1, flags
);
1581 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1583 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1584 success
= WordLeft(1, flags
);
1586 success
= MoveLeft(1, flags
);
1588 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1590 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1591 success
= MoveToParagraphStart(flags
);
1593 success
= MoveUp(1, flags
);
1595 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1597 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1598 success
= MoveToParagraphEnd(flags
);
1600 success
= MoveDown(1, flags
);
1602 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1604 success
= PageUp(1, flags
);
1606 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1608 success
= PageDown(1, flags
);
1610 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1612 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1613 success
= MoveHome(flags
);
1615 success
= MoveToLineStart(flags
);
1617 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1619 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1620 success
= MoveEnd(flags
);
1622 success
= MoveToLineEnd(flags
);
1627 ScrollIntoView(m_caretPosition
, keyCode
);
1628 SetDefaultStyleToCursorStyle();
1634 /// Extend the selection. Selections are in caret positions.
1635 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1637 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1639 if (oldPos
== newPos
)
1642 wxRichTextSelection oldSelection
= m_selection
;
1644 m_selection
.SetContainer(GetFocusObject());
1646 wxRichTextRange oldRange
;
1647 if (m_selection
.IsValid())
1648 oldRange
= m_selection
.GetRange();
1650 oldRange
= wxRICHTEXT_NO_SELECTION
;
1651 wxRichTextRange newRange
;
1653 // If not currently selecting, start selecting
1654 if (oldRange
.GetStart() == -2)
1656 m_selectionAnchor
= oldPos
;
1658 if (oldPos
> newPos
)
1659 newRange
.SetRange(newPos
+1, oldPos
);
1661 newRange
.SetRange(oldPos
+1, newPos
);
1665 // Always ensure that the selection range start is greater than
1667 if (newPos
> m_selectionAnchor
)
1668 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1669 else if (newPos
== m_selectionAnchor
)
1670 newRange
= wxRichTextRange(-2, -2);
1672 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1675 m_selection
.SetRange(newRange
);
1677 RefreshForSelectionChange(oldSelection
, m_selection
);
1679 if (newRange
.GetStart() > newRange
.GetEnd())
1681 wxLogDebug(wxT("Strange selection range"));
1690 /// Scroll into view, returning true if we scrolled.
1691 /// This takes a _caret_ position.
1692 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1694 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1700 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1702 int startXUnits
, startYUnits
;
1703 GetViewStart(& startXUnits
, & startYUnits
);
1704 int startY
= startYUnits
* ppuY
;
1707 GetVirtualSize(& sx
, & sy
);
1713 wxRect rect
= GetScaledRect(line
->GetRect());
1715 bool scrolled
= false;
1717 wxSize clientSize
= GetClientSize();
1719 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1722 wxClientDC
dc(this);
1723 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1724 topMargin
, bottomMargin
);
1726 clientSize
.y
-= (int) (0.5 + bottomMargin
* GetScale());
1728 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1730 int y
= rect
.y
- GetClientSize().y
/2;
1731 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1732 if (y
>= 0 && (y
+ clientSize
.y
) < (int) (0.5 + GetBuffer().GetCachedSize().y
* GetScale()))
1734 if (startYUnits
!= yUnits
)
1736 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1739 #if !wxRICHTEXT_USE_OWN_CARET
1749 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1750 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1751 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1752 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1754 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1756 // Make it scroll so this item is at the bottom
1758 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1759 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1761 // If we're still off the screen, scroll another line down
1762 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1765 if (startYUnits
!= yUnits
)
1767 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1771 else if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale())))
1773 // Make it scroll so this item is at the top
1775 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1776 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1778 if (startYUnits
!= yUnits
)
1780 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1786 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1787 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1788 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1789 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1791 if (rect
.y
< (startY
+ (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale())))
1793 // Make it scroll so this item is at the top
1795 int y
= rect
.y
- (int) (0.5 + GetBuffer().GetTopMargin() * GetScale());
1796 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1798 if (startYUnits
!= yUnits
)
1800 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1804 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1806 // Make it scroll so this item is at the bottom
1808 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1809 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1811 // If we're still off the screen, scroll another line down
1812 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1815 if (startYUnits
!= yUnits
)
1817 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1823 #if !wxRICHTEXT_USE_OWN_CARET
1831 /// Is the given position visible on the screen?
1832 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1834 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1840 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1843 GetViewStart(& startX
, & startY
);
1845 startY
= startY
* ppuY
;
1847 wxRect rect
= GetScaledRect(line
->GetRect());
1848 wxSize clientSize
= GetClientSize();
1849 clientSize
.y
-= (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale());
1851 return (rect
.GetTop() >= (startY
+ (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()))) &&
1852 (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1855 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1857 m_caretPosition
= position
;
1858 m_caretAtLineStart
= showAtLineStart
;
1861 /// Move caret one visual step forward: this may mean setting a flag
1862 /// and keeping the same position if we're going from the end of one line
1863 /// to the start of the next, which may be the exact same caret position.
1864 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1866 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1868 // Only do the check if we're not at the end of the paragraph (where things work OK
1870 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1872 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1876 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1878 // We're at the end of a line. See whether we need to
1879 // stay at the same actual caret position but change visual
1880 // position, or not.
1881 if (oldPosition
== lineRange
.GetEnd())
1883 if (m_caretAtLineStart
)
1885 // We're already at the start of the line, so actually move on now.
1886 m_caretPosition
= oldPosition
+ 1;
1887 m_caretAtLineStart
= false;
1891 // We're showing at the end of the line, so keep to
1892 // the same position but indicate that we're to show
1893 // at the start of the next line.
1894 m_caretPosition
= oldPosition
;
1895 m_caretAtLineStart
= true;
1897 SetDefaultStyleToCursorStyle();
1903 SetDefaultStyleToCursorStyle();
1906 /// Move caret one visual step backward: this may mean setting a flag
1907 /// and keeping the same position if we're going from the end of one line
1908 /// to the start of the next, which may be the exact same caret position.
1909 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1911 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1913 // Only do the check if we're not at the start of the paragraph (where things work OK
1915 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1917 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1921 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1923 // We're at the start of a line. See whether we need to
1924 // stay at the same actual caret position but change visual
1925 // position, or not.
1926 if (oldPosition
== lineRange
.GetStart())
1928 m_caretPosition
= oldPosition
-1;
1929 m_caretAtLineStart
= true;
1932 else if (oldPosition
== lineRange
.GetEnd())
1934 if (m_caretAtLineStart
)
1936 // We're at the start of the line, so keep the same caret position
1937 // but clear the start-of-line flag.
1938 m_caretPosition
= oldPosition
;
1939 m_caretAtLineStart
= false;
1943 // We're showing at the end of the line, so go back
1944 // to the previous character position.
1945 m_caretPosition
= oldPosition
- 1;
1947 SetDefaultStyleToCursorStyle();
1953 SetDefaultStyleToCursorStyle();
1957 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1959 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1961 if (m_caretPosition
+ noPositions
< endPos
)
1963 long oldPos
= m_caretPosition
;
1964 long newPos
= m_caretPosition
+ noPositions
;
1966 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1970 // Determine by looking at oldPos and m_caretPosition whether
1971 // we moved from the end of a line to the start of the next line, in which case
1972 // we want to adjust the caret position such that it is positioned at the
1973 // start of the next line, rather than jumping past the first character of the
1975 if (noPositions
== 1)
1976 MoveCaretForward(oldPos
);
1978 SetCaretPosition(newPos
);
1981 SetDefaultStyleToCursorStyle();
1990 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1994 if (m_caretPosition
> startPos
- noPositions
+ 1)
1996 long oldPos
= m_caretPosition
;
1997 long newPos
= m_caretPosition
- noPositions
;
1998 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2002 if (noPositions
== 1)
2003 MoveCaretBack(oldPos
);
2005 SetCaretPosition(newPos
);
2008 SetDefaultStyleToCursorStyle();
2016 // Find the caret position for the combination of hit-test flags and character position.
2017 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2018 // since this is ambiguous (same position used for end of line and start of next).
2019 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2020 bool& caretLineStart
)
2022 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2023 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2024 // so we view the caret at the start of the line.
2025 caretLineStart
= false;
2026 long caretPosition
= position
;
2028 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2030 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2031 wxRichTextRange lineRange
;
2033 lineRange
= thisLine
->GetAbsoluteRange();
2035 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2038 caretLineStart
= true;
2042 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2043 if (para
&& para
->GetRange().GetStart() == position
)
2047 return caretPosition
;
2051 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2053 return MoveDown(- noLines
, flags
);
2057 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2062 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2063 wxPoint pt
= GetCaret()->GetPosition();
2064 long newLine
= lineNumber
+ noLines
;
2065 bool notInThisObject
= false;
2067 if (lineNumber
!= -1)
2071 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2072 if (newLine
> lastLine
)
2073 notInThisObject
= true;
2078 notInThisObject
= true;
2082 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2083 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
;
2085 if (notInThisObject
)
2087 // If we know we're navigating out of the current object,
2088 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2089 container
= & GetBuffer();
2090 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2092 if (noLines
> 0) // going down
2094 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2098 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2103 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2105 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2111 wxClientDC
dc(this);
2113 dc
.SetFont(GetFont());
2115 wxRichTextObject
* hitObj
= NULL
;
2116 wxRichTextObject
* contextObj
= NULL
;
2117 wxRichTextDrawingContext
context(& GetBuffer());
2118 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2121 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2122 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2125 if (notInThisObject
)
2127 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2128 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2130 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2132 container
= actualContainer
;
2136 bool caretLineStart
= true;
2137 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2138 long newSelEnd
= caretPosition
;
2141 if (notInThisObject
)
2144 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2149 SetCaretPosition(caretPosition
, caretLineStart
);
2151 SetDefaultStyleToCursorStyle();
2159 /// Move to the end of the paragraph
2160 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2162 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2165 long newPos
= para
->GetRange().GetEnd() - 1;
2166 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2170 SetCaretPosition(newPos
);
2172 SetDefaultStyleToCursorStyle();
2180 /// Move to the start of the paragraph
2181 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2183 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2186 long newPos
= para
->GetRange().GetStart() - 1;
2187 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2191 SetCaretPosition(newPos
, true);
2193 SetDefaultStyleToCursorStyle();
2201 /// Move to the end of the line
2202 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2204 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2208 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2209 long newPos
= lineRange
.GetEnd();
2210 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2214 SetCaretPosition(newPos
);
2216 SetDefaultStyleToCursorStyle();
2224 /// Move to the start of the line
2225 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2227 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2230 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2231 long newPos
= lineRange
.GetStart()-1;
2233 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2237 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2239 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2241 SetDefaultStyleToCursorStyle();
2249 /// Move to the start of the buffer
2250 bool wxRichTextCtrl::MoveHome(int flags
)
2252 if (m_caretPosition
!= -1)
2254 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2258 SetCaretPosition(-1);
2260 SetDefaultStyleToCursorStyle();
2268 /// Move to the end of the buffer
2269 bool wxRichTextCtrl::MoveEnd(int flags
)
2271 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2273 if (m_caretPosition
!= endPos
)
2275 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2279 SetCaretPosition(endPos
);
2281 SetDefaultStyleToCursorStyle();
2289 /// Move noPages pages up
2290 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2292 return PageDown(- noPages
, flags
);
2295 /// Move noPages pages down
2296 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2298 // Calculate which line occurs noPages * screen height further down.
2299 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2302 wxSize clientSize
= GetClientSize();
2303 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2305 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2308 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2309 long pos
= lineRange
.GetStart()-1;
2310 if (pos
!= m_caretPosition
)
2312 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2314 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2318 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2320 SetDefaultStyleToCursorStyle();
2330 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2332 return str
== wxT(" ") || str
== wxT("\t") || (!str
.empty() && (str
[0] == (wxChar
) 160));
2335 // Finds the caret position for the next word
2336 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2338 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2342 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2344 // First skip current text to space
2345 while (i
< endPos
&& i
> -1)
2347 // i is in character, not caret positions
2348 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2349 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2350 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2354 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2361 while (i
< endPos
&& i
> -1)
2363 // i is in character, not caret positions
2364 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2365 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2366 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2367 return wxMax(-1, i
);
2369 if (text
.empty()) // End of paragraph, or maybe an image
2370 return wxMax(-1, i
- 1);
2371 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2375 // Convert to caret position
2376 return wxMax(-1, i
- 1);
2385 long i
= m_caretPosition
;
2387 // First skip white space
2388 while (i
< endPos
&& i
> -1)
2390 // i is in character, not caret positions
2391 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2392 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2394 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2396 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2401 // Next skip current text to space
2402 while (i
< endPos
&& i
> -1)
2404 // i is in character, not caret positions
2405 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2406 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2407 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2410 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2423 /// Move n words left
2424 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2426 long pos
= FindNextWordPosition(-1);
2427 if (pos
!= m_caretPosition
)
2429 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2431 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2435 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2437 SetDefaultStyleToCursorStyle();
2445 /// Move n words right
2446 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2448 long pos
= FindNextWordPosition(1);
2449 if (pos
!= m_caretPosition
)
2451 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2453 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2457 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2459 SetDefaultStyleToCursorStyle();
2468 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2470 // Only do sizing optimization for large buffers
2471 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2473 m_fullLayoutRequired
= true;
2474 m_fullLayoutTime
= wxGetLocalTimeMillis();
2475 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2476 LayoutContent(true /* onlyVisibleRect */);
2479 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2481 #if wxRICHTEXT_BUFFERED_PAINTING
2488 // Force any pending layout due to large buffer
2489 void wxRichTextCtrl::ForceDelayedLayout()
2491 if (m_fullLayoutRequired
)
2493 m_fullLayoutRequired
= false;
2494 m_fullLayoutTime
= 0;
2495 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2496 ShowPosition(m_fullLayoutSavedPosition
);
2502 /// Idle-time processing
2503 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2505 #if wxRICHTEXT_USE_OWN_CARET
2506 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2508 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2514 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2516 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2518 m_fullLayoutRequired
= false;
2519 m_fullLayoutTime
= 0;
2520 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2521 ShowPosition(m_fullLayoutSavedPosition
);
2525 if (m_caretPositionForDefaultStyle
!= -2)
2527 // If the caret position has changed, no longer reflect the default style
2529 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2530 m_caretPositionForDefaultStyle
= -2;
2537 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2539 #if wxRICHTEXT_USE_OWN_CARET
2540 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2543 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2550 /// Set up scrollbars, e.g. after a resize
2551 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2556 if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled
)
2558 SetScrollbars(0, 0, 0, 0, 0, 0);
2562 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2563 // of pixels. See e.g. wxVScrolledWindow for ideas.
2564 int pixelsPerUnit
= 5;
2565 wxSize clientSize
= GetClientSize();
2567 int maxHeight
= (int) (0.5 + GetScale() * (GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin()));
2569 // Round up so we have at least maxHeight pixels
2570 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2572 int startX
= 0, startY
= 0;
2574 GetViewStart(& startX
, & startY
);
2576 int maxPositionX
= 0;
2577 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2579 int newStartX
= wxMin(maxPositionX
, startX
);
2580 int newStartY
= wxMin(maxPositionY
, startY
);
2582 int oldPPUX
, oldPPUY
;
2583 int oldStartX
, oldStartY
;
2584 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2585 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2586 GetViewStart(& oldStartX
, & oldStartY
);
2587 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2589 oldVirtualSizeY
/= oldPPUY
;
2591 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2594 // Don't set scrollbars if there were none before, and there will be none now.
2595 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2598 // Move to previous scroll position if
2600 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2603 /// Paint the background
2604 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2606 wxColour backgroundColour
= GetBackgroundColour();
2607 if (!backgroundColour
.IsOk())
2608 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2610 // Clear the background
2611 dc
.SetBrush(wxBrush(backgroundColour
));
2612 dc
.SetPen(*wxTRANSPARENT_PEN
);
2613 wxRect
windowRect(GetClientSize());
2614 windowRect
.x
-= 2; windowRect
.y
-= 2;
2615 windowRect
.width
+= 4; windowRect
.height
+= 4;
2617 // We need to shift the rectangle to take into account
2618 // scrolling. Converting device to logical coordinates.
2619 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2620 dc
.DrawRectangle(windowRect
);
2623 #if wxRICHTEXT_BUFFERED_PAINTING
2624 /// Recreate buffer bitmap if necessary
2625 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2628 if (sz
== wxDefaultSize
)
2629 sz
= GetClientSize();
2631 if (sz
.x
< 1 || sz
.y
< 1)
2634 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2635 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2636 return m_bufferBitmap
.IsOk();
2640 // ----------------------------------------------------------------------------
2641 // file IO functions
2642 // ----------------------------------------------------------------------------
2644 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2646 SetFocusObject(& GetBuffer(), true);
2648 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2650 m_filename
= filename
;
2653 SetInsertionPoint(0);
2656 SetupScrollbars(true);
2658 wxTextCtrl::SendTextUpdatedEvent(this);
2664 wxLogError(_("File couldn't be loaded."));
2670 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2672 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2674 m_filename
= filename
;
2681 wxLogError(_("The text couldn't be saved."));
2686 // ----------------------------------------------------------------------------
2687 // wxRichTextCtrl specific functionality
2688 // ----------------------------------------------------------------------------
2690 /// Add a new paragraph of text to the end of the buffer
2691 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2693 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2694 GetBuffer().Invalidate();
2700 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2702 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2703 GetBuffer().Invalidate();
2708 // ----------------------------------------------------------------------------
2709 // selection and ranges
2710 // ----------------------------------------------------------------------------
2712 void wxRichTextCtrl::SelectAll()
2714 SetSelection(-1, -1);
2718 void wxRichTextCtrl::SelectNone()
2720 if (m_selection
.IsValid())
2722 wxRichTextSelection oldSelection
= m_selection
;
2724 m_selection
.Reset();
2726 RefreshForSelectionChange(oldSelection
, m_selection
);
2728 m_selectionAnchor
= -2;
2729 m_selectionAnchorObject
= NULL
;
2730 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2733 static bool wxIsWordDelimiter(const wxString
& text
)
2735 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2738 /// Select the word at the given character position
2739 bool wxRichTextCtrl::SelectWord(long position
)
2741 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2744 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2748 if (position
== para
->GetRange().GetEnd())
2751 long positionStart
= position
;
2752 long positionEnd
= position
;
2754 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2756 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2757 if (wxIsWordDelimiter(text
))
2763 if (positionStart
< para
->GetRange().GetStart())
2764 positionStart
= para
->GetRange().GetStart();
2766 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2768 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2769 if (wxIsWordDelimiter(text
))
2775 if (positionEnd
>= para
->GetRange().GetEnd())
2776 positionEnd
= para
->GetRange().GetEnd();
2778 if (positionEnd
< positionStart
)
2781 SetSelection(positionStart
, positionEnd
+1);
2783 if (positionStart
>= 0)
2785 MoveCaret(positionStart
-1, true);
2786 SetDefaultStyleToCursorStyle();
2792 wxString
wxRichTextCtrl::GetStringSelection() const
2795 GetSelection(&from
, &to
);
2797 return GetRange(from
, to
);
2800 // ----------------------------------------------------------------------------
2802 // ----------------------------------------------------------------------------
2804 wxTextCtrlHitTestResult
2805 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2807 // implement in terms of the other overload as the native ports typically
2808 // can get the position and not (x, y) pair directly (although wxUniv
2809 // directly gets x and y -- and so overrides this method as well)
2811 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2813 if ( rc
!= wxTE_HT_UNKNOWN
)
2815 PositionToXY(pos
, x
, y
);
2821 wxTextCtrlHitTestResult
2822 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2825 wxClientDC
dc((wxRichTextCtrl
*) this);
2826 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2828 // Buffer uses logical position (relative to start of buffer)
2830 wxPoint pt2
= GetLogicalPoint(pt
);
2832 wxRichTextObject
* hitObj
= NULL
;
2833 wxRichTextObject
* contextObj
= NULL
;
2834 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2835 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2837 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2838 return wxTE_HT_BEFORE
;
2839 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2840 return wxTE_HT_BEYOND
;
2841 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2842 return wxTE_HT_ON_TEXT
;
2844 return wxTE_HT_UNKNOWN
;
2847 wxRichTextParagraphLayoutBox
*
2848 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2850 wxClientDC
dc(this);
2852 dc
.SetFont(GetFont());
2854 wxPoint logicalPt
= GetLogicalPoint(pt
);
2856 wxRichTextObject
* contextObj
= NULL
;
2857 wxRichTextDrawingContext
context(& GetBuffer());
2858 hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, &hitObj
, &contextObj
, flags
);
2859 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2865 // ----------------------------------------------------------------------------
2866 // set/get the controls text
2867 // ----------------------------------------------------------------------------
2869 wxString
wxRichTextCtrl::DoGetValue() const
2871 return GetBuffer().GetText();
2874 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2876 // Public API for range is different from internals
2877 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2880 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2882 // Don't call Clear here, since it always sends a text updated event
2883 m_buffer
.ResetAndClearCommands();
2884 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2885 m_caretPosition
= -1;
2886 m_caretPositionForDefaultStyle
= -2;
2887 m_caretAtLineStart
= false;
2888 m_selection
.Reset();
2889 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2899 if (!value
.IsEmpty())
2901 // Remove empty paragraph
2902 GetBuffer().Clear();
2903 DoWriteText(value
, flags
);
2905 // for compatibility, don't move the cursor when doing SetValue()
2906 SetInsertionPoint(0);
2910 // still send an event for consistency
2911 if (flags
& SetValue_SendEvent
)
2912 wxTextCtrl::SendTextUpdatedEvent(this);
2917 void wxRichTextCtrl::WriteText(const wxString
& value
)
2922 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2924 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2926 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2927 GetBuffer().Defragment();
2929 if ( flags
& SetValue_SendEvent
)
2930 wxTextCtrl::SendTextUpdatedEvent(this);
2933 void wxRichTextCtrl::AppendText(const wxString
& text
)
2935 SetInsertionPointEnd();
2940 /// Write an image at the current insertion point
2941 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2943 wxRichTextImageBlock imageBlock
;
2945 wxImage image2
= image
;
2946 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2947 return WriteImage(imageBlock
, textAttr
);
2952 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2954 wxRichTextImageBlock imageBlock
;
2957 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2958 return WriteImage(imageBlock
, textAttr
);
2963 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2965 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2968 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2972 wxRichTextImageBlock imageBlock
;
2974 wxImage image
= bitmap
.ConvertToImage();
2975 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2976 return WriteImage(imageBlock
, textAttr
);
2982 // Write a text box at the current insertion point.
2983 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2985 wxRichTextBox
* textBox
= new wxRichTextBox
;
2986 textBox
->SetAttributes(textAttr
);
2987 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2988 textBox
->AddParagraph(wxEmptyString
);
2989 textBox
->SetParent(NULL
);
2991 // The object returned is the one actually inserted into the buffer,
2992 // while the original one is deleted.
2993 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2994 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
2998 wxRichTextField
* wxRichTextCtrl::WriteField(const wxString
& fieldType
, const wxRichTextProperties
& properties
,
2999 const wxRichTextAttr
& textAttr
)
3001 return GetFocusObject()->InsertFieldWithUndo(& GetBuffer(), m_caretPosition
+1, fieldType
, properties
,
3002 this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
, textAttr
);
3005 // Write a table at the current insertion point, returning the table.
3006 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3008 wxASSERT(rows
> 0 && cols
> 0);
3010 if (rows
<= 0 || cols
<= 0)
3013 wxRichTextTable
* table
= new wxRichTextTable
;
3014 table
->SetAttributes(tableAttr
);
3015 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3017 table
->CreateTable(rows
, cols
);
3019 table
->SetParent(NULL
);
3022 for (j
= 0; j
< rows
; j
++)
3024 for (i
= 0; i
< cols
; i
++)
3026 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
3030 // The object returned is the one actually inserted into the buffer,
3031 // while the original one is deleted.
3032 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3033 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3038 /// Insert a newline (actually paragraph) at the current insertion point.
3039 bool wxRichTextCtrl::Newline()
3041 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3044 /// Insert a line break at the current insertion point.
3045 bool wxRichTextCtrl::LineBreak()
3048 text
= wxRichTextLineBreakChar
;
3049 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3052 // ----------------------------------------------------------------------------
3053 // Clipboard operations
3054 // ----------------------------------------------------------------------------
3056 void wxRichTextCtrl::Copy()
3060 wxRichTextRange range
= GetInternalSelectionRange();
3061 GetBuffer().CopyToClipboard(range
);
3065 void wxRichTextCtrl::Cut()
3069 wxRichTextRange range
= GetInternalSelectionRange();
3070 GetBuffer().CopyToClipboard(range
);
3072 DeleteSelectedContent();
3078 void wxRichTextCtrl::Paste()
3082 BeginBatchUndo(_("Paste"));
3084 long newPos
= m_caretPosition
;
3085 DeleteSelectedContent(& newPos
);
3087 GetBuffer().PasteFromClipboard(newPos
);
3093 void wxRichTextCtrl::DeleteSelection()
3095 if (CanDeleteSelection())
3097 DeleteSelectedContent();
3101 bool wxRichTextCtrl::HasSelection() const
3103 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3106 bool wxRichTextCtrl::HasUnfocusedSelection() const
3108 return m_selection
.IsValid();
3111 bool wxRichTextCtrl::CanCopy() const
3113 // Can copy if there's a selection
3114 return HasSelection();
3117 bool wxRichTextCtrl::CanCut() const
3119 return CanDeleteSelection();
3122 bool wxRichTextCtrl::CanPaste() const
3124 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3127 return GetBuffer().CanPasteFromClipboard();
3130 bool wxRichTextCtrl::CanDeleteSelection() const
3132 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3136 // ----------------------------------------------------------------------------
3138 // ----------------------------------------------------------------------------
3140 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3142 if (m_contextMenu
&& m_contextMenu
!= menu
)
3143 delete m_contextMenu
;
3144 m_contextMenu
= menu
;
3147 void wxRichTextCtrl::SetEditable(bool editable
)
3149 m_editable
= editable
;
3152 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3156 m_caretPosition
= pos
- 1;
3157 m_caretAtLineStart
= true;
3161 SetDefaultStyleToCursorStyle();
3164 void wxRichTextCtrl::SetInsertionPointEnd()
3166 long pos
= GetLastPosition();
3167 SetInsertionPoint(pos
);
3170 long wxRichTextCtrl::GetInsertionPoint() const
3172 return m_caretPosition
+1;
3175 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3177 return GetFocusObject()->GetOwnRange().GetEnd();
3180 // If the return values from and to are the same, there is no
3182 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3184 if (m_selection
.IsValid())
3186 *from
= m_selection
.GetRange().GetStart();
3187 *to
= m_selection
.GetRange().GetEnd();
3197 bool wxRichTextCtrl::IsEditable() const
3202 // ----------------------------------------------------------------------------
3204 // ----------------------------------------------------------------------------
3206 void wxRichTextCtrl::SetSelection(long from
, long to
)
3208 // if from and to are both -1, it means (in wxWidgets) that all text should
3210 if ( (from
== -1) && (to
== -1) )
3213 to
= GetLastPosition()+1;
3222 wxRichTextSelection oldSelection
= m_selection
;
3224 m_selectionAnchor
= from
-1;
3225 m_selectionAnchorObject
= NULL
;
3226 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3228 m_caretPosition
= wxMax(-1, to
-1);
3230 RefreshForSelectionChange(oldSelection
, m_selection
);
3235 // ----------------------------------------------------------------------------
3237 // ----------------------------------------------------------------------------
3239 void wxRichTextCtrl::Replace(long from
, long to
,
3240 const wxString
& value
)
3242 BeginBatchUndo(_("Replace"));
3244 SetSelection(from
, to
);
3246 wxRichTextAttr attr
= GetDefaultStyle();
3248 DeleteSelectedContent();
3250 SetDefaultStyle(attr
);
3252 DoWriteText(value
, SetValue_SelectionOnly
);
3257 void wxRichTextCtrl::Remove(long from
, long to
)
3261 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3268 bool wxRichTextCtrl::IsModified() const
3270 return m_buffer
.IsModified();
3273 void wxRichTextCtrl::MarkDirty()
3275 m_buffer
.Modify(true);
3278 void wxRichTextCtrl::DiscardEdits()
3280 m_caretPositionForDefaultStyle
= -2;
3281 m_buffer
.Modify(false);
3282 m_buffer
.GetCommandProcessor()->ClearCommands();
3285 int wxRichTextCtrl::GetNumberOfLines() const
3287 return GetFocusObject()->GetParagraphCount();
3290 // ----------------------------------------------------------------------------
3291 // Positions <-> coords
3292 // ----------------------------------------------------------------------------
3294 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3296 return GetFocusObject()->XYToPosition(x
, y
);
3299 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3301 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3304 // ----------------------------------------------------------------------------
3306 // ----------------------------------------------------------------------------
3308 void wxRichTextCtrl::ShowPosition(long pos
)
3310 if (!IsPositionVisible(pos
))
3311 ScrollIntoView(pos
-1, WXK_DOWN
);
3314 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3316 return GetFocusObject()->GetParagraphLength(lineNo
);
3319 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3321 return GetFocusObject()->GetParagraphText(lineNo
);
3324 // ----------------------------------------------------------------------------
3326 // ----------------------------------------------------------------------------
3328 void wxRichTextCtrl::Undo()
3332 GetCommandProcessor()->Undo();
3336 void wxRichTextCtrl::Redo()
3340 GetCommandProcessor()->Redo();
3344 bool wxRichTextCtrl::CanUndo() const
3346 return GetCommandProcessor()->CanUndo() && IsEditable();
3349 bool wxRichTextCtrl::CanRedo() const
3351 return GetCommandProcessor()->CanRedo() && IsEditable();
3354 // ----------------------------------------------------------------------------
3355 // implementation details
3356 // ----------------------------------------------------------------------------
3358 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3360 SetValue(event
.GetString());
3361 GetEventHandler()->ProcessEvent(event
);
3364 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3366 // By default, load the first file into the text window.
3367 if (event
.GetNumberOfFiles() > 0)
3369 LoadFile(event
.GetFiles()[0]);
3373 wxSize
wxRichTextCtrl::DoGetBestSize() const
3375 return wxSize(10, 10);
3378 // ----------------------------------------------------------------------------
3379 // standard handlers for standard edit menu events
3380 // ----------------------------------------------------------------------------
3382 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3387 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3392 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3397 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3402 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3407 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3412 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3414 event
.Enable( CanCut() );
3417 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3419 event
.Enable( CanCopy() );
3422 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3424 event
.Enable( CanDeleteSelection() );
3427 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3429 event
.Enable( CanPaste() );
3432 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3434 event
.Enable( CanUndo() );
3435 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3438 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3440 event
.Enable( CanRedo() );
3441 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3444 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3446 if (GetLastPosition() > 0)
3450 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3452 event
.Enable(GetLastPosition() > 0);
3455 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3457 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3458 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3460 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3461 if (obj
&& CanEditProperties(obj
))
3462 EditProperties(obj
, this);
3464 m_contextMenuPropertiesInfo
.Clear();
3468 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3470 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3471 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3474 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3476 if (event
.GetEventObject() != this)
3482 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3485 // Prepares the context menu, adding appropriate property-editing commands.
3486 // Returns the number of property commands added.
3487 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3489 wxClientDC
dc(this);
3491 dc
.SetFont(GetFont());
3493 m_contextMenuPropertiesInfo
.Clear();
3496 wxRichTextObject
* hitObj
= NULL
;
3497 wxRichTextObject
* contextObj
= NULL
;
3498 if (pt
!= wxDefaultPosition
)
3500 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3501 wxRichTextDrawingContext
context(& GetBuffer());
3502 int hit
= GetBuffer().HitTest(dc
, context
, GetUnscaledPoint(logicalPt
), position
, & hitObj
, & contextObj
);
3504 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3506 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3507 if (hitObj
&& actualContainer
)
3509 if (actualContainer
->AcceptsFocus())
3511 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3512 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3515 if (addPropertyCommands
)
3516 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3520 if (addPropertyCommands
)
3521 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3526 if (addPropertyCommands
)
3527 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3532 // Invoked from the keyboard, so don't set the caret position and don't use the event
3534 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3536 contextObj
= hitObj
->GetParentContainer();
3538 contextObj
= GetFocusObject();
3540 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3541 if (hitObj
&& actualContainer
)
3543 if (addPropertyCommands
)
3544 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3548 if (addPropertyCommands
)
3549 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3555 if (addPropertyCommands
)
3556 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3557 return m_contextMenuPropertiesInfo
.GetCount();
3563 // Shows the context menu, adding appropriate property-editing commands
3564 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3568 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3576 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3578 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3581 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3583 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3586 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3588 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3591 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3593 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3596 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
, int flags
)
3598 GetFocusObject()->SetStyle(obj
, textAttr
, flags
);
3601 // extended style setting operation with flags including:
3602 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3603 // see richtextbuffer.h for more details.
3605 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3607 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3610 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3612 return GetBuffer().SetDefaultStyle(style
);
3615 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3617 wxRichTextAttr
attr1(style
);
3618 attr1
.GetTextBoxAttr().Reset();
3619 return GetBuffer().SetDefaultStyle(attr1
);
3622 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3624 return GetBuffer().GetDefaultStyle();
3627 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3629 wxRichTextAttr attr
;
3630 if (GetFocusObject()->GetStyle(position
, attr
))
3639 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3641 return GetFocusObject()->GetStyle(position
, style
);
3644 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3646 wxRichTextAttr attr
;
3647 if (container
->GetStyle(position
, attr
))
3656 // get the common set of styles for the range
3657 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3659 wxRichTextAttr attr
;
3660 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3669 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3671 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3674 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3676 return container
->GetStyleForRange(range
.ToInternal(), style
);
3679 /// Get the content (uncombined) attributes for this position.
3680 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3682 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3685 /// Get the content (uncombined) attributes for this position.
3686 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3688 return container
->GetUncombinedStyle(position
, style
);
3691 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3693 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3696 /// Set font, and also the buffer attributes
3697 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3699 wxControl::SetFont(font
);
3701 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3703 GetBuffer().SetBasicStyle(attr
);
3705 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3711 /// Transform logical to physical
3712 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3715 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3720 /// Transform physical to logical
3721 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3724 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3729 /// Position the caret
3730 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3735 //wxLogDebug(wxT("PositionCaret"));
3738 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3740 caretRect
= GetScaledRect(caretRect
);
3741 int topMargin
= (int) (0.5 + GetScale()*GetBuffer().GetTopMargin());
3742 int bottomMargin
= (int) (0.5 + GetScale()*GetBuffer().GetBottomMargin());
3743 wxPoint newPt
= caretRect
.GetPosition();
3744 wxSize newSz
= caretRect
.GetSize();
3745 wxPoint pt
= GetPhysicalPoint(newPt
);
3746 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3749 if (GetCaret()->GetSize() != newSz
)
3750 GetCaret()->SetSize(newSz
);
3752 // Adjust size so the caret size and position doesn't appear in the margins
3753 if (((pt
.y
+ newSz
.y
) <= topMargin
) || (pt
.y
>= (GetClientSize().y
- bottomMargin
)))
3758 else if (pt
.y
< topMargin
&& (pt
.y
+ newSz
.y
) > topMargin
)
3760 newSz
.y
-= (topMargin
- pt
.y
);
3764 GetCaret()->SetSize(newSz
);
3767 else if (pt
.y
< (GetClientSize().y
- bottomMargin
) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- bottomMargin
))
3769 newSz
.y
= GetClientSize().y
- bottomMargin
- pt
.y
;
3770 GetCaret()->SetSize(newSz
);
3773 GetCaret()->Move(pt
);
3779 /// Get the caret height and position for the given character position
3780 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3782 wxClientDC
dc(this);
3784 dc
.SetUserScale(GetScale(), GetScale());
3785 dc
.SetFont(GetFont());
3791 container
= GetFocusObject();
3793 wxRichTextDrawingContext
context(& GetBuffer());
3794 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3796 // Caret height can't be zero
3798 height
= dc
.GetCharHeight();
3800 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3807 /// Gets the line for the visible caret position. If the caret is
3808 /// shown at the very end of the line, it means the next character is actually
3809 /// on the following line. So let's get the line we're expecting to find
3810 /// if this is the case.
3811 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3813 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3814 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3817 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3818 if (caretPosition
== lineRange
.GetStart()-1 &&
3819 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3821 if (!m_caretAtLineStart
)
3822 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3829 /// Move the caret to the given character position
3830 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3832 if (GetBuffer().IsDirty())
3836 container
= GetFocusObject();
3838 if (pos
<= container
->GetOwnRange().GetEnd())
3840 SetCaretPosition(pos
, showAtLineStart
);
3842 PositionCaret(container
);
3850 /// Layout the buffer: which we must do before certain operations, such as
3851 /// setting the caret position.
3852 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3854 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3856 wxRect
availableSpace(GetUnscaledSize(GetClientSize()));
3857 if (availableSpace
.width
== 0)
3858 availableSpace
.width
= 10;
3859 if (availableSpace
.height
== 0)
3860 availableSpace
.height
= 10;
3862 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3863 if (onlyVisibleRect
)
3865 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3866 availableSpace
.SetPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))));
3869 wxClientDC
dc(this);
3872 dc
.SetFont(GetFont());
3874 wxRichTextDrawingContext
context(& GetBuffer());
3875 GetBuffer().Defragment();
3876 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3877 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3878 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3880 if (!IsFrozen() && !onlyVisibleRect
)
3887 /// Is all of the selection, or the current caret position, bold?
3888 bool wxRichTextCtrl::IsSelectionBold()
3892 wxRichTextAttr attr
;
3893 wxRichTextRange range
= GetSelectionRange();
3894 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3895 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3897 return HasCharacterAttributes(range
, attr
);
3901 // If no selection, then we need to combine current style with default style
3902 // to see what the effect would be if we started typing.
3903 wxRichTextAttr attr
;
3904 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3906 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3907 if (GetStyle(pos
, attr
))
3909 if (IsDefaultStyleShowing())
3910 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3911 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3917 /// Is all of the selection, or the current caret position, italics?
3918 bool wxRichTextCtrl::IsSelectionItalics()
3922 wxRichTextRange range
= GetSelectionRange();
3923 wxRichTextAttr attr
;
3924 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3925 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3927 return HasCharacterAttributes(range
, attr
);
3931 // If no selection, then we need to combine current style with default style
3932 // to see what the effect would be if we started typing.
3933 wxRichTextAttr attr
;
3934 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3936 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3937 if (GetStyle(pos
, attr
))
3939 if (IsDefaultStyleShowing())
3940 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3941 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3947 /// Is all of the selection, or the current caret position, underlined?
3948 bool wxRichTextCtrl::IsSelectionUnderlined()
3952 wxRichTextRange range
= GetSelectionRange();
3953 wxRichTextAttr attr
;
3954 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3955 attr
.SetFontUnderlined(true);
3957 return HasCharacterAttributes(range
, attr
);
3961 // If no selection, then we need to combine current style with default style
3962 // to see what the effect would be if we started typing.
3963 wxRichTextAttr attr
;
3964 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3965 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3967 if (GetStyle(pos
, attr
))
3969 if (IsDefaultStyleShowing())
3970 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3971 return attr
.GetFontUnderlined();
3977 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3978 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3980 wxRichTextAttr attr
;
3981 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3982 attr
.SetTextEffectFlags(flag
);
3983 attr
.SetTextEffects(flag
);
3987 return HasCharacterAttributes(GetSelectionRange(), attr
);
3991 // If no selection, then we need to combine current style with default style
3992 // to see what the effect would be if we started typing.
3993 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3994 if (GetStyle(pos
, attr
))
3996 if (IsDefaultStyleShowing())
3997 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3998 return (attr
.GetTextEffectFlags() & flag
) != 0;
4004 /// Apply bold to the selection
4005 bool wxRichTextCtrl::ApplyBoldToSelection()
4007 wxRichTextAttr attr
;
4008 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
4009 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4012 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4015 wxRichTextAttr current
= GetDefaultStyleEx();
4016 current
.Apply(attr
);
4017 SetAndShowDefaultStyle(current
);
4022 /// Apply italic to the selection
4023 bool wxRichTextCtrl::ApplyItalicToSelection()
4025 wxRichTextAttr attr
;
4026 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4027 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4030 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4033 wxRichTextAttr current
= GetDefaultStyleEx();
4034 current
.Apply(attr
);
4035 SetAndShowDefaultStyle(current
);
4040 /// Apply underline to the selection
4041 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4043 wxRichTextAttr attr
;
4044 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4045 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4048 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4051 wxRichTextAttr current
= GetDefaultStyleEx();
4052 current
.Apply(attr
);
4053 SetAndShowDefaultStyle(current
);
4058 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4059 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4061 wxRichTextAttr attr
;
4062 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4063 attr
.SetTextEffectFlags(flags
);
4064 if (!DoesSelectionHaveTextEffectFlag(flags
))
4065 attr
.SetTextEffects(flags
);
4067 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4070 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4073 wxRichTextAttr current
= GetDefaultStyleEx();
4074 current
.Apply(attr
);
4075 SetAndShowDefaultStyle(current
);
4080 /// Is all of the selection aligned according to the specified flag?
4081 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4083 wxRichTextRange range
;
4085 range
= GetSelectionRange();
4087 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4089 wxRichTextAttr attr
;
4090 attr
.SetAlignment(alignment
);
4092 return HasParagraphAttributes(range
, attr
);
4095 /// Apply alignment to the selection
4096 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4098 wxRichTextAttr attr
;
4099 attr
.SetAlignment(alignment
);
4101 return SetStyle(GetSelectionRange(), attr
);
4104 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4106 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4111 /// Apply a named style to the selection
4112 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4114 // Flags are defined within each definition, so only certain
4115 // attributes are applied.
4116 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4118 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4120 if (wxDynamicCast(def
, wxRichTextListStyleDefinition
))
4122 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4124 wxRichTextRange range
;
4127 range
= GetSelectionRange();
4130 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4131 range
= wxRichTextRange(pos
, pos
+1);
4134 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4137 bool isPara
= false;
4139 // Make sure the attr has the style name
4140 if (wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
))
4143 attr
.SetParagraphStyleName(def
->GetName());
4145 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4146 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4147 // to change its style independently.
4148 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4150 else if (wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
))
4151 attr
.SetCharacterStyleName(def
->GetName());
4152 else if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4153 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4155 if (wxDynamicCast(def
, wxRichTextBoxStyleDefinition
))
4157 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4159 SetStyle(GetFocusObject(), attr
);
4165 else if (HasSelection())
4166 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4169 wxRichTextAttr current
= GetDefaultStyleEx();
4170 wxRichTextAttr
defaultStyle(attr
);
4173 // Don't apply extra character styles since they are already implied
4174 // in the paragraph style
4175 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4177 current
.Apply(defaultStyle
);
4178 SetAndShowDefaultStyle(current
);
4180 // If it's a paragraph style, we want to apply the style to the
4181 // current paragraph even if we didn't select any text.
4184 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4185 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4188 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4195 /// Apply the style sheet to the buffer, for example if the styles have changed.
4196 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4199 styleSheet
= GetBuffer().GetStyleSheet();
4203 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4205 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4213 /// Sets the default style to the style under the cursor
4214 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4216 wxRichTextAttr attr
;
4217 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4219 // If at the start of a paragraph, use the next position.
4220 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4222 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4223 if (obj
&& obj
->IsTopLevel())
4225 // Don't use the attributes of a top-level object, since they might apply
4226 // to content of the object, e.g. background colour.
4227 SetDefaultStyle(wxRichTextAttr());
4230 else if (GetUncombinedStyle(pos
, attr
))
4232 SetDefaultStyle(attr
);
4239 /// Returns the first visible position in the current view
4240 long wxRichTextCtrl::GetFirstVisiblePosition() const
4242 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))).y
);
4244 return line
->GetAbsoluteRange().GetStart();
4249 /// Get the first visible point in the window
4250 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4253 int startXUnits
, startYUnits
;
4255 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4256 GetViewStart(& startXUnits
, & startYUnits
);
4258 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4261 /// The adjusted caret position is the character position adjusted to take
4262 /// into account whether we're at the start of a paragraph, in which case
4263 /// style information should be taken from the next position, not current one.
4264 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4266 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4268 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4273 /// Get/set the selection range in character positions. -1, -1 means no selection.
4274 /// The range is in API convention, i.e. a single character selection is denoted
4276 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4278 wxRichTextRange range
= GetInternalSelectionRange();
4279 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4280 range
.SetEnd(range
.GetEnd() + 1);
4284 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4286 SetSelection(range
.GetStart(), range
.GetEnd());
4290 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4292 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4295 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4297 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4300 /// Clear list for given range
4301 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4303 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4306 /// Number/renumber any list elements in the given range
4307 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4309 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4312 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4314 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4317 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4318 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4320 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4323 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4325 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4328 // Given a character position at which there is a list style, find the range
4329 // encompassing the same list style by looking backwards and forwards.
4330 wxRichTextRange
wxRichTextCtrl::FindRangeForList(long pos
, bool& isNumberedList
)
4332 wxRichTextParagraphLayoutBox
* focusObject
= GetFocusObject();
4333 wxRichTextRange range
= wxRichTextRange(-1, -1);
4334 wxRichTextParagraph
* para
= focusObject
->GetParagraphAtPosition(pos
);
4335 if (!para
|| !para
->GetAttributes().HasListStyleName())
4339 wxString listStyle
= para
->GetAttributes().GetListStyleName();
4340 range
= para
->GetRange();
4342 isNumberedList
= para
->GetAttributes().HasBulletNumber();
4345 wxRichTextObjectList::compatibility_iterator initialNode
= focusObject
->GetChildren().Find(para
);
4348 wxRichTextObjectList::compatibility_iterator startNode
= initialNode
->GetPrevious();
4351 wxRichTextParagraph
* p
= wxDynamicCast(startNode
->GetData(), wxRichTextParagraph
);
4354 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4357 range
.SetStart(p
->GetRange().GetStart());
4360 startNode
= startNode
->GetPrevious();
4364 wxRichTextObjectList::compatibility_iterator endNode
= initialNode
->GetNext();
4367 wxRichTextParagraph
* p
= wxDynamicCast(endNode
->GetData(), wxRichTextParagraph
);
4370 if (!p
->GetAttributes().HasListStyleName() || p
->GetAttributes().GetListStyleName() != listStyle
)
4373 range
.SetEnd(p
->GetRange().GetEnd());
4376 endNode
= endNode
->GetNext();
4383 /// Deletes the content in the given range
4384 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4386 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4389 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4391 if (sm_availableFontNames
.GetCount() == 0)
4393 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4394 sm_availableFontNames
.Sort();
4396 return sm_availableFontNames
;
4399 void wxRichTextCtrl::ClearAvailableFontNames()
4401 sm_availableFontNames
.Clear();
4404 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4406 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4408 wxTextAttrEx basicStyle
= GetBasicStyle();
4409 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4410 SetBasicStyle(basicStyle
);
4411 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4416 // Refresh the area affected by a selection change
4417 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4419 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4420 // the selection contains whole containers rather than just text, so refresh everything
4421 // for now as it would be hard to compute the rectangle bounding all selections.
4422 // TODO: improve on this.
4423 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4424 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4430 wxRichTextRange oldRange
, newRange
;
4431 if (oldSelection
.IsValid())
4432 oldRange
= oldSelection
.GetRange();
4434 oldRange
= wxRICHTEXT_NO_SELECTION
;
4435 if (newSelection
.IsValid())
4436 newRange
= newSelection
.GetRange();
4438 newRange
= wxRICHTEXT_NO_SELECTION
;
4440 // Calculate the refresh rectangle - just the affected lines
4441 long firstPos
, lastPos
;
4442 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4444 firstPos
= newRange
.GetStart();
4445 lastPos
= newRange
.GetEnd();
4447 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4449 firstPos
= oldRange
.GetStart();
4450 lastPos
= oldRange
.GetEnd();
4452 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4458 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4459 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4462 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4463 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4465 if (firstLine
&& lastLine
)
4467 wxSize clientSize
= GetClientSize();
4468 wxPoint pt1
= GetPhysicalPoint(GetScaledPoint(firstLine
->GetAbsolutePosition()));
4469 wxPoint pt2
= GetPhysicalPoint(GetScaledPoint(lastLine
->GetAbsolutePosition())) + wxPoint(0, (int) (0.5 + lastLine
->GetSize().y
* GetScale()));
4472 pt1
.y
= wxMax(0, pt1
.y
);
4474 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4476 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4477 RefreshRect(rect
, false);
4485 // margins functions
4486 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4488 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4489 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4490 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4491 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4496 wxPoint
wxRichTextCtrl::DoGetMargins() const
4498 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4499 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4502 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4504 if (obj
&& !obj
->AcceptsFocus())
4507 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4508 bool changingContainer
= (m_focusObject
!= obj
);
4510 if (changingContainer
&& HasSelection())
4513 m_focusObject
= obj
;
4516 m_focusObject
= & m_buffer
;
4518 if (setCaretPosition
&& changingContainer
)
4520 m_selection
.Reset();
4521 m_selectionAnchor
= -2;
4522 m_selectionAnchorObject
= NULL
;
4523 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4527 m_caretAtLineStart
= false;
4528 MoveCaret(pos
, m_caretAtLineStart
);
4529 SetDefaultStyleToCursorStyle();
4531 wxRichTextEvent
cmdEvent(
4532 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4534 cmdEvent
.SetEventObject(this);
4535 cmdEvent
.SetPosition(m_caretPosition
+1);
4536 cmdEvent
.SetOldContainer(oldContainer
);
4537 cmdEvent
.SetContainer(m_focusObject
);
4539 GetEventHandler()->ProcessEvent(cmdEvent
);
4544 #if wxUSE_DRAG_AND_DROP
4545 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4549 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4554 if (!GetSelection().IsValid())
4559 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4560 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4562 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4565 long position
= GetCaretPosition();
4566 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4567 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4569 // It doesn't make sense to move onto itself
4573 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4574 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4575 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4576 if ((def
== wxDragMove
) && !DeleteAfter
)
4578 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4579 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4582 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4583 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4585 delete richTextBuffer
;
4589 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4590 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4597 #endif // wxUSE_DRAG_AND_DROP
4600 #if wxUSE_DRAG_AND_DROP
4601 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4603 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4607 wxRichTextObject
* hitObj
= NULL
;
4608 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->GetUnscaledPoint(m_rtc
->ScreenToClient(wxGetMousePosition())), position
, hit
, hitObj
);
4610 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4612 m_rtc
->StoreFocusObject(container
);
4613 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4616 return false; // so that the base-class sets a cursor
4618 #endif // wxUSE_DRAG_AND_DROP
4620 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4625 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4630 void wxRichTextCtrl::EnableVerticalScrollbar(bool enable
)
4632 m_verticalScrollbarEnabled
= enable
;
4636 void wxRichTextCtrl::SetFontScale(double fontScale
, bool refresh
)
4638 GetBuffer().SetFontScale(fontScale
);
4641 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4646 void wxRichTextCtrl::SetDimensionScale(double dimScale
, bool refresh
)
4648 GetBuffer().SetDimensionScale(dimScale
);
4651 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4656 // Sets an overall scale factor for displaying and editing the content.
4657 void wxRichTextCtrl::SetScale(double scale
, bool refresh
)
4662 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4667 // Get an unscaled point
4668 wxPoint
wxRichTextCtrl::GetUnscaledPoint(const wxPoint
& pt
) const
4670 if (GetScale() == 1.0)
4673 return wxPoint((int) (0.5 + double(pt
.x
) / GetScale()), (int) (0.5 + double(pt
.y
) / GetScale()));
4676 // Get a scaled point
4677 wxPoint
wxRichTextCtrl::GetScaledPoint(const wxPoint
& pt
) const
4679 if (GetScale() == 1.0)
4682 return wxPoint((int) (0.5 + double(pt
.x
) * GetScale()), (int) (0.5 + double(pt
.y
) * GetScale()));
4685 // Get an unscaled size
4686 wxSize
wxRichTextCtrl::GetUnscaledSize(const wxSize
& sz
) const
4688 if (GetScale() == 1.0)
4691 return wxSize((int) (0.5 + double(sz
.x
) / GetScale()), (int) (0.5 + double(sz
.y
) / GetScale()));
4694 // Get a scaled size
4695 wxSize
wxRichTextCtrl::GetScaledSize(const wxSize
& sz
) const
4697 if (GetScale() == 1.0)
4700 return wxSize((int) (0.5 + double(sz
.x
) * GetScale()), (int) (0.5 + double(sz
.y
) * GetScale()));
4703 // Get an unscaled rect
4704 wxRect
wxRichTextCtrl::GetUnscaledRect(const wxRect
& rect
) const
4706 if (GetScale() == 1.0)
4709 return wxRect((int) (0.5 + double(rect
.x
) / GetScale()), (int) (0.5 + double(rect
.y
) / GetScale()),
4710 (int) (0.5 + double(rect
.width
) / GetScale()), (int) (0.5 + double(rect
.height
) / GetScale()));
4713 // Get a scaled rect
4714 wxRect
wxRichTextCtrl::GetScaledRect(const wxRect
& rect
) const
4716 if (GetScale() == 1.0)
4719 return wxRect((int) (0.5 + double(rect
.x
) * GetScale()), (int) (0.5 + double(rect
.y
) * GetScale()),
4720 (int) (0.5 + double(rect
.width
) * GetScale()), (int) (0.5 + double(rect
.height
) * GetScale()));
4723 #if wxRICHTEXT_USE_OWN_CARET
4725 // ----------------------------------------------------------------------------
4726 // initialization and destruction
4727 // ----------------------------------------------------------------------------
4729 void wxRichTextCaret::Init()
4732 m_refreshEnabled
= true;
4736 m_richTextCtrl
= NULL
;
4737 m_needsUpdate
= false;
4741 wxRichTextCaret::~wxRichTextCaret()
4743 if (m_timer
.IsRunning())
4747 // ----------------------------------------------------------------------------
4748 // showing/hiding/moving the caret (base class interface)
4749 // ----------------------------------------------------------------------------
4751 void wxRichTextCaret::DoShow()
4755 if (!m_timer
.IsRunning())
4756 m_timer
.Start(GetBlinkTime());
4761 void wxRichTextCaret::DoHide()
4763 if (m_timer
.IsRunning())
4769 void wxRichTextCaret::DoMove()
4775 if (m_xOld
!= -1 && m_yOld
!= -1)
4777 if (m_richTextCtrl
&& m_refreshEnabled
)
4779 wxRect
rect(GetPosition(), GetSize());
4780 m_richTextCtrl
->RefreshRect(rect
, false);
4789 void wxRichTextCaret::DoSize()
4791 int countVisible
= m_countVisible
;
4792 if (countVisible
> 0)
4798 if (countVisible
> 0)
4800 m_countVisible
= countVisible
;
4805 // ----------------------------------------------------------------------------
4806 // handling the focus
4807 // ----------------------------------------------------------------------------
4809 void wxRichTextCaret::OnSetFocus()
4817 void wxRichTextCaret::OnKillFocus()
4822 // ----------------------------------------------------------------------------
4823 // drawing the caret
4824 // ----------------------------------------------------------------------------
4826 void wxRichTextCaret::Refresh()
4828 if (m_richTextCtrl
&& m_refreshEnabled
)
4830 wxRect
rect(GetPosition(), GetSize());
4831 m_richTextCtrl
->RefreshRect(rect
, false);
4835 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4837 dc
->SetPen( *wxBLACK_PEN
);
4839 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4840 dc
->SetPen(*wxBLACK_PEN
);
4842 wxPoint
pt(m_x
, m_y
);
4846 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4848 if (IsVisible() && m_flashOn
)
4849 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4852 void wxRichTextCaret::Notify()
4854 m_flashOn
= !m_flashOn
;
4858 void wxRichTextCaretTimer::Notify()
4863 // wxRICHTEXT_USE_OWN_CARET
4866 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4870 m_labels
.Add(label
);
4878 // Returns number of menu items were added.
4879 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4881 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4882 // If none of the standard properties identifiers are in the menu, add them if necessary.
4883 // If no items to add, just set the text to something generic
4884 if (GetCount() == 0)
4888 menu
->SetLabel(startCmd
, _("&Properties"));
4890 // Delete the others if necessary
4892 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4894 if (menu
->FindItem(i
))
4905 // Find the position of the first properties item
4906 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4908 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4909 if (item
&& item
->GetId() == startCmd
)
4918 int insertBefore
= pos
+1;
4919 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4921 if (menu
->FindItem(i
))
4923 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4927 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4928 menu
->Append(i
, m_labels
[i
- startCmd
]);
4930 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4935 // Delete any old items still left on the menu
4936 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4938 if (menu
->FindItem(i
))
4946 // No existing property identifiers were found, so append to the end of the menu.
4947 menu
->AppendSeparator();
4948 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4950 menu
->Append(i
, m_labels
[i
- startCmd
]);
4958 // Add appropriate menu items for the current container and clicked on object
4959 // (and container's parent, if appropriate).
4960 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4963 if (obj
&& ctrl
->CanEditProperties(obj
))
4964 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
4966 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
4967 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
4969 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
4970 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());