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