[gnome-applets: 260/263] netspeed: fix removed size_request signal



commit 359bbd4cbcd5d2285b6e8f35377bc1a3f3eb4b6c
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Wed Mar 25 15:41:53 2015 +0200

    netspeed: fix removed size_request signal

 netspeed/src/Makefile.am |    8 +++-
 netspeed/src/label.c     |   92 ++++++++++++++++++++++++++++++++++++++++++++++
 netspeed/src/label.h     |   35 +++++++++++++++++
 netspeed/src/netspeed.c  |   52 +++++++-------------------
 4 files changed, 148 insertions(+), 39 deletions(-)
---
diff --git a/netspeed/src/Makefile.am b/netspeed/src/Makefile.am
index 627bbe2..91b07e2 100644
--- a/netspeed/src/Makefile.am
+++ b/netspeed/src/Makefile.am
@@ -10,7 +10,13 @@ AM_CFLAGS = \
 
 libexec_PROGRAMS = netspeed_applet2
 
-netspeed_applet2_SOURCES = backend.h backend.c netspeed.c netspeed.h
+netspeed_applet2_SOURCES = \
+  backend.h \
+  backend.c \
+  label.h \
+  label.c \
+  netspeed.h \
+  netspeed.c
 
 netspeed_applet2_LDADD = \
         $(GNOME_APPLETS_LIBS) \
diff --git a/netspeed/src/label.c b/netspeed/src/label.c
new file mode 100644
index 0000000..f418888
--- /dev/null
+++ b/netspeed/src/label.c
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2015 Alberts Muktupāvels
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *     Alberts Muktupāvels <alberts muktupavels gmail com>
+ */
+
+#include "label.h"
+
+struct _NetspeedLabel
+{
+       GtkLabel parent;
+
+  gboolean dont_shrink;
+       gint     width;
+};
+
+G_DEFINE_TYPE (NetspeedLabel, netspeed_label, GTK_TYPE_LABEL)
+
+static void
+netspeed_label_size_allocate (GtkWidget     *widget,
+                              GtkAllocation *allocation)
+{
+  NetspeedLabel *label;
+
+  label = NETSPEED_LABEL (widget);
+
+  if (allocation->width > label->width)
+    label->width = allocation->width;
+
+  GTK_WIDGET_CLASS (netspeed_label_parent_class)->size_allocate (widget, allocation);
+}
+
+static void
+netspeed_label_get_preferred_width (GtkWidget *widget,
+                                    gint      *minimum_width,
+                                    gint      *natural_width)
+{
+  NetspeedLabel *label;
+
+  label = NETSPEED_LABEL (widget);
+
+  GTK_WIDGET_CLASS (netspeed_label_parent_class)->get_preferred_width (widget,
+                                                                       minimum_width,
+                                                                       natural_width);
+
+  if (label->dont_shrink && (*minimum_width < label->width || *natural_width < label->width))
+    *minimum_width = *natural_width = label->width;
+}
+
+static void
+netspeed_label_class_init (NetspeedLabelClass *label_class)
+{
+       GtkWidgetClass *widget_class;
+
+       widget_class = GTK_WIDGET_CLASS (label_class);
+
+       widget_class->size_allocate = netspeed_label_size_allocate;
+       widget_class->get_preferred_width = netspeed_label_get_preferred_width;
+}
+
+static void
+netspeed_label_init (NetspeedLabel *label)
+{
+}
+
+GtkWidget *
+netspeed_label_new (void)
+{
+  return g_object_new (NETSPEED_TYPE_LABEL, NULL);
+}
+
+void
+netspeed_label_set_dont_shrink (NetspeedLabel *label,
+                                gboolean       dont_shrink)
+{
+  g_return_if_fail (NETSPEED_IS_LABEL (label));
+  label->dont_shrink = dont_shrink;
+}
diff --git a/netspeed/src/label.h b/netspeed/src/label.h
new file mode 100644
index 0000000..4d3978e
--- /dev/null
+++ b/netspeed/src/label.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2015 Alberts Muktupāvels
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *     Alberts Muktupāvels <alberts muktupavels gmail com>
+ */
+
+#ifndef NETSPEED_LABEL_H
+#define NETSPEED_LABEL_H
+
+#include <gtk/gtk.h>
+
+#define NETSPEED_TYPE_LABEL netspeed_label_get_type ()
+G_DECLARE_FINAL_TYPE (NetspeedLabel, netspeed_label,
+                      NETSPEED, LABEL,
+                      GtkLabel)
+
+GtkWidget *netspeed_label_new             (void);
+void       netspeed_label_set_dont_shrink (NetspeedLabel *label,
+                                           gboolean       dont_shrink);
+
+#endif
diff --git a/netspeed/src/netspeed.c b/netspeed/src/netspeed.c
index 8452a58..54cd736 100644
--- a/netspeed/src/netspeed.c
+++ b/netspeed/src/netspeed.c
@@ -26,6 +26,7 @@
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 #include "backend.h"
+#include "label.h"
 #include "netspeed.h"
 
  /* Icons for the interfaces */
