core/vgui/vgui_event_condition.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_event_condition.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 // \brief  See vgui_event_condition.h for a description of this file.
00009 
00010 
00011 #include "vgui_event_condition.h"
00012 
00013 #include <vcl_sstream.h>
00014 #include <vgui/vgui_event.h>
00015 
00016 //----------------------------------------------------------------------------
00017 // Initialise event condition and check for impossible events.
00018 void vgui_event_condition::init(vgui_key k, vgui_key a,
00019                                 vgui_button b, vgui_modifier m,
00020                                 bool is_pressed, bool is_on, event_types h)
00021 {
00022   on = is_on;
00023   pressed = is_pressed;
00024   key = k;
00025   ascii_char = a;
00026   button = b;
00027   modifier = m;
00028   how_checked = h;
00029 
00030   // key should always be lower case.
00031   if (k > 0 && k < 32)
00032   {
00033     // Convert control character to lower case and force the control
00034     // modifier.
00035     key = vgui_key(k + 'a' -1);
00036     modifier = vgui_CTRL;
00037   }
00038 
00039   if (k >= 'A' && k <= 'Z')
00040   {
00041     // Convert upper to lower case and force the shift modifier.
00042     key = vgui_key(k + 'a' - 'A');
00043     modifier = vgui_SHIFT;
00044   }
00045 }
00046 
00047 //----------------------------------------------------------------------------
00048 //: Default constructor.
00049 vgui_event_condition::vgui_event_condition()
00050 {
00051   init(vgui_KEY_NULL, vgui_KEY_NULL, vgui_BUTTON_NULL, vgui_MODIFIER_NULL,
00052        false, false, null_event);
00053 }
00054 
00055 //----------------------------------------------------------------------------
00056 //: Constructor for a key press event condition (using ascii char).
00057 vgui_event_condition::vgui_event_condition(vgui_key ascii_code,
00058                                            bool is_pressed)
00059 {
00060   init (vgui_KEY_NULL, ascii_code, vgui_BUTTON_NULL, vgui_MODIFIER_NULL,
00061         is_pressed, true, ascii_char_event);
00062 }
00063 
00064 //----------------------------------------------------------------------------
00065 //: Constructor for a key press event condition (using key and modifier).
00066 vgui_event_condition::vgui_event_condition(vgui_key k, vgui_modifier m, bool p)
00067 {
00068   init(k, vgui_KEY_NULL, vgui_BUTTON_NULL, m, p, true, key_event);
00069 }
00070 
00071 //----------------------------------------------------------------------------
00072 //: Constructor for a mouse button press event condition.
00073 vgui_event_condition::vgui_event_condition(vgui_button b, vgui_modifier m,
00074                                            bool p)
00075 {
00076   init(vgui_KEY_NULL, vgui_KEY_NULL, b, m, p, true, mouse_event);
00077 }
00078 
00079 //----------------------------------------------------------------------------
00080 bool vgui_event_condition::operator()(vgui_event const &e) const
00081 {
00082   if (! on)
00083     return false;
00084   else if (e.type == vgui_KEY_PRESS)
00085   {
00086     if (how_checked == key_event)
00087       return pressed && e.key == key && e.modifier == modifier;
00088     else if (how_checked == ascii_char_event)
00089       return pressed && e.ascii_char == ascii_char;
00090     else
00091       return false;
00092   }
00093   else if (e.type == vgui_KEY_RELEASE)
00094   {
00095     if (how_checked == key_event)
00096       return !pressed && e.key == key && e.modifier == modifier;
00097     else if (how_checked == ascii_char_event)
00098       return !pressed && e.ascii_char == ascii_char;
00099     else
00100       return false;
00101   }
00102   else if (e.type == vgui_MOUSE_DOWN)
00103     return how_checked == mouse_event && pressed && e.button == button
00104     && e.modifier == modifier;
00105   else if (e.type == vgui_MOUSE_UP)
00106     return how_checked == mouse_event && !pressed && e.button == button
00107     && e.modifier == modifier;
00108   else
00109     return false;
00110 }
00111 
00112 //----------------------------------------------------------------------------
00113 bool vgui_event_condition::operator()(vgui_key k, vgui_modifier m) const
00114 {
00115   if (! on)
00116     return false;
00117   return k == key && m == modifier;
00118 }
00119 
00120 
00121 //----------------------------------------------------------------------------
00122 bool vgui_event_condition::operator()(vgui_button b, vgui_modifier m) const
00123 {
00124   if (! on)
00125     return false;
00126   return b == button && m == modifier;
00127 }
00128 
00129 //----------------------------------------------------------------------------
00130 //: Return a string describing the event condition.
00131 vcl_string vgui_event_condition::as_string(int  /*field_width*/) const
00132 {
00133   vcl_string r;
00134   if (modifier & vgui_SHIFT)
00135     r += "shift ";
00136   if (modifier & vgui_CTRL)
00137     r += "ctrl ";
00138   if (modifier & vgui_META)
00139     r += "meta ";
00140   if (modifier & vgui_ALT)
00141     r += "alt ";
00142 
00143   if (button == vgui_LEFT) r += "left";
00144   if (button == vgui_MIDDLE) r += "middle";
00145   if (button == vgui_RIGHT) r += "right";
00146 
00147   if (vgui_key/*egcs for i386 needs cast*/(key) != vgui_KEY_NULL) {
00148     vcl_ostringstream s;
00149     s << key;
00150     r += s.str();
00151   }
00152   if (vgui_key/*egcs for i386 needs cast*/(ascii_char) != vgui_KEY_NULL) {
00153     vcl_ostringstream s;
00154     s << ascii_char;
00155     r += s.str();
00156   }
00157 
00158   return r;
00159 }