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