Free Electron
WindowEvent.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __window_WindowEvent_h__
8 #define __window_WindowEvent_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 #ifndef FE_EVENT_LAYOUT
16 #define FE_EVENT_LAYOUT "WindowEvent"
17 #endif
18 
19 /**************************************************************************//**
20  @brief Generalized windowing event
21 
22  @ingroup window
23 
24  This is a convenience class to access a window event Record.
25 
26  Instead of a massive enumeration, the events are interpreted into
27  a useful triple: source/item/state. This is not only more
28  manageable, but it lets you process events with simple masks.
29 
30  The source is where the event originated.
31 
32  The item is the aspect of the source that was affected.
33 
34  The state is how the aspect was affected.
35  A few events merit a second state value, given by state2.
36 
37  The mouse positions and button states are also available for most events.
38  Not all events necessarily merit mouse information.
39 
40  Several simple convenience functions of the form is*() are provided to
41  check common events or event masks.
42 *//***************************************************************************/
43 class FE_DL_EXPORT WindowEvent: public RecordView
44 {
45  public:
46  Functor<I32> eventSource;
47  Functor<I32> eventItem;
48  Functor<I32> eventState;
49  Functor<I32> eventState2;
50  Functor<I32> eventMouseX;
51  Functor<I32> eventMouseY;
52  Functor<I32> eventMouseButtons;
53 
54 
55  /******************************************************************//**
56  @brief Source of a window event
57  *//*******************************************************************/
58  typedef enum
59  {
60  e_sourceNull= 0x0000,
61  e_sourceMousePosition= 0x0001,
62  e_sourceMouseButton= 0x0002,
63  e_sourceKeyboard= 0x0004,
64  e_sourceSystem= 0x0008,
65  e_sourceUser= 0x0010,
66  e_sourceJoy0= 0x0100,
67  e_sourceJoy1= 0x0200,
68  e_sourceJoy2= 0x0400,
69  e_sourceJoy3= 0x0800,
70  e_sourceWheel0= 0x1000,
71  e_sourceWheel1= 0x2000,
72  e_sourceWheel2= 0x4000,
73  e_sourceWheel3= 0x8000,
74  e_sourceAny= 0x7fffffff
75  } Source;
76 
77  /******************************************************************//**
78  @brief Aspect of change in a window event
79  *//*******************************************************************/
80  typedef enum
81  {
82  e_itemNull= 0x00,
83  e_itemAny= 0x7fffffff,
84 
85  //* e_sourceMousePosition
86  e_itemMove= 0x01,
87  e_itemDrag= 0x02,
88 
89  //* e_sourceMouseButton
90  e_itemLeft= 0x01,
91  e_itemMiddle= 0x02,
92  e_itemRight= 0x04,
93  e_itemWheel= 0x08,
94 
95  //* e_sourceKeyboard
96  e_keyBackspace= 0x08,
97  e_keyTab= 0x09,
98  e_keyLineFeed= 0x0A,
99  e_keyCarriageReturn= 0x0D,
100  e_keySpace= 0x20,
101  e_keyEscape= 0x1B,
102  e_keyDelete= 0x7F,
103  e_keyAsciiMask= 0xFF,
104  e_keyCursor= 0x100,
105  e_keyCursorUp= 0x101,
106  e_keyCursorDown= 0x102,
107  e_keyCursorLeft= 0x103,
108  e_keyCursorRight= 0x104,
109  e_keyCursorMask= 0x107,
110  e_keyFunction= 0x200,
111  e_keyFunction1= 0x201,
112  e_keyFunction2= 0x202,
113  e_keyFunction3= 0x203,
114  e_keyFunction4= 0x204,
115  e_keyFunction5= 0x205,
116  e_keyFunction6= 0x206,
117  e_keyFunction7= 0x207,
118  e_keyFunction8= 0x208,
119  e_keyFunction9= 0x209,
120  e_keyFunction10= 0x20A,
121  e_keyFunction11= 0x20B,
122  e_keyFunction12= 0x20C,
123  e_keyFunctionMask= 0x20F,
124  e_keyShift= 1<<24,
125  e_keyCapLock= 1<<25,
126  e_keyControl= 1<<26,
127  e_keyAlt= 1<<27,
128  e_keyModifiers= (e_keyShift|e_keyCapLock|
129  e_keyControl|e_keyAlt),
130  e_keyUsed= 0x1FFF,
131 
132  //* e_sourceSystem
133  e_itemIdle= 0x01,
134  e_itemResize= 0x02,
135  e_itemExpose= 0x04,
136  e_itemCloseWindow= 0x08,
137  e_itemQuit= 0x10, //* TODO Win32
138  e_itemPoll= 0x20, //* allow devices to produce events
139  e_itemEnter= 0x40,
140  e_itemExit= 0x80,
141 
142  //* e_sourceUser
143  e_windowClear= 0x01,
144  e_windowSwapBuffers= 0x02,
145 
146  //* e_sourceJoy
147  e_joyLeftStickX= 0x0000001,
148  e_joyLeftStickY= 0x0000002,
149  e_joyLeftPadX= 0x0000004,
150  e_joyLeftPadY= 0x0000008,
151  e_joyRightStickX= 0x0000010,
152  e_joyRightStickY= 0x0000020,
153  e_joyLeftTrigger= 0x0000040,
154  e_joyRightTrigger= 0x0000080,
155 
156  e_joySelect= 0x0000100,
157  e_joyBack= 0x0000100, //* Back is the new Select
158  e_joyGuide= 0x0000200,
159  e_joyStart= 0x0000400,
160 
161  e_joyLeftUp= 0x0001000,
162  e_joyLeftDown= 0x0002000,
163  e_joyLeftLeft= 0x0004000,
164  e_joyLeftRight= 0x0008000,
165 
166  e_joyLeftHigh= 0x0010000,
167  e_joyLeftLow= 0x0020000,
168  e_joyLeftDepress= 0x0040000,
169 
170  e_joyRightUp= 0x0100000,
171  e_joyRightDown= 0x0200000,
172  e_joyRightLeft= 0x0400000,
173  e_joyRightRight= 0x0800000,
174 
175  e_joyRightHigh= 0x1000000,
176  e_joyRightLow= 0x2000000,
177  e_joyRightDepress= 0x4000000,
178 
179  e_joyAxes= 0x00000FF,
180  e_joyButtons= 0xFF7F700,
181  e_joyAny= 0xFF7F7FF,
182 
183  e_JoyWheel= e_joyLeftStickX,
184  e_JoyClutch= e_joyLeftStickY,
185  e_JoyGasPedal= e_joyRightStickX,
186  e_JoyBrake= e_joyRightStickY,
187  e_JoyShifterRight= e_joyLeftLow,
188  e_JoyShifterLeft= e_joyRightLow,
189  } Item;
190 
191  /******************************************************************//**
192  @brief Effect of change in a window event
193  *//*******************************************************************/
194  typedef enum
195  {
196  e_stateForceSigned= -1, //* force enum to be signed
197  e_stateNull= 0x00,
198  e_statePress= 0x01, //* key or mousebutton
199  e_stateRelease= 0x02, //* key or mousebutton
200  e_stateRepeat= 0x04, //* key held down
201  e_stateAny= 0x7fffffff,
202 
203  e_state2ClickGeneric= 0x00,
204  e_state2ClickSingle= 0x01,
205  e_state2ClickDouble= 0x02,
206  e_state2ClickTriple= 0x03,
207 
208  e_state2NoRelease= 0x04,
209  } State;
210 
211  /******************************************************************//**
212  @brief State of the mouse buttons during an event
213  *//*******************************************************************/
214  typedef enum
215  {
216  e_mbNone= 0x00,
217  e_mbLeft= 0x01,
218  e_mbMiddle= 0x02,
219  e_mbRight= 0x04,
220  e_mbAll= 0x07,
221  e_mbAny= 0x7fffffff,
222  } MouseButtons;
223 
224  class Mask
225  {
226  public:
227  Mask( void)
228  {
229  m_source=e_sourceNull;
230  m_item=e_itemNull;
231  m_state=e_stateNull;
232  }
233  Mask( Source source, Item item, State state)
234  {
235  m_source=source;
236  m_item=item;
237  m_state=state;
238  }
239  bool operator <(const Mask &a_other) const
240  {
241  if(a_other.m_source > m_source) { return true; }
242  if(a_other.m_source < m_source) { return false; }
243  if(a_other.m_item > m_item) { return true; }
244  if(a_other.m_item < m_item) { return false; }
245  if(a_other.m_state > m_state) { return true; }
246  if(a_other.m_state < m_state) { return false; }
247  return false;
248  }
249  Source source(void) const
250  { return m_source; }
251  void setSource(Source value)
252  { m_source=value; }
253  void orSource(Source value)
254  { m_source=(WindowEvent::Source)(m_source|value); }
255  Item item(void) const
256  { return m_item; }
257  void setItem(Item value)
258  { m_item=value; }
259  void orItem(Item value)
260  { m_item=(WindowEvent::Item)(m_item|value); }
261  State state(void) const
262  { return m_state; }
263  void setState(State value)
264  { m_state=value; }
265  void orState(State value)
266  { m_state=(WindowEvent::State)(m_state|value); }
267  void setSIS(Source source, Item item, State state)
268  {
269  m_source=source;
270  m_item=item;
271  m_state=state;
272  }
273  private:
274  Source m_source;
275  Item m_item;
276  State m_state;
277  };
278 static WindowEvent::Mask stringToMask(const String &a_string);
279 
280  WindowEvent(void) { setName(FE_EVENT_LAYOUT); }
281 
282 virtual void addFunctors(void)
283  {
284  add(eventSource,
285  FE_SPEC("evt:source",
286  "Origin of change (alleged)"));
287  add(eventItem,
288  FE_SPEC("evt:item","Aspect of change"));
289  add(eventState,
290  FE_SPEC("evt:state","Effect of change"));
291  add(eventState2,
292  FE_SPEC("evt:state2","Orthogonal effect"));
293  add(eventMouseX,
294  FE_SPEC("evt:mouseX","X of mouse (optional)"));
295  add(eventMouseY,
296  FE_SPEC("evt:mouseY","Y of mouse (optional)"));
297  add(eventMouseButtons,
298  FE_SPEC("evt:mButtons",
299  "Mouse buttons (optional)"));
300  }
301 
302 virtual void initializeRecord(void) { reset(); }
303 
304  bool operator==(const WindowEvent &other) const
305  {
306  if( source() != e_sourceAny
307  && other.source() != e_sourceAny
308  && source() != other.source())
309  {
310  return false;
311  }
312  if( item() != e_itemAny
313  && other.item() != e_itemAny
314  && item() != other.item())
315  {
316  return false;
317  }
318  if( state() != e_stateAny
319  && other.state() != e_stateAny
320  && state() != other.state())
321  {
322  return false;
323  }
324  return true;
325  }
326 
327  bool operator<(const WindowEvent &other) const
328  {
329  if(source() != e_sourceAny
330  && other.source() != e_sourceAny)
331  {
332  if(source() < other.source())
333  {
334  return true;
335  }
336  if(source() > other.source())
337  {
338  return false;
339  }
340  }
341 
342  if(item() != e_itemAny
343  && other.item() != e_itemAny)
344  {
345  if(item() < other.item())
346  {
347  return true;
348  }
349  if(item() > other.item())
350  {
351  return false;
352  }
353  }
354 
355  if(state() != e_stateAny
356  && other.state() != e_stateAny)
357  {
358  if(state() < other.state())
359  {
360  return true;
361  }
362  if(state() > other.state())
363  {
364  return false;
365  }
366  }
367 
368  return false;
369  }
370 
371  Source source(void) const
372  { return Source(eventSource.check()?
373  eventSource(): e_sourceNull); }
374  void setSource(Source value)
375  { eventSource()=value; }
376 
377  Item item(void) const
378  { return Item(eventItem.check()?
379  eventItem(): e_itemNull); }
380  void setItem(Item value)
381  { eventItem()=value; }
382 
383  State state(void) const
384  { return State(eventState.check()?
385  eventState(): e_stateNull); }
386  void setState(State value)
387  { eventState()=value; }
388 
389  State state2(void) const
390  { return State(eventState2.check()?
391  eventState2(): e_stateNull); }
392  void setState2(State value)
393  { eventState2()=value; }
394 
395  I32 mouseX(void) const
396  { return eventMouseX.check()? eventMouseX(): -1; }
397  void setMouseX(I32 value)
398  { eventMouseX()=value; }
399 
400  I32 mouseY(void) const
401  { return eventMouseY.check()? eventMouseY(): -1; }
402  void setMouseY(I32 value)
403  { eventMouseY()=value; }
404 
405  MouseButtons mouseButtons(void) const
406  { return MouseButtons(eventMouseButtons.check()?
407  eventMouseButtons(): e_mbNone); }
408  void setMouseButtons(MouseButtons value)
409  { eventMouseButtons()=value; }
410 
411  void reset(void)
412  {
413  setSource(e_sourceNull);
414  setItem(e_itemNull);
415  setState(e_stateNull);
416  setState2(e_stateNull);
417  setMousePosition(0,0);
418  setMouseButtons(e_mbNone);
419  }
420 
421  void getMousePosition(I32 *pMx,I32 *pMy) const
422  { *pMx=mouseX();
423  *pMy=mouseY(); }
424 
425  void setMousePosition(I32 mx,I32 my)
426  { setMouseX(mx);
427  setMouseY(my); }
428 
429  void setSIS(Source source,Item item,State state)
430  { setSource(source);
431  setItem(item);
432  setState(state); }
433 
434  BWORD matchesSource(Source sourceMask) const
435  { return sourceMask==source() || sourceMask==e_sourceAny;}
436  BWORD matchesItem(Item itemMask) const
437  { return itemMask==item() || itemMask==e_itemAny ||
438  itemMask==(item()&(e_itemAny^e_keyModifiers)); }
439  BWORD matchesItemUnmodified(Item itemMask) const
440  { return itemMask==item() || itemMask==e_itemAny; }
441  BWORD matchesState(State stateMask) const
442  { return stateMask==state() || stateMask==e_stateAny; }
443 
444  BWORD is(Source sourceMask,Item itemMask,State stateMask) const
445  { return matchesSource(sourceMask) &&
446  matchesItem(itemMask) &&
447  matchesState(stateMask); }
448  BWORD is(const WindowEvent &mask) const
449  { return matchesSource(mask.source()) &&
450  matchesItem(mask.item()) &&
451  matchesState(mask.state()); }
452  BWORD is(const Mask &mask) const
453  { return matchesSource(mask.source()) &&
454  matchesItem(mask.item()) &&
455  matchesState(mask.state()); }
456  BWORD isUnmodified(Source sourceMask,Item itemMask,
457  State stateMask) const
458  { return matchesSource(sourceMask) &&
459  matchesItemUnmodified(itemMask) &&
460  matchesState(stateMask); }
461  BWORD isModified(void)
462  { return (item()&e_keyModifiers)!=0x0; }
463  BWORD isModifiedBy(Item itemMask)
464  { return itemMask==(item()&e_keyModifiers); }
465 
466  BWORD isNull(void) const
467  { return is(e_sourceNull,e_itemAny,e_stateAny); }
468  BWORD isExpose(void) const
469  { return is(e_sourceSystem,e_itemExpose,e_stateAny);}
470  BWORD isResize(void) const
471  { return is(e_sourceSystem,e_itemResize,e_stateAny);}
472  BWORD isCloseWindow(void) const
473  { return is(e_sourceSystem,e_itemCloseWindow,e_stateAny);}
474  BWORD isIdleMouse(void) const
475  { return is(e_sourceSystem,e_itemIdle,e_stateAny); }
476  BWORD isPoll(void) const
477  { return is(e_sourceSystem,e_itemPoll,e_stateAny); }
478  BWORD isContinuous(void) const
479  { return isIdleMouse() || isPoll(); }
480  BWORD isMouseEnter(void) const
481  { return is(e_sourceSystem,e_itemEnter,e_stateNull); }
482  BWORD isMouseExit(void) const
483  { return is(e_sourceSystem,e_itemExit,e_stateNull); }
484  BWORD isMouseMotion(void) const
485  { return is(e_sourceMousePosition,e_itemAny,e_stateAny); }
486  BWORD isMouseMove(void) const
487  { return is(e_sourceMousePosition,e_itemMove,e_stateAny);}
488  BWORD isMouseDrag(void) const
489  { return is(e_sourceMousePosition,e_itemDrag,e_stateAny);}
490  BWORD isKeyboard() const
491  { return is(e_sourceKeyboard,e_itemAny,e_stateAny);}
492  BWORD isKeyPress(Item key) const
493  { return is(e_sourceKeyboard,key,e_statePress); }
494  BWORD isKeyRepeat(Item key) const
495  { return is(e_sourceKeyboard,key,e_stateRepeat); }
496  BWORD isKeyRelease(Item key) const
497  { return is(e_sourceKeyboard,key,e_stateRelease); }
498  BWORD isKeyTyped(Item key) const
499  { return isKeyPress(key) || isKeyRepeat(key); }
500  BWORD isMouseButton(Item item=e_itemAny) const
501  { return is(e_sourceMouseButton,item,e_stateAny); }
502  BWORD isMousePress(Item item=e_itemAny) const
503  { return is(e_sourceMouseButton,item,e_statePress); }
504  BWORD isMouseRelease(Item item=e_itemAny) const
505  { return is(e_sourceMouseButton,item,e_stateRelease); }
506  BWORD isMouseWheel(void) const
507  { return is(e_sourceMouseButton,e_itemWheel,e_stateAny); }
508  BWORD isClear(void) const
509  { return is(e_sourceUser,e_windowClear,e_stateAny); }
510  BWORD isSwap(void) const
511  { return is(e_sourceUser,e_windowSwapBuffers,e_stateAny);}
512  BWORD isSystem(void) const
513  { return matchesSource(e_sourceSystem); }
514  BWORD isUser(void) const
515  { return matchesSource(e_sourceUser); }
516  BWORD isJoy(void) const
517  { return matchesSource(e_sourceJoy0); }
518  BWORD isJoyAxis(void) const
519  { return isJoy() && (item()&e_joyAxes); }
520  BWORD isJoyButton(void) const
521  { return is(e_sourceJoy0,e_joyButtons,e_stateAny);}
522 
523  /** @brief Returns TRUE if item can be affected by
524  shift/control/caplock */
525  BWORD usesModifiers(void) const
526  { return (source()==e_sourceMousePosition ||
527  source()==e_sourceMouseButton ||
528  source()==e_sourceKeyboard); }
529 };
530 
531 } /* namespace ext */
532 } /* namespace fe */
533 
534 namespace fe
535 {
536 
537 FE_DL_EXPORT String print(const ext::WindowEvent& event);
538 
539 } /* namespace fe */
540 
541 #endif /* __window_WindowEvent_h__ */
virtual void addFunctors(void)
Called at instantiation to add functors.
Definition: WindowEvent.h:282
MouseButtons
State of the mouse buttons during an event.
Definition: WindowEvent.h:214
Item
Aspect of change in a window event.
Definition: WindowEvent.h:80
kernel
Definition: namespace.dox:3
Bound accessor in a RecordView.
Definition: RecordView.h:147
Source
Source of a window event.
Definition: WindowEvent.h:58
State
Effect of change in a window event.
Definition: WindowEvent.h:194
virtual void initializeRecord(void)
Called at instantiation to initialize attributes.
Definition: WindowEvent.h:302
BWORD usesModifiers(void) const
Returns TRUE if item can be affected by shift/control/caplock.
Definition: WindowEvent.h:525
Generalized windowing event.
Definition: WindowEvent.h:43
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s.
Definition: DualString.h:208
Automatically reference-counted string container.
Definition: String.h:128
Bindable collection of accessor Functors.
Definition: RecordView.h:106