diff options
author | Andre Miranda <andreldm@xfce.org> | 2017-11-19 15:02:23 -0300 |
---|---|---|
committer | Andre Miranda <andreldm@xfce.org> | 2017-11-19 15:02:23 -0300 |
commit | fb32b8e48e49224ef94675c1a3b84c3d6f1c3cbe (patch) | |
tree | aeac87ed3777153f601620c1427bdf655f431ea9 /thunarx | |
parent | 0cb9fb73930ee91f75d9ba1d1b2ce8f915d7e496 (diff) | |
download | thunar-fb32b8e48e49224ef94675c1a3b84c3d6f1c3cbe.tar.gz |
Introduce ThunarxMenu
Also refactoring some menu related code into thunar-menu-util.c
Diffstat (limited to 'thunarx')
-rw-r--r-- | thunarx/Makefile.am | 2 | ||||
-rw-r--r-- | thunarx/thunarx-menu-item.c | 71 | ||||
-rw-r--r-- | thunarx/thunarx-menu-item.h | 42 | ||||
-rw-r--r-- | thunarx/thunarx-menu.c | 144 | ||||
-rw-r--r-- | thunarx/thunarx-menu.h | 115 | ||||
-rw-r--r-- | thunarx/thunarx.h | 2 | ||||
-rw-r--r-- | thunarx/thunarx.symbols | 8 |
7 files changed, 335 insertions, 49 deletions
diff --git a/thunarx/Makefile.am b/thunarx/Makefile.am index abea8793..82161ec8 100644 --- a/thunarx/Makefile.am +++ b/thunarx/Makefile.am @@ -9,6 +9,7 @@ libthunarx_headers = \ thunarx.h \ thunarx-config.h \ thunarx-file-info.h \ + thunarx-menu.h \ thunarx-menu-item.h \ thunarx-menu-provider.h \ thunarx-preferences-provider.h \ @@ -32,6 +33,7 @@ libthunarx_3_la_SOURCES = \ $(libthunarx_headers) \ thunarx-config.c \ thunarx-file-info.c \ + thunarx-menu.c \ thunarx-menu-item.c \ thunarx-menu-provider.c \ thunarx-preferences-provider.c \ diff --git a/thunarx/thunarx-menu-item.c b/thunarx/thunarx-menu-item.c index 3a48d6fe..9f4f608d 100644 --- a/thunarx/thunarx-menu-item.c +++ b/thunarx/thunarx-menu-item.c @@ -23,6 +23,7 @@ #include <thunarx/thunarx-private.h> #include <thunarx/thunarx-menu-item.h> +#include <thunarx/thunarx-menu.h> @@ -77,12 +78,13 @@ static void thunarx_menu_item_finalize (GObject *object); struct _ThunarxMenuItemPrivate { - gchar *name; - gchar *label; - gchar *tooltip; - gchar *icon; - gboolean sensitive; - gboolean priority; + gchar *name; + gchar *label; + gchar *tooltip; + gchar *icon; + gboolean sensitive; + gboolean priority; + ThunarxMenu *menu; }; @@ -146,7 +148,6 @@ thunarx_menu_item_class_init (ThunarxMenuItemClass *klass) "Name of the icon to display in the menu item", NULL, G_PARAM_READWRITE)); - g_object_class_install_property (gobject_class, PROP_SENSITIVE, g_param_spec_boolean ("sensitive", @@ -161,6 +162,13 @@ thunarx_menu_item_class_init (ThunarxMenuItemClass *klass) "Show priority text in toolbars", TRUE, G_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, + PROP_MENU, + g_param_spec_object ("menu", + "Menu", + "The menu belonging to this item. May be null.", + THUNARX_TYPE_MENU, + G_PARAM_READWRITE)); } @@ -171,6 +179,7 @@ thunarx_menu_item_init (ThunarxMenuItem *item) item->priv = THUNARX_MENU_ITEM_GET_PRIVATE (item); item->priv->sensitive = TRUE; item->priv->priority = FALSE; + item->priv->menu = NULL; } @@ -209,6 +218,10 @@ thunarx_menu_item_get_property (GObject *object, g_value_set_boolean (value, item->priv->priority); break; + case PROP_MENU: + g_value_set_object (value, item->priv->menu); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); break; @@ -261,6 +274,13 @@ thunarx_menu_item_set_property (GObject *object, g_object_notify (object, "priority"); break; + case PROP_MENU: + if (item->priv->menu) + g_object_unref (item->priv->menu); + item->priv->menu = g_object_ref (g_value_get_object (value)); + g_object_notify (object, "menu"); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); break; @@ -279,6 +299,9 @@ thunarx_menu_item_finalize (GObject *object) g_free (item->priv->tooltip); g_free (item->priv->icon); + if (item->priv->menu) + g_object_unref (item->priv->menu); + (*G_OBJECT_CLASS (thunarx_menu_item_parent_class)->finalize) (object); } @@ -357,3 +380,37 @@ thunarx_menu_item_set_sensitive (ThunarxMenuItem *item, g_return_if_fail (THUNARX_IS_MENU_ITEM (item)); item->priv->sensitive = sensitive; } + + + +/** + * thunarx_menu_item_set_menu: + * @item: pointer to a #ThunarxMenuItem instance + * @menu: pointer to a #ThunarxMenu instance + * + * Attaches @menu to menu item. + */ +void +thunarx_menu_item_set_menu (ThunarxMenuItem *item, + ThunarxMenu *menu) +{ + g_return_if_fail (THUNARX_IS_MENU_ITEM (item)); + g_return_if_fail (THUNARX_IS_MENU (menu)); + + g_object_set (item, "menu", THUNARX_MENU (menu), NULL); +} + + + +/** + * thunarx_menu_item_list_free: + * @items: (element-type ThunarxMenuItem): a list of #ThunarxMenuItem + */ +void +thunarx_menu_item_list_free (GList *items) +{ + g_return_if_fail (items != NULL); + + g_list_foreach (items, (GFunc)g_object_unref, NULL); + g_list_free (items); +} diff --git a/thunarx/thunarx-menu-item.h b/thunarx/thunarx-menu-item.h index 3eaed6b4..00550906 100644 --- a/thunarx/thunarx-menu-item.h +++ b/thunarx/thunarx-menu-item.h @@ -23,46 +23,6 @@ #ifndef __THUNARX_MENU_ITEM_H__ #define __THUNARX_MENU_ITEM_H__ -#include <glib-object.h> - -G_BEGIN_DECLS; - -typedef struct _ThunarxMenuItemPrivate ThunarxMenuItemPrivate; -typedef struct _ThunarxMenuItemClass ThunarxMenuItemClass; -typedef struct _ThunarxMenuItem ThunarxMenuItem; - -#define THUNARX_TYPE_MENU_ITEM (thunarx_menu_item_get_type ()) -#define THUNARX_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNARX_TYPE_MENU_ITEM, ThunarxMenuItem)) -#define THUNARX_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNARX_TYPE_MENU_ITEM, ThunarxMenuItemClass)) -#define THUNARX_IS_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNARX_TYPE_MENU_ITEM)) -#define THUNARX_IS_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), THUNARX_TYPE_MENU_ITEM)) -#define THUNARX_MENU_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), THUNARX_TYPE_MENU_ITEM)) - -struct _ThunarxMenuItemClass { - GObjectClass __parent__; - void (*activate) (ThunarxMenuItem *item); -}; - -struct _ThunarxMenuItem { - GObject __parent__; - - /*< private >*/ - ThunarxMenuItemPrivate *priv; -}; - -GType thunarx_menu_item_get_type (void) G_GNUC_CONST; - -ThunarxMenuItem *thunarx_menu_item_new (const gchar *name, - const gchar *label, - const gchar *tip, - const gchar *icon) G_GNUC_MALLOC; - -void thunarx_menu_item_activate (ThunarxMenuItem *item); - -gboolean thunarx_menu_item_get_sensitive (ThunarxMenuItem *item); -void thunarx_menu_item_set_sensitive (ThunarxMenuItem *item, - gboolean sensitive); - -G_END_DECLS; +#include "thunarx-menu.h" #endif /* !__THUNARX_MENU_ITEM_H__ */ diff --git a/thunarx/thunarx-menu.c b/thunarx/thunarx-menu.c new file mode 100644 index 00000000..97961b59 --- /dev/null +++ b/thunarx/thunarx-menu.c @@ -0,0 +1,144 @@ +/*- + * Copyright (c) 2017 Andre Miranda <andreldm@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. + * + * 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 <config.h> +#endif + +#include <glib/gi18n-lib.h> + +#include <thunarx/thunarx-private.h> +#include <thunarx/thunarx-menu-item.h> +#include <thunarx/thunarx-menu.h> + + + +#define THUNARX_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), THUNARX_TYPE_MENU, ThunarxMenuPrivate)) + +/** + * SECTION: thunarx-menu + * @short_description: The base class for submenus added to the context menus + * @title: ThunarxMenu + * @include: thunarx/thunarx.h + * + * The class for submenus that can be added to Thunar's context menus. Extensions + * can provide ThunarxMenu objects by attaching them to ThunarxMenuItem objects, + * using thunarx_menu_item_set_menu(). + */ + + + +static void thunarx_menu_finalize (GObject *object); + + + +struct _ThunarxMenuPrivate +{ + GList *items; +}; + + + +G_DEFINE_TYPE (ThunarxMenu, thunarx_menu, G_TYPE_OBJECT) + + + +static void +thunarx_menu_class_init (ThunarxMenuClass *klass) +{ + GObjectClass *gobject_class; + + /* add our private data to the class type */ + g_type_class_add_private (klass, sizeof (ThunarxMenuPrivate)); + + gobject_class = G_OBJECT_CLASS (klass); + gobject_class->finalize = thunarx_menu_finalize; +} + + + +static void +thunarx_menu_init (ThunarxMenu *menu) +{ + menu->priv = THUNARX_MENU_GET_PRIVATE (menu); + menu->priv->items = NULL; +} + + + +static void +thunarx_menu_finalize (GObject *object) +{ + ThunarxMenu *menu = THUNARX_MENU (object); + + if (menu->priv->items) + g_list_free (menu->priv->items); + + (*G_OBJECT_CLASS (thunarx_menu_parent_class)->finalize) (object); +} + + + +/** + * thunarx_menu_new: + * + * Creates a new menu that can be added to the toolbar or to a contextual menu. + * + * Returns: a newly created #ThunarxMenu + */ +ThunarxMenu * +thunarx_menu_new (void) +{ + return g_object_new (THUNARX_TYPE_MENU, NULL); +} + + + +/** + * thunarx_menu_append_item: + * @menu: a #ThunarxMenu + * @item: a #ThunarxMenuItem + */ +void +thunarx_menu_append_item (ThunarxMenu *menu, ThunarxMenuItem *item) +{ + g_return_if_fail (menu != NULL); + g_return_if_fail (item != NULL); + + menu->priv->items = g_list_append (menu->priv->items, g_object_ref (item)); +} + + + +/** + * thunarx_menu_get_items: + * @menu: a #ThunarxMenu + * + * Returns: (element-type ThunarxMenuItem) (transfer full): the provided #ThunarxMenuItem list + */ +GList * +thunarx_menu_get_items (ThunarxMenu *menu) +{ + GList *items; + + g_return_val_if_fail (menu != NULL, NULL); + + items = g_list_copy (menu->priv->items); + g_list_foreach (items, (GFunc) g_object_ref, NULL); + + return items; +} diff --git a/thunarx/thunarx-menu.h b/thunarx/thunarx-menu.h new file mode 100644 index 00000000..1f43f6e5 --- /dev/null +++ b/thunarx/thunarx-menu.h @@ -0,0 +1,115 @@ +/*- + * Copyright (c) 2017 Andre Miranda <andreldm@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. + * + * 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 <thunarx/thunarx.h> can be included directly, this file may disappear or change contents" +#endif + +#ifndef __THUNARX_MENU_H__ +#define __THUNARX_MENU_H__ + +#include <glib-object.h> + +G_BEGIN_DECLS; + +/* ThunarxMenu types */ +typedef struct _ThunarxMenuPrivate ThunarxMenuPrivate; +typedef struct _ThunarxMenuClass ThunarxMenuClass; +typedef struct _ThunarxMenu ThunarxMenu; + +/* ThunarxMenuItem types */ +typedef struct _ThunarxMenuItemPrivate ThunarxMenuItemPrivate; +typedef struct _ThunarxMenuItemClass ThunarxMenuItemClass; +typedef struct _ThunarxMenuItem ThunarxMenuItem; + + + + +/* ThunarxMenu defines */ +#define THUNARX_TYPE_MENU (thunarx_menu_get_type ()) +#define THUNARX_MENU(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNARX_TYPE_MENU, ThunarxMenu)) +#define THUNARX_MENU_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNARX_TYPE_MENU, ThunarxMenuClass)) +#define THUNARX_IS_MENU(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNARX_TYPE_MENU)) +#define THUNARX_IS_MENU_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), THUNARX_TYPE_MENU)) +#define THUNARX_MENU_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), THUNARX_TYPE_MENU)) + +/* ThunarxMenuItem defines */ +#define THUNARX_TYPE_MENU_ITEM (thunarx_menu_item_get_type ()) +#define THUNARX_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNARX_TYPE_MENU_ITEM, ThunarxMenuItem)) +#define THUNARX_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNARX_TYPE_MENU_ITEM, ThunarxMenuItemClass)) +#define THUNARX_IS_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNARX_TYPE_MENU_ITEM)) +#define THUNARX_IS_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), THUNARX_TYPE_MENU_ITEM)) +#define THUNARX_MENU_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), THUNARX_TYPE_MENU_ITEM)) + + + +/* ThunarxMenu structs */ +struct _ThunarxMenuClass { + GObjectClass __parent__; +}; + +struct _ThunarxMenu { + GObject __parent__; + ThunarxMenuPrivate *priv; +}; + +/* ThunarxMenuItem structs */ +struct _ThunarxMenuItemClass { + GObjectClass __parent__; + void (*activate) (ThunarxMenuItem *item); +}; + +struct _ThunarxMenuItem { + GObject __parent__; + ThunarxMenuItemPrivate *priv; +}; + + + +/* ThunarxMenu methods */ +GType thunarx_menu_get_type (void) G_GNUC_CONST; + +ThunarxMenu *thunarx_menu_new (void) G_GNUC_MALLOC; + +void thunarx_menu_append_item (ThunarxMenu *menu, + ThunarxMenuItem *item); + +GList* thunarx_menu_get_items (ThunarxMenu *menu); + +/* ThunarxMenuItem methods */ +GType thunarx_menu_item_get_type (void) G_GNUC_CONST; + +ThunarxMenuItem *thunarx_menu_item_new (const gchar *name, + const gchar *label, + const gchar *tooltip, + const gchar *icon) G_GNUC_MALLOC; + +void thunarx_menu_item_activate (ThunarxMenuItem *item); + +gboolean thunarx_menu_item_get_sensitive (ThunarxMenuItem *item); +void thunarx_menu_item_set_sensitive (ThunarxMenuItem *item, + gboolean sensitive); + +void thunarx_menu_item_list_free (GList *items); + +void thunarx_menu_item_set_menu (ThunarxMenuItem *item, + ThunarxMenu *menu); + +G_END_DECLS; + +#endif /* !__THUNARX_MENU_H__ */ diff --git a/thunarx/thunarx.h b/thunarx/thunarx.h index d6de6913..192bf9a7 100644 --- a/thunarx/thunarx.h +++ b/thunarx/thunarx.h @@ -25,7 +25,7 @@ #include <thunarx/thunarx-config.h> #include <thunarx/thunarx-file-info.h> -#include <thunarx/thunarx-menu-item.h> +#include <thunarx/thunarx-menu.h> #include <thunarx/thunarx-menu-provider.h> #include <thunarx/thunarx-preferences-provider.h> #include <thunarx/thunarx-property-page.h> diff --git a/thunarx/thunarx.symbols b/thunarx/thunarx.symbols index a53ed091..750d55fe 100644 --- a/thunarx/thunarx.symbols +++ b/thunarx/thunarx.symbols @@ -46,12 +46,20 @@ thunarx_file_info_list_get_type thunarx_file_info_list_copy thunarx_file_info_list_free +/* ThunarxMenu methods */ +thunarx_menu_get_type G_GNUC_CONST +thunarx_menu_new G_GNUC_MALLOC +thunarx_menu_append_item +thunarx_menu_get_items + /* ThunarxMenuItem methods */ thunarx_menu_item_get_type G_GNUC_CONST thunarx_menu_item_new G_GNUC_MALLOC thunarx_menu_item_activate thunarx_menu_item_get_sensitive thunarx_menu_item_set_sensitive +thunarx_menu_item_set_menu +thunarx_menu_item_list_free /* ThunarxMenuProvider methods */ thunarx_menu_provider_get_type G_GNUC_CONST |