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