diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2018-08-20 20:34:57 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2018-08-20 20:34:57 -0400 |
commit | a4460f6d9453bbd7e584937686449cef3e19f052 (patch) | |
tree | 037c208f1e20302ed048c0952ef8e3418add9c86 /src/gui/window_events_mouse.cpp |
Initial commit0.0.0
Diffstat (limited to 'src/gui/window_events_mouse.cpp')
-rw-r--r-- | src/gui/window_events_mouse.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/gui/window_events_mouse.cpp b/src/gui/window_events_mouse.cpp new file mode 100644 index 0000000..16238d5 --- /dev/null +++ b/src/gui/window_events_mouse.cpp @@ -0,0 +1,166 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// GraphColoring is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// + +#include "window.hpp" + +#include <iostream> + +#include "utils/errors.hpp" + +namespace gui { + + +void GtkMousedownCallback(GtkWidget*, GdkEventButton* event, gpointer data) +{ + Window* win = (Window*)data; + win->ProcessMousedown(event); +} + +void Window::ProcessMousedown(GdkEventButton* event) +{ + mousemap[event->button] = true; + is_mousedown_callbacks_modified = false; + for (mouse_callback_t callback : + CheckCallbackMap(mousedown_callbacks, event->button)) + { + if (!callback) continue; + callback(this, event->x, event->y); + if (is_mousedown_callbacks_modified) + break; + } +} + +int Window::SetMousedownCallback(mouse_callback_t callback, guint button) +{ + is_mousedown_callbacks_modified = true; + return AddToCallbackMap(mousedown_callbacks, button, callback); +} + +void Window::RemoveMousedownCallback(int id, guint button) +{ + is_mousedown_callbacks_modified = true; + if (mousedown_callbacks.count(button) == 0) + utils::errors::Die("Invalid button passed to remove mouse callback."); + RemoveFromCallbackList(mousedown_callbacks[button], id); +} + +void GtkMouseupCallback(GtkWidget*, GdkEventButton* event, gpointer data) +{ + Window* win = (Window*)data; + win->ProcessMouseup(event); +} + +void Window::ProcessMouseup(GdkEventButton* event) +{ + mousemap[event->button] = false; + is_mouseup_callbacks_modified = false; + for (mouse_callback_t callback : mouseup_callbacks[event->button]) + { + if (!callback) continue; + callback(this, event->x, event->y); + if (is_mouseup_callbacks_modified) + break; + } + +} + +int Window::SetMouseupCallback(mouse_callback_t callback, guint button) +{ + is_mouseup_callbacks_modified = true; + return AddToCallbackMap(mouseup_callbacks, button, callback); +} + +void Window::RemoveMouseupCallback(int id, guint button) +{ + is_mouseup_callbacks_modified = true; + if (mouseup_callbacks.count(button) == 0) + utils::errors::Die("Invalid button passed to remove mouse callback."); + RemoveFromCallbackList(mouseup_callbacks[button], id); +} + +void GtkMousemotionCallback(GtkWidget*, GdkEventMotion* event, gpointer data) +{ + Window* win = (Window*)data; + win->ProcessMousemotion(event); +} + +void Window::ProcessMousemotion(GdkEventMotion* event) +{ + mouse_x = event->x; + mouse_y = event->y; + is_mousemotion_callbacks_modified = false; + for (mouse_callback_t callback : mousemotion_callbacks) + { + if (!callback) continue; + callback(this, event->x, event->y); + if (is_mousemotion_callbacks_modified) + break; + } +} + +int Window::SetMousemotionCallback(mouse_callback_t callback) +{ + is_mousemotion_callbacks_modified = true; + mousemotion_callbacks.push_back(callback); + return mousemotion_callbacks.size()-1; +} + +void Window::RemoveMousemotionCallback(int id) +{ + is_mousemotion_callbacks_modified = true; + RemoveFromCallbackList(mousemotion_callbacks, id); +} + +void GtkScrollCallback(GtkWidget*, GdkEventScroll* event, gpointer data) +{ + Window* window = (Window*) data; + window->ProcessScroll(event); +} + +void Window::ProcessScroll(GdkEventScroll* event) +{ + is_scroll_callbacks_modified = false; + for (scroll_callback_t callback : scroll_callbacks) + { + if (!callback) continue; + callback(this, event->direction); + if (is_scroll_callbacks_modified) + break; + } +} + +int Window::SetScrollCallback(scroll_callback_t callback) +{ + is_scroll_callbacks_modified = true; + scroll_callbacks.push_back(callback); + return scroll_callbacks.size()-1; +} + +void Window::RemoveScrollCallback(int id) +{ + is_scroll_callbacks_modified = true; + RemoveFromCallbackList(scroll_callbacks, id); +} + +bool Window::IsMouseDown(guint button) const { return mousemap.at(button); } + +int Window::GetMouseX() const { return mouse_x; } +int Window::GetMouseY() const { return mouse_y; } + +} // namespace gui + |