[Xfce4-commits] <xfce4-cpugraph-plugin:master> Make the cpu bars optional

Florian noreply at xfce.org
Sat Apr 17 16:16:39 CEST 2010


Updating branch refs/heads/master
         to 55bf6f0f9bae6e6e22cd137722023e2deb5e9333 (commit)
       from b3e6892eb8a2eda35c13ac2a10e0a9a2791fe227 (commit)

commit 55bf6f0f9bae6e6e22cd137722023e2deb5e9333
Author: Florian <frivoal at gmail.com>
Date:   Thu Mar 11 22:47:43 2010 +0900

    Make the cpu bars optional
    
    The progress bars used to show the level of activity on each cpu were
    not part of the early versions of cpugraph, and some user liked it
    better without. Make it possible to turn them off. This resolves the
    second issue of bug 5433.

 panel-plugin/cpu.c        |  142 ++++++++++++++++++++++++++++++++-------------
 panel-plugin/cpu.h        |    2 +
 panel-plugin/properties.c |    7 ++
 panel-plugin/settings.c   |    3 +
 4 files changed, 114 insertions(+), 40 deletions(-)

diff --git a/panel-plugin/cpu.c b/panel-plugin/cpu.c
index 125350a..6788347 100644
--- a/panel-plugin/cpu.c
+++ b/panel-plugin/cpu.c
@@ -5,10 +5,14 @@
 
 static void cpugraph_construct( XfcePanelPlugin *plugin );
 static CPUGraph *create_gui( XfcePanelPlugin *plugin );
+static void create_bars( CPUGraph *base );
 static guint init_cpu_data( CpuData **data );
 static void shutdown( XfcePanelPlugin *plugin, CPUGraph *base );
+static void delete_bars( CPUGraph *base );
 static gboolean size_cb( XfcePanelPlugin *plugin, int size, CPUGraph *base );
+static void set_bars_size( CPUGraph *base, gint size, GtkOrientation orientation );
 static void orientation_cb( XfcePanelPlugin *plugin, GtkOrientation orientation, CPUGraph *base );
+static void set_bars_orientation( CPUGraph *base, GtkOrientation orientation);
 static gboolean update_cb( CPUGraph *base );
 static void update_tooltip( CPUGraph *base );
 static void draw_area_cb( GtkWidget *da, GdkEventExpose *event, gpointer data );
@@ -55,20 +59,16 @@ static CPUGraph * create_gui( XfcePanelPlugin * plugin )
 	base->m_Box = xfce_hvbox_new(orientation, FALSE, 0);
 	gtk_container_add(GTK_CONTAINER(ebox), base->m_Box);
 
-	base->m_pBar = (GtkWidget **) g_malloc( sizeof( GtkWidget * ) * base->nr_cores );
-
-	for(i=0; i<base->nr_cores; i++) {
-		base->m_pBar[i] = GTK_WIDGET(gtk_progress_bar_new());
-		gtk_box_pack_start( GTK_BOX(base->m_Box), base->m_pBar[i], FALSE, FALSE, 0);
-	}
-
 	base->m_FrameWidget = frame = gtk_frame_new( NULL );
-	gtk_box_pack_start( GTK_BOX(base->m_Box), frame, TRUE, TRUE, 0);
+	gtk_box_pack_end( GTK_BOX(base->m_Box), frame, TRUE, TRUE, 0);
 
 	base->m_DrawArea = gtk_drawing_area_new();
 	gtk_container_add( GTK_CONTAINER( frame ), GTK_WIDGET( base->m_DrawArea ) );
 	g_signal_connect_after( base->m_DrawArea, "expose-event", G_CALLBACK( draw_area_cb ), base );
 
+	base->bars = FALSE;
+	base->m_pBar = NULL;
+
 	orientation_cb(plugin, orientation, base);
 
 	gtk_widget_show_all(ebox);
@@ -76,6 +76,18 @@ static CPUGraph * create_gui( XfcePanelPlugin * plugin )
 	return base;
 }
 
