[gnome-system-monitor/wip/newdesign: 6/7] Reworked GSM colorbutton api to support setting text to be displayed



commit fe17e6d02bd5bf48805231e8b01416b02562712d
Author: Robert Roth <robert roth off gmail com>
Date:   Thu Aug 15 00:56:19 2013 +0300

    Reworked GSM colorbutton api to support setting text to be displayed

 src/gsm_color_button.c |  124 ++++++++++++++++++++++++++++++++---------------
 src/gsm_color_button.h |    5 +-
 src/load-graph.cpp     |   14 +++--
 src/util.cpp           |   24 ++++-----
 src/util.h             |   23 +--------
 5 files changed, 109 insertions(+), 81 deletions(-)
---
diff --git a/src/gsm_color_button.c b/src/gsm_color_button.c
index 860b6cc..2e66a03 100644
--- a/src/gsm_color_button.c
+++ b/src/gsm_color_button.c
@@ -33,7 +33,8 @@ typedef struct
   GtkWidget *cc_dialog;                /* Color chooser dialog */
 
   gchar *title;                        /* Title for the color selection window */
-
+  gchar *text; 
+  
   GdkRGBA color;
   gdouble fraction;            /* Only used by GSMCP_TYPE_PIE */
   guint type;
@@ -53,6 +54,7 @@ enum
   PROP_TITLE,
   PROP_COLOR,
   PROP_TYPE, 
+  PROP_TEXT, 
 };
 
 /* Signals */
@@ -152,6 +154,14 @@ gsm_color_button_class_init (GsmColorButtonClass * klass)
                                                        _("The title of the color selection dialog"),
                                                        _("Pick a Color"),
                                                        G_PARAM_READWRITE));
+                            
+  g_object_class_install_property (gobject_class,
+                                  PROP_TEXT,
+                                  g_param_spec_string ("text",
+                                                       _("Text"),
+                                                       _("The text on the color picker"),
+                                                       NULL,
+                                                       G_PARAM_READWRITE));
 
   g_object_class_install_property (gobject_class,
                                   PROP_COLOR,
@@ -191,7 +201,7 @@ gsm_color_button_draw (GtkWidget *widget, cairo_t * cr)
   PangoLayout* layout;
   PangoFontDescription* font_desc;
   PangoRectangle extents;
-  gchar * caption = NULL;
+  GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (widget));
   
   if (sensitive && priv->highlight > 0) {
     highlight_factor = 0.125 * priv->highlight;
@@ -201,20 +211,17 @@ gsm_color_button_draw (GtkWidget *widget, cairo_t * cr)
     color->blue = MIN (1.0, color->blue + highlight_factor) ;
     
     color->green = MIN (1.0, color->green + highlight_factor);
-  } else if (!sensitive) {
-    GtkStyleContext *context = gtk_widget_get_style_context (widget);
-    
+  } else if (!sensitive)
     gtk_style_context_get_color (context, GTK_STATE_FLAG_INSENSITIVE, color);
-  }
+  
   gdk_cairo_set_source_rgba (cr, color);
   width  = gdk_window_get_width (gtk_widget_get_window (widget));
   height = gdk_window_get_height(gtk_widget_get_window (widget));
