1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
30 #include "wx/textfile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/fontenum.h"
38 #if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
39 #define wxHAVE_PRIMARY_SELECTION 1
41 #define wxHAVE_PRIMARY_SELECTION 0
44 #if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION
45 #include "wx/clipbrd.h"
48 // DLL options compatibility check:
50 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
54 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
60 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
61 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
62 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
63 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
65 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
66 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
67 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
68 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
69 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
70 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
, wxRichTextEvent
);
72 #if wxRICHTEXT_USE_OWN_CARET
77 * This implements a non-flashing cursor in case there
78 * are platform-specific problems with the generic caret.
79 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
82 class wxRichTextCaret
;
83 class wxRichTextCaretTimer
: public wxTimer
86 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
90 virtual void Notify();
91 wxRichTextCaret
* m_caret
;
94 class wxRichTextCaret
: public wxCaret
99 // default - use Create()
100 wxRichTextCaret(): m_timer(this) { Init(); }
101 // creates a block caret associated with the given window
102 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
103 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
104 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
105 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
107 virtual ~wxRichTextCaret();
112 // called by wxWindow (not using the event tables)
113 virtual void OnSetFocus();
114 virtual void OnKillFocus();
116 // draw the caret on the given DC
117 void DoDraw(wxDC
*dc
);
119 // get the visible count
120 int GetVisibleCount() const { return m_countVisible
; }
122 // delay repositioning
123 bool GetNeedsUpdate() const { return m_needsUpdate
; }
124 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
129 virtual void DoShow();
130 virtual void DoHide();
131 virtual void DoMove();
132 virtual void DoSize();
142 bool m_hasFocus
; // true => our window has focus
143 bool m_needsUpdate
; // must be repositioned
145 wxRichTextCaretTimer m_timer
;
146 wxRichTextCtrl
* m_richTextCtrl
;
150 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl
)
152 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
154 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
155 EVT_PAINT(wxRichTextCtrl::OnPaint
)
156 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
157 EVT_IDLE(wxRichTextCtrl::OnIdle
)
158 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
159 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
160 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
161 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
162 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
163 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
164 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
165 EVT_CHAR(wxRichTextCtrl::OnChar
)
166 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
167 EVT_SIZE(wxRichTextCtrl::OnSize
)
168 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
169 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
170 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
171 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
172 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
174 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
175 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
177 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
178 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
180 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
181 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
183 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
184 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
186 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
187 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
189 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
190 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
192 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
193 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
195 EVT_MENU(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnProperties
)
196 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnUpdateProperties
)
198 EVT_MENU(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnProperties
)
199 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnUpdateProperties
)
201 EVT_MENU(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnProperties
)
202 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnUpdateProperties
)
210 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
212 wxRichTextCtrl::wxRichTextCtrl()
213 : wxScrollHelper(this)
218 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
220 const wxString
& value
,
224 const wxValidator
& validator
,
225 const wxString
& name
)
226 : wxScrollHelper(this)
229 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
233 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
234 const wxValidator
& validator
, const wxString
& name
)
238 if (!wxControl::Create(parent
, id
, pos
, size
,
239 style
|wxFULL_REPAINT_ON_RESIZE
,
243 if (!GetFont().IsOk())
245 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
248 // No physical scrolling, so we can preserve margins
249 EnableScrolling(false, false);
251 if (style
& wxTE_READONLY
)
254 // The base attributes must all have default values
255 wxRichTextAttr attributes
;
256 attributes
.SetFont(GetFont());
257 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
258 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
259 attributes
.SetLineSpacing(10);
260 attributes
.SetParagraphSpacingAfter(10);
261 attributes
.SetParagraphSpacingBefore(0);
263 SetBasicStyle(attributes
);
266 SetMargins(margin
, margin
);
268 // The default attributes will be merged with base attributes, so
269 // can be empty to begin with
270 wxRichTextAttr defaultAttributes
;
271 SetDefaultStyle(defaultAttributes
);
273 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
274 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
277 GetBuffer().SetRichTextCtrl(this);
279 #if wxRICHTEXT_USE_OWN_CARET
280 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
282 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
285 // Tell the sizers to use the given or best size
286 SetInitialSize(size
);
288 #if wxRICHTEXT_BUFFERED_PAINTING
290 RecreateBuffer(size
);
293 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
294 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
296 SetCursor(m_textCursor
);
298 if (!value
.IsEmpty())
301 GetBuffer().AddEventHandler(this);
304 wxAcceleratorEntry entries
[6];
306 entries
[0].Set(wxACCEL_CTRL
, (int) 'C', wxID_COPY
);
307 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_CUT
);
308 entries
[2].Set(wxACCEL_CTRL
, (int) 'V', wxID_PASTE
);
309 entries
[3].Set(wxACCEL_CTRL
, (int) 'A', wxID_SELECTALL
);
310 entries
[4].Set(wxACCEL_CTRL
, (int) 'Z', wxID_UNDO
);
311 entries
[5].Set(wxACCEL_CTRL
, (int) 'Y', wxID_REDO
);
313 wxAcceleratorTable
accel(6, entries
);
314 SetAcceleratorTable(accel
);
316 m_contextMenu
= new wxMenu
;
317 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
318 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
319 m_contextMenu
->AppendSeparator();
320 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
321 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
322 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
323 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
324 m_contextMenu
->AppendSeparator();
325 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
326 m_contextMenu
->AppendSeparator();
327 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
332 wxRichTextCtrl::~wxRichTextCtrl()
334 SetFocusObject(& GetBuffer(), false);
335 GetBuffer().RemoveEventHandler(this);
337 delete m_contextMenu
;
340 /// Member initialisation
341 void wxRichTextCtrl::Init()
343 m_contextMenu
= NULL
;
345 m_caretPosition
= -1;
346 m_selectionAnchor
= -2;
347 m_selectionAnchorObject
= NULL
;
348 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
350 m_caretAtLineStart
= false;
352 m_fullLayoutRequired
= false;
353 m_fullLayoutTime
= 0;
354 m_fullLayoutSavedPosition
= 0;
355 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
356 m_caretPositionForDefaultStyle
= -2;
357 m_focusObject
= & m_buffer
;
360 void wxRichTextCtrl::DoThaw()
362 if (GetBuffer().IsDirty())
371 void wxRichTextCtrl::Clear()
373 if (GetFocusObject() == & GetBuffer())
375 m_buffer
.ResetAndClearCommands();
376 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
380 GetFocusObject()->Reset();
383 m_caretPosition
= -1;
384 m_caretPositionForDefaultStyle
= -2;
385 m_caretAtLineStart
= false;
387 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
397 wxTextCtrl::SendTextUpdatedEvent(this);
401 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
403 #if !wxRICHTEXT_USE_OWN_CARET
404 if (GetCaret() && !IsFrozen())
409 #if wxRICHTEXT_BUFFERED_PAINTING
410 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
420 dc
.SetFont(GetFont());
422 // Paint the background
425 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
427 wxRect
drawingArea(GetUpdateRegion().GetBox());
428 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
430 wxRect
availableSpace(GetClientSize());
431 if (GetBuffer().IsDirty())
433 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
434 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
438 wxRect
clipRect(availableSpace
);
439 clipRect
.x
+= GetBuffer().GetLeftMargin();
440 clipRect
.y
+= GetBuffer().GetTopMargin();
441 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
442 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
443 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
444 dc
.SetClippingRegion(clipRect
);
447 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
448 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
450 GetBuffer().Draw(dc
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
452 dc
.DestroyClippingRegion();
454 // Other user defined painting after everything else (i.e. all text) is painted
455 PaintAboveContent(dc
);
457 #if wxRICHTEXT_USE_OWN_CARET
458 if (GetCaret()->IsVisible())
460 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
465 #if !wxRICHTEXT_USE_OWN_CARET
470 #if !defined(__WXMAC__)
471 // Causes caret dropouts on Mac
477 // Empty implementation, to prevent flicker
478 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
482 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
486 #if !wxRICHTEXT_USE_OWN_CARET
492 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
493 // Work around dropouts when control is focused
501 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
506 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
507 // Work around dropouts when control is focused
515 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
520 // Set up the caret for the given position and container, after a mouse click
521 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
523 bool caretAtLineStart
= false;
525 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
527 // If we're at the start of a line (but not first in para)
528 // then we should keep the caret showing at the start of the line
529 // by showing the m_caretAtLineStart flag.
530 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
531 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
533 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
534 caretAtLineStart
= true;
538 if (extendSelection
&& (m_caretPosition
!= position
))
539 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
541 MoveCaret(position
, caretAtLineStart
);
542 SetDefaultStyleToCursorStyle();
548 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
554 dc
.SetFont(GetFont());
556 // TODO: detect change of focus object
558 wxRichTextObject
* hitObj
= NULL
;
559 wxRichTextObject
* contextObj
= NULL
;
560 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
562 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
564 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
565 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
566 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
568 SetFocusObject(container
, false /* don't set caret position yet */);
571 m_dragStart
= event
.GetLogicalPosition(dc
);
575 long oldCaretPos
= m_caretPosition
;
577 SetCaretPositionAfterClick(container
, position
, hit
);
579 // For now, don't handle shift-click when we're selecting multiple objects.
580 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
581 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
590 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
595 if (GetCapture() == this)
598 // See if we clicked on a URL
601 dc
.SetFont(GetFont());
604 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
605 wxRichTextObject
* hitObj
= NULL
;
606 wxRichTextObject
* contextObj
= NULL
;
607 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
608 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
610 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
612 wxRichTextEvent
cmdEvent(
613 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
615 cmdEvent
.SetEventObject(this);
616 cmdEvent
.SetPosition(position
);
618 cmdEvent
.SetContainer(hitObj
->GetContainer());
620 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
623 if (GetStyle(position
, attr
))
625 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
627 wxString urlTarget
= attr
.GetURL();
628 if (!urlTarget
.IsEmpty())
630 wxMouseEvent
mouseEvent(event
);
632 long startPos
= 0, endPos
= 0;
633 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
636 startPos
= obj
->GetRange().GetStart();
637 endPos
= obj
->GetRange().GetEnd();
640 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
641 InitCommandEvent(urlEvent
);
643 urlEvent
.SetString(urlTarget
);
645 GetEventHandler()->ProcessEvent(urlEvent
);
653 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
654 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
656 // Put the selection in PRIMARY, if it exists
657 wxTheClipboard
->UsePrimarySelection(true);
659 wxRichTextRange range
= GetInternalSelectionRange();
660 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
662 wxTheClipboard
->UsePrimarySelection(false);
668 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
672 dc
.SetFont(GetFont());
675 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
676 wxRichTextObject
* hitObj
= NULL
;
677 wxRichTextObject
* contextObj
= NULL
;
681 // If we're dragging, let's only consider positions at this level; otherwise
682 // selecting a range is not going to work.
683 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
686 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
687 container
= GetFocusObject();
689 int hit
= container
->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
691 // See if we need to change the cursor
694 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
696 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
698 if (actualContainer
&& GetStyle(position
, attr
, actualContainer
))
700 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
702 SetCursor(m_urlCursor
);
704 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
706 SetCursor(m_textCursor
);
711 SetCursor(m_textCursor
);
714 if (!event
.Dragging())
722 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
723 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
724 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
726 // Check for dragging across multiple containers
728 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
729 int hit2
= GetBuffer().HitTest(dc
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
730 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
732 // See if we can find a common ancestor
733 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
735 firstContainer
= GetFocusObject();
736 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
740 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
741 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
742 // is the common ancestor.
743 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
746 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
748 wxRichTextObject
* p
= hitObj2
;
751 if (p
->GetParent() == commonAncestor
)
753 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
760 if (commonAncestor
&& firstContainer
&& otherContainer
)
762 // We have now got a second container that shares a parent with the current or anchor object.
763 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
765 // Don't go into common-ancestor selection mode if we still have the same
767 if (otherContainer
!= firstContainer
)
769 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
770 m_selectionAnchorObject
= firstContainer
;
771 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
773 // The common ancestor, such as a table, returns the cell selection
774 // between the anchor and current position.
775 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
780 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
785 if (otherContainer
->AcceptsFocus())
786 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
787 MoveCaret(-1, false);
788 SetDefaultStyleToCursorStyle();
793 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
795 // TODO: test closeness
796 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
801 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
807 dc
.SetFont(GetFont());
810 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
811 wxRichTextObject
* hitObj
= NULL
;
812 wxRichTextObject
* contextObj
= NULL
;
813 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
815 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
817 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
818 if (actualContainer
&& actualContainer
->AcceptsFocus())
820 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
821 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
825 wxRichTextEvent
cmdEvent(
826 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
828 cmdEvent
.SetEventObject(this);
829 cmdEvent
.SetPosition(position
);
831 cmdEvent
.SetContainer(hitObj
->GetContainer());
833 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
837 /// Left-double-click
838 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
840 wxRichTextEvent
cmdEvent(
841 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
843 cmdEvent
.SetEventObject(this);
844 cmdEvent
.SetPosition(m_caretPosition
+1);
845 cmdEvent
.SetContainer(GetFocusObject());
847 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
849 SelectWord(GetCaretPosition()+1);
854 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
856 wxRichTextEvent
cmdEvent(
857 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
859 cmdEvent
.SetEventObject(this);
860 cmdEvent
.SetPosition(m_caretPosition
+1);
861 cmdEvent
.SetContainer(GetFocusObject());
863 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
866 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
867 // Paste any PRIMARY selection, if it exists
868 wxTheClipboard
->UsePrimarySelection(true);
870 wxTheClipboard
->UsePrimarySelection(false);
875 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
879 flags
|= wxRICHTEXT_CTRL_DOWN
;
880 if (event
.ShiftDown())
881 flags
|= wxRICHTEXT_SHIFT_DOWN
;
883 flags
|= wxRICHTEXT_ALT_DOWN
;
885 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
887 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
889 KeyboardNavigate(event
.GetKeyCode(), flags
);
893 long keycode
= event
.GetKeyCode();
953 case WXK_NUMPAD_HOME
:
954 case WXK_NUMPAD_LEFT
:
956 case WXK_NUMPAD_RIGHT
:
957 case WXK_NUMPAD_DOWN
:
958 case WXK_NUMPAD_PAGEUP
:
959 case WXK_NUMPAD_PAGEDOWN
:
961 case WXK_NUMPAD_BEGIN
:
962 case WXK_NUMPAD_INSERT
:
963 case WXK_WINDOWS_LEFT
:
972 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
973 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
975 BeginBatchUndo(_("Delete Text"));
977 long newPos
= m_caretPosition
;
979 bool processed
= DeleteSelectedContent(& newPos
);
981 // Submit range in character positions, which are greater than caret positions,
982 // so subtract 1 for deleted character and add 1 for conversion to character position.
987 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
990 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
996 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1001 if (GetLastPosition() == -1)
1003 GetFocusObject()->Reset();
1005 m_caretPosition
= -1;
1007 SetDefaultStyleToCursorStyle();
1010 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1012 wxRichTextEvent
cmdEvent(
1013 wxEVT_COMMAND_RICHTEXT_DELETE
,
1015 cmdEvent
.SetEventObject(this);
1016 cmdEvent
.SetFlags(flags
);
1017 cmdEvent
.SetPosition(m_caretPosition
+1);
1018 cmdEvent
.SetContainer(GetFocusObject());
1019 GetEventHandler()->ProcessEvent(cmdEvent
);
1029 // all the other keys modify the controls contents which shouldn't be
1030 // possible if we're read-only
1031 if ( !IsEditable() )
1037 if (event
.GetKeyCode() == WXK_RETURN
)
1039 BeginBatchUndo(_("Insert Text"));
1041 long newPos
= m_caretPosition
;
1043 DeleteSelectedContent(& newPos
);
1045 if (event
.ShiftDown())
1048 text
= wxRichTextLineBreakChar
;
1049 GetFocusObject()->InsertTextWithUndo(newPos
+1, text
, this, & GetBuffer());
1050 m_caretAtLineStart
= true;
1054 GetFocusObject()->InsertNewlineWithUndo(newPos
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1057 SetDefaultStyleToCursorStyle();
1059 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1061 wxRichTextEvent
cmdEvent(
1062 wxEVT_COMMAND_RICHTEXT_RETURN
,
1064 cmdEvent
.SetEventObject(this);
1065 cmdEvent
.SetFlags(flags
);
1066 cmdEvent
.SetPosition(newPos
+1);
1067 cmdEvent
.SetContainer(GetFocusObject());
1069 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1071 // Generate conventional event
1072 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1073 InitCommandEvent(textEvent
);
1075 GetEventHandler()->ProcessEvent(textEvent
);
1079 else if (event
.GetKeyCode() == WXK_BACK
)
1081 BeginBatchUndo(_("Delete Text"));
1083 long newPos
= m_caretPosition
;
1085 bool processed
= DeleteSelectedContent(& newPos
);
1087 // Submit range in character positions, which are greater than caret positions,
1088 // so subtract 1 for deleted character and add 1 for conversion to character position.
1091 if (event
.CmdDown())
1093 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1096 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
1102 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1107 if (GetLastPosition() == -1)
1109 GetFocusObject()->Reset();
1111 m_caretPosition
= -1;
1113 SetDefaultStyleToCursorStyle();
1116 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1118 wxRichTextEvent
cmdEvent(
1119 wxEVT_COMMAND_RICHTEXT_DELETE
,
1121 cmdEvent
.SetEventObject(this);
1122 cmdEvent
.SetFlags(flags
);
1123 cmdEvent
.SetPosition(m_caretPosition
+1);
1124 cmdEvent
.SetContainer(GetFocusObject());
1125 GetEventHandler()->ProcessEvent(cmdEvent
);
1129 else if (event
.GetKeyCode() == WXK_DELETE
)
1131 BeginBatchUndo(_("Delete Text"));
1133 long newPos
= m_caretPosition
;
1135 bool processed
= DeleteSelectedContent(& newPos
);
1137 // Submit range in character positions, which are greater than caret positions,
1138 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1140 if (event
.CmdDown())
1142 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1143 if (pos
!= -1 && (pos
> newPos
))
1145 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this, & GetBuffer());
1150 if (!processed
&& newPos
< (GetLastPosition()-1))
1151 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this, & GetBuffer());
1156 if (GetLastPosition() == -1)
1158 GetFocusObject()->Reset();
1160 m_caretPosition
= -1;
1162 SetDefaultStyleToCursorStyle();
1165 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1167 wxRichTextEvent
cmdEvent(
1168 wxEVT_COMMAND_RICHTEXT_DELETE
,
1170 cmdEvent
.SetEventObject(this);
1171 cmdEvent
.SetFlags(flags
);
1172 cmdEvent
.SetPosition(m_caretPosition
+1);
1173 cmdEvent
.SetContainer(GetFocusObject());
1174 GetEventHandler()->ProcessEvent(cmdEvent
);
1180 long keycode
= event
.GetKeyCode();
1192 if (event
.CmdDown())
1194 // Fixes AltGr+key with European input languages on Windows
1195 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1202 wxRichTextEvent
cmdEvent(
1203 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1205 cmdEvent
.SetEventObject(this);
1206 cmdEvent
.SetFlags(flags
);
1208 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1210 cmdEvent
.SetCharacter((wxChar
) keycode
);
1212 cmdEvent
.SetPosition(m_caretPosition
+1);
1213 cmdEvent
.SetContainer(GetFocusObject());
1215 if (keycode
== wxT('\t'))
1217 // See if we need to promote or demote the selection or paragraph at the cursor
1218 // position, instead of inserting a tab.
1219 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1220 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1221 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1223 wxRichTextRange range
;
1225 range
= GetSelectionRange();
1227 range
= para
->GetRange().FromInternal();
1229 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1231 PromoteList(promoteBy
, range
, NULL
);
1233 GetEventHandler()->ProcessEvent(cmdEvent
);
1239 BeginBatchUndo(_("Insert Text"));
1241 long newPos
= m_caretPosition
;
1242 DeleteSelectedContent(& newPos
);
1245 wxString str
= event
.GetUnicodeKey();
1247 wxString str
= (wxChar
) event
.GetKeyCode();
1249 GetFocusObject()->InsertTextWithUndo(newPos
+1, str
, this, & GetBuffer(), 0);
1253 SetDefaultStyleToCursorStyle();
1254 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1256 cmdEvent
.SetPosition(m_caretPosition
);
1257 GetEventHandler()->ProcessEvent(cmdEvent
);
1265 /// Delete content if there is a selection, e.g. when pressing a key.
1266 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1270 long pos
= m_selection
.GetRange().GetStart();
1271 wxRichTextRange range
= m_selection
.GetRange();
1273 // SelectAll causes more to be selected than doing it interactively,
1274 // and causes a new paragraph to be inserted. So for multiline buffers,
1275 // don't delete the final position.
1276 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1277 range
.SetEnd(range
.GetEnd()-1);
1279 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1280 m_selection
.Reset();
1281 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1291 /// Keyboard navigation
1295 Left: left one character
1296 Right: right one character
1299 Ctrl-Left: left one word
1300 Ctrl-Right: right one word
1301 Ctrl-Up: previous paragraph start
1302 Ctrl-Down: next start of paragraph
1305 Ctrl-Home: start of document
1306 Ctrl-End: end of document
1307 Page-Up: Up a screen
1308 Page-Down: Down a screen
1312 Ctrl-Alt-PgUp: Start of window
1313 Ctrl-Alt-PgDn: End of window
1314 F8: Start selection mode
1315 Esc: End selection mode
1317 Adding Shift does the above but starts/extends selection.
1322 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1324 bool success
= false;
1326 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1328 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1329 success
= WordRight(1, flags
);
1331 success
= MoveRight(1, flags
);
1333 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1335 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1336 success
= WordLeft(1, flags
);
1338 success
= MoveLeft(1, flags
);
1340 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1342 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1343 success
= MoveToParagraphStart(flags
);
1345 success
= MoveUp(1, flags
);
1347 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1349 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1350 success
= MoveToParagraphEnd(flags
);
1352 success
= MoveDown(1, flags
);
1354 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1356 success
= PageUp(1, flags
);
1358 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1360 success
= PageDown(1, flags
);
1362 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1364 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1365 success
= MoveHome(flags
);
1367 success
= MoveToLineStart(flags
);
1369 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1371 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1372 success
= MoveEnd(flags
);
1374 success
= MoveToLineEnd(flags
);
1379 ScrollIntoView(m_caretPosition
, keyCode
);
1380 SetDefaultStyleToCursorStyle();
1386 /// Extend the selection. Selections are in caret positions.
1387 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1389 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1391 if (oldPos
== newPos
)
1394 wxRichTextSelection oldSelection
= m_selection
;
1396 m_selection
.SetContainer(GetFocusObject());
1398 wxRichTextRange oldRange
;
1399 if (m_selection
.IsValid())
1400 oldRange
= m_selection
.GetRange();
1402 oldRange
= wxRICHTEXT_NO_SELECTION
;
1403 wxRichTextRange newRange
;
1405 // If not currently selecting, start selecting
1406 if (oldRange
.GetStart() == -2)
1408 m_selectionAnchor
= oldPos
;
1410 if (oldPos
> newPos
)
1411 newRange
.SetRange(newPos
+1, oldPos
);
1413 newRange
.SetRange(oldPos
+1, newPos
);
1417 // Always ensure that the selection range start is greater than
1419 if (newPos
> m_selectionAnchor
)
1420 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1421 else if (newPos
== m_selectionAnchor
)
1422 newRange
= wxRichTextRange(-2, -2);
1424 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1427 m_selection
.SetRange(newRange
);
1429 RefreshForSelectionChange(oldSelection
, m_selection
);
1431 if (newRange
.GetStart() > newRange
.GetEnd())
1433 wxLogDebug(wxT("Strange selection range"));
1442 /// Scroll into view, returning true if we scrolled.
1443 /// This takes a _caret_ position.
1444 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1446 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1452 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1454 int startXUnits
, startYUnits
;
1455 GetViewStart(& startXUnits
, & startYUnits
);
1456 int startY
= startYUnits
* ppuY
;
1459 GetVirtualSize(& sx
, & sy
);
1465 wxRect rect
= line
->GetRect();
1467 bool scrolled
= false;
1469 wxSize clientSize
= GetClientSize();
1471 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1474 wxClientDC
dc(this);
1475 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1476 topMargin
, bottomMargin
);
1478 // clientSize.y -= GetBuffer().GetBottomMargin();
1479 clientSize
.y
-= bottomMargin
;
1481 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1483 int y
= rect
.y
- GetClientSize().y
/2;
1484 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1485 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1487 if (startYUnits
!= yUnits
)
1489 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1492 #if !wxRICHTEXT_USE_OWN_CARET
1502 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1503 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1504 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1505 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1507 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1509 // Make it scroll so this item is at the bottom
1511 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1512 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1514 // If we're still off the screen, scroll another line down
1515 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1518 if (startYUnits
!= yUnits
)
1520 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1524 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1526 // Make it scroll so this item is at the top
1528 int y
= rect
.y
- GetBuffer().GetTopMargin();
1529 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1531 if (startYUnits
!= yUnits
)
1533 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1539 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1540 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1541 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1542 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1544 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1546 // Make it scroll so this item is at the top
1548 int y
= rect
.y
- GetBuffer().GetTopMargin();
1549 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1551 if (startYUnits
!= yUnits
)
1553 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1557 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1559 // Make it scroll so this item is at the bottom
1561 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1562 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1564 // If we're still off the screen, scroll another line down
1565 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1568 if (startYUnits
!= yUnits
)
1570 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1576 #if !wxRICHTEXT_USE_OWN_CARET
1584 /// Is the given position visible on the screen?
1585 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1587 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1593 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1596 GetViewStart(& startX
, & startY
);
1598 startY
= startY
* ppuY
;
1600 wxRect rect
= line
->GetRect();
1601 wxSize clientSize
= GetClientSize();
1602 clientSize
.y
-= GetBuffer().GetBottomMargin();
1604 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1607 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1609 m_caretPosition
= position
;
1610 m_caretAtLineStart
= showAtLineStart
;
1613 /// Move caret one visual step forward: this may mean setting a flag
1614 /// and keeping the same position if we're going from the end of one line
1615 /// to the start of the next, which may be the exact same caret position.
1616 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1618 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1620 // Only do the check if we're not at the end of the paragraph (where things work OK
1622 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1624 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1628 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1630 // We're at the end of a line. See whether we need to
1631 // stay at the same actual caret position but change visual
1632 // position, or not.
1633 if (oldPosition
== lineRange
.GetEnd())
1635 if (m_caretAtLineStart
)
1637 // We're already at the start of the line, so actually move on now.
1638 m_caretPosition
= oldPosition
+ 1;
1639 m_caretAtLineStart
= false;
1643 // We're showing at the end of the line, so keep to
1644 // the same position but indicate that we're to show
1645 // at the start of the next line.
1646 m_caretPosition
= oldPosition
;
1647 m_caretAtLineStart
= true;
1649 SetDefaultStyleToCursorStyle();
1655 SetDefaultStyleToCursorStyle();
1658 /// Move caret one visual step backward: this may mean setting a flag
1659 /// and keeping the same position if we're going from the end of one line
1660 /// to the start of the next, which may be the exact same caret position.
1661 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1663 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1665 // Only do the check if we're not at the start of the paragraph (where things work OK
1667 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1669 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1673 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1675 // We're at the start of a line. See whether we need to
1676 // stay at the same actual caret position but change visual
1677 // position, or not.
1678 if (oldPosition
== lineRange
.GetStart())
1680 m_caretPosition
= oldPosition
-1;
1681 m_caretAtLineStart
= true;
1684 else if (oldPosition
== lineRange
.GetEnd())
1686 if (m_caretAtLineStart
)
1688 // We're at the start of the line, so keep the same caret position
1689 // but clear the start-of-line flag.
1690 m_caretPosition
= oldPosition
;
1691 m_caretAtLineStart
= false;
1695 // We're showing at the end of the line, so go back
1696 // to the previous character position.
1697 m_caretPosition
= oldPosition
- 1;
1699 SetDefaultStyleToCursorStyle();
1705 SetDefaultStyleToCursorStyle();
1709 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1711 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1713 if (m_caretPosition
+ noPositions
< endPos
)
1715 long oldPos
= m_caretPosition
;
1716 long newPos
= m_caretPosition
+ noPositions
;
1718 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1722 // Determine by looking at oldPos and m_caretPosition whether
1723 // we moved from the end of a line to the start of the next line, in which case
1724 // we want to adjust the caret position such that it is positioned at the
1725 // start of the next line, rather than jumping past the first character of the
1727 if (noPositions
== 1 && !extendSel
)
1728 MoveCaretForward(oldPos
);
1730 SetCaretPosition(newPos
);
1733 SetDefaultStyleToCursorStyle();
1742 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1746 if (m_caretPosition
> startPos
- noPositions
+ 1)
1748 long oldPos
= m_caretPosition
;
1749 long newPos
= m_caretPosition
- noPositions
;
1750 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1754 if (noPositions
== 1 && !extendSel
)
1755 MoveCaretBack(oldPos
);
1757 SetCaretPosition(newPos
);
1760 SetDefaultStyleToCursorStyle();
1768 // Find the caret position for the combination of hit-test flags and character position.
1769 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
1770 // since this is ambiguous (same position used for end of line and start of next).
1771 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
1772 bool& caretLineStart
)
1774 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1775 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1776 // so we view the caret at the start of the line.
1777 caretLineStart
= false;
1778 long caretPosition
= position
;
1780 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
1782 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
1783 wxRichTextRange lineRange
;
1785 lineRange
= thisLine
->GetAbsoluteRange();
1787 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
1790 caretLineStart
= true;
1794 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
1795 if (para
&& para
->GetRange().GetStart() == position
)
1799 return caretPosition
;
1803 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1805 return MoveDown(- noLines
, flags
);
1809 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1814 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1815 wxPoint pt
= GetCaret()->GetPosition();
1816 long newLine
= lineNumber
+ noLines
;
1817 bool notInThisObject
= false;
1819 if (lineNumber
!= -1)
1823 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
1824 if (newLine
> lastLine
)
1825 notInThisObject
= true;
1830 notInThisObject
= true;
1834 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
1835 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
1837 if (notInThisObject
)
1839 // If we know we're navigating out of the current object,
1840 // try to find an object anywhere in the buffer at the new position (up or down a bit)
1841 container
= & GetBuffer();
1842 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
1844 if (noLines
> 0) // going down
1846 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
1850 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
1855 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
1857 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1863 wxClientDC
dc(this);
1865 dc
.SetFont(GetFont());
1867 wxRichTextObject
* hitObj
= NULL
;
1868 wxRichTextObject
* contextObj
= NULL
;
1869 int hitTest
= container
->HitTest(dc
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
1872 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
1873 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
1876 if (notInThisObject
)
1878 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
1879 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
1881 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
1883 container
= actualContainer
;
1887 bool caretLineStart
= true;
1888 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
1889 long newSelEnd
= caretPosition
;
1892 if (notInThisObject
)
1895 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1900 SetCaretPosition(caretPosition
, caretLineStart
);
1902 SetDefaultStyleToCursorStyle();
1910 /// Move to the end of the paragraph
1911 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1913 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1916 long newPos
= para
->GetRange().GetEnd() - 1;
1917 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1921 SetCaretPosition(newPos
);
1923 SetDefaultStyleToCursorStyle();
1931 /// Move to the start of the paragraph
1932 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1934 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1937 long newPos
= para
->GetRange().GetStart() - 1;
1938 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1942 SetCaretPosition(newPos
);
1944 SetDefaultStyleToCursorStyle();
1952 /// Move to the end of the line
1953 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1955 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1959 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1960 long newPos
= lineRange
.GetEnd();
1961 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1965 SetCaretPosition(newPos
);
1967 SetDefaultStyleToCursorStyle();
1975 /// Move to the start of the line
1976 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1978 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1981 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1982 long newPos
= lineRange
.GetStart()-1;
1984 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1988 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
1990 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1992 SetDefaultStyleToCursorStyle();
2000 /// Move to the start of the buffer
2001 bool wxRichTextCtrl::MoveHome(int flags
)
2003 if (m_caretPosition
!= -1)
2005 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2009 SetCaretPosition(-1);
2011 SetDefaultStyleToCursorStyle();
2019 /// Move to the end of the buffer
2020 bool wxRichTextCtrl::MoveEnd(int flags
)
2022 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2024 if (m_caretPosition
!= endPos
)
2026 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2030 SetCaretPosition(endPos
);
2032 SetDefaultStyleToCursorStyle();
2040 /// Move noPages pages up
2041 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2043 return PageDown(- noPages
, flags
);
2046 /// Move noPages pages down
2047 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2049 // Calculate which line occurs noPages * screen height further down.
2050 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2053 wxSize clientSize
= GetClientSize();
2054 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2056 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2059 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2060 long pos
= lineRange
.GetStart()-1;
2061 if (pos
!= m_caretPosition
)
2063 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2065 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2069 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2071 SetDefaultStyleToCursorStyle();
2081 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2083 return str
== wxT(" ") || str
== wxT("\t");
2086 // Finds the caret position for the next word
2087 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2089 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2093 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2095 // First skip current text to space
2096 while (i
< endPos
&& i
> -1)
2098 // i is in character, not caret positions
2099 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2100 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2101 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2105 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
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);
2117 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2118 return wxMax(-1, i
);
2120 if (text
.empty()) // End of paragraph, or maybe an image
2121 return wxMax(-1, i
- 1);
2122 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2126 // Convert to caret position
2127 return wxMax(-1, i
- 1);
2136 long i
= m_caretPosition
;
2138 // First skip white space
2139 while (i
< endPos
&& i
> -1)
2141 // i is in character, not caret positions
2142 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2143 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2145 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2147 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2152 // Next skip current text to space
2153 while (i
< endPos
&& i
> -1)
2155 // i is in character, not caret positions
2156 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2157 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2158 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2161 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2174 /// Move n words left
2175 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2177 long pos
= FindNextWordPosition(-1);
2178 if (pos
!= m_caretPosition
)
2180 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2182 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2186 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2188 SetDefaultStyleToCursorStyle();
2196 /// Move n words right
2197 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2199 long pos
= FindNextWordPosition(1);
2200 if (pos
!= m_caretPosition
)
2202 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2204 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2208 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2210 SetDefaultStyleToCursorStyle();
2219 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2221 // Only do sizing optimization for large buffers
2222 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2224 m_fullLayoutRequired
= true;
2225 m_fullLayoutTime
= wxGetLocalTimeMillis();
2226 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2227 LayoutContent(true /* onlyVisibleRect */);
2230 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2232 #if wxRICHTEXT_BUFFERED_PAINTING
2239 // Force any pending layout due to large buffer
2240 void wxRichTextCtrl::ForceDelayedLayout()
2242 if (m_fullLayoutRequired
)
2244 m_fullLayoutRequired
= false;
2245 m_fullLayoutTime
= 0;
2246 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2247 ShowPosition(m_fullLayoutSavedPosition
);
2253 /// Idle-time processing
2254 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2256 #if wxRICHTEXT_USE_OWN_CARET
2257 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2259 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2265 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2267 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2269 m_fullLayoutRequired
= false;
2270 m_fullLayoutTime
= 0;
2271 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2272 ShowPosition(m_fullLayoutSavedPosition
);
2276 if (m_caretPositionForDefaultStyle
!= -2)
2278 // If the caret position has changed, no longer reflect the default style
2280 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2281 m_caretPositionForDefaultStyle
= -2;
2288 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2290 #if wxRICHTEXT_USE_OWN_CARET
2291 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2294 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2301 /// Set up scrollbars, e.g. after a resize
2302 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2307 if (GetBuffer().IsEmpty())
2309 SetScrollbars(0, 0, 0, 0, 0, 0);
2313 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2314 // of pixels. See e.g. wxVScrolledWindow for ideas.
2315 int pixelsPerUnit
= 5;
2316 wxSize clientSize
= GetClientSize();
2318 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2320 // Round up so we have at least maxHeight pixels
2321 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2323 int startX
= 0, startY
= 0;
2325 GetViewStart(& startX
, & startY
);
2327 int maxPositionX
= 0;
2328 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2330 int newStartX
= wxMin(maxPositionX
, startX
);
2331 int newStartY
= wxMin(maxPositionY
, startY
);
2333 int oldPPUX
, oldPPUY
;
2334 int oldStartX
, oldStartY
;
2335 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2336 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2337 GetViewStart(& oldStartX
, & oldStartY
);
2338 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2340 oldVirtualSizeY
/= oldPPUY
;
2342 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2345 // Don't set scrollbars if there were none before, and there will be none now.
2346 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2349 // Move to previous scroll position if
2351 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2354 /// Paint the background
2355 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2357 wxColour backgroundColour
= GetBackgroundColour();
2358 if (!backgroundColour
.IsOk())
2359 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2361 // Clear the background
2362 dc
.SetBrush(wxBrush(backgroundColour
));
2363 dc
.SetPen(*wxTRANSPARENT_PEN
);
2364 wxRect
windowRect(GetClientSize());
2365 windowRect
.x
-= 2; windowRect
.y
-= 2;
2366 windowRect
.width
+= 4; windowRect
.height
+= 4;
2368 // We need to shift the rectangle to take into account
2369 // scrolling. Converting device to logical coordinates.
2370 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2371 dc
.DrawRectangle(windowRect
);
2374 #if wxRICHTEXT_BUFFERED_PAINTING
2375 /// Recreate buffer bitmap if necessary
2376 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2379 if (sz
== wxDefaultSize
)
2380 sz
= GetClientSize();
2382 if (sz
.x
< 1 || sz
.y
< 1)
2385 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2386 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2387 return m_bufferBitmap
.IsOk();
2391 // ----------------------------------------------------------------------------
2392 // file IO functions
2393 // ----------------------------------------------------------------------------
2395 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2397 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2399 m_filename
= filename
;
2402 SetInsertionPoint(0);
2405 SetupScrollbars(true);
2407 wxTextCtrl::SendTextUpdatedEvent(this);
2413 wxLogError(_("File couldn't be loaded."));
2419 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2421 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2423 m_filename
= filename
;
2430 wxLogError(_("The text couldn't be saved."));
2435 // ----------------------------------------------------------------------------
2436 // wxRichTextCtrl specific functionality
2437 // ----------------------------------------------------------------------------
2439 /// Add a new paragraph of text to the end of the buffer
2440 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2442 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2443 GetBuffer().Invalidate();
2449 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2451 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2452 GetBuffer().Invalidate();
2457 // ----------------------------------------------------------------------------
2458 // selection and ranges
2459 // ----------------------------------------------------------------------------
2461 void wxRichTextCtrl::SelectAll()
2463 SetSelection(-1, -1);
2467 void wxRichTextCtrl::SelectNone()
2469 if (m_selection
.IsValid())
2471 wxRichTextSelection oldSelection
= m_selection
;
2473 m_selection
.Reset();
2475 RefreshForSelectionChange(oldSelection
, m_selection
);
2477 m_selectionAnchor
= -2;
2478 m_selectionAnchorObject
= NULL
;
2479 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2482 static bool wxIsWordDelimiter(const wxString
& text
)
2484 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2487 /// Select the word at the given character position
2488 bool wxRichTextCtrl::SelectWord(long position
)
2490 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2493 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2497 if (position
== para
->GetRange().GetEnd())
2500 long positionStart
= position
;
2501 long positionEnd
= position
;
2503 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2505 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2506 if (wxIsWordDelimiter(text
))
2512 if (positionStart
< para
->GetRange().GetStart())
2513 positionStart
= para
->GetRange().GetStart();
2515 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2517 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2518 if (wxIsWordDelimiter(text
))
2524 if (positionEnd
>= para
->GetRange().GetEnd())
2525 positionEnd
= para
->GetRange().GetEnd();
2527 if (positionEnd
< positionStart
)
2530 SetSelection(positionStart
, positionEnd
+1);
2532 if (positionStart
>= 0)
2534 MoveCaret(positionStart
-1, true);
2535 SetDefaultStyleToCursorStyle();
2541 wxString
wxRichTextCtrl::GetStringSelection() const
2544 GetSelection(&from
, &to
);
2546 return GetRange(from
, to
);
2549 // ----------------------------------------------------------------------------
2551 // ----------------------------------------------------------------------------
2553 wxTextCtrlHitTestResult
2554 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2556 // implement in terms of the other overload as the native ports typically
2557 // can get the position and not (x, y) pair directly (although wxUniv
2558 // directly gets x and y -- and so overrides this method as well)
2560 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2562 if ( rc
!= wxTE_HT_UNKNOWN
)
2564 PositionToXY(pos
, x
, y
);
2570 wxTextCtrlHitTestResult
2571 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2574 wxClientDC
dc((wxRichTextCtrl
*) this);
2575 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2577 // Buffer uses logical position (relative to start of buffer)
2579 wxPoint pt2
= GetLogicalPoint(pt
);
2581 wxRichTextObject
* hitObj
= NULL
;
2582 wxRichTextObject
* contextObj
= NULL
;
2583 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2585 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2586 return wxTE_HT_BEFORE
;
2587 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2588 return wxTE_HT_BEYOND
;
2589 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2590 return wxTE_HT_ON_TEXT
;
2592 return wxTE_HT_UNKNOWN
;
2595 // ----------------------------------------------------------------------------
2596 // set/get the controls text
2597 // ----------------------------------------------------------------------------
2599 wxString
wxRichTextCtrl::DoGetValue() const
2601 return GetBuffer().GetText();
2604 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2606 // Public API for range is different from internals
2607 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2610 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2612 // Don't call Clear here, since it always sends a text updated event
2613 m_buffer
.ResetAndClearCommands();
2614 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2615 m_caretPosition
= -1;
2616 m_caretPositionForDefaultStyle
= -2;
2617 m_caretAtLineStart
= false;
2618 m_selection
.Reset();
2619 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2629 if (!value
.IsEmpty())
2631 // Remove empty paragraph
2632 GetBuffer().Clear();
2633 DoWriteText(value
, flags
);
2635 // for compatibility, don't move the cursor when doing SetValue()
2636 SetInsertionPoint(0);
2640 // still send an event for consistency
2641 if (flags
& SetValue_SendEvent
)
2642 wxTextCtrl::SendTextUpdatedEvent(this);
2647 void wxRichTextCtrl::WriteText(const wxString
& value
)
2652 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2654 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2656 GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2658 if ( flags
& SetValue_SendEvent
)
2659 wxTextCtrl::SendTextUpdatedEvent(this);
2662 void wxRichTextCtrl::AppendText(const wxString
& text
)
2664 SetInsertionPointEnd();
2669 /// Write an image at the current insertion point
2670 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2672 wxRichTextImageBlock imageBlock
;
2674 wxImage image2
= image
;
2675 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2676 return WriteImage(imageBlock
, textAttr
);
2681 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2683 wxRichTextImageBlock imageBlock
;
2686 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2687 return WriteImage(imageBlock
, textAttr
);
2692 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2694 return GetFocusObject()->InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this, & GetBuffer(), 0, textAttr
);
2697 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2701 wxRichTextImageBlock imageBlock
;
2703 wxImage image
= bitmap
.ConvertToImage();
2704 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2705 return WriteImage(imageBlock
, textAttr
);
2711 // Write a text box at the current insertion point.
2712 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2714 wxRichTextBox
* textBox
= new wxRichTextBox
;
2715 textBox
->SetAttributes(textAttr
);
2716 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2717 textBox
->AddParagraph(wxEmptyString
);
2718 textBox
->SetParent(NULL
);
2720 // The object returned is the one actually inserted into the buffer,
2721 // while the original one is deleted.
2722 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, textBox
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2723 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
2727 // Write a table at the current insertion point, returning the table.
2728 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
2730 wxASSERT(rows
> 0 && cols
> 0);
2732 if (rows
<= 0 || cols
<= 0)
2735 wxRichTextTable
* table
= new wxRichTextTable
;
2736 table
->SetAttributes(tableAttr
);
2737 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2739 table
->CreateTable(rows
, cols
);
2741 table
->SetParent(NULL
);
2744 for (j
= 0; j
< rows
; j
++)
2746 for (i
= 0; i
< cols
; i
++)
2748 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
2752 // The object returned is the one actually inserted into the buffer,
2753 // while the original one is deleted.
2754 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, table
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2755 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
2760 /// Insert a newline (actually paragraph) at the current insertion point.
2761 bool wxRichTextCtrl::Newline()
2763 return GetFocusObject()->InsertNewlineWithUndo(m_caretPosition
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2766 /// Insert a line break at the current insertion point.
2767 bool wxRichTextCtrl::LineBreak()
2770 text
= wxRichTextLineBreakChar
;
2771 return GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, text
, this, & GetBuffer());
2774 // ----------------------------------------------------------------------------
2775 // Clipboard operations
2776 // ----------------------------------------------------------------------------
2778 void wxRichTextCtrl::Copy()
2782 wxRichTextRange range
= GetInternalSelectionRange();
2783 GetBuffer().CopyToClipboard(range
);
2787 void wxRichTextCtrl::Cut()
2791 wxRichTextRange range
= GetInternalSelectionRange();
2792 GetBuffer().CopyToClipboard(range
);
2794 DeleteSelectedContent();
2800 void wxRichTextCtrl::Paste()
2804 BeginBatchUndo(_("Paste"));
2806 long newPos
= m_caretPosition
;
2807 DeleteSelectedContent(& newPos
);
2809 GetBuffer().PasteFromClipboard(newPos
);
2815 void wxRichTextCtrl::DeleteSelection()
2817 if (CanDeleteSelection())
2819 DeleteSelectedContent();
2823 bool wxRichTextCtrl::HasSelection() const
2825 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
2828 bool wxRichTextCtrl::HasUnfocusedSelection() const
2830 return m_selection
.IsValid();
2833 bool wxRichTextCtrl::CanCopy() const
2835 // Can copy if there's a selection
2836 return HasSelection();
2839 bool wxRichTextCtrl::CanCut() const
2841 return HasSelection() && IsEditable();
2844 bool wxRichTextCtrl::CanPaste() const
2846 if ( !IsEditable() )
2849 return GetBuffer().CanPasteFromClipboard();
2852 bool wxRichTextCtrl::CanDeleteSelection() const
2854 return HasSelection() && IsEditable();
2858 // ----------------------------------------------------------------------------
2860 // ----------------------------------------------------------------------------
2862 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
2864 if (m_contextMenu
&& m_contextMenu
!= menu
)
2865 delete m_contextMenu
;
2866 m_contextMenu
= menu
;
2869 void wxRichTextCtrl::SetEditable(bool editable
)
2871 m_editable
= editable
;
2874 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2878 m_caretPosition
= pos
- 1;
2882 SetDefaultStyleToCursorStyle();
2885 void wxRichTextCtrl::SetInsertionPointEnd()
2887 long pos
= GetLastPosition();
2888 SetInsertionPoint(pos
);
2891 long wxRichTextCtrl::GetInsertionPoint() const
2893 return m_caretPosition
+1;
2896 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2898 return GetFocusObject()->GetOwnRange().GetEnd();
2901 // If the return values from and to are the same, there is no
2903 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2905 if (m_selection
.IsValid())
2907 *from
= m_selection
.GetRange().GetStart();
2908 *to
= m_selection
.GetRange().GetEnd();
2918 bool wxRichTextCtrl::IsEditable() const
2923 // ----------------------------------------------------------------------------
2925 // ----------------------------------------------------------------------------
2927 void wxRichTextCtrl::SetSelection(long from
, long to
)
2929 // if from and to are both -1, it means (in wxWidgets) that all text should
2931 if ( (from
== -1) && (to
== -1) )
2934 to
= GetLastPosition()+1;
2943 wxRichTextSelection oldSelection
= m_selection
;
2945 m_selectionAnchor
= from
-1;
2946 m_selectionAnchorObject
= NULL
;
2947 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
2949 m_caretPosition
= wxMax(-1, to
-1);
2951 RefreshForSelectionChange(oldSelection
, m_selection
);
2956 // ----------------------------------------------------------------------------
2958 // ----------------------------------------------------------------------------
2960 void wxRichTextCtrl::Replace(long from
, long to
,
2961 const wxString
& value
)
2963 BeginBatchUndo(_("Replace"));
2965 SetSelection(from
, to
);
2967 wxRichTextAttr attr
= GetDefaultStyle();
2969 DeleteSelectedContent();
2971 SetDefaultStyle(attr
);
2973 DoWriteText(value
, SetValue_SelectionOnly
);
2978 void wxRichTextCtrl::Remove(long from
, long to
)
2982 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
2989 bool wxRichTextCtrl::IsModified() const
2991 return m_buffer
.IsModified();
2994 void wxRichTextCtrl::MarkDirty()
2996 m_buffer
.Modify(true);
2999 void wxRichTextCtrl::DiscardEdits()
3001 m_caretPositionForDefaultStyle
= -2;
3002 m_buffer
.Modify(false);
3003 m_buffer
.GetCommandProcessor()->ClearCommands();
3006 int wxRichTextCtrl::GetNumberOfLines() const
3008 return GetFocusObject()->GetParagraphCount();
3011 // ----------------------------------------------------------------------------
3012 // Positions <-> coords
3013 // ----------------------------------------------------------------------------
3015 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3017 return GetFocusObject()->XYToPosition(x
, y
);
3020 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3022 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3025 // ----------------------------------------------------------------------------
3027 // ----------------------------------------------------------------------------
3029 void wxRichTextCtrl::ShowPosition(long pos
)
3031 if (!IsPositionVisible(pos
))
3032 ScrollIntoView(pos
-1, WXK_DOWN
);
3035 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3037 return GetFocusObject()->GetParagraphLength(lineNo
);
3040 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3042 return GetFocusObject()->GetParagraphText(lineNo
);
3045 // ----------------------------------------------------------------------------
3047 // ----------------------------------------------------------------------------
3049 void wxRichTextCtrl::Undo()
3053 GetCommandProcessor()->Undo();
3057 void wxRichTextCtrl::Redo()
3061 GetCommandProcessor()->Redo();
3065 bool wxRichTextCtrl::CanUndo() const
3067 return GetCommandProcessor()->CanUndo() && IsEditable();
3070 bool wxRichTextCtrl::CanRedo() const
3072 return GetCommandProcessor()->CanRedo() && IsEditable();
3075 // ----------------------------------------------------------------------------
3076 // implementation details
3077 // ----------------------------------------------------------------------------
3079 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3081 SetValue(event
.GetString());
3082 GetEventHandler()->ProcessEvent(event
);
3085 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3087 // By default, load the first file into the text window.
3088 if (event
.GetNumberOfFiles() > 0)
3090 LoadFile(event
.GetFiles()[0]);
3094 wxSize
wxRichTextCtrl::DoGetBestSize() const
3096 return wxSize(10, 10);
3099 // ----------------------------------------------------------------------------
3100 // standard handlers for standard edit menu events
3101 // ----------------------------------------------------------------------------
3103 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3108 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3113 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3118 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3123 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3128 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3133 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3135 event
.Enable( CanCut() );
3138 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3140 event
.Enable( CanCopy() );
3143 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3145 event
.Enable( CanDeleteSelection() );
3148 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3150 event
.Enable( CanPaste() );
3153 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3155 event
.Enable( CanUndo() );
3156 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3159 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3161 event
.Enable( CanRedo() );
3162 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3165 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3167 if (GetLastPosition() > 0)
3171 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3173 event
.Enable(GetLastPosition() > 0);
3176 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3178 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3179 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3181 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3182 if (obj
&& obj
->CanEditProperties())
3183 obj
->EditProperties(this, & GetBuffer());
3185 m_contextMenuPropertiesInfo
.Clear();
3189 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3191 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3192 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3195 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3197 if (event
.GetEventObject() != this)
3203 wxClientDC
dc(this);
3205 dc
.SetFont(GetFont());
3208 wxPoint pt
= event
.GetPosition();
3209 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3210 wxRichTextObject
* hitObj
= NULL
;
3211 wxRichTextObject
* contextObj
= NULL
;
3212 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
3214 m_contextMenuPropertiesInfo
.Clear();
3216 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3218 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3219 if (hitObj
&& actualContainer
)
3221 if (actualContainer
->AcceptsFocus())
3223 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3224 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3227 m_contextMenuPropertiesInfo
.AddItems(actualContainer
, hitObj
);
3230 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3234 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3239 m_contextMenuPropertiesInfo
.AddMenuItems(m_contextMenu
);
3240 PopupMenu(m_contextMenu
);
3244 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3246 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3249 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3251 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3254 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3256 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3259 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3261 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3264 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
)
3266 GetFocusObject()->SetStyle(obj
, textAttr
);
3269 // extended style setting operation with flags including:
3270 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3271 // see richtextbuffer.h for more details.
3273 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3275 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3278 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3280 return GetBuffer().SetDefaultStyle(style
);
3283 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3285 wxRichTextAttr
attr1(style
);
3286 attr1
.GetTextBoxAttr().Reset();
3287 return GetBuffer().SetDefaultStyle(attr1
);
3290 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3292 return GetBuffer().GetDefaultStyle();
3295 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3297 wxRichTextAttr attr
;
3298 if (GetFocusObject()->GetStyle(position
, attr
))
3307 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3309 return GetFocusObject()->GetStyle(position
, style
);
3312 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3314 wxRichTextAttr attr
;
3315 if (container
->GetStyle(position
, attr
))
3324 // get the common set of styles for the range
3325 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3327 wxRichTextAttr attr
;
3328 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3337 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3339 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3342 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3344 return container
->GetStyleForRange(range
.ToInternal(), style
);
3347 /// Get the content (uncombined) attributes for this position.
3348 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3350 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3353 /// Get the content (uncombined) attributes for this position.
3354 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3356 return container
->GetUncombinedStyle(position
, style
);
3359 /// Set font, and also the buffer attributes
3360 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3362 wxControl::SetFont(font
);
3364 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3366 GetBuffer().SetBasicStyle(attr
);
3368 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3374 /// Transform logical to physical
3375 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3378 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3383 /// Transform physical to logical
3384 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3387 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3392 /// Position the caret
3393 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3398 //wxLogDebug(wxT("PositionCaret"));
3401 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3403 wxPoint newPt
= caretRect
.GetPosition();
3404 wxSize newSz
= caretRect
.GetSize();
3405 wxPoint pt
= GetPhysicalPoint(newPt
);
3406 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3409 if (GetCaret()->GetSize() != newSz
)
3410 GetCaret()->SetSize(newSz
);
3412 // Adjust size so the caret size and position doesn't appear in the margins
3413 if (((pt
.y
+ newSz
.y
) <= GetBuffer().GetTopMargin()) || (pt
.y
>= (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3418 else if (pt
.y
< GetBuffer().GetTopMargin() && (pt
.y
+ newSz
.y
) > GetBuffer().GetTopMargin())
3420 newSz
.y
-= (GetBuffer().GetTopMargin() - pt
.y
);
3423 pt
.y
= GetBuffer().GetTopMargin();
3424 GetCaret()->SetSize(newSz
);
3427 else if (pt
.y
< (GetClientSize().y
- GetBuffer().GetBottomMargin()) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- GetBuffer().GetBottomMargin()))
3429 newSz
.y
= GetClientSize().y
- GetBuffer().GetBottomMargin() - pt
.y
;
3430 GetCaret()->SetSize(newSz
);
3433 GetCaret()->Move(pt
);
3439 /// Get the caret height and position for the given character position
3440 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3442 wxClientDC
dc(this);
3443 dc
.SetFont(GetFont());
3451 container
= GetFocusObject();
3453 if (container
->FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
3455 // Caret height can't be zero
3457 height
= dc
.GetCharHeight();
3459 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3466 /// Gets the line for the visible caret position. If the caret is
3467 /// shown at the very end of the line, it means the next character is actually
3468 /// on the following line. So let's get the line we're expecting to find
3469 /// if this is the case.
3470 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3472 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3473 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3476 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3477 if (caretPosition
== lineRange
.GetStart()-1 &&
3478 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3480 if (!m_caretAtLineStart
)
3481 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3488 /// Move the caret to the given character position
3489 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3491 if (GetBuffer().IsDirty())
3495 container
= GetFocusObject();
3497 if (pos
<= container
->GetOwnRange().GetEnd())
3499 SetCaretPosition(pos
, showAtLineStart
);
3501 PositionCaret(container
);
3509 /// Layout the buffer: which we must do before certain operations, such as
3510 /// setting the caret position.
3511 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3513 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3515 wxRect
availableSpace(GetClientSize());
3516 if (availableSpace
.width
== 0)
3517 availableSpace
.width
= 10;
3518 if (availableSpace
.height
== 0)
3519 availableSpace
.height
= 10;
3521 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3522 if (onlyVisibleRect
)
3524 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3525 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3528 wxClientDC
dc(this);
3529 dc
.SetFont(GetFont());
3533 GetBuffer().Defragment();
3534 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3535 GetBuffer().Layout(dc
, availableSpace
, flags
);
3536 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3545 /// Is all of the selection, or the current caret position, bold?
3546 bool wxRichTextCtrl::IsSelectionBold()
3550 wxRichTextAttr attr
;
3551 wxRichTextRange range
= GetSelectionRange();
3552 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3553 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3555 return HasCharacterAttributes(range
, attr
);
3559 // If no selection, then we need to combine current style with default style
3560 // to see what the effect would be if we started typing.
3561 wxRichTextAttr attr
;
3562 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3564 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3565 if (GetStyle(pos
, attr
))
3567 if (IsDefaultStyleShowing())
3568 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3569 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3575 /// Is all of the selection, or the current caret position, italics?
3576 bool wxRichTextCtrl::IsSelectionItalics()
3580 wxRichTextRange range
= GetSelectionRange();
3581 wxRichTextAttr attr
;
3582 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3583 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3585 return HasCharacterAttributes(range
, attr
);
3589 // If no selection, then we need to combine current style with default style
3590 // to see what the effect would be if we started typing.
3591 wxRichTextAttr attr
;
3592 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3594 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3595 if (GetStyle(pos
, attr
))
3597 if (IsDefaultStyleShowing())
3598 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3599 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3605 /// Is all of the selection, or the current caret position, underlined?
3606 bool wxRichTextCtrl::IsSelectionUnderlined()
3610 wxRichTextRange range
= GetSelectionRange();
3611 wxRichTextAttr attr
;
3612 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3613 attr
.SetFontUnderlined(true);
3615 return HasCharacterAttributes(range
, attr
);
3619 // If no selection, then we need to combine current style with default style
3620 // to see what the effect would be if we started typing.
3621 wxRichTextAttr attr
;
3622 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3623 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3625 if (GetStyle(pos
, attr
))
3627 if (IsDefaultStyleShowing())
3628 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3629 return attr
.GetFontUnderlined();
3635 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3636 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3638 wxRichTextAttr attr
;
3639 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3640 attr
.SetTextEffectFlags(flag
);
3641 attr
.SetTextEffects(flag
);
3645 return HasCharacterAttributes(GetSelectionRange(), attr
);
3649 // If no selection, then we need to combine current style with default style
3650 // to see what the effect would be if we started typing.
3651 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3652 if (GetStyle(pos
, attr
))
3654 if (IsDefaultStyleShowing())
3655 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3656 return (attr
.GetTextEffectFlags() & flag
) != 0;
3662 /// Apply bold to the selection
3663 bool wxRichTextCtrl::ApplyBoldToSelection()
3665 wxRichTextAttr attr
;
3666 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3667 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3670 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3673 wxRichTextAttr current
= GetDefaultStyleEx();
3674 current
.Apply(attr
);
3675 SetAndShowDefaultStyle(current
);
3680 /// Apply italic to the selection
3681 bool wxRichTextCtrl::ApplyItalicToSelection()
3683 wxRichTextAttr attr
;
3684 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3685 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3688 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3691 wxRichTextAttr current
= GetDefaultStyleEx();
3692 current
.Apply(attr
);
3693 SetAndShowDefaultStyle(current
);
3698 /// Apply underline to the selection
3699 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3701 wxRichTextAttr attr
;
3702 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3703 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3706 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3709 wxRichTextAttr current
= GetDefaultStyleEx();
3710 current
.Apply(attr
);
3711 SetAndShowDefaultStyle(current
);
3716 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
3717 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
3719 wxRichTextAttr attr
;
3720 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3721 attr
.SetTextEffectFlags(flags
);
3722 if (!DoesSelectionHaveTextEffectFlag(flags
))
3723 attr
.SetTextEffects(flags
);
3725 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
3728 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3731 wxRichTextAttr current
= GetDefaultStyleEx();
3732 current
.Apply(attr
);
3733 SetAndShowDefaultStyle(current
);
3738 /// Is all of the selection aligned according to the specified flag?
3739 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3741 wxRichTextRange range
;
3743 range
= GetSelectionRange();
3745 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3747 wxRichTextAttr attr
;
3748 attr
.SetAlignment(alignment
);
3750 return HasParagraphAttributes(range
, attr
);
3753 /// Apply alignment to the selection
3754 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3756 wxRichTextAttr attr
;
3757 attr
.SetAlignment(alignment
);
3759 return SetStyle(GetSelectionRange(), attr
);
3762 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
3764 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3769 /// Apply a named style to the selection
3770 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3772 // Flags are defined within each definition, so only certain
3773 // attributes are applied.
3774 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3776 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3778 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3780 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3782 wxRichTextRange range
;
3785 range
= GetSelectionRange();
3788 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3789 range
= wxRichTextRange(pos
, pos
+1);
3792 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3795 bool isPara
= false;
3797 // Make sure the attr has the style name
3798 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3801 attr
.SetParagraphStyleName(def
->GetName());
3803 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3804 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3805 // to change its style independently.
3806 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3809 attr
.SetCharacterStyleName(def
->GetName());
3812 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3815 wxRichTextAttr current
= GetDefaultStyleEx();
3816 wxRichTextAttr
defaultStyle(attr
);
3819 // Don't apply extra character styles since they are already implied
3820 // in the paragraph style
3821 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
3823 current
.Apply(defaultStyle
);
3824 SetAndShowDefaultStyle(current
);
3826 // If it's a paragraph style, we want to apply the style to the
3827 // current paragraph even if we didn't select any text.
3830 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3831 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
3834 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
3841 /// Apply the style sheet to the buffer, for example if the styles have changed.
3842 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3845 styleSheet
= GetBuffer().GetStyleSheet();
3849 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3851 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3859 /// Sets the default style to the style under the cursor
3860 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3862 wxRichTextAttr attr
;
3863 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3865 // If at the start of a paragraph, use the next position.
3866 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3868 if (GetUncombinedStyle(pos
, attr
))
3870 SetDefaultStyle(attr
);
3877 /// Returns the first visible position in the current view
3878 long wxRichTextCtrl::GetFirstVisiblePosition() const
3880 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3882 return line
->GetAbsoluteRange().GetStart();
3887 /// Get the first visible point in the window
3888 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3891 int startXUnits
, startYUnits
;
3893 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3894 GetViewStart(& startXUnits
, & startYUnits
);
3896 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3899 /// The adjusted caret position is the character position adjusted to take
3900 /// into account whether we're at the start of a paragraph, in which case
3901 /// style information should be taken from the next position, not current one.
3902 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3904 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
3906 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3911 /// Get/set the selection range in character positions. -1, -1 means no selection.
3912 /// The range is in API convention, i.e. a single character selection is denoted
3914 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3916 wxRichTextRange range
= GetInternalSelectionRange();
3917 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3918 range
.SetEnd(range
.GetEnd() + 1);
3922 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3924 SetSelection(range
.GetStart(), range
.GetEnd());
3928 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3930 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3933 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3935 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3938 /// Clear list for given range
3939 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3941 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
3944 /// Number/renumber any list elements in the given range
3945 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3947 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3950 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3952 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3955 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3956 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3958 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3961 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3963 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3966 /// Deletes the content in the given range
3967 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3969 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
3972 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3974 if (sm_availableFontNames
.GetCount() == 0)
3976 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3977 sm_availableFontNames
.Sort();
3979 return sm_availableFontNames
;
3982 void wxRichTextCtrl::ClearAvailableFontNames()
3984 sm_availableFontNames
.Clear();
3987 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3989 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3991 wxTextAttrEx basicStyle
= GetBasicStyle();
3992 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3993 SetBasicStyle(basicStyle
);
3994 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3999 // Refresh the area affected by a selection change
4000 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4002 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4003 // the selection contains whole containers rather than just text, so refresh everything
4004 // for now as it would be hard to compute the rectangle bounding all selections.
4005 // TODO: improve on this.
4006 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4007 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4013 wxRichTextRange oldRange
, newRange
;
4014 if (oldSelection
.IsValid())
4015 oldRange
= oldSelection
.GetRange();
4017 oldRange
= wxRICHTEXT_NO_SELECTION
;
4018 if (newSelection
.IsValid())
4019 newRange
= newSelection
.GetRange();
4021 newRange
= wxRICHTEXT_NO_SELECTION
;
4023 // Calculate the refresh rectangle - just the affected lines
4024 long firstPos
, lastPos
;
4025 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4027 firstPos
= newRange
.GetStart();
4028 lastPos
= newRange
.GetEnd();
4030 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4032 firstPos
= oldRange
.GetStart();
4033 lastPos
= oldRange
.GetEnd();
4035 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4041 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4042 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4045 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4046 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4048 if (firstLine
&& lastLine
)
4050 wxSize clientSize
= GetClientSize();
4051 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
4052 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
4055 pt1
.y
= wxMax(0, pt1
.y
);
4057 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4059 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4060 RefreshRect(rect
, false);
4068 // margins functions
4069 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4071 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4072 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4073 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4074 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4079 wxPoint
wxRichTextCtrl::DoGetMargins() const
4081 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4082 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4085 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4087 if (obj
&& !obj
->AcceptsFocus())
4090 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4091 bool changingContainer
= (m_focusObject
!= obj
);
4093 m_focusObject
= obj
;
4096 m_focusObject
= & m_buffer
;
4098 if (setCaretPosition
&& changingContainer
)
4100 m_selection
.Reset();
4101 m_selectionAnchor
= -2;
4102 m_selectionAnchorObject
= NULL
;
4103 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4107 m_caretAtLineStart
= false;
4108 MoveCaret(pos
, m_caretAtLineStart
);
4109 SetDefaultStyleToCursorStyle();
4111 wxRichTextEvent
cmdEvent(
4112 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4114 cmdEvent
.SetEventObject(this);
4115 cmdEvent
.SetPosition(m_caretPosition
+1);
4116 cmdEvent
.SetOldContainer(oldContainer
);
4117 cmdEvent
.SetContainer(m_focusObject
);
4119 GetEventHandler()->ProcessEvent(cmdEvent
);
4124 #if wxRICHTEXT_USE_OWN_CARET
4126 // ----------------------------------------------------------------------------
4127 // initialization and destruction
4128 // ----------------------------------------------------------------------------
4130 void wxRichTextCaret::Init()
4136 m_richTextCtrl
= NULL
;
4137 m_needsUpdate
= false;
4141 wxRichTextCaret::~wxRichTextCaret()
4143 if (m_timer
.IsRunning())
4147 // ----------------------------------------------------------------------------
4148 // showing/hiding/moving the caret (base class interface)
4149 // ----------------------------------------------------------------------------
4151 void wxRichTextCaret::DoShow()
4155 if (!m_timer
.IsRunning())
4156 m_timer
.Start(GetBlinkTime());
4161 void wxRichTextCaret::DoHide()
4163 if (m_timer
.IsRunning())
4169 void wxRichTextCaret::DoMove()
4175 if (m_xOld
!= -1 && m_yOld
!= -1)
4179 wxRect
rect(GetPosition(), GetSize());
4180 m_richTextCtrl
->RefreshRect(rect
, false);
4189 void wxRichTextCaret::DoSize()
4191 int countVisible
= m_countVisible
;
4192 if (countVisible
> 0)
4198 if (countVisible
> 0)
4200 m_countVisible
= countVisible
;
4205 // ----------------------------------------------------------------------------
4206 // handling the focus
4207 // ----------------------------------------------------------------------------
4209 void wxRichTextCaret::OnSetFocus()
4217 void wxRichTextCaret::OnKillFocus()
4222 // ----------------------------------------------------------------------------
4223 // drawing the caret
4224 // ----------------------------------------------------------------------------
4226 void wxRichTextCaret::Refresh()
4230 wxRect
rect(GetPosition(), GetSize());
4231 m_richTextCtrl
->RefreshRect(rect
, false);
4235 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4237 dc
->SetPen( *wxBLACK_PEN
);
4239 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4240 dc
->SetPen(*wxBLACK_PEN
);
4242 wxPoint
pt(m_x
, m_y
);
4246 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4248 if (IsVisible() && m_flashOn
)
4249 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4252 void wxRichTextCaret::Notify()
4254 m_flashOn
= !m_flashOn
;
4258 void wxRichTextCaretTimer::Notify()
4263 // wxRICHTEXT_USE_OWN_CARET
4266 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4270 m_labels
.Add(label
);
4278 // Returns number of menu items were added.
4279 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4281 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4282 // If none of the standard properties identifiers are in the menu, assume it's
4283 // a custom menu without properties commands, and don't add them.
4286 // If no items, to add just set the text to something generic
4287 if (GetCount() == 0)
4289 menu
->SetLabel(startCmd
, _("&Properties"));
4291 // Delete the others if necessary
4293 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4295 if (menu
->FindItem(i
))
4305 // Find the position of the first properties item
4306 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4308 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4309 if (item
&& item
->GetId() == startCmd
)
4318 int insertBefore
= pos
+1;
4319 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4321 if (menu
->FindItem(i
))
4323 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4327 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4328 menu
->Append(i
, m_labels
[i
- startCmd
]);
4330 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4335 // Delete any old items still left on the menu
4336 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4338 if (menu
->FindItem(i
))
4350 // Add appropriate menu items for the current container and clicked on object
4351 // (and container's parent, if appropriate).
4352 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextObject
* container
, wxRichTextObject
* obj
)
4355 if (obj
&& obj
->CanEditProperties())
4356 AddItem(obj
->GetPropertiesMenuLabel(), obj
);
4358 if (container
&& container
!= obj
&& container
->CanEditProperties() && m_labels
.Index(container
->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4359 AddItem(container
->GetPropertiesMenuLabel(), container
);
4361 if (container
&& container
->GetParent() && container
->GetParent()->CanEditProperties() && m_labels
.Index(container
->GetParent()->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4362 AddItem(container
->GetParent()->GetPropertiesMenuLabel(), container
->GetParent());