aboutsummaryrefslogtreecommitdiff
path: root/src/notificationwindow.c
blob: 018871cd6e9202edc514f84e8b1defbdae0d04c5 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 * Copyright (C) 1999-2013 Hiroyuki Yamamoto
 *
 * 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>

#ifdef G_OS_WIN32
#  include <windows.h>
#endif

#include "notificationwindow.h"
#include "mainwindow.h"
#include "utils.h"

#define NOTIFICATIONWINDOW_NOTIFY_PERIOD	10000
#define NOTIFICATIONWINDOW_WIDTH		300
#define NOTIFICATIONWINDOW_HEIGHT		64
#define FADE_REFRESH_RATE			50
#define FADE_SPEED				5

typedef struct _NotificationWindow
{
	GtkWidget *window;

	GtkWidget *msglabel;
	GtkWidget *sublabel;

	guint notify_tag;

	gint x;
	gint y;
	gint width;
	gint height;
	gint fade_length;
	gint fade_count;
	gint notify_event_count;
	guint timeout;
} NotificationWindow;

static NotificationWindow notify_window;

static void notification_window_destroy(void);

static gboolean notify_timeout_cb(gpointer	 data);

static gboolean nwin_button_pressed	(GtkWidget	*widget,
					 GdkEventButton	*event,
					 gpointer	 data);
static gboolean nwin_entered		(GtkWidget	*widget,
					 GdkEventCrossing *event,
					 gpointer	 data);
static gboolean nwin_motion_notify	(GtkWidget	*widget,
					 GdkEventMotion *event,
					 gpointer	 data);

static void nwin_destroy_cb	(GtkWidget	*widget,
				 gpointer	 data);


static void get_work_area(GdkRectangle *rect)
{
#ifdef G_OS_WIN32
	RECT rc;

	SystemParametersInfoW(SPI_GETWORKAREA, 0, &rc, 0);
	rect->x = rc.left;
	rect->y = rc.top;
	rect->width = rc.right - rc.left;
	rect->height = rc.bottom - rc.top;
#else
	rect->x = 0;
	rect->y = 0;
	rect->width = gdk_screen_width();
	rect->height = gdk_screen_height();
#endif
}

gint notification_window_open(const gchar *message, const gchar *submessage,
			      guint timeout)
{
	GtkWidget *window;
	GtkWidget *vbox;
	GtkWidget *msglabel;
	GtkWidget *sublabel;
	GdkRectangle rect;
	gint x, y;
	GtkRequisition requisition;

	if (notify_window.window) {
		notification_window_destroy();
	}

	window = gtk_window_new(GTK_WINDOW_POPUP);
	gtk_window_set_title(GTK_WINDOW(window), _("Notification"));
	gtk_window_set_wmclass(GTK_WINDOW(window), "notification", "Sylpheed");
	gtk_container_set_border_width(GTK_CONTAINER(window), 8);
	gtk_widget_set_events(window, GDK_EXPOSURE_MASK|GDK_BUTTON_MOTION_MASK|GDK_POINTER_MOTION_MASK|GDK_POINTER_MOTION_HINT_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK|GDK_ENTER_NOTIFY_MASK|GDK_LEAVE_NOTIFY_MASK);
	gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), TRUE);
	gtk_window_set_gravity(GTK_WINDOW(window), GDK_GRAVITY_SOUTH_EAST);
	gtk_widget_set_size_request(window, NOTIFICATIONWINDOW_WIDTH, -1);
	gtk_widget_realize(window);
	gdk_window_set_type_hint(window->window, GDK_WINDOW_TYPE_HINT_NOTIFICATION);

	/* move window bottom-right */
	get_work_area(&rect);
	x = rect.x + rect.width - NOTIFICATIONWINDOW_WIDTH - 2;
	if (x < 0) x = 0;
	y = rect.y + rect.height - NOTIFICATIONWINDOW_HEIGHT - 2;
	if (y < 0) y = 0;
	gtk_window_move(GTK_WINDOW(window), x, y);

	g_signal_connect(G_OBJECT(window), "destroy",
			 G_CALLBACK(nwin_destroy_cb), NULL);
	g_signal_connect(G_OBJECT(window), "button_press_event",
			 G_CALLBACK(nwin_button_pressed), NULL);
	g_signal_connect(G_OBJECT(window), "enter_notify_event",
			 G_CALLBACK(nwin_entered), NULL);
	g_signal_connect(G_OBJECT(window), "motion_notify_event",
			 G_CALLBACK(nwin_motion_notify), NULL);

	vbox = gtk_vbox_new(FALSE, 8);
	gtk_container_add(GTK_CONTAINER(window), vbox);

	msglabel = gtk_label_new(message);
	gtk_box_pack_start(GTK_BOX(vbox), msglabel, FALSE, FALSE, 0);

	sublabel = gtk_label_new("");
	gtk_label_set_ellipsize(GTK_LABEL(sublabel), PANGO_ELLIPSIZE_END);
	gtk_label_set_markup(GTK_LABEL(sublabel), submessage);
	gtk_box_pack_start(GTK_BOX(vbox), sublabel, FALSE, FALSE, 0);
	gtk_label_set_justify(GTK_LABEL(sublabel), GTK_JUSTIFY_LEFT);
	gtk_misc_set_alignment(GTK_MISC(sublabel), 0.0, 0.5);

	gtk_widget_show_all(window);

	/* adjust window size and position */
	gtk_widget_get_child_requisition(window, &requisition);
	notify_window.width = NOTIFICATIONWINDOW_WIDTH;
	notify_window.height = MAX(requisition.height, NOTIFICATIONWINDOW_HEIGHT);
	gtk_widget_set_size_request(window, NOTIFICATIONWINDOW_WIDTH, notify_window.height);
	y = rect.y + rect.height - notify_window.height - 2;
	if (y < 0) y = 0;
	gtk_window_move(GTK_WINDOW(window), x, y);

	if (timeout == 0)
		timeout = 1;
	notify_window.notify_tag = g_timeout_add(timeout * 1000,
						 notify_timeout_cb, NULL);

	debug_print("notification window created\n");

	notify_window.window = window;
	notify_window.msglabel = msglabel;
	notify_window.sublabel = sublabel;
	notify_window.x = x;
	notify_window.y = y;
	notify_window.fade_length = 0;
	notify_window.fade_count = 0;
	notify_window.notify_event_count = 0;
	notify_window.timeout = timeout;

	return 0;
}

