[Xfce4-commits] [panel-plugins/xfce4-pulseaudio-plugin] 03/04: Added a menu

noreply at xfce.org noreply at xfce.org
Wed Mar 4 03:40:02 CET 2015


This is an automated email from the git hooks/post-receive script.

andrzejr pushed a commit to branch master
in repository panel-plugins/xfce4-pulseaudio-plugin.

commit 98e08df1734ec7527514fc87898c07202af5ab01
Author: Andrzej <ndrwrdck at gmail.com>
Date:   Wed Mar 4 02:35:34 2015 +0000

    Added a menu
---
 panel-plugin/Makefile.am         |    2 +
 panel-plugin/pulseaudio-button.c |   69 ++++------
 panel-plugin/pulseaudio-button.h |    3 +-
 panel-plugin/pulseaudio-menu.c   |  263 ++++++++++++++++++++++++++++++++++++++
 panel-plugin/pulseaudio-menu.h   |   47 +++++++
 panel-plugin/pulseaudio-plugin.c |    3 +-
 panel-plugin/scalemenuitem.h     |    4 +-
 7 files changed, 345 insertions(+), 46 deletions(-)

diff --git a/panel-plugin/Makefile.am b/panel-plugin/Makefile.am
index b4fc98b..1a540d3 100644
--- a/panel-plugin/Makefile.am
+++ b/panel-plugin/Makefile.am
@@ -33,6 +33,8 @@ libpulseaudio_plugin_la_SOURCES = \
 	pulseaudio-plugin.h \
 	pulseaudio-dialog.c \
 	pulseaudio-dialog.h \
+	pulseaudio-menu.c \
+	pulseaudio-menu.h \
 	scalemenuitem.c \
 	scalemenuitem.h
 
diff --git a/panel-plugin/pulseaudio-button.c b/panel-plugin/pulseaudio-button.c
index 03ab209..89225c5 100644
--- a/panel-plugin/pulseaudio-button.c
+++ b/panel-plugin/pulseaudio-button.c
@@ -41,6 +41,7 @@
 
 #include "pulseaudio-plugin.h"
 #include "pulseaudio-config.h"
+#include "pulseaudio-menu.h"
 #include "pulseaudio-button.h"
 
 #define V_MUTED  0
@@ -63,12 +64,10 @@ static const char *icons[] = {
 static void                 pulseaudio_button_finalize        (GObject            *object);
 static gboolean             pulseaudio_button_button_press    (GtkWidget          *widget,
                                                                GdkEventButton     *event);
-static gboolean             pulseaudio_button_button_release  (GtkWidget          *widget,
-                                                               GdkEventButton     *event);
 static gboolean             pulseaudio_button_scroll_event    (GtkWidget          *widget,
                                                                GdkEventScroll     *event);
 static void                 pulseaudio_button_menu_deactivate (PulseaudioButton   *button,
-                                                               GtkMenu            *menu);
+                                                               GtkMenuShell       *menu);
 static void                 pulseaudio_button_update_icons    (PulseaudioButton   *button);
 static void                 pulseaudio_button_update          (PulseaudioButton   *button,
                                                                gboolean            force_update);
@@ -78,6 +77,7 @@ struct _PulseaudioButton
 {
   GtkToggleButton       __parent__;
 
+  PulseaudioPlugin     *plugin;
   PulseaudioConfig     *config;
   PulseaudioVolume     *volume;
 
@@ -91,7 +91,7 @@ struct _PulseaudioButton
   guint                 pixbuf_idx;
   GdkPixbuf           **pixbufs;
 
-  GtkMenu              *menu;
+  GtkWidget            *menu;
 
   gulong                volume_changed_id;
   gulong                deactivate_id;
@@ -118,7 +118,6 @@ pulseaudio_button_class_init (PulseaudioButtonClass *klass)
 
   gtkwidget_class = GTK_WIDGET_CLASS (klass);
   gtkwidget_class->button_press_event   = pulseaudio_button_button_press;
-  gtkwidget_class->button_release_event = pulseaudio_button_button_release;
   gtkwidget_class->scroll_event         = pulseaudio_button_scroll_event;
 }
 
@@ -149,6 +148,7 @@ pulseaudio_button_init (PulseaudioButton *button)
   /* Intercept scroll events */
   gtk_widget_add_events (GTK_WIDGET (button), GDK_SCROLL_MASK);
 
