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