make wxKeyEvent and wxMouseEvent derive from the same wxKeyboardState object (indirectly via wxMouseState in the case of the latter) to make Get/HasModifiers() available in wxMouseEvent as well while avoiding code duplication

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55745 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-09-19 18:41:41 +00:00
parent d577449426
commit 0e0977894a
10 changed files with 417 additions and 351 deletions

View File

@@ -20,6 +20,7 @@
#if wxUSE_GUI
#include "wx/gdicmn.h"
#include "wx/cursor.h"
#include "wx/mousestate.h"
#endif
#include "wx/dynarray.h"
@@ -295,12 +296,6 @@ enum Propagation_state
class WXDLLIMPEXP_BASE wxEvent : public wxObject
{
private:
wxEvent& operator=(const wxEvent&);
protected:
wxEvent(const wxEvent&); // for implementing Clone()
public:
wxEvent(int winid = 0, wxEventType commandType = wxEVT_NULL );
@@ -367,10 +362,13 @@ protected:
// backwards compatibility as it is new
int m_propagationLevel;
protected:
bool m_skipped;
bool m_isCommandEvent;
protected:
wxEvent(const wxEvent&); // for implementing Clone()
wxEvent& operator=(const wxEvent&); // for derived classes operator=()
private:
// it needs to access our m_propagationLevel
friend class WXDLLIMPEXP_FWD_BASE wxPropagateOnce;
@@ -644,12 +642,17 @@ enum
wxMOUSE_BTN_MAX
};
class WXDLLIMPEXP_CORE wxMouseEvent : public wxEvent
class WXDLLIMPEXP_CORE wxMouseEvent : public wxEvent,
public wxMouseState
{
public:
wxMouseEvent(wxEventType mouseType = wxEVT_NULL);
wxMouseEvent(const wxMouseEvent& event) : wxEvent(event)
{ Assign(event); }
wxMouseEvent(const wxMouseEvent& event)
: wxEvent(event),
wxMouseState(event)
{
Assign(event);
}
// Was it a button event? (*doesn't* mean: is any button *down*?)
bool IsButton() const { return Button(wxMOUSE_BTN_ANY); }
@@ -672,20 +675,6 @@ public:
// Get the button which is changing state (wxMOUSE_BTN_NONE if none)
int GetButton() const;
// Find state of shift/control keys
bool ControlDown() const { return m_controlDown; }
bool MetaDown() const { return m_metaDown; }
bool AltDown() const { return m_altDown; }
bool ShiftDown() const { return m_shiftDown; }
bool CmdDown() const
{
#if defined(__WXMAC__) || defined(__WXCOCOA__)
return MetaDown();
#else
return ControlDown();
#endif
}
// Find which event was just generated
bool LeftDown() const { return (m_eventType == wxEVT_LEFT_DOWN); }
bool MiddleDown() const { return (m_eventType == wxEVT_MIDDLE_DOWN); }
@@ -792,7 +781,12 @@ public:
virtual wxEvent *Clone() const { return new wxMouseEvent(*this); }
wxMouseEvent& operator=(const wxMouseEvent& event) { if (&event != this) Assign(event); return *this; }
wxMouseEvent& operator=(const wxMouseEvent& event)
{
if (&event != this)
Assign(event);
return *this;
}
public:
wxCoord m_x, m_y;
@@ -803,11 +797,6 @@ public:
bool m_aux1Down;
bool m_aux2Down;
bool m_controlDown;
bool m_shiftDown;
bool m_altDown;
bool m_metaDown;
int m_clickCount;
int m_wheelAxis;
@@ -870,48 +859,13 @@ private:
wxEVT_HOTKEY
*/
class WXDLLIMPEXP_CORE wxKeyEvent : public wxEvent
class WXDLLIMPEXP_CORE wxKeyEvent : public wxEvent,
public wxKeyboardState
{
public:
wxKeyEvent(wxEventType keyType = wxEVT_NULL);
wxKeyEvent(const wxKeyEvent& evt);
// can be used check if the key event has exactly the given modifiers:
// "GetModifiers() = wxMOD_CONTROL" is easier to write than "ControlDown()
// && !MetaDown() && !AltDown() && !ShiftDown()"
int GetModifiers() const
{
return (m_controlDown ? wxMOD_CONTROL : 0) |
(m_shiftDown ? wxMOD_SHIFT : 0) |
(m_metaDown ? wxMOD_META : 0) |
(m_altDown ? wxMOD_ALT : 0);
}
// Find state of shift/control keys
bool ControlDown() const { return m_controlDown; }
bool ShiftDown() const { return m_shiftDown; }
bool MetaDown() const { return m_metaDown; }
bool AltDown() const { return m_altDown; }
// "Cmd" is a pseudo key which is Control for PC and Unix platforms but
// Apple ("Command") key under Macs: it makes often sense to use it instead
// of, say, ControlDown() because Cmd key is used for the same thing under
// Mac as Ctrl elsewhere (but Ctrl still exists, just not used for this
// purpose under Mac)
bool CmdDown() const
{
#if defined(__WXMAC__) || defined(__WXCOCOA__)
return MetaDown();
#else
return ControlDown();
#endif
}
// exclude MetaDown() from HasModifiers() because NumLock under X is often
// configured as mod2 modifier, yet the key events even when it is pressed
// should be processed normally, not like Ctrl- or Alt-key
bool HasModifiers() const { return ControlDown() || AltDown(); }
// get the key code: an ASCII7 char or an element of wxKeyCode enum
int GetKeyCode() const { return (int)m_keyCode; }
@@ -956,15 +910,14 @@ public:
{
if (&evt != this)
{
wxEvent::operator=(evt);
wxKeyboardState::operator=(evt);
m_x = evt.m_x;
m_y = evt.m_y;
m_keyCode = evt.m_keyCode;
m_controlDown = evt.m_controlDown;
m_shiftDown = evt.m_shiftDown;
m_altDown = evt.m_altDown;
m_metaDown = evt.m_metaDown;
m_scanCode = evt.m_scanCode;
m_rawCode = evt.m_rawCode;
m_rawFlags = evt.m_rawFlags;
@@ -980,12 +933,6 @@ public:
long m_keyCode;
// TODO: replace those with a single m_modifiers bitmask of wxMOD_XXX?
bool m_controlDown;
bool m_shiftDown;
bool m_altDown;
bool m_metaDown;
// FIXME: what is this for? relation to m_rawXXX?
bool m_scanCode;

91
include/wx/kbdstate.h Normal file
View File

@@ -0,0 +1,91 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/kbdstate.h
// Purpose: Declaration of wxKeyboardState class
// Author: Vadim Zeitlin
// Created: 2008-09-19
// RCS-ID: $Id$
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_KBDSTATE_H_
#define _WX_KBDSTATE_H_
#include "wx/defs.h"
// ----------------------------------------------------------------------------
// wxKeyboardState stores the state of the keyboard modifier keys
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxKeyboardState
{
public:
wxKeyboardState()
: m_controlDown(false),
m_shiftDown(false),
m_altDown(false),
m_metaDown(false)
{
}
// default copy ctor, assignment operator and dtor are ok
// accessors for the various modifier keys
// ---------------------------------------
// should be used check if the key event has exactly the given modifiers:
// "GetModifiers() = wxMOD_CONTROL" is easier to write than "ControlDown()
// && !MetaDown() && !AltDown() && !ShiftDown()"
int GetModifiers() const
{
return (m_controlDown ? wxMOD_CONTROL : 0) |
(m_shiftDown ? wxMOD_SHIFT : 0) |
(m_metaDown ? wxMOD_META : 0) |
(m_altDown ? wxMOD_ALT : 0);
}
// returns true if any modifiers at all are pressed
bool HasModifiers() const { return GetModifiers() != wxMOD_NONE; }
// accessors for individual modifier keys
bool ControlDown() const { return m_controlDown; }
bool ShiftDown() const { return m_shiftDown; }
bool MetaDown() const { return m_metaDown; }
bool AltDown() const { return m_altDown; }
// "Cmd" is a pseudo key which is Control for PC and Unix platforms but
// Apple ("Command") key under Macs: it makes often sense to use it instead
// of, say, ControlDown() because Cmd key is used for the same thing under
// Mac as Ctrl elsewhere (but Ctrl still exists, just not used for this
// purpose under Mac)
bool CmdDown() const
{
#if defined(__WXMAC__) || defined(__WXCOCOA__)
return MetaDown();
#else
return ControlDown();
#endif
}
// these functions are mostly used by wxWidgets itself
// ---------------------------------------------------
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; }
// for backwards compatibility with the existing code accessing these
// members of wxKeyEvent directly, these variables are public, however you
// should not use them in any new code, please use the accessors instead
public:
bool m_controlDown : 1;
bool m_shiftDown : 1;
bool m_altDown : 1;
bool m_metaDown : 1;
};
#endif // _WX_KBDSTATE_H_

70
include/wx/mousestate.h Normal file
View File

@@ -0,0 +1,70 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/mousestate.h
// Purpose: Declaration of wxMouseState class
// Author: Vadim Zeitlin
// Created: 2008-09-19 (extracted from wx/utils.h)
// RCS-ID: $Id$
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_MOUSESTATE_H_
#define _WX_MOUSESTATE_H_
#include "wx/kbdstate.h"
// ----------------------------------------------------------------------------
// wxMouseState contains the information about mouse position, buttons and also
// key modifiers
// ----------------------------------------------------------------------------
// wxMouseState is used to hold information about button and modifier state
// and is what is returned from wxGetMouseState.
class WXDLLIMPEXP_CORE wxMouseState : public wxKeyboardState
{
public:
wxMouseState()
: m_x(0), m_y(0),
m_leftDown(false), m_middleDown(false), m_rightDown(false),
m_aux1Down(false), m_aux2Down(false)
{
}
// default copy ctor, assignment operator and dtor are ok
// accessors for the mouse position
wxCoord GetX() const { return m_x; }
wxCoord GetY() const { return m_y; }
wxPoint GetPosition() const { return wxPoint(m_x, m_y); }
// accessors for the pressed buttons
bool LeftDown() const { return m_leftDown; }
bool MiddleDown() const { return m_middleDown; }
bool RightDown() const { return m_rightDown; }
bool Aux1Down() const { return m_aux1Down; }
bool Aux2Down() const { return m_aux2Down; }
// these functions are mostly used by wxWidgets itself
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 SetAux1Down(bool down) { m_aux1Down = down; }
void SetAux2Down(bool down) { m_aux2Down = down; }
private:
bool m_leftDown : 1;
bool m_middleDown : 1;
bool m_rightDown : 1;
bool m_aux1Down : 1;
bool m_aux2Down : 1;
wxCoord m_x,
m_y;
};
#endif // _WX_MOUSESTATE_H_

View File

@@ -19,8 +19,10 @@
#include "wx/object.h"
#include "wx/list.h"
#include "wx/filefn.h"
#if wxUSE_GUI
#include "wx/gdicmn.h"
#include "wx/mousestate.h"
#endif
class WXDLLIMPEXP_FWD_BASE wxArrayString;
@@ -208,74 +210,6 @@ WXDLLIMPEXP_CORE bool wxGetKeyState(wxKeyCode key);
// in wxMSW.
WXDLLIMPEXP_CORE bool wxSetDetectableAutoRepeat( bool flag );
// wxMouseState is used to hold information about button and modifier state
// and is what is returned from wxGetMouseState.
class WXDLLIMPEXP_CORE wxMouseState
{
public:
wxMouseState()
: m_x(0), m_y(0),
m_leftDown(false), m_middleDown(false), m_rightDown(false),
m_aux1Down(false), m_aux2Down(false),
m_controlDown(false), m_shiftDown(false), m_altDown(false),
m_metaDown(false)
{}
wxCoord GetX() const { return m_x; }
wxCoord GetY() const { return m_y; }
wxPoint GetPosition() const { return wxPoint(m_x, m_y); }
bool LeftDown() const { return m_leftDown; }
bool MiddleDown() const { return m_middleDown; }
bool RightDown() const { return m_rightDown; }
bool Aux1Down() const { return m_aux1Down; }
bool Aux2Down() const { return m_aux2Down; }
bool ControlDown() const { return m_controlDown; }
bool ShiftDown() const { return m_shiftDown; }
bool AltDown() const { return m_altDown; }
bool MetaDown() const { return m_metaDown; }
bool CmdDown() const
{
#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 SetAux1Down(bool down) { m_aux1Down = down; }
void SetAux2Down(bool down) { m_aux2Down = 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,
m_y;
bool m_leftDown : 1;
bool m_middleDown : 1;
bool m_rightDown : 1;
bool m_aux1Down : 1;
bool m_aux2Down : 1;
bool m_controlDown : 1;
bool m_shiftDown : 1;
bool m_altDown : 1;
bool m_metaDown : 1;
};
// Returns the current state of the mouse position, buttons and modifers
WXDLLIMPEXP_CORE wxMouseState wxGetMouseState();