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