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