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