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