Fix notify::label emmision in GtkMenuItem

Emit notify::label in GtkMenuItem also when label is changed through GtkAction.

Refactor GtkMenuItem and remove duplicated code for GtkLabel creation. Reset
the accel-widget back to the GtkMenuItem itself when there is no action related
to the GtkMenuItem anymore.

Add test for notify::label emmisions.

Fixes bug 612574 - GtkMenuItem does not emit notify::label when label is
changed through GtkAction.
This commit is contained in:
Jan Arne Petersen 2010-03-11 15:40:55 +01:00
parent e8c0920494
commit 63c21edab7
3 changed files with 116 additions and 12 deletions

View File

@ -636,7 +636,7 @@ activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
const gchar *label;
label = gtk_action_get_label (action);
gtk_label_set_label (GTK_LABEL (child), label ? label : "");
gtk_menu_item_set_label (menu_item, label);
}
}
@ -669,6 +669,16 @@ gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);
if (!priv->use_action_appearance || !action)
{
GtkWidget *label = GTK_BIN (menu_item)->child;
label = GTK_BIN (menu_item)->child;
if (GTK_IS_ACCEL_LABEL (label))
gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
}
if (!action)
return;
@ -688,18 +698,17 @@ gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
label = NULL;
}
if (!label)
label = g_object_new (GTK_TYPE_ACCEL_LABEL,
"use-underline", TRUE,
"xalign", 0.0,
"visible", TRUE,
"parent", menu_item,
NULL);
gtk_menu_item_ensure_label (menu_item);
gtk_menu_item_set_use_underline (menu_item, TRUE);
label = GTK_BIN (menu_item)->child;
if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
g_object_set (label,
"accel-closure", gtk_action_get_accel_closure (action),
NULL);
{
gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
gtk_action_get_accel_closure (action));
}
activatable_update_label (menu_item, action);
}
@ -2115,7 +2124,7 @@ gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
if (!GTK_BIN (menu_item)->child)
{
accel_label = (GtkWidget *)g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
gtk_container_add (GTK_CONTAINER (menu_item), accel_label);

View File

@ -91,4 +91,8 @@ TEST_PROGS += expander
expander_SOURCES = expander.c
expander_LDADD = $(progs_ldadd)
TEST_PROGS += action
action_SOURCES = action.c
action_LDADD = $(progs_ldadd)
-include $(top_srcdir)/git.mk

91
gtk/tests/action.c Normal file
View File

@ -0,0 +1,91 @@
/* GtkAction tests.
*
* Authors: Jan Arne Petersen <jpetersen@openismus.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <gtk/gtk.h>
/* Fixture */
typedef struct
{
GtkAction *action;
} ActionTest;
static void
action_test_setup (ActionTest *fixture,
gconstpointer test_data)
{
fixture->action = gtk_action_new ("name", "label", NULL, NULL);
}
static void
action_test_teardown (ActionTest *fixture,
gconstpointer test_data)
{
g_object_unref (fixture->action);
}
static void
notify_count_emmisions (GObject *object,
GParamSpec *pspec,
gpointer data)
{
unsigned int *i = data;
(*i)++;
}
static void
menu_item_label_notify_count (ActionTest *fixture,
gconstpointer test_data)
{
GtkMenuItem *item = gtk_menu_item_new ();
unsigned int emmisions = 0;
g_signal_connect (item, "notify::label",
G_CALLBACK (notify_count_emmisions), &emmisions);
gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (item),
fixture->action);
g_assert_cmpuint (emmisions, ==, 1);
gtk_action_set_label (fixture->action, "new label");
g_assert_cmpuint (emmisions, ==, 2);
g_object_unref (item);
}
/* main */
int
main (int argc,
char **argv)
{
gtk_test_init (&argc, &argv, NULL);
g_test_add ("/Action/MenuItem/label-notify-count",
ActionTest, NULL,
action_test_setup,
menu_item_label_notify_count,
action_test_teardown);
return g_test_run ();
}