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