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
, long *pos
) const;
195 virtual wxTextCtrlHitTestResult
HitTest(const wxPoint
& pt
,
197 wxTextCoord
*row
) const;
199 // find the character at this position in the given line, return value as
202 // NB: x is the logical coord (client and unscrolled)
203 wxTextCtrlHitTestResult
HitTestLine(const wxString
& line
,
205 wxTextCoord
*colOut
) const;
207 // bring the given position into view
208 void ShowHorzPosition(wxCoord pos
);
210 // scroll the window horizontally so that the first character shown is in
212 void ScrollText(wxTextCoord col
);
214 // adjust the DC for horz text control scrolling too
215 virtual void DoPrepareDC(wxDC
& dc
);
217 // implementation only from now on
218 // -------------------------------
220 // override this to take into account our scrollbar-less scrolling
221 virtual void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
222 virtual void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
224 // ensure we have correct default border
225 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_SUNKEN
; }
228 virtual bool PerformAction(const wxControlAction
& action
,
230 const wxString
& strArg
= wxEmptyString
);
232 // override these methods to handle the caret
233 virtual bool SetFont(const wxFont
&font
);
234 virtual bool Enable(bool enable
= true);
236 // more readable flag testing methods
237 bool IsPassword() const { return (GetWindowStyle() & wxTE_PASSWORD
) != 0; }
238 bool WrapLines() const
239 { return !IsSingleLine() && !(GetWindowStyle() & wxHSCROLL
); }
241 // only for wxStdTextCtrlInputHandler
242 void RefreshSelection();
245 // override base class methods
246 virtual void DoDrawBorder(wxDC
& dc
, const wxRect
& rect
);
247 virtual void DoDraw(wxControlRenderer
*renderer
);
249 // calc the size from the text extent
250 virtual wxSize
DoGetBestClientSize() const;
252 // common part of all ctors
258 // draw the text in the given rectangle
259 void DoDrawTextInRect(wxDC
& dc
, const wxRect
& rectUpdate
);
261 // draw the line wrap marks in this rect
262 void DoDrawLineWrapMarks(wxDC
& dc
, const wxRect
& rectUpdate
);
264 // line/row geometry calculations
265 // ------------------------------
267 // get the extent (width) of the text
268 wxCoord
GetTextWidth(const wxString
& text
) const;
270 // get the logical text width (accounting for scrolling)
271 wxCoord
GetTotalWidth() const;
273 // get total number of rows (different from number of lines if the lines
275 wxTextCoord
GetRowCount() const;
277 // find the number of rows in this line (only if WrapLines())
278 wxTextCoord
GetRowsPerLine(wxTextCoord line
) const;
280 // get the starting row of the given line
281 wxTextCoord
GetFirstRowOfLine(wxTextCoord line
) const;
283 // get the row following this line
284 wxTextCoord
GetRowAfterLine(wxTextCoord line
) const;
289 // the text area is the part of the window in which the text can be
290 // displayed, i.e. part of it inside the margins and the real text area is
291 // the area in which the text *is* currently displayed: for example, in the
292 // multiline control case the text area can have extra space at the bottom
293 // which is not tall enough for another line and which is then not included
294 // into the real text area
295 wxRect
GetRealTextArea() const;
297 // refresh the text in the given (in logical coords) rect
298 void RefreshTextRect(const wxRect
& rect
, bool textOnly
= true);
300 // refresh the line wrap marks for the given range of lines (inclusive)
301 void RefreshLineWrapMarks(wxTextCoord rowFirst
, wxTextCoord rowLast
);
303 // refresh the text in the given range (in logical coords) of this line, if
304 // width is 0, refresh to the end of line
305 void RefreshPixelRange(wxTextCoord line
, wxCoord start
, wxCoord width
);
307 // refresh the text in the given range (in text coords) in this line
308 void RefreshColRange(wxTextCoord line
, wxTextPos start
, size_t count
);
310 // refresh the text from in the given line range (inclusive)
311 void RefreshLineRange(wxTextCoord lineFirst
, wxTextCoord lineLast
);
313 // refresh the text in the given range which can span multiple lines
314 // (this method accepts arguments in any order)
315 void RefreshTextRange(wxTextPos start
, wxTextPos end
);
317 // get the text to show: either the text itself or the text replaced with
318 // starts for wxTE_PASSWORD control
319 wxString
GetTextToShow(const wxString
& text
) const;
321 // find the row in this line where the given position (counted from the
323 wxTextCoord
GetRowInLine(wxTextCoord line
,
325 wxTextCoord
*colRowStart
= NULL
) const;
327 // find the number of characters of a line before it wraps
328 // (and optionally also the real width of the line)
329 size_t GetPartOfWrappedLine(const wxChar
* text
,
330 wxCoord
*widthReal
= NULL
) const;
332 // get the start and end of the selection for this line: if the line is
333 // outside the selection, both will be -1 and false will be returned
334 bool GetSelectedPartOfLine(wxTextCoord line
,
335 wxTextPos
*start
, wxTextPos
*end
) const;
337 // update the text rect: the zone inside our client rect (its coords are
338 // client coords) which contains the text
339 void UpdateTextRect();
341 // calculate the last visible position
342 void UpdateLastVisible();
344 // move caret to the given position unconditionally
345 // (SetInsertionPoint() does nothing if the position didn't change)
346 void DoSetInsertionPoint(wxTextPos pos
);
348 // move caret to the new position without updating the display (for
349 // internal use only)
350 void MoveInsertionPoint(wxTextPos pos
);
352 // set the caret to its initial (default) position
353 void InitInsertionPoint();
355 // get the width of the longest line in pixels
356 wxCoord
GetMaxWidth() const;
358 // force recalculation of the max line width
359 void RecalcMaxWidth();
361 // update the max width after the given line was modified
362 void UpdateMaxWidth(wxTextCoord line
);
367 // HitTest2() is more efficient than 2 consecutive HitTest()s with the same
368 // line (i.e. y) and it also returns the offset of the starting position in
371 // as the last hack, this function accepts either logical or device (by
372 // default) coords depending on devCoords flag
373 wxTextCtrlHitTestResult
HitTest2(wxCoord y
,
377 wxTextCoord
*colStart
,
379 wxTextCoord
*colRowStart
,
380 bool devCoords
= true) const;
382 // HitTest() version which takes the logical text coordinates and not the
384 wxTextCtrlHitTestResult
HitTestLogical(const wxPoint
& pos
,
386 wxTextCoord
*row
) const;
388 // get the line and the row in this line corresponding to the given row,
389 // return true if ok and false if row is out of range
391 // NB: this function can only be called for controls which wrap lines
392 bool GetLineAndRow(wxTextCoord row
,
394 wxTextCoord
*rowInLine
) const;
396 // get the height of one line (the same for all lines)
397 wxCoord
GetLineHeight() const
399 // this one should be already precalculated
400 wxASSERT_MSG( m_heightLine
!= -1, _T("should have line height") );
405 // get the average char width
406 wxCoord
GetAverageWidth() const { return m_widthAvg
; }
408 // recalc the line height and char width (to call when the font changes)
409 void RecalcFontMetrics();
411 // vertical scrolling helpers
412 // --------------------------
414 // all these functions are for multi line controls only
416 // get the number of visible lines
417 size_t GetLinesPerPage() const;
419 // return the position above the cursor or INVALID_POS_VALUE
420 wxTextPos
GetPositionAbove();
422 // return the position below the cursor or INVALID_POS_VALUE
423 wxTextPos
GetPositionBelow();
427 void OnChar(wxKeyEvent
& event
);
428 void OnSize(wxSizeEvent
& event
);
430 // overrdie wxScrollHelper method to prevent (auto)scrolling beyond the end
432 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
434 // return the struct containing control-type dependent data
435 struct wxTextSingleLineData
& SData() { return *m_data
.sdata
; }
436 struct wxTextMultiLineData
& MData() { return *m_data
.mdata
; }
437 struct wxTextWrappedData
& WData() { return *m_data
.wdata
; }
438 const wxTextSingleLineData
& SData() const { return *m_data
.sdata
; }
439 const wxTextMultiLineData
& MData() const { return *m_data
.mdata
; }
440 const wxTextWrappedData
& WData() const { return *m_data
.wdata
; }
442 // clipboard operations (unlike the versions without Do prefix, they have a
448 virtual void OnInternalIdle();
450 // all these methods are for multiline text controls only
452 // update the scrollbars (only called from OnIdle)
453 void UpdateScrollbars();
455 // get read only access to the lines of multiline control
456 inline const wxArrayString
& GetLines() const;
457 inline size_t GetLineCount() const;
459 // replace a line (returns true if the number of rows in thel ine changed)
460 bool ReplaceLine(wxTextCoord line
, const wxString
& text
);
463 void RemoveLine(wxTextCoord line
);
465 // insert a line at this position
466 void InsertLine(wxTextCoord line
, const wxString
& text
);
468 // calculate geometry of this line
469 void LayoutLine(wxTextCoord line
, class wxWrappedLineData
& lineData
) const;
471 // calculate geometry of all lines until the given one
472 void LayoutLines(wxTextCoord lineLast
) const;
474 // the initially specified control size
475 wxSize m_sizeInitial
;
477 // the global control text
482 wxTextCoord m_curCol
,
485 // last position (only used by GetLastPosition())
489 wxTextPos m_selAnchor
,
498 // the rectangle (in client coordinates) to draw text inside
501 // the height of one line (cached value of GetCharHeight)
502 wxCoord m_heightLine
;
504 // and the average char width (cached value of GetCharWidth)
507 // we have some data which depends on the kind of control (single or multi
511 wxTextSingleLineData
*sdata
;
512 wxTextMultiLineData
*mdata
;
513 wxTextWrappedData
*wdata
;
517 // the object to which we delegate our undo/redo implementation
518 wxTextCtrlCommandProcessor
*m_cmdProcessor
;
520 DECLARE_EVENT_TABLE()
521 DECLARE_DYNAMIC_CLASS(wxTextCtrl
)
524 // ----------------------------------------------------------------------------
525 // wxStdTextCtrlInputHandler: this control handles only the mouse/kbd actions
526 // common to Win32 and GTK, platform-specific things are implemented elsewhere
527 // ----------------------------------------------------------------------------
529 class WXDLLEXPORT wxStdTextCtrlInputHandler
: public wxStdInputHandler
532 wxStdTextCtrlInputHandler(wxInputHandler
*inphand
);
534 virtual bool HandleKey(wxInputConsumer
*consumer
,
535 const wxKeyEvent
& event
,
537 virtual bool HandleMouse(wxInputConsumer
*consumer
,
538 const wxMouseEvent
& event
);
539 virtual bool HandleMouseMove(wxInputConsumer
*consumer
,
540 const wxMouseEvent
& event
);
541 virtual bool HandleFocus(wxInputConsumer
*consumer
, const wxFocusEvent
& event
);
544 // get the position of the mouse click
545 static wxTextPos
HitTest(const wxTextCtrl
*text
, const wxPoint
& pos
);
548 wxTextCtrl
*m_winCapture
;
551 #endif // _WX_UNIV_TEXTCTRL_H_