]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/richtext/richtextctrl.cpp
don't append anything back in DoDeleteOneItem() if the control becomes empty (fixes...
[wxWidgets.git] / src / richtext / richtextctrl.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/richtext/richeditctrl.cpp
3// Purpose: A rich edit control
4// Author: Julian Smart
5// Modified by:
6// Created: 2005-09-30
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_RICHTEXT
20
21#include "wx/richtext/richtextctrl.h"
22#include "wx/richtext/richtextstyles.h"
23
24#ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #include "wx/settings.h"
27#endif
28
29#include "wx/textfile.h"
30#include "wx/ffile.h"
31#include "wx/filename.h"
32#include "wx/dcbuffer.h"
33#include "wx/arrimpl.cpp"
34#include "wx/fontenum.h"
35
36// DLL options compatibility check:
37#include "wx/app.h"
38WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
39
40DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK)
41DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK)
42DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK)
43DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK)
44DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN)
45DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER)
46DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE)
47
48DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING)
49DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED)
50DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING)
51DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED)
52
53DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED)
54DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED)
55DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED)
56DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED)
57
58IMPLEMENT_CLASS( wxRichTextCtrl, wxControl )
59
60IMPLEMENT_CLASS( wxRichTextEvent, wxNotifyEvent )
61
62BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl )
63 EVT_PAINT(wxRichTextCtrl::OnPaint)
64 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
65 EVT_IDLE(wxRichTextCtrl::OnIdle)
66 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
67 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick)
68 EVT_MOTION(wxRichTextCtrl::OnMoveMouse)
69 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp)
70 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick)
71 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick)
72 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick)
73 EVT_CHAR(wxRichTextCtrl::OnChar)
74 EVT_SIZE(wxRichTextCtrl::OnSize)
75 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
76 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
77 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost)
78 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
79 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged)
80
81 EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
82 EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
83
84 EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo)
85 EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo)
86
87 EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy)
88 EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy)
89
90 EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste)
91 EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste)
92
93 EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut)
94 EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut)
95
96 EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear)
97 EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear)
98
99 EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll)
100 EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll)
101END_EVENT_TABLE()
102
103/*!
104 * wxRichTextCtrl
105 */
106
107wxArrayString wxRichTextCtrl::sm_availableFontNames;
108
109wxRichTextCtrl::wxRichTextCtrl()
110 : wxScrollHelper(this)
111{
112 Init();
113}
114
115wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent,
116 wxWindowID id,
117 const wxString& value,
118 const wxPoint& pos,
119 const wxSize& size,
120 long style,
121 const wxValidator& validator,
122 const wxString& name)
123 : wxScrollHelper(this)
124{
125 Init();
126 Create(parent, id, value, pos, size, style, validator, name);
127}
128
129/// Creation
130bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style,
131 const wxValidator& validator, const wxString& name)
132{
133 if (!wxControl::Create(parent, id, pos, size,
134 style|wxFULL_REPAINT_ON_RESIZE,
135 validator, name))
136 return false;
137
138 if (!GetFont().Ok())
139 {
140 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
141 }
142
143 GetBuffer().Reset();
144 GetBuffer().SetRichTextCtrl(this);
145
146 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
147 GetCaret()->Show();
148
149 if (style & wxTE_READONLY)
150 SetEditable(false);
151
152 // The base attributes must all have default values
153 wxTextAttr attributes;
154 attributes.SetFont(GetFont());
155 attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
156 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
157 attributes.SetLineSpacing(10);
158 attributes.SetParagraphSpacingAfter(10);
159 attributes.SetParagraphSpacingBefore(0);
160 attributes.SetTextEffects(0);
161 attributes.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH|wxTEXT_ATTR_EFFECT_CAPITALS);
162
163 SetBasicStyle(attributes);
164
165 // The default attributes will be merged with base attributes, so
166 // can be empty to begin with
167 wxTextAttr defaultAttributes;
168 SetDefaultStyle(defaultAttributes);
169
170 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
171 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
172
173 // Tell the sizers to use the given or best size
174 SetInitialSize(size);
175
176#if wxRICHTEXT_BUFFERED_PAINTING
177 // Create a buffer
178 RecreateBuffer(size);
179#endif
180
181 m_textCursor = wxCursor(wxCURSOR_IBEAM);
182 m_urlCursor = wxCursor(wxCURSOR_HAND);
183
184 SetCursor(m_textCursor);
185
186 if (!value.IsEmpty())
187 SetValue(value);
188
189 GetBuffer().AddEventHandler(this);
190
191 return true;
192}
193
194wxRichTextCtrl::~wxRichTextCtrl()
195{
196 GetBuffer().RemoveEventHandler(this);
197
198 delete m_contextMenu;
199}
200
201/// Member initialisation
202void wxRichTextCtrl::Init()
203{
204 m_contextMenu = NULL;
205 m_caret = NULL;
206 m_caretPosition = -1;
207 m_selectionRange.SetRange(-2, -2);
208 m_selectionAnchor = -2;
209 m_editable = true;
210 m_caretAtLineStart = false;
211 m_dragging = false;
212 m_fullLayoutRequired = false;
213 m_fullLayoutTime = 0;
214 m_fullLayoutSavedPosition = 0;
215 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
216 m_caretPositionForDefaultStyle = -2;
217}
218
219void wxRichTextCtrl::DoThaw()
220{
221 if (GetBuffer().GetDirty())
222 LayoutContent();
223 else
224 SetupScrollbars();
225 Refresh(false);
226}
227
228/// Clear all text
229void wxRichTextCtrl::Clear()
230{
231 m_buffer.ResetAndClearCommands();
232 m_buffer.SetDirty(true);
233 m_caretPosition = -1;
234 m_caretPositionForDefaultStyle = -2;
235 m_caretAtLineStart = false;
236 m_selectionRange.SetRange(-2, -2);
237
238 if (!IsFrozen())
239 {
240 LayoutContent();
241 Refresh(false);
242 }
243
244 wxTextCtrl::SendTextUpdatedEvent(this);
245}
246
247/// Painting
248void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
249{
250 if (GetCaret() && GetCaret()->IsVisible())
251 GetCaret()->Hide();
252
253 {
254#if wxRICHTEXT_BUFFERED_PAINTING
255 wxBufferedPaintDC dc(this, m_bufferBitmap);
256#else
257 wxPaintDC dc(this);
258#endif
259
260 if (IsFrozen())
261 return;
262
263 PrepareDC(dc);
264
265 dc.SetFont(GetFont());
266
267 // Paint the background
268 PaintBackground(dc);
269
270 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
271
272 wxRect drawingArea(GetUpdateRegion().GetBox());
273 drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition()));
274
275 wxRect availableSpace(GetClientSize());
276 if (GetBuffer().GetDirty())
277 {
278 GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
279 GetBuffer().SetDirty(false);
280 SetupScrollbars();
281 }
282
283 GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
284 }
285
286 if (GetCaret() && !GetCaret()->IsVisible())
287 GetCaret()->Show();
288
289 PositionCaret();
290}
291
292// Empty implementation, to prevent flicker
293void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
294{
295}
296
297void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
298{
299 if (GetCaret())
300 {
301 if (!GetCaret()->IsVisible())
302 GetCaret()->Show();
303 PositionCaret();
304 }
305
306 // if (!IsFrozen())
307 // Refresh(false);
308}
309
310void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
311{
312 if (GetCaret() && GetCaret()->IsVisible())
313 GetCaret()->Hide();
314
315 // if (!IsFrozen())
316 // Refresh(false);
317}
318
319void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
320{
321 m_dragging = false;
322}
323
324/// Left-click
325void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
326{
327 SetFocus();
328
329 wxClientDC dc(this);
330 PrepareDC(dc);
331 dc.SetFont(GetFont());
332
333 long position = 0;
334 int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position);
335
336 if (hit != wxRICHTEXT_HITTEST_NONE)
337 {
338 m_dragStart = event.GetLogicalPosition(dc);
339 m_dragging = true;
340 CaptureMouse();
341
342 SelectNone();
343
344 bool caretAtLineStart = false;
345
346 if (hit & wxRICHTEXT_HITTEST_BEFORE)
347 {
348 // If we're at the start of a line (but not first in para)
349 // then we should keep the caret showing at the start of the line
350 // by showing the m_caretAtLineStart flag.
351 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
352 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
353
354 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
355 caretAtLineStart = true;
356 position --;
357 }
358
359 MoveCaret(position, caretAtLineStart);
360 SetDefaultStyleToCursorStyle();
361 }
362
363 event.Skip();
364}
365
366/// Left-up
367void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event)
368{
369 if (m_dragging)
370 {
371 m_dragging = false;
372 if (GetCapture() == this)
373 ReleaseMouse();
374
375 // See if we clicked on a URL
376 wxClientDC dc(this);
377 PrepareDC(dc);
378 dc.SetFont(GetFont());
379
380 long position = 0;
381 wxPoint logicalPt = event.GetLogicalPosition(dc);
382 int hit = GetBuffer().HitTest(dc, logicalPt, position);
383
384 if (hit != wxRICHTEXT_HITTEST_NONE)
385 {
386 wxRichTextEvent cmdEvent(
387 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK,
388 GetId());
389 cmdEvent.SetEventObject(this);
390 cmdEvent.SetPosition(m_caretPosition+1);
391
392 if (!GetEventHandler()->ProcessEvent(cmdEvent))
393 {
394 wxTextAttr attr;
395 if (GetStyle(position, attr))
396 {
397 if (attr.HasFlag(wxTEXT_ATTR_URL))
398 {
399 wxString urlTarget = attr.GetURL();
400 if (!urlTarget.IsEmpty())
401 {
402 wxMouseEvent mouseEvent(event);
403
404 long startPos = 0, endPos = 0;
405 wxRichTextObject* obj = GetBuffer().GetLeafObjectAtPosition(position);
406 if (obj)
407 {
408 startPos = obj->GetRange().GetStart();
409 endPos = obj->GetRange().GetEnd();
410 }
411
412 wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos);
413 InitCommandEvent(urlEvent);
414
415 urlEvent.SetString(urlTarget);
416
417 GetEventHandler()->ProcessEvent(urlEvent);
418 }
419 }
420 }
421 }
422 }
423 }
424}
425
426/// Left-click
427void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
428{
429 wxClientDC dc(this);
430 PrepareDC(dc);
431 dc.SetFont(GetFont());
432
433 long position = 0;
434 wxPoint logicalPt = event.GetLogicalPosition(dc);
435 int hit = GetBuffer().HitTest(dc, logicalPt, position);
436
437 // See if we need to change the cursor
438
439 {
440 if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
441 {
442 wxTextAttr attr;
443 if (GetStyle(position, attr))
444 {
445 if (attr.HasFlag(wxTEXT_ATTR_URL))
446 {
447 SetCursor(m_urlCursor);
448 }
449 else if (!attr.HasFlag(wxTEXT_ATTR_URL))
450 {
451 SetCursor(m_textCursor);
452 }
453 }
454 }
455 else
456 SetCursor(m_textCursor);
457 }
458
459 if (!event.Dragging())
460 {
461 event.Skip();
462 return;
463 }
464
465 if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE)
466 {
467 // TODO: test closeness
468
469 bool caretAtLineStart = false;
470
471 if (hit & wxRICHTEXT_HITTEST_BEFORE)
472 {
473 // If we're at the start of a line (but not first in para)
474 // then we should keep the caret showing at the start of the line
475 // by showing the m_caretAtLineStart flag.
476 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
477 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
478
479 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
480 caretAtLineStart = true;
481 position --;
482 }
483
484 if (m_caretPosition != position)
485 {
486 bool extendSel = ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
487
488 MoveCaret(position, caretAtLineStart);
489 SetDefaultStyleToCursorStyle();
490
491 if (extendSel)
492 Refresh(false);
493 }
494 }
495}
496
497/// Right-click
498void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
499{
500 SetFocus();
501
502 wxRichTextEvent cmdEvent(
503 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK,
504 GetId());
505 cmdEvent.SetEventObject(this);
506 cmdEvent.SetPosition(m_caretPosition+1);
507
508 if (!GetEventHandler()->ProcessEvent(cmdEvent))
509 event.Skip();
510}
511
512/// Left-double-click
513void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event))
514{
515 wxRichTextEvent cmdEvent(
516 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK,
517 GetId());
518 cmdEvent.SetEventObject(this);
519 cmdEvent.SetPosition(m_caretPosition+1);
520
521 if (!GetEventHandler()->ProcessEvent(cmdEvent))
522 {
523 SelectWord(GetCaretPosition()+1);
524 }
525}
526
527/// Middle-click
528void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
529{
530 wxRichTextEvent cmdEvent(
531 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK,
532 GetId());
533 cmdEvent.SetEventObject(this);
534 cmdEvent.SetPosition(m_caretPosition+1);
535
536 if (!GetEventHandler()->ProcessEvent(cmdEvent))
537 event.Skip();
538}
539
540/// Key press
541void wxRichTextCtrl::OnChar(wxKeyEvent& event)
542{
543 int flags = 0;
544 if (event.CmdDown())
545 flags |= wxRICHTEXT_CTRL_DOWN;
546 if (event.ShiftDown())
547 flags |= wxRICHTEXT_SHIFT_DOWN;
548 if (event.AltDown())
549 flags |= wxRICHTEXT_ALT_DOWN;
550
551 if (event.GetKeyCode() == WXK_LEFT ||
552 event.GetKeyCode() == WXK_RIGHT ||
553 event.GetKeyCode() == WXK_UP ||
554 event.GetKeyCode() == WXK_DOWN ||
555 event.GetKeyCode() == WXK_HOME ||
556 event.GetKeyCode() == WXK_PAGEUP ||
557 event.GetKeyCode() == WXK_PAGEDOWN ||
558 event.GetKeyCode() == WXK_END ||
559
560 event.GetKeyCode() == WXK_NUMPAD_LEFT ||
561 event.GetKeyCode() == WXK_NUMPAD_RIGHT ||
562 event.GetKeyCode() == WXK_NUMPAD_UP ||
563 event.GetKeyCode() == WXK_NUMPAD_DOWN ||
564 event.GetKeyCode() == WXK_NUMPAD_HOME ||
565 event.GetKeyCode() == WXK_NUMPAD_PAGEUP ||
566 event.GetKeyCode() == WXK_NUMPAD_PAGEDOWN ||
567 event.GetKeyCode() == WXK_NUMPAD_END)
568 {
569 KeyboardNavigate(event.GetKeyCode(), flags);
570 return;
571 }
572
573 // all the other keys modify the controls contents which shouldn't be
574 // possible if we're read-only
575 if ( !IsEditable() )
576 {
577 event.Skip();
578 return;
579 }
580
581 if (event.GetKeyCode() == WXK_RETURN)
582 {
583 BeginBatchUndo(_("Insert Text"));
584
585 long newPos = m_caretPosition;
586
587 DeleteSelectedContent(& newPos);
588
589 if (event.ShiftDown())
590 {
591 wxString text;
592 text = wxRichTextLineBreakChar;
593 GetBuffer().InsertTextWithUndo(newPos+1, text, this);
594 }
595 else
596 GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
597
598 EndBatchUndo();
599 SetDefaultStyleToCursorStyle();
600
601 ScrollIntoView(m_caretPosition, WXK_RIGHT);
602
603 wxRichTextEvent cmdEvent(
604 wxEVT_COMMAND_RICHTEXT_RETURN,
605 GetId());
606 cmdEvent.SetEventObject(this);
607 cmdEvent.SetFlags(flags);
608 cmdEvent.SetPosition(newPos+1);
609
610 if (!GetEventHandler()->ProcessEvent(cmdEvent))
611 {
612 // Generate conventional event
613 wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId());
614 InitCommandEvent(textEvent);
615
616 GetEventHandler()->ProcessEvent(textEvent);
617 }
618 Update();
619 }
620 else if (event.GetKeyCode() == WXK_BACK)
621 {
622 BeginBatchUndo(_("Delete Text"));
623
624 // Submit range in character positions, which are greater than caret positions,
625 // so subtract 1 for deleted character and add 1 for conversion to character position.
626 if (m_caretPosition > -1 && !HasSelection())
627 {
628 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition), this);
629 }
630 else
631 DeleteSelectedContent();
632
633 EndBatchUndo();
634
635 if (GetLastPosition() == -1)
636 {
637 GetBuffer().Reset();
638
639 m_caretPosition = -1;
640 PositionCaret();
641 SetDefaultStyleToCursorStyle();
642 }
643
644 ScrollIntoView(m_caretPosition, WXK_LEFT);
645
646 wxRichTextEvent cmdEvent(
647 wxEVT_COMMAND_RICHTEXT_DELETE,
648 GetId());
649 cmdEvent.SetEventObject(this);
650 cmdEvent.SetFlags(flags);
651 cmdEvent.SetPosition(m_caretPosition+1);
652 GetEventHandler()->ProcessEvent(cmdEvent);
653
654 Update();
655 }
656 else if (event.GetKeyCode() == WXK_DELETE)
657 {
658 BeginBatchUndo(_("Delete Text"));
659
660 // Submit range in character positions, which are greater than caret positions,
661 if (m_caretPosition < GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
662 {
663 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition+1, m_caretPosition+1), this);
664 }
665 else
666 DeleteSelectedContent();
667
668 EndBatchUndo();
669
670 if (GetLastPosition() == -1)
671 {
672 GetBuffer().Reset();
673
674 m_caretPosition = -1;
675 PositionCaret();
676 SetDefaultStyleToCursorStyle();
677 }
678
679 wxRichTextEvent cmdEvent(
680 wxEVT_COMMAND_RICHTEXT_DELETE,
681 GetId());
682 cmdEvent.SetEventObject(this);
683 cmdEvent.SetFlags(flags);
684 cmdEvent.SetPosition(m_caretPosition+1);
685 GetEventHandler()->ProcessEvent(cmdEvent);
686
687 Update();
688 }
689 else
690 {
691 long keycode = event.GetKeyCode();
692 switch ( keycode )
693 {
694 case WXK_ESCAPE:
695 case WXK_DELETE:
696 case WXK_START:
697 case WXK_LBUTTON:
698 case WXK_RBUTTON:
699 case WXK_CANCEL:
700 case WXK_MBUTTON:
701 case WXK_CLEAR:
702 case WXK_SHIFT:
703 case WXK_ALT:
704 case WXK_CONTROL:
705 case WXK_MENU:
706 case WXK_PAUSE:
707 case WXK_CAPITAL:
708 case WXK_END:
709 case WXK_HOME:
710 case WXK_LEFT:
711 case WXK_UP:
712 case WXK_RIGHT:
713 case WXK_DOWN:
714 case WXK_SELECT:
715 case WXK_PRINT:
716 case WXK_EXECUTE:
717 case WXK_SNAPSHOT:
718 case WXK_INSERT:
719 case WXK_HELP:
720 case WXK_NUMPAD0:
721 case WXK_NUMPAD1:
722 case WXK_NUMPAD2:
723 case WXK_NUMPAD3:
724 case WXK_NUMPAD4:
725 case WXK_NUMPAD5:
726 case WXK_NUMPAD6:
727 case WXK_NUMPAD7:
728 case WXK_NUMPAD8:
729 case WXK_NUMPAD9:
730 case WXK_MULTIPLY:
731 case WXK_ADD:
732 case WXK_SEPARATOR:
733 case WXK_SUBTRACT:
734 case WXK_DECIMAL:
735 case WXK_DIVIDE:
736 case WXK_F1:
737 case WXK_F2:
738 case WXK_F3:
739 case WXK_F4:
740 case WXK_F5:
741 case WXK_F6:
742 case WXK_F7:
743 case WXK_F8:
744 case WXK_F9:
745 case WXK_F10:
746 case WXK_F11:
747 case WXK_F12:
748 case WXK_F13:
749 case WXK_F14:
750 case WXK_F15:
751 case WXK_F16:
752 case WXK_F17:
753 case WXK_F18:
754 case WXK_F19:
755 case WXK_F20:
756 case WXK_F21:
757 case WXK_F22:
758 case WXK_F23:
759 case WXK_F24:
760 case WXK_NUMLOCK:
761 case WXK_SCROLL:
762 case WXK_PAGEUP:
763 case WXK_PAGEDOWN:
764 case WXK_NUMPAD_SPACE:
765 case WXK_NUMPAD_TAB:
766 case WXK_NUMPAD_ENTER:
767 case WXK_NUMPAD_F1:
768 case WXK_NUMPAD_F2:
769 case WXK_NUMPAD_F3:
770 case WXK_NUMPAD_F4:
771 case WXK_NUMPAD_HOME:
772 case WXK_NUMPAD_LEFT:
773 case WXK_NUMPAD_UP:
774 case WXK_NUMPAD_RIGHT:
775 case WXK_NUMPAD_DOWN:
776 case WXK_NUMPAD_PAGEUP:
777 case WXK_NUMPAD_PAGEDOWN:
778 case WXK_NUMPAD_END:
779 case WXK_NUMPAD_BEGIN:
780 case WXK_NUMPAD_INSERT:
781 case WXK_NUMPAD_DELETE:
782 case WXK_NUMPAD_EQUAL:
783 case WXK_NUMPAD_MULTIPLY:
784 case WXK_NUMPAD_ADD:
785 case WXK_NUMPAD_SEPARATOR:
786 case WXK_NUMPAD_SUBTRACT:
787 case WXK_NUMPAD_DECIMAL:
788 case WXK_WINDOWS_LEFT:
789 {
790 event.Skip();
791 return;
792 }
793
794 default:
795 {
796 if (event.CmdDown() || event.AltDown())
797 {
798 event.Skip();
799 return;
800 }
801
802 wxRichTextEvent cmdEvent(
803 wxEVT_COMMAND_RICHTEXT_CHARACTER,
804 GetId());
805 cmdEvent.SetEventObject(this);
806 cmdEvent.SetFlags(flags);
807#if wxUSE_UNICODE
808 cmdEvent.SetCharacter(event.GetUnicodeKey());
809#else
810 cmdEvent.SetCharacter((wxChar) keycode);
811#endif
812 cmdEvent.SetPosition(m_caretPosition+1);
813
814 if (keycode == wxT('\t'))
815 {
816 // See if we need to promote or demote the selection or paragraph at the cursor
817 // position, instead of inserting a tab.
818 long pos = GetAdjustedCaretPosition(GetCaretPosition());
819 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos);
820 if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName())
821 {
822 wxRichTextRange range;
823 if (HasSelection())
824 range = GetSelectionRange();
825 else
826 range = para->GetRange().FromInternal();
827
828 int promoteBy = event.ShiftDown() ? 1 : -1;
829
830 PromoteList(promoteBy, range, NULL);
831
832 GetEventHandler()->ProcessEvent(cmdEvent);
833
834 return;
835 }
836 }
837
838 BeginBatchUndo(_("Insert Text"));
839
840 long newPos = m_caretPosition;
841 DeleteSelectedContent(& newPos);
842
843#if wxUSE_UNICODE
844 wxString str = event.GetUnicodeKey();
845#else
846 wxString str = (wxChar) event.GetKeyCode();
847#endif
848 GetBuffer().InsertTextWithUndo(newPos+1, str, this, 0);
849
850 EndBatchUndo();
851
852 SetDefaultStyleToCursorStyle();
853 ScrollIntoView(m_caretPosition, WXK_RIGHT);
854
855 GetEventHandler()->ProcessEvent(cmdEvent);
856
857 Update();
858 }
859 }
860 }
861}
862
863/// Delete content if there is a selection, e.g. when pressing a key.
864bool wxRichTextCtrl::DeleteSelectedContent(long* newPos)
865{
866 if (HasSelection())
867 {
868 long pos = m_selectionRange.GetStart();
869 GetBuffer().DeleteRangeWithUndo(m_selectionRange, this);
870 m_selectionRange.SetRange(-2, -2);
871
872 if (newPos)
873 *newPos = pos-1;
874 return true;
875 }
876 else
877 return false;
878}
879
880/// Keyboard navigation
881
882/*
883
884Left: left one character
885Right: right one character
886Up: up one line
887Down: down one line
888Ctrl-Left: left one word
889Ctrl-Right: right one word
890Ctrl-Up: previous paragraph start
891Ctrl-Down: next start of paragraph
892Home: start of line
893End: end of line
894Ctrl-Home: start of document
895Ctrl-End: end of document
896Page-Up: Up a screen
897Page-Down: Down a screen
898
899Maybe:
900
901Ctrl-Alt-PgUp: Start of window
902Ctrl-Alt-PgDn: End of window
903F8: Start selection mode
904Esc: End selection mode
905
906Adding Shift does the above but starts/extends selection.
907
908
909 */
910
911bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags)
912{
913 bool success = false;
914
915 if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT)
916 {
917 if (flags & wxRICHTEXT_CTRL_DOWN)
918 success = WordRight(1, flags);
919 else
920 success = MoveRight(1, flags);
921 }
922 else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT)
923 {
924 if (flags & wxRICHTEXT_CTRL_DOWN)
925 success = WordLeft(1, flags);
926 else
927 success = MoveLeft(1, flags);
928 }
929 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP)
930 {
931 if (flags & wxRICHTEXT_CTRL_DOWN)
932 success = MoveToParagraphStart(flags);
933 else
934 success = MoveUp(1, flags);
935 }
936 else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN)
937 {
938 if (flags & wxRICHTEXT_CTRL_DOWN)
939 success = MoveToParagraphEnd(flags);
940 else
941 success = MoveDown(1, flags);
942 }
943 else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP)
944 {
945 success = PageUp(1, flags);
946 }
947 else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
948 {
949 success = PageDown(1, flags);
950 }
951 else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME)
952 {
953 if (flags & wxRICHTEXT_CTRL_DOWN)
954 success = MoveHome(flags);
955 else
956 success = MoveToLineStart(flags);
957 }
958 else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END)
959 {
960 if (flags & wxRICHTEXT_CTRL_DOWN)
961 success = MoveEnd(flags);
962 else
963 success = MoveToLineEnd(flags);
964 }
965
966 if (success)
967 {
968 ScrollIntoView(m_caretPosition, keyCode);
969 SetDefaultStyleToCursorStyle();
970 }
971
972 return success;
973}
974
975/// Extend the selection. Selections are in caret positions.
976bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
977{
978 if (flags & wxRICHTEXT_SHIFT_DOWN)
979 {
980 // If not currently selecting, start selecting
981 if (m_selectionRange.GetStart() == -2)
982 {
983 m_selectionAnchor = oldPos;
984
985 if (oldPos > newPos)
986 m_selectionRange.SetRange(newPos+1, oldPos);
987 else
988 m_selectionRange.SetRange(oldPos+1, newPos);
989 }
990 else
991 {
992 // Always ensure that the selection range start is greater than
993 // the end.
994 if (newPos > m_selectionAnchor)
995 m_selectionRange.SetRange(m_selectionAnchor+1, newPos);
996 else
997 m_selectionRange.SetRange(newPos+1, m_selectionAnchor);
998 }
999
1000 if (m_selectionRange.GetStart() > m_selectionRange.GetEnd())
1001 {
1002 wxLogDebug(wxT("Strange selection range"));
1003 }
1004
1005 return true;
1006 }
1007 else
1008 return false;
1009}
1010
1011/// Scroll into view, returning true if we scrolled.
1012/// This takes a _caret_ position.
1013bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
1014{
1015 wxRichTextLine* line = GetVisibleLineForCaretPosition(position);
1016
1017 if (!line)
1018 return false;
1019
1020 int ppuX, ppuY;
1021 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1022
1023 int startXUnits, startYUnits;
1024 GetViewStart(& startXUnits, & startYUnits);
1025 int startY = startYUnits * ppuY;
1026
1027 int sx = 0, sy = 0;
1028 GetVirtualSize(& sx, & sy);
1029 int sxUnits = 0;
1030 int syUnits = 0;
1031 if (ppuY != 0)
1032 syUnits = sy/ppuY;
1033
1034 wxRect rect = line->GetRect();
1035
1036 bool scrolled = false;
1037
1038 wxSize clientSize = GetClientSize();
1039
1040 // Going down
1041 if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN ||
1042 keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT ||
1043 keyCode == WXK_END || keyCode == WXK_NUMPAD_END ||
1044 keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
1045 {
1046 if ((rect.y + rect.height) > (clientSize.y + startY))
1047 {
1048 // Make it scroll so this item is at the bottom
1049 // of the window
1050 int y = rect.y - (clientSize.y - rect.height);
1051 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1052
1053 // If we're still off the screen, scroll another line down
1054 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1055 yUnits ++;
1056
1057 if (startYUnits != yUnits)
1058 {
1059 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1060 scrolled = true;
1061 }
1062 }
1063 else if (rect.y < startY)
1064 {
1065 // Make it scroll so this item is at the top
1066 // of the window
1067 int y = rect.y ;
1068 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1069
1070 if (startYUnits != yUnits)
1071 {
1072 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1073 scrolled = true;
1074 }
1075 }
1076 }
1077 // Going up
1078 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP ||
1079 keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT ||
1080 keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME ||
1081 keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP )
1082 {
1083 if (rect.y < startY)
1084 {
1085 // Make it scroll so this item is at the top
1086 // of the window
1087 int y = rect.y ;
1088 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1089
1090 if (startYUnits != yUnits)
1091 {
1092 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1093 scrolled = true;
1094 }
1095 }
1096 else if ((rect.y + rect.height) > (clientSize.y + startY))
1097 {
1098 // Make it scroll so this item is at the bottom
1099 // of the window
1100 int y = rect.y - (clientSize.y - rect.height);
1101 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1102
1103 // If we're still off the screen, scroll another line down
1104 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1105 yUnits ++;
1106
1107 if (startYUnits != yUnits)
1108 {
1109 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1110 scrolled = true;
1111 }
1112 }
1113 }
1114 PositionCaret();
1115
1116 return scrolled;
1117}
1118
1119/// Is the given position visible on the screen?
1120bool wxRichTextCtrl::IsPositionVisible(long pos) const
1121{
1122 wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1);
1123
1124 if (!line)
1125 return false;
1126
1127 int ppuX, ppuY;
1128 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1129
1130 int startX, startY;
1131 GetViewStart(& startX, & startY);
1132 startX = 0;
1133 startY = startY * ppuY;
1134
1135 wxRect rect = line->GetRect();
1136 wxSize clientSize = GetClientSize();
1137
1138 return (rect.GetBottom() > startY) && (rect.GetTop() < (startY + clientSize.y));
1139}
1140
1141void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart)
1142{
1143 m_caretPosition = position;
1144 m_caretAtLineStart = showAtLineStart;
1145}
1146
1147/// Move caret one visual step forward: this may mean setting a flag
1148/// and keeping the same position if we're going from the end of one line
1149/// to the start of the next, which may be the exact same caret position.
1150void wxRichTextCtrl::MoveCaretForward(long oldPosition)
1151{
1152 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1153
1154 // Only do the check if we're not at the end of the paragraph (where things work OK
1155 // anyway)
1156 if (para && (oldPosition != para->GetRange().GetEnd() - 1))
1157 {
1158 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1159
1160 if (line)
1161 {
1162 wxRichTextRange lineRange = line->GetAbsoluteRange();
1163
1164 // We're at the end of a line. See whether we need to
1165 // stay at the same actual caret position but change visual
1166 // position, or not.
1167 if (oldPosition == lineRange.GetEnd())
1168 {
1169 if (m_caretAtLineStart)
1170 {
1171 // We're already at the start of the line, so actually move on now.
1172 m_caretPosition = oldPosition + 1;
1173 m_caretAtLineStart = false;
1174 }
1175 else
1176 {
1177 // We're showing at the end of the line, so keep to
1178 // the same position but indicate that we're to show
1179 // at the start of the next line.
1180 m_caretPosition = oldPosition;
1181 m_caretAtLineStart = true;
1182 }
1183 SetDefaultStyleToCursorStyle();
1184 return;
1185 }
1186 }
1187 }
1188 m_caretPosition ++;
1189 SetDefaultStyleToCursorStyle();
1190}
1191
1192/// Move caret one visual step backward: this may mean setting a flag
1193/// and keeping the same position if we're going from the end of one line
1194/// to the start of the next, which may be the exact same caret position.
1195void wxRichTextCtrl::MoveCaretBack(long oldPosition)
1196{
1197 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1198
1199 // Only do the check if we're not at the start of the paragraph (where things work OK
1200 // anyway)
1201 if (para && (oldPosition != para->GetRange().GetStart()))
1202 {
1203 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1204
1205 if (line)
1206 {
1207 wxRichTextRange lineRange = line->GetAbsoluteRange();
1208
1209 // We're at the start of a line. See whether we need to
1210 // stay at the same actual caret position but change visual
1211 // position, or not.
1212 if (oldPosition == lineRange.GetStart())
1213 {
1214 m_caretPosition = oldPosition-1;
1215 m_caretAtLineStart = true;
1216 return;
1217 }
1218 else if (oldPosition == lineRange.GetEnd())
1219 {
1220 if (m_caretAtLineStart)
1221 {
1222 // We're at the start of the line, so keep the same caret position
1223 // but clear the start-of-line flag.
1224 m_caretPosition = oldPosition;
1225 m_caretAtLineStart = false;
1226 }
1227 else
1228 {
1229 // We're showing at the end of the line, so go back
1230 // to the previous character position.
1231 m_caretPosition = oldPosition - 1;
1232 }
1233 SetDefaultStyleToCursorStyle();
1234 return;
1235 }
1236 }
1237 }
1238 m_caretPosition --;
1239 SetDefaultStyleToCursorStyle();
1240}
1241
1242/// Move right
1243bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
1244{
1245 long endPos = GetBuffer().GetRange().GetEnd();
1246
1247 if (m_caretPosition + noPositions < endPos)
1248 {
1249 long oldPos = m_caretPosition;
1250 long newPos = m_caretPosition + noPositions;
1251
1252 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1253 if (!extendSel)
1254 SelectNone();
1255
1256 // Determine by looking at oldPos and m_caretPosition whether
1257 // we moved from the end of a line to the start of the next line, in which case
1258 // we want to adjust the caret position such that it is positioned at the
1259 // start of the next line, rather than jumping past the first character of the
1260 // line.
1261 if (noPositions == 1 && !extendSel)
1262 MoveCaretForward(oldPos);
1263 else
1264 SetCaretPosition(newPos);
1265
1266 PositionCaret();
1267 SetDefaultStyleToCursorStyle();
1268
1269 if (extendSel)
1270 Refresh(false);
1271 return true;
1272 }
1273 else
1274 return false;
1275}
1276
1277/// Move left
1278bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
1279{
1280 long startPos = -1;
1281
1282 if (m_caretPosition > startPos - noPositions + 1)
1283 {
1284 long oldPos = m_caretPosition;
1285 long newPos = m_caretPosition - noPositions;
1286 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1287 if (!extendSel)
1288 SelectNone();
1289
1290 if (noPositions == 1 && !extendSel)
1291 MoveCaretBack(oldPos);
1292 else
1293 SetCaretPosition(newPos);
1294
1295 PositionCaret();
1296 SetDefaultStyleToCursorStyle();
1297
1298 if (extendSel)
1299 Refresh(false);
1300 return true;
1301 }
1302 else
1303 return false;
1304}
1305
1306/// Move up
1307bool wxRichTextCtrl::MoveUp(int noLines, int flags)
1308{
1309 return MoveDown(- noLines, flags);
1310}
1311
1312/// Move up
1313bool wxRichTextCtrl::MoveDown(int noLines, int flags)
1314{
1315 if (!GetCaret())
1316 return false;
1317
1318 long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
1319 wxPoint pt = GetCaret()->GetPosition();
1320 long newLine = lineNumber + noLines;
1321
1322 if (lineNumber != -1)
1323 {
1324 if (noLines > 0)
1325 {
1326 long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1327
1328 if (newLine > lastLine)
1329 return false;
1330 }
1331 else
1332 {
1333 if (newLine < 0)
1334 return false;
1335 }
1336 }
1337
1338 wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine);
1339 if (lineObj)
1340 {
1341 pt.y = lineObj->GetAbsolutePosition().y + 2;
1342 }
1343 else
1344 return false;
1345
1346 long newPos = 0;
1347 wxClientDC dc(this);
1348 PrepareDC(dc);
1349 dc.SetFont(GetFont());
1350
1351 int hitTest = GetBuffer().HitTest(dc, pt, newPos);
1352
1353 if (hitTest != wxRICHTEXT_HITTEST_NONE)
1354 {
1355 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1356 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1357 // so we view the caret at the start of the line.
1358 bool caretLineStart = false;
1359 if (hitTest & wxRICHTEXT_HITTEST_BEFORE)
1360 {
1361 wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1);
1362 wxRichTextRange lineRange;
1363 if (thisLine)
1364 lineRange = thisLine->GetAbsoluteRange();
1365
1366 if (thisLine && (newPos-1) == lineRange.GetEnd())
1367 {
1368 newPos --;
1369 caretLineStart = true;
1370 }
1371 else
1372 {
1373 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos);
1374 if (para && para->GetRange().GetStart() == newPos)
1375 newPos --;
1376 }
1377 }
1378
1379 long newSelEnd = newPos;
1380
1381 bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
1382 if (!extendSel)
1383 SelectNone();
1384
1385 SetCaretPosition(newPos, caretLineStart);
1386 PositionCaret();
1387 SetDefaultStyleToCursorStyle();
1388
1389 if (extendSel)
1390 Refresh(false);
1391 return true;
1392 }
1393
1394 return false;
1395}
1396
1397/// Move to the end of the paragraph
1398bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1399{
1400 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1401 if (para)
1402 {
1403 long newPos = para->GetRange().GetEnd() - 1;
1404 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1405 if (!extendSel)
1406 SelectNone();
1407
1408 SetCaretPosition(newPos);
1409 PositionCaret();
1410 SetDefaultStyleToCursorStyle();
1411
1412 if (extendSel)
1413 Refresh(false);
1414 return true;
1415 }
1416
1417 return false;
1418}
1419
1420/// Move to the start of the paragraph
1421bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1422{
1423 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1424 if (para)
1425 {
1426 long newPos = para->GetRange().GetStart() - 1;
1427 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1428 if (!extendSel)
1429 SelectNone();
1430
1431 SetCaretPosition(newPos);
1432 PositionCaret();
1433 SetDefaultStyleToCursorStyle();
1434
1435 if (extendSel)
1436 Refresh(false);
1437 return true;
1438 }
1439
1440 return false;
1441}
1442
1443/// Move to the end of the line
1444bool wxRichTextCtrl::MoveToLineEnd(int flags)
1445{
1446 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1447
1448 if (line)
1449 {
1450 wxRichTextRange lineRange = line->GetAbsoluteRange();
1451 long newPos = lineRange.GetEnd();
1452 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1453 if (!extendSel)
1454 SelectNone();
1455
1456 SetCaretPosition(newPos);
1457 PositionCaret();
1458 SetDefaultStyleToCursorStyle();
1459
1460 if (extendSel)
1461 Refresh(false);
1462 return true;
1463 }
1464
1465 return false;
1466}
1467
1468/// Move to the start of the line
1469bool wxRichTextCtrl::MoveToLineStart(int flags)
1470{
1471 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1472 if (line)
1473 {
1474 wxRichTextRange lineRange = line->GetAbsoluteRange();
1475 long newPos = lineRange.GetStart()-1;
1476
1477 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1478 if (!extendSel)
1479 SelectNone();
1480
1481 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
1482
1483 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
1484 PositionCaret();
1485 SetDefaultStyleToCursorStyle();
1486
1487 if (extendSel)
1488 Refresh(false);
1489 return true;
1490 }
1491
1492 return false;
1493}
1494
1495/// Move to the start of the buffer
1496bool wxRichTextCtrl::MoveHome(int flags)
1497{
1498 if (m_caretPosition != -1)
1499 {
1500 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
1501 if (!extendSel)
1502 SelectNone();
1503
1504 SetCaretPosition(-1);
1505 PositionCaret();
1506 SetDefaultStyleToCursorStyle();
1507
1508 if (extendSel)
1509 Refresh(false);
1510 return true;
1511 }
1512 else
1513 return false;
1514}
1515
1516/// Move to the end of the buffer
1517bool wxRichTextCtrl::MoveEnd(int flags)
1518{
1519 long endPos = GetBuffer().GetRange().GetEnd()-1;
1520
1521 if (m_caretPosition != endPos)
1522 {
1523 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
1524 if (!extendSel)
1525 SelectNone();
1526
1527 SetCaretPosition(endPos);
1528 PositionCaret();
1529 SetDefaultStyleToCursorStyle();
1530
1531 if (extendSel)
1532 Refresh(false);
1533 return true;
1534 }
1535 else
1536 return false;
1537}
1538
1539/// Move noPages pages up
1540bool wxRichTextCtrl::PageUp(int noPages, int flags)
1541{
1542 return PageDown(- noPages, flags);
1543}
1544
1545/// Move noPages pages down
1546bool wxRichTextCtrl::PageDown(int noPages, int flags)
1547{
1548 // Calculate which line occurs noPages * screen height further down.
1549 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1550 if (line)
1551 {
1552 wxSize clientSize = GetClientSize();
1553 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
1554
1555 wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY);
1556 if (newLine)
1557 {
1558 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
1559 long pos = lineRange.GetStart()-1;
1560 if (pos != m_caretPosition)
1561 {
1562 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
1563
1564 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1565 if (!extendSel)
1566 SelectNone();
1567
1568 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
1569 PositionCaret();
1570 SetDefaultStyleToCursorStyle();
1571
1572 if (extendSel)
1573 Refresh(false);
1574 return true;
1575 }
1576 }
1577 }
1578
1579 return false;
1580}
1581
1582static bool wxRichTextCtrlIsWhitespace(const wxString& str)
1583{
1584 return str == wxT(" ") || str == wxT("\t");
1585}
1586
1587// Finds the caret position for the next word
1588long wxRichTextCtrl::FindNextWordPosition(int direction) const
1589{
1590 long endPos = GetBuffer().GetRange().GetEnd();
1591
1592 if (direction > 0)
1593 {
1594 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
1595
1596 // First skip current text to space
1597 while (i < endPos && i > -1)
1598 {
1599 // i is in character, not caret positions
1600 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1601 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1602 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1603 {
1604 break;
1605 }
1606 else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty())
1607 i += direction;
1608 else
1609 {
1610 break;
1611 }
1612 }
1613 while (i < endPos && i > -1)
1614 {
1615 // i is in character, not caret positions
1616 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1617 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1618 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1619 return wxMax(-1, i);
1620
1621 if (text.empty()) // End of paragraph, or maybe an image
1622 return wxMax(-1, i - 1);
1623 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
1624 i += direction;
1625 else
1626 {
1627 // Convert to caret position
1628 return wxMax(-1, i - 1);
1629 }
1630 }
1631 if (i >= endPos)
1632 return endPos-1;
1633 return i-1;
1634 }
1635 else
1636 {
1637 long i = m_caretPosition;
1638
1639 // First skip white space
1640 while (i < endPos && i > -1)
1641 {
1642 // i is in character, not caret positions
1643 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1644 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1645
1646 if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1647 break;
1648 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
1649 i += direction;
1650 else
1651 break;
1652 }
1653 // Next skip current text to space
1654 while (i < endPos && i > -1)
1655 {
1656 // i is in character, not caret positions
1657 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1658 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1659 if (line && line->GetAbsoluteRange().GetStart() == i)
1660 return i-1;
1661
1662 if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */)
1663 i += direction;
1664 else
1665 {
1666 return i;
1667 }
1668 }
1669 if (i < -1)
1670 return -1;
1671 return i;
1672 }
1673}
1674
1675/// Move n words left
1676bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
1677{
1678 long pos = FindNextWordPosition(-1);
1679 if (pos != m_caretPosition)
1680 {
1681 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1682
1683 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1684 if (!extendSel)
1685 SelectNone();
1686
1687 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1688 PositionCaret();
1689 SetDefaultStyleToCursorStyle();
1690
1691 if (extendSel)
1692 Refresh(false);
1693 return true;
1694 }
1695
1696 return false;
1697}
1698
1699/// Move n words right
1700bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
1701{
1702 long pos = FindNextWordPosition(1);
1703 if (pos != m_caretPosition)
1704 {
1705 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1706
1707 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1708 if (!extendSel)
1709 SelectNone();
1710
1711 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1712 PositionCaret();
1713 SetDefaultStyleToCursorStyle();
1714
1715 if (extendSel)
1716 Refresh(false);
1717 return true;
1718 }
1719
1720 return false;
1721}
1722
1723/// Sizing
1724void wxRichTextCtrl::OnSize(wxSizeEvent& event)
1725{
1726 // Only do sizing optimization for large buffers
1727 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold)
1728 {
1729 m_fullLayoutRequired = true;
1730 m_fullLayoutTime = wxGetLocalTimeMillis();
1731 m_fullLayoutSavedPosition = GetFirstVisiblePosition();
1732 LayoutContent(true /* onlyVisibleRect */);
1733 }
1734 else
1735 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1736
1737#if wxRICHTEXT_BUFFERED_PAINTING
1738 RecreateBuffer();
1739#endif
1740
1741 event.Skip();
1742}
1743
1744
1745/// Idle-time processing
1746void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
1747{
1748 const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL;
1749
1750 if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval)))
1751 {
1752 m_fullLayoutRequired = false;
1753 m_fullLayoutTime = 0;
1754 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1755 ShowPosition(m_fullLayoutSavedPosition);
1756 Refresh(false);
1757 }
1758
1759 if (m_caretPositionForDefaultStyle != -2)
1760 {
1761 // If the caret position has changed, no longer reflect the default style
1762 // in the UI.
1763 if (GetCaretPosition() != m_caretPositionForDefaultStyle)
1764 m_caretPositionForDefaultStyle = -2;
1765 }
1766
1767 event.Skip();
1768}
1769
1770/// Scrolling
1771void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
1772{
1773 // Not used
1774 event.Skip();
1775}
1776
1777/// Set up scrollbars, e.g. after a resize
1778void wxRichTextCtrl::SetupScrollbars(bool atTop)
1779{
1780 if (IsFrozen())
1781 return;
1782
1783 if (GetBuffer().IsEmpty())
1784 {
1785 SetScrollbars(0, 0, 0, 0, 0, 0);
1786 return;
1787 }
1788
1789 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1790 // of pixels. See e.g. wxVScrolledWindow for ideas.
1791 int pixelsPerUnit = 5;
1792 wxSize clientSize = GetClientSize();
1793
1794 int maxHeight = GetBuffer().GetCachedSize().y;
1795
1796 // Round up so we have at least maxHeight pixels
1797 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
1798
1799 int startX = 0, startY = 0;
1800 if (!atTop)
1801 GetViewStart(& startX, & startY);
1802
1803 int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0);
1804 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
1805
1806 // Move to previous scroll position if
1807 // possible
1808 SetScrollbars(0, pixelsPerUnit,
1809 0, unitsY,
1810 wxMin(maxPositionX, startX), wxMin(maxPositionY, startY));
1811}
1812
1813/// Paint the background
1814void wxRichTextCtrl::PaintBackground(wxDC& dc)
1815{
1816 wxColour backgroundColour = GetBackgroundColour();
1817 if (!backgroundColour.Ok())
1818 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1819
1820 // Clear the background
1821 dc.SetBrush(wxBrush(backgroundColour));
1822 dc.SetPen(*wxTRANSPARENT_PEN);
1823 wxRect windowRect(GetClientSize());
1824 windowRect.x -= 2; windowRect.y -= 2;
1825 windowRect.width += 4; windowRect.height += 4;
1826
1827 // We need to shift the rectangle to take into account
1828 // scrolling. Converting device to logical coordinates.
1829 CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y);
1830 dc.DrawRectangle(windowRect);
1831}
1832
1833#if wxRICHTEXT_BUFFERED_PAINTING
1834/// Recreate buffer bitmap if necessary
1835bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
1836{
1837 wxSize sz = size;
1838 if (sz == wxDefaultSize)
1839 sz = GetClientSize();
1840
1841 if (sz.x < 1 || sz.y < 1)
1842 return false;
1843
1844 if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
1845 m_bufferBitmap = wxBitmap(sz.x, sz.y);
1846 return m_bufferBitmap.Ok();
1847}
1848#endif
1849
1850// ----------------------------------------------------------------------------
1851// file IO functions
1852// ----------------------------------------------------------------------------
1853
1854bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
1855{
1856 bool success = GetBuffer().LoadFile(filename, fileType);
1857 if (success)
1858 m_filename = filename;
1859
1860 DiscardEdits();
1861 SetInsertionPoint(0);
1862 LayoutContent();
1863 PositionCaret();
1864 SetupScrollbars(true);
1865 Refresh(false);
1866 wxTextCtrl::SendTextUpdatedEvent(this);
1867
1868 if (success)
1869 return true;
1870 else
1871 {
1872 wxLogError(_("File couldn't be loaded."));
1873
1874 return false;
1875 }
1876}
1877
1878bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
1879{
1880 if (GetBuffer().SaveFile(filename, fileType))
1881 {
1882 m_filename = filename;
1883
1884 DiscardEdits();
1885
1886 return true;
1887 }
1888
1889 wxLogError(_("The text couldn't be saved."));
1890
1891 return false;
1892}
1893
1894// ----------------------------------------------------------------------------
1895// wxRichTextCtrl specific functionality
1896// ----------------------------------------------------------------------------
1897
1898/// Add a new paragraph of text to the end of the buffer
1899wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
1900{
1901 return GetBuffer().AddParagraph(text);
1902}
1903
1904/// Add an image
1905wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
1906{
1907 return GetBuffer().AddImage(image);
1908}
1909
1910// ----------------------------------------------------------------------------
1911// selection and ranges
1912// ----------------------------------------------------------------------------
1913
1914void wxRichTextCtrl::SelectAll()
1915{
1916 SetSelection(0, GetLastPosition()+1);
1917 m_selectionAnchor = -1;
1918}
1919
1920/// Select none
1921void wxRichTextCtrl::SelectNone()
1922{
1923 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1924 SetSelection(-2, -2);
1925 m_selectionAnchor = -2;
1926}
1927
1928static bool wxIsWordDelimiter(const wxString& text)
1929{
1930 return !text.IsEmpty() && !wxIsalnum(text[0]);
1931}
1932
1933/// Select the word at the given character position
1934bool wxRichTextCtrl::SelectWord(long position)
1935{
1936 if (position < 0 || position > GetBuffer().GetRange().GetEnd())
1937 return false;
1938
1939 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
1940 if (!para)
1941 return false;
1942
1943 long positionStart = position;
1944 long positionEnd = position;
1945
1946 for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --)
1947 {
1948 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionStart, positionStart));
1949 if (wxIsWordDelimiter(text))
1950 {
1951 positionStart ++;
1952 break;
1953 }
1954 }
1955 if (positionStart < para->GetRange().GetStart())
1956 positionStart = para->GetRange().GetStart();
1957
1958 for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++)
1959 {
1960 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionEnd, positionEnd));
1961 if (wxIsWordDelimiter(text))
1962 {
1963 positionEnd --;
1964 break;
1965 }
1966 }
1967 if (positionEnd >= para->GetRange().GetEnd())
1968 positionEnd = para->GetRange().GetEnd();
1969
1970 SetSelection(positionStart, positionEnd+1);
1971
1972 if (positionStart >= 0)
1973 {
1974 MoveCaret(positionStart-1, true);
1975 SetDefaultStyleToCursorStyle();
1976 }
1977
1978 return true;
1979}
1980
1981wxString wxRichTextCtrl::GetStringSelection() const
1982{
1983 long from, to;
1984 GetSelection(&from, &to);
1985
1986 return GetRange(from, to);
1987}
1988
1989// ----------------------------------------------------------------------------
1990// hit testing
1991// ----------------------------------------------------------------------------
1992
1993wxTextCtrlHitTestResult
1994wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
1995{
1996 // implement in terms of the other overload as the native ports typically
1997 // can get the position and not (x, y) pair directly (although wxUniv
1998 // directly gets x and y -- and so overrides this method as well)
1999 long pos;
2000 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
2001
2002 if ( rc != wxTE_HT_UNKNOWN )
2003 {
2004 PositionToXY(pos, x, y);
2005 }
2006
2007 return rc;
2008}
2009
2010wxTextCtrlHitTestResult
2011wxRichTextCtrl::HitTest(const wxPoint& pt,
2012 long * pos) const
2013{
2014 wxClientDC dc((wxRichTextCtrl*) this);
2015 ((wxRichTextCtrl*)this)->PrepareDC(dc);
2016
2017 // Buffer uses logical position (relative to start of buffer)
2018 // so convert
2019 wxPoint pt2 = GetLogicalPoint(pt);
2020
2021 int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos);
2022
2023 if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2024 return wxTE_HT_BEFORE;
2025 else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2026 return wxTE_HT_BEYOND;
2027 else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER))
2028 return wxTE_HT_ON_TEXT;
2029
2030 return wxTE_HT_UNKNOWN;
2031}
2032
2033// ----------------------------------------------------------------------------
2034// set/get the controls text
2035// ----------------------------------------------------------------------------
2036
2037wxString wxRichTextCtrl::GetValue() const
2038{
2039 return GetBuffer().GetText();
2040}
2041
2042wxString wxRichTextCtrl::GetRange(long from, long to) const
2043{
2044 // Public API for range is different from internals
2045 return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1));
2046}
2047
2048void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
2049{
2050 Clear();
2051
2052 if (!value.IsEmpty())
2053 {
2054 // Remove empty paragraph
2055 GetBuffer().Clear();
2056 DoWriteText(value);
2057
2058 // for compatibility, don't move the cursor when doing SetValue()
2059 SetInsertionPoint(0);
2060 }
2061 else
2062 {
2063 // still send an event for consistency
2064 if (flags & SetValue_SendEvent)
2065 wxTextCtrl::SendTextUpdatedEvent(this);
2066 }
2067 DiscardEdits();
2068}
2069
2070void wxRichTextCtrl::WriteText(const wxString& value)
2071{
2072 DoWriteText(value);
2073}
2074
2075void wxRichTextCtrl::DoWriteText(const wxString& value, int flags)
2076{
2077 wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix);
2078
2079 GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
2080
2081 if ( flags & SetValue_SendEvent )
2082 wxTextCtrl::SendTextUpdatedEvent(this);
2083}
2084
2085void wxRichTextCtrl::AppendText(const wxString& text)
2086{
2087 SetInsertionPointEnd();
2088
2089 WriteText(text);
2090}
2091
2092/// Write an image at the current insertion point
2093bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType)
2094{
2095 wxRichTextImageBlock imageBlock;
2096
2097 wxImage image2 = image;
2098 if (imageBlock.MakeImageBlock(image2, bitmapType))
2099 return WriteImage(imageBlock);
2100
2101 return false;
2102}
2103
2104bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType)
2105{
2106 wxRichTextImageBlock imageBlock;
2107
2108 wxImage image;
2109 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
2110 return WriteImage(imageBlock);
2111
2112 return false;
2113}
2114
2115bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
2116{
2117 return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
2118}
2119
2120bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType)
2121{
2122 if (bitmap.Ok())
2123 {
2124 wxRichTextImageBlock imageBlock;
2125
2126 wxImage image = bitmap.ConvertToImage();
2127 if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType))
2128 return WriteImage(imageBlock);
2129 }
2130
2131 return false;
2132}
2133
2134/// Insert a newline (actually paragraph) at the current insertion point.
2135bool wxRichTextCtrl::Newline()
2136{
2137 return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this);
2138}
2139
2140/// Insert a line break at the current insertion point.
2141bool wxRichTextCtrl::LineBreak()
2142{
2143 wxString text;
2144 text = wxRichTextLineBreakChar;
2145 return GetBuffer().InsertTextWithUndo(m_caretPosition+1, text, this);
2146}
2147
2148// ----------------------------------------------------------------------------
2149// Clipboard operations
2150// ----------------------------------------------------------------------------
2151
2152void wxRichTextCtrl::Copy()
2153{
2154 if (CanCopy())
2155 {
2156 wxRichTextRange range = GetInternalSelectionRange();
2157 GetBuffer().CopyToClipboard(range);
2158 }
2159}
2160
2161void wxRichTextCtrl::Cut()
2162{
2163 if (CanCut())
2164 {
2165 wxRichTextRange range = GetInternalSelectionRange();
2166 GetBuffer().CopyToClipboard(range);
2167
2168 DeleteSelectedContent();
2169 LayoutContent();
2170 Refresh(false);
2171 }
2172}
2173
2174void wxRichTextCtrl::Paste()
2175{
2176 if (CanPaste())
2177 {
2178 BeginBatchUndo(_("Paste"));
2179
2180 long newPos = m_caretPosition;
2181 DeleteSelectedContent(& newPos);
2182
2183 GetBuffer().PasteFromClipboard(newPos);
2184
2185 EndBatchUndo();
2186 }
2187}
2188
2189void wxRichTextCtrl::DeleteSelection()
2190{
2191 if (CanDeleteSelection())
2192 {
2193 DeleteSelectedContent();
2194 }
2195}
2196
2197bool wxRichTextCtrl::HasSelection() const
2198{
2199 return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2;
2200}
2201
2202bool wxRichTextCtrl::CanCopy() const
2203{
2204 // Can copy if there's a selection
2205 return HasSelection();
2206}
2207
2208bool wxRichTextCtrl::CanCut() const
2209{
2210 return HasSelection() && IsEditable();
2211}
2212
2213bool wxRichTextCtrl::CanPaste() const
2214{
2215 if ( !IsEditable() )
2216 return false;
2217
2218 return GetBuffer().CanPasteFromClipboard();
2219}
2220
2221bool wxRichTextCtrl::CanDeleteSelection() const
2222{
2223 return HasSelection() && IsEditable();
2224}
2225
2226
2227// ----------------------------------------------------------------------------
2228// Accessors
2229// ----------------------------------------------------------------------------
2230
2231void wxRichTextCtrl::SetEditable(bool editable)
2232{
2233 m_editable = editable;
2234}
2235
2236void wxRichTextCtrl::SetInsertionPoint(long pos)
2237{
2238 SelectNone();
2239
2240 m_caretPosition = pos - 1;
2241
2242 PositionCaret();
2243}
2244
2245void wxRichTextCtrl::SetInsertionPointEnd()
2246{
2247 long pos = GetLastPosition();
2248 SetInsertionPoint(pos);
2249}
2250
2251long wxRichTextCtrl::GetInsertionPoint() const
2252{
2253 return m_caretPosition+1;
2254}
2255
2256wxTextPos wxRichTextCtrl::GetLastPosition() const
2257{
2258 return GetBuffer().GetRange().GetEnd();
2259}
2260
2261// If the return values from and to are the same, there is no
2262// selection.
2263void wxRichTextCtrl::GetSelection(long* from, long* to) const
2264{
2265 *from = m_selectionRange.GetStart();
2266 *to = m_selectionRange.GetEnd();
2267 if ((*to) != -1 && (*to) != -2)
2268 (*to) ++;
2269}
2270
2271bool wxRichTextCtrl::IsEditable() const
2272{
2273 return m_editable;
2274}
2275
2276// ----------------------------------------------------------------------------
2277// selection
2278// ----------------------------------------------------------------------------
2279
2280void wxRichTextCtrl::SetSelection(long from, long to)
2281{
2282 // if from and to are both -1, it means (in wxWidgets) that all text should
2283 // be selected.
2284 if ( (from == -1) && (to == -1) )
2285 {
2286 from = 0;
2287 to = GetLastPosition()+1;
2288 }
2289
2290 DoSetSelection(from, to);
2291}
2292
2293void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
2294{
2295 m_selectionAnchor = from;
2296 m_selectionRange.SetRange(from, to-1);
2297 if (from > -2)
2298 m_caretPosition = from-1;
2299
2300 Refresh(false);
2301 PositionCaret();
2302}
2303
2304// ----------------------------------------------------------------------------
2305// Editing
2306// ----------------------------------------------------------------------------
2307
2308void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to),
2309 const wxString& value)
2310{
2311 BeginBatchUndo(_("Replace"));
2312
2313 DeleteSelectedContent();
2314
2315 DoWriteText(value, SetValue_SelectionOnly);
2316
2317 EndBatchUndo();
2318}
2319
2320void wxRichTextCtrl::Remove(long from, long to)
2321{
2322 SelectNone();
2323
2324 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to), this);
2325
2326 LayoutContent();
2327 if (!IsFrozen())
2328 Refresh(false);
2329}
2330
2331bool wxRichTextCtrl::IsModified() const
2332{
2333 return m_buffer.IsModified();
2334}
2335
2336void wxRichTextCtrl::MarkDirty()
2337{
2338 m_buffer.Modify(true);
2339}
2340
2341void wxRichTextCtrl::DiscardEdits()
2342{
2343 m_caretPositionForDefaultStyle = -2;
2344 m_buffer.Modify(false);
2345 m_buffer.GetCommandProcessor()->ClearCommands();
2346}
2347
2348int wxRichTextCtrl::GetNumberOfLines() const
2349{
2350 return GetBuffer().GetParagraphCount();
2351}
2352
2353// ----------------------------------------------------------------------------
2354// Positions <-> coords
2355// ----------------------------------------------------------------------------
2356
2357long wxRichTextCtrl::XYToPosition(long x, long y) const
2358{
2359 return GetBuffer().XYToPosition(x, y);
2360}
2361
2362bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
2363{
2364 return GetBuffer().PositionToXY(pos, x, y);
2365}
2366
2367// ----------------------------------------------------------------------------
2368//
2369// ----------------------------------------------------------------------------
2370
2371void wxRichTextCtrl::ShowPosition(long pos)
2372{
2373 if (!IsPositionVisible(pos))
2374 ScrollIntoView(pos-1, WXK_DOWN);
2375}
2376
2377int wxRichTextCtrl::GetLineLength(long lineNo) const
2378{
2379 return GetBuffer().GetParagraphLength(lineNo);
2380}
2381
2382wxString wxRichTextCtrl::GetLineText(long lineNo) const
2383{
2384 return GetBuffer().GetParagraphText(lineNo);
2385}
2386
2387// ----------------------------------------------------------------------------
2388// Undo/redo
2389// ----------------------------------------------------------------------------
2390
2391void wxRichTextCtrl::Undo()
2392{
2393 if (CanUndo())
2394 {
2395 GetCommandProcessor()->Undo();
2396 }
2397}
2398
2399void wxRichTextCtrl::Redo()
2400{
2401 if (CanRedo())
2402 {
2403 GetCommandProcessor()->Redo();
2404 }
2405}
2406
2407bool wxRichTextCtrl::CanUndo() const
2408{
2409 return GetCommandProcessor()->CanUndo();
2410}
2411
2412bool wxRichTextCtrl::CanRedo() const
2413{
2414 return GetCommandProcessor()->CanRedo();
2415}
2416
2417// ----------------------------------------------------------------------------
2418// implementation details
2419// ----------------------------------------------------------------------------
2420
2421void wxRichTextCtrl::Command(wxCommandEvent& event)
2422{
2423 SetValue(event.GetString());
2424 GetEventHandler()->ProcessEvent(event);
2425}
2426
2427void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
2428{
2429 // By default, load the first file into the text window.
2430 if (event.GetNumberOfFiles() > 0)
2431 {
2432 LoadFile(event.GetFiles()[0]);
2433 }
2434}
2435
2436wxSize wxRichTextCtrl::DoGetBestSize() const
2437{
2438 return wxSize(10, 10);
2439}
2440
2441// ----------------------------------------------------------------------------
2442// standard handlers for standard edit menu events
2443// ----------------------------------------------------------------------------
2444
2445void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2446{
2447 Cut();
2448}
2449
2450void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
2451{
2452 DeleteSelection();
2453}
2454
2455void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2456{
2457 Copy();
2458}
2459
2460void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2461{
2462 Paste();
2463}
2464
2465void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2466{
2467 Undo();
2468}
2469
2470void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2471{
2472 Redo();
2473}
2474
2475void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2476{
2477 event.Enable( CanCut() );
2478}
2479
2480void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2481{
2482 event.Enable( CanCopy() );
2483}
2484
2485void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2486{
2487 event.Enable( CanDeleteSelection() );
2488}
2489
2490void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2491{
2492 event.Enable( CanPaste() );
2493}
2494
2495void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2496{
2497 event.Enable( CanUndo() );
2498 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2499}
2500
2501void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2502{
2503 event.Enable( CanRedo() );
2504 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2505}
2506
2507void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2508{
2509 SelectAll();
2510}
2511
2512void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2513{
2514 event.Enable(GetLastPosition() > 0);
2515}
2516
2517void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event)
2518{
2519 if (event.GetEventObject() != this)
2520 {
2521 event.Skip();
2522 return;
2523 }
2524
2525 if (!m_contextMenu)
2526 {
2527 m_contextMenu = new wxMenu;
2528 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
2529 m_contextMenu->Append(wxID_REDO, _("&Redo"));
2530 m_contextMenu->AppendSeparator();
2531 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
2532 m_contextMenu->Append(wxID_COPY, _("&Copy"));
2533 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
2534 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
2535 m_contextMenu->AppendSeparator();
2536 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
2537 }
2538 PopupMenu(m_contextMenu);
2539 return;
2540}
2541
2542bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2543{
2544 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttr(style));
2545}
2546
2547bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style)
2548{
2549 return GetBuffer().SetStyle(range.ToInternal(), style);
2550}
2551
2552// extended style setting operation with flags including:
2553// wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2554// see richtextbuffer.h for more details.
2555
2556bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttr& style, int flags)
2557{
2558 return GetBuffer().SetStyle(range.ToInternal(), style, flags);
2559}
2560
2561bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2562{
2563 return GetBuffer().SetDefaultStyle(wxTextAttr(style));
2564}
2565
2566const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const
2567{
2568 return GetBuffer().GetDefaultStyle();
2569}
2570
2571bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
2572{
2573 return GetBuffer().GetStyle(position, style);
2574}
2575
2576// get the common set of styles for the range
2577bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style)
2578{
2579 return GetBuffer().GetStyleForRange(range.ToInternal(), style);
2580}
2581
2582/// Get the content (uncombined) attributes for this position.
2583bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style)
2584{
2585 return GetBuffer().GetUncombinedStyle(position, style);
2586}
2587
2588/// Set font, and also the buffer attributes
2589bool wxRichTextCtrl::SetFont(const wxFont& font)
2590{
2591 wxControl::SetFont(font);
2592
2593 wxTextAttr attr = GetBuffer().GetAttributes();
2594 attr.SetFont(font);
2595 GetBuffer().SetBasicStyle(attr);
2596
2597 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2598 Refresh(false);
2599
2600 return true;
2601}
2602
2603/// Transform logical to physical
2604wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
2605{
2606 wxPoint pt;
2607 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
2608
2609 return pt;
2610}
2611
2612/// Transform physical to logical
2613wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
2614{
2615 wxPoint pt;
2616 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2617
2618 return pt;
2619}
2620
2621/// Position the caret
2622void wxRichTextCtrl::PositionCaret()
2623{
2624 if (!GetCaret())
2625 return;
2626
2627 //wxLogDebug(wxT("PositionCaret"));
2628
2629 wxRect caretRect;
2630 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2631 {
2632 wxPoint newPt = caretRect.GetPosition();
2633 wxSize newSz = caretRect.GetSize();
2634 wxPoint pt = GetPhysicalPoint(newPt);
2635 if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz)
2636 {
2637 GetCaret()->Move(pt);
2638 GetCaret()->SetSize(newSz);
2639 }
2640 }
2641}
2642
2643/// Get the caret height and position for the given character position
2644bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2645{
2646 wxClientDC dc(this);
2647 dc.SetFont(GetFont());
2648
2649 PrepareDC(dc);
2650
2651 wxPoint pt;
2652 int height = 0;
2653
2654 if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart))
2655 {
2656 // Caret height can't be zero
2657 if (height == 0)
2658 height = dc.GetCharHeight();
2659
2660 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
2661 return true;
2662 }
2663
2664 return false;
2665}
2666
2667/// Gets the line for the visible caret position. If the caret is
2668/// shown at the very end of the line, it means the next character is actually
2669/// on the following line. So let's get the line we're expecting to find
2670/// if this is the case.
2671wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2672{
2673 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2674 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2675 if (line)
2676 {
2677 wxRichTextRange lineRange = line->GetAbsoluteRange();
2678 if (caretPosition == lineRange.GetStart()-1 &&
2679 (para->GetRange().GetStart() != lineRange.GetStart()))
2680 {
2681 if (!m_caretAtLineStart)
2682 line = GetBuffer().GetLineAtPosition(caretPosition-1, true);
2683 }
2684 }
2685 return line;
2686}
2687
2688
2689/// Move the caret to the given character position
2690bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2691{
2692 if (GetBuffer().GetDirty())
2693 LayoutContent();
2694
2695 if (pos <= GetBuffer().GetRange().GetEnd())
2696 {
2697 SetCaretPosition(pos, showAtLineStart);
2698
2699 PositionCaret();
2700
2701 return true;
2702 }
2703 else
2704 return false;
2705}
2706
2707/// Layout the buffer: which we must do before certain operations, such as
2708/// setting the caret position.
2709bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
2710{
2711 if (GetBuffer().GetDirty() || onlyVisibleRect)
2712 {
2713 wxRect availableSpace(GetClientSize());
2714 if (availableSpace.width == 0)
2715 availableSpace.width = 10;
2716 if (availableSpace.height == 0)
2717 availableSpace.height = 10;
2718
2719 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
2720 if (onlyVisibleRect)
2721 {
2722 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
2723 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2724 }
2725
2726 wxClientDC dc(this);
2727 dc.SetFont(GetFont());
2728
2729 PrepareDC(dc);
2730
2731 GetBuffer().Defragment();
2732 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2733 GetBuffer().Layout(dc, availableSpace, flags);
2734 GetBuffer().SetDirty(false);
2735
2736 if (!IsFrozen())
2737 SetupScrollbars();
2738 }
2739
2740 return true;
2741}
2742
2743/// Is all of the selection bold?
2744bool wxRichTextCtrl::IsSelectionBold()
2745{
2746 if (HasSelection())
2747 {
2748 wxTextAttr attr;
2749 wxRichTextRange range = GetSelectionRange();
2750 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2751 attr.SetFontWeight(wxBOLD);
2752
2753 return HasCharacterAttributes(range, attr);
2754 }
2755 else
2756 {
2757 // If no selection, then we need to combine current style with default style
2758 // to see what the effect would be if we started typing.
2759 wxTextAttr attr;
2760 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2761
2762 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2763 if (GetStyle(pos, attr))
2764 {
2765 if (IsDefaultStyleShowing())
2766 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2767 return attr.GetFontWeight() == wxBOLD;
2768 }
2769 }
2770 return false;
2771}
2772
2773/// Is all of the selection italics?
2774bool wxRichTextCtrl::IsSelectionItalics()
2775{
2776 if (HasSelection())
2777 {
2778 wxRichTextRange range = GetSelectionRange();
2779 wxTextAttr attr;
2780 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2781 attr.SetFontStyle(wxITALIC);
2782
2783 return HasCharacterAttributes(range, attr);
2784 }
2785 else
2786 {
2787 // If no selection, then we need to combine current style with default style
2788 // to see what the effect would be if we started typing.
2789 wxTextAttr attr;
2790 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2791
2792 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2793 if (GetStyle(pos, attr))
2794 {
2795 if (IsDefaultStyleShowing())
2796 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2797 return attr.GetFontStyle() == wxITALIC;
2798 }
2799 }
2800 return false;
2801}
2802
2803/// Is all of the selection underlined?
2804bool wxRichTextCtrl::IsSelectionUnderlined()
2805{
2806 if (HasSelection())
2807 {
2808 wxRichTextRange range = GetSelectionRange();
2809 wxTextAttr attr;
2810 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2811 attr.SetFontUnderlined(true);
2812
2813 return HasCharacterAttributes(range, attr);
2814 }
2815 else
2816 {
2817 // If no selection, then we need to combine current style with default style
2818 // to see what the effect would be if we started typing.
2819 wxTextAttr attr;
2820 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2821 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2822
2823 if (GetStyle(pos, attr))
2824 {
2825 if (IsDefaultStyleShowing())
2826 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2827 return attr.GetFontUnderlined();
2828 }
2829 }
2830 return false;
2831}
2832
2833/// Apply bold to the selection
2834bool wxRichTextCtrl::ApplyBoldToSelection()
2835{
2836 wxTextAttr attr;
2837 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2838 attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD);
2839
2840 if (HasSelection())
2841 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
2842 else
2843 SetAndShowDefaultStyle(attr);
2844 return true;
2845}
2846
2847/// Apply italic to the selection
2848bool wxRichTextCtrl::ApplyItalicToSelection()
2849{
2850 wxTextAttr attr;
2851 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2852 attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC);
2853
2854 if (HasSelection())
2855 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
2856 else
2857 SetAndShowDefaultStyle(attr);
2858 return true;
2859}
2860
2861/// Apply underline to the selection
2862bool wxRichTextCtrl::ApplyUnderlineToSelection()
2863{
2864 wxTextAttr attr;
2865 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2866 attr.SetFontUnderlined(!IsSelectionUnderlined());
2867
2868 if (HasSelection())
2869 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
2870 else
2871 SetAndShowDefaultStyle(attr);
2872 return true;
2873}
2874
2875/// Is all of the selection aligned according to the specified flag?
2876bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
2877{
2878 wxRichTextRange range;
2879 if (HasSelection())
2880 range = GetSelectionRange();
2881 else
2882 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2883
2884 wxTextAttr attr;
2885 attr.SetAlignment(alignment);
2886
2887 return HasParagraphAttributes(range, attr);
2888}
2889
2890/// Apply alignment to the selection
2891bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
2892{
2893 wxTextAttr attr;
2894 attr.SetAlignment(alignment);
2895 if (HasSelection())
2896 return SetStyle(GetSelectionRange(), attr);
2897 else
2898 {
2899 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2900 if (para)
2901 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
2902 }
2903 return true;
2904}
2905
2906/// Apply a named style to the selection
2907bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
2908{
2909 // Flags are defined within each definition, so only certain
2910 // attributes are applied.
2911 wxTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
2912
2913 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE;
2914
2915 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
2916 {
2917 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
2918
2919 wxRichTextRange range;
2920
2921 if (HasSelection())
2922 range = GetSelectionRange();
2923 else
2924 {
2925 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2926 range = wxRichTextRange(pos, pos+1);
2927 }
2928
2929 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
2930 }
2931
2932 // Make sure the attr has the style name
2933 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
2934 {
2935 attr.SetParagraphStyleName(def->GetName());
2936
2937 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2938 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2939 // to change its style independently.
2940 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
2941 }
2942 else
2943 attr.SetCharacterStyleName(def->GetName());
2944
2945 if (HasSelection())
2946 return SetStyleEx(GetSelectionRange(), attr, flags);
2947 else
2948 {
2949 SetAndShowDefaultStyle(attr);
2950 return true;
2951 }
2952}
2953
2954/// Apply the style sheet to the buffer, for example if the styles have changed.
2955bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
2956{
2957 if (!styleSheet)
2958 styleSheet = GetBuffer().GetStyleSheet();
2959 if (!styleSheet)
2960 return false;
2961
2962 if (GetBuffer().ApplyStyleSheet(styleSheet))
2963 {
2964 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2965 Refresh(false);
2966 return true;
2967 }
2968 else
2969 return false;
2970}
2971
2972/// Sets the default style to the style under the cursor
2973bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2974{
2975 wxTextAttr attr;
2976 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
2977
2978 // If at the start of a paragraph, use the next position.
2979 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2980
2981 if (GetUncombinedStyle(pos, attr))
2982 {
2983 SetDefaultStyle(attr);
2984 return true;
2985 }
2986
2987 return false;
2988}
2989
2990/// Returns the first visible position in the current view
2991long wxRichTextCtrl::GetFirstVisiblePosition() const
2992{
2993 wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
2994 if (line)
2995 return line->GetAbsoluteRange().GetStart();
2996 else
2997 return 0;
2998}
2999
3000/// Get the first visible point in the window
3001wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const
3002{
3003 int ppuX, ppuY;
3004 int startXUnits, startYUnits;
3005
3006 GetScrollPixelsPerUnit(& ppuX, & ppuY);
3007 GetViewStart(& startXUnits, & startYUnits);
3008
3009 return wxPoint(startXUnits * ppuX, startYUnits * ppuY);
3010}
3011
3012/// The adjusted caret position is the character position adjusted to take
3013/// into account whether we're at the start of a paragraph, in which case
3014/// style information should be taken from the next position, not current one.
3015long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
3016{
3017 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1);
3018
3019 if (para && (caretPos+1 == para->GetRange().GetStart()))
3020 caretPos ++;
3021 return caretPos;
3022}
3023
3024/// Get/set the selection range in character positions. -1, -1 means no selection.
3025/// The range is in API convention, i.e. a single character selection is denoted
3026/// by (n, n+1)
3027wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
3028{
3029 wxRichTextRange range = GetInternalSelectionRange();
3030 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
3031 range.SetEnd(range.GetEnd() + 1);
3032 return range;
3033}
3034
3035void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
3036{
3037 wxRichTextRange range1(range);
3038 if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) )
3039 range1.SetEnd(range1.GetEnd() - 1);
3040
3041 wxASSERT( range1.GetStart() > range1.GetEnd() );
3042
3043 SetInternalSelectionRange(range1);
3044}
3045
3046/// Set list style
3047bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3048{
3049 return GetBuffer().SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3050}
3051
3052bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3053{
3054 return GetBuffer().SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3055}
3056
3057/// Clear list for given range
3058bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags)
3059{
3060 return GetBuffer().ClearListStyle(range.ToInternal(), flags);
3061}
3062
3063/// Number/renumber any list elements in the given range
3064bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3065{
3066 return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3067}
3068
3069bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3070{
3071 return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3072}
3073
3074/// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3075bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
3076{
3077 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel);
3078}
3079
3080bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
3081{
3082 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel);
3083}
3084
3085/// Deletes the content in the given range
3086bool wxRichTextCtrl::Delete(const wxRichTextRange& range)
3087{
3088 return GetBuffer().DeleteRangeWithUndo(range.ToInternal(), this);
3089}
3090
3091const wxArrayString& wxRichTextCtrl::GetAvailableFontNames()
3092{
3093 if (sm_availableFontNames.GetCount() == 0)
3094 {
3095 sm_availableFontNames = wxFontEnumerator::GetFacenames();
3096 sm_availableFontNames.Sort();
3097 }
3098 return sm_availableFontNames;
3099}
3100
3101void wxRichTextCtrl::ClearAvailableFontNames()
3102{
3103 sm_availableFontNames.Clear();
3104}
3105
3106void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
3107{
3108 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3109
3110 wxTextAttrEx basicStyle = GetBasicStyle();
3111 basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3112 SetBasicStyle(basicStyle);
3113 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3114
3115 Refresh();
3116}
3117
3118#endif
3119 // wxUSE_RICHTEXT