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