@@ -83,8 +84,6 @@ struct _NetspeedApplet
 
        GtkWidget      *signalbar;
 
-       gboolean        labels_dont_shrink;
-
        DevInfo         devinfo;
        gboolean        device_has_changed;
 
@@ -159,6 +158,7 @@ applet_change_size_or_orient(PanelApplet *applet_widget, int arg1, NetspeedApple
 {
        int size;
        PanelAppletOrient orient;
+       gboolean labels_dont_shrink;
        
        g_assert(applet);
        
@@ -203,21 +203,25 @@ applet_change_size_or_orient(PanelApplet *applet_widget, int arg1, NetspeedApple
                        applet->in_box = gtk_vbox_new(FALSE, 0);
                        applet->out_box = gtk_vbox_new(FALSE, 0);
                }
-               applet->labels_dont_shrink = FALSE;
+               labels_dont_shrink = FALSE;
        } else {
                applet->in_box = gtk_hbox_new(FALSE, 1);
                applet->out_box = gtk_hbox_new(FALSE, 1);
                if (size < 48) {
                        applet->sum_box = gtk_hbox_new(FALSE, 2);
                        applet->box = gtk_hbox_new(FALSE, 1);
-                       applet->labels_dont_shrink = TRUE;
+                       labels_dont_shrink = TRUE;
                } else {
                        applet->sum_box = gtk_vbox_new(FALSE, 0);
                        applet->box = gtk_vbox_new(FALSE, 0);
-                       applet->labels_dont_shrink = !applet->show_sum;
+                       labels_dont_shrink = !applet->show_sum;
                }
        }               
-       
+
+       netspeed_label_set_dont_shrink (NETSPEED_LABEL (applet->in_label), labels_dont_shrink);
+       netspeed_label_set_dont_shrink (NETSPEED_LABEL (applet->out_label), labels_dont_shrink);
+       netspeed_label_set_dont_shrink (NETSPEED_LABEL (applet->sum_label), labels_dont_shrink);
+
        gtk_box_pack_start(GTK_BOX(applet->in_box), applet->in_pix, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(applet->in_box), applet->in_label, TRUE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(applet->out_box), applet->out_pix, FALSE, FALSE, 0);
@@ -1262,23 +1266,6 @@ details_cb (GSimpleAction *action,
        gtk_widget_show_all(GTK_WIDGET(applet->details));
 }      
 
-/* Block the size_request signal emit by the label if the
- * text changes. Only if the label wants to grow, we give
- * permission. This will eventually result in the maximal
- * size of the applet and prevents the icons and labels from
- * "jumping around" in the panel which looks uggly
- */
-static void
-label_size_request_cb(GtkWidget *widget, GtkRequisition *requisition, NetspeedApplet *applet)
-{
-       if (applet->labels_dont_shrink) {
-               if (requisition->width <= applet->width)
-                       requisition->width = applet->width;
-               else
-                       applet->width = requisition->width;
-       }
-}      
-
 static void
 update_tooltip(NetspeedApplet* applet)
 {
@@ -1658,10 +1645,10 @@ netspeed_applet_factory (PanelApplet *applet,
                get_device_info ("lo", &netspeed->devinfo);
        netspeed->device_has_changed = TRUE;
 
-       netspeed->in_label = gtk_label_new ("");
-       netspeed->out_label = gtk_label_new ("");
-       netspeed->sum_label = gtk_label_new ("");
-       
+       netspeed->in_label = netspeed_label_new ();
+       netspeed->out_label = netspeed_label_new ();
+       netspeed->sum_label = netspeed_label_new ();
+
        netspeed->in_pix = gtk_image_new ();
        netspeed->out_pix = gtk_image_new ();
        netspeed->dev_pix = gtk_image_new ();
@@ -1701,17 +1688,6 @@ netspeed_applet_factory (PanelApplet *applet,
                          G_CALLBACK (applet_change_size_or_orient),
                          netspeed);
 
-       /* FIXME: size-request is removed signal... */
-       g_signal_connect (netspeed->in_label, "size_request",
-                         G_CALLBACK (label_size_request_cb),
-                         netspeed);
-       g_signal_connect (netspeed->out_label, "size_request",
-                         G_CALLBACK (label_size_request_cb),
-                         netspeed);
-       g_signal_connect (netspeed->sum_label, "size_request",
-                         G_CALLBACK (label_size_request_cb),
-                         netspeed);
-
        setup_menu (applet);
 
        return TRUE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]