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