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