summaryrefslogtreecommitdiff
path: root/src/gui/window_events.cpp
blob: 635d86eeef56e254d72c69ac5c7a47ac47fac4b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
////////////////////////////////////////////////////////////////////////////////
// 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 <iostream>
#include "utils/errors.hpp"
#include "window.hpp"
#include "window_events_keyboard.hpp"
#include "window_events_mouse.hpp"

namespace gui
{

template<typename F>
int Window::AddToCallbackMap(std::map<guint,std::vector<F>>& callback_map,
    	guint id, F callback)
{
	if (callback_map.count(id))
	{
		// There are already callbacks for this key.
		callback_map[id].push_back(callback);
	}
	else
	{
		std::vector<F> callbacks = {callback};
		callback_map[id] = callbacks;
	}
	return callback_map[id].size()-1;
}

template<typename F>
std::vector<F> Window::CheckCallbackMap(
	std::map<guint,std::vector<F>>& callback_map, guint id)
{
	// mouse_x and mouse_y will be passed to the function if it is a
	// mouse_callback_t. Otherwise, they will be set to -1, and will
	// not be passed.
	if (callback_map.count(id))
	{
		// There are callbacks for this key
		return callback_map[id];
	}
	else
	{
		return std::vector<F>();
	}
}

template<typename F>
void Window::RemoveFromCallbackList(
	std::vector<F>& callback_list, int id)
{
	if (id < 0 || id >= (int)callback_list.size())
		utils::errors::Die("Invalid callback ID.");
	callback_list[id] = nullptr;
}

template int Window::AddToCallbackMap<Window::mouse_callback_t>(
		mouse_callback_map_t&, guint, mouse_callback_t);
template int Window::AddToCallbackMap<Window::callback_t>(
		callback_map_t&, guint, callback_t);

template std::vector<Window::mouse_callback_t>
	Window::CheckCallbackMap<Window::mouse_callback_t>(
	mouse_callback_map_t&, guint);
template std::vector<Window::callback_t>
	Window::CheckCallbackMap<Window::callback_t>(callback_map_t&, guint);


template void Window::RemoveFromCallbackList<Window::mouse_callback_t>(
	std::vector<mouse_callback_t>&, int);
template void Window::RemoveFromCallbackList<Window::callback_t>(
	std::vector<callback_t>&, int);
template void Window::RemoveFromCallbackList<Window::scroll_callback_t>(
	std::vector<scroll_callback_t>&, int);

void Window::InitializeEvents()
{
	gtk_widget_set_events(window, GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK
			| GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK
			| GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK
			| GDK_BUTTON_RELEASE_MASK);

    g_signal_connect(G_OBJECT(window), "key-press-event",
        G_CALLBACK(GtkKeydownCallback), this);
    g_signal_connect(G_OBJECT(window), "key-release-event",
        G_CALLBACK(GtkKeyupCallback), this);
    g_signal_connect(G_OBJECT(window), "button-press-event",
    	G_CALLBACK(GtkMousedownCallback), this);
    g_signal_connect(G_OBJECT(window), "button-release-event",
    	G_CALLBACK(GtkMouseupCallback), this);
    g_signal_connect(G_OBJECT(window), "motion-notify-event",
    	G_CALLBACK(GtkMousemotionCallback), this);
    g_signal_connect(G_OBJECT(window), "scroll-event",
    	G_CALLBACK(GtkScrollCallback), this);
}

} // namespace gui