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