static TzLocation * e_tz_map_location_from_point (ETzMap *tzmap, EMapPoint *point) { TzLocation *tz_loc = NULL; TzDB *tz_db; GPtrArray *locs; double p_longitude, p_latitude; double l_longitude, l_latitude; int i; tz_db = e_tz_map_get_tz_db (tzmap); locs = tz_get_locations (tz_db); e_map_point_get_location (point, &p_longitude, &p_latitude); for (i = 0; i < locs->len; i++) { tz_location_get_position (g_ptr_array_index (locs, i), &l_longitude, &l_latitude); if (l_longitude - 0.005 <= p_longitude && l_longitude + 0.005 >= p_longitude && l_latitude - 0.005 <= p_latitude && l_latitude + 0.005 >= p_latitude) { tz_loc = g_ptr_array_index (locs, i); break; } } return (tz_loc); }
// ----------------------------------------------------------------------------- GPtrArray* tzn::getLocs() { DEBUGLOGB; GPtrArray* locs = tz_get_locations(tzdb); DEBUGLOGE; return locs; }
void e_tz_map_set_tz_from_name (ETzMap *tzmap, const gchar *name) { TzLocation *tz_loc = NULL; TzDB *tz_db; GPtrArray *locs; double l_longitude = 0.0, l_latitude = 0.0; int i; g_return_if_fail (tzmap != NULL); tz_db = e_tz_map_get_tz_db (tzmap); locs = tz_get_locations (tz_db); if (name) { for (i = 0; i < locs->len; i++) { tz_loc = g_ptr_array_index (locs, i); if (!strcmp(tz_location_get_zone(tz_loc), name)) { tz_location_get_position (tz_loc, &l_longitude, &l_latitude); break; } } } if (tzmap->point_selected) e_map_point_set_color_rgba (tzmap->map, tzmap->point_selected, TZ_MAP_POINT_NORMAL_RGBA); tzmap->point_selected = e_map_get_closest_point (tzmap->map, l_longitude, l_latitude, FALSE); e_tz_map_set_location_text (tzmap, tz_location_get_zone (e_tz_map_location_from_point (tzmap, tzmap->point_selected))); }
TzLocation * e_tz_map_get_location_by_name (ETzMap *tzmap, const gchar *name) { TzLocation *tz_loc = NULL; TzDB *tz_db; GPtrArray *locs; int i; tz_db = e_tz_map_get_tz_db (tzmap); locs = tz_get_locations (tz_db); for (i = 0; i < locs->len; i++) { TzLocation *tz_loc_temp; tz_loc_temp = g_ptr_array_index (locs, i); if (!strcmp (tz_location_get_zone (tz_loc_temp), name)) { tz_loc = tz_loc_temp; break; } } return tz_loc; }
gboolean cc_timezone_map_set_timezone (CcTimezoneMap *map, const gchar *timezone) { GPtrArray *locations; guint i; char *real_tz; gboolean ret; real_tz = tz_info_get_clean_name (map->priv->tzdb, timezone); locations = tz_get_locations (map->priv->tzdb); ret = FALSE; for (i = 0; i < locations->len; i++) { TzLocation *loc = locations->pdata[i]; if (!g_strcmp0 (loc->zone, real_tz ? real_tz : timezone)) { set_location (map, loc); ret = TRUE; break; } } if (ret) gtk_widget_queue_draw (GTK_WIDGET (map)); g_free (real_tz); return ret; }
ETzMap * e_tz_map_new (GstTool *tool) { ETzMap *tzmap; GPtrArray *locs; TzLocation *tzl; GtkWidget *location_combo; int i; tzmap = g_new0 (ETzMap, 1); tzmap->tool = GST_TOOL (tool); tzmap->tzdb = tz_load_db (); if (!tzmap->tzdb) g_error ("Unable to load system timezone database."); tzmap->map = e_map_new (); if (!tzmap->map) g_error ("Unable to create map widget."); gtk_widget_set_events (GTK_WIDGET (tzmap->map), gtk_widget_get_events (GTK_WIDGET (tzmap->map)) | GDK_LEAVE_NOTIFY_MASK | GDK_VISIBILITY_NOTIFY_MASK); locs = tz_get_locations (tzmap->tzdb); for (i = 0; i < locs->len; i++) { tzl = g_ptr_array_index (locs, i); e_map_add_point (tzmap->map, NULL, tzl->longitude, tzl->latitude, TZ_MAP_POINT_NORMAL_RGBA); } g_signal_connect (G_OBJECT (tzmap->map), "map-event", G_CALLBACK (map), tzmap); g_signal_connect (G_OBJECT (tzmap->map), "unmap-event", G_CALLBACK (unmap), tzmap); g_signal_connect(G_OBJECT (tzmap->map), "motion-notify-event", G_CALLBACK (motion), (gpointer) tzmap); g_signal_connect(G_OBJECT(tzmap->map), "button-press-event", G_CALLBACK (button_pressed), (gpointer) tzmap); g_signal_connect(G_OBJECT (tzmap->map), "enter-notify-event", G_CALLBACK (in_map), (gpointer) tzmap); g_signal_connect(G_OBJECT (tzmap->map), "leave-notify-event", G_CALLBACK (out_map), (gpointer) tzmap); location_combo = gst_dialog_get_widget (tzmap->tool->main_dialog, "location_combo"); g_signal_connect (G_OBJECT (location_combo), "changed", G_CALLBACK (update_map), (gpointer) tzmap); return tzmap; }
// ----------------------------------------------------------------------------- bool tzn::got(long tm_gmtoff, TzLocation& locret, TzInfo& nforet) { DEBUGLOGB; int fnd = 0; bool ret = false; DEBUGLOGP("requested entry gmt offset is %d\n", (int)tm_gmtoff); GPtrArray* locs = tz_get_locations(tzdb); if( locs ) { TzInfo* nfo; TzLocation* loc; for( size_t i = 0; i < locs->len; i++ ) { if( loc = (TzLocation*)locs->pdata[i] ) { if( nfo = tz_info_from_location(loc) ) { if( nfo->utc_offset == tm_gmtoff ) { DEBUGLOGP("%s/%s/%s\n", loc->country, loc->zone, loc->comment); locret = *loc; nforet = *nfo; ret = true; fnd++; } tz_info_free(nfo); if( fnd > 1 ) break; // if( ret ) break; } } } } DEBUGLOGE; return ret && fnd == 1; }
// ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- bool tzn::got(const char* tzcode, TzLocation& locret, TzInfo& nforet, bool exact) { DEBUGLOGB; int fnd = 0; bool ret = false; DEBUGLOGP("requested entry tzcode is %s, exact is %s\n", tzcode, exact ? "on" : "off"); GPtrArray* locs = tz_get_locations(tzdb); if( locs ) { char* tzn = tz_info_get_clean_name(tzdb, tzcode); const char* tzs = tzn ? tzn : (!exact ? tzcode : NULL); if( tzs ) { DEBUGLOGP("requested entry tzcode (cleaned) is %s\n", tzs); TzInfo* nfo; TzLocation* loc; bool got; bool cont_ok, city_ok; const char* cont_db, *city_db; const char* cont_in = strchr (tzs, '/'); // end of continent string for search string const char* city_in = strrchr(tzs, '/'); // begin of city string for search string int cont_il = cont_in ? cont_in-tzs : 0; int city_il = city_in ? strlen(city_in) : 0; DEBUGLOGP("cont_in is %s\n", cont_in); DEBUGLOGP("city_in is %s\n", city_in); if( !exact || (exact && g_quark_try_string(tzs)) ) // NOTE: according to tz.c comments, this will work { for( size_t i = 0; i < locs->len; i++ ) { if( !(loc = (TzLocation*)locs->pdata[i]) ) continue; if( !(nfo = tz_info_from_location(loc)) ) continue; if( !(got = stricmp(loc->zone, tzs) == 0) && !exact ) { cont_db = strchr (loc->zone, '/'); // end of continent string in db city_db = strrchr(loc->zone, '/'); // begin of city string in db if( cont_db != city_db ) { // just compare the 1st and last partitions, i.e., continent and city, // disregarding the 'middle' part, i.e., country or state DEBUGLOGS("tzcode (cleaned) not found and exact is off"); DEBUGLOGS("comparing just the continent and city strings"); DEBUGLOGP("cont_db is %s\n", cont_db); DEBUGLOGP("city_db is %s\n", city_db); cont_ok = cont_in && cont_db && cont_il == (cont_db-loc->zone) && strnicmp(tzs, loc->zone, cont_il) == 0; city_ok = city_in && city_db && city_il == strlen(city_db) && strnicmp(city_in, city_db, city_il) == 0; got = cont_ok && city_ok; DEBUGLOGP("cont_ok is %s, city_ok is %s\n", cont_ok ? "on" : "off", city_ok ? "on" : "off"); } } if( got ) { // DEBUGLOGP("%s/%s/%s\n", loc->country, loc->zone, loc->comment); DEBUGLOGP("%s\n", loc->zone); locret = *loc; nforet = *nfo; ret = true; fnd++; } tz_info_free(nfo); if( fnd > 1 ) break; // if( ret ) break; } } #ifdef DEBUGLOG else DEBUGLOGS("requested entry tzcode (cleaned) not found"); #endif } if( tzn ) g_free(tzn); } DEBUGLOGE; return ret && fnd == 1; }
static gboolean button_press_event (GtkWidget *widget, GdkEventButton *event) { CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv; gint x, y; guchar r, g, b, a; guchar *pixels; gint rowstride; gsize i; const GPtrArray *array; gint width, height; GList *distances = NULL; GtkAllocation alloc; x = event->x; y = event->y; rowstride = priv->visible_map_rowstride; pixels = priv->visible_map_pixels; r = pixels[(rowstride * y + x * 4)]; g = pixels[(rowstride * y + x * 4) + 1]; b = pixels[(rowstride * y + x * 4) + 2]; a = pixels[(rowstride * y + x * 4) + 3]; for (i = 0; color_codes[i].offset != -100; i++) { if (color_codes[i].red == r && color_codes[i].green == g && color_codes[i].blue == b && color_codes[i].alpha == a) { priv->selected_offset = color_codes[i].offset; } } gtk_widget_queue_draw (widget); /* work out the co-ordinates */ array = tz_get_locations (priv->tzdb); gtk_widget_get_allocation (widget, &alloc); width = alloc.width; height = alloc.height; for (i = 0; i < array->len; i++) { gdouble pointx, pointy, dx, dy; TzLocation *loc = array->pdata[i]; pointx = convert_longtitude_to_x (loc->longitude, width); pointy = convert_latitude_to_y (loc->latitude, height); dx = pointx - x; dy = pointy - y; loc->dist = dx * dx + dy * dy; distances = g_list_prepend (distances, loc); } distances = g_list_sort (distances, (GCompareFunc) sort_locations); set_location (CC_TIMEZONE_MAP (widget), (TzLocation*) distances->data); g_list_free (distances); return TRUE; }