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