From efe66bbc0e9f463a6dd3ad49717474dd801bebf3 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 4 Feb 2004 14:56:14 +0000 Subject: [PATCH] added wxTextCtrl::HitTest(); implemented it for MSW git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25496 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 4 +- docs/latex/wx/text.tex | 117 ++++++++++++++++++++++++++++++++ include/wx/msw/textctrl.h | 3 + include/wx/textctrl.h | 37 +++++++++- samples/widgets/textctrl.cpp | 66 ++++++++++++++++-- src/common/textcmn.cpp | 12 ++++ src/msw/textctrl.cpp | 127 +++++++++++++++++++++++++++++++---- 7 files changed, 346 insertions(+), 20 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 1fe5f262df..555dab7ca4 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -140,8 +140,8 @@ wxMSW: - wxComboBox::GetValue within a wxEVT_COMMAND_TEXT_UPDATED event should now pass the correct value even if the handler for wxEVT_COMMAND_COMBOBOX_SELECTED changed the selection -- wxFileDialog now returns correct filter index for multiple-file - dialogs +- wxFileDialog now returns correct filter index for multiple-file dialogs +- added wxTextCtrl::HitTest() wxGTK: diff --git a/docs/latex/wx/text.tex b/docs/latex/wx/text.tex index 7e2751726a..4554eeef07 100644 --- a/docs/latex/wx/text.tex +++ b/docs/latex/wx/text.tex @@ -17,6 +17,17 @@ No base class +\wxheading{Typedefs} + +\texttt{wxTextPos} is the type containing the index of a position in a text +control. \texttt{wxTextCoord} contains the index of a column or a row in the +control. + +Note that alghough both of these types should probably have been unsigned, due +to backwards compatibility reasons, are defined as \texttt{long} currently. +Their use (instead of plain \texttt{long}) is still encouraged as it makes the +code more readable. + \wxheading{Constants} The following values can be passed to SetAlignment to determine @@ -59,8 +70,27 @@ for a text control. \end{verbatim} } +The values below are the possible return codes of the +\helpref{HitTest}{wxtextctrlhittest} method: +{\small +\begin{verbatim} +// the point asked is ... +enum wxTextCtrlHitTestResult +{ + wxTE_HT_UNKNOWN = -2, // this means HitTest() is simply not implemented + wxTE_HT_BEFORE, // either to the left or upper + wxTE_HT_ON_TEXT, // directly on + wxTE_HT_BELOW, // below [the last line] + wxTE_HT_BEYOND // after [the end of line] +}; +// ... the character returned +\end{verbatim} +} + + \latexignore{\rtfignore{\wxheading{Members}}} + \membersection{wxTextAttr::wxTextAttr}\label{wxtextattrctor} \func{}{wxTextAttr}{\void} @@ -73,36 +103,42 @@ colour, font, and alignment. The values not initialized in the constructor can b later, otherwise \helpref{wxTextCtrl::SetStyle}{wxtextctrlsetstyle} will use the default values for them. + \membersection{wxTextAttr::GetAlignment}\label{wxtextattrgetalignment} \constfunc{wxTextAttrAlignment}{GetAlignment}{\void} Returns the paragraph alignment. + \membersection{wxTextAttr::GetBackgroundColour}\label{wxtextattrgetbackgroundcolour} \constfunc{const wxColour\&}{GetBackgroundColour}{\void} Return the background colour specified by this attribute. + \membersection{wxTextAttr::GetFont}\label{wxtextattrgetfont} \constfunc{const wxFont\&}{GetFont}{\void} Return the text font specified by this attribute. + \membersection{wxTextAttr::GetLeftIndent}\label{wxtextattrgetleftindent} \constfunc{int}{GetLeftIndent}{\void} Returns the left indent in tenths of a millimetre. + \membersection{wxTextAttr::GetRightIndent}\label{wxtextattrgetrightindent} \constfunc{int}{GetRightIndent}{\void} Returns the right indent in tenths of a millimetre. + \membersection{wxTextAttr::GetTabs}\label{wxtextattrgettabs} \constfunc{const wxArrayInt\&}{GetTabs}{\void} @@ -110,78 +146,91 @@ Returns the right indent in tenths of a millimetre. Returns the array of integers representing the tab stops. Each array element specifies the tab stop in tenths of a millimetre. + \membersection{wxTextAttr::GetTextColour}\label{wxtextattrgettextcolour} \constfunc{const wxColour\&}{GetTextColour}{\void} Return the text colour specified by this attribute. + \membersection{wxTextAttr::HasBackgroundColour}\label{wxtextattrhasbackgroundcolour} \constfunc{bool}{HasBackgroundColour}{\void} Returns {\tt true} if this style specifies the background colour to use. + \membersection{wxTextAttr::HasFont}\label{wxtextattrhasfont} \constfunc{bool}{HasFont}{\void} Returns {\tt true} if this style specifies the font to use. + \membersection{wxTextAttr::HasTextColour}\label{wxtextattrhastextcolour} \constfunc{bool}{HasTextColour}{\void} Returns {\tt true} if this style specifies the foreground colour to use. + \membersection{wxTextAttr::GetFlags}\label{wxtextattrgetflags} \func{long}{GetFlags}{\void} Returns a bitlist indicating which attributes will be set. + \membersection{wxTextAttr::IsDefault}\label{wxtextattrisdefault} \constfunc{bool}{IsDefault}{\void} Returns {\tt true} if this style specifies any non-default attributes. + \membersection{wxTextAttr::SetAlignment}\label{wxtextattrsetalignment} \func{void}{SetAlignment}{\param{wxTextAttrAlignment}{ alignment}} Sets the paragraph alignment. + \membersection{wxTextAttr::SetBackgroundColour}\label{wxtextattrsetbackgroundcolour} \func{void}{SetBackgroundColour}{\param{const wxColour\& }{colour}} Sets the background colour. + \membersection{wxTextAttr::SetFlags}\label{wxtextattrsetflags} \func{void}{SetFlags}{\param{long}{ flags}} Pass a bitlist indicating which attributes will be set. + \membersection{wxTextAttr::SetFont}\label{wxtextattrsetfont} \func{void}{SetFont}{\param{const wxFont\&}{ font}} Sets the text font. + \membersection{wxTextAttr::SetLeftIndent}\label{wxtextattrsetleftindent} \func{void}{SetLeftIndent}{\param{int }{indent}} Sets the left indent in tenths of a millimetre. + \membersection{wxTextAttr::SetRightIndent}\label{wxtextattrsetrightindent} \func{void}{SetRightIndent}{\param{int }{indent}} Sets the right indent in tenths of a millimetre. + \membersection{wxTextAttr::SetTabs}\label{wxtextattrsettabs} \func{void}{SetTabs}{\param{const wxArrayInt\&}{ tabs}} @@ -189,6 +238,7 @@ Sets the right indent in tenths of a millimetre. Sets the array of integers representing the tab stops. Each array element specifies the tab stop in tenths of a millimetre. + \membersection{wxTextAttr::SetTextColour}\label{wxtextattrsettextcolour} \func{void}{SetTextColour}{\param{const wxColour\& }{colour}} @@ -409,6 +459,7 @@ into the control than the limit set by \latexignore{\rtfignore{\wxheading{Members}}} + \membersection{wxTextCtrl::wxTextCtrl}\label{wxtextctrlconstr} \func{}{wxTextCtrl}{\void} @@ -458,12 +509,14 @@ visible. \helpref{wxTextCtrl::Create}{wxtextctrlcreate}, \helpref{wxValidator}{wxvalidator} + \membersection{wxTextCtrl::\destruct{wxTextCtrl}} \func{}{\destruct{wxTextCtrl}}{\void} Destructor, destroying the text control. + \membersection{wxTextCtrl::AppendText}\label{wxtextctrlappendtext} \func{void}{AppendText}{\param{const wxString\& }{ text}} @@ -483,18 +536,21 @@ the programmer should use \helpref{GetInsertionPoint}{wxtextctrlgetinsertionpoin \helpref{wxTextCtrl::WriteText}{wxtextctrlwritetext} + \membersection{wxTextCtrl::CanCopy}\label{wxtextctrlcancopy} \func{virtual bool}{CanCopy}{\void} Returns {\tt true} if the selection can be copied to the clipboard. + \membersection{wxTextCtrl::CanCut}\label{wxtextctrlcancut} \func{virtual bool}{CanCut}{\void} Returns {\tt true} if the selection can be cut to the clipboard. + \membersection{wxTextCtrl::CanPaste}\label{wxtextctrlcanpaste} \func{virtual bool}{CanPaste}{\void} @@ -503,6 +559,7 @@ Returns {\tt true} if the contents of the clipboard can be pasted into the text control. On some platforms (Motif, GTK) this is an approximation and returns {\tt true} if the control is editable, {\tt false} otherwise. + \membersection{wxTextCtrl::CanRedo}\label{wxtextctrlcanredo} \func{virtual bool}{CanRedo}{\void} @@ -510,6 +567,7 @@ and returns {\tt true} if the control is editable, {\tt false} otherwise. Returns {\tt true} if there is a redo facility available and the last operation can be redone. + \membersection{wxTextCtrl::CanUndo}\label{wxtextctrlcanundo} \func{virtual bool}{CanUndo}{\void} @@ -517,6 +575,7 @@ can be redone. Returns {\tt true} if there is an undo facility available and the last operation can be undone. + \membersection{wxTextCtrl::Clear}\label{wxtextctrlclear} \func{virtual void}{Clear}{\void} @@ -526,12 +585,14 @@ Clears the text in the control. Note that this function will generate a {\tt wxEVT\_COMMAND\_TEXT\_UPDATED} event. + \membersection{wxTextCtrl::Copy}\label{wxtextctrlcopy} \func{virtual void}{Copy}{\void} Copies the selected text to the clipboard under Motif and MS Windows. + \membersection{wxTextCtrl::Create}\label{wxtextctrlcreate} \func{bool}{Create}{\param{wxWindow* }{parent}, \param{wxWindowID}{ id},\rtfsp @@ -542,18 +603,21 @@ Creates the text control for two-step construction. Derived classes should call or replace this function. See \helpref{wxTextCtrl::wxTextCtrl}{wxtextctrlconstr}\rtfsp for further details. + \membersection{wxTextCtrl::Cut}\label{wxtextctrlcut} \func{virtual void}{Cut}{\void} Copies the selected text to the clipboard and removes the selection. + \membersection{wxTextCtrl::DiscardEdits} \func{void}{DiscardEdits}{\void} Resets the internal `modified' flag as if the current edits had been saved. + \membersection{wxTextCtrl::EmulateKeyPress} \func{bool}{EmulateKeyPress}{\param{const wxKeyEvent\& }{event}} @@ -571,6 +635,7 @@ under any platform but MSW. {\tt true} if the event resulted in a change to the control, {\tt false} otherwise. + \membersection{wxTextCtrl::GetDefaultStyle}\label{wxtextctrlgetdefaultstyle} \constfunc{const wxTextAttr\& }{GetDefaultStyle}{\void} @@ -581,6 +646,7 @@ Returns the style currently used for the new text. \helpref{SetDefaultStyle}{wxtextctrlsetdefaultstyle} + \membersection{wxTextCtrl::GetInsertionPoint}\label{wxtextctrlgetinsertionpoint} \constfunc{virtual long}{GetInsertionPoint}{\void} @@ -604,6 +670,7 @@ point or the zero character if the point is at the end of the control. \end{verbatim} }% + \membersection{wxTextCtrl::GetLastPosition}\label{wxtextctrlgetlastposition} \constfunc{virtual long}{GetLastPosition}{\void} @@ -611,6 +678,7 @@ point or the zero character if the point is at the end of the control. Returns the zero based index of the last position in the text control, which is equal to the number of characters in the control. + \membersection{wxTextCtrl::GetLineLength}\label{wxtextctrlgetlinelength} \constfunc{int}{GetLineLength}{\param{long}{ lineNo}} @@ -626,6 +694,7 @@ character(s). The length of the line, or -1 if {\it lineNo} was invalid. + \membersection{wxTextCtrl::GetLineText}\label{wxtextctrlgetlinetext} \constfunc{wxString}{GetLineText}{\param{long}{ lineNo}} @@ -641,6 +710,7 @@ any trailing newline character(s). The contents of the line. + \membersection{wxTextCtrl::GetNumberOfLines}\label{wxtextctrlgetnumberoflines} \constfunc{int}{GetNumberOfLines}{\void} @@ -657,6 +727,7 @@ calculated by actually counting newline characters in the buffer. You may wish to avoid using functions that work with line numbers if you are working with controls that contain large amounts of text. + \membersection{wxTextCtrl::GetRange}\label{wxtextctrlgetrange} \constfunc{virtual wxString}{GetRange}{\param{long}{ from}, \param{long}{ to}} @@ -672,6 +743,7 @@ representations ({\tt CR} or {\tt CR LF}) and so this method should be used to obtain the correct results instead of extracting parts of the entire value. It may also be more efficient, especially if the control contains a lot of data. + \membersection{wxTextCtrl::GetSelection}\label{wxtextctrlgetselection} \constfunc{virtual void}{GetSelection}{\param{long*}{ from}, \param{long*}{ to}} @@ -698,6 +770,7 @@ consisting of the from and to values.} \perlnote{In wxPerl this method takes no parameter and returns a 2-element list {\tt ( from, to )}.} + \membersection{wxTextCtrl::GetStringSelection}\label{wxtextctrlgetstringselection} \func{virtual wxString}{GetStringSelection}{\void} @@ -705,6 +778,7 @@ consisting of the from and to values.} Gets the text currently selected in the control. If there is no selection, the returned string is empty. + \membersection{wxTextCtrl::GetStyle}\label{wxtextctrlgetstyle} \func{bool}{GetStyle}{\param{long }{position}, \param{wxTextAttr\& }{style}} @@ -721,6 +795,7 @@ the styles are not supported under this platform. \helpref{wxTextCtrl::SetStyle}{wxtextctrlsetstyle}, \helpref{wxTextAttr}{wxtextattr} + \membersection{wxTextCtrl::GetValue}\label{wxtextctrlgetvalue} \constfunc{wxString}{GetValue}{\void} @@ -730,6 +805,24 @@ the lines will be separated by (Unix-style) $\backslash$n characters, even under Windows where they are separated by a $\backslash$r$\backslash$n sequence in the native control. + +\membersection{wxTextCtrl::HitTest}\label{wxtextctrlhittest} + +\constfunc{wxTextCtrlHitTestResult}{HitTest}{\param{const wxPoint\& }{pt}, \param{wxTextCoord }{*col}, \param{wxTextCoord }{*row}} + +This function finds the character at the specified position expressed in +pixels. If the return code is not \texttt{wxTE\_HT\_UNKNOWN} the row and column +of the character closest to this position are returned in the \arg{col} and +\arg{row} parameters (unless the pointers are \textt{NULL} which is allowed). + +Please note that this function is currently only implemented in wxUniv and +wxMSW ports. + +\wxheading{See also} + +\helpref{PositionToXY}{wxtextctrlpositiontoxy}, \helpref{XYToPosition}{wxtextctrlxytoposition}, + + \membersection{wxTextCtrl::IsEditable}\label{wxtextctrliseditable} \constfunc{bool}{IsEditable}{\void} @@ -739,6 +832,7 @@ always can be changed by the program), i.e. if the control hasn't been put in read-only mode by a previous call to \helpref{SetEditable}{wxtextctrlseteditable}. + \membersection{wxTextCtrl::IsModified}\label{wxtextctrlismodified} \constfunc{bool}{IsModified}{\void} @@ -746,6 +840,7 @@ read-only mode by a previous call to Returns {\tt true} if the text has been modified by user. Note that calling \helpref{SetValue}{wxtextctrlsetvalue} doesn't make the control modified. + \membersection{wxTextCtrl::IsMultiLine}\label{wxtextctrlismultiline} \constfunc{bool}{IsMultiLine}{\void} @@ -757,6 +852,7 @@ otherwise. \helpref{IsSingleLine}{wxtextctrlissingleline} + \membersection{wxTextCtrl::IsSingleLine}\label{wxtextctrlissingleline} \constfunc{bool}{IsSingleLine}{\void} @@ -768,6 +864,7 @@ otherwise. \helpref{IsMultiLine}{wxtextctrlissingleline} + \membersection{wxTextCtrl::LoadFile}\label{wxtextctrlloadfile} \func{bool}{LoadFile}{\param{const wxString\& }{ filename}} @@ -812,6 +909,7 @@ Loads and displays the named file, if it exists. %% %% \helpref{wxKeyEvent}{wxkeyevent} + \membersection{wxTextCtrl::OnDropFiles}\label{wxtextctrlondropfiles} \func{void}{OnDropFiles}{\param{wxDropFilesEvent\& }{event}} @@ -831,12 +929,14 @@ This is not implemented on non-Windows platforms. \helpref{wxDropFilesEvent}{wxdropfilesevent} + \membersection{wxTextCtrl::Paste}\label{wxtextctrlpaste} \func{virtual void}{Paste}{\void} Pastes text from the clipboard to the text item. + \membersection{wxTextCtrl::PositionToXY}\label{wxtextctrlpositiontoxy} \constfunc{bool}{PositionToXY}{\param{long }{pos}, \param{long *}{x}, \param{long *}{y}} @@ -867,6 +967,7 @@ above.} \perlnote{In wxPerl this method only takes the {\tt pos} parameter, and returns a 2-element list {\tt ( x, y )}.} + \membersection{wxTextCtrl::Redo}\label{wxtextctrlredo} \func{virtual void}{Redo}{\void} @@ -874,6 +975,7 @@ returns a 2-element list {\tt ( x, y )}.} If there is a redo facility and the last operation can be redone, redoes the last operation. Does nothing if there is no redo facility. + \membersection{wxTextCtrl::Remove}\label{wxtextctrlremove} \func{virtual void}{Remove}{\param{long}{ from}, \param{long}{ to}} @@ -887,6 +989,7 @@ the character at the last position. \docparam{to}{The last position.} + \membersection{wxTextCtrl::Replace}\label{wxtextctrlreplace} \func{virtual void}{Replace}{\param{long}{ from}, \param{long}{ to}, \param{const wxString\& }{value}} @@ -902,6 +1005,7 @@ the character at the last position with the given text. \docparam{value}{The value to replace the existing text with.} + \membersection{wxTextCtrl::SaveFile}\label{wxtextctrlsavefile} \func{bool}{SaveFile}{\param{const wxString\& }{ filename}} @@ -916,6 +1020,7 @@ Saves the contents of the control in a text file. {\tt true} if the operation was successful, {\tt false} otherwise. + \membersection{wxTextCtrl::SetDefaultStyle}\label{wxtextctrlsetdefaultstyle} \func{bool}{SetDefaultStyle}{\param{const wxTextAttr\& }{style}} @@ -946,6 +1051,7 @@ the styles are not supported under this platform. \helpref{GetDefaultStyle}{wxtextctrlgetdefaultstyle} + \membersection{wxTextCtrl::SetEditable}\label{wxtextctrlseteditable} \func{virtual void}{SetEditable}{\param{const bool}{ editable}} @@ -960,6 +1066,7 @@ Makes the text item editable or read-only, overriding the {\bf wxTE\_READONLY} f \helpref{IsEditable}{wxtextctrliseditable} + \membersection{wxTextCtrl::SetInsertionPoint}\label{wxtextctrlsetinsertionpoint} \func{virtual void}{SetInsertionPoint}{\param{long}{ pos}} @@ -970,6 +1077,7 @@ Sets the insertion point at the given position. \docparam{pos}{Position to set.} + \membersection{wxTextCtrl::SetInsertionPointEnd}\label{wxtextctrlsetinsertionpointend} \func{virtual void}{SetInsertionPointEnd}{\void} @@ -977,6 +1085,7 @@ Sets the insertion point at the given position. Sets the insertion point at the end of the text control. This is equivalent to \helpref{SetInsertionPoint}{wxtextctrlsetinsertionpoint}(\helpref{GetLastPosition}{wxtextctrlgetlastposition}()). + \membersection{wxTextCtrl::SetMaxLength}\label{wxtextctrlsetmaxlength} \func{virtual void}{SetMaxLength}{\param{unsigned long }{len}} @@ -1001,6 +1110,7 @@ Note that this function may only be used with single line text controls. Only implemented in wxMSW/wxGTK starting with wxWindows 2.3.2. + \membersection{wxTextCtrl::SetSelection}\label{wxtextctrlsetselection} \func{virtual void}{SetSelection}{\param{long}{ from}, \param{long}{ to}} @@ -1015,6 +1125,7 @@ in the control is selected. \docparam{to}{The last position.} + \membersection{wxTextCtrl::SetStyle}\label{wxtextctrlsetstyle} \func{bool}{SetStyle}{\param{long }{start}, \param{long }{end}, \param{const wxTextAttr\& }{style}} @@ -1039,6 +1150,7 @@ the styles are not supported under this platform. \helpref{wxTextCtrl::GetStyle}{wxtextctrlgetstyle}, \helpref{wxTextAttr}{wxtextattr} + \membersection{wxTextCtrl::SetValue}\label{wxtextctrlsetvalue} \func{virtual void}{SetValue}{\param{const wxString\& }{ value}} @@ -1054,6 +1166,7 @@ event. \docparam{value}{The new value to set. It may contain newline characters if the text control is multi-line.} + \membersection{wxTextCtrl::ShowPosition}\label{wxtextctrlshowposition} \func{void}{ShowPosition}{\param{long}{ pos}} @@ -1064,6 +1177,7 @@ Makes the line containing the given position visible. \docparam{pos}{The position that should be visible.} + \membersection{wxTextCtrl::Undo}\label{wxtextctrlundo} \func{virtual void}{Undo}{\void} @@ -1071,6 +1185,7 @@ Makes the line containing the given position visible. If there is an undo facility and the last operation can be undone, undoes the last operation. Does nothing if there is no undo facility. + \membersection{wxTextCtrl::WriteText}\label{wxtextctrlwritetext} \func{void}{WriteText}{\param{const wxString\& }{ text}} @@ -1089,6 +1204,7 @@ line breaks. See \helpref{wxTextCtrl::\cinsert}{wxtextctrlinsert} and \helpref{ After the write operation, the insertion point will be at the end of the inserted text, so subsequent write operations will be appended. To append text after the user may have interacted with the control, call \helpref{wxTextCtrl::SetInsertionPointEnd}{wxtextctrlsetinsertionpointend} before writing. + \membersection{wxTextCtrl::XYToPosition}\label{wxtextctrlxytoposition} \func{long}{XYToPosition}{\param{long}{ x}, \param{long}{ y}} @@ -1105,6 +1221,7 @@ Converts the given zero based column and line number to a position. The position value. + \membersection{wxTextCtrl::operator \cinsert}\label{wxtextctrlinsert} \func{wxTextCtrl\&}{operator \cinsert}{\param{const wxString\& }{s}} diff --git a/include/wx/msw/textctrl.h b/include/wx/msw/textctrl.h index dfc07966c5..058004f57a 100644 --- a/include/wx/msw/textctrl.h +++ b/include/wx/msw/textctrl.h @@ -103,6 +103,9 @@ public: virtual bool PositionToXY(long pos, long *x, long *y) const; virtual void ShowPosition(long pos); + virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, + wxTextCoord *col, + wxTextCoord *row) const; // Clipboard operations virtual void Copy(); diff --git a/include/wx/textctrl.h b/include/wx/textctrl.h index fa7d7814fe..b5551bbef5 100644 --- a/include/wx/textctrl.h +++ b/include/wx/textctrl.h @@ -25,7 +25,8 @@ #if wxUSE_TEXTCTRL #include "wx/control.h" // the base class -#include "wx/dynarray.h" +#include "wx/dynarray.h" // wxArrayInt +#include "wx/gdicmn.h" // wxPoint // 16-bit Borland 4.0 doesn't seem to allow multiple inheritance with wxWindow // and streambuf: it complains about deriving a huge class from the huge class @@ -48,6 +49,17 @@ class WXDLLEXPORT wxTextCtrl; class WXDLLEXPORT wxTextCtrlBase; +// ---------------------------------------------------------------------------- +// wxTextCtrl types +// ---------------------------------------------------------------------------- + +// wxTextPos is the position in the text +typedef long wxTextPos; + +// wxTextCoord is the line or row number (which should have been unsigned but +// is long for backwards compatibility) +typedef long wxTextCoord; + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -102,6 +114,21 @@ WXDLLEXPORT_DATA(extern const wxChar*) wxTextCtrlNameStr; // wxTE_RICH controls - can be used together with or instead of wxTE_RICH #define wxTE_RICH2 0x8000 +// ---------------------------------------------------------------------------- +// wxTextCtrl::HitTest return values +// ---------------------------------------------------------------------------- + +// the point asked is ... +enum wxTextCtrlHitTestResult +{ + wxTE_HT_UNKNOWN = -2, // this means HitTest() is simply not implemented + wxTE_HT_BEFORE, // either to the left or upper + wxTE_HT_ON_TEXT, // directly on + wxTE_HT_BELOW, // below [the last line] + wxTE_HT_BEYOND // after [the end of line] +}; +// ... the character returned + // ---------------------------------------------------------------------------- // Types for wxTextAttr // ---------------------------------------------------------------------------- @@ -295,6 +322,14 @@ public: virtual void ShowPosition(long pos) = 0; + // find the character at position given in pixels + // + // NB: pt is in device coords (not adjusted for the client area origin nor + // scrolling) + virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, + wxTextCoord *col, + wxTextCoord *row) const; + // Clipboard operations virtual void Copy() = 0; virtual void Cut() = 0; diff --git a/samples/widgets/textctrl.cpp b/samples/widgets/textctrl.cpp index f20eaf5cb7..a30268abdd 100644 --- a/samples/widgets/textctrl.cpp +++ b/samples/widgets/textctrl.cpp @@ -208,6 +208,63 @@ private: DECLARE_WIDGETS_PAGE(TextWidgetsPage) }; +// ---------------------------------------------------------------------------- +// WidgetsTextCtrl +// ---------------------------------------------------------------------------- + +class WidgetsTextCtrl : public wxTextCtrl +{ +public: + WidgetsTextCtrl(wxWindow *parent, + wxWindowID id, + const wxString& value, + int flags) + : wxTextCtrl(parent, id, value, wxDefaultPosition, wxDefaultSize, flags) + { + } + +protected: + void OnRightClick(wxMouseEvent& event) + { + wxString where; + wxTextCoord x, y; + switch ( HitTest(event.GetPosition(), &x, &y) ) + { + default: + wxFAIL_MSG( _T("unexpected HitTest() result") ); + // fall through + + case wxTE_HT_UNKNOWN: + x = y = -1; + where = _T("nowhere near"); + break; + + case wxTE_HT_BEFORE: + where = _T("before"); + break; + + case wxTE_HT_BELOW: + where = _T("below"); + break; + + case wxTE_HT_BEYOND: + where = _T("beyond"); + break; + + case wxTE_HT_ON_TEXT: + where = _T("at"); + break; + } + + wxLogMessage(_T("Mouse is %s (%ld, %ld)"), where.c_str(), x, y); + + event.Skip(); + } + +private: + DECLARE_EVENT_TABLE() +}; + // ---------------------------------------------------------------------------- // event tables // ---------------------------------------------------------------------------- @@ -237,6 +294,10 @@ BEGIN_EVENT_TABLE(TextWidgetsPage, WidgetsPage) EVT_RADIOBOX(wxID_ANY, TextWidgetsPage::OnCheckOrRadioBox) END_EVENT_TABLE() +BEGIN_EVENT_TABLE(WidgetsTextCtrl, wxTextCtrl) + EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick) +END_EVENT_TABLE() + // ============================================================================ // implementation // ============================================================================ @@ -552,10 +613,7 @@ void TextWidgetsPage::CreateText() valueOld = _T("Hello, Universe!"); } - m_text = new wxTextCtrl(this, TextPage_Textctrl, - valueOld, - wxDefaultPosition, wxDefaultSize, - flags); + m_text = new WidgetsTextCtrl(this, TextPage_Textctrl, valueOld, flags); // cast to int needed to silence gcc warning about different enums m_sizerText->Add(m_text, 1, wxALL | diff --git a/src/common/textcmn.cpp b/src/common/textcmn.cpp index cf77965c5b..10e92ab2c1 100644 --- a/src/common/textcmn.cpp +++ b/src/common/textcmn.cpp @@ -488,6 +488,18 @@ void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event) } } +// ---------------------------------------------------------------------------- +// hit testing +// ---------------------------------------------------------------------------- + +wxTextCtrlHitTestResult +wxTextCtrlBase::HitTest(const wxPoint& WXUNUSED(pt), + wxTextCoord * WXUNUSED(col), + wxTextCoord * WXUNUSED(row)) const +{ + // not implemented + return wxTE_HT_UNKNOWN; +} #else // !wxUSE_TEXTCTRL diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 7dd6a84fcf..07593f4f37 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -1256,6 +1256,23 @@ void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) #endif // Win32/16 } +// ---------------------------------------------------------------------------- +// Working with files +// ---------------------------------------------------------------------------- + +bool wxTextCtrl::LoadFile(const wxString& file) +{ + if ( wxTextCtrlBase::LoadFile(file) ) + { + // update the size limit if needed + AdjustSpaceLimit(); + + return TRUE; + } + + return FALSE; +} + // ---------------------------------------------------------------------------- // Editing // ---------------------------------------------------------------------------- @@ -1273,19 +1290,6 @@ void wxTextCtrl::Remove(long from, long to) Replace(from, to, wxEmptyString); } -bool wxTextCtrl::LoadFile(const wxString& file) -{ - if ( wxTextCtrlBase::LoadFile(file) ) - { - // update the size limit if needed - AdjustSpaceLimit(); - - return TRUE; - } - - return FALSE; -} - bool wxTextCtrl::IsModified() const { return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; @@ -1306,6 +1310,10 @@ int wxTextCtrl::GetNumberOfLines() const return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); } +// ---------------------------------------------------------------------------- +// Positions <-> coords +// ---------------------------------------------------------------------------- + long wxTextCtrl::XYToPosition(long x, long y) const { // This gets the char index for the _beginning_ of this line @@ -1353,6 +1361,99 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const return TRUE; } +wxTextCtrlHitTestResult +wxTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *col, wxTextCoord *row) const +{ + // first get the position from Windows + LPARAM lParam; + +#if wxUSE_RICHEDIT + POINTL ptl; + if ( IsRich() ) + { + // for rich edit controls the position is passed iva the struct fields + ptl.x = pt.x; + ptl.y = pt.y; + lParam = (LPARAM)&ptl; + } + else +#endif // wxUSE_RICHEDIT + { + // for the plain ones, we are limited to 16 bit positions which are + // combined in a single 32 bit value + lParam = MAKELPARAM(pt.x, pt.y); + } + + LRESULT pos = SendMessage(GetHwnd(), EM_CHARFROMPOS, 0, lParam); + + if ( pos == -1 ) + { + // this seems to indicate an error... + return wxTE_HT_UNKNOWN; + } + +#if wxUSE_RICHEDIT + if ( !IsRich() ) +#endif // wxUSE_RICHEDIT + { + // for plain EDIT controls the higher word contains something else + pos = LOWORD(pos); + } + + + // next determine where it is relatively to our point: EM_CHARFROMPOS + // always returns the closest character but we need to be more precise, so + // double check that we really are where it pretends + POINTL ptReal; + +#if wxUSE_RICHEDIT + // FIXME: we need to distinguish between richedit 2 and 3 here somehow but + // we don't know how to do it + if ( IsRich() ) + { + SendMessage(GetHwnd(), EM_POSFROMCHAR, (WPARAM)&ptReal, pos); + } + else +#endif // wxUSE_RICHEDIT + { + LRESULT lRc = SendMessage(GetHwnd(), EM_POSFROMCHAR, pos, 0); + + if ( lRc == -1 ) + { + // this is apparently returned when pos corresponds to the last + // position + ptReal.x = + ptReal.y = 0; + } + else + { + ptReal.x = LOWORD(lRc); + ptReal.y = HIWORD(lRc); + } + } + + wxTextCtrlHitTestResult rc; + + if ( pt.y > ptReal.y + GetCharHeight() ) + rc = wxTE_HT_BELOW; + else if ( pt.x > ptReal.x + GetCharWidth() ) + rc = wxTE_HT_BEYOND; + else + rc = wxTE_HT_ON_TEXT; + + // finally translate to column/row + if ( !PositionToXY(pos, col, row) ) + { + wxFAIL_MSG( _T("PositionToXY() not expected to fail in HitTest()") ); + } + + return rc; +} + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- + void wxTextCtrl::ShowPosition(long pos) { HWND hWnd = GetHwnd(); -- 2.45.2