-  GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (widget));
+  
   
   switch (priv->type)
     {
-    case GSMCP_TYPE_NETWORK:
-    case GSMCP_TYPE_CPU:
+    case GSMCP_TYPE_RECTANGLE:
       // colored background
       cairo_paint (cr);
       cairo_set_line_width (cr, 1);
@@ -225,37 +232,35 @@ gsm_color_button_draw (GtkWidget *widget, cairo_t * cr)
       cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
       cairo_rectangle (cr, 1.5, 1.5, width - 3, height - 3);
       cairo_stroke (cr);
-      // label text with the usage percentage
-      layout = pango_cairo_create_layout (cr);
-      gtk_style_context_get (context, GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_FONT, &font_desc, NULL);
-      pango_font_description_set_size (font_desc, 10 * PANGO_SCALE );
-      pango_layout_set_font_description (layout, font_desc);
-      pango_font_description_free (font_desc);
-      pango_layout_set_alignment (layout, PANGO_ALIGN_RIGHT);
-      if (priv->type == GSMCP_TYPE_NETWORK) {
-        char* rate = g_format_size(priv->fraction);
-        caption = g_strdup_printf ("%s", rate);
-        g_free (rate);
-      }  else if (priv->type == GSMCP_TYPE_CPU) {
-        caption = g_strdup_printf ("%.1f%%", priv->fraction * 100.0f);
-      }
-      pango_layout_set_text (layout, caption, -1);
-      g_free (caption);
-      pango_layout_get_extents (layout, NULL, &extents);
-      // draw label outline
-      cairo_move_to (cr, (width - 1.3 * extents.width / PANGO_SCALE)/2 + 9.5 ,
-                     (height - 1.3 * extents.height / PANGO_SCALE)/2 + 2.5);
       
-      cairo_set_line_width (cr, 3);
-      cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
-      pango_cairo_layout_path (cr, layout);
-      cairo_stroke (cr);
-      // draw label text
-      cairo_move_to (cr, (width - 1.3 * extents.width / PANGO_SCALE)/2 + 9.1,
-                     (height - 1.3 * extents.height / PANGO_SCALE)/2 + 2);
-      cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
       
-      pango_cairo_show_layout (cr, layout);
+      if (priv->text != NULL) {
+        // label text with the usage percentage or network rate
+        layout = pango_cairo_create_layout (cr);
+        gtk_style_context_get (context, GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_FONT, &font_desc, NULL);
+        pango_font_description_set_size (font_desc, 10 * PANGO_SCALE );
+        pango_layout_set_font_description (layout, font_desc);
+        pango_font_description_free (font_desc);
+        pango_layout_set_alignment (layout, PANGO_ALIGN_RIGHT);
+        pango_layout_set_text (layout, priv->text, -1);
+        pango_layout_get_extents (layout, NULL, &extents);
+        // draw label outline
+        cairo_move_to (cr, (width - 1.3 * extents.width / PANGO_SCALE)/2 + 9.5 ,
+                       (height - 1.3 * extents.height / PANGO_SCALE)/2 + 2.5);
+      
+        cairo_set_line_width (cr, 3);
+        cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
+        pango_cairo_layout_path (cr, layout);
+        cairo_stroke (cr);
+        // draw label text
+        cairo_move_to (cr, (width - 1.3 * extents.width / PANGO_SCALE)/2 + 9.1,
+                       (height - 1.3 * extents.height / PANGO_SCALE)/2 + 2);
+        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
+      
+        pango_cairo_show_layout (cr, layout);
+      } 
+      
+      
       break;
     case GSMCP_TYPE_PIE:
       if (width < 32)          // 32px minimum size
@@ -448,9 +453,10 @@ gsm_color_button_init (GsmColorButton * color_button)
   priv->color.green = 0;
   priv->color.blue = 0;
   priv->fraction = 0.5;
-  priv->type = GSMCP_TYPE_CPU;
+  priv->type = GSMCP_TYPE_RECTANGLE;
   priv->image_buffer = NULL;
   priv->title = g_strdup (_("Pick a Color"));  /* default title */
+  priv->text = NULL;
   priv->in_button = FALSE;
   priv->button_down = FALSE;
   
@@ -488,7 +494,9 @@ gsm_color_button_finalize (GObject * object)
 
   g_free (priv->title);
   priv->title = NULL;
-
+  if (priv->text != NULL)
+    g_free (priv->text);
+  priv->text = NULL;
   cairo_surface_destroy (priv->image_buffer);
   priv->image_buffer = NULL;
 
@@ -739,6 +747,36 @@ gsm_color_button_get_title (GsmColorButton * color_button)
   return priv->title;
 }
 