+  button->plugin = NULL;
   button->config = NULL;
   button->volume = NULL;
 
@@ -185,8 +185,8 @@ pulseaudio_button_finalize (GObject *object)
 
   if (button->menu != NULL)
     {
-      gtk_menu_detach (button->menu);
-      gtk_menu_popdown (button->menu);
+      gtk_menu_detach (GTK_MENU (button->menu));
+      gtk_menu_popdown (GTK_MENU (button->menu));
       button->menu = NULL;
     }
 
@@ -203,43 +203,24 @@ pulseaudio_button_button_press (GtkWidget      *widget,
   if(event->button == 1) /* left button */
     {
       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
+      if (button->menu == NULL)
+          button->menu = pulseaudio_menu_new (button->volume, button->config, widget);
+      if (button->deactivate_id == 0)
+          button->deactivate_id = g_signal_connect_swapped
+            (GTK_MENU_SHELL (button->menu), "deactivate",
+             G_CALLBACK (pulseaudio_button_menu_deactivate), button);
+
+      gtk_menu_popup (GTK_MENU (button->menu),
+                      NULL, NULL,
+                      xfce_panel_plugin_position_menu, button->plugin,
+                      //NULL, NULL,
+                      0,
+                      gtk_get_current_event_time ());
       return TRUE;
     }
 
   if(event->button == 2) /* middle button */
     {
-      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
-      return TRUE;
-    }
-
-  return FALSE;
-}
-
-
-static gboolean
-pulseaudio_button_button_release (GtkWidget      *widget,
-                                  GdkEventButton *event)
-{
-  PulseaudioButton *button = PULSEAUDIO_BUTTON (widget);
-  GError *error = NULL;
-
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
-
-  if(event->button == 1) /* left button */
-    {
-      if (!xfce_spawn_command_line_on_screen (gtk_widget_get_screen (widget),
-                                              pulseaudio_config_get_mixer_command (button->config),
-                                              FALSE, FALSE, &error))
-        {
-          xfce_dialog_show_error (NULL, error, _("Failed to execute command \"%s\"."),
-                                  pulseaudio_config_get_mixer_command (button->config));
-          g_error_free (error);
-        }
-      return TRUE;
-    }
-
-  if (event->button == 2) /* middle button */
-    {
       pulseaudio_volume_toggle_muted (button->volume);
       return TRUE;
     }
@@ -267,10 +248,10 @@ pulseaudio_button_scroll_event (GtkWidget *widget, GdkEventScroll *event)
 
 static void
 pulseaudio_button_menu_deactivate (PulseaudioButton *button,
-                                   GtkMenu          *menu)
+                                   GtkMenuShell     *menu)
 {
   g_return_if_fail (IS_PULSEAUDIO_BUTTON (button));
-  g_return_if_fail (GTK_IS_MENU (menu));
+  g_return_if_fail (GTK_IS_MENU_SHELL (menu));
 
   if (button->deactivate_id)
     {
@@ -375,15 +356,19 @@ pulseaudio_button_volume_changed (PulseaudioButton  *button,
 
 
 GtkWidget *
-pulseaudio_button_new (PulseaudioConfig *config,
+pulseaudio_button_new (PulseaudioPlugin *plugin,
+                       PulseaudioConfig *config,
                        PulseaudioVolume *volume)
 {
   PulseaudioButton *button;
 
+  g_return_val_if_fail (IS_PULSEAUDIO_PLUGIN (plugin), NULL);
+  g_return_val_if_fail (IS_PULSEAUDIO_CONFIG (config), NULL);
   g_return_val_if_fail (IS_PULSEAUDIO_VOLUME (volume), NULL);
 
   button = g_object_new (TYPE_PULSEAUDIO_BUTTON, NULL);
 
+  button->plugin = plugin;
   button->volume = volume;
   button->config = config;
   button->volume_changed_id =
diff --git a/panel-plugin/pulseaudio-button.h b/panel-plugin/pulseaudio-button.h
index d43268c..5e5b0d5 100644
--- a/panel-plugin/pulseaudio-button.h
+++ b/panel-plugin/pulseaudio-button.h
@@ -38,7 +38,8 @@ GType pulseaudio_button_get_type (void);
 typedef struct          _PulseaudioButton              PulseaudioButton;
 typedef struct          _PulseaudioButtonClass         PulseaudioButtonClass;
 
-GtkWidget              *pulseaudio_button_new         (PulseaudioConfig *config,
+GtkWidget              *pulseaudio_button_new         (PulseaudioPlugin *plugin,
+                                                       PulseaudioConfig *config,
                                                        PulseaudioVolume *volume);
 
 void                    pulseaudio_button_set_size    (PulseaudioButton *button,
diff --git a/panel-plugin/pulseaudio-menu.c b/panel-plugin/pulseaudio-menu.c
new file mode 100644
index 0000000..196e930
--- /dev/null
+++ b/panel-plugin/pulseaudio-menu.c
@@ -0,0 +1,263 @@
+/*  Copyright (c) 2015 Andrzej <ndrwrdck at gmail.com>
+ *
+ *  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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+
+
+/*
+ *  This file implements a plugin menu
+ *
+ */
+
+
+
+#include <glib.h>
+#include <gtk/gtk.h>
+#include <libxfce4panel/libxfce4panel.h>
+#include <libxfce4util/libxfce4util.h>
+
+#include "pulseaudio-menu.h"
+#include "scalemenuitem.h"
+
+
+struct _PulseaudioMenu
+{
+  GtkMenu             __parent__;
+
+  PulseaudioVolume     *volume;
+  PulseaudioConfig     *config;
+  GtkWidget            *button;
+  GtkWidget            *range_output;
+  GtkWidget            *mute_output_item;
+
+  gulong                volume_changed_id;
+};
+
+struct _PulseaudioMenuClass
+{
+  GtkMenuClass        __parent__;
+};
+
+
+static void             pulseaudio_menu_finalize         (GObject       *object);
+
+
+G_DEFINE_TYPE (PulseaudioMenu, pulseaudio_menu, GTK_TYPE_MENU)
+
+static void
+pulseaudio_menu_class_init (PulseaudioMenuClass *klass)
+{
+  GObjectClass      *gobject_class;
+  GtkWidgetClass    *gtkwidget_class;
+
+  gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->finalize = pulseaudio_menu_finalize;
+
+  gtkwidget_class = GTK_WIDGET_CLASS (klass);
+}
+
+
+
+static void
+pulseaudio_menu_init (PulseaudioMenu *menu)
+{
+  menu->volume                         = NULL;
+  menu->button                         = NULL;
+  menu->range_output                   = NULL;
+  menu->mute_output_item               = NULL;
+}
+
+
+static void
+pulseaudio_menu_finalize (GObject *object)
+{
+  PulseaudioMenu *menu;
+
+  menu = PULSEAUDIO_MENU (object);
+
+  G_OBJECT_CLASS (pulseaudio_menu_parent_class)->finalize (object);
+}
+
+
+static void
+pulseaudio_menu_output_range_scroll (GtkWidget        *widget,
+                                     GdkEvent         *event,
+                                     PulseaudioMenu   *menu)
+{
+  gdouble         new_volume;
+  gdouble         volume;
+  gdouble         volume_step;
+  GdkEventScroll *scroll_event;
+
+  g_return_if_fail (IS_PULSEAUDIO_MENU (menu));
+  volume =  pulseaudio_volume_get_volume (menu->volume);
+  volume_step = pulseaudio_config_get_volume_step (menu->config) / 100.0;
+
+  scroll_event = (GdkEventScroll*)event;
+
+  new_volume = MIN (MAX (volume + (1.0 - 2.0 * scroll_event->direction) * volume_step, 0.0), 1.0);
+  pulseaudio_volume_set_volume (menu->volume, new_volume);
+  //printf ("scroll %d %g %g\n", scroll_event->direction, volume, new_volume);
+}
+
+static void
+pulseaudio_menu_output_range_value_changed (PulseaudioMenu   *menu,
+                                            GtkWidget        *widget)
+{
+  gdouble  new_volume;
+
+  g_return_if_fail (IS_PULSEAUDIO_MENU (menu));
+
+  new_volume = gtk_range_get_value (GTK_RANGE (menu->range_output)) / 100.0;
+  pulseaudio_volume_set_volume (menu->volume, new_volume);
+  //printf ("range value changed %g\n", new_volume);
+}
+
+
+static void
+pulseaudio_menu_mute_output_item_toggled (PulseaudioMenu   *menu,
+                                          GtkCheckMenuItem *menu_item)
+{
+  g_return_if_fail (IS_PULSEAUDIO_MENU (menu));
+
+  pulseaudio_volume_set_muted (menu->volume, gtk_check_menu_item_get_active (menu_item));
+}
+
+
+
+static void
+pulseaudio_menu_run_audio_mixer (PulseaudioMenu   *menu,
+                                 GtkCheckMenuItem *menu_item)
+{
+  GError *error = NULL;
+
+  g_return_if_fail (IS_PULSEAUDIO_MENU (menu));
+
+  if (!xfce_spawn_command_line_on_screen (gtk_widget_get_screen (GTK_WIDGET (menu)),
+                                          pulseaudio_config_get_mixer_command (menu->config),
+                                          FALSE, FALSE, &error))
+    {
+      xfce_dialog_show_error (NULL, error, _("Failed to execute command \"%s\"."),
+                              pulseaudio_config_get_mixer_command (menu->config));
+      g_error_free (error);
+    }
+}
+
+
+
+static void
+pulseaudio_menu_volume_changed (PulseaudioMenu   *menu,
+                                PulseaudioVolume *volume)
+{
+  g_return_if_fail (IS_PULSEAUDIO_MENU (menu));
+
+  g_signal_handlers_block_by_func (G_OBJECT (menu->mute_output_item),
+                                   pulseaudio_menu_mute_output_item_toggled,
+                                   menu);
+  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu->mute_output_item),
+                                  pulseaudio_volume_get_muted (volume));
+  g_signal_handlers_unblock_by_func (G_OBJECT (menu->mute_output_item),
+                                     pulseaudio_menu_mute_output_item_toggled,
+                                     menu);
+
+  gtk_range_set_value (GTK_RANGE (menu->range_output), pulseaudio_volume_get_volume (menu->volume) * 100.0);
+}
+
+
+
+GtkWidget *
+pulseaudio_menu_new (PulseaudioVolume *volume,
+                     PulseaudioConfig *config,
+                     GtkWidget        *widget)
+{
+  PulseaudioMenu *menu;
+  GdkScreen      *gscreen;
+  GtkWidget      *mi;
+  GtkWidget      *img = NULL;
+  GdkPixbuf      *pix;
+
+  g_return_val_if_fail (IS_PULSEAUDIO_VOLUME (volume), NULL);
+  g_return_val_if_fail (IS_PULSEAUDIO_CONFIG (config), NULL);
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+
+  if (gtk_widget_has_screen (widget))
+    gscreen = gtk_widget_get_screen (widget);
+  else
+    gscreen = gdk_display_get_default_screen (gdk_display_get_default ());
+
+
+  menu = g_object_new (TYPE_PULSEAUDIO_MENU, NULL);
+  gtk_menu_set_screen (GTK_MENU (menu), gscreen);
+
+  menu->volume = volume;
+  menu->config = config;
+  menu->button = widget;
+  menu->volume_changed_id =
+    g_signal_connect_swapped (G_OBJECT (menu->volume), "volume-changed",
+                              G_CALLBACK (pulseaudio_menu_volume_changed), menu);
+
+  /* output volume slider */
+  mi = scale_menu_item_new_with_range (0.0, 100.0, 1.0);
+
+  /* attempt to load and display the brightness icon */
+  pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+                                  "audio-volume-high",
+                                  32,
+                                  GTK_ICON_LOOKUP_GENERIC_FALLBACK,
+                                  NULL);
+  if (pix)
+    {
+      img = gtk_image_new_from_pixbuf (pix);
+      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mi), img);
+    }
+
+  scale_menu_item_set_description_label (SCALE_MENU_ITEM (mi), _("<b>Audio output volume</b>"));
+
+  /* range slider */
+  menu->range_output = scale_menu_item_get_scale (SCALE_MENU_ITEM (mi));
+
+  /* update the slider to the current brightness level */
+  //gtk_range_set_value (GTK_RANGE (menu->range_output), current_level);
+
+  g_signal_connect_swapped (mi, "value-changed", G_CALLBACK (pulseaudio_menu_output_range_value_changed), menu);
+  g_signal_connect (mi, "scroll-event", G_CALLBACK (pulseaudio_menu_output_range_scroll), menu);
+
+  gtk_widget_show_all (mi);
+  gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
+
+  menu->mute_output_item = gtk_check_menu_item_new_with_mnemonic (_("_Mute audio output"));
+  gtk_widget_show_all (menu->mute_output_item);
+  gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu->mute_output_item);
+  g_signal_connect_swapped (G_OBJECT (menu->mute_output_item), "toggled", G_CALLBACK (pulseaudio_menu_mute_output_item_toggled), menu);
+
+  /* separator */
+  mi = gtk_separator_menu_item_new ();
+  gtk_widget_show (mi);
+  gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
+
+  /* Audio mixers */
+  mi = gtk_menu_item_new_with_mnemonic (_("_Run audio mixer..."));
+  gtk_widget_show (mi);
+  gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
+  g_signal_connect_swapped (G_OBJECT (mi), "activate", G_CALLBACK (pulseaudio_menu_run_audio_mixer), menu);
+
+  pulseaudio_menu_volume_changed (menu, menu->volume);
+
+
+  return GTK_WIDGET (menu);
+}
+
+
diff --git a/panel-plugin/pulseaudio-menu.h b/panel-plugin/pulseaudio-menu.h
new file mode 100644
index 0000000..c57c5aa
--- /dev/null
+++ b/panel-plugin/pulseaudio-menu.h
@@ -0,0 +1,47 @@
+/*  Copyright (c) 2015 Andrzej <ndrwrdck at gmail.com>
+ *
+ *  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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __PULSEAUDIO_MENU_H__
+#define __PULSEAUDIO_MENU_H__
+
+#include <glib.h>
+#include <gtk/gtk.h>
+
+#include "pulseaudio-volume.h"
+#include "pulseaudio-config.h"
+
+G_BEGIN_DECLS
+
+GType pulseaudio_menu_get_type (void);
+
+#define TYPE_PULSEAUDIO_MENU             (pulseaudio_menu_get_type())
+#define PULSEAUDIO_MENU(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_PULSEAUDIO_MENU, PulseaudioMenu))
+#define PULSEAUDIO_MENU_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),  TYPE_PULSEAUDIO_MENU, PulseaudioMenuClass))
+#define IS_PULSEAUDIO_MENU(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_PULSEAUDIO_MENU))
+#define IS_PULSEAUDIO_MENU_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),  TYPE_PULSEAUDIO_MENU))
+#define PULSEAUDIO_MENU_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj),  TYPE_PULSEAUDIO_MENU, PulseaudioMenuClass))
+
+typedef struct          _PulseaudioMenu              PulseaudioMenu;
+typedef struct          _PulseaudioMenuClass         PulseaudioMenuClass;
+
+GtkWidget              *pulseaudio_menu_new         (PulseaudioVolume *volume,
+                                                     PulseaudioConfig *config,
+                                                     GtkWidget        *widget);
+
+G_END_DECLS
+
+#endif /* !__PULSEAUDIO_MENU_H__ */
diff --git a/panel-plugin/pulseaudio-plugin.c b/panel-plugin/pulseaudio-plugin.c
index cad20cd..6859732 100644
--- a/panel-plugin/pulseaudio-plugin.c
+++ b/panel-plugin/pulseaudio-plugin.c
@@ -389,7 +389,8 @@ pulseaudio_plugin_construct (XfcePanelPlugin *plugin)
   gtk_widget_show (mixer_menu_item);
 
   /* instantiate a button box */
-  pulseaudio_plugin->button = pulseaudio_button_new (pulseaudio_plugin->config,
+  pulseaudio_plugin->button = pulseaudio_button_new (pulseaudio_plugin,
+                                                     pulseaudio_plugin->config,
                                                      pulseaudio_plugin->volume);
   gtk_container_add (GTK_CONTAINER (plugin), GTK_WIDGET (pulseaudio_plugin->button));
   gtk_widget_show (GTK_WIDGET (pulseaudio_plugin->button));
diff --git a/panel-plugin/scalemenuitem.h b/panel-plugin/scalemenuitem.h
index 0a4fa55..772419a 100644
--- a/panel-plugin/scalemenuitem.h
+++ b/panel-plugin/scalemenuitem.h
@@ -46,14 +46,14 @@ typedef struct _ScaleMenuItemPrivate ScaleMenuItemPrivate;
 
 struct _ScaleMenuItem
 {
-  GtkMenuItem parent_instance;
+  GtkImageMenuItem parent_instance;
 
   ScaleMenuItemPrivate *priv;
 };
 
 struct _ScaleMenuItemClass
 {
-  GtkMenuItemClass parent_class;
+  GtkImageMenuItemClass parent_class;
 };
 
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list