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