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