+
+bool OBBindings::addButton(const std::string &but, MouseContext context,
+ MouseAction action, PyObject *callback)
+{
+ assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
+
+ Binding b(0,0);
+ if (!translate(but, b, false))
+ return false;
+
+ ButtonBindingList::iterator it, end = _buttons[context].end();
+
+ // look for a duplicate binding
+ for (it = _buttons[context].begin(); it != end; ++it)
+ if ((*it)->binding.key == b.key &&
+ (*it)->binding.modifiers == b.modifiers) {
+ if ((*it)->callback[action] == callback)
+ return true; // already bound
+ break;
+ }
+
+ ButtonBinding *bind;
+
+ // the binding didnt exist yet, add it
+ if (it == end) {
+ bind = new ButtonBinding();
+ bind->binding.key = b.key;
+ bind->binding.modifiers = b.modifiers;
+ _buttons[context].push_back(bind);
+ // XXX GRAB the new button everywhere!
+ } else
+ bind = *it;
+ Py_XDECREF(bind->callback[action]); // if it was already bound, unbind it
+ bind->callback[action] = callback;
+ Py_INCREF(callback);
+ return true;
+}
+
+void OBBindings::removeAllButtons()
+{
+ // XXX: UNGRAB shits
+ for (int i = i; i < NUM_MOUSE_CONTEXT; ++i) {
+ ButtonBindingList::iterator it, end = _buttons[i].end();
+ for (it = _buttons[i].begin(); it != end; ++it)
+ for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
+ Py_XDECREF((*it)->callback[a]);
+ (*it)->callback[a] = 0;
+ }
+ }
+}
+
+void OBBindings::grabButtons(bool grab, OBClient *client)
+{
+ for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
+ Window win;
+ int mode = GrabModeAsync;
+ switch (i) {
+ case MC_Frame:
+ win = client->frame->window();
+ break;
+ case MC_Window:
+ win = client->frame->plate();
+ mode = GrabModeSync; // this is handled in the plate's buttonPressHandler
+ break;
+ default:
+ // any other elements already get button events, don't grab on them
+ continue;
+ }
+ ButtonBindingList::iterator it, end = _buttons[i].end();
+ for (it = _buttons[i].begin(); it != end; ++it)
+ if (grab)
+ otk::OBDisplay::grabButton((*it)->binding.key,
+ (*it)->binding.modifiers, win, false,
+ ButtonPressMask | ButtonMotionMask |
+ ButtonReleaseMask, mode, GrabModeAsync,
+ None, None, false);
+ else
+ otk::OBDisplay::ungrabButton((*it)->binding.key,
+ (*it)->binding.modifiers, win);
+ }
+}
+
+void OBBindings::fireButton(ButtonData *data)
+{
+ printf("but.mods %d.%d\n", data->button, data->state);
+
+ ButtonBindingList::iterator it, end = _buttons[data->context].end();
+ for (it = _buttons[data->context].begin(); it != end; ++it)
+ if ((*it)->binding.key == data->button &&
+ (*it)->binding.modifiers == data->state) {
+ if ((*it)->callback[data->action])
+ python_callback((*it)->callback[data->action], (PyObject*)data);
+ }
+}
+