Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/src/bindings.hh

Go to the documentation of this file.
00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
00002 #ifndef __binding_hh
00003 #define __binding_hh
00004 
00005 /*! @file bindings.hh
00006   @brief I dunno.. some binding stuff?
00007 */
00008 
00009 #include "actions.hh"
00010 #include "python.hh"
00011 #include "otk/timer.hh"
00012 
00013 extern "C" {
00014 #include <Python.h>
00015 }
00016 
00017 #include <string>
00018 #include <list>
00019 #include <vector>
00020 
00021 namespace ob {
00022 
00023 class Client;
00024 
00025 typedef std::list<PyObject *> CallbackList;
00026 
00027 typedef struct Binding {
00028   unsigned int modifiers;
00029   unsigned int key;
00030 
00031   bool operator==(struct Binding &b2) { return key == b2.key &&
00032             modifiers == b2.modifiers; }
00033   bool operator!=(struct Binding &b2) { return key != b2.key ||
00034             modifiers != b2.modifiers; }
00035   Binding(unsigned int mod, unsigned int k) { modifiers = mod; key = k; }
00036 } Binding;
00037 
00038 typedef struct KeyBindingTree {
00039   Binding binding;
00040   CallbackList callbacks; // the callbacks given for the binding in add()
00041   bool chain;     // true if this is a chain to another key (not an action)
00042 
00043   struct KeyBindingTree *next_sibling; // the next binding in the tree at the same
00044                                     // level
00045   struct KeyBindingTree *first_child;  // the first child of this binding (next
00046                                     // binding in a chained sequence).
00047   KeyBindingTree() : binding(0, 0) {
00048     chain = true; next_sibling = first_child = 0;
00049   }
00050 } KeyBindingTree;
00051 
00052 typedef struct ButtonBinding {
00053   Binding binding;
00054   CallbackList callbacks[MouseAction::NUM_MOUSE_ACTION];
00055   ButtonBinding() : binding(0, 0) {}
00056 };
00057 
00058 class Bindings {
00059 public:
00060   //! A list of strings
00061   typedef std::vector<std::string> StringVect;
00062 
00063 private:
00064   // root node of the tree (this doesn't have siblings!)
00065   KeyBindingTree _keytree; 
00066   KeyBindingTree *_curpos; // position in the keytree
00067 
00068   Binding _resetkey; // the key which resets the key chain status
00069 
00070   otk::Timer *_timer;
00071   
00072   KeyBindingTree *find(KeyBindingTree *search, bool *conflict) const;
00073   KeyBindingTree *buildtree(const StringVect &keylist,
00074                             PyObject *callback) const;
00075   void assimilate(KeyBindingTree *node);
00076 
00077   static void resetChains(Bindings *self); // the timer's timeout function
00078 
00079   typedef std::list <ButtonBinding*> ButtonBindingList;
00080   ButtonBindingList _buttons[MouseContext::NUM_MOUSE_CONTEXT];
00081 
00082   void grabButton(bool grab, const Binding &b, MouseContext::MC context,
00083                   Client *client);
00084 
00085   CallbackList _eventlist[EventAction::NUM_EVENTS];
00086 
00087   PyObject *_keybgrab_callback;
00088   
00089 public:
00090   //! Initializes an Bindings object
00091   Bindings();
00092   //! Destroys the Bindings object
00093   virtual ~Bindings();
00094 
00095   //! Translates a binding string into the actual Binding
00096   bool translate(const std::string &str, Binding &b, bool askey = true) const;
00097   
00098   //! Adds a new key binding
00099   /*!
00100     A binding will fail to be added if the binding already exists (as part of
00101     a chain or not), or if any of the strings in the keylist are invalid.    
00102     @return true if the binding could be added; false if it could not.
00103   */
00104   bool addKey(const StringVect &keylist, PyObject *callback);
00105 
00106   ////! Removes a key binding
00107   ///*!
00108   //  @return The callbackid of the binding, or '< 0' if there was no binding to
00109   //          be removed.
00110   //*/
00111   //bool removeKey(const StringVect &keylist, PyObject *callback);
00112 
00113   //! Removes all key bindings
00114   void removeAllKeys();
00115 
00116   void fireKey(int screen, unsigned int modifiers,unsigned int key, Time time,
00117                KeyAction::KA action);
00118 
00119   void setResetKey(const std::string &key);
00120 
00121   void grabKeys(bool grab);
00122 
00123   bool grabKeyboard(int screen, PyObject *callback);
00124   void ungrabKeyboard();
00125 
00126   bool grabPointer(int screen);
00127   void ungrabPointer();
00128 
00129   bool addButton(const std::string &but, MouseContext::MC context,
00130                  MouseAction::MA action, PyObject *callback);
00131 
00132   void grabButtons(bool grab, Client *client);
00133 
00134   //! Removes all button bindings
00135   void removeAllButtons();
00136 
00137   void fireButton(MouseData *data);
00138 
00139   //! Bind a callback for an event
00140   bool addEvent(EventAction::EA action, PyObject *callback);
00141 
00142   //! Unbind the callback function from an event
00143   bool removeEvent(EventAction::EA action, PyObject *callback);
00144 
00145   //! Remove all callback functions
00146   void removeAllEvents();
00147 
00148   void fireEvent(EventData *data);
00149 };
00150 
00151 }
00152 
00153 #endif // __binding_hh

Generated on Tue Feb 4 22:58:57 2003 for Openbox by doxygen1.3-rc2