void notification_window_set_message(const gchar *message,
				     const gchar *submessage)
{
	if (notify_window.window) {
		gtk_label_set_text(GTK_LABEL(notify_window.msglabel), message);
		gtk_label_set_markup(GTK_LABEL(notify_window.sublabel), submessage);
	}
}

void notification_window_close(void)
{
	notification_window_destroy();
}

static void notification_window_destroy(void)
{
	if (notify_window.window) {
		if (notify_window.notify_tag > 0) {
			g_source_remove(notify_window.notify_tag);
			notify_window.notify_tag = 0;
		}

		gtk_widget_destroy(notify_window.window);

		notify_window.window = NULL;
		notify_window.msglabel = NULL;
		notify_window.sublabel = NULL;

		debug_print("notification window removed\n");
	}
}

static gboolean notify_fadeout_timeout_cb(gpointer data)
{
	gdk_threads_enter();
	notify_window.fade_length -= FADE_SPEED;
	notify_window.fade_count++;

	gtk_window_move(GTK_WINDOW(notify_window.window),
			notify_window.x, notify_window.y + notify_window.fade_count * FADE_SPEED);

	if (notify_window.fade_length <= 0) {
		notification_window_destroy();
		gdk_threads_leave();
		return FALSE;
	}
	gdk_threads_leave();
	return TRUE;
}

static gboolean notify_timeout_cb(gpointer data)
{
	gdk_threads_enter();
	notify_window.fade_length = gdk_screen_height() - notify_window.y;
	notify_window.notify_tag = g_timeout_add(FADE_REFRESH_RATE,
						 notify_fadeout_timeout_cb,
						 NULL);
	gdk_threads_leave();

	return FALSE;
}

static gboolean nwin_button_pressed(GtkWidget *widget, GdkEventButton *event,
				    gpointer data)
{
	if (!event)
		return FALSE;

	notification_window_destroy();
	main_window_popup(main_window_get());

	return TRUE;
}

static gboolean nwin_entered(GtkWidget *widget, GdkEventCrossing *event,
			     gpointer data)
{
	return FALSE;
}

static gboolean nwin_motion_notify(GtkWidget *widget, GdkEventMotion *event,
				   gpointer data)
{
	if (notify_window.fade_count > 0 &&
	    notify_window.notify_event_count == 0) {
		notify_window.notify_event_count++;
		return FALSE;
	}

	if (notify_window.notify_tag > 0) {
		g_source_remove(notify_window.notify_tag);
		notify_window.notify_tag = 0;
	}
	notify_window.fade_count = 0;
	notify_window.notify_event_count = 0;
	gtk_window_move(GTK_WINDOW(notify_window.window),
			notify_window.x, notify_window.y);
	notify_window.notify_tag = g_timeout_add(notify_window.timeout * 1000,
						 notify_timeout_cb, NULL);

	return FALSE;
}

static void nwin_destroy_cb(GtkWidget *widget, gpointer data)
{
}