Skip to content
Snippets Groups Projects
main.c 3.67 KiB
Newer Older
Bruce Cowan's avatar
Bruce Cowan committed
/* main.c
Bruce Cowan's avatar
Bruce Cowan committed
 * Copyright 2012, 2016-2018 Bruce Cowan <bruce@bcowan.eu>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This file 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Bruce Cowan's avatar
Bruce Cowan committed
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
Bruce Cowan's avatar
Bruce Cowan committed
#include <gtk/gtk.h>
Bruce Cowan's avatar
Bruce Cowan committed

Bruce Cowan's avatar
Bruce Cowan committed
#include "rugby-app-window.h"
Bruce Cowan's avatar
Bruce Cowan committed

static void
Bruce Cowan's avatar
Bruce Cowan committed
on_activate (GApplication *app,
             gpointer      user_data)
Bruce Cowan's avatar
Bruce Cowan committed
{
Bruce Cowan's avatar
Bruce Cowan committed
    RugbyAppWindow *win = rugby_app_window_new (GTK_APPLICATION (app));
Bruce Cowan's avatar
Bruce Cowan committed
    gtk_window_present (GTK_WINDOW (win));
Bruce Cowan's avatar
Bruce Cowan committed
}

static void
Bruce Cowan's avatar
Bruce Cowan committed
about_activated (GSimpleAction *simple,
                 GVariant      *parameter,
                 gpointer       user_data)
Bruce Cowan's avatar
Bruce Cowan committed
{
Bruce Cowan's avatar
Bruce Cowan committed
    GtkApplication *app = GTK_APPLICATION (user_data);
    GtkWindow *window = gtk_application_get_active_window (app);

    const gchar *authors[] = { "Bruce Cowan", NULL };
Bruce Cowan's avatar
Bruce Cowan committed
    g_autoptr (GDateTime) date = g_date_time_new_now_local ();
    gint year = g_date_time_get_year (date);
    g_autofree gchar *copyright = g_strdup_printf ("Copyright © 2012–%d Bruce Cowan",
                                                   year);

    gtk_show_about_dialog (window,
                           "logo-icon-name", "face-wink",
                           "program-name", "Rugby",
                           "copyright", copyright,
                           "license-type", GTK_LICENSE_LGPL_3_0,
                           "authors", authors,
                           "comments", "Rugby scores possiblities program",
                           NULL);

}

static void
quit_activated (GSimpleAction *simple,
                GVariant      *parameter,
                gpointer       user_data)
{
    g_application_quit (G_APPLICATION (user_data));
}

static void
Bruce Cowan's avatar
Bruce Cowan committed
on_startup (GApplication *app,
            gpointer      user_data)
Bruce Cowan's avatar
Bruce Cowan committed
{
Bruce Cowan's avatar
Bruce Cowan committed
    const GActionEntry app_entries[] =
    {
        { "about", about_activated },
        { "quit", quit_activated }
    };

Bruce Cowan's avatar
Bruce Cowan committed
    g_action_map_add_action_entries (G_ACTION_MAP (app),
Bruce Cowan's avatar
Bruce Cowan committed
                                     app_entries,
                                     G_N_ELEMENTS (app_entries),
Bruce Cowan's avatar
Bruce Cowan committed
                                     app);

    g_autoptr (GtkBuilder) builder = gtk_builder_new_from_resource ("/uk/me/bcowan/rugby/menu.ui");
    GMenuModel *app_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "appmenu"));
    gtk_application_set_app_menu (GTK_APPLICATION (app), app_menu);

    // CSS styling
    g_autoptr (GtkCssProvider) provider = gtk_css_provider_new ();
    gtk_css_provider_load_from_resource (provider, "/uk/me/bcowan/rugby/rugby.css");

    gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                               GTK_STYLE_PROVIDER (provider),
                                               GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
Bruce Cowan's avatar
Bruce Cowan committed
}

Bruce Cowan's avatar
Bruce Cowan committed
int
main (int    argc,
      char **argv)
Bruce Cowan's avatar
Bruce Cowan committed
{
Bruce Cowan's avatar
Bruce Cowan committed
    GtkApplication *app = gtk_application_new ("uk.me.bcowan.rugby",
                                               G_APPLICATION_FLAGS_NONE);
Bruce Cowan's avatar
Bruce Cowan committed
    g_signal_connect (app, "startup",
Bruce Cowan's avatar
Bruce Cowan committed
                      G_CALLBACK (on_startup), NULL);
Bruce Cowan's avatar
Bruce Cowan committed
    g_signal_connect (app, "activate",
                      G_CALLBACK (on_activate), NULL);
Bruce Cowan's avatar
Bruce Cowan committed
    return g_application_run (G_APPLICATION (app), argc, argv);
Bruce Cowan's avatar
Bruce Cowan committed
}