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