evolution-3.6.4/widgets/misc/e-preview-pane.c

No issues found

  1 /*
  2  * e-preview-pane.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 19  *
 20  */
 21 
 22 #ifdef HAVE_CONFIG_H
 23 #include <config.h>
 24 #endif
 25 
 26 #include "e-preview-pane.h"
 27 
 28 #include <gdk/gdkkeysyms.h>
 29 
 30 #include <libevolution-utils/e-alert-sink.h>
 31 #include <libevolution-utils/e-alert-dialog.h>
 32 
 33 #include "e-alert-bar.h"
 34 
 35 #define E_PREVIEW_PANE_GET_PRIVATE(obj) \
 36 	(G_TYPE_INSTANCE_GET_PRIVATE \
 37 	((obj), E_TYPE_PREVIEW_PANE, EPreviewPanePrivate))
 38 
 39 struct _EPreviewPanePrivate {
 40 	GtkWidget *alert_bar;
 41 	GtkWidget *web_view;
 42 	GtkWidget *search_bar;
 43 };
 44 
 45 enum {
 46 	PROP_0,
 47 	PROP_SEARCH_BAR,
 48 	PROP_WEB_VIEW
 49 };
 50 
 51 enum {
 52 	SHOW_SEARCH_BAR,
 53 	LAST_SIGNAL
 54 };
 55 
 56 static guint signals[LAST_SIGNAL];
 57 
 58 /* Forward Declarations */
 59 static void	e_preview_pane_alert_sink_init
 60 					(EAlertSinkInterface *interface);
 61 
 62 G_DEFINE_TYPE_WITH_CODE (
 63 	EPreviewPane,
 64 	e_preview_pane,
 65 	GTK_TYPE_VBOX,
 66 	G_IMPLEMENT_INTERFACE (
 67 		E_TYPE_ALERT_SINK,
 68 		e_preview_pane_alert_sink_init))
 69 
 70 static void
 71 preview_pane_set_web_view (EPreviewPane *preview_pane,
 72                            EWebView *web_view)
 73 {
 74 	g_return_if_fail (E_IS_WEB_VIEW (web_view));
 75 	g_return_if_fail (preview_pane->priv->web_view == NULL);
 76 
 77 	preview_pane->priv->web_view = g_object_ref_sink (web_view);
 78 }
 79 
 80 static void
 81 preview_pane_set_property (GObject *object,
 82                            guint property_id,
 83                            const GValue *value,
 84                            GParamSpec *pspec)
 85 {
 86 	switch (property_id) {
 87 		case PROP_WEB_VIEW:
 88 			preview_pane_set_web_view (
 89 				E_PREVIEW_PANE (object),
 90 				g_value_get_object (value));
 91 			return;
 92 	}
 93 
 94 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 95 }
 96 
 97 static void
 98 preview_pane_get_property (GObject *object,
 99                            guint property_id,
100                            GValue *value,
101                            GParamSpec *pspec)
102 {
103 	switch (property_id) {
104 		case PROP_SEARCH_BAR:
105 			g_value_set_object (
106 				value, e_preview_pane_get_search_bar (
107 				E_PREVIEW_PANE (object)));
108 			return;
109 
110 		case PROP_WEB_VIEW:
111 			g_value_set_object (
112 				value, e_preview_pane_get_web_view (
113 				E_PREVIEW_PANE (object)));
114 			return;
115 	}
116 
117 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118 }
119 
120 static void
121 preview_pane_dispose (GObject *object)
122 {
123 	EPreviewPanePrivate *priv;
124 
125 	priv = E_PREVIEW_PANE_GET_PRIVATE (object);
126 
127 	if (priv->alert_bar != NULL) {
128 		g_object_unref (priv->alert_bar);
129 		priv->alert_bar = NULL;
130 	}
131 
132 	if (priv->search_bar != NULL) {
133 		g_object_unref (priv->search_bar);
134 		priv->search_bar = NULL;
135 	}
136 
137 	if (priv->web_view != NULL) {
138 		g_object_unref (priv->web_view);
139 		priv->web_view = NULL;
140 	}
141 
142 	/* Chain up to parent's dispose() method. */
143 	G_OBJECT_CLASS (e_preview_pane_parent_class)->dispose (object);
144 }
145 
146 static void
147 preview_pane_constructed (GObject *object)
148 {
149 	EPreviewPanePrivate *priv;
150 	GtkWidget *widget;
151 
152 	priv = E_PREVIEW_PANE_GET_PRIVATE (object);
153 
154 	widget = e_alert_bar_new ();
155 	gtk_box_pack_start (GTK_BOX (object), widget, FALSE, FALSE, 0);
156 	priv->alert_bar = g_object_ref (widget);
157 	/* EAlertBar controls its own visibility. */
158 
159 	widget = gtk_scrolled_window_new (NULL, NULL);
160 	gtk_scrolled_window_set_shadow_type (
161 		GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
162 	gtk_box_pack_start (GTK_BOX (object), widget, TRUE, TRUE, 0);
163 	gtk_container_add (GTK_CONTAINER (widget), priv->web_view);
164 	gtk_widget_show (widget);
165 	gtk_widget_show (priv->web_view);
166 
167 	widget = e_search_bar_new (E_WEB_VIEW (priv->web_view));
168 	gtk_box_pack_start (GTK_BOX (object), widget, FALSE, FALSE, 0);
169 	priv->search_bar = g_object_ref (widget);
170 	gtk_widget_hide (widget);
171 
172 	/* Chain up to parent's constructed() method. */
173 	G_OBJECT_CLASS (e_preview_pane_parent_class)->constructed (object);
174 }
175 
176 static void
177 preview_pane_show_search_bar (EPreviewPane *preview_pane)
178 {
179 	GtkWidget *search_bar;
180 
181 	search_bar = preview_pane->priv->search_bar;
182 
183 	if (!gtk_widget_get_visible (search_bar))
184 		gtk_widget_show (search_bar);
185 }
186 
187 static void
188 preview_pane_submit_alert (EAlertSink *alert_sink,
189                            EAlert *alert)
190 {
191 	EPreviewPane *preview_pane;
192 	EAlertBar *alert_bar;
193 	GtkWidget *dialog;
194 	GtkWindow *parent;
195 
196 	preview_pane = E_PREVIEW_PANE (alert_sink);
197 	alert_bar = E_ALERT_BAR (preview_pane->priv->alert_bar);
198 
199 	switch (e_alert_get_message_type (alert)) {
200 		case GTK_MESSAGE_INFO:
201 		case GTK_MESSAGE_WARNING:
202 		case GTK_MESSAGE_QUESTION:
203 		case GTK_MESSAGE_ERROR:
204 			e_alert_bar_add_alert (alert_bar, alert);
205 			break;
206 
207 		default:
208 			parent = GTK_WINDOW (alert_sink);
209 			dialog = e_alert_dialog_new (parent, alert);
210 			gtk_dialog_run (GTK_DIALOG (dialog));
211 			gtk_widget_destroy (dialog);
212 			break;
213 	}
214 }
215 
216 static void
217 e_preview_pane_class_init (EPreviewPaneClass *class)
218 {
219 	GObjectClass *object_class;
220 	GtkBindingSet *binding_set;
221 
222 	g_type_class_add_private (class, sizeof (EPreviewPanePrivate));
223 
224 	object_class = G_OBJECT_CLASS (class);
225 	object_class->set_property = preview_pane_set_property;
226 	object_class->get_property = preview_pane_get_property;
227 	object_class->dispose = preview_pane_dispose;
228 	object_class->constructed = preview_pane_constructed;
229 
230 	class->show_search_bar = preview_pane_show_search_bar;
231 
232 	g_object_class_install_property (
233 		object_class,
234 		PROP_SEARCH_BAR,
235 		g_param_spec_object (
236 			"search-bar",
237 			"Search Bar",
238 			NULL,
239 			E_TYPE_SEARCH_BAR,
240 			G_PARAM_READABLE));
241 
242 	g_object_class_install_property (
243 		object_class,
244 		PROP_WEB_VIEW,
245 		g_param_spec_object (
246 			"web-view",
247 			"Web View",
248 			NULL,
249 			E_TYPE_WEB_VIEW,
250 			G_PARAM_READWRITE |
251 			G_PARAM_CONSTRUCT_ONLY));
252 
253 	signals[SHOW_SEARCH_BAR] = g_signal_new (
254 		"show-search-bar",
255 		G_TYPE_FROM_CLASS (class),
256 		G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
257 		G_STRUCT_OFFSET (EPreviewPaneClass, show_search_bar),
258 		NULL, NULL,
259 		g_cclosure_marshal_VOID__VOID,
260 		G_TYPE_NONE, 0);
261 
262 	binding_set = gtk_binding_set_by_class (class);
263 
264 	gtk_binding_entry_add_signal (
265 		binding_set, GDK_KEY_f, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
266 		"show-search-bar", 0);
267 }
268 
269 static void
270 e_preview_pane_alert_sink_init (EAlertSinkInterface *interface)
271 {
272 	interface->submit_alert = preview_pane_submit_alert;
273 }
274 
275 static void
276 e_preview_pane_init (EPreviewPane *preview_pane)
277 {
278 	preview_pane->priv = E_PREVIEW_PANE_GET_PRIVATE (preview_pane);
279 
280 	gtk_box_set_spacing (GTK_BOX (preview_pane), 1);
281 }
282 
283 GtkWidget *
284 e_preview_pane_new (EWebView *web_view)
285 {
286 	g_return_val_if_fail (E_IS_WEB_VIEW (web_view), NULL);
287 
288 	return g_object_new (
289 		E_TYPE_PREVIEW_PANE,
290 		"web-view", web_view, NULL);
291 }
292 
293 EWebView *
294 e_preview_pane_get_web_view (EPreviewPane *preview_pane)
295 {
296 	g_return_val_if_fail (E_IS_PREVIEW_PANE (preview_pane), NULL);
297 
298 	return E_WEB_VIEW (preview_pane->priv->web_view);
299 }
300 
301 ESearchBar *
302 e_preview_pane_get_search_bar (EPreviewPane *preview_pane)
303 {
304 	g_return_val_if_fail (E_IS_PREVIEW_PANE (preview_pane), NULL);
305 
306 	return E_SEARCH_BAR (preview_pane->priv->search_bar);
307 }
308 
309 void
310 e_preview_pane_clear_alerts (EPreviewPane *preview_pane)
311 {
312 	g_return_if_fail (E_IS_PREVIEW_PANE (preview_pane));
313 
314 	e_alert_bar_clear (E_ALERT_BAR (preview_pane->priv->alert_bar));
315 }
316 
317 void
318 e_preview_pane_show_search_bar (EPreviewPane *preview_pane)
319 {
320 	g_return_if_fail (E_IS_PREVIEW_PANE (preview_pane));
321 
322 	g_signal_emit (preview_pane, signals[SHOW_SEARCH_BAR], 0);
323 }