summaryrefslogtreecommitdiff
path: root/src/gui/window_main.cpp
blob: 3d79989ab0591c29925fe562c5fde86cf20a1f25 (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
////////////////////////////////////////////////////////////////////////////////
// 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"

namespace gui {

void Window::InitializeWindow()
{
    // Initialize the Gtk Window
    window = gtk_application_window_new(application);
    gtk_window_set_title(GTK_WINDOW(window), title);
    gtk_window_set_default_size(GTK_WINDOW(window), GetWidth(), GetHeight());

    InitializeDrawingArea();
    InitializeEvents();

    gtk_widget_show_all(window);
    on_activate();
}

void Window::OnActivate(std::function<void()> callback)
{
	on_activate = callback;
}

void Window::RemoveAllCallbacks()
{
	keyup_callbacks.clear();
	keydown_callbacks.clear();
	mouseup_callbacks.clear();
	mousedown_callbacks.clear();
	mousemotion_callbacks.erase(mousemotion_callbacks.begin(),
		mousemotion_callbacks.end());
	scroll_callbacks.erase(scroll_callbacks.begin(), scroll_callbacks.end());
	render_callbacks.erase(render_callbacks.begin(), render_callbacks.end());
}

void Activate(GtkApplication*, gpointer data)
{
    // This will be called when the application activates.
    // It simply calls Window::InitializeWindow.
    Window* win = (Window*)data;
    win->InitializeWindow();
}

void Window::InitializeApplication(const char* t)
{
    title = t;
    application = gtk_application_new("com.pommicket.graphcoloring",
                                      G_APPLICATION_FLAGS_NONE);
    g_signal_connect(application, "activate", G_CALLBACK(&Activate), this);
}

Window::Window(const char* title, int w, int h, int fps)
    : size(w,h), FPS(fps)
{
    InitializeApplication(title);
}

Window::~Window() {}

int Window::GetWidth()  const { return size.X();  }
int Window::GetHeight() const { return size.Y(); }

const Size* Window::GetSizePtr() const { return &size; }

Position Window::GetPosition(int x, int y, double xrel, double yrel)
{
	return Position(x, y, xrel, yrel, GetSizePtr());
}

void Window::Quit()
{
	g_application_quit(G_APPLICATION(application));
}

void Window::Mainloop()
{
    g_application_run(G_APPLICATION(application), 0, NULL);
    g_object_unref(application);
}

} // namespace gui