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