2009-10-03 [colin] 3.7.2cvs42
[claws.git] / src / noticeview.c
1 /* 
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 Hiroyuki Yamamoto & The Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32
33 #if HAVE_LIBCOMPFACE
34 #  include <compface.h>
35 #endif
36
37 #include "prefs_common.h"
38 #include "gtkutils.h"
39 #include "utils.h"
40 #include "stock_pixmap.h"
41
42 #include "noticeview.h"
43
44 static void noticeview_button_pressed   (GtkButton *button, NoticeView *noticeview);
45 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview);
46 static gboolean noticeview_icon_pressed (GtkWidget *widget, GdkEventButton *evt,
47                                          NoticeView *noticeview);
48 static gboolean noticeview_visi_notify(GtkWidget *widget,
49                                        GdkEventVisibility *event,
50                                        NoticeView *noticeview);
51 static gboolean noticeview_leave_notify(GtkWidget *widget,
52                                       GdkEventCrossing *event,
53                                       NoticeView *textview);
54 static gboolean noticeview_enter_notify(GtkWidget *widget,
55                                       GdkEventCrossing *event,
56                                       NoticeView *textview);
57
58 static GdkCursor *hand_cursor = NULL;
59
60 NoticeView *noticeview_create(MainWindow *mainwin)
61 {
62         NoticeView *noticeview;
63         GtkWidget  *vbox;
64         GtkWidget  *hsep;
65         GtkWidget  *hbox;
66         GtkWidget  *icon;
67         GtkWidget  *text;
68         GtkWidget  *widget;
69         GtkWidget  *widget2;
70         GtkWidget  *evtbox;
71         CLAWS_TIP_DECL();
72
73         debug_print("Creating notice view...\n");
74         noticeview = g_new0(NoticeView, 1);
75
76         if (!hand_cursor)
77                 hand_cursor = gdk_cursor_new(GDK_HAND2);
78
79         noticeview->window = mainwin->window;
80         
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);
85         
86         hbox = gtk_hbox_new(FALSE, 4);
87         gtk_widget_show(hbox);
88         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 1);
89
90         evtbox = gtk_event_box_new();
91         gtk_event_box_set_visible_window(GTK_EVENT_BOX(evtbox), FALSE);
92         gtk_widget_show(evtbox);
93
94         icon = stock_pixmap_widget(noticeview->window, STOCK_PIXMAP_NOTICE_WARN); 
95
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);
106         
107         gtk_container_add(GTK_CONTAINER(evtbox), icon);
108         gtk_box_pack_start(GTK_BOX(hbox), evtbox, FALSE, TRUE, 0);
109         
110         text = gtk_label_new("");
111         gtk_widget_show(text);
112         gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 0);
113
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);
119         
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);
125         
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 #if !(GTK_CHECK_VERSION(2,12,0))
136         noticeview->tooltips = tips;
137 #endif
138         return noticeview;
139 }
140
141 void noticeview_destroy(NoticeView *noticeview)
142 {
143         g_free(noticeview);
144 }
145
146 gboolean noticeview_is_visible(NoticeView *noticeview)
147 {
148         return noticeview->visible;
149 }
150
151 void noticeview_show(NoticeView *noticeview)
152 {
153         if (!noticeview->visible) {
154                 gtk_widget_show(GTK_WIDGET_PTR(noticeview));
155                 noticeview->visible = TRUE;
156         }       
157 }
158
159 void noticeview_hide(NoticeView *noticeview)
160 {
161         if (noticeview && noticeview->visible) {
162                 gtk_widget_hide(GTK_WIDGET_PTR(noticeview));
163                 noticeview->visible = FALSE;
164         }       
165 }
166
167 void noticeview_set_text(NoticeView *noticeview, const char *text)
168 {
169         cm_return_if_fail(noticeview);
170         gtk_label_set_text(GTK_LABEL(noticeview->text), text);
171 }
172
173 void noticeview_set_button_text(NoticeView *noticeview, const char *text)
174 {
175         cm_return_if_fail(noticeview);
176
177         if (text != NULL) {
178                 gtk_label_set_text
179                         (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button)))), text);
180                 gtk_widget_show(noticeview->button);
181         } else
182                 gtk_widget_hide(noticeview->button);
183         
184         /* Callers defining only one button don't have to mind 
185          * resetting the second one. Callers defining two have
186          * to define the second button after the first one. 
187          */
188         gtk_widget_hide(noticeview->button2);
189 }
190
191 void noticeview_set_button_press_callback(NoticeView    *noticeview,
192                                           void          (*callback)(void),
193                                           gpointer      *user_data)
194 {
195         noticeview->press     = (void (*) (NoticeView *, gpointer)) callback;
196         noticeview->user_data = user_data;
197 }
198
199 static void noticeview_button_pressed(GtkButton *button, NoticeView *noticeview)
200 {
201         if (noticeview->press) {
202                 noticeview->press(noticeview, noticeview->user_data);
203         }
204 }
205
206 static gboolean noticeview_icon_pressed(GtkWidget *widget, GdkEventButton *evt,
207                                     NoticeView *noticeview)
208 {
209         if (evt && evt->button == 1 && noticeview->icon_clickable) {
210                 noticeview_button_pressed(NULL, noticeview);
211         }
212         return FALSE;
213 }
214
215 static gboolean noticeview_visi_notify(GtkWidget *widget,
216                                        GdkEventVisibility *event,
217                                        NoticeView *noticeview)
218 {
219         if (noticeview->icon_clickable)
220                 gdk_window_set_cursor(noticeview->evtbox->window, hand_cursor);
221         return FALSE;
222 }
223
224 static gboolean noticeview_leave_notify(GtkWidget *widget,
225                                       GdkEventCrossing *event,
226                                       NoticeView *noticeview)
227 {
228         gdk_window_set_cursor(noticeview->evtbox->window, NULL);
229         return FALSE;
230 }
231
232 static gboolean noticeview_enter_notify(GtkWidget *widget,
233                                       GdkEventCrossing *event,
234                                       NoticeView *noticeview)
235 {
236         if (noticeview->icon_clickable)
237                 gdk_window_set_cursor(noticeview->evtbox->window, hand_cursor);
238         return FALSE;
239 }
240
241 void noticeview_set_2ndbutton_text(NoticeView *noticeview, const char *text)
242 {
243         cm_return_if_fail(noticeview);
244
245         if (text != NULL) {
246                 gtk_label_set_text
247                         (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button2)))), text);
248                 gtk_widget_show(noticeview->button2);
249         } else
250                 gtk_widget_hide(noticeview->button2);
251 }
252
253 void noticeview_set_2ndbutton_press_callback(NoticeView *noticeview,
254                                           void          (*callback)(void),
255                                           gpointer      *user_data)
256 {
257         noticeview->press2     = (void (*) (NoticeView *, gpointer)) callback;
258         noticeview->user_data2 = user_data;
259 }
260
261 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview)
262 {
263         if (noticeview->press2) {
264                 noticeview->press2(noticeview, noticeview->user_data2);
265         }
266 }
267
268 void noticeview_set_icon(NoticeView *noticeview, StockPixmap icon)
269 {
270         GdkPixbuf *pixbuf;
271         
272         if (stock_pixbuf_gdk(noticeview->window, icon, &pixbuf) < 0)
273                 return;
274         
275         gtk_image_set_from_pixbuf(GTK_IMAGE(noticeview->icon), pixbuf);
276 }
277
278 void noticeview_set_icon_clickable(NoticeView *noticeview, gboolean setting)
279 {
280         noticeview->icon_clickable = setting;
281 }               
282
283 void noticeview_set_tooltip (NoticeView *noticeview, const gchar *text)
284 {
285 #if !(GTK_CHECK_VERSION(2,12,0))
286         GtkTooltips *tips = noticeview->tooltips;
287 #endif
288         CLAWS_SET_TIP(noticeview->evtbox,
289                         text);
290
291 }