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