Skip to content

Commit

Permalink
menu: standardize menu item construction / updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LBCrion committed Sep 14, 2024
1 parent 9fb2634 commit 32a1518
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 58 deletions.
23 changes: 6 additions & 17 deletions modules/appmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,25 +230,14 @@ static void app_menu_item_free ( app_menu_item_t *item )

static GtkWidget *app_menu_item_build ( gchar *title, gchar *icon )
{
GtkWidget *menu_item, *grid, *label, *image;
GtkWidget *item;

menu_item = gtk_menu_item_new();
gtk_widget_set_name(menu_item, "menu_item");
grid = gtk_grid_new();
item = gtk_menu_item_new();
gtk_widget_set_name(item, "menu_item");
menu_item_update(item, title, icon);
g_object_set_data(G_OBJECT(item), "title", title);

image = scale_image_new();
scale_image_set_image(image, icon, NULL);
gtk_grid_attach(GTK_GRID(grid), image, 1, 1, 1, 1);

if(title)
{
label = gtk_label_new_with_mnemonic(title);
gtk_grid_attach(GTK_GRID(grid), label, 2, 1, 1, 1);
}
gtk_container_add(GTK_CONTAINER(menu_item), grid);
g_object_set_data(G_OBJECT(menu_item), "title", title);

return menu_item;
return item;
}

static void app_menu_item_insert ( GtkWidget *menu, GtkWidget *item )
Expand Down
53 changes: 37 additions & 16 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,43 @@ gboolean menu_action_cb ( GtkWidget *w ,action_t *action )
return TRUE;
}

void menu_item_update ( GtkWidget *item, const gchar *label, const gchar *icon )
{
GtkWidget *box, *wicon, *wlabel;

g_return_if_fail(GTK_IS_MENU_ITEM(item));

if( !(box = gtk_bin_get_child(GTK_BIN(item))) )
{
box = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(item), box);
}
wicon = gtk_grid_get_child_at(GTK_GRID(box), 1, 1);
if(wicon && !icon)
gtk_widget_destroy(wicon);
else if(!wicon && icon)
{
wicon = scale_image_new();
gtk_grid_attach(GTK_GRID(box), wicon, 1, 1, 1, 1);
}
if(wicon)
scale_image_set_image(wicon, icon, NULL);

wlabel = gtk_grid_get_child_at(GTK_GRID(box), 2, 1);
if(wlabel && !label)
gtk_widget_destroy(wlabel);
else if(!wlabel && label)
{
wlabel = gtk_label_new_with_mnemonic(label);
gtk_grid_attach(GTK_GRID(box), wlabel, 2, 1, 1, 1);
}
else if(wlabel && label)
gtk_label_set_text_with_mnemonic(GTK_LABEL(wlabel), label);
}

