091de1c329d547dc53c26c70d997f370587896e3
[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/gtkwidget.h>
29 #include <gtk/gtkhbox.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtklabel.h>
32 #include <gtk/gtkpixmap.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <time.h>
36
37 #if HAVE_LIBCOMPFACE
38 #  include <compface.h>
39 #endif
40
41 #include "prefs_common.h"
42 #include "gtkutils.h"
43 #include "utils.h"
44 #include "stock_pixmap.h"
45
46 #include "noticeview.h"
47
48 static void noticeview_button_pressed   (GtkButton *button, NoticeView *noticeview);
49 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview);
50 static gboolean noticeview_icon_pressed (GtkWidget *widget, GdkEventButton *evt,
51                                          NoticeView *noticeview);
52 static gboolean noticeview_visi_notify(GtkWidget *widget,
53                                        GdkEventVisibility *event,
54                                        NoticeView *noticeview);
55 static gboolean noticeview_leave_notify(GtkWidget *widget,
56                                       GdkEventCrossing *event,
57                                       NoticeView *textview);
58 static gboolean noticeview_enter_notify(GtkWidget *widget,
59                                       GdkEventCrossing *event,
60                                       NoticeView *textview);
61
62 static GdkCursor *hand_cursor = NULL;
63
64 NoticeView *noticeview_create(MainWindow *mainwin)
65 {
66         NoticeView *noticeview;
67         GtkWidget  *vbox;
68         GtkWidget  *hsep;
69         GtkWidget  *hbox;
70         GtkWidget  *icon;
71         GtkWidget  *text;
72         GtkWidget  *widget;
73         GtkWidget  *widget2;
74         GtkWidget  *evtbox;
75         CLAWS_TIP_DECL();
76
77         debug_print("Creating notice view...\n");
78         noticeview = g_new0(NoticeView, 1);
79
80         if (!hand_cursor)
81                 hand_cursor = gdk_cursor_new(GDK_HAND2);
82
83         noticeview->window = mainwin->window;
84         
85         vbox = gtk_vbox_new(FALSE, 4);
86         gtk_widget_show(vbox);
87         hsep = gtk_hseparator_new();
88         gtk_box_pack_start(GTK_BOX(vbox), hsep, FALSE, TRUE, 1);
89         
90         hbox = gtk_hbox_new(FALSE, 4);
91         gtk_widget_show(hbox);
92         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 1);
93
94         evtbox = gtk_event_box_new();
95         gtk_event_box_set_visible_window(GTK_EVENT_BOX(evtbox), FALSE);
96         gtk_widget_show(evtbox);
97
98         icon = stock_pixmap_widget(noticeview->window, STOCK_PIXMAP_NOTICE_WARN); 
99
100         gtk_widget_show(icon);
101         g_signal_connect(G_OBJECT(evtbox), "button-press-event", 
102                          G_CALLBACK(noticeview_icon_pressed),
103                          (gpointer) noticeview);
104         g_signal_connect(G_OBJECT(evtbox), "visibility-notify-event",
105                          G_CALLBACK(noticeview_visi_notify), noticeview);
106         g_signal_connect(G_OBJECT(evtbox), "motion-notify-event",
107                          G_CALLBACK(noticeview_visi_notify), noticeview);
108         g_signal_connect(G_OBJECT(evtbox), "leave-notify-event",
109                          G_CALLBACK(noticeview_leave_notify), noticeview);
110         g_signal_connect(G_OBJECT(evtbox), "enter-notify-event",
111                          G_CALLBACK(noticeview_enter_notify), noticeview);
112         
113         gtk_container_add(GTK_CONTAINER(evtbox), icon);
114         gtk_box_pack_start(GTK_BOX(hbox), evtbox, FALSE, TRUE, 0);
115         
116         text = gtk_label_new("");
117         gtk_widget_show(text);
118         gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 0);
119
120         widget = gtk_button_new_with_label("");
121         g_signal_connect(G_OBJECT(widget), "clicked", 
122                          G_CALLBACK(noticeview_button_pressed),
123                          (gpointer) noticeview);
124         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, FALSE, 4);
125         
126         widget2 = gtk_button_new_with_label("");
127         g_signal_connect(G_OBJECT(widget2), "clicked", 
128                          G_CALLBACK(noticeview_2ndbutton_pressed),
129                          (gpointer) noticeview);
130         gtk_box_pack_start(GTK_BOX(hbox), widget2, FALSE, FALSE, 0);
131         
132         noticeview->vbox   = vbox;
133         noticeview->hsep   = hsep;
134         noticeview->hbox   = hbox;
135         noticeview->icon   = icon;
136         noticeview->text   = text;
137         noticeview->button = widget;
138         noticeview->button2= widget2;
139         noticeview->evtbox = evtbox;
140         noticeview->visible= TRUE;
141 #if !(GTK_CHECK_VERSION(2,12,0))
142         noticeview->tooltips = tips;
143 #endif
144         return noticeview;
145 }
146
147 void noticeview_destroy(NoticeView *noticeview)
148 {
149         g_free(noticeview);
150 }
151
152 gboolean noticeview_is_visible(NoticeView *noticeview)
153 {
154         return noticeview->visible;
155 }
156
157 void noticeview_show(NoticeView *noticeview)
158 {
159         if (!noticeview->visible) {
160                 gtk_widget_show(GTK_WIDGET_PTR(noticeview));
161                 noticeview->visible = TRUE;
162         }       
163 }
164
165 void noticeview_hide(NoticeView *noticeview)
166 {
167         if (noticeview && noticeview->visible) {
168                 gtk_widget_hide(GTK_WIDGET_PTR(noticeview));
169                 noticeview->visible = FALSE;
170         }       
171 }
172
173 void noticeview_set_text(NoticeView *noticeview, const char *text)
174 {
175         g_return_if_fail(noticeview);
176         gtk_label_set_text(GTK_LABEL(noticeview->text), text);
177 }
178
179 void noticeview_set_button_text(NoticeView *noticeview, const char *text)
180 {
181         g_return_if_fail(noticeview);
182
183         if (text != NULL) {
184                 gtk_label_set_text
185                         (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button)))), text);
186                 gtk_widget_show(noticeview->button);
187         } else
188                 gtk_widget_hide(noticeview->button);
189         
190         /* Callers defining only one button don't have to mind 
191          * resetting the second one. Callers defining two have
192          * to define the second button after the first one. 
193          */
194         gtk_widget_hide(noticeview->button2);
195 }
196
197 void noticeview_set_button_press_callback(NoticeView    *noticeview,
198                                           GtkSignalFunc  callback,
199                                           gpointer      *user_data)
200 {
201         noticeview->press     = (void (*) (NoticeView *, gpointer)) callback;
202         noticeview->user_data = user_data;
203 }
204
205 static void noticeview_button_pressed(GtkButton *button, NoticeView *noticeview)
206 {
207         if (noticeview->press) {
208                 noticeview->press(noticeview, noticeview->user_data);
209         }
210 }
211
212 static gboolean noticeview_icon_pressed(GtkWidget *widget, GdkEventButton *evt,
213                                     NoticeView *noticeview)
214 {
215         if (evt && evt->button == 1 && noticeview->icon_clickable) {
216                 noticeview_button_pressed(NULL, noticeview);
217         }
218         return FALSE;
219 }
220
221 static gboolean noticeview_visi_notify(GtkWidget *widget,
222                                        GdkEventVisibility *event,
223                                        NoticeView *noticeview)
224 {
225         if (noticeview->icon_clickable)
226                 gdk_window_set_cursor(noticeview->evtbox->window, hand_cursor);
227         return FALSE;
228 }
229
230 static gboolean noticeview_leave_notify(GtkWidget *widget,
231                                       GdkEventCrossing *event,
232                                       NoticeView *noticeview)
233 {
234         gdk_window_set_cursor(noticeview->evtbox->window, NULL);
235         return FALSE;
236 }
237
238 static gboolean noticeview_enter_notify(GtkWidget *widget,
239                                       GdkEventCrossing *event,
240                                       NoticeView *noticeview)
241 {
242         if (noticeview->icon_clickable)
243                 gdk_window_set_cursor(noticeview->evtbox->window, hand_cursor);
244         return FALSE;
245 }
246
247 void noticeview_set_2ndbutton_text(NoticeView *noticeview, const char *text)
248 {
249         g_return_if_fail(noticeview);
250
251         if (text != NULL) {
252                 gtk_label_set_text
253                         (GTK_LABEL(gtk_bin_get_child(GTK_BIN((noticeview->button2)))), text);
254                 gtk_widget_show(noticeview->button2);
255         } else
256                 gtk_widget_hide(noticeview->button2);
257 }
258
259 void noticeview_set_2ndbutton_press_callback(NoticeView *noticeview,
260                                           GtkSignalFunc  callback,
261                                           gpointer      *user_data)
262 {
263         noticeview->press2     = (void (*) (NoticeView *, gpointer)) callback;
264         noticeview->user_data2 = user_data;
265 }
266
267 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview)
268 {
269         if (noticeview->press2) {
270                 noticeview->press2(noticeview, noticeview->user_data2);
271         }
272 }
273
274 void noticeview_set_icon(NoticeView *noticeview, StockPixmap icon)
275 {
276         GdkPixmap *pixmap;
277         GdkBitmap *bitmap;
278         
279         if (stock_pixmap_gdk(noticeview->window, icon, &pixmap, &bitmap) < 0)
280                 return;
281         
282         gtk_image_set_from_pixmap(GTK_IMAGE(noticeview->icon), pixmap, bitmap);
283 }
284
285 void noticeview_set_icon_clickable(NoticeView *noticeview, gboolean setting)
286 {
287         noticeview->icon_clickable = setting;
288 }               
289
290 void noticeview_set_tooltip (NoticeView *noticeview, const gchar *text)
291 {
292 #if !(GTK_CHECK_VERSION(2,12,0))
293         GtkTooltips *tips = noticeview->tooltips;
294 #endif
295         CLAWS_SET_TIP(noticeview->evtbox,
296                         text);
297
298 }