First part of a layout optimization
[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 drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
235 wxRect availableSpace(wxPoint(0, 0), GetClientSize());
236 if (GetBuffer().GetDirty())
237 {
238 GetBuffer().Layout(dc, availableSpace, GetBuffer().GetRange(), 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->GetRange().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->GetRange().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 // We're at the end of a line. See whether we need to
796 // stay at the same actual caret position but change visual
797 // position, or not.
798 if (oldPosition == line->GetRange().GetEnd())
799 {
800 if (m_caretAtLineStart)
801 {
802 // We're already at the start of the line, so actually move on now.
803 m_caretPosition = oldPosition + 1;
804 m_caretAtLineStart = false;
805 }
806 else
807 {
808 // We're showing at the end of the line, so keep to
809 // the same position but indicate that we're to show
810 // at the start of the next line.
811 m_caretPosition = oldPosition;
812 m_caretAtLineStart = true;
813 }
814 SetDefaultStyleToCursorStyle();
815 return;
816 }
817 }
818 }
819 m_caretPosition ++;
820 SetDefaultStyleToCursorStyle();
821 }
822
823 /// Move caret one visual step backward: this may mean setting a flag
824 /// and keeping the same position if we're going from the end of one line
825 /// to the start of the next, which may be the exact same caret position.
826 void wxRichTextCtrl::MoveCaretBack(long oldPosition)
827 {
828 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
829
830 // Only do the check if we're not at the start of the paragraph (where things work OK
831 // anyway)
832 if (para && (oldPosition != para->GetRange().GetStart()))
833 {
834 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
835
836 if (line)
837 {
838 // We're at the start of a line. See whether we need to
839 // stay at the same actual caret position but change visual
840 // position, or not.
841 if (oldPosition == line->GetRange().GetStart())
842 {
843 m_caretPosition = oldPosition-1;
844 m_caretAtLineStart = true;
845 return;
846 }
847 else if (oldPosition == line->GetRange().GetEnd())
848 {
849 if (m_caretAtLineStart)
850 {
851 // We're at the start of the line, so keep the same caret position
852 // but clear the start-of-line flag.
853 m_caretPosition = oldPosition;
854 m_caretAtLineStart = false;
855 }
856 else
857 {
858 // We're showing at the end of the line, so go back
859 // to the previous character position.
860 m_caretPosition = oldPosition - 1;
861 }
862 SetDefaultStyleToCursorStyle();
863 return;
864 }
865 }
866 }
867 m_caretPosition --;
868 SetDefaultStyleToCursorStyle();
869 }
870
871 /// Move right
872 bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
873 {
874 long endPos = GetBuffer().GetRange().GetEnd();
875
876 if (m_caretPosition + noPositions < endPos)
877 {
878 long oldPos = m_caretPosition;
879 long newPos = m_caretPosition + noPositions;
880
881 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
882 if (!extendSel)
883 SelectNone();
884
885 // Determine by looking at oldPos and m_caretPosition whether
886 // we moved from the end of a line to the start of the next line, in which case
887 // we want to adjust the caret position such that it is positioned at the
888 // start of the next line, rather than jumping past the first character of the
889 // line.
890 if (noPositions == 1 && !extendSel)
891 MoveCaretForward(oldPos);
892 else
893 SetCaretPosition(newPos);
894
895 PositionCaret();
896 SetDefaultStyleToCursorStyle();
897
898 if (!IsFrozen())
899 Refresh(); // TODO: optimize so that if we didn't change the selection, we don't refresh
900 return true;
901 }
902 else
903 return false;
904 }
905
906 /// Move left
907 bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
908 {
909 long startPos = -1;
910
911 if (m_caretPosition > startPos - noPositions + 1)
912 {
913 long oldPos = m_caretPosition;
914 long newPos = m_caretPosition - noPositions;
915 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
916 if (!extendSel)
917 SelectNone();
918
919 if (noPositions == 1 && !extendSel)
920 MoveCaretBack(oldPos);
921 else
922 SetCaretPosition(newPos);
923
924 PositionCaret();
925 SetDefaultStyleToCursorStyle();
926
927 if (!IsFrozen())
928 Refresh();
929 return true;
930 }
931 else
932 return false;
933 }
934
935 /// Move up
936 bool wxRichTextCtrl::MoveUp(int noLines, int flags)
937 {
938 return MoveDown(- noLines, flags);
939 }
940
941 /// Move up
942 bool wxRichTextCtrl::MoveDown(int noLines, int flags)
943 {
944 long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
945 wxPoint pt = GetCaret()->GetPosition();
946 long newLine = lineNumber + noLines;
947
948 if (lineNumber != -1)
949 {
950 if (noLines > 0)
951 {
952 long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
953
954 if (newLine > lastLine)
955 return false;
956 }
957 else
958 {
959 if (newLine < 0)
960 return false;
961 }
962 }
963
964 wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine);
965 if (lineObj)
966 {
967 pt.y = lineObj->GetAbsolutePosition().y + 2;
968 }
969 else
970 return false;
971
972 long newPos = 0;
973 wxClientDC dc(this);
974 PrepareDC(dc);
975 dc.SetFont(GetFont());
976
977 int hitTest = GetBuffer().HitTest(dc, pt, newPos);
978
979 if (hitTest != wxRICHTEXT_HITTEST_NONE)
980 {
981 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
982 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
983 // so we view the caret at the start of the line.
984 bool caretLineStart = false;
985 if (hitTest == wxRICHTEXT_HITTEST_BEFORE)
986 {
987 wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1);
988 if (thisLine && (newPos-1) == thisLine->GetRange().GetEnd())
989 {
990 // if (para->GetRange().GetStart() != thisLine->GetRange().GetStart())
991 {
992 newPos --;
993 caretLineStart = true;
994 }
995 }
996 else
997 {
998 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos);
999 if (para && para->GetRange().GetStart() == newPos)
1000 newPos --;
1001 }
1002 }
1003
1004 long newSelEnd = newPos;
1005
1006 if (!ExtendSelection(m_caretPosition, newSelEnd, flags))
1007 SelectNone();
1008
1009 SetCaretPosition(newPos, caretLineStart);
1010 PositionCaret();
1011 SetDefaultStyleToCursorStyle();
1012
1013 if (!IsFrozen())
1014 Refresh();
1015 return true;
1016 }
1017 else
1018 return false;
1019 }
1020
1021 /// Move to the end of the paragraph
1022 bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1023 {
1024 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1025 if (para)
1026 {
1027 long newPos = para->GetRange().GetEnd() - 1;
1028 if (!ExtendSelection(m_caretPosition, newPos, flags))
1029 SelectNone();
1030
1031 SetCaretPosition(newPos);
1032 PositionCaret();
1033 SetDefaultStyleToCursorStyle();
1034
1035 if (!IsFrozen())
1036 Refresh();
1037 return true;
1038 }
1039
1040 return false;
1041 }
1042
1043 /// Move to the start of the paragraph
1044 bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1045 {
1046 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1047 if (para)
1048 {
1049 long newPos = para->GetRange().GetStart() - 1;
1050 if (!ExtendSelection(m_caretPosition, newPos, flags))
1051 SelectNone();
1052
1053 SetCaretPosition(newPos);
1054 PositionCaret();
1055 SetDefaultStyleToCursorStyle();
1056
1057 if (!IsFrozen())
1058 Refresh();
1059 return true;
1060 }
1061
1062 return false;
1063 }
1064
1065 /// Move to the end of the line
1066 bool wxRichTextCtrl::MoveToLineEnd(int flags)
1067 {
1068 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1069
1070 if (line)
1071 {
1072 long newPos = line->GetRange().GetEnd();
1073 if (!ExtendSelection(m_caretPosition, newPos, flags))
1074 SelectNone();
1075
1076 SetCaretPosition(newPos);
1077 PositionCaret();
1078 SetDefaultStyleToCursorStyle();
1079
1080 if (!IsFrozen())
1081 Refresh();
1082 return true;
1083 }
1084
1085 return false;
1086 }
1087
1088 /// Move to the start of the line
1089 bool wxRichTextCtrl::MoveToLineStart(int flags)
1090 {
1091 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1092 if (line)
1093 {
1094 long newPos = line->GetRange().GetStart()-1;
1095
1096 if (!ExtendSelection(m_caretPosition, newPos, flags))
1097 SelectNone();
1098
1099 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
1100
1101 SetCaretPosition(newPos, para->GetRange().GetStart() != line->GetRange().GetStart());
1102 PositionCaret();
1103 SetDefaultStyleToCursorStyle();
1104
1105 if (!IsFrozen())
1106 Refresh();
1107 return true;
1108 }
1109
1110 return false;
1111 }
1112
1113 /// Move to the start of the buffer
1114 bool wxRichTextCtrl::MoveHome(int flags)
1115 {
1116 if (m_caretPosition != -1)
1117 {
1118 if (!ExtendSelection(m_caretPosition, -1, flags))
1119 SelectNone();
1120
1121 SetCaretPosition(-1);
1122 PositionCaret();
1123 SetDefaultStyleToCursorStyle();
1124
1125 if (!IsFrozen())
1126 Refresh();
1127 return true;
1128 }
1129 else
1130 return false;
1131 }
1132
1133 /// Move to the end of the buffer
1134 bool wxRichTextCtrl::MoveEnd(int flags)
1135 {
1136 long endPos = GetBuffer().GetRange().GetEnd()-1;
1137
1138 if (m_caretPosition != endPos)
1139 {
1140 if (!ExtendSelection(m_caretPosition, endPos, flags))
1141 SelectNone();
1142
1143 SetCaretPosition(endPos);
1144 PositionCaret();
1145 SetDefaultStyleToCursorStyle();
1146
1147 if (!IsFrozen())
1148 Refresh();
1149 return true;
1150 }
1151 else
1152 return false;
1153 }
1154
1155 /// Move noPages pages up
1156 bool wxRichTextCtrl::PageUp(int noPages, int flags)
1157 {
1158 return PageDown(- noPages, flags);
1159 }
1160
1161 /// Move noPages pages down
1162 bool wxRichTextCtrl::PageDown(int noPages, int flags)
1163 {
1164 // Calculate which line occurs noPages * screen height further down.
1165 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1166 if (line)
1167 {
1168 wxSize clientSize = GetClientSize();
1169 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
1170
1171 wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY);
1172 if (newLine)
1173 {
1174 long pos = newLine->GetRange().GetStart()-1;
1175 if (pos != m_caretPosition)
1176 {
1177 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
1178
1179 if (!ExtendSelection(m_caretPosition, pos, flags))
1180 SelectNone();
1181
1182 SetCaretPosition(pos, para->GetRange().GetStart() != newLine->GetRange().GetStart());
1183 PositionCaret();
1184 SetDefaultStyleToCursorStyle();
1185
1186 if (!IsFrozen())
1187 Refresh();
1188 return true;
1189 }
1190 }
1191 }
1192
1193 return false;
1194 }
1195
1196 // Finds the caret position for the next word
1197 long wxRichTextCtrl::FindNextWordPosition(int direction) const
1198 {
1199 long endPos = GetBuffer().GetRange().GetEnd();
1200
1201 if (direction > 0)
1202 {
1203 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
1204
1205 // First skip current text to space
1206 while (i < endPos && i > -1)
1207 {
1208 // i is in character, not caret positions
1209 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1210 if (text != wxT(" ") && !text.IsEmpty())
1211 i += direction;
1212 else
1213 {
1214 break;
1215 }
1216 }
1217 while (i < endPos && i > -1)
1218 {
1219 // i is in character, not caret positions
1220 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1221 if (text.IsEmpty()) // End of paragraph, or maybe an image
1222 return wxMax(-1, i - 1);
1223 else if (text == wxT(" ") || text.IsEmpty())
1224 i += direction;
1225 else
1226 {
1227 // Convert to caret position
1228 return wxMax(-1, i - 1);
1229 }
1230 }
1231 if (i >= endPos)
1232 return endPos-1;
1233 return i-1;
1234 }
1235 else
1236 {
1237 long i = m_caretPosition;
1238
1239 // First skip white space
1240 while (i < endPos && i > -1)
1241 {
1242 // i is in character, not caret positions
1243 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1244 if (text.IsEmpty()) // End of paragraph, or maybe an image
1245 break;
1246 else if (text == wxT(" ") || text.IsEmpty())
1247 i += direction;
1248 else
1249 break;
1250 }
1251 // Next skip current text to space
1252 while (i < endPos && i > -1)
1253 {
1254 // i is in character, not caret positions
1255 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1256 if (text != wxT(" ") /* && !text.IsEmpty() */)
1257 i += direction;
1258 else
1259 {
1260 return i;
1261 break;
1262 }
1263 }
1264 if (i < -1)
1265 return -1;
1266 return i;
1267 }
1268 }
1269
1270 /// Move n words left
1271 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
1272 {
1273 long pos = FindNextWordPosition(-1);
1274 if (pos != m_caretPosition)
1275 {
1276 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1277
1278 if (!ExtendSelection(m_caretPosition, pos, flags))
1279 SelectNone();
1280
1281 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1282 PositionCaret();
1283 SetDefaultStyleToCursorStyle();
1284
1285 if (!IsFrozen())
1286 Refresh();
1287 return true;
1288 }
1289
1290 return false;
1291 }
1292
1293 /// Move n words right
1294 bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
1295 {
1296 long pos = FindNextWordPosition(1);
1297 if (pos != m_caretPosition)
1298 {
1299 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1300
1301 if (!ExtendSelection(m_caretPosition, pos, flags))
1302 SelectNone();
1303
1304 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1305 PositionCaret();
1306 SetDefaultStyleToCursorStyle();
1307
1308 if (!IsFrozen())
1309 Refresh();
1310 return true;
1311 }
1312
1313 return false;
1314 }
1315
1316 /// Sizing
1317 void wxRichTextCtrl::OnSize(wxSizeEvent& event)
1318 {
1319 GetBuffer().SetDirty(true);
1320
1321 RecreateBuffer();
1322
1323 event.Skip();
1324 }
1325
1326 /// Set up scrollbars, e.g. after a resize
1327 void wxRichTextCtrl::SetupScrollbars()
1328 {
1329 if (m_freezeCount)
1330 return;
1331
1332 if (GetBuffer().IsEmpty())
1333 {
1334 SetScrollbars(0, 0, 0, 0, 0, 0);
1335 return;
1336 }
1337
1338 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1339 // of pixels. See e.g. wxVScrolledWindow for ideas.
1340 int pixelsPerUnit = 5; // 10;
1341 wxSize clientSize = GetClientSize();
1342
1343 int maxHeight = GetBuffer().GetCachedSize().y;
1344
1345 int unitsY = maxHeight/pixelsPerUnit;
1346
1347 int startX, startY;
1348 GetViewStart(& startX, & startY);
1349
1350 int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0);
1351 int maxPositionY = (wxMax(maxHeight - clientSize.y, 0))/pixelsPerUnit;
1352
1353 // Move to previous scroll position if
1354 // possible
1355 SetScrollbars(0, pixelsPerUnit,
1356 0, unitsY,
1357 wxMin(maxPositionX, startX), wxMin(maxPositionY, startY));
1358 }
1359
1360 /// Paint the background
1361 void wxRichTextCtrl::PaintBackground(wxDC& dc)
1362 {
1363 wxColour backgroundColour = GetBackgroundColour();
1364 if (!backgroundColour.Ok())
1365 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1366
1367 // Clear the background
1368 dc.SetBrush(wxBrush(backgroundColour));
1369 dc.SetPen(*wxTRANSPARENT_PEN);
1370 wxRect windowRect(wxPoint(0, 0), GetClientSize());
1371 windowRect.x -= 2; windowRect.y -= 2;
1372 windowRect.width += 4; windowRect.height += 4;
1373
1374 // We need to shift the rectangle to take into account
1375 // scrolling. Converting device to logical coordinates.
1376 CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y);
1377 dc.DrawRectangle(windowRect);
1378 }
1379
1380 /// Recreate buffer bitmap if necessary
1381 bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
1382 {
1383 wxSize sz = size;
1384 if (sz == wxDefaultSize)
1385 sz = GetClientSize();
1386
1387 if (sz.x < 1 || sz.y < 1)
1388 return false;
1389
1390 if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
1391 m_bufferBitmap = wxBitmap(sz.x, sz.y);
1392 return m_bufferBitmap.Ok();
1393 }
1394
1395 // ----------------------------------------------------------------------------
1396 // file IO functions
1397 // ----------------------------------------------------------------------------
1398
1399 bool wxRichTextCtrl::LoadFile(const wxString& filename, int type)
1400 {
1401 bool success = GetBuffer().LoadFile(filename, type);
1402 if (success)
1403 m_filename = filename;
1404
1405 DiscardEdits();
1406 SetInsertionPoint(0);
1407 Layout();
1408 PositionCaret();
1409 Refresh();
1410 SendUpdateEvent();
1411
1412 if (success)
1413 return true;
1414 else
1415 {
1416 wxLogError(_("File couldn't be loaded."));
1417
1418 return false;
1419 }
1420 }
1421
1422 bool wxRichTextCtrl::SaveFile(const wxString& filename, int type)
1423 {
1424 wxString filenameToUse = filename.empty() ? m_filename : filename;
1425 if ( filenameToUse.empty() )
1426 {
1427 // what kind of message to give? is it an error or a program bug?
1428 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1429
1430 return false;
1431 }
1432
1433 if (GetBuffer().SaveFile(filenameToUse, type))
1434 {
1435 m_filename = filenameToUse;
1436
1437 DiscardEdits();
1438
1439 return true;
1440
1441 }
1442
1443 wxLogError(_("The text couldn't be saved."));
1444
1445 return false;
1446 }
1447
1448 // ----------------------------------------------------------------------------
1449 // wxRichTextCtrl specific functionality
1450 // ----------------------------------------------------------------------------
1451
1452 /// Add a new paragraph of text to the end of the buffer
1453 wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
1454 {
1455 return GetBuffer().AddParagraph(text);
1456 }
1457
1458 /// Add an image
1459 wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
1460 {
1461 return GetBuffer().AddImage(image);
1462 }
1463
1464 // ----------------------------------------------------------------------------
1465 // selection and ranges
1466 // ----------------------------------------------------------------------------
1467
1468 void wxRichTextCtrl::SelectAll()
1469 {
1470 SetSelection(0, GetLastPosition());
1471 m_selectionAnchor = -1;
1472 }
1473
1474 /// Select none
1475 void wxRichTextCtrl::SelectNone()
1476 {
1477 SetSelection(-2, -2);
1478 m_selectionAnchor = -2;
1479 }
1480
1481 wxString wxRichTextCtrl::GetStringSelection() const
1482 {
1483 long from, to;
1484 GetSelection(&from, &to);
1485
1486 return GetRange(from, to);
1487 }
1488
1489 // do the window-specific processing after processing the update event
1490 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event)
1491 {
1492 if ( event.GetSetEnabled() )
1493 Enable(event.GetEnabled());
1494
1495 if ( event.GetSetText() )
1496 {
1497 if ( event.GetText() != GetValue() )
1498 SetValue(event.GetText());
1499 }
1500 }
1501
1502 // ----------------------------------------------------------------------------
1503 // hit testing
1504 // ----------------------------------------------------------------------------
1505
1506 wxTextCtrlHitTestResult
1507 wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
1508 {
1509 // implement in terms of the other overload as the native ports typically
1510 // can get the position and not (x, y) pair directly (although wxUniv
1511 // directly gets x and y -- and so overrides this method as well)
1512 long pos;
1513 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
1514
1515 if ( rc != wxTE_HT_UNKNOWN )
1516 {
1517 PositionToXY(pos, x, y);
1518 }
1519
1520 return rc;
1521 }
1522
1523 wxTextCtrlHitTestResult
1524 wxRichTextCtrl::HitTest(const wxPoint& pt,
1525 long * pos) const
1526 {
1527 wxClientDC dc((wxRichTextCtrl*) this);
1528 ((wxRichTextCtrl*)this)->PrepareDC(dc);
1529
1530 int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt, *pos);
1531 if (hit == wxRICHTEXT_HITTEST_BEFORE)
1532 return wxTE_HT_BEFORE;
1533 else if (hit == wxRICHTEXT_HITTEST_AFTER)
1534 return wxTE_HT_BEYOND;
1535 else if (hit == wxRICHTEXT_HITTEST_ON)
1536 return wxTE_HT_ON_TEXT;
1537 else
1538 return wxTE_HT_UNKNOWN;
1539 }
1540
1541 // ----------------------------------------------------------------------------
1542 // set/get the controls text
1543 // ----------------------------------------------------------------------------
1544
1545 wxString wxRichTextCtrl::GetValue() const
1546 {
1547 return GetBuffer().GetText();
1548 }
1549
1550 wxString wxRichTextCtrl::GetRange(long from, long to) const
1551 {
1552 return GetBuffer().GetTextForRange(wxRichTextRange(from, to));
1553 }
1554
1555 void wxRichTextCtrl::SetValue(const wxString& value)
1556 {
1557 Clear();
1558
1559 // if the text is long enough, it's faster to just set it instead of first
1560 // comparing it with the old one (chances are that it will be different
1561 // anyhow, this comparison is there to avoid flicker for small single-line
1562 // edit controls mostly)
1563 if ( (value.length() > 0x400) || (value != GetValue()) )
1564 {
1565 DoWriteText(value, false /* not selection only */);
1566
1567 // for compatibility, don't move the cursor when doing SetValue()
1568 SetInsertionPoint(0);
1569 }
1570 else // same text
1571 {
1572 // still send an event for consistency
1573 SendUpdateEvent();
1574 }
1575
1576 // we should reset the modified flag even if the value didn't really change
1577
1578 // mark the control as being not dirty - we changed its text, not the
1579 // user
1580 DiscardEdits();
1581 }
1582
1583 void wxRichTextCtrl::WriteText(const wxString& value)
1584 {
1585 DoWriteText(value);
1586 }
1587
1588 void wxRichTextCtrl::DoWriteText(const wxString& value, bool WXUNUSED(selectionOnly))
1589 {
1590 GetBuffer().InsertTextWithUndo(m_caretPosition+1, value, this);
1591 }
1592
1593 void wxRichTextCtrl::AppendText(const wxString& text)
1594 {
1595 SetInsertionPointEnd();
1596
1597 WriteText(text);
1598 }
1599
1600 /// Write an image at the current insertion point
1601 bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType)
1602 {
1603 wxRichTextImageBlock imageBlock;
1604
1605 wxImage image2 = image;
1606 if (imageBlock.MakeImageBlock(image2, bitmapType))
1607 return WriteImage(imageBlock);
1608 else
1609 return false;
1610 }
1611
1612 bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType)
1613 {
1614 wxRichTextImageBlock imageBlock;
1615
1616 wxImage image;
1617 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
1618 return WriteImage(imageBlock);
1619 else
1620 return false;
1621 }
1622
1623 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
1624 {
1625 return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
1626 }
1627
1628 bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType)
1629 {
1630 if (bitmap.Ok())
1631 {
1632 wxRichTextImageBlock imageBlock;
1633
1634 wxImage image = bitmap.ConvertToImage();
1635 if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType))
1636 return WriteImage(imageBlock);
1637 else
1638 return false;
1639 }
1640 return false;
1641 }
1642
1643 /// Insert a newline (actually paragraph) at the current insertion point.
1644 bool wxRichTextCtrl::Newline()
1645 {
1646 return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this);
1647 }
1648
1649
1650 // ----------------------------------------------------------------------------
1651 // Clipboard operations
1652 // ----------------------------------------------------------------------------
1653
1654 void wxRichTextCtrl::Copy()
1655 {
1656 if (CanCopy())
1657 {
1658 wxRichTextRange range = GetSelectionRange();
1659 GetBuffer().CopyToClipboard(range);
1660 }
1661 }
1662
1663 void wxRichTextCtrl::Cut()
1664 {
1665 if (CanCut())
1666 {
1667 wxRichTextRange range = GetSelectionRange();
1668 GetBuffer().CopyToClipboard(range);
1669
1670 DeleteSelectedContent();
1671 Layout();
1672 Refresh();
1673 }
1674 }
1675
1676 void wxRichTextCtrl::Paste()
1677 {
1678 if (CanPaste())
1679 {
1680 BeginBatchUndo(_("Paste"));
1681
1682 long newPos = m_caretPosition;
1683 DeleteSelectedContent(& newPos);
1684
1685 GetBuffer().PasteFromClipboard(newPos);
1686
1687 EndBatchUndo();
1688 }
1689 }
1690
1691 void wxRichTextCtrl::DeleteSelection()
1692 {
1693 if (CanDeleteSelection())
1694 {
1695 DeleteSelectedContent();
1696 }
1697 }
1698
1699 bool wxRichTextCtrl::HasSelection() const
1700 {
1701 return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2;
1702 }
1703
1704 bool wxRichTextCtrl::CanCopy() const
1705 {
1706 // Can copy if there's a selection
1707 return HasSelection();
1708 }
1709
1710 bool wxRichTextCtrl::CanCut() const
1711 {
1712 return HasSelection() && IsEditable();
1713 }
1714
1715 bool wxRichTextCtrl::CanPaste() const
1716 {
1717 if ( !IsEditable() )
1718 return false;
1719
1720 return GetBuffer().CanPasteFromClipboard();
1721 }
1722
1723 bool wxRichTextCtrl::CanDeleteSelection() const
1724 {
1725 return HasSelection() && IsEditable();
1726 }
1727
1728
1729 // ----------------------------------------------------------------------------
1730 // Accessors
1731 // ----------------------------------------------------------------------------
1732
1733 void wxRichTextCtrl::SetEditable(bool editable)
1734 {
1735 m_editable = editable;
1736 }
1737
1738 void wxRichTextCtrl::SetInsertionPoint(long pos)
1739 {
1740 SelectNone();
1741
1742 m_caretPosition = pos - 1;
1743 }
1744
1745 void wxRichTextCtrl::SetInsertionPointEnd()
1746 {
1747 long pos = GetLastPosition();
1748 SetInsertionPoint(pos);
1749 }
1750
1751 long wxRichTextCtrl::GetInsertionPoint() const
1752 {
1753 return m_caretPosition+1;
1754 }
1755
1756 wxTextPos wxRichTextCtrl::GetLastPosition() const
1757 {
1758 return GetBuffer().GetRange().GetEnd();
1759 }
1760
1761 // If the return values from and to are the same, there is no
1762 // selection.
1763 void wxRichTextCtrl::GetSelection(long* from, long* to) const
1764 {
1765 *from = m_selectionRange.GetStart();
1766 *to = m_selectionRange.GetEnd();
1767 }
1768
1769 bool wxRichTextCtrl::IsEditable() const
1770 {
1771 return m_editable;
1772 }
1773
1774 // ----------------------------------------------------------------------------
1775 // selection
1776 // ----------------------------------------------------------------------------
1777
1778 void wxRichTextCtrl::SetSelection(long from, long to)
1779 {
1780 // if from and to are both -1, it means (in wxWidgets) that all text should
1781 // be selected.
1782 if ( (from == -1) && (to == -1) )
1783 {
1784 from = 0;
1785 to = GetLastPosition();
1786 }
1787
1788 DoSetSelection(from, to);
1789 }
1790
1791 void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
1792 {
1793 m_selectionAnchor = from;
1794 m_selectionRange.SetRange(from, to);
1795 if (!IsFrozen())
1796 Refresh();
1797 PositionCaret();
1798 }
1799
1800 // ----------------------------------------------------------------------------
1801 // Editing
1802 // ----------------------------------------------------------------------------
1803
1804 void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to), const wxString& value)
1805 {
1806 BeginBatchUndo(_("Replace"));
1807
1808 DeleteSelectedContent();
1809
1810 DoWriteText(value, true /* selection only */);
1811
1812 EndBatchUndo();
1813 }
1814
1815 void wxRichTextCtrl::Remove(long from, long to)
1816 {
1817 SelectNone();
1818
1819 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to),
1820 m_caretPosition, // Current caret position
1821 from, // New caret position
1822 this);
1823
1824 Layout();
1825 if (!IsFrozen())
1826 Refresh();
1827 }
1828
1829 bool wxRichTextCtrl::IsModified() const
1830 {
1831 return m_buffer.IsModified();
1832 }
1833
1834 void wxRichTextCtrl::MarkDirty()
1835 {
1836 m_buffer.Modify(true);
1837 }
1838
1839 void wxRichTextCtrl::DiscardEdits()
1840 {
1841 m_buffer.Modify(false);
1842 m_buffer.GetCommandProcessor()->ClearCommands();
1843 }
1844
1845 int wxRichTextCtrl::GetNumberOfLines() const
1846 {
1847 return GetBuffer().GetParagraphCount();
1848 }
1849
1850 // ----------------------------------------------------------------------------
1851 // Positions <-> coords
1852 // ----------------------------------------------------------------------------
1853
1854 long wxRichTextCtrl::XYToPosition(long x, long y) const
1855 {
1856 return GetBuffer().XYToPosition(x, y);
1857 }
1858
1859 bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
1860 {
1861 return GetBuffer().PositionToXY(pos, x, y);
1862 }
1863
1864 // ----------------------------------------------------------------------------
1865 //
1866 // ----------------------------------------------------------------------------
1867
1868 void wxRichTextCtrl::ShowPosition(long pos)
1869 {
1870 if (!IsPositionVisible(pos))
1871 ScrollIntoView(pos-1, WXK_DOWN);
1872 }
1873
1874 int wxRichTextCtrl::GetLineLength(long lineNo) const
1875 {
1876 return GetBuffer().GetParagraphLength(lineNo);
1877 }
1878
1879 wxString wxRichTextCtrl::GetLineText(long lineNo) const
1880 {
1881 return GetBuffer().GetParagraphText(lineNo);
1882 }
1883
1884 // ----------------------------------------------------------------------------
1885 // Undo/redo
1886 // ----------------------------------------------------------------------------
1887
1888 void wxRichTextCtrl::Undo()
1889 {
1890 if (CanUndo())
1891 {
1892 GetCommandProcessor()->Undo();
1893 }
1894 }
1895
1896 void wxRichTextCtrl::Redo()
1897 {
1898 if (CanRedo())
1899 {
1900 GetCommandProcessor()->Redo();
1901 }
1902 }
1903
1904 bool wxRichTextCtrl::CanUndo() const
1905 {
1906 return GetCommandProcessor()->CanUndo();
1907 }
1908
1909 bool wxRichTextCtrl::CanRedo() const
1910 {
1911 return GetCommandProcessor()->CanRedo();
1912 }
1913
1914 // ----------------------------------------------------------------------------
1915 // implemenation details
1916 // ----------------------------------------------------------------------------
1917
1918 void wxRichTextCtrl::Command(wxCommandEvent & event)
1919 {
1920 SetValue(event.GetString());
1921 GetEventHandler()->ProcessEvent(event);
1922 }
1923
1924 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1925 {
1926 // By default, load the first file into the text window.
1927 if (event.GetNumberOfFiles() > 0)
1928 {
1929 LoadFile(event.GetFiles()[0]);
1930 }
1931 }
1932
1933 // ----------------------------------------------------------------------------
1934 // text control event processing
1935 // ----------------------------------------------------------------------------
1936
1937 bool wxRichTextCtrl::SendUpdateEvent()
1938 {
1939 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
1940 InitCommandEvent(event);
1941
1942 return GetEventHandler()->ProcessEvent(event);
1943 }
1944
1945 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent& event) const
1946 {
1947 event.SetEventObject((wxControlBase *)this); // const_cast
1948
1949 switch ( m_clientDataType )
1950 {
1951 case wxClientData_Void:
1952 event.SetClientData(GetClientData());
1953 break;
1954
1955 case wxClientData_Object:
1956 event.SetClientObject(GetClientObject());
1957 break;
1958
1959 case wxClientData_None:
1960 // nothing to do
1961 ;
1962 }
1963 }
1964
1965
1966 wxSize wxRichTextCtrl::DoGetBestSize() const
1967 {
1968 return wxSize(10, 10);
1969 }
1970
1971 // ----------------------------------------------------------------------------
1972 // standard handlers for standard edit menu events
1973 // ----------------------------------------------------------------------------
1974
1975 void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1976 {
1977 Cut();
1978 }
1979
1980 void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
1981 {
1982 DeleteSelection();
1983 }
1984
1985 void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1986 {
1987 Copy();
1988 }
1989
1990 void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1991 {
1992 Paste();
1993 }
1994
1995 void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1996 {
1997 Undo();
1998 }
1999
2000 void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2001 {
2002 Redo();
2003 }
2004
2005 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2006 {
2007 event.Enable( CanCut() );
2008 }
2009
2010 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2011 {
2012 event.Enable( CanCopy() );
2013 }
2014
2015 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2016 {
2017 event.Enable( CanDeleteSelection() );
2018 }
2019
2020 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2021 {
2022 event.Enable( CanPaste() );
2023 }
2024
2025 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2026 {
2027 event.Enable( CanUndo() );
2028 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2029 }
2030
2031 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2032 {
2033 event.Enable( CanRedo() );
2034 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2035 }
2036
2037 void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2038 {
2039 SelectAll();
2040 }
2041
2042 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2043 {
2044 event.Enable(GetLastPosition() > 0);
2045 }
2046
2047 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& WXUNUSED(event))
2048 {
2049 if (!m_contextMenu)
2050 {
2051 m_contextMenu = new wxMenu;
2052 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
2053 m_contextMenu->Append(wxID_REDO, _("&Redo"));
2054 m_contextMenu->AppendSeparator();
2055 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
2056 m_contextMenu->Append(wxID_COPY, _("&Copy"));
2057 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
2058 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
2059 m_contextMenu->AppendSeparator();
2060 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
2061 }
2062 PopupMenu(m_contextMenu);
2063 return;
2064 }
2065
2066 bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style)
2067 {
2068 return GetBuffer().SetStyle(wxRichTextRange(start, end), style);
2069 }
2070
2071 bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style)
2072 {
2073 return GetBuffer().SetStyle(range, style);
2074 }
2075
2076 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style)
2077 {
2078 return GetBuffer().SetDefaultStyle(style);
2079 }
2080
2081 const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const
2082 {
2083 return GetBuffer().GetDefaultStyle();
2084 }
2085
2086 bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style) const
2087 {
2088 return GetBuffer().GetStyle(position, style);
2089 }
2090
2091 bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style) const
2092 {
2093 return GetBuffer().GetStyle(position, style);
2094 }
2095
2096 /// Set font, and also the buffer attributes
2097 bool wxRichTextCtrl::SetFont(const wxFont& font)
2098 {
2099 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2100 wxControl::SetFont(font);
2101 #else
2102 wxScrolledWindow::SetFont(font);
2103 #endif
2104
2105 wxTextAttrEx attr = GetBuffer().GetAttributes();
2106 attr.SetFont(font);
2107 GetBuffer().SetBasicStyle(attr);
2108 GetBuffer().SetDefaultStyle(attr);
2109
2110 return true;
2111 }
2112
2113 /// Transform logical to physical (unscrolling)
2114 wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical)
2115 {
2116 wxPoint pt;
2117 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
2118
2119 return pt;
2120 }
2121
2122 /// Transform physical to logical
2123 wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical)
2124 {
2125 wxPoint pt;
2126 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2127
2128 return pt;
2129 }
2130
2131 /// Position the caret
2132 void wxRichTextCtrl::PositionCaret()
2133 {
2134 wxRect caretRect;
2135 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2136 {
2137 wxPoint originalPt = caretRect.GetPosition();
2138 wxPoint pt = GetPhysicalPoint(originalPt);
2139
2140 GetCaret()->Move(pt);
2141 GetCaret()->SetSize(caretRect.GetSize());
2142 }
2143 }
2144
2145 /// Get the caret height and position for the given character position
2146 bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2147 {
2148 wxClientDC dc(this);
2149 dc.SetFont(GetFont());
2150
2151 PrepareDC(dc);
2152
2153 wxPoint pt;
2154 int height = 0;
2155
2156 if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart))
2157 {
2158 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
2159 return true;
2160 }
2161 else
2162 return false;
2163 }
2164
2165 /// Gets the line for the visible caret position. If the caret is
2166 /// shown at the very end of the line, it means the next character is actually
2167 /// on the following line. So let's get the line we're expecting to find
2168 /// if this is the case.
2169 wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2170 {
2171 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2172 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2173 if (line)
2174 {
2175 if (caretPosition == line->GetRange().GetStart()-1 &&
2176 (para->GetRange().GetStart() != line->GetRange().GetStart()))
2177 {
2178 if (!m_caretAtLineStart)
2179 line = GetBuffer().GetLineAtPosition(caretPosition-1, true);
2180 }
2181 }
2182 return line;
2183 }
2184
2185
2186 /// Move the caret to the given character position
2187 bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2188 {
2189 if (GetBuffer().GetDirty())
2190 Layout();
2191
2192 if (pos <= GetBuffer().GetRange().GetEnd())
2193 {
2194 SetCaretPosition(pos, showAtLineStart);
2195
2196 PositionCaret();
2197
2198 return true;
2199 }
2200 else
2201 return false;
2202 }
2203
2204 /// Layout the buffer: which we must do before certain operations, such as
2205 /// setting the caret position.
2206 bool wxRichTextCtrl::Layout()
2207 {
2208 wxRect availableSpace(wxPoint(0, 0), GetClientSize());
2209 if (availableSpace.width == 0)
2210 availableSpace.width = 10;
2211 if (availableSpace.height == 0)
2212 availableSpace.height = 10;
2213
2214 wxClientDC dc(this);
2215 dc.SetFont(GetFont());
2216
2217 PrepareDC(dc);
2218
2219 GetBuffer().Defragment();
2220 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2221 GetBuffer().Layout(dc, availableSpace, GetBuffer().GetRange(), wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
2222 GetBuffer().SetDirty(false);
2223
2224 if (!IsFrozen())
2225 SetupScrollbars();
2226
2227 return true;
2228 }
2229
2230 /// Is all of the selection bold?
2231 bool wxRichTextCtrl::IsSelectionBold() const
2232 {
2233 if (HasSelection())
2234 {
2235 wxRichTextAttr attr;
2236 wxRichTextRange range = GetSelectionRange();
2237 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2238 attr.SetFontWeight(wxBOLD);
2239
2240 return HasCharacterAttributes(range, attr);
2241 }
2242 else
2243 {
2244 // If no selection, then we need to combine current style with default style
2245 // to see what the effect would be if we started typing.
2246 wxRichTextAttr attr;
2247 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2248 if (GetStyle(GetCaretPosition()+1, attr))
2249 {
2250 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2251 return attr.GetFontWeight() == wxBOLD;
2252 }
2253 }
2254 return false;
2255 }
2256
2257 /// Is all of the selection italics?
2258 bool wxRichTextCtrl::IsSelectionItalics() const
2259 {
2260 if (HasSelection())
2261 {
2262 wxRichTextRange range = GetSelectionRange();
2263 wxRichTextAttr attr;
2264 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2265 attr.SetFontStyle(wxITALIC);
2266
2267 return HasCharacterAttributes(range, attr);
2268 }
2269 else
2270 {
2271 // If no selection, then we need to combine current style with default style
2272 // to see what the effect would be if we started typing.
2273 wxRichTextAttr attr;
2274 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2275 if (GetStyle(GetCaretPosition()+1, attr))
2276 {
2277 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2278 return attr.GetFontStyle() == wxITALIC;
2279 }
2280 }
2281 return false;
2282 }
2283
2284 /// Is all of the selection underlined?
2285 bool wxRichTextCtrl::IsSelectionUnderlined() const
2286 {
2287 if (HasSelection())
2288 {
2289 wxRichTextRange range = GetSelectionRange();
2290 wxRichTextAttr attr;
2291 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2292 attr.SetFontUnderlined(true);
2293
2294 return HasCharacterAttributes(range, attr);
2295 }
2296 else
2297 {
2298 // If no selection, then we need to combine current style with default style
2299 // to see what the effect would be if we started typing.
2300 wxRichTextAttr attr;
2301 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2302 if (GetStyle(GetCaretPosition()+1, attr))
2303 {
2304 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2305 return attr.GetFontUnderlined();
2306 }
2307 }
2308 return false;
2309 }
2310
2311 /// Apply bold to the selection
2312 bool wxRichTextCtrl::ApplyBoldToSelection()
2313 {
2314 wxRichTextAttr attr;
2315 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2316 attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD);
2317
2318 if (HasSelection())
2319 return SetStyle(GetSelectionRange(), attr);
2320 else
2321 SetDefaultStyle(attr);
2322 return true;
2323 }
2324
2325 /// Apply italic to the selection
2326 bool wxRichTextCtrl::ApplyItalicToSelection()
2327 {
2328 wxRichTextAttr attr;
2329 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2330 attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC);
2331
2332 if (HasSelection())
2333 return SetStyle(GetSelectionRange(), attr);
2334 else
2335 SetDefaultStyle(attr);
2336 return true;
2337 }
2338
2339 /// Apply underline to the selection
2340 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2341 {
2342 wxRichTextAttr attr;
2343 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2344 attr.SetFontWeight(!IsSelectionUnderlined());
2345
2346 if (HasSelection())
2347 return SetStyle(GetSelectionRange(), attr);
2348 else
2349 SetDefaultStyle(attr);
2350 return true;
2351 }
2352
2353 /// Is all of the selection aligned according to the specified flag?
2354 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) const
2355 {
2356 if (HasSelection())
2357 {
2358 wxRichTextRange range = GetSelectionRange();
2359 wxRichTextAttr attr;
2360 attr.SetAlignment(alignment);
2361
2362 return HasParagraphAttributes(range, attr);
2363 }
2364 else
2365 {
2366 // If no selection, then we need to get information from the current paragraph.
2367 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2368 if (para)
2369 return para->GetAttributes().GetAlignment() == alignment;
2370 }
2371 return false;
2372 }
2373
2374 /// Apply alignment to the selection
2375 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
2376 {
2377 wxRichTextAttr attr;
2378 attr.SetAlignment(alignment);
2379 if (HasSelection())
2380 return SetStyle(GetSelectionRange(), attr);
2381 else
2382 {
2383 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2384 if (para)
2385 return SetStyle(para->GetRange(), attr);
2386 }
2387 return true;
2388 }
2389
2390 /// Sets the default style to the style under the cursor
2391 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2392 {
2393 wxTextAttrEx attr;
2394 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
2395
2396 if (GetStyle(GetCaretPosition(), attr))
2397 {
2398 SetDefaultStyle(attr);
2399 return true;
2400 }
2401 else
2402 return false;
2403 }
2404
2405 #endif
2406 // wxUSE_RICHTEXT
2407