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