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