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