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