[Xfce4-commits] <xfce4-taskmanager:master> Put "Settings" items within a sub-classed GtkMenuToolButton
Mike Massonnet
noreply at xfce.org
Sun Aug 8 14:14:01 CEST 2010
Updating branch refs/heads/master
to 4fc28e6b8ba263e79f630b5a299b7d1ceafece80 (commit)
from b4d453b526f937a367ca2cdc8c313e21e12c5139 (commit)
commit 4fc28e6b8ba263e79f630b5a299b7d1ceafece80
Author: Mike Massonnet <mmassonnet at xfce.org>
Date: Wed Aug 4 11:44:21 2010 +0200
Put "Settings" items within a sub-classed GtkMenuToolButton
New class XtmSettingsToolButton that replaces the old GtkToolButton for
the settings menu.
src/Makefile.am | 1 +
src/exec-tool-button.c | 24 +-----
src/process-window.c | 141 +++-----------------------------
src/process-window.ui | 8 +-
src/settings-tool-button.c | 194 ++++++++++++++++++++++++++++++++++++++++++++
src/settings-tool-button.h | 32 +++++++
6 files changed, 250 insertions(+), 150 deletions(-)
diff --git a/src/Makefile.am b/src/Makefile.am
index 87df5eb..2d16b7a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,6 +30,7 @@ xfce4_taskmanager_SOURCES = \
process-tree-view.c process-tree-view.h \
process-statusbar.c process-statusbar.h \
exec-tool-button.c exec-tool-button.h \
+ settings-tool-button.c settings-tool-button.h \
task-manager.c task-manager.h \
settings.c settings.h \
$(NULL)
diff --git a/src/exec-tool-button.c b/src/exec-tool-button.c
index 960e525..4ff0ce7 100644
--- a/src/exec-tool-button.c
+++ b/src/exec-tool-button.c
@@ -15,6 +15,10 @@
#include <glib/gi18n.h>
#include <gtk/gtk.h>
+#ifdef HAVE_GKSU
+#include <libgksu/libgksu.h>
+#endif
+
#include "exec-tool-button.h"
@@ -31,8 +35,6 @@ struct _XtmExecToolButton
};
G_DEFINE_TYPE (XtmExecToolButton, xtm_exec_tool_button, GTK_TYPE_MENU_TOOL_BUTTON)
-static void xtm_exec_tool_button_finalize (GObject *object);
-
static GtkWidget * construct_menu ();
static void execute_default_command ();
@@ -41,9 +43,7 @@ static void execute_default_command ();
static void
xtm_exec_tool_button_class_init (XtmExecToolButtonClass *klass)
{
- GObjectClass *class = G_OBJECT_CLASS (klass);
xtm_exec_tool_button_parent_class = g_type_class_peek_parent (klass);
- class->finalize = xtm_exec_tool_button_finalize;
}
static void
@@ -61,12 +61,6 @@ xtm_exec_tool_button_init (XtmExecToolButton *button)
gtk_widget_show_all (GTK_WIDGET (button));
}
-static void
-xtm_exec_tool_button_finalize (GObject *object)
-{
- G_OBJECT_CLASS (xtm_exec_tool_button_parent_class)->finalize (object);
-}
-
#ifdef HAVE_GKSU
@@ -191,20 +185,12 @@ construct_menu ()
menu_append_item (GTK_MENU (menu), _("Terminal emulator"), "exo-open --launch TerminalEmulator", "terminal");
else if (program_exists ("xterm"))
menu_append_item (GTK_MENU (menu), _("XTerm"), "xterm -fg grey -bg black", "terminal");
+
gtk_widget_show_all (menu);
return menu;
}
-static void
-position_menu (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, GtkWidget *widget)
-{
- gdk_window_get_origin (widget->window, x, y);
- *x += widget->allocation.x;
- *y += widget->allocation.height;
- *push_in = TRUE;
-}
-
GtkWidget *
diff --git a/src/process-window.c b/src/process-window.c
index 3aa20f2..8432634 100644
--- a/src/process-window.c
+++ b/src/process-window.c
@@ -19,10 +19,6 @@
#include <glib/gi18n.h>
#include <gtk/gtk.h>
-#ifdef HAVE_GKSU
-#include <libgksu/libgksu.h>
-#endif
-
#include "settings.h"
#include "process-window.h"
#include "process-window_ui.h"
@@ -30,6 +26,7 @@
#include "process-tree-view.h"
#include "process-statusbar.h"
#include "exec-tool-button.h"
+#include "settings-tool-button.h"
@@ -53,6 +50,8 @@ struct _XtmProcessWindowPriv
GtkWidget * mem_monitor;
GtkWidget * treeview;
GtkWidget * statusbar;
+ GtkWidget * exec_button;
+ GtkWidget * settings_button;
XtmSettings * settings;
};
#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), XTM_TYPE_PROCESS_WINDOW, XtmProcessWindowPriv))
@@ -65,8 +64,6 @@ static void xtm_process_window_hide (GtkWidget *widget);
static void emit_destroy_signal (XtmProcessWindow *window);
static gboolean emit_delete_event_signal (XtmProcessWindow *window, GdkEvent *event);
static void monitor_update_step_size (XtmProcessWindow *window);
-static void show_menu_execute_task (XtmProcessWindow *window, GtkButton *button);
-static void show_menu_preferences (XtmProcessWindow *window, GtkButton *button);
static void show_about_dialog (XtmProcessWindow *window);
@@ -144,11 +141,11 @@ xtm_process_window_init (XtmProcessWindow *window)
gtk_widget_show (window->priv->statusbar);
gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (window->priv->builder, "process-vbox")), window->priv->statusbar, FALSE, FALSE, 0);
- button = xtm_exec_tool_button_new ();
- gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (window->priv->builder, "toolbutton-execute")), button);
+ window->priv->exec_button = xtm_exec_tool_button_new ();
+ gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (window->priv->builder, "toolbutton-execute")), window->priv->exec_button);
- button = GTK_WIDGET (gtk_builder_get_object (window->priv->builder, "toolbutton-preferences"));
- g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_menu_preferences), window);
+ window->priv->settings_button = xtm_settings_tool_button_new ();
+ gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (window->priv->builder, "toolbutton-settings")), window->priv->settings_button);
button = GTK_WIDGET (gtk_builder_get_object (window->priv->builder, "toolbutton-about"));
g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_about_dialog), window);
@@ -184,6 +181,12 @@ xtm_process_window_finalize (GObject *object)
if (GTK_IS_STATUSBAR (priv->statusbar))
gtk_widget_destroy (priv->statusbar);
+ if (GTK_IS_TOOL_ITEM (priv->exec_button))
+ gtk_widget_destroy (priv->exec_button);
+
+ if (GTK_IS_TOOL_ITEM (priv->settings_button))
+ gtk_widget_destroy (priv->settings_button);
+
if (XTM_IS_SETTINGS (priv->settings))
g_object_unref (priv->settings);
}
@@ -215,124 +218,6 @@ monitor_update_step_size (XtmProcessWindow *window)
g_object_set (window->priv->mem_monitor, "step-size", refresh_rate / 1000.0, NULL);
}
-static void
-menu_position_func (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, GtkWidget *widget)
-{
- gdk_window_get_origin (widget->window, x, y);
- *x += widget->allocation.x;
- *y += widget->allocation.height;
- *push_in = TRUE;
-}
-
-static void
-refresh_rate_toggled (GtkCheckMenuItem *mi, XtmSettings *settings)
-{
- guint refresh_rate = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (mi), "refresh-rate"));
- g_object_set (settings, "refresh-rate", refresh_rate, NULL);
-}
-
-static void
-menu_refresh_rate_append_item (GtkMenu *menu, gchar *title, guint refresh_rate, XtmSettings *settings)
-{
- GtkWidget *mi;
- guint cur_refresh_rate;
-
- g_object_get (settings, "refresh-rate", &cur_refresh_rate, NULL);
-
- if (cur_refresh_rate == refresh_rate)
- {
- mi = gtk_radio_menu_item_new_with_label (NULL, title);
- gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi), TRUE);
- }
- else
- {
- mi = gtk_menu_item_new_with_label (title);
- }
- gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
- g_object_set_data (G_OBJECT (mi), "refresh-rate", GUINT_TO_POINTER (refresh_rate));
- g_signal_connect (mi, "activate", G_CALLBACK (refresh_rate_toggled), settings);
-}
-
-static GtkWidget *
-build_refresh_rate_menu (XtmSettings *settings)
-{
- GtkWidget *menu;
-
- menu = gtk_menu_new ();
-
- /* TRANSLATORS: The next values are in seconds or milliseconds */
- menu_refresh_rate_append_item (GTK_MENU (menu), _("500ms"), 500, settings);
- menu_refresh_rate_append_item (GTK_MENU (menu), _("750ms"), 750, settings);
- menu_refresh_rate_append_item (GTK_MENU (menu), _("1s"), 1000, settings);
- menu_refresh_rate_append_item (GTK_MENU (menu), _("2s"), 2000, settings);
- menu_refresh_rate_append_item (GTK_MENU (menu), _("5s"), 5000, settings);
- menu_refresh_rate_append_item (GTK_MENU (menu), _("10s"), 10000, settings);
-
- return menu;
-}
-
-static void
-preferences_toggled (GtkCheckMenuItem *mi, XtmSettings *settings)
-{
- gboolean active = gtk_check_menu_item_get_active (mi);
- gchar *setting_name = g_object_get_data (G_OBJECT (mi), "setting-name");
- g_object_set (settings, setting_name, active, NULL);
-}
-
-static void
-menu_preferences_append_item (GtkMenu *menu, gchar *title, gchar *setting_name, XtmSettings *settings)
-{
- GtkWidget *mi;
- gboolean active = FALSE;
-
- g_object_get (settings, setting_name, &active, NULL);
-
- mi = gtk_check_menu_item_new_with_label (title);
- gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi), active);
- gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
- g_object_set_data (G_OBJECT (mi), "setting-name", setting_name);
- g_signal_connect (mi, "toggled", G_CALLBACK (preferences_toggled), settings);
-}
-
-static void
-show_menu_preferences (XtmProcessWindow *window, GtkButton *button)
-{
- static GtkWidget *menu = NULL;
- GtkWidget *refresh_rate_menu;
- GtkWidget *mi;
-
- if (menu != NULL)
- {
- gtk_widget_destroy (menu);
- }
-
- menu = gtk_menu_new ();
- menu_preferences_append_item (GTK_MENU (menu), _("Show all processes"), "show-all-processes", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("More precision"), "more-precision", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("Full command line"), "full-command-line", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("Show status icon"), "show-status-icon", window->priv->settings);
-
- refresh_rate_menu = build_refresh_rate_menu (window->priv->settings);
- mi = gtk_menu_item_new_with_label (_("Refresh rate"));
- gtk_menu_item_set_submenu (GTK_MENU_ITEM (mi), refresh_rate_menu);
- gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
-
- mi = gtk_separator_menu_item_new ();
- gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
-
- menu_preferences_append_item (GTK_MENU (menu), _("PID"), "column-pid", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("PPID"), "column-ppid", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("State"), "column-state", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("Virtual Bytes"), "column-vsz", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("Private Bytes"), "column-rss", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("UID"), "column-uid", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("CPU"), "column-cpu", window->priv->settings);
- menu_preferences_append_item (GTK_MENU (menu), _("Priority"), "column-priority", window->priv->settings);
-
- gtk_widget_show_all (menu);
- gtk_menu_popup (GTK_MENU (menu), NULL, NULL, (GtkMenuPositionFunc)menu_position_func, button, 0, gtk_get_current_event_time ());
-}
-
#if !GTK_CHECK_VERSION(2,18,0)
static void
url_hook_about_dialog (GtkAboutDialog *dialog, const gchar *uri, gpointer user_data)
diff --git a/src/process-window.ui b/src/process-window.ui
index ff61708..d6e7da2 100644
--- a/src/process-window.ui
+++ b/src/process-window.ui
@@ -29,10 +29,12 @@
</packing>
</child>
<child>
- <object class="GtkToolButton" id="toolbutton-preferences">
+ <object class="GtkToolItem" id="toolbutton-settings">
<property name="visible">True</property>
- <property name="use_underline">True</property>
- <property name="stock_id">gtk-preferences</property>
+ <property name="is_important">True</property>
+ <child>
+ <placeholder/>
+ </child>
</object>
<packing>
<property name="expand">False</property>
diff --git a/src/settings-tool-button.c b/src/settings-tool-button.c
new file mode 100644
index 0000000..7842167
--- /dev/null
+++ b/src/settings-tool-button.c
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2010 Mike Massonnet, <mmassonnet at 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib-object.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "settings-tool-button.h"
+#include "settings.h"
+
+
+
+typedef struct _XtmSettingsToolButtonClass XtmSettingsToolButtonClass;
+struct _XtmSettingsToolButtonClass
+{
+ GtkMenuToolButtonClass parent_class;
+};
+struct _XtmSettingsToolButton
+{
+ GtkMenuToolButton parent;
+ /*<private>*/
+};
+G_DEFINE_TYPE (XtmSettingsToolButton, xtm_settings_tool_button, GTK_TYPE_MENU_TOOL_BUTTON)
+
+static GtkWidget * construct_menu ();
+static void show_settings_dialog ();
+
+
+
+static void
+xtm_settings_tool_button_class_init (XtmSettingsToolButtonClass *klass)
+{
+ xtm_settings_tool_button_parent_class = g_type_class_peek_parent (klass);
+}
+
+static void
+xtm_settings_tool_button_init (XtmSettingsToolButton *button)
+{
+ GtkWidget *menu;
+
+ gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (button), "gtk-preferences");
+ gtk_tool_button_set_use_underline (GTK_TOOL_BUTTON (button), TRUE);
+
+ menu = construct_menu ();
+ gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (button), menu);
+ g_signal_connect (button, "clicked", G_CALLBACK (show_settings_dialog), NULL);
+
+ gtk_widget_show_all (GTK_WIDGET (button));
+}
+
+
+
+static void
+show_settings_dialog ()
+{
+ g_debug ("show settings dialog");
+}
+
+static void
+refresh_rate_toggled (GtkCheckMenuItem *mi, XtmSettings *settings)
+{
+ guint refresh_rate = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (mi), "refresh-rate"));
+ g_object_set (settings, "refresh-rate", refresh_rate, NULL);
+}
+
+static void
+menu_refresh_rate_append_item (GtkMenu *menu, gchar *title, guint refresh_rate, XtmSettings *settings)
+{
+ GtkWidget *mi;
+ guint cur_refresh_rate;
+
+ g_object_get (settings, "refresh-rate", &cur_refresh_rate, NULL);
+
+ if (cur_refresh_rate == refresh_rate)
+ {
+ mi = gtk_radio_menu_item_new_with_label (NULL, title);
+ gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi), TRUE);
+ }
+ else
+ {
+ mi = gtk_menu_item_new_with_label (title);
+ }
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
+ g_object_set_data (G_OBJECT (mi), "refresh-rate", GUINT_TO_POINTER (refresh_rate));
+ g_signal_connect (mi, "activate", G_CALLBACK (refresh_rate_toggled), settings);
+}
+
+static GtkWidget *
+build_refresh_rate_menu (XtmSettings *settings)
+{
+ GtkWidget *menu;
+
+ menu = gtk_menu_new ();
+
+ /* TRANSLATORS: The next values are in seconds or milliseconds */
+ menu_refresh_rate_append_item (GTK_MENU (menu), _("500ms"), 500, settings);
+ menu_refresh_rate_append_item (GTK_MENU (menu), _("750ms"), 750, settings);
+ menu_refresh_rate_append_item (GTK_MENU (menu), _("1s"), 1000, settings);
+ menu_refresh_rate_append_item (GTK_MENU (menu), _("2s"), 2000, settings);
+ menu_refresh_rate_append_item (GTK_MENU (menu), _("5s"), 5000, settings);
+ menu_refresh_rate_append_item (GTK_MENU (menu), _("10s"), 10000, settings);
+
+ return menu;
+}
+
+static void
+item_toggled (GtkCheckMenuItem *mi, XtmSettings *settings)
+{
+ gboolean active = gtk_check_menu_item_get_active (mi);
+ gchar *setting_name = g_object_get_data (G_OBJECT (mi), "setting-name");
+ g_object_set (settings, setting_name, active, NULL);
+}
+
+static void
+settings_notify (GObject *object, GParamSpec *pspec, GtkCheckMenuItem *mi)
+{
+ gboolean active;
+ g_object_get (object, pspec->name, &active, NULL);
+ gtk_check_menu_item_set_active (mi, active);
+}
+
+static void
+menu_append_item (GtkMenu *menu, gchar *title, gchar *setting_name, XtmSettings *settings)
+{
+ GtkWidget *mi;
+ gboolean active = FALSE;
+ gchar *notify_name;
+
+ g_object_get (settings, setting_name, &active, NULL);
+
+ mi = gtk_check_menu_item_new_with_label (title);
+ gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi), active);
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
+ g_object_set_data (G_OBJECT (mi), "setting-name", setting_name);
+ g_signal_connect (mi, "toggled", G_CALLBACK (item_toggled), settings);
+
+ notify_name = g_strdup_printf ("notify::%s", setting_name);
+ g_signal_connect (settings, notify_name, G_CALLBACK (settings_notify), mi);
+ g_free (notify_name);
+}
+
+static inline GtkWidget *
+construct_menu ()
+{
+ XtmSettings *settings = xtm_settings_get_default ();
+ GtkWidget *menu = gtk_menu_new ();
+ GtkWidget *refresh_rate_menu;
+ GtkWidget *mi;
+
+ menu_append_item (GTK_MENU (menu), _("Show all processes"), "show-all-processes", settings);
+ //menu_append_item (GTK_MENU (menu), _("More precision"), "more-precision", settings);
+ //menu_append_item (GTK_MENU (menu), _("Full command line"), "full-command-line", settings);
+ //menu_append_item (GTK_MENU (menu), _("Show status icon"), "show-status-icon", settings);
+
+ refresh_rate_menu = build_refresh_rate_menu (settings);
+ mi = gtk_menu_item_new_with_label (_("Refresh rate"));
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (mi), refresh_rate_menu);
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
+
+ mi = gtk_separator_menu_item_new ();
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
+
+ menu_append_item (GTK_MENU (menu), _("PID"), "column-pid", settings);
+ menu_append_item (GTK_MENU (menu), _("PPID"), "column-ppid", settings);
+ menu_append_item (GTK_MENU (menu), _("State"), "column-state", settings);
+ menu_append_item (GTK_MENU (menu), _("Virtual Bytes"), "column-vsz", settings);
+ menu_append_item (GTK_MENU (menu), _("Private Bytes"), "column-rss", settings);
+ menu_append_item (GTK_MENU (menu), _("UID"), "column-uid", settings);
+ menu_append_item (GTK_MENU (menu), _("CPU"), "column-cpu", settings);
+ menu_append_item (GTK_MENU (menu), _("Priority"), "column-priority", settings);
+
+ gtk_widget_show_all (menu);
+
+ return menu;
+}
+
+
+
+GtkWidget *
+xtm_settings_tool_button_new (void)
+{
+ return g_object_new (XTM_TYPE_SETTINGS_TOOL_BUTTON, NULL);
+}
+
diff --git a/src/settings-tool-button.h b/src/settings-tool-button.h
new file mode 100644
index 0000000..0b18bed
--- /dev/null
+++ b/src/settings-tool-button.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2010 Mike Massonnet, <mmassonnet at 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.
+ */
+
+#ifndef SETTINGS_TOOL_BUTTON_H
+#define SETTINGS_TOOL_BUTTON_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+#define XTM_TYPE_SETTINGS_TOOL_BUTTON (xtm_settings_tool_button_get_type ())
+#define XTM_SETTINGS_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), XTM_TYPE_SETTINGS_TOOL_BUTTON, XtmSettingsToolButton))
+#define XTM_SETTINGS_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), XTM_TYPE_SETTINGS_TOOL_BUTTON, XtmSettingsToolButtonClass))
+#define XTM_IS_SETTINGS_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), XTM_TYPE_SETTINGS_TOOL_BUTTON))
+#define XTM_IS_SETTINGS_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), XTM_TYPE_SETTINGS_TOOL_BUTTON))
+#define XTM_SETTINGS_TOOL_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), XTM_TYPE_SETTINGS_TOOL_BUTTON, XtmSettingsToolButtonClass))
+
+typedef struct _XtmSettingsToolButton XtmSettingsToolButton;
+
+GType xtm_settings_tool_button_get_type (void);
+GtkWidget * xtm_settings_tool_button_new ();
+
+#endif /* !SETTINGS_TOOL_BUTTON_H */
More information about the Xfce4-commits
mailing list