core/vgui/vgui_event.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_event.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief  See vgui_event.h for a description of this file.
00008 // \author Philip C. Pritchett, Robotics Research Group, University of Oxford
00009 // \date   11 Sep 99
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   11-SEP-1999 P.Pritchett - Initial version.
00014 // \endverbatim
00015 
00016 #include "vgui_event.h"
00017 #include <vcl_iostream.h>
00018 #include <vul/vul_get_timestamp.h>
00019 
00020 //----------------------------------------------------------------------------
00021 //: Initialise default event.
00022 void vgui_event::init()
00023 {
00024   type = vgui_EVENT_NULL;
00025   button = vgui_BUTTON_NULL;
00026   key = vgui_KEY_NULL;
00027   ascii_char = vgui_KEY_NULL;
00028   modifier = vgui_MODIFIER_NULL;
00029   wx = 0;
00030   wy = 0;
00031   {
00032     static int secs_0 = -1;
00033     static int msecs_0 = -1;
00034     int secs_now, msecs_now;
00035     vul_get_timestamp(secs_now, msecs_now);
00036     if (secs_0 == -1 && msecs_0 == -1)
00037       secs_0 = secs_now, msecs_0 = msecs_now;
00038     timestamp = 1000*(secs_now - secs_0) + (msecs_now - msecs_0);
00039   }
00040   origin = 0;
00041   timer_id = 0;
00042   str = "";
00043   user = 0;
00044   data = 0;
00045 }
00046 
00047 //----------------------------------------------------------------------------
00048 //: Constructor - create an event of the given type.
00049 vgui_event::vgui_event(vgui_event_type etype)
00050 {
00051   init();
00052   type = etype;
00053 }
00054 
00055 //----------------------------------------------------------------------------
00056 //: Convert the given key to lower case and use that to set the key.
00057 //  I added this to avoid the complication of doing this conversion in each
00058 //  GUI impl - kym.
00059 void vgui_event::set_key(vgui_key c)
00060 {
00061   if (c >= 'A' && c <= 'Z')
00062   {
00063     // Convert upper case to lower case
00064     key = vgui_key(c + 'a' - 'A');
00065   }
00066   else
00067   {
00068     key = vgui_key(c);
00069   }
00070 }
00071 
00072 //----------------------------------------------------------------------------
00073 bool vgui_event::modifier_is_down(int mods) const
00074 {
00075   return (mods & modifier) == mods;
00076 }
00077 
00078 //----------------------------------------------------------------------------
00079 double vgui_event::secs_since(vgui_event const& e) const
00080 {
00081   return (this->timestamp - e.timestamp) * 1e-3;
00082 }
00083 
00084 //----------------------------------------------------------------------------
00085 long vgui_event::usecs_since(vgui_event const& e) const
00086 {
00087   return long(this->timestamp - e.timestamp) * 1000;
00088 }
00089 
00090 //----------------------------------------------------------------------------
00091 static struct
00092 {
00093   vgui_event_type t;
00094   char const *name;
00095 } fsm_event_table[] = {
00096 #if defined(macro)
00097 # error blah
00098 #endif
00099 #define macro(e) {vgui_ ## e,#e}
00100   // doing it this way means we don't rely on the event types being
00101   // enummed in any particular order (the code had that particular
00102   // bug before I changed it). fsm.
00103   macro(EVENT_NULL),
00104   macro(ENTER),
00105   macro(LEAVE),
00106   macro(BUTTON_DOWN),
00107   macro(BUTTON_UP),
00108   macro(MOTION),
00109   macro(KEY_PRESS),
00110   macro(KEY_RELEASE),
00111   macro(RESHAPE),
00112   macro(TIMER),
00113   macro(DRAW),
00114   macro(DRAW_OVERLAY),
00115   macro(STRING),
00116   macro(HSCROLL),
00117   macro(VSCROLL),
00118   macro(DESTROY),
00119   macro(OTHER)
00120 #undef macro
00121 };
00122 static const int fsm_event_table_size = sizeof(fsm_event_table)/sizeof(fsm_event_table[0]);
00123 
00124 //-----------------------------------------------------------------------------
00125 vcl_ostream& operator<<(vcl_ostream& s, vgui_event_type t)
00126 {
00127   for (int i=0; i<fsm_event_table_size; ++i)
00128     if (fsm_event_table[i].t == t)
00129       return s << fsm_event_table[i].name;
00130   return s << "[" __FILE__ " : bad event, code " << int(t) << ']';
00131 }
00132 
00133 //-----------------------------------------------------------------------------
00134 vcl_ostream& operator<<(vcl_ostream& s, vgui_event const& e)
00135 {
00136   s << "[type:" << e.type;
00137   if (e.key != vgui_KEY_NULL) s << ", key:" << vgui_key(e.key);
00138   if (e.ascii_char != 0) s << ", ascii_char: " << vgui_key(e.ascii_char);
00139   if (e.button != vgui_BUTTON_NULL) s << ", button:" << e.button;
00140   if (e.modifier != vgui_MODIFIER_NULL) s << ", modifiers:" << vgui_modifier(e.modifier);
00141   s << ", w(" << e.wx << ',' << e.wy << ')'
00142     << ", time:" << e.timestamp << "ms";
00143   if (e.str != "") s << ", vcl_string:\"" << e.str << "\"";
00144   return s << ']';
00145 }
00146 
00147 //-----------------------------------------------------------------------------
00148 //: Returns true if events are the same.
00149 //  Isn't this what the compiler would have generated anyway?
00150 //  moreover, the compiler-generated one wouldn't need to be
00151 //  updated when the fields are changed. fsm.
00152 bool operator==(vgui_event const& a, vgui_event const& b)
00153 {
00154   return  a.type    == b.type &&
00155           a.button  == b.button &&
00156           a.key     == b.key &&
00157           a.modifier== b.modifier &&
00158           a.wx      == b.wx &&
00159           a.wy      == b.wy &&
00160           a.origin  == b.origin &&
00161           a.timer_id== b.timer_id &&
00162           a.str     == b.str &&
00163           a.user    == b.user &&
00164           a.data    == b.data;
00165 }
00166