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