1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
6 @brief I dunno.. some binding stuff?
11 #include "otk/timer.hh"
25 typedef struct Binding {
26 unsigned int modifiers;
29 bool operator==(struct Binding &b2) { return key == b2.key &&
30 modifiers == b2.modifiers; }
31 bool operator!=(struct Binding &b2) { return key != b2.key ||
32 modifiers != b2.modifiers; }
33 Binding(unsigned int mod, unsigned int k) { modifiers = mod; key = k; }
36 typedef struct KeyBindingTree {
38 PyObject *callback; // the callback given for the binding in add()
39 bool chain; // true if this is a chain to another key (not an action)
41 struct KeyBindingTree *next_sibling; // the next binding in the tree at the same
43 struct KeyBindingTree *first_child; // the first child of this binding (next
44 // binding in a chained sequence).
45 KeyBindingTree(PyObject *callback) : binding(0, 0) {
46 this->callback = callback; chain = true; next_sibling = first_child = 0;
48 KeyBindingTree() : binding(0, 0) {
49 this->callback = 0; chain = true; next_sibling = first_child = 0;
53 typedef struct ButtonBinding {
55 PyObject *callback[NUM_MOUSE_ACTION];
56 ButtonBinding() : binding(0, 0) {
57 for(int i=0; i<NUM_MOUSE_ACTION; ++i) callback[i] = 0;
64 typedef std::vector<std::string> StringVect;
67 // root node of the tree (this doesn't have siblings!)
68 KeyBindingTree _keytree;
69 KeyBindingTree *_curpos; // position in the keytree
71 Binding _resetkey; // the key which resets the key chain status
75 PyObject *find(KeyBindingTree *search, bool *conflict) const;
76 KeyBindingTree *buildtree(const StringVect &keylist,
77 PyObject *callback) const;
78 void assimilate(KeyBindingTree *node);
80 static void resetChains(OBBindings *self); // the timer's timeout function
82 typedef std::list <ButtonBinding*> ButtonBindingList;
83 ButtonBindingList _buttons[NUM_MOUSE_CONTEXT];
85 void grabButton(bool grab, const Binding &b, MouseContext context,
88 PyObject *_events[NUM_EVENTS];
91 //! Initializes an OBBindings object
93 //! Destroys the OBBindings object
94 virtual ~OBBindings();
96 //! Translates a binding string into the actual Binding
97 bool translate(const std::string &str, Binding &b, bool askey = true) const;
99 //! Adds a new key binding
101 A binding will fail to be added if the binding already exists (as part of
102 a chain or not), or if any of the strings in the keylist are invalid.
103 @return true if the binding could be added; false if it could not.
105 bool addKey(const StringVect &keylist, PyObject *callback);
107 //! Removes a key binding
109 @return The callbackid of the binding, or '< 0' if there was no binding to
112 bool removeKey(const StringVect &keylist);
114 //! Removes all key bindings
115 void removeAllKeys();
117 void fireKey(unsigned int modifiers,unsigned int key, Time time);
119 void setResetKey(const std::string &key);
121 void grabKeys(bool grab);
123 bool addButton(const std::string &but, MouseContext context,
124 MouseAction action, PyObject *callback);
126 void grabButtons(bool grab, OBClient *client);
128 //! Removes all button bindings
129 void removeAllButtons();
131 void fireButton(ButtonData *data);
133 //! Bind a callback for an event
134 bool addEvent(EventAction action, PyObject *callback);
136 //! Unbind the callback function from an event
137 bool removeEvent(EventAction action);
139 //! Remove all callback functions
140 void removeAllEvents();
142 void fireEvent(EventData *data);
147 #endif // __binding_hh