summaryrefslogtreecommitdiff
path: root/src/gui/window_events_keyboard.cpp
blob: d3a584172903cf1f026439f5096675f7d9913087 (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
////////////////////////////////////////////////////////////////////////////////
// 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 "utils/errors.hpp"

namespace gui {

int Window::SetKeydownCallback(callback_t callback, guint key)
{
	is_keydown_callbacks_modified = true;
	return AddToCallbackMap(keydown_callbacks, key, callback);
}

void Window::RemoveKeydownCallback(int id, guint key)
{
	if (keydown_callbacks.count(key) == 0)
		utils::errors::Die("Invalid key passed to remove key callback.");
	RemoveFromCallbackList(keydown_callbacks[key], id);
	is_keydown_callbacks_modified = true;
}

void Window::ProcessKeydown(GdkEventKey* event)
{
	keymap[event->keyval] = true;
	is_keydown_callbacks_modified = false;
	for (callback_t callback
		: CheckCallbackMap<callback_t>(keydown_callbacks, event->keyval))
	{
		if (!callback) continue;
		callback(this);
		if (is_keydown_callbacks_modified)
			break;
	}
}

void GtkKeydownCallback(GtkWidget*, GdkEventKey* event, gpointer data)
{
    Window* win = (Window*)data;
    win->ProcessKeydown(event);
}


int Window::SetKeyupCallback(callback_t callback, guint key)
{
	is_keyup_callbacks_modified = true;
	return AddToCallbackMap(keyup_callbacks, key, callback);
}

void Window::RemoveKeyupCallback(int id, guint key)
{
	if (keyup_callbacks.count(key) == 0)
		utils::errors::Die("Invalid key passed to remove key callback.");
	RemoveFromCallbackList(keyup_callbacks[key], id);
	is_keyup_callbacks_modified = true;
}

void Window::ProcessKeyup(GdkEventKey* event)
{
	keymap[event->keyval] = false;
	is_keyup_callbacks_modified = false;
	for (callback_t callback : CheckCallbackMap(keyup_callbacks, event->keyval))
	{
		if (!callback) continue;
		callback(this);
		if (is_keyup_callbacks_modified)
			break;
	}
}

void GtkKeyupCallback(GtkWidget*, GdkEventKey* event, gpointer data)
{
    Window* win = (Window*)data;
    win->ProcessKeyup(event);
}

bool Window::IsKeyDown(guint key) const
{
	if (keymap.count(key))
		return keymap.at(key);
	else
		return false;
}

bool Window::IsControlDown() const
{
	return IsKeyDown(GDK_KEY_Control_L) || IsKeyDown(GDK_KEY_Control_R);
}

} // namespace gui