2008-12-10 [colin] 3.6.1cvs68
[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), "visibility-notify-event",
101                          G_CALLBACK(noticeview_visi_notify), noticeview);
102         g_signal_connect(G_OBJECT(evtbox), "motion-notify-event",
103                          G_CALLBACK(noticeview_visi_notify), noticeview);
104         g_signal_connect(G_OBJECT(evtbox), "leave-notify-event",
105                          G_CALLBACK(noticeview_leave_notify), noticeview);
106         g_signal_connect(G_OBJECT(evtbox), "enter-notify-event",
107                          G_CALLBACK(noticeview_enter_notify), noticeview);
108         
109         gtk_container_add(GTK_CONTAINER(evtbox), icon);
110         gtk_box_pack_start(GTK_BOX(hbox), evtbox, FALSE, TRUE, 0);
111         
112         text = gtk_label_new("");
113         gtk_widget_show(text);
114         gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 0);
115
116         widget = gtk_button_new_with_label("");
117         g_signal_connect(G_OBJECT(widget), "clicked", 
118                          G_CALLBACK(noticeview_button_pressed),
119                          (gpointer) noticeview);
120         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, FALSE, 4);
121         
122         widget2 = gtk_button_new_with_label("");
123         g_signal_connect(G_OBJECT(widget2), "clicked", 
124                          G_CALLBACK(noticeview_2ndbutton_pressed),
125                          (gpointer) noticeview);
126         gtk_box_pack_start(GTK_BOX(hbox), widget2, FALSE, FALSE, 0);
127         
128         noticeview->vbox   = vbox;
129         noticeview->hsep   = hsep;
130         noticeview->hbox   = hbox;
131         noticeview->icon   = icon;
132         noticeview->text   = text;
133         noticeview->button = widget;
134         noticeview->button2= widget2;
135         noticeview->evtbox = evtbox;
136         noticeview->visible= TRUE;
137 #if !(GTK_CHECK_VERSION(2,12,0))
138         noticeview->tooltips = tips;
139 #endif
140         return noticeview;
141 }
142
143 void noticeview_destroy(NoticeView *noticeview)
144 {
145         g_free(noticeview);
146 }
147
148 gboolean noticeview_is_visible(NoticeView *noticeview)
149 {
150         return noticeview->visible;
151 }
152
153 void noticeview_show(NoticeView *noticeview)
154 {
155         if (!noticeview->visible) {
156                 gtk_widget_show(GTK_WIDGET_PTR(noticeview));
157                 noticeview->visible = TRUE;
158         }       
159 }
160
161 void noticeview_hide(NoticeView *noticeview)
162 {
163         if (noticeview && noticeview->visible) {
164                 gtk_widget_hide(GTK_WIDGET_PTR(noticeview));
165                 noticeview->visible = FALSE;
166         }       
167 }
168
169 void noticeview_set_text(NoticeView *noticeview, const char *text)
170 {
171         g_return_if_fail(noticeview);
172         gtk_label_set_text(GTK_LABEL(noticeview->text), text);
173 }
174
175 void noticeview_set_button_text(NoticeView *noticeview, const char *text)
176 {
177         g_return_if_fail(noticeview);
178
179         if (text != NULL) {
180                 gtk_label_set_text
181                         (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button)))), text);
182                 gtk_widget_show(noticeview->button);
183         } else
184                 gtk_widget_hide(noticeview->button);
185         
186         /* Callers defining only one button don't have to mind 
187          * resetting the second one. Callers defining two have
188          * to define the second button after the first one. 
189          */
190         gtk_widget_hide(noticeview->button2);
191 }
192
193 void noticeview_set_button_press_callback(NoticeView    *noticeview,
194                                           void          (*callback)(void),
195                                           gpointer      *user_data)
196 {
197         noticeview->press     = (void (*) (NoticeView *, gpointer)) callback;
198         noticeview->user_data = user_data;
199 }
200
201 static void noticeview_button_pressed(GtkButton *button, NoticeView *noticeview)
202 {
203         if (noticeview->press) {
204                 noticeview->press(noticeview, noticeview->user_data);
205         }
206 }
207
208 static gboolean noticeview_icon_pressed(GtkWidget *widget, GdkEventButton *evt,
209                                     NoticeView *noticeview)
210 {
211         if (evt && evt->button == 1 && noticeview->icon_clickable) {
212                 noticeview_button_pressed(NULL, noticeview);
213         }
214         return FALSE;
215 }
216
217 static gboolean noticeview_visi_notify(GtkWidget *widget,
218                                        GdkEventVisibility *event,
219                                        NoticeView *noticeview)
220 {
221         if (noticeview->icon_clickable)
222                 gdk_window_set_cursor(noticeview->evtbox->window, hand_cursor);
223         return FALSE;
224 }
225
226 static gboolean noticeview_leave_notify(GtkWidget *widget,
227                                       GdkEventCrossing *event,
228                                       NoticeView *noticeview)
229 {
230         gdk_window_set_cursor(noticeview->evtbox->window, NULL);
231         return FALSE;
232 }
233
234 static gboolean noticeview_enter_notify(GtkWidget *widget,
235                                       GdkEventCrossing *event,
236                                       NoticeView *noticeview)
237 {
238         if (noticeview->icon_clickable)
239                 gdk_window_set_cursor(noticeview->evtbox->window, hand_cursor);
240         return FALSE;
241 }
242
243 void noticeview_set_2ndbutton_text(NoticeView *noticeview, const char *text)
244 {
245         g_return_if_fail(noticeview);
246
247         if (text != NULL) {
248                 gtk_label_set_text
249                         (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button2)))), text);
250                 gtk_widget_show(noticeview->button2);
251         } else
252                 gtk_widget_hide(noticeview->button2);
253 }
254
255 void noticeview_set_2ndbutton_press_callback(NoticeView *noticeview,
256                                           void          (*callback)(void),
257                                           gpointer      *user_data)
258 {
259         noticeview->press2     = (void (*) (NoticeView *, gpointer)) callback;
260         noticeview->user_data2 = user_data;
261 }
262
263 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview)
264 {
265         if (noticeview->press2) {
266                 noticeview->press2(noticeview, noticeview->user_data2);
267         }
268 }
269
270 void noticeview_set_icon(NoticeView *noticeview, StockPixmap icon)
271 {
272         GdkPixmap *pixmap;
273         GdkBitmap *bitmap;
274         
275         if (stock_pixmap_gdk(noticeview->window, icon, &pixmap, &bitmap) < 0)
276                 return;
277         
278         gtk_image_set_from_pixmap(GTK_IMAGE(noticeview->icon), pixmap, bitmap);
279 }
280
281 void noticeview_set_icon_clickable(NoticeView *noticeview, gboolean setting)
282 {
283         noticeview->icon_clickable = setting;
284 }               
285
286 void noticeview_set_tooltip (NoticeView *noticeview, const gchar *text)
287 {
288 #if !(GTK_CHECK_VERSION(2,12,0))
289         GtkTooltips *tips = noticeview->tooltips;
290 #endif
291         CLAWS_SET_TIP(noticeview->evtbox,
292                         text);
293
294 }