]> git.saurik.com Git - wxWidgets.git/blame - include/wx/univ/textctrl.h
Implemented Enable for wxMenuBar to avoid assert
[wxWidgets.git] / include / wx / univ / textctrl.h
CommitLineData
1e6feb95
VZ
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$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
1e6feb95
VZ
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_UNIV_TEXTCTRL_H_
13#define _WX_UNIV_TEXTCTRL_H_
14
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3870b2f 16 #pragma interface "univtextctrl.h"
1e6feb95
VZ
17#endif
18
19class WXDLLEXPORT wxCaret;
20class 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
1e6feb95
VZ
69// ----------------------------------------------------------------------------
70// wxTextCtrl
71// ----------------------------------------------------------------------------
72
73class WXDLLEXPORT wxTextCtrl : public wxTextCtrlBase, public wxScrollHelper
74{
75public:
76 // creation
77 // --------
78
6463b9f5 79 wxTextCtrl() { Init(); }
1e6feb95
VZ
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,
6463b9f5
JS
88 const wxString& name = wxTextCtrlNameStr)
89 {
90 Init();
91
92 Create(parent, id, value, pos, size, style, validator, name);
93 }
1e6feb95
VZ
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
3a9fa0d6
VZ
130 // sets/clears the dirty flag
131 virtual void MarkDirty();
1e6feb95
VZ
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
f08bc784
VZ
194 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt,
195 wxTextCoord *col,
196 wxTextCoord *row) const;
1e6feb95
VZ
197
198 // find the character at this position in the given line, return value as
199 // for HitTest()
200 //
201 // NB: x is the logical coord (client and unscrolled)
202 wxTextCtrlHitTestResult HitTestLine(const wxString& line,
203 wxCoord x,
204 wxTextCoord *colOut) const;
205
206 // bring the given position into view
207 void ShowHorzPosition(wxCoord pos);
208
209 // scroll the window horizontally so that the first character shown is in
210 // position pos
211 void ScrollText(wxTextCoord col);
212
213 // adjust the DC for horz text control scrolling too
214 virtual void DoPrepareDC(wxDC& dc);
215
216 // implementation only from now on
217 // -------------------------------
218
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;
222
81b344a5 223 // ensure we have correct default border
1e6feb95
VZ
224 virtual wxBorder GetDefaultBorder() const { return wxBORDER_SUNKEN; }
225
226 // perform an action
227 virtual bool PerformAction(const wxControlAction& action,
228 long numArg = -1,
229 const wxString& strArg = wxEmptyString);
230
231 // override these methods to handle the caret
232 virtual bool SetFont(const wxFont &font);
233 virtual bool Enable(bool enable = TRUE);
234
235 // more readable flag testing methods
1e6feb95
VZ
236 bool IsPassword() const { return (GetWindowStyle() & wxTE_PASSWORD) != 0; }
237 bool WrapLines() const
238 { return !IsSingleLine() && !(GetWindowStyle() & wxHSCROLL); }
239
240 // only for wxStdTextCtrlInputHandler
241 void RefreshSelection();
242
243protected:
244 // override base class methods
245 virtual void DoDrawBorder(wxDC& dc, const wxRect& rect);
246 virtual void DoDraw(wxControlRenderer *renderer);
247
248 // calc the size from the text extent
249 virtual wxSize DoGetBestClientSize() const;
250
251 // common part of all ctors
252 void Init();
253
254 // drawing
255 // -------
256
257 // draw the text in the given rectangle
258 void DoDrawTextInRect(wxDC& dc, const wxRect& rectUpdate);
259
260 // draw the line wrap marks in this rect
261 void DoDrawLineWrapMarks(wxDC& dc, const wxRect& rectUpdate);
262
263 // line/row geometry calculations
264 // ------------------------------
265
266 // get the extent (width) of the text
267 wxCoord GetTextWidth(const wxString& text) const;
268
269 // get the logical text width (accounting for scrolling)
270 wxCoord GetTotalWidth() const;
271
272 // get total number of rows (different from number of lines if the lines
273 // can be wrapped)
274 wxTextCoord GetRowCount() const;
275
276 // find the number of rows in this line (only if WrapLines())
277 wxTextCoord GetRowsPerLine(wxTextCoord line) const;
278
279 // get the starting row of the given line
280 wxTextCoord GetFirstRowOfLine(wxTextCoord line) const;
281
282 // get the row following this line
283 wxTextCoord GetRowAfterLine(wxTextCoord line) const;
284
285 // refresh functions
286 // -----------------
287
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;
295
296 // refresh the text in the given (in logical coords) rect
297 void RefreshTextRect(const wxRect& rect, bool textOnly = TRUE);
298
299 // refresh the line wrap marks for the given range of lines (inclusive)
300 void RefreshLineWrapMarks(wxTextCoord rowFirst, wxTextCoord rowLast);
301
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);
305
306 // refresh the text in the given range (in text coords) in this line
307 void RefreshColRange(wxTextCoord line, wxTextPos start, size_t count);
308
309 // refresh the text from in the given line range (inclusive)
310 void RefreshLineRange(wxTextCoord lineFirst, wxTextCoord lineLast);
311
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);
315
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;
319
320 // find the row in this line where the given position (counted from the
321 // start of line) is
322 wxTextCoord GetRowInLine(wxTextCoord line,
323 wxTextCoord col,
324 wxTextCoord *colRowStart = NULL) const;
325
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;
330
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;
335
336 // update the text rect: the zone inside our client rect (its coords are
337 // client coords) which contains the text
338 void UpdateTextRect();
339
340 // calculate the last visible position
341 void UpdateLastVisible();
342
343 // move caret to the given position unconditionally
344 // (SetInsertionPoint() does nothing if the position didn't change)
345 void DoSetInsertionPoint(wxTextPos pos);
346
347 // move caret to the new position without updating the display (for
348 // internal use only)
349 void MoveInsertionPoint(wxTextPos pos);
350
351 // set the caret to its initial (default) position
352 void InitInsertionPoint();
353
354 // get the width of the longest line in pixels
355 wxCoord GetMaxWidth() const;
356
357 // force recalculation of the max line width
358 void RecalcMaxWidth();
359
360 // update the max width after the given line was modified
361 void UpdateMaxWidth(wxTextCoord line);
362
363 // hit testing
364 // -----------
365
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
368 // pixels
369 //
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,
373 wxCoord x1,
374 wxCoord x2,
375 wxTextCoord *row,
376 wxTextCoord *colStart,
377 wxTextCoord *colEnd,
378 wxTextCoord *colRowStart,
379 bool devCoords = TRUE) const;
380
381 // HitTest() version which takes the logical text coordinates and not the
382 // device ones
383 wxTextCtrlHitTestResult HitTestLogical(const wxPoint& pos,
384 wxTextCoord *col,
385 wxTextCoord *row) const;
386
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
389 //
390 // NB: this function can only be called for controls which wrap lines
391 bool GetLineAndRow(wxTextCoord row,
392 wxTextCoord *line,
393 wxTextCoord *rowInLine) const;
394
395 // get the height of one line (the same for all lines)
396 wxCoord GetLineHeight() const
397 {
398 // this one should be already precalculated
399 wxASSERT_MSG( m_heightLine != -1, _T("should have line height") );
400
401 return m_heightLine;
402 }
403
404 // get the average char width
405 wxCoord GetAverageWidth() const { return m_widthAvg; }
406
407 // recalc the line height and char width (to call when the font changes)
408 void RecalcFontMetrics();
409
410 // vertical scrolling helpers
411 // --------------------------
412
413 // all these functions are for multi line controls only
414
415 // get the number of visible lines
416 size_t GetLinesPerPage() const;
417
418 // return the position above the cursor or INVALID_POS_VALUE
419 wxTextPos GetPositionAbove();
420
421 // return the position below the cursor or INVALID_POS_VALUE
422 wxTextPos GetPositionBelow();
423
424 // event handlers
425 // --------------
1e6feb95
VZ
426 void OnChar(wxKeyEvent& event);
427 void OnSize(wxSizeEvent& event);
428
429 // overrdie wxScrollHelper method to prevent (auto)scrolling beyond the end
430 // of line
431 virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const;
432
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; }
440
441 // clipboard operations (unlike the versions without Do prefix, they have a
442 // return code)
443 bool DoCut();
444 bool DoPaste();
445
e39af974
JS
446 // idle processing
447 virtual void OnInternalIdle();
1e6feb95
VZ
448private:
449 // all these methods are for multiline text controls only
450
451 // update the scrollbars (only called from OnIdle)
452 void UpdateScrollbars();
453
454 // get read only access to the lines of multiline control
455 inline const wxArrayString& GetLines() const;
456 inline size_t GetLineCount() const;
457
458 // replace a line (returns TRUE if the number of rows in thel ine changed)
459 bool ReplaceLine(wxTextCoord line, const wxString& text);
460
461 // remove a line
462 void RemoveLine(wxTextCoord line);
463
464 // insert a line at this position
465 void InsertLine(wxTextCoord line, const wxString& text);
466
467 // calculate geometry of this line
468 void LayoutLine(wxTextCoord line, class wxWrappedLineData& lineData) const;
469
470 // calculate geometry of all lines until the given one
471 void LayoutLines(wxTextCoord lineLast) const;
472
473 // the initially specified control size
474 wxSize m_sizeInitial;
475
476 // the global control text
477 wxString m_value;
478
479 // current position
480 wxTextPos m_curPos;
481 wxTextCoord m_curCol,
482 m_curRow;
483
484 // last position (only used by GetLastPosition())
485 wxTextPos m_posLast;
486
487 // selection
488 wxTextPos m_selAnchor,
489 m_selStart,
490 m_selEnd;
491
492 // flags
493 bool m_isModified:1,
494 m_isEditable:1,
495 m_hasCaret:1;
496
497 // the rectangle (in client coordinates) to draw text inside
498 wxRect m_rectText;
499
500 // the height of one line (cached value of GetCharHeight)
501 wxCoord m_heightLine;
502
503 // and the average char width (cached value of GetCharWidth)
504 wxCoord m_widthAvg;
505
506 // we have some data which depends on the kind of control (single or multi
507 // line)
508 union
509 {
510 wxTextSingleLineData *sdata;
511 wxTextMultiLineData *mdata;
512 wxTextWrappedData *wdata;
513 void *data;
514 } m_data;
515
516 // the object to which we delegate our undo/redo implementation
517 wxTextCtrlCommandProcessor *m_cmdProcessor;
518
519 DECLARE_EVENT_TABLE()
520 DECLARE_DYNAMIC_CLASS(wxTextCtrl)
521};
522
523// ----------------------------------------------------------------------------
524// wxStdTextCtrlInputHandler: this control handles only the mouse/kbd actions
525// common to Win32 and GTK, platform-specific things are implemented elsewhere
526// ----------------------------------------------------------------------------
527
528class WXDLLEXPORT wxStdTextCtrlInputHandler : public wxStdInputHandler
529{
530public:
531 wxStdTextCtrlInputHandler(wxInputHandler *inphand);
532
23645bfa 533 virtual bool HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
534 const wxKeyEvent& event,
535 bool pressed);
23645bfa 536 virtual bool HandleMouse(wxInputConsumer *consumer,
1e6feb95 537 const wxMouseEvent& event);
23645bfa 538 virtual bool HandleMouseMove(wxInputConsumer *consumer,
1e6feb95 539 const wxMouseEvent& event);
23645bfa 540 virtual bool HandleFocus(wxInputConsumer *consumer, const wxFocusEvent& event);
1e6feb95
VZ
541
542protected:
543 // get the position of the mouse click
544 static wxTextPos HitTest(const wxTextCtrl *text, const wxPoint& pos);
545
546 // capture data
547 wxTextCtrl *m_winCapture;
548};
549
550#endif // _WX_UNIV_TEXTCTRL_H_
551