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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "univtextctrl.h"
19 class WXDLLEXPORT wxCaret
;
20 class WXDLLEXPORT wxTextCtrlCommandProcessor
;
22 #include "wx/scrolwin.h" // for wxScrollHelper
24 #include "wx/univ/inphand.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 // cursor movement and also selection and delete operations
31 #define wxACTION_TEXT_GOTO _T("goto") // to pos in numArg
32 #define wxACTION_TEXT_FIRST _T("first") // go to pos 0
33 #define wxACTION_TEXT_LAST _T("last") // go to last pos
34 #define wxACTION_TEXT_HOME _T("home")
35 #define wxACTION_TEXT_END _T("end")
36 #define wxACTION_TEXT_LEFT _T("left")
37 #define wxACTION_TEXT_RIGHT _T("right")
38 #define wxACTION_TEXT_UP _T("up")
39 #define wxACTION_TEXT_DOWN _T("down")
40 #define wxACTION_TEXT_WORD_LEFT _T("wordleft")
41 #define wxACTION_TEXT_WORD_RIGHT _T("wordright")
42 #define wxACTION_TEXT_PAGE_UP _T("pageup")
43 #define wxACTION_TEXT_PAGE_DOWN _T("pagedown")
45 // clipboard operations
46 #define wxACTION_TEXT_COPY _T("copy")
47 #define wxACTION_TEXT_CUT _T("cut")
48 #define wxACTION_TEXT_PASTE _T("paste")
50 // insert text at the cursor position: the text is in strArg of PerformAction
51 #define wxACTION_TEXT_INSERT _T("insert")
53 // if the action starts with either of these prefixes and the rest of the
54 // string is one of the movement commands, it means to select/delete text from
55 // the current cursor position to the new one
56 #define wxACTION_TEXT_PREFIX_SEL _T("sel")
57 #define wxACTION_TEXT_PREFIX_DEL _T("del")
60 #define wxACTION_TEXT_ANCHOR_SEL _T("anchorsel")
61 #define wxACTION_TEXT_EXTEND_SEL _T("extendsel")
62 #define wxACTION_TEXT_SEL_WORD _T("wordsel")
63 #define wxACTION_TEXT_SEL_LINE _T("linesel")
66 #define wxACTION_TEXT_UNDO _T("undo")
67 #define wxACTION_TEXT_REDO _T("redo")
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class WXDLLEXPORT wxTextCtrl
: public wxTextCtrlBase
, public wxScrollHelper
79 wxTextCtrl() { Init(); }
81 wxTextCtrl(wxWindow
*parent
,
83 const wxString
& value
= wxEmptyString
,
84 const wxPoint
& pos
= wxDefaultPosition
,
85 const wxSize
& size
= wxDefaultSize
,
87 const wxValidator
& validator
= wxDefaultValidator
,
88 const wxString
& name
= wxTextCtrlNameStr
)
92 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
95 bool Create(wxWindow
*parent
,
97 const wxString
& value
= wxEmptyString
,
98 const wxPoint
& pos
= wxDefaultPosition
,
99 const wxSize
& size
= wxDefaultSize
,
101 const wxValidator
& validator
= wxDefaultValidator
,
102 const wxString
& name
= wxTextCtrlNameStr
);
104 virtual ~wxTextCtrl();
106 // implement base class pure virtuals
107 // ----------------------------------
109 virtual wxString
GetValue() const;
110 virtual void SetValue(const wxString
& value
);
112 virtual int GetLineLength(wxTextCoord lineNo
) const;
113 virtual wxString
GetLineText(wxTextCoord lineNo
) const;
114 virtual int GetNumberOfLines() const;
116 virtual bool IsModified() const;
117 virtual bool IsEditable() const;
119 // If the return values from and to are the same, there is no selection.
120 virtual void GetSelection(wxTextPos
* from
, wxTextPos
* to
) const;
126 virtual void Clear();
127 virtual void Replace(wxTextPos from
, wxTextPos to
, const wxString
& value
);
128 virtual void Remove(wxTextPos from
, wxTextPos to
);
130 // sets/clears the dirty flag
131 virtual void MarkDirty();
132 virtual void DiscardEdits();
134 // writing text inserts it at the current position, appending always
135 // inserts it at the end
136 virtual void WriteText(const wxString
& text
);
137 virtual void AppendText(const wxString
& text
);
139 // translate between the position (which is just an index in the text ctrl
140 // considering all its contents as a single strings) and (x, y) coordinates
141 // which represent (logical, i.e. unwrapped) column and line.
142 virtual wxTextPos
XYToPosition(wxTextCoord x
, wxTextCoord y
) const;
143 virtual bool PositionToXY(wxTextPos pos
,
144 wxTextCoord
*x
, wxTextCoord
*y
) const;
146 // wxUniv-specific: find a screen position (in client coordinates) of the
147 // given text position or of the caret
148 bool PositionToLogicalXY(wxTextPos pos
, wxCoord
*x
, wxCoord
*y
) const;
149 bool PositionToDeviceXY(wxTextPos pos
, wxCoord
*x
, wxCoord
*y
) const;
150 wxPoint
GetCaretPosition() const;
152 virtual void ShowPosition(wxTextPos pos
);
154 // Clipboard operations
157 virtual void Paste();
163 virtual bool CanUndo() const;
164 virtual bool CanRedo() const;
167 virtual void SetInsertionPoint(wxTextPos pos
);
168 virtual void SetInsertionPointEnd();
169 virtual wxTextPos
GetInsertionPoint() const;
170 virtual wxTextPos
GetLastPosition() const;
172 virtual void SetSelection(wxTextPos from
, wxTextPos to
);
173 virtual void SetEditable(bool editable
);
175 // wxUniv-specific methods
176 // -----------------------
179 virtual void ShowCaret(bool show
= TRUE
);
180 void HideCaret() { ShowCaret(FALSE
); }
181 void CreateCaret(); // for the current font size
183 // helpers for cursor movement
184 wxTextPos
GetWordStart() const;
185 wxTextPos
GetWordEnd() const;
188 bool HasSelection() const
189 { return m_selStart
!= -1 && m_selEnd
> m_selStart
; }
190 void ClearSelection();
191 void RemoveSelection();
192 wxString
GetSelectionText() const;
194 virtual wxTextCtrlHitTestResult
HitTest(const wxPoint
& pt
,
196 wxTextCoord
*row
) const;
198 // find the character at this position in the given line, return value as
201 // NB: x is the logical coord (client and unscrolled)
202 wxTextCtrlHitTestResult
HitTestLine(const wxString
& line
,
204 wxTextCoord
*colOut
) const;
206 // bring the given position into view
207 void ShowHorzPosition(wxCoord pos
);
209 // scroll the window horizontally so that the first character shown is in
211 void ScrollText(wxTextCoord col
);
213 // adjust the DC for horz text control scrolling too
214 virtual void DoPrepareDC(wxDC
& dc
);
216 // implementation only from now on
217 // -------------------------------
219 // override this to take into account our scrollbar-less scrolling
220 virtual void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
221 virtual void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
223 // ensure we have correct default border
224 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_SUNKEN
; }
227 virtual bool PerformAction(const wxControlAction
& action
,
229 const wxString
& strArg
= wxEmptyString
);
231 // override these methods to handle the caret
232 virtual bool SetFont(const wxFont
&font
);
233 virtual bool Enable(bool enable
= TRUE
);
235 // more readable flag testing methods
236 bool IsPassword() const { return (GetWindowStyle() & wxTE_PASSWORD
) != 0; }
237 bool WrapLines() const
238 { return !IsSingleLine() && !(GetWindowStyle() & wxHSCROLL
); }
240 // only for wxStdTextCtrlInputHandler
241 void RefreshSelection();
244 // override base class methods
245 virtual void DoDrawBorder(wxDC
& dc
, const wxRect
& rect
);
246 virtual void DoDraw(wxControlRenderer
*renderer
);
248 // calc the size from the text extent
249 virtual wxSize
DoGetBestClientSize() const;
251 // common part of all ctors
257 // draw the text in the given rectangle
258 void DoDrawTextInRect(wxDC
& dc
, const wxRect
& rectUpdate
);
260 // draw the line wrap marks in this rect
261 void DoDrawLineWrapMarks(wxDC
& dc
, const wxRect
& rectUpdate
);
263 // line/row geometry calculations
264 // ------------------------------
266 // get the extent (width) of the text
267 wxCoord
GetTextWidth(const wxString
& text
) const;
269 // get the logical text width (accounting for scrolling)
270 wxCoord
GetTotalWidth() const;
272 // get total number of rows (different from number of lines if the lines
274 wxTextCoord
GetRowCount() const;
276 // find the number of rows in this line (only if WrapLines())
277 wxTextCoord
GetRowsPerLine(wxTextCoord line
) const;
279 // get the starting row of the given line
280 wxTextCoord
GetFirstRowOfLine(wxTextCoord line
) const;
282 // get the row following this line
283 wxTextCoord
GetRowAfterLine(wxTextCoord line
) const;
288 // the text area is the part of the window in which the text can be
289 // displayed, i.e. part of it inside the margins and the real text area is
290 // the area in which the text *is* currently displayed: for example, in the
291 // multiline control case the text area can have extra space at the bottom
292 // which is not tall enough for another line and which is then not included
293 // into the real text area
294 wxRect
GetRealTextArea() const;
296 // refresh the text in the given (in logical coords) rect
297 void RefreshTextRect(const wxRect
& rect
, bool textOnly
= TRUE
);
299 // refresh the line wrap marks for the given range of lines (inclusive)
300 void RefreshLineWrapMarks(wxTextCoord rowFirst
, wxTextCoord rowLast
);
302 // refresh the text in the given range (in logical coords) of this line, if
303 // width is 0, refresh to the end of line
304 void RefreshPixelRange(wxTextCoord line
, wxCoord start
, wxCoord width
);
306 // refresh the text in the given range (in text coords) in this line
307 void RefreshColRange(wxTextCoord line
, wxTextPos start
, size_t count
);
309 // refresh the text from in the given line range (inclusive)
310 void RefreshLineRange(wxTextCoord lineFirst
, wxTextCoord lineLast
);
312 // refresh the text in the given range which can span multiple lines
313 // (this method accepts arguments in any order)
314 void RefreshTextRange(wxTextPos start
, wxTextPos end
);
316 // get the text to show: either the text itself or the text replaced with
317 // starts for wxTE_PASSWORD control
318 wxString
GetTextToShow(const wxString
& text
) const;
320 // find the row in this line where the given position (counted from the
322 wxTextCoord
GetRowInLine(wxTextCoord line
,
324 wxTextCoord
*colRowStart
= NULL
) const;
326 // find the number of characters of a line before it wraps
327 // (and optionally also the real width of the line)
328 size_t GetPartOfWrappedLine(const wxChar
* text
,
329 wxCoord
*widthReal
= NULL
) const;
331 // get the start and end of the selection for this line: if the line is
332 // outside the selection, both will be -1 and FALSE will be returned
333 bool GetSelectedPartOfLine(wxTextCoord line
,
334 wxTextPos
*start
, wxTextPos
*end
) const;
336 // update the text rect: the zone inside our client rect (its coords are
337 // client coords) which contains the text
338 void UpdateTextRect();
340 // calculate the last visible position
341 void UpdateLastVisible();
343 // move caret to the given position unconditionally
344 // (SetInsertionPoint() does nothing if the position didn't change)
345 void DoSetInsertionPoint(wxTextPos pos
);
347 // move caret to the new position without updating the display (for
348 // internal use only)
349 void MoveInsertionPoint(wxTextPos pos
);
351 // set the caret to its initial (default) position
352 void InitInsertionPoint();
354 // get the width of the longest line in pixels
355 wxCoord
GetMaxWidth() const;
357 // force recalculation of the max line width
358 void RecalcMaxWidth();
360 // update the max width after the given line was modified
361 void UpdateMaxWidth(wxTextCoord line
);
366 // HitTest2() is more efficient than 2 consecutive HitTest()s with the same
367 // line (i.e. y) and it also returns the offset of the starting position in
370 // as the last hack, this function accepts either logical or device (by
371 // default) coords depending on devCoords flag
372 wxTextCtrlHitTestResult
HitTest2(wxCoord y
,
376 wxTextCoord
*colStart
,
378 wxTextCoord
*colRowStart
,
379 bool devCoords
= TRUE
) const;
381 // HitTest() version which takes the logical text coordinates and not the
383 wxTextCtrlHitTestResult
HitTestLogical(const wxPoint
& pos
,
385 wxTextCoord
*row
) const;
387 // get the line and the row in this line corresponding to the given row,
388 // return TRUE if ok and FALSE if row is out of range
390 // NB: this function can only be called for controls which wrap lines
391 bool GetLineAndRow(wxTextCoord row
,
393 wxTextCoord
*rowInLine
) const;
395 // get the height of one line (the same for all lines)
396 wxCoord
GetLineHeight() const
398 // this one should be already precalculated
399 wxASSERT_MSG( m_heightLine
!= -1, _T("should have line height") );
404 // get the average char width
405 wxCoord
GetAverageWidth() const { return m_widthAvg
; }
407 // recalc the line height and char width (to call when the font changes)
408 void RecalcFontMetrics();
410 // vertical scrolling helpers
411 // --------------------------
413 // all these functions are for multi line controls only
415 // get the number of visible lines
416 size_t GetLinesPerPage() const;
418 // return the position above the cursor or INVALID_POS_VALUE
419 wxTextPos
GetPositionAbove();
421 // return the position below the cursor or INVALID_POS_VALUE
422 wxTextPos
GetPositionBelow();
426 void OnChar(wxKeyEvent
& event
);
427 void OnSize(wxSizeEvent
& event
);
429 // overrdie wxScrollHelper method to prevent (auto)scrolling beyond the end
431 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
433 // return the struct containing control-type dependent data
434 struct wxTextSingleLineData
& SData() { return *m_data
.sdata
; }
435 struct wxTextMultiLineData
& MData() { return *m_data
.mdata
; }
436 struct wxTextWrappedData
& WData() { return *m_data
.wdata
; }
437 const wxTextSingleLineData
& SData() const { return *m_data
.sdata
; }
438 const wxTextMultiLineData
& MData() const { return *m_data
.mdata
; }
439 const wxTextWrappedData
& WData() const { return *m_data
.wdata
; }
441 // clipboard operations (unlike the versions without Do prefix, they have a
447 virtual void OnInternalIdle();
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
)
523 // ----------------------------------------------------------------------------
524 // wxStdTextCtrlInputHandler: this control handles only the mouse/kbd actions
525 // common to Win32 and GTK, platform-specific things are implemented elsewhere
526 // ----------------------------------------------------------------------------
528 class WXDLLEXPORT wxStdTextCtrlInputHandler
: public wxStdInputHandler
531 wxStdTextCtrlInputHandler(wxInputHandler
*inphand
);
533 virtual bool HandleKey(wxInputConsumer
*consumer
,
534 const wxKeyEvent
& event
,
536 virtual bool HandleMouse(wxInputConsumer
*consumer
,
537 const wxMouseEvent
& event
);
538 virtual bool HandleMouseMove(wxInputConsumer
*consumer
,
539 const wxMouseEvent
& event
);
540 virtual bool HandleFocus(wxInputConsumer
*consumer
, const wxFocusEvent
& event
);
543 // get the position of the mouse click
544 static wxTextPos
HitTest(const wxTextCtrl
*text
, const wxPoint
& pos
);
547 wxTextCtrl
*m_winCapture
;
550 #endif // _WX_UNIV_TEXTCTRL_H_