From 5d8d44a32d19063be8237f52ebf5d56c4e90f7d6 Mon Sep 17 00:00:00 2001 From: Jason Conti Date: Sun, 26 Feb 2012 15:03:47 -0500 Subject: * Move the button events to notification menuitem. * Add a clicked event to the notification menuitem. * Drop the old new_notification_menuitem code. --- src/indicator-notifications.c | 97 ++++--------------------------------------- src/notification-menuitem.c | 56 +++++++++++++++++++++++++ src/notification-menuitem.h | 4 ++ 3 files changed, 69 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/indicator-notifications.c b/src/indicator-notifications.c index db37c02..03b9099 100644 --- a/src/indicator-notifications.c +++ b/src/indicator-notifications.c @@ -110,18 +110,15 @@ static void clear_menuitems(IndicatorNotifications *self); static void insert_menuitem(IndicatorNotifications *self, GtkWidget *item); static void remove_menuitem(IndicatorNotifications *self, GtkWidget *item); static GdkPixbuf *load_icon(const gchar *name, gint size); -static GtkWidget *new_notification_menuitem(Notification *note); static void update_clear_item_markup(IndicatorNotifications *self); /* Callbacks */ static void clear_item_activated_cb(GtkMenuItem *menuitem, gpointer user_data); static void menu_visible_notify_cb(GtkWidget *menu, GParamSpec *pspec, gpointer user_data); static void message_received_cb(DBusSpy *spy, Notification *note, gpointer user_data); +static void notification_clicked_cb(NotificationMenuItem *menuitem, gpointer user_data); static void style_changed_cb(GtkWidget *widget, GtkStyle *oldstyle, gpointer user_data); -static gboolean notification_button_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data); -static gboolean notification_button_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data); - /* Indicator Module Config */ INDICATOR_SET_VERSION INDICATOR_SET_TYPE(INDICATOR_NOTIFICATIONS_TYPE) @@ -435,62 +432,6 @@ load_icon(const gchar *name, gint size) return pixbuf; } -/** - * new_notification_menuitem: - * @note: the notification object - * - * Constructs a new notification menuitem from the given notification object. - * - * Returns: a new notification menuitem - **/ -static GtkWidget * -new_notification_menuitem(Notification *note) -{ - g_return_val_if_fail(IS_NOTIFICATION(note), NULL); - gchar *unescaped_timestamp_string = notification_timestamp_for_locale(note); - - gchar *app_name = g_markup_escape_text(notification_get_app_name(note), -1); - gchar *summary = g_markup_escape_text(notification_get_summary(note), -1); - gchar *body = g_markup_escape_text(notification_get_body(note), -1); - gchar *timestamp_string = g_markup_escape_text(unescaped_timestamp_string, -1); - - gchar *markup = g_strdup_printf("%s\n%s\n%s %s %s", - summary, body, timestamp_string, _("from"), app_name); - - g_free(app_name); - g_free(summary); - g_free(body); - g_free(unescaped_timestamp_string); - g_free(timestamp_string); - - GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); - - GtkWidget *label = gtk_label_new(NULL); - gtk_misc_set_alignment(GTK_MISC(label), 0, 0); - gtk_label_set_use_markup(GTK_LABEL(label), TRUE); - gtk_label_set_markup(GTK_LABEL(label), markup); - gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); - gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD_CHAR); - - gtk_label_set_max_width_chars(GTK_LABEL(label), 42); - - gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); - gtk_widget_show(label); - - g_free(markup); - - GtkWidget *close_image = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU); - gtk_widget_show(close_image); - gtk_box_pack_start(GTK_BOX(hbox), close_image, FALSE, FALSE, 0); - - GtkWidget *item = gtk_menu_item_new(); - gtk_container_add(GTK_CONTAINER(item), hbox); - gtk_widget_show(hbox); - gtk_widget_show(item); - - return item; -} - /** * update_clear_item_markup: * @self: the indicator object @@ -580,8 +521,7 @@ message_received_cb(DBusSpy *spy, Notification *note, gpointer user_data) GtkWidget *item = notification_menuitem_new(); notification_menuitem_set_from_notification(NOTIFICATION_MENUITEM(item), note); - g_signal_connect(item, "button-press-event", G_CALLBACK(notification_button_press_cb), self); - g_signal_connect(item, "button-release-event", G_CALLBACK(notification_button_release_cb), self); + g_signal_connect(item, NOTIFICATION_MENUITEM_SIGNAL_CLICKED, G_CALLBACK(notification_clicked_cb), self); gtk_widget_show(item); g_object_unref(note); @@ -638,37 +578,18 @@ style_changed_cb(GtkWidget *widget, GtkStyle *oldstyle, gpointer user_data) } /** - * notification_button_press_cb: - * @widget: the menuitem - * @event: the button press event - * @user_data: the indicator object - * - * Override the menuitem button-press-event. - **/ -static gboolean -notification_button_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data) -{ - return TRUE; -} - -/** - * notification_button_release_cb: + * notification_clicked_cb: * @widget: the menuitem - * @event: the button release event * @user_data: the indicator object * - * Override the menuitem button-release-event so that the menu isn't hidden when the - * item is removed. - * - * FIXME: Only remove the item when the close image is clicked. + * Remove the menuitem when clicked. **/ -static gboolean -notification_button_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data) +static void +notification_clicked_cb(NotificationMenuItem *menuitem, gpointer user_data) { - g_return_val_if_fail(GTK_IS_MENU_ITEM(widget), FALSE); - g_return_val_if_fail(IS_INDICATOR_NOTIFICATIONS(user_data), FALSE); + g_return_if_fail(IS_NOTIFICATION_MENUITEM(menuitem)); + g_return_if_fail(IS_INDICATOR_NOTIFICATIONS(user_data)); IndicatorNotifications *self = INDICATOR_NOTIFICATIONS(user_data); - remove_menuitem(self, widget); - return TRUE; + remove_menuitem(self, GTK_WIDGET(menuitem)); } diff --git a/src/notification-menuitem.c b/src/notification-menuitem.c index 9840c31..9e3c029 100644 --- a/src/notification-menuitem.c +++ b/src/notification-menuitem.c @@ -11,9 +11,19 @@ #define NOTIFICATION_MENUITEM_MAX_CHARS 42 +enum { + CLICKED, + LAST_SIGNAL +}; + static void notification_menuitem_class_init(NotificationMenuItemClass *klass); static void notification_menuitem_init(NotificationMenuItem *self); +static gboolean notification_button_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data); +static gboolean notification_button_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data); + +static guint notification_menuitem_signals[LAST_SIGNAL] = { 0 }; + G_DEFINE_TYPE (NotificationMenuItem, notification_menuitem, GTK_TYPE_MENU_ITEM); static void @@ -24,6 +34,15 @@ notification_menuitem_class_init(NotificationMenuItemClass *klass) g_type_class_add_private(klass, sizeof(NotificationMenuItemPrivate)); menu_item_class->hide_on_activate = FALSE; + + notification_menuitem_signals[CLICKED] = + g_signal_new(NOTIFICATION_MENUITEM_SIGNAL_CLICKED, + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET(NotificationMenuItemClass, clicked), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); } static void @@ -49,6 +68,9 @@ notification_menuitem_init(NotificationMenuItem *self) gtk_container_add(GTK_CONTAINER(self), hbox); gtk_widget_show(hbox); + + g_signal_connect(self, "button-press-event", G_CALLBACK(notification_button_press_cb), NULL); + g_signal_connect(self, "button-release-event", G_CALLBACK(notification_button_release_cb), NULL); } GtkWidget * @@ -81,3 +103,37 @@ notification_menuitem_set_from_notification(NotificationMenuItem *self, Notifica g_free(markup); } + +/** + * notification_button_press_cb: + * @widget: the menuitem + * @event: the button press event + * @user_data: not used + * + * Override the menuitem button-press-event. + **/ +static gboolean +notification_button_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data) +{ + return TRUE; +} + +/** + * notification_button_release_cb: + * @widget: the menuitem + * @event: the button release event + * @user_data: not used + * + * Override the menuitem button-release-event so that the menu isn't hidden when the + * item is removed. + * + * FIXME: Only remove the item when the close image is clicked. + **/ +static gboolean +notification_button_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer user_data) +{ + g_return_val_if_fail(IS_NOTIFICATION_MENUITEM(widget), FALSE); + + g_signal_emit(NOTIFICATION_MENUITEM(widget), notification_menuitem_signals[CLICKED], 0); + return TRUE; +} diff --git a/src/notification-menuitem.h b/src/notification-menuitem.h index f76704a..11421c1 100644 --- a/src/notification-menuitem.h +++ b/src/notification-menuitem.h @@ -29,6 +29,8 @@ struct _NotificationMenuItem struct _NotificationMenuItemClass { GtkMenuItemClass parent_class; + + void (* clicked) (NotificationMenuItem *menuitem); }; struct _NotificationMenuItemPrivate { @@ -39,6 +41,8 @@ struct _NotificationMenuItemPrivate { #define NOTIFICATION_MENUITEM_GET_PRIVATE(o) \ (G_TYPE_INSTANCE_GET_PRIVATE ((o), NOTIFICATION_MENUITEM_TYPE, NotificationMenuItemPrivate)) +#define NOTIFICATION_MENUITEM_SIGNAL_CLICKED "clicked" + GType notification_menuitem_get_type(void); GtkWidget *notification_menuitem_new(void); void notification_menuitem_set_from_notification(NotificationMenuItem *self, Notification *note); -- cgit v1.2.3