2011-11-28 18:23:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This program 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 2 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2014-01-23 12:57:27 +01:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2011-11-28 18:23:05 +00:00
|
|
|
*
|
|
|
|
* Authors: Bastien Nocera <hadess@hadess.net>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-01-16 17:47:35 +00:00
|
|
|
#include <glib/gi18n.h>
|
2011-11-28 18:23:05 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2012-01-16 17:47:35 +00:00
|
|
|
#include "cc-wacom-nav-button.h"
|
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
struct _CcWacomNavButton
|
2011-11-28 18:23:05 +00:00
|
|
|
{
|
2018-06-03 09:42:43 +04:00
|
|
|
GtkBox parent_instance;
|
|
|
|
|
2011-11-28 18:23:05 +00:00
|
|
|
GtkNotebook *notebook;
|
2012-01-16 17:47:35 +00:00
|
|
|
GtkWidget *label;
|
2011-11-28 18:23:05 +00:00
|
|
|
GtkWidget *prev;
|
|
|
|
GtkWidget *next;
|
|
|
|
guint page_added_id;
|
|
|
|
guint page_removed_id;
|
|
|
|
guint page_switched_id;
|
|
|
|
gboolean ignore_first_page;
|
|
|
|
};
|
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
G_DEFINE_TYPE (CcWacomNavButton, cc_wacom_nav_button, GTK_TYPE_BOX)
|
|
|
|
|
2011-11-28 18:23:05 +00:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_NOTEBOOK,
|
|
|
|
PROP_IGNORE_FIRST
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_wacom_nav_button_update (CcWacomNavButton *nav)
|
|
|
|
{
|
|
|
|
int num_pages;
|
2012-01-30 16:08:44 +00:00
|
|
|
int current_page;
|
|
|
|
char *text;
|
2011-11-28 18:23:05 +00:00
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
if (nav->notebook == NULL) {
|
2011-11-28 18:23:05 +00:00
|
|
|
gtk_widget_hide (GTK_WIDGET (nav));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (nav->notebook));
|
2011-11-28 18:23:05 +00:00
|
|
|
if (num_pages == 0)
|
|
|
|
return;
|
2018-06-03 09:42:43 +04:00
|
|
|
if (nav->ignore_first_page && num_pages == 1)
|
2011-11-28 18:23:05 +00:00
|
|
|
return;
|
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
if (nav->ignore_first_page)
|
2011-11-28 18:23:05 +00:00
|
|
|
num_pages--;
|
|
|
|
|
|
|
|
g_assert (num_pages >= 1);
|
|
|
|
|
2017-06-07 18:16:48 +02:00
|
|
|
gtk_revealer_set_reveal_child (GTK_REVEALER (gtk_widget_get_parent (GTK_WIDGET (nav))),
|
2016-06-24 19:21:37 +02:00
|
|
|
num_pages > 1);
|
2012-01-30 16:08:44 +00:00
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK (nav->notebook));
|
2012-01-30 16:08:44 +00:00
|
|
|
if (current_page < 0)
|
|
|
|
return;
|
2018-06-03 09:42:43 +04:00
|
|
|
if (nav->ignore_first_page)
|
2012-01-30 16:08:44 +00:00
|
|
|
current_page--;
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_widget_set_sensitive (nav->prev, current_page == 0 ? FALSE : TRUE);
|
|
|
|
gtk_widget_set_sensitive (nav->next, current_page + 1 == num_pages ? FALSE : TRUE);
|
2012-01-30 16:08:44 +00:00
|
|
|
|
|
|
|
text = g_strdup_printf (_("%d of %d"),
|
|
|
|
current_page + 1,
|
|
|
|
num_pages);
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_label_set_text (GTK_LABEL (nav->label), text);
|
2011-11-28 18:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pages_changed (GtkNotebook *notebook,
|
|
|
|
GtkWidget *child,
|
|
|
|
guint page_num,
|
|
|
|
CcWacomNavButton *nav)
|
|
|
|
{
|
|
|
|
cc_wacom_nav_button_update (nav);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
page_switched (GtkNotebook *notebook,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
CcWacomNavButton *nav)
|
|
|
|
{
|
|
|
|
cc_wacom_nav_button_update (nav);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
next_clicked (GtkButton *button,
|
|
|
|
CcWacomNavButton *nav)
|
|
|
|
{
|
|
|
|
int current_page;
|
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK (nav->notebook));
|
2011-11-28 18:23:05 +00:00
|
|
|
current_page++;
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_notebook_set_current_page (GTK_NOTEBOOK (nav->notebook), current_page);
|
2011-11-28 18:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prev_clicked (GtkButton *button,
|
|
|
|
CcWacomNavButton *nav)
|
|
|
|
{
|
|
|
|
int current_page;
|
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK (nav->notebook));
|
2011-11-28 18:23:05 +00:00
|
|
|
current_page--;
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_notebook_set_current_page (GTK_NOTEBOOK (nav->notebook), current_page--);
|
2011-11-28 18:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_wacom_nav_button_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
CcWacomNavButton *nav = CC_WACOM_NAV_BUTTON (object);
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_NOTEBOOK:
|
2018-06-03 09:42:43 +04:00
|
|
|
if (nav->notebook) {
|
|
|
|
g_signal_handler_disconnect (nav->notebook, nav->page_added_id);
|
|
|
|
g_signal_handler_disconnect (nav->notebook, nav->page_removed_id);
|
|
|
|
g_signal_handler_disconnect (nav->notebook, nav->page_switched_id);
|
2011-11-28 18:23:05 +00:00
|
|
|
}
|
2018-06-03 10:22:08 +04:00
|
|
|
g_clear_object (&nav->notebook);
|
2018-06-03 09:42:43 +04:00
|
|
|
nav->notebook = g_value_dup_object (value);
|
|
|
|
nav->page_added_id = g_signal_connect (G_OBJECT (nav->notebook), "page-added",
|
|
|
|
G_CALLBACK (pages_changed), nav);
|
|
|
|
nav->page_removed_id = g_signal_connect (G_OBJECT (nav->notebook), "page-removed",
|
|
|
|
G_CALLBACK (pages_changed), nav);
|
|
|
|
nav->page_switched_id = g_signal_connect (G_OBJECT (nav->notebook), "notify::page",
|
|
|
|
G_CALLBACK (page_switched), nav);
|
2011-11-28 18:23:05 +00:00
|
|
|
cc_wacom_nav_button_update (nav);
|
|
|
|
break;
|
|
|
|
case PROP_IGNORE_FIRST:
|
2018-06-03 09:42:43 +04:00
|
|
|
nav->ignore_first_page = g_value_get_boolean (value);
|
2011-11-28 18:23:05 +00:00
|
|
|
cc_wacom_nav_button_update (nav);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_wacom_nav_button_dispose (GObject *object)
|
|
|
|
{
|
2018-06-03 09:42:43 +04:00
|
|
|
CcWacomNavButton *self = CC_WACOM_NAV_BUTTON (object);
|
|
|
|
|
|
|
|
if (self->notebook) {
|
|
|
|
g_signal_handler_disconnect (self->notebook, self->page_added_id);
|
|
|
|
self->page_added_id = 0;
|
|
|
|
g_signal_handler_disconnect (self->notebook, self->page_removed_id);
|
|
|
|
self->page_removed_id = 0;
|
|
|
|
g_signal_handler_disconnect (self->notebook, self->page_switched_id);
|
|
|
|
self->page_switched_id = 0;
|
2018-06-03 10:22:08 +04:00
|
|
|
g_clear_object (&self->notebook);
|
2011-11-28 18:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (cc_wacom_nav_button_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_wacom_nav_button_class_init (CcWacomNavButtonClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->set_property = cc_wacom_nav_button_set_property;
|
|
|
|
object_class->dispose = cc_wacom_nav_button_dispose;
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_NOTEBOOK,
|
|
|
|
g_param_spec_object ("notebook", "notebook", "notebook",
|
|
|
|
GTK_TYPE_NOTEBOOK,
|
|
|
|
G_PARAM_WRITABLE));
|
|
|
|
g_object_class_install_property (object_class, PROP_IGNORE_FIRST,
|
|
|
|
g_param_spec_boolean ("ignore-first", "ignore-first", "ignore-first",
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_WRITABLE));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_wacom_nav_button_init (CcWacomNavButton *self)
|
|
|
|
{
|
2011-11-28 14:52:24 -05:00
|
|
|
GtkStyleContext *context;
|
2012-01-23 11:27:12 -05:00
|
|
|
GtkWidget *image, *box;
|
2011-11-28 18:23:05 +00:00
|
|
|
|
2012-01-16 17:47:35 +00:00
|
|
|
/* Label */
|
2018-06-03 09:42:43 +04:00
|
|
|
self->label = gtk_label_new (NULL);
|
|
|
|
gtk_style_context_add_class (gtk_widget_get_style_context (self->label), "dim-label");
|
|
|
|
gtk_box_pack_start (GTK_BOX (self), self->label,
|
2012-01-23 11:27:12 -05:00
|
|
|
FALSE, FALSE, 8);
|
|
|
|
|
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
context = gtk_widget_get_style_context (GTK_WIDGET (box));
|
|
|
|
gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
|
|
|
|
gtk_box_pack_start (GTK_BOX (self), box,
|
2012-02-23 12:45:01 +01:00
|
|
|
FALSE, FALSE, 0);
|
2012-01-16 17:47:35 +00:00
|
|
|
|
|
|
|
/* Prev button */
|
2018-06-03 09:42:43 +04:00
|
|
|
self->prev = gtk_button_new ();
|
2014-05-23 12:31:53 +03:00
|
|
|
image = gtk_image_new_from_icon_name ("go-previous-symbolic", GTK_ICON_SIZE_MENU);
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_container_add (GTK_CONTAINER (self->prev), image);
|
|
|
|
g_signal_connect (G_OBJECT (self->prev), "clicked",
|
2011-11-28 18:23:05 +00:00
|
|
|
G_CALLBACK (prev_clicked), self);
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_widget_set_valign (self->prev, GTK_ALIGN_CENTER);
|
2011-11-28 18:23:05 +00:00
|
|
|
|
2012-01-16 17:47:35 +00:00
|
|
|
/* Next button */
|
2018-06-03 09:42:43 +04:00
|
|
|
self->next = gtk_button_new ();
|
2014-05-23 12:31:53 +03:00
|
|
|
image = gtk_image_new_from_icon_name ("go-next-symbolic", GTK_ICON_SIZE_MENU);
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_container_add (GTK_CONTAINER (self->next), image);
|
|
|
|
g_signal_connect (G_OBJECT (self->next), "clicked",
|
2011-11-28 18:23:05 +00:00
|
|
|
G_CALLBACK (next_clicked), self);
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_widget_set_valign (self->next, GTK_ALIGN_CENTER);
|
2011-11-28 18:23:05 +00:00
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_box_pack_start (GTK_BOX (box), self->prev,
|
2011-11-28 18:23:05 +00:00
|
|
|
FALSE, FALSE, 0);
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_box_pack_start (GTK_BOX (box), self->next,
|
2011-11-28 18:23:05 +00:00
|
|
|
FALSE, FALSE, 0);
|
|
|
|
|
2018-06-03 09:42:43 +04:00
|
|
|
gtk_widget_show (self->label);
|
2012-01-23 11:27:12 -05:00
|
|
|
gtk_widget_show_all (box);
|
2011-11-28 18:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
cc_wacom_nav_button_new (void)
|
|
|
|
{
|
|
|
|
return GTK_WIDGET (g_object_new (CC_TYPE_WACOM_NAV_BUTTON, NULL));
|
|
|
|
}
|