]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextctrl.cpp
Implement wxDataViewModel::IsEnabled() support in generic implementation.
[wxWidgets.git] / src / richtext / richtextctrl.cpp
CommitLineData
5d7836c4 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/richtext/richtextctrl.cpp
5d7836c4
JS
3// Purpose: A rich edit control
4// Author: Julian Smart
7fe8059f 5// Modified by:
5d7836c4 6// Created: 2005-09-30
7fe8059f 7// RCS-ID: $Id$
5d7836c4
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
9eddec69 16 #pragma hdrstop
5d7836c4
JS
17#endif
18
b01ca8b6
JS
19#if wxUSE_RICHTEXT
20
21#include "wx/richtext/richtextctrl.h"
ab14c7aa 22#include "wx/richtext/richtextstyles.h"
b01ca8b6 23
5d7836c4 24#ifndef WX_PRECOMP
fe5aa22c 25 #include "wx/wx.h"
9eddec69 26 #include "wx/settings.h"
5d7836c4
JS
27#endif
28
a188ac29 29#include "wx/timer.h"
5d7836c4
JS
30#include "wx/textfile.h"
31#include "wx/ffile.h"
5d7836c4
JS
32#include "wx/filename.h"
33#include "wx/dcbuffer.h"
5d7836c4 34#include "wx/arrimpl.cpp"
dadd4f55 35#include "wx/fontenum.h"
addcf76c 36#include "wx/accel.h"
5d7836c4 37
5dec941a
JS
38#if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
39#define wxHAVE_PRIMARY_SELECTION 1
40#else
41#define wxHAVE_PRIMARY_SELECTION 0
42#endif
43
44#if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION
45#include "wx/clipbrd.h"
46#endif
47
27e20452
JS
48// DLL options compatibility check:
49#include "wx/app.h"
50WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
51
9b11752c
VZ
52wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, wxRichTextEvent );
53wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, wxRichTextEvent );
54wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, wxRichTextEvent );
55wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, wxRichTextEvent );
56wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN, wxRichTextEvent );
57wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER, wxRichTextEvent );
58wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE, wxRichTextEvent );
5912d19e 59
9b11752c
VZ
60wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, wxRichTextEvent );
61wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED, wxRichTextEvent );
62wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING, wxRichTextEvent );
63wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED, wxRichTextEvent );
5d7836c4 64
9b11752c
VZ
65wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, wxRichTextEvent );
66wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, wxRichTextEvent );
67wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, wxRichTextEvent );
68wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED, wxRichTextEvent );
69wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, wxRichTextEvent );
603f702b 70wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED, wxRichTextEvent );
5912d19e 71
1c13f06e
JS
72#if wxRICHTEXT_USE_OWN_CARET
73
74/*!
75 * wxRichTextCaret
76 *
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.
80 */
81
a188ac29
JS
82class wxRichTextCaret;
83class wxRichTextCaretTimer: public wxTimer
84{
85 public:
86 wxRichTextCaretTimer(wxRichTextCaret* caret)
87 {
88 m_caret = caret;
89 }
90 virtual void Notify();
91 wxRichTextCaret* m_caret;
92};
93
1c13f06e
JS
94class wxRichTextCaret: public wxCaret
95{
96public:
97 // ctors
98 // -----
99 // default - use Create()
a188ac29 100 wxRichTextCaret(): m_timer(this) { Init(); }
1c13f06e
JS
101 // creates a block caret associated with the given window
102 wxRichTextCaret(wxRichTextCtrl *window, int width, int height)
a188ac29 103 : wxCaret(window, width, height), m_timer(this) { Init(); m_richTextCtrl = window; }
1c13f06e 104 wxRichTextCaret(wxRichTextCtrl *window, const wxSize& size)
a188ac29 105 : wxCaret(window, size), m_timer(this) { Init(); m_richTextCtrl = window; }
1c13f06e
JS
106
107 virtual ~wxRichTextCaret();
108
109 // implementation
110 // --------------
111
112 // called by wxWindow (not using the event tables)
113 virtual void OnSetFocus();
114 virtual void OnKillFocus();
115
116 // draw the caret on the given DC
117 void DoDraw(wxDC *dc);
118
119 // get the visible count
120 int GetVisibleCount() const { return m_countVisible; }
121
122 // delay repositioning
123 bool GetNeedsUpdate() const { return m_needsUpdate; }
124 void SetNeedsUpdate(bool needsUpdate = true ) { m_needsUpdate = needsUpdate; }
125
a188ac29
JS
126 void Notify();
127
1c13f06e
JS
128protected:
129 virtual void DoShow();
130 virtual void DoHide();
131 virtual void DoMove();
132 virtual void DoSize();
133
134 // refresh the caret
135 void Refresh();
136
137private:
138 void Init();
139
140 int m_xOld,
141 m_yOld;
142 bool m_hasFocus; // true => our window has focus
143 bool m_needsUpdate; // must be repositioned
a188ac29
JS
144 bool m_flashOn;
145 wxRichTextCaretTimer m_timer;
1c13f06e
JS
146 wxRichTextCtrl* m_richTextCtrl;
147};
148#endif
149
24777478 150IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl, wxControl )
5d7836c4 151
d0960f6d 152IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent, wxNotifyEvent )
5d7836c4 153
24777478 154BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl )
5d7836c4
JS
155 EVT_PAINT(wxRichTextCtrl::OnPaint)
156 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
4d551ad5 157 EVT_IDLE(wxRichTextCtrl::OnIdle)
c59f6793 158 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
5d7836c4
JS
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)
c80e8cbe 166 EVT_KEY_DOWN(wxRichTextCtrl::OnChar)
5d7836c4
JS
167 EVT_SIZE(wxRichTextCtrl::OnSize)
168 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
169 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
61f84e24 170 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost)
5d7836c4 171 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
52170c5b 172 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged)
5d7836c4
JS
173
174 EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
175 EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
176
177 EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo)
178 EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo)
179
180 EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy)
181 EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy)
182
183 EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste)
184 EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste)
185
186 EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut)
187 EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut)
188
189 EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear)
190 EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear)
191
192 EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll)
193 EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll)
603f702b
JS
194
195 EVT_MENU(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnProperties)
196 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnUpdateProperties)
197
198 EVT_MENU(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnProperties)
199 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnUpdateProperties)
200
201 EVT_MENU(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnProperties)
202 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnUpdateProperties)
203
5d7836c4
JS
204END_EVENT_TABLE()
205
206/*!
207 * wxRichTextCtrl
208 */
209
dadd4f55
JS
210wxArrayString wxRichTextCtrl::sm_availableFontNames;
211
5d7836c4 212wxRichTextCtrl::wxRichTextCtrl()
343ae70d 213 : wxScrollHelper(this)
5d7836c4
JS
214{
215 Init();
216}
217
343ae70d
VZ
218wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent,
219 wxWindowID id,
220 const wxString& value,
221 const wxPoint& pos,
222 const wxSize& size,
4e8d9558
JS
223 long style,
224 const wxValidator& validator,
225 const wxString& name)
343ae70d 226 : wxScrollHelper(this)
5d7836c4
JS
227{
228 Init();
4e8d9558 229 Create(parent, id, value, pos, size, style, validator, name);
5d7836c4
JS
230}
231
232/// Creation
4e8d9558
JS
233bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style,
234 const wxValidator& validator, const wxString& name)
5d7836c4 235{
6d595f3d
JS
236 style |= wxVSCROLL;
237
24777478 238 if (!wxControl::Create(parent, id, pos, size,
0ec1179b
VZ
239 style|wxFULL_REPAINT_ON_RESIZE,
240 validator, name))
5d7836c4 241 return false;
5d7836c4 242
a1b806b9 243 if (!GetFont().IsOk())
5d7836c4
JS
244 {
245 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
246 }
7fe8059f 247
e9f10004
JS
248 // No physical scrolling, so we can preserve margins
249 EnableScrolling(false, false);
250
dadd4f55
JS
251 if (style & wxTE_READONLY)
252 SetEditable(false);
253
42688aea 254 // The base attributes must all have default values
24777478 255 wxRichTextAttr attributes;
5d7836c4 256 attributes.SetFont(GetFont());
79f955ec 257 attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
5d7836c4 258 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
3e3a754f
JS
259 attributes.SetLineSpacing(10);
260 attributes.SetParagraphSpacingAfter(10);
261 attributes.SetParagraphSpacingBefore(0);
4fe83b93 262
5d7836c4
JS
263 SetBasicStyle(attributes);
264
603f702b
JS
265 int margin = 5;
266 SetMargins(margin, margin);
267
fe5aa22c
JS
268 // The default attributes will be merged with base attributes, so
269 // can be empty to begin with
24777478 270 wxRichTextAttr defaultAttributes;
fe5aa22c
JS
271 SetDefaultStyle(defaultAttributes);
272
79f955ec 273 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
5d7836c4
JS
274 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
275
cd8ba0d9
JS
276 GetBuffer().Reset();
277 GetBuffer().SetRichTextCtrl(this);
278
1c13f06e
JS
279#if wxRICHTEXT_USE_OWN_CARET
280 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
281#else
cd8ba0d9 282 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
1c13f06e 283#endif
cd8ba0d9 284
7fe8059f 285 // Tell the sizers to use the given or best size
170acdc9 286 SetInitialSize(size);
7fe8059f 287
3e3a754f 288#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4
JS
289 // Create a buffer
290 RecreateBuffer(size);
3e3a754f 291#endif
5d7836c4 292
d2d0adc7
JS
293 m_textCursor = wxCursor(wxCURSOR_IBEAM);
294 m_urlCursor = wxCursor(wxCURSOR_HAND);
295
296 SetCursor(m_textCursor);
61399247 297
27e20452
JS
298 if (!value.IsEmpty())
299 SetValue(value);
7fe8059f 300
d2d0adc7
JS
301 GetBuffer().AddEventHandler(this);
302
addcf76c 303 // Accelerators
0951fe0a 304 wxAcceleratorEntry entries[6];
addcf76c 305
675ec665
SC
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);
addcf76c 312
0951fe0a 313 wxAcceleratorTable accel(6, entries);
addcf76c
JS
314 SetAcceleratorTable(accel);
315
8e43fc77
JS
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"));
cdaed652 326 m_contextMenu->AppendSeparator();
603f702b 327 m_contextMenu->Append(wxID_RICHTEXT_PROPERTIES1, _("&Properties"));
cdaed652 328
5d7836c4
JS
329 return true;
330}
331
332wxRichTextCtrl::~wxRichTextCtrl()
333{
603f702b 334 SetFocusObject(& GetBuffer(), false);
d2d0adc7 335 GetBuffer().RemoveEventHandler(this);
88bee7e6 336
5d7836c4
JS
337 delete m_contextMenu;
338}
339
340/// Member initialisation
341void wxRichTextCtrl::Init()
342{
5d7836c4
JS
343 m_contextMenu = NULL;
344 m_caret = NULL;
345 m_caretPosition = -1;
5d7836c4 346 m_selectionAnchor = -2;
603f702b
JS
347 m_selectionAnchorObject = NULL;
348 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4
JS
349 m_editable = true;
350 m_caretAtLineStart = false;
351 m_dragging = false;
4d551ad5
JS
352 m_fullLayoutRequired = false;
353 m_fullLayoutTime = 0;
354 m_fullLayoutSavedPosition = 0;
355 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
ab14c7aa 356 m_caretPositionForDefaultStyle = -2;
603f702b 357 m_focusObject = & m_buffer;
5d7836c4
JS
358}
359
17808a75 360void wxRichTextCtrl::DoThaw()
5d7836c4 361{
603f702b 362 if (GetBuffer().IsDirty())
17808a75
VZ
363 LayoutContent();
364 else
365 SetupScrollbars();
8251c171 366
6a8e61e0 367 wxWindow::DoThaw();
5d7836c4
JS
368}
369
370/// Clear all text
371void wxRichTextCtrl::Clear()
372{
603f702b
JS
373 if (GetFocusObject() == & GetBuffer())
374 {
375 m_buffer.ResetAndClearCommands();
376 m_buffer.Invalidate(wxRICHTEXT_ALL);
377 }
378 else
379 {
380 GetFocusObject()->Reset();
381 }
382
5d7836c4 383 m_caretPosition = -1;
ab14c7aa 384 m_caretPositionForDefaultStyle = -2;
5d7836c4 385 m_caretAtLineStart = false;
603f702b
JS
386 m_selection.Reset();
387 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4 388
495a248e
JS
389 Scroll(0,0);
390
17808a75 391 if (!IsFrozen())
5d7836c4 392 {
cecf8eee 393 LayoutContent();
76bcd815 394 Refresh(false);
5d7836c4 395 }
0ec1179b
VZ
396
397 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
398}
399
400/// Painting
401void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
402{
1c13f06e
JS
403#if !wxRICHTEXT_USE_OWN_CARET
404 if (GetCaret() && !IsFrozen())
d2142335 405 GetCaret()->Hide();
1c13f06e 406#endif
d2142335 407
5d7836c4 408 {
3e3a754f 409#if wxRICHTEXT_BUFFERED_PAINTING
c59f6793 410 wxBufferedPaintDC dc(this, m_bufferBitmap);
3e3a754f
JS
411#else
412 wxPaintDC dc(this);
413#endif
41e155b4 414
17808a75 415 if (IsFrozen())
c59f6793 416 return;
41e155b4 417
17808a75
VZ
418 PrepareDC(dc);
419
c59f6793 420 dc.SetFont(GetFont());
41e155b4 421
c59f6793
JS
422 // Paint the background
423 PaintBackground(dc);
41e155b4 424
ea160b2e
JS
425 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
426
427 wxRect drawingArea(GetUpdateRegion().GetBox());
428 drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition()));
429
c59f6793 430 wxRect availableSpace(GetClientSize());
603f702b 431 if (GetBuffer().IsDirty())
c59f6793
JS
432 {
433 GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
603f702b 434 GetBuffer().Invalidate(wxRICHTEXT_NONE);
c59f6793
JS
435 SetupScrollbars();
436 }
41e155b4 437
e9f10004
JS
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);
445
603f702b
JS
446 int flags = 0;
447 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES) == 0)
448 flags |= wxRICHTEXT_DRAW_GUIDELINES;
4fe83b93 449
603f702b 450 GetBuffer().Draw(dc, GetBuffer().GetOwnRange(), GetSelection(), drawingArea, 0 /* descent */, flags);
e9f10004
JS
451
452 dc.DestroyClippingRegion();
453
35bdbae5
JS
454 // Other user defined painting after everything else (i.e. all text) is painted
455 PaintAboveContent(dc);
456
1c13f06e
JS
457#if wxRICHTEXT_USE_OWN_CARET
458 if (GetCaret()->IsVisible())
459 {
460 ((wxRichTextCaret*) GetCaret())->DoDraw(& dc);
461 }
462#endif
5d7836c4 463 }
d2142335 464
1c13f06e
JS
465#if !wxRICHTEXT_USE_OWN_CARET
466 if (GetCaret())
d2142335 467 GetCaret()->Show();
7383cf9d 468 PositionCaret();
5219cf05
JS
469#else
470#if !defined(__WXMAC__)
471 // Causes caret dropouts on Mac
472 PositionCaret();
473#endif
1c13f06e 474#endif
5d7836c4
JS
475}
476
477// Empty implementation, to prevent flicker
478void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
479{
480}
481
482void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
483{
ea160b2e
JS
484 if (GetCaret())
485 {
1c13f06e 486#if !wxRICHTEXT_USE_OWN_CARET
ea160b2e 487 PositionCaret();
1c13f06e
JS
488#endif
489 GetCaret()->Show();
ea160b2e 490 }
c59f6793 491
1c13f06e
JS
492#if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
493 // Work around dropouts when control is focused
494 if (!IsFrozen())
495 {
496 Refresh(false);
497 }
498#endif
5d7836c4
JS
499}
500
501void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
502{
1c13f06e 503 if (GetCaret())
ea160b2e 504 GetCaret()->Hide();
c59f6793 505
1c13f06e
JS
506#if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
507 // Work around dropouts when control is focused
508 if (!IsFrozen())
509 {
510 Refresh(false);
511 }
512#endif
5d7836c4
JS
513}
514
61f84e24
JS
515void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
516{
88bee7e6 517 m_dragging = false;
61f84e24
JS
518}
519
603f702b
JS
520// Set up the caret for the given position and container, after a mouse click
521bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox* container, long position, int hitTestFlags, bool extendSelection)
522{
523 bool caretAtLineStart = false;
524
525 if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE)
526 {
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);
532
533 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
534 caretAtLineStart = true;
535 position --;
536 }
537
538 if (extendSelection && (m_caretPosition != position))
539 ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
540
541 MoveCaret(position, caretAtLineStart);
542 SetDefaultStyleToCursorStyle();
4fe83b93 543
603f702b
JS
544 return true;
545}
546
5d7836c4
JS
547/// Left-click
548void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
549{
550 SetFocus();
551
552 wxClientDC dc(this);
553 PrepareDC(dc);
554 dc.SetFont(GetFont());
7fe8059f 555
603f702b 556 // TODO: detect change of focus object
5d7836c4 557 long position = 0;
603f702b
JS
558 wxRichTextObject* hitObj = NULL;
559 wxRichTextObject* contextObj = NULL;
560 int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position, & hitObj, & contextObj);
5d7836c4 561
603f702b 562 if (hit != wxRICHTEXT_HITTEST_NONE && hitObj)
5d7836c4 563 {
603f702b
JS
564 wxRichTextParagraphLayoutBox* oldFocusObject = GetFocusObject();
565 wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
566 if (container && container != GetFocusObject() && container->AcceptsFocus())
567 {
568 SetFocusObject(container, false /* don't set caret position yet */);
569 }
4fe83b93 570
5d7836c4
JS
571 m_dragStart = event.GetLogicalPosition(dc);
572 m_dragging = true;
573 CaptureMouse();
574
f7222f4c
JS
575 long oldCaretPos = m_caretPosition;
576
603f702b 577 SetCaretPositionAfterClick(container, position, hit);
f7222f4c 578
603f702b
JS
579 // For now, don't handle shift-click when we're selecting multiple objects.
580 if (event.ShiftDown() && GetFocusObject() == oldFocusObject && m_selectionState == wxRichTextCtrlSelectionState_Normal)
77ea6c48 581 ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
f7222f4c
JS
582 else
583 SelectNone();
5d7836c4
JS
584 }
585
586 event.Skip();
587}
588
589/// Left-up
d2d0adc7 590void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event)
5d7836c4
JS
591{
592 if (m_dragging)
593 {
594 m_dragging = false;
595 if (GetCapture() == this)
596 ReleaseMouse();
d2d0adc7
JS
597
598 // See if we clicked on a URL
599 wxClientDC dc(this);
600 PrepareDC(dc);
601 dc.SetFont(GetFont());
602
603 long position = 0;
604 wxPoint logicalPt = event.GetLogicalPosition(dc);
603f702b
JS
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);
88bee7e6 609
62381daa 610 if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
d2d0adc7 611 {
db6634e0
JS
612 wxRichTextEvent cmdEvent(
613 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK,
614 GetId());
615 cmdEvent.SetEventObject(this);
603f702b
JS
616 cmdEvent.SetPosition(position);
617 if (hitObj)
618 cmdEvent.SetContainer(hitObj->GetContainer());
db6634e0
JS
619
620 if (!GetEventHandler()->ProcessEvent(cmdEvent))
d2d0adc7 621 {
24777478 622 wxRichTextAttr attr;
db6634e0 623 if (GetStyle(position, attr))
d2d0adc7 624 {
db6634e0 625 if (attr.HasFlag(wxTEXT_ATTR_URL))
d2d0adc7 626 {
db6634e0
JS
627 wxString urlTarget = attr.GetURL();
628 if (!urlTarget.IsEmpty())
d2d0adc7 629 {
db6634e0 630 wxMouseEvent mouseEvent(event);
1a75935d 631
db6634e0 632 long startPos = 0, endPos = 0;
603f702b 633 wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(position);
db6634e0
JS
634 if (obj)
635 {
636 startPos = obj->GetRange().GetStart();
637 endPos = obj->GetRange().GetEnd();
638 }
1a75935d 639
db6634e0
JS
640 wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos);
641 InitCommandEvent(urlEvent);
1a75935d 642
db6634e0 643 urlEvent.SetString(urlTarget);
1a75935d 644
db6634e0 645 GetEventHandler()->ProcessEvent(urlEvent);
88bee7e6 646 }
d2d0adc7
JS
647 }
648 }
649 }
650 }
5d7836c4 651 }
5dec941a
JS
652
653#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
654 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
655 {
656 // Put the selection in PRIMARY, if it exists
657 wxTheClipboard->UsePrimarySelection(true);
658
659 wxRichTextRange range = GetInternalSelectionRange();
660 GetFocusObject()->GetBuffer()->CopyToClipboard(range);
661
662 wxTheClipboard->UsePrimarySelection(false);
663 }
664#endif
5d7836c4
JS
665}
666
667/// Left-click
668void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
669{
5d7836c4
JS
670 wxClientDC dc(this);
671 PrepareDC(dc);
672 dc.SetFont(GetFont());
7fe8059f 673
5d7836c4
JS
674 long position = 0;
675 wxPoint logicalPt = event.GetLogicalPosition(dc);
603f702b
JS
676 wxRichTextObject* hitObj = NULL;
677 wxRichTextObject* contextObj = NULL;
4fe83b93 678
603f702b 679 int flags = 0;
4fe83b93 680
603f702b
JS
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();
684 if (m_dragging)
685 {
686 flags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS;
687 container = GetFocusObject();
688 }
689 int hit = container->HitTest(dc, logicalPt, position, & hitObj, & contextObj, flags);
4fe83b93 690
d2d0adc7 691 // See if we need to change the cursor
88bee7e6 692
d2d0adc7 693 {
603f702b 694 if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE) && hitObj)
d2d0adc7 695 {
603f702b 696 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
24777478 697 wxRichTextAttr attr;
603f702b 698 if (actualContainer && GetStyle(position, attr, actualContainer))
d2d0adc7
JS
699 {
700 if (attr.HasFlag(wxTEXT_ATTR_URL))
701 {
e4ef49c7 702 SetCursor(m_urlCursor);
d2d0adc7
JS
703 }
704 else if (!attr.HasFlag(wxTEXT_ATTR_URL))
705 {
e4ef49c7 706 SetCursor(m_textCursor);
d2d0adc7
JS
707 }
708 }
709 }
f262b25c
JS
710 else
711 SetCursor(m_textCursor);
d2d0adc7
JS
712 }
713
714 if (!event.Dragging())
715 {
716 event.Skip();
717 return;
718 }
4fe83b93 719
603f702b 720 if (m_dragging)
5d7836c4 721 {
603f702b
JS
722 wxRichTextParagraphLayoutBox* commonAncestor = NULL;
723 wxRichTextParagraphLayoutBox* otherContainer = NULL;
724 wxRichTextParagraphLayoutBox* firstContainer = NULL;
725
726 // Check for dragging across multiple containers
727 long position2 = 0;
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)
731 {
732 // See if we can find a common ancestor
733 if (m_selectionState == wxRichTextCtrlSelectionState_Normal)
734 {
735 firstContainer = GetFocusObject();
736 commonAncestor = wxDynamicCast(firstContainer->GetParent(), wxRichTextParagraphLayoutBox);
737 }
738 else
739 {
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);
744 }
4fe83b93 745
603f702b
JS
746 if (commonAncestor && commonAncestor->HandlesChildSelections())
747 {
748 wxRichTextObject* p = hitObj2;
749 while (p)
750 {
751 if (p->GetParent() == commonAncestor)
752 {
753 otherContainer = wxDynamicCast(p, wxRichTextParagraphLayoutBox);
754 break;
755 }
756 p = p->GetParent();
757 }
758 }
5d7836c4 759
603f702b
JS
760 if (commonAncestor && firstContainer && otherContainer)
761 {
762 // We have now got a second container that shares a parent with the current or anchor object.
763 if (m_selectionState == wxRichTextCtrlSelectionState_Normal)
764 {
765 // Don't go into common-ancestor selection mode if we still have the same
766 // container.
767 if (otherContainer != firstContainer)
768 {
769 m_selectionState = wxRichTextCtrlSelectionState_CommonAncestor;
770 m_selectionAnchorObject = firstContainer;
771 m_selectionAnchor = firstContainer->GetRange().GetStart();
5d7836c4 772
603f702b
JS
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());
776 }
777 }
778 else
779 {
780 m_selection = commonAncestor->GetSelection(m_selectionAnchor, otherContainer->GetRange().GetStart());
781 }
5d7836c4 782
603f702b 783 Refresh();
7fe8059f 784
603f702b
JS
785 if (otherContainer->AcceptsFocus())
786 SetFocusObject(otherContainer, false /* don't set caret and clear selection */);
4fe83b93 787 MoveCaret(-1, false);
603f702b
JS
788 SetDefaultStyleToCursorStyle();
789 }
5d7836c4
JS
790 }
791 }
603f702b
JS
792
793 if (hitObj && m_dragging && hit != wxRICHTEXT_HITTEST_NONE && m_selectionState == wxRichTextCtrlSelectionState_Normal)
794 {
795 // TODO: test closeness
796 SetCaretPositionAfterClick(container, position, hit, true /* extend selection */);
797 }
5d7836c4
JS
798}
799
800/// Right-click
52170c5b 801void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
5d7836c4
JS
802{
803 SetFocus();
4fe83b93 804
603f702b
JS
805 wxClientDC dc(this);
806 PrepareDC(dc);
807 dc.SetFont(GetFont());
808
809 long position = 0;
810 wxPoint logicalPt = event.GetLogicalPosition(dc);
811 wxRichTextObject* hitObj = NULL;
812 wxRichTextObject* contextObj = NULL;
813 int hit = GetFocusObject()->HitTest(dc, logicalPt, position, & hitObj, & contextObj);
814
815 if (hitObj && hitObj->GetContainer() != GetFocusObject())
816 {
817 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
818 if (actualContainer && actualContainer->AcceptsFocus())
819 {
820 SetFocusObject(actualContainer, false /* don't set caret position yet */);
821 SetCaretPositionAfterClick(actualContainer, position, hit);
822 }
823 }
db6634e0
JS
824
825 wxRichTextEvent cmdEvent(
826 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK,
827 GetId());
828 cmdEvent.SetEventObject(this);
603f702b
JS
829 cmdEvent.SetPosition(position);
830 if (hitObj)
831 cmdEvent.SetContainer(hitObj->GetContainer());
1a75935d 832
52170c5b
JS
833 if (!GetEventHandler()->ProcessEvent(cmdEvent))
834 event.Skip();
5d7836c4
JS
835}
836
837/// Left-double-click
db6634e0 838void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event))
5d7836c4 839{
db6634e0
JS
840 wxRichTextEvent cmdEvent(
841 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK,
842 GetId());
843 cmdEvent.SetEventObject(this);
844 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 845 cmdEvent.SetContainer(GetFocusObject());
1a75935d 846
db6634e0
JS
847 if (!GetEventHandler()->ProcessEvent(cmdEvent))
848 {
849 SelectWord(GetCaretPosition()+1);
850 }
5d7836c4
JS
851}
852
853/// Middle-click
854void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
855{
db6634e0
JS
856 wxRichTextEvent cmdEvent(
857 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK,
858 GetId());
859 cmdEvent.SetEventObject(this);
860 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 861 cmdEvent.SetContainer(GetFocusObject());
1a75935d 862
db6634e0
JS
863 if (!GetEventHandler()->ProcessEvent(cmdEvent))
864 event.Skip();
5dec941a
JS
865
866#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
867 // Paste any PRIMARY selection, if it exists
868 wxTheClipboard->UsePrimarySelection(true);
869 Paste();
870 wxTheClipboard->UsePrimarySelection(false);
871#endif
5d7836c4
JS
872}
873
874/// Key press
875void wxRichTextCtrl::OnChar(wxKeyEvent& event)
876{
877 int flags = 0;
3e3a754f 878 if (event.CmdDown())
5d7836c4
JS
879 flags |= wxRICHTEXT_CTRL_DOWN;
880 if (event.ShiftDown())
881 flags |= wxRICHTEXT_SHIFT_DOWN;
882 if (event.AltDown())
883 flags |= wxRICHTEXT_ALT_DOWN;
884
c80e8cbe
JS
885 if (event.GetEventType() == wxEVT_KEY_DOWN)
886 {
7a34307e 887 if (event.IsKeyInCategory(WXK_CATEGORY_NAVIGATION))
8c9e72f9
JS
888 {
889 KeyboardNavigate(event.GetKeyCode(), flags);
890 return;
891 }
892
893 long keycode = event.GetKeyCode();
894 switch ( keycode )
895 {
896 case WXK_ESCAPE:
897 case WXK_START:
898 case WXK_LBUTTON:
899 case WXK_RBUTTON:
900 case WXK_CANCEL:
901 case WXK_MBUTTON:
902 case WXK_CLEAR:
903 case WXK_SHIFT:
904 case WXK_ALT:
905 case WXK_CONTROL:
906 case WXK_MENU:
907 case WXK_PAUSE:
908 case WXK_CAPITAL:
909 case WXK_END:
910 case WXK_HOME:
911 case WXK_LEFT:
912 case WXK_UP:
913 case WXK_RIGHT:
914 case WXK_DOWN:
915 case WXK_SELECT:
916 case WXK_PRINT:
917 case WXK_EXECUTE:
918 case WXK_SNAPSHOT:
919 case WXK_INSERT:
920 case WXK_HELP:
921 case WXK_F1:
922 case WXK_F2:
923 case WXK_F3:
924 case WXK_F4:
925 case WXK_F5:
926 case WXK_F6:
927 case WXK_F7:
928 case WXK_F8:
929 case WXK_F9:
930 case WXK_F10:
931 case WXK_F11:
932 case WXK_F12:
933 case WXK_F13:
934 case WXK_F14:
935 case WXK_F15:
936 case WXK_F16:
937 case WXK_F17:
938 case WXK_F18:
939 case WXK_F19:
940 case WXK_F20:
941 case WXK_F21:
942 case WXK_F22:
943 case WXK_F23:
944 case WXK_F24:
945 case WXK_NUMLOCK:
946 case WXK_SCROLL:
947 case WXK_PAGEUP:
948 case WXK_PAGEDOWN:
949 case WXK_NUMPAD_F1:
950 case WXK_NUMPAD_F2:
951 case WXK_NUMPAD_F3:
952 case WXK_NUMPAD_F4:
953 case WXK_NUMPAD_HOME:
954 case WXK_NUMPAD_LEFT:
955 case WXK_NUMPAD_UP:
956 case WXK_NUMPAD_RIGHT:
957 case WXK_NUMPAD_DOWN:
958 case WXK_NUMPAD_PAGEUP:
959 case WXK_NUMPAD_PAGEDOWN:
960 case WXK_NUMPAD_END:
961 case WXK_NUMPAD_BEGIN:
962 case WXK_NUMPAD_INSERT:
8c9e72f9
JS
963 case WXK_WINDOWS_LEFT:
964 {
965 return;
966 }
967 default:
968 {
969 }
970 }
971
c80e8cbe
JS
972 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
973 if (event.CmdDown() && event.GetKeyCode() == WXK_BACK)
974 {
975 BeginBatchUndo(_("Delete Text"));
976
977 long newPos = m_caretPosition;
978
29dbface 979 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe
JS
980
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.
983 if (newPos > -1)
984 {
c80e8cbe
JS
985 if (event.CmdDown())
986 {
987 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
8251c171 988 if (pos < newPos)
c80e8cbe 989 {
603f702b 990 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this, & GetBuffer());
c80e8cbe
JS
991 processed = true;
992 }
993 }
994
995 if (!processed)
603f702b 996 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this, & GetBuffer());
c80e8cbe
JS
997 }
998
999 EndBatchUndo();
1000
1001 if (GetLastPosition() == -1)
1002 {
603f702b 1003 GetFocusObject()->Reset();
c80e8cbe
JS
1004
1005 m_caretPosition = -1;
1006 PositionCaret();
1007 SetDefaultStyleToCursorStyle();
1008 }
1009
1010 ScrollIntoView(m_caretPosition, WXK_LEFT);
1011
1012 wxRichTextEvent cmdEvent(
1013 wxEVT_COMMAND_RICHTEXT_DELETE,
1014 GetId());
1015 cmdEvent.SetEventObject(this);
1016 cmdEvent.SetFlags(flags);
1017 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 1018 cmdEvent.SetContainer(GetFocusObject());
c80e8cbe
JS
1019 GetEventHandler()->ProcessEvent(cmdEvent);
1020
1021 Update();
1022 }
1023 else
1024 event.Skip();
1025
1026 return;
1027 }
1028
99257cbe
VZ
1029 // all the other keys modify the controls contents which shouldn't be
1030 // possible if we're read-only
1031 if ( !IsEditable() )
1032 {
1033 event.Skip();
1034 return;
1035 }
1036
1037 if (event.GetKeyCode() == WXK_RETURN)
5d7836c4
JS
1038 {
1039 BeginBatchUndo(_("Insert Text"));
1040
1041 long newPos = m_caretPosition;
1042
1043 DeleteSelectedContent(& newPos);
1044
ff76711f
JS
1045 if (event.ShiftDown())
1046 {
1047 wxString text;
1048 text = wxRichTextLineBreakChar;
603f702b 1049 GetFocusObject()->InsertTextWithUndo(newPos+1, text, this, & GetBuffer());
4bf9e5bc 1050 m_caretAtLineStart = true;
043c0d58 1051 PositionCaret();
ff76711f
JS
1052 }
1053 else
603f702b 1054 GetFocusObject()->InsertNewlineWithUndo(newPos+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE);
ff76711f 1055
d2d0adc7 1056 EndBatchUndo();
0a21bea0 1057 SetDefaultStyleToCursorStyle();
d2d0adc7
JS
1058
1059 ScrollIntoView(m_caretPosition, WXK_RIGHT);
5d7836c4
JS
1060
1061 wxRichTextEvent cmdEvent(
1062 wxEVT_COMMAND_RICHTEXT_RETURN,
1063 GetId());
1064 cmdEvent.SetEventObject(this);
1065 cmdEvent.SetFlags(flags);
5912d19e 1066 cmdEvent.SetPosition(newPos+1);
603f702b 1067 cmdEvent.SetContainer(GetFocusObject());
5912d19e 1068
d2d0adc7
JS
1069 if (!GetEventHandler()->ProcessEvent(cmdEvent))
1070 {
1071 // Generate conventional event
1072 wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId());
1073 InitCommandEvent(textEvent);
88bee7e6 1074
d2d0adc7
JS
1075 GetEventHandler()->ProcessEvent(textEvent);
1076 }
621ae374 1077 Update();
5d7836c4
JS
1078 }
1079 else if (event.GetKeyCode() == WXK_BACK)
1080 {
1081 BeginBatchUndo(_("Delete Text"));
88bee7e6 1082
c80e8cbe
JS
1083 long newPos = m_caretPosition;
1084
dc5de0dd 1085 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe 1086
5d7836c4
JS
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.
c80e8cbe 1089 if (newPos > -1)
5d7836c4 1090 {
62381daa
JS
1091 if (event.CmdDown())
1092 {
1093 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
8251c171 1094 if (pos < newPos)
62381daa 1095 {
603f702b 1096 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this, & GetBuffer());
62381daa
JS
1097 processed = true;
1098 }
1099 }
1100
1101 if (!processed)
603f702b 1102 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this, & GetBuffer());
5d7836c4 1103 }
5d7836c4
JS
1104
1105 EndBatchUndo();
1106
5d7836c4
JS
1107 if (GetLastPosition() == -1)
1108 {
603f702b 1109 GetFocusObject()->Reset();
5d7836c4
JS
1110
1111 m_caretPosition = -1;
1112 PositionCaret();
0a21bea0 1113 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1114 }
1115
f9ed2d16 1116 ScrollIntoView(m_caretPosition, WXK_LEFT);
5912d19e
JS
1117
1118 wxRichTextEvent cmdEvent(
1119 wxEVT_COMMAND_RICHTEXT_DELETE,
1120 GetId());
1121 cmdEvent.SetEventObject(this);
1122 cmdEvent.SetFlags(flags);
1123 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 1124 cmdEvent.SetContainer(GetFocusObject());
5912d19e
JS
1125 GetEventHandler()->ProcessEvent(cmdEvent);
1126
621ae374 1127 Update();
5d7836c4
JS
1128 }
1129 else if (event.GetKeyCode() == WXK_DELETE)
1130 {
1131 BeginBatchUndo(_("Delete Text"));
1132
c80e8cbe
JS
1133 long newPos = m_caretPosition;
1134
dc5de0dd 1135 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe 1136
5d7836c4 1137 // Submit range in character positions, which are greater than caret positions,
603f702b 1138 if (newPos < GetFocusObject()->GetOwnRange().GetEnd()+1)
5d7836c4 1139 {
c80e8cbe
JS
1140 if (event.CmdDown())
1141 {
1142 long pos = wxRichTextCtrl::FindNextWordPosition(1);
1143 if (pos != -1 && (pos > newPos))
1144 {
603f702b 1145 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos+1, pos), this, & GetBuffer());
c80e8cbe
JS
1146 processed = true;
1147 }
1148 }
1149
fc6d9d0d 1150 if (!processed && newPos < (GetLastPosition()-1))
603f702b 1151 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos+1, newPos+1), this, & GetBuffer());
5d7836c4 1152 }
5d7836c4
JS
1153
1154 EndBatchUndo();
1155
5d7836c4
JS
1156 if (GetLastPosition() == -1)
1157 {
603f702b 1158 GetFocusObject()->Reset();
5d7836c4
JS
1159
1160 m_caretPosition = -1;
1161 PositionCaret();
0a21bea0 1162 SetDefaultStyleToCursorStyle();
5d7836c4 1163 }
5912d19e 1164
575be976
JS
1165 ScrollIntoView(m_caretPosition, WXK_LEFT);
1166
5912d19e
JS
1167 wxRichTextEvent cmdEvent(
1168 wxEVT_COMMAND_RICHTEXT_DELETE,
1169 GetId());
1170 cmdEvent.SetEventObject(this);
1171 cmdEvent.SetFlags(flags);
1172 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 1173 cmdEvent.SetContainer(GetFocusObject());
5912d19e
JS
1174 GetEventHandler()->ProcessEvent(cmdEvent);
1175
621ae374 1176 Update();
5d7836c4
JS
1177 }
1178 else
1179 {
3e3a754f
JS
1180 long keycode = event.GetKeyCode();
1181 switch ( keycode )
1182 {
1183 case WXK_ESCAPE:
3e3a754f
JS
1184 {
1185 event.Skip();
1186 return;
1187 }
1807a1f3 1188
3e3a754f
JS
1189 default:
1190 {
cfceb4a8 1191#ifdef __WXMAC__
91babb9d 1192 if (event.CmdDown())
cfceb4a8 1193#else
d2bb72ff
JS
1194 // Fixes AltGr+key with European input languages on Windows
1195 if ((event.CmdDown() && !event.AltDown()) || (event.AltDown() && !event.CmdDown()))
cfceb4a8 1196#endif
3e3a754f
JS
1197 {
1198 event.Skip();
1199 return;
1200 }
41a85215 1201
5912d19e
JS
1202 wxRichTextEvent cmdEvent(
1203 wxEVT_COMMAND_RICHTEXT_CHARACTER,
1204 GetId());
1205 cmdEvent.SetEventObject(this);
1206 cmdEvent.SetFlags(flags);
0b14a7bd
JS
1207#if wxUSE_UNICODE
1208 cmdEvent.SetCharacter(event.GetUnicodeKey());
1209#else
5912d19e 1210 cmdEvent.SetCharacter((wxChar) keycode);
0b14a7bd 1211#endif
5912d19e 1212 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 1213 cmdEvent.SetContainer(GetFocusObject());
88bee7e6 1214
dadd4f55
JS
1215 if (keycode == wxT('\t'))
1216 {
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());
603f702b 1220 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos);
dadd4f55
JS
1221 if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName())
1222 {
1223 wxRichTextRange range;
1224 if (HasSelection())
1225 range = GetSelectionRange();
1226 else
1227 range = para->GetRange().FromInternal();
41a85215 1228
dadd4f55
JS
1229 int promoteBy = event.ShiftDown() ? 1 : -1;
1230
1231 PromoteList(promoteBy, range, NULL);
d2d0adc7 1232
5912d19e
JS
1233 GetEventHandler()->ProcessEvent(cmdEvent);
1234
dadd4f55
JS
1235 return;
1236 }
1237 }
5d7836c4 1238
3e3a754f 1239 BeginBatchUndo(_("Insert Text"));
5d7836c4 1240
3e3a754f
JS
1241 long newPos = m_caretPosition;
1242 DeleteSelectedContent(& newPos);
5d7836c4 1243
0b14a7bd
JS
1244#if wxUSE_UNICODE
1245 wxString str = event.GetUnicodeKey();
1246#else
3e3a754f 1247 wxString str = (wxChar) event.GetKeyCode();
0b14a7bd 1248#endif
603f702b 1249 GetFocusObject()->InsertTextWithUndo(newPos+1, str, this, & GetBuffer(), 0);
5d7836c4 1250
3e3a754f
JS
1251 EndBatchUndo();
1252
0a21bea0 1253 SetDefaultStyleToCursorStyle();
3e3a754f 1254 ScrollIntoView(m_caretPosition, WXK_RIGHT);
88bee7e6 1255
c1dd924d 1256 cmdEvent.SetPosition(m_caretPosition);
5912d19e
JS
1257 GetEventHandler()->ProcessEvent(cmdEvent);
1258
621ae374 1259 Update();
3e3a754f
JS
1260 }
1261 }
5d7836c4 1262 }
5d7836c4
JS
1263}
1264
1265/// Delete content if there is a selection, e.g. when pressing a key.
1266bool wxRichTextCtrl::DeleteSelectedContent(long* newPos)
1267{
1268 if (HasSelection())
1269 {
603f702b
JS
1270 long pos = m_selection.GetRange().GetStart();
1271 wxRichTextRange range = m_selection.GetRange();
205a1ad9
JS
1272
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);
1278
603f702b
JS
1279 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1280 m_selection.Reset();
1281 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4
JS
1282
1283 if (newPos)
1284 *newPos = pos-1;
1285 return true;
1286 }
1287 else
1288 return false;
1289}
1290
1291/// Keyboard navigation
1292
1293/*
1294
1295Left: left one character
1296Right: right one character
1297Up: up one line
1298Down: down one line
1299Ctrl-Left: left one word
1300Ctrl-Right: right one word
1301Ctrl-Up: previous paragraph start
1302Ctrl-Down: next start of paragraph
1303Home: start of line
1304End: end of line
1305Ctrl-Home: start of document
1306Ctrl-End: end of document
1307Page-Up: Up a screen
1308Page-Down: Down a screen
1309
1310Maybe:
1311
1312Ctrl-Alt-PgUp: Start of window
1313Ctrl-Alt-PgDn: End of window
1314F8: Start selection mode
1315Esc: End selection mode
1316
1317Adding Shift does the above but starts/extends selection.
1318
1319
1320 */
1321
0bab774b 1322bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags)
5d7836c4
JS
1323{
1324 bool success = false;
5d7836c4 1325
3e3a754f 1326 if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT)
5d7836c4
JS
1327 {
1328 if (flags & wxRICHTEXT_CTRL_DOWN)
1329 success = WordRight(1, flags);
1330 else
1331 success = MoveRight(1, flags);
1332 }
3e3a754f 1333 else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT)
5d7836c4
JS
1334 {
1335 if (flags & wxRICHTEXT_CTRL_DOWN)
1336 success = WordLeft(1, flags);
1337 else
1338 success = MoveLeft(1, flags);
1339 }
3e3a754f 1340 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP)
5d7836c4
JS
1341 {
1342 if (flags & wxRICHTEXT_CTRL_DOWN)
1343 success = MoveToParagraphStart(flags);
1344 else
1345 success = MoveUp(1, flags);
1346 }
3e3a754f 1347 else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN)
5d7836c4
JS
1348 {
1349 if (flags & wxRICHTEXT_CTRL_DOWN)
1350 success = MoveToParagraphEnd(flags);
1351 else
1352 success = MoveDown(1, flags);
1353 }
3e3a754f 1354 else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP)
5d7836c4
JS
1355 {
1356 success = PageUp(1, flags);
1357 }
3e3a754f 1358 else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
5d7836c4
JS
1359 {
1360 success = PageDown(1, flags);
1361 }
3e3a754f 1362 else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME)
5d7836c4
JS
1363 {
1364 if (flags & wxRICHTEXT_CTRL_DOWN)
1365 success = MoveHome(flags);
1366 else
1367 success = MoveToLineStart(flags);
1368 }
3e3a754f 1369 else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END)
5d7836c4
JS
1370 {
1371 if (flags & wxRICHTEXT_CTRL_DOWN)
1372 success = MoveEnd(flags);
1373 else
1374 success = MoveToLineEnd(flags);
1375 }
1376
1377 if (success)
1378 {
1379 ScrollIntoView(m_caretPosition, keyCode);
0a21bea0 1380 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1381 }
1382
5d7836c4
JS
1383 return success;
1384}
1385
1386/// Extend the selection. Selections are in caret positions.
1387bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
1388{
1389 if (flags & wxRICHTEXT_SHIFT_DOWN)
1390 {
e2d0875a
JS
1391 if (oldPos == newPos)
1392 return false;
1393
603f702b 1394 wxRichTextSelection oldSelection = m_selection;
4fe83b93 1395
603f702b 1396 m_selection.SetContainer(GetFocusObject());
4fe83b93 1397
603f702b
JS
1398 wxRichTextRange oldRange;
1399 if (m_selection.IsValid())
1400 oldRange = m_selection.GetRange();
1401 else
1402 oldRange = wxRICHTEXT_NO_SELECTION;
1403 wxRichTextRange newRange;
7051fa41 1404
5d7836c4 1405 // If not currently selecting, start selecting
603f702b 1406 if (oldRange.GetStart() == -2)
5d7836c4
JS
1407 {
1408 m_selectionAnchor = oldPos;
1409
1410 if (oldPos > newPos)
603f702b 1411 newRange.SetRange(newPos+1, oldPos);
5d7836c4 1412 else
603f702b 1413 newRange.SetRange(oldPos+1, newPos);
5d7836c4
JS
1414 }
1415 else
1416 {
1417 // Always ensure that the selection range start is greater than
1418 // the end.
1419 if (newPos > m_selectionAnchor)
603f702b 1420 newRange.SetRange(m_selectionAnchor+1, newPos);
e2d0875a 1421 else if (newPos == m_selectionAnchor)
603f702b 1422 newRange = wxRichTextRange(-2, -2);
5d7836c4 1423 else
603f702b 1424 newRange.SetRange(newPos+1, m_selectionAnchor);
5d7836c4 1425 }
4fe83b93 1426
603f702b 1427 m_selection.SetRange(newRange);
5d7836c4 1428
603f702b 1429 RefreshForSelectionChange(oldSelection, m_selection);
7051fa41 1430
603f702b 1431 if (newRange.GetStart() > newRange.GetEnd())
5d7836c4
JS
1432 {
1433 wxLogDebug(wxT("Strange selection range"));
1434 }
7fe8059f 1435
5d7836c4
JS
1436 return true;
1437 }
1438 else
1439 return false;
1440}
1441
1442/// Scroll into view, returning true if we scrolled.
1443/// This takes a _caret_ position.
1444bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
1445{
1446 wxRichTextLine* line = GetVisibleLineForCaretPosition(position);
1447
1448 if (!line)
1449 return false;
1450
1451 int ppuX, ppuY;
1452 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1453
3956d02a
JS
1454 int startXUnits, startYUnits;
1455 GetViewStart(& startXUnits, & startYUnits);
1456 int startY = startYUnits * ppuY;
5d7836c4 1457
8d7eaf91 1458 int sx = 0, sy = 0;
5d7836c4 1459 GetVirtualSize(& sx, & sy);
3956d02a
JS
1460 int sxUnits = 0;
1461 int syUnits = 0;
5d7836c4 1462 if (ppuY != 0)
3956d02a 1463 syUnits = sy/ppuY;
5d7836c4
JS
1464
1465 wxRect rect = line->GetRect();
1466
1467 bool scrolled = false;
1468
1469 wxSize clientSize = GetClientSize();
4fe83b93 1470
603f702b
JS
1471 int leftMargin, rightMargin, topMargin, bottomMargin;
1472
1473 {
1474 wxClientDC dc(this);
1475 wxRichTextObject::GetTotalMargin(dc, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin, rightMargin,
1476 topMargin, bottomMargin);
1477 }
1478// clientSize.y -= GetBuffer().GetBottomMargin();
1479 clientSize.y -= bottomMargin;
e9f10004
JS
1480
1481 if (GetWindowStyle() & wxRE_CENTRE_CARET)
1482 {
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)
1486 {
1487 if (startYUnits != yUnits)
1488 {
1489 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1490 scrolled = true;
1491 }
1492#if !wxRICHTEXT_USE_OWN_CARET
1493 if (scrolled)
1494#endif
1495 PositionCaret();
1496
1497 return scrolled;
1498 }
1499 }
5d7836c4
JS
1500
1501 // Going down
3e3a754f 1502 if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN ||
ec36b155 1503 keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT ||
3e3a754f
JS
1504 keyCode == WXK_END || keyCode == WXK_NUMPAD_END ||
1505 keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
5d7836c4
JS
1506 {
1507 if ((rect.y + rect.height) > (clientSize.y + startY))
1508 {
1509 // Make it scroll so this item is at the bottom
1510 // of the window
1511 int y = rect.y - (clientSize.y - rect.height);
3956d02a 1512 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1513
3956d02a
JS
1514 // If we're still off the screen, scroll another line down
1515 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1516 yUnits ++;
1517
1518 if (startYUnits != yUnits)
c59f6793 1519 {
3956d02a 1520 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1521 scrolled = true;
1522 }
5d7836c4 1523 }
e9f10004 1524 else if (rect.y < (startY + GetBuffer().GetTopMargin()))
5d7836c4
JS
1525 {
1526 // Make it scroll so this item is at the top
1527 // of the window
e9f10004 1528 int y = rect.y - GetBuffer().GetTopMargin();
3956d02a 1529 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1530
3956d02a 1531 if (startYUnits != yUnits)
c59f6793 1532 {
3956d02a 1533 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1534 scrolled = true;
1535 }
5d7836c4 1536 }
5d7836c4
JS
1537 }
1538 // Going up
3e3a754f
JS
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 )
5d7836c4 1543 {
e9f10004 1544 if (rect.y < (startY + GetBuffer().GetBottomMargin()))
5d7836c4
JS
1545 {
1546 // Make it scroll so this item is at the top
1547 // of the window
e9f10004 1548 int y = rect.y - GetBuffer().GetTopMargin();
3956d02a 1549 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1550
3956d02a 1551 if (startYUnits != yUnits)
c59f6793 1552 {
3956d02a 1553 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1554 scrolled = true;
1555 }
5d7836c4
JS
1556 }
1557 else if ((rect.y + rect.height) > (clientSize.y + startY))
1558 {
1559 // Make it scroll so this item is at the bottom
1560 // of the window
1561 int y = rect.y - (clientSize.y - rect.height);
3956d02a
JS
1562 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1563
1564 // If we're still off the screen, scroll another line down
1565 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1566 yUnits ++;
c59f6793 1567
3956d02a 1568 if (startYUnits != yUnits)
c59f6793 1569 {
3956d02a 1570 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1571 scrolled = true;
1572 }
5d7836c4 1573 }
5d7836c4 1574 }
109bfc88 1575
1c13f06e 1576#if !wxRICHTEXT_USE_OWN_CARET
109bfc88 1577 if (scrolled)
1c13f06e 1578#endif
109bfc88 1579 PositionCaret();
5d7836c4
JS
1580
1581 return scrolled;
1582}
1583
1584/// Is the given position visible on the screen?
1585bool wxRichTextCtrl::IsPositionVisible(long pos) const
1586{
1587 wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1);
1588
1589 if (!line)
1590 return false;
1591
1592 int ppuX, ppuY;
1593 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1594
1595 int startX, startY;
1596 GetViewStart(& startX, & startY);
1597 startX = 0;
1598 startY = startY * ppuY;
1599
5d7836c4 1600 wxRect rect = line->GetRect();
5d7836c4 1601 wxSize clientSize = GetClientSize();
e9f10004 1602 clientSize.y -= GetBuffer().GetBottomMargin();
5d7836c4 1603
1437c7fb 1604 return (rect.GetTop() >= (startY + GetBuffer().GetTopMargin())) && (rect.GetBottom() <= (startY + clientSize.y));
5d7836c4
JS
1605}
1606
1607void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart)
1608{
1609 m_caretPosition = position;
1610 m_caretAtLineStart = showAtLineStart;
1611}
1612
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.
1616void wxRichTextCtrl::MoveCaretForward(long oldPosition)
1617{
603f702b 1618 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition);
5d7836c4
JS
1619
1620 // Only do the check if we're not at the end of the paragraph (where things work OK
1621 // anyway)
1622 if (para && (oldPosition != para->GetRange().GetEnd() - 1))
1623 {
603f702b 1624 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition);
5d7836c4
JS
1625
1626 if (line)
1627 {
1e967276
JS
1628 wxRichTextRange lineRange = line->GetAbsoluteRange();
1629
5d7836c4
JS
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.
1e967276 1633 if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
1634 {
1635 if (m_caretAtLineStart)
1636 {
1637 // We're already at the start of the line, so actually move on now.
1638 m_caretPosition = oldPosition + 1;
1639 m_caretAtLineStart = false;
1640 }
1641 else
1642 {
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;
7fe8059f 1647 m_caretAtLineStart = true;
5d7836c4 1648 }
0a21bea0 1649 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1650 return;
1651 }
1652 }
1653 }
1654 m_caretPosition ++;
0a21bea0 1655 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1656}
1657
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.
1661void wxRichTextCtrl::MoveCaretBack(long oldPosition)
1662{
603f702b 1663 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition);
5d7836c4
JS
1664
1665 // Only do the check if we're not at the start of the paragraph (where things work OK
1666 // anyway)
1667 if (para && (oldPosition != para->GetRange().GetStart()))
1668 {
603f702b 1669 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition);
5d7836c4
JS
1670
1671 if (line)
1672 {
1e967276
JS
1673 wxRichTextRange lineRange = line->GetAbsoluteRange();
1674
5d7836c4
JS
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.
1e967276 1678 if (oldPosition == lineRange.GetStart())
5d7836c4
JS
1679 {
1680 m_caretPosition = oldPosition-1;
1681 m_caretAtLineStart = true;
1682 return;
1683 }
1e967276 1684 else if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
1685 {
1686 if (m_caretAtLineStart)
1687 {
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;
1692 }
1693 else
1694 {
1695 // We're showing at the end of the line, so go back
1696 // to the previous character position.
1697 m_caretPosition = oldPosition - 1;
1698 }
0a21bea0 1699 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1700 return;
1701 }
1702 }
1703 }
1704 m_caretPosition --;
0a21bea0 1705 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1706}
1707
1708/// Move right
1709bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
1710{
603f702b 1711 long endPos = GetFocusObject()->GetOwnRange().GetEnd();
5d7836c4
JS
1712
1713 if (m_caretPosition + noPositions < endPos)
1714 {
1715 long oldPos = m_caretPosition;
1716 long newPos = m_caretPosition + noPositions;
1717
1718 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1719 if (!extendSel)
1720 SelectNone();
1721
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
1726 // line.
1727 if (noPositions == 1 && !extendSel)
1728 MoveCaretForward(oldPos);
1729 else
1730 SetCaretPosition(newPos);
1731
1732 PositionCaret();
0a21bea0 1733 SetDefaultStyleToCursorStyle();
5d7836c4 1734
5d7836c4
JS
1735 return true;
1736 }
1737 else
7fe8059f 1738 return false;
5d7836c4
JS
1739}
1740
1741/// Move left
1742bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
1743{
1744 long startPos = -1;
1745
1746 if (m_caretPosition > startPos - noPositions + 1)
1747 {
1748 long oldPos = m_caretPosition;
1749 long newPos = m_caretPosition - noPositions;
1750 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1751 if (!extendSel)
1752 SelectNone();
1753
1754 if (noPositions == 1 && !extendSel)
1755 MoveCaretBack(oldPos);
1756 else
1757 SetCaretPosition(newPos);
1758
1759 PositionCaret();
0a21bea0 1760 SetDefaultStyleToCursorStyle();
5d7836c4 1761
5d7836c4
JS
1762 return true;
1763 }
1764 else
7fe8059f 1765 return false;
5d7836c4
JS
1766}
1767
603f702b
JS
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).
1771long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position, int hitTestFlags, wxRichTextParagraphLayoutBox* container,
1772 bool& caretLineStart)
1773{
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;
1779
1780 if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE)
1781 {
1782 wxRichTextLine* thisLine = container->GetLineAtPosition(position-1);
1783 wxRichTextRange lineRange;
1784 if (thisLine)
1785 lineRange = thisLine->GetAbsoluteRange();
1786
1787 if (thisLine && (position-1) == lineRange.GetEnd())
1788 {
1789 caretPosition --;
1790 caretLineStart = true;
1791 }
1792 else
1793 {
1794 wxRichTextParagraph* para = container->GetParagraphAtPosition(position);
1795 if (para && para->GetRange().GetStart() == position)
1796 caretPosition --;
1797 }
1798 }
1799 return caretPosition;
1800}
1801
5d7836c4
JS
1802/// Move up
1803bool wxRichTextCtrl::MoveUp(int noLines, int flags)
1804{
1805 return MoveDown(- noLines, flags);
1806}
1807
1808/// Move up
1809bool wxRichTextCtrl::MoveDown(int noLines, int flags)
1810{
c59f6793
JS
1811 if (!GetCaret())
1812 return false;
1813
603f702b 1814 long lineNumber = GetFocusObject()->GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
5d7836c4 1815 wxPoint pt = GetCaret()->GetPosition();
4fe83b93 1816 long newLine = lineNumber + noLines;
603f702b 1817 bool notInThisObject = false;
5d7836c4
JS
1818
1819 if (lineNumber != -1)
1820 {
1821 if (noLines > 0)
1822 {
603f702b 1823 long lastLine = GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
5d7836c4 1824 if (newLine > lastLine)
603f702b 1825 notInThisObject = true;
5d7836c4
JS
1826 }
1827 else
1828 {
1829 if (newLine < 0)
603f702b 1830 notInThisObject = true;
5d7836c4
JS
1831 }
1832 }
4fe83b93 1833
603f702b 1834 wxRichTextParagraphLayoutBox* container = GetFocusObject();
343ef639 1835 int hitTestFlags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS;
5d7836c4 1836
603f702b 1837 if (notInThisObject)
7fe8059f 1838 {
603f702b
JS
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();
343ef639 1842 hitTestFlags &= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS;
4fe83b93 1843
603f702b
JS
1844 if (noLines > 0) // going down
1845 {
1846 pt.y = GetFocusObject()->GetPosition().y + GetFocusObject()->GetCachedSize().y + 2;
1847 }
1848 else // going up
1849 {
1850 pt.y = GetFocusObject()->GetPosition().y - 2;
1851 }
5d7836c4
JS
1852 }
1853 else
603f702b
JS
1854 {
1855 wxRichTextLine* lineObj = GetFocusObject()->GetLineForVisibleLineNumber(newLine);
1856 if (lineObj)
1857 pt.y = lineObj->GetAbsolutePosition().y + 2;
1858 else
1859 return false;
1860 }
5d7836c4
JS
1861
1862 long newPos = 0;
1863 wxClientDC dc(this);
1864 PrepareDC(dc);
1865 dc.SetFont(GetFont());
7fe8059f 1866
603f702b
JS
1867 wxRichTextObject* hitObj = NULL;
1868 wxRichTextObject* contextObj = NULL;
1869 int hitTest = container->HitTest(dc, pt, newPos, & hitObj, & contextObj, hitTestFlags);
7fe8059f 1870
343ef639
JS
1871 if (hitObj &&
1872 ((hitTest & wxRICHTEXT_HITTEST_NONE) == 0) &&
1873 (! (hitObj == (& m_buffer) && ((hitTest & wxRICHTEXT_HITTEST_OUTSIDE) != 0))) // outside the buffer counts as 'do nothing'
1874 )
5d7836c4 1875 {
603f702b 1876 if (notInThisObject)
5d7836c4 1877 {
603f702b
JS
1878 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
1879 if (actualContainer && actualContainer != GetFocusObject() && actualContainer->AcceptsFocus())
5d7836c4 1880 {
603f702b 1881 SetFocusObject(actualContainer, false /* don't set caret position yet */);
4fe83b93 1882
603f702b 1883 container = actualContainer;
5d7836c4
JS
1884 }
1885 }
4fe83b93 1886
603f702b
JS
1887 bool caretLineStart = true;
1888 long caretPosition = FindCaretPositionForCharacterPosition(newPos, hitTest, container, caretLineStart);
1889 long newSelEnd = caretPosition;
1890 bool extendSel;
1891
1892 if (notInThisObject)
1893 extendSel = false;
1894 else
1895 extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
5d7836c4 1896
c59f6793 1897 if (!extendSel)
5d7836c4
JS
1898 SelectNone();
1899
603f702b 1900 SetCaretPosition(caretPosition, caretLineStart);
5d7836c4 1901 PositionCaret();
0a21bea0 1902 SetDefaultStyleToCursorStyle();
5d7836c4 1903
5d7836c4
JS
1904 return true;
1905 }
41e155b4
WS
1906
1907 return false;
5d7836c4
JS
1908}
1909
1910/// Move to the end of the paragraph
1911bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1912{
603f702b 1913 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true);
5d7836c4
JS
1914 if (para)
1915 {
1916 long newPos = para->GetRange().GetEnd() - 1;
c59f6793
JS
1917 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1918 if (!extendSel)
5d7836c4
JS
1919 SelectNone();
1920
1921 SetCaretPosition(newPos);
1922 PositionCaret();
0a21bea0 1923 SetDefaultStyleToCursorStyle();
5d7836c4 1924
5d7836c4
JS
1925 return true;
1926 }
1927
1928 return false;
1929}
1930
1931/// Move to the start of the paragraph
1932bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1933{
603f702b 1934 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true);
5d7836c4
JS
1935 if (para)
1936 {
1937 long newPos = para->GetRange().GetStart() - 1;
c59f6793
JS
1938 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1939 if (!extendSel)
5d7836c4
JS
1940 SelectNone();
1941
1942 SetCaretPosition(newPos);
1943 PositionCaret();
0a21bea0 1944 SetDefaultStyleToCursorStyle();
5d7836c4 1945
5d7836c4
JS
1946 return true;
1947 }
1948
1949 return false;
1950}
1951
1952/// Move to the end of the line
1953bool wxRichTextCtrl::MoveToLineEnd(int flags)
1954{
1955 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1956
1957 if (line)
1958 {
1e967276
JS
1959 wxRichTextRange lineRange = line->GetAbsoluteRange();
1960 long newPos = lineRange.GetEnd();
c59f6793
JS
1961 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1962 if (!extendSel)
5d7836c4
JS
1963 SelectNone();
1964
1965 SetCaretPosition(newPos);
1966 PositionCaret();
0a21bea0 1967 SetDefaultStyleToCursorStyle();
5d7836c4 1968
5d7836c4
JS
1969 return true;
1970 }
1971
1972 return false;
1973}
1974
1975/// Move to the start of the line
1976bool wxRichTextCtrl::MoveToLineStart(int flags)
1977{
1978 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1979 if (line)
1980 {
1e967276
JS
1981 wxRichTextRange lineRange = line->GetAbsoluteRange();
1982 long newPos = lineRange.GetStart()-1;
5d7836c4 1983
c59f6793
JS
1984 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1985 if (!extendSel)
5d7836c4
JS
1986 SelectNone();
1987
603f702b 1988 wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(line);
5d7836c4 1989
1e967276 1990 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4 1991 PositionCaret();
0a21bea0 1992 SetDefaultStyleToCursorStyle();
5d7836c4 1993
5d7836c4
JS
1994 return true;
1995 }
1996
1997 return false;
1998}
1999
2000/// Move to the start of the buffer
2001bool wxRichTextCtrl::MoveHome(int flags)
2002{
2003 if (m_caretPosition != -1)
2004 {
c59f6793
JS
2005 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
2006 if (!extendSel)
5d7836c4
JS
2007 SelectNone();
2008
2009 SetCaretPosition(-1);
2010 PositionCaret();
0a21bea0 2011 SetDefaultStyleToCursorStyle();
5d7836c4 2012
5d7836c4
JS
2013 return true;
2014 }
2015 else
7fe8059f 2016 return false;
5d7836c4
JS
2017}
2018
2019/// Move to the end of the buffer
2020bool wxRichTextCtrl::MoveEnd(int flags)
2021{
603f702b 2022 long endPos = GetFocusObject()->GetOwnRange().GetEnd()-1;
5d7836c4
JS
2023
2024 if (m_caretPosition != endPos)
2025 {
c59f6793
JS
2026 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
2027 if (!extendSel)
5d7836c4
JS
2028 SelectNone();
2029
2030 SetCaretPosition(endPos);
2031 PositionCaret();
0a21bea0 2032 SetDefaultStyleToCursorStyle();
5d7836c4 2033
5d7836c4
JS
2034 return true;
2035 }
2036 else
7fe8059f 2037 return false;
5d7836c4
JS
2038}
2039
2040/// Move noPages pages up
2041bool wxRichTextCtrl::PageUp(int noPages, int flags)
2042{
2043 return PageDown(- noPages, flags);
2044}
2045
2046/// Move noPages pages down
2047bool wxRichTextCtrl::PageDown(int noPages, int flags)
2048{
2049 // Calculate which line occurs noPages * screen height further down.
2050 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
2051 if (line)
2052 {
2053 wxSize clientSize = GetClientSize();
2054 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
2055
603f702b 2056 wxRichTextLine* newLine = GetFocusObject()->GetLineAtYPosition(newY);
5d7836c4
JS
2057 if (newLine)
2058 {
1e967276
JS
2059 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
2060 long pos = lineRange.GetStart()-1;
5d7836c4
JS
2061 if (pos != m_caretPosition)
2062 {
603f702b 2063 wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(newLine);
5d7836c4 2064
c59f6793
JS
2065 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
2066 if (!extendSel)
5d7836c4
JS
2067 SelectNone();
2068
1e967276 2069 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4 2070 PositionCaret();
0a21bea0 2071 SetDefaultStyleToCursorStyle();
5d7836c4 2072
5d7836c4
JS
2073 return true;
2074 }
2075 }
2076 }
2077
2078 return false;
2079}
2080
0871b343
JS
2081static bool wxRichTextCtrlIsWhitespace(const wxString& str)
2082{
2083 return str == wxT(" ") || str == wxT("\t");
2084}
2085
5d7836c4
JS
2086// Finds the caret position for the next word
2087long wxRichTextCtrl::FindNextWordPosition(int direction) const
7fe8059f 2088{
603f702b 2089 long endPos = GetFocusObject()->GetOwnRange().GetEnd();
5d7836c4
JS
2090
2091 if (direction > 0)
2092 {
2093 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
7fe8059f 2094
5d7836c4
JS
2095 // First skip current text to space
2096 while (i < endPos && i > -1)
2097 {
2098 // i is in character, not caret positions
603f702b
JS
2099 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2100 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2101 if (line && (i == line->GetAbsoluteRange().GetEnd()))
2102 {
2103 break;
2104 }
2105 else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty())
5d7836c4
JS
2106 i += direction;
2107 else
2108 {
2109 break;
2110 }
2111 }
2112 while (i < endPos && i > -1)
2113 {
2114 // i is in character, not caret positions
603f702b
JS
2115 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2116 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2117 if (line && (i == line->GetAbsoluteRange().GetEnd()))
2118 return wxMax(-1, i);
2119
7fe8059f 2120 if (text.empty()) // End of paragraph, or maybe an image
5d7836c4 2121 return wxMax(-1, i - 1);
0871b343 2122 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
5d7836c4
JS
2123 i += direction;
2124 else
2125 {
2126 // Convert to caret position
2127 return wxMax(-1, i - 1);
2128 }
2129 }
2130 if (i >= endPos)
2131 return endPos-1;
2132 return i-1;
2133 }
2134 else
2135 {
2136 long i = m_caretPosition;
2137
2138 // First skip white space
2139 while (i < endPos && i > -1)
2140 {
2141 // i is in character, not caret positions
603f702b
JS
2142 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2143 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2144
2145 if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
5d7836c4 2146 break;
0871b343 2147 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
5d7836c4
JS
2148 i += direction;
2149 else
2150 break;
2151 }
2152 // Next skip current text to space
2153 while (i < endPos && i > -1)
2154 {
2155 // i is in character, not caret positions
603f702b
JS
2156 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2157 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2158 if (line && line->GetAbsoluteRange().GetStart() == i)
2159 return i-1;
2160
2161 if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */)
5d7836c4
JS
2162 i += direction;
2163 else
2164 {
2165 return i;
5d7836c4
JS
2166 }
2167 }
2168 if (i < -1)
2169 return -1;
2170 return i;
2171 }
2172}
2173
2174/// Move n words left
2175bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
2176{
2177 long pos = FindNextWordPosition(-1);
2178 if (pos != m_caretPosition)
2179 {
603f702b 2180 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true);
7fe8059f 2181
c59f6793
JS
2182 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
2183 if (!extendSel)
5d7836c4 2184 SelectNone();
7fe8059f 2185
5d7836c4
JS
2186 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
2187 PositionCaret();
0a21bea0 2188 SetDefaultStyleToCursorStyle();
5d7836c4 2189
5d7836c4
JS
2190 return true;
2191 }
7fe8059f 2192
5d7836c4
JS
2193 return false;
2194}
2195
2196/// Move n words right
2197bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
2198{
2199 long pos = FindNextWordPosition(1);
2200 if (pos != m_caretPosition)
2201 {
603f702b 2202 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true);
7fe8059f 2203
c59f6793
JS
2204 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
2205 if (!extendSel)
5d7836c4 2206 SelectNone();
7fe8059f 2207
5d7836c4
JS
2208 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
2209 PositionCaret();
0a21bea0 2210 SetDefaultStyleToCursorStyle();
5d7836c4 2211
5d7836c4
JS
2212 return true;
2213 }
7fe8059f 2214
5d7836c4
JS
2215 return false;
2216}
2217
2218/// Sizing
2219void wxRichTextCtrl::OnSize(wxSizeEvent& event)
2220{
4d551ad5 2221 // Only do sizing optimization for large buffers
603f702b 2222 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold)
4d551ad5
JS
2223 {
2224 m_fullLayoutRequired = true;
2225 m_fullLayoutTime = wxGetLocalTimeMillis();
2226 m_fullLayoutSavedPosition = GetFirstVisiblePosition();
2f36e8dc 2227 LayoutContent(true /* onlyVisibleRect */);
4d551ad5
JS
2228 }
2229 else
2230 GetBuffer().Invalidate(wxRICHTEXT_ALL);
7fe8059f 2231
3e3a754f 2232#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4 2233 RecreateBuffer();
3e3a754f 2234#endif
5d7836c4
JS
2235
2236 event.Skip();
2237}
2238
98769552
JS
2239// Force any pending layout due to large buffer
2240void wxRichTextCtrl::ForceDelayedLayout()
2241{
2242 if (m_fullLayoutRequired)
2243 {
2244 m_fullLayoutRequired = false;
2245 m_fullLayoutTime = 0;
2246 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2247 ShowPosition(m_fullLayoutSavedPosition);
2248 Refresh(false);
2249 Update();
2250 }
2251}
4d551ad5
JS
2252
2253/// Idle-time processing
2254void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
2255{
1c13f06e
JS
2256#if wxRICHTEXT_USE_OWN_CARET
2257 if (((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
2258 {
2259 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(false);
2260 PositionCaret();
2261 GetCaret()->Show();
2262 }
2263#endif
2264
4d551ad5
JS
2265 const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL;
2266
2267 if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval)))
2268 {
2269 m_fullLayoutRequired = false;
2270 m_fullLayoutTime = 0;
2271 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2272 ShowPosition(m_fullLayoutSavedPosition);
76bcd815 2273 Refresh(false);
4d551ad5 2274 }
ab14c7aa
JS
2275
2276 if (m_caretPositionForDefaultStyle != -2)
2277 {
2278 // If the caret position has changed, no longer reflect the default style
2279 // in the UI.
2280 if (GetCaretPosition() != m_caretPositionForDefaultStyle)
2281 m_caretPositionForDefaultStyle = -2;
2282 }
2283
4d551ad5
JS
2284 event.Skip();
2285}
2286
c59f6793
JS
2287/// Scrolling
2288void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
2289{
1c13f06e
JS
2290#if wxRICHTEXT_USE_OWN_CARET
2291 if (!((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
2292 {
2293 GetCaret()->Hide();
2294 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate();
2295 }
2296#endif
2297
c59f6793
JS
2298 event.Skip();
2299}
2300
5d7836c4 2301/// Set up scrollbars, e.g. after a resize
169adfa9 2302void wxRichTextCtrl::SetupScrollbars(bool atTop)
5d7836c4 2303{
17808a75 2304 if (IsFrozen())
5d7836c4
JS
2305 return;
2306
2307 if (GetBuffer().IsEmpty())
2308 {
2309 SetScrollbars(0, 0, 0, 0, 0, 0);
2310 return;
2311 }
2312
2313 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2314 // of pixels. See e.g. wxVScrolledWindow for ideas.
3956d02a 2315 int pixelsPerUnit = 5;
5d7836c4
JS
2316 wxSize clientSize = GetClientSize();
2317
e9f10004 2318 int maxHeight = GetBuffer().GetCachedSize().y + GetBuffer().GetTopMargin();
5d7836c4 2319
3956d02a
JS
2320 // Round up so we have at least maxHeight pixels
2321 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
5d7836c4 2322
169adfa9
JS
2323 int startX = 0, startY = 0;
2324 if (!atTop)
2325 GetViewStart(& startX, & startY);
7fe8059f 2326
7051fa41 2327 int maxPositionX = 0;
3956d02a 2328 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
7fe8059f 2329
7051fa41
JS
2330 int newStartX = wxMin(maxPositionX, startX);
2331 int newStartY = wxMin(maxPositionY, startY);
2332
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);
2339 if (oldPPUY > 0)
2340 oldVirtualSizeY /= oldPPUY;
2341
2342 if (oldPPUX == 0 && oldPPUY == pixelsPerUnit && oldVirtualSizeY == unitsY && oldStartX == newStartX && oldStartY == newStartY)
2343 return;
2344
2345 // Don't set scrollbars if there were none before, and there will be none now.
205a1ad9 2346 if (oldPPUY != 0 && (oldVirtualSizeY*oldPPUY < clientSize.y) && (unitsY*pixelsPerUnit < clientSize.y))
7051fa41
JS
2347 return;
2348
5d7836c4
JS
2349 // Move to previous scroll position if
2350 // possible
7051fa41 2351 SetScrollbars(0, pixelsPerUnit, 0, unitsY, newStartX, newStartY);
5d7836c4
JS
2352}
2353
2354/// Paint the background
2355void wxRichTextCtrl::PaintBackground(wxDC& dc)
2356{
2357 wxColour backgroundColour = GetBackgroundColour();
a1b806b9 2358 if (!backgroundColour.IsOk())
5d7836c4
JS
2359 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
2360
2361 // Clear the background
2362 dc.SetBrush(wxBrush(backgroundColour));
2363 dc.SetPen(*wxTRANSPARENT_PEN);
7fe8059f 2364 wxRect windowRect(GetClientSize());
5d7836c4
JS
2365 windowRect.x -= 2; windowRect.y -= 2;
2366 windowRect.width += 4; windowRect.height += 4;
7fe8059f 2367
5d7836c4
JS
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);
2372}
2373
3e3a754f 2374#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4
JS
2375/// Recreate buffer bitmap if necessary
2376bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
2377{
2378 wxSize sz = size;
2379 if (sz == wxDefaultSize)
2380 sz = GetClientSize();
7fe8059f 2381
5d7836c4
JS
2382 if (sz.x < 1 || sz.y < 1)
2383 return false;
7fe8059f 2384
a1b806b9 2385 if (!m_bufferBitmap.IsOk() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
5d7836c4 2386 m_bufferBitmap = wxBitmap(sz.x, sz.y);
a1b806b9 2387 return m_bufferBitmap.IsOk();
5d7836c4 2388}
3e3a754f 2389#endif
5d7836c4
JS
2390
2391// ----------------------------------------------------------------------------
2392// file IO functions
2393// ----------------------------------------------------------------------------
2394
3306aec1
JS
2395bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
2396{
d75a69e8 2397 bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType);
5d7836c4
JS
2398 if (success)
2399 m_filename = filename;
2400
2401 DiscardEdits();
2402 SetInsertionPoint(0);
2f36e8dc 2403 LayoutContent();
5d7836c4 2404 PositionCaret();
169adfa9 2405 SetupScrollbars(true);
76bcd815 2406 Refresh(false);
0ec1179b 2407 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
2408
2409 if (success)
2410 return true;
2411 else
2412 {
2413 wxLogError(_("File couldn't be loaded."));
7fe8059f 2414
5d7836c4
JS
2415 return false;
2416 }
2417}
2418
3306aec1 2419bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
5d7836c4 2420{
d75a69e8 2421 if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType))
5d7836c4 2422 {
3306aec1 2423 m_filename = filename;
5d7836c4
JS
2424
2425 DiscardEdits();
2426
2427 return true;
5d7836c4
JS
2428 }
2429
2430 wxLogError(_("The text couldn't be saved."));
2431
2432 return false;
2433}
2434
2435// ----------------------------------------------------------------------------
2436// wxRichTextCtrl specific functionality
2437// ----------------------------------------------------------------------------
2438
2439/// Add a new paragraph of text to the end of the buffer
2440wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
2441{
603f702b 2442 wxRichTextRange range = GetFocusObject()->AddParagraph(text);
4fe83b93 2443 GetBuffer().Invalidate();
62381daa
JS
2444 LayoutContent();
2445 return range;
5d7836c4
JS
2446}
2447
2448/// Add an image
2449wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
2450{
603f702b 2451 wxRichTextRange range = GetFocusObject()->AddImage(image);
4fe83b93 2452 GetBuffer().Invalidate();
62381daa
JS
2453 LayoutContent();
2454 return range;
5d7836c4
JS
2455}
2456
2457// ----------------------------------------------------------------------------
2458// selection and ranges
2459// ----------------------------------------------------------------------------
2460
2461void wxRichTextCtrl::SelectAll()
2462{
76d7fc29 2463 SetSelection(-1, -1);
5d7836c4
JS
2464}
2465
2466/// Select none
2467void wxRichTextCtrl::SelectNone()
2468{
603f702b 2469 if (m_selection.IsValid())
c1aec196 2470 {
603f702b 2471 wxRichTextSelection oldSelection = m_selection;
7051fa41 2472
603f702b 2473 m_selection.Reset();
7051fa41 2474
603f702b 2475 RefreshForSelectionChange(oldSelection, m_selection);
c1aec196 2476 }
5d7836c4 2477 m_selectionAnchor = -2;
603f702b
JS
2478 m_selectionAnchorObject = NULL;
2479 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4
JS
2480}
2481
0ca07313
JS
2482static bool wxIsWordDelimiter(const wxString& text)
2483{
1c07e924 2484 return !text.IsEmpty() && !wxIsalnum(text[0]);
0ca07313
JS
2485}
2486
2487/// Select the word at the given character position
2488bool wxRichTextCtrl::SelectWord(long position)
2489{
603f702b 2490 if (position < 0 || position > GetFocusObject()->GetOwnRange().GetEnd())
0ca07313 2491 return false;
1807a1f3 2492
603f702b 2493 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(position);
0ca07313
JS
2494 if (!para)
2495 return false;
2496
62381daa
JS
2497 if (position == para->GetRange().GetEnd())
2498 position --;
2499
0ca07313
JS
2500 long positionStart = position;
2501 long positionEnd = position;
1807a1f3 2502
0ca07313
JS
2503 for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --)
2504 {
603f702b 2505 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart, positionStart));
0ca07313
JS
2506 if (wxIsWordDelimiter(text))
2507 {
2508 positionStart ++;
2509 break;
2510 }
2511 }
2512 if (positionStart < para->GetRange().GetStart())
2513 positionStart = para->GetRange().GetStart();
1807a1f3 2514
0ca07313
JS
2515 for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++)
2516 {
603f702b 2517 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd, positionEnd));
0ca07313
JS
2518 if (wxIsWordDelimiter(text))
2519 {
2520 positionEnd --;
2521 break;
2522 }
2523 }
2524 if (positionEnd >= para->GetRange().GetEnd())
2525 positionEnd = para->GetRange().GetEnd();
1807a1f3 2526
62381daa
JS
2527 if (positionEnd < positionStart)
2528 return false;
2529
0ca07313
JS
2530 SetSelection(positionStart, positionEnd+1);
2531
2532 if (positionStart >= 0)
2533 {
2534 MoveCaret(positionStart-1, true);
0a21bea0 2535 SetDefaultStyleToCursorStyle();
0ca07313
JS
2536 }
2537
2538 return true;
2539}
2540
5d7836c4
JS
2541wxString wxRichTextCtrl::GetStringSelection() const
2542{
2543 long from, to;
2544 GetSelection(&from, &to);
2545
2546 return GetRange(from, to);
2547}
2548
5d7836c4
JS
2549// ----------------------------------------------------------------------------
2550// hit testing
2551// ----------------------------------------------------------------------------
2552
2553wxTextCtrlHitTestResult
2554wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
2555{
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)
2559 long pos;
2560 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
2561
2562 if ( rc != wxTE_HT_UNKNOWN )
2563 {
2564 PositionToXY(pos, x, y);
2565 }
2566
2567 return rc;
2568}
2569
2570wxTextCtrlHitTestResult
2571wxRichTextCtrl::HitTest(const wxPoint& pt,
2572 long * pos) const
2573{
2574 wxClientDC dc((wxRichTextCtrl*) this);
2575 ((wxRichTextCtrl*)this)->PrepareDC(dc);
61399247 2576
563d25ba
JS
2577 // Buffer uses logical position (relative to start of buffer)
2578 // so convert
2579 wxPoint pt2 = GetLogicalPoint(pt);
5d7836c4 2580
603f702b
JS
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);
41e155b4 2584
f262b25c
JS
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;
25d6eb3a 2589 else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER))
f262b25c 2590 return wxTE_HT_ON_TEXT;
41e155b4
WS
2591
2592 return wxTE_HT_UNKNOWN;
5d7836c4
JS
2593}
2594
2595// ----------------------------------------------------------------------------
2596// set/get the controls text
2597// ----------------------------------------------------------------------------
2598
805c8d92 2599wxString wxRichTextCtrl::DoGetValue() const
5d7836c4
JS
2600{
2601 return GetBuffer().GetText();
2602}
2603
2604wxString wxRichTextCtrl::GetRange(long from, long to) const
2605{
96c9f0f6 2606 // Public API for range is different from internals
603f702b 2607 return GetFocusObject()->GetTextForRange(wxRichTextRange(from, to-1));
5d7836c4
JS
2608}
2609
343ae70d 2610void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
5d7836c4 2611{
d081739d
JS
2612 // Don't call Clear here, since it always sends a text updated event
2613 m_buffer.ResetAndClearCommands();
603f702b 2614 m_buffer.Invalidate(wxRICHTEXT_ALL);
d081739d
JS
2615 m_caretPosition = -1;
2616 m_caretPositionForDefaultStyle = -2;
2617 m_caretAtLineStart = false;
603f702b
JS
2618 m_selection.Reset();
2619 m_selectionState = wxRichTextCtrlSelectionState_Normal;
d081739d
JS
2620
2621 Scroll(0,0);
2622
2623 if (!IsFrozen())
2624 {
2625 LayoutContent();
2626 Refresh(false);
2627 }
5d7836c4 2628
1a75935d 2629 if (!value.IsEmpty())
5d7836c4 2630 {
1a75935d
JS
2631 // Remove empty paragraph
2632 GetBuffer().Clear();
d081739d 2633 DoWriteText(value, flags);
5d7836c4
JS
2634
2635 // for compatibility, don't move the cursor when doing SetValue()
2636 SetInsertionPoint(0);
2637 }
1a75935d 2638 else
5d7836c4 2639 {
1a75935d
JS
2640 // still send an event for consistency
2641 if (flags & SetValue_SendEvent)
0ec1179b 2642 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4 2643 }
5d7836c4
JS
2644 DiscardEdits();
2645}
2646
2647void wxRichTextCtrl::WriteText(const wxString& value)
2648{
2649 DoWriteText(value);
2650}
2651
343ae70d 2652void wxRichTextCtrl::DoWriteText(const wxString& value, int flags)
5d7836c4 2653{
0ca07313 2654 wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix);
fe5aa22c 2655
603f702b 2656 GetFocusObject()->InsertTextWithUndo(m_caretPosition+1, valueUnix, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
343ae70d
VZ
2657
2658 if ( flags & SetValue_SendEvent )
0ec1179b 2659 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
2660}
2661
2662void wxRichTextCtrl::AppendText(const wxString& text)
2663{
2664 SetInsertionPointEnd();
2665
2666 WriteText(text);
2667}
2668
2669/// Write an image at the current insertion point
24777478 2670bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType, const wxRichTextAttr& textAttr)
5d7836c4
JS
2671{
2672 wxRichTextImageBlock imageBlock;
2673
2674 wxImage image2 = image;
2675 if (imageBlock.MakeImageBlock(image2, bitmapType))
24777478 2676 return WriteImage(imageBlock, textAttr);
41e155b4
WS
2677
2678 return false;
5d7836c4
JS
2679}
2680
24777478 2681bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType, const wxRichTextAttr& textAttr)
5d7836c4
JS
2682{
2683 wxRichTextImageBlock imageBlock;
2684
2685 wxImage image;
2686 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
24777478 2687 return WriteImage(imageBlock, textAttr);
41e155b4
WS
2688
2689 return false;
5d7836c4
JS
2690}
2691
24777478 2692bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock, const wxRichTextAttr& textAttr)
5d7836c4 2693{
603f702b 2694 return GetFocusObject()->InsertImageWithUndo(m_caretPosition+1, imageBlock, this, & GetBuffer(), 0, textAttr);
5d7836c4
JS
2695}
2696
24777478 2697bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType, const wxRichTextAttr& textAttr)
5d7836c4 2698{
a1b806b9 2699 if (bitmap.IsOk())
5d7836c4
JS
2700 {
2701 wxRichTextImageBlock imageBlock;
7fe8059f 2702
5d7836c4 2703 wxImage image = bitmap.ConvertToImage();
a1b806b9 2704 if (image.IsOk() && imageBlock.MakeImageBlock(image, bitmapType))
24777478 2705 return WriteImage(imageBlock, textAttr);
5d7836c4 2706 }
41e155b4 2707
5d7836c4
JS
2708 return false;
2709}
2710
603f702b
JS
2711// Write a text box at the current insertion point.
2712wxRichTextBox* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr& textAttr)
2713{
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);
2719
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);
2724 return box;
2725}
2726
2727// Write a table at the current insertion point, returning the table.
2728wxRichTextTable* wxRichTextCtrl::WriteTable(int rows, int cols, const wxRichTextAttr& tableAttr, const wxRichTextAttr& cellAttr)
2729{
2730 wxASSERT(rows > 0 && cols > 0);
2731
2732 if (rows <= 0 || cols <= 0)
2733 return NULL;
2734
2735 wxRichTextTable* table = new wxRichTextTable;
2736 table->SetAttributes(tableAttr);
2737 table->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
4fe83b93 2738
603f702b
JS
2739 table->CreateTable(rows, cols);
2740
2741 table->SetParent(NULL);
2742
2743 int i, j;
2744 for (j = 0; j < rows; j++)
2745 {
2746 for (i = 0; i < cols; i++)
2747 {
2748 table->GetCell(j, i)->GetAttributes() = cellAttr;
2749 }
2750 }
2751
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);
2756 return tableResult;
2757}
2758
2759
5d7836c4
JS
2760/// Insert a newline (actually paragraph) at the current insertion point.
2761bool wxRichTextCtrl::Newline()
2762{
603f702b 2763 return GetFocusObject()->InsertNewlineWithUndo(m_caretPosition+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
5d7836c4
JS
2764}
2765
ff76711f
JS
2766/// Insert a line break at the current insertion point.
2767bool wxRichTextCtrl::LineBreak()
2768{
2769 wxString text;
2770 text = wxRichTextLineBreakChar;
603f702b 2771 return GetFocusObject()->InsertTextWithUndo(m_caretPosition+1, text, this, & GetBuffer());
ff76711f 2772}
5d7836c4
JS
2773
2774// ----------------------------------------------------------------------------
2775// Clipboard operations
2776// ----------------------------------------------------------------------------
2777
2778void wxRichTextCtrl::Copy()
2779{
2780 if (CanCopy())
2781 {
fe5aa22c 2782 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2783 GetBuffer().CopyToClipboard(range);
2784 }
2785}
2786
2787void wxRichTextCtrl::Cut()
2788{
2789 if (CanCut())
2790 {
fe5aa22c 2791 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2792 GetBuffer().CopyToClipboard(range);
2793
2794 DeleteSelectedContent();
2f36e8dc 2795 LayoutContent();
76bcd815 2796 Refresh(false);
5d7836c4
JS
2797 }
2798}
2799
2800void wxRichTextCtrl::Paste()
2801{
2802 if (CanPaste())
2803 {
2804 BeginBatchUndo(_("Paste"));
7fe8059f 2805
5d7836c4
JS
2806 long newPos = m_caretPosition;
2807 DeleteSelectedContent(& newPos);
7fe8059f 2808
5d7836c4
JS
2809 GetBuffer().PasteFromClipboard(newPos);
2810
2811 EndBatchUndo();
2812 }
2813}
2814
2815void wxRichTextCtrl::DeleteSelection()
2816{
2817 if (CanDeleteSelection())
2818 {
2819 DeleteSelectedContent();
2820 }
2821}
2822
2823bool wxRichTextCtrl::HasSelection() const
2824{
603f702b
JS
2825 return (m_selection.IsValid() && m_selection.GetContainer() == GetFocusObject());
2826}
2827
2828bool wxRichTextCtrl::HasUnfocusedSelection() const
2829{
2830 return m_selection.IsValid();
5d7836c4
JS
2831}
2832
2833bool wxRichTextCtrl::CanCopy() const
2834{
2835 // Can copy if there's a selection
2836 return HasSelection();
2837}
2838
2839bool wxRichTextCtrl::CanCut() const
2840{
2841 return HasSelection() && IsEditable();
2842}
2843
2844bool wxRichTextCtrl::CanPaste() const
2845{
2846 if ( !IsEditable() )
2847 return false;
2848
2849 return GetBuffer().CanPasteFromClipboard();
2850}
2851
2852bool wxRichTextCtrl::CanDeleteSelection() const
2853{
2854 return HasSelection() && IsEditable();
2855}
2856
2857
2858// ----------------------------------------------------------------------------
2859// Accessors
2860// ----------------------------------------------------------------------------
2861
8e43fc77
JS
2862void wxRichTextCtrl::SetContextMenu(wxMenu* menu)
2863{
2864 if (m_contextMenu && m_contextMenu != menu)
2865 delete m_contextMenu;
2866 m_contextMenu = menu;
2867}
2868
5d7836c4
JS
2869void wxRichTextCtrl::SetEditable(bool editable)
2870{
2871 m_editable = editable;
2872}
2873
2874void wxRichTextCtrl::SetInsertionPoint(long pos)
2875{
2876 SelectNone();
2877
2878 m_caretPosition = pos - 1;
6f5d63bc
JS
2879
2880 PositionCaret();
4427f297
JS
2881
2882 SetDefaultStyleToCursorStyle();
5d7836c4
JS
2883}
2884
2885void wxRichTextCtrl::SetInsertionPointEnd()
2886{
2887 long pos = GetLastPosition();
2888 SetInsertionPoint(pos);
2889}
2890
2891long wxRichTextCtrl::GetInsertionPoint() const
2892{
2893 return m_caretPosition+1;
2894}
2895
2896wxTextPos wxRichTextCtrl::GetLastPosition() const
2897{
603f702b 2898 return GetFocusObject()->GetOwnRange().GetEnd();
5d7836c4
JS
2899}
2900
2901// If the return values from and to are the same, there is no
2902// selection.
2903void wxRichTextCtrl::GetSelection(long* from, long* to) const
2904{
603f702b
JS
2905 if (m_selection.IsValid())
2906 {
2907 *from = m_selection.GetRange().GetStart();
2908 *to = m_selection.GetRange().GetEnd();
96c9f0f6 2909 (*to) ++;
603f702b
JS
2910 }
2911 else
2912 {
2913 *from = -2;
2914 *to = -2;
2915 }
5d7836c4
JS
2916}
2917
2918bool wxRichTextCtrl::IsEditable() const
2919{
2920 return m_editable;
2921}
2922
2923// ----------------------------------------------------------------------------
2924// selection
2925// ----------------------------------------------------------------------------
2926
2927void wxRichTextCtrl::SetSelection(long from, long to)
2928{
2929 // if from and to are both -1, it means (in wxWidgets) that all text should
2930 // be selected.
2931 if ( (from == -1) && (to == -1) )
2932 {
2933 from = 0;
96c9f0f6 2934 to = GetLastPosition()+1;
5d7836c4
JS
2935 }
2936
c1aec196
JS
2937 if (from == to)
2938 {
2939 SelectNone();
2940 }
2941 else
2942 {
603f702b
JS
2943 wxRichTextSelection oldSelection = m_selection;
2944
205a1ad9 2945 m_selectionAnchor = from-1;
603f702b
JS
2946 m_selectionAnchorObject = NULL;
2947 m_selection.Set(wxRichTextRange(from, to-1), GetFocusObject());
76d7fc29 2948
a216dfa2 2949 m_caretPosition = wxMax(-1, to-1);
0ca07313 2950
603f702b 2951 RefreshForSelectionChange(oldSelection, m_selection);
c1aec196
JS
2952 PositionCaret();
2953 }
5d7836c4
JS
2954}
2955
2956// ----------------------------------------------------------------------------
2957// Editing
2958// ----------------------------------------------------------------------------
2959
e3bb4c54 2960void wxRichTextCtrl::Replace(long from, long to,
343ae70d 2961 const wxString& value)
5d7836c4
JS
2962{
2963 BeginBatchUndo(_("Replace"));
2964
e3bb4c54
JS
2965 SetSelection(from, to);
2966
2967 wxRichTextAttr attr = GetDefaultStyle();
2968
5d7836c4
JS
2969 DeleteSelectedContent();
2970
e3bb4c54
JS
2971 SetDefaultStyle(attr);
2972
343ae70d 2973 DoWriteText(value, SetValue_SelectionOnly);
5d7836c4
JS
2974
2975 EndBatchUndo();
2976}
2977
2978void wxRichTextCtrl::Remove(long from, long to)
2979{
2980 SelectNone();
2981
603f702b 2982 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from, to-1), this, & GetBuffer());
5d7836c4 2983
2f36e8dc 2984 LayoutContent();
5d7836c4 2985 if (!IsFrozen())
76bcd815 2986 Refresh(false);
5d7836c4
JS
2987}
2988
2989bool wxRichTextCtrl::IsModified() const
2990{
2991 return m_buffer.IsModified();
2992}
2993
2994void wxRichTextCtrl::MarkDirty()
2995{
2996 m_buffer.Modify(true);
2997}
2998
2999void wxRichTextCtrl::DiscardEdits()
3000{
ab14c7aa 3001 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
3002 m_buffer.Modify(false);
3003 m_buffer.GetCommandProcessor()->ClearCommands();
3004}
3005
3006int wxRichTextCtrl::GetNumberOfLines() const
3007{
603f702b 3008 return GetFocusObject()->GetParagraphCount();
5d7836c4
JS
3009}
3010
3011// ----------------------------------------------------------------------------
3012// Positions <-> coords
3013// ----------------------------------------------------------------------------
3014
3015long wxRichTextCtrl::XYToPosition(long x, long y) const
3016{
603f702b 3017 return GetFocusObject()->XYToPosition(x, y);
5d7836c4
JS
3018}
3019
3020bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
3021{
603f702b 3022 return GetFocusObject()->PositionToXY(pos, x, y);
5d7836c4
JS
3023}
3024
3025// ----------------------------------------------------------------------------
3026//
3027// ----------------------------------------------------------------------------
3028
3029void wxRichTextCtrl::ShowPosition(long pos)
3030{
3031 if (!IsPositionVisible(pos))
3032 ScrollIntoView(pos-1, WXK_DOWN);
3033}
3034
3035int wxRichTextCtrl::GetLineLength(long lineNo) const
3036{
603f702b 3037 return GetFocusObject()->GetParagraphLength(lineNo);
5d7836c4
JS
3038}
3039
3040wxString wxRichTextCtrl::GetLineText(long lineNo) const
3041{
603f702b 3042 return GetFocusObject()->GetParagraphText(lineNo);
5d7836c4
JS
3043}
3044
3045// ----------------------------------------------------------------------------
3046// Undo/redo
3047// ----------------------------------------------------------------------------
3048
3049void wxRichTextCtrl::Undo()
3050{
3051 if (CanUndo())
3052 {
3053 GetCommandProcessor()->Undo();
3054 }
3055}
3056
3057void wxRichTextCtrl::Redo()
3058{
3059 if (CanRedo())
3060 {
3061 GetCommandProcessor()->Redo();
3062 }
3063}
3064
3065bool wxRichTextCtrl::CanUndo() const
3066{
3a4a38c1 3067 return GetCommandProcessor()->CanUndo() && IsEditable();
5d7836c4
JS
3068}
3069
3070bool wxRichTextCtrl::CanRedo() const
3071{
3a4a38c1 3072 return GetCommandProcessor()->CanRedo() && IsEditable();
5d7836c4
JS
3073}
3074
3075// ----------------------------------------------------------------------------
563d25ba 3076// implementation details
5d7836c4
JS
3077// ----------------------------------------------------------------------------
3078
563d25ba 3079void wxRichTextCtrl::Command(wxCommandEvent& event)
5d7836c4
JS
3080{
3081 SetValue(event.GetString());
3082 GetEventHandler()->ProcessEvent(event);
3083}
3084
3085void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
3086{
3087 // By default, load the first file into the text window.
3088 if (event.GetNumberOfFiles() > 0)
3089 {
3090 LoadFile(event.GetFiles()[0]);
3091 }
3092}
3093
5d7836c4
JS
3094wxSize wxRichTextCtrl::DoGetBestSize() const
3095{
3096 return wxSize(10, 10);
3097}
3098
3099// ----------------------------------------------------------------------------
3100// standard handlers for standard edit menu events
3101// ----------------------------------------------------------------------------
3102
3103void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
3104{
3105 Cut();
3106}
3107
3108void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
3109{
3110 DeleteSelection();
3111}
3112
3113void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
3114{
3115 Copy();
3116}
3117
3118void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
3119{
3120 Paste();
3121}
3122
3123void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
3124{
3125 Undo();
3126}
3127
3128void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
3129{
3130 Redo();
3131}
3132
3133void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
3134{
3135 event.Enable( CanCut() );
3136}
3137
3138void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
3139{
3140 event.Enable( CanCopy() );
3141}
3142
3143void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
3144{
3145 event.Enable( CanDeleteSelection() );
3146}
3147
3148void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
3149{
3150 event.Enable( CanPaste() );
3151}
3152
3153void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
3154{
3155 event.Enable( CanUndo() );
3156 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3157}
3158
3159void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
3160{
3161 event.Enable( CanRedo() );
3162 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3163}
3164
3165void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
3166{
b30661fd
JS
3167 if (GetLastPosition() > 0)
3168 SelectAll();
5d7836c4
JS
3169}
3170
3171void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
3172{
b30661fd 3173 event.Enable(GetLastPosition() > 0);
5d7836c4
JS
3174}
3175
603f702b 3176void wxRichTextCtrl::OnProperties(wxCommandEvent& event)
cdaed652 3177{
603f702b
JS
3178 int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1;
3179 if (idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount())
3180 {
3181 wxRichTextObject* obj = m_contextMenuPropertiesInfo.GetObject(idx);
3182 if (obj && obj->CanEditProperties())
3183 obj->EditProperties(this, & GetBuffer());
3184
3185 m_contextMenuPropertiesInfo.Clear();
3186 }
cdaed652
VZ
3187}
3188
603f702b 3189void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent& event)
cdaed652 3190{
603f702b
JS
3191 int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1;
3192 event.Enable(idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount());
cdaed652
VZ
3193}
3194
b4ae0def 3195void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event)
5d7836c4 3196{
b4ae0def
JS
3197 if (event.GetEventObject() != this)
3198 {
3199 event.Skip();
3200 return;
3201 }
3202
cdaed652
VZ
3203 wxClientDC dc(this);
3204 PrepareDC(dc);
3205 dc.SetFont(GetFont());
3206
3207 long position = 0;
3208 wxPoint pt = event.GetPosition();
3209 wxPoint logicalPt = GetLogicalPoint(ScreenToClient(pt));
603f702b
JS
3210 wxRichTextObject* hitObj = NULL;
3211 wxRichTextObject* contextObj = NULL;
3212 int hit = GetFocusObject()->HitTest(dc, logicalPt, position, & hitObj, & contextObj);
4fe83b93 3213
603f702b 3214 m_contextMenuPropertiesInfo.Clear();
4fe83b93 3215
cdaed652
VZ
3216 if (hit == wxRICHTEXT_HITTEST_ON || hit == wxRICHTEXT_HITTEST_BEFORE || hit == wxRICHTEXT_HITTEST_AFTER)
3217 {
603f702b
JS
3218 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
3219 if (hitObj && actualContainer)
3220 {
3221 if (actualContainer->AcceptsFocus())
3222 {
3223 SetFocusObject(actualContainer, false /* don't set caret position yet */);
3224 SetCaretPositionAfterClick(actualContainer, position, hit);
3225 }
4fe83b93 3226
603f702b
JS
3227 m_contextMenuPropertiesInfo.AddItems(actualContainer, hitObj);
3228 }
3229 else
3230 m_contextMenuPropertiesInfo.AddItems(GetFocusObject(), NULL);
cdaed652
VZ
3231 }
3232 else
3233 {
603f702b 3234 m_contextMenuPropertiesInfo.AddItems(GetFocusObject(), NULL);
cdaed652
VZ
3235 }
3236
8e43fc77 3237 if (m_contextMenu)
603f702b
JS
3238 {
3239 m_contextMenuPropertiesInfo.AddMenuItems(m_contextMenu);
8e43fc77 3240 PopupMenu(m_contextMenu);
603f702b 3241 }
5d7836c4
JS
3242}
3243
27e20452
JS
3244bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
3245{
603f702b 3246 return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), wxRichTextAttr(style));
24777478
JS
3247}
3248
3249bool wxRichTextCtrl::SetStyle(long start, long end, const wxRichTextAttr& style)
3250{
603f702b 3251 return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), style);
27e20452
JS
3252}
3253
44cc96a8 3254bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style)
24777478 3255{
603f702b 3256 return GetFocusObject()->SetStyle(range.ToInternal(), wxRichTextAttr(style));
24777478
JS
3257}
3258
3259bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style)
5d7836c4 3260{
603f702b 3261 return GetFocusObject()->SetStyle(range.ToInternal(), style);
5d7836c4
JS
3262}
3263
603f702b 3264void wxRichTextCtrl::SetStyle(wxRichTextObject *obj, const wxRichTextAttr& textAttr)
cdaed652 3265{
603f702b 3266 GetFocusObject()->SetStyle(obj, textAttr);
cdaed652
VZ
3267}
3268
3e3a754f
JS
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.
3e3a754f 3272
24777478 3273bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxRichTextAttr& style, int flags)
3e3a754f 3274{
603f702b 3275 return GetFocusObject()->SetStyle(range.ToInternal(), style, flags);
3e3a754f
JS
3276}
3277
27e20452
JS
3278bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
3279{
24777478 3280 return GetBuffer().SetDefaultStyle(style);
5d7836c4
JS
3281}
3282
24777478
JS
3283bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr& style)
3284{
603f702b
JS
3285 wxRichTextAttr attr1(style);
3286 attr1.GetTextBoxAttr().Reset();
3287 return GetBuffer().SetDefaultStyle(attr1);
24777478
JS
3288}
3289
3290const wxRichTextAttr& wxRichTextCtrl::GetDefaultStyleEx() const
27e20452 3291{
61399247 3292 return GetBuffer().GetDefaultStyle();
27e20452
JS
3293}
3294
3966a9f4 3295bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
24777478
JS
3296{
3297 wxRichTextAttr attr;
603f702b 3298 if (GetFocusObject()->GetStyle(position, attr))
24777478
JS
3299 {
3300 style = attr;
3301 return true;
3302 }
3303 else
3304 return false;
3305}
3306
3307bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style)
5d7836c4 3308{
603f702b
JS
3309 return GetFocusObject()->GetStyle(position, style);
3310}
3311
3312bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container)
3313{
3314 wxRichTextAttr attr;
3315 if (container->GetStyle(position, attr))
3316 {
3317 style = attr;
3318 return true;
3319 }
3320 else
3321 return false;
5d7836c4
JS
3322}
3323
a7ed48a5 3324// get the common set of styles for the range
44cc96a8 3325bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style)
24777478
JS
3326{
3327 wxRichTextAttr attr;
603f702b 3328 if (GetFocusObject()->GetStyleForRange(range.ToInternal(), attr))
24777478
JS
3329 {
3330 style = attr;
3331 return true;
3332 }
3333 else
3334 return false;
3335}
3336
3337bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style)
a7ed48a5 3338{
603f702b
JS
3339 return GetFocusObject()->GetStyleForRange(range.ToInternal(), style);
3340}
3341
3342bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container)
3343{
3344 return container->GetStyleForRange(range.ToInternal(), style);
a7ed48a5
JS
3345}
3346
fe5aa22c 3347/// Get the content (uncombined) attributes for this position.
24777478 3348bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style)
fe5aa22c 3349{
603f702b
JS
3350 return GetFocusObject()->GetUncombinedStyle(position, style);
3351}
3352
3353/// Get the content (uncombined) attributes for this position.
3354bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container)
3355{
3356 return container->GetUncombinedStyle(position, style);
fe5aa22c
JS
3357}
3358
5d7836c4
JS
3359/// Set font, and also the buffer attributes
3360bool wxRichTextCtrl::SetFont(const wxFont& font)
3361{
24777478 3362 wxControl::SetFont(font);
5d7836c4 3363
24777478 3364 wxRichTextAttr attr = GetBuffer().GetAttributes();
5d7836c4
JS
3365 attr.SetFont(font);
3366 GetBuffer().SetBasicStyle(attr);
fe5aa22c 3367
fe5aa22c
JS
3368 GetBuffer().Invalidate(wxRICHTEXT_ALL);
3369 Refresh(false);
5d7836c4
JS
3370
3371 return true;
3372}
3373
4d551ad5
JS
3374/// Transform logical to physical
3375wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
5d7836c4
JS
3376{
3377 wxPoint pt;
3378 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
3379
3380 return pt;
3381}
3382
3383/// Transform physical to logical
4d551ad5 3384wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
5d7836c4
JS
3385{
3386 wxPoint pt;
3387 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
3388
3389 return pt;
3390}
3391
3392/// Position the caret
603f702b 3393void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox* container)
5d7836c4 3394{
c59f6793
JS
3395 if (!GetCaret())
3396 return;
3397
3398 //wxLogDebug(wxT("PositionCaret"));
3399
5d7836c4 3400 wxRect caretRect;
603f702b 3401 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect, container))
5d7836c4 3402 {
7e4fe193
JS
3403 wxPoint newPt = caretRect.GetPosition();
3404 wxSize newSz = caretRect.GetSize();
3405 wxPoint pt = GetPhysicalPoint(newPt);
3406 if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz)
c59f6793 3407 {
1c13f06e
JS
3408 GetCaret()->Hide();
3409 if (GetCaret()->GetSize() != newSz)
3410 GetCaret()->SetSize(newSz);
e9f10004 3411
423b9ba5
JS
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())))
603f702b
JS
3414 {
3415 pt.x = -200;
e9f10004 3416 pt.y = -200;
603f702b 3417 }
423b9ba5
JS
3418 else if (pt.y < GetBuffer().GetTopMargin() && (pt.y + newSz.y) > GetBuffer().GetTopMargin())
3419 {
3420 newSz.y -= (GetBuffer().GetTopMargin() - pt.y);
3421 if (newSz.y > 0)
3422 {
3423 pt.y = GetBuffer().GetTopMargin();
3424 GetCaret()->SetSize(newSz);
3425 }
3426 }
3427 else if (pt.y < (GetClientSize().y - GetBuffer().GetBottomMargin()) && (pt.y + newSz.y) > (GetClientSize().y - GetBuffer().GetBottomMargin()))
3428 {
3429 newSz.y = GetClientSize().y - GetBuffer().GetBottomMargin() - pt.y;
3430 GetCaret()->SetSize(newSz);
3431 }
e9f10004 3432
c59f6793 3433 GetCaret()->Move(pt);
1c13f06e 3434 GetCaret()->Show();
c59f6793 3435 }
5d7836c4
JS
3436 }
3437}
3438
3439/// Get the caret height and position for the given character position
603f702b 3440bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect, wxRichTextParagraphLayoutBox* container)
5d7836c4
JS
3441{
3442 wxClientDC dc(this);
3443 dc.SetFont(GetFont());
7fe8059f 3444
5d7836c4
JS
3445 PrepareDC(dc);
3446
3447 wxPoint pt;
3448 int height = 0;
4fe83b93 3449
603f702b
JS
3450 if (!container)
3451 container = GetFocusObject();
5d7836c4 3452
603f702b 3453 if (container->FindPosition(dc, position, pt, & height, m_caretAtLineStart))
5d7836c4 3454 {
88bee7e6
JS
3455 // Caret height can't be zero
3456 if (height == 0)
3457 height = dc.GetCharHeight();
3458
5d7836c4
JS
3459 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
3460 return true;
3461 }
41e155b4
WS
3462
3463 return false;
5d7836c4
JS
3464}
3465
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.
3470wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
3471{
603f702b
JS
3472 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(caretPosition, true);
3473 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPosition, true);
5d7836c4
JS
3474 if (line)
3475 {
1e967276
JS
3476 wxRichTextRange lineRange = line->GetAbsoluteRange();
3477 if (caretPosition == lineRange.GetStart()-1 &&
3478 (para->GetRange().GetStart() != lineRange.GetStart()))
5d7836c4
JS
3479 {
3480 if (!m_caretAtLineStart)
603f702b 3481 line = GetFocusObject()->GetLineAtPosition(caretPosition-1, true);
5d7836c4
JS
3482 }
3483 }
3484 return line;
3485}
3486
3487
3488/// Move the caret to the given character position
603f702b 3489bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart, wxRichTextParagraphLayoutBox* container)
5d7836c4 3490{
603f702b 3491 if (GetBuffer().IsDirty())
2f36e8dc 3492 LayoutContent();
5d7836c4 3493
603f702b
JS
3494 if (!container)
3495 container = GetFocusObject();
3496
3497 if (pos <= container->GetOwnRange().GetEnd())
5d7836c4
JS
3498 {
3499 SetCaretPosition(pos, showAtLineStart);
3500
603f702b 3501 PositionCaret(container);
5d7836c4
JS
3502
3503 return true;
3504 }
3505 else
3506 return false;
3507}
3508
3509/// Layout the buffer: which we must do before certain operations, such as
3510/// setting the caret position.
2f36e8dc 3511bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
5d7836c4 3512{
603f702b 3513 if (GetBuffer().IsDirty() || onlyVisibleRect)
1e967276
JS
3514 {
3515 wxRect availableSpace(GetClientSize());
3516 if (availableSpace.width == 0)
3517 availableSpace.width = 10;
3518 if (availableSpace.height == 0)
3519 availableSpace.height = 10;
4d551ad5
JS
3520
3521 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
3522 if (onlyVisibleRect)
3523 {
3524 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
3525 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3526 }
41e155b4 3527
1e967276
JS
3528 wxClientDC dc(this);
3529 dc.SetFont(GetFont());
41e155b4 3530
1e967276 3531 PrepareDC(dc);
41e155b4 3532
1e967276
JS
3533 GetBuffer().Defragment();
3534 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
4d551ad5 3535 GetBuffer().Layout(dc, availableSpace, flags);
603f702b 3536 GetBuffer().Invalidate(wxRICHTEXT_NONE);
41e155b4 3537
1e967276
JS
3538 if (!IsFrozen())
3539 SetupScrollbars();
3540 }
5d7836c4
JS
3541
3542 return true;
3543}
3544
75936ec6 3545/// Is all of the selection, or the current caret position, bold?
3966a9f4 3546bool wxRichTextCtrl::IsSelectionBold()
5d7836c4
JS
3547{
3548 if (HasSelection())
3549 {
24777478 3550 wxRichTextAttr attr;
a7ed48a5 3551 wxRichTextRange range = GetSelectionRange();
5d7836c4 3552 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
7d76fbd5 3553 attr.SetFontWeight(wxFONTWEIGHT_BOLD);
7fe8059f 3554
5d7836c4
JS
3555 return HasCharacterAttributes(range, attr);
3556 }
3557 else
3558 {
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.
24777478 3561 wxRichTextAttr attr;
5d7836c4 3562 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
ab14c7aa
JS
3563
3564 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3565 if (GetStyle(pos, attr))
5d7836c4 3566 {
ab14c7aa
JS
3567 if (IsDefaultStyleShowing())
3568 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
7d76fbd5 3569 return attr.GetFontWeight() == wxFONTWEIGHT_BOLD;
5d7836c4
JS
3570 }
3571 }
3572 return false;
3573}
3574
75936ec6 3575/// Is all of the selection, or the current caret position, italics?
3966a9f4 3576bool wxRichTextCtrl::IsSelectionItalics()
5d7836c4
JS
3577{
3578 if (HasSelection())
3579 {
a7ed48a5 3580 wxRichTextRange range = GetSelectionRange();
24777478 3581 wxRichTextAttr attr;
5d7836c4 3582 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
7d76fbd5 3583 attr.SetFontStyle(wxFONTSTYLE_ITALIC);
7fe8059f 3584
5d7836c4
JS
3585 return HasCharacterAttributes(range, attr);
3586 }
3587 else
3588 {
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.
24777478 3591 wxRichTextAttr attr;
5d7836c4 3592 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
ab14c7aa
JS
3593
3594 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3595 if (GetStyle(pos, attr))
5d7836c4 3596 {
ab14c7aa
JS
3597 if (IsDefaultStyleShowing())
3598 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
7d76fbd5 3599 return attr.GetFontStyle() == wxFONTSTYLE_ITALIC;
5d7836c4
JS
3600 }
3601 }
3602 return false;
3603}
3604
75936ec6 3605/// Is all of the selection, or the current caret position, underlined?
3966a9f4 3606bool wxRichTextCtrl::IsSelectionUnderlined()
5d7836c4
JS
3607{
3608 if (HasSelection())
3609 {
a7ed48a5 3610 wxRichTextRange range = GetSelectionRange();
24777478 3611 wxRichTextAttr attr;
5d7836c4
JS
3612 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3613 attr.SetFontUnderlined(true);
7fe8059f 3614
5d7836c4
JS
3615 return HasCharacterAttributes(range, attr);
3616 }
3617 else
3618 {
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.
24777478 3621 wxRichTextAttr attr;
5d7836c4 3622 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
ab14c7aa 3623 long pos = GetAdjustedCaretPosition(GetCaretPosition());
fe5aa22c 3624
ab14c7aa 3625 if (GetStyle(pos, attr))
5d7836c4 3626 {
ab14c7aa
JS
3627 if (IsDefaultStyleShowing())
3628 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
5d7836c4
JS
3629 return attr.GetFontUnderlined();
3630 }
3631 }
3632 return false;
3633}
3634
75936ec6
JS
3635/// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3636bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag)
3637{
3638 wxRichTextAttr attr;
3639 attr.SetFlags(wxTEXT_ATTR_EFFECTS);
3640 attr.SetTextEffectFlags(flag);
3641 attr.SetTextEffects(flag);
3642
3643 if (HasSelection())
3644 {
3645 return HasCharacterAttributes(GetSelectionRange(), attr);
3646 }
3647 else
3648 {
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))
3653 {
3654 if (IsDefaultStyleShowing())
3655 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
dabf7342 3656 return (attr.GetTextEffectFlags() & flag) != 0;
75936ec6
JS
3657 }
3658 }
3659 return false;
3660}
3661
5d7836c4
JS
3662/// Apply bold to the selection
3663bool wxRichTextCtrl::ApplyBoldToSelection()
3664{
24777478 3665 wxRichTextAttr attr;
5d7836c4 3666 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
7d76fbd5 3667 attr.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL : wxFONTWEIGHT_BOLD);
5d7836c4
JS
3668
3669 if (HasSelection())
3e3a754f 3670 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 3671 else
063dbace
JS
3672 {
3673 wxRichTextAttr current = GetDefaultStyleEx();
3674 current.Apply(attr);
3675 SetAndShowDefaultStyle(current);
3676 }
5d7836c4
JS
3677 return true;
3678}
3679
3680/// Apply italic to the selection
3681bool wxRichTextCtrl::ApplyItalicToSelection()
3682{
24777478 3683 wxRichTextAttr attr;
5d7836c4 3684 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
7d76fbd5 3685 attr.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL : wxFONTSTYLE_ITALIC);
5d7836c4
JS
3686
3687 if (HasSelection())
3e3a754f 3688 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 3689 else
063dbace
JS
3690 {
3691 wxRichTextAttr current = GetDefaultStyleEx();
3692 current.Apply(attr);
3693 SetAndShowDefaultStyle(current);
3694 }
5d7836c4
JS
3695 return true;
3696}
3697
3698/// Apply underline to the selection
3699bool wxRichTextCtrl::ApplyUnderlineToSelection()
3700{
24777478 3701 wxRichTextAttr attr;
5d7836c4 3702 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
4d551ad5 3703 attr.SetFontUnderlined(!IsSelectionUnderlined());
5d7836c4 3704
75936ec6
JS
3705 if (HasSelection())
3706 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3707 else
3708 {
3709 wxRichTextAttr current = GetDefaultStyleEx();
3710 current.Apply(attr);
3711 SetAndShowDefaultStyle(current);
3712 }
3713 return true;
3714}
3715
3716/// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
3717bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags)
3718{
3719 wxRichTextAttr attr;
3720 attr.SetFlags(wxTEXT_ATTR_EFFECTS);
3721 attr.SetTextEffectFlags(flags);
3722 if (!DoesSelectionHaveTextEffectFlag(flags))
3723 attr.SetTextEffects(flags);
3724 else
3725 attr.SetTextEffects(attr.GetTextEffectFlags() & ~flags);
3726
5d7836c4 3727 if (HasSelection())
3e3a754f 3728 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 3729 else
063dbace
JS
3730 {
3731 wxRichTextAttr current = GetDefaultStyleEx();
3732 current.Apply(attr);
3733 SetAndShowDefaultStyle(current);
3734 }
5d7836c4
JS
3735 return true;
3736}
3737
3738/// Is all of the selection aligned according to the specified flag?
3966a9f4 3739bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
5d7836c4 3740{
0ca07313 3741 wxRichTextRange range;
5d7836c4 3742 if (HasSelection())
a7ed48a5 3743 range = GetSelectionRange();
5d7836c4 3744 else
a7ed48a5 3745 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
0ca07313 3746
24777478 3747 wxRichTextAttr attr;
0ca07313
JS
3748 attr.SetAlignment(alignment);
3749
3750 return HasParagraphAttributes(range, attr);
5d7836c4
JS
3751}
3752
3753/// Apply alignment to the selection
3754bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
3755{
24777478 3756 wxRichTextAttr attr;
5d7836c4
JS
3757 attr.SetAlignment(alignment);
3758 if (HasSelection())
3759 return SetStyle(GetSelectionRange(), attr);
3760 else
3761 {
603f702b 3762 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
5d7836c4 3763 if (para)
3e3a754f 3764 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
5d7836c4
JS
3765 }
3766 return true;
3767}
3768
ab14c7aa 3769/// Apply a named style to the selection
1807a1f3 3770bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
ab14c7aa
JS
3771{
3772 // Flags are defined within each definition, so only certain
3773 // attributes are applied.
24777478 3774 wxRichTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
1807a1f3 3775
f2ab7bf3 3776 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET;
ab14c7aa 3777
38f833b1
JS
3778 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
3779 {
3780 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
41a85215 3781
38f833b1 3782 wxRichTextRange range;
41a85215 3783
38f833b1
JS
3784 if (HasSelection())
3785 range = GetSelectionRange();
3786 else
3787 {
41a85215 3788 long pos = GetAdjustedCaretPosition(GetCaretPosition());
38f833b1
JS
3789 range = wxRichTextRange(pos, pos+1);
3790 }
41a85215
WS
3791
3792 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
3793 }
3794
edae63b0
JS
3795 bool isPara = false;
3796
ab14c7aa
JS
3797 // Make sure the attr has the style name
3798 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
3e3a754f 3799 {
edae63b0 3800 isPara = true;
ab14c7aa 3801 attr.SetParagraphStyleName(def->GetName());
1807a1f3 3802
3e3a754f 3803 // If applying a paragraph style, we only want the paragraph nodes to adopt these
38f833b1 3804 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3e3a754f
JS
3805 // to change its style independently.
3806 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3807 }
ab14c7aa
JS
3808 else
3809 attr.SetCharacterStyleName(def->GetName());
3810
3811 if (HasSelection())
1807a1f3 3812 return SetStyleEx(GetSelectionRange(), attr, flags);
ab14c7aa 3813 else
1807a1f3 3814 {
24777478
JS
3815 wxRichTextAttr current = GetDefaultStyleEx();
3816 wxRichTextAttr defaultStyle(attr);
edae63b0
JS
3817 if (isPara)
3818 {
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);
3822 }
3823 current.Apply(defaultStyle);
063dbace 3824 SetAndShowDefaultStyle(current);
edae63b0
JS
3825
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.
3828 if (isPara)
3829 {
3830 long pos = GetAdjustedCaretPosition(GetCaretPosition());
603f702b 3831 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos);
edae63b0
JS
3832 if (para)
3833 {
3834 return SetStyleEx(para->GetRange().FromInternal(), attr, flags);
3835 }
3836 }
1807a1f3
JS
3837 return true;
3838 }
ab14c7aa
JS
3839}
3840
fe5aa22c
JS
3841/// Apply the style sheet to the buffer, for example if the styles have changed.
3842bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
3843{
3844 if (!styleSheet)
3845 styleSheet = GetBuffer().GetStyleSheet();
3846 if (!styleSheet)
3847 return false;
3848
3849 if (GetBuffer().ApplyStyleSheet(styleSheet))
3850 {
3851 GetBuffer().Invalidate(wxRICHTEXT_ALL);
3852 Refresh(false);
3853 return true;
3854 }
3855 else
3856 return false;
3857}
3858
5d7836c4
JS
3859/// Sets the default style to the style under the cursor
3860bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3861{
24777478 3862 wxRichTextAttr attr;
5d7836c4
JS
3863 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
3864
ab14c7aa
JS
3865 // If at the start of a paragraph, use the next position.
3866 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3867
fe5aa22c 3868 if (GetUncombinedStyle(pos, attr))
5d7836c4
JS
3869 {
3870 SetDefaultStyle(attr);
3871 return true;
3872 }
41e155b4
WS
3873
3874 return false;
5d7836c4
JS
3875}
3876
4d551ad5
JS
3877/// Returns the first visible position in the current view
3878long wxRichTextCtrl::GetFirstVisiblePosition() const
3879{
603f702b 3880 wxRichTextLine* line = GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
4d551ad5
JS
3881 if (line)
3882 return line->GetAbsoluteRange().GetStart();
3883 else
3884 return 0;
3885}
3886
ea160b2e
JS
3887/// Get the first visible point in the window
3888wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const
3889{
3890 int ppuX, ppuY;
3891 int startXUnits, startYUnits;
3892
3893 GetScrollPixelsPerUnit(& ppuX, & ppuY);
3894 GetViewStart(& startXUnits, & startYUnits);
3895
3896 return wxPoint(startXUnits * ppuX, startYUnits * ppuY);
3897}
3898
ab14c7aa
JS
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.
3902long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
3903{
603f702b 3904 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPos+1);
ab14c7aa
JS
3905
3906 if (para && (caretPos+1 == para->GetRange().GetStart()))
3907 caretPos ++;
3908 return caretPos;
3909}
3910
96c9f0f6
JS
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
3913/// by (n, n+1)
3914wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
3915{
3916 wxRichTextRange range = GetInternalSelectionRange();
3917 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
3918 range.SetEnd(range.GetEnd() + 1);
3919 return range;
3920}
3921
3922void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
3923{
76d7fc29 3924 SetSelection(range.GetStart(), range.GetEnd());
96c9f0f6
JS
3925}
3926
38f833b1
JS
3927/// Set list style
3928bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3929{
603f702b 3930 return GetFocusObject()->SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel);
38f833b1
JS
3931}
3932
3933bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3934{
603f702b 3935 return GetFocusObject()->SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
38f833b1
JS
3936}
3937
3938/// Clear list for given range
3939bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags)
3940{
603f702b 3941 return GetFocusObject()->ClearListStyle(range.ToInternal(), flags);
38f833b1
JS
3942}
3943
3944/// Number/renumber any list elements in the given range
3945bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3946{
603f702b 3947 return GetFocusObject()->NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
38f833b1
JS
3948}
3949
3950bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3951{
603f702b 3952 return GetFocusObject()->NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
38f833b1
JS
3953}
3954
3955/// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3956bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
3957{
603f702b 3958 return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel);
38f833b1
JS
3959}
3960
3961bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
3962{
603f702b 3963 return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel);
38f833b1
JS
3964}
3965
12cc29c5
JS
3966/// Deletes the content in the given range
3967bool wxRichTextCtrl::Delete(const wxRichTextRange& range)
3968{
603f702b 3969 return GetFocusObject()->DeleteRangeWithUndo(range.ToInternal(), this, & GetBuffer());
12cc29c5
JS
3970}
3971
dadd4f55
JS
3972const wxArrayString& wxRichTextCtrl::GetAvailableFontNames()
3973{
3974 if (sm_availableFontNames.GetCount() == 0)
3975 {
3976 sm_availableFontNames = wxFontEnumerator::GetFacenames();
3977 sm_availableFontNames.Sort();
3978 }
3979 return sm_availableFontNames;
3980}
3981
3982void wxRichTextCtrl::ClearAvailableFontNames()
3983{
3984 sm_availableFontNames.Clear();
3985}
3986
c0a5145a 3987void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
52170c5b
JS
3988{
3989 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3990
3991 wxTextAttrEx basicStyle = GetBasicStyle();
3992 basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3993 SetBasicStyle(basicStyle);
3994 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3995
3996 Refresh();
3997}
3998
7051fa41 3999// Refresh the area affected by a selection change
603f702b
JS
4000bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection& oldSelection, const wxRichTextSelection& newSelection)
4001{
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)))
4008 {
4009 Refresh(false);
4010 return true;
4011 }
4fe83b93 4012
603f702b
JS
4013 wxRichTextRange oldRange, newRange;
4014 if (oldSelection.IsValid())
4015 oldRange = oldSelection.GetRange();
4016 else
4017 oldRange = wxRICHTEXT_NO_SELECTION;
4018 if (newSelection.IsValid())
4019 newRange = newSelection.GetRange();
4020 else
4021 newRange = wxRICHTEXT_NO_SELECTION;
4fe83b93 4022
7051fa41
JS
4023 // Calculate the refresh rectangle - just the affected lines
4024 long firstPos, lastPos;
603f702b 4025 if (oldRange.GetStart() == -2 && newRange.GetStart() != -2)
7051fa41 4026 {
603f702b
JS
4027 firstPos = newRange.GetStart();
4028 lastPos = newRange.GetEnd();
7051fa41 4029 }
603f702b 4030 else if (oldRange.GetStart() != -2 && newRange.GetStart() == -2)
7051fa41 4031 {
603f702b
JS
4032 firstPos = oldRange.GetStart();
4033 lastPos = oldRange.GetEnd();
7051fa41 4034 }
603f702b 4035 else if (oldRange.GetStart() == -2 && newRange.GetStart() == -2)
7051fa41
JS
4036 {
4037 return false;
4038 }
4039 else
4040 {
603f702b
JS
4041 firstPos = wxMin(oldRange.GetStart(), newRange.GetStart());
4042 lastPos = wxMax(oldRange.GetEnd(), newRange.GetEnd());
7051fa41
JS
4043 }
4044
603f702b
JS
4045 wxRichTextLine* firstLine = GetFocusObject()->GetLineAtPosition(firstPos);
4046 wxRichTextLine* lastLine = GetFocusObject()->GetLineAtPosition(lastPos);
7051fa41
JS
4047
4048 if (firstLine && lastLine)
4049 {
4050 wxSize clientSize = GetClientSize();
4051 wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition());
4052 wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y);
4053
4054 pt1.x = 0;
4055 pt1.y = wxMax(0, pt1.y);
4056 pt2.x = 0;
4057 pt2.y = wxMin(clientSize.y, pt2.y);
4058
4059 wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y));
4060 RefreshRect(rect, false);
4061 }
4062 else
4063 Refresh(false);
4064
4065 return true;
4066}
4067
603f702b
JS
4068// margins functions
4069bool wxRichTextCtrl::DoSetMargins(const wxPoint& pt)
4070{
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);
4fe83b93 4075
603f702b
JS
4076 return true;
4077}
4078
4079wxPoint wxRichTextCtrl::DoGetMargins() const
4080{
4081 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4082 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4083}
4084
4085bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox* obj, bool setCaretPosition)
4086{
4087 if (obj && !obj->AcceptsFocus())
4088 return false;
4089
4090 wxRichTextParagraphLayoutBox* oldContainer = GetFocusObject();
4091 bool changingContainer = (m_focusObject != obj);
4092
4093 m_focusObject = obj;
4fe83b93 4094
603f702b
JS
4095 if (!obj)
4096 m_focusObject = & m_buffer;
4097
4098 if (setCaretPosition && changingContainer)
4099 {
4100 m_selection.Reset();
4101 m_selectionAnchor = -2;
4102 m_selectionAnchorObject = NULL;
4103 m_selectionState = wxRichTextCtrlSelectionState_Normal;
4104
4105 long pos = -1;
4fe83b93 4106
603f702b
JS
4107 m_caretAtLineStart = false;
4108 MoveCaret(pos, m_caretAtLineStart);
4109 SetDefaultStyleToCursorStyle();
4110
4111 wxRichTextEvent cmdEvent(
4112 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED,
4113 GetId());
4114 cmdEvent.SetEventObject(this);
4115 cmdEvent.SetPosition(m_caretPosition+1);
4116 cmdEvent.SetOldContainer(oldContainer);
4117 cmdEvent.SetContainer(m_focusObject);
4118
4119 GetEventHandler()->ProcessEvent(cmdEvent);
4120 }
4121 return true;
4122}
4123
1c13f06e
JS
4124#if wxRICHTEXT_USE_OWN_CARET
4125
4126// ----------------------------------------------------------------------------
4127// initialization and destruction
4128// ----------------------------------------------------------------------------
4129
4130void wxRichTextCaret::Init()
4131{
4132 m_hasFocus = true;
4133
4134 m_xOld =
4135 m_yOld = -1;
4136 m_richTextCtrl = NULL;
4137 m_needsUpdate = false;
a188ac29 4138 m_flashOn = true;
1c13f06e
JS
4139}
4140
4141wxRichTextCaret::~wxRichTextCaret()
4142{
a188ac29
JS
4143 if (m_timer.IsRunning())
4144 m_timer.Stop();
1c13f06e
JS
4145}
4146
4147// ----------------------------------------------------------------------------
4148// showing/hiding/moving the caret (base class interface)
4149// ----------------------------------------------------------------------------
4150
4151void wxRichTextCaret::DoShow()
4152{
a188ac29 4153 m_flashOn = true;
ab52bac8 4154
a188ac29
JS
4155 if (!m_timer.IsRunning())
4156 m_timer.Start(GetBlinkTime());
4157
1c13f06e
JS
4158 Refresh();
4159}
4160
4161void wxRichTextCaret::DoHide()
4162{
a188ac29
JS
4163 if (m_timer.IsRunning())
4164 m_timer.Stop();
4165
1c13f06e
JS
4166 Refresh();
4167}
4168
4169void wxRichTextCaret::DoMove()
4170{
4171 if (IsVisible())
4172 {
4173 Refresh();
4174
4175 if (m_xOld != -1 && m_yOld != -1)
4176 {
4177 if (m_richTextCtrl)
4178 {
4179 wxRect rect(GetPosition(), GetSize());
4180 m_richTextCtrl->RefreshRect(rect, false);
4181 }
4182 }
4183 }
4184
4185 m_xOld = m_x;
4186 m_yOld = m_y;
4187}
4188
4189void wxRichTextCaret::DoSize()
4190{
4191 int countVisible = m_countVisible;
4192 if (countVisible > 0)
4193 {
4194 m_countVisible = 0;
4195 DoHide();
4196 }
4197
4198 if (countVisible > 0)
4199 {
4200 m_countVisible = countVisible;
4201 DoShow();
4202 }
4203}
4204
4205// ----------------------------------------------------------------------------
4206// handling the focus
4207// ----------------------------------------------------------------------------
4208
4209void wxRichTextCaret::OnSetFocus()
4210{
4211 m_hasFocus = true;
4212
4213 if ( IsVisible() )
4214 Refresh();
4215}
4216
4217void wxRichTextCaret::OnKillFocus()
4218{
4219 m_hasFocus = false;
4220}
4221
4222// ----------------------------------------------------------------------------
4223// drawing the caret
4224// ----------------------------------------------------------------------------
4225
4226void wxRichTextCaret::Refresh()
4227{
4228 if (m_richTextCtrl)
4229 {
4230 wxRect rect(GetPosition(), GetSize());
4231 m_richTextCtrl->RefreshRect(rect, false);
4232 }
4233}
4234
4235void wxRichTextCaret::DoDraw(wxDC *dc)
4236{
4237 dc->SetPen( *wxBLACK_PEN );
4238
4239 dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
4240 dc->SetPen(*wxBLACK_PEN);
4241
1c13f06e
JS
4242 wxPoint pt(m_x, m_y);
4243
4244 if (m_richTextCtrl)
4245 {
4246 pt = m_richTextCtrl->GetLogicalPoint(pt);
4247 }
a188ac29
JS
4248 if (IsVisible() && m_flashOn)
4249 dc->DrawRectangle(pt.x, pt.y, m_width, m_height);
4250}
4251
4252void wxRichTextCaret::Notify()
4253{
4254 m_flashOn = !m_flashOn;
4255 Refresh();
4256}
4257
4258void wxRichTextCaretTimer::Notify()
4259{
4260 m_caret->Notify();
1c13f06e
JS
4261}
4262#endif
4263 // wxRICHTEXT_USE_OWN_CARET
4264
603f702b
JS
4265// Add an item
4266bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString& label, wxRichTextObject* obj)
4267{
4268 if (GetCount() < 3)
4269 {
4270 m_labels.Add(label);
4271 m_objects.Add(obj);
4272 return true;
4273 }
4274 else
4275 return false;
4276}
4277
4278// Returns number of menu items were added.
4279int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu* menu, int startCmd) const
4280{
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.
4284 if (item)
4285 {
4286 // If no items, to add just set the text to something generic
4287 if (GetCount() == 0)
4288 {
4289 menu->SetLabel(startCmd, _("&Properties"));
4fe83b93 4290
603f702b
JS
4291 // Delete the others if necessary
4292 int i;
4293 for (i = startCmd+1; i < startCmd+3; i++)
4294 {
4295 if (menu->FindItem(i))
4296 {
4297 menu->Delete(i);
4298 }
4299 }
4300 }
4301 else
4302 {
4303 int i;
4304 int pos = -1;
4305 // Find the position of the first properties item
4306 for (i = 0; i < (int) menu->GetMenuItemCount(); i++)
4307 {
4308 wxMenuItem* item = menu->FindItemByPosition(i);
4309 if (item && item->GetId() == startCmd)
4310 {
4311 pos = i;
4312 break;
4313 }
4314 }
4fe83b93 4315
603f702b
JS
4316 if (pos != -1)
4317 {
4318 int insertBefore = pos+1;
4319 for (i = startCmd; i < startCmd+GetCount(); i++)
4320 {
4321 if (menu->FindItem(i))
4322 {
4323 menu->SetLabel(i, m_labels[i - startCmd]);
4324 }
4325 else
4326 {
4327 if (insertBefore >= (int) menu->GetMenuItemCount())
4328 menu->Append(i, m_labels[i - startCmd]);
4329 else
4330 menu->Insert(insertBefore, i, m_labels[i - startCmd]);
4331 }
4332 insertBefore ++;
4333 }
4fe83b93 4334
603f702b
JS
4335 // Delete any old items still left on the menu
4336 for (i = startCmd + GetCount(); i < startCmd+3; i++)
4337 {
4338 if (menu->FindItem(i))
4339 {
4340 menu->Delete(i);
4341 }
4342 }
4343 }
4344 }
4345 }
4346
4347 return GetCount();
4348}
4349
4350// Add appropriate menu items for the current container and clicked on object
4351// (and container's parent, if appropriate).
4352int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextObject* container, wxRichTextObject* obj)
4353{
4354 Clear();
4355 if (obj && obj->CanEditProperties())
4356 AddItem(obj->GetPropertiesMenuLabel(), obj);
4357
4358 if (container && container != obj && container->CanEditProperties() && m_labels.Index(container->GetPropertiesMenuLabel()) == wxNOT_FOUND)
4359 AddItem(container->GetPropertiesMenuLabel(), container);
4360
4361 if (container && container->GetParent() && container->GetParent()->CanEditProperties() && m_labels.Index(container->GetParent()->GetPropertiesMenuLabel()) == wxNOT_FOUND)
4362 AddItem(container->GetParent()->GetPropertiesMenuLabel(), container->GetParent());
4363
4364 return GetCount();
4365}
4366
5d7836c4
JS
4367#endif
4368 // wxUSE_RICHTEXT