/*
* Main window
*
* Copyright (C) 2017 Patrick McDermott
*
* This file is part of Marquee.
*
* Marquee 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.
*
* Marquee 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 Marquee. If not, see .
*/
#include "window.h"
#include
#include
#include
#include "application.h"
#include "config/config.h"
#include "i18n.h"
#include "notebook.h"
#include "tab-page.h"
struct _MqWindow {
GtkWindow parent_instance;
MqApplication *application;
const gchar **uris;
MqConfig *config;
GtkWidget *notebook;
MqTabPage *root_tab;
gboolean fullscreen;
};
enum {
PROP_APPLICATION = 1,
PROP_URIS,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = {NULL,};
struct _MqWindowClass {
GtkWindowClass parent_class;
};
G_DEFINE_TYPE(MqWindow, mq_window, GTK_TYPE_WINDOW)
static void
is_maximized_cb(MqWindow *window, GParamSpec G_GNUC_UNUSED *param_spec)
{
mq_config_set_boolean(window->config, "window.maximized",
gtk_window_is_maximized(GTK_WINDOW(window)));
mq_config_save(window->config);
}
static void
configure_event_cb(MqWindow G_GNUC_UNUSED *window, GdkEventConfigure *event)
{
mq_config_set_integer(window->config, "window.width", event->width);
mq_config_set_integer(window->config, "window.height", event->height);
mq_config_save(window->config);
}
static gboolean
window_state_event_cb(MqWindow *window, GdkEventWindowState *event)
{
window->fullscreen =
event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
return FALSE;
}
static void
close_confirm_warn_check_button_toggled_cb(GtkToggleButton *toggle_button,
MqWindow *window)
{
mq_config_set_boolean(window->config, "tabs.warn-on-close",
gtk_toggle_button_get_active(toggle_button));
mq_config_save(window->config);
}
static void
close_confirm_response_cb(GtkWidget *dialog, gint response_id, MqWindow *window)
{
gtk_widget_destroy(dialog);
if (response_id == GTK_RESPONSE_OK) {
gtk_widget_destroy(GTK_WIDGET(window));
}
}
static gboolean
delete_event_cb(MqWindow *window, GdkEvent G_GNUC_UNUSED *event)
{
guint num_tabs;
gchar *message;
GtkWidget *message_label;
GtkWidget *check_button;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *dialog;
num_tabs = mq_window_get_num_tabs(window);
if (num_tabs == 1) {
return FALSE;
}
if (!mq_config_get_boolean(window->config, "tabs.warn-on-close")) {
return FALSE;
}
/* Message */
/* The number of tabs is always greater than 1, but some non-English
* languages pluralize nouns differently depending on the number. */
message = g_strdup_printf(PL_(
"You are about to close %u tab. "
"Are you sure you want to continue?",
"You are about to close %u tabs. "
"Are you sure you want to continue?",
num_tabs), num_tabs);
message_label = gtk_label_new(message);
g_free(message);
/* Check button */
check_button = gtk_check_button_new_with_mnemonic(
_("_Warn when closing multiple tabs or windows"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button),
mq_config_get_boolean(window->config, "tabs.warn-on-close"));
/* Connect signal after setting initial toggle status, to avoid a
* spurious signal. */
g_signal_connect(check_button, "toggled",
G_CALLBACK(close_confirm_warn_check_button_toggled_cb),
window);
/* Right vertical box (message and check button) */
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(vbox), message_label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), check_button, FALSE, FALSE, 0);
/* Horizontal box (icon and right vertical box) */
hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start(GTK_BOX(hbox), gtk_image_new_from_icon_name(
"dialog-question", GTK_ICON_SIZE_DIALOG), FALSE, FALSE,
0);
gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
/* Dialog */
dialog = gtk_dialog_new_with_buttons(_("Confirm Close"),
GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("Cl_ose Tabs"), GTK_RESPONSE_OK,
NULL);
gtk_container_add(
GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
hbox);
g_signal_connect(dialog, "response",
G_CALLBACK(close_confirm_response_cb), window);
gtk_widget_grab_focus(gtk_dialog_get_widget_for_response(
GTK_DIALOG(dialog), GTK_RESPONSE_OK));
gtk_widget_show_all(dialog);
return TRUE;
}
static void
destroy_cb(MqWindow *window)
{
mq_application_delete_window(window->application, window);
}
static void
set_title(MqWindow *window, const gchar *title)
{
gchar *window_title;
window_title = g_strdup_printf("%s - Marquee", title);
gtk_window_set_title(GTK_WINDOW(window), window_title);
g_free(window_title);
}
static void
constructed(GObject *object)
{
MqWindow *window;
gsize i;
window = MQ_WINDOW(object);
if (mq_config_get_boolean(window->config, "window.maximized")) {
gtk_window_maximize(GTK_WINDOW(window));
} else {
gtk_window_unmaximize(GTK_WINDOW(window));
}
gtk_window_set_default_size(GTK_WINDOW(window),
mq_config_get_integer(window->config, "window.width"),
mq_config_get_integer(window->config, "window.height"));
if (window->uris && window->uris[0]) {
for (i = 0; window->uris && window->uris[i]; ++i) {
mq_notebook_insert_top(MQ_NOTEBOOK(window->notebook),
window->uris[i], FALSE);
}
} else {
mq_notebook_insert_top(MQ_NOTEBOOK(window->notebook), NULL,
FALSE);
}
gtk_widget_show_all(GTK_WIDGET(window));
}
static void
get_property(GObject *object, guint property_id, GValue *value,
GParamSpec *param_spec)
{
MqWindow *window;
window = MQ_WINDOW(object);
switch (property_id) {
case PROP_APPLICATION:
g_value_set_pointer(value, window->application);
break;
case PROP_URIS:
g_value_set_pointer(value, window->uris);
break;
;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id,
param_spec);
break;
}
}
static void
set_property(GObject *object, guint property_id, const GValue *value,
GParamSpec *param_spec)
{
MqWindow *window;
window = MQ_WINDOW(object);
switch (property_id) {
case PROP_APPLICATION:
window->application = g_value_get_pointer(value);
window->config = mq_application_get_config(
window->application);
break;
case PROP_URIS:
window->uris = g_value_get_pointer(value);
break;
;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id,
param_spec);
break;
}
}
static void
mq_window_class_init(MqWindowClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->constructed = constructed;
object_class->get_property = get_property;
object_class->set_property = set_property;
obj_properties[PROP_APPLICATION] = g_param_spec_pointer(
"application",
P_("Application"),
P_("The parent MqApplication instance"),
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
obj_properties[PROP_URIS] = g_param_spec_pointer(
"uris",
P_("URIs"),
P_("A NULL-terminated string array of URIs to load"),
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
g_object_class_install_properties(object_class, N_PROPERTIES,
obj_properties);
}
static void
mq_window_init(MqWindow *window)
{
window->fullscreen = FALSE;
g_signal_connect(window, "notify::is-maximized",
G_CALLBACK(is_maximized_cb), NULL);
g_signal_connect(window, "configure-event",
G_CALLBACK(configure_event_cb), NULL);
g_signal_connect(window, "window-state-event",
G_CALLBACK(window_state_event_cb), NULL);
g_signal_connect(window, "delete-event",
G_CALLBACK(delete_event_cb), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK(destroy_cb), NULL);
window->notebook = mq_notebook_new(window);
gtk_container_add(GTK_CONTAINER(window), window->notebook);
}
MqWindow *
mq_window_new(MqApplication *application, const gchar **uris)
{
return g_object_new(MQ_TYPE_WINDOW,
"type", GTK_WINDOW_TOPLEVEL,
"application", application,
"uris", uris,
NULL);
}
void
mq_window_quit(MqWindow *window)
{
mq_application_quit(window->application, GTK_WINDOW(window));
}
MqApplication *
mq_window_get_application(MqWindow *window)
{
return window->application;
}
void
mq_window_toggle_fullscreen(MqWindow *window)
{
if (!window->fullscreen) {
gtk_window_fullscreen(GTK_WINDOW(window));
} else {
gtk_window_unfullscreen(GTK_WINDOW(window));
}
}
guint
mq_window_get_num_tabs(MqWindow *window)
{
return mq_notebook_get_n_pages(MQ_NOTEBOOK(window->notebook));
}
void
mq_window_set_title(MqWindow *window, const gchar *title)
{
set_title(window, title);
}
void
mq_window_scroll_tab_labels(MqWindow *window)
{
mq_tab_page_scroll_tab_labels(window->root_tab);
}
void
mq_window_begin_scrolling_tab_labels(MqWindow *window)
{
mq_tab_page_begin_scrolling_tab_labels(window->root_tab);
}
void
mq_window_end_scrolling_tab_labels(MqWindow *window)
{
mq_tab_page_end_scrolling_tab_labels(window->root_tab);
}