+void
+gsm_color_button_set_text (GsmColorButton * color_button,
+                            const gchar * text)
+{
+  GsmColorButtonPrivate *priv;
+  gchar *old_text;
+
+  g_return_if_fail (GSM_IS_COLOR_BUTTON (color_button));
+
+  priv = gsm_color_button_get_instance_private (color_button);
+
+  old_text = priv->text;
+  priv->text = g_strdup (text);
+  if (old_text != NULL)
+    g_free (old_text);
+
+  g_object_notify (G_OBJECT (color_button), "text");
+}
+
+gchar *
+gsm_color_button_get_text (GsmColorButton * color_button)
+{
+  GsmColorButtonPrivate *priv;
+
+  g_return_val_if_fail (GSM_IS_COLOR_BUTTON (color_button), NULL);
+
+  priv = gsm_color_button_get_instance_private (color_button);
+  return priv->text;
+}
+
 static void
 gsm_color_button_set_property (GObject * object,
                               guint param_id,
@@ -755,6 +793,9 @@ gsm_color_button_set_property (GObject * object,
     case PROP_TITLE:
       gsm_color_button_set_title (color_button, g_value_get_string (value));
       break;
+    case PROP_TEXT:
+      gsm_color_button_set_text (color_button, g_value_get_string (value));
+      break;
     case PROP_COLOR:
       gsm_color_button_set_color (color_button, g_value_get_boxed (value));
       break;
@@ -784,6 +825,9 @@ gsm_color_button_get_property (GObject * object,
     case PROP_TITLE:
       g_value_set_string (value, gsm_color_button_get_title (color_button));
       break;
+    case PROP_TEXT:
+      g_value_set_string (value, gsm_color_button_get_text (color_button));
+      break;
     case PROP_COLOR:
       gsm_color_button_get_color (color_button, &color);
       g_value_set_boxed (value, &color);
diff --git a/src/gsm_color_button.h b/src/gsm_color_button.h
index 1b63e3b..484ae03 100644
--- a/src/gsm_color_button.h
+++ b/src/gsm_color_button.h
@@ -50,9 +50,8 @@ struct _GsmColorButton
 /* Widget types */
 enum
 {
-    GSMCP_TYPE_CPU,
+    GSMCP_TYPE_RECTANGLE,
     GSMCP_TYPE_PIE,
-    GSMCP_TYPE_NETWORK,
     GSMCP_TYPES
 };
 
@@ -71,6 +70,8 @@ gdouble     gsm_color_button_get_fraction  (GsmColorButton * color_button);
 guint       gsm_color_button_get_cbtype    (GsmColorButton * color_button);
 void        gsm_color_button_set_title     (GsmColorButton * color_button, const gchar * title);
 gchar     * gsm_color_button_get_title     (GsmColorButton * color_button);
+void        gsm_color_button_set_text      (GsmColorButton * color_button, const gchar * text);
+gchar     * gsm_color_button_get_text      (GsmColorButton * color_button);
 
 G_END_DECLS
 
diff --git a/src/load-graph.cpp b/src/load-graph.cpp
index 5207ebb..281f4f7 100644
--- a/src/load-graph.cpp
+++ b/src/load-graph.cpp
@@ -340,7 +340,9 @@ get_load (LoadGraph *graph)
             }
         }
 
-        gsm_color_button_set_fraction (GSM_COLOR_BUTTON (load_graph_get_cpu_color_picker (graph, i)), load);
+        gchar *label = g_strdup_printf("%.1f%%", load * 100.0f);
+        gsm_color_button_set_text (GSM_COLOR_BUTTON (load_graph_get_cpu_color_picker (graph, i)), label);
+        g_free (label);
     }
 
     graph->cpu.now ^= 1;
@@ -608,8 +610,8 @@ get_net (LoadGraph *graph)
     if (!first)
         net_scale(graph, din, dout);
 
-    gsm_color_button_set_fraction (GSM_COLOR_BUTTON (graph->net_in_color_picker), din);
-    gsm_color_button_set_fraction (GSM_COLOR_BUTTON (graph->net_out_color_picker), dout);
+    gsm_color_button_set_text (GSM_COLOR_BUTTON (graph->net_in_color_picker), 
procman::format_network_rate(din).c_str());
+    gsm_color_button_set_text (GSM_COLOR_BUTTON (graph->net_out_color_picker), 
procman::format_network_rate(dout).c_str());
 }
 
 
@@ -760,7 +762,7 @@ LoadGraph::LoadGraph(guint type)
             memcpy(&colors[0], GsmApplication::get()->config.cpu_color,
                    n * sizeof colors[0]);
             for(guint i = 0; i < n; ++i) {
-                cpu_color_pickers = g_list_append (cpu_color_pickers, GTK_WIDGET (gsm_color_button_new 
(&colors[i], GSMCP_TYPE_CPU)));
+                cpu_color_pickers = g_list_append (cpu_color_pickers, GTK_WIDGET (gsm_color_button_new 
(&colors[i], GSMCP_TYPE_RECTANGLE)));
             }
             break;
         case LOAD_GRAPH_MEM:
