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
;
372 void wxRichTextCtrl::DoThaw()
374 if (GetBuffer().IsDirty())
383 void wxRichTextCtrl::Clear()
385 if (GetFocusObject() == & GetBuffer())
387 m_buffer
.ResetAndClearCommands();
388 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
392 GetFocusObject()->Reset();
395 m_caretPosition
= -1;
396 m_caretPositionForDefaultStyle
= -2;
397 m_caretAtLineStart
= false;
399 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
409 wxTextCtrl::SendTextUpdatedEvent(this);
413 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
415 #if !wxRICHTEXT_USE_OWN_CARET
416 if (GetCaret() && !IsFrozen())
419 // Stop the caret refreshing the control from within the
422 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(false);
426 #if wxRICHTEXT_BUFFERED_PAINTING
427 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
437 dc
.SetFont(GetFont());
439 // Paint the background
442 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
444 wxRect
drawingArea(GetUpdateRegion().GetBox());
445 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
447 wxRect
availableSpace(GetClientSize());
448 wxRichTextDrawingContext
context(& GetBuffer());
449 if (GetBuffer().IsDirty())
451 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
452 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
456 wxRect
clipRect(availableSpace
);
457 clipRect
.x
+= GetBuffer().GetLeftMargin();
458 clipRect
.y
+= GetBuffer().GetTopMargin();
459 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
460 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
461 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
462 dc
.SetClippingRegion(clipRect
);
465 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
466 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
468 GetBuffer().Draw(dc
, context
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
470 dc
.DestroyClippingRegion();
472 // Other user defined painting after everything else (i.e. all text) is painted
473 PaintAboveContent(dc
);
475 #if wxRICHTEXT_USE_OWN_CARET
476 if (GetCaret()->IsVisible())
479 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
484 #if !wxRICHTEXT_USE_OWN_CARET
490 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(true);
494 // Empty implementation, to prevent flicker
495 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
499 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
503 #if !wxRICHTEXT_USE_OWN_CARET
509 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
510 // Work around dropouts when control is focused
518 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
523 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
524 // Work around dropouts when control is focused
532 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
537 // Set up the caret for the given position and container, after a mouse click
538 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
540 bool caretAtLineStart
= false;
542 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
544 // If we're at the start of a line (but not first in para)
545 // then we should keep the caret showing at the start of the line
546 // by showing the m_caretAtLineStart flag.
547 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
548 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
550 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
551 caretAtLineStart
= true;
555 if (extendSelection
&& (m_caretPosition
!= position
))
556 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
558 MoveCaret(position
, caretAtLineStart
);
559 SetDefaultStyleToCursorStyle();
565 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
571 dc
.SetFont(GetFont());
573 // TODO: detect change of focus object
575 wxRichTextObject
* hitObj
= NULL
;
576 wxRichTextObject
* contextObj
= NULL
;
577 wxRichTextDrawingContext
context(& GetBuffer());
578 int hit
= GetBuffer().HitTest(dc
, context
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
580 #if wxUSE_DRAG_AND_DROP
581 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
582 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position
))
584 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
586 m_dragStartPoint
= event
.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
589 m_dragStartTime
= wxDateTime::UNow();
590 #endif // wxUSE_DATETIME
592 // Preserve behaviour of clicking on an object within the selection
593 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
596 return; // Don't skip the event, else the selection will be lost
598 #endif // wxUSE_DRAG_AND_DROP
600 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
602 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
603 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
604 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
606 SetFocusObject(container
, false /* don't set caret position yet */);
612 long oldCaretPos
= m_caretPosition
;
614 SetCaretPositionAfterClick(container
, position
, hit
);
616 // For now, don't handle shift-click when we're selecting multiple objects.
617 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
618 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
627 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
632 if (GetCapture() == this)
635 // See if we clicked on a URL
638 dc
.SetFont(GetFont());
641 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
642 wxRichTextObject
* hitObj
= NULL
;
643 wxRichTextObject
* contextObj
= NULL
;
644 wxRichTextDrawingContext
context(& GetBuffer());
645 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
646 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
648 #if wxUSE_DRAG_AND_DROP
651 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
652 m_preDrag
= false; // Tell DnD not to happen now: we are processing Left Up ourselves.
654 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
656 wxRichTextObject
* hitObj
= NULL
;
657 wxRichTextObject
* contextObj
= NULL
;
658 int hit
= GetBuffer().HitTest(dc
, context
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
659 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
660 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
661 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
663 SetFocusObject(container
, false /* don't set caret position yet */);
666 long oldCaretPos
= m_caretPosition
;
668 SetCaretPositionAfterClick(container
, position
, hit
);
670 // For now, don't handle shift-click when we're selecting multiple objects.
671 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
672 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
678 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
680 wxRichTextEvent
cmdEvent(
681 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
683 cmdEvent
.SetEventObject(this);
684 cmdEvent
.SetPosition(position
);
686 cmdEvent
.SetContainer(hitObj
->GetContainer());
688 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
691 if (GetStyle(position
, attr
))
693 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
695 wxString urlTarget
= attr
.GetURL();
696 if (!urlTarget
.IsEmpty())
698 wxMouseEvent
mouseEvent(event
);
700 long startPos
= 0, endPos
= 0;
701 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
704 startPos
= obj
->GetRange().GetStart();
705 endPos
= obj
->GetRange().GetEnd();
708 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
709 InitCommandEvent(urlEvent
);
711 urlEvent
.SetString(urlTarget
);
713 GetEventHandler()->ProcessEvent(urlEvent
);
721 #if wxUSE_DRAG_AND_DROP
723 #endif // wxUSE_DRAG_AND_DROP
725 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
726 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
728 // Put the selection in PRIMARY, if it exists
729 wxTheClipboard
->UsePrimarySelection(true);
731 wxRichTextRange range
= GetInternalSelectionRange();
732 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
734 wxTheClipboard
->UsePrimarySelection(false);
740 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
742 #if wxUSE_DRAG_AND_DROP
744 if (m_preDrag
|| m_dragging
)
746 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
747 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
748 distance
= abs(x
) + abs(y
);
751 // See if we're starting Drag'n'Drop
755 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
759 && (diff
.GetMilliseconds() > 100)
766 wxRichTextRange range
= GetInternalSelectionRange();
767 if (range
== wxRICHTEXT_NONE
)
769 // Don't try to drag an empty range
774 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
775 long oldPos
= GetCaretPosition();
776 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
778 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
779 wxString text
= GetFocusObject()->GetTextForRange(range
);
781 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
783 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
785 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
786 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
787 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
789 wxRichTextDropSource
source(*compositeObject
, this);
790 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
791 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
792 BeginBatchUndo(_("Drag"));
793 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
796 case wxDragCopy
: break;
799 wxLogError(wxT("An error occurred during drag and drop operation"));
802 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
803 SetCaretPosition(oldPos
);
804 SetFocusObject(oldFocus
, false);
813 #endif // wxUSE_DRAG_AND_DROP
817 dc
.SetFont(GetFont());
820 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
821 wxRichTextObject
* hitObj
= NULL
;
822 wxRichTextObject
* contextObj
= NULL
;
826 // If we're dragging, let's only consider positions at this level; otherwise
827 // selecting a range is not going to work.
828 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
831 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
832 container
= GetFocusObject();
834 wxRichTextDrawingContext
context(& GetBuffer());
835 int hit
= container
->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
837 // See if we need to change the cursor
840 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
842 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
844 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
847 SetCursor(m_textCursor
);
850 if (!event
.Dragging())
857 #if wxUSE_DRAG_AND_DROP
863 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
864 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
865 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
867 // Check for dragging across multiple containers
869 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
870 int hit2
= GetBuffer().HitTest(dc
, context
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
871 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
873 // See if we can find a common ancestor
874 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
876 firstContainer
= GetFocusObject();
877 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
881 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
882 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
883 // is the common ancestor.
884 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
887 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
889 wxRichTextObject
* p
= hitObj2
;
892 if (p
->GetParent() == commonAncestor
)
894 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
901 if (commonAncestor
&& firstContainer
&& otherContainer
)
903 // We have now got a second container that shares a parent with the current or anchor object.
904 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
906 // Don't go into common-ancestor selection mode if we still have the same
908 if (otherContainer
!= firstContainer
)
910 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
911 m_selectionAnchorObject
= firstContainer
;
912 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
914 // The common ancestor, such as a table, returns the cell selection
915 // between the anchor and current position.
916 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
921 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
926 if (otherContainer
->AcceptsFocus())
927 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
928 MoveCaret(-1, false);
929 SetDefaultStyleToCursorStyle();
934 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
935 #if wxUSE_DRAG_AND_DROP
941 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
946 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
952 dc
.SetFont(GetFont());
955 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
956 wxRichTextObject
* hitObj
= NULL
;
957 wxRichTextObject
* contextObj
= NULL
;
958 wxRichTextDrawingContext
context(& GetBuffer());
959 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_HONOUR_ATOMIC
);
961 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
963 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
964 if (actualContainer
&& actualContainer
->AcceptsFocus())
966 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
967 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
971 wxRichTextEvent
cmdEvent(
972 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
974 cmdEvent
.SetEventObject(this);
975 cmdEvent
.SetPosition(position
);
977 cmdEvent
.SetContainer(hitObj
->GetContainer());
979 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
983 /// Left-double-click
984 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
986 wxRichTextEvent
cmdEvent(
987 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
989 cmdEvent
.SetEventObject(this);
990 cmdEvent
.SetPosition(m_caretPosition
+1);
991 cmdEvent
.SetContainer(GetFocusObject());
993 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
995 SelectWord(GetCaretPosition()+1);
1000 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
1002 wxRichTextEvent
cmdEvent(
1003 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
1005 cmdEvent
.SetEventObject(this);
1006 cmdEvent
.SetPosition(m_caretPosition
+1);
1007 cmdEvent
.SetContainer(GetFocusObject());
1009 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1012 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1013 // Paste any PRIMARY selection, if it exists
1014 wxTheClipboard
->UsePrimarySelection(true);
1016 wxTheClipboard
->UsePrimarySelection(false);
1021 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1024 if (event
.CmdDown())
1025 flags
|= wxRICHTEXT_CTRL_DOWN
;
1026 if (event
.ShiftDown())
1027 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1028 if (event
.AltDown())
1029 flags
|= wxRICHTEXT_ALT_DOWN
;
1031 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1033 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1035 KeyboardNavigate(event
.GetKeyCode(), flags
);
1039 long keycode
= event
.GetKeyCode();
1099 case WXK_NUMPAD_HOME
:
1100 case WXK_NUMPAD_LEFT
:
1102 case WXK_NUMPAD_RIGHT
:
1103 case WXK_NUMPAD_DOWN
:
1104 case WXK_NUMPAD_PAGEUP
:
1105 case WXK_NUMPAD_PAGEDOWN
:
1106 case WXK_NUMPAD_END
:
1107 case WXK_NUMPAD_BEGIN
:
1108 case WXK_NUMPAD_INSERT
:
1109 case WXK_WINDOWS_LEFT
:
1118 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1119 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1126 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1131 BeginBatchUndo(_("Delete Text"));
1133 long newPos
= m_caretPosition
;
1135 bool processed
= DeleteSelectedContent(& newPos
);
1141 // Submit range in character positions, which are greater than caret positions,
1142 // so subtract 1 for deleted character and add 1 for conversion to character position.
1145 if (event
.CmdDown())
1147 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1150 wxRichTextRange
range(pos
+1, newPos
);
1151 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1153 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1162 wxRichTextRange
range(newPos
, newPos
);
1163 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1165 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1173 if (GetLastPosition() == -1)
1175 GetFocusObject()->Reset();
1177 m_caretPosition
= -1;
1179 SetDefaultStyleToCursorStyle();
1182 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1184 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1186 wxRichTextEvent
cmdEvent(
1187 wxEVT_COMMAND_RICHTEXT_DELETE
,
1189 cmdEvent
.SetEventObject(this);
1190 cmdEvent
.SetFlags(flags
);
1191 cmdEvent
.SetPosition(m_caretPosition
+1);
1192 cmdEvent
.SetContainer(GetFocusObject());
1193 GetEventHandler()->ProcessEvent(cmdEvent
);
1204 // all the other keys modify the controls contents which shouldn't be
1205 // possible if we're read-only
1206 if ( !IsEditable() )
1212 if (event
.GetKeyCode() == WXK_RETURN
)
1214 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1217 long newPos
= m_caretPosition
;
1219 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1224 BeginBatchUndo(_("Insert Text"));
1226 DeleteSelectedContent(& newPos
);
1228 if (event
.ShiftDown())
1231 text
= wxRichTextLineBreakChar
;
1232 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1233 m_caretAtLineStart
= true;
1237 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1240 SetDefaultStyleToCursorStyle();
1242 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1244 wxRichTextEvent
cmdEvent(
1245 wxEVT_COMMAND_RICHTEXT_RETURN
,
1247 cmdEvent
.SetEventObject(this);
1248 cmdEvent
.SetFlags(flags
);
1249 cmdEvent
.SetPosition(newPos
+1);
1250 cmdEvent
.SetContainer(GetFocusObject());
1252 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1254 // Generate conventional event
1255 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1256 InitCommandEvent(textEvent
);
1258 GetEventHandler()->ProcessEvent(textEvent
);
1262 else if (event
.GetKeyCode() == WXK_BACK
)
1264 long newPos
= m_caretPosition
;
1266 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1271 BeginBatchUndo(_("Delete Text"));
1273 bool processed
= DeleteSelectedContent(& newPos
);
1279 // Submit range in character positions, which are greater than caret positions,
1280 // so subtract 1 for deleted character and add 1 for conversion to character position.
1283 if (event
.CmdDown())
1285 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1288 wxRichTextRange
range(pos
+1, newPos
);
1289 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1291 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1300 wxRichTextRange
range(newPos
, newPos
);
1301 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1303 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1311 if (GetLastPosition() == -1)
1313 GetFocusObject()->Reset();
1315 m_caretPosition
= -1;
1317 SetDefaultStyleToCursorStyle();
1320 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1322 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1324 wxRichTextEvent
cmdEvent(
1325 wxEVT_COMMAND_RICHTEXT_DELETE
,
1327 cmdEvent
.SetEventObject(this);
1328 cmdEvent
.SetFlags(flags
);
1329 cmdEvent
.SetPosition(m_caretPosition
+1);
1330 cmdEvent
.SetContainer(GetFocusObject());
1331 GetEventHandler()->ProcessEvent(cmdEvent
);
1336 else if (event
.GetKeyCode() == WXK_DELETE
)
1338 long newPos
= m_caretPosition
;
1340 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1345 BeginBatchUndo(_("Delete Text"));
1347 bool processed
= DeleteSelectedContent(& newPos
);
1353 // Submit range in character positions, which are greater than caret positions,
1354 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1356 if (event
.CmdDown())
1358 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1359 if (pos
!= -1 && (pos
> newPos
))
1361 wxRichTextRange
range(newPos
+1, pos
);
1362 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1364 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1371 if (!processed
&& newPos
< (GetLastPosition()-1))
1373 wxRichTextRange
range(newPos
+1, newPos
+1);
1374 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1376 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1384 if (GetLastPosition() == -1)
1386 GetFocusObject()->Reset();
1388 m_caretPosition
= -1;
1390 SetDefaultStyleToCursorStyle();
1393 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1395 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1397 wxRichTextEvent
cmdEvent(
1398 wxEVT_COMMAND_RICHTEXT_DELETE
,
1400 cmdEvent
.SetEventObject(this);
1401 cmdEvent
.SetFlags(flags
);
1402 cmdEvent
.SetPosition(m_caretPosition
+1);
1403 cmdEvent
.SetContainer(GetFocusObject());
1404 GetEventHandler()->ProcessEvent(cmdEvent
);
1411 long keycode
= event
.GetKeyCode();
1423 if (event
.CmdDown())
1425 // Fixes AltGr+key with European input languages on Windows
1426 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1433 wxRichTextEvent
cmdEvent(
1434 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1436 cmdEvent
.SetEventObject(this);
1437 cmdEvent
.SetFlags(flags
);
1439 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1441 cmdEvent
.SetCharacter((wxChar
) keycode
);
1443 cmdEvent
.SetPosition(m_caretPosition
+1);
1444 cmdEvent
.SetContainer(GetFocusObject());
1446 if (keycode
== wxT('\t'))
1448 // See if we need to promote or demote the selection or paragraph at the cursor
1449 // position, instead of inserting a tab.
1450 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1451 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1452 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1454 wxRichTextRange range
;
1456 range
= GetSelectionRange();
1458 range
= para
->GetRange().FromInternal();
1460 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1462 PromoteList(promoteBy
, range
, NULL
);
1464 GetEventHandler()->ProcessEvent(cmdEvent
);
1470 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1473 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1476 BeginBatchUndo(_("Insert Text"));
1478 long newPos
= m_caretPosition
;
1479 DeleteSelectedContent(& newPos
);
1482 wxString str
= event
.GetUnicodeKey();
1484 wxString str
= (wxChar
) event
.GetKeyCode();
1486 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1490 SetDefaultStyleToCursorStyle();
1491 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1493 cmdEvent
.SetPosition(m_caretPosition
);
1494 GetEventHandler()->ProcessEvent(cmdEvent
);
1502 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1504 wxRichTextAttr attr
;
1505 if (container
&& GetStyle(position
, attr
, container
))
1507 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1509 SetCursor(m_urlCursor
);
1511 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1513 SetCursor(m_textCursor
);
1521 /// Delete content if there is a selection, e.g. when pressing a key.
1522 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1526 long pos
= m_selection
.GetRange().GetStart();
1527 wxRichTextRange range
= m_selection
.GetRange();
1529 // SelectAll causes more to be selected than doing it interactively,
1530 // and causes a new paragraph to be inserted. So for multiline buffers,
1531 // don't delete the final position.
1532 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1533 range
.SetEnd(range
.GetEnd()-1);
1535 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1536 m_selection
.Reset();
1537 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1547 /// Keyboard navigation
1551 Left: left one character
1552 Right: right one character
1555 Ctrl-Left: left one word
1556 Ctrl-Right: right one word
1557 Ctrl-Up: previous paragraph start
1558 Ctrl-Down: next start of paragraph
1561 Ctrl-Home: start of document
1562 Ctrl-End: end of document
1563 Page-Up: Up a screen
1564 Page-Down: Down a screen
1568 Ctrl-Alt-PgUp: Start of window
1569 Ctrl-Alt-PgDn: End of window
1570 F8: Start selection mode
1571 Esc: End selection mode
1573 Adding Shift does the above but starts/extends selection.
1578 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1580 bool success
= false;
1582 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1584 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1585 success
= WordRight(1, flags
);
1587 success
= MoveRight(1, flags
);
1589 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1591 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1592 success
= WordLeft(1, flags
);
1594 success
= MoveLeft(1, flags
);
1596 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1598 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1599 success
= MoveToParagraphStart(flags
);
1601 success
= MoveUp(1, flags
);
1603 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1605 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1606 success
= MoveToParagraphEnd(flags
);
1608 success
= MoveDown(1, flags
);
1610 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1612 success
= PageUp(1, flags
);
1614 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1616 success
= PageDown(1, flags
);
1618 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1620 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1621 success
= MoveHome(flags
);
1623 success
= MoveToLineStart(flags
);
1625 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1627 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1628 success
= MoveEnd(flags
);
1630 success
= MoveToLineEnd(flags
);
1635 ScrollIntoView(m_caretPosition
, keyCode
);
1636 SetDefaultStyleToCursorStyle();
1642 /// Extend the selection. Selections are in caret positions.
1643 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1645 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1647 if (oldPos
== newPos
)
1650 wxRichTextSelection oldSelection
= m_selection
;
1652 m_selection
.SetContainer(GetFocusObject());
1654 wxRichTextRange oldRange
;
1655 if (m_selection
.IsValid())
1656 oldRange
= m_selection
.GetRange();
1658 oldRange
= wxRICHTEXT_NO_SELECTION
;
1659 wxRichTextRange newRange
;
1661 // If not currently selecting, start selecting
1662 if (oldRange
.GetStart() == -2)
1664 m_selectionAnchor
= oldPos
;
1666 if (oldPos
> newPos
)
1667 newRange
.SetRange(newPos
+1, oldPos
);
1669 newRange
.SetRange(oldPos
+1, newPos
);
1673 // Always ensure that the selection range start is greater than
1675 if (newPos
> m_selectionAnchor
)
1676 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1677 else if (newPos
== m_selectionAnchor
)
1678 newRange
= wxRichTextRange(-2, -2);
1680 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1683 m_selection
.SetRange(newRange
);
1685 RefreshForSelectionChange(oldSelection
, m_selection
);
1687 if (newRange
.GetStart() > newRange
.GetEnd())
1689 wxLogDebug(wxT("Strange selection range"));
1698 /// Scroll into view, returning true if we scrolled.
1699 /// This takes a _caret_ position.
1700 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1702 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1708 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1710 int startXUnits
, startYUnits
;
1711 GetViewStart(& startXUnits
, & startYUnits
);
1712 int startY
= startYUnits
* ppuY
;
1715 GetVirtualSize(& sx
, & sy
);
1721 wxRect rect
= line
->GetRect();
1723 bool scrolled
= false;
1725 wxSize clientSize
= GetClientSize();
1727 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1730 wxClientDC
dc(this);
1731 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1732 topMargin
, bottomMargin
);
1734 // clientSize.y -= GetBuffer().GetBottomMargin();
1735 clientSize
.y
-= bottomMargin
;
1737 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1739 int y
= rect
.y
- GetClientSize().y
/2;
1740 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1741 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1743 if (startYUnits
!= yUnits
)
1745 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1748 #if !wxRICHTEXT_USE_OWN_CARET
1758 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1759 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1760 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1761 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1763 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1765 // Make it scroll so this item is at the bottom
1767 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1768 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1770 // If we're still off the screen, scroll another line down
1771 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1774 if (startYUnits
!= yUnits
)
1776 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1780 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1782 // Make it scroll so this item is at the top
1784 int y
= rect
.y
- GetBuffer().GetTopMargin();
1785 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1787 if (startYUnits
!= yUnits
)
1789 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1795 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1796 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1797 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1798 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1800 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1802 // Make it scroll so this item is at the top
1804 int y
= rect
.y
- GetBuffer().GetTopMargin();
1805 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1807 if (startYUnits
!= yUnits
)
1809 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1813 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1815 // Make it scroll so this item is at the bottom
1817 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1818 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1820 // If we're still off the screen, scroll another line down
1821 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1824 if (startYUnits
!= yUnits
)
1826 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1832 #if !wxRICHTEXT_USE_OWN_CARET
1840 /// Is the given position visible on the screen?
1841 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1843 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1849 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1852 GetViewStart(& startX
, & startY
);
1854 startY
= startY
* ppuY
;
1856 wxRect rect
= line
->GetRect();
1857 wxSize clientSize
= GetClientSize();
1858 clientSize
.y
-= GetBuffer().GetBottomMargin();
1860 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1863 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1865 m_caretPosition
= position
;
1866 m_caretAtLineStart
= showAtLineStart
;
1869 /// Move caret one visual step forward: this may mean setting a flag
1870 /// and keeping the same position if we're going from the end of one line
1871 /// to the start of the next, which may be the exact same caret position.
1872 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1874 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1876 // Only do the check if we're not at the end of the paragraph (where things work OK
1878 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1880 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1884 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1886 // We're at the end of a line. See whether we need to
1887 // stay at the same actual caret position but change visual
1888 // position, or not.
1889 if (oldPosition
== lineRange
.GetEnd())
1891 if (m_caretAtLineStart
)
1893 // We're already at the start of the line, so actually move on now.
1894 m_caretPosition
= oldPosition
+ 1;
1895 m_caretAtLineStart
= false;
1899 // We're showing at the end of the line, so keep to
1900 // the same position but indicate that we're to show
1901 // at the start of the next line.
1902 m_caretPosition
= oldPosition
;
1903 m_caretAtLineStart
= true;
1905 SetDefaultStyleToCursorStyle();
1911 SetDefaultStyleToCursorStyle();
1914 /// Move caret one visual step backward: this may mean setting a flag
1915 /// and keeping the same position if we're going from the end of one line
1916 /// to the start of the next, which may be the exact same caret position.
1917 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1919 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1921 // Only do the check if we're not at the start of the paragraph (where things work OK
1923 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1925 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1929 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1931 // We're at the start of a line. See whether we need to
1932 // stay at the same actual caret position but change visual
1933 // position, or not.
1934 if (oldPosition
== lineRange
.GetStart())
1936 m_caretPosition
= oldPosition
-1;
1937 m_caretAtLineStart
= true;
1940 else if (oldPosition
== lineRange
.GetEnd())
1942 if (m_caretAtLineStart
)
1944 // We're at the start of the line, so keep the same caret position
1945 // but clear the start-of-line flag.
1946 m_caretPosition
= oldPosition
;
1947 m_caretAtLineStart
= false;
1951 // We're showing at the end of the line, so go back
1952 // to the previous character position.
1953 m_caretPosition
= oldPosition
- 1;
1955 SetDefaultStyleToCursorStyle();
1961 SetDefaultStyleToCursorStyle();
1965 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1967 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1969 if (m_caretPosition
+ noPositions
< endPos
)
1971 long oldPos
= m_caretPosition
;
1972 long newPos
= m_caretPosition
+ noPositions
;
1974 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1978 // Determine by looking at oldPos and m_caretPosition whether
1979 // we moved from the end of a line to the start of the next line, in which case
1980 // we want to adjust the caret position such that it is positioned at the
1981 // start of the next line, rather than jumping past the first character of the
1983 if (noPositions
== 1 && !extendSel
)
1984 MoveCaretForward(oldPos
);
1986 SetCaretPosition(newPos
);
1989 SetDefaultStyleToCursorStyle();
1998 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
2002 if (m_caretPosition
> startPos
- noPositions
+ 1)
2004 long oldPos
= m_caretPosition
;
2005 long newPos
= m_caretPosition
- noPositions
;
2006 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2010 if (noPositions
== 1 && !extendSel
)
2011 MoveCaretBack(oldPos
);
2013 SetCaretPosition(newPos
);
2016 SetDefaultStyleToCursorStyle();
2024 // Find the caret position for the combination of hit-test flags and character position.
2025 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2026 // since this is ambiguous (same position used for end of line and start of next).
2027 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2028 bool& caretLineStart
)
2030 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2031 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2032 // so we view the caret at the start of the line.
2033 caretLineStart
= false;
2034 long caretPosition
= position
;
2036 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2038 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2039 wxRichTextRange lineRange
;
2041 lineRange
= thisLine
->GetAbsoluteRange();
2043 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2046 caretLineStart
= true;
2050 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2051 if (para
&& para
->GetRange().GetStart() == position
)
2055 return caretPosition
;
2059 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2061 return MoveDown(- noLines
, flags
);
2065 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2070 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2071 wxPoint pt
= GetCaret()->GetPosition();
2072 long newLine
= lineNumber
+ noLines
;
2073 bool notInThisObject
= false;
2075 if (lineNumber
!= -1)
2079 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2080 if (newLine
> lastLine
)
2081 notInThisObject
= true;
2086 notInThisObject
= true;
2090 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2091 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
|wxRICHTEXT_HITTEST_HONOUR_ATOMIC
;
2093 if (notInThisObject
)
2095 // If we know we're navigating out of the current object,
2096 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2097 container
= & GetBuffer();
2098 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2100 if (noLines
> 0) // going down
2102 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2106 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2111 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2113 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2119 wxClientDC
dc(this);
2121 dc
.SetFont(GetFont());
2123 wxRichTextObject
* hitObj
= NULL
;
2124 wxRichTextObject
* contextObj
= NULL
;
2125 wxRichTextDrawingContext
context(& GetBuffer());
2126 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2129 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2130 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2133 if (notInThisObject
)
2135 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2136 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2138 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2140 container
= actualContainer
;
2144 bool caretLineStart
= true;
2145 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2146 long newSelEnd
= caretPosition
;
2149 if (notInThisObject
)
2152 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2157 SetCaretPosition(caretPosition
, caretLineStart
);
2159 SetDefaultStyleToCursorStyle();
2167 /// Move to the end of the paragraph
2168 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2170 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2173 long newPos
= para
->GetRange().GetEnd() - 1;
2174 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2178 SetCaretPosition(newPos
);
2180 SetDefaultStyleToCursorStyle();
2188 /// Move to the start of the paragraph
2189 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2191 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2194 long newPos
= para
->GetRange().GetStart() - 1;
2195 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2199 SetCaretPosition(newPos
);
2201 SetDefaultStyleToCursorStyle();
2209 /// Move to the end of the line
2210 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2212 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2216 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2217 long newPos
= lineRange
.GetEnd();
2218 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2222 SetCaretPosition(newPos
);
2224 SetDefaultStyleToCursorStyle();
2232 /// Move to the start of the line
2233 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2235 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2238 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2239 long newPos
= lineRange
.GetStart()-1;
2241 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2245 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2247 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2249 SetDefaultStyleToCursorStyle();
2257 /// Move to the start of the buffer
2258 bool wxRichTextCtrl::MoveHome(int flags
)
2260 if (m_caretPosition
!= -1)
2262 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2266 SetCaretPosition(-1);
2268 SetDefaultStyleToCursorStyle();
2276 /// Move to the end of the buffer
2277 bool wxRichTextCtrl::MoveEnd(int flags
)
2279 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2281 if (m_caretPosition
!= endPos
)
2283 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2287 SetCaretPosition(endPos
);
2289 SetDefaultStyleToCursorStyle();
2297 /// Move noPages pages up
2298 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2300 return PageDown(- noPages
, flags
);
2303 /// Move noPages pages down
2304 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2306 // Calculate which line occurs noPages * screen height further down.
2307 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2310 wxSize clientSize
= GetClientSize();
2311 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2313 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2316 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2317 long pos
= lineRange
.GetStart()-1;
2318 if (pos
!= m_caretPosition
)
2320 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2322 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2326 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2328 SetDefaultStyleToCursorStyle();
2338 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2340 return str
== wxT(" ") || str
== wxT("\t") || (!str
.empty() && (str
[0] == (wxChar
) 160));
2343 // Finds the caret position for the next word
2344 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2346 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2350 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2352 // First skip current text to space
2353 while (i
< endPos
&& i
> -1)
2355 // i is in character, not caret positions
2356 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2357 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2358 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2362 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2369 while (i
< endPos
&& i
> -1)
2371 // i is in character, not caret positions
2372 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2373 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2374 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2375 return wxMax(-1, i
);
2377 if (text
.empty()) // End of paragraph, or maybe an image
2378 return wxMax(-1, i
- 1);
2379 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2383 // Convert to caret position
2384 return wxMax(-1, i
- 1);
2393 long i
= m_caretPosition
;
2395 // First skip white space
2396 while (i
< endPos
&& i
> -1)
2398 // i is in character, not caret positions
2399 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2400 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2402 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2404 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2409 // Next skip current text to space
2410 while (i
< endPos
&& i
> -1)
2412 // i is in character, not caret positions
2413 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2414 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2415 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2418 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2431 /// Move n words left
2432 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2434 long pos
= FindNextWordPosition(-1);
2435 if (pos
!= m_caretPosition
)
2437 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2439 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2443 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2445 SetDefaultStyleToCursorStyle();
2453 /// Move n words right
2454 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2456 long pos
= FindNextWordPosition(1);
2457 if (pos
!= m_caretPosition
)
2459 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2461 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2465 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2467 SetDefaultStyleToCursorStyle();
2476 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2478 // Only do sizing optimization for large buffers
2479 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2481 m_fullLayoutRequired
= true;
2482 m_fullLayoutTime
= wxGetLocalTimeMillis();
2483 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2484 LayoutContent(true /* onlyVisibleRect */);
2487 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2489 #if wxRICHTEXT_BUFFERED_PAINTING
2496 // Force any pending layout due to large buffer
2497 void wxRichTextCtrl::ForceDelayedLayout()
2499 if (m_fullLayoutRequired
)
2501 m_fullLayoutRequired
= false;
2502 m_fullLayoutTime
= 0;
2503 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2504 ShowPosition(m_fullLayoutSavedPosition
);
2510 /// Idle-time processing
2511 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2513 #if wxRICHTEXT_USE_OWN_CARET
2514 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2516 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2522 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2524 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2526 m_fullLayoutRequired
= false;
2527 m_fullLayoutTime
= 0;
2528 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2529 ShowPosition(m_fullLayoutSavedPosition
);
2533 if (m_caretPositionForDefaultStyle
!= -2)
2535 // If the caret position has changed, no longer reflect the default style
2537 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2538 m_caretPositionForDefaultStyle
= -2;
2545 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2547 #if wxRICHTEXT_USE_OWN_CARET
2548 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2551 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2558 /// Set up scrollbars, e.g. after a resize
2559 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2564 if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled
)
2566 SetScrollbars(0, 0, 0, 0, 0, 0);
2570 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2571 // of pixels. See e.g. wxVScrolledWindow for ideas.
2572 int pixelsPerUnit
= 5;
2573 wxSize clientSize
= GetClientSize();
2575 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2577 // Round up so we have at least maxHeight pixels
2578 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2580 int startX
= 0, startY
= 0;
2582 GetViewStart(& startX
, & startY
);
2584 int maxPositionX
= 0;
2585 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2587 int newStartX
= wxMin(maxPositionX
, startX
);
2588 int newStartY
= wxMin(maxPositionY
, startY
);
2590 int oldPPUX
, oldPPUY
;
2591 int oldStartX
, oldStartY
;
2592 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2593 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2594 GetViewStart(& oldStartX
, & oldStartY
);
2595 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2597 oldVirtualSizeY
/= oldPPUY
;
2599 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2602 // Don't set scrollbars if there were none before, and there will be none now.
2603 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2606 // Move to previous scroll position if
2608 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2611 /// Paint the background
2612 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2614 wxColour backgroundColour
= GetBackgroundColour();
2615 if (!backgroundColour
.IsOk())
2616 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2618 // Clear the background
2619 dc
.SetBrush(wxBrush(backgroundColour
));
2620 dc
.SetPen(*wxTRANSPARENT_PEN
);
2621 wxRect
windowRect(GetClientSize());
2622 windowRect
.x
-= 2; windowRect
.y
-= 2;
2623 windowRect
.width
+= 4; windowRect
.height
+= 4;
2625 // We need to shift the rectangle to take into account
2626 // scrolling. Converting device to logical coordinates.
2627 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2628 dc
.DrawRectangle(windowRect
);
2631 #if wxRICHTEXT_BUFFERED_PAINTING
2632 /// Recreate buffer bitmap if necessary
2633 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2636 if (sz
== wxDefaultSize
)
2637 sz
= GetClientSize();
2639 if (sz
.x
< 1 || sz
.y
< 1)
2642 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2643 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2644 return m_bufferBitmap
.IsOk();
2648 // ----------------------------------------------------------------------------
2649 // file IO functions
2650 // ----------------------------------------------------------------------------
2652 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2654 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2656 m_filename
= filename
;
2659 SetInsertionPoint(0);
2662 SetupScrollbars(true);
2664 wxTextCtrl::SendTextUpdatedEvent(this);
2670 wxLogError(_("File couldn't be loaded."));
2676 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2678 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2680 m_filename
= filename
;
2687 wxLogError(_("The text couldn't be saved."));
2692 // ----------------------------------------------------------------------------
2693 // wxRichTextCtrl specific functionality
2694 // ----------------------------------------------------------------------------
2696 /// Add a new paragraph of text to the end of the buffer
2697 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2699 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2700 GetBuffer().Invalidate();
2706 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2708 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2709 GetBuffer().Invalidate();
2714 // ----------------------------------------------------------------------------
2715 // selection and ranges
2716 // ----------------------------------------------------------------------------
2718 void wxRichTextCtrl::SelectAll()
2720 SetSelection(-1, -1);
2724 void wxRichTextCtrl::SelectNone()
2726 if (m_selection
.IsValid())
2728 wxRichTextSelection oldSelection
= m_selection
;
2730 m_selection
.Reset();
2732 RefreshForSelectionChange(oldSelection
, m_selection
);
2734 m_selectionAnchor
= -2;
2735 m_selectionAnchorObject
= NULL
;
2736 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2739 static bool wxIsWordDelimiter(const wxString
& text
)
2741 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2744 /// Select the word at the given character position
2745 bool wxRichTextCtrl::SelectWord(long position
)
2747 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2750 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2754 if (position
== para
->GetRange().GetEnd())
2757 long positionStart
= position
;
2758 long positionEnd
= position
;
2760 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2762 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2763 if (wxIsWordDelimiter(text
))
2769 if (positionStart
< para
->GetRange().GetStart())
2770 positionStart
= para
->GetRange().GetStart();
2772 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2774 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2775 if (wxIsWordDelimiter(text
))
2781 if (positionEnd
>= para
->GetRange().GetEnd())
2782 positionEnd
= para
->GetRange().GetEnd();
2784 if (positionEnd
< positionStart
)
2787 SetSelection(positionStart
, positionEnd
+1);
2789 if (positionStart
>= 0)
2791 MoveCaret(positionStart
-1, true);
2792 SetDefaultStyleToCursorStyle();
2798 wxString
wxRichTextCtrl::GetStringSelection() const
2801 GetSelection(&from
, &to
);
2803 return GetRange(from
, to
);
2806 // ----------------------------------------------------------------------------
2808 // ----------------------------------------------------------------------------
2810 wxTextCtrlHitTestResult
2811 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2813 // implement in terms of the other overload as the native ports typically
2814 // can get the position and not (x, y) pair directly (although wxUniv
2815 // directly gets x and y -- and so overrides this method as well)
2817 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2819 if ( rc
!= wxTE_HT_UNKNOWN
)
2821 PositionToXY(pos
, x
, y
);
2827 wxTextCtrlHitTestResult
2828 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2831 wxClientDC
dc((wxRichTextCtrl
*) this);
2832 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2834 // Buffer uses logical position (relative to start of buffer)
2836 wxPoint pt2
= GetLogicalPoint(pt
);
2838 wxRichTextObject
* hitObj
= NULL
;
2839 wxRichTextObject
* contextObj
= NULL
;
2840 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2841 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2843 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2844 return wxTE_HT_BEFORE
;
2845 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2846 return wxTE_HT_BEYOND
;
2847 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2848 return wxTE_HT_ON_TEXT
;
2850 return wxTE_HT_UNKNOWN
;
2853 wxRichTextParagraphLayoutBox
*
2854 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2856 wxClientDC
dc(this);
2858 dc
.SetFont(GetFont());
2860 wxPoint logicalPt
= GetLogicalPoint(pt
);
2862 wxRichTextObject
* contextObj
= NULL
;
2863 wxRichTextDrawingContext
context(& GetBuffer());
2864 hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, &hitObj
, &contextObj
, flags
);
2865 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2871 // ----------------------------------------------------------------------------
2872 // set/get the controls text
2873 // ----------------------------------------------------------------------------
2875 wxString
wxRichTextCtrl::DoGetValue() const
2877 return GetBuffer().GetText();
2880 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2882 // Public API for range is different from internals
2883 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2886 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2888 // Don't call Clear here, since it always sends a text updated event
2889 m_buffer
.ResetAndClearCommands();
2890 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2891 m_caretPosition
= -1;
2892 m_caretPositionForDefaultStyle
= -2;
2893 m_caretAtLineStart
= false;
2894 m_selection
.Reset();
2895 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2905 if (!value
.IsEmpty())
2907 // Remove empty paragraph
2908 GetBuffer().Clear();
2909 DoWriteText(value
, flags
);
2911 // for compatibility, don't move the cursor when doing SetValue()
2912 SetInsertionPoint(0);
2916 // still send an event for consistency
2917 if (flags
& SetValue_SendEvent
)
2918 wxTextCtrl::SendTextUpdatedEvent(this);
2923 void wxRichTextCtrl::WriteText(const wxString
& value
)
2928 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2930 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2932 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2933 GetBuffer().Defragment();
2935 if ( flags
& SetValue_SendEvent
)
2936 wxTextCtrl::SendTextUpdatedEvent(this);
2939 void wxRichTextCtrl::AppendText(const wxString
& text
)
2941 SetInsertionPointEnd();
2946 /// Write an image at the current insertion point
2947 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2949 wxRichTextImageBlock imageBlock
;
2951 wxImage image2
= image
;
2952 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2953 return WriteImage(imageBlock
, textAttr
);
2958 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2960 wxRichTextImageBlock imageBlock
;
2963 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2964 return WriteImage(imageBlock
, textAttr
);
2969 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2971 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2974 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2978 wxRichTextImageBlock imageBlock
;
2980 wxImage image
= bitmap
.ConvertToImage();
2981 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2982 return WriteImage(imageBlock
, textAttr
);
2988 // Write a text box at the current insertion point.
2989 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2991 wxRichTextBox
* textBox
= new wxRichTextBox
;
2992 textBox
->SetAttributes(textAttr
);
2993 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2994 textBox
->AddParagraph(wxEmptyString
);
2995 textBox
->SetParent(NULL
);
2997 // The object returned is the one actually inserted into the buffer,
2998 // while the original one is deleted.
2999 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3000 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
3004 wxRichTextField
* wxRichTextCtrl::WriteField(const wxString
& fieldType
, const wxRichTextProperties
& properties
,
3005 const wxRichTextAttr
& textAttr
)
3007 return GetFocusObject()->InsertFieldWithUndo(& GetBuffer(), m_caretPosition
+1, fieldType
, properties
,
3008 this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
, textAttr
);
3011 // Write a table at the current insertion point, returning the table.
3012 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3014 wxASSERT(rows
> 0 && cols
> 0);
3016 if (rows
<= 0 || cols
<= 0)
3019 wxRichTextTable
* table
= new wxRichTextTable
;
3020 table
->SetAttributes(tableAttr
);
3021 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3023 table
->CreateTable(rows
, cols
);
3025 table
->SetParent(NULL
);
3028 for (j
= 0; j
< rows
; j
++)
3030 for (i
= 0; i
< cols
; i
++)
3032 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
3036 // The object returned is the one actually inserted into the buffer,
3037 // while the original one is deleted.
3038 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3039 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3044 /// Insert a newline (actually paragraph) at the current insertion point.
3045 bool wxRichTextCtrl::Newline()
3047 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3050 /// Insert a line break at the current insertion point.
3051 bool wxRichTextCtrl::LineBreak()
3054 text
= wxRichTextLineBreakChar
;
3055 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3058 // ----------------------------------------------------------------------------
3059 // Clipboard operations
3060 // ----------------------------------------------------------------------------
3062 void wxRichTextCtrl::Copy()
3066 wxRichTextRange range
= GetInternalSelectionRange();
3067 GetBuffer().CopyToClipboard(range
);
3071 void wxRichTextCtrl::Cut()
3075 wxRichTextRange range
= GetInternalSelectionRange();
3076 GetBuffer().CopyToClipboard(range
);
3078 DeleteSelectedContent();
3084 void wxRichTextCtrl::Paste()
3088 BeginBatchUndo(_("Paste"));
3090 long newPos
= m_caretPosition
;
3091 DeleteSelectedContent(& newPos
);
3093 GetBuffer().PasteFromClipboard(newPos
);
3099 void wxRichTextCtrl::DeleteSelection()
3101 if (CanDeleteSelection())
3103 DeleteSelectedContent();
3107 bool wxRichTextCtrl::HasSelection() const
3109 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3112 bool wxRichTextCtrl::HasUnfocusedSelection() const
3114 return m_selection
.IsValid();
3117 bool wxRichTextCtrl::CanCopy() const
3119 // Can copy if there's a selection
3120 return HasSelection();
3123 bool wxRichTextCtrl::CanCut() const
3125 return CanDeleteSelection();
3128 bool wxRichTextCtrl::CanPaste() const
3130 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3133 return GetBuffer().CanPasteFromClipboard();
3136 bool wxRichTextCtrl::CanDeleteSelection() const
3138 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3142 // ----------------------------------------------------------------------------
3144 // ----------------------------------------------------------------------------
3146 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3148 if (m_contextMenu
&& m_contextMenu
!= menu
)
3149 delete m_contextMenu
;
3150 m_contextMenu
= menu
;
3153 void wxRichTextCtrl::SetEditable(bool editable
)
3155 m_editable
= editable
;
3158 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3162 m_caretPosition
= pos
- 1;
3166 SetDefaultStyleToCursorStyle();
3169 void wxRichTextCtrl::SetInsertionPointEnd()
3171 long pos
= GetLastPosition();
3172 SetInsertionPoint(pos
);
3175 long wxRichTextCtrl::GetInsertionPoint() const
3177 return m_caretPosition
+1;
3180 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3182 return GetFocusObject()->GetOwnRange().GetEnd();
3185 // If the return values from and to are the same, there is no
3187 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3189 if (m_selection
.IsValid())
3191 *from
= m_selection
.GetRange().GetStart();
3192 *to
= m_selection
.GetRange().GetEnd();
3202 bool wxRichTextCtrl::IsEditable() const
3207 // ----------------------------------------------------------------------------
3209 // ----------------------------------------------------------------------------
3211 void wxRichTextCtrl::SetSelection(long from
, long to
)
3213 // if from and to are both -1, it means (in wxWidgets) that all text should
3215 if ( (from
== -1) && (to
== -1) )
3218 to
= GetLastPosition()+1;
3227 wxRichTextSelection oldSelection
= m_selection
;
3229 m_selectionAnchor
= from
-1;
3230 m_selectionAnchorObject
= NULL
;
3231 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3233 m_caretPosition
= wxMax(-1, to
-1);
3235 RefreshForSelectionChange(oldSelection
, m_selection
);
3240 // ----------------------------------------------------------------------------
3242 // ----------------------------------------------------------------------------
3244 void wxRichTextCtrl::Replace(long from
, long to
,
3245 const wxString
& value
)
3247 BeginBatchUndo(_("Replace"));
3249 SetSelection(from
, to
);
3251 wxRichTextAttr attr
= GetDefaultStyle();
3253 DeleteSelectedContent();
3255 SetDefaultStyle(attr
);
3257 DoWriteText(value
, SetValue_SelectionOnly
);
3262 void wxRichTextCtrl::Remove(long from
, long to
)
3266 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3273 bool wxRichTextCtrl::IsModified() const
3275 return m_buffer
.IsModified();
3278 void wxRichTextCtrl::MarkDirty()
3280 m_buffer
.Modify(true);
3283 void wxRichTextCtrl::DiscardEdits()
3285 m_caretPositionForDefaultStyle
= -2;
3286 m_buffer
.Modify(false);
3287 m_buffer
.GetCommandProcessor()->ClearCommands();
3290 int wxRichTextCtrl::GetNumberOfLines() const
3292 return GetFocusObject()->GetParagraphCount();
3295 // ----------------------------------------------------------------------------
3296 // Positions <-> coords
3297 // ----------------------------------------------------------------------------
3299 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3301 return GetFocusObject()->XYToPosition(x
, y
);
3304 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3306 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3309 // ----------------------------------------------------------------------------
3311 // ----------------------------------------------------------------------------
3313 void wxRichTextCtrl::ShowPosition(long pos
)
3315 if (!IsPositionVisible(pos
))
3316 ScrollIntoView(pos
-1, WXK_DOWN
);
3319 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3321 return GetFocusObject()->GetParagraphLength(lineNo
);
3324 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3326 return GetFocusObject()->GetParagraphText(lineNo
);
3329 // ----------------------------------------------------------------------------
3331 // ----------------------------------------------------------------------------
3333 void wxRichTextCtrl::Undo()
3337 GetCommandProcessor()->Undo();
3341 void wxRichTextCtrl::Redo()
3345 GetCommandProcessor()->Redo();
3349 bool wxRichTextCtrl::CanUndo() const
3351 return GetCommandProcessor()->CanUndo() && IsEditable();
3354 bool wxRichTextCtrl::CanRedo() const
3356 return GetCommandProcessor()->CanRedo() && IsEditable();
3359 // ----------------------------------------------------------------------------
3360 // implementation details
3361 // ----------------------------------------------------------------------------
3363 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3365 SetValue(event
.GetString());
3366 GetEventHandler()->ProcessEvent(event
);
3369 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3371 // By default, load the first file into the text window.
3372 if (event
.GetNumberOfFiles() > 0)
3374 LoadFile(event
.GetFiles()[0]);
3378 wxSize
wxRichTextCtrl::DoGetBestSize() const
3380 return wxSize(10, 10);
3383 // ----------------------------------------------------------------------------
3384 // standard handlers for standard edit menu events
3385 // ----------------------------------------------------------------------------
3387 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3392 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3397 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3402 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3407 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3412 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3417 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3419 event
.Enable( CanCut() );
3422 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3424 event
.Enable( CanCopy() );
3427 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3429 event
.Enable( CanDeleteSelection() );
3432 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3434 event
.Enable( CanPaste() );
3437 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3439 event
.Enable( CanUndo() );
3440 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3443 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3445 event
.Enable( CanRedo() );
3446 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3449 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3451 if (GetLastPosition() > 0)
3455 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3457 event
.Enable(GetLastPosition() > 0);
3460 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3462 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3463 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3465 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3466 if (obj
&& CanEditProperties(obj
))
3467 EditProperties(obj
, this);
3469 m_contextMenuPropertiesInfo
.Clear();
3473 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3475 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3476 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3479 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3481 if (event
.GetEventObject() != this)
3487 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3490 // Prepares the context menu, adding appropriate property-editing commands.
3491 // Returns the number of property commands added.
3492 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3494 wxClientDC
dc(this);
3496 dc
.SetFont(GetFont());
3498 m_contextMenuPropertiesInfo
.Clear();
3501 wxRichTextObject
* hitObj
= NULL
;
3502 wxRichTextObject
* contextObj
= NULL
;
3503 if (pt
!= wxDefaultPosition
)
3505 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3506 wxRichTextDrawingContext
context(& GetBuffer());
3507 int hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
3509 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3511 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3512 if (hitObj
&& actualContainer
)
3514 if (actualContainer
->AcceptsFocus())
3516 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3517 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3520 if (addPropertyCommands
)
3521 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3525 if (addPropertyCommands
)
3526 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3531 if (addPropertyCommands
)
3532 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3537 // Invoked from the keyboard, so don't set the caret position and don't use the event
3539 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3541 contextObj
= hitObj
->GetParentContainer();
3543 contextObj
= GetFocusObject();
3545 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3546 if (hitObj
&& actualContainer
)
3548 if (addPropertyCommands
)
3549 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3553 if (addPropertyCommands
)
3554 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3560 if (addPropertyCommands
)
3561 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3562 return m_contextMenuPropertiesInfo
.GetCount();
3568 // Shows the context menu, adding appropriate property-editing commands
3569 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3573 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3581 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3583 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3586 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3588 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3591 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3593 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3596 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3598 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3601 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
, int flags
)
3603 GetFocusObject()->SetStyle(obj
, textAttr
, flags
);
3606 // extended style setting operation with flags including:
3607 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3608 // see richtextbuffer.h for more details.
3610 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3612 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3615 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3617 return GetBuffer().SetDefaultStyle(style
);
3620 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3622 wxRichTextAttr
attr1(style
);
3623 attr1
.GetTextBoxAttr().Reset();
3624 return GetBuffer().SetDefaultStyle(attr1
);
3627 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3629 return GetBuffer().GetDefaultStyle();
3632 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3634 wxRichTextAttr attr
;
3635 if (GetFocusObject()->GetStyle(position
, attr
))
3644 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3646 return GetFocusObject()->GetStyle(position
, style
);
3649 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3651 wxRichTextAttr attr
;
3652 if (container
->GetStyle(position
, attr
))
3661 // get the common set of styles for the range
3662 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3664 wxRichTextAttr attr
;
3665 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3674 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3676 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3679 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3681 return container
->GetStyleForRange(range
.ToInternal(), style
);
3684 /// Get the content (uncombined) attributes for this position.
3685 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3687 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3690 /// Get the content (uncombined) attributes for this position.
3691 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3693 return container
->GetUncombinedStyle(position
, style
);
3696 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3698 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3701 /// Set font, and also the buffer attributes
3702 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3704 wxControl::SetFont(font
);
3706 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3708 GetBuffer().SetBasicStyle(attr
);
3710 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3716 /// Transform logical to physical
3717 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3720 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3725 /// Transform physical to logical
3726 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3729 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3734 /// Position the caret
3735 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3740 //wxLogDebug(wxT("PositionCaret"));
3743 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3745 wxPoint newPt
= caretRect
.GetPosition();
3746 wxSize newSz
= caretRect
.GetSize();
3747 wxPoint pt
= GetPhysicalPoint(newPt
);
3748 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3751 if (GetCaret()->GetSize() != newSz
)
3752 GetCaret()->SetSize(newSz
);
3754 // Adjust size so the caret size and position doesn't appear in the margins
3755 if (((pt
.y
+ newSz
.y
) <= GetBuffer().GetTopMargin()) || (pt
.y
>= (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3760 else if (pt
.y
< GetBuffer().GetTopMargin() && (pt
.y
+ newSz
.y
) > GetBuffer().GetTopMargin())
3762 newSz
.y
-= (GetBuffer().GetTopMargin() - pt
.y
);
3765 pt
.y
= GetBuffer().GetTopMargin();
3766 GetCaret()->SetSize(newSz
);
3769 else if (pt
.y
< (GetClientSize().y
- GetBuffer().GetBottomMargin()) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- GetBuffer().GetBottomMargin()))
3771 newSz
.y
= GetClientSize().y
- GetBuffer().GetBottomMargin() - pt
.y
;
3772 GetCaret()->SetSize(newSz
);
3775 GetCaret()->Move(pt
);
3781 /// Get the caret height and position for the given character position
3782 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3784 wxClientDC
dc(this);
3785 dc
.SetFont(GetFont());
3793 container
= GetFocusObject();
3795 wxRichTextDrawingContext
context(& GetBuffer());
3796 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3798 // Caret height can't be zero
3800 height
= dc
.GetCharHeight();
3802 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3809 /// Gets the line for the visible caret position. If the caret is
3810 /// shown at the very end of the line, it means the next character is actually
3811 /// on the following line. So let's get the line we're expecting to find
3812 /// if this is the case.
3813 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3815 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3816 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3819 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3820 if (caretPosition
== lineRange
.GetStart()-1 &&
3821 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3823 if (!m_caretAtLineStart
)
3824 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3831 /// Move the caret to the given character position
3832 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3834 if (GetBuffer().IsDirty())
3838 container
= GetFocusObject();
3840 if (pos
<= container
->GetOwnRange().GetEnd())
3842 SetCaretPosition(pos
, showAtLineStart
);
3844 PositionCaret(container
);
3852 /// Layout the buffer: which we must do before certain operations, such as
3853 /// setting the caret position.
3854 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3856 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3858 wxRect
availableSpace(GetClientSize());
3859 if (availableSpace
.width
== 0)
3860 availableSpace
.width
= 10;
3861 if (availableSpace
.height
== 0)
3862 availableSpace
.height
= 10;
3864 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3865 if (onlyVisibleRect
)
3867 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3868 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3871 wxClientDC
dc(this);
3872 dc
.SetFont(GetFont());
3876 wxRichTextDrawingContext
context(& GetBuffer());
3877 GetBuffer().Defragment();
3878 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3879 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3880 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3889 /// Is all of the selection, or the current caret position, bold?
3890 bool wxRichTextCtrl::IsSelectionBold()
3894 wxRichTextAttr attr
;
3895 wxRichTextRange range
= GetSelectionRange();
3896 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3897 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3899 return HasCharacterAttributes(range
, attr
);
3903 // If no selection, then we need to combine current style with default style
3904 // to see what the effect would be if we started typing.
3905 wxRichTextAttr attr
;
3906 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3908 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3909 if (GetStyle(pos
, attr
))
3911 if (IsDefaultStyleShowing())
3912 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3913 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3919 /// Is all of the selection, or the current caret position, italics?
3920 bool wxRichTextCtrl::IsSelectionItalics()
3924 wxRichTextRange range
= GetSelectionRange();
3925 wxRichTextAttr attr
;
3926 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3927 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3929 return HasCharacterAttributes(range
, attr
);
3933 // If no selection, then we need to combine current style with default style
3934 // to see what the effect would be if we started typing.
3935 wxRichTextAttr attr
;
3936 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3938 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3939 if (GetStyle(pos
, attr
))
3941 if (IsDefaultStyleShowing())
3942 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3943 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3949 /// Is all of the selection, or the current caret position, underlined?
3950 bool wxRichTextCtrl::IsSelectionUnderlined()
3954 wxRichTextRange range
= GetSelectionRange();
3955 wxRichTextAttr attr
;
3956 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3957 attr
.SetFontUnderlined(true);
3959 return HasCharacterAttributes(range
, attr
);
3963 // If no selection, then we need to combine current style with default style
3964 // to see what the effect would be if we started typing.
3965 wxRichTextAttr attr
;
3966 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3967 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3969 if (GetStyle(pos
, attr
))
3971 if (IsDefaultStyleShowing())
3972 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3973 return attr
.GetFontUnderlined();
3979 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3980 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3982 wxRichTextAttr attr
;
3983 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3984 attr
.SetTextEffectFlags(flag
);
3985 attr
.SetTextEffects(flag
);
3989 return HasCharacterAttributes(GetSelectionRange(), attr
);
3993 // If no selection, then we need to combine current style with default style
3994 // to see what the effect would be if we started typing.
3995 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3996 if (GetStyle(pos
, attr
))
3998 if (IsDefaultStyleShowing())
3999 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
4000 return (attr
.GetTextEffectFlags() & flag
) != 0;
4006 /// Apply bold to the selection
4007 bool wxRichTextCtrl::ApplyBoldToSelection()
4009 wxRichTextAttr attr
;
4010 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
4011 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4014 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4017 wxRichTextAttr current
= GetDefaultStyleEx();
4018 current
.Apply(attr
);
4019 SetAndShowDefaultStyle(current
);
4024 /// Apply italic to the selection
4025 bool wxRichTextCtrl::ApplyItalicToSelection()
4027 wxRichTextAttr attr
;
4028 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4029 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4032 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4035 wxRichTextAttr current
= GetDefaultStyleEx();
4036 current
.Apply(attr
);
4037 SetAndShowDefaultStyle(current
);
4042 /// Apply underline to the selection
4043 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4045 wxRichTextAttr attr
;
4046 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4047 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4050 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4053 wxRichTextAttr current
= GetDefaultStyleEx();
4054 current
.Apply(attr
);
4055 SetAndShowDefaultStyle(current
);
4060 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4061 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4063 wxRichTextAttr attr
;
4064 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4065 attr
.SetTextEffectFlags(flags
);
4066 if (!DoesSelectionHaveTextEffectFlag(flags
))
4067 attr
.SetTextEffects(flags
);
4069 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4072 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4075 wxRichTextAttr current
= GetDefaultStyleEx();
4076 current
.Apply(attr
);
4077 SetAndShowDefaultStyle(current
);
4082 /// Is all of the selection aligned according to the specified flag?
4083 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4085 wxRichTextRange range
;
4087 range
= GetSelectionRange();
4089 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4091 wxRichTextAttr attr
;
4092 attr
.SetAlignment(alignment
);
4094 return HasParagraphAttributes(range
, attr
);
4097 /// Apply alignment to the selection
4098 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4100 wxRichTextAttr attr
;
4101 attr
.SetAlignment(alignment
);
4103 return SetStyle(GetSelectionRange(), attr
);
4106 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4108 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4113 /// Apply a named style to the selection
4114 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4116 // Flags are defined within each definition, so only certain
4117 // attributes are applied.
4118 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4120 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4122 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
4124 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4126 wxRichTextRange range
;
4129 range
= GetSelectionRange();
4132 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4133 range
= wxRichTextRange(pos
, pos
+1);
4136 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4139 bool isPara
= false;
4141 // Make sure the attr has the style name
4142 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
4145 attr
.SetParagraphStyleName(def
->GetName());
4147 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4148 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4149 // to change its style independently.
4150 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4152 else if (def
->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition
)))
4153 attr
.SetCharacterStyleName(def
->GetName());
4154 else if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4155 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4157 if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4159 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4161 SetStyle(GetFocusObject(), attr
);
4167 else if (HasSelection())
4168 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4171 wxRichTextAttr current
= GetDefaultStyleEx();
4172 wxRichTextAttr
defaultStyle(attr
);
4175 // Don't apply extra character styles since they are already implied
4176 // in the paragraph style
4177 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4179 current
.Apply(defaultStyle
);
4180 SetAndShowDefaultStyle(current
);
4182 // If it's a paragraph style, we want to apply the style to the
4183 // current paragraph even if we didn't select any text.
4186 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4187 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4190 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4197 /// Apply the style sheet to the buffer, for example if the styles have changed.
4198 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4201 styleSheet
= GetBuffer().GetStyleSheet();
4205 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4207 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4215 /// Sets the default style to the style under the cursor
4216 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4218 wxRichTextAttr attr
;
4219 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4221 // If at the start of a paragraph, use the next position.
4222 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4224 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4225 if (obj
&& obj
->IsTopLevel())
4227 // Don't use the attributes of a top-level object, since they might apply
4228 // to content of the object, e.g. background colour.
4229 SetDefaultStyle(wxRichTextAttr());
4232 else if (GetUncombinedStyle(pos
, attr
))
4234 SetDefaultStyle(attr
);
4241 /// Returns the first visible position in the current view
4242 long wxRichTextCtrl::GetFirstVisiblePosition() const
4244 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
4246 return line
->GetAbsoluteRange().GetStart();
4251 /// Get the first visible point in the window
4252 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4255 int startXUnits
, startYUnits
;
4257 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4258 GetViewStart(& startXUnits
, & startYUnits
);
4260 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4263 /// The adjusted caret position is the character position adjusted to take
4264 /// into account whether we're at the start of a paragraph, in which case
4265 /// style information should be taken from the next position, not current one.
4266 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4268 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4270 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4275 /// Get/set the selection range in character positions. -1, -1 means no selection.
4276 /// The range is in API convention, i.e. a single character selection is denoted
4278 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4280 wxRichTextRange range
= GetInternalSelectionRange();
4281 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4282 range
.SetEnd(range
.GetEnd() + 1);
4286 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4288 SetSelection(range
.GetStart(), range
.GetEnd());
4292 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4294 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4297 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4299 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4302 /// Clear list for given range
4303 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4305 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4308 /// Number/renumber any list elements in the given range
4309 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4311 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4314 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4316 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4319 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4320 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4322 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4325 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4327 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4330 /// Deletes the content in the given range
4331 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4333 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4336 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4338 if (sm_availableFontNames
.GetCount() == 0)
4340 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4341 sm_availableFontNames
.Sort();
4343 return sm_availableFontNames
;
4346 void wxRichTextCtrl::ClearAvailableFontNames()
4348 sm_availableFontNames
.Clear();
4351 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4353 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4355 wxTextAttrEx basicStyle
= GetBasicStyle();
4356 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4357 SetBasicStyle(basicStyle
);
4358 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4363 // Refresh the area affected by a selection change
4364 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4366 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4367 // the selection contains whole containers rather than just text, so refresh everything
4368 // for now as it would be hard to compute the rectangle bounding all selections.
4369 // TODO: improve on this.
4370 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4371 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4377 wxRichTextRange oldRange
, newRange
;
4378 if (oldSelection
.IsValid())
4379 oldRange
= oldSelection
.GetRange();
4381 oldRange
= wxRICHTEXT_NO_SELECTION
;
4382 if (newSelection
.IsValid())
4383 newRange
= newSelection
.GetRange();
4385 newRange
= wxRICHTEXT_NO_SELECTION
;
4387 // Calculate the refresh rectangle - just the affected lines
4388 long firstPos
, lastPos
;
4389 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4391 firstPos
= newRange
.GetStart();
4392 lastPos
= newRange
.GetEnd();
4394 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4396 firstPos
= oldRange
.GetStart();
4397 lastPos
= oldRange
.GetEnd();
4399 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4405 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4406 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4409 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4410 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4412 if (firstLine
&& lastLine
)
4414 wxSize clientSize
= GetClientSize();
4415 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
4416 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
4419 pt1
.y
= wxMax(0, pt1
.y
);
4421 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4423 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4424 RefreshRect(rect
, false);
4432 // margins functions
4433 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4435 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4436 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4437 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4438 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4443 wxPoint
wxRichTextCtrl::DoGetMargins() const
4445 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4446 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4449 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4451 if (obj
&& !obj
->AcceptsFocus())
4454 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4455 bool changingContainer
= (m_focusObject
!= obj
);
4457 if (changingContainer
&& HasSelection())
4460 m_focusObject
= obj
;
4463 m_focusObject
= & m_buffer
;
4465 if (setCaretPosition
&& changingContainer
)
4467 m_selection
.Reset();
4468 m_selectionAnchor
= -2;
4469 m_selectionAnchorObject
= NULL
;
4470 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4474 m_caretAtLineStart
= false;
4475 MoveCaret(pos
, m_caretAtLineStart
);
4476 SetDefaultStyleToCursorStyle();
4478 wxRichTextEvent
cmdEvent(
4479 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4481 cmdEvent
.SetEventObject(this);
4482 cmdEvent
.SetPosition(m_caretPosition
+1);
4483 cmdEvent
.SetOldContainer(oldContainer
);
4484 cmdEvent
.SetContainer(m_focusObject
);
4486 GetEventHandler()->ProcessEvent(cmdEvent
);
4491 #if wxUSE_DRAG_AND_DROP
4492 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4496 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4501 if (!GetSelection().IsValid())
4506 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4507 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4509 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4512 long position
= GetCaretPosition();
4513 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4514 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4516 // It doesn't make sense to move onto itself
4520 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4521 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4522 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4523 if ((def
== wxDragMove
) && !DeleteAfter
)
4525 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4526 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4529 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4530 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4532 delete richTextBuffer
;
4536 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4537 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4544 #endif // wxUSE_DRAG_AND_DROP
4547 #if wxUSE_DRAG_AND_DROP
4548 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4550 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4554 wxRichTextObject
* hitObj
= NULL
;
4555 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->ScreenToClient(wxGetMousePosition()), position
, hit
, hitObj
);
4557 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4559 m_rtc
->StoreFocusObject(container
);
4560 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4563 return false; // so that the base-class sets a cursor
4565 #endif // wxUSE_DRAG_AND_DROP
4567 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4572 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4577 void wxRichTextCtrl::EnableVerticalScrollbar(bool enable
)
4579 m_verticalScrollbarEnabled
= enable
;
4583 void wxRichTextCtrl::SetFontScale(double fontScale
, bool refresh
)
4585 GetBuffer().SetFontScale(fontScale
);
4588 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4593 void wxRichTextCtrl::SetDimensionScale(double dimScale
, bool refresh
)
4595 GetBuffer().SetDimensionScale(dimScale
);
4598 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4603 #if wxRICHTEXT_USE_OWN_CARET
4605 // ----------------------------------------------------------------------------
4606 // initialization and destruction
4607 // ----------------------------------------------------------------------------
4609 void wxRichTextCaret::Init()
4612 m_refreshEnabled
= true;
4616 m_richTextCtrl
= NULL
;
4617 m_needsUpdate
= false;
4621 wxRichTextCaret::~wxRichTextCaret()
4623 if (m_timer
.IsRunning())
4627 // ----------------------------------------------------------------------------
4628 // showing/hiding/moving the caret (base class interface)
4629 // ----------------------------------------------------------------------------
4631 void wxRichTextCaret::DoShow()
4635 if (!m_timer
.IsRunning())
4636 m_timer
.Start(GetBlinkTime());
4641 void wxRichTextCaret::DoHide()
4643 if (m_timer
.IsRunning())
4649 void wxRichTextCaret::DoMove()
4655 if (m_xOld
!= -1 && m_yOld
!= -1)
4657 if (m_richTextCtrl
&& m_refreshEnabled
)
4659 wxRect
rect(GetPosition(), GetSize());
4660 m_richTextCtrl
->RefreshRect(rect
, false);
4669 void wxRichTextCaret::DoSize()
4671 int countVisible
= m_countVisible
;
4672 if (countVisible
> 0)
4678 if (countVisible
> 0)
4680 m_countVisible
= countVisible
;
4685 // ----------------------------------------------------------------------------
4686 // handling the focus
4687 // ----------------------------------------------------------------------------
4689 void wxRichTextCaret::OnSetFocus()
4697 void wxRichTextCaret::OnKillFocus()
4702 // ----------------------------------------------------------------------------
4703 // drawing the caret
4704 // ----------------------------------------------------------------------------
4706 void wxRichTextCaret::Refresh()
4708 if (m_richTextCtrl
&& m_refreshEnabled
)
4710 wxRect
rect(GetPosition(), GetSize());
4711 m_richTextCtrl
->RefreshRect(rect
, false);
4715 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4717 dc
->SetPen( *wxBLACK_PEN
);
4719 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4720 dc
->SetPen(*wxBLACK_PEN
);
4722 wxPoint
pt(m_x
, m_y
);
4726 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4728 if (IsVisible() && m_flashOn
)
4729 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4732 void wxRichTextCaret::Notify()
4734 m_flashOn
= !m_flashOn
;
4738 void wxRichTextCaretTimer::Notify()
4743 // wxRICHTEXT_USE_OWN_CARET
4746 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4750 m_labels
.Add(label
);
4758 // Returns number of menu items were added.
4759 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4761 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4762 // If none of the standard properties identifiers are in the menu, add them if necessary.
4763 // If no items to add, just set the text to something generic
4764 if (GetCount() == 0)
4768 menu
->SetLabel(startCmd
, _("&Properties"));
4770 // Delete the others if necessary
4772 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4774 if (menu
->FindItem(i
))
4785 // Find the position of the first properties item
4786 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4788 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4789 if (item
&& item
->GetId() == startCmd
)
4798 int insertBefore
= pos
+1;
4799 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4801 if (menu
->FindItem(i
))
4803 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4807 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4808 menu
->Append(i
, m_labels
[i
- startCmd
]);
4810 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4815 // Delete any old items still left on the menu
4816 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4818 if (menu
->FindItem(i
))
4826 // No existing property identifiers were found, so append to the end of the menu.
4827 menu
->AppendSeparator();
4828 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4830 menu
->Append(i
, m_labels
[i
- startCmd
]);
4838 // Add appropriate menu items for the current container and clicked on object
4839 // (and container's parent, if appropriate).
4840 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4843 if (obj
&& ctrl
->CanEditProperties(obj
))
4844 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
4846 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
4847 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
4849 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
4850 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());