menuitem: port arrow gadget to GtkBuiltinIcon

Saves a lot of code.
This commit is contained in:
Cosimo Cecchi 2015-12-19 12:29:39 -08:00
parent 22ec899f23
commit ce6cd49475
5 changed files with 30 additions and 86 deletions

View File

@ -27,6 +27,7 @@
#include <string.h>
#include "gtkaccellabel.h"
#include "gtkbuiltiniconprivate.h"
#include "gtkcontainerprivate.h"
#include "gtkcsscustomgadgetprivate.h"
#include "gtkmain.h"
@ -277,38 +278,6 @@ gtk_menu_item_actionable_interface_init (GtkActionableInterface *iface)
iface->get_action_target_value = gtk_menu_item_get_action_target_value;
}
static gboolean
gtk_menu_item_render_arrow (GtkCssGadget *gadget,
cairo_t *cr,
int x,
int y,
int width,
int height,
gpointer data)
{
GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
GtkMenuItemPrivate *priv = menu_item->priv;
GtkStyleContext *context;
GtkTextDirection direction;
gdouble angle;
context = gtk_widget_get_style_context (widget);
gtk_style_context_save_to_node (context, priv->arrow_node);
direction = gtk_widget_get_direction (widget);
if (direction == GTK_TEXT_DIR_LTR)
angle = G_PI / 2;
else
angle = (3 * G_PI) / 2;
gtk_render_arrow (context, cr, angle, x, y, width);
gtk_style_context_restore (context);
return FALSE;
}
static gboolean
gtk_menu_item_render (GtkCssGadget *gadget,
cairo_t *cr,
@ -1248,13 +1217,6 @@ gtk_menu_item_detacher (GtkWidget *widget,
g_return_if_fail (priv->submenu == (GtkWidget*) menu);
priv->submenu = NULL;
if (priv->arrow_node)
{
gtk_css_node_set_parent (priv->arrow_node, NULL);
priv->arrow_node = NULL;
}
g_clear_object (&priv->arrow_gadget);
}
@ -1531,45 +1493,37 @@ gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
}
}
static void
node_style_changed_cb (GtkCssNode *node,
GtkCssStyleChange *change,
GtkWidget *widget)
{
if (gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_SIZE | GTK_CSS_AFFECTS_CLIP))
gtk_widget_queue_resize (widget);
else
gtk_widget_queue_draw (widget);
}
static void
update_node_classes (GtkMenuItem *menu_item)
{
GtkMenuItemPrivate *priv = menu_item->priv;
GtkCssNode *widget_node, *node;
GtkCssNode *arrow_node, *widget_node, *node;
if (!priv->arrow_node)
if (!priv->arrow_gadget)
return;
arrow_node = gtk_css_gadget_get_node (priv->arrow_gadget);
widget_node = gtk_widget_get_css_node (GTK_WIDGET (menu_item));
gtk_css_node_set_state (arrow_node, gtk_css_node_get_state (widget_node));
if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
{
gtk_css_node_add_class (priv->arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_LEFT));
gtk_css_node_remove_class (priv->arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_RIGHT));
gtk_css_node_add_class (arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_LEFT));
gtk_css_node_remove_class (arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_RIGHT));
node = gtk_css_node_get_first_child (widget_node);
if (node != priv->arrow_node)
gtk_css_node_insert_before (widget_node, priv->arrow_node, node);
if (node != arrow_node)
gtk_css_node_insert_before (widget_node, arrow_node, node);
}
else
{
gtk_css_node_remove_class (priv->arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_LEFT));
gtk_css_node_add_class (priv->arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_RIGHT));
gtk_css_node_remove_class (arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_LEFT));
gtk_css_node_add_class (arrow_node, g_quark_from_static_string (GTK_STYLE_CLASS_RIGHT));
node = gtk_css_node_get_last_child (widget_node);
if (node != priv->arrow_node)
gtk_css_node_insert_after (widget_node, priv->arrow_node, node);
if (node != arrow_node)
gtk_css_node_insert_after (widget_node, arrow_node, node);
}
}
@ -1608,26 +1562,11 @@ gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
if (!GTK_IS_MENU_BAR (gtk_widget_get_parent (widget)))
{
GtkCssNode *widget_node;
widget_node = gtk_widget_get_css_node (widget);
priv->arrow_node = gtk_css_node_new ();
gtk_css_node_set_name (priv->arrow_node, I_("arrow"));
gtk_css_node_set_parent (priv->arrow_node, widget_node);
gtk_css_node_set_state (priv->arrow_node, gtk_css_node_get_state (widget_node));
g_signal_connect_object (priv->arrow_node, "style-changed", G_CALLBACK (node_style_changed_cb), menu_item, 0);
priv->arrow_gadget =
gtk_css_custom_gadget_new_for_node (priv->arrow_node,
widget,
NULL,
NULL,
gtk_menu_item_render_arrow,
NULL, NULL);
priv->arrow_gadget = gtk_builtin_icon_new ("arrow",
widget,
priv->gadget,
NULL);
update_node_classes (menu_item);
g_object_unref (priv->arrow_node);
}
}

View File

@ -43,7 +43,6 @@ struct _GtkMenuItemPrivate
GtkCssGadget *gadget;
GtkCssGadget *arrow_gadget;
GtkCssNode *arrow_node;
guint submenu_placement : 1;
guint submenu_direction : 1;

View File

@ -1513,11 +1513,13 @@ menu,
}
//submenu indicators
& arrow {
-gtk-icon-source: -gtk-icontheme('pan-end-symbolic');
margin-left: 10px;
min-height: 16px;
min-width: 16px;
}
& arrow:dir(ltr) {
-gtk-icon-source: -gtk-icontheme('pan-end-symbolic');
margin-left: 10px;
}
& arrow:dir(rtl) {
-gtk-icon-source:-gtk-icontheme('pan-end-symbolic-rtl');
margin-right: 10px;

View File

@ -2124,10 +2124,12 @@ menu,
background-color: transparent; }
menu menuitem arrow,
.menu menuitem arrow {
-gtk-icon-source: -gtk-icontheme("pan-end-symbolic");
margin-left: 10px;
min-height: 16px;
min-width: 16px; }
menu menuitem arrow:dir(ltr),
.menu menuitem arrow:dir(ltr) {
-gtk-icon-source: -gtk-icontheme("pan-end-symbolic");
margin-left: 10px; }
menu menuitem arrow:dir(rtl),
.menu menuitem arrow:dir(rtl) {
-gtk-icon-source: -gtk-icontheme("pan-end-symbolic-rtl");

View File

@ -2130,10 +2130,12 @@ menu,
background-color: transparent; }
menu menuitem arrow,
.menu menuitem arrow {
-gtk-icon-source: -gtk-icontheme("pan-end-symbolic");
margin-left: 10px;
min-height: 16px;
min-width: 16px; }
menu menuitem arrow:dir(ltr),
.menu menuitem arrow:dir(ltr) {
-gtk-icon-source: -gtk-icontheme("pan-end-symbolic");
margin-left: 10px; }
menu menuitem arrow:dir(rtl),
.menu menuitem arrow:dir(rtl) {
-gtk-icon-source: -gtk-icontheme("pan-end-symbolic-rtl");