gem-graph-client/junks/0_application.c

151 lines
4.9 KiB
C

/*
* Gem-graph OpenGL experiments
*
* Desc: User interface functions
*
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
*
* This file is part of Gem-graph.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../../include/base.h"
#include "../../include/ui.h"
struct _GemGraphClientApplication
{
GtkApplication parent_instance;
};
G_DEFINE_TYPE (GemGraphClientApplication,
gem_graph_client_application,
GTK_TYPE_APPLICATION)
static GemGraphClientApplication *application;
/* -------------------------------------------------------------------------- */
void ui_enable_action(const char *name) {
g_simple_action_set_enabled(
(GSimpleAction *)g_action_map_lookup_action(
G_ACTION_MAP(application),
name),
true);
}
void ui_disable_action(const char *name) {
g_simple_action_set_enabled(
(GSimpleAction *)g_action_map_lookup_action(
G_ACTION_MAP(application),
name),
false);
}
/*
* Window actual presentation on screen
*
*/
static void gem_graph_client_application_activate(GApplication *app)
{
GtkWindow *window;
g_assert(GEM_GRAPH_CLIENT_IS_APPLICATION(app));
window = gtk_application_get_active_window(GTK_APPLICATION (app));
if (window == NULL)
window = g_object_new(GEM_GRAPH_CLIENT_TYPE_WINDOW,
"application", app,
NULL);
ui_set_stack(HOME_MODE);
gtk_window_present(window);
}
/*
* Action records are registered here
*
*/
static void gem_graph_client_application_init(GemGraphClientApplication *self)
{
g_action_map_add_action_entries(G_ACTION_MAP(self),
app_actions,
G_N_ELEMENTS(app_actions),
self);
// Setup shortcuts
gtk_application_set_accels_for_action(GTK_APPLICATION(self),
"app.quit",
(const char *[]) { "<primary>q", NULL });
gtk_application_set_accels_for_action(GTK_APPLICATION(self),
"app.editmode",
(const char *[]) { "<primary>e", NULL });
gtk_application_set_accels_for_action(GTK_APPLICATION(self),
"app.runmode",
(const char *[]) { "<primary>r", NULL });
gtk_application_set_accels_for_action(GTK_APPLICATION(self),
"app.presentmode",
(const char *[]) { "<primary>p", NULL });
gtk_application_set_accels_for_action(GTK_APPLICATION(self),
"app.savefile",
(const char *[]) { "<primary>s", NULL });
application = self;
//
// Disable unneeded/inoperant actions
//
ui_disable_action("savefile");
ui_disable_action("closefile");
ui_disable_action("editmode");
ui_disable_action("runmode");
ui_disable_action("presentmode");
ui_disable_action("togglesidebar");
}
void ui_send_notification(const char *message)
{
g_print("NOTIFICATION: %s\n", message);
g_application_send_notification(G_APPLICATION(application),
"notification",
g_notification_new(message)
);
}
/* -------------------------------------------------------------------------- */
static void gem_graph_client_application_class_init(
GemGraphClientApplicationClass *klass)
{
GApplicationClass *app_class = G_APPLICATION_CLASS(klass);
app_class->activate = gem_graph_client_application_activate;
}
GemGraphClientApplication *gem_graph_client_application_new(
const char *application_id,
GApplicationFlags flags)
{
g_return_val_if_fail(application_id != NULL, NULL);
return g_object_new(GEM_GRAPH_CLIENT_TYPE_APPLICATION,
"application-id", application_id,
"flags", flags,
NULL);
}