+static void create_bars( CPUGraph *base )
+{
+	gint i;
+	base->m_pBar = (GtkWidget **) g_malloc( sizeof( GtkWidget * ) * base->nr_cores );
+
+	for(i=0; i<base->nr_cores; i++) {
+		base->m_pBar[i] = GTK_WIDGET(gtk_progress_bar_new());
+		gtk_box_pack_end( GTK_BOX(base->m_Box), base->m_pBar[i], FALSE, FALSE, 0);
+		gtk_widget_show( base->m_pBar[i] );
+	}
+}
+
 guint init_cpu_data( CpuData **data )
 {
 	guint cpuNr;
@@ -91,13 +103,10 @@ guint init_cpu_data( CpuData **data )
 
 static void shutdown( XfcePanelPlugin * plugin, CPUGraph * base )
 {
-	gint i;
 	g_free( base->cpu_data );
 	base->cpu_data = NULL;
 
-	for(i=0; i<base->nr_cores-1; i++)
-		gtk_widget_destroy(base->m_pBar[i]);
-	g_free( base->m_pBar );
+	delete_bars( base );
 
 	gtk_widget_destroy(base->m_Box);
 
@@ -110,59 +119,91 @@ static void shutdown( XfcePanelPlugin * plugin, CPUGraph * base )
 	g_free( base );
 }
 
-static gboolean size_cb( XfcePanelPlugin *plugin, int size, CPUGraph *base )
+static void delete_bars( CPUGraph *base )
 {
 	gint i;
-	gint frame_h, frame_v, bar_h, bar_v, history;
+	if( base->m_pBar )
+	{
+		for( i=0; i < base->nr_cores; i++ )
+		{
+			gtk_widget_hide( base->m_pBar[i] );
+			gtk_widget_destroy( base->m_pBar[i] );
+		}
+		g_free( base->m_pBar );
+		base->m_pBar = NULL;
+	}
+}
 
-	if( xfce_panel_plugin_get_orientation( plugin ) == GTK_ORIENTATION_HORIZONTAL )
+static gboolean size_cb( XfcePanelPlugin *plugin, int size, CPUGraph *base )
+{
+	gint frame_h, frame_v, history;
+	GtkOrientation orientation;
+	
+	orientation = xfce_panel_plugin_get_orientation( plugin );
+
+	if( orientation == GTK_ORIENTATION_HORIZONTAL )
 	{
 		frame_h = base->size;
 		frame_v = size;
-		bar_h = BORDER;
-		bar_v = size;
 	       	history = base->size;
 	}
 	else
 	{
 		frame_h = size;
 		frame_v = base->size;
-		bar_h = size;
-		bar_v = BORDER;
 	       	history = size;
 	}
 
 	gtk_widget_set_size_request( GTK_WIDGET( base->m_FrameWidget ), frame_h, frame_v );
 
-	for( i=0; i<base->nr_cores; i++ )
-		gtk_widget_set_size_request( GTK_WIDGET(base->m_pBar[i]), bar_h, bar_v );
-
 	base->history = (int *) g_realloc( base->history, history * sizeof( int ) );
 	if( history > base->history_size )
 		memset( base->history + base->history_size, 0, (history - base->history_size) * sizeof( int ) );
 	base->history_size = history;
 
+	if( base->bars )
+		set_bars_size( base, size, orientation );
+
 	return TRUE;
 }
 
-static void orientation_cb( XfcePanelPlugin * plugin, GtkOrientation orientation, CPUGraph *base )
+static void set_bars_size( CPUGraph *base, gint size, GtkOrientation orientation )
 {
-	GtkProgressBarOrientation barOrientation;
-	gpointer p_pBar[base->nr_cores];
-	gint i; 
+	gint i;
+	gint h, v;
+	if( orientation == GTK_ORIENTATION_HORIZONTAL )
+	{
+		h = BORDER;
+		v = size;
+	}
+	else
+	{
+		h = size;
+		v = BORDER;
+	}
+	for( i=0; i < base->nr_cores; i++ )
+		gtk_widget_set_size_request( GTK_WIDGET(base->m_pBar[i]), h, v );
+}
 
+static void orientation_cb( XfcePanelPlugin * plugin, GtkOrientation orientation, CPUGraph *base )
+{
 	xfce_hvbox_set_orientation( XFCE_HVBOX( base->m_Box ), orientation );
+	if( base->bars )
+		set_bars_orientation( base, orientation );
+
+}
 
+static void set_bars_orientation( CPUGraph *base, GtkOrientation orientation)
+{
+	GtkProgressBarOrientation barOrientation;
+	gint i; 
 	if( orientation == GTK_ORIENTATION_HORIZONTAL )
 		barOrientation = GTK_PROGRESS_BOTTOM_TO_TOP;
 	else
 		barOrientation = GTK_PROGRESS_LEFT_TO_RIGHT;
 
 	for( i=0; i<base->nr_cores; i++ )
-	{
 		gtk_progress_bar_set_orientation( GTK_PROGRESS_BAR( base->m_pBar[i] ), barOrientation );	
-	}
-
 }
 
 static gboolean update_cb( CPUGraph * base )
@@ -170,18 +211,21 @@ static gboolean update_cb( CPUGraph * base )
 	gint i;
 	if( !read_cpu_data( base->cpu_data, base->nr_cores ) )
 		return TRUE;
-	if( base->nr_cores == 1 )
-	{
-		gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(base->m_pBar[0]),
-		                               (gdouble)base->cpu_data[0].load / CPU_SCALE
-		                             );
-	}
-	else
+	if( base->bars )
 	{
-		for( i=0; i<base->nr_cores; i++ )
-			gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(base->m_pBar[i]),
-			                               (gdouble)base->cpu_data[i+1].load / CPU_SCALE
-			                             );
+		if( base->nr_cores == 1 )
+		{
+			gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(base->m_pBar[0]),
+					(gdouble)base->cpu_data[0].load / CPU_SCALE
+					);
+		}
+		else
+		{
+			for( i=0; i<base->nr_cores; i++ )
+				gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(base->m_pBar[i]),
+						(gdouble)base->cpu_data[i+1].load / CPU_SCALE
+						);
+		}
 	}
 
 	if( base->non_linear )
