/*
 * Size adjustment code (size_allocate_h(), find_icon_size()) taken
 * from GtkStatusIcon,
 * Copyright (C) 2003 Sun Microsystems, Inc.
 * Copyright (C) 2005 Hans Breuer <hans@breuer.org>
 * Copyright (C) 2005 Novell, Inc.
 *
 * Mail Notification
 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

%headertop{
#include <gtk/gtk.h>
#include "eggtrayicon.h"
%}

%privateheader{
#include "mn-tooltips.h"
%}

%{
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include "mn-stock.h"
#include "mn-util.h"
#include "mn-mail-icon-widget.h"
%}

class MN:Mail:Icon from Egg:Tray:Icon
{
  private MNTooltips *tooltips = {mn_tooltips_new()} unrefwith g_object_unref;
  private GtkWidget *widget = {mn_mail_icon_widget_new()};

  private char *stock_id destroywith g_free;
  private int size;

  private GtkWidget *menu = {gtk_menu_new()} destroywith gtk_widget_destroy;
  public GtkWidget *mail_reader_item;
  public GtkWidget *open_latest_message_item;
  public GtkWidget *update_item;
  public GtkWidget *consider_new_mail_as_read_item;

  signal (ACTION) private NONE (NONE)
    void activate (self);
  signal (ACTION) private NONE (NONE)
    void activate_mail_reader (self);
  signal (ACTION) private NONE (NONE)
    void activate_open_latest_message (self);
  signal (ACTION) private NONE (NONE)
    void activate_consider_new_mail_as_read (self);
  signal (ACTION) private NONE (NONE)
    void activate_update (self);
  signal (ACTION) private NONE (NONE)
    void activate_properties (self);
  signal (ACTION) private NONE (NONE)
    void activate_help (self);
  signal (ACTION) private NONE (NONE)
    void activate_about (self);
  signal (ACTION) private NONE (NONE)
    void activate_remove (self);

  init (self)
  {
    GtkMenuShell *shell;
    GtkWidget *properties_item;
    GtkWidget *help_item;
    GtkWidget *about_item;
    GtkWidget *remove_item;

    shell = GTK_MENU_SHELL(selfp->menu);
    /* translators: header capitalization */
    self->mail_reader_item = mn_menu_shell_append(shell, MN_STOCK_MAIL_READER, _("_Mail Reader"));
    /* translators: header capitalization */
    self->open_latest_message_item = mn_menu_shell_append(shell, MN_STOCK_OPEN_MESSAGE, _("_Open Latest Message"));

    mn_menu_shell_append(shell, NULL, NULL);

    /* translators: header capitalization */
    self->consider_new_mail_as_read_item = mn_menu_shell_append(shell, MN_STOCK_CONSIDER_NEW_MAIL_AS_READ, _("_Consider New Mail as Read"));
    /* translators: header capitalization */
    self->update_item = mn_menu_shell_append(shell, GTK_STOCK_REFRESH, _("_Update"));

    mn_menu_shell_append(shell, NULL, NULL);

    properties_item = mn_menu_shell_append(shell, GTK_STOCK_PROPERTIES, NULL);
    help_item = mn_menu_shell_append(shell, GTK_STOCK_HELP, NULL);
    about_item = mn_menu_shell_append(shell, GTK_STOCK_ABOUT, NULL);

    mn_menu_shell_append(shell, NULL, NULL);

    /* translators: header capitalization */
    remove_item = mn_menu_shell_append(shell, GTK_STOCK_REMOVE, _("R_emove From Notification Area"));

    g_signal_connect_swapped(self->mail_reader_item, "activate", G_CALLBACK(self_activate_mail_reader), self);
    g_signal_connect_swapped(self->open_latest_message_item, "activate", G_CALLBACK(self_activate_open_latest_message), self);
    g_signal_connect_swapped(self->consider_new_mail_as_read_item, "activate", G_CALLBACK(self_activate_consider_new_mail_as_read), self);
    g_signal_connect_swapped(self->update_item, "activate", G_CALLBACK(self_activate_update), self);
    g_signal_connect_swapped(properties_item, "activate", G_CALLBACK(self_activate_properties), self);
    g_signal_connect_swapped(help_item, "activate", G_CALLBACK(self_activate_help), self);
    g_signal_connect_swapped(about_item, "activate", G_CALLBACK(self_activate_about), self);
    g_signal_connect_swapped(remove_item, "activate", G_CALLBACK(self_activate_remove), self);

    gtk_widget_add_events(GTK_WIDGET(self), GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);

    gtk_container_add(GTK_CONTAINER(self), selfp->widget);

    g_object_connect(self,
		     "signal::button-press-event", self_button_press_event_h, self,
		     "signal::button-release-event", self_button_release_event_h, self,
		     NULL);

    g_signal_connect(selfp->widget, "size-allocate", G_CALLBACK(self_size_allocate_h), self);

    gtk_widget_show(selfp->widget);
  }

  private void
    size_allocate_h (GtkWidget *widget,
		     GtkAllocation *allocation,
		     gpointer user_data)
  {
    Self *self = user_data;
    GtkOrientation orientation;
    int size;

    orientation = egg_tray_icon_get_orientation(EGG_TRAY_ICON(self));

    if (orientation == GTK_ORIENTATION_HORIZONTAL)
      size = allocation->height;
    else
      size = allocation->width;

    if (selfp->size != size)
      {
	selfp->size = size;
	self_update_image(self);
      }
  }

  private GtkIconSize
    find_icon_size (Gtk:Widget *widget (check null type), int pixel_size)
  {
    GdkScreen *screen;
    GtkSettings *settings;
    GtkIconSize s, size;
    int w, h, d, dist;

    screen = gtk_widget_get_screen(widget);

    if (! screen)
      return GTK_ICON_SIZE_MENU;

    settings = gtk_settings_get_for_screen(screen);

    dist = G_MAXINT;
    size = GTK_ICON_SIZE_MENU;

    for (s = GTK_ICON_SIZE_MENU; s < GTK_ICON_SIZE_DIALOG; s++)
      {
	if (gtk_icon_size_lookup_for_settings(settings, s, &w, &h)
	    && w <= pixel_size
	    && h <= pixel_size)
	  {
	    d = MAX(pixel_size - w, pixel_size - h);
	    if (d < dist)
	      {
		dist = d;
		size = s;
	      }
	  }
      }

    return size;
  }

  private void
    update_image (self)
  {
    GtkIconSize size;

    size = self_find_icon_size(selfp->widget, selfp->size);
    mn_mail_icon_widget_set_from_stock(MN_MAIL_ICON_WIDGET(selfp->widget), selfp->stock_id, size);
  }

  private gboolean
    button_press_event_h (GtkWidget *widget,
			  GdkEventButton *event,
			  gpointer user_data)
  {
    Self *self = user_data;

    if (event->button == 1)
      self_activate(self);
    else if (event->button == 3)
      {
	gtk_menu_popup(GTK_MENU(selfp->menu), NULL, NULL, self_popup_menu_position_cb, self, event->button, event->time);
	return TRUE;		/* do not propagate event */
      }

    return FALSE;		/* propagate event */
  }

  private gboolean
    button_release_event_h (GtkWidget *widget,
			    GdkEventButton *event,
			    gpointer user_data)
  {
    Self *self = user_data;

    if (event->button == 3)
      {
	gtk_menu_popdown(GTK_MENU(selfp->menu));
	return TRUE;		/* do not propagate event */
      }

    return FALSE;		/* propagate event */
  }

  private void
    popup_menu_position_cb (GtkMenu *menu,
			    int *x,
			    int *y,
			    gboolean *push_in,
			    gpointer user_data)
  {
    GtkWidget *widget = user_data;
    GtkAllocation allocation;

    gdk_window_get_origin(gtk_widget_get_window(widget), x, y);
    gtk_widget_get_allocation(widget, &allocation);

    *x += allocation.x;
    *y += allocation.y;

    if (*y > gdk_screen_get_height(gtk_widget_get_screen(widget)) / 2)
      {
	GtkRequisition req;

	gtk_widget_size_request(GTK_WIDGET(menu), &req);
	*y -= req.height;
      }
    else
      *y += allocation.height;

    *push_in = TRUE;
  }

  public void
    set_from_stock (self, const char *stock_id)
  {
    g_free(selfp->stock_id);
    selfp->stock_id = g_strdup(stock_id);

    self_update_image(self);
  }

  public void
    set_blinking (self, gboolean blinking)
  {
    mn_mail_icon_widget_set_blinking(MN_MAIL_ICON_WIDGET(selfp->widget), blinking);
  }

  public void
    set_count (self, int count)
  {
    mn_mail_icon_widget_set_count(MN_MAIL_ICON_WIDGET(selfp->widget), count);
  }

  public void
    set_tip (self, const char *tip)
  {
    mn_tooltips_set_tip(selfp->tooltips, GTK_WIDGET(self), tip);
  }

  public void
    set_tip_widget (self, Gtk:Widget *tip_widget)
  {
    mn_tooltips_set_tip_widget(selfp->tooltips, GTK_WIDGET(self), tip_widget);
  }

  public GtkWidget *
    new (void)
  {
    return GTK_WIDGET(GET_NEW_VARG("title", _("Mail Notification"), NULL));
  }
}
