]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtextctrl.cpp
Better button size on wxX11
[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 #include "wx/fontenum.h"
35
36 // DLL options compatibility check:
37 #include "wx/app.h"
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
39
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN)
47
48 IMPLEMENT_CLASS( wxRichTextCtrl, wxControl )
49
50 IMPLEMENT_CLASS( wxRichTextEvent, wxNotifyEvent )
51
52 BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl )
53 EVT_PAINT(wxRichTextCtrl::OnPaint)
54 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
55 EVT_IDLE(wxRichTextCtrl::OnIdle)
56 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
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)
89 END_EVENT_TABLE()
90
91 /*!
92 * wxRichTextCtrl
93 */
94
95 wxArrayString wxRichTextCtrl::sm_availableFontNames;
96
97 wxRichTextCtrl::wxRichTextCtrl()
98 : wxScrollHelper(this)
99 {
100 Init();
101 }
102
103 wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent,
104 wxWindowID id,
105 const wxString& value,
106 const wxPoint& pos,
107 const wxSize& size,
108 long style)
109 : wxScrollHelper(this)
110 {
111 Init();
112 Create(parent, id, value, pos, size, style);
113 }
114
115 /// Creation
116 bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style)
117 {
118 if (!wxTextCtrlBase::Create(parent, id, pos, size,
119 style|wxFULL_REPAINT_ON_RESIZE))
120 return false;
121
122 if (!GetFont().Ok())
123 {
124 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
125 }
126
127 GetBuffer().SetRichTextCtrl(this);
128
129 if (style & wxTE_READONLY)
130 SetEditable(false);
131
132 wxTextAttrEx attributes;
133 attributes.SetFont(GetFont());
134 attributes.SetTextColour(*wxBLACK);
135 attributes.SetBackgroundColour(*wxWHITE);
136 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
137 attributes.SetLineSpacing(10);
138 attributes.SetParagraphSpacingAfter(10);
139 attributes.SetParagraphSpacingBefore(0);
140 attributes.SetFlags(wxTEXT_ATTR_ALL);
141 SetBasicStyle(attributes);
142
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
148 SetBackgroundColour(*wxWHITE);
149 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
150
151 // Tell the sizers to use the given or best size
152 SetBestFittingSize(size);
153
154 #if wxRICHTEXT_BUFFERED_PAINTING
155 // Create a buffer
156 RecreateBuffer(size);
157 #endif
158
159 SetCursor(wxCursor(wxCURSOR_IBEAM));
160
161 if (!value.IsEmpty())
162 SetValue(value);
163
164 return true;
165 }
166
167 wxRichTextCtrl::~wxRichTextCtrl()
168 {
169 delete m_contextMenu;
170 }
171
172 /// Member initialisation
173 void 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;
184 m_fullLayoutRequired = false;
185 m_fullLayoutTime = 0;
186 m_fullLayoutSavedPosition = 0;
187 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
188 m_caretPositionForDefaultStyle = -2;
189 }
190
191 /// Call Freeze to prevent refresh
192 void wxRichTextCtrl::Freeze()
193 {
194 m_freezeCount ++;
195 }
196
197 /// Call Thaw to refresh
198 void wxRichTextCtrl::Thaw()
199 {
200 m_freezeCount --;
201
202 if (m_freezeCount == 0)
203 {
204 SetupScrollbars();
205 Refresh(false);
206 }
207 }
208
209 /// Clear all text
210 void wxRichTextCtrl::Clear()
211 {
212 m_buffer.Reset();
213 m_buffer.SetDirty(true);
214 m_caretPosition = -1;
215 m_caretPositionForDefaultStyle = -2;
216 m_caretAtLineStart = false;
217 m_selectionRange.SetRange(-2, -2);
218
219 SetScrollbars(0, 0, 0, 0, 0, 0);
220
221 if (m_freezeCount == 0)
222 {
223 SetupScrollbars();
224 Refresh(false);
225 }
226 SendTextUpdatedEvent();
227 }
228
229 /// Painting
230 void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
231 {
232 if (GetCaret())
233 GetCaret()->Hide();
234
235 {
236 #if wxRICHTEXT_BUFFERED_PAINTING
237 wxBufferedPaintDC dc(this, m_bufferBitmap);
238 #else
239 wxPaintDC dc(this);
240 #endif
241 PrepareDC(dc);
242
243 if (m_freezeCount > 0)
244 return;
245
246 dc.SetFont(GetFont());
247
248 // Paint the background
249 PaintBackground(dc);
250
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 }
259
260 GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
261 }
262
263 if (GetCaret())
264 GetCaret()->Show();
265
266 PositionCaret();
267 }
268
269 // Empty implementation, to prevent flicker
270 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
271 {
272 }
273
274 void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
275 {
276 wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16);
277 SetCaret(caret);
278 caret->Show();
279 PositionCaret();
280
281 if (!IsFrozen())
282 Refresh(false);
283 }
284
285 void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
286 {
287 SetCaret(NULL);
288
289 if (!IsFrozen())
290 Refresh(false);
291 }
292
293 /// Left-click
294 void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
295 {
296 SetFocus();
297
298 wxClientDC dc(this);
299 PrepareDC(dc);
300 dc.SetFont(GetFont());
301
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
323 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
324 caretAtLineStart = true;
325 position --;
326 }
327
328 MoveCaret(position, caretAtLineStart);
329 SetDefaultStyleToCursorStyle();
330 }
331
332 event.Skip();
333 }
334
335 /// Left-up
336 void 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
347 void 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());
358
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
377 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
378 caretAtLineStart = true;
379 position --;
380 }
381
382 if (m_caretPosition != position)
383 {
384 bool extendSel = ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
385
386 MoveCaret(position, caretAtLineStart);
387 SetDefaultStyleToCursorStyle();
388
389 if (extendSel)
390 Refresh(false);
391 }
392 }
393 }
394
395 /// Right-click
396 void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
397 {
398 SetFocus();
399 event.Skip();
400 }
401
402 /// Left-double-click
403 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& event)
404 {
405 SelectWord(GetCaretPosition()+1);
406 event.Skip();
407 }
408
409 /// Middle-click
410 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
411 {
412 event.Skip();
413 }
414
415 /// Key press
416 void wxRichTextCtrl::OnChar(wxKeyEvent& event)
417 {
418 int flags = 0;
419 if (event.CmdDown())
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 ||
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)
443 {
444 KeyboardNavigate(event.GetKeyCode(), flags);
445 return;
446 }
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)
457 {
458 BeginBatchUndo(_("Insert Text"));
459
460 long newPos = m_caretPosition;
461
462 DeleteSelectedContent(& newPos);
463
464 GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
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();
475
476 ScrollIntoView(m_caretPosition, WXK_RIGHT);
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
506 ScrollIntoView(m_caretPosition, WXK_LEFT);
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 {
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 }
639
640 default:
641 {
642 if (event.CmdDown() || event.AltDown())
643 {
644 event.Skip();
645 return;
646 }
647
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();
661
662 int promoteBy = event.ShiftDown() ? 1 : -1;
663
664 PromoteList(promoteBy, range, NULL);
665 return;
666 }
667 }
668
669 BeginBatchUndo(_("Insert Text"));
670
671 long newPos = m_caretPosition;
672 DeleteSelectedContent(& newPos);
673
674 wxString str = (wxChar) event.GetKeyCode();
675 GetBuffer().InsertTextWithUndo(newPos+1, str, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
676
677 EndBatchUndo();
678
679 SetDefaultStyleToCursorStyle();
680 ScrollIntoView(m_caretPosition, WXK_RIGHT);
681 }
682 }
683 }
684 }
685
686 /// Delete content if there is a selection, e.g. when pressing a key.
687 bool 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
710 Left: left one character
711 Right: right one character
712 Up: up one line
713 Down: down one line
714 Ctrl-Left: left one word
715 Ctrl-Right: right one word
716 Ctrl-Up: previous paragraph start
717 Ctrl-Down: next start of paragraph
718 Home: start of line
719 End: end of line
720 Ctrl-Home: start of document
721 Ctrl-End: end of document
722 Page-Up: Up a screen
723 Page-Down: Down a screen
724
725 Maybe:
726
727 Ctrl-Alt-PgUp: Start of window
728 Ctrl-Alt-PgDn: End of window
729 F8: Start selection mode
730 Esc: End selection mode
731
732 Adding Shift does the above but starts/extends selection.
733
734
735 */
736
737 bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags)
738 {
739 bool success = false;
740
741 if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT)
742 {
743 if (flags & wxRICHTEXT_CTRL_DOWN)
744 success = WordRight(1, flags);
745 else
746 success = MoveRight(1, flags);
747 }
748 else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT)
749 {
750 if (flags & wxRICHTEXT_CTRL_DOWN)
751 success = WordLeft(1, flags);
752 else
753 success = MoveLeft(1, flags);
754 }
755 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP)
756 {
757 if (flags & wxRICHTEXT_CTRL_DOWN)
758 success = MoveToParagraphStart(flags);
759 else
760 success = MoveUp(1, flags);
761 }
762 else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN)
763 {
764 if (flags & wxRICHTEXT_CTRL_DOWN)
765 success = MoveToParagraphEnd(flags);
766 else
767 success = MoveDown(1, flags);
768 }
769 else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP)
770 {
771 success = PageUp(1, flags);
772 }
773 else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
774 {
775 success = PageDown(1, flags);
776 }
777 else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME)
778 {
779 if (flags & wxRICHTEXT_CTRL_DOWN)
780 success = MoveHome(flags);
781 else
782 success = MoveToLineStart(flags);
783 }
784 else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END)
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
798 return success;
799 }
800
801 /// Extend the selection. Selections are in caret positions.
802 bool 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 }
830
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.
839 bool 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
849 int startXUnits, startYUnits;
850 GetViewStart(& startXUnits, & startYUnits);
851 int startY = startYUnits * ppuY;
852
853 int sx = 0, sy = 0;
854 GetVirtualSize(& sx, & sy);
855 int sxUnits = 0;
856 int syUnits = 0;
857 if (ppuY != 0)
858 syUnits = sy/ppuY;
859
860 wxRect rect = line->GetRect();
861
862 bool scrolled = false;
863
864 wxSize clientSize = GetClientSize();
865
866 // Going down
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)
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);
877 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
878
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)
884 {
885 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
886 scrolled = true;
887 }
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 ;
894 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
895
896 if (startYUnits != yUnits)
897 {
898 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
899 scrolled = true;
900 }
901 }
902 }
903 // Going up
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 )
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 ;
914 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
915
916 if (startYUnits != yUnits)
917 {
918 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
919 scrolled = true;
920 }
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);
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 ++;
932
933 if (startYUnits != yUnits)
934 {
935 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
936 scrolled = true;
937 }
938 }
939 }
940 PositionCaret();
941
942 return scrolled;
943 }
944
945 /// Is the given position visible on the screen?
946 bool 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
961 int sx = 0, sy = 0;
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
974 void 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.
983 void 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 {
995 wxRichTextRange lineRange = line->GetAbsoluteRange();
996
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.
1000 if (oldPosition == lineRange.GetEnd())
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;
1014 m_caretAtLineStart = true;
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.
1028 void 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 {
1040 wxRichTextRange lineRange = line->GetAbsoluteRange();
1041
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.
1045 if (oldPosition == lineRange.GetStart())
1046 {
1047 m_caretPosition = oldPosition-1;
1048 m_caretAtLineStart = true;
1049 return;
1050 }
1051 else if (oldPosition == lineRange.GetEnd())
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
1076 bool 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
1102 if (extendSel)
1103 Refresh(false);
1104 return true;
1105 }
1106 else
1107 return false;
1108 }
1109
1110 /// Move left
1111 bool 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
1131 if (extendSel)
1132 Refresh(false);
1133 return true;
1134 }
1135 else
1136 return false;
1137 }
1138
1139 /// Move up
1140 bool wxRichTextCtrl::MoveUp(int noLines, int flags)
1141 {
1142 return MoveDown(- noLines, flags);
1143 }
1144
1145 /// Move up
1146 bool wxRichTextCtrl::MoveDown(int noLines, int flags)
1147 {
1148 if (!GetCaret())
1149 return false;
1150
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)
1173 {
1174 pt.y = lineObj->GetAbsolutePosition().y + 2;
1175 }
1176 else
1177 return false;
1178
1179 long newPos = 0;
1180 wxClientDC dc(this);
1181 PrepareDC(dc);
1182 dc.SetFont(GetFont());
1183
1184 int hitTest = GetBuffer().HitTest(dc, pt, newPos);
1185
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);
1195 wxRichTextRange lineRange;
1196 if (thisLine)
1197 lineRange = thisLine->GetAbsoluteRange();
1198
1199 if (thisLine && (newPos-1) == lineRange.GetEnd())
1200 {
1201 newPos --;
1202 caretLineStart = true;
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
1214 bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
1215 if (!extendSel)
1216 SelectNone();
1217
1218 SetCaretPosition(newPos, caretLineStart);
1219 PositionCaret();
1220 SetDefaultStyleToCursorStyle();
1221
1222 if (extendSel)
1223 Refresh(false);
1224 return true;
1225 }
1226
1227 return false;
1228 }
1229
1230 /// Move to the end of the paragraph
1231 bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1232 {
1233 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1234 if (para)
1235 {
1236 long newPos = para->GetRange().GetEnd() - 1;
1237 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1238 if (!extendSel)
1239 SelectNone();
1240
1241 SetCaretPosition(newPos);
1242 PositionCaret();
1243 SetDefaultStyleToCursorStyle();
1244
1245 if (extendSel)
1246 Refresh(false);
1247 return true;
1248 }
1249
1250 return false;
1251 }
1252
1253 /// Move to the start of the paragraph
1254 bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1255 {
1256 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1257 if (para)
1258 {
1259 long newPos = para->GetRange().GetStart() - 1;
1260 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1261 if (!extendSel)
1262 SelectNone();
1263
1264 SetCaretPosition(newPos);
1265 PositionCaret();
1266 SetDefaultStyleToCursorStyle();
1267
1268 if (extendSel)
1269 Refresh(false);
1270 return true;
1271 }
1272
1273 return false;
1274 }
1275
1276 /// Move to the end of the line
1277 bool wxRichTextCtrl::MoveToLineEnd(int flags)
1278 {
1279 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1280
1281 if (line)
1282 {
1283 wxRichTextRange lineRange = line->GetAbsoluteRange();
1284 long newPos = lineRange.GetEnd();
1285 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1286 if (!extendSel)
1287 SelectNone();
1288
1289 SetCaretPosition(newPos);
1290 PositionCaret();
1291 SetDefaultStyleToCursorStyle();
1292
1293 if (extendSel)
1294 Refresh(false);
1295 return true;
1296 }
1297
1298 return false;
1299 }
1300
1301 /// Move to the start of the line
1302 bool wxRichTextCtrl::MoveToLineStart(int flags)
1303 {
1304 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1305 if (line)
1306 {
1307 wxRichTextRange lineRange = line->GetAbsoluteRange();
1308 long newPos = lineRange.GetStart()-1;
1309
1310 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1311 if (!extendSel)
1312 SelectNone();
1313
1314 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
1315
1316 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
1317 PositionCaret();
1318 SetDefaultStyleToCursorStyle();
1319
1320 if (extendSel)
1321 Refresh(false);
1322 return true;
1323 }
1324
1325 return false;
1326 }
1327
1328 /// Move to the start of the buffer
1329 bool wxRichTextCtrl::MoveHome(int flags)
1330 {
1331 if (m_caretPosition != -1)
1332 {
1333 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
1334 if (!extendSel)
1335 SelectNone();
1336
1337 SetCaretPosition(-1);
1338 PositionCaret();
1339 SetDefaultStyleToCursorStyle();
1340
1341 if (extendSel)
1342 Refresh(false);
1343 return true;
1344 }
1345 else
1346 return false;
1347 }
1348
1349 /// Move to the end of the buffer
1350 bool wxRichTextCtrl::MoveEnd(int flags)
1351 {
1352 long endPos = GetBuffer().GetRange().GetEnd()-1;
1353
1354 if (m_caretPosition != endPos)
1355 {
1356 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
1357 if (!extendSel)
1358 SelectNone();
1359
1360 SetCaretPosition(endPos);
1361 PositionCaret();
1362 SetDefaultStyleToCursorStyle();
1363
1364 if (extendSel)
1365 Refresh(false);
1366 return true;
1367 }
1368 else
1369 return false;
1370 }
1371
1372 /// Move noPages pages up
1373 bool wxRichTextCtrl::PageUp(int noPages, int flags)
1374 {
1375 return PageDown(- noPages, flags);
1376 }
1377
1378 /// Move noPages pages down
1379 bool 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 {
1391 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
1392 long pos = lineRange.GetStart()-1;
1393 if (pos != m_caretPosition)
1394 {
1395 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
1396
1397 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1398 if (!extendSel)
1399 SelectNone();
1400
1401 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
1402 PositionCaret();
1403 SetDefaultStyleToCursorStyle();
1404
1405 if (extendSel)
1406 Refresh(false);
1407 return true;
1408 }
1409 }
1410 }
1411
1412 return false;
1413 }
1414
1415 // Finds the caret position for the next word
1416 long wxRichTextCtrl::FindNextWordPosition(int direction) const
1417 {
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
1423
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));
1429 if (text != wxT(" ") && !text.empty())
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));
1440 if (text.empty()) // End of paragraph, or maybe an image
1441 return wxMax(-1, i - 1);
1442 else if (text == wxT(" ") || text.empty())
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));
1463 if (text.empty()) // End of paragraph, or maybe an image
1464 break;
1465 else if (text == wxT(" ") || text.empty())
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));
1475 if (text != wxT(" ") /* && !text.empty() */)
1476 i += direction;
1477 else
1478 {
1479 return i;
1480 }
1481 }
1482 if (i < -1)
1483 return -1;
1484 return i;
1485 }
1486 }
1487
1488 /// Move n words left
1489 bool 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);
1495
1496 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1497 if (!extendSel)
1498 SelectNone();
1499
1500 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1501 PositionCaret();
1502 SetDefaultStyleToCursorStyle();
1503
1504 if (extendSel)
1505 Refresh(false);
1506 return true;
1507 }
1508
1509 return false;
1510 }
1511
1512 /// Move n words right
1513 bool 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);
1519
1520 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1521 if (!extendSel)
1522 SelectNone();
1523
1524 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1525 PositionCaret();
1526 SetDefaultStyleToCursorStyle();
1527
1528 if (extendSel)
1529 Refresh(false);
1530 return true;
1531 }
1532
1533 return false;
1534 }
1535
1536 /// Sizing
1537 void wxRichTextCtrl::OnSize(wxSizeEvent& event)
1538 {
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();
1545 LayoutContent(true /* onlyVisibleRect */);
1546 }
1547 else
1548 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1549
1550 #if wxRICHTEXT_BUFFERED_PAINTING
1551 RecreateBuffer();
1552 #endif
1553
1554 event.Skip();
1555 }
1556
1557
1558 /// Idle-time processing
1559 void 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);
1569 Refresh(false);
1570 }
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
1580 event.Skip();
1581 }
1582
1583 /// Scrolling
1584 void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
1585 {
1586 // Not used
1587 event.Skip();
1588 }
1589
1590 /// Set up scrollbars, e.g. after a resize
1591 void wxRichTextCtrl::SetupScrollbars(bool atTop)
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.
1604 int pixelsPerUnit = 5;
1605 wxSize clientSize = GetClientSize();
1606
1607 int maxHeight = GetBuffer().GetCachedSize().y;
1608
1609 // Round up so we have at least maxHeight pixels
1610 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
1611
1612 int startX = 0, startY = 0;
1613 if (!atTop)
1614 GetViewStart(& startX, & startY);
1615
1616 int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0);
1617 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
1618
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
1627 void 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);
1636 wxRect windowRect(GetClientSize());
1637 windowRect.x -= 2; windowRect.y -= 2;
1638 windowRect.width += 4; windowRect.height += 4;
1639
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
1646 #if wxRICHTEXT_BUFFERED_PAINTING
1647 /// Recreate buffer bitmap if necessary
1648 bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
1649 {
1650 wxSize sz = size;
1651 if (sz == wxDefaultSize)
1652 sz = GetClientSize();
1653
1654 if (sz.x < 1 || sz.y < 1)
1655 return false;
1656
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 }
1661 #endif
1662
1663 // ----------------------------------------------------------------------------
1664 // file IO functions
1665 // ----------------------------------------------------------------------------
1666
1667 bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
1668 {
1669 bool success = GetBuffer().LoadFile(filename, fileType);
1670 if (success)
1671 m_filename = filename;
1672
1673 DiscardEdits();
1674 SetInsertionPoint(0);
1675 LayoutContent();
1676 PositionCaret();
1677 SetupScrollbars(true);
1678 Refresh(false);
1679 SendTextUpdatedEvent();
1680
1681 if (success)
1682 return true;
1683 else
1684 {
1685 wxLogError(_("File couldn't be loaded."));
1686
1687 return false;
1688 }
1689 }
1690
1691 bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
1692 {
1693 if (GetBuffer().SaveFile(filename, fileType))
1694 {
1695 m_filename = filename;
1696
1697 DiscardEdits();
1698
1699 return true;
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
1712 wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
1713 {
1714 return GetBuffer().AddParagraph(text);
1715 }
1716
1717 /// Add an image
1718 wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
1719 {
1720 return GetBuffer().AddImage(image);
1721 }
1722
1723 // ----------------------------------------------------------------------------
1724 // selection and ranges
1725 // ----------------------------------------------------------------------------
1726
1727 void wxRichTextCtrl::SelectAll()
1728 {
1729 SetSelection(0, GetLastPosition()+1);
1730 m_selectionAnchor = -1;
1731 }
1732
1733 /// Select none
1734 void wxRichTextCtrl::SelectNone()
1735 {
1736 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1737 SetSelection(-2, -2);
1738 m_selectionAnchor = -2;
1739 }
1740
1741 static bool wxIsWordDelimiter(const wxString& text)
1742 {
1743 return !text.IsEmpty() && !wxIsalnum(text[0]);
1744 }
1745
1746 /// Select the word at the given character position
1747 bool wxRichTextCtrl::SelectWord(long position)
1748 {
1749 if (position < 0 || position > GetBuffer().GetRange().GetEnd())
1750 return false;
1751
1752 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
1753 if (!para)
1754 return false;
1755
1756 long positionStart = position;
1757 long positionEnd = position;
1758
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();
1770
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();
1782
1783 SetSelection(positionStart, positionEnd+1);
1784
1785 if (positionStart >= 0)
1786 {
1787 MoveCaret(positionStart-1, true);
1788 SetDefaultStyleToCursorStyle();
1789 }
1790
1791 return true;
1792 }
1793
1794 wxString wxRichTextCtrl::GetStringSelection() const
1795 {
1796 long from, to;
1797 GetSelection(&from, &to);
1798
1799 return GetRange(from, to);
1800 }
1801
1802 // ----------------------------------------------------------------------------
1803 // hit testing
1804 // ----------------------------------------------------------------------------
1805
1806 wxTextCtrlHitTestResult
1807 wxRichTextCtrl::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
1823 wxTextCtrlHitTestResult
1824 wxRichTextCtrl::HitTest(const wxPoint& pt,
1825 long * pos) const
1826 {
1827 wxClientDC dc((wxRichTextCtrl*) this);
1828 ((wxRichTextCtrl*)this)->PrepareDC(dc);
1829
1830 // Buffer uses logical position (relative to start of buffer)
1831 // so convert
1832 wxPoint pt2 = GetLogicalPoint(pt);
1833
1834 int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos);
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;
1849 }
1850
1851 // ----------------------------------------------------------------------------
1852 // set/get the controls text
1853 // ----------------------------------------------------------------------------
1854
1855 wxString wxRichTextCtrl::GetValue() const
1856 {
1857 return GetBuffer().GetText();
1858 }
1859
1860 wxString wxRichTextCtrl::GetRange(long from, long to) const
1861 {
1862 // Public API for range is different from internals
1863 return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1));
1864 }
1865
1866 void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
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 {
1876 DoWriteText(value);
1877
1878 // for compatibility, don't move the cursor when doing SetValue()
1879 SetInsertionPoint(0);
1880 }
1881 else // same text
1882 {
1883 if ( flags & SetValue_SendEvent )
1884 {
1885 // still send an event for consistency
1886 SendTextUpdatedEvent();
1887 }
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
1897 void wxRichTextCtrl::WriteText(const wxString& value)
1898 {
1899 DoWriteText(value);
1900 }
1901
1902 void wxRichTextCtrl::DoWriteText(const wxString& value, int flags)
1903 {
1904 wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix);
1905
1906 GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueUnix, this);
1907
1908 if ( flags & SetValue_SendEvent )
1909 SendTextUpdatedEvent();
1910 }
1911
1912 void wxRichTextCtrl::AppendText(const wxString& text)
1913 {
1914 SetInsertionPointEnd();
1915
1916 WriteText(text);
1917 }
1918
1919 /// Write an image at the current insertion point
1920 bool 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);
1927
1928 return false;
1929 }
1930
1931 bool 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);
1938
1939 return false;
1940 }
1941
1942 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
1943 {
1944 return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
1945 }
1946
1947 bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType)
1948 {
1949 if (bitmap.Ok())
1950 {
1951 wxRichTextImageBlock imageBlock;
1952
1953 wxImage image = bitmap.ConvertToImage();
1954 if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType))
1955 return WriteImage(imageBlock);
1956 }
1957
1958 return false;
1959 }
1960
1961 /// Insert a newline (actually paragraph) at the current insertion point.
1962 bool wxRichTextCtrl::Newline()
1963 {
1964 return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this);
1965 }
1966
1967
1968 // ----------------------------------------------------------------------------
1969 // Clipboard operations
1970 // ----------------------------------------------------------------------------
1971
1972 void wxRichTextCtrl::Copy()
1973 {
1974 if (CanCopy())
1975 {
1976 wxRichTextRange range = GetInternalSelectionRange();
1977 GetBuffer().CopyToClipboard(range);
1978 }
1979 }
1980
1981 void wxRichTextCtrl::Cut()
1982 {
1983 if (CanCut())
1984 {
1985 wxRichTextRange range = GetInternalSelectionRange();
1986 GetBuffer().CopyToClipboard(range);
1987
1988 DeleteSelectedContent();
1989 LayoutContent();
1990 Refresh(false);
1991 }
1992 }
1993
1994 void wxRichTextCtrl::Paste()
1995 {
1996 if (CanPaste())
1997 {
1998 BeginBatchUndo(_("Paste"));
1999
2000 long newPos = m_caretPosition;
2001 DeleteSelectedContent(& newPos);
2002
2003 GetBuffer().PasteFromClipboard(newPos);
2004
2005 EndBatchUndo();
2006 }
2007 }
2008
2009 void wxRichTextCtrl::DeleteSelection()
2010 {
2011 if (CanDeleteSelection())
2012 {
2013 DeleteSelectedContent();
2014 }
2015 }
2016
2017 bool wxRichTextCtrl::HasSelection() const
2018 {
2019 return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2;
2020 }
2021
2022 bool wxRichTextCtrl::CanCopy() const
2023 {
2024 // Can copy if there's a selection
2025 return HasSelection();
2026 }
2027
2028 bool wxRichTextCtrl::CanCut() const
2029 {
2030 return HasSelection() && IsEditable();
2031 }
2032
2033 bool wxRichTextCtrl::CanPaste() const
2034 {
2035 if ( !IsEditable() )
2036 return false;
2037
2038 return GetBuffer().CanPasteFromClipboard();
2039 }
2040
2041 bool wxRichTextCtrl::CanDeleteSelection() const
2042 {
2043 return HasSelection() && IsEditable();
2044 }
2045
2046
2047 // ----------------------------------------------------------------------------
2048 // Accessors
2049 // ----------------------------------------------------------------------------
2050
2051 void wxRichTextCtrl::SetEditable(bool editable)
2052 {
2053 m_editable = editable;
2054 }
2055
2056 void wxRichTextCtrl::SetInsertionPoint(long pos)
2057 {
2058 SelectNone();
2059
2060 m_caretPosition = pos - 1;
2061 }
2062
2063 void wxRichTextCtrl::SetInsertionPointEnd()
2064 {
2065 long pos = GetLastPosition();
2066 SetInsertionPoint(pos);
2067 }
2068
2069 long wxRichTextCtrl::GetInsertionPoint() const
2070 {
2071 return m_caretPosition+1;
2072 }
2073
2074 wxTextPos 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.
2081 void wxRichTextCtrl::GetSelection(long* from, long* to) const
2082 {
2083 *from = m_selectionRange.GetStart();
2084 *to = m_selectionRange.GetEnd();
2085 if ((*to) != -1 && (*to) != -2)
2086 (*to) ++;
2087 }
2088
2089 bool wxRichTextCtrl::IsEditable() const
2090 {
2091 return m_editable;
2092 }
2093
2094 // ----------------------------------------------------------------------------
2095 // selection
2096 // ----------------------------------------------------------------------------
2097
2098 void 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;
2105 to = GetLastPosition()+1;
2106 }
2107
2108 DoSetSelection(from, to);
2109 }
2110
2111 void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
2112 {
2113 m_selectionAnchor = from;
2114 m_selectionRange.SetRange(from, to-1);
2115
2116 Refresh(false);
2117 PositionCaret();
2118 }
2119
2120 // ----------------------------------------------------------------------------
2121 // Editing
2122 // ----------------------------------------------------------------------------
2123
2124 void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to),
2125 const wxString& value)
2126 {
2127 BeginBatchUndo(_("Replace"));
2128
2129 DeleteSelectedContent();
2130
2131 DoWriteText(value, SetValue_SelectionOnly);
2132
2133 EndBatchUndo();
2134 }
2135
2136 void 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
2145 LayoutContent();
2146 if (!IsFrozen())
2147 Refresh(false);
2148 }
2149
2150 bool wxRichTextCtrl::IsModified() const
2151 {
2152 return m_buffer.IsModified();
2153 }
2154
2155 void wxRichTextCtrl::MarkDirty()
2156 {
2157 m_buffer.Modify(true);
2158 }
2159
2160 void wxRichTextCtrl::DiscardEdits()
2161 {
2162 m_caretPositionForDefaultStyle = -2;
2163 m_buffer.Modify(false);
2164 m_buffer.GetCommandProcessor()->ClearCommands();
2165 }
2166
2167 int wxRichTextCtrl::GetNumberOfLines() const
2168 {
2169 return GetBuffer().GetParagraphCount();
2170 }
2171
2172 // ----------------------------------------------------------------------------
2173 // Positions <-> coords
2174 // ----------------------------------------------------------------------------
2175
2176 long wxRichTextCtrl::XYToPosition(long x, long y) const
2177 {
2178 return GetBuffer().XYToPosition(x, y);
2179 }
2180
2181 bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
2182 {
2183 return GetBuffer().PositionToXY(pos, x, y);
2184 }
2185
2186 // ----------------------------------------------------------------------------
2187 //
2188 // ----------------------------------------------------------------------------
2189
2190 void wxRichTextCtrl::ShowPosition(long pos)
2191 {
2192 if (!IsPositionVisible(pos))
2193 ScrollIntoView(pos-1, WXK_DOWN);
2194 }
2195
2196 int wxRichTextCtrl::GetLineLength(long lineNo) const
2197 {
2198 return GetBuffer().GetParagraphLength(lineNo);
2199 }
2200
2201 wxString wxRichTextCtrl::GetLineText(long lineNo) const
2202 {
2203 return GetBuffer().GetParagraphText(lineNo);
2204 }
2205
2206 // ----------------------------------------------------------------------------
2207 // Undo/redo
2208 // ----------------------------------------------------------------------------
2209
2210 void wxRichTextCtrl::Undo()
2211 {
2212 if (CanUndo())
2213 {
2214 GetCommandProcessor()->Undo();
2215 }
2216 }
2217
2218 void wxRichTextCtrl::Redo()
2219 {
2220 if (CanRedo())
2221 {
2222 GetCommandProcessor()->Redo();
2223 }
2224 }
2225
2226 bool wxRichTextCtrl::CanUndo() const
2227 {
2228 return GetCommandProcessor()->CanUndo();
2229 }
2230
2231 bool wxRichTextCtrl::CanRedo() const
2232 {
2233 return GetCommandProcessor()->CanRedo();
2234 }
2235
2236 // ----------------------------------------------------------------------------
2237 // implementation details
2238 // ----------------------------------------------------------------------------
2239
2240 void wxRichTextCtrl::Command(wxCommandEvent& event)
2241 {
2242 SetValue(event.GetString());
2243 GetEventHandler()->ProcessEvent(event);
2244 }
2245
2246 void 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
2255 wxSize wxRichTextCtrl::DoGetBestSize() const
2256 {
2257 return wxSize(10, 10);
2258 }
2259
2260 // ----------------------------------------------------------------------------
2261 // standard handlers for standard edit menu events
2262 // ----------------------------------------------------------------------------
2263
2264 void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2265 {
2266 Cut();
2267 }
2268
2269 void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
2270 {
2271 DeleteSelection();
2272 }
2273
2274 void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2275 {
2276 Copy();
2277 }
2278
2279 void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2280 {
2281 Paste();
2282 }
2283
2284 void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2285 {
2286 Undo();
2287 }
2288
2289 void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2290 {
2291 Redo();
2292 }
2293
2294 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2295 {
2296 event.Enable( CanCut() );
2297 }
2298
2299 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2300 {
2301 event.Enable( CanCopy() );
2302 }
2303
2304 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2305 {
2306 event.Enable( CanDeleteSelection() );
2307 }
2308
2309 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2310 {
2311 event.Enable( CanPaste() );
2312 }
2313
2314 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2315 {
2316 event.Enable( CanUndo() );
2317 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2318 }
2319
2320 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2321 {
2322 event.Enable( CanRedo() );
2323 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2324 }
2325
2326 void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2327 {
2328 SelectAll();
2329 }
2330
2331 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2332 {
2333 event.Enable(GetLastPosition() > 0);
2334 }
2335
2336 void 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
2355 bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style)
2356 {
2357 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style);
2358 }
2359
2360 bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2361 {
2362 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttrEx(style));
2363 }
2364
2365 bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style)
2366 {
2367 return GetBuffer().SetStyle(range.ToInternal(), style);
2368 }
2369
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.
2373 bool wxRichTextCtrl::SetStyleEx(long start, long end, const wxTextAttrEx& style, int flags)
2374 {
2375 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style, flags);
2376 }
2377
2378 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttrEx& style, int flags)
2379 {
2380 return GetBuffer().SetStyle(range.ToInternal(), style, flags);
2381 }
2382
2383 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxRichTextAttr& style, int flags)
2384 {
2385 return GetBuffer().SetStyle(range.ToInternal(), style, flags);
2386 }
2387
2388 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style)
2389 {
2390 return GetBuffer().SetDefaultStyle(style);
2391 }
2392
2393 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2394 {
2395 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style));
2396 }
2397
2398 const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const
2399 {
2400 return GetBuffer().GetDefaultStyle();
2401 }
2402
2403 const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const
2404 {
2405 return GetBuffer().GetDefaultStyle();
2406 }
2407
2408 bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
2409 {
2410 wxTextAttrEx attr(style);
2411 if (GetBuffer().GetStyle(position, attr))
2412 {
2413 style = attr;
2414 return true;
2415 }
2416 else
2417 return false;
2418 }
2419
2420 bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style)
2421 {
2422 return GetBuffer().GetStyle(position, style);
2423 }
2424
2425 bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style)
2426 {
2427 return GetBuffer().GetStyle(position, style);
2428 }
2429
2430 /// Get the content (uncombined) attributes for this position.
2431
2432 bool 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
2444 bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttrEx& style)
2445 {
2446 return GetBuffer().GetUncombinedStyle(position, style);
2447 }
2448
2449 bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style)
2450 {
2451 return GetBuffer().GetUncombinedStyle(position, style);
2452 }
2453
2454 /// Set font, and also the buffer attributes
2455 bool wxRichTextCtrl::SetFont(const wxFont& font)
2456 {
2457 wxControl::SetFont(font);
2458
2459 wxTextAttrEx attr = GetBuffer().GetAttributes();
2460 attr.SetFont(font);
2461 GetBuffer().SetBasicStyle(attr);
2462
2463 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2464 Refresh(false);
2465
2466 return true;
2467 }
2468
2469 /// Transform logical to physical
2470 wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
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
2479 wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
2480 {
2481 wxPoint pt;
2482 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2483
2484 return pt;
2485 }
2486
2487 /// Position the caret
2488 void wxRichTextCtrl::PositionCaret()
2489 {
2490 if (!GetCaret())
2491 return;
2492
2493 //wxLogDebug(wxT("PositionCaret"));
2494
2495 wxRect caretRect;
2496 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2497 {
2498 wxPoint originalPt = caretRect.GetPosition();
2499 wxPoint pt = GetPhysicalPoint(originalPt);
2500 if (GetCaret()->GetPosition() != pt)
2501 {
2502 GetCaret()->Move(pt);
2503 GetCaret()->SetSize(caretRect.GetSize());
2504 }
2505 }
2506 }
2507
2508 /// Get the caret height and position for the given character position
2509 bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2510 {
2511 wxClientDC dc(this);
2512 dc.SetFont(GetFont());
2513
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 }
2524
2525 return false;
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.
2532 wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2533 {
2534 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2535 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2536 if (line)
2537 {
2538 wxRichTextRange lineRange = line->GetAbsoluteRange();
2539 if (caretPosition == lineRange.GetStart()-1 &&
2540 (para->GetRange().GetStart() != lineRange.GetStart()))
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
2551 bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2552 {
2553 if (GetBuffer().GetDirty())
2554 LayoutContent();
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.
2570 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
2571 {
2572 if (GetBuffer().GetDirty() || onlyVisibleRect)
2573 {
2574 wxRect availableSpace(GetClientSize());
2575 if (availableSpace.width == 0)
2576 availableSpace.width = 10;
2577 if (availableSpace.height == 0)
2578 availableSpace.height = 10;
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 }
2586
2587 wxClientDC dc(this);
2588 dc.SetFont(GetFont());
2589
2590 PrepareDC(dc);
2591
2592 GetBuffer().Defragment();
2593 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2594 GetBuffer().Layout(dc, availableSpace, flags);
2595 GetBuffer().SetDirty(false);
2596
2597 if (!IsFrozen())
2598 SetupScrollbars();
2599 }
2600
2601 return true;
2602 }
2603
2604 /// Is all of the selection bold?
2605 bool wxRichTextCtrl::IsSelectionBold()
2606 {
2607 if (HasSelection())
2608 {
2609 wxRichTextAttr attr;
2610 wxRichTextRange range = GetInternalSelectionRange();
2611 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2612 attr.SetFontWeight(wxBOLD);
2613
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);
2622
2623 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2624 if (GetStyle(pos, attr))
2625 {
2626 if (IsDefaultStyleShowing())
2627 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2628 return attr.GetFontWeight() == wxBOLD;
2629 }
2630 }
2631 return false;
2632 }
2633
2634 /// Is all of the selection italics?
2635 bool wxRichTextCtrl::IsSelectionItalics()
2636 {
2637 if (HasSelection())
2638 {
2639 wxRichTextRange range = GetInternalSelectionRange();
2640 wxRichTextAttr attr;
2641 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2642 attr.SetFontStyle(wxITALIC);
2643
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);
2652
2653 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2654 if (GetStyle(pos, attr))
2655 {
2656 if (IsDefaultStyleShowing())
2657 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2658 return attr.GetFontStyle() == wxITALIC;
2659 }
2660 }
2661 return false;
2662 }
2663
2664 /// Is all of the selection underlined?
2665 bool wxRichTextCtrl::IsSelectionUnderlined()
2666 {
2667 if (HasSelection())
2668 {
2669 wxRichTextRange range = GetInternalSelectionRange();
2670 wxRichTextAttr attr;
2671 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2672 attr.SetFontUnderlined(true);
2673
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);
2682 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2683
2684 if (GetStyle(pos, attr))
2685 {
2686 if (IsDefaultStyleShowing())
2687 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2688 return attr.GetFontUnderlined();
2689 }
2690 }
2691 return false;
2692 }
2693
2694 /// Apply bold to the selection
2695 bool wxRichTextCtrl::ApplyBoldToSelection()
2696 {
2697 wxRichTextAttr attr;
2698 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2699 attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD);
2700
2701 if (HasSelection())
2702 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
2703 else
2704 SetAndShowDefaultStyle(attr);
2705 return true;
2706 }
2707
2708 /// Apply italic to the selection
2709 bool wxRichTextCtrl::ApplyItalicToSelection()
2710 {
2711 wxRichTextAttr attr;
2712 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2713 attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC);
2714
2715 if (HasSelection())
2716 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
2717 else
2718 SetAndShowDefaultStyle(attr);
2719 return true;
2720 }
2721
2722 /// Apply underline to the selection
2723 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2724 {
2725 wxRichTextAttr attr;
2726 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2727 attr.SetFontUnderlined(!IsSelectionUnderlined());
2728
2729 if (HasSelection())
2730 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
2731 else
2732 SetAndShowDefaultStyle(attr);
2733 return true;
2734 }
2735
2736 /// Is all of the selection aligned according to the specified flag?
2737 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
2738 {
2739 wxRichTextRange range;
2740 if (HasSelection())
2741 range = GetInternalSelectionRange();
2742 else
2743 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2744
2745 wxRichTextAttr attr;
2746 attr.SetAlignment(alignment);
2747
2748 return HasParagraphAttributes(range, attr);
2749 }
2750
2751 /// Apply alignment to the selection
2752 bool 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)
2762 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
2763 }
2764 return true;
2765 }
2766
2767 /// Apply a named style to the selection
2768 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
2769 {
2770 // Flags are defined within each definition, so only certain
2771 // attributes are applied.
2772 wxRichTextAttr attr(def->GetStyle());
2773
2774 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE;
2775
2776 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
2777 {
2778 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
2779
2780 wxRichTextRange range;
2781
2782 if (HasSelection())
2783 range = GetSelectionRange();
2784 else
2785 {
2786 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2787 range = wxRichTextRange(pos, pos+1);
2788 }
2789
2790 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
2791 }
2792
2793 // Make sure the attr has the style name
2794 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
2795 {
2796 attr.SetParagraphStyleName(def->GetName());
2797
2798 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2799 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2800 // to change its style independently.
2801 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
2802 }
2803 else
2804 attr.SetCharacterStyleName(def->GetName());
2805
2806 if (HasSelection())
2807 return SetStyleEx(GetSelectionRange(), attr, flags);
2808 else
2809 {
2810 SetAndShowDefaultStyle(attr);
2811 return true;
2812 }
2813 }
2814
2815 /// Apply the style sheet to the buffer, for example if the styles have changed.
2816 bool 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
2833 /// Sets the default style to the style under the cursor
2834 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2835 {
2836 wxTextAttrEx attr;
2837 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
2838
2839 // If at the start of a paragraph, use the next position.
2840 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2841
2842 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2843 if (GetUncombinedStyle(pos, attr))
2844 #else
2845 if (GetStyle(pos, attr))
2846 #endif
2847 {
2848 SetDefaultStyle(attr);
2849 return true;
2850 }
2851
2852 return false;
2853 }
2854
2855 /// Returns the first visible position in the current view
2856 long 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
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.
2868 long 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
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)
2880 wxRichTextRange 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
2888 void 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() );
2895
2896 SetInternalSelectionRange(range1);
2897 }
2898
2899 /// Set list style
2900 bool 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
2905 bool 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
2911 bool 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
2917 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
2918 {
2919 return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
2920 }
2921
2922 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
2923 {
2924 return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
2925 }
2926
2927 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2928 bool 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
2933 bool 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
2938 const 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
2948 void wxRichTextCtrl::ClearAvailableFontNames()
2949 {
2950 sm_availableFontNames.Clear();
2951 }
2952
2953 #endif
2954 // wxUSE_RICHTEXT