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