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 // DLL options compatibility check:
40 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
42 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
43 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
44 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
45 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
46 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
47 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
48 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
50 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
51 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
59 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
60 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
, wxRichTextEvent
);
62 #if wxRICHTEXT_USE_OWN_CARET
67 * This implements a non-flashing cursor in case there
68 * are platform-specific problems with the generic caret.
69 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
72 class wxRichTextCaret
;
73 class wxRichTextCaretTimer
: public wxTimer
76 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
80 virtual void Notify();
81 wxRichTextCaret
* m_caret
;
84 class wxRichTextCaret
: public wxCaret
89 // default - use Create()
90 wxRichTextCaret(): m_timer(this) { Init(); }
91 // creates a block caret associated with the given window
92 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
93 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
94 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
95 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
97 virtual ~wxRichTextCaret();
102 // called by wxWindow (not using the event tables)
103 virtual void OnSetFocus();
104 virtual void OnKillFocus();
106 // draw the caret on the given DC
107 void DoDraw(wxDC
*dc
);
109 // get the visible count
110 int GetVisibleCount() const { return m_countVisible
; }
112 // delay repositioning
113 bool GetNeedsUpdate() const { return m_needsUpdate
; }
114 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
119 virtual void DoShow();
120 virtual void DoHide();
121 virtual void DoMove();
122 virtual void DoSize();
132 bool m_hasFocus
; // true => our window has focus
133 bool m_needsUpdate
; // must be repositioned
135 wxRichTextCaretTimer m_timer
;
136 wxRichTextCtrl
* m_richTextCtrl
;
140 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl
)
142 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
144 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
145 EVT_PAINT(wxRichTextCtrl::OnPaint
)
146 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
147 EVT_IDLE(wxRichTextCtrl::OnIdle
)
148 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
149 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
150 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
151 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
152 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
153 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
154 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
155 EVT_CHAR(wxRichTextCtrl::OnChar
)
156 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
157 EVT_SIZE(wxRichTextCtrl::OnSize
)
158 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
159 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
160 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
161 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
162 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
164 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
165 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
167 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
168 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
170 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
171 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
173 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
174 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
176 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
177 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
179 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
180 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
182 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
183 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
185 EVT_MENU(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnProperties
)
186 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnUpdateProperties
)
188 EVT_MENU(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnProperties
)
189 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnUpdateProperties
)
191 EVT_MENU(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnProperties
)
192 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnUpdateProperties
)
200 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
202 wxRichTextCtrl::wxRichTextCtrl()
203 : wxScrollHelper(this)
208 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
210 const wxString
& value
,
214 const wxValidator
& validator
,
215 const wxString
& name
)
216 : wxScrollHelper(this)
219 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
223 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
224 const wxValidator
& validator
, const wxString
& name
)
228 if (!wxControl::Create(parent
, id
, pos
, size
,
229 style
|wxFULL_REPAINT_ON_RESIZE
,
233 if (!GetFont().IsOk())
235 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
238 // No physical scrolling, so we can preserve margins
239 EnableScrolling(false, false);
241 if (style
& wxTE_READONLY
)
244 // The base attributes must all have default values
245 wxRichTextAttr attributes
;
246 attributes
.SetFont(GetFont());
247 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
248 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
249 attributes
.SetLineSpacing(10);
250 attributes
.SetParagraphSpacingAfter(10);
251 attributes
.SetParagraphSpacingBefore(0);
253 SetBasicStyle(attributes
);
256 SetMargins(margin
, margin
);
258 // The default attributes will be merged with base attributes, so
259 // can be empty to begin with
260 wxRichTextAttr defaultAttributes
;
261 SetDefaultStyle(defaultAttributes
);
263 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
264 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
267 GetBuffer().SetRichTextCtrl(this);
269 #if wxRICHTEXT_USE_OWN_CARET
270 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
272 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
275 // Tell the sizers to use the given or best size
276 SetInitialSize(size
);
278 #if wxRICHTEXT_BUFFERED_PAINTING
280 RecreateBuffer(size
);
283 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
284 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
286 SetCursor(m_textCursor
);
288 if (!value
.IsEmpty())
291 GetBuffer().AddEventHandler(this);
294 wxAcceleratorEntry entries
[6];
296 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
297 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
298 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
299 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
300 entries
[4].Set(wxACCEL_CMD
, (int) 'Z', wxID_UNDO
);
301 entries
[5].Set(wxACCEL_CMD
, (int) 'Y', wxID_REDO
);
303 wxAcceleratorTable
accel(6, entries
);
304 SetAcceleratorTable(accel
);
306 m_contextMenu
= new wxMenu
;
307 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
308 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
309 m_contextMenu
->AppendSeparator();
310 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
311 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
312 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
313 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
314 m_contextMenu
->AppendSeparator();
315 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
316 m_contextMenu
->AppendSeparator();
317 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
322 wxRichTextCtrl::~wxRichTextCtrl()
324 SetFocusObject(& GetBuffer(), false);
325 GetBuffer().RemoveEventHandler(this);
327 delete m_contextMenu
;
330 /// Member initialisation
331 void wxRichTextCtrl::Init()
333 m_contextMenu
= NULL
;
335 m_caretPosition
= -1;
336 m_selectionAnchor
= -2;
337 m_selectionAnchorObject
= NULL
;
338 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
340 m_caretAtLineStart
= false;
342 m_fullLayoutRequired
= false;
343 m_fullLayoutTime
= 0;
344 m_fullLayoutSavedPosition
= 0;
345 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
346 m_caretPositionForDefaultStyle
= -2;
347 m_focusObject
= & m_buffer
;
350 void wxRichTextCtrl::DoThaw()
352 if (GetBuffer().IsDirty())
361 void wxRichTextCtrl::Clear()
363 if (GetFocusObject() == & GetBuffer())
365 m_buffer
.ResetAndClearCommands();
366 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
370 GetFocusObject()->Reset();
373 m_caretPosition
= -1;
374 m_caretPositionForDefaultStyle
= -2;
375 m_caretAtLineStart
= false;
377 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
387 wxTextCtrl::SendTextUpdatedEvent(this);
391 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
393 #if !wxRICHTEXT_USE_OWN_CARET
394 if (GetCaret() && !IsFrozen())
399 #if wxRICHTEXT_BUFFERED_PAINTING
400 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
410 dc
.SetFont(GetFont());
412 // Paint the background
415 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
417 wxRect
drawingArea(GetUpdateRegion().GetBox());
418 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
420 wxRect
availableSpace(GetClientSize());
421 if (GetBuffer().IsDirty())
423 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
424 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
428 wxRect
clipRect(availableSpace
);
429 clipRect
.x
+= GetBuffer().GetLeftMargin();
430 clipRect
.y
+= GetBuffer().GetTopMargin();
431 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
432 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
433 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
434 dc
.SetClippingRegion(clipRect
);
437 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
438 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
440 GetBuffer().Draw(dc
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
442 dc
.DestroyClippingRegion();
444 // Other user defined painting after everything else (i.e. all text) is painted
445 PaintAboveContent(dc
);
447 #if wxRICHTEXT_USE_OWN_CARET
448 if (GetCaret()->IsVisible())
450 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
455 #if !wxRICHTEXT_USE_OWN_CARET
460 #if !defined(__WXMAC__)
461 // Causes caret dropouts on Mac
467 // Empty implementation, to prevent flicker
468 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
472 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
476 #if !wxRICHTEXT_USE_OWN_CARET
482 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
483 // Work around dropouts when control is focused
491 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
496 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
497 // Work around dropouts when control is focused
505 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
510 // Set up the caret for the given position and container, after a mouse click
511 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
513 bool caretAtLineStart
= false;
515 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
517 // If we're at the start of a line (but not first in para)
518 // then we should keep the caret showing at the start of the line
519 // by showing the m_caretAtLineStart flag.
520 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
521 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
523 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
524 caretAtLineStart
= true;
528 if (extendSelection
&& (m_caretPosition
!= position
))
529 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
531 MoveCaret(position
, caretAtLineStart
);
532 SetDefaultStyleToCursorStyle();
538 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
544 dc
.SetFont(GetFont());
546 // TODO: detect change of focus object
548 wxRichTextObject
* hitObj
= NULL
;
549 wxRichTextObject
* contextObj
= NULL
;
550 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
552 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
554 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
555 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
556 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
558 SetFocusObject(container
, false /* don't set caret position yet */);
561 m_dragStart
= event
.GetLogicalPosition(dc
);
565 long oldCaretPos
= m_caretPosition
;
567 SetCaretPositionAfterClick(container
, position
, hit
);
569 // For now, don't handle shift-click when we're selecting multiple objects.
570 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
572 if (!m_selection
.IsValid())
573 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
575 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
585 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
590 if (GetCapture() == this)
593 // See if we clicked on a URL
596 dc
.SetFont(GetFont());
599 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
600 wxRichTextObject
* hitObj
= NULL
;
601 wxRichTextObject
* contextObj
= NULL
;
602 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
603 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
605 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
607 wxRichTextEvent
cmdEvent(
608 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
610 cmdEvent
.SetEventObject(this);
611 cmdEvent
.SetPosition(position
);
613 cmdEvent
.SetContainer(hitObj
->GetContainer());
615 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
618 if (GetStyle(position
, attr
))
620 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
622 wxString urlTarget
= attr
.GetURL();
623 if (!urlTarget
.IsEmpty())
625 wxMouseEvent
mouseEvent(event
);
627 long startPos
= 0, endPos
= 0;
628 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
631 startPos
= obj
->GetRange().GetStart();
632 endPos
= obj
->GetRange().GetEnd();
635 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
636 InitCommandEvent(urlEvent
);
638 urlEvent
.SetString(urlTarget
);
640 GetEventHandler()->ProcessEvent(urlEvent
);
650 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
654 dc
.SetFont(GetFont());
657 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
658 wxRichTextObject
* hitObj
= NULL
;
659 wxRichTextObject
* contextObj
= NULL
;
663 // If we're dragging, let's only consider positions at this level; otherwise
664 // selecting a range is not going to work.
665 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
668 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
669 container
= GetFocusObject();
671 int hit
= container
->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
673 // See if we need to change the cursor
676 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
678 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
680 if (actualContainer
&& GetStyle(position
, attr
, actualContainer
))
682 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
684 SetCursor(m_urlCursor
);
686 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
688 SetCursor(m_textCursor
);
693 SetCursor(m_textCursor
);
696 if (!event
.Dragging())
704 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
705 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
706 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
708 // Check for dragging across multiple containers
710 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
711 int hit2
= GetBuffer().HitTest(dc
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
712 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
714 // See if we can find a common ancestor
715 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
717 firstContainer
= GetFocusObject();
718 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
722 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
723 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
724 // is the common ancestor.
725 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
728 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
730 wxRichTextObject
* p
= hitObj2
;
733 if (p
->GetParent() == commonAncestor
)
735 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
742 if (commonAncestor
&& firstContainer
&& otherContainer
)
744 // We have now got a second container that shares a parent with the current or anchor object.
745 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
747 // Don't go into common-ancestor selection mode if we still have the same
749 if (otherContainer
!= firstContainer
)
751 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
752 m_selectionAnchorObject
= firstContainer
;
753 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
755 // The common ancestor, such as a table, returns the cell selection
756 // between the anchor and current position.
757 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
762 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
767 if (otherContainer
->AcceptsFocus())
768 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
769 MoveCaret(-1, false);
770 SetDefaultStyleToCursorStyle();
775 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
777 // TODO: test closeness
778 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
783 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
789 dc
.SetFont(GetFont());
792 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
793 wxRichTextObject
* hitObj
= NULL
;
794 wxRichTextObject
* contextObj
= NULL
;
795 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
797 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
799 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
800 if (actualContainer
&& actualContainer
->AcceptsFocus())
802 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
803 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
807 wxRichTextEvent
cmdEvent(
808 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
810 cmdEvent
.SetEventObject(this);
811 cmdEvent
.SetPosition(position
);
813 cmdEvent
.SetContainer(hitObj
->GetContainer());
815 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
819 /// Left-double-click
820 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
822 wxRichTextEvent
cmdEvent(
823 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
825 cmdEvent
.SetEventObject(this);
826 cmdEvent
.SetPosition(m_caretPosition
+1);
827 cmdEvent
.SetContainer(GetFocusObject());
829 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
831 SelectWord(GetCaretPosition()+1);
836 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
838 wxRichTextEvent
cmdEvent(
839 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
841 cmdEvent
.SetEventObject(this);
842 cmdEvent
.SetPosition(m_caretPosition
+1);
843 cmdEvent
.SetContainer(GetFocusObject());
845 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
850 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
854 flags
|= wxRICHTEXT_CTRL_DOWN
;
855 if (event
.ShiftDown())
856 flags
|= wxRICHTEXT_SHIFT_DOWN
;
858 flags
|= wxRICHTEXT_ALT_DOWN
;
860 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
862 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
864 KeyboardNavigate(event
.GetKeyCode(), flags
);
868 long keycode
= event
.GetKeyCode();
928 case WXK_NUMPAD_HOME
:
929 case WXK_NUMPAD_LEFT
:
931 case WXK_NUMPAD_RIGHT
:
932 case WXK_NUMPAD_DOWN
:
933 case WXK_NUMPAD_PAGEUP
:
934 case WXK_NUMPAD_PAGEDOWN
:
936 case WXK_NUMPAD_BEGIN
:
937 case WXK_NUMPAD_INSERT
:
938 case WXK_WINDOWS_LEFT
:
947 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
948 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
950 BeginBatchUndo(_("Delete Text"));
952 long newPos
= m_caretPosition
;
954 bool processed
= DeleteSelectedContent(& newPos
);
956 // Submit range in character positions, which are greater than caret positions,
957 // so subtract 1 for deleted character and add 1 for conversion to character position.
962 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
965 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
971 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
976 if (GetLastPosition() == -1)
978 GetFocusObject()->Reset();
980 m_caretPosition
= -1;
982 SetDefaultStyleToCursorStyle();
985 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
987 wxRichTextEvent
cmdEvent(
988 wxEVT_COMMAND_RICHTEXT_DELETE
,
990 cmdEvent
.SetEventObject(this);
991 cmdEvent
.SetFlags(flags
);
992 cmdEvent
.SetPosition(m_caretPosition
+1);
993 cmdEvent
.SetContainer(GetFocusObject());
994 GetEventHandler()->ProcessEvent(cmdEvent
);
1004 // all the other keys modify the controls contents which shouldn't be
1005 // possible if we're read-only
1006 if ( !IsEditable() )
1012 if (event
.GetKeyCode() == WXK_RETURN
)
1014 BeginBatchUndo(_("Insert Text"));
1016 long newPos
= m_caretPosition
;
1018 DeleteSelectedContent(& newPos
);
1020 if (event
.ShiftDown())
1023 text
= wxRichTextLineBreakChar
;
1024 GetFocusObject()->InsertTextWithUndo(newPos
+1, text
, this, & GetBuffer());
1025 m_caretAtLineStart
= true;
1029 GetFocusObject()->InsertNewlineWithUndo(newPos
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1032 SetDefaultStyleToCursorStyle();
1034 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1036 wxRichTextEvent
cmdEvent(
1037 wxEVT_COMMAND_RICHTEXT_RETURN
,
1039 cmdEvent
.SetEventObject(this);
1040 cmdEvent
.SetFlags(flags
);
1041 cmdEvent
.SetPosition(newPos
+1);
1042 cmdEvent
.SetContainer(GetFocusObject());
1044 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1046 // Generate conventional event
1047 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1048 InitCommandEvent(textEvent
);
1050 GetEventHandler()->ProcessEvent(textEvent
);
1054 else if (event
.GetKeyCode() == WXK_BACK
)
1056 BeginBatchUndo(_("Delete Text"));
1058 long newPos
= m_caretPosition
;
1060 bool processed
= DeleteSelectedContent(& newPos
);
1062 // Submit range in character positions, which are greater than caret positions,
1063 // so subtract 1 for deleted character and add 1 for conversion to character position.
1066 if (event
.CmdDown())
1068 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1071 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
1077 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1082 if (GetLastPosition() == -1)
1084 GetFocusObject()->Reset();
1086 m_caretPosition
= -1;
1088 SetDefaultStyleToCursorStyle();
1091 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1093 wxRichTextEvent
cmdEvent(
1094 wxEVT_COMMAND_RICHTEXT_DELETE
,
1096 cmdEvent
.SetEventObject(this);
1097 cmdEvent
.SetFlags(flags
);
1098 cmdEvent
.SetPosition(m_caretPosition
+1);
1099 cmdEvent
.SetContainer(GetFocusObject());
1100 GetEventHandler()->ProcessEvent(cmdEvent
);
1104 else if (event
.GetKeyCode() == WXK_DELETE
)
1106 BeginBatchUndo(_("Delete Text"));
1108 long newPos
= m_caretPosition
;
1110 bool processed
= DeleteSelectedContent(& newPos
);
1112 // Submit range in character positions, which are greater than caret positions,
1113 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1115 if (event
.CmdDown())
1117 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1118 if (pos
!= -1 && (pos
> newPos
))
1120 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this, & GetBuffer());
1125 if (!processed
&& newPos
< (GetLastPosition()-1))
1126 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this, & GetBuffer());
1131 if (GetLastPosition() == -1)
1133 GetFocusObject()->Reset();
1135 m_caretPosition
= -1;
1137 SetDefaultStyleToCursorStyle();
1140 wxRichTextEvent
cmdEvent(
1141 wxEVT_COMMAND_RICHTEXT_DELETE
,
1143 cmdEvent
.SetEventObject(this);
1144 cmdEvent
.SetFlags(flags
);
1145 cmdEvent
.SetPosition(m_caretPosition
+1);
1146 cmdEvent
.SetContainer(GetFocusObject());
1147 GetEventHandler()->ProcessEvent(cmdEvent
);
1153 long keycode
= event
.GetKeyCode();
1165 if (event
.CmdDown())
1167 // Fixes AltGr+key with European input languages on Windows
1168 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1175 wxRichTextEvent
cmdEvent(
1176 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1178 cmdEvent
.SetEventObject(this);
1179 cmdEvent
.SetFlags(flags
);
1181 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1183 cmdEvent
.SetCharacter((wxChar
) keycode
);
1185 cmdEvent
.SetPosition(m_caretPosition
+1);
1186 cmdEvent
.SetContainer(GetFocusObject());
1188 if (keycode
== wxT('\t'))
1190 // See if we need to promote or demote the selection or paragraph at the cursor
1191 // position, instead of inserting a tab.
1192 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1193 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1194 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1196 wxRichTextRange range
;
1198 range
= GetSelectionRange();
1200 range
= para
->GetRange().FromInternal();
1202 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1204 PromoteList(promoteBy
, range
, NULL
);
1206 GetEventHandler()->ProcessEvent(cmdEvent
);
1212 BeginBatchUndo(_("Insert Text"));
1214 long newPos
= m_caretPosition
;
1215 DeleteSelectedContent(& newPos
);
1218 wxString str
= event
.GetUnicodeKey();
1220 wxString str
= (wxChar
) event
.GetKeyCode();
1222 GetFocusObject()->InsertTextWithUndo(newPos
+1, str
, this, & GetBuffer(), 0);
1226 SetDefaultStyleToCursorStyle();
1227 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1229 cmdEvent
.SetPosition(m_caretPosition
);
1230 GetEventHandler()->ProcessEvent(cmdEvent
);
1238 /// Delete content if there is a selection, e.g. when pressing a key.
1239 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1243 long pos
= m_selection
.GetRange().GetStart();
1244 wxRichTextRange range
= m_selection
.GetRange();
1246 // SelectAll causes more to be selected than doing it interactively,
1247 // and causes a new paragraph to be inserted. So for multiline buffers,
1248 // don't delete the final position.
1249 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1250 range
.SetEnd(range
.GetEnd()-1);
1252 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1253 m_selection
.Reset();
1254 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1264 /// Keyboard navigation
1268 Left: left one character
1269 Right: right one character
1272 Ctrl-Left: left one word
1273 Ctrl-Right: right one word
1274 Ctrl-Up: previous paragraph start
1275 Ctrl-Down: next start of paragraph
1278 Ctrl-Home: start of document
1279 Ctrl-End: end of document
1280 Page-Up: Up a screen
1281 Page-Down: Down a screen
1285 Ctrl-Alt-PgUp: Start of window
1286 Ctrl-Alt-PgDn: End of window
1287 F8: Start selection mode
1288 Esc: End selection mode
1290 Adding Shift does the above but starts/extends selection.
1295 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1297 bool success
= false;
1299 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1301 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1302 success
= WordRight(1, flags
);
1304 success
= MoveRight(1, flags
);
1306 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1308 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1309 success
= WordLeft(1, flags
);
1311 success
= MoveLeft(1, flags
);
1313 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1315 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1316 success
= MoveToParagraphStart(flags
);
1318 success
= MoveUp(1, flags
);
1320 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1322 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1323 success
= MoveToParagraphEnd(flags
);
1325 success
= MoveDown(1, flags
);
1327 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1329 success
= PageUp(1, flags
);
1331 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1333 success
= PageDown(1, flags
);
1335 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1337 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1338 success
= MoveHome(flags
);
1340 success
= MoveToLineStart(flags
);
1342 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1344 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1345 success
= MoveEnd(flags
);
1347 success
= MoveToLineEnd(flags
);
1352 ScrollIntoView(m_caretPosition
, keyCode
);
1353 SetDefaultStyleToCursorStyle();
1359 /// Extend the selection. Selections are in caret positions.
1360 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1362 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1364 if (oldPos
== newPos
)
1367 wxRichTextSelection oldSelection
= m_selection
;
1369 m_selection
.SetContainer(GetFocusObject());
1371 wxRichTextRange oldRange
;
1372 if (m_selection
.IsValid())
1373 oldRange
= m_selection
.GetRange();
1375 oldRange
= wxRICHTEXT_NO_SELECTION
;
1376 wxRichTextRange newRange
;
1378 // If not currently selecting, start selecting
1379 if (oldRange
.GetStart() == -2)
1381 m_selectionAnchor
= oldPos
;
1383 if (oldPos
> newPos
)
1384 newRange
.SetRange(newPos
+1, oldPos
);
1386 newRange
.SetRange(oldPos
+1, newPos
);
1390 // Always ensure that the selection range start is greater than
1392 if (newPos
> m_selectionAnchor
)
1393 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1394 else if (newPos
== m_selectionAnchor
)
1395 newRange
= wxRichTextRange(-2, -2);
1397 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1400 m_selection
.SetRange(newRange
);
1402 RefreshForSelectionChange(oldSelection
, m_selection
);
1404 if (newRange
.GetStart() > newRange
.GetEnd())
1406 wxLogDebug(wxT("Strange selection range"));
1415 /// Scroll into view, returning true if we scrolled.
1416 /// This takes a _caret_ position.
1417 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1419 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1425 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1427 int startXUnits
, startYUnits
;
1428 GetViewStart(& startXUnits
, & startYUnits
);
1429 int startY
= startYUnits
* ppuY
;
1432 GetVirtualSize(& sx
, & sy
);
1438 wxRect rect
= line
->GetRect();
1440 bool scrolled
= false;
1442 wxSize clientSize
= GetClientSize();
1444 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1447 wxClientDC
dc(this);
1448 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1449 topMargin
, bottomMargin
);
1451 // clientSize.y -= GetBuffer().GetBottomMargin();
1452 clientSize
.y
-= bottomMargin
;
1454 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1456 int y
= rect
.y
- GetClientSize().y
/2;
1457 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1458 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1460 if (startYUnits
!= yUnits
)
1462 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1465 #if !wxRICHTEXT_USE_OWN_CARET
1475 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1476 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1477 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1478 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1480 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1482 // Make it scroll so this item is at the bottom
1484 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1485 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1487 // If we're still off the screen, scroll another line down
1488 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1491 if (startYUnits
!= yUnits
)
1493 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1497 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1499 // Make it scroll so this item is at the top
1501 int y
= rect
.y
- GetBuffer().GetTopMargin();
1502 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1504 if (startYUnits
!= yUnits
)
1506 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1512 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1513 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1514 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1515 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1517 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1519 // Make it scroll so this item is at the top
1521 int y
= rect
.y
- GetBuffer().GetTopMargin();
1522 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1524 if (startYUnits
!= yUnits
)
1526 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1530 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1532 // Make it scroll so this item is at the bottom
1534 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1535 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1537 // If we're still off the screen, scroll another line down
1538 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1541 if (startYUnits
!= yUnits
)
1543 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1549 #if !wxRICHTEXT_USE_OWN_CARET
1557 /// Is the given position visible on the screen?
1558 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1560 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1566 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1569 GetViewStart(& startX
, & startY
);
1571 startY
= startY
* ppuY
;
1573 wxRect rect
= line
->GetRect();
1574 wxSize clientSize
= GetClientSize();
1575 clientSize
.y
-= GetBuffer().GetBottomMargin();
1577 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1580 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1582 m_caretPosition
= position
;
1583 m_caretAtLineStart
= showAtLineStart
;
1586 /// Move caret one visual step forward: this may mean setting a flag
1587 /// and keeping the same position if we're going from the end of one line
1588 /// to the start of the next, which may be the exact same caret position.
1589 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1591 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1593 // Only do the check if we're not at the end of the paragraph (where things work OK
1595 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1597 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1601 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1603 // We're at the end of a line. See whether we need to
1604 // stay at the same actual caret position but change visual
1605 // position, or not.
1606 if (oldPosition
== lineRange
.GetEnd())
1608 if (m_caretAtLineStart
)
1610 // We're already at the start of the line, so actually move on now.
1611 m_caretPosition
= oldPosition
+ 1;
1612 m_caretAtLineStart
= false;
1616 // We're showing at the end of the line, so keep to
1617 // the same position but indicate that we're to show
1618 // at the start of the next line.
1619 m_caretPosition
= oldPosition
;
1620 m_caretAtLineStart
= true;
1622 SetDefaultStyleToCursorStyle();
1628 SetDefaultStyleToCursorStyle();
1631 /// Move caret one visual step backward: this may mean setting a flag
1632 /// and keeping the same position if we're going from the end of one line
1633 /// to the start of the next, which may be the exact same caret position.
1634 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1636 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1638 // Only do the check if we're not at the start of the paragraph (where things work OK
1640 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1642 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1646 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1648 // We're at the start of a line. See whether we need to
1649 // stay at the same actual caret position but change visual
1650 // position, or not.
1651 if (oldPosition
== lineRange
.GetStart())
1653 m_caretPosition
= oldPosition
-1;
1654 m_caretAtLineStart
= true;
1657 else if (oldPosition
== lineRange
.GetEnd())
1659 if (m_caretAtLineStart
)
1661 // We're at the start of the line, so keep the same caret position
1662 // but clear the start-of-line flag.
1663 m_caretPosition
= oldPosition
;
1664 m_caretAtLineStart
= false;
1668 // We're showing at the end of the line, so go back
1669 // to the previous character position.
1670 m_caretPosition
= oldPosition
- 1;
1672 SetDefaultStyleToCursorStyle();
1678 SetDefaultStyleToCursorStyle();
1682 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1684 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1686 if (m_caretPosition
+ noPositions
< endPos
)
1688 long oldPos
= m_caretPosition
;
1689 long newPos
= m_caretPosition
+ noPositions
;
1691 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1695 // Determine by looking at oldPos and m_caretPosition whether
1696 // we moved from the end of a line to the start of the next line, in which case
1697 // we want to adjust the caret position such that it is positioned at the
1698 // start of the next line, rather than jumping past the first character of the
1700 if (noPositions
== 1 && !extendSel
)
1701 MoveCaretForward(oldPos
);
1703 SetCaretPosition(newPos
);
1706 SetDefaultStyleToCursorStyle();
1715 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1719 if (m_caretPosition
> startPos
- noPositions
+ 1)
1721 long oldPos
= m_caretPosition
;
1722 long newPos
= m_caretPosition
- noPositions
;
1723 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1727 if (noPositions
== 1 && !extendSel
)
1728 MoveCaretBack(oldPos
);
1730 SetCaretPosition(newPos
);
1733 SetDefaultStyleToCursorStyle();
1741 // Find the caret position for the combination of hit-test flags and character position.
1742 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
1743 // since this is ambiguous (same position used for end of line and start of next).
1744 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
1745 bool& caretLineStart
)
1747 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1748 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1749 // so we view the caret at the start of the line.
1750 caretLineStart
= false;
1751 long caretPosition
= position
;
1753 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
1755 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
1756 wxRichTextRange lineRange
;
1758 lineRange
= thisLine
->GetAbsoluteRange();
1760 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
1763 caretLineStart
= true;
1767 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
1768 if (para
&& para
->GetRange().GetStart() == position
)
1772 return caretPosition
;
1776 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1778 return MoveDown(- noLines
, flags
);
1782 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1787 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1788 wxPoint pt
= GetCaret()->GetPosition();
1789 long newLine
= lineNumber
+ noLines
;
1790 bool notInThisObject
= false;
1792 if (lineNumber
!= -1)
1796 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
1797 if (newLine
> lastLine
)
1798 notInThisObject
= true;
1803 notInThisObject
= true;
1807 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
1808 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
1810 if (notInThisObject
)
1812 // If we know we're navigating out of the current object,
1813 // try to find an object anywhere in the buffer at the new position (up or down a bit)
1814 container
= & GetBuffer();
1815 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
1817 if (noLines
> 0) // going down
1819 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
1823 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
1828 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
1830 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1836 wxClientDC
dc(this);
1838 dc
.SetFont(GetFont());
1840 wxRichTextObject
* hitObj
= NULL
;
1841 wxRichTextObject
* contextObj
= NULL
;
1842 int hitTest
= container
->HitTest(dc
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
1845 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
1846 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
1849 if (notInThisObject
)
1851 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
1852 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
1854 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
1856 container
= actualContainer
;
1860 bool caretLineStart
= true;
1861 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
1862 long newSelEnd
= caretPosition
;
1865 if (notInThisObject
)
1868 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1873 SetCaretPosition(caretPosition
, caretLineStart
);
1875 SetDefaultStyleToCursorStyle();
1883 /// Move to the end of the paragraph
1884 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1886 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1889 long newPos
= para
->GetRange().GetEnd() - 1;
1890 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1894 SetCaretPosition(newPos
);
1896 SetDefaultStyleToCursorStyle();
1904 /// Move to the start of the paragraph
1905 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1907 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1910 long newPos
= para
->GetRange().GetStart() - 1;
1911 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1915 SetCaretPosition(newPos
);
1917 SetDefaultStyleToCursorStyle();
1925 /// Move to the end of the line
1926 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1928 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1932 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1933 long newPos
= lineRange
.GetEnd();
1934 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1938 SetCaretPosition(newPos
);
1940 SetDefaultStyleToCursorStyle();
1948 /// Move to the start of the line
1949 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1951 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1954 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1955 long newPos
= lineRange
.GetStart()-1;
1957 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1961 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
1963 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1965 SetDefaultStyleToCursorStyle();
1973 /// Move to the start of the buffer
1974 bool wxRichTextCtrl::MoveHome(int flags
)
1976 if (m_caretPosition
!= -1)
1978 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1982 SetCaretPosition(-1);
1984 SetDefaultStyleToCursorStyle();
1992 /// Move to the end of the buffer
1993 bool wxRichTextCtrl::MoveEnd(int flags
)
1995 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
1997 if (m_caretPosition
!= endPos
)
1999 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2003 SetCaretPosition(endPos
);
2005 SetDefaultStyleToCursorStyle();
2013 /// Move noPages pages up
2014 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2016 return PageDown(- noPages
, flags
);
2019 /// Move noPages pages down
2020 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2022 // Calculate which line occurs noPages * screen height further down.
2023 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2026 wxSize clientSize
= GetClientSize();
2027 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2029 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2032 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2033 long pos
= lineRange
.GetStart()-1;
2034 if (pos
!= m_caretPosition
)
2036 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2038 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2042 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2044 SetDefaultStyleToCursorStyle();
2054 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2056 return str
== wxT(" ") || str
== wxT("\t");
2059 // Finds the caret position for the next word
2060 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2062 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2066 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2068 // First skip current text to space
2069 while (i
< endPos
&& i
> -1)
2071 // i is in character, not caret positions
2072 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2073 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2074 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2078 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2085 while (i
< endPos
&& i
> -1)
2087 // i is in character, not caret positions
2088 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2089 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2090 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2091 return wxMax(-1, i
);
2093 if (text
.empty()) // End of paragraph, or maybe an image
2094 return wxMax(-1, i
- 1);
2095 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2099 // Convert to caret position
2100 return wxMax(-1, i
- 1);
2109 long i
= m_caretPosition
;
2111 // First skip white space
2112 while (i
< endPos
&& i
> -1)
2114 // i is in character, not caret positions
2115 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2116 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2118 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2120 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2125 // Next skip current text to space
2126 while (i
< endPos
&& i
> -1)
2128 // i is in character, not caret positions
2129 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2130 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2131 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2134 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2147 /// Move n words left
2148 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2150 long pos
= FindNextWordPosition(-1);
2151 if (pos
!= m_caretPosition
)
2153 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2155 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2159 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2161 SetDefaultStyleToCursorStyle();
2169 /// Move n words right
2170 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2172 long pos
= FindNextWordPosition(1);
2173 if (pos
!= m_caretPosition
)
2175 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2177 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2181 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2183 SetDefaultStyleToCursorStyle();
2192 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2194 // Only do sizing optimization for large buffers
2195 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2197 m_fullLayoutRequired
= true;
2198 m_fullLayoutTime
= wxGetLocalTimeMillis();
2199 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2200 LayoutContent(true /* onlyVisibleRect */);
2203 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2205 #if wxRICHTEXT_BUFFERED_PAINTING
2212 // Force any pending layout due to large buffer
2213 void wxRichTextCtrl::ForceDelayedLayout()
2215 if (m_fullLayoutRequired
)
2217 m_fullLayoutRequired
= false;
2218 m_fullLayoutTime
= 0;
2219 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2220 ShowPosition(m_fullLayoutSavedPosition
);
2226 /// Idle-time processing
2227 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2229 #if wxRICHTEXT_USE_OWN_CARET
2230 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2232 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2238 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2240 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2242 m_fullLayoutRequired
= false;
2243 m_fullLayoutTime
= 0;
2244 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2245 ShowPosition(m_fullLayoutSavedPosition
);
2249 if (m_caretPositionForDefaultStyle
!= -2)
2251 // If the caret position has changed, no longer reflect the default style
2253 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2254 m_caretPositionForDefaultStyle
= -2;
2261 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2263 #if wxRICHTEXT_USE_OWN_CARET
2264 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2267 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2274 /// Set up scrollbars, e.g. after a resize
2275 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2280 if (GetBuffer().IsEmpty())
2282 SetScrollbars(0, 0, 0, 0, 0, 0);
2286 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2287 // of pixels. See e.g. wxVScrolledWindow for ideas.
2288 int pixelsPerUnit
= 5;
2289 wxSize clientSize
= GetClientSize();
2291 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2293 // Round up so we have at least maxHeight pixels
2294 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2296 int startX
= 0, startY
= 0;
2298 GetViewStart(& startX
, & startY
);
2300 int maxPositionX
= 0;
2301 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2303 int newStartX
= wxMin(maxPositionX
, startX
);
2304 int newStartY
= wxMin(maxPositionY
, startY
);
2306 int oldPPUX
, oldPPUY
;
2307 int oldStartX
, oldStartY
;
2308 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2309 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2310 GetViewStart(& oldStartX
, & oldStartY
);
2311 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2313 oldVirtualSizeY
/= oldPPUY
;
2315 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2318 // Don't set scrollbars if there were none before, and there will be none now.
2319 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2322 // Move to previous scroll position if
2324 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2327 /// Paint the background
2328 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2330 wxColour backgroundColour
= GetBackgroundColour();
2331 if (!backgroundColour
.IsOk())
2332 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2334 // Clear the background
2335 dc
.SetBrush(wxBrush(backgroundColour
));
2336 dc
.SetPen(*wxTRANSPARENT_PEN
);
2337 wxRect
windowRect(GetClientSize());
2338 windowRect
.x
-= 2; windowRect
.y
-= 2;
2339 windowRect
.width
+= 4; windowRect
.height
+= 4;
2341 // We need to shift the rectangle to take into account
2342 // scrolling. Converting device to logical coordinates.
2343 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2344 dc
.DrawRectangle(windowRect
);
2347 #if wxRICHTEXT_BUFFERED_PAINTING
2348 /// Recreate buffer bitmap if necessary
2349 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2352 if (sz
== wxDefaultSize
)
2353 sz
= GetClientSize();
2355 if (sz
.x
< 1 || sz
.y
< 1)
2358 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2359 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2360 return m_bufferBitmap
.IsOk();
2364 // ----------------------------------------------------------------------------
2365 // file IO functions
2366 // ----------------------------------------------------------------------------
2368 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2370 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2372 m_filename
= filename
;
2375 SetInsertionPoint(0);
2378 SetupScrollbars(true);
2380 wxTextCtrl::SendTextUpdatedEvent(this);
2386 wxLogError(_("File couldn't be loaded."));
2392 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2394 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2396 m_filename
= filename
;
2403 wxLogError(_("The text couldn't be saved."));
2408 // ----------------------------------------------------------------------------
2409 // wxRichTextCtrl specific functionality
2410 // ----------------------------------------------------------------------------
2412 /// Add a new paragraph of text to the end of the buffer
2413 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2415 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2416 GetBuffer().Invalidate();
2422 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2424 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2425 GetBuffer().Invalidate();
2430 // ----------------------------------------------------------------------------
2431 // selection and ranges
2432 // ----------------------------------------------------------------------------
2434 void wxRichTextCtrl::SelectAll()
2436 SetSelection(-1, -1);
2440 void wxRichTextCtrl::SelectNone()
2442 if (m_selection
.IsValid())
2444 wxRichTextSelection oldSelection
= m_selection
;
2446 m_selection
.Reset();
2448 RefreshForSelectionChange(oldSelection
, m_selection
);
2450 m_selectionAnchor
= -2;
2451 m_selectionAnchorObject
= NULL
;
2452 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2455 static bool wxIsWordDelimiter(const wxString
& text
)
2457 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2460 /// Select the word at the given character position
2461 bool wxRichTextCtrl::SelectWord(long position
)
2463 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2466 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2470 if (position
== para
->GetRange().GetEnd())
2473 long positionStart
= position
;
2474 long positionEnd
= position
;
2476 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2478 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2479 if (wxIsWordDelimiter(text
))
2485 if (positionStart
< para
->GetRange().GetStart())
2486 positionStart
= para
->GetRange().GetStart();
2488 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2490 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2491 if (wxIsWordDelimiter(text
))
2497 if (positionEnd
>= para
->GetRange().GetEnd())
2498 positionEnd
= para
->GetRange().GetEnd();
2500 if (positionEnd
< positionStart
)
2503 SetSelection(positionStart
, positionEnd
+1);
2505 if (positionStart
>= 0)
2507 MoveCaret(positionStart
-1, true);
2508 SetDefaultStyleToCursorStyle();
2514 wxString
wxRichTextCtrl::GetStringSelection() const
2517 GetSelection(&from
, &to
);
2519 return GetRange(from
, to
);
2522 // ----------------------------------------------------------------------------
2524 // ----------------------------------------------------------------------------
2526 wxTextCtrlHitTestResult
2527 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2529 // implement in terms of the other overload as the native ports typically
2530 // can get the position and not (x, y) pair directly (although wxUniv
2531 // directly gets x and y -- and so overrides this method as well)
2533 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2535 if ( rc
!= wxTE_HT_UNKNOWN
)
2537 PositionToXY(pos
, x
, y
);
2543 wxTextCtrlHitTestResult
2544 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2547 wxClientDC
dc((wxRichTextCtrl
*) this);
2548 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2550 // Buffer uses logical position (relative to start of buffer)
2552 wxPoint pt2
= GetLogicalPoint(pt
);
2554 wxRichTextObject
* hitObj
= NULL
;
2555 wxRichTextObject
* contextObj
= NULL
;
2556 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2558 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2559 return wxTE_HT_BEFORE
;
2560 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2561 return wxTE_HT_BEYOND
;
2562 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2563 return wxTE_HT_ON_TEXT
;
2565 return wxTE_HT_UNKNOWN
;
2568 // ----------------------------------------------------------------------------
2569 // set/get the controls text
2570 // ----------------------------------------------------------------------------
2572 wxString
wxRichTextCtrl::DoGetValue() const
2574 return GetBuffer().GetText();
2577 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2579 // Public API for range is different from internals
2580 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2583 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2585 // Don't call Clear here, since it always sends a text updated event
2586 m_buffer
.ResetAndClearCommands();
2587 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2588 m_caretPosition
= -1;
2589 m_caretPositionForDefaultStyle
= -2;
2590 m_caretAtLineStart
= false;
2591 m_selection
.Reset();
2592 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2602 if (!value
.IsEmpty())
2604 // Remove empty paragraph
2605 GetBuffer().Clear();
2606 DoWriteText(value
, flags
);
2608 // for compatibility, don't move the cursor when doing SetValue()
2609 SetInsertionPoint(0);
2613 // still send an event for consistency
2614 if (flags
& SetValue_SendEvent
)
2615 wxTextCtrl::SendTextUpdatedEvent(this);
2620 void wxRichTextCtrl::WriteText(const wxString
& value
)
2625 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2627 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2629 GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2631 if ( flags
& SetValue_SendEvent
)
2632 wxTextCtrl::SendTextUpdatedEvent(this);
2635 void wxRichTextCtrl::AppendText(const wxString
& text
)
2637 SetInsertionPointEnd();
2642 /// Write an image at the current insertion point
2643 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2645 wxRichTextImageBlock imageBlock
;
2647 wxImage image2
= image
;
2648 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2649 return WriteImage(imageBlock
, textAttr
);
2654 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2656 wxRichTextImageBlock imageBlock
;
2659 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2660 return WriteImage(imageBlock
, textAttr
);
2665 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2667 return GetFocusObject()->InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this, & GetBuffer(), 0, textAttr
);
2670 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2674 wxRichTextImageBlock imageBlock
;
2676 wxImage image
= bitmap
.ConvertToImage();
2677 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2678 return WriteImage(imageBlock
, textAttr
);
2684 // Write a text box at the current insertion point.
2685 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2687 wxRichTextBox
* textBox
= new wxRichTextBox
;
2688 textBox
->SetAttributes(textAttr
);
2689 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2690 textBox
->AddParagraph(wxEmptyString
);
2691 textBox
->SetParent(NULL
);
2693 // The object returned is the one actually inserted into the buffer,
2694 // while the original one is deleted.
2695 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, textBox
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2696 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
2700 // Write a table at the current insertion point, returning the table.
2701 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
2703 wxASSERT(rows
> 0 && cols
> 0);
2705 if (rows
<= 0 || cols
<= 0)
2708 wxRichTextTable
* table
= new wxRichTextTable
;
2709 table
->SetAttributes(tableAttr
);
2710 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2712 table
->CreateTable(rows
, cols
);
2714 table
->SetParent(NULL
);
2717 for (j
= 0; j
< rows
; j
++)
2719 for (i
= 0; i
< cols
; i
++)
2721 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
2725 // The object returned is the one actually inserted into the buffer,
2726 // while the original one is deleted.
2727 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, table
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2728 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
2733 /// Insert a newline (actually paragraph) at the current insertion point.
2734 bool wxRichTextCtrl::Newline()
2736 return GetFocusObject()->InsertNewlineWithUndo(m_caretPosition
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2739 /// Insert a line break at the current insertion point.
2740 bool wxRichTextCtrl::LineBreak()
2743 text
= wxRichTextLineBreakChar
;
2744 return GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, text
, this, & GetBuffer());
2747 // ----------------------------------------------------------------------------
2748 // Clipboard operations
2749 // ----------------------------------------------------------------------------
2751 void wxRichTextCtrl::Copy()
2755 wxRichTextRange range
= GetInternalSelectionRange();
2756 GetBuffer().CopyToClipboard(range
);
2760 void wxRichTextCtrl::Cut()
2764 wxRichTextRange range
= GetInternalSelectionRange();
2765 GetBuffer().CopyToClipboard(range
);
2767 DeleteSelectedContent();
2773 void wxRichTextCtrl::Paste()
2777 BeginBatchUndo(_("Paste"));
2779 long newPos
= m_caretPosition
;
2780 DeleteSelectedContent(& newPos
);
2782 GetBuffer().PasteFromClipboard(newPos
);
2788 void wxRichTextCtrl::DeleteSelection()
2790 if (CanDeleteSelection())
2792 DeleteSelectedContent();
2796 bool wxRichTextCtrl::HasSelection() const
2798 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
2801 bool wxRichTextCtrl::HasUnfocusedSelection() const
2803 return m_selection
.IsValid();
2806 bool wxRichTextCtrl::CanCopy() const
2808 // Can copy if there's a selection
2809 return HasSelection();
2812 bool wxRichTextCtrl::CanCut() const
2814 return HasSelection() && IsEditable();
2817 bool wxRichTextCtrl::CanPaste() const
2819 if ( !IsEditable() )
2822 return GetBuffer().CanPasteFromClipboard();
2825 bool wxRichTextCtrl::CanDeleteSelection() const
2827 return HasSelection() && IsEditable();
2831 // ----------------------------------------------------------------------------
2833 // ----------------------------------------------------------------------------
2835 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
2837 if (m_contextMenu
&& m_contextMenu
!= menu
)
2838 delete m_contextMenu
;
2839 m_contextMenu
= menu
;
2842 void wxRichTextCtrl::SetEditable(bool editable
)
2844 m_editable
= editable
;
2847 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2851 m_caretPosition
= pos
- 1;
2855 SetDefaultStyleToCursorStyle();
2858 void wxRichTextCtrl::SetInsertionPointEnd()
2860 long pos
= GetLastPosition();
2861 SetInsertionPoint(pos
);
2864 long wxRichTextCtrl::GetInsertionPoint() const
2866 return m_caretPosition
+1;
2869 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2871 return GetFocusObject()->GetOwnRange().GetEnd();
2874 // If the return values from and to are the same, there is no
2876 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2878 if (m_selection
.IsValid())
2880 *from
= m_selection
.GetRange().GetStart();
2881 *to
= m_selection
.GetRange().GetEnd();
2891 bool wxRichTextCtrl::IsEditable() const
2896 // ----------------------------------------------------------------------------
2898 // ----------------------------------------------------------------------------
2900 void wxRichTextCtrl::SetSelection(long from
, long to
)
2902 // if from and to are both -1, it means (in wxWidgets) that all text should
2904 if ( (from
== -1) && (to
== -1) )
2907 to
= GetLastPosition()+1;
2916 wxRichTextSelection oldSelection
= m_selection
;
2918 m_selectionAnchor
= from
-1;
2919 m_selectionAnchorObject
= NULL
;
2920 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
2922 m_caretPosition
= wxMax(-1, to
-1);
2924 RefreshForSelectionChange(oldSelection
, m_selection
);
2929 // ----------------------------------------------------------------------------
2931 // ----------------------------------------------------------------------------
2933 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2934 const wxString
& value
)
2936 BeginBatchUndo(_("Replace"));
2938 DeleteSelectedContent();
2940 DoWriteText(value
, SetValue_SelectionOnly
);
2945 void wxRichTextCtrl::Remove(long from
, long to
)
2949 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
2956 bool wxRichTextCtrl::IsModified() const
2958 return m_buffer
.IsModified();
2961 void wxRichTextCtrl::MarkDirty()
2963 m_buffer
.Modify(true);
2966 void wxRichTextCtrl::DiscardEdits()
2968 m_caretPositionForDefaultStyle
= -2;
2969 m_buffer
.Modify(false);
2970 m_buffer
.GetCommandProcessor()->ClearCommands();
2973 int wxRichTextCtrl::GetNumberOfLines() const
2975 return GetFocusObject()->GetParagraphCount();
2978 // ----------------------------------------------------------------------------
2979 // Positions <-> coords
2980 // ----------------------------------------------------------------------------
2982 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2984 return GetFocusObject()->XYToPosition(x
, y
);
2987 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2989 return GetFocusObject()->PositionToXY(pos
, x
, y
);
2992 // ----------------------------------------------------------------------------
2994 // ----------------------------------------------------------------------------
2996 void wxRichTextCtrl::ShowPosition(long pos
)
2998 if (!IsPositionVisible(pos
))
2999 ScrollIntoView(pos
-1, WXK_DOWN
);
3002 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3004 return GetFocusObject()->GetParagraphLength(lineNo
);
3007 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3009 return GetFocusObject()->GetParagraphText(lineNo
);
3012 // ----------------------------------------------------------------------------
3014 // ----------------------------------------------------------------------------
3016 void wxRichTextCtrl::Undo()
3020 GetCommandProcessor()->Undo();
3024 void wxRichTextCtrl::Redo()
3028 GetCommandProcessor()->Redo();
3032 bool wxRichTextCtrl::CanUndo() const
3034 return GetCommandProcessor()->CanUndo() && IsEditable();
3037 bool wxRichTextCtrl::CanRedo() const
3039 return GetCommandProcessor()->CanRedo() && IsEditable();
3042 // ----------------------------------------------------------------------------
3043 // implementation details
3044 // ----------------------------------------------------------------------------
3046 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3048 SetValue(event
.GetString());
3049 GetEventHandler()->ProcessEvent(event
);
3052 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3054 // By default, load the first file into the text window.
3055 if (event
.GetNumberOfFiles() > 0)
3057 LoadFile(event
.GetFiles()[0]);
3061 wxSize
wxRichTextCtrl::DoGetBestSize() const
3063 return wxSize(10, 10);
3066 // ----------------------------------------------------------------------------
3067 // standard handlers for standard edit menu events
3068 // ----------------------------------------------------------------------------
3070 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3075 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3080 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3085 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3090 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3095 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3100 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3102 event
.Enable( CanCut() );
3105 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3107 event
.Enable( CanCopy() );
3110 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3112 event
.Enable( CanDeleteSelection() );
3115 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3117 event
.Enable( CanPaste() );
3120 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3122 event
.Enable( CanUndo() );
3123 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3126 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3128 event
.Enable( CanRedo() );
3129 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3132 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3134 if (GetLastPosition() > 0)
3138 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3140 event
.Enable(GetLastPosition() > 0);
3143 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3145 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3146 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3148 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3149 if (obj
&& obj
->CanEditProperties())
3150 obj
->EditProperties(this, & GetBuffer());
3152 m_contextMenuPropertiesInfo
.Clear();
3156 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3158 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3159 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3162 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3164 if (event
.GetEventObject() != this)
3170 wxClientDC
dc(this);
3172 dc
.SetFont(GetFont());
3175 wxPoint pt
= event
.GetPosition();
3176 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3177 wxRichTextObject
* hitObj
= NULL
;
3178 wxRichTextObject
* contextObj
= NULL
;
3179 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
3181 m_contextMenuPropertiesInfo
.Clear();
3183 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3185 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3186 if (hitObj
&& actualContainer
)
3188 if (actualContainer
->AcceptsFocus())
3190 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3191 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3194 m_contextMenuPropertiesInfo
.AddItems(actualContainer
, hitObj
);
3197 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3201 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3206 m_contextMenuPropertiesInfo
.AddMenuItems(m_contextMenu
);
3207 PopupMenu(m_contextMenu
);
3211 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3213 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3216 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3218 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3221 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3223 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3226 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3228 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3231 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
)
3233 GetFocusObject()->SetStyle(obj
, textAttr
);
3236 // extended style setting operation with flags including:
3237 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3238 // see richtextbuffer.h for more details.
3240 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3242 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3245 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3247 return GetBuffer().SetDefaultStyle(style
);
3250 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3252 wxRichTextAttr
attr1(style
);
3253 attr1
.GetTextBoxAttr().Reset();
3254 return GetBuffer().SetDefaultStyle(attr1
);
3257 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3259 return GetBuffer().GetDefaultStyle();
3262 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3264 wxRichTextAttr attr
;
3265 if (GetFocusObject()->GetStyle(position
, attr
))
3274 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3276 return GetFocusObject()->GetStyle(position
, style
);
3279 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3281 wxRichTextAttr attr
;
3282 if (container
->GetStyle(position
, attr
))
3291 // get the common set of styles for the range
3292 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3294 wxRichTextAttr attr
;
3295 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3304 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3306 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3309 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3311 return container
->GetStyleForRange(range
.ToInternal(), style
);
3314 /// Get the content (uncombined) attributes for this position.
3315 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3317 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3320 /// Get the content (uncombined) attributes for this position.
3321 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3323 return container
->GetUncombinedStyle(position
, style
);
3326 /// Set font, and also the buffer attributes
3327 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3329 wxControl::SetFont(font
);
3331 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3333 GetBuffer().SetBasicStyle(attr
);
3335 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3341 /// Transform logical to physical
3342 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3345 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3350 /// Transform physical to logical
3351 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3354 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3359 /// Position the caret
3360 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3365 //wxLogDebug(wxT("PositionCaret"));
3368 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3370 wxPoint newPt
= caretRect
.GetPosition();
3371 wxSize newSz
= caretRect
.GetSize();
3372 wxPoint pt
= GetPhysicalPoint(newPt
);
3373 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3376 if (GetCaret()->GetSize() != newSz
)
3377 GetCaret()->SetSize(newSz
);
3379 int halfSize
= newSz
.y
/2;
3380 // If the caret is beyond the margin, hide it by moving it out of the way
3381 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3387 GetCaret()->Move(pt
);
3393 /// Get the caret height and position for the given character position
3394 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3396 wxClientDC
dc(this);
3397 dc
.SetFont(GetFont());
3405 container
= GetFocusObject();
3407 if (container
->FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
3409 // Caret height can't be zero
3411 height
= dc
.GetCharHeight();
3413 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3420 /// Gets the line for the visible caret position. If the caret is
3421 /// shown at the very end of the line, it means the next character is actually
3422 /// on the following line. So let's get the line we're expecting to find
3423 /// if this is the case.
3424 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3426 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3427 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3430 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3431 if (caretPosition
== lineRange
.GetStart()-1 &&
3432 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3434 if (!m_caretAtLineStart
)
3435 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3442 /// Move the caret to the given character position
3443 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3445 if (GetBuffer().IsDirty())
3449 container
= GetFocusObject();
3451 if (pos
<= container
->GetOwnRange().GetEnd())
3453 SetCaretPosition(pos
, showAtLineStart
);
3455 PositionCaret(container
);
3463 /// Layout the buffer: which we must do before certain operations, such as
3464 /// setting the caret position.
3465 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3467 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3469 wxRect
availableSpace(GetClientSize());
3470 if (availableSpace
.width
== 0)
3471 availableSpace
.width
= 10;
3472 if (availableSpace
.height
== 0)
3473 availableSpace
.height
= 10;
3475 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3476 if (onlyVisibleRect
)
3478 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3479 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3482 wxClientDC
dc(this);
3483 dc
.SetFont(GetFont());
3487 GetBuffer().Defragment();
3488 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3489 GetBuffer().Layout(dc
, availableSpace
, flags
);
3490 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3499 /// Is all of the selection bold?
3500 bool wxRichTextCtrl::IsSelectionBold()
3504 wxRichTextAttr attr
;
3505 wxRichTextRange range
= GetSelectionRange();
3506 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3507 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3509 return HasCharacterAttributes(range
, attr
);
3513 // If no selection, then we need to combine current style with default style
3514 // to see what the effect would be if we started typing.
3515 wxRichTextAttr attr
;
3516 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3518 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3519 if (GetStyle(pos
, attr
))
3521 if (IsDefaultStyleShowing())
3522 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3523 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3529 /// Is all of the selection italics?
3530 bool wxRichTextCtrl::IsSelectionItalics()
3534 wxRichTextRange range
= GetSelectionRange();
3535 wxRichTextAttr attr
;
3536 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3537 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3539 return HasCharacterAttributes(range
, attr
);
3543 // If no selection, then we need to combine current style with default style
3544 // to see what the effect would be if we started typing.
3545 wxRichTextAttr attr
;
3546 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3548 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3549 if (GetStyle(pos
, attr
))
3551 if (IsDefaultStyleShowing())
3552 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3553 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3559 /// Is all of the selection underlined?
3560 bool wxRichTextCtrl::IsSelectionUnderlined()
3564 wxRichTextRange range
= GetSelectionRange();
3565 wxRichTextAttr attr
;
3566 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3567 attr
.SetFontUnderlined(true);
3569 return HasCharacterAttributes(range
, attr
);
3573 // If no selection, then we need to combine current style with default style
3574 // to see what the effect would be if we started typing.
3575 wxRichTextAttr attr
;
3576 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3577 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3579 if (GetStyle(pos
, attr
))
3581 if (IsDefaultStyleShowing())
3582 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3583 return attr
.GetFontUnderlined();
3589 /// Apply bold to the selection
3590 bool wxRichTextCtrl::ApplyBoldToSelection()
3592 wxRichTextAttr attr
;
3593 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3594 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3597 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3600 wxRichTextAttr current
= GetDefaultStyleEx();
3601 current
.Apply(attr
);
3602 SetAndShowDefaultStyle(current
);
3607 /// Apply italic to the selection
3608 bool wxRichTextCtrl::ApplyItalicToSelection()
3610 wxRichTextAttr attr
;
3611 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3612 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3615 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3618 wxRichTextAttr current
= GetDefaultStyleEx();
3619 current
.Apply(attr
);
3620 SetAndShowDefaultStyle(current
);
3625 /// Apply underline to the selection
3626 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3628 wxRichTextAttr attr
;
3629 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3630 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3633 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3636 wxRichTextAttr current
= GetDefaultStyleEx();
3637 current
.Apply(attr
);
3638 SetAndShowDefaultStyle(current
);
3643 /// Is all of the selection aligned according to the specified flag?
3644 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3646 wxRichTextRange range
;
3648 range
= GetSelectionRange();
3650 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3652 wxRichTextAttr attr
;
3653 attr
.SetAlignment(alignment
);
3655 return HasParagraphAttributes(range
, attr
);
3658 /// Apply alignment to the selection
3659 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3661 wxRichTextAttr attr
;
3662 attr
.SetAlignment(alignment
);
3664 return SetStyle(GetSelectionRange(), attr
);
3667 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
3669 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3674 /// Apply a named style to the selection
3675 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3677 // Flags are defined within each definition, so only certain
3678 // attributes are applied.
3679 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3681 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3683 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3685 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3687 wxRichTextRange range
;
3690 range
= GetSelectionRange();
3693 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3694 range
= wxRichTextRange(pos
, pos
+1);
3697 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3700 bool isPara
= false;
3702 // Make sure the attr has the style name
3703 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3706 attr
.SetParagraphStyleName(def
->GetName());
3708 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3709 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3710 // to change its style independently.
3711 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3714 attr
.SetCharacterStyleName(def
->GetName());
3717 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3720 wxRichTextAttr current
= GetDefaultStyleEx();
3721 wxRichTextAttr
defaultStyle(attr
);
3724 // Don't apply extra character styles since they are already implied
3725 // in the paragraph style
3726 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
3728 current
.Apply(defaultStyle
);
3729 SetAndShowDefaultStyle(current
);
3731 // If it's a paragraph style, we want to apply the style to the
3732 // current paragraph even if we didn't select any text.
3735 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3736 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
3739 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
3746 /// Apply the style sheet to the buffer, for example if the styles have changed.
3747 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3750 styleSheet
= GetBuffer().GetStyleSheet();
3754 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3756 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3764 /// Sets the default style to the style under the cursor
3765 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3767 wxRichTextAttr attr
;
3768 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3770 // If at the start of a paragraph, use the next position.
3771 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3773 if (GetUncombinedStyle(pos
, attr
))
3775 SetDefaultStyle(attr
);
3782 /// Returns the first visible position in the current view
3783 long wxRichTextCtrl::GetFirstVisiblePosition() const
3785 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3787 return line
->GetAbsoluteRange().GetStart();
3792 /// Get the first visible point in the window
3793 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3796 int startXUnits
, startYUnits
;
3798 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3799 GetViewStart(& startXUnits
, & startYUnits
);
3801 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3804 /// The adjusted caret position is the character position adjusted to take
3805 /// into account whether we're at the start of a paragraph, in which case
3806 /// style information should be taken from the next position, not current one.
3807 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3809 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
3811 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3816 /// Get/set the selection range in character positions. -1, -1 means no selection.
3817 /// The range is in API convention, i.e. a single character selection is denoted
3819 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3821 wxRichTextRange range
= GetInternalSelectionRange();
3822 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3823 range
.SetEnd(range
.GetEnd() + 1);
3827 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3829 SetSelection(range
.GetStart(), range
.GetEnd());
3833 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3835 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3838 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3840 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3843 /// Clear list for given range
3844 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3846 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
3849 /// Number/renumber any list elements in the given range
3850 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3852 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3855 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3857 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3860 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3861 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3863 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3866 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3868 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3871 /// Deletes the content in the given range
3872 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3874 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
3877 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3879 if (sm_availableFontNames
.GetCount() == 0)
3881 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3882 sm_availableFontNames
.Sort();
3884 return sm_availableFontNames
;
3887 void wxRichTextCtrl::ClearAvailableFontNames()
3889 sm_availableFontNames
.Clear();
3892 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3894 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3896 wxTextAttrEx basicStyle
= GetBasicStyle();
3897 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3898 SetBasicStyle(basicStyle
);
3899 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3904 // Refresh the area affected by a selection change
3905 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
3907 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
3908 // the selection contains whole containers rather than just text, so refresh everything
3909 // for now as it would be hard to compute the rectangle bounding all selections.
3910 // TODO: improve on this.
3911 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
3912 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
3918 wxRichTextRange oldRange
, newRange
;
3919 if (oldSelection
.IsValid())
3920 oldRange
= oldSelection
.GetRange();
3922 oldRange
= wxRICHTEXT_NO_SELECTION
;
3923 if (newSelection
.IsValid())
3924 newRange
= newSelection
.GetRange();
3926 newRange
= wxRICHTEXT_NO_SELECTION
;
3928 // Calculate the refresh rectangle - just the affected lines
3929 long firstPos
, lastPos
;
3930 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
3932 firstPos
= newRange
.GetStart();
3933 lastPos
= newRange
.GetEnd();
3935 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
3937 firstPos
= oldRange
.GetStart();
3938 lastPos
= oldRange
.GetEnd();
3940 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
3946 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
3947 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
3950 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
3951 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
3953 if (firstLine
&& lastLine
)
3955 wxSize clientSize
= GetClientSize();
3956 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3957 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3960 pt1
.y
= wxMax(0, pt1
.y
);
3962 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3964 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3965 RefreshRect(rect
, false);
3973 // margins functions
3974 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
3976 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
3977 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
3978 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
3979 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
3984 wxPoint
wxRichTextCtrl::DoGetMargins() const
3986 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
3987 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
3990 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
3992 if (obj
&& !obj
->AcceptsFocus())
3995 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
3996 bool changingContainer
= (m_focusObject
!= obj
);
3998 m_focusObject
= obj
;
4001 m_focusObject
= & m_buffer
;
4003 if (setCaretPosition
&& changingContainer
)
4005 m_selection
.Reset();
4006 m_selectionAnchor
= -2;
4007 m_selectionAnchorObject
= NULL
;
4008 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4012 m_caretAtLineStart
= false;
4013 MoveCaret(pos
, m_caretAtLineStart
);
4014 SetDefaultStyleToCursorStyle();
4016 wxRichTextEvent
cmdEvent(
4017 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4019 cmdEvent
.SetEventObject(this);
4020 cmdEvent
.SetPosition(m_caretPosition
+1);
4021 cmdEvent
.SetOldContainer(oldContainer
);
4022 cmdEvent
.SetContainer(m_focusObject
);
4024 GetEventHandler()->ProcessEvent(cmdEvent
);
4029 #if wxRICHTEXT_USE_OWN_CARET
4031 // ----------------------------------------------------------------------------
4032 // initialization and destruction
4033 // ----------------------------------------------------------------------------
4035 void wxRichTextCaret::Init()
4041 m_richTextCtrl
= NULL
;
4042 m_needsUpdate
= false;
4046 wxRichTextCaret::~wxRichTextCaret()
4048 if (m_timer
.IsRunning())
4052 // ----------------------------------------------------------------------------
4053 // showing/hiding/moving the caret (base class interface)
4054 // ----------------------------------------------------------------------------
4056 void wxRichTextCaret::DoShow()
4060 if (!m_timer
.IsRunning())
4061 m_timer
.Start(GetBlinkTime());
4066 void wxRichTextCaret::DoHide()
4068 if (m_timer
.IsRunning())
4074 void wxRichTextCaret::DoMove()
4080 if (m_xOld
!= -1 && m_yOld
!= -1)
4084 wxRect
rect(GetPosition(), GetSize());
4085 m_richTextCtrl
->RefreshRect(rect
, false);
4094 void wxRichTextCaret::DoSize()
4096 int countVisible
= m_countVisible
;
4097 if (countVisible
> 0)
4103 if (countVisible
> 0)
4105 m_countVisible
= countVisible
;
4110 // ----------------------------------------------------------------------------
4111 // handling the focus
4112 // ----------------------------------------------------------------------------
4114 void wxRichTextCaret::OnSetFocus()
4122 void wxRichTextCaret::OnKillFocus()
4127 // ----------------------------------------------------------------------------
4128 // drawing the caret
4129 // ----------------------------------------------------------------------------
4131 void wxRichTextCaret::Refresh()
4135 wxRect
rect(GetPosition(), GetSize());
4136 m_richTextCtrl
->RefreshRect(rect
, false);
4140 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4142 dc
->SetPen( *wxBLACK_PEN
);
4144 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4145 dc
->SetPen(*wxBLACK_PEN
);
4147 wxPoint
pt(m_x
, m_y
);
4151 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4153 if (IsVisible() && m_flashOn
)
4154 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4157 void wxRichTextCaret::Notify()
4159 m_flashOn
= !m_flashOn
;
4163 void wxRichTextCaretTimer::Notify()
4168 // wxRICHTEXT_USE_OWN_CARET
4171 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4175 m_labels
.Add(label
);
4183 // Returns number of menu items were added.
4184 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4186 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4187 // If none of the standard properties identifiers are in the menu, assume it's
4188 // a custom menu without properties commands, and don't add them.
4191 // If no items, to add just set the text to something generic
4192 if (GetCount() == 0)
4194 menu
->SetLabel(startCmd
, _("&Properties"));
4196 // Delete the others if necessary
4198 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4200 if (menu
->FindItem(i
))
4210 // Find the position of the first properties item
4211 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4213 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4214 if (item
&& item
->GetId() == startCmd
)
4223 int insertBefore
= pos
+1;
4224 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4226 if (menu
->FindItem(i
))
4228 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4232 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4233 menu
->Append(i
, m_labels
[i
- startCmd
]);
4235 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4240 // Delete any old items still left on the menu
4241 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4243 if (menu
->FindItem(i
))
4255 // Add appropriate menu items for the current container and clicked on object
4256 // (and container's parent, if appropriate).
4257 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextObject
* container
, wxRichTextObject
* obj
)
4260 if (obj
&& obj
->CanEditProperties())
4261 AddItem(obj
->GetPropertiesMenuLabel(), obj
);
4263 if (container
&& container
!= obj
&& container
->CanEditProperties() && m_labels
.Index(container
->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4264 AddItem(container
->GetPropertiesMenuLabel(), container
);
4266 if (container
&& container
->GetParent() && container
->GetParent()->CanEditProperties() && m_labels
.Index(container
->GetParent()->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4267 AddItem(container
->GetParent()->GetPropertiesMenuLabel(), container
->GetParent());