2012-07-15 20:30:51 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Mike Massonnet <mmassonnet@xfce.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
2017-01-07 06:04:11 +08:00
|
|
|
#include <libxfce4util/libxfce4util.h>
|
2012-07-15 20:30:51 +08:00
|
|
|
|
|
|
|
#include "monitor-label.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _XnlpMonitorLabelClass XnlpMonitorLabelClass;
|
|
|
|
struct _XnlpMonitorLabelClass
|
|
|
|
{
|
|
|
|
GtkLabelClass parent_class;
|
|
|
|
};
|
|
|
|
struct _XnlpMonitorLabel
|
|
|
|
{
|
|
|
|
GtkLabel parent;
|
|
|
|
/*<private>*/
|
2017-01-07 06:04:11 +08:00
|
|
|
GtkCssProvider *css_provider;
|
2012-07-15 20:30:51 +08:00
|
|
|
gint count_width;
|
|
|
|
gint count_height;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (XnlpMonitorLabel, xnlp_monitor_label, GTK_TYPE_LABEL)
|
|
|
|
|
|
|
|
static void xnlp_monitor_label_constructed (GObject *object);
|
|
|
|
|
|
|
|
static void cb_label_changed (GObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer user_data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
xnlp_monitor_label_class_init (XnlpMonitorLabelClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *class = G_OBJECT_CLASS (klass);
|
|
|
|
xnlp_monitor_label_parent_class = g_type_class_peek_parent (klass);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
xnlp_monitor_label_init (XnlpMonitorLabel *label)
|
|
|
|
{
|
|
|
|
label->count_width = 0;
|
|
|
|
label->count_height = 0;
|
|
|
|
label->width = 0;
|
|
|
|
label->height = 0;
|
2017-01-07 06:04:11 +08:00
|
|
|
label->css_provider = gtk_css_provider_new ();
|
|
|
|
gtk_style_context_add_provider (
|
|
|
|
GTK_STYLE_CONTEXT (gtk_widget_get_style_context (GTK_WIDGET (label))),
|
|
|
|
GTK_STYLE_PROVIDER (label->css_provider),
|
|
|
|
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
2012-07-15 20:30:51 +08:00
|
|
|
|
|
|
|
g_signal_connect (label, "notify::label", G_CALLBACK (cb_label_changed), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_label_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
|
|
|
|
{
|
|
|
|
XnlpMonitorLabel *label = XNLP_MONITOR_LABEL (object);
|
|
|
|
GtkWidget *widget = GTK_WIDGET (object);
|
2016-05-15 04:52:01 +08:00
|
|
|
GtkRequisition minimum_size;
|
|
|
|
GtkRequisition natural_size;
|
2012-07-15 20:30:51 +08:00
|
|
|
|
|
|
|
gtk_widget_set_size_request (widget, -1, -1);
|
2016-05-15 04:52:01 +08:00
|
|
|
gtk_widget_get_preferred_size (widget, &minimum_size, &natural_size);
|
2012-07-15 20:30:51 +08:00
|
|
|
|
2016-05-15 04:52:01 +08:00
|
|
|
if (minimum_size.width >= label->width)
|
2012-07-15 20:30:51 +08:00
|
|
|
{
|
2016-05-15 04:52:01 +08:00
|
|
|
label->width = minimum_size.width;
|
2012-07-15 20:30:51 +08:00
|
|
|
label->count_width = 0;
|
|
|
|
}
|
|
|
|
else if (label->count_width > 10)
|
|
|
|
{
|
2016-05-15 04:52:01 +08:00
|
|
|
label->width = minimum_size.width;
|
2012-07-15 20:30:51 +08:00
|
|
|
label->count_width = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 04:52:01 +08:00
|
|
|
minimum_size.width = label->width;
|
2012-07-15 20:30:51 +08:00
|
|
|
label->count_width++;
|
|
|
|
}
|
|
|
|
|
2016-05-15 04:52:01 +08:00
|
|
|
if (minimum_size.height >= label->height)
|
2012-07-15 20:30:51 +08:00
|
|
|
{
|
2016-05-15 04:52:01 +08:00
|
|
|
label->height = minimum_size.height;
|
2012-07-15 20:30:51 +08:00
|
|
|
label->count_height = 0;
|
|
|
|
}
|
|
|
|
else if (label->count_height > 10)
|
|
|
|
{
|
2016-05-15 04:52:01 +08:00
|
|
|
label->height = minimum_size.height;
|
2012-07-15 20:30:51 +08:00
|
|
|
label->count_height = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 04:52:01 +08:00
|
|
|
minimum_size.height = label->height;
|
2012-07-15 20:30:51 +08:00
|
|
|
label->count_height++;
|
|
|
|
}
|
|
|
|
|
2016-05-15 04:52:01 +08:00
|
|
|
gtk_widget_set_size_request (widget, minimum_size.width, minimum_size.height);
|
2012-07-15 20:30:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
xnlp_monitor_label_new (const gchar *str)
|
|
|
|
{
|
|
|
|
GtkLabel *label;
|
|
|
|
|
|
|
|
label = g_object_new (XNLP_TYPE_MONITOR_LABEL, NULL);
|
|
|
|
|
|
|
|
if (str && *str)
|
|
|
|
gtk_label_set_text (label, str);
|
|
|
|
|
|
|
|
return GTK_WIDGET (label);
|
|
|
|
}
|
|
|
|
|
2017-01-07 06:04:11 +08:00
|
|
|
void
|
|
|
|
xnlp_monitor_label_set_color (XnlpMonitorLabel *label, GdkRGBA* color)
|
|
|
|
{
|
|
|
|
gchar * css;
|
|
|
|
if (color != NULL)
|
|
|
|
{
|
|
|
|
#if GTK_CHECK_VERSION (3, 20, 0)
|
|
|
|
css = g_strdup_printf("label { color: %s; }",
|
|
|
|
#else
|
|
|
|
css = g_strdup_printf(".label { color: %s; }",
|
|
|
|
#endif
|
|
|
|
gdk_rgba_to_string(color));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if GTK_CHECK_VERSION (3, 20, 0)
|
|
|
|
css = g_strdup_printf("label { color: inherit; }");
|
|
|
|
#else
|
|
|
|
css = g_strdup_printf(".label { color: inherit; }");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
gtk_css_provider_load_from_data (label->css_provider, css, strlen(css), NULL);
|
2017-02-10 03:15:49 +08:00
|
|
|
DBG("setting label css: %s", gtk_css_provider_to_string (label->css_provider));
|
2017-01-07 06:04:11 +08:00
|
|
|
g_free(css);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-15 20:30:51 +08:00
|
|
|
void
|
|
|
|
xnlp_monitor_label_reinit_size_request (XnlpMonitorLabel *label)
|
|
|
|
{
|
|
|
|
label->count_width = 0;
|
|
|
|
label->count_height = 0;
|
|
|
|
label->width = 0;
|
|
|
|
label->height = 0;
|
|
|
|
|
2012-07-15 22:43:49 +08:00
|
|
|
gtk_widget_set_size_request (GTK_WIDGET (label), -1, -1);
|
2012-07-15 20:30:51 +08:00
|
|
|
}
|
|
|
|
|