summaryrefslogtreecommitdiff
path: root/src/notification.c
blob: 42f72c5b7af4b5d00072766938f6b20f4d085e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * notification.c
 *
 * Copyright (C) 2002 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 *
 * Authors: Glynn Foster <glynn.foster@sun.com>
 */

#include <glade/glade.h>
#include <time.h>
#include "zenity.h"
#include "eggtrayicon.h"
#include "util.h"

EggTrayIcon *tray_icon;

static gboolean
zenity_notification_icon_press_callback (GtkWidget *widget, GdkEventButton *event, gpointer data)
{
  ZenityData *zen_data;

  zen_data = data;

  zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK);
  gtk_main_quit ();
}

static gboolean
zenity_notification_icon_expose_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  if (GTK_WIDGET_HAS_FOCUS (widget)) {
    gint focus_width, focus_pad;
    gint x, y, width, height;
                                                                                                                                                             
    gtk_widget_style_get (widget,
                          "focus-line-width", &focus_width,
                          "focus-padding", &focus_pad,
                          NULL);
    x = widget->allocation.x + focus_pad;
    y = widget->allocation.y + focus_pad;
    width = widget->allocation.width - 2 * focus_pad;
    height = widget->allocation.height - 2 * focus_pad;
                                                                                                                                                             
    gtk_paint_focus (widget->style, widget->window,
                     GTK_WIDGET_STATE (widget),
                     &event->area, widget, "button",
                     x, y, width, height);
    }
                                                                                                                                                             
    return FALSE;
}

static gboolean
zenity_notification_icon_destroy_callback (GtkWidget *widget, gpointer data)
{
  ZenityData *zen_data;

  zen_data = data;
  gtk_widget_destroy (GTK_WIDGET (tray_icon));

  zen_data->exit_code = zenity_util_return_exit_code (ZENITY_ESC);
  gtk_main_quit ();
}

void 
zenity_notification (ZenityData *data, ZenityNotificationData *notification_data)
{
  GtkWidget *icon_image;
  GtkWidget *icon_event_box;
  GtkTooltips *tooltips;
  GdkPixbuf *pixbuf = NULL;

  tray_icon = egg_tray_icon_new (_("Zenity notification"));
  tooltips = gtk_tooltips_new ();

  if (data->window_icon != NULL)
    pixbuf = zenity_util_pixbuf_new_from_file (GTK_WIDGET (tray_icon), data->window_icon);
  else
    pixbuf = gdk_pixbuf_new_from_file (ZENITY_IMAGE_FULLPATH ("zenity-notification.png"), NULL);

  icon_event_box = gtk_event_box_new ();

  if (pixbuf) {
    icon_image = gtk_image_new_from_pixbuf (pixbuf);
    gdk_pixbuf_unref (pixbuf);
  } else {
    g_warning ("Could not load notification icon : %s", ZENITY_IMAGE_FULLPATH ("zenity-notification.png"));
    return; 
  }

  gtk_container_add (GTK_CONTAINER (icon_event_box), icon_image);

  if (notification_data->notification_text) 
    gtk_tooltips_set_tip (tooltips, icon_event_box, notification_data->notification_text, notification_data->notification_text);
  else
    gtk_tooltips_set_tip (tooltips, icon_event_box, _("Zenity notification"), _("Zenity notification"));
 
  gtk_widget_add_events (GTK_WIDGET (tray_icon), GDK_BUTTON_PRESS_MASK | GDK_FOCUS_CHANGE_MASK);
  gtk_container_add (GTK_CONTAINER (tray_icon), icon_event_box);

  g_signal_connect (tray_icon, "button_press_event",
		    G_CALLBACK (zenity_notification_icon_press_callback), data);

  g_signal_connect (tray_icon, "destroy",
		    G_CALLBACK (zenity_notification_icon_destroy_callback), data);

  g_signal_connect (tray_icon, "expose_event",
		    G_CALLBACK (zenity_notification_icon_expose_callback), data);

  gtk_widget_show_all (GTK_WIDGET (tray_icon));
  
  /* Does nothing at the moment */
  gtk_main ();
}