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