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