From 7dd40b6f4b31e6bc9c7f626f1461aff8adf7714c Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Thu, 5 Jan 2006 04:31:27 +0000 Subject: [PATCH] Added wxGetMouseState which returns the current state of the mouse. Returns an instance of a wxMouseState object that contains the current position of the mouse pointer in screen coordinants, as well as boolean values indicating the up/down status of the mouse buttons and the modifier keys. Implemented for wxMSW, wxGTK and wxMac. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@36691 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + docs/latex/wx/function.tex | 50 +++++++++++++++++++++++++++++ include/wx/utils.h | 64 ++++++++++++++++++++++++++++++++++++++ src/gtk/window.cpp | 25 +++++++++++++++ src/gtk1/window.cpp | 25 +++++++++++++++ src/mac/carbon/app.cpp | 23 ++++++++++++++ src/msw/window.cpp | 22 +++++++++++++ 7 files changed, 210 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index 92eb3d4dc4..81898ce7fc 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -51,6 +51,7 @@ All (GUI): - Implemented and handling in wxHTML (based on patch by Sandro Sigala) - Added caption parameter to wxGetFontFromUser and wxGetColourFromUser. +- Added wxGetMouseState function. wxMSW: diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex index cc3f336e88..9b71dc0532 100644 --- a/docs/latex/wx/function.tex +++ b/docs/latex/wx/function.tex @@ -128,6 +128,7 @@ the corresponding topic. \helpref{wxGetLocalTimeMillis}{wxgetlocaltimemillis}\\ \helpref{wxGetLocalTime}{wxgetlocaltime}\\ \helpref{wxGetMousePosition}{wxgetmouseposition}\\ +\helpref{wxGetMouseState}{wxgetmousestate}\\ \helpref{wxGetMultipleChoices}{wxgetmultiplechoices}\\ \helpref{wxGetMultipleChoice}{wxgetmultiplechoice}\\ \helpref{wxGetNumberFromUser}{wxgetnumberfromuser}\\ @@ -3009,6 +3010,55 @@ Returns the mouse position in screen coordinates. +\membersection{::wxGetMouseState}\label{wxgetmousestate} + +\func{wxMouseState}{wxGetMouseState}{\void} + +Returns the current state of the mouse. Returns a wxMouseState +instance that contains the current position of the mouse pointer in +screen coordinants, as well as boolean values indicating the up/down +status of the mouse buttons and the modifier keys. + +\wxheading{Include files} + + + +wxMouseState has the following interface: + +\begin{verbatim} +class wxMouseState +{ +public: + wxMouseState(); + + wxCoord GetX(); + wxCoord GetY(); + + bool LeftDown(); + bool MiddleDown(); + bool RightDown(); + + bool ControlDown(); + bool ShiftDown(); + bool AltDown(); + bool MetaDown(); + bool CmdDown(); + + void SetX(wxCoord x); + void SetY(wxCoord y); + + void SetLeftDown(bool down); + void SetMiddleDown(bool down); + void SetRightDown(bool down); + + void SetControlDown(bool down); + void SetShiftDown(bool down); + void SetAltDown(bool down); + void SetMetaDown(bool down); +}; +\end{verbatim} + + \membersection{::wxGetResource}\label{wxgetresource} \func{bool}{wxGetResource}{\param{const wxString\& }{section}, \param{const wxString\& }{entry}, diff --git a/include/wx/utils.h b/include/wx/utils.h index 03e24056b0..517385f7b7 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -114,6 +114,70 @@ WXDLLEXPORT bool wxGetKeyState(wxKeyCode key); // in wxMSW. WXDLLEXPORT bool wxSetDetectableAutoRepeat( bool flag ); + +// wxMouseState is used to hold information about button and modifier state +// and is what is returned from wxGetMouseState. +class WXDLLEXPORT wxMouseState +{ +public: + wxMouseState() + : m_x(0), m_y(0), + m_leftDown(false), m_middleDown(false), m_rightDown(false), + m_controlDown(false), m_shiftDown(false), m_altDown(false), + m_metaDown(false) + {} + + wxCoord GetX() { return m_x; } + wxCoord GetY() { return m_y; } + + bool LeftDown() { return m_leftDown; } + bool MiddleDown() { return m_middleDown; } + bool RightDown() { return m_rightDown; } + + bool ControlDown() { return m_controlDown; } + bool ShiftDown() { return m_shiftDown; } + bool AltDown() { return m_altDown; } + bool MetaDown() { return m_metaDown; } + bool CmdDown() + { +#if defined(__WXMAC__) || defined(__WXCOCOA__) + return MetaDown(); +#else + return ControlDown(); +#endif + } + + void SetX(wxCoord x) { m_x = x; } + void SetY(wxCoord y) { m_y = y; } + + void SetLeftDown(bool down) { m_leftDown = down; } + void SetMiddleDown(bool down) { m_middleDown = down; } + void SetRightDown(bool down) { m_rightDown = down; } + + void SetControlDown(bool down) { m_controlDown = down; } + void SetShiftDown(bool down) { m_shiftDown = down; } + void SetAltDown(bool down) { m_altDown = down; } + void SetMetaDown(bool down) { m_metaDown = down; } + +private: + wxCoord m_x; + wxCoord m_y; + + bool m_leftDown; + bool m_middleDown; + bool m_rightDown; + + bool m_controlDown; + bool m_shiftDown; + bool m_altDown; + bool m_metaDown; +}; + + +// Returns the current state of the mouse position, buttons and modifers +WXDLLEXPORT wxMouseState wxGetMouseState(); + + // ---------------------------------------------------------------------------- // Window ID management // ---------------------------------------------------------------------------- diff --git a/src/gtk/window.cpp b/src/gtk/window.cpp index 1e9038e20a..d27d7d642b 100644 --- a/src/gtk/window.cpp +++ b/src/gtk/window.cpp @@ -2672,6 +2672,31 @@ wxWindow *wxGetActiveWindow() return wxWindow::FindFocus(); } + +wxMouseState wxGetMouseState() +{ + wxMouseState ms; + + gint x; + gint y; + GdkModifierType mask; + + gdk_window_get_pointer(NULL, &x, &y, &mask); + + ms.SetX(x); + ms.SetY(y); + ms.SetLeftDown(mask & GDK_BUTTON1_MASK); + ms.SetMiddleDown(mask & GDK_BUTTON2_MASK); + ms.SetRightDown(mask & GDK_BUTTON3_MASK); + + ms.SetControlDown(mask & GDK_CONTROL_MASK); + ms.SetShiftDown(mask & GDK_SHIFT_MASK); + ms.SetAltDown(mask & GDK_MOD1_MASK); + ms.SetMetaDown(mask & GDK_MOD2_MASK); + + return ms; +} + //----------------------------------------------------------------------------- // wxWindowGTK //----------------------------------------------------------------------------- diff --git a/src/gtk1/window.cpp b/src/gtk1/window.cpp index 1e9038e20a..d27d7d642b 100644 --- a/src/gtk1/window.cpp +++ b/src/gtk1/window.cpp @@ -2672,6 +2672,31 @@ wxWindow *wxGetActiveWindow() return wxWindow::FindFocus(); } + +wxMouseState wxGetMouseState() +{ + wxMouseState ms; + + gint x; + gint y; + GdkModifierType mask; + + gdk_window_get_pointer(NULL, &x, &y, &mask); + + ms.SetX(x); + ms.SetY(y); + ms.SetLeftDown(mask & GDK_BUTTON1_MASK); + ms.SetMiddleDown(mask & GDK_BUTTON2_MASK); + ms.SetRightDown(mask & GDK_BUTTON3_MASK); + + ms.SetControlDown(mask & GDK_CONTROL_MASK); + ms.SetShiftDown(mask & GDK_SHIFT_MASK); + ms.SetAltDown(mask & GDK_MOD1_MASK); + ms.SetMetaDown(mask & GDK_MOD2_MASK); + + return ms; +} + //----------------------------------------------------------------------------- // wxWindowGTK //----------------------------------------------------------------------------- diff --git a/src/mac/carbon/app.cpp b/src/mac/carbon/app.cpp index 6c327441c2..e7eb2d80ea 100644 --- a/src/mac/carbon/app.cpp +++ b/src/mac/carbon/app.cpp @@ -1499,6 +1499,29 @@ bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below #endif +wxMouseState wxGetMouseState() +{ + wxMouseState ms; + + wxPoint pt = wxGetMousePosition(); + ms.SetX(pt.x); + ms.SetY(pt.y); + + UInt32 buttons = GetCurrentButtonState(); + ms.SetLeftDown( (buttons & 0x01) != 0 ); + ms.SetMiddleDown( (buttons & 0x04) != 0 ); + ms.SetRightDown( (buttons & 0x02) != 0 ); + + UInt32 modifiers = GetCurrentKeyModifiers(); + ms.SetControlDown(modifiers & controlKey); + ms.SetShiftDown(modifiers & shiftKey); + ms.SetAltDown(modifiers & optionKey); + ms.SetMetaDown(modifiers & cmdKey); + + return ms; +} + + bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar ) { if ( !focus ) diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 07f84ec60e..2ee65a51cf 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -5390,6 +5390,28 @@ bool wxGetKeyState(wxKeyCode key) #endif } + +wxMouseState wxGetMouseState() +{ + wxMouseState ms; + POINT pt; + GetCursorPos( &pt ); + + ms.SetX(pt.x); + ms.SetY(pt.y); + ms.SetLeftDown( (GetAsyncKeyState(VK_LBUTTON) & (1<<15)) != 0 ); + ms.SetMiddleDown( (GetAsyncKeyState(VK_MBUTTON) & (1<<15)) != 0 ); + ms.SetRightDown( (GetAsyncKeyState(VK_RBUTTON) & (1<<15)) != 0 ); + + ms.SetControlDown( (GetAsyncKeyState(VK_CONTROL) & (1<<15)) != 0 ); + ms.SetShiftDown( (GetAsyncKeyState(VK_SHIFT) & (1<<15)) != 0 ); + ms.SetAltDown( (GetAsyncKeyState(VK_MENU) & (1<<15)) != 0 ); +// ms.SetMetaDown(); + + return ms; +} + + wxWindow *wxGetActiveWindow() { HWND hWnd = GetActiveWindow(); -- 2.45.2