/*
* e-attachment-button.c
*
* This program 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) version 3.
*
* This program 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 the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
/* Much of the popup menu logic here was ripped from GtkMenuToolButton. */
#include "e-attachment-button.h"
#include "e-util/e-binding.h"
#define E_ATTACHMENT_BUTTON_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_ATTACHMENT_BUTTON, EAttachmentButtonPrivate))
struct _EAttachmentButtonPrivate {
EAttachmentView *view;
EAttachment *attachment;
gulong reference_handler_id;
EMutualBinding *can_show_binding;
EMutualBinding *shown_binding;
GtkWidget *expand_button;
GtkWidget *toggle_button;
GtkWidget *cell_view;
GtkWidget *popup_menu;
guint expandable : 1;
guint expanded : 1;
};
enum {
PROP_0,
PROP_ATTACHMENT,
PROP_EXPANDABLE,
PROP_EXPANDED,
PROP_VIEW
};
G_DEFINE_TYPE (
EAttachmentButton,
e_attachment_button,
GTK_TYPE_HBOX)
static void
attachment_button_menu_deactivate_cb (EAttachmentButton *button)
{
EAttachmentView *view;
GtkActionGroup *action_group;
GtkToggleButton *toggle_button;
view = e_attachment_button_get_view (button);
action_group = e_attachment_view_get_action_group (view, "inline");
toggle_button = GTK_TOGGLE_BUTTON (button->priv->toggle_button);
gtk_toggle_button_set_active (toggle_button, FALSE);
gtk_action_group_set_visible (action_group, FALSE);
}
static void
attachment_button_menu_position (GtkMenu *menu,
gint *x,
gint *y,
gboolean *push_in,
EAttachmentButton *button)
{
GtkRequisition menu_requisition;
GtkTextDirection direction;
GtkAllocation allocation;
GdkRectangle monitor;
GdkScreen *screen;
GdkWindow *window;
GtkWidget *widget;
GtkWidget *toggle_button;
gint monitor_num;
widget = GTK_WIDGET (button);
toggle_button = button->priv->toggle_button;
gtk_widget_size_request (GTK_WIDGET (menu), &menu_requisition);
window = gtk_widget_get_parent_window (widget);
screen = gtk_widget_get_screen (GTK_WIDGET (menu));
monitor_num = gdk_screen_get_monitor_at_window (screen, window);
if (monitor_num < 0)
monitor_num = 0;
gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
gtk_widget_get_allocation (widget, &allocation);
gdk_window_get_origin (window, x, y);
*x += allocation.x;
*y += allocation.y;
direction = gtk_widget_get_direction (widget);
if (direction == GTK_TEXT_DIR_LTR)
*x += MAX (allocation.width - menu_requisition.width, 0);
else if (menu_requisition.width > allocation.width)
*x -= menu_requisition.width - allocation.width;
gtk_widget_get_allocation (toggle_button, &allocation);
if ((*y + allocation.height +
menu_requisition.height) <= monitor.y + monitor.height)
*y += allocation.height;
else if ((*y - menu_requisition.height) >= monitor.y)
*y -= menu_requisition.height;
else if (monitor.y + monitor.height -
(*y + allocation.height) > *y)
*y += allocation.height;
else
*y -= menu_requisition.height;
*push_in = FALSE;
}
static void
attachment_button_select_path (EAttachmentButton *button)
{
EAttachmentView *view;
EAttachment *attachment;
GtkTreeRowReference *reference;
GtkTreePath *path;
attachment = e_attachment_button_get_attachment (button);
g_return_if_fail (E_IS_ATTACHMENT (attachment));
reference = e_attachment_get_reference (attachment);
g_return_if_fail (gtk_tree_row_reference_valid (reference));
view = e_attachment_button_get_view (button);
path = gtk_tree_row_reference_get_path (reference);
e_attachment_view_unselect_all (view);
e_attachment_view_select_path (view, path);
gtk_tree_path_free (path);
}
static void
attachment_button_show_popup_menu (EAttachmentButton *button,
GdkEventButton *event)
{
EAttachmentView *view;
GtkActionGroup *action_group;
GtkToggleButton *toggle_button;
view = e_attachment_button_get_view (button);
action_group = e_attachment_view_get_action_group (view, "inline");
toggle_button = GTK_TOGGLE_BUTTON (button->priv->toggle_button);
attachment_button_select_path (button);
gtk_toggle_button_set_active (toggle_button, TRUE);
e_attachment_view_show_popup_menu (
view, event, (GtkMenuPositionFunc)
attachment_button_menu_position, button);
gtk_action_group_set_visible (action_group, TRUE);
}
static void
attachment_button_update_cell_view (EAttachmentButton *button)
{
GtkCellView *cell_view;
EAttachment *attachment;
GtkTreeRowReference *reference;
GtkTreeModel *model = NULL;
GtkTreePath *path = NULL;
cell_view = GTK_CELL_VIEW (button->priv->cell_view);
attachment = e_attachment_button_get_attachment (button);
if (attachment == NULL)
goto exit;
reference = e_attachment_get_reference (attachment);
if (reference == NULL)
goto exit;
model = gtk_tree_row_reference_get_model (reference);
path = gtk_tree_row_reference_get_path (reference);
exit:
gtk_cell_view_set_model (cell_view, model);
gtk_cell_view_set_displayed_row (cell_view, path);
if (path != NULL)
gtk_tree_path_free (path);
}
static void
attachment_button_update_pixbufs (EAttachmentButton *button)
{
GtkCellLayout *cell_layout;
GtkCellRenderer *renderer;
GdkPixbuf *pixbuf_expander_open;
GdkPixbuf *pixbuf_expander_closed;