]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextctrl.cpp
Fix signatures of various image handlers methods.
[wxWidgets.git] / src / richtext / richtextctrl.cpp
CommitLineData
5d7836c4 1/////////////////////////////////////////////////////////////////////////////
faa94f3e 2// Name: src/richtext/richeditctrl.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
27e20452
JS
38// DLL options compatibility check:
39#include "wx/app.h"
40WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
41
9b11752c
VZ
42wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, wxRichTextEvent );
43wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, wxRichTextEvent );
44wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, wxRichTextEvent );
45wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, wxRichTextEvent );
46wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN, wxRichTextEvent );
47wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER, wxRichTextEvent );
48wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE, wxRichTextEvent );
5912d19e 49
9b11752c
VZ
50wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, wxRichTextEvent );
51wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED, wxRichTextEvent );
52wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING, wxRichTextEvent );
53wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED, wxRichTextEvent );
5d7836c4 54
9b11752c
VZ
55wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, wxRichTextEvent );
56wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, wxRichTextEvent );
57wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, wxRichTextEvent );
58wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED, wxRichTextEvent );
59wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, wxRichTextEvent );
5912d19e 60
1c13f06e
JS
61#if wxRICHTEXT_USE_OWN_CARET
62
63/*!
64 * wxRichTextCaret
65 *
66 * This implements a non-flashing cursor in case there
67 * are platform-specific problems with the generic caret.
68 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
69 */
70
a188ac29
JS
71class wxRichTextCaret;
72class wxRichTextCaretTimer: public wxTimer
73{
74 public:
75 wxRichTextCaretTimer(wxRichTextCaret* caret)
76 {
77 m_caret = caret;
78 }
79 virtual void Notify();
80 wxRichTextCaret* m_caret;
81};
82
1c13f06e
JS
83class wxRichTextCaret: public wxCaret
84{
85public:
86 // ctors
87 // -----
88 // default - use Create()
a188ac29 89 wxRichTextCaret(): m_timer(this) { Init(); }
1c13f06e
JS
90 // creates a block caret associated with the given window
91 wxRichTextCaret(wxRichTextCtrl *window, int width, int height)
a188ac29 92 : wxCaret(window, width, height), m_timer(this) { Init(); m_richTextCtrl = window; }
1c13f06e 93 wxRichTextCaret(wxRichTextCtrl *window, const wxSize& size)
a188ac29 94 : wxCaret(window, size), m_timer(this) { Init(); m_richTextCtrl = window; }
1c13f06e
JS
95
96 virtual ~wxRichTextCaret();
97
98 // implementation
99 // --------------
100
101 // called by wxWindow (not using the event tables)
102 virtual void OnSetFocus();
103 virtual void OnKillFocus();
104
105 // draw the caret on the given DC
106 void DoDraw(wxDC *dc);
107
108 // get the visible count
109 int GetVisibleCount() const { return m_countVisible; }
110
111 // delay repositioning
112 bool GetNeedsUpdate() const { return m_needsUpdate; }
113 void SetNeedsUpdate(bool needsUpdate = true ) { m_needsUpdate = needsUpdate; }
114
a188ac29
JS
115 void Notify();
116
1c13f06e
JS
117protected:
118 virtual void DoShow();
119 virtual void DoHide();
120 virtual void DoMove();
121 virtual void DoSize();
122
123 // refresh the caret
124 void Refresh();
125
126private:
127 void Init();
128
129 int m_xOld,
130 m_yOld;
131 bool m_hasFocus; // true => our window has focus
132 bool m_needsUpdate; // must be repositioned
a188ac29
JS
133 bool m_flashOn;
134 wxRichTextCaretTimer m_timer;
1c13f06e
JS
135 wxRichTextCtrl* m_richTextCtrl;
136};
137#endif
138
492d6611 139IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl, wxTextCtrlBase )
5d7836c4 140
d0960f6d 141IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent, wxNotifyEvent )
5d7836c4 142
492d6611 143BEGIN_EVENT_TABLE( wxRichTextCtrl, wxTextCtrlBase )
5d7836c4
JS
144 EVT_PAINT(wxRichTextCtrl::OnPaint)
145 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
4d551ad5 146 EVT_IDLE(wxRichTextCtrl::OnIdle)
c59f6793 147 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
5d7836c4
JS
148 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick)
149 EVT_MOTION(wxRichTextCtrl::OnMoveMouse)
150 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp)
151 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick)
152 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick)
153 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick)
154 EVT_CHAR(wxRichTextCtrl::OnChar)
c80e8cbe 155 EVT_KEY_DOWN(wxRichTextCtrl::OnChar)
5d7836c4
JS
156 EVT_SIZE(wxRichTextCtrl::OnSize)
157 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
158 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
61f84e24 159 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost)
5d7836c4 160 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
52170c5b 161 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged)
5d7836c4
JS
162
163 EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
164 EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
165
166 EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo)
167 EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo)
168
169 EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy)
170 EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy)
171
172 EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste)
173 EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste)
174
175 EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut)
176 EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut)
177
178 EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear)
179 EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear)
180
181 EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll)
182 EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll)
183END_EVENT_TABLE()
184
185/*!
186 * wxRichTextCtrl
187 */
188
dadd4f55
JS
189wxArrayString wxRichTextCtrl::sm_availableFontNames;
190
5d7836c4 191wxRichTextCtrl::wxRichTextCtrl()
343ae70d 192 : wxScrollHelper(this)
5d7836c4
JS
193{
194 Init();
195}
196
343ae70d
VZ
197wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent,
198 wxWindowID id,
199 const wxString& value,
200 const wxPoint& pos,
201 const wxSize& size,
4e8d9558
JS
202 long style,
203 const wxValidator& validator,
204 const wxString& name)
343ae70d 205 : wxScrollHelper(this)
5d7836c4
JS
206{
207 Init();
4e8d9558 208 Create(parent, id, value, pos, size, style, validator, name);
5d7836c4
JS
209}
210
211/// Creation
4e8d9558
JS
212bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style,
213 const wxValidator& validator, const wxString& name)
5d7836c4 214{
6d595f3d
JS
215 style |= wxVSCROLL;
216
f5172305 217 if (!wxTextCtrlBase::Create(parent, id, pos, size,
0ec1179b
VZ
218 style|wxFULL_REPAINT_ON_RESIZE,
219 validator, name))
5d7836c4 220 return false;
5d7836c4
JS
221
222 if (!GetFont().Ok())
223 {
224 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
225 }
7fe8059f 226
e9f10004
JS
227 // No physical scrolling, so we can preserve margins
228 EnableScrolling(false, false);
229
dadd4f55
JS
230 if (style & wxTE_READONLY)
231 SetEditable(false);
232
42688aea 233 // The base attributes must all have default values
44cc96a8 234 wxTextAttr attributes;
5d7836c4 235 attributes.SetFont(GetFont());
79f955ec 236 attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
5d7836c4 237 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
3e3a754f
JS
238 attributes.SetLineSpacing(10);
239 attributes.SetParagraphSpacingAfter(10);
240 attributes.SetParagraphSpacingBefore(0);
d2d0adc7 241
5d7836c4
JS
242 SetBasicStyle(attributes);
243
fe5aa22c
JS
244 // The default attributes will be merged with base attributes, so
245 // can be empty to begin with
44cc96a8 246 wxTextAttr defaultAttributes;
fe5aa22c
JS
247 SetDefaultStyle(defaultAttributes);
248
79f955ec 249 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
5d7836c4
JS
250 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
251
cd8ba0d9
JS
252 GetBuffer().Reset();
253 GetBuffer().SetRichTextCtrl(this);
254
1c13f06e
JS
255#if wxRICHTEXT_USE_OWN_CARET
256 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
257#else
cd8ba0d9 258 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
1c13f06e 259#endif
cd8ba0d9 260
7fe8059f 261 // Tell the sizers to use the given or best size
170acdc9 262 SetInitialSize(size);
7fe8059f 263
3e3a754f 264#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4
JS
265 // Create a buffer
266 RecreateBuffer(size);
3e3a754f 267#endif
5d7836c4 268
d2d0adc7
JS
269 m_textCursor = wxCursor(wxCURSOR_IBEAM);
270 m_urlCursor = wxCursor(wxCURSOR_HAND);
271
272 SetCursor(m_textCursor);
61399247 273
27e20452
JS
274 if (!value.IsEmpty())
275 SetValue(value);
7fe8059f 276
d2d0adc7
JS
277 GetBuffer().AddEventHandler(this);
278
addcf76c 279 // Accelerators
0951fe0a 280 wxAcceleratorEntry entries[6];
addcf76c
JS
281
282 entries[0].Set(wxACCEL_CMD, (int) 'C', wxID_COPY);
283 entries[1].Set(wxACCEL_CMD, (int) 'X', wxID_CUT);
284 entries[2].Set(wxACCEL_CMD, (int) 'V', wxID_PASTE);
285 entries[3].Set(wxACCEL_CMD, (int) 'A', wxID_SELECTALL);
0951fe0a
JS
286 entries[4].Set(wxACCEL_CMD, (int) 'Z', wxID_UNDO);
287 entries[5].Set(wxACCEL_CMD, (int) 'Y', wxID_REDO);
addcf76c 288
0951fe0a 289 wxAcceleratorTable accel(6, entries);
addcf76c
JS
290 SetAcceleratorTable(accel);
291
5d7836c4
JS
292 return true;
293}
294
295wxRichTextCtrl::~wxRichTextCtrl()
296{
d2d0adc7 297 GetBuffer().RemoveEventHandler(this);
88bee7e6 298
5d7836c4
JS
299 delete m_contextMenu;
300}
301
302/// Member initialisation
303void wxRichTextCtrl::Init()
304{
5d7836c4
JS
305 m_contextMenu = NULL;
306 m_caret = NULL;
307 m_caretPosition = -1;
308 m_selectionRange.SetRange(-2, -2);
309 m_selectionAnchor = -2;
310 m_editable = true;
311 m_caretAtLineStart = false;
312 m_dragging = false;
4d551ad5
JS
313 m_fullLayoutRequired = false;
314 m_fullLayoutTime = 0;
315 m_fullLayoutSavedPosition = 0;
316 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
ab14c7aa 317 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
318}
319
17808a75 320void wxRichTextCtrl::DoThaw()
5d7836c4 321{
17808a75
VZ
322 if (GetBuffer().GetDirty())
323 LayoutContent();
324 else
325 SetupScrollbars();
8251c171 326
6a8e61e0 327 wxWindow::DoThaw();
5d7836c4
JS
328}
329
330/// Clear all text
331void wxRichTextCtrl::Clear()
332{
85d8909b 333 m_buffer.ResetAndClearCommands();
5d7836c4
JS
334 m_buffer.SetDirty(true);
335 m_caretPosition = -1;
ab14c7aa 336 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
337 m_caretAtLineStart = false;
338 m_selectionRange.SetRange(-2, -2);
339
495a248e
JS
340 Scroll(0,0);
341
17808a75 342 if (!IsFrozen())
5d7836c4 343 {
cecf8eee 344 LayoutContent();
76bcd815 345 Refresh(false);
5d7836c4 346 }
0ec1179b
VZ
347
348 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
349}
350
351/// Painting
352void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
353{
1c13f06e
JS
354#if !wxRICHTEXT_USE_OWN_CARET
355 if (GetCaret() && !IsFrozen())
d2142335 356 GetCaret()->Hide();
1c13f06e 357#endif
d2142335 358
5d7836c4 359 {
3e3a754f 360#if wxRICHTEXT_BUFFERED_PAINTING
c59f6793 361 wxBufferedPaintDC dc(this, m_bufferBitmap);
3e3a754f
JS
362#else
363 wxPaintDC dc(this);
364#endif
41e155b4 365
17808a75 366 if (IsFrozen())
c59f6793 367 return;
41e155b4 368
17808a75
VZ
369 PrepareDC(dc);
370
c59f6793 371 dc.SetFont(GetFont());
41e155b4 372
c59f6793
JS
373 // Paint the background
374 PaintBackground(dc);
41e155b4 375
ea160b2e
JS
376 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
377
378 wxRect drawingArea(GetUpdateRegion().GetBox());
379 drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition()));
380
c59f6793
JS
381 wxRect availableSpace(GetClientSize());
382 if (GetBuffer().GetDirty())
383 {
384 GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
385 GetBuffer().SetDirty(false);
386 SetupScrollbars();
387 }
41e155b4 388
e9f10004
JS
389 wxRect clipRect(availableSpace);
390 clipRect.x += GetBuffer().GetLeftMargin();
391 clipRect.y += GetBuffer().GetTopMargin();
392 clipRect.width -= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
393 clipRect.height -= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
394 clipRect.SetPosition(GetLogicalPoint(clipRect.GetPosition()));
395 dc.SetClippingRegion(clipRect);
396
96c9f0f6 397 GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
e9f10004
JS
398
399 dc.DestroyClippingRegion();
400
1c13f06e
JS
401#if wxRICHTEXT_USE_OWN_CARET
402 if (GetCaret()->IsVisible())
403 {
404 ((wxRichTextCaret*) GetCaret())->DoDraw(& dc);
405 }
406#endif
5d7836c4 407 }
d2142335 408
1c13f06e
JS
409#if !wxRICHTEXT_USE_OWN_CARET
410 if (GetCaret())
d2142335 411 GetCaret()->Show();
7383cf9d 412 PositionCaret();
1c13f06e 413#endif
5d7836c4
JS
414}
415
416// Empty implementation, to prevent flicker
417void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
418{
419}
420
421void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
422{
ea160b2e
JS
423 if (GetCaret())
424 {
1c13f06e 425#if !wxRICHTEXT_USE_OWN_CARET
ea160b2e 426 PositionCaret();
1c13f06e
JS
427#endif
428 GetCaret()->Show();
ea160b2e 429 }
c59f6793 430
1c13f06e
JS
431#if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
432 // Work around dropouts when control is focused
433 if (!IsFrozen())
434 {
435 Refresh(false);
436 }
437#endif
5d7836c4
JS
438}
439
440void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
441{
1c13f06e 442 if (GetCaret())
ea160b2e 443 GetCaret()->Hide();
c59f6793 444
1c13f06e
JS
445#if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
446 // Work around dropouts when control is focused
447 if (!IsFrozen())
448 {
449 Refresh(false);
450 }
451#endif
5d7836c4
JS
452}
453
61f84e24
JS
454void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
455{
88bee7e6 456 m_dragging = false;
61f84e24
JS
457}
458
5d7836c4
JS
459/// Left-click
460void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
461{
462 SetFocus();
463
464 wxClientDC dc(this);
465 PrepareDC(dc);
466 dc.SetFont(GetFont());
7fe8059f 467
5d7836c4
JS
468 long position = 0;
469 int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position);
470
471 if (hit != wxRICHTEXT_HITTEST_NONE)
472 {
473 m_dragStart = event.GetLogicalPosition(dc);
474 m_dragging = true;
475 CaptureMouse();
476
5d7836c4
JS
477 bool caretAtLineStart = false;
478
479 if (hit & wxRICHTEXT_HITTEST_BEFORE)
480 {
481 // If we're at the start of a line (but not first in para)
482 // then we should keep the caret showing at the start of the line
483 // by showing the m_caretAtLineStart flag.
484 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
485 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
486
1e967276 487 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
5d7836c4
JS
488 caretAtLineStart = true;
489 position --;
490 }
491
f7222f4c
JS
492 long oldCaretPos = m_caretPosition;
493
5d7836c4 494 MoveCaret(position, caretAtLineStart);
0a21bea0 495 SetDefaultStyleToCursorStyle();
f7222f4c
JS
496
497 if (event.ShiftDown())
498 {
f7222f4c 499 if (m_selectionRange.GetStart() == -2)
7051fa41 500 ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
f7222f4c 501 else
7051fa41 502 ExtendSelection(m_caretPosition, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
f7222f4c
JS
503 }
504 else
505 SelectNone();
5d7836c4
JS
506 }
507
508 event.Skip();
509}
510
511/// Left-up
d2d0adc7 512void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event)
5d7836c4
JS
513{
514 if (m_dragging)
515 {
516 m_dragging = false;
517 if (GetCapture() == this)
518 ReleaseMouse();
d2d0adc7
JS
519
520 // See if we clicked on a URL
521 wxClientDC dc(this);
522 PrepareDC(dc);
523 dc.SetFont(GetFont());
524
525 long position = 0;
526 wxPoint logicalPt = event.GetLogicalPosition(dc);
527 int hit = GetBuffer().HitTest(dc, logicalPt, position);
88bee7e6 528
62381daa 529 if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
d2d0adc7 530 {
db6634e0
JS
531 wxRichTextEvent cmdEvent(
532 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK,
533 GetId());
534 cmdEvent.SetEventObject(this);
535 cmdEvent.SetPosition(m_caretPosition+1);
536
537 if (!GetEventHandler()->ProcessEvent(cmdEvent))
d2d0adc7 538 {
44cc96a8 539 wxTextAttr attr;
db6634e0 540 if (GetStyle(position, attr))
d2d0adc7 541 {
db6634e0 542 if (attr.HasFlag(wxTEXT_ATTR_URL))
d2d0adc7 543 {
db6634e0
JS
544 wxString urlTarget = attr.GetURL();
545 if (!urlTarget.IsEmpty())
d2d0adc7 546 {
db6634e0 547 wxMouseEvent mouseEvent(event);
1a75935d 548
db6634e0
JS
549 long startPos = 0, endPos = 0;
550 wxRichTextObject* obj = GetBuffer().GetLeafObjectAtPosition(position);
551 if (obj)
552 {
553 startPos = obj->GetRange().GetStart();
554 endPos = obj->GetRange().GetEnd();
555 }
1a75935d 556
db6634e0
JS
557 wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos);
558 InitCommandEvent(urlEvent);
1a75935d 559
db6634e0 560 urlEvent.SetString(urlTarget);
1a75935d 561
db6634e0 562 GetEventHandler()->ProcessEvent(urlEvent);
88bee7e6 563 }
d2d0adc7
JS
564 }
565 }
566 }
567 }
5d7836c4
JS
568 }
569}
570
571/// Left-click
572void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
573{
5d7836c4
JS
574 wxClientDC dc(this);
575 PrepareDC(dc);
576 dc.SetFont(GetFont());
7fe8059f 577
5d7836c4
JS
578 long position = 0;
579 wxPoint logicalPt = event.GetLogicalPosition(dc);
580 int hit = GetBuffer().HitTest(dc, logicalPt, position);
88bee7e6 581
d2d0adc7 582 // See if we need to change the cursor
88bee7e6 583
d2d0adc7 584 {
efb2c52f 585 if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
d2d0adc7 586 {
44cc96a8 587 wxTextAttr attr;
d2d0adc7
JS
588 if (GetStyle(position, attr))
589 {
590 if (attr.HasFlag(wxTEXT_ATTR_URL))
591 {
e4ef49c7 592 SetCursor(m_urlCursor);
d2d0adc7
JS
593 }
594 else if (!attr.HasFlag(wxTEXT_ATTR_URL))
595 {
e4ef49c7 596 SetCursor(m_textCursor);
d2d0adc7
JS
597 }
598 }
599 }
f262b25c
JS
600 else
601 SetCursor(m_textCursor);
d2d0adc7
JS
602 }
603
604 if (!event.Dragging())
605 {
606 event.Skip();
607 return;
608 }
5d7836c4
JS
609
610 if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE)
611 {
612 // TODO: test closeness
613
614 bool caretAtLineStart = false;
615
616 if (hit & wxRICHTEXT_HITTEST_BEFORE)
617 {
618 // If we're at the start of a line (but not first in para)
619 // then we should keep the caret showing at the start of the line
620 // by showing the m_caretAtLineStart flag.
621 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
622 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
623
1e967276 624 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
5d7836c4
JS
625 caretAtLineStart = true;
626 position --;
627 }
628
629 if (m_caretPosition != position)
630 {
7051fa41 631 ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
7fe8059f 632
5d7836c4 633 MoveCaret(position, caretAtLineStart);
0a21bea0 634 SetDefaultStyleToCursorStyle();
5d7836c4
JS
635 }
636 }
637}
638
639/// Right-click
52170c5b 640void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
5d7836c4
JS
641{
642 SetFocus();
db6634e0
JS
643
644 wxRichTextEvent cmdEvent(
645 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK,
646 GetId());
647 cmdEvent.SetEventObject(this);
648 cmdEvent.SetPosition(m_caretPosition+1);
1a75935d 649
52170c5b
JS
650 if (!GetEventHandler()->ProcessEvent(cmdEvent))
651 event.Skip();
5d7836c4
JS
652}
653
654/// Left-double-click
db6634e0 655void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event))
5d7836c4 656{
db6634e0
JS
657 wxRichTextEvent cmdEvent(
658 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK,
659 GetId());
660 cmdEvent.SetEventObject(this);
661 cmdEvent.SetPosition(m_caretPosition+1);
1a75935d 662
db6634e0
JS
663 if (!GetEventHandler()->ProcessEvent(cmdEvent))
664 {
665 SelectWord(GetCaretPosition()+1);
666 }
5d7836c4
JS
667}
668
669/// Middle-click
670void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
671{
db6634e0
JS
672 wxRichTextEvent cmdEvent(
673 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK,
674 GetId());
675 cmdEvent.SetEventObject(this);
676 cmdEvent.SetPosition(m_caretPosition+1);
1a75935d 677
db6634e0
JS
678 if (!GetEventHandler()->ProcessEvent(cmdEvent))
679 event.Skip();
5d7836c4
JS
680}
681
682/// Key press
683void wxRichTextCtrl::OnChar(wxKeyEvent& event)
684{
685 int flags = 0;
3e3a754f 686 if (event.CmdDown())
5d7836c4
JS
687 flags |= wxRICHTEXT_CTRL_DOWN;
688 if (event.ShiftDown())
689 flags |= wxRICHTEXT_SHIFT_DOWN;
690 if (event.AltDown())
691 flags |= wxRICHTEXT_ALT_DOWN;
692
c80e8cbe
JS
693 if (event.GetEventType() == wxEVT_KEY_DOWN)
694 {
8c9e72f9
JS
695 if (event.GetKeyCode() == WXK_LEFT ||
696 event.GetKeyCode() == WXK_RIGHT ||
697 event.GetKeyCode() == WXK_UP ||
698 event.GetKeyCode() == WXK_DOWN ||
699 event.GetKeyCode() == WXK_HOME ||
700 event.GetKeyCode() == WXK_PAGEUP ||
701 event.GetKeyCode() == WXK_PAGEDOWN ||
702 event.GetKeyCode() == WXK_END ||
703
704 event.GetKeyCode() == WXK_NUMPAD_LEFT ||
705 event.GetKeyCode() == WXK_NUMPAD_RIGHT ||
706 event.GetKeyCode() == WXK_NUMPAD_UP ||
707 event.GetKeyCode() == WXK_NUMPAD_DOWN ||
708 event.GetKeyCode() == WXK_NUMPAD_HOME ||
709 event.GetKeyCode() == WXK_NUMPAD_PAGEUP ||
710 event.GetKeyCode() == WXK_NUMPAD_PAGEDOWN ||
711 event.GetKeyCode() == WXK_NUMPAD_END)
712 {
713 KeyboardNavigate(event.GetKeyCode(), flags);
714 return;
715 }
716
717 long keycode = event.GetKeyCode();
718 switch ( keycode )
719 {
720 case WXK_ESCAPE:
721 case WXK_START:
722 case WXK_LBUTTON:
723 case WXK_RBUTTON:
724 case WXK_CANCEL:
725 case WXK_MBUTTON:
726 case WXK_CLEAR:
727 case WXK_SHIFT:
728 case WXK_ALT:
729 case WXK_CONTROL:
730 case WXK_MENU:
731 case WXK_PAUSE:
732 case WXK_CAPITAL:
733 case WXK_END:
734 case WXK_HOME:
735 case WXK_LEFT:
736 case WXK_UP:
737 case WXK_RIGHT:
738 case WXK_DOWN:
739 case WXK_SELECT:
740 case WXK_PRINT:
741 case WXK_EXECUTE:
742 case WXK_SNAPSHOT:
743 case WXK_INSERT:
744 case WXK_HELP:
745 case WXK_F1:
746 case WXK_F2:
747 case WXK_F3:
748 case WXK_F4:
749 case WXK_F5:
750 case WXK_F6:
751 case WXK_F7:
752 case WXK_F8:
753 case WXK_F9:
754 case WXK_F10:
755 case WXK_F11:
756 case WXK_F12:
757 case WXK_F13:
758 case WXK_F14:
759 case WXK_F15:
760 case WXK_F16:
761 case WXK_F17:
762 case WXK_F18:
763 case WXK_F19:
764 case WXK_F20:
765 case WXK_F21:
766 case WXK_F22:
767 case WXK_F23:
768 case WXK_F24:
769 case WXK_NUMLOCK:
770 case WXK_SCROLL:
771 case WXK_PAGEUP:
772 case WXK_PAGEDOWN:
773 case WXK_NUMPAD_F1:
774 case WXK_NUMPAD_F2:
775 case WXK_NUMPAD_F3:
776 case WXK_NUMPAD_F4:
777 case WXK_NUMPAD_HOME:
778 case WXK_NUMPAD_LEFT:
779 case WXK_NUMPAD_UP:
780 case WXK_NUMPAD_RIGHT:
781 case WXK_NUMPAD_DOWN:
782 case WXK_NUMPAD_PAGEUP:
783 case WXK_NUMPAD_PAGEDOWN:
784 case WXK_NUMPAD_END:
785 case WXK_NUMPAD_BEGIN:
786 case WXK_NUMPAD_INSERT:
8c9e72f9
JS
787 case WXK_WINDOWS_LEFT:
788 {
789 return;
790 }
791 default:
792 {
793 }
794 }
795
c80e8cbe
JS
796 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
797 if (event.CmdDown() && event.GetKeyCode() == WXK_BACK)
798 {
799 BeginBatchUndo(_("Delete Text"));
800
801 long newPos = m_caretPosition;
802
29dbface 803 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe
JS
804
805 // Submit range in character positions, which are greater than caret positions,
806 // so subtract 1 for deleted character and add 1 for conversion to character position.
807 if (newPos > -1)
808 {
c80e8cbe
JS
809 if (event.CmdDown())
810 {
811 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
8251c171 812 if (pos < newPos)
c80e8cbe
JS
813 {
814 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
815 processed = true;
816 }
817 }
818
819 if (!processed)
820 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
821 }
822
823 EndBatchUndo();
824
825 if (GetLastPosition() == -1)
826 {
827 GetBuffer().Reset();
828
829 m_caretPosition = -1;
830 PositionCaret();
831 SetDefaultStyleToCursorStyle();
832 }
833
834 ScrollIntoView(m_caretPosition, WXK_LEFT);
835
836 wxRichTextEvent cmdEvent(
837 wxEVT_COMMAND_RICHTEXT_DELETE,
838 GetId());
839 cmdEvent.SetEventObject(this);
840 cmdEvent.SetFlags(flags);
841 cmdEvent.SetPosition(m_caretPosition+1);
842 GetEventHandler()->ProcessEvent(cmdEvent);
843
844 Update();
845 }
846 else
847 event.Skip();
848
849 return;
850 }
851
99257cbe
VZ
852 // all the other keys modify the controls contents which shouldn't be
853 // possible if we're read-only
854 if ( !IsEditable() )
855 {
856 event.Skip();
857 return;
858 }
859
860 if (event.GetKeyCode() == WXK_RETURN)
5d7836c4
JS
861 {
862 BeginBatchUndo(_("Insert Text"));
863
864 long newPos = m_caretPosition;
865
866 DeleteSelectedContent(& newPos);
867
ff76711f
JS
868 if (event.ShiftDown())
869 {
870 wxString text;
871 text = wxRichTextLineBreakChar;
872 GetBuffer().InsertTextWithUndo(newPos+1, text, this);
4bf9e5bc 873 m_caretAtLineStart = true;
043c0d58 874 PositionCaret();
ff76711f
JS
875 }
876 else
c025e094 877 GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE);
ff76711f 878
d2d0adc7 879 EndBatchUndo();
0a21bea0 880 SetDefaultStyleToCursorStyle();
d2d0adc7
JS
881
882 ScrollIntoView(m_caretPosition, WXK_RIGHT);
5d7836c4
JS
883
884 wxRichTextEvent cmdEvent(
885 wxEVT_COMMAND_RICHTEXT_RETURN,
886 GetId());
887 cmdEvent.SetEventObject(this);
888 cmdEvent.SetFlags(flags);
5912d19e
JS
889 cmdEvent.SetPosition(newPos+1);
890
d2d0adc7
JS
891 if (!GetEventHandler()->ProcessEvent(cmdEvent))
892 {
893 // Generate conventional event
894 wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId());
895 InitCommandEvent(textEvent);
88bee7e6 896
d2d0adc7
JS
897 GetEventHandler()->ProcessEvent(textEvent);
898 }
621ae374 899 Update();
5d7836c4
JS
900 }
901 else if (event.GetKeyCode() == WXK_BACK)
902 {
903 BeginBatchUndo(_("Delete Text"));
88bee7e6 904
c80e8cbe
JS
905 long newPos = m_caretPosition;
906
dc5de0dd 907 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe 908
5d7836c4
JS
909 // Submit range in character positions, which are greater than caret positions,
910 // so subtract 1 for deleted character and add 1 for conversion to character position.
c80e8cbe 911 if (newPos > -1)
5d7836c4 912 {
62381daa
JS
913 if (event.CmdDown())
914 {
915 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
8251c171 916 if (pos < newPos)
62381daa 917 {
c80e8cbe 918 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
62381daa
JS
919 processed = true;
920 }
921 }
922
923 if (!processed)
c80e8cbe 924 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
5d7836c4 925 }
5d7836c4
JS
926
927 EndBatchUndo();
928
5d7836c4
JS
929 if (GetLastPosition() == -1)
930 {
931 GetBuffer().Reset();
932
933 m_caretPosition = -1;
934 PositionCaret();
0a21bea0 935 SetDefaultStyleToCursorStyle();
5d7836c4
JS
936 }
937
f9ed2d16 938 ScrollIntoView(m_caretPosition, WXK_LEFT);
5912d19e
JS
939
940 wxRichTextEvent cmdEvent(
941 wxEVT_COMMAND_RICHTEXT_DELETE,
942 GetId());
943 cmdEvent.SetEventObject(this);
944 cmdEvent.SetFlags(flags);
945 cmdEvent.SetPosition(m_caretPosition+1);
946 GetEventHandler()->ProcessEvent(cmdEvent);
947
621ae374 948 Update();
5d7836c4
JS
949 }
950 else if (event.GetKeyCode() == WXK_DELETE)
951 {
952 BeginBatchUndo(_("Delete Text"));
953
c80e8cbe
JS
954 long newPos = m_caretPosition;
955
dc5de0dd 956 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe 957
5d7836c4 958 // Submit range in character positions, which are greater than caret positions,
c80e8cbe 959 if (newPos < GetBuffer().GetRange().GetEnd()+1)
5d7836c4 960 {
c80e8cbe
JS
961 if (event.CmdDown())
962 {
963 long pos = wxRichTextCtrl::FindNextWordPosition(1);
964 if (pos != -1 && (pos > newPos))
965 {
966 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, pos), this);
967 processed = true;
968 }
969 }
970
971 if (!processed)
972 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, newPos+1), this);
5d7836c4 973 }
5d7836c4
JS
974
975 EndBatchUndo();
976
5d7836c4
JS
977 if (GetLastPosition() == -1)
978 {
979 GetBuffer().Reset();
980
981 m_caretPosition = -1;
982 PositionCaret();
0a21bea0 983 SetDefaultStyleToCursorStyle();
5d7836c4 984 }
5912d19e
JS
985
986 wxRichTextEvent cmdEvent(
987 wxEVT_COMMAND_RICHTEXT_DELETE,
988 GetId());
989 cmdEvent.SetEventObject(this);
990 cmdEvent.SetFlags(flags);
991 cmdEvent.SetPosition(m_caretPosition+1);
992 GetEventHandler()->ProcessEvent(cmdEvent);
993
621ae374 994 Update();
5d7836c4
JS
995 }
996 else
997 {
3e3a754f
JS
998 long keycode = event.GetKeyCode();
999 switch ( keycode )
1000 {
1001 case WXK_ESCAPE:
3e3a754f
JS
1002 {
1003 event.Skip();
1004 return;
1005 }
1807a1f3 1006
3e3a754f
JS
1007 default:
1008 {
cfceb4a8 1009#ifdef __WXMAC__
91babb9d 1010 if (event.CmdDown())
cfceb4a8 1011#else
d2bb72ff
JS
1012 // Fixes AltGr+key with European input languages on Windows
1013 if ((event.CmdDown() && !event.AltDown()) || (event.AltDown() && !event.CmdDown()))
cfceb4a8 1014#endif
3e3a754f
JS
1015 {
1016 event.Skip();
1017 return;
1018 }
41a85215 1019
5912d19e
JS
1020 wxRichTextEvent cmdEvent(
1021 wxEVT_COMMAND_RICHTEXT_CHARACTER,
1022 GetId());
1023 cmdEvent.SetEventObject(this);
1024 cmdEvent.SetFlags(flags);
0b14a7bd
JS
1025#if wxUSE_UNICODE
1026 cmdEvent.SetCharacter(event.GetUnicodeKey());
1027#else
5912d19e 1028 cmdEvent.SetCharacter((wxChar) keycode);
0b14a7bd 1029#endif
5912d19e 1030 cmdEvent.SetPosition(m_caretPosition+1);
88bee7e6 1031
dadd4f55
JS
1032 if (keycode == wxT('\t'))
1033 {
1034 // See if we need to promote or demote the selection or paragraph at the cursor
1035 // position, instead of inserting a tab.
1036 long pos = GetAdjustedCaretPosition(GetCaretPosition());
1037 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos);
1038 if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName())
1039 {
1040 wxRichTextRange range;
1041 if (HasSelection())
1042 range = GetSelectionRange();
1043 else
1044 range = para->GetRange().FromInternal();
41a85215 1045
dadd4f55
JS
1046 int promoteBy = event.ShiftDown() ? 1 : -1;
1047
1048 PromoteList(promoteBy, range, NULL);
d2d0adc7 1049
5912d19e
JS
1050 GetEventHandler()->ProcessEvent(cmdEvent);
1051
dadd4f55
JS
1052 return;
1053 }
1054 }
5d7836c4 1055
3e3a754f 1056 BeginBatchUndo(_("Insert Text"));
5d7836c4 1057
3e3a754f
JS
1058 long newPos = m_caretPosition;
1059 DeleteSelectedContent(& newPos);
5d7836c4 1060
0b14a7bd
JS
1061#if wxUSE_UNICODE
1062 wxString str = event.GetUnicodeKey();
1063#else
3e3a754f 1064 wxString str = (wxChar) event.GetKeyCode();
0b14a7bd 1065#endif
523d2f14 1066 GetBuffer().InsertTextWithUndo(newPos+1, str, this, 0);
5d7836c4 1067
3e3a754f
JS
1068 EndBatchUndo();
1069
0a21bea0 1070 SetDefaultStyleToCursorStyle();
3e3a754f 1071 ScrollIntoView(m_caretPosition, WXK_RIGHT);
88bee7e6 1072
5912d19e
JS
1073 GetEventHandler()->ProcessEvent(cmdEvent);
1074
621ae374 1075 Update();
3e3a754f
JS
1076 }
1077 }
5d7836c4 1078 }
5d7836c4
JS
1079}
1080
1081/// Delete content if there is a selection, e.g. when pressing a key.
1082bool wxRichTextCtrl::DeleteSelectedContent(long* newPos)
1083{
1084 if (HasSelection())
1085 {
1086 long pos = m_selectionRange.GetStart();
f52eff54 1087 GetBuffer().DeleteRangeWithUndo(m_selectionRange, this);
5d7836c4
JS
1088 m_selectionRange.SetRange(-2, -2);
1089
1090 if (newPos)
1091 *newPos = pos-1;
1092 return true;
1093 }
1094 else
1095 return false;
1096}
1097
1098/// Keyboard navigation
1099
1100/*
1101
1102Left: left one character
1103Right: right one character
1104Up: up one line
1105Down: down one line
1106Ctrl-Left: left one word
1107Ctrl-Right: right one word
1108Ctrl-Up: previous paragraph start
1109Ctrl-Down: next start of paragraph
1110Home: start of line
1111End: end of line
1112Ctrl-Home: start of document
1113Ctrl-End: end of document
1114Page-Up: Up a screen
1115Page-Down: Down a screen
1116
1117Maybe:
1118
1119Ctrl-Alt-PgUp: Start of window
1120Ctrl-Alt-PgDn: End of window
1121F8: Start selection mode
1122Esc: End selection mode
1123
1124Adding Shift does the above but starts/extends selection.
1125
1126
1127 */
1128
0bab774b 1129bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags)
5d7836c4
JS
1130{
1131 bool success = false;
5d7836c4 1132
3e3a754f 1133 if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT)
5d7836c4
JS
1134 {
1135 if (flags & wxRICHTEXT_CTRL_DOWN)
1136 success = WordRight(1, flags);
1137 else
1138 success = MoveRight(1, flags);
1139 }
3e3a754f 1140 else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT)
5d7836c4
JS
1141 {
1142 if (flags & wxRICHTEXT_CTRL_DOWN)
1143 success = WordLeft(1, flags);
1144 else
1145 success = MoveLeft(1, flags);
1146 }
3e3a754f 1147 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP)
5d7836c4
JS
1148 {
1149 if (flags & wxRICHTEXT_CTRL_DOWN)
1150 success = MoveToParagraphStart(flags);
1151 else
1152 success = MoveUp(1, flags);
1153 }
3e3a754f 1154 else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN)
5d7836c4
JS
1155 {
1156 if (flags & wxRICHTEXT_CTRL_DOWN)
1157 success = MoveToParagraphEnd(flags);
1158 else
1159 success = MoveDown(1, flags);
1160 }
3e3a754f 1161 else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP)
5d7836c4
JS
1162 {
1163 success = PageUp(1, flags);
1164 }
3e3a754f 1165 else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
5d7836c4
JS
1166 {
1167 success = PageDown(1, flags);
1168 }
3e3a754f 1169 else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME)
5d7836c4
JS
1170 {
1171 if (flags & wxRICHTEXT_CTRL_DOWN)
1172 success = MoveHome(flags);
1173 else
1174 success = MoveToLineStart(flags);
1175 }
3e3a754f 1176 else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END)
5d7836c4
JS
1177 {
1178 if (flags & wxRICHTEXT_CTRL_DOWN)
1179 success = MoveEnd(flags);
1180 else
1181 success = MoveToLineEnd(flags);
1182 }
1183
1184 if (success)
1185 {
1186 ScrollIntoView(m_caretPosition, keyCode);
0a21bea0 1187 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1188 }
1189
5d7836c4
JS
1190 return success;
1191}
1192
1193/// Extend the selection. Selections are in caret positions.
1194bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
1195{
1196 if (flags & wxRICHTEXT_SHIFT_DOWN)
1197 {
e2d0875a
JS
1198 if (oldPos == newPos)
1199 return false;
1200
7051fa41
JS
1201 wxRichTextRange oldSelection = m_selectionRange;
1202
5d7836c4
JS
1203 // If not currently selecting, start selecting
1204 if (m_selectionRange.GetStart() == -2)
1205 {
1206 m_selectionAnchor = oldPos;
1207
1208 if (oldPos > newPos)
1209 m_selectionRange.SetRange(newPos+1, oldPos);
1210 else
1211 m_selectionRange.SetRange(oldPos+1, newPos);
1212 }
1213 else
1214 {
1215 // Always ensure that the selection range start is greater than
1216 // the end.
1217 if (newPos > m_selectionAnchor)
1218 m_selectionRange.SetRange(m_selectionAnchor+1, newPos);
e2d0875a
JS
1219 else if (newPos == m_selectionAnchor)
1220 m_selectionRange = wxRichTextRange(-2, -2);
5d7836c4
JS
1221 else
1222 m_selectionRange.SetRange(newPos+1, m_selectionAnchor);
1223 }
1224
7051fa41
JS
1225 RefreshForSelectionChange(oldSelection, m_selectionRange);
1226
5d7836c4
JS
1227 if (m_selectionRange.GetStart() > m_selectionRange.GetEnd())
1228 {
1229 wxLogDebug(wxT("Strange selection range"));
1230 }
7fe8059f 1231
5d7836c4
JS
1232 return true;
1233 }
1234 else
1235 return false;
1236}
1237
1238/// Scroll into view, returning true if we scrolled.
1239/// This takes a _caret_ position.
1240bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
1241{
1242 wxRichTextLine* line = GetVisibleLineForCaretPosition(position);
1243
1244 if (!line)
1245 return false;
1246
1247 int ppuX, ppuY;
1248 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1249
3956d02a
JS
1250 int startXUnits, startYUnits;
1251 GetViewStart(& startXUnits, & startYUnits);
1252 int startY = startYUnits * ppuY;
5d7836c4 1253
8d7eaf91 1254 int sx = 0, sy = 0;
5d7836c4 1255 GetVirtualSize(& sx, & sy);
3956d02a
JS
1256 int sxUnits = 0;
1257 int syUnits = 0;
5d7836c4 1258 if (ppuY != 0)
3956d02a 1259 syUnits = sy/ppuY;
5d7836c4
JS
1260
1261 wxRect rect = line->GetRect();
1262
1263 bool scrolled = false;
1264
1265 wxSize clientSize = GetClientSize();
e9f10004
JS
1266 clientSize.y -= GetBuffer().GetBottomMargin();
1267
1268 if (GetWindowStyle() & wxRE_CENTRE_CARET)
1269 {
1270 int y = rect.y - GetClientSize().y/2;
1271 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1272 if (y >= 0 && (y + clientSize.y) < GetBuffer().GetCachedSize().y)
1273 {
1274 if (startYUnits != yUnits)
1275 {
1276 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1277 scrolled = true;
1278 }
1279#if !wxRICHTEXT_USE_OWN_CARET
1280 if (scrolled)
1281#endif
1282 PositionCaret();
1283
1284 return scrolled;
1285 }
1286 }
5d7836c4
JS
1287
1288 // Going down
3e3a754f 1289 if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN ||
ec36b155 1290 keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT ||
3e3a754f
JS
1291 keyCode == WXK_END || keyCode == WXK_NUMPAD_END ||
1292 keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
5d7836c4
JS
1293 {
1294 if ((rect.y + rect.height) > (clientSize.y + startY))
1295 {
1296 // Make it scroll so this item is at the bottom
1297 // of the window
1298 int y = rect.y - (clientSize.y - rect.height);
3956d02a 1299 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1300
3956d02a
JS
1301 // If we're still off the screen, scroll another line down
1302 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1303 yUnits ++;
1304
1305 if (startYUnits != yUnits)
c59f6793 1306 {
3956d02a 1307 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1308 scrolled = true;
1309 }
5d7836c4 1310 }
e9f10004 1311 else if (rect.y < (startY + GetBuffer().GetTopMargin()))
5d7836c4
JS
1312 {
1313 // Make it scroll so this item is at the top
1314 // of the window
e9f10004 1315 int y = rect.y - GetBuffer().GetTopMargin();
3956d02a 1316 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1317
3956d02a 1318 if (startYUnits != yUnits)
c59f6793 1319 {
3956d02a 1320 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1321 scrolled = true;
1322 }
5d7836c4 1323 }
5d7836c4
JS
1324 }
1325 // Going up
3e3a754f
JS
1326 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP ||
1327 keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT ||
1328 keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME ||
1329 keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP )
5d7836c4 1330 {
e9f10004 1331 if (rect.y < (startY + GetBuffer().GetBottomMargin()))
5d7836c4
JS
1332 {
1333 // Make it scroll so this item is at the top
1334 // of the window
e9f10004 1335 int y = rect.y - GetBuffer().GetTopMargin();
3956d02a 1336 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1337
3956d02a 1338 if (startYUnits != yUnits)
c59f6793 1339 {
3956d02a 1340 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1341 scrolled = true;
1342 }
5d7836c4
JS
1343 }
1344 else if ((rect.y + rect.height) > (clientSize.y + startY))
1345 {
1346 // Make it scroll so this item is at the bottom
1347 // of the window
1348 int y = rect.y - (clientSize.y - rect.height);
3956d02a
JS
1349 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1350
1351 // If we're still off the screen, scroll another line down
1352 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1353 yUnits ++;
c59f6793 1354
3956d02a 1355 if (startYUnits != yUnits)
c59f6793 1356 {
3956d02a 1357 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1358 scrolled = true;
1359 }
5d7836c4 1360 }
5d7836c4 1361 }
109bfc88 1362
1c13f06e 1363#if !wxRICHTEXT_USE_OWN_CARET
109bfc88 1364 if (scrolled)
1c13f06e 1365#endif
109bfc88 1366 PositionCaret();
5d7836c4
JS
1367
1368 return scrolled;
1369}
1370
1371/// Is the given position visible on the screen?
1372bool wxRichTextCtrl::IsPositionVisible(long pos) const
1373{
1374 wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1);
1375
1376 if (!line)
1377 return false;
1378
1379 int ppuX, ppuY;
1380 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1381
1382 int startX, startY;
1383 GetViewStart(& startX, & startY);
1384 startX = 0;
1385 startY = startY * ppuY;
1386
5d7836c4 1387 wxRect rect = line->GetRect();
5d7836c4 1388 wxSize clientSize = GetClientSize();
e9f10004 1389 clientSize.y -= GetBuffer().GetBottomMargin();
5d7836c4 1390
e9f10004 1391 return (rect.GetBottom() > (startY + GetBuffer().GetTopMargin())) && (rect.GetTop() < (startY + clientSize.y));
5d7836c4
JS
1392}
1393
1394void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart)
1395{
1396 m_caretPosition = position;
1397 m_caretAtLineStart = showAtLineStart;
1398}
1399
1400/// Move caret one visual step forward: this may mean setting a flag
1401/// and keeping the same position if we're going from the end of one line
1402/// to the start of the next, which may be the exact same caret position.
1403void wxRichTextCtrl::MoveCaretForward(long oldPosition)
1404{
1405 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1406
1407 // Only do the check if we're not at the end of the paragraph (where things work OK
1408 // anyway)
1409 if (para && (oldPosition != para->GetRange().GetEnd() - 1))
1410 {
1411 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1412
1413 if (line)
1414 {
1e967276
JS
1415 wxRichTextRange lineRange = line->GetAbsoluteRange();
1416
5d7836c4
JS
1417 // We're at the end of a line. See whether we need to
1418 // stay at the same actual caret position but change visual
1419 // position, or not.
1e967276 1420 if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
1421 {
1422 if (m_caretAtLineStart)
1423 {
1424 // We're already at the start of the line, so actually move on now.
1425 m_caretPosition = oldPosition + 1;
1426 m_caretAtLineStart = false;
1427 }
1428 else
1429 {
1430 // We're showing at the end of the line, so keep to
1431 // the same position but indicate that we're to show
1432 // at the start of the next line.
1433 m_caretPosition = oldPosition;
7fe8059f 1434 m_caretAtLineStart = true;
5d7836c4 1435 }
0a21bea0 1436 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1437 return;
1438 }
1439 }
1440 }
1441 m_caretPosition ++;
0a21bea0 1442 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1443}
1444
1445/// Move caret one visual step backward: this may mean setting a flag
1446/// and keeping the same position if we're going from the end of one line
1447/// to the start of the next, which may be the exact same caret position.
1448void wxRichTextCtrl::MoveCaretBack(long oldPosition)
1449{
1450 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1451
1452 // Only do the check if we're not at the start of the paragraph (where things work OK
1453 // anyway)
1454 if (para && (oldPosition != para->GetRange().GetStart()))
1455 {
1456 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1457
1458 if (line)
1459 {
1e967276
JS
1460 wxRichTextRange lineRange = line->GetAbsoluteRange();
1461
5d7836c4
JS
1462 // We're at the start of a line. See whether we need to
1463 // stay at the same actual caret position but change visual
1464 // position, or not.
1e967276 1465 if (oldPosition == lineRange.GetStart())
5d7836c4
JS
1466 {
1467 m_caretPosition = oldPosition-1;
1468 m_caretAtLineStart = true;
1469 return;
1470 }
1e967276 1471 else if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
1472 {
1473 if (m_caretAtLineStart)
1474 {
1475 // We're at the start of the line, so keep the same caret position
1476 // but clear the start-of-line flag.
1477 m_caretPosition = oldPosition;
1478 m_caretAtLineStart = false;
1479 }
1480 else
1481 {
1482 // We're showing at the end of the line, so go back
1483 // to the previous character position.
1484 m_caretPosition = oldPosition - 1;
1485 }
0a21bea0 1486 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1487 return;
1488 }
1489 }
1490 }
1491 m_caretPosition --;
0a21bea0 1492 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1493}
1494
1495/// Move right
1496bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
1497{
1498 long endPos = GetBuffer().GetRange().GetEnd();
1499
1500 if (m_caretPosition + noPositions < endPos)
1501 {
1502 long oldPos = m_caretPosition;
1503 long newPos = m_caretPosition + noPositions;
1504
1505 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1506 if (!extendSel)
1507 SelectNone();
1508
1509 // Determine by looking at oldPos and m_caretPosition whether
1510 // we moved from the end of a line to the start of the next line, in which case
1511 // we want to adjust the caret position such that it is positioned at the
1512 // start of the next line, rather than jumping past the first character of the
1513 // line.
1514 if (noPositions == 1 && !extendSel)
1515 MoveCaretForward(oldPos);
1516 else
1517 SetCaretPosition(newPos);
1518
1519 PositionCaret();
0a21bea0 1520 SetDefaultStyleToCursorStyle();
5d7836c4 1521
5d7836c4
JS
1522 return true;
1523 }
1524 else
7fe8059f 1525 return false;
5d7836c4
JS
1526}
1527
1528/// Move left
1529bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
1530{
1531 long startPos = -1;
1532
1533 if (m_caretPosition > startPos - noPositions + 1)
1534 {
1535 long oldPos = m_caretPosition;
1536 long newPos = m_caretPosition - noPositions;
1537 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1538 if (!extendSel)
1539 SelectNone();
1540
1541 if (noPositions == 1 && !extendSel)
1542 MoveCaretBack(oldPos);
1543 else
1544 SetCaretPosition(newPos);
1545
1546 PositionCaret();
0a21bea0 1547 SetDefaultStyleToCursorStyle();
5d7836c4 1548
5d7836c4
JS
1549 return true;
1550 }
1551 else
7fe8059f 1552 return false;
5d7836c4
JS
1553}
1554
1555/// Move up
1556bool wxRichTextCtrl::MoveUp(int noLines, int flags)
1557{
1558 return MoveDown(- noLines, flags);
1559}
1560
1561/// Move up
1562bool wxRichTextCtrl::MoveDown(int noLines, int flags)
1563{
c59f6793
JS
1564 if (!GetCaret())
1565 return false;
1566
5d7836c4
JS
1567 long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
1568 wxPoint pt = GetCaret()->GetPosition();
1569 long newLine = lineNumber + noLines;
1570
1571 if (lineNumber != -1)
1572 {
1573 if (noLines > 0)
1574 {
1575 long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1576
1577 if (newLine > lastLine)
1578 return false;
1579 }
1580 else
1581 {
1582 if (newLine < 0)
1583 return false;
1584 }
1585 }
1586
1587 wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine);
1588 if (lineObj)
7fe8059f
WS
1589 {
1590 pt.y = lineObj->GetAbsolutePosition().y + 2;
5d7836c4
JS
1591 }
1592 else
1593 return false;
1594
1595 long newPos = 0;
1596 wxClientDC dc(this);
1597 PrepareDC(dc);
1598 dc.SetFont(GetFont());
7fe8059f 1599
5d7836c4 1600 int hitTest = GetBuffer().HitTest(dc, pt, newPos);
7fe8059f 1601
5d7836c4
JS
1602 if (hitTest != wxRICHTEXT_HITTEST_NONE)
1603 {
1604 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1605 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1606 // so we view the caret at the start of the line.
1607 bool caretLineStart = false;
f262b25c 1608 if (hitTest & wxRICHTEXT_HITTEST_BEFORE)
5d7836c4
JS
1609 {
1610 wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1);
1e967276
JS
1611 wxRichTextRange lineRange;
1612 if (thisLine)
1613 lineRange = thisLine->GetAbsoluteRange();
1614
1615 if (thisLine && (newPos-1) == lineRange.GetEnd())
5d7836c4 1616 {
1e967276
JS
1617 newPos --;
1618 caretLineStart = true;
5d7836c4
JS
1619 }
1620 else
1621 {
1622 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos);
1623 if (para && para->GetRange().GetStart() == newPos)
1624 newPos --;
1625 }
1626 }
1627
1628 long newSelEnd = newPos;
1629
c59f6793
JS
1630 bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
1631 if (!extendSel)
5d7836c4
JS
1632 SelectNone();
1633
1634 SetCaretPosition(newPos, caretLineStart);
1635 PositionCaret();
0a21bea0 1636 SetDefaultStyleToCursorStyle();
5d7836c4 1637
5d7836c4
JS
1638 return true;
1639 }
41e155b4
WS
1640
1641 return false;
5d7836c4
JS
1642}
1643
1644/// Move to the end of the paragraph
1645bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1646{
1647 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1648 if (para)
1649 {
1650 long newPos = para->GetRange().GetEnd() - 1;
c59f6793
JS
1651 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1652 if (!extendSel)
5d7836c4
JS
1653 SelectNone();
1654
1655 SetCaretPosition(newPos);
1656 PositionCaret();
0a21bea0 1657 SetDefaultStyleToCursorStyle();
5d7836c4 1658
5d7836c4
JS
1659 return true;
1660 }
1661
1662 return false;
1663}
1664
1665/// Move to the start of the paragraph
1666bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1667{
1668 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1669 if (para)
1670 {
1671 long newPos = para->GetRange().GetStart() - 1;
c59f6793
JS
1672 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1673 if (!extendSel)
5d7836c4
JS
1674 SelectNone();
1675
1676 SetCaretPosition(newPos);
1677 PositionCaret();
0a21bea0 1678 SetDefaultStyleToCursorStyle();
5d7836c4 1679
5d7836c4
JS
1680 return true;
1681 }
1682
1683 return false;
1684}
1685
1686/// Move to the end of the line
1687bool wxRichTextCtrl::MoveToLineEnd(int flags)
1688{
1689 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1690
1691 if (line)
1692 {
1e967276
JS
1693 wxRichTextRange lineRange = line->GetAbsoluteRange();
1694 long newPos = lineRange.GetEnd();
c59f6793
JS
1695 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1696 if (!extendSel)
5d7836c4
JS
1697 SelectNone();
1698
1699 SetCaretPosition(newPos);
1700 PositionCaret();
0a21bea0 1701 SetDefaultStyleToCursorStyle();
5d7836c4 1702
5d7836c4
JS
1703 return true;
1704 }
1705
1706 return false;
1707}
1708
1709/// Move to the start of the line
1710bool wxRichTextCtrl::MoveToLineStart(int flags)
1711{
1712 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1713 if (line)
1714 {
1e967276
JS
1715 wxRichTextRange lineRange = line->GetAbsoluteRange();
1716 long newPos = lineRange.GetStart()-1;
5d7836c4 1717
c59f6793
JS
1718 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1719 if (!extendSel)
5d7836c4
JS
1720 SelectNone();
1721
1722 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
1723
1e967276 1724 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4 1725 PositionCaret();
0a21bea0 1726 SetDefaultStyleToCursorStyle();
5d7836c4 1727
5d7836c4
JS
1728 return true;
1729 }
1730
1731 return false;
1732}
1733
1734/// Move to the start of the buffer
1735bool wxRichTextCtrl::MoveHome(int flags)
1736{
1737 if (m_caretPosition != -1)
1738 {
c59f6793
JS
1739 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
1740 if (!extendSel)
5d7836c4
JS
1741 SelectNone();
1742
1743 SetCaretPosition(-1);
1744 PositionCaret();
0a21bea0 1745 SetDefaultStyleToCursorStyle();
5d7836c4 1746
5d7836c4
JS
1747 return true;
1748 }
1749 else
7fe8059f 1750 return false;
5d7836c4
JS
1751}
1752
1753/// Move to the end of the buffer
1754bool wxRichTextCtrl::MoveEnd(int flags)
1755{
1756 long endPos = GetBuffer().GetRange().GetEnd()-1;
1757
1758 if (m_caretPosition != endPos)
1759 {
c59f6793
JS
1760 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
1761 if (!extendSel)
5d7836c4
JS
1762 SelectNone();
1763
1764 SetCaretPosition(endPos);
1765 PositionCaret();
0a21bea0 1766 SetDefaultStyleToCursorStyle();
5d7836c4 1767
5d7836c4
JS
1768 return true;
1769 }
1770 else
7fe8059f 1771 return false;
5d7836c4
JS
1772}
1773
1774/// Move noPages pages up
1775bool wxRichTextCtrl::PageUp(int noPages, int flags)
1776{
1777 return PageDown(- noPages, flags);
1778}
1779
1780/// Move noPages pages down
1781bool wxRichTextCtrl::PageDown(int noPages, int flags)
1782{
1783 // Calculate which line occurs noPages * screen height further down.
1784 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1785 if (line)
1786 {
1787 wxSize clientSize = GetClientSize();
1788 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
1789
1790 wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY);
1791 if (newLine)
1792 {
1e967276
JS
1793 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
1794 long pos = lineRange.GetStart()-1;
5d7836c4
JS
1795 if (pos != m_caretPosition)
1796 {
1797 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
1798
c59f6793
JS
1799 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1800 if (!extendSel)
5d7836c4
JS
1801 SelectNone();
1802
1e967276 1803 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4 1804 PositionCaret();
0a21bea0 1805 SetDefaultStyleToCursorStyle();
5d7836c4 1806
5d7836c4
JS
1807 return true;
1808 }
1809 }
1810 }
1811
1812 return false;
1813}
1814
0871b343
JS
1815static bool wxRichTextCtrlIsWhitespace(const wxString& str)
1816{
1817 return str == wxT(" ") || str == wxT("\t");
1818}
1819
5d7836c4
JS
1820// Finds the caret position for the next word
1821long wxRichTextCtrl::FindNextWordPosition(int direction) const
7fe8059f 1822{
5d7836c4
JS
1823 long endPos = GetBuffer().GetRange().GetEnd();
1824
1825 if (direction > 0)
1826 {
1827 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
7fe8059f 1828
5d7836c4
JS
1829 // First skip current text to space
1830 while (i < endPos && i > -1)
1831 {
1832 // i is in character, not caret positions
1833 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
0871b343
JS
1834 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1835 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1836 {
1837 break;
1838 }
1839 else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty())
5d7836c4
JS
1840 i += direction;
1841 else
1842 {
1843 break;
1844 }
1845 }
1846 while (i < endPos && i > -1)
1847 {
1848 // i is in character, not caret positions
1849 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
0871b343
JS
1850 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1851 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1852 return wxMax(-1, i);
1853
7fe8059f 1854 if (text.empty()) // End of paragraph, or maybe an image
5d7836c4 1855 return wxMax(-1, i - 1);
0871b343 1856 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
5d7836c4
JS
1857 i += direction;
1858 else
1859 {
1860 // Convert to caret position
1861 return wxMax(-1, i - 1);
1862 }
1863 }
1864 if (i >= endPos)
1865 return endPos-1;
1866 return i-1;
1867 }
1868 else
1869 {
1870 long i = m_caretPosition;
1871
1872 // First skip white space
1873 while (i < endPos && i > -1)
1874 {
1875 // i is in character, not caret positions
1876 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
0871b343
JS
1877 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1878
1879 if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
5d7836c4 1880 break;
0871b343 1881 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
5d7836c4
JS
1882 i += direction;
1883 else
1884 break;
1885 }
1886 // Next skip current text to space
1887 while (i < endPos && i > -1)
1888 {
1889 // i is in character, not caret positions
1890 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
0871b343
JS
1891 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1892 if (line && line->GetAbsoluteRange().GetStart() == i)
1893 return i-1;
1894
1895 if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */)
5d7836c4
JS
1896 i += direction;
1897 else
1898 {
1899 return i;
5d7836c4
JS
1900 }
1901 }
1902 if (i < -1)
1903 return -1;
1904 return i;
1905 }
1906}
1907
1908/// Move n words left
1909bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
1910{
1911 long pos = FindNextWordPosition(-1);
1912 if (pos != m_caretPosition)
1913 {
1914 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
7fe8059f 1915
c59f6793
JS
1916 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1917 if (!extendSel)
5d7836c4 1918 SelectNone();
7fe8059f 1919
5d7836c4
JS
1920 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1921 PositionCaret();
0a21bea0 1922 SetDefaultStyleToCursorStyle();
5d7836c4 1923
5d7836c4
JS
1924 return true;
1925 }
7fe8059f 1926
5d7836c4
JS
1927 return false;
1928}
1929
1930/// Move n words right
1931bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
1932{
1933 long pos = FindNextWordPosition(1);
1934 if (pos != m_caretPosition)
1935 {
1936 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
7fe8059f 1937
c59f6793
JS
1938 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1939 if (!extendSel)
5d7836c4 1940 SelectNone();
7fe8059f 1941
5d7836c4
JS
1942 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1943 PositionCaret();
0a21bea0 1944 SetDefaultStyleToCursorStyle();
5d7836c4 1945
5d7836c4
JS
1946 return true;
1947 }
7fe8059f 1948
5d7836c4
JS
1949 return false;
1950}
1951
1952/// Sizing
1953void wxRichTextCtrl::OnSize(wxSizeEvent& event)
1954{
4d551ad5
JS
1955 // Only do sizing optimization for large buffers
1956 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold)
1957 {
1958 m_fullLayoutRequired = true;
1959 m_fullLayoutTime = wxGetLocalTimeMillis();
1960 m_fullLayoutSavedPosition = GetFirstVisiblePosition();
2f36e8dc 1961 LayoutContent(true /* onlyVisibleRect */);
4d551ad5
JS
1962 }
1963 else
1964 GetBuffer().Invalidate(wxRICHTEXT_ALL);
7fe8059f 1965
3e3a754f 1966#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4 1967 RecreateBuffer();
3e3a754f 1968#endif
5d7836c4
JS
1969
1970 event.Skip();
1971}
1972
4d551ad5
JS
1973
1974/// Idle-time processing
1975void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
1976{
1c13f06e
JS
1977#if wxRICHTEXT_USE_OWN_CARET
1978 if (((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
1979 {
1980 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(false);
1981 PositionCaret();
1982 GetCaret()->Show();
1983 }
1984#endif
1985
4d551ad5
JS
1986 const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL;
1987
1988 if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval)))
1989 {
1990 m_fullLayoutRequired = false;
1991 m_fullLayoutTime = 0;
1992 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1993 ShowPosition(m_fullLayoutSavedPosition);
76bcd815 1994 Refresh(false);
4d551ad5 1995 }
ab14c7aa
JS
1996
1997 if (m_caretPositionForDefaultStyle != -2)
1998 {
1999 // If the caret position has changed, no longer reflect the default style
2000 // in the UI.
2001 if (GetCaretPosition() != m_caretPositionForDefaultStyle)
2002 m_caretPositionForDefaultStyle = -2;
2003 }
2004
4d551ad5
JS
2005 event.Skip();
2006}
2007
c59f6793
JS
2008/// Scrolling
2009void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
2010{
1c13f06e
JS
2011#if wxRICHTEXT_USE_OWN_CARET
2012 if (!((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
2013 {
2014 GetCaret()->Hide();
2015 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate();
2016 }
2017#endif
2018
c59f6793
JS
2019 event.Skip();
2020}
2021
5d7836c4 2022/// Set up scrollbars, e.g. after a resize
169adfa9 2023void wxRichTextCtrl::SetupScrollbars(bool atTop)
5d7836c4 2024{
17808a75 2025 if (IsFrozen())
5d7836c4
JS
2026 return;
2027
2028 if (GetBuffer().IsEmpty())
2029 {
2030 SetScrollbars(0, 0, 0, 0, 0, 0);
2031 return;
2032 }
2033
2034 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2035 // of pixels. See e.g. wxVScrolledWindow for ideas.
3956d02a 2036 int pixelsPerUnit = 5;
5d7836c4
JS
2037 wxSize clientSize = GetClientSize();
2038
e9f10004 2039 int maxHeight = GetBuffer().GetCachedSize().y + GetBuffer().GetTopMargin();
5d7836c4 2040
3956d02a
JS
2041 // Round up so we have at least maxHeight pixels
2042 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
5d7836c4 2043
169adfa9
JS
2044 int startX = 0, startY = 0;
2045 if (!atTop)
2046 GetViewStart(& startX, & startY);
7fe8059f 2047
7051fa41 2048 int maxPositionX = 0;
3956d02a 2049 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
7fe8059f 2050
7051fa41
JS
2051 int newStartX = wxMin(maxPositionX, startX);
2052 int newStartY = wxMin(maxPositionY, startY);
2053
2054 int oldPPUX, oldPPUY;
2055 int oldStartX, oldStartY;
2056 int oldVirtualSizeX = 0, oldVirtualSizeY = 0;
2057 GetScrollPixelsPerUnit(& oldPPUX, & oldPPUY);
2058 GetViewStart(& oldStartX, & oldStartY);
2059 GetVirtualSize(& oldVirtualSizeX, & oldVirtualSizeY);
2060 if (oldPPUY > 0)
2061 oldVirtualSizeY /= oldPPUY;
2062
2063 if (oldPPUX == 0 && oldPPUY == pixelsPerUnit && oldVirtualSizeY == unitsY && oldStartX == newStartX && oldStartY == newStartY)
2064 return;
2065
2066 // Don't set scrollbars if there were none before, and there will be none now.
2067 if (oldPPUY != 0 && (oldVirtualSizeY < clientSize.y) && (unitsY*pixelsPerUnit < clientSize.y))
2068 return;
2069
5d7836c4
JS
2070 // Move to previous scroll position if
2071 // possible
7051fa41 2072 SetScrollbars(0, pixelsPerUnit, 0, unitsY, newStartX, newStartY);
5d7836c4
JS
2073}
2074
2075/// Paint the background
2076void wxRichTextCtrl::PaintBackground(wxDC& dc)
2077{
2078 wxColour backgroundColour = GetBackgroundColour();
2079 if (!backgroundColour.Ok())
2080 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
2081
2082 // Clear the background
2083 dc.SetBrush(wxBrush(backgroundColour));
2084 dc.SetPen(*wxTRANSPARENT_PEN);
7fe8059f 2085 wxRect windowRect(GetClientSize());
5d7836c4
JS
2086 windowRect.x -= 2; windowRect.y -= 2;
2087 windowRect.width += 4; windowRect.height += 4;
7fe8059f 2088
5d7836c4
JS
2089 // We need to shift the rectangle to take into account
2090 // scrolling. Converting device to logical coordinates.
2091 CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y);
2092 dc.DrawRectangle(windowRect);
2093}
2094
3e3a754f 2095#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4
JS
2096/// Recreate buffer bitmap if necessary
2097bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
2098{
2099 wxSize sz = size;
2100 if (sz == wxDefaultSize)
2101 sz = GetClientSize();
7fe8059f 2102
5d7836c4
JS
2103 if (sz.x < 1 || sz.y < 1)
2104 return false;
7fe8059f 2105
5d7836c4
JS
2106 if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
2107 m_bufferBitmap = wxBitmap(sz.x, sz.y);
2108 return m_bufferBitmap.Ok();
2109}
3e3a754f 2110#endif
5d7836c4
JS
2111
2112// ----------------------------------------------------------------------------
2113// file IO functions
2114// ----------------------------------------------------------------------------
2115
3306aec1
JS
2116bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
2117{
d75a69e8 2118 bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType);
5d7836c4
JS
2119 if (success)
2120 m_filename = filename;
2121
2122 DiscardEdits();
2123 SetInsertionPoint(0);
2f36e8dc 2124 LayoutContent();
5d7836c4 2125 PositionCaret();
169adfa9 2126 SetupScrollbars(true);
76bcd815 2127 Refresh(false);
0ec1179b 2128 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
2129
2130 if (success)
2131 return true;
2132 else
2133 {
2134 wxLogError(_("File couldn't be loaded."));
7fe8059f 2135
5d7836c4
JS
2136 return false;
2137 }
2138}
2139
3306aec1 2140bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
5d7836c4 2141{
d75a69e8 2142 if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType))
5d7836c4 2143 {
3306aec1 2144 m_filename = filename;
5d7836c4
JS
2145
2146 DiscardEdits();
2147
2148 return true;
5d7836c4
JS
2149 }
2150
2151 wxLogError(_("The text couldn't be saved."));
2152
2153 return false;
2154}
2155
2156// ----------------------------------------------------------------------------
2157// wxRichTextCtrl specific functionality
2158// ----------------------------------------------------------------------------
2159
2160/// Add a new paragraph of text to the end of the buffer
2161wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
2162{
62381daa
JS
2163 wxRichTextRange range = GetBuffer().AddParagraph(text);
2164 LayoutContent();
2165 return range;
5d7836c4
JS
2166}
2167
2168/// Add an image
2169wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
2170{
62381daa
JS
2171 wxRichTextRange range = GetBuffer().AddImage(image);
2172 LayoutContent();
2173 return range;
5d7836c4
JS
2174}
2175
2176// ----------------------------------------------------------------------------
2177// selection and ranges
2178// ----------------------------------------------------------------------------
2179
2180void wxRichTextCtrl::SelectAll()
2181{
96c9f0f6 2182 SetSelection(0, GetLastPosition()+1);
5d7836c4
JS
2183 m_selectionAnchor = -1;
2184}
2185
2186/// Select none
2187void wxRichTextCtrl::SelectNone()
2188{
c59f6793 2189 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
c1aec196 2190 {
7051fa41
JS
2191 wxRichTextRange oldSelection = m_selectionRange;
2192
c1aec196 2193 m_selectionRange = wxRichTextRange(-2, -2);
7051fa41
JS
2194
2195 RefreshForSelectionChange(oldSelection, m_selectionRange);
c1aec196 2196 }
5d7836c4
JS
2197 m_selectionAnchor = -2;
2198}
2199
0ca07313
JS
2200static bool wxIsWordDelimiter(const wxString& text)
2201{
1c07e924 2202 return !text.IsEmpty() && !wxIsalnum(text[0]);
0ca07313
JS
2203}
2204
2205/// Select the word at the given character position
2206bool wxRichTextCtrl::SelectWord(long position)
2207{
2208 if (position < 0 || position > GetBuffer().GetRange().GetEnd())
2209 return false;
1807a1f3 2210
0ca07313
JS
2211 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
2212 if (!para)
2213 return false;
2214
62381daa
JS
2215 if (position == para->GetRange().GetEnd())
2216 position --;
2217
0ca07313
JS
2218 long positionStart = position;
2219 long positionEnd = position;
1807a1f3 2220
0ca07313
JS
2221 for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --)
2222 {
2223 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionStart, positionStart));
2224 if (wxIsWordDelimiter(text))
2225 {
2226 positionStart ++;
2227 break;
2228 }
2229 }
2230 if (positionStart < para->GetRange().GetStart())
2231 positionStart = para->GetRange().GetStart();
1807a1f3 2232
0ca07313
JS
2233 for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++)
2234 {
2235 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionEnd, positionEnd));
2236 if (wxIsWordDelimiter(text))
2237 {
2238 positionEnd --;
2239 break;
2240 }
2241 }
2242 if (positionEnd >= para->GetRange().GetEnd())
2243 positionEnd = para->GetRange().GetEnd();
1807a1f3 2244
62381daa
JS
2245 if (positionEnd < positionStart)
2246 return false;
2247
0ca07313
JS
2248 SetSelection(positionStart, positionEnd+1);
2249
2250 if (positionStart >= 0)
2251 {
2252 MoveCaret(positionStart-1, true);
0a21bea0 2253 SetDefaultStyleToCursorStyle();
0ca07313
JS
2254 }
2255
2256 return true;
2257}
2258
5d7836c4
JS
2259wxString wxRichTextCtrl::GetStringSelection() const
2260{
2261 long from, to;
2262 GetSelection(&from, &to);
2263
2264 return GetRange(from, to);
2265}
2266
5d7836c4
JS
2267// ----------------------------------------------------------------------------
2268// hit testing
2269// ----------------------------------------------------------------------------
2270
2271wxTextCtrlHitTestResult
2272wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
2273{
2274 // implement in terms of the other overload as the native ports typically
2275 // can get the position and not (x, y) pair directly (although wxUniv
2276 // directly gets x and y -- and so overrides this method as well)
2277 long pos;
2278 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
2279
2280 if ( rc != wxTE_HT_UNKNOWN )
2281 {
2282 PositionToXY(pos, x, y);
2283 }
2284
2285 return rc;
2286}
2287
2288wxTextCtrlHitTestResult
2289wxRichTextCtrl::HitTest(const wxPoint& pt,
2290 long * pos) const
2291{
2292 wxClientDC dc((wxRichTextCtrl*) this);
2293 ((wxRichTextCtrl*)this)->PrepareDC(dc);
61399247 2294
563d25ba
JS
2295 // Buffer uses logical position (relative to start of buffer)
2296 // so convert
2297 wxPoint pt2 = GetLogicalPoint(pt);
5d7836c4 2298
563d25ba 2299 int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos);
41e155b4 2300
f262b25c
JS
2301 if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2302 return wxTE_HT_BEFORE;
2303 else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2304 return wxTE_HT_BEYOND;
25d6eb3a 2305 else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER))
f262b25c 2306 return wxTE_HT_ON_TEXT;
41e155b4
WS
2307
2308 return wxTE_HT_UNKNOWN;
5d7836c4
JS
2309}
2310
2311// ----------------------------------------------------------------------------
2312// set/get the controls text
2313// ----------------------------------------------------------------------------
2314
805c8d92 2315wxString wxRichTextCtrl::DoGetValue() const
5d7836c4
JS
2316{
2317 return GetBuffer().GetText();
2318}
2319
2320wxString wxRichTextCtrl::GetRange(long from, long to) const
2321{
96c9f0f6
JS
2322 // Public API for range is different from internals
2323 return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1));
5d7836c4
JS
2324}
2325
343ae70d 2326void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
5d7836c4 2327{
d081739d
JS
2328 // Don't call Clear here, since it always sends a text updated event
2329 m_buffer.ResetAndClearCommands();
2330 m_buffer.SetDirty(true);
2331 m_caretPosition = -1;
2332 m_caretPositionForDefaultStyle = -2;
2333 m_caretAtLineStart = false;
2334 m_selectionRange.SetRange(-2, -2);
2335
2336 Scroll(0,0);
2337
2338 if (!IsFrozen())
2339 {
2340 LayoutContent();
2341 Refresh(false);
2342 }
5d7836c4 2343
1a75935d 2344 if (!value.IsEmpty())
5d7836c4 2345 {
1a75935d
JS
2346 // Remove empty paragraph
2347 GetBuffer().Clear();
d081739d 2348 DoWriteText(value, flags);
5d7836c4
JS
2349
2350 // for compatibility, don't move the cursor when doing SetValue()
2351 SetInsertionPoint(0);
2352 }
1a75935d 2353 else
5d7836c4 2354 {
1a75935d
JS
2355 // still send an event for consistency
2356 if (flags & SetValue_SendEvent)
0ec1179b 2357 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4 2358 }
5d7836c4
JS
2359 DiscardEdits();
2360}
2361
2362void wxRichTextCtrl::WriteText(const wxString& value)
2363{
2364 DoWriteText(value);
2365}
2366
343ae70d 2367void wxRichTextCtrl::DoWriteText(const wxString& value, int flags)
5d7836c4 2368{
0ca07313 2369 wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix);
fe5aa22c 2370
33dddf58 2371 GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
343ae70d
VZ
2372
2373 if ( flags & SetValue_SendEvent )
0ec1179b 2374 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
2375}
2376
2377void wxRichTextCtrl::AppendText(const wxString& text)
2378{
2379 SetInsertionPointEnd();
2380
2381 WriteText(text);
2382}
2383
2384/// Write an image at the current insertion point
d75a69e8 2385bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType)
5d7836c4
JS
2386{
2387 wxRichTextImageBlock imageBlock;
2388
2389 wxImage image2 = image;
2390 if (imageBlock.MakeImageBlock(image2, bitmapType))
2391 return WriteImage(imageBlock);
41e155b4
WS
2392
2393 return false;
5d7836c4
JS
2394}
2395
d75a69e8 2396bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType)
5d7836c4
JS
2397{
2398 wxRichTextImageBlock imageBlock;
2399
2400 wxImage image;
2401 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
2402 return WriteImage(imageBlock);
41e155b4
WS
2403
2404 return false;
5d7836c4
JS
2405}
2406
2407bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
2408{
2409 return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
2410}
2411
d75a69e8 2412bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType)
5d7836c4
JS
2413{
2414 if (bitmap.Ok())
2415 {
2416 wxRichTextImageBlock imageBlock;
7fe8059f 2417
5d7836c4
JS
2418 wxImage image = bitmap.ConvertToImage();
2419 if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType))
2420 return WriteImage(imageBlock);
5d7836c4 2421 }
41e155b4 2422
5d7836c4
JS
2423 return false;
2424}
2425
2426/// Insert a newline (actually paragraph) at the current insertion point.
2427bool wxRichTextCtrl::Newline()
2428{
5ef8ac72 2429 return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
5d7836c4
JS
2430}
2431
ff76711f
JS
2432/// Insert a line break at the current insertion point.
2433bool wxRichTextCtrl::LineBreak()
2434{
2435 wxString text;
2436 text = wxRichTextLineBreakChar;
2437 return GetBuffer().InsertTextWithUndo(m_caretPosition+1, text, this);
2438}
5d7836c4
JS
2439
2440// ----------------------------------------------------------------------------
2441// Clipboard operations
2442// ----------------------------------------------------------------------------
2443
2444void wxRichTextCtrl::Copy()
2445{
2446 if (CanCopy())
2447 {
fe5aa22c 2448 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2449 GetBuffer().CopyToClipboard(range);
2450 }
2451}
2452
2453void wxRichTextCtrl::Cut()
2454{
2455 if (CanCut())
2456 {
fe5aa22c 2457 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2458 GetBuffer().CopyToClipboard(range);
2459
2460 DeleteSelectedContent();
2f36e8dc 2461 LayoutContent();
76bcd815 2462 Refresh(false);
5d7836c4
JS
2463 }
2464}
2465
2466void wxRichTextCtrl::Paste()
2467{
2468 if (CanPaste())
2469 {
2470 BeginBatchUndo(_("Paste"));
7fe8059f 2471
5d7836c4
JS
2472 long newPos = m_caretPosition;
2473 DeleteSelectedContent(& newPos);
7fe8059f 2474
5d7836c4
JS
2475 GetBuffer().PasteFromClipboard(newPos);
2476
2477 EndBatchUndo();
2478 }
2479}
2480
2481void wxRichTextCtrl::DeleteSelection()
2482{
2483 if (CanDeleteSelection())
2484 {
2485 DeleteSelectedContent();
2486 }
2487}
2488
2489bool wxRichTextCtrl::HasSelection() const
2490{
2491 return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2;
2492}
2493
2494bool wxRichTextCtrl::CanCopy() const
2495{
2496 // Can copy if there's a selection
2497 return HasSelection();
2498}
2499
2500bool wxRichTextCtrl::CanCut() const
2501{
2502 return HasSelection() && IsEditable();
2503}
2504
2505bool wxRichTextCtrl::CanPaste() const
2506{
2507 if ( !IsEditable() )
2508 return false;
2509
2510 return GetBuffer().CanPasteFromClipboard();
2511}
2512
2513bool wxRichTextCtrl::CanDeleteSelection() const
2514{
2515 return HasSelection() && IsEditable();
2516}
2517
2518
2519// ----------------------------------------------------------------------------
2520// Accessors
2521// ----------------------------------------------------------------------------
2522
2523void wxRichTextCtrl::SetEditable(bool editable)
2524{
2525 m_editable = editable;
2526}
2527
2528void wxRichTextCtrl::SetInsertionPoint(long pos)
2529{
2530 SelectNone();
2531
2532 m_caretPosition = pos - 1;
6f5d63bc
JS
2533
2534 PositionCaret();
5d7836c4
JS
2535}
2536
2537void wxRichTextCtrl::SetInsertionPointEnd()
2538{
2539 long pos = GetLastPosition();
2540 SetInsertionPoint(pos);
2541}
2542
2543long wxRichTextCtrl::GetInsertionPoint() const
2544{
2545 return m_caretPosition+1;
2546}
2547
2548wxTextPos wxRichTextCtrl::GetLastPosition() const
2549{
2550 return GetBuffer().GetRange().GetEnd();
2551}
2552
2553// If the return values from and to are the same, there is no
2554// selection.
2555void wxRichTextCtrl::GetSelection(long* from, long* to) const
2556{
2557 *from = m_selectionRange.GetStart();
2558 *to = m_selectionRange.GetEnd();
96c9f0f6
JS
2559 if ((*to) != -1 && (*to) != -2)
2560 (*to) ++;
5d7836c4
JS
2561}
2562
2563bool wxRichTextCtrl::IsEditable() const
2564{
2565 return m_editable;
2566}
2567
2568// ----------------------------------------------------------------------------
2569// selection
2570// ----------------------------------------------------------------------------
2571
2572void wxRichTextCtrl::SetSelection(long from, long to)
2573{
2574 // if from and to are both -1, it means (in wxWidgets) that all text should
2575 // be selected.
2576 if ( (from == -1) && (to == -1) )
2577 {
2578 from = 0;
96c9f0f6 2579 to = GetLastPosition()+1;
5d7836c4
JS
2580 }
2581
c1aec196
JS
2582 if (from == to)
2583 {
2584 SelectNone();
2585 }
2586 else
2587 {
7051fa41 2588 wxRichTextRange oldSelection = m_selectionRange;
c1aec196
JS
2589 m_selectionAnchor = from;
2590 m_selectionRange.SetRange(from, to-1);
2591 if (from > -2)
2592 m_caretPosition = from-1;
0ca07313 2593
7051fa41 2594 RefreshForSelectionChange(oldSelection, m_selectionRange);
c1aec196
JS
2595 PositionCaret();
2596 }
5d7836c4
JS
2597}
2598
2599// ----------------------------------------------------------------------------
2600// Editing
2601// ----------------------------------------------------------------------------
2602
343ae70d
VZ
2603void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to),
2604 const wxString& value)
5d7836c4
JS
2605{
2606 BeginBatchUndo(_("Replace"));
2607
2608 DeleteSelectedContent();
2609
343ae70d 2610 DoWriteText(value, SetValue_SelectionOnly);
5d7836c4
JS
2611
2612 EndBatchUndo();
2613}
2614
2615void wxRichTextCtrl::Remove(long from, long to)
2616{
2617 SelectNone();
2618
4b3483e7 2619 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to-1), this);
5d7836c4 2620
2f36e8dc 2621 LayoutContent();
5d7836c4 2622 if (!IsFrozen())
76bcd815 2623 Refresh(false);
5d7836c4
JS
2624}
2625
2626bool wxRichTextCtrl::IsModified() const
2627{
2628 return m_buffer.IsModified();
2629}
2630
2631void wxRichTextCtrl::MarkDirty()
2632{
2633 m_buffer.Modify(true);
2634}
2635
2636void wxRichTextCtrl::DiscardEdits()
2637{
ab14c7aa 2638 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
2639 m_buffer.Modify(false);
2640 m_buffer.GetCommandProcessor()->ClearCommands();
2641}
2642
2643int wxRichTextCtrl::GetNumberOfLines() const
2644{
2645 return GetBuffer().GetParagraphCount();
2646}
2647
2648// ----------------------------------------------------------------------------
2649// Positions <-> coords
2650// ----------------------------------------------------------------------------
2651
2652long wxRichTextCtrl::XYToPosition(long x, long y) const
2653{
2654 return GetBuffer().XYToPosition(x, y);
2655}
2656
2657bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
2658{
2659 return GetBuffer().PositionToXY(pos, x, y);
2660}
2661
2662// ----------------------------------------------------------------------------
2663//
2664// ----------------------------------------------------------------------------
2665
2666void wxRichTextCtrl::ShowPosition(long pos)
2667{
2668 if (!IsPositionVisible(pos))
2669 ScrollIntoView(pos-1, WXK_DOWN);
2670}
2671
2672int wxRichTextCtrl::GetLineLength(long lineNo) const
2673{
2674 return GetBuffer().GetParagraphLength(lineNo);
2675}
2676
2677wxString wxRichTextCtrl::GetLineText(long lineNo) const
2678{
2679 return GetBuffer().GetParagraphText(lineNo);
2680}
2681
2682// ----------------------------------------------------------------------------
2683// Undo/redo
2684// ----------------------------------------------------------------------------
2685
2686void wxRichTextCtrl::Undo()
2687{
2688 if (CanUndo())
2689 {
2690 GetCommandProcessor()->Undo();
2691 }
2692}
2693
2694void wxRichTextCtrl::Redo()
2695{
2696 if (CanRedo())
2697 {
2698 GetCommandProcessor()->Redo();
2699 }
2700}
2701
2702bool wxRichTextCtrl::CanUndo() const
2703{
2704 return GetCommandProcessor()->CanUndo();
2705}
2706
2707bool wxRichTextCtrl::CanRedo() const
2708{
2709 return GetCommandProcessor()->CanRedo();
2710}
2711
2712// ----------------------------------------------------------------------------
563d25ba 2713// implementation details
5d7836c4
JS
2714// ----------------------------------------------------------------------------
2715
563d25ba 2716void wxRichTextCtrl::Command(wxCommandEvent& event)
5d7836c4
JS
2717{
2718 SetValue(event.GetString());
2719 GetEventHandler()->ProcessEvent(event);
2720}
2721
2722void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
2723{
2724 // By default, load the first file into the text window.
2725 if (event.GetNumberOfFiles() > 0)
2726 {
2727 LoadFile(event.GetFiles()[0]);
2728 }
2729}
2730
5d7836c4
JS
2731wxSize wxRichTextCtrl::DoGetBestSize() const
2732{
2733 return wxSize(10, 10);
2734}
2735
2736// ----------------------------------------------------------------------------
2737// standard handlers for standard edit menu events
2738// ----------------------------------------------------------------------------
2739
2740void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2741{
2742 Cut();
2743}
2744
2745void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
2746{
2747 DeleteSelection();
2748}
2749
2750void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2751{
2752 Copy();
2753}
2754
2755void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2756{
2757 Paste();
2758}
2759
2760void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2761{
2762 Undo();
2763}
2764
2765void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2766{
2767 Redo();
2768}
2769
2770void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2771{
2772 event.Enable( CanCut() );
2773}
2774
2775void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2776{
2777 event.Enable( CanCopy() );
2778}
2779
2780void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2781{
2782 event.Enable( CanDeleteSelection() );
2783}
2784
2785void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2786{
2787 event.Enable( CanPaste() );
2788}
2789
2790void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2791{
2792 event.Enable( CanUndo() );
2793 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2794}
2795
2796void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2797{
2798 event.Enable( CanRedo() );
2799 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2800}
2801
2802void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2803{
2804 SelectAll();
2805}
2806
2807void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2808{
2809 event.Enable(GetLastPosition() > 0);
2810}
2811
b4ae0def 2812void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event)
5d7836c4 2813{
b4ae0def
JS
2814 if (event.GetEventObject() != this)
2815 {
2816 event.Skip();
2817 return;
2818 }
2819
5d7836c4
JS
2820 if (!m_contextMenu)
2821 {
2822 m_contextMenu = new wxMenu;
2823 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
2824 m_contextMenu->Append(wxID_REDO, _("&Redo"));
2825 m_contextMenu->AppendSeparator();
2826 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
2827 m_contextMenu->Append(wxID_COPY, _("&Copy"));
2828 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
2829 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
2830 m_contextMenu->AppendSeparator();
2831 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
2832 }
2833 PopupMenu(m_contextMenu);
2834 return;
2835}
2836
27e20452
JS
2837bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2838{
44cc96a8 2839 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttr(style));
27e20452
JS
2840}
2841
44cc96a8 2842bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style)
5d7836c4 2843{
96c9f0f6 2844 return GetBuffer().SetStyle(range.ToInternal(), style);
5d7836c4
JS
2845}
2846
3e3a754f
JS
2847// extended style setting operation with flags including:
2848// wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2849// see richtextbuffer.h for more details.
3e3a754f 2850
44cc96a8 2851bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttr& style, int flags)
3e3a754f
JS
2852{
2853 return GetBuffer().SetStyle(range.ToInternal(), style, flags);
2854}
2855
27e20452
JS
2856bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2857{
44cc96a8 2858 return GetBuffer().SetDefaultStyle(wxTextAttr(style));
5d7836c4
JS
2859}
2860
27e20452
JS
2861const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const
2862{
61399247 2863 return GetBuffer().GetDefaultStyle();
27e20452
JS
2864}
2865
3966a9f4 2866bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
5d7836c4
JS
2867{
2868 return GetBuffer().GetStyle(position, style);
2869}
2870
a7ed48a5 2871// get the common set of styles for the range
44cc96a8 2872bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style)
a7ed48a5
JS
2873{
2874 return GetBuffer().GetStyleForRange(range.ToInternal(), style);
2875}
2876
fe5aa22c 2877/// Get the content (uncombined) attributes for this position.
fe5aa22c 2878bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style)
fe5aa22c
JS
2879{
2880 return GetBuffer().GetUncombinedStyle(position, style);
2881}
2882
5d7836c4
JS
2883/// Set font, and also the buffer attributes
2884bool wxRichTextCtrl::SetFont(const wxFont& font)
2885{
f5172305 2886 wxTextCtrlBase::SetFont(font);
5d7836c4 2887
44cc96a8 2888 wxTextAttr attr = GetBuffer().GetAttributes();
5d7836c4
JS
2889 attr.SetFont(font);
2890 GetBuffer().SetBasicStyle(attr);
fe5aa22c 2891
fe5aa22c
JS
2892 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2893 Refresh(false);
5d7836c4
JS
2894
2895 return true;
2896}
2897
4d551ad5
JS
2898/// Transform logical to physical
2899wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
5d7836c4
JS
2900{
2901 wxPoint pt;
2902 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
2903
2904 return pt;
2905}
2906
2907/// Transform physical to logical
4d551ad5 2908wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
5d7836c4
JS
2909{
2910 wxPoint pt;
2911 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2912
2913 return pt;
2914}
2915
2916/// Position the caret
2917void wxRichTextCtrl::PositionCaret()
2918{
c59f6793
JS
2919 if (!GetCaret())
2920 return;
2921
2922 //wxLogDebug(wxT("PositionCaret"));
2923
5d7836c4
JS
2924 wxRect caretRect;
2925 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2926 {
7e4fe193
JS
2927 wxPoint newPt = caretRect.GetPosition();
2928 wxSize newSz = caretRect.GetSize();
2929 wxPoint pt = GetPhysicalPoint(newPt);
2930 if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz)
c59f6793 2931 {
1c13f06e
JS
2932 GetCaret()->Hide();
2933 if (GetCaret()->GetSize() != newSz)
2934 GetCaret()->SetSize(newSz);
e9f10004
JS
2935
2936 int halfSize = newSz.y/2;
2937 // If the caret is beyond the margin, hide it by moving it out of the way
2938 if (((pt.y + halfSize) < GetBuffer().GetTopMargin()) || ((pt.y + halfSize) > (GetClientSize().y - GetBuffer().GetBottomMargin())))
2939 pt.y = -200;
2940
c59f6793 2941 GetCaret()->Move(pt);
1c13f06e 2942 GetCaret()->Show();
c59f6793 2943 }
5d7836c4
JS
2944 }
2945}
2946
2947/// Get the caret height and position for the given character position
2948bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2949{
2950 wxClientDC dc(this);
2951 dc.SetFont(GetFont());
7fe8059f 2952
5d7836c4
JS
2953 PrepareDC(dc);
2954
2955 wxPoint pt;
2956 int height = 0;
2957
2958 if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart))
2959 {
88bee7e6
JS
2960 // Caret height can't be zero
2961 if (height == 0)
2962 height = dc.GetCharHeight();
2963
5d7836c4
JS
2964 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
2965 return true;
2966 }
41e155b4
WS
2967
2968 return false;
5d7836c4
JS
2969}
2970
2971/// Gets the line for the visible caret position. If the caret is
2972/// shown at the very end of the line, it means the next character is actually
2973/// on the following line. So let's get the line we're expecting to find
2974/// if this is the case.
2975wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2976{
2977 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2978 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2979 if (line)
2980 {
1e967276
JS
2981 wxRichTextRange lineRange = line->GetAbsoluteRange();
2982 if (caretPosition == lineRange.GetStart()-1 &&
2983 (para->GetRange().GetStart() != lineRange.GetStart()))
5d7836c4
JS
2984 {
2985 if (!m_caretAtLineStart)
2986 line = GetBuffer().GetLineAtPosition(caretPosition-1, true);
2987 }
2988 }
2989 return line;
2990}
2991
2992
2993/// Move the caret to the given character position
2994bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2995{
2996 if (GetBuffer().GetDirty())
2f36e8dc 2997 LayoutContent();
5d7836c4
JS
2998
2999 if (pos <= GetBuffer().GetRange().GetEnd())
3000 {
3001 SetCaretPosition(pos, showAtLineStart);
3002
3003 PositionCaret();
3004
3005 return true;
3006 }
3007 else
3008 return false;
3009}
3010
3011/// Layout the buffer: which we must do before certain operations, such as
3012/// setting the caret position.
2f36e8dc 3013bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
5d7836c4 3014{
4d551ad5 3015 if (GetBuffer().GetDirty() || onlyVisibleRect)
1e967276
JS
3016 {
3017 wxRect availableSpace(GetClientSize());
3018 if (availableSpace.width == 0)
3019 availableSpace.width = 10;
3020 if (availableSpace.height == 0)
3021 availableSpace.height = 10;
4d551ad5
JS
3022
3023 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
3024 if (onlyVisibleRect)
3025 {
3026 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
3027 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3028 }
41e155b4 3029
1e967276
JS
3030 wxClientDC dc(this);
3031 dc.SetFont(GetFont());
41e155b4 3032
1e967276 3033 PrepareDC(dc);
41e155b4 3034
1e967276
JS
3035 GetBuffer().Defragment();
3036 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
4d551ad5 3037 GetBuffer().Layout(dc, availableSpace, flags);
1e967276 3038 GetBuffer().SetDirty(false);
41e155b4 3039
1e967276
JS
3040 if (!IsFrozen())
3041 SetupScrollbars();
3042 }
5d7836c4
JS
3043
3044 return true;
3045}
3046
3047/// Is all of the selection bold?
3966a9f4 3048bool wxRichTextCtrl::IsSelectionBold()
5d7836c4
JS
3049{
3050 if (HasSelection())
3051 {
44cc96a8 3052 wxTextAttr attr;
a7ed48a5 3053 wxRichTextRange range = GetSelectionRange();
5d7836c4 3054 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
7d76fbd5 3055 attr.SetFontWeight(wxFONTWEIGHT_BOLD);
7fe8059f 3056
5d7836c4
JS
3057 return HasCharacterAttributes(range, attr);
3058 }
3059 else
3060 {
3061 // If no selection, then we need to combine current style with default style
3062 // to see what the effect would be if we started typing.
44cc96a8 3063 wxTextAttr attr;
5d7836c4 3064 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
ab14c7aa
JS
3065
3066 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3067 if (GetStyle(pos, attr))
5d7836c4 3068 {
ab14c7aa
JS
3069 if (IsDefaultStyleShowing())
3070 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
7d76fbd5 3071 return attr.GetFontWeight() == wxFONTWEIGHT_BOLD;
5d7836c4
JS
3072 }
3073 }
3074 return false;
3075}
3076
3077/// Is all of the selection italics?
3966a9f4 3078bool wxRichTextCtrl::IsSelectionItalics()
5d7836c4
JS
3079{
3080 if (HasSelection())
3081 {
a7ed48a5 3082 wxRichTextRange range = GetSelectionRange();
44cc96a8 3083 wxTextAttr attr;
5d7836c4 3084 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
7d76fbd5 3085 attr.SetFontStyle(wxFONTSTYLE_ITALIC);
7fe8059f 3086
5d7836c4
JS
3087 return HasCharacterAttributes(range, attr);
3088 }
3089 else
3090 {
3091 // If no selection, then we need to combine current style with default style
3092 // to see what the effect would be if we started typing.
44cc96a8 3093 wxTextAttr attr;
5d7836c4 3094 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
ab14c7aa
JS
3095
3096 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3097 if (GetStyle(pos, attr))
5d7836c4 3098 {
ab14c7aa
JS
3099 if (IsDefaultStyleShowing())
3100 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
7d76fbd5 3101 return attr.GetFontStyle() == wxFONTSTYLE_ITALIC;
5d7836c4
JS
3102 }
3103 }
3104 return false;
3105}
3106
3107/// Is all of the selection underlined?
3966a9f4 3108bool wxRichTextCtrl::IsSelectionUnderlined()
5d7836c4
JS
3109{
3110 if (HasSelection())
3111 {
a7ed48a5 3112 wxRichTextRange range = GetSelectionRange();
44cc96a8 3113 wxTextAttr attr;
5d7836c4
JS
3114 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3115 attr.SetFontUnderlined(true);
7fe8059f 3116
5d7836c4
JS
3117 return HasCharacterAttributes(range, attr);
3118 }
3119 else
3120 {
3121 // If no selection, then we need to combine current style with default style
3122 // to see what the effect would be if we started typing.
44cc96a8 3123 wxTextAttr attr;
5d7836c4 3124 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
ab14c7aa 3125 long pos = GetAdjustedCaretPosition(GetCaretPosition());
fe5aa22c 3126
ab14c7aa 3127 if (GetStyle(pos, attr))
5d7836c4 3128 {
ab14c7aa
JS
3129 if (IsDefaultStyleShowing())
3130 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
5d7836c4
JS
3131 return attr.GetFontUnderlined();
3132 }
3133 }
3134 return false;
3135}
3136
3137/// Apply bold to the selection
3138bool wxRichTextCtrl::ApplyBoldToSelection()
3139{
44cc96a8 3140 wxTextAttr attr;
5d7836c4 3141 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
7d76fbd5 3142 attr.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL : wxFONTWEIGHT_BOLD);
5d7836c4
JS
3143
3144 if (HasSelection())
3e3a754f 3145 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 3146 else
063dbace
JS
3147 {
3148 wxRichTextAttr current = GetDefaultStyleEx();
3149 current.Apply(attr);
3150 SetAndShowDefaultStyle(current);
3151 }
5d7836c4
JS
3152 return true;
3153}
3154
3155/// Apply italic to the selection
3156bool wxRichTextCtrl::ApplyItalicToSelection()
3157{
44cc96a8 3158 wxTextAttr attr;
5d7836c4 3159 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
7d76fbd5 3160 attr.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL : wxFONTSTYLE_ITALIC);
5d7836c4
JS
3161
3162 if (HasSelection())
3e3a754f 3163 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 3164 else
063dbace
JS
3165 {
3166 wxRichTextAttr current = GetDefaultStyleEx();
3167 current.Apply(attr);
3168 SetAndShowDefaultStyle(current);
3169 }
5d7836c4
JS
3170 return true;
3171}
3172
3173/// Apply underline to the selection
3174bool wxRichTextCtrl::ApplyUnderlineToSelection()
3175{
44cc96a8 3176 wxTextAttr attr;
5d7836c4 3177 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
4d551ad5 3178 attr.SetFontUnderlined(!IsSelectionUnderlined());
5d7836c4
JS
3179
3180 if (HasSelection())
3e3a754f 3181 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 3182 else
063dbace
JS
3183 {
3184 wxRichTextAttr current = GetDefaultStyleEx();
3185 current.Apply(attr);
3186 SetAndShowDefaultStyle(current);
3187 }
5d7836c4
JS
3188 return true;
3189}
3190
3191/// Is all of the selection aligned according to the specified flag?
3966a9f4 3192bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
5d7836c4 3193{
0ca07313 3194 wxRichTextRange range;
5d7836c4 3195 if (HasSelection())
a7ed48a5 3196 range = GetSelectionRange();
5d7836c4 3197 else
a7ed48a5 3198 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
0ca07313 3199
44cc96a8 3200 wxTextAttr attr;
0ca07313
JS
3201 attr.SetAlignment(alignment);
3202
3203 return HasParagraphAttributes(range, attr);
5d7836c4
JS
3204}
3205
3206/// Apply alignment to the selection
3207bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
3208{
44cc96a8 3209 wxTextAttr attr;
5d7836c4
JS
3210 attr.SetAlignment(alignment);
3211 if (HasSelection())
3212 return SetStyle(GetSelectionRange(), attr);
3213 else
3214 {
3215 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3216 if (para)
3e3a754f 3217 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
5d7836c4
JS
3218 }
3219 return true;
3220}
3221
ab14c7aa 3222/// Apply a named style to the selection
1807a1f3 3223bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
ab14c7aa
JS
3224{
3225 // Flags are defined within each definition, so only certain
3226 // attributes are applied.
44cc96a8 3227 wxTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
1807a1f3 3228
f2ab7bf3 3229 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET;
ab14c7aa 3230
38f833b1
JS
3231 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
3232 {
3233 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
41a85215 3234
38f833b1 3235 wxRichTextRange range;
41a85215 3236
38f833b1
JS
3237 if (HasSelection())
3238 range = GetSelectionRange();
3239 else
3240 {
41a85215 3241 long pos = GetAdjustedCaretPosition(GetCaretPosition());
38f833b1
JS
3242 range = wxRichTextRange(pos, pos+1);
3243 }
41a85215
WS
3244
3245 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
3246 }
3247
ab14c7aa
JS
3248 // Make sure the attr has the style name
3249 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
3e3a754f 3250 {
ab14c7aa 3251 attr.SetParagraphStyleName(def->GetName());
1807a1f3 3252
3e3a754f 3253 // If applying a paragraph style, we only want the paragraph nodes to adopt these
38f833b1 3254 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3e3a754f
JS
3255 // to change its style independently.
3256 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3257 }
ab14c7aa
JS
3258 else
3259 attr.SetCharacterStyleName(def->GetName());
3260
3261 if (HasSelection())
1807a1f3 3262 return SetStyleEx(GetSelectionRange(), attr, flags);
ab14c7aa 3263 else
1807a1f3 3264 {
063dbace
JS
3265 wxRichTextAttr current = GetDefaultStyleEx();
3266 current.Apply(attr);
3267 SetAndShowDefaultStyle(current);
1807a1f3
JS
3268 return true;
3269 }
ab14c7aa
JS
3270}
3271
fe5aa22c
JS
3272/// Apply the style sheet to the buffer, for example if the styles have changed.
3273bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
3274{
3275 if (!styleSheet)
3276 styleSheet = GetBuffer().GetStyleSheet();
3277 if (!styleSheet)
3278 return false;
3279
3280 if (GetBuffer().ApplyStyleSheet(styleSheet))
3281 {
3282 GetBuffer().Invalidate(wxRICHTEXT_ALL);
3283 Refresh(false);
3284 return true;
3285 }
3286 else
3287 return false;
3288}
3289
5d7836c4
JS
3290/// Sets the default style to the style under the cursor
3291bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3292{
44cc96a8 3293 wxTextAttr attr;
5d7836c4
JS
3294 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
3295
ab14c7aa
JS
3296 // If at the start of a paragraph, use the next position.
3297 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3298
fe5aa22c 3299 if (GetUncombinedStyle(pos, attr))
5d7836c4
JS
3300 {
3301 SetDefaultStyle(attr);
3302 return true;
3303 }
41e155b4
WS
3304
3305 return false;
5d7836c4
JS
3306}
3307
4d551ad5
JS
3308/// Returns the first visible position in the current view
3309long wxRichTextCtrl::GetFirstVisiblePosition() const
3310{
3311 wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
3312 if (line)
3313 return line->GetAbsoluteRange().GetStart();
3314 else
3315 return 0;
3316}
3317
ea160b2e
JS
3318/// Get the first visible point in the window
3319wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const
3320{
3321 int ppuX, ppuY;
3322 int startXUnits, startYUnits;
3323
3324 GetScrollPixelsPerUnit(& ppuX, & ppuY);
3325 GetViewStart(& startXUnits, & startYUnits);
3326
3327 return wxPoint(startXUnits * ppuX, startYUnits * ppuY);
3328}
3329
ab14c7aa
JS
3330/// The adjusted caret position is the character position adjusted to take
3331/// into account whether we're at the start of a paragraph, in which case
3332/// style information should be taken from the next position, not current one.
3333long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
3334{
3335 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1);
3336
3337 if (para && (caretPos+1 == para->GetRange().GetStart()))
3338 caretPos ++;
3339 return caretPos;
3340}
3341
96c9f0f6
JS
3342/// Get/set the selection range in character positions. -1, -1 means no selection.
3343/// The range is in API convention, i.e. a single character selection is denoted
3344/// by (n, n+1)
3345wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
3346{
3347 wxRichTextRange range = GetInternalSelectionRange();
3348 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
3349 range.SetEnd(range.GetEnd() + 1);
3350 return range;
3351}
3352
3353void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
3354{
3355 wxRichTextRange range1(range);
3356 if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) )
3357 range1.SetEnd(range1.GetEnd() - 1);
3358
3359 wxASSERT( range1.GetStart() > range1.GetEnd() );
fe5aa22c 3360
96c9f0f6
JS
3361 SetInternalSelectionRange(range1);
3362}
3363
38f833b1
JS
3364/// Set list style
3365bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3366{
3367 return GetBuffer().SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3368}
3369
3370bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3371{
3372 return GetBuffer().SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3373}
3374
3375/// Clear list for given range
3376bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags)
3377{
3378 return GetBuffer().ClearListStyle(range.ToInternal(), flags);
3379}
3380
3381/// Number/renumber any list elements in the given range
3382bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3383{
41a85215 3384 return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
38f833b1
JS
3385}
3386
3387bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3388{
41a85215 3389 return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
38f833b1
JS
3390}
3391
3392/// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3393bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
3394{
3395 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel);
3396}
3397
3398bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
3399{
3400 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel);
3401}
3402
12cc29c5
JS
3403/// Deletes the content in the given range
3404bool wxRichTextCtrl::Delete(const wxRichTextRange& range)
3405{
3406 return GetBuffer().DeleteRangeWithUndo(range.ToInternal(), this);
3407}
3408
dadd4f55
JS
3409const wxArrayString& wxRichTextCtrl::GetAvailableFontNames()
3410{
3411 if (sm_availableFontNames.GetCount() == 0)
3412 {
3413 sm_availableFontNames = wxFontEnumerator::GetFacenames();
3414 sm_availableFontNames.Sort();
3415 }
3416 return sm_availableFontNames;
3417}
3418
3419void wxRichTextCtrl::ClearAvailableFontNames()
3420{
3421 sm_availableFontNames.Clear();
3422}
3423
c0a5145a 3424void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
52170c5b
JS
3425{
3426 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3427
3428 wxTextAttrEx basicStyle = GetBasicStyle();
3429 basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3430 SetBasicStyle(basicStyle);
3431 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3432
3433 Refresh();
3434}
3435
7051fa41
JS
3436// Refresh the area affected by a selection change
3437bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange& oldSelection, const wxRichTextRange& newSelection)
3438{
3439 // Calculate the refresh rectangle - just the affected lines
3440 long firstPos, lastPos;
3441 if (oldSelection.GetStart() == -2 && newSelection.GetStart() != -2)
3442 {
3443 firstPos = newSelection.GetStart();
3444 lastPos = newSelection.GetEnd();
3445 }
3446 else if (oldSelection.GetStart() != -2 && newSelection.GetStart() == -2)
3447 {
3448 firstPos = oldSelection.GetStart();
3449 lastPos = oldSelection.GetEnd();
3450 }
3451 else if (oldSelection.GetStart() == -2 && newSelection.GetStart() == -2)
3452 {
3453 return false;
3454 }
3455 else
3456 {
3457 firstPos = wxMin(oldSelection.GetStart(), newSelection.GetStart());
3458 lastPos = wxMax(oldSelection.GetEnd(), newSelection.GetEnd());
3459 }
3460
3461 wxRichTextLine* firstLine = GetBuffer().GetLineAtPosition(firstPos);
3462 wxRichTextLine* lastLine = GetBuffer().GetLineAtPosition(lastPos);
3463
3464 if (firstLine && lastLine)
3465 {
3466 wxSize clientSize = GetClientSize();
3467 wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition());
3468 wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y);
3469
3470 pt1.x = 0;
3471 pt1.y = wxMax(0, pt1.y);
3472 pt2.x = 0;
3473 pt2.y = wxMin(clientSize.y, pt2.y);
3474
3475 wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y));
3476 RefreshRect(rect, false);
3477 }
3478 else
3479 Refresh(false);
3480
3481 return true;
3482}
3483
1c13f06e
JS
3484#if wxRICHTEXT_USE_OWN_CARET
3485
3486// ----------------------------------------------------------------------------
3487// initialization and destruction
3488// ----------------------------------------------------------------------------
3489
3490void wxRichTextCaret::Init()
3491{
3492 m_hasFocus = true;
3493
3494 m_xOld =
3495 m_yOld = -1;
3496 m_richTextCtrl = NULL;
3497 m_needsUpdate = false;
a188ac29 3498 m_flashOn = true;
1c13f06e
JS
3499}
3500
3501wxRichTextCaret::~wxRichTextCaret()
3502{
a188ac29
JS
3503 if (m_timer.IsRunning())
3504 m_timer.Stop();
1c13f06e
JS
3505}
3506
3507// ----------------------------------------------------------------------------
3508// showing/hiding/moving the caret (base class interface)
3509// ----------------------------------------------------------------------------
3510
3511void wxRichTextCaret::DoShow()
3512{
a188ac29 3513 m_flashOn = true;
ab52bac8 3514
a188ac29
JS
3515 if (!m_timer.IsRunning())
3516 m_timer.Start(GetBlinkTime());
3517
1c13f06e
JS
3518 Refresh();
3519}
3520
3521void wxRichTextCaret::DoHide()
3522{
a188ac29
JS
3523 if (m_timer.IsRunning())
3524 m_timer.Stop();
3525
1c13f06e
JS
3526 Refresh();
3527}
3528
3529void wxRichTextCaret::DoMove()
3530{
3531 if (IsVisible())
3532 {
3533 Refresh();
3534
3535 if (m_xOld != -1 && m_yOld != -1)
3536 {
3537 if (m_richTextCtrl)
3538 {
3539 wxRect rect(GetPosition(), GetSize());
3540 m_richTextCtrl->RefreshRect(rect, false);
3541 }
3542 }
3543 }
3544
3545 m_xOld = m_x;
3546 m_yOld = m_y;
3547}
3548
3549void wxRichTextCaret::DoSize()
3550{
3551 int countVisible = m_countVisible;
3552 if (countVisible > 0)
3553 {
3554 m_countVisible = 0;
3555 DoHide();
3556 }
3557
3558 if (countVisible > 0)
3559 {
3560 m_countVisible = countVisible;
3561 DoShow();
3562 }
3563}
3564
3565// ----------------------------------------------------------------------------
3566// handling the focus
3567// ----------------------------------------------------------------------------
3568
3569void wxRichTextCaret::OnSetFocus()
3570{
3571 m_hasFocus = true;
3572
3573 if ( IsVisible() )
3574 Refresh();
3575}
3576
3577void wxRichTextCaret::OnKillFocus()
3578{
3579 m_hasFocus = false;
3580}
3581
3582// ----------------------------------------------------------------------------
3583// drawing the caret
3584// ----------------------------------------------------------------------------
3585
3586void wxRichTextCaret::Refresh()
3587{
3588 if (m_richTextCtrl)
3589 {
3590 wxRect rect(GetPosition(), GetSize());
3591 m_richTextCtrl->RefreshRect(rect, false);
3592 }
3593}
3594
3595void wxRichTextCaret::DoDraw(wxDC *dc)
3596{
3597 dc->SetPen( *wxBLACK_PEN );
3598
3599 dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
3600 dc->SetPen(*wxBLACK_PEN);
3601
1c13f06e
JS
3602 wxPoint pt(m_x, m_y);
3603
3604 if (m_richTextCtrl)
3605 {
3606 pt = m_richTextCtrl->GetLogicalPoint(pt);
3607 }
a188ac29
JS
3608 if (IsVisible() && m_flashOn)
3609 dc->DrawRectangle(pt.x, pt.y, m_width, m_height);
3610}
3611
3612void wxRichTextCaret::Notify()
3613{
3614 m_flashOn = !m_flashOn;
3615 Refresh();
3616}
3617
3618void wxRichTextCaretTimer::Notify()
3619{
3620 m_caret->Notify();
1c13f06e
JS
3621}
3622#endif
3623 // wxRICHTEXT_USE_OWN_CARET
3624
5d7836c4
JS
3625#endif
3626 // wxUSE_RICHTEXT