]> git.saurik.com Git - wxWidgets.git/blob - include/wx/univ/textctrl.h
Fix for broken MSWUniv after wxABI_VERSION removal (otherwise wxQTMediaBackend::QTWnd...
[wxWidgets.git] / include / wx / univ / textctrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/textctrl.h
3 // Purpose: wxTextCtrl class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 15.09.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_UNIV_TEXTCTRL_H_
13 #define _WX_UNIV_TEXTCTRL_H_
14
15 class WXDLLEXPORT wxCaret;
16 class WXDLLEXPORT wxTextCtrlCommandProcessor;
17
18 #include "wx/scrolwin.h" // for wxScrollHelper
19
20 #include "wx/univ/inphand.h"
21
22 // ----------------------------------------------------------------------------
23 // wxTextCtrl actions
24 // ----------------------------------------------------------------------------
25
26 // cursor movement and also selection and delete operations
27 #define wxACTION_TEXT_GOTO _T("goto") // to pos in numArg
28 #define wxACTION_TEXT_FIRST _T("first") // go to pos 0
29 #define wxACTION_TEXT_LAST _T("last") // go to last pos
30 #define wxACTION_TEXT_HOME _T("home")
31 #define wxACTION_TEXT_END _T("end")
32 #define wxACTION_TEXT_LEFT _T("left")
33 #define wxACTION_TEXT_RIGHT _T("right")
34 #define wxACTION_TEXT_UP _T("up")
35 #define wxACTION_TEXT_DOWN _T("down")
36 #define wxACTION_TEXT_WORD_LEFT _T("wordleft")
37 #define wxACTION_TEXT_WORD_RIGHT _T("wordright")
38 #define wxACTION_TEXT_PAGE_UP _T("pageup")
39 #define wxACTION_TEXT_PAGE_DOWN _T("pagedown")
40
41 // clipboard operations
42 #define wxACTION_TEXT_COPY _T("copy")
43 #define wxACTION_TEXT_CUT _T("cut")
44 #define wxACTION_TEXT_PASTE _T("paste")
45
46 // insert text at the cursor position: the text is in strArg of PerformAction
47 #define wxACTION_TEXT_INSERT _T("insert")
48
49 // if the action starts with either of these prefixes and the rest of the
50 // string is one of the movement commands, it means to select/delete text from
51 // the current cursor position to the new one
52 #define wxACTION_TEXT_PREFIX_SEL _T("sel")
53 #define wxACTION_TEXT_PREFIX_DEL _T("del")
54
55 // mouse selection
56 #define wxACTION_TEXT_ANCHOR_SEL _T("anchorsel")
57 #define wxACTION_TEXT_EXTEND_SEL _T("extendsel")
58 #define wxACTION_TEXT_SEL_WORD _T("wordsel")
59 #define wxACTION_TEXT_SEL_LINE _T("linesel")
60
61 // undo or redo
62 #define wxACTION_TEXT_UNDO _T("undo")
63 #define wxACTION_TEXT_REDO _T("redo")
64
65 // ----------------------------------------------------------------------------
66 // wxTextCtrl
67 // ----------------------------------------------------------------------------
68
69 class WXDLLEXPORT wxTextCtrl : public wxTextCtrlBase, public wxScrollHelper
70 {
71 public:
72 // creation
73 // --------
74
75 wxTextCtrl() { Init(); }
76
77 wxTextCtrl(wxWindow *parent,
78 wxWindowID id,
79 const wxString& value = wxEmptyString,
80 const wxPoint& pos = wxDefaultPosition,
81 const wxSize& size = wxDefaultSize,
82 long style = 0,
83 const wxValidator& validator = wxDefaultValidator,
84 const wxString& name = wxTextCtrlNameStr)
85 {
86 Init();
87
88 Create(parent, id, value, pos, size, style, validator, name);
89 }
90
91 bool Create(wxWindow *parent,
92 wxWindowID id,
93 const wxString& value = wxEmptyString,
94 const wxPoint& pos = wxDefaultPosition,
95 const wxSize& size = wxDefaultSize,
96 long style = 0,
97 const wxValidator& validator = wxDefaultValidator,
98 const wxString& name = wxTextCtrlNameStr);
99
100 virtual ~wxTextCtrl();
101
102 // implement base class pure virtuals
103 // ----------------------------------
104
105 virtual wxString GetValue() const;
106 virtual void SetValue(const wxString& value);
107
108 virtual int GetLineLength(wxTextCoord lineNo) const;
109 virtual wxString GetLineText(wxTextCoord lineNo) const;
110 virtual int GetNumberOfLines() const;
111
112 virtual bool IsModified() const;
113 virtual bool IsEditable() const;
114
115 // If the return values from and to are the same, there is no selection.
116 virtual void GetSelection(wxTextPos* from, wxTextPos* to) const;
117
118 // operations
119 // ----------
120
121 // editing
122 virtual void Clear();
123 virtual void Replace(wxTextPos from, wxTextPos to, const wxString& value);
124 virtual void Remove(wxTextPos from, wxTextPos to);
125
126 // sets/clears the dirty flag
127 virtual void MarkDirty();
128 virtual void DiscardEdits();
129
130 // writing text inserts it at the current position, appending always
131 // inserts it at the end
132 virtual void WriteText(const wxString& text);
133 virtual void AppendText(const wxString& text);
134
135 // translate between the position (which is just an index in the text ctrl
136 // considering all its contents as a single strings) and (x, y) coordinates
137 // which represent (logical, i.e. unwrapped) column and line.
138 virtual wxTextPos XYToPosition(wxTextCoord x, wxTextCoord y) const;
139 virtual bool PositionToXY(wxTextPos pos,
140 wxTextCoord *x, wxTextCoord *y) const;
141
142 // wxUniv-specific: find a screen position (in client coordinates) of the
143 // given text position or of the caret
144 bool PositionToLogicalXY(wxTextPos pos, wxCoord *x, wxCoord *y) const;
145 bool PositionToDeviceXY(wxTextPos pos, wxCoord *x, wxCoord *y) const;
146 wxPoint GetCaretPosition() const;
147
148 virtual void ShowPosition(wxTextPos pos);
149
150 // Clipboard operations
151 virtual void Copy();
152 virtual void Cut();
153 virtual void Paste();
154
155 // Undo/redo
156 virtual void Undo();
157 virtual void Redo();
158
159 virtual bool CanUndo() const;
160 virtual bool CanRedo() const;
161
162 // Insertion point
163 virtual void SetInsertionPoint(wxTextPos pos);
164 virtual void SetInsertionPointEnd();
165 virtual wxTextPos GetInsertionPoint() const;
166 virtual wxTextPos GetLastPosition() const;
167
168 virtual void SetSelection(wxTextPos from, wxTextPos to);
169 virtual void SetEditable(bool editable);
170
171 // wxUniv-specific methods
172 // -----------------------
173
174 // caret stuff
175 virtual void ShowCaret(bool show = true);
176 void HideCaret() { ShowCaret(false); }
177 void CreateCaret(); // for the current font size
178
179 // helpers for cursor movement
180 wxTextPos GetWordStart() const;
181 wxTextPos GetWordEnd() const;
182
183 // selection helpers
184 bool HasSelection() const
185 { return m_selStart != -1 && m_selEnd > m_selStart; }
186 void ClearSelection();
187 void RemoveSelection();
188 wxString GetSelectionText() const;
189
190 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const;
191 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt,
192 wxTextCoord *col,
193 wxTextCoord *row) const;
194
195 // find the character at this position in the given line, return value as
196 // for HitTest()
197 //
198 // NB: x is the logical coord (client and unscrolled)
199 wxTextCtrlHitTestResult HitTestLine(const wxString& line,
200 wxCoord x,
201 wxTextCoord *colOut) const;
202
203 // bring the given position into view
204 void ShowHorzPosition(wxCoord pos);
205
206 // scroll the window horizontally so that the first character shown is in
207 // position pos
208 void ScrollText(wxTextCoord col);
209
210 // adjust the DC for horz text control scrolling too
211 virtual void DoPrepareDC(wxDC& dc);
212
213 // implementation only from now on
214 // -------------------------------
215
216 // override this to take into account our scrollbar-less scrolling
217 virtual void CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const;
218 virtual void CalcScrolledPosition(int x, int y, int *xx, int *yy) const;
219
220 // ensure we have correct default border
221 virtual wxBorder GetDefaultBorder() const { return wxBORDER_SUNKEN; }
222
223 // perform an action
224 virtual bool PerformAction(const wxControlAction& action,
225 long numArg = -1,
226 const wxString& strArg = wxEmptyString);
227
228 // override these methods to handle the caret
229 virtual bool SetFont(const wxFont &font);
230 virtual bool Enable(bool enable = true);
231
232 // more readable flag testing methods
233 bool IsPassword() const { return (GetWindowStyle() & wxTE_PASSWORD) != 0; }
234 bool WrapLines() const
235 { return !IsSingleLine() && !(GetWindowStyle() & wxHSCROLL); }
236
237 // only for wxStdTextCtrlInputHandler
238 void RefreshSelection();
239
240 protected:
241 // override base class methods
242 virtual void DoDrawBorder(wxDC& dc, const wxRect& rect);
243 virtual void DoDraw(wxControlRenderer *renderer);
244
245 // calc the size from the text extent
246 virtual wxSize DoGetBestClientSize() const;
247
248 // common part of all ctors
249 void Init();
250
251 // drawing
252 // -------
253
254 // draw the text in the given rectangle
255 void DoDrawTextInRect(wxDC& dc, const wxRect& rectUpdate);
256
257 // draw the line wrap marks in this rect
258 void DoDrawLineWrapMarks(wxDC& dc, const wxRect& rectUpdate);
259
260 // line/row geometry calculations
261 // ------------------------------
262
263 // get the extent (width) of the text
264 wxCoord GetTextWidth(const wxString& text) const;
265
266 // get the logical text width (accounting for scrolling)
267 wxCoord GetTotalWidth() const;
268
269 // get total number of rows (different from number of lines if the lines
270 // can be wrapped)
271 wxTextCoord GetRowCount() const;
272
273 // find the number of rows in this line (only if WrapLines())
274 wxTextCoord GetRowsPerLine(wxTextCoord line) const;
275
276 // get the starting row of the given line
277 wxTextCoord GetFirstRowOfLine(wxTextCoord line) const;
278
279 // get the row following this line
280 wxTextCoord GetRowAfterLine(wxTextCoord line) const;
281
282 // refresh functions
283 // -----------------
284
285 // the text area is the part of the window in which the text can be
286 // displayed, i.e. part of it inside the margins and the real text area is
287 // the area in which the text *is* currently displayed: for example, in the
288 // multiline control case the text area can have extra space at the bottom
289 // which is not tall enough for another line and which is then not included
290 // into the real text area
291 wxRect GetRealTextArea() const;
292
293 // refresh the text in the given (in logical coords) rect
294 void RefreshTextRect(const wxRect& rect, bool textOnly = true);
295
296 // refresh the line wrap marks for the given range of lines (inclusive)
297 void RefreshLineWrapMarks(wxTextCoord rowFirst, wxTextCoord rowLast);
298
299 // refresh the text in the given range (in logical coords) of this line, if
300 // width is 0, refresh to the end of line
301 void RefreshPixelRange(wxTextCoord line, wxCoord start, wxCoord width);
302
303 // refresh the text in the given range (in text coords) in this line
304 void RefreshColRange(wxTextCoord line, wxTextPos start, size_t count);
305
306 // refresh the text from in the given line range (inclusive)
307 void RefreshLineRange(wxTextCoord lineFirst, wxTextCoord lineLast);
308
309 // refresh the text in the given range which can span multiple lines
310 // (this method accepts arguments in any order)
311 void RefreshTextRange(wxTextPos start, wxTextPos end);
312
313 // get the text to show: either the text itself or the text replaced with
314 // starts for wxTE_PASSWORD control
315 wxString GetTextToShow(const wxString& text) const;
316
317 // find the row in this line where the given position (counted from the
318 // start of line) is
319 wxTextCoord GetRowInLine(wxTextCoord line,
320 wxTextCoord col,
321 wxTextCoord *colRowStart = NULL) const;
322
323 // find the number of characters of a line before it wraps
324 // (and optionally also the real width of the line)
325 size_t GetPartOfWrappedLine(const wxChar* text,
326 wxCoord *widthReal = NULL) const;
327
328 // get the start and end of the selection for this line: if the line is
329 // outside the selection, both will be -1 and false will be returned
330 bool GetSelectedPartOfLine(wxTextCoord line,
331 wxTextPos *start, wxTextPos *end) const;
332
333 // update the text rect: the zone inside our client rect (its coords are
334 // client coords) which contains the text
335 void UpdateTextRect();
336
337 // calculate the last visible position
338 void UpdateLastVisible();
339
340 // move caret to the given position unconditionally
341 // (SetInsertionPoint() does nothing if the position didn't change)
342 void DoSetInsertionPoint(wxTextPos pos);
343
344 // move caret to the new position without updating the display (for
345 // internal use only)
346 void MoveInsertionPoint(wxTextPos pos);
347
348 // set the caret to its initial (default) position
349 void InitInsertionPoint();
350
351 // get the width of the longest line in pixels
352 wxCoord GetMaxWidth() const;
353
354 // force recalculation of the max line width
355 void RecalcMaxWidth();
356
357 // update the max width after the given line was modified
358 void UpdateMaxWidth(wxTextCoord line);
359
360 // hit testing
361 // -----------
362
363 // HitTest2() is more efficient than 2 consecutive HitTest()s with the same
364 // line (i.e. y) and it also returns the offset of the starting position in
365 // pixels
366 //
367 // as the last hack, this function accepts either logical or device (by
368 // default) coords depending on devCoords flag
369 wxTextCtrlHitTestResult HitTest2(wxCoord y,
370 wxCoord x1,
371 wxCoord x2,
372 wxTextCoord *row,
373 wxTextCoord *colStart,
374 wxTextCoord *colEnd,
375 wxTextCoord *colRowStart,
376 bool devCoords = true) const;
377
378 // HitTest() version which takes the logical text coordinates and not the
379 // device ones
380 wxTextCtrlHitTestResult HitTestLogical(const wxPoint& pos,
381 wxTextCoord *col,
382 wxTextCoord *row) const;
383
384 // get the line and the row in this line corresponding to the given row,
385 // return true if ok and false if row is out of range
386 //
387 // NB: this function can only be called for controls which wrap lines
388 bool GetLineAndRow(wxTextCoord row,
389 wxTextCoord *line,
390 wxTextCoord *rowInLine) const;
391
392 // get the height of one line (the same for all lines)
393 wxCoord GetLineHeight() const
394 {
395 // this one should be already precalculated
396 wxASSERT_MSG( m_heightLine != -1, _T("should have line height") );
397
398 return m_heightLine;
399 }
400
401 // get the average char width
402 wxCoord GetAverageWidth() const { return m_widthAvg; }
403
404 // recalc the line height and char width (to call when the font changes)
405 void RecalcFontMetrics();
406
407 // vertical scrolling helpers
408 // --------------------------
409
410 // all these functions are for multi line controls only
411
412 // get the number of visible lines
413 size_t GetLinesPerPage() const;
414
415 // return the position above the cursor or INVALID_POS_VALUE
416 wxTextPos GetPositionAbove();
417
418 // return the position below the cursor or INVALID_POS_VALUE
419 wxTextPos GetPositionBelow();
420
421 // event handlers
422 // --------------
423 void OnChar(wxKeyEvent& event);
424 void OnSize(wxSizeEvent& event);
425
426 // overrdie wxScrollHelper method to prevent (auto)scrolling beyond the end
427 // of line
428 virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const;
429
430 // return the struct containing control-type dependent data
431 struct wxTextSingleLineData& SData() { return *m_data.sdata; }
432 struct wxTextMultiLineData& MData() { return *m_data.mdata; }
433 struct wxTextWrappedData& WData() { return *m_data.wdata; }
434 const wxTextSingleLineData& SData() const { return *m_data.sdata; }
435 const wxTextMultiLineData& MData() const { return *m_data.mdata; }
436 const wxTextWrappedData& WData() const { return *m_data.wdata; }
437
438 // clipboard operations (unlike the versions without Do prefix, they have a
439 // return code)
440 bool DoCut();
441 bool DoPaste();
442
443 // idle processing
444 virtual void OnInternalIdle();
445 private:
446 // all these methods are for multiline text controls only
447
448 // update the scrollbars (only called from OnIdle)
449 void UpdateScrollbars();
450
451 // get read only access to the lines of multiline control
452 inline const wxArrayString& GetLines() const;
453 inline size_t GetLineCount() const;
454
455 // replace a line (returns true if the number of rows in thel ine changed)
456 bool ReplaceLine(wxTextCoord line, const wxString& text);
457
458 // remove a line
459 void RemoveLine(wxTextCoord line);
460
461 // insert a line at this position
462 void InsertLine(wxTextCoord line, const wxString& text);
463
464 // calculate geometry of this line
465 void LayoutLine(wxTextCoord line, class wxWrappedLineData& lineData) const;
466
467 // calculate geometry of all lines until the given one
468 void LayoutLines(wxTextCoord lineLast) const;
469
470 // the initially specified control size
471 wxSize m_sizeInitial;
472
473 // the global control text
474 wxString m_value;
475
476 // current position
477 wxTextPos m_curPos;
478 wxTextCoord m_curCol,
479 m_curRow;
480
481 // last position (only used by GetLastPosition())
482 wxTextPos m_posLast;
483
484 // selection
485 wxTextPos m_selAnchor,
486 m_selStart,
487 m_selEnd;
488
489 // flags
490 bool m_isModified:1,
491 m_isEditable:1,
492 m_hasCaret:1;
493
494 // the rectangle (in client coordinates) to draw text inside
495 wxRect m_rectText;
496
497 // the height of one line (cached value of GetCharHeight)
498 wxCoord m_heightLine;
499
500 // and the average char width (cached value of GetCharWidth)
501 wxCoord m_widthAvg;
502
503 // we have some data which depends on the kind of control (single or multi
504 // line)
505 union
506 {
507 wxTextSingleLineData *sdata;
508 wxTextMultiLineData *mdata;
509 wxTextWrappedData *wdata;
510 void *data;
511 } m_data;
512
513 // the object to which we delegate our undo/redo implementation
514 wxTextCtrlCommandProcessor *m_cmdProcessor;
515
516 DECLARE_EVENT_TABLE()
517 DECLARE_DYNAMIC_CLASS(wxTextCtrl)
518
519 friend class wxWrappedLineData;
520 };
521
522 // ----------------------------------------------------------------------------
523 // wxStdTextCtrlInputHandler: this control handles only the mouse/kbd actions
524 // common to Win32 and GTK, platform-specific things are implemented elsewhere
525 // ----------------------------------------------------------------------------
526
527 class WXDLLEXPORT wxStdTextCtrlInputHandler : public wxStdInputHandler
528 {
529 public:
530 wxStdTextCtrlInputHandler(wxInputHandler *inphand);
531
532 virtual bool HandleKey(wxInputConsumer *consumer,
533 const wxKeyEvent& event,
534 bool pressed);
535 virtual bool HandleMouse(wxInputConsumer *consumer,
536 const wxMouseEvent& event);
537 virtual bool HandleMouseMove(wxInputConsumer *consumer,
538 const wxMouseEvent& event);
539 virtual bool HandleFocus(wxInputConsumer *consumer, const wxFocusEvent& event);
540
541 protected:
542 // get the position of the mouse click
543 static wxTextPos HitTest(const wxTextCtrl *text, const wxPoint& pos);
544
545 // capture data
546 wxTextCtrl *m_winCapture;
547 };
548
549 #endif // _WX_UNIV_TEXTCTRL_H_
550