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