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