]> icculus.org git repositories - dana/openbox.git/blob - src/bindings.hh
add some const
[dana/openbox.git] / src / bindings.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef __binding_hh
3 #define __binding_hh
4
5 /*! @file binding.hh
6   @brief I dunno.. some binding stuff?
7 */
8
9 #include <string>
10 #include <vector>
11
12 namespace ob {
13
14 typedef struct Binding {
15   unsigned int modifiers;
16   unsigned int key;
17
18   bool operator==(struct Binding &b2) { return key == b2.key &&
19                                           modifiers == b2.modifiers; }
20   bool operator!=(struct Binding &b2) { return key != b2.key ||
21                                           modifiers != b2.modifiers; }
22   Binding(unsigned int mod, unsigned int k) { modifiers = mod; key = k; }
23 } Binding;
24
25 typedef struct BindingTree {
26   Binding binding;
27   std::string text;
28   int id;     // the id given for the binding in add()
29   bool chain; // true if this is a chain to another key (not an action)
30
31   struct BindingTree *next_sibling; // the next binding in the tree at the same
32                                     // level
33   struct BindingTree *first_child;  // the first child of this binding (next
34                                     // binding in a chained sequence).
35   BindingTree(int id) : binding(0, 0) {
36     this->id = id; chain = true; next_sibling = first_child = 0;
37   }
38   BindingTree() : binding(0, 0) {
39     this->id = -1; chain = true; next_sibling = first_child = 0;
40   }
41 } BindingTree;
42
43 class OBBindings {
44 public:
45   //! A list of strings
46   typedef std::vector<std::string> StringVect;
47
48 private:
49   BindingTree _keytree; // root node of the tree (this doesn't have siblings!)
50   BindingTree *_curpos; // position in the keytree
51
52   BindingTree *_mousetree; // this tree is a list. it has only siblings
53   
54   int find_key(BindingTree *search) const;
55   bool translate(const std::string &str, Binding &b, bool askey) const;
56   BindingTree *buildtree(const StringVect &keylist, int id) const;
57   void OBBindings::assimilate(BindingTree *node);
58  
59 public:
60   //! Initializes an OBBinding object
61   OBBindings();
62   //! Destroys the OBBinding object
63   virtual ~OBBindings();
64
65   //! Adds a new mouse binding
66   /*!
67     A binding will fail to be added if the binding already exists, or if the
68     string is invalid.    
69     @return true if the binding could be added; false if it could not.
70   */
71   bool add_mouse(const std::string &button, int id);
72
73   //! Removes a mouse binding
74   /*!
75     @return The id of the binding that was removed, or '< 0' if none were
76             removed.
77   */
78   int remove_mouse(const std::string &button);
79
80   //! Adds a new key binding
81   /*!
82     A binding will fail to be added if the binding already exists (as part of
83     a chain or not), or if any of the strings in the keylist are invalid.    
84     @return true if the binding could be added; false if it could not.
85   */
86   bool add_key(const StringVect &keylist, int id);
87
88   //! Removes a key binding
89   /*!
90     @return The id of the binding that was removed, or '< 0' if none were
91             removed.
92   */
93   int remove_key(const StringVect &keylist);
94
95   //! Removes all key bindings
96   void remove_all();
97
98   //! Finds a keybinding and returns its id or '< 0' if it isn't found.
99   /*!
100     @return -1 if the keybinding was not found but does not conflict with
101     any others; -2 if the keybinding conflicts with another.
102   */
103   int find_key(const StringVect &keylist);
104
105   void process(unsigned int modifiers, unsigned int key);
106
107   // XXX: need an exec() function or something that will be used by openbox
108   //      and hold state for which chain we're in etc. (it could have a timer
109   //      for reseting too...)
110
111   void display();
112 };
113
114 }
115
116 #endif // __binding_hh