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