evolution-3.6.4/calendar/gui/e-week-view-main-item.c

No issues found

Incomplete coverage

Tool Failure ID Location Function Message Data
clang-analyzer no-output-found e-week-view-main-item.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
clang-analyzer no-output-found e-week-view-main-item.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
  1 /*
  2  * EWeekViewMainItem - displays the background grid and dates for the Week and
  3  * Month calendar views.
  4  *
  5  * This program is free software; you can redistribute it and/or
  6  * modify it under the terms of the GNU Lesser General Public
  7  * License as published by the Free Software Foundation; either
  8  * version 2 of the License, or (at your option) version 3.
  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 GNU
 13  * Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public
 16  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 17  *
 18  * Authors:
 19  *		Damon Chaplin <damon@ximian.com>
 20  *
 21  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include <string.h>
 29 #include <glib/gi18n.h>
 30 
 31 #include "e-week-view-main-item.h"
 32 #include "ea-calendar.h"
 33 #include "calendar-config.h"
 34 
 35 #define E_WEEK_VIEW_MAIN_ITEM_GET_PRIVATE(obj) \
 36 	(G_TYPE_INSTANCE_GET_PRIVATE \
 37 	((obj), E_TYPE_WEEK_VIEW_MAIN_ITEM, EWeekViewMainItemPrivate))
 38 
 39 struct _EWeekViewMainItemPrivate {
 40 	EWeekView *week_view;
 41 };
 42 
 43 enum {
 44 	PROP_0,
 45 	PROP_WEEK_VIEW
 46 };
 47 
 48 G_DEFINE_TYPE (
 49 	EWeekViewMainItem,
 50 	e_week_view_main_item,
 51 	GNOME_TYPE_CANVAS_ITEM)
 52 
 53 static gint
 54 gdate_to_cal_weekdays (GDateWeekday wd)
 55 {
 56 	switch (wd) {
 57 	case G_DATE_MONDAY: return CAL_MONDAY;
 58 	case G_DATE_TUESDAY: return CAL_TUESDAY;
 59 	case G_DATE_WEDNESDAY: return CAL_WEDNESDAY;
 60 	case G_DATE_THURSDAY: return CAL_THURSDAY;
 61 	case G_DATE_FRIDAY: return CAL_FRIDAY;
 62 	case G_DATE_SATURDAY: return CAL_SATURDAY;
 63 	case G_DATE_SUNDAY: return CAL_SUNDAY;
 64 	default: break;
 65 	}
 66 
 67 	return 0;
 68 }
 69 
 70 static void
 71 week_view_main_item_draw_day (EWeekViewMainItem *main_item,
 72                               gint day,
 73                               GDate *date,
 74                               cairo_t *cr,
 75                               gint x,
 76                               gint y,
 77                               gint width,
 78                               gint height)
 79 {
 80 	EWeekView *week_view;
 81 	GtkStyle *style;
 82 	gint right_edge, bottom_edge, date_width, date_x, line_y;
 83 	gboolean show_day_name, show_month_name, selected;
 84 	gchar buffer[128], *format_string;
 85 	gint day_of_week, month, day_of_month, max_width;
 86 	GdkColor *bg_color;
 87 	PangoFontDescription *font_desc;
 88 	PangoContext *pango_context;
 89 	PangoFontMetrics *font_metrics;
 90 	PangoLayout *layout;
 91 	gboolean today = FALSE;
 92 	CalWeekdays working_days;
 93 
 94 	week_view = e_week_view_main_item_get_week_view (main_item);
 95 	style = gtk_widget_get_style (GTK_WIDGET (week_view));
 96 
 97 	/* Set up Pango prerequisites */
 98 	font_desc = pango_font_description_copy (style->font_desc);
 99 	pango_context = gtk_widget_get_pango_context (GTK_WIDGET (week_view));
