1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/textctrl.h
3 // Purpose: wxTextCtrl class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_UNIV_TEXTCTRL_H_
13 #define _WX_UNIV_TEXTCTRL_H_
15 class WXDLLEXPORT wxCaret
;
16 class WXDLLEXPORT wxTextCtrlCommandProcessor
;
18 #include "wx/scrolwin.h" // for wxScrollHelper
20 #include "wx/univ/inphand.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
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")
41 // clipboard operations
42 #define wxACTION_TEXT_COPY _T("copy")
43 #define wxACTION_TEXT_CUT _T("cut")
44 #define wxACTION_TEXT_PASTE _T("paste")
46 // insert text at the cursor position: the text is in strArg of PerformAction
47 #define wxACTION_TEXT_INSERT _T("insert")
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")
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")
62 #define wxACTION_TEXT_UNDO _T("undo")
63 #define wxACTION_TEXT_REDO _T("redo")
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 class WXDLLEXPORT wxTextCtrl
: public wxTextCtrlBase
,
76 wxTextCtrl() : wxScrollHelper(this) { Init(); }
78 wxTextCtrl(wxWindow
*parent
,
80 const wxString
& value
= wxEmptyString
,
81 const wxPoint
& pos
= wxDefaultPosition
,
82 const wxSize
& size
= wxDefaultSize
,
84 const wxValidator
& validator
= wxDefaultValidator
,
85 const wxString
& name
= wxTextCtrlNameStr
)
86 : wxScrollHelper(this)
90 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
93 bool Create(wxWindow
*parent
,
95 const wxString
& value
= wxEmptyString
,
96 const wxPoint
& pos
= wxDefaultPosition
,
97 const wxSize
& size
= wxDefaultSize
,
99 const wxValidator
& validator
= wxDefaultValidator
,
100 const wxString
& name
= wxTextCtrlNameStr
);
102 virtual ~wxTextCtrl();
104 // implement base class pure virtuals
105 // ----------------------------------
107 virtual wxString
GetValue() const;
108 virtual void SetValue(const wxString
& value
);
110 virtual int GetLineLength(wxTextCoord lineNo
) const;
111 virtual wxString
GetLineText(wxTextCoord lineNo
) const;
112 virtual int GetNumberOfLines() const;
114 virtual bool IsModified() const;
115 virtual bool IsEditable() const;
117 // If the return values from and to are the same, there is no selection.
118 virtual void GetSelection(wxTextPos
* from
, wxTextPos
* to
) const;
124 virtual void Clear();
125 virtual void Replace(wxTextPos from
, wxTextPos to
, const wxString
& value
);
126 virtual void Remove(wxTextPos from
, wxTextPos to
);
128 // sets/clears the dirty flag
129 virtual void MarkDirty();
130 virtual void DiscardEdits();
132 // writing text inserts it at the current position, appending always
133 // inserts it at the end
134 virtual void WriteText(const wxString
& text
);
135 virtual void AppendText(const wxString
& text
);
137 // translate between the position (which is just an index in the text ctrl
138 // considering all its contents as a single strings) and (x, y) coordinates
139 // which represent (logical, i.e. unwrapped) column and line.
140 virtual wxTextPos
XYToPosition(wxTextCoord x
, wxTextCoord y
) const;
141 virtual bool PositionToXY(wxTextPos pos
,
142 wxTextCoord
*x
, wxTextCoord
*y
) const;
144 // wxUniv-specific: find a screen position (in client coordinates) of the
145 // given text position or of the caret
146 bool PositionToLogicalXY(wxTextPos pos
, wxCoord
*x
, wxCoord
*y
) const;
147 bool PositionToDeviceXY(wxTextPos pos
, wxCoord
*x
, wxCoord
*y
) const;
148 wxPoint
GetCaretPosition() const;
150 virtual void ShowPosition(wxTextPos pos
);
152 // Clipboard operations
155 virtual void Paste();
161 virtual bool CanUndo() const;
162 virtual bool CanRedo() const;
165 virtual void SetInsertionPoint(wxTextPos pos
);
166 virtual void SetInsertionPointEnd();
167 virtual wxTextPos
GetInsertionPoint() const;
168 virtual wxTextPos
GetLastPosition() const;
170 virtual void SetSelection(wxTextPos from
, wxTextPos to
);
171 virtual void SetEditable(bool editable
);
173 // wxUniv-specific methods
174 // -----------------------
177 virtual void ShowCaret(bool show
= true);
178 void HideCaret() { ShowCaret(false); }
179 void CreateCaret(); // for the current font size
181 // helpers for cursor movement
182 wxTextPos
GetWordStart() const;
183 wxTextPos
GetWordEnd() const;
186 bool HasSelection() const
187 { return m_selStart
!= -1 && m_selEnd
> m_selStart
; }
188 void ClearSelection();
189 void RemoveSelection();
190 wxString
GetSelectionText() const;
192 virtual wxTextCtrlHitTestResult
HitTest(const wxPoint
& pt
, long *pos
) const;
193 virtual wxTextCtrlHitTestResult
HitTest(const wxPoint
& pt
,
195 wxTextCoord
*row
) const;
197 // find the character at this position in the given line, return value as
200 // NB: x is the logical coord (client and unscrolled)
201 wxTextCtrlHitTestResult
HitTestLine(const wxString
& line
,
203 wxTextCoord
*colOut
) const;
205 // bring the given position into view
206 void ShowHorzPosition(wxCoord pos
);
208 // scroll the window horizontally so that the first character shown is in
210 void ScrollText(wxTextCoord col
);
212 // adjust the DC for horz text control scrolling too
213 virtual void DoPrepareDC(wxDC
& dc
);
215 // implementation only from now on
216 // -------------------------------
218 // override this to take into account our scrollbar-less scrolling
219 virtual void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
220 virtual void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
223 virtual bool PerformAction(const wxControlAction
& action
,
225 const wxString
& strArg
= wxEmptyString
);
227 // override these methods to handle the caret
228 virtual bool SetFont(const wxFont
&font
);
229 virtual bool Enable(bool enable
= true);
231 // more readable flag testing methods
232 bool IsPassword() const { return (GetWindowStyle() & wxTE_PASSWORD
) != 0; }
233 bool WrapLines() const
234 { return !IsSingleLine() && !(GetWindowStyle() & wxHSCROLL
); }
236 // only for wxStdTextCtrlInputHandler
237 void RefreshSelection();
239 // override wxScrollHelper method to prevent (auto)scrolling beyond the end
241 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
244 virtual void OnInternalIdle();
247 // ensure we have correct default border
248 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_SUNKEN
; }
250 // override base class methods
251 virtual void DoDrawBorder(wxDC
& dc
, const wxRect
& rect
);
252 virtual void DoDraw(wxControlRenderer
*renderer
);
254 // calc the size from the text extent
255 virtual wxSize
DoGetBestClientSize() const;
257 // common part of all ctors
263 // draw the text in the given rectangle
264 void DoDrawTextInRect(wxDC
& dc
, const wxRect
& rectUpdate
);
266 // draw the line wrap marks in this rect
267 void DoDrawLineWrapMarks(wxDC
& dc
, const wxRect
& rectUpdate
);
269 // line/row geometry calculations
270 // ------------------------------
272 // get the extent (width) of the text
273 wxCoord
GetTextWidth(const wxString
& text
) const;
275 // get the logical text width (accounting for scrolling)
276 wxCoord
GetTotalWidth() const;
278 // get total number of rows (different from number of lines if the lines
280 wxTextCoord
GetRowCount() const;
282 // find the number of rows in this line (only if WrapLines())
283 wxTextCoord
GetRowsPerLine(wxTextCoord line
) const;
285 // get the starting row of the given line
286 wxTextCoord
GetFirstRowOfLine(wxTextCoord line
) const;
288 // get the row following this line
289 wxTextCoord
GetRowAfterLine(wxTextCoord line
) const;
294 // the text area is the part of the window in which the text can be
295 // displayed, i.e. part of it inside the margins and the real text area is
296 // the area in which the text *is* currently displayed: for example, in the
297 // multiline control case the text area can have extra space at the bottom
298 // which is not tall enough for another line and which is then not included
299 // into the real text area
300 wxRect
GetRealTextArea() const;
302 // refresh the text in the given (in logical coords) rect
303 void RefreshTextRect(const wxRect
& rect
, bool textOnly
= true);
305 // refresh the line wrap marks for the given range of lines (inclusive)
306 void RefreshLineWrapMarks(wxTextCoord rowFirst
, wxTextCoord rowLast
);
308 // refresh the text in the given range (in logical coords) of this line, if
309 // width is 0, refresh to the end of line
310 void RefreshPixelRange(wxTextCoord line
, wxCoord start
, wxCoord width
);
312 // refresh the text in the given range (in text coords) in this line
313 void RefreshColRange(wxTextCoord line
, wxTextPos start
, size_t count
);
315 // refresh the text from in the given line range (inclusive)
316 void RefreshLineRange(wxTextCoord lineFirst
, wxTextCoord lineLast
);
318 // refresh the text in the given range which can span multiple lines
319 // (this method accepts arguments in any order)
320 void RefreshTextRange(wxTextPos start
, wxTextPos end
);
322 // get the text to show: either the text itself or the text replaced with
323 // starts for wxTE_PASSWORD control
324 wxString
GetTextToShow(const wxString
& text
) const;
326 // find the row in this line where the given position (counted from the
328 wxTextCoord
GetRowInLine(wxTextCoord line
,
330 wxTextCoord
*colRowStart
= NULL
) const;
332 // find the number of characters of a line before it wraps
333 // (and optionally also the real width of the line)
334 size_t GetPartOfWrappedLine(const wxChar
* text
,
335 wxCoord
*widthReal
= NULL
) const;
337 // get the start and end of the selection for this line: if the line is
338 // outside the selection, both will be -1 and false will be returned
339 bool GetSelectedPartOfLine(wxTextCoord line
,
340 wxTextPos
*start
, wxTextPos
*end
) const;
342 // update the text rect: the zone inside our client rect (its coords are
343 // client coords) which contains the text
344 void UpdateTextRect();
346 // calculate the last visible position
347 void UpdateLastVisible();
349 // move caret to the given position unconditionally
350 // (SetInsertionPoint() does nothing if the position didn't change)
351 void DoSetInsertionPoint(wxTextPos pos
);
353 // move caret to the new position without updating the display (for
354 // internal use only)
355 void MoveInsertionPoint(wxTextPos pos
);
357 // set the caret to its initial (default) position
358 void InitInsertionPoint();
360 // get the width of the longest line in pixels
361 wxCoord
GetMaxWidth() const;
363 // force recalculation of the max line width
364 void RecalcMaxWidth();
366 // update the max width after the given line was modified
367 void UpdateMaxWidth(wxTextCoord line
);
372 // HitTest2() is more efficient than 2 consecutive HitTest()s with the same
373 // line (i.e. y) and it also returns the offset of the starting position in
376 // as the last hack, this function accepts either logical or device (by
377 // default) coords depending on devCoords flag
378 wxTextCtrlHitTestResult
HitTest2(wxCoord y
,
382 wxTextCoord
*colStart
,
384 wxTextCoord
*colRowStart
,
385 bool devCoords
= true) const;
387 // HitTest() version which takes the logical text coordinates and not the
389 wxTextCtrlHitTestResult
HitTestLogical(const wxPoint
& pos
,
391 wxTextCoord
*row
) const;
393 // get the line and the row in this line corresponding to the given row,
394 // return true if ok and false if row is out of range
396 // NB: this function can only be called for controls which wrap lines
397 bool GetLineAndRow(wxTextCoord row
,
399 wxTextCoord
*rowInLine
) const;
401 // get the height of one line (the same for all lines)
402 wxCoord
GetLineHeight() const
404 // this one should be already precalculated
405 wxASSERT_MSG( m_heightLine
!= -1, _T("should have line height") );
410 // get the average char width
411 wxCoord
GetAverageWidth() const { return m_widthAvg
; }
413 // recalc the line height and char width (to call when the font changes)
414 void RecalcFontMetrics();
416 // vertical scrolling helpers
417 // --------------------------
419 // all these functions are for multi line controls only
421 // get the number of visible lines
422 size_t GetLinesPerPage() const;
424 // return the position above the cursor or INVALID_POS_VALUE
425 wxTextPos
GetPositionAbove();
427 // return the position below the cursor or INVALID_POS_VALUE
428 wxTextPos
GetPositionBelow();
432 void OnChar(wxKeyEvent
& event
);
433 void OnSize(wxSizeEvent
& event
);
435 // return the struct containing control-type dependent data
436 struct wxTextSingleLineData
& SData() { return *m_data
.sdata
; }
437 struct wxTextMultiLineData
& MData() { return *m_data
.mdata
; }
438 struct wxTextWrappedData
& WData() { return *m_data
.wdata
; }
439 const wxTextSingleLineData
& SData() const { return *m_data
.sdata
; }
440 const wxTextMultiLineData
& MData() const { return *m_data
.mdata
; }
441 const wxTextWrappedData
& WData() const { return *m_data
.wdata
; }
443 // clipboard operations (unlike the versions without Do prefix, they have a
449 // all these methods are for multiline text controls only
451 // update the scrollbars (only called from OnIdle)
452 void UpdateScrollbars();
454 // get read only access to the lines of multiline control
455 inline const wxArrayString
& GetLines() const;
456 inline size_t GetLineCount() const;
458 // replace a line (returns true if the number of rows in thel ine changed)
459 bool ReplaceLine(wxTextCoord line
, const wxString
& text
);
462 void RemoveLine(wxTextCoord line
);
464 // insert a line at this position
465 void InsertLine(wxTextCoord line
, const wxString
& text
);
467 // calculate geometry of this line
468 void LayoutLine(wxTextCoord line
, class wxWrappedLineData
& lineData
) const;
470 // calculate geometry of all lines until the given one
471 void LayoutLines(wxTextCoord lineLast
) const;
473 // the initially specified control size
474 wxSize m_sizeInitial
;
476 // the global control text
481 wxTextCoord m_curCol
,
484 // last position (only used by GetLastPosition())
488 wxTextPos m_selAnchor
,
497 // the rectangle (in client coordinates) to draw text inside
500 // the height of one line (cached value of GetCharHeight)
501 wxCoord m_heightLine
;
503 // and the average char width (cached value of GetCharWidth)
506 // we have some data which depends on the kind of control (single or multi
510 wxTextSingleLineData
*sdata
;
511 wxTextMultiLineData
*mdata
;
512 wxTextWrappedData
*wdata
;
516 // the object to which we delegate our undo/redo implementation
517 wxTextCtrlCommandProcessor
*m_cmdProcessor
;
519 DECLARE_EVENT_TABLE()
520 DECLARE_DYNAMIC_CLASS(wxTextCtrl
)
522 friend class wxWrappedLineData
;
525 // ----------------------------------------------------------------------------
526 // wxStdTextCtrlInputHandler: this control handles only the mouse/kbd actions
527 // common to Win32 and GTK, platform-specific things are implemented elsewhere
528 // ----------------------------------------------------------------------------
530 class WXDLLEXPORT wxStdTextCtrlInputHandler
: public wxStdInputHandler
533 wxStdTextCtrlInputHandler(wxInputHandler
*inphand
);
535 virtual bool HandleKey(wxInputConsumer
*consumer
,
536 const wxKeyEvent
& event
,
538 virtual bool HandleMouse(wxInputConsumer
*consumer
,
539 const wxMouseEvent
& event
);
540 virtual bool HandleMouseMove(wxInputConsumer
*consumer
,
541 const wxMouseEvent
& event
);
542 virtual bool HandleFocus(wxInputConsumer
*consumer
, const wxFocusEvent
& event
);
545 // get the position of the mouse click
546 static wxTextPos
HitTest(const wxTextCtrl
*text
, const wxPoint
& pos
);
549 wxTextCtrl
*m_winCapture
;
552 #endif // _WX_UNIV_TEXTCTRL_H_