highlight queue if there are msgs in its sub-folders and the tree is collapsed
[claws.git] / src / noticeview.c
1 /* 
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2012 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 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
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>
33
34 #if HAVE_LIBCOMPFACE
35 #  include <compface.h>
36 #endif
37
38 #include "prefs_common.h"
39 #include "gtkutils.h"
40 #include "utils.h"
41 #include "stock_pixmap.h"
42
43 #include "noticeview.h"
44
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);
58
59 static GdkCursor *hand_cursor = NULL;
60
61 NoticeView *noticeview_create(MainWindow *mainwin)
62 {
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;
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         return noticeview;
136 }
137
138 void noticeview_destroy(NoticeView *noticeview)
139 {
140         g_free(noticeview);
141 }
142
143 gboolean noticeview_is_visible(NoticeView *noticeview)
144 {
145         return noticeview->visible;
146 }
147
148 void noticeview_show(NoticeView *noticeview)
149 {
150         if (!noticeview->visible) {
151                 gtk_widget_show(GTK_WIDGET_PTR(noticeview));
152                 noticeview->visible = TRUE;
153         }       
154 }
155
156 void noticeview_hide(NoticeView *noticeview)
157 {
158         if (noticeview && noticeview->visible) {
159                 gtk_widget_hide(GTK_WIDGET_PTR(noticeview));
160                 noticeview->visible = FALSE;
161         }       
162 }
163
164 void noticeview_set_text(NoticeView *noticeview, const char *text)
165 {
166         cm_return_if_fail(noticeview);
167         gtk_label_set_text(GTK_LABEL(noticeview->text), text);
168 }
169
170 void noticeview_set_button_text(NoticeView *noticeview, const char *text)
171 {
172         cm_return_if_fail(noticeview);
173
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);
180         
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. 
184          */
185         gtk_widget_hide(noticeview->button2);
186 }
187
188 void noticeview_set_button_press_callback(NoticeView    *noticeview,
189                                           void          (*callback)(void),
190                                           gpointer      *user_data)
191 {
192         noticeview->press     = (void (*) (NoticeView *, gpointer)) callback;
193         noticeview->user_data = user_data;
194 }
195
196 static void noticeview_button_pressed(GtkButton *button, NoticeView *noticeview)
197 {
198         if (noticeview->press) {
199                 noticeview->press(noticeview, noticeview->user_data);
200         }
201 }
202
203 static gboolean noticeview_icon_pressed(GtkWidget *widget, GdkEventButton *evt,
204                                     NoticeView *noticeview)
205 {
206         if (evt && evt->button == 1 && noticeview->icon_clickable) {
207                 noticeview_button_pressed(NULL, noticeview);
208         }
209         return FALSE;
210 }
211
212 static gboolean noticeview_visi_notify(GtkWidget *widget,
213                                        GdkEventVisibility *event,
214                                        NoticeView *noticeview)
215 {
216         if (noticeview->icon_clickable)
217                 gdk_window_set_cursor(gtk_widget_get_window(noticeview->evtbox), hand_cursor);
218         return FALSE;
219 }
220
221 static gboolean noticeview_leave_notify(GtkWidget *widget,
222                                       GdkEventCrossing *event,
223                                       NoticeView *noticeview)
224 {
225         gdk_window_set_cursor(gtk_widget_get_window(noticeview->evtbox), NULL);
226         return FALSE;
227 }
228
229 static gboolean noticeview_enter_notify(GtkWidget *widget,
230                                       GdkEventCrossing *event,
231                                       NoticeView *noticeview)
232 {
233         if (noticeview->icon_clickable)
234                 gdk_window_set_cursor(gtk_widget_get_window(noticeview->evtbox), hand_cursor);
235         return FALSE;
236 }
237
238 void noticeview_set_2ndbutton_text(NoticeView *noticeview, const char *text)
239 {
240         cm_return_if_fail(noticeview);
241
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);
248 }
249
250 void noticeview_set_2ndbutton_press_callback(NoticeView *noticeview,
251                                           void          (*callback)(void),
252                                           gpointer      *user_data)
253 {
254         noticeview->press2     = (void (*) (NoticeView *, gpointer)) callback;
255         noticeview->user_data2 = user_data;
256 }
257
258 static void noticeview_2ndbutton_pressed(GtkButton *button, NoticeView *noticeview)
259 {
260         if (noticeview->press2) {
261                 noticeview->press2(noticeview, noticeview->user_data2);
262         }
263 }
264
265 void noticeview_set_icon(NoticeView *noticeview, StockPixmap icon)
266 {
267         GdkPixbuf *pixbuf;
268         
269         if (stock_pixbuf_gdk(noticeview->window, icon, &pixbuf) < 0)
270                 return;
271         
272         gtk_image_set_from_pixbuf(GTK_IMAGE(noticeview->icon), pixbuf);
273 }
274
275 void noticeview_set_icon_clickable(NoticeView *noticeview, gboolean setting)
276 {
277         noticeview->icon_clickable = setting;
278 }               
279
280 void noticeview_set_tooltip (NoticeView *noticeview, const gchar *text)
281 {
282         CLAWS_SET_TIP(noticeview->evtbox,
283                         text);
284
285 }