100 	font_metrics = pango_context_get_metrics (
101 		pango_context, font_desc,
102 		pango_context_get_language (pango_context));
103 
104 	day_of_week = gdate_to_cal_weekdays (g_date_get_weekday (date));
105 	month = g_date_get_month (date);
106 	day_of_month = g_date_get_day (date);
107 	line_y = y + E_WEEK_VIEW_DATE_T_PAD +
108 		PANGO_PIXELS (pango_font_metrics_get_ascent (font_metrics)) +
109 		PANGO_PIXELS (pango_font_metrics_get_descent (font_metrics)) +
110 		E_WEEK_VIEW_DATE_LINE_T_PAD;
111 
112 	if (!today) {
113 		ECalendarView *view;
114 		struct icaltimetype tt;
115 		const icaltimezone *zone;
116 
117 		view = E_CALENDAR_VIEW (week_view);
118 		zone = e_calendar_view_get_timezone (view);
119 
120 		/* Check if we are drawing today */
121 		tt = icaltime_from_timet_with_zone (
122 			time (NULL), FALSE, zone);
123 		today = g_date_get_year (date) == tt.year
124 			&& g_date_get_month (date) == tt.month
125 			&& g_date_get_day (date) == tt.day;
126 	}
127 
128 	working_days = calendar_config_get_working_days ();
129 
130 	/* Draw the background of the day. In the month view odd months are
131 	 * one color and even months another, so you can easily see when each
132 	 * month starts (defaults are white for odd - January, March, ... and
133 	 * light gray for even). In the week view the background is always the
134 	 * same color, the color used for the odd months in the month view. */
135 	if (today)
136 		bg_color = &week_view->colors[E_WEEK_VIEW_COLOR_TODAY_BACKGROUND];
137 	else if ((working_days & day_of_week) == 0)
138 		bg_color = &week_view->colors[E_WEEK_VIEW_COLOR_MONTH_NONWORKING_DAY];
139 	else if (week_view->multi_week_view && (month % 2 == 0))
140 		bg_color = &week_view->colors[E_WEEK_VIEW_COLOR_EVEN_MONTHS];
141 	else
142 		bg_color = &week_view->colors[E_WEEK_VIEW_COLOR_ODD_MONTHS];
143 
144 	cairo_save (cr);
145 	gdk_cairo_set_source_color (cr, bg_color);
146 	cairo_rectangle (cr, x, y, width, height);
147 	cairo_fill (cr);
148 	cairo_restore (cr);
149 
150 	/* Draw the lines on the right and bottom of the cell. The canvas is
151 	 * sized so that the lines on the right & bottom edges will be off the
152 	 * edge of the canvas, so we don't have to worry about them. */
153 	right_edge = x + width - 1;
154 	bottom_edge = y + height - 1;
155 
156 	cairo_save (cr);
157 	gdk_cairo_set_source_color (cr,  &week_view->colors[E_WEEK_VIEW_COLOR_GRID]);
158 	cairo_set_line_width (cr, 0.7);
159 	cairo_move_to (cr, right_edge, y);
160 	cairo_line_to (cr, right_edge, bottom_edge);
161 	cairo_move_to (cr, x, bottom_edge);
162 	cairo_line_to (cr, right_edge, bottom_edge);
163 	cairo_stroke (cr);
164 	cairo_restore (cr);
165 
166 	/* If the day is selected, draw the blue background. */
167 	cairo_save (cr);
168 	selected = TRUE;
169 	if (week_view->selection_start_day == -1
170 	    || week_view->selection_start_day > day
171 	    || week_view->selection_end_day < day)
172 		selected = FALSE;
173 	if (selected) {
174 		if (gtk_widget_has_focus (GTK_WIDGET (week_view))) {
175 			gdk_cairo_set_source_color (
176 				cr, &week_view->colors[E_WEEK_VIEW_COLOR_SELECTED]);
177 		} else {
178 			gdk_cairo_set_source_color (
179 				cr, &week_view->colors[E_WEEK_VIEW_COLOR_SELECTED]);
180 		}
181 
182 		if (week_view->multi_week_view) {
183 			cairo_rectangle (
184 				cr, x + 2, y + 1,
185 				width - 5,
186 				E_WEEK_VIEW_DATE_T_PAD - 1 +
187 				PANGO_PIXELS (pango_font_metrics_get_ascent (font_metrics)) +
188 				PANGO_PIXELS (pango_font_metrics_get_descent (font_metrics)));
189 			cairo_fill (cr);
190 		} else {
191 			cairo_rectangle (
192 				cr, x + 2, y + 1,
193 				width - 5, line_y - y);
194 			cairo_fill (cr);
195 		}
196 	}
197 	cairo_restore (cr);
198 
199 	/* Display the date in the top of the cell.
200 	 * In the week view, display the long format "10 January" in all cells,
201 	 * or abbreviate it to "10 Jan" or "10" if that doesn't fit.
202 	 * In the month view, only use the long format for the first cell and
203 	 * the 1st of each month, otherwise use "10". */
204 	show_day_name = FALSE;
205 	show_month_name = FALSE;
206 	if (!week_view->multi_week_view) {
207 		show_day_name = TRUE;
208 		show_month_name = TRUE;
209 	} else if (day == 0 || day_of_month == 1) {
210 		show_month_name = TRUE;
211 	}
212 
213 	/* Now find the longest form of the date that will fit. */
214 	max_width = width - 4;
215 	format_string = NULL;
216 	if (show_day_name) {
217 		if (week_view->max_day_width + week_view->digit_width * 2
218 		    + week_view->space_width * 2
219 		    + week_view->month_widths[month - 1] < max_width)
220 			/* strftime format %A = full weekday name, %d = day of
221 			 * month, %B = full month name. You can change the
222 			 * order but don't change the specifiers or add
223 			 * anything. */
224 			format_string = _("%A %d %B");
225 		else if (week_view->max_abbr_day_width
226 			 + week_view->digit_width * 2
227 			 + week_view->space_width * 2
228 			 + week_view->abbr_month_widths[month - 1] < max_width)
229 			/* strftime format %a = abbreviated weekday name,
230 			 * %d = day of month, %b = abbreviated month name.
231 			 * You can change the order but don't change the
232 			 * specifiers or add anything. */
233 			format_string = _("%a %d %b");
234 	}
235 	if (!format_string && show_month_name) {
236 		if (week_view->digit_width * 2 + week_view->space_width
237 		    + week_view->month_widths[month - 1] < max_width)
238 			/* strftime format %d = day of month, %B = full
239 			 * month name. You can change the order but don't
240 			 * change the specifiers or add anything. */
241 			format_string = _("%d %B");
242 		else if (week_view->digit_width * 2 + week_view->space_width
243 		    + week_view->abbr_month_widths[month - 1] < max_width)
244 			/* strftime format %d = day of month, %b = abbreviated
245 			 * month name. You can change the order but don't
246 			 * change the specifiers or add anything. */
247 			format_string = _("%d %b");
248 	}
249 
250 	cairo_save (cr);
251 	if (selected) {
252 		gdk_cairo_set_source_color (
253 			cr, &week_view->colors[E_WEEK_VIEW_COLOR_DATES_SELECTED]);
254 	} else if (week_view->multi_week_view) {
255 		if (today) {
256 			gdk_cairo_set_source_color (
257 				cr, &week_view->colors[E_WEEK_VIEW_COLOR_TODAY]);
258 		} else {
259 			gdk_cairo_set_source_color (
260 				cr, &week_view->colors[E_WEEK_VIEW_COLOR_DATES]);
261 		}
262 	} else {
263 		gdk_cairo_set_source_color (
264 			cr, &week_view->colors[E_WEEK_VIEW_COLOR_DATES]);
265 	}
266 
267 	if (today) {
268 		g_date_strftime (
269 			buffer, sizeof (buffer),
270 			format_string ? format_string : "<b>%d</b>", date);
271 		pango_cairo_update_context (cr, pango_context);
272 		layout = pango_cairo_create_layout (cr);
273 		pango_layout_set_font_description (layout, font_desc);
274 		pango_layout_set_text (layout, buffer, -1);
275 		pango_layout_set_markup (layout, buffer, strlen (buffer));
276 	} else {
277 		g_date_strftime (
278 			buffer, sizeof (buffer),
279 			format_string ? format_string : "%d", date);
280 		pango_cairo_update_context (cr, pango_context);
281 		layout = pango_cairo_create_layout (cr);
282 		pango_layout_set_font_description (layout, font_desc);
283 		pango_layout_set_text (layout, buffer, -1);
284 	}
285 
286 	pango_layout_get_pixel_size (layout, &date_width, NULL);
287 	date_x = x + width - date_width - E_WEEK_VIEW_DATE_R_PAD;
288 	date_x = MAX (date_x, x + 1);
289 
290 	cairo_translate (cr, date_x, y + E_WEEK_VIEW_DATE_T_PAD);
291 	pango_cairo_update_layout (cr, layout);
292 	pango_cairo_show_layout (cr, layout);
293 	cairo_restore (cr);
294 	g_object_unref (layout);
295 
296 	/* Draw the line under the date. */
297 	if (!week_view->multi_week_view) {
298 		cairo_save (cr);
299 		gdk_cairo_set_source_color (
300 			cr, &week_view->colors[E_WEEK_VIEW_COLOR_GRID]);
301 		cairo_set_line_width (cr, 0.7);
302 		cairo_move_to (cr, x + E_WEEK_VIEW_DATE_LINE_L_PAD, line_y);
303 		cairo_line_to (cr, right_edge, line_y);
304 		cairo_stroke (cr);
305 		cairo_restore (cr);
306 	}
307 	pango_font_metrics_unref (font_metrics);
308 	pango_font_description_free (font_desc);
309 }
310 
311 static void
312 week_view_main_item_set_property (GObject *object,
313                                   guint property_id,
314                                   const GValue *value,
315                                   GParamSpec *pspec)
316 {
317 	switch (property_id) {
318 		case PROP_WEEK_VIEW:
319 			e_week_view_main_item_set_week_view (
320 				E_WEEK_VIEW_MAIN_ITEM (object),
321 				g_value_get_object (value));
322 			return;
323 	}
324 
325 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
326 }
327 
328 static void
329 week_view_main_item_get_property (GObject *object,
330                                   guint property_id,
331                                   GValue *value,
332                                   GParamSpec *pspec)
333 {
334 	switch (property_id) {
335 		case PROP_WEEK_VIEW:
336 			g_value_set_object (
337 				value, e_week_view_main_item_get_week_view (
338 				E_WEEK_VIEW_MAIN_ITEM (object)));
339 			return;
340 	}
341 
342 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
343 }
344 
345 static void
346 week_view_main_item_dispose (GObject *object)
347 {
348 	EWeekViewMainItemPrivate *priv;
349 
350 	priv = E_WEEK_VIEW_MAIN_ITEM_GET_PRIVATE (object);
351 
352 	if (priv->week_view != NULL) {
353 		g_object_unref (priv->week_view);
354 		priv->week_view = NULL;
355 	}
356 
357 	/* Chain up to parent's dispose() method. */
358 	G_OBJECT_CLASS (e_week_view_main_item_parent_class)->dispose (object);
359 }
360 
361 static void
362 week_view_main_item_update (GnomeCanvasItem *item,
363                             const cairo_matrix_t *i2c,
364                             gint flags)
365 {
366 	GnomeCanvasItemClass *canvas_item_class;
367 
368 	/* Chain up to parent's update() method. */
369 	canvas_item_class =
370 		GNOME_CANVAS_ITEM_CLASS (e_week_view_main_item_parent_class);
371 	canvas_item_class->update (item, i2c, flags);
372 
373 	/* The item covers the entire canvas area. */
374 	item->x1 = 0;
375 	item->y1 = 0;
376 	item->x2 = INT_MAX;
377 	item->y2 = INT_MAX;
378 }
379 
380 static void
381 week_view_main_item_draw (GnomeCanvasItem *canvas_item,
382                           cairo_t *cr,
383                           gint x,
384                           gint y,
385                           gint width,
386                           gint height)
387 {
388 	EWeekViewMainItem *main_item;
389 	EWeekView *week_view;
390 	GDate date;
391 	gint num_days, day, day_x, day_y, day_w, day_h;
392 
393 	main_item = E_WEEK_VIEW_MAIN_ITEM (canvas_item);
394 	week_view = e_week_view_main_item_get_week_view (main_item);
395 	g_return_if_fail (week_view != NULL);
396 
397 	/* Step through each of the days. */
398 	date = week_view->first_day_shown;
399 
400 	/* If no date has been set, we just use Dec 1999/January 2000. */
401 	if (!g_date_valid (&date))
402 		g_date_set_dmy (&date, 27, 12, 1999);
403 
404 	num_days = week_view->multi_week_view ? week_view->weeks_shown * 7 : 7;
405 	for (day = 0; day < num_days; day++) {
406 		e_week_view_get_day_position (
407 			week_view, day,
408 			&day_x, &day_y,
409 			&day_w, &day_h);
410 		/* Skip any days which are outside the area. */
411 		if (day_x < x + width && day_x + day_w >= x
412 		    && day_y < y + height && day_y + day_h >= y) {
413 			week_view_main_item_draw_day (
414 				main_item, day, &date, cr,
415 				day_x - x, day_y - y, day_w, day_h);
416 		}
417 		g_date_add_days (&date, 1);
418 	}
419 }
420 
421 static GnomeCanvasItem *
422 week_view_main_item_point (GnomeCanvasItem *item,
423                            gdouble x,
424                            gdouble y,
425                            gint cx,
426                            gint cy)
427 {
428 	return item;
429 }
430 
431 static void
432 e_week_view_main_item_class_init (EWeekViewMainItemClass *class)
433 {
434 	GObjectClass  *object_class;
435 	GnomeCanvasItemClass *item_class;
436 
437 	g_type_class_add_private (class, sizeof (EWeekViewMainItemPrivate));
438 
439 	object_class = G_OBJECT_CLASS (class);
440 	object_class->set_property = week_view_main_item_set_property;
441 	object_class->get_property = week_view_main_item_get_property;
442 	object_class->dispose = week_view_main_item_dispose;
443 
444 	item_class = GNOME_CANVAS_ITEM_CLASS (class);
445 	item_class->update = week_view_main_item_update;
446 	item_class->draw = week_view_main_item_draw;
447 	item_class->point = week_view_main_item_point;
448 
449 	g_object_class_install_property (
450 		object_class,
451 		PROP_WEEK_VIEW,
452 		g_param_spec_object (
453 			"week-view",
454 			"Week View",
455 			NULL,
456 			E_TYPE_WEEK_VIEW,
457 			G_PARAM_READWRITE));
458 
459 	/* init the accessibility support for e_week_view_main_item */
460 	e_week_view_main_item_a11y_init ();
461 }
462 
463 static void
464 e_week_view_main_item_init (EWeekViewMainItem *main_item)
465 {
466 	main_item->priv = E_WEEK_VIEW_MAIN_ITEM_GET_PRIVATE (main_item);
467 }
468 
469 EWeekView *
470 e_week_view_main_item_get_week_view (EWeekViewMainItem *main_item)
471 {
472 	g_return_val_if_fail (E_IS_WEEK_VIEW_MAIN_ITEM (main_item), NULL);
473 
474 	return main_item->priv->week_view;
475 }
476 
477 void
478 e_week_view_main_item_set_week_view (EWeekViewMainItem *main_item,
479                                      EWeekView *week_view)
480 {
481 	g_return_if_fail (E_IS_WEEK_VIEW_MAIN_ITEM (main_item));
482 	g_return_if_fail (E_IS_WEEK_VIEW (week_view));
483 
484 	if (main_item->priv->week_view == week_view)
485 		return;
486 
487 	if (main_item->priv->week_view != NULL)
488 		g_object_unref (main_item->priv->week_view);
489 
490 	main_item->priv->week_view = g_object_ref (week_view);
491 
492 	g_object_notify (G_OBJECT (main_item), "week-view");
493 }