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