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