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