Example #1
0
/**
 * dbusmenu_menuitem_property_set_image:
 * @menuitem: The #DbusmenuMenuitem to set the property on.
 * @property: Name of the property to set.
 * @data: The image to place on the property.
 * 
 * This function takes the pixbuf that is stored in @data and
 * turns it into a base64 encoded PNG so that it can be placed
 * onto a standard #DbusmenuMenuitem property.
 * 
 * Return value: Whether the function was able to set the property
 * 	or not.
*/
gboolean
dbusmenu_menuitem_property_set_image (DbusmenuMenuitem * menuitem, const gchar * property, const GdkPixbuf * data)
{
	g_return_val_if_fail(GDK_IS_PIXBUF(data), FALSE);
	g_return_val_if_fail(DBUSMENU_IS_MENUITEM(menuitem), FALSE);
	g_return_val_if_fail(property != NULL && property[0] != '\0', FALSE);

	GError * error = NULL;
	gchar * png_data;
	gsize png_data_len;

	if (!gdk_pixbuf_save_to_buffer((GdkPixbuf *)data, &png_data, &png_data_len, "png", &error, NULL)) {
		if (error == NULL) {
			g_warning("Unable to create pixbuf data stream: %d", (gint)png_data_len);
		} else {
			g_warning("Unable to create pixbuf data stream: %s", error->message);
			g_error_free(error);
			error = NULL;
		}

		return FALSE;
	}

	gboolean propreturn = FALSE;
	propreturn = dbusmenu_menuitem_property_set_byte_array(menuitem, property, (guchar *)png_data, png_data_len);

	g_free(png_data);

	return propreturn;
}
void setItemIcon(DbusmenuMenuitem *item, const char *iconBytesPng, int iconBytesCount) {
  const gboolean propreturn = dbusmenu_menuitem_property_set_byte_array(item, DBUSMENU_MENUITEM_PROP_ICON_DATA,
                                                                        (guchar *) iconBytesPng, iconBytesCount);
  // NOTE: memory copied (try to call memset(iconBytesPng, 0, iconBytesCount) after)

//  if (propreturn)
//    _logmsg(LOG_LEVEL_INFO, "\tset %d icon bytes for item %s", iconBytesCount, _getItemLabel(item));
//  else
//    _logmsg(LOG_LEVEL_ERROR, "\tcan't set %d icon bytes for item %s", iconBytesCount, _getItemLabel(item));
}
void setItemIcon(DbusmenuMenuitem *item, const char *iconBytesPng, int iconBytesCount) {
  dbusmenu_menuitem_property_set_byte_array(item, DBUSMENU_MENUITEM_PROP_ICON_DATA, (guchar*)iconBytesPng, (gsize)iconBytesCount);
  // NOTE: memory copied (try to call memset(iconBytesPng, 0, iconBytesCount) after)
}