]> git.saurik.com Git - wxWidgets.git/blob - include/wx/univ/textctrl.h
Always refresh the editor when setting property value to unspecified.
[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 WXDLLIMPEXP_FWD_CORE wxCaret;
16 class WXDLLIMPEXP_FWD_CORE 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 wxT("goto") // to pos in numArg
28 #define wxACTION_TEXT_FIRST wxT("first") // go to pos 0
29 #define wxACTION_TEXT_LAST wxT("last") // go to last pos
30 #define wxACTION_TEXT_HOME wxT("home")
31 #define wxACTION_TEXT_END wxT("end")
32 #define wxACTION_TEXT_LEFT wxT("left")
33 #define wxACTION_TEXT_RIGHT wxT("right")
34 #define wxACTION_TEXT_UP wxT("up")
35 #define wxACTION_TEXT_DOWN wxT("down")
36 #define wxACTION_TEXT_WORD_LEFT wxT("wordleft")
37 #define wxACTION_TEXT_WORD_RIGHT wxT("wordright")
38 #define wxACTION_TEXT_PAGE_UP wxT("pageup")
39 #define wxACTION_TEXT_PAGE_DOWN wxT("pagedown")
40
41 // clipboard operations
42 #define wxACTION_TEXT_COPY wxT("copy")
43 #define wxACTION_TEXT_CUT wxT("cut")
44 #define wxACTION_TEXT_PASTE wxT("paste")
45
46 // insert text at the cursor position: the text is in strArg of PerformAction
47 #define wxACTION_TEXT_INSERT wxT("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 wxT("sel")
53 #define wxACTION_TEXT_PREFIX_DEL wxT("del")
54
55 // mouse selection
56 #define wxACTION_TEXT_ANCHOR_SEL wxT("anchorsel")
57 #define wxACTION_TEXT_EXTEND_SEL wxT("extendsel")
58 #define wxACTION_TEXT_SEL_WORD wxT("wordsel")
59 #define wxACTION_TEXT_SEL_LINE wxT("linesel")
60
61 // undo or redo
62 #define wxACTION_TEXT_UNDO wxT("undo")
63 #define wxACTION_TEXT_REDO wxT("redo")
64
65 // ----------------------------------------------------------------------------
66 // wxTextCtrl
67 // ----------------------------------------------------------------------------
68
69 class WXDLLIMPEXP_CORE 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 int GetLineLength(wxTextCoord lineNo) const;
108 virtual wxString GetLineText(wxTextCoord lineNo) const;
109 virtual int GetNumberOfLines() const;
110
111 virtual bool IsModified() const;
112 virtual bool IsEditable() const;
113
114 // If the return values from and to are the same, there is no selection.
115 virtual void GetSelection(wxTextPos* from, wxTextPos* to) const;
116
117 // operations
118 // ----------
119
120 // editing
121 virtual void Clear();
122 virtual void Replace(wxTextPos from, wxTextPos to, const wxString& value);
123 virtual void Remove(wxTextPos from, wxTextPos to);
124
125 // sets/clears the dirty flag
126 virtual void MarkDirty();
127 virtual void DiscardEdits();
128
129 // writing text inserts it at the current position, appending always
130 // inserts it at the end
131 virtual void WriteText(const wxString& text);
132 virtual void AppendText(const wxString& text);
133
134 // translate between the position (which is just an index in the text ctrl
135 // considering all its contents as a single strings) and (x, y) coordinates
136 // which represent (logical, i.e. unwrapped) column and line.
137 virtual wxTextPos XYToPosition(wxTextCoord x, wxTextCoord y) const;
138 virtual bool PositionToXY(wxTextPos pos,
139 wxTextCoord *x, wxTextCoord *y) const;
140
141 // wxUniv-specific: find a screen position (in client coordinates) of the
142 // given text position or of the caret
143 bool PositionToLogicalXY(wxTextPos pos, wxCoord *x, wxCoord *y) const;
144 bool PositionToDeviceXY(wxTextPos pos, wxCoord *x, wxCoord *y) const;
145 wxPoint GetCaretPosition() const;
146
147 virtual void ShowPosition(wxTextPos pos);
148
149 // Clipboard operations
150 virtual void Copy();
151 virtual void Cut();
152 virtual void Paste();
153
154 // Undo/redo
155 virtual void Undo();
156 virtual void Redo();
157
158 virtual bool CanUndo() const;
159 virtual bool CanRedo() const;
160
161 // Insertion point
162 virtual void SetInsertionPoint(wxTextPos pos);
163 virtual void SetInsertionPointEnd();
164 virtual wxTextPos GetInsertionPoint() const;
165 virtual wxTextPos GetLastPosition() const;
166
167 virtual void SetSelection(wxTextPos from, wxTextPos to);
168 virtual void SetEditable(bool editable);
169
170 // wxUniv-specific methods
171 // -----------------------
172
173 // caret stuff
174 virtual void ShowCaret(bool show = true);
175 void HideCaret() { ShowCaret(false); }
176 void CreateCaret(); // for the current font size
177
178 // helpers for cursor movement
179 wxTextPos GetWordStart() const;
180 wxTextPos GetWordEnd() const;
181
182 // selection helpers
183 bool HasSelection() const
184 { return m_selStart != -1 && m_selEnd > m_selStart; }
185 void ClearSelection();
186 void RemoveSelection();
187 wxString GetSelectionText() const;
188
189 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const;
190 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt,
191 wxTextCoord *col,
192 wxTextCoord *row) const;
193
194 // find the character at this position in the given line, return value as
195 // for HitTest()
196 //
197 // NB: x is the logical coord (client and unscrolled)
198 wxTextCtrlHitTestResult HitTestLine(const wxString& line,
199 wxCoord x,
200 wxTextCoord *colOut) const;
201
202 // bring the given position into view
203 void ShowHorzPosition(wxCoord pos);
204
205 // scroll the window horizontally so that the first character shown is in
206 // position pos
207 void ScrollText(wxTextCoord col);
208
209 // adjust the DC for horz text control scrolling too
210 virtual void DoPrepareDC(wxDC& dc);
211
212 // implementation only from now on
213 // -------------------------------
214
215 // override this to take into account our scrollbar-less scrolling
216 virtual void CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const;
217 virtual void CalcScrolledPosition(int x, int y, int *xx, int *yy) const;
218
219 // perform an action
220 virtual bool PerformAction(const wxControlAction& action,
221 long numArg = -1,
222 const wxString& strArg = wxEmptyString);
223
224 static wxInputHandler *GetStdInputHandler(wxInputHandler *handlerDef);
225 virtual wxInputHandler *DoGetStdInputHandler(wxInputHandler *handlerDef)
226 {
227 return GetStdInputHandler(handlerDef);
228 }
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 HasFlag(wxTE_PASSWORD); }
236 bool WrapLines() const { return m_wrapLines; }
237
238 // only for wxStdTextCtrlInputHandler
239 void RefreshSelection();
240
241 // override wxScrollHelper method to prevent (auto)scrolling beyond the end
242 // of line
243 virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const;
244
245 // idle processing
246 virtual void OnInternalIdle();
247
248 protected:
249 // ensure we have correct default border
250 virtual wxBorder GetDefaultBorder() const { return wxBORDER_SUNKEN; }
251
252 // override base class methods
253 virtual void DoDrawBorder(wxDC& dc, const wxRect& rect);
254 virtual void DoDraw(wxControlRenderer *renderer);
255
256 // calc the size from the text extent
257 virtual wxSize DoGetBestClientSize() const;
258
259 // implements Set/ChangeValue()
260 virtual void DoSetValue(const wxString& value, int flags = 0);
261 virtual wxString DoGetValue() const;
262
263 // common part of all ctors
264 void Init();
265
266 // drawing
267 // -------
268
269 // draw the text in the given rectangle
270 void DoDrawTextInRect(wxDC& dc, const wxRect& rectUpdate);
271
272 // draw the line wrap marks in this rect
273 void DoDrawLineWrapMarks(wxDC& dc, const wxRect& rectUpdate);
274
275 // line/row geometry calculations
276 // ------------------------------
277
278 // get the extent (width) of the text
279 wxCoord GetTextWidth(const wxString& text) const;
280
281 // get the logical text width (accounting for scrolling)
282 wxCoord GetTotalWidth() const;
283
284 // get total number of rows (different from number of lines if the lines
285 // can be wrapped)
286 wxTextCoord GetRowCount() const;
287
288 // find the number of rows in this line (only if WrapLines())
289 wxTextCoord GetRowsPerLine(wxTextCoord line) const;
290
291 // get the starting row of the given line
292 wxTextCoord GetFirstRowOfLine(wxTextCoord line) const;
293
294 // get the row following this line
295 wxTextCoord GetRowAfterLine(wxTextCoord line) const;
296
297 // refresh functions
298 // -----------------
299
300 // the text area is the part of the window in which the text can be
301 // displayed, i.e. part of it inside the margins and the real text area is
302 // the area in which the text *is* currently displayed: for example, in the
303 // multiline control case the text area can have extra space at the bottom
304 // which is not tall enough for another line and which is then not included
305 // into the real text area
306 wxRect GetRealTextArea() const;
307
308 // refresh the text in the given (in logical coords) rect
309 void RefreshTextRect(const wxRect& rect, bool textOnly = true);
310
311 // refresh the line wrap marks for the given range of lines (inclusive)
312 void RefreshLineWrapMarks(wxTextCoord rowFirst, wxTextCoord rowLast);
313
314 // refresh the text in the given range (in logical coords) of this line, if
315 // width is 0, refresh to the end of line
316 void RefreshPixelRange(wxTextCoord line, wxCoord start, wxCoord width);
317
318 // refresh the text in the given range (in text coords) in this line
319 void RefreshColRange(wxTextCoord line, wxTextPos start, size_t count);
320
321 // refresh the text from in the given line range (inclusive)
322 void RefreshLineRange(wxTextCoord lineFirst, wxTextCoord lineLast);
323
324 // refresh the text in the given range which can span multiple lines
325 // (this method accepts arguments in any order)
326 void RefreshTextRange(wxTextPos start, wxTextPos end);
327
328 // get the text to show: either the text itself or the text replaced with
329 // starts for wxTE_PASSWORD control
330 wxString GetTextToShow(const wxString& text) const;
331
332 // find the row in this line where the given position (counted from the
333 // start of line) is
334 wxTextCoord GetRowInLine(wxTextCoord line,
335 wxTextCoord col,
336 wxTextCoord *colRowStart = NULL) const;
337
338 // find the number of characters of a line before it wraps
339 // (and optionally also the real width of the line)
340 size_t GetPartOfWrappedLine(const wxChar* text,
341 wxCoord *widthReal = NULL) const;
342
343 // get the start and end of the selection for this line: if the line is
344 // outside the selection, both will be -1 and false will be returned
345 bool GetSelectedPartOfLine(wxTextCoord line,
346 wxTextPos *start, wxTextPos *end) const;
347
348 // update the text rect: the zone inside our client rect (its coords are
349 // client coords) which contains the text
350 void UpdateTextRect();
351
352 // calculate the last visible position
353 void UpdateLastVisible();
354
355 // move caret to the given position unconditionally
356 // (SetInsertionPoint() does nothing if the position didn't change)
357 void DoSetInsertionPoint(wxTextPos pos);
358
359 // move caret to the new position without updating the display (for
360 // internal use only)
361 void MoveInsertionPoint(wxTextPos pos);
362
363 // set the caret to its initial (default) position
364 void InitInsertionPoint();
365
366 // get the width of the longest line in pixels
367 wxCoord GetMaxWidth() const;
368
369 // force recalculation of the max line width
370 void RecalcMaxWidth();
371
372 // update the max width after the given line was modified
373 void UpdateMaxWidth(wxTextCoord line);
374
375 // hit testing
376 // -----------
377
378 // HitTest2() is more efficient than 2 consecutive HitTest()s with the same
379 // line (i.e. y) and it also returns the offset of the starting position in
380 // pixels
381 //
382 // as the last hack, this function accepts either logical or device (by
383 // default) coords depending on devCoords flag
384 wxTextCtrlHitTestResult HitTest2(wxCoord y,
385 wxCoord x1,
386 wxCoord x2,
387 wxTextCoord *row,
388 wxTextCoord *colStart,
389 wxTextCoord *colEnd,
390 wxTextCoord *colRowStart,
391 bool devCoords = true) const;
392
393 // HitTest() version which takes the logical text coordinates and not the
394 // device ones
395 wxTextCtrlHitTestResult HitTestLogical(const wxPoint& pos,
396 wxTextCoord *col,
397 wxTextCoord *row) const;
398
399 // get the line and the row in this line corresponding to the given row,
400 // return true if ok and false if row is out of range
401 //
402 // NB: this function can only be called for controls which wrap lines
403 bool GetLineAndRow(wxTextCoord row,
404 wxTextCoord *line,
405 wxTextCoord *rowInLine) const;
406
407 // get the height of one line (the same for all lines)
408 wxCoord GetLineHeight() const
409 {
410 // this one should be already precalculated
411 wxASSERT_MSG( m_heightLine != -1, wxT("should have line height") );
412
413 return m_heightLine;
414 }
415
416 // get the average char width
417 wxCoord GetAverageWidth() const { return m_widthAvg; }
418
419 // recalc the line height and char width (to call when the font changes)
420 void RecalcFontMetrics();
421
422 // vertical scrolling helpers
423 // --------------------------
424
425 // all these functions are for multi line controls only
426
427 // get the number of visible lines
428 size_t GetLinesPerPage() const;
429
430 // return the position above the cursor or INVALID_POS_VALUE
431 wxTextPos GetPositionAbove();
432
433 // return the position below the cursor or INVALID_POS_VALUE
434 wxTextPos GetPositionBelow();
435
436 // event handlers
437 // --------------
438 void OnChar(wxKeyEvent& event);
439 void OnSize(wxSizeEvent& event);
440
441 // return the struct containing control-type dependent data
442 struct wxTextSingleLineData& SData() { return *m_data.sdata; }
443 struct wxTextMultiLineData& MData() { return *m_data.mdata; }
444 struct wxTextWrappedData& WData() { return *m_data.wdata; }
445 const wxTextSingleLineData& SData() const { return *m_data.sdata; }
446 const wxTextMultiLineData& MData() const { return *m_data.mdata; }
447 const wxTextWrappedData& WData() const { return *m_data.wdata; }
448
449 // clipboard operations (unlike the versions without Do prefix, they have a
450 // return code)
451 bool DoCut();
452 bool DoPaste();
453
454 private:
455 // all these methods are for multiline text controls only
456
457 // update the scrollbars (only called from OnIdle)
458 void UpdateScrollbars();
459
460 // get read only access to the lines of multiline control
461 inline const wxArrayString& GetLines() const;
462 inline size_t GetLineCount() const;
463
464 // replace a line (returns true if the number of rows in thel ine changed)
465 bool ReplaceLine(wxTextCoord line, const wxString& text);
466
467 // remove a line
468 void RemoveLine(wxTextCoord line);
469
470 // insert a line at this position
471 void InsertLine(wxTextCoord line, const wxString& text);
472
473 // calculate geometry of this line
474 void LayoutLine(wxTextCoord line, class wxWrappedLineData& lineData) const;
475
476 // calculate geometry of all lines until the given one
477 void LayoutLines(wxTextCoord lineLast) const;
478
479 // the initially specified control size
480 wxSize m_sizeInitial;
481
482 // the global control text
483 wxString m_value;
484
485 // current position
486 wxTextPos m_curPos;
487 wxTextCoord m_curCol,
488 m_curRow;
489
490 // last position (only used by GetLastPosition())
491 wxTextPos m_posLast;
492
493 // selection
494 wxTextPos m_selAnchor,
495 m_selStart,
496 m_selEnd;
497
498 // flags
499 bool m_isModified:1,
500 m_isEditable:1,
501 m_hasCaret:1,
502 m_wrapLines:1; // can't be changed after creation
503
504 // the rectangle (in client coordinates) to draw text inside
505 wxRect m_rectText;
506
507 // the height of one line (cached value of GetCharHeight)
508 wxCoord m_heightLine;
509
510 // and the average char width (cached value of GetCharWidth)
511 wxCoord m_widthAvg;
512
513 // we have some data which depends on the kind of control (single or multi
514 // line)
515 union
516 {
517 wxTextSingleLineData *sdata;
518 wxTextMultiLineData *mdata;
519 wxTextWrappedData *wdata;
520 void *data;
521 } m_data;
522
523 // the object to which we delegate our undo/redo implementation
524 wxTextCtrlCommandProcessor *m_cmdProcessor;
525
526 DECLARE_EVENT_TABLE()
527 DECLARE_DYNAMIC_CLASS(wxTextCtrl)
528
529 friend class wxWrappedLineData;
530 };
531
532 #endif // _WX_UNIV_TEXTCTRL_H_
533