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