GtkWidget *menu_item_new ( gchar *label, action_t *action, gchar *id )
{
GtkWidget *item, *box, *wlabel, *img;
GtkWidget *item;
gchar *text, *icon;

icon = strchr(label,'%');
Expand All @@ -179,21 +213,8 @@ GtkWidget *menu_item_new ( gchar *label, action_t *action, gchar *id )

item = gtk_menu_item_new();
gtk_widget_set_name(item, "menu_item");
box = gtk_grid_new();
if(icon)
{
img = scale_image_new();
scale_image_set_image(img, icon+1, NULL);
if(img)
gtk_grid_attach(GTK_GRID(box), img, 1, 1, 1, 1);
}
if(text)
{
wlabel = gtk_label_new_with_mnemonic(text);
gtk_grid_attach(GTK_GRID(box), wlabel, 2, 1, 1, 1);
g_free(text);
}
gtk_container_add(GTK_CONTAINER(item), box);
menu_item_update(item, text, icon?icon+1:NULL);
g_free(text);

if(action)
{
Expand Down
1 change: 1 addition & 0 deletions src/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ void menu_remove ( gchar *name );
void menu_item_remove ( gchar *id );
void menu_popup ( GtkWidget *, GtkWidget *, GdkEvent *, gpointer, guint16 * );
GtkWidget *menu_item_new ( gchar *label, action_t *action, gchar *id );
void menu_item_update ( GtkWidget *item, const gchar *label, const gchar *icon );

#endif
51 changes: 26 additions & 25 deletions src/snimenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <unistd.h>
#include "sfwbar.h"
#include "sni.h"
#include "menu.h"
#include "scaleimage.h"
#include "flowitem.h"

const gchar *sni_menu_iface = "com.canonical.dbusmenu";
Expand All @@ -16,13 +18,14 @@ static void sni_menu_parse ( GtkWidget *widget, GVariantIter *viter );
static void sni_menu_about_to_show_cb ( GDBusConnection *, GAsyncResult *,
gpointer);

GtkWidget *sni_variant_get_pixbuf ( GVariant *dict, gchar *key )
gchar *sni_menu_get_pixbuf ( GVariant *dict, gchar *key )
{
GVariant *ptr;
GdkPixbufLoader *loader;
GdkPixbuf *pixbuf;
GtkWidget *img = NULL;
static gint pb_counter;
guchar *buff;
gchar *id;
gsize len;

ptr = g_variant_lookup_value(dict,key,G_VARIANT_TYPE_ARRAY);
Expand All @@ -35,15 +38,19 @@ GtkWidget *sni_variant_get_pixbuf ( GVariant *dict, gchar *key )
loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, buff, len, NULL);
gdk_pixbuf_loader_close(loader,NULL);
pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
if(pixbuf)
img = gtk_image_new_from_pixbuf(pixbuf);
if( (pixbuf = gdk_pixbuf_loader_get_pixbuf(loader)) )
{
id = g_strdup_printf("<pixbufcache/>snimenu-%d", pb_counter++);
scale_image_cache_insert(id, gdk_pixbuf_copy(pixbuf));
}
else
id = NULL;
g_object_unref(G_OBJECT(loader));
}

g_variant_unref(ptr);

return img;
return id;
}

static GtkWidget *sni_menu_item_find ( GtkWidget *widget, gint32 id )
Expand Down Expand Up @@ -90,40 +97,34 @@ static void sni_menu_map_cb( GtkWidget *menu, SniItem *sni )
(GAsyncReadyCallback)sni_menu_about_to_show_cb, menu);
}

static void sni_menu_pixbuf_free ( gchar *id )
{
scale_image_cache_remove(id);
g_free(id);
}

static void sni_menu_item_update ( GtkWidget *item, GVariant *dict,
GVariantIter *viter )
{
GtkWidget *box, *wlabel, *wicon, *submenu;
GtkWidget *submenu;
const gchar *label, *icon, *sub;
gboolean has_submenu, state;

gtk_widget_set_name(item, "tray");

if(!GTK_IS_SEPARATOR_MENU_ITEM(item))
{
if( !(box = gtk_bin_get_child(GTK_BIN(item))) )
if( !(g_variant_lookup(dict, "icon-name", "&s", &icon)) )
{
box = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(item), box);
icon = sni_menu_get_pixbuf(dict, "icon-data");
g_object_set_data_full(G_OBJECT(item), "pixbuf", (gpointer)icon,
(GDestroyNotify)sni_menu_pixbuf_free);
}
if( (wicon=gtk_grid_get_child_at(GTK_GRID(box), 1, 1)) )
gtk_widget_destroy(wicon);
if( (g_variant_lookup(dict, "icon-name", "&s", &icon)) )
wicon = gtk_image_new_from_icon_name(icon, GTK_ICON_SIZE_MENU);
else
wicon = sni_variant_get_pixbuf(dict, "icon-data");
if(wicon)
gtk_grid_attach(GTK_GRID(box), wicon, 1, 1, 1, 1);

if( !(g_variant_lookup(dict, "label", "&s", &label)) )
label = "";
if( (wlabel=gtk_grid_get_child_at(GTK_GRID(box), 2, 1)) )
gtk_widget_destroy(wlabel);
if(label)
{
wlabel = gtk_label_new_with_mnemonic(label);
gtk_grid_attach(GTK_GRID(box), wlabel, 2, 1, 1, 1);
}

menu_item_update(item, label, icon);

has_submenu = g_variant_lookup(dict, "children-display", "&s", &sub) &&
!g_strcmp0(sub, "submenu");
Expand Down

0 comments on commit 32a1518

Please sign in to comment.