1 // This is core/vgui/vgui_event.h
2 #ifndef vgui_event_h_
3 #define vgui_event_h_
4 //:
5 // \file
6 // \author Philip C. Pritchett, Robotics Research Group, University of Oxford
7 // \date   11 Sep 99
8 // \brief  vgui_event class encapsulates the events handled by the vgui system.
9 //
10 // \verbatim
11 //  Modifications
12 //   16-Sep-1999  fsm. various.
13 //    5-Oct-1999  fsm. replaced (x,y) by (wx,wy) and (ux,uy).
14 //   10-Oct-1999  pcp         added timestamp
15 //   20-Oct-1999  awf         Changed timestamp to int.
16 //   19-Oct-1999  fsm. added pointer to adaptor.
17 //    1-Nov-1999  fsm. events now use viewport, not window coordinates.
18 //   28-Nov-1999  fsm. added std::string event.
19 //   22-Aug-2000  Marko Bacic. added support for scroll bar events
20 //   04-Oct-2002  K.Y.McGaul - Added set_key() to make sure vgui_key is now
21 //                             always lower case to save confusion.
22 //                           - Added ascii_char value to vgui_event.
23 // \endverbatim
24 
25 #include <string>
26 #include <iosfwd>
27 #ifdef _MSC_VER
28 #  include <vcl_msvc_warnings.h>
29 #endif
30 #include "vgui_key.h"
31 #include "vgui_button.h"
32 #include "vgui_modifier.h"
33 class vgui_adaptor;
34 
35 enum vgui_event_type
36 {
37   vgui_EVENT_NULL = 0,
38   vgui_ENTER,
39   vgui_LEAVE,
40   vgui_BUTTON_DOWN,  /* */ vgui_MOUSE_DOWN = vgui_BUTTON_DOWN, vgui_MOUSE_PRESS = vgui_BUTTON_DOWN,
41   vgui_BUTTON_UP,    /* */ vgui_MOUSE_UP = vgui_BUTTON_UP, vgui_MOUSE_RELEASE = vgui_BUTTON_UP,
42   vgui_MOTION,       /* */ vgui_MOUSE_MOTION = vgui_MOTION,
43   vgui_KEY_PRESS,    /* */ vgui_KEY_DOWN = vgui_KEY_PRESS,
44   vgui_KEY_RELEASE,  /* */ vgui_KEY_UP = vgui_KEY_RELEASE,
45   vgui_RESHAPE,
46   vgui_TIMER,
47   vgui_DRAW,
48   vgui_DRAW_OVERLAY, /* */ vgui_OVERLAY_DRAW = vgui_DRAW_OVERLAY,
49   vgui_STRING,
50   vgui_HSCROLL,
51   vgui_VSCROLL,
52   vgui_DESTROY,
53   vgui_IDLE,
54   vgui_OTHER,
55   vgui_FOCUSGAINED,
56   vgui_FOCUSLOST,
57   vgui_WHEEL_UP,
58   vgui_WHEEL_DOWN,
59   vgui_EVENT_MAX    // This must be the last entry in the list
60 };
61 
62 std::ostream& operator<<(std::ostream& s, vgui_event_type e);
63 
64 //: The vgui_event class encapsulates the events handled by the vgui system.
65 //
66 // For key presses with modifiers the following standards apply:
67 // \verbatim
68 //         a   modifier = vgui_NULL   key = 'a'  ascii_char = 'a'
69 //    CTRL+a   modifier = vgui_CTRL   key = 'a'  ascii_char = '^A'
70 //   SHIFT+a   modifier = vgui_SHIFT  key = 'a'  ascii_char = 'A'
71 // \endverbatim
72 //
73 // We have decided to make it a standard that key is always lower case for
74 // simplicity.  In particular people have been defining impossible
75 // vgui_event_conditions, eg key='A', modifier=NULL (where NULL is the
76 // default modifier) and then wondering why SHIFT+a doesn't work.
77 //
78 // A new data type has been added (ascii_char) which holds the actual
79 // key stroke pressed by the user.
80 class vgui_event
81 {
82  public:
83   //: Constructor - create a default event.
vgui_event()84   vgui_event() { init(); }
85 
86   //: Constructor - create an event of the given type.
87   vgui_event(vgui_event_type);
88 
89   //: The type of event (key press, mouse motion, etc).
90   vgui_event_type type;
91 
92   //: Mouse button used (if it is a mouse event).
93   vgui_button button;
94 
95   //: The key pressed in lower case (if it is a key event).
96   vgui_key key;
97 
98   //: Convert given key to lower case and use that to set key.
99   void set_key(vgui_key c);
100 
101   //: Which modifiers are pressed during the event (NULL, CTRL, SHIFT).
102   vgui_modifier modifier;
103 
104   //: The actual key stroke pressed by the user.
105   vgui_key ascii_char;
106 
107   //: Position of the mouse pointer in viewport coordinates when event occurred.
108   int wx,wy;
109 
110   //: Timestamp in milliseconds since app started.
111   int timestamp;
112 
113   //: The adaptor from which the event came.
114   vgui_adaptor *origin;
115 
116   //: If the event is a timer event, this holds the ID.
117   // For an event of type vgui_TIMER, this field holds the name
118   // that was given when the timer request was posted.
119   int timer_id;
120 
121   //: A std::string message, for an event of type vgui_STRING.
122   //  An event of type vgui_STRING implies that
123   //  this field contains some sort of textual message. The exact
124   //  encoding of these messages is unspecified; the sender and the
125   //  receiver may use any protocol they like. Caveat : as a
126   //  corollary there is no guarantee that one protocol will not
127   //  clash with another.
128   std::string str;
129 
130   //: Type and data for events of type vgui_OTHER.
131   //  The fields user and data are used only when the event type is vgui_OTHER.
132   //  The 'user' field must uniquely identify the type of event, in the
133   //  sense that once the user field is known, the 'data' field can be
134   //  safely cast to point to the client data (type).
135   void const *user;
136   void const *data;
137 
138   // methods
139   bool modifier_is_down(int) const;
140   double secs_since(vgui_event const &) const;
141   long usecs_since(vgui_event const &) const;
142 
143  private:
144   void init();
145 };
146 
147 bool operator==(vgui_event const& a, vgui_event const& b);
148 std::ostream& operator<<(std::ostream&, vgui_event const&);
149 
150 #endif // vgui_event_h_
151