2
* Copyright (C) 2010 Intel, Inc
4
* Portions from Ubiquity, Copyright (C) 2009 Canonical Ltd.
5
* Written by Evan Dandrea <evand@ubuntu.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
* Author: Thomas Wood <thomas.wood@intel.com>
25
#include "cc-timezone-map.h"
30
G_DEFINE_TYPE (CcTimezoneMap, cc_timezone_map, GTK_TYPE_WIDGET)
32
#define TIMEZONE_MAP_PRIVATE(o) \
33
(G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_TIMEZONE_MAP, CcTimezoneMapPrivate))
43
} CcTimezoneMapOffset;
45
struct _CcTimezoneMapPrivate
47
GdkPixbuf *orig_background;
48
GdkPixbuf *orig_color_map;
50
GdkPixbuf *background;
53
guchar *visible_map_pixels;
54
gint visible_map_rowstride;
56
gdouble selected_offset;
68
static guint signals[LAST_SIGNAL];
71
static CcTimezoneMapOffset color_codes[] =
73
{-11.0, 43, 0, 0, 255 },
74
{-10.0, 85, 0, 0, 255 },
75
{-9.5, 102, 255, 0, 255 },
76
{-9.0, 128, 0, 0, 255 },
77
{-8.0, 170, 0, 0, 255 },
78
{-7.0, 212, 0, 0, 255 },
79
{-6.0, 255, 0, 1, 255 }, // north
80
{-6.0, 255, 0, 0, 255 }, // south
81
{-5.0, 255, 42, 42, 255 },
82
{-4.5, 192, 255, 0, 255 },
83
{-4.0, 255, 85, 85, 255 },
84
{-3.5, 0, 255, 0, 255 },
85
{-3.0, 255, 128, 128, 255 },
86
{-2.0, 255, 170, 170, 255 },
87
{-1.0, 255, 213, 213, 255 },
88
{0.0, 43, 17, 0, 255 },
89
{1.0, 85, 34, 0, 255 },
90
{2.0, 128, 51, 0, 255 },
91
{3.0, 170, 68, 0, 255 },
92
{3.5, 0, 255, 102, 255 },
93
{4.0, 212, 85, 0, 255 },
94
{4.5, 0, 204, 255, 255 },
95
{5.0, 255, 102, 0, 255 },
96
{5.5, 0, 102, 255, 255 },
97
{5.75, 0, 238, 207, 247 },
98
{6.0, 255, 127, 42, 255 },
99
{6.5, 204, 0, 254, 254 },
100
{7.0, 255, 153, 85, 255 },
101
{8.0, 255, 179, 128, 255 },
102
{9.0, 255, 204, 170, 255 },
103
{9.5, 170, 0, 68, 250 },
104
{10.0, 255, 230, 213, 255 },
105
{10.5, 212, 124, 21, 250 },
106
{11.0, 212, 170, 0, 255 },
107
{11.5, 249, 25, 87, 253 },
108
{12.0, 255, 204, 0, 255 },
109
{12.75, 254, 74, 100, 248 },
110
{13.0, 255, 85, 153, 250 },
116
cc_timezone_map_get_property (GObject *object,
124
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
129
cc_timezone_map_set_property (GObject *object,
137
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
142
cc_timezone_map_dispose (GObject *object)
144
CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (object)->priv;
146
if (priv->orig_background)
148
g_object_unref (priv->orig_background);
149
priv->orig_background = NULL;
152
if (priv->orig_color_map)
154
g_object_unref (priv->orig_color_map);
155
priv->orig_color_map = NULL;
158
if (priv->background)
160
g_object_unref (priv->background);
161
priv->background = NULL;
166
g_object_unref (priv->color_map);
167
priv->color_map = NULL;
169
priv->visible_map_pixels = NULL;
170
priv->visible_map_rowstride = 0;
173
G_OBJECT_CLASS (cc_timezone_map_parent_class)->dispose (object);
177
cc_timezone_map_finalize (GObject *object)
179
CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (object)->priv;
183
tz_db_free (priv->tzdb);
188
G_OBJECT_CLASS (cc_timezone_map_parent_class)->finalize (object);
191
/* GtkWidget functions */
193
cc_timezone_map_get_preferred_width (GtkWidget *widget,
197
/* choose a minimum size small enough to prevent the window
198
* from growing horizontally
207
cc_timezone_map_get_preferred_height (GtkWidget *widget,
211
CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv;
214
/* The + 20 here is a slight tweak to make the map fill the
215
* panel better without causing horizontal growing
217
size = 300 * gdk_pixbuf_get_height (priv->orig_background) / gdk_pixbuf_get_width (priv->orig_background) + 20;
225
cc_timezone_map_size_allocate (GtkWidget *widget,
226
GtkAllocation *allocation)
228
CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv;
230
if (priv->background)
231
g_object_unref (priv->background);
233
priv->background = gdk_pixbuf_scale_simple (priv->orig_background,
236
GDK_INTERP_BILINEAR);
239
g_object_unref (priv->color_map);
241
priv->color_map = gdk_pixbuf_scale_simple (priv->orig_color_map,
244
GDK_INTERP_BILINEAR);
246
priv->visible_map_pixels = gdk_pixbuf_get_pixels (priv->color_map);
247
priv->visible_map_rowstride = gdk_pixbuf_get_rowstride (priv->color_map);
249
GTK_WIDGET_CLASS (cc_timezone_map_parent_class)->size_allocate (widget,
254
cc_timezone_map_realize (GtkWidget *widget)
256
GdkWindowAttr attr = { 0, };
257
GtkAllocation allocation;
261
gtk_widget_get_allocation (widget, &allocation);
263
gtk_widget_set_realized (widget, TRUE);
265
attr.window_type = GDK_WINDOW_CHILD;
266
attr.wclass = GDK_INPUT_OUTPUT;
267
attr.width = allocation.width;
268
attr.height = allocation.height;
269
attr.x = allocation.x;
270
attr.y = allocation.y;
271
attr.event_mask = gtk_widget_get_events (widget)
272
| GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
274
window = gdk_window_new (gtk_widget_get_parent_window (widget), &attr,
275
GDK_WA_X | GDK_WA_Y);
277
cursor = gdk_cursor_new (GDK_HAND2);
278
gdk_window_set_cursor (window, cursor);
280
gdk_window_set_user_data (window, widget);
281
gtk_widget_set_window (widget, window);
286
convert_longtitude_to_x (gdouble longitude, gint map_width)
288
const gdouble xdeg_offset = -6;
291
x = (map_width * (180.0 + longitude) / 360.0)
292
+ (map_width * xdeg_offset / 180.0);
298
radians (gdouble degrees)
300
return (degrees / 360.0) * G_PI * 2;
304
convert_latitude_to_y (gdouble latitude, gdouble map_height)
306
gdouble bottom_lat = -59;
307
gdouble top_lat = 81;
308
gdouble top_per, y, full_range, top_offset, map_range;
310
top_per = top_lat / 180.0;
311
y = 1.25 * log (tan (G_PI_4 + 0.4 * radians (latitude)));
312
full_range = 4.6068250867599998;
313
top_offset = full_range * top_per;
314
map_range = fabs (1.25 * log (tan (G_PI_4 + 0.4 * radians (bottom_lat))) - top_offset);
315
y = fabs (y - top_offset);
323
cc_timezone_map_draw (GtkWidget *widget,
326
CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv;
327
GdkPixbuf *hilight, *orig_hilight, *pin;
331
gdouble pointx, pointy;
334
gtk_widget_get_allocation (widget, &alloc);
336
/* paint background */
337
gdk_cairo_set_source_pixbuf (cr, priv->background, 0, 0);
341
file = g_strdup_printf (DATADIR "/timezone_%s.png",
342
g_ascii_formatd (buf, sizeof (buf),
343
"%g", priv->selected_offset));
344
orig_hilight = gdk_pixbuf_new_from_file (file, &err);
350
g_warning ("Could not load hilight: %s",
351
(err) ? err->message : "Unknown Error");
353
g_clear_error (&err);
358
hilight = gdk_pixbuf_scale_simple (orig_hilight, alloc.width,
359
alloc.height, GDK_INTERP_BILINEAR);
360
gdk_cairo_set_source_pixbuf (cr, hilight, 0, 0);
363
g_object_unref (hilight);
364
g_object_unref (orig_hilight);
368
pin = gdk_pixbuf_new_from_file (DATADIR "/pin.png", &err);
372
g_warning ("Could not load pin icon: %s", err->message);
373
g_clear_error (&err);
378
pointx = convert_longtitude_to_x (priv->location->longitude, alloc.width);
379
pointy = convert_latitude_to_y (priv->location->latitude, alloc.height);
381
if (pointy > alloc.height)
382
pointy = alloc.height;
386
gdk_cairo_set_source_pixbuf (cr, pin, pointx - 8, pointy - 14);
393
g_object_unref (pin);
401
cc_timezone_map_class_init (CcTimezoneMapClass *klass)
403
GObjectClass *object_class = G_OBJECT_CLASS (klass);
404
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
406
g_type_class_add_private (klass, sizeof (CcTimezoneMapPrivate));
408
object_class->get_property = cc_timezone_map_get_property;
409
object_class->set_property = cc_timezone_map_set_property;
410
object_class->dispose = cc_timezone_map_dispose;
411
object_class->finalize = cc_timezone_map_finalize;
413
widget_class->get_preferred_width = cc_timezone_map_get_preferred_width;
414
widget_class->get_preferred_height = cc_timezone_map_get_preferred_height;
415
widget_class->size_allocate = cc_timezone_map_size_allocate;
416
widget_class->realize = cc_timezone_map_realize;
417
widget_class->draw = cc_timezone_map_draw;
419
signals[LOCATION_CHANGED] = g_signal_new ("location-changed",
420
CC_TYPE_TIMEZONE_MAP,
425
g_cclosure_marshal_VOID__POINTER,
432
sort_locations (TzLocation *a,
435
if (a->dist > b->dist)
438
if (a->dist < b->dist)
445
set_location (CcTimezoneMap *map,
446
TzLocation *location)
448
CcTimezoneMapPrivate *priv = map->priv;
451
priv->location = location;
453
info = tz_info_from_location (priv->location);
455
priv->selected_offset = tz_location_get_utc_offset (priv->location)
456
/ (60.0*60.0) + ((info->daylight) ? -1.0 : 0.0);
458
g_signal_emit (map, signals[LOCATION_CHANGED], 0, priv->location);
464
button_press_event (GtkWidget *widget,
465
GdkEventButton *event)
467
CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv;
474
const GPtrArray *array;
476
GList *distances = NULL;
483
rowstride = priv->visible_map_rowstride;
484
pixels = priv->visible_map_pixels;
486
r = pixels[(rowstride * y + x * 4)];
487
g = pixels[(rowstride * y + x * 4) + 1];
488
b = pixels[(rowstride * y + x * 4) + 2];
489
a = pixels[(rowstride * y + x * 4) + 3];
492
for (i = 0; color_codes[i].offset != -100; i++)
494
if (color_codes[i].red == r && color_codes[i].green == g
495
&& color_codes[i].blue == b && color_codes[i].alpha == a)
497
priv->selected_offset = color_codes[i].offset;
501
gtk_widget_queue_draw (widget);
503
/* work out the co-ordinates */
505
array = tz_get_locations (priv->tzdb);
507
gtk_widget_get_allocation (widget, &alloc);
509
height = alloc.height;
511
for (i = 0; i < array->len; i++)
513
gdouble pointx, pointy, dx, dy;
514
TzLocation *loc = array->pdata[i];
516
pointx = convert_longtitude_to_x (loc->longitude, width);
517
pointy = convert_latitude_to_y (loc->latitude, height);
522
loc->dist = dx * dx + dy * dy;
523
distances = g_list_prepend (distances, loc);
526
distances = g_list_sort (distances, (GCompareFunc) sort_locations);
529
set_location (CC_TIMEZONE_MAP (widget), (TzLocation*) distances->data);
531
g_list_free (distances);
537
cc_timezone_map_init (CcTimezoneMap *self)
539
CcTimezoneMapPrivate *priv;
542
priv = self->priv = TIMEZONE_MAP_PRIVATE (self);
544
priv->orig_background = gdk_pixbuf_new_from_file (DATADIR "/bg.png",
547
if (!priv->orig_background)
549
g_warning ("Could not load background image: %s",
550
(err) ? err->message : "Unknown error");
551
g_clear_error (&err);
554
priv->orig_color_map = gdk_pixbuf_new_from_file (DATADIR "/cc.png",
556
if (!priv->orig_color_map)
558
g_warning ("Could not load background image: %s",
559
(err) ? err->message : "Unknown error");
560
g_clear_error (&err);
563
priv->tzdb = tz_load_db ();
565
g_signal_connect (self, "button-press-event", G_CALLBACK (button_press_event),
570
cc_timezone_map_new (void)
572
return g_object_new (CC_TYPE_TIMEZONE_MAP, NULL);
576
cc_timezone_map_set_timezone (CcTimezoneMap *map,
577
const gchar *timezone)
579
GPtrArray *locations;
584
real_tz = tz_info_get_clean_name (map->priv->tzdb, timezone);
586
locations = tz_get_locations (map->priv->tzdb);
589
for (i = 0; i < locations->len; i++)
591
TzLocation *loc = locations->pdata[i];
593
if (!g_strcmp0 (loc->zone, real_tz ? real_tz : timezone))
595
set_location (map, loc);
602
gtk_widget_queue_draw (GTK_WIDGET (map));
610
cc_timezone_map_get_location (CcTimezoneMap *map)
612
return map->priv->location;