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