@@ -271,6 +315,24 @@ void set_command( CPUGraph *base, const gchar *command )
 	base->command = g_strdup( command );
 }
 
+void set_bars( CPUGraph * base, gboolean bars)
+{
+	GtkOrientation orientation;
+	if( base->bars != bars )
+	{
+		base->bars = bars;
+		if(bars)
+		{
+			orientation = xfce_panel_plugin_get_orientation( base->plugin );
+			create_bars( base );
+			set_bars_orientation( base, orientation );
+			set_bars_size( base, xfce_panel_plugin_get_size( base->plugin ), orientation );
+		}
+		else
+			delete_bars( base );
+	}
+}
+
 void set_border( CPUGraph *base, gboolean border )
 {
 	base->border = border;
diff --git a/panel-plugin/cpu.h b/panel-plugin/cpu.h
index 4937f60..25d7d4f 100644
--- a/panel-plugin/cpu.h
+++ b/panel-plugin/cpu.h
@@ -39,6 +39,7 @@ typedef struct
 	int color_mode;
 	gboolean frame;
 	gboolean border;
+	gboolean bars;
 	gchar  *command;
 	GdkColor colors[4];
 
@@ -53,6 +54,7 @@ typedef struct
 } CPUGraph;
 
 void set_command( CPUGraph *base, const gchar *command );
+void set_bars( CPUGraph * base, gboolean bars);
 void set_border( CPUGraph *base, gboolean border);
 void set_frame( CPUGraph *base, gboolean frame );
 void set_nonlinear_time( CPUGraph *base, gboolean nonlinear );
diff --git a/panel-plugin/properties.c b/panel-plugin/properties.c
index 00e6d93..c4c9099 100644
--- a/panel-plugin/properties.c
+++ b/panel-plugin/properties.c
@@ -25,6 +25,7 @@ static void change_color_mode( GtkOptionMenu *om, CPUGraph *base );
 static void response_cb( GtkWidget *dlg, int response, CPUGraph *base );
 static void change_frame( GtkToggleButton *button, CPUGraph *base );
 static void change_border( GtkToggleButton *button, CPUGraph *base );
+static void change_bars( GtkToggleButton * button, CPUGraph * base );
 static void change_size( GtkSpinButton *sb, CPUGraph *base );
 static void change_time_scale( GtkToggleButton *button, CPUGraph *base );
 static void change_update( GtkOptionMenu *om, CPUGraph *base );
@@ -67,6 +68,7 @@ void create_options( XfcePanelPlugin *plugin, CPUGraph *base )
 	create_check_box( vbox, sg, _("Non-linear time-scale"), base->non_linear, change_time_scale, base );
 	create_check_box( vbox, sg, _("Show frame"), base->frame, change_frame, base );
 	create_check_box( vbox, sg, _("Border"), base->border, change_border, base );
+	create_check_box( vbox, sg, _("Show Bar(s)"), base->bars, change_bars, base );
 	setup_command_option( vbox, sg, base );
 
 	vbox2 = create_tab();
@@ -318,6 +320,11 @@ static void change_border( GtkToggleButton * button, CPUGraph * base )
 	set_border( base, gtk_toggle_button_get_active( button ) );
 }
 
+static void change_bars( GtkToggleButton * button, CPUGraph * base )
+{
+	set_bars( base, gtk_toggle_button_get_active( button ) );
+}
+
 static void change_size( GtkSpinButton * sb, CPUGraph *base)
 {
 	set_size( base, gtk_spin_button_get_value_as_int( sb ) );
diff --git a/panel-plugin/settings.c b/panel-plugin/settings.c
index 8673262..0e97d2c 100644
--- a/panel-plugin/settings.c
+++ b/panel-plugin/settings.c
@@ -15,6 +15,7 @@ void read_settings( XfcePanelPlugin * plugin, CPUGraph * base )
 	int color_mode = 0;
 	gboolean frame = FALSE;
 	gboolean border = TRUE;
+	gboolean bars = TRUE;
 	const gchar  *associated_command = DEFAULT_COMMAND;
 
 	GdkColor foreground1;
@@ -53,6 +54,7 @@ void read_settings( XfcePanelPlugin * plugin, CPUGraph * base )
 			frame = xfce_rc_read_int_entry( rc, "Frame", frame );
 			associated_command = xfce_rc_read_entry( rc, "AssociateCommand", associated_command );
 			border = xfce_rc_read_int_entry( rc, "Border", border );
+			bars = xfce_rc_read_int_entry( rc, "Bars", bars );
 
 			if( (value = xfce_rc_read_entry( rc, "Foreground1", NULL )) )
 				gdk_color_parse( value, &foreground1 );
@@ -75,6 +77,7 @@ void read_settings( XfcePanelPlugin * plugin, CPUGraph * base )
 	set_frame( base, frame );
 	set_command( base, associated_command );
 	set_border( base, border);
+	set_bars( base, bars);
 	set_color( base, 1, foreground1 );
 	set_color( base, 2, foreground2 );
 	set_color( base, 3, foreground3 );



More information about the Xfce4-commits mailing list