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