1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/textctrl.h
3 // Purpose: wxTextCtrl class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_UNIV_TEXTCTRL_H_
12 #define _WX_UNIV_TEXTCTRL_H_
14 class WXDLLIMPEXP_FWD_CORE wxCaret
;
15 class WXDLLIMPEXP_FWD_CORE wxTextCtrlCommandProcessor
;
17 #include "wx/scrolwin.h" // for wxScrollHelper
19 #include "wx/univ/inphand.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // cursor movement and also selection and delete operations
26 #define wxACTION_TEXT_GOTO wxT("goto") // to pos in numArg
27 #define wxACTION_TEXT_FIRST wxT("first") // go to pos 0
28 #define wxACTION_TEXT_LAST wxT("last") // go to last pos
29 #define wxACTION_TEXT_HOME wxT("home")
30 #define wxACTION_TEXT_END wxT("end")
31 #define wxACTION_TEXT_LEFT wxT("left")
32 #define wxACTION_TEXT_RIGHT wxT("right")
33 #define wxACTION_TEXT_UP wxT("up")
34 #define wxACTION_TEXT_DOWN wxT("down")
35 #define wxACTION_TEXT_WORD_LEFT wxT("wordleft")
36 #define wxACTION_TEXT_WORD_RIGHT wxT("wordright")
37 #define wxACTION_TEXT_PAGE_UP wxT("pageup")
38 #define wxACTION_TEXT_PAGE_DOWN wxT("pagedown")
40 // clipboard operations
41 #define wxACTION_TEXT_COPY wxT("copy")
42 #define wxACTION_TEXT_CUT wxT("cut")
43 #define wxACTION_TEXT_PASTE wxT("paste")
45 // insert text at the cursor position: the text is in strArg of PerformAction
46 #define wxACTION_TEXT_INSERT wxT("insert")
48 // if the action starts with either of these prefixes and the rest of the
49 // string is one of the movement commands, it means to select/delete text from
50 // the current cursor position to the new one
51 #define wxACTION_TEXT_PREFIX_SEL wxT("sel")
52 #define wxACTION_TEXT_PREFIX_DEL wxT("del")
55 #define wxACTION_TEXT_ANCHOR_SEL wxT("anchorsel")
56 #define wxACTION_TEXT_EXTEND_SEL wxT("extendsel")
57 #define wxACTION_TEXT_SEL_WORD wxT("wordsel")
58 #define wxACTION_TEXT_SEL_LINE wxT("linesel")
61 #define wxACTION_TEXT_UNDO wxT("undo")
62 #define wxACTION_TEXT_REDO wxT("redo")
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 class WXDLLIMPEXP_CORE wxTextCtrl
: public wxTextCtrlBase
,
75 wxTextCtrl() : wxScrollHelper(this) { Init(); }
77 wxTextCtrl(wxWindow
*parent
,
79 const wxString
& value
= wxEmptyString
,
80 const wxPoint
& pos
= wxDefaultPosition
,
81 const wxSize
& size
= wxDefaultSize
,
83 const wxValidator
& validator
= wxDefaultValidator
,
84 const wxString
& name
= wxTextCtrlNameStr
)
85 : wxScrollHelper(this)
89 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
92 bool Create(wxWindow
*parent
,
94 const wxString
& value
= wxEmptyString
,
95 const wxPoint
& pos
= wxDefaultPosition
,
96 const wxSize
& size
= wxDefaultSize
,
98 const wxValidator
& validator
= wxDefaultValidator
,
99 const wxString
& name
= wxTextCtrlNameStr
);
101 virtual ~wxTextCtrl();
103 // implement base class pure virtuals
104 // ----------------------------------
106 virtual int GetLineLength(wxTextCoord lineNo
) const;
107 virtual wxString
GetLineText(wxTextCoord lineNo
) const;
108 virtual int GetNumberOfLines() const;
110 virtual bool IsModified() const;
111 virtual bool IsEditable() const;
113 // If the return values from and to are the same, there is no selection.
114 virtual void GetSelection(wxTextPos
* from
, wxTextPos
* to
) const;
120 virtual void Clear();
121 virtual void Replace(wxTextPos from
, wxTextPos to
, const wxString
& value
);
122 virtual void Remove(wxTextPos from
, wxTextPos to
);
124 // sets/clears the dirty flag
125 virtual void MarkDirty();
126 virtual void DiscardEdits();
128 // writing text inserts it at the current position, appending always
129 // inserts it at the end
130 virtual void WriteText(const wxString
& text
);
131 virtual void AppendText(const wxString
& text
);
133 // translate between the position (which is just an index in the text ctrl
134 // considering all its contents as a single strings) and (x, y) coordinates
135 // which represent (logical, i.e. unwrapped) column and line.
136 virtual wxTextPos
XYToPosition(wxTextCoord x
, wxTextCoord y
) const;
137 virtual bool PositionToXY(wxTextPos pos
,
138 wxTextCoord
*x
, wxTextCoord
*y
) const;
140 // wxUniv-specific: find a screen position (in client coordinates) of the
141 // given text position or of the caret
142 bool PositionToLogicalXY(wxTextPos pos
, wxCoord
*x
, wxCoord
*y
) const;
143 bool PositionToDeviceXY(wxTextPos pos
, wxCoord
*x
, wxCoord
*y
) const;
144 wxPoint
GetCaretPosition() const;
146 virtual void ShowPosition(wxTextPos pos
);
148 // Clipboard operations
151 virtual void Paste();
157 virtual bool CanUndo() const;
158 virtual bool CanRedo() const;
161 virtual void SetInsertionPoint(wxTextPos pos
);
162 virtual void SetInsertionPointEnd();
163 virtual wxTextPos
GetInsertionPoint() const;
164 virtual wxTextPos
GetLastPosition() const;
166 virtual void SetSelection(wxTextPos from
, wxTextPos to
);
167 virtual void SetEditable(bool editable
);
169 // wxUniv-specific methods
170 // -----------------------
173 virtual void ShowCaret(bool show
= true);
174 void HideCaret() { ShowCaret(false); }
175 void CreateCaret(); // for the current font size
177 // helpers for cursor movement
178 wxTextPos
GetWordStart() const;
179 wxTextPos
GetWordEnd() const;
182 bool HasSelection() const
183 { return m_selStart
!= -1 && m_selEnd
> m_selStart
; }
184 void ClearSelection();
185 void RemoveSelection();
186 wxString
GetSelectionText() const;
188 virtual wxTextCtrlHitTestResult
HitTest(const wxPoint
& pt
, long *pos
) const;
189 virtual wxTextCtrlHitTestResult
HitTest(const wxPoint
& pt
,
191 wxTextCoord
*row
) const;
193 // find the character at this position in the given line, return value as
196 // NB: x is the logical coord (client and unscrolled)
197 wxTextCtrlHitTestResult
HitTestLine(const wxString
& line
,
199 wxTextCoord
*colOut
) const;
201 // bring the given position into view
202 void ShowHorzPosition(wxCoord pos
);
204 // scroll the window horizontally so that the first character shown is in
206 void ScrollText(wxTextCoord col
);
208 // adjust the DC for horz text control scrolling too
209 virtual void DoPrepareDC(wxDC
& dc
);
211 // implementation only from now on
212 // -------------------------------
214 // override this to take into account our scrollbar-less scrolling
215 virtual void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
216 virtual void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
219 virtual bool PerformAction(const wxControlAction
& action
,
221 const wxString
& strArg
= wxEmptyString
);
223 static wxInputHandler
*GetStdInputHandler(wxInputHandler
*handlerDef
);
224 virtual wxInputHandler
*DoGetStdInputHandler(wxInputHandler
*handlerDef
)
226 return GetStdInputHandler(handlerDef
);
229 // override these methods to handle the caret
230 virtual bool SetFont(const wxFont
&font
);
231 virtual bool Enable(bool enable
= true);
233 // more readable flag testing methods
234 bool IsPassword() const { return HasFlag(wxTE_PASSWORD
); }
235 bool WrapLines() const { return m_wrapLines
; }
237 // only for wxStdTextCtrlInputHandler
238 void RefreshSelection();
240 // override wxScrollHelper method to prevent (auto)scrolling beyond the end
242 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
245 virtual void OnInternalIdle();
248 // ensure we have correct default border
249 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_SUNKEN
; }
251 // override base class methods
252 virtual void DoDrawBorder(wxDC
& dc
, const wxRect
& rect
);
253 virtual void DoDraw(wxControlRenderer
*renderer
);
255 // calc the size from the text extent
256 virtual wxSize
DoGetBestClientSize() const;
258 // implements Set/ChangeValue()
259 virtual void DoSetValue(const wxString
& value
, int flags
= 0);
260 virtual wxString
DoGetValue() const;
262 // common part of all ctors
268 // draw the text in the given rectangle
269 void DoDrawTextInRect(wxDC
& dc
, const wxRect
& rectUpdate
);
271 // draw the line wrap marks in this rect
272 void DoDrawLineWrapMarks(wxDC
& dc
, const wxRect
& rectUpdate
);
274 // line/row geometry calculations
275 // ------------------------------
277 // get the extent (width) of the text
278 wxCoord
GetTextWidth(const wxString
& text
) const;
280 // get the logical text width (accounting for scrolling)
281 wxCoord
GetTotalWidth() const;
283 // get total number of rows (different from number of lines if the lines
285 wxTextCoord
GetRowCount() const;
287 // find the number of rows in this line (only if WrapLines())
288 wxTextCoord
GetRowsPerLine(wxTextCoord line
) const;
290 // get the starting row of the given line
291 wxTextCoord
GetFirstRowOfLine(wxTextCoord line
) const;
293 // get the row following this line
294 wxTextCoord
GetRowAfterLine(wxTextCoord line
) const;
299 // the text area is the part of the window in which the text can be
300 // displayed, i.e. part of it inside the margins and the real text area is
301 // the area in which the text *is* currently displayed: for example, in the
302 // multiline control case the text area can have extra space at the bottom
303 // which is not tall enough for another line and which is then not included
304 // into the real text area
305 wxRect
GetRealTextArea() const;
307 // refresh the text in the given (in logical coords) rect
308 void RefreshTextRect(const wxRect
& rect
, bool textOnly
= true);
310 // refresh the line wrap marks for the given range of lines (inclusive)
311 void RefreshLineWrapMarks(wxTextCoord rowFirst
, wxTextCoord rowLast
);
313 // refresh the text in the given range (in logical coords) of this line, if
314 // width is 0, refresh to the end of line
315 void RefreshPixelRange(wxTextCoord line
, wxCoord start
, wxCoord width
);
317 // refresh the text in the given range (in text coords) in this line
318 void RefreshColRange(wxTextCoord line
, wxTextPos start
, size_t count
);
320 // refresh the text from in the given line range (inclusive)
321 void RefreshLineRange(wxTextCoord lineFirst
, wxTextCoord lineLast
);
323 // refresh the text in the given range which can span multiple lines
324 // (this method accepts arguments in any order)
325 void RefreshTextRange(wxTextPos start
, wxTextPos end
);
327 // get the text to show: either the text itself or the text replaced with
328 // starts for wxTE_PASSWORD control
329 wxString
GetTextToShow(const wxString
& text
) const;
331 // find the row in this line where the given position (counted from the
333 wxTextCoord
GetRowInLine(wxTextCoord line
,
335 wxTextCoord
*colRowStart
= NULL
) const;
337 // find the number of characters of a line before it wraps
338 // (and optionally also the real width of the line)
339 size_t GetPartOfWrappedLine(const wxChar
* text
,
340 wxCoord
*widthReal
= NULL
) const;
342 // get the start and end of the selection for this line: if the line is
343 // outside the selection, both will be -1 and false will be returned
344 bool GetSelectedPartOfLine(wxTextCoord line
,
345 wxTextPos
*start
, wxTextPos
*end
) const;
347 // update the text rect: the zone inside our client rect (its coords are
348 // client coords) which contains the text
349 void UpdateTextRect();
351 // calculate the last visible position
352 void UpdateLastVisible();
354 // move caret to the given position unconditionally
355 // (SetInsertionPoint() does nothing if the position didn't change)
356 void DoSetInsertionPoint(wxTextPos pos
);
358 // move caret to the new position without updating the display (for
359 // internal use only)
360 void MoveInsertionPoint(wxTextPos pos
);
362 // set the caret to its initial (default) position
363 void InitInsertionPoint();
365 // get the width of the longest line in pixels
366 wxCoord
GetMaxWidth() const;
368 // force recalculation of the max line width
369 void RecalcMaxWidth();
371 // update the max width after the given line was modified
372 void UpdateMaxWidth(wxTextCoord line
);
377 // HitTest2() is more efficient than 2 consecutive HitTest()s with the same
378 // line (i.e. y) and it also returns the offset of the starting position in
381 // as the last hack, this function accepts either logical or device (by
382 // default) coords depending on devCoords flag
383 wxTextCtrlHitTestResult
HitTest2(wxCoord y
,
387 wxTextCoord
*colStart
,
389 wxTextCoord
*colRowStart
,
390 bool devCoords
= true) const;
392 // HitTest() version which takes the logical text coordinates and not the
394 wxTextCtrlHitTestResult
HitTestLogical(const wxPoint
& pos
,
396 wxTextCoord
*row
) const;
398 // get the line and the row in this line corresponding to the given row,
399 // return true if ok and false if row is out of range
401 // NB: this function can only be called for controls which wrap lines
402 bool GetLineAndRow(wxTextCoord row
,
404 wxTextCoord
*rowInLine
) const;
406 // get the height of one line (the same for all lines)
407 wxCoord
GetLineHeight() const
409 // this one should be already precalculated
410 wxASSERT_MSG( m_heightLine
!= -1, wxT("should have line height") );
415 // get the average char width
416 wxCoord
GetAverageWidth() const { return m_widthAvg
; }
418 // recalc the line height and char width (to call when the font changes)
419 void RecalcFontMetrics();
421 // vertical scrolling helpers
422 // --------------------------
424 // all these functions are for multi line controls only
426 // get the number of visible lines
427 size_t GetLinesPerPage() const;
429 // return the position above the cursor or INVALID_POS_VALUE
430 wxTextPos
GetPositionAbove();
432 // return the position below the cursor or INVALID_POS_VALUE
433 wxTextPos
GetPositionBelow();
437 void OnChar(wxKeyEvent
& event
);
438 void OnSize(wxSizeEvent
& event
);
440 // return the struct containing control-type dependent data
441 struct wxTextSingleLineData
& SData() { return *m_data
.sdata
; }
442 struct wxTextMultiLineData
& MData() { return *m_data
.mdata
; }
443 struct wxTextWrappedData
& WData() { return *m_data
.wdata
; }
444 const wxTextSingleLineData
& SData() const { return *m_data
.sdata
; }
445 const wxTextMultiLineData
& MData() const { return *m_data
.mdata
; }
446 const wxTextWrappedData
& WData() const { return *m_data
.wdata
; }
448 // clipboard operations (unlike the versions without Do prefix, they have a
454 // all these methods are for multiline text controls only
456 // update the scrollbars (only called from OnIdle)
457 void UpdateScrollbars();
459 // get read only access to the lines of multiline control
460 inline const wxArrayString
& GetLines() const;
461 inline size_t GetLineCount() const;
463 // replace a line (returns true if the number of rows in thel ine changed)
464 bool ReplaceLine(wxTextCoord line
, const wxString
& text
);
467 void RemoveLine(wxTextCoord line
);
469 // insert a line at this position
470 void InsertLine(wxTextCoord line
, const wxString
& text
);
472 // calculate geometry of this line
473 void LayoutLine(wxTextCoord line
, class wxWrappedLineData
& lineData
) const;
475 // calculate geometry of all lines until the given one
476 void LayoutLines(wxTextCoord lineLast
) const;
478 // the initially specified control size
479 wxSize m_sizeInitial
;
481 // the global control text
486 wxTextCoord m_curCol
,
489 // last position (only used by GetLastPosition())
493 wxTextPos m_selAnchor
,
501 m_wrapLines
:1; // can't be changed after creation
503 // the rectangle (in client coordinates) to draw text inside
506 // the height of one line (cached value of GetCharHeight)
507 wxCoord m_heightLine
;
509 // and the average char width (cached value of GetCharWidth)
512 // we have some data which depends on the kind of control (single or multi
516 wxTextSingleLineData
*sdata
;
517 wxTextMultiLineData
*mdata
;
518 wxTextWrappedData
*wdata
;
522 // the object to which we delegate our undo/redo implementation
523 wxTextCtrlCommandProcessor
*m_cmdProcessor
;
525 DECLARE_EVENT_TABLE()
526 DECLARE_DYNAMIC_CLASS(wxTextCtrl
)
528 friend class wxWrappedLineData
;
531 #endif // _WX_UNIV_TEXTCTRL_H_