Fix bug #3574: Template addressing
[claws.git] / src / noticeview.c
blobda93572e408aba662fd7aa2922438549f614a376
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2012 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
34 #if HAVE_LIBCOMPFACE
35 # include <compface.h>
36 #endif
38 #include "prefs_common.h"
39 #include "gtkutils.h"
40 #include "utils.h"
41 #include "stock_pixmap.h"
43 #include "noticeview.h"
45 static void noticeview_button_pressed (GtkButton *button, NoticeView *noticeview);
46 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview);
47 static gboolean noticeview_icon_pressed (GtkWidget *widget, GdkEventButton *evt,
48 NoticeView *noticeview);
49 static gboolean noticeview_visi_notify(GtkWidget *widget,
50 GdkEventVisibility *event,
51 NoticeView *noticeview);
52 static gboolean noticeview_leave_notify(GtkWidget *widget,
53 GdkEventCrossing *event,
54 NoticeView *textview);
55 static gboolean noticeview_enter_notify(GtkWidget *widget,
56 GdkEventCrossing *event,
57 NoticeView *textview);
59 static GdkCursor *hand_cursor = NULL;
61 NoticeView *noticeview_create(MainWindow *mainwin)
63 NoticeView *noticeview;
64 GtkWidget *vbox;
65 GtkWidget *hsep;
66 GtkWidget *hbox;
67 GtkWidget *icon;
68 GtkWidget *text;
69 GtkWidget *widget;
70 GtkWidget *widget2;
71 GtkWidget *evtbox;
73 debug_print("Creating notice view...\n");
74 noticeview = g_new0(NoticeView, 1);
76 if (!hand_cursor)
77 hand_cursor = gdk_cursor_new(GDK_HAND2);
79 noticeview->window = mainwin->window;
81 vbox = gtk_vbox_new(FALSE, 4);
82 gtk_widget_show(vbox);
83 hsep = gtk_hseparator_new();
84 gtk_box_pack_start(GTK_BOX(vbox), hsep, FALSE, TRUE, 1);
86 hbox = gtk_hbox_new(FALSE, 4);
87 gtk_widget_show(hbox);
88 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 1);
90 evtbox = gtk_event_box_new();
91 gtk_event_box_set_visible_window(GTK_EVENT_BOX(evtbox), FALSE);
92 gtk_widget_show(evtbox);
94 icon = stock_pixmap_widget(STOCK_PIXMAP_NOTICE_WARN);
96 gtk_widget_show(icon);
97 g_signal_connect(G_OBJECT(evtbox), "button-press-event",
98 G_CALLBACK(noticeview_icon_pressed),
99 (gpointer) noticeview);
100 g_signal_connect(G_OBJECT(evtbox), "motion-notify-event",
101 G_CALLBACK(noticeview_visi_notify), noticeview);
102 g_signal_connect(G_OBJECT(evtbox), "leave-notify-event",
103 G_CALLBACK(noticeview_leave_notify), noticeview);
104 g_signal_connect(G_OBJECT(evtbox), "enter-notify-event",
105 G_CALLBACK(noticeview_enter_notify), noticeview);
107 gtk_container_add(GTK_CONTAINER(evtbox), icon);
108 gtk_box_pack_start(GTK_BOX(hbox), evtbox, FALSE, TRUE, 0);
110 text = gtk_label_new("");
111 gtk_widget_show(text);
112 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 0);
114 widget = gtk_button_new_with_label("");
115 g_signal_connect(G_OBJECT(widget), "clicked",
116 G_CALLBACK(noticeview_button_pressed),
117 (gpointer) noticeview);
118 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, FALSE, 4);
120 widget2 = gtk_button_new_with_label("");
121 g_signal_connect(G_OBJECT(widget2), "clicked",
122 G_CALLBACK(noticeview_2ndbutton_pressed),
123 (gpointer) noticeview);
124 gtk_box_pack_start(GTK_BOX(hbox), widget2, FALSE, FALSE, 0);
126 noticeview->vbox = vbox;
127 noticeview->hsep = hsep;
128 noticeview->hbox = hbox;
129 noticeview->icon = icon;
130 noticeview->text = text;
131 noticeview->button = widget;
132 noticeview->button2= widget2;
133 noticeview->evtbox = evtbox;
134 noticeview->visible= TRUE;
135 return noticeview;
138 void noticeview_destroy(NoticeView *noticeview)
140 g_free(noticeview);
143 gboolean noticeview_is_visible(NoticeView *noticeview)
145 return noticeview->visible;
148 void noticeview_show(NoticeView *noticeview)
150 if (!noticeview->visible) {
151 gtk_widget_show(GTK_WIDGET_PTR(noticeview));
152 noticeview->visible = TRUE;
156 void noticeview_hide(NoticeView *noticeview)
158 if (noticeview && noticeview->visible) {
159 gtk_widget_hide(GTK_WIDGET_PTR(noticeview));
160 noticeview->visible = FALSE;
164 void noticeview_set_text(NoticeView *noticeview, const char *text)
166 cm_return_if_fail(noticeview);
167 gtk_label_set_text(GTK_LABEL(noticeview->text), text);
170 void noticeview_set_button_text(NoticeView *noticeview, const char *text)
172 cm_return_if_fail(noticeview);
174 if (text != NULL) {
175 gtk_label_set_text
176 (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button)))), text);
177 gtk_widget_show(noticeview->button);
178 } else
179 gtk_widget_hide(noticeview->button);
181 /* Callers defining only one button don't have to mind
182 * resetting the second one. Callers defining two have
183 * to define the second button after the first one.
185 gtk_widget_hide(noticeview->button2);
188 void noticeview_set_button_press_callback(NoticeView *noticeview,
189 void (*callback)(void),
190 gpointer *user_data)
192 noticeview->press = (void (*) (NoticeView *, gpointer)) callback;
193 noticeview->user_data = user_data;
196 static void noticeview_button_pressed(GtkButton *button, NoticeView *noticeview)
198 if (noticeview->press) {
199 noticeview->press(noticeview, noticeview->user_data);
203 static gboolean noticeview_icon_pressed(GtkWidget *widget, GdkEventButton *evt,
204 NoticeView *noticeview)
206 if (evt && evt->button == 1 && noticeview->icon_clickable) {
207 noticeview_button_pressed(NULL, noticeview);
209 return FALSE;
212 static gboolean noticeview_visi_notify(GtkWidget *widget,
213 GdkEventVisibility *event,
214 NoticeView *noticeview)
216 if (noticeview->icon_clickable)
217 gdk_window_set_cursor(gtk_widget_get_window(noticeview->evtbox), hand_cursor);
218 return FALSE;
221 static gboolean noticeview_leave_notify(GtkWidget *widget,
222 GdkEventCrossing *event,
223 NoticeView *noticeview)
225 gdk_window_set_cursor(gtk_widget_get_window(noticeview->evtbox), NULL);
226 return FALSE;
229 static gboolean noticeview_enter_notify(GtkWidget *widget,
230 GdkEventCrossing *event,
231 NoticeView *noticeview)
233 if (noticeview->icon_clickable)
234 gdk_window_set_cursor(gtk_widget_get_window(noticeview->evtbox), hand_cursor);
235 return FALSE;
238 void noticeview_set_2ndbutton_text(NoticeView *noticeview, const char *text)
240 cm_return_if_fail(noticeview);
242 if (text != NULL) {
243 gtk_label_set_text
244 (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button2)))), text);
245 gtk_widget_show(noticeview->button2);
246 } else
247 gtk_widget_hide(noticeview->button2);
250 void noticeview_set_2ndbutton_press_callback(NoticeView *noticeview,
251 void (*callback)(void),
252 gpointer *user_data)
254 noticeview->press2 = (void (*) (NoticeView *, gpointer)) callback;
255 noticeview->user_data2 = user_data;
258 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview)
260 if (noticeview->press2) {
261 noticeview->press2(noticeview, noticeview->user_data2);
265 void noticeview_set_icon(NoticeView *noticeview, StockPixmap icon)
267 GdkPixbuf *pixbuf;
269 if (stock_pixbuf_gdk(icon, &pixbuf) < 0)
270 return;
272 gtk_image_set_from_pixbuf(GTK_IMAGE(noticeview->icon), pixbuf);
275 void noticeview_set_icon_clickable(NoticeView *noticeview, gboolean setting)
277 noticeview->icon_clickable = setting;
280 void noticeview_set_tooltip (NoticeView *noticeview, const gchar *text)
282 CLAWS_SET_TIP(noticeview->evtbox,
283 text);