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