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