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