@@ -775,9 +777,9 @@ LoadGraph::LoadGraph(guint type)
             colors[0] = GsmApplication::get()->config.net_in_color;
             colors[1] = GsmApplication::get()->config.net_out_color;
             net_in_color_picker = gsm_color_button_new (&colors[0],
-                                                        GSMCP_TYPE_NETWORK);
+                                                        GSMCP_TYPE_RECTANGLE);
             net_out_color_picker = gsm_color_button_new (&colors[1],
-                                                        GSMCP_TYPE_NETWORK);
+                                                        GSMCP_TYPE_RECTANGLE);
             break;
     }
 
diff --git a/src/util.cpp b/src/util.cpp
index 04f03cb..88ff429 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -144,7 +144,7 @@ procman_make_label_for_mmaps_or_ofiles(const char *format,
  **/
 
 gchar*
-procman_format_size(guint64 size, guint64 max_size, bool want_bits)
+procman::format_size(guint64 size, guint64 max_size, bool want_bits)
 {
 
     enum {
@@ -207,15 +207,6 @@ procman_format_size(guint64 size, guint64 max_size, bool want_bits)
     }
 }
 
-gchar* procman_format_rate(guint64 rate, guint64 max_rate, bool want_bits)
-{
-    char* bytes = procman_format_size(rate, max_rate, want_bits);
-    // xgettext: rate, 10MiB/s or 10Mbit/s
-    gchar* formatted_rate = g_strdup_printf(_("%s/s"), bytes);
-    g_free(bytes);
-    return formatted_rate;
-}
-    
 gchar *
 procman::get_nice_level (gint nice)
 {
@@ -630,11 +621,18 @@ namespace procman
         g_free(current_value);
     }
 
-
+    std::string format_rate(guint64 rate, guint64 max_rate, bool want_bits)
+    {
+        char* bytes = procman::format_size(rate, max_rate, want_bits);
+        // xgettext: rate, 10MiB/s or 10Mbit/s
+        std::string formatted_rate(make_string(g_strdup_printf(_("%s/s"), bytes)));
+        g_free(bytes);
+        return formatted_rate;
+    }
 
     std::string format_network(guint64 rate, guint64 max_rate)
     {
-        char* bytes = procman_format_size(rate, max_rate, GsmApplication::get()->config.network_in_bits);
+        char* bytes = procman::format_size(rate, max_rate, GsmApplication::get()->config.network_in_bits);
         std::string formatted(bytes);
         g_free(bytes);
         return formatted;
@@ -642,7 +640,7 @@ namespace procman
     
     std::string format_network_rate(guint64 rate, guint64 max_rate)
     {
-        return procman_format_rate (rate, max_rate, GsmApplication::get()->config.network_in_bits);
+        return procman::format_rate(rate, max_rate, GsmApplication::get()->config.network_in_bits);
     }
 
 }
diff --git a/src/util.h b/src/util.h
index c3ac57f..f0dc381 100644
--- a/src/util.h
+++ b/src/util.h
@@ -5,16 +5,10 @@
 #include <glib.h>
 #include <gtk/gtk.h>
 
-#ifdef __cplusplus
-#define EXTERNC extern "C"
 #include <string>
 
 using std::string;
 
-#else
-#define EXTERNC
-#endif
-
 /* check if logind is running */
 #define LOGIND_RUNNING() (access("/run/systemd/seats/", F_OK) >= 0)
 
@@ -30,18 +24,6 @@ const char*
 format_process_state(guint state);
 
 
-EXTERNC 
-gchar* 
-procman_format_rate(guint64 size, guint64 max, gboolean want_bits);
-
-
-#ifdef __cplusplus
-
-gchar* 
-procman_format_size(guint64 size, guint64 max = 0, gboolean want_bits = false);
-gchar* 
-procman_format_rate(guint64 size, guint64 max = 0, gboolean want_bits = false);
-
 void
 procman_debug_real(const char *file, int line, const char *func,
                    const char *format, ...) G_GNUC_PRINTF(4, 5);
@@ -134,7 +116,9 @@ namespace procman
     {
         tree_store_update<const char>(model, iter, column, new_value);
     }
-
+    
+    gchar* format_size(guint64 size, guint64 max = 0, bool want_bits = false);
+    
     gchar* get_nice_level (gint nice);
 
     gchar* get_nice_level_with_priority (gint nice);
@@ -144,5 +128,4 @@ namespace procman
     std::string format_network(guint64 rate, guint64 max_rate = 0);
     std::string format_network_rate(guint64 rate, guint64 max_rate = 0);
 }
-#endif /* __cpluspluc */
 #endif /* _GSM_UTIL_H_ */


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