From 7f20e9df87a70a374f273b092bfcd096b661e86e Mon Sep 17 00:00:00 2001 From: Peter de Ridder Date: Sat, 3 Jan 2015 15:50:43 +0100 Subject: [PATCH] Added ThunarxActionMenu to ease menu providers with submenus --- thunarx/Makefile.am | 2 + thunarx/thunarx-action-menu.c | 156 ++++++++++++++++++++++++++++++++++++++++++ thunarx/thunarx-action-menu.h | 64 +++++++++++++++++ thunarx/thunarx.h | 1 + 4 files changed, 223 insertions(+) create mode 100644 thunarx/thunarx-action-menu.c create mode 100644 thunarx/thunarx-action-menu.h diff --git a/thunarx/Makefile.am b/thunarx/Makefile.am index 1e522bf..9fcf868 100644 --- a/thunarx/Makefile.am +++ b/thunarx/Makefile.am @@ -9,6 +9,7 @@ AM_CPPFLAGS = \ libthunarx_headers = \ thunarx.h \ + thunarx-action-menu.h \ thunarx-config.h \ thunarx-file-info.h \ thunarx-menu-provider.h \ @@ -31,6 +32,7 @@ lib_LTLIBRARIES = \ libthunarx_2_la_SOURCES = \ $(libthunarx_headers) \ + thunarx-action-menu.c \ thunarx-config.c \ thunarx-file-info.c \ thunarx-menu-provider.c \ diff --git a/thunarx/thunarx-action-menu.c b/thunarx/thunarx-action-menu.c new file mode 100644 index 0000000..41137c6 --- /dev/null +++ b/thunarx/thunarx-action-menu.c @@ -0,0 +1,156 @@ +/*- + * Copyright (C) 2015 Peter de Ridder + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include +#include + + + +#define THUNARX_ACTION_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), THUNARX_TYPE_ACTION_MENU, ThunarxActionMenuPrivate)) + + + +static GtkWidget *thunarx_action_menu_create_menu_item (GtkAction *action); +static GtkWidget *thunarx_action_menu_create_menu (GtkAction *action); +static void thunarx_action_menu_finalize (GObject *object); + + + +struct _ThunarxActionMenuPrivate +{ + GList *menu; +}; + + + +G_DEFINE_TYPE (ThunarxActionMenu, thunarx_action_menu, GTK_TYPE_ACTION) + + + +static void +thunarx_action_menu_class_init (ThunarxActionMenuClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + GtkActionClass *gtkaction_class = GTK_ACTION_CLASS (klass); + + /* add our private data to the class type */ + g_type_class_add_private (klass, sizeof (ThunarxActionMenuPrivate)); + + gobject_class->finalize = thunarx_action_menu_finalize; + + gtkaction_class->create_menu_item = thunarx_action_menu_create_menu_item; + gtkaction_class->create_menu = thunarx_action_menu_create_menu; +} + + + +static void +thunarx_action_menu_init (ThunarxActionMenu *self) +{ + self->priv = THUNARX_ACTION_MENU_GET_PRIVATE (self); +} + + + +GtkAction * +thunarx_action_menu_new (const gchar *name, + const gchar *label, + const gchar *tooltip, + const gchar *stock_id, + GList *menu) +{ + GtkAction *action; + + g_return_val_if_fail(name, NULL); + g_return_val_if_fail(label, NULL); + + action = g_object_new (THUNARX_TYPE_ACTION_MENU, + "name", name, + "label", label, + "tooltip", tooltip, + "stock-id", stock_id, + NULL); + THUNARX_ACTION_MENU (action)->priv->menu = menu; + return action; +} + + + +static void +thunarx_action_menu_finalize (GObject *object) +{ + GList *iter; + + for (iter = THUNARX_ACTION_MENU (object)->priv->menu; iter; iter = iter->next) + { + g_object_unref (iter->data); + } + g_list_free (THUNARX_ACTION_MENU (object)->priv->menu); + THUNARX_ACTION_MENU (object)->priv->menu = NULL; + + G_OBJECT_CLASS (thunarx_action_menu_parent_class)->finalize (object); +} + + + +static GtkWidget * +thunarx_action_menu_create_menu_item (GtkAction *action) +{ + GtkWidget *item; + GtkWidget *menu; + + item = GTK_ACTION_CLASS(thunarx_action_menu_parent_class)->create_menu_item (action); + + menu = gtk_action_create_menu (action); + + gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu); + + return item; +} + + + +static GtkWidget * +thunarx_action_menu_create_menu (GtkAction *action) +{ + GtkWidget *menu; + GList *iter; + + menu = gtk_menu_new (); + + for (iter = THUNARX_ACTION_MENU (action)->priv->menu; iter; iter = iter->next) + { + GtkAction *subaction; + GtkWidget *subitem; + + subaction = GTK_ACTION (iter->data); + subitem = gtk_action_create_menu_item (subaction); + + gtk_menu_shell_append (GTK_MENU_SHELL (menu), subitem); + gtk_widget_show(subitem); + } + + return menu; +} + diff --git a/thunarx/thunarx-action-menu.h b/thunarx/thunarx-action-menu.h new file mode 100644 index 0000000..bcf746c --- /dev/null +++ b/thunarx/thunarx-action-menu.h @@ -0,0 +1,64 @@ +/*- + * Copyright (C) 2015 Peter de Ridder + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if !defined(THUNARX_INSIDE_THUNARX_H) && !defined(THUNARX_COMPILATION) +#error "Only can be included directly, this file may disappear or change contents" +#endif + +#ifndef __THUNARX_ACTION_MENU_H__ +#define __THUNARX_ACTION_MENU_H__ + +#include + +G_BEGIN_DECLS; + +typedef struct _ThunarxActionMenuPrivate ThunarxActionMenuPrivate; +typedef struct _ThunarxActionMenuClass ThunarxActionMenuClass; +typedef struct _ThunarxActionMenu ThunarxActionMenu; + +#define THUNARX_TYPE_ACTION_MENU (thunarx_action_menu_get_type ()) +#define THUNARX_ACTION_MENU(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNARX_TYPE_ACTION_MENU, ThunarxActionMenu)) +#define THUNARX_ACTION_MENU_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNARX_TYPE_ACTION_MENU, ThunarAxctionMenuClass)) +#define THUNARX_IS_ACTION_MENU(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNARX_TYPE_ACTION_MENU)) +#define THUNARX_IS_ACTION_MENU_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNARX_TYPE_ACTION_MENU)) +#define THUNARX_ACTION_MENU_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNARX_TYPE_ACTION_MENU, ThunarxActionMenuClass)) + +struct _ThunarxActionMenuClass +{ + GtkActionClass __parent__; +}; + +struct _ThunarxActionMenu +{ + GtkAction __parent__; + + /*< private >*/ + ThunarxActionMenuPrivate *priv; +}; + +GType thunarx_action_menu_get_type (void) G_GNUC_CONST; + +GtkAction *thunarx_action_menu_new (const gchar *name, + const gchar *label, + const gchar *tooltip, + const gchar *stock_id, + GList *menu) G_GNUC_MALLOC; + +G_END_DECLS; + +#endif /* !__THUNARX_ACTION_MENU_H__ */ diff --git a/thunarx/thunarx.h b/thunarx/thunarx.h index 73d86b9..df2ff6a 100644 --- a/thunarx/thunarx.h +++ b/thunarx/thunarx.h @@ -24,6 +24,7 @@ #define THUNARX_INSIDE_THUNARX_H #include +#include #include #include #include -- 2.2.1