From: Bluezery <ohpowel@gmail.com>
[framework/uifw/elementary.git] / src / lib / elm_map.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4
5 #include "Elementary.h"
6 #include "elm_priv.h"
7 #include "els_scroller.h"
8
9 #ifdef HAVE_ELEMENTARY_ECORE_CON
10
11 typedef struct _Widget_Data Widget_Data;
12 typedef struct _Pan Pan;
13 typedef struct _Grid Grid;
14 typedef struct _Grid_Item Grid_Item;
15 typedef struct _Marker_Group Marker_Group;
16 typedef struct _Event Event;
17 typedef struct _Path_Node Path_Node;
18 typedef struct _Path_Waypoint Path_Waypoint;
19 typedef struct _Url_Data Url_Data;
20 typedef struct _Route_Dump Route_Dump;
21 typedef struct _Name_Dump Name_Dump;
22 typedef struct _Track_Dump Track_Dump;
23
24 #define CACHE_ROOT_PATH   "/tmp/elm_map"
25 #define CACHE_PATH        CACHE_ROOT_PATH"/%d/%d/%d"
26 #define CACHE_FILE_PATH   "%s/%d.png"
27 #define DEST_ROUTE_XML_FILE "/tmp/elm_map-route-XXXXXX"
28 #define DEST_NAME_XML_FILE "/tmp/elm_map-name-XXXXXX"
29
30 #define ROUTE_YOURS_URL "http://www.yournavigation.org/api/dev/route.php"
31 #define ROUTE_TYPE_MOTORCAR "motocar"
32 #define ROUTE_TYPE_BICYCLE "bicycle"
33 #define ROUTE_TYPE_FOOT "foot"
34 #define YOURS_DISTANCE "distance"
35 #define YOURS_DESCRIPTION "description"
36 #define YOURS_COORDINATES "coordinates"
37
38 // TODO: fix monav & ors url
39 #define ROUTE_MONAV_URL "http://"
40 #define ROUTE_ORS_URL "http:///"
41
42 #define NAME_NOMINATIM_URL "http://nominatim.openstreetmap.org"
43 #define NOMINATIM_RESULT "result"
44 #define NOMINATIM_PLACE "place"
45 #define NOMINATIM_ATTR_LON "lon"
46 #define NOMINATIM_ATTR_LAT "lat"
47
48 #define PINCH_ZOOM_MIN 0.25
49 #define PINCH_ZOOM_MAX 4.0
50 #define MAX_CONCURRENT_DOWNLOAD 10
51
52 #define GPX_NAME "name>"
53 #define GPX_COORDINATES "trkpt "
54 #define GPX_LON "lon"
55 #define GPX_LAT "lat"
56 #define GPX_ELE "ele>"
57 #define GPX_TIME "time>"
58
59 // Map sources
60 // Currently the size of a tile must be 256*256
61 // and the size of the map must be pow(2.0, z)*tile_size
62 typedef struct _Map_Sources_Tab
63 {
64    const char *name;
65    int zoom_min;
66    int zoom_max;
67    ElmMapModuleUrlFunc url_cb;
68    Elm_Map_Route_Sources route_source;
69    ElmMapModuleRouteUrlFunc route_url_cb;
70    ElmMapModuleNameUrlFunc name_url_cb;
71    ElmMapModuleGeoIntoCoordFunc geo_into_coord;
72    ElmMapModuleCoordIntoGeoFunc coord_into_geo;
73 } Map_Sources_Tab;
74
75 //Zemm min is supposed to be 0
76 static char *_mapnik_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom);
77 static char *_osmarender_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom);
78 static char *_cyclemap_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom);
79 static char *_mapquest_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom);
80 static char *_mapquest_aerial_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom);
81
82 static char *_yours_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat);
83 /*
84 static char *_monav_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat)
85 static char *_ors_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat);
86  */
87 static char *_nominatim_url_cb(Evas_Object *obj, int method, char *name, double lon, double lat);
88
89 static Map_Sources_Tab default_map_sources_tab[] =
90 {
91      {"Mapnik", 0, 18, _mapnik_url_cb, ELM_MAP_ROUTE_SOURCE_YOURS, _yours_url_cb, _nominatim_url_cb, NULL, NULL},
92      {"Osmarender", 0, 17, _osmarender_url_cb, ELM_MAP_ROUTE_SOURCE_YOURS, _yours_url_cb, _nominatim_url_cb, NULL, NULL},
93      {"CycleMap", 0, 16, _cyclemap_url_cb, ELM_MAP_ROUTE_SOURCE_YOURS, _yours_url_cb, _nominatim_url_cb, NULL, NULL},
94      {"MapQuest", 0, 18, _mapquest_url_cb, ELM_MAP_ROUTE_SOURCE_YOURS, _yours_url_cb, _nominatim_url_cb, NULL, NULL},
95      {"MapQuest Open Aerial", 0, 11, _mapquest_aerial_url_cb, ELM_MAP_ROUTE_SOURCE_YOURS, _yours_url_cb, _nominatim_url_cb, NULL, NULL},
96 };
97
98 struct _Url_Data
99 {
100    Ecore_Con_Url *con_url;
101
102    FILE *fd;
103    char *fname;
104 };
105
106 struct _Elm_Map_Marker_Class
107 {
108    const char *style;
109    int zoom_displayed;
110
111    struct _Elm_Map_Marker_Class_Func {
112         ElmMapMarkerGetFunc get;
113         ElmMapMarkerDelFunc del; //if NULL the object will be destroyed with evas_object_del()
114         ElmMapMarkerIconGetFunc icon_get;
115    } func;
116
117    struct { //this part is private, do not modify these values
118         Eina_Bool set : 1;
119         Evas_Coord edje_w, edje_h;
120    } priv;
121 };
122
123 struct _Elm_Map_Marker
124 {
125    Widget_Data *wd;
126    Elm_Map_Marker_Class *clas;
127    Elm_Map_Group_Class *clas_group;
128    double longitude, latitude;
129
130    Evas_Coord map_size;
131    Evas_Coord *x, *y;
132    void *data;
133    Marker_Group **groups;
134    Evas_Object *content;
135 };
136
137 struct _Elm_Map_Group_Class
138 {
139    const char *style;
140    void *data;
141    int zoom_displayed; // display the group if the zoom is >= to zoom_display
142    int zoom_grouped; // group the markers only if the zoom is <= to zoom_groups
143    Eina_Bool hide : 1;
144
145    struct {
146         ElmMapGroupIconGetFunc icon_get;
147    } func;
148
149    struct { //this part is private, do not modify these values
150         Eina_Bool set : 1;
151         Evas_Coord edje_w, edje_h;
152         Evas_Coord edje_max_w, edje_max_h;
153
154         Eina_List *objs_used;
155         Eina_List *objs_notused;
156    } priv;
157 };
158
159 struct _Marker_Group
160 {
161    Widget_Data *wd;
162    Eina_Matrixsparse_Cell *cell;
163    Elm_Map_Group_Class *clas;
164
165    Eina_List *markers;
166    long long sum_x, sum_y;
167    Evas_Coord x, y;
168    Evas_Coord w, h;
169
170    Evas_Object *obj, *bubble, *sc, *bx, *rect;
171    Eina_Bool open : 1;
172    Eina_Bool bringin : 1;
173    Eina_Bool update_nbelems : 1;
174    Eina_Bool update_resize : 1;
175    Eina_Bool update_raise : 1;
176    Eina_Bool delete_object : 1;
177 };
178
179 struct _Elm_Map_Route
180 {
181    Widget_Data *wd;
182
183    Path_Node *n;
184    Path_Waypoint *w;
185    Ecore_Con_Url *con_url;
186
187    int type;
188    int method;
189    int x, y;
190    double flon, flat, tlon, tlat;
191
192    Eina_List *nodes, *path;
193    Eina_List *waypoint;
194
195    struct {
196       int node_count;
197       int waypoint_count;
198       const char *nodes;
199       const char *waypoints;
200       double distance; /* unit : km */
201    } info;
202
203    Eina_List *handlers;
204    Url_Data ud;
205
206    struct {
207       int r;
208       int g;
209       int b;
210       int a;
211    } color;
212
213    Eina_Bool inbound : 1;
214 };
215
216 struct _Path_Node
217 {
218    Widget_Data *wd;
219
220    int idx;
221    struct {
222       double lon, lat;
223       char *address;
224    } pos;
225 };
226
227 struct _Path_Waypoint
228 {
229    Widget_Data *wd;
230
231    const char *point;
232 };
233
234 struct _Elm_Map_Name
235 {
236    Widget_Data *wd;
237
238    Ecore_Con_Url *con_url;
239    int method;
240    char *address;
241    double lon, lat;
242    Url_Data ud;
243    Ecore_Event_Handler *handler;
244 };
245
246 struct _Grid_Item
247 {
248    Widget_Data *wd;
249    Grid *g;
250    int zoom;
251    Evas_Object *img;
252    //Evas_Object *txt;
253    const char *file;
254    const char *source;
255    struct {
256         int x, y, w, h;
257    } src, out;
258
259    Eina_Bool file_have : 1;
260    Ecore_File_Download_Job *job;
261    int try_num;
262 };
263
264 struct _Grid
265 {
266    Widget_Data *wd;
267    int tsize; // size of tile (tsize x tsize pixels)
268    int zoom; // zoom level tiles want for optimal display (1, 2, 4, 8)
269    int iw, ih; // size of image in pixels
270    int w, h; // size of grid image in pixels (represented by grid)
271    int gw, gh; // size of grid in tiles
272    Eina_Matrixsparse *grid;
273 };
274
275 struct _Widget_Data
276 {
277    Evas_Object *obj;
278    Evas_Object *scr;
279    Evas_Object *ges;
280    Evas_Object *pan_smart;
281    Evas_Object *rect;
282    Evas_Object *sep_maps_markers; //map objects are below this object and marker objects are on top
283    Pan *pan;
284    Evas_Coord pan_x, pan_y, minw, minh;
285
286    int id;
287    int zoom;
288    int zoom_method;
289    Elm_Map_Zoom_Mode mode;
290
291    Ecore_Job *calc_job;
292    Ecore_Timer *scr_timer;
293    Ecore_Timer *long_timer;
294    Ecore_Animator *zoom_animator;
295    double t;
296    struct {
297         int w, h;
298         int ow, oh, nw, nh;
299         struct {
300              double x, y;
301         } spos;
302    } size;
303    struct {
304         Eina_Bool show : 1;
305         Evas_Coord x, y ,w ,h;
306    } show;
307    int tsize;
308    int nosmooth;
309    Eina_List *grids;
310    Eina_Bool resized : 1;
311    Eina_Bool on_hold : 1;
312    Eina_Bool paused : 1;
313    Eina_Bool paused_markers : 1;
314
315    struct {
316         Eina_Bool enabled;
317         double lon, lat;
318    } center_on;
319
320    Ecore_Job *markers_place_job;
321    Eina_Matrixsparse **markers;
322    Eina_List *cells_displayed; // list of Eina_Matrixsparse_Cell
323    Evas_Coord markers_max_num;
324    int marker_max_w, marker_max_h;
325    int marker_zoom;
326    Eina_List *opened_bubbles; //opened bubbles, list of Map_Group *
327
328    Eina_List *groups_clas; // list of Elm_Map_Group_Class*
329    Eina_List *markers_clas; // list of Elm_Map_Markers_Class*
330
331    Elm_Map_Route_Sources route_source;
332    Eina_List *s_event_list;
333    int try_num;
334    int finish_num;
335
336    Eina_Hash *ua;
337    const char *user_agent;
338    Eina_List *route;
339    Eina_List *track;
340    Evas_Event_Mouse_Down ev;
341    Eina_List *names;
342    int multi_count;
343
344    struct {
345         Evas_Coord cx, cy;
346         double level, diff;
347    } pinch;
348
349    struct {
350         Evas_Coord cx, cy;
351         double a, d;
352    } rotate;
353
354    int wheel_diff;
355    Eina_Bool wheel_disabled : 1;
356    Eina_Bool scr_started : 1;
357
358    Eina_Array *modules;
359    Eina_List *map_sources_tab;
360    const char **source_names;
361    Evas_Map *map;
362    Ecore_Timer *zoom_timer;
363    Map_Sources_Tab *src;
364    const char *gpx_file;
365    int zoom_min, zoom_max;
366    Eina_List *download_list;
367    int download_num;
368 };
369
370 struct _Pan
371 {
372    Evas_Object_Smart_Clipped_Data __clipped_data;
373    Widget_Data *wd;
374 };
375
376 struct _Event
377 {
378    int device;
379
380    struct {
381         Evas_Coord x, y;
382    } start;
383
384    struct {
385         Evas_Coord x, y;
386    } prev;
387
388    Evas_Coord x, y, w, h;
389
390    Evas_Object *object;
391
392    int pinch_start_dis;
393    int pinch_dis;
394 };
395
396 struct _Route_Dump
397 {
398    int id;
399    char *fname;
400    double distance;
401    char *description;
402    char *coordinates;
403 };
404
405 enum _Route_Xml_Attribute
406 {
407    ROUTE_XML_NONE,
408    ROUTE_XML_DISTANCE,
409    ROUTE_XML_DESCRIPTION,
410    ROUTE_XML_COORDINATES,
411    ROUTE_XML_LAST
412 } Route_Xml_Attibute;
413
414 struct _Name_Dump
415 {
416    int id;
417    char *address;
418    double lon;
419    double lat;
420 };
421
422 enum _Name_Xml_Attribute
423 {
424    NAME_XML_NONE,
425    NAME_XML_NAME,
426    NAME_XML_LON,
427    NAME_XML_LAT,
428    NAME_XML_LAST
429 } Name_Xml_Attibute;
430
431 enum _Zoom_Method
432 {
433    ZOOM_METHOD_NONE,
434    ZOOM_METHOD_IN,
435    ZOOM_METHOD_OUT,
436    ZOOM_METHOD_LAST
437 } Zoom_Mode;
438
439 enum _Track_Xml_Attribute
440 {
441    TRACK_XML_NONE,
442    TRACK_XML_COORDINATES,
443    TRACK_XML_LAST
444 } Track_Xml_Attibute;
445
446 static Evas_Smart_Class _pan_sc = EVAS_SMART_CLASS_INIT_NULL;
447 static const char *widtype = NULL;
448 static int idnum = 1;
449
450 static const char SIG_CHANGED[] = "changed";
451 static const char SIG_CLICKED[] = "clicked";
452 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
453 static const char SIG_LOADED_DETAIL[] = "loaded,detail";
454 static const char SIG_LOAD_DETAIL[] = "load,detail";
455 static const char SIG_LONGPRESSED[] = "longpressed";
456 static const char SIG_PRESS[] = "press";
457 static const char SIG_SCROLL[] = "scroll";
458 static const char SIG_SCROLL_DRAG_START[] = "scroll,drag,start";
459 static const char SIG_SCROLL_DRAG_STOP[] = "scroll,drag,stop";
460 static const char SIG_ZOOM_CHANGE[] = "zoom,change";
461 static const char SIG_ZOOM_START[] = "zoom,start";
462 static const char SIG_ZOOM_STOP[] = "zoom,stop";
463 static const char SIG_DOWNLOADED[] = "downloaded";
464 static const char SIG_ROUTE_LOAD[] = "route,load";
465 static const char SIG_ROUTE_LOADED[] = "route,loaded";
466 static const char SIG_NAME_LOAD[] = "name,load";
467 static const char SIG_NAME_LOADED[] = "name,loaded";
468 static const Evas_Smart_Cb_Description _signals[] = {
469        {SIG_CHANGED, ""},
470        {SIG_CLICKED, ""},
471        {SIG_CLICKED_DOUBLE, ""},
472        {SIG_LOADED_DETAIL, ""},
473        {SIG_LOAD_DETAIL, ""},
474        {SIG_LONGPRESSED, ""},
475        {SIG_PRESS, ""},
476        {SIG_SCROLL, ""},
477        {SIG_SCROLL_DRAG_START, ""},
478        {SIG_SCROLL_DRAG_STOP, ""},
479        {SIG_ZOOM_CHANGE, ""},
480        {SIG_ZOOM_START, ""},
481        {SIG_ZOOM_STOP, ""},
482        {SIG_DOWNLOADED, ""},
483        {SIG_ROUTE_LOAD, ""},
484        {SIG_ROUTE_LOADED, ""},
485        {SIG_NAME_LOAD, ""},
486        {SIG_NAME_LOADED, ""},
487        {NULL, NULL}
488 };
489
490 static void _pan_calculate(Evas_Object *obj);
491
492 static void _rect_resize_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
493 static void _del_hook(Evas_Object *obj);
494 static void _theme_hook(Evas_Object *obj);
495 static void _on_focus_hook(void *data, Evas_Object *obj);
496 static void _sizing_eval(Evas_Object *obj);
497 static void _calc_job(void *data);
498 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
499                              Evas_Callback_Type type, void *event_info);
500 static void grid_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh);
501 static void grid_load(Evas_Object *obj, Grid *g);
502
503 static void _process_download_list(Evas_Object *obj);
504 static void _add_download_list(Evas_Object *obj, Grid_Item *gi);
505
506 static void _group_object_create(Marker_Group *group);
507 static void _group_object_free(Marker_Group *group);
508 static void _group_open_cb(void *data, Evas_Object *obj, const char *emission, const char *soure);
509 static void _group_bringin_cb(void *data, Evas_Object *obj, const char *emission, const char *soure);
510 static void _group_bubble_create(Marker_Group *group);
511 static void _group_bubble_free(Marker_Group *group);
512 static void _group_bubble_place(Marker_Group *group);
513
514 static int _group_bubble_content_update(Marker_Group *group);
515 static void _group_bubble_content_free(Marker_Group *group);
516 static void marker_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh);
517 static void _bubble_sc_hints_changed_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
518
519 static void _mouse_down(void *data, Evas *evas, Evas_Object *obj, void *event_info);
520 static void _mouse_up(void *data, Evas *evas, Evas_Object *obj, void *event_info);
521
522 static void route_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh);
523 static void track_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh);
524
525 static Eina_Bool
526 module_list_cb(Eina_Module *m, void *data)
527 {
528    ELM_CHECK_WIDTYPE(data, widtype) EINA_FALSE;
529    Widget_Data *wd = elm_widget_data_get(data);
530    Map_Sources_Tab *s;
531    ElmMapModuleSourceFunc source;
532    ElmMapModuleZoomMinFunc zoom_min;
533    ElmMapModuleZoomMaxFunc zoom_max;
534    ElmMapModuleUrlFunc url;
535    ElmMapModuleRouteSourceFunc route_source;
536    ElmMapModuleRouteUrlFunc route_url;
537    ElmMapModuleNameUrlFunc name_url;
538    ElmMapModuleGeoIntoCoordFunc geo_into_coord;
539    ElmMapModuleCoordIntoGeoFunc coord_into_geo;
540    const char *file;
541
542    if (!wd) return EINA_FALSE;
543
544    file = eina_module_file_get(m);
545    if (!eina_module_load(m))
546      {
547         ERR("could not load module \"%s\": %s", file, eina_error_msg_get(eina_error_get()));
548         return EINA_FALSE;
549      }
550
551    source = eina_module_symbol_get(m, "map_module_source_get");
552    zoom_min = eina_module_symbol_get(m, "map_module_zoom_min_get");
553    zoom_max = eina_module_symbol_get(m, "map_module_zoom_max_get");
554    url = eina_module_symbol_get(m, "map_module_url_get");
555    route_source = eina_module_symbol_get(m, "map_module_route_source_get");
556    route_url = eina_module_symbol_get(m, "map_module_route_url_get");
557    name_url = eina_module_symbol_get(m, "map_module_name_url_get");
558    geo_into_coord = eina_module_symbol_get(m, "map_module_geo_into_coord");
559    coord_into_geo = eina_module_symbol_get(m, "map_module_coord_into_geo");
560    if ((!source) || (!zoom_min) || (!zoom_max) || (!url) || (!route_source) || (!route_url) || (!name_url) || (!geo_into_coord) || (!coord_into_geo))
561      {
562         WRN("could not find map_module_source_get() in module \"%s\": %s", file, eina_error_msg_get(eina_error_get()));
563         eina_module_unload(m);
564         return EINA_FALSE;
565      }
566    s = calloc(1, sizeof(Map_Sources_Tab));
567    EINA_SAFETY_ON_NULL_RETURN_VAL(s, EINA_FALSE);
568    s->name = source();
569    s->zoom_min = zoom_min();
570    s->zoom_max = zoom_max();
571    s->url_cb = url;
572    s->route_source = route_source();
573    s->route_url_cb = route_url;
574    s->name_url_cb = name_url;
575    s->geo_into_coord = geo_into_coord;
576    s->coord_into_geo = coord_into_geo;
577    wd->map_sources_tab = eina_list_append(wd->map_sources_tab, s);
578
579    return EINA_TRUE;
580 }
581
582 static void
583 module_init(void *data)
584 {
585    ELM_CHECK_WIDTYPE(data, widtype);
586    Widget_Data *wd = elm_widget_data_get(data);
587
588    if (!wd) return;
589    wd->modules = eina_module_list_get(wd->modules, MODULES_PATH, 1, &module_list_cb, data);
590 }
591
592 static void
593 source_init(void *data)
594 {
595    ELM_CHECK_WIDTYPE(data, widtype);
596    Widget_Data *wd = elm_widget_data_get(data);
597    Map_Sources_Tab *s;
598    Eina_List *l;
599    unsigned int idx;
600
601    if (!wd) return;
602    for (idx = 0; idx < sizeof(default_map_sources_tab)/sizeof(Map_Sources_Tab); idx++)
603      {
604         s = calloc(1, sizeof(Map_Sources_Tab));
605         EINA_SAFETY_ON_NULL_RETURN(s);
606         s->name = default_map_sources_tab[idx].name;
607         s->zoom_min = default_map_sources_tab[idx].zoom_min;
608         s->zoom_max = default_map_sources_tab[idx].zoom_max;
609         s->url_cb = default_map_sources_tab[idx].url_cb;
610         s->route_source = default_map_sources_tab[idx].route_source;
611         s->route_url_cb = default_map_sources_tab[idx].route_url_cb;
612         s->name_url_cb = default_map_sources_tab[idx].name_url_cb;
613         s->geo_into_coord = default_map_sources_tab[idx].geo_into_coord;
614         s->coord_into_geo = default_map_sources_tab[idx].coord_into_geo;
615         wd->map_sources_tab = eina_list_append(wd->map_sources_tab, s);
616         if (!idx) wd->src = s;
617      }
618    module_init(data);
619
620    int n = eina_list_count(wd->map_sources_tab);
621    wd->source_names = malloc(sizeof(char *) * (n + 1));
622    if (!wd->source_names)
623      {
624         ERR("init source names failed.");
625         return;
626      }
627    idx = 0;
628    EINA_LIST_FOREACH(wd->map_sources_tab, l, s)
629      {
630         wd->source_names[idx] = strdup(s->name);
631         INF("source : %s", wd->source_names[idx]);
632         if (s->zoom_min < wd->zoom_min) wd->zoom_min = s->zoom_min;
633         if (s->zoom_max > wd->zoom_max) wd->zoom_max = s->zoom_max;
634         idx++;
635      }
636    wd->source_names[idx] = NULL;
637 }
638
639 static void
640 obj_rotate_zoom(void *data, Evas_Object *obj)
641 {
642    ELM_CHECK_WIDTYPE(data, widtype);
643    Widget_Data *wd = elm_widget_data_get(data);
644    int ow, oh, iw, ih;
645    if ((!wd->pinch.cx) && (!wd->pinch.cy))
646      {
647         wd->pinch.cx = wd->rotate.cx;
648         wd->pinch.cy = wd->rotate.cy;
649      }
650
651    evas_map_util_points_populate_from_object_full(wd->map, obj, 0);
652    evas_object_image_size_get(obj, &iw, &ih);
653    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
654    if ((ow < iw) || (oh < ih))
655      {
656         ow *= (double)iw / ow;
657         oh *= (double)ih / oh;
658         evas_map_point_image_uv_set(wd->map, 1, ow, 0);
659         evas_map_point_image_uv_set(wd->map, 2, ow, oh);
660         evas_map_point_image_uv_set(wd->map, 3, 0, oh);
661      }
662    evas_map_util_zoom(wd->map, wd->pinch.level, wd->pinch.level, wd->pinch.cx, wd->pinch.cy);
663    evas_map_util_rotate(wd->map, wd->rotate.d, wd->rotate.cx, wd->rotate.cy);
664    evas_object_map_enable_set(obj, EINA_TRUE);
665    evas_object_map_set(obj, wd->map);
666 }
667
668 static void
669 #ifdef ELM_EMAP
670 track_place(Evas_Object *obj, Grid *g __UNUSED__, Evas_Coord px, Evas_Coord py, Evas_Coord ox __UNUSED__, Evas_Coord oy __UNUSED__, Evas_Coord ow, Evas_Coord oh)
671 #else
672 track_place(Evas_Object *obj __UNUSED__, Grid *g __UNUSED__, Evas_Coord px __UNUSED__, Evas_Coord py __UNUSED__, Evas_Coord ox __UNUSED__, Evas_Coord oy __UNUSED__, Evas_Coord ow __UNUSED__, Evas_Coord oh __UNUSED__)
673 #endif
674 {
675 #ifdef ELM_EMAP
676    ELM_CHECK_WIDTYPE(obj, widtype);
677    Widget_Data *wd = elm_widget_data_get(obj);
678    Eina_List *l;
679    Evas_Object *route;
680    int xmin, xmax, ymin, ymax;
681
682    if (!wd) return;
683    Evas_Coord size = pow(2.0, wd->zoom)*wd->tsize;
684
685    EINA_LIST_FOREACH(wd->track, l, route)
686      {
687         elm_map_utils_convert_geo_into_coord(wd->obj, elm_route_lon_min_get(route), elm_route_lat_max_get(route), size, &xmin, &ymin);
688         elm_map_utils_convert_geo_into_coord(wd->obj, elm_route_lon_max_get(route), elm_route_lat_min_get(route), size, &xmax, &ymax);
689
690         if( !(xmin < px && xmax < px) && !(xmin > px+ow && xmax > px+ow))
691         {
692            if( !(ymin < py && ymax < py) && !(ymin > py+oh && ymax > py+oh))
693            {
694               //display the route
695               evas_object_move(route, xmin - px + ox, ymin - py + oy);
696               evas_object_resize(route, xmax - xmin, ymax - ymin);
697
698               evas_object_raise(route);
699               obj_rotate_zoom(obj, route);
700               evas_object_show(route);
701
702               continue;
703            }
704         }
705         //the route is not display
706         evas_object_hide(route);
707      }
708 #endif
709 }
710 static void
711 route_place(Evas_Object *obj, Grid *g __UNUSED__, Evas_Coord px, Evas_Coord py, Evas_Coord ox __UNUSED__, Evas_Coord oy __UNUSED__, Evas_Coord ow, Evas_Coord oh)
712 {
713    ELM_CHECK_WIDTYPE(obj, widtype);
714    Widget_Data *wd = elm_widget_data_get(obj);
715    Eina_List *lr, *lp, *ln;
716    Path_Node *n;
717    Evas_Object *p;
718    Elm_Map_Route *r;
719    int nodes;
720    int x, y, rx, ry;
721    double a;
722
723    if (!wd) return;
724    Evas_Coord size = pow(2.0, wd->zoom)*wd->tsize;
725
726    EINA_LIST_FOREACH(wd->route, lr, r)
727      {
728         EINA_LIST_FOREACH(r->path, lp, p)
729           {
730              evas_object_polygon_points_clear(p);
731           }
732
733         evas_object_geometry_get(wd->rect, &rx, &ry, NULL, NULL);
734         nodes = eina_list_count(r->nodes);
735
736         EINA_LIST_FOREACH(r->nodes, ln, n)
737           {
738              if ((!wd->zoom) || ((n->idx) &&
739                  ((n->idx % (int)ceil((double)nodes/(double)size*100.0))))) continue;
740              if (r->inbound)
741                {
742                   elm_map_utils_convert_geo_into_coord(wd->obj, n->pos.lon, n->pos.lat, size, &x, &y);
743                   if ((x >= px - ow) && (x <= (px + ow*2)) &&
744                       (y >= py - oh) && (y <= (py + oh*2)))
745                     {
746                        x = x - px + rx;
747                        y = y - py + ry;
748
749                        p = eina_list_nth(r->path, n->idx);
750                        a = (double)(y - r->y) / (double)(x - r->x);
751                        if ((abs(a) >= 1) || (r->x == x))
752                          {
753                             evas_object_polygon_point_add(p, r->x - 3, r->y);
754                             evas_object_polygon_point_add(p, r->x + 3, r->y);
755                             evas_object_polygon_point_add(p, x + 3, y);
756                             evas_object_polygon_point_add(p, x - 3, y);
757                          }
758                        else
759                          {
760                             evas_object_polygon_point_add(p, r->x, r->y - 3);
761                             evas_object_polygon_point_add(p, r->x, r->y + 3);
762                             evas_object_polygon_point_add(p, x, y + 3);
763                             evas_object_polygon_point_add(p, x, y - 3);
764                          }
765
766                        evas_object_color_set(p, r->color.r, r->color.g, r->color.b, r->color.a);
767                        evas_object_raise(p);
768                        obj_rotate_zoom(obj, p);
769                        evas_object_show(p);
770                        r->x = x;
771                        r->y = y;
772                     }
773                   else r->inbound = EINA_FALSE;
774                }
775              else
776                {
777                   elm_map_utils_convert_geo_into_coord(wd->obj, n->pos.lon, n->pos.lat, size, &x, &y);
778                   if ((x >= px - ow) && (x <= (px + ow*2)) &&
779                       (y >= py - oh) && (y <= (py + oh*2)))
780                     {
781                        r->x = x - px + rx;
782                        r->y = y - py + ry;
783                        r->inbound = EINA_TRUE;
784                     }
785                   else r->inbound = EINA_FALSE;
786                }
787           }
788           r->inbound = EINA_FALSE;
789      }
790 }
791
792 static void
793 rect_place(Evas_Object *obj, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
794 {
795    ELM_CHECK_WIDTYPE(obj, widtype);
796    Widget_Data *wd = elm_widget_data_get(obj);
797    Evas_Coord ax, ay, gw, gh, hh, ww;
798
799    if (!wd) return;
800    evas_object_geometry_get(wd->rect, NULL, NULL, &ww, &hh);
801
802    ax = 0;
803    ay = 0;
804    gw = wd->size.w;
805    gh = wd->size.h;
806
807    if ((ww == gw) && (hh == gh)) return;
808
809    if (ow > gw) ax = (ow - gw) / 2;
810    if (oh > gh) ay = (oh - gh) / 2;
811    evas_object_move(wd->rect,
812                     ox + 0 - px + ax,
813                     oy + 0 - py + ay);
814    evas_object_resize(wd->rect, gw, gh);
815
816    if (wd->show.show)
817      {
818         wd->show.show = EINA_FALSE;
819         elm_smart_scroller_child_region_show(wd->scr, wd->show.x, wd->show.y, wd->show.w, wd->show.h);
820      }
821 }
822
823 static void
824 marker_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
825 {
826    ELM_CHECK_WIDTYPE(obj, widtype);
827    Widget_Data *wd = elm_widget_data_get(obj);
828    Evas_Coord ax, ay, gw, gh, tx, ty;
829    Eina_List *l, *markers;
830    Eina_Matrixsparse_Cell *cell;
831    Marker_Group *group;
832    int xx, yy, ww, hh;
833    char buf[PATH_MAX];
834    int y, x;
835    int g_xx, g_yy, g_hh, g_ww;
836
837    if (!wd) return;
838
839    ax = 0;
840    ay = 0;
841    gw = wd->size.w;
842    gh = wd->size.h;
843    if (ow > gw) ax = (ow - gw) / 2;
844    if (oh > gh) ay = (oh - gh) / 2;
845
846    if (wd->zoom != wd->marker_zoom)
847      {
848         EINA_LIST_FREE(wd->cells_displayed, cell)
849           {
850              EINA_LIST_FOREACH(eina_matrixsparse_cell_data_get(cell), l, group)
851                {
852                   if (group->obj) _group_object_free(group);
853                }
854           }
855      }
856    wd->marker_zoom = wd->zoom;
857
858    if ((wd->paused_markers)
859        && ((wd->size.nw != wd->size.w) || (wd->size.nh != wd->size.h)) )
860      return;
861
862    g_xx = wd->pan_x / wd->tsize;
863    if (g_xx < 0) g_xx = 0;
864    g_yy = wd->pan_y / wd->tsize;
865    if (g_yy < 0) g_yy = 0;
866    g_ww = (ow / wd->tsize) + 1;
867    if (g_xx + g_ww >= g->gw) g_ww = g->gw - g_xx - 1;
868    g_hh = (oh / wd->tsize) + 1;
869    if (g_yy + g_hh >= g->gh) g_hh = g->gh - g_yy - 1;
870
871    //hide groups no more displayed
872    EINA_LIST_FREE(wd->cells_displayed, cell)
873      {
874         eina_matrixsparse_cell_position_get(cell, (unsigned long *)&y, (unsigned long *)&x);
875         if ((y < g_yy) || (y > g_yy + g_hh) || (x < g_xx) || (x > g_xx + g_ww))
876           {
877              EINA_LIST_FOREACH(eina_matrixsparse_cell_data_get(cell), l, group)
878                {
879                   if (group->obj) _group_object_free(group);
880                }
881           }
882      }
883
884    if (!wd->marker_zoom)
885      {
886         g_ww = 0;
887         g_hh = 0;
888      }
889
890    for (y = g_yy; y <= g_yy + g_hh; y++)
891      {
892         for (x = g_xx; x <= g_xx + g_ww; x++)
893           {
894              if (!wd->markers[wd->zoom]) continue;
895              eina_matrixsparse_cell_idx_get(wd->markers[wd->zoom], y, x, &cell);
896              if (!cell) continue;
897              wd->cells_displayed = eina_list_append(wd->cells_displayed, cell);
898              markers = eina_matrixsparse_cell_data_get(cell);
899              EINA_LIST_FOREACH(markers, l, group)
900                {
901                   if (!group->markers) continue;
902                   if (group->clas->zoom_displayed > wd->zoom) continue;
903
904                   xx = group->x;
905                   yy = group->y;
906                   ww = group->w;
907                   hh = group->h;
908
909                   if (eina_list_count(group->markers) == 1)
910                     {
911                        Elm_Map_Marker *m = eina_list_data_get(group->markers);
912                        ww = m->clas->priv.edje_w;
913                        hh = m->clas->priv.edje_h;
914                     }
915
916                   if (ww <= 0) ww = 1;
917                   if (hh <= 0) hh = 1;
918
919                   if ((gw != g->w) && (g->w > 0))
920                     {
921                        tx = xx;
922                        xx = ((long long )gw * xx) / g->w;
923                        ww = (((long long)gw * (tx + ww)) / g->w) - xx;
924                     }
925                   if ((gh != g->h) && (g->h > 0))
926                     {
927                        ty = yy;
928                        yy = ((long long)gh * yy) / g->h;
929                        hh = (((long long)gh * (ty + hh)) / g->h) - yy;
930                     }
931
932                   if ((!group->clas->hide)
933                       && (xx-px+ax+ox >= ox) && (xx-px+ax+ox<= ox+ow)
934                       && (yy-py+ay+oy >= oy) && (yy-py+ay+oy<= oy+oh))
935                     {
936                        if (!group->obj) _group_object_create(group);
937
938                        if (group->update_nbelems)
939                          {
940                             group->update_nbelems = EINA_FALSE;
941                             if (eina_list_count(group->markers) > 1)
942                               {
943                                  snprintf(buf, sizeof(buf), "%d", eina_list_count(group->markers));
944                                  edje_object_part_text_set(elm_layout_edje_get(group->obj), "elm.text", buf);
945                               }
946                             else
947                               edje_object_part_text_set(elm_layout_edje_get(group->obj), "elm.text", "");
948                          }
949                        evas_object_move(group->obj,
950                                         xx - px + ax + ox - ww/2,
951                                         yy - py + ay + oy - hh/2);
952                        if ((!wd->paused_markers) || (group->update_resize))
953                          {
954                             group->update_resize = EINA_FALSE;
955                             evas_object_resize(group->obj, ww, hh);
956                             obj_rotate_zoom(obj, group->obj);
957                          }
958                        if (group->update_raise)
959                          {
960                             group->update_raise = EINA_FALSE;
961                             evas_object_raise(group->obj);
962                             obj_rotate_zoom(obj, group->obj);
963                             evas_object_show(group->obj);
964                          }
965                        if (group->bubble) _group_bubble_place(group);
966                     }
967                   else if (group->obj)
968                     {
969                        _group_object_free(group);
970                     }
971                }
972           }
973      }
974 }
975
976 static void
977 grid_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
978 {
979    ELM_CHECK_WIDTYPE(obj, widtype);
980    Widget_Data *wd = elm_widget_data_get(obj);
981    Evas_Coord ax, ay, gw, gh, tx, ty;
982    int xx, yy, ww, hh;
983
984    if (!wd) return;
985    ax = 0;
986    ay = 0;
987    gw = wd->size.w;
988    gh = wd->size.h;
989    if (ow > gw) ax = (ow - gw) / 2;
990    if (oh > gh) ay = (oh - gh) / 2;
991
992    Eina_Iterator *it = eina_matrixsparse_iterator_new(g->grid);
993    Eina_Matrixsparse_Cell *cell;
994
995    EINA_ITERATOR_FOREACH(it, cell)
996      {
997         Grid_Item *gi = eina_matrixsparse_cell_data_get(cell);
998
999         xx = gi->out.x;
1000         yy = gi->out.y;
1001         ww = gi->out.w;
1002         hh = gi->out.h;
1003         if ((gw != g->w) && (g->w > 0))
1004           {
1005              tx = xx;
1006              xx = ((long long )gw * xx) / g->w;
1007              ww = (((long long)gw * (tx + ww)) / g->w) - xx;
1008           }
1009         if ((gh != g->h) && (g->h > 0))
1010           {
1011              ty = yy;
1012              yy = ((long long)gh * yy) / g->h;
1013              hh = (((long long)gh * (ty + hh)) / g->h) - yy;
1014           }
1015         evas_object_move(gi->img,
1016                          xx - px + ax + ox,
1017                          yy - py + ay + oy);
1018
1019         evas_object_resize(gi->img, ww, hh);
1020
1021         obj_rotate_zoom(obj, gi->img);
1022         /*evas_object_move(gi->txt,
1023                            xx - px + ax + ox,
1024                            yy - py + ay + oy);
1025
1026           evas_object_resize(gi->txt, ww, hh);
1027          */
1028      }
1029    eina_iterator_free(it);
1030 }
1031
1032 static void
1033 _tile_update(Grid_Item *gi)
1034 {
1035    evas_object_image_file_set(gi->img, gi->file, NULL);
1036    Evas_Load_Error err = evas_object_image_load_error_get(gi->img);
1037    if (err != EVAS_LOAD_ERROR_NONE)
1038      {
1039         ERR("Image loading error (%s): %s", gi->file, evas_load_error_str(err));
1040         ecore_file_remove(gi->file);
1041         gi->file_have = EINA_FALSE;
1042      }
1043    else
1044      {
1045         obj_rotate_zoom(gi->wd->obj, gi->img);
1046         evas_object_show(gi->img);
1047         gi->file_have = EINA_TRUE;
1048         //evas_object_text_text_set(gi->txt, gi->file);
1049         //evas_object_show(gi->txt);
1050      }
1051 }
1052
1053 static void
1054 _tile_downloaded(void *data, const char *file __UNUSED__, int status)
1055 {
1056    Grid_Item *gi = data;
1057
1058    gi->job = NULL;
1059
1060    if (status == 200)
1061      {
1062         DBG("Download success from %s to %s", gi->source, gi->file);
1063         _tile_update(gi);
1064         gi->wd->finish_num++;
1065      }
1066    else
1067      {
1068         WRN("Download failed from %s to %s (%d) ", gi->source, gi->file, status);
1069         ecore_file_remove(gi->file);
1070         gi->file_have = EINA_FALSE;
1071      }
1072
1073    gi->wd->download_num--;
1074    evas_object_smart_callback_call(gi->wd->obj, SIG_DOWNLOADED, NULL);
1075    if (!gi->wd->download_num)
1076      {
1077         edje_object_signal_emit(elm_smart_scroller_edje_object_get(gi->wd->scr), "elm,state,busy,stop", "elm");
1078         evas_object_smart_callback_call(gi->wd->obj, SIG_LOADED_DETAIL, NULL);
1079      }
1080    _process_download_list(gi->wd->obj);
1081 }
1082
1083 static void
1084 _process_download_list(Evas_Object *obj)
1085 {
1086    ELM_CHECK_WIDTYPE(obj, widtype);
1087    Widget_Data *wd = elm_widget_data_get(obj);
1088    Eina_List *l, *ll;
1089    Evas_Coord ox, oy, ow, oh, cvx, cvy, cvw, cvh, tx, ty, gw, gh, xx, yy, ww, hh;
1090    Grid_Item *gi;
1091
1092    evas_object_geometry_get(wd->pan_smart, &ox, &oy, &ow, &oh);
1093    evas_output_viewport_get(evas_object_evas_get(wd->obj), &cvx, &cvy, &cvw, &cvh);
1094
1095    gw = wd->size.w;
1096    gh = wd->size.h;
1097
1098    EINA_LIST_REVERSE_FOREACH_SAFE(wd->download_list, l, ll, gi)
1099      {
1100         xx = gi->out.x;
1101         yy = gi->out.y;
1102         ww = gi->out.w;
1103         hh = gi->out.h;
1104
1105         if ((gw != gi->g->w) && (gi->g->w > 0))
1106           {
1107              tx = xx;
1108              xx = ((long long )gw * xx) / gi->g->w;
1109              ww = (((long long)gw * (tx + ww)) / gi->g->w) - xx;
1110           }
1111         if ((gh != gi->g->h) && (gi->g->h > 0))
1112           {
1113              ty = yy;
1114              yy = ((long long)gh * yy) / gi->g->h;
1115              hh = (((long long)gh * (ty + hh)) / gi->g->h) - yy;
1116           }
1117         if (!ELM_RECTS_INTERSECT(xx - wd->pan_x + ox,
1118                                  yy  - wd->pan_y + oy,
1119                                  ww, hh,
1120                                  cvx, cvy, cvw, cvh) ||
1121            (gi->zoom != wd->zoom))
1122           {
1123              wd->download_list = eina_list_remove(wd->download_list, gi);
1124              continue;
1125           }
1126
1127         if (gi->wd->download_num >= MAX_CONCURRENT_DOWNLOAD) break;
1128
1129         Eina_Bool ret = ecore_file_download_full(gi->source, gi->file, _tile_downloaded, NULL, gi, &(gi->job), wd->ua);
1130         if (!ret || !gi->job) ERR("Can't start to download from %s to %s", gi->source, gi->file);
1131         else
1132           {
1133              gi->wd->download_num++;
1134              wd->try_num++;
1135              wd->download_list = eina_list_remove(wd->download_list, gi);
1136              if (wd->download_num == 1)
1137                edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr), "elm,state,busy,start", "elm");
1138           }
1139      }
1140 }
1141
1142 static void
1143 _add_download_list(Evas_Object *obj, Grid_Item *gi)
1144 {
1145    ELM_CHECK_WIDTYPE(obj, widtype);
1146    Widget_Data *wd = elm_widget_data_get(obj);
1147
1148    wd->download_list = eina_list_remove(wd->download_list, gi);
1149    wd->download_list = eina_list_append(wd->download_list, gi);
1150    _process_download_list(obj);
1151 }
1152
1153 static void
1154 grid_create_all(Evas_Object *obj)
1155 {
1156    ELM_CHECK_WIDTYPE(obj, widtype);
1157    Widget_Data *wd = elm_widget_data_get(obj);
1158    Grid *g;
1159    int zoom = 0;
1160
1161    EINA_SAFETY_ON_NULL_RETURN(wd);
1162    EINA_SAFETY_ON_NULL_RETURN(wd->src);
1163
1164    for (zoom = wd->src->zoom_min; zoom <= wd->src->zoom_max; zoom++)
1165      {
1166         g = calloc(1, sizeof(Grid));
1167         EINA_SAFETY_ON_NULL_RETURN(g);
1168         g->zoom = zoom;
1169         g->tsize = wd->tsize;
1170         g->wd = wd;
1171         int size =  pow(2.0, g->zoom);
1172         g->gw = size;
1173         g->gh = size;
1174         g->w = g->tsize * g->gw;
1175         g->h = g->tsize * g->gh;
1176
1177         g->grid = eina_matrixsparse_new(g->gh, g->gw, NULL, NULL);
1178         wd->grids = eina_list_append(wd->grids, g);
1179      }
1180 }
1181
1182 static void
1183 grid_clear_all(Evas_Object *obj)
1184 {
1185    ELM_CHECK_WIDTYPE(obj, widtype);
1186    Widget_Data *wd = elm_widget_data_get(obj);
1187    Grid *g;
1188    Grid_Item *gi;
1189
1190    EINA_SAFETY_ON_NULL_RETURN(wd);
1191
1192    EINA_LIST_FREE(wd->grids, g)
1193      {
1194         Eina_Iterator *it = eina_matrixsparse_iterator_new(g->grid);
1195         Eina_Matrixsparse_Cell *cell;
1196
1197         EINA_ITERATOR_FOREACH(it, cell)
1198           {
1199              gi = eina_matrixsparse_cell_data_get(cell);
1200              evas_object_del(gi->img);
1201              //evas_object_del(gi->txt);
1202
1203              if (gi->job)
1204                {
1205                   DBG("DOWNLOAD abort %s", gi->file);
1206                   ecore_file_download_abort(gi->job);
1207                   ecore_file_remove(gi->file);
1208                   gi->file_have = EINA_FALSE;
1209                   gi->job = NULL;
1210                   wd->try_num--;
1211                }
1212              if (gi->file)   eina_stringshare_del(gi->file);
1213              if (gi->source) eina_stringshare_del(gi->source);
1214              free(gi);
1215           }
1216         eina_matrixsparse_free(g->grid);
1217         eina_iterator_free(it);
1218         free(g);
1219      }
1220
1221    EINA_LIST_FREE(wd->download_list, gi);
1222    if (!ecore_file_recursive_rm("/tmp/elm_map/")) WRN("Deletion of /tmp/elm_map/ failed");
1223
1224 }
1225
1226 static void
1227 grid_unload(Evas_Object *obj, Grid *g)
1228 {
1229    ELM_CHECK_WIDTYPE(obj, widtype);
1230    Widget_Data *wd = elm_widget_data_get(obj);
1231
1232    Eina_Iterator *it;
1233    Eina_Matrixsparse_Cell *cell;
1234    Grid_Item *gi;
1235
1236    EINA_SAFETY_ON_NULL_RETURN(wd);
1237
1238    it = eina_matrixsparse_iterator_new(g->grid);
1239    EINA_ITERATOR_FOREACH(it, cell)
1240      {
1241         gi = eina_matrixsparse_cell_data_get(cell);
1242
1243         if (gi->file_have)
1244           {
1245              evas_object_hide(gi->img);
1246              //evas_object_hide(gi->txt);
1247              evas_object_image_file_set(gi->img, NULL, NULL);
1248           }
1249         else if (gi->job)
1250           {
1251              DBG("DOWNLOAD abort %s", gi->file);
1252              ecore_file_download_abort(gi->job);
1253              ecore_file_remove(gi->file);
1254              gi->job = NULL;
1255              wd->try_num--;
1256           }
1257      }
1258    eina_iterator_free(it);
1259 }
1260
1261
1262 static void
1263 grid_load(Evas_Object *obj, Grid *g)
1264 {
1265    ELM_CHECK_WIDTYPE(obj, widtype);
1266    Widget_Data *wd = elm_widget_data_get(obj);
1267    int x, y;
1268    int size;
1269    Evas_Coord ox, oy, ow, oh, cvx, cvy, cvw, cvh, tx, ty, gw, gh, xx, yy, ww, hh;
1270    Eina_Iterator *it;
1271    Eina_Matrixsparse_Cell *cell;
1272    Grid_Item *gi;
1273
1274    EINA_SAFETY_ON_NULL_RETURN(wd);
1275    EINA_SAFETY_ON_NULL_RETURN(wd->src);
1276
1277    evas_object_geometry_get(wd->pan_smart, &ox, &oy, &ow, &oh);
1278    evas_output_viewport_get(evas_object_evas_get(wd->obj), &cvx, &cvy, &cvw, &cvh);
1279
1280    gw = wd->size.w;
1281    gh = wd->size.h;
1282
1283    if ((gw <= 0) || (gh <= 0)) return;
1284
1285    size = g->tsize;
1286    if ((gw != g->w) && (g->w > 0))
1287      size = ((long long)gw * size) / g->w;
1288    if (size < (g->tsize / 2)) return; // else we will load to much tiles
1289
1290    it = eina_matrixsparse_iterator_new(g->grid);
1291
1292    EINA_ITERATOR_FOREACH(it, cell)
1293      {
1294         gi = eina_matrixsparse_cell_data_get(cell);
1295
1296         xx = gi->out.x;
1297         yy = gi->out.y;
1298         ww = gi->out.w;
1299         hh = gi->out.h;
1300
1301         if ((gw != g->w) && (g->w > 0))
1302           {
1303              tx = xx;
1304              xx = ((long long )gw * xx) / g->w;
1305              ww = (((long long)gw * (tx + ww)) / g->w) - xx;
1306           }
1307         if ((gh != g->h) && (g->h > 0))
1308           {
1309              ty = yy;
1310              yy = ((long long)gh * yy) / g->h;
1311              hh = (((long long)gh * (ty + hh)) / g->h) - yy;
1312           }
1313
1314         if (!ELM_RECTS_INTERSECT(xx - wd->pan_x + ox,
1315                                  yy  - wd->pan_y + oy,
1316                                  ww, hh,
1317                                  cvx, cvy, cvw, cvh))
1318           {
1319              if (gi->file_have)
1320                {
1321                   evas_object_hide(gi->img);
1322                   //evas_object_hide(gi->txt);
1323                   evas_object_image_file_set(gi->img, NULL, NULL);
1324                }
1325              else if (gi->job)
1326                {
1327                   DBG("Download abort %s", gi->file);
1328                   ecore_file_download_abort(gi->job);
1329                   ecore_file_remove(gi->file);
1330                   gi->job = NULL;
1331                   wd->try_num--;
1332                }
1333           }
1334      }
1335    eina_iterator_free(it);
1336
1337    xx = wd->pan_x / size - 1;
1338    if (xx < 0) xx = 0;
1339
1340    yy = wd->pan_y / size - 1;
1341    if (yy < 0) yy = 0;
1342
1343    ww = ow / size + 2;
1344    if (xx + ww >= g->gw) ww = g->gw - xx - 1;
1345
1346    hh = oh / size + 2;
1347    if (yy + hh >= g->gh) hh = g->gh - yy - 1;
1348
1349    for (y = yy; y <= yy + hh; y++)
1350      {
1351         for (x = xx; x <= xx + ww; x++)
1352           {
1353              gi = eina_matrixsparse_data_idx_get(g->grid, y, x);
1354
1355              if (!gi)
1356                {
1357                   char buf[PATH_MAX];
1358                   char buf2[PATH_MAX];
1359                   char *source;
1360
1361                   gi = calloc(1, sizeof(Grid_Item));
1362                   EINA_SAFETY_ON_NULL_RETURN(gi);
1363
1364                   gi->wd = wd;
1365                   gi->g = g;
1366                   gi->zoom = g->zoom;
1367                   gi->file_have = EINA_FALSE;
1368                   gi->job = NULL;
1369
1370                   gi->src.x = x * g->tsize;
1371                   gi->src.y = y * g->tsize;
1372                   gi->src.w = g->tsize;
1373                   gi->src.h = g->tsize;
1374
1375                   gi->out.x = gi->src.x;
1376                   gi->out.y = gi->src.y;
1377                   gi->out.w = gi->src.w;
1378                   gi->out.h = gi->src.h;
1379
1380                   gi->img = evas_object_image_add(evas_object_evas_get(obj));
1381                   evas_object_image_scale_hint_set(gi->img, EVAS_IMAGE_SCALE_HINT_DYNAMIC);
1382                   evas_object_image_filled_set(gi->img, 1);
1383
1384                   evas_object_smart_member_add(gi->img, wd->pan_smart);
1385                   elm_widget_sub_object_add(obj, gi->img);
1386                   evas_object_pass_events_set(gi->img, EINA_TRUE);
1387                   evas_object_stack_below(gi->img, wd->sep_maps_markers);
1388
1389 /*                gi->txt = evas_object_text_add(evas_object_evas_get(obj));
1390                   evas_object_text_font_set(gi->txt, "Vera", 12);
1391                   evas_object_color_set(gi->txt, 100, 100, 100, 255);
1392                   evas_object_smart_member_add(gi->txt,
1393                                                wd->pan_smart);
1394                   elm_widget_sub_object_add(obj, gi->txt);
1395                   evas_object_pass_events_set(gi->txt, EINA_TRUE);
1396 */
1397                   snprintf(buf, sizeof(buf), CACHE_PATH, wd->id, g->zoom, x);
1398                   snprintf(buf2, sizeof(buf2), CACHE_FILE_PATH, buf, y);
1399                   if (!ecore_file_exists(buf)) ecore_file_mkpath(buf);
1400
1401                   eina_stringshare_replace(&gi->file, buf2);
1402                   source = wd->src->url_cb(obj, x, y, g->zoom);
1403                   if ((!source) || (strlen(source)==0))
1404                     {
1405                        eina_stringshare_replace(&gi->source, NULL);
1406                        WRN("Getting source url failed: %s", gi->file);
1407                     }
1408                   else eina_stringshare_replace(&gi->source, source);
1409                   if (source) free(source);
1410
1411                   eina_matrixsparse_data_idx_set(g->grid, y, x, gi);
1412                }
1413
1414                if (gi->file_have)
1415                  {
1416                     DBG("File exists: %s", gi->file);
1417                     _tile_update(gi);
1418                  }
1419                else if (!gi->job)
1420                  {
1421                     DBG("Added to download list: %s", gi->file);
1422                     _add_download_list(obj, gi);
1423                  }
1424           }
1425      }
1426 }
1427
1428 static void
1429 _smooth_update(Evas_Object *obj)
1430 {
1431    ELM_CHECK_WIDTYPE(obj, widtype);
1432    Widget_Data *wd = elm_widget_data_get(obj);
1433    Eina_List *l;
1434    Grid *g;
1435
1436    if (!wd) return;
1437    EINA_LIST_FOREACH(wd->grids, l, g)
1438      {
1439         Eina_Iterator *it = eina_matrixsparse_iterator_new(g->grid);
1440         Eina_Matrixsparse_Cell *cell;
1441
1442         EINA_ITERATOR_FOREACH(it, cell)
1443           {
1444              Grid_Item *gi = eina_matrixsparse_cell_data_get(cell);
1445              evas_object_image_smooth_scale_set(gi->img, (!wd->nosmooth));
1446           }
1447         eina_iterator_free(it);
1448      }
1449 }
1450
1451 static Eina_Bool
1452 _scr_timeout(void *data)
1453 {
1454    ELM_CHECK_WIDTYPE(data, widtype) ECORE_CALLBACK_CANCEL;
1455    Widget_Data *wd = elm_widget_data_get(data);
1456
1457    if (!wd) return ECORE_CALLBACK_CANCEL;
1458    wd->nosmooth--;
1459    if (!wd->nosmooth) _smooth_update(data);
1460    wd->scr_timer = NULL;
1461    return ECORE_CALLBACK_CANCEL;
1462 }
1463
1464 static void
1465 _scr(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1466 {
1467    ELM_CHECK_WIDTYPE(data, widtype);
1468    Widget_Data *wd = elm_widget_data_get(data);
1469
1470    if (!wd) return;
1471    if (!wd->scr_timer)
1472      {
1473         wd->nosmooth++;
1474         if (wd->nosmooth == 1) _smooth_update(data);
1475      }
1476    if (wd->scr_timer) ecore_timer_del(wd->scr_timer);
1477    wd->scr_timer = ecore_timer_add(0.5, _scr_timeout, data);
1478 }
1479
1480 static void
1481 zoom_do(Evas_Object *obj)
1482 {
1483    ELM_CHECK_WIDTYPE(obj, widtype);
1484    Widget_Data *wd = elm_widget_data_get(obj);
1485    Evas_Coord xx, yy, ow, oh;
1486
1487    if (!wd) return;
1488    wd->size.w = wd->size.nw;
1489    wd->size.h = wd->size.nh;
1490
1491    elm_smart_scroller_child_viewport_size_get(wd->scr, &ow, &oh);
1492
1493    if (wd->center_on.enabled)
1494      {
1495         elm_map_utils_convert_geo_into_coord(obj, wd->center_on.lon, wd->center_on.lat, wd->size.w, &xx, &yy);
1496         xx -= ow / 2;
1497         yy -= oh / 2;
1498      }
1499    else
1500      {
1501         xx = (wd->size.spos.x * wd->size.w) - (ow / 2);
1502         yy = (wd->size.spos.y * wd->size.h) - (oh / 2);
1503      }
1504
1505
1506    if (xx < 0) xx = 0;
1507    else if (xx > (wd->size.w - ow)) xx = wd->size.w - ow;
1508    if (yy < 0) yy = 0;
1509    else if (yy > (wd->size.h - oh)) yy = wd->size.h - oh;
1510
1511    wd->show.show = EINA_TRUE;
1512    wd->show.x = xx;
1513    wd->show.y = yy;
1514    wd->show.w = ow;
1515    wd->show.h = oh;
1516
1517    if (wd->calc_job) ecore_job_del(wd->calc_job);
1518    wd->calc_job = ecore_job_add(_calc_job, wd);
1519 }
1520
1521 static Eina_Bool
1522 _zoom_timeout(void *data)
1523 {
1524    ELM_CHECK_WIDTYPE(data, widtype) ECORE_CALLBACK_CANCEL;
1525    Widget_Data *wd = elm_widget_data_get(data);
1526
1527    if (!wd) return ECORE_CALLBACK_CANCEL;
1528    wd->zoom_timer = NULL;
1529    wd->pinch.level = 1.0;
1530    zoom_do(data);
1531    evas_object_smart_callback_call(data, SIG_ZOOM_STOP, NULL);
1532    return ECORE_CALLBACK_CANCEL;
1533 }
1534
1535 static Eina_Bool
1536 _zoom_anim(void *data)
1537 {
1538    ELM_CHECK_WIDTYPE(data, widtype) ECORE_CALLBACK_CANCEL;
1539    Evas_Object *obj = data;
1540    Widget_Data *wd = elm_widget_data_get(obj);
1541
1542    if (!wd) return ECORE_CALLBACK_CANCEL;
1543    if (wd->zoom_method == ZOOM_METHOD_IN) wd->t += 0.1 ;
1544    else if (wd->zoom_method == ZOOM_METHOD_OUT) wd->t -= 0.05;
1545    else
1546      {
1547         wd->zoom_animator = NULL;
1548         zoom_do(obj);
1549         evas_object_smart_callback_call(data, SIG_ZOOM_STOP, NULL);
1550         return ECORE_CALLBACK_CANCEL;
1551      }
1552
1553    if (wd->t >= 2.0)
1554      {
1555         wd->zoom_animator = NULL;
1556         wd->pinch.level = 2.0;
1557         if (wd->zoom_timer) ecore_timer_del(wd->zoom_timer);
1558         wd->zoom_timer = ecore_timer_add(0.35, _zoom_timeout, obj);
1559         return ECORE_CALLBACK_CANCEL;
1560      }
1561    else if (wd->t <= 0.5)
1562      {
1563         wd->zoom_animator = NULL;
1564         wd->pinch.level = 0.5;
1565         if (wd->zoom_timer) ecore_timer_del(wd->zoom_timer);
1566         wd->zoom_timer = ecore_timer_add(1.35, _zoom_timeout, obj);
1567         return ECORE_CALLBACK_CANCEL;
1568      }
1569    else if (wd->t != 1.0)
1570      {
1571         Evas_Coord x, y, w, h;
1572         float half_w, half_h;
1573         evas_object_geometry_get(data, &x, &y, &w, &h);
1574         half_w = (float)w * 0.5;
1575         half_h = (float)h * 0.5;
1576         wd->pinch.cx = x + half_w;
1577         wd->pinch.cy = y + half_h;
1578         wd->pinch.level = wd->t;
1579         if (wd->calc_job) ecore_job_del(wd->calc_job);
1580         wd->calc_job = ecore_job_add(_calc_job, wd);
1581      }
1582    return ECORE_CALLBACK_RENEW;
1583 }
1584
1585 static Eina_Bool
1586 _long_press(void *data)
1587 {
1588    ELM_CHECK_WIDTYPE(data, widtype) ECORE_CALLBACK_CANCEL;
1589    Widget_Data *wd = elm_widget_data_get(data);
1590
1591    if (!wd) return ECORE_CALLBACK_CANCEL;
1592    wd->long_timer = NULL;
1593    evas_object_smart_callback_call(data, SIG_LONGPRESSED, &wd->ev);
1594    return ECORE_CALLBACK_CANCEL;
1595 }
1596
1597 static void
1598 _mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1599 {
1600    ELM_CHECK_WIDTYPE(data, widtype);
1601    Widget_Data *wd = elm_widget_data_get(data);
1602    Evas_Event_Mouse_Down *ev = event_info;
1603
1604    if (ev->button != 1) return;
1605    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
1606    else wd->on_hold = EINA_FALSE;
1607    if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
1608      evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, ev);
1609    else
1610      evas_object_smart_callback_call(data, SIG_PRESS, ev);
1611    if (wd->long_timer) ecore_timer_del(wd->long_timer);
1612    wd->long_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press, data);
1613 }
1614
1615 static void
1616 _mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1617 {
1618    ELM_CHECK_WIDTYPE(data, widtype);
1619    Widget_Data *wd = elm_widget_data_get(data);
1620    EINA_SAFETY_ON_NULL_RETURN(wd);
1621
1622    Evas_Event_Mouse_Up *ev = event_info;
1623    EINA_SAFETY_ON_NULL_RETURN(ev);
1624
1625    if (ev->button != 1) return;
1626    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
1627    else wd->on_hold = EINA_FALSE;
1628    if (wd->long_timer)
1629      {
1630         ecore_timer_del(wd->long_timer);
1631         wd->long_timer = NULL;
1632      }
1633    if (!wd->on_hold) evas_object_smart_callback_call(data, SIG_CLICKED, ev);
1634    wd->on_hold = EINA_FALSE;
1635 }
1636
1637 static void
1638 _mouse_wheel_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1639 {
1640    ELM_CHECK_WIDTYPE(data, widtype);
1641    Widget_Data *wd = elm_widget_data_get(data);
1642    EINA_SAFETY_ON_NULL_RETURN(wd);
1643
1644    if (!wd->paused)
1645      {
1646         Evas_Event_Mouse_Wheel *ev = (Evas_Event_Mouse_Wheel*) event_info;
1647         Evas_Coord x, y, w, h;
1648
1649         evas_object_geometry_get(data, &x, &y, &w, &h);
1650
1651         if (wd->calc_job) ecore_job_del(wd->calc_job);
1652         wd->calc_job = ecore_job_add(_calc_job, wd);
1653
1654         wd->wheel_diff -= ev->z;
1655         wd->pinch.level = pow(2.0, (double)wd->wheel_diff/10);
1656         wd->pinch.cx = x + ((double)w * 0.5);
1657         wd->pinch.cy = y + ((double)h * 0.5);
1658
1659         if (wd->wheel_diff >= 10 || wd->wheel_diff <= -10)
1660           {
1661
1662
1663              Elm_Map_Zoom_Mode temp;
1664              temp = wd->mode;
1665              wd->mode = ELM_MAP_ZOOM_MODE_MANUAL;
1666              wd->paused = EINA_TRUE;
1667              elm_map_zoom_set(data, wd->zoom + (wd->wheel_diff/10));
1668              wd->paused = EINA_FALSE;
1669              wd->mode = temp;
1670              wd->pinch.level = 1.0;
1671              wd->wheel_diff = 0;
1672           }
1673         else
1674           {
1675              if (wd->calc_job) ecore_job_del(wd->calc_job);
1676              wd->calc_job = ecore_job_add(_calc_job, wd);
1677           }
1678      }
1679 }
1680
1681 static void
1682 _rect_resize_cb(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1683 {
1684    ELM_CHECK_WIDTYPE(data, widtype);
1685    Widget_Data *wd = elm_widget_data_get(data);
1686    int x, y, w, h;
1687
1688    evas_object_geometry_get(wd->rect, &x, &y, &w, &h);
1689    evas_object_geometry_get(wd->pan_smart, &x, &y, &w, &h);
1690    evas_object_resize(wd->rect, w, h);
1691    evas_object_move(wd->rect, x, y);
1692 }
1693
1694 static void
1695 _del_hook(Evas_Object *obj)
1696 {
1697    ELM_CHECK_WIDTYPE(obj, widtype);
1698    Widget_Data *wd = elm_widget_data_get(obj);
1699    Elm_Map_Group_Class *group_clas;
1700    Elm_Map_Marker_Class *marker_clas;
1701    Eina_List *l;
1702    Evas_Object *p;
1703    Path_Node *n;
1704    Path_Waypoint *w;
1705    Ecore_Event_Handler *h;
1706    Elm_Map_Route *r;
1707    Elm_Map_Name *na;
1708    Evas_Object *route;
1709
1710    if (!wd) return;
1711    EINA_LIST_FREE(wd->groups_clas, group_clas)
1712      {
1713         if (group_clas->style)
1714           eina_stringshare_del(group_clas->style);
1715         free(group_clas);
1716      }
1717
1718    EINA_LIST_FREE(wd->markers_clas, marker_clas)
1719      {
1720         if (marker_clas->style)
1721           eina_stringshare_del(marker_clas->style);
1722         free(marker_clas);
1723      }
1724
1725    EINA_LIST_FOREACH(wd->route, l, r)
1726      {
1727         EINA_LIST_FREE(r->path, p)
1728           {
1729              evas_object_del(p);
1730           }
1731
1732         EINA_LIST_FREE(r->waypoint, w)
1733           {
1734              if (w->point) eina_stringshare_del(w->point);
1735              free(w);
1736           }
1737
1738         EINA_LIST_FREE(r->nodes, n)
1739           {
1740              if (n->pos.address) eina_stringshare_del(n->pos.address);
1741              free(n);
1742           }
1743
1744         EINA_LIST_FREE(r->handlers, h)
1745           {
1746              ecore_event_handler_del(h);
1747           }
1748
1749         if (r->con_url) ecore_con_url_free(r->con_url);
1750         if (r->info.nodes) eina_stringshare_del(r->info.nodes);
1751         if (r->info.waypoints) eina_stringshare_del(r->info.waypoints);
1752      }
1753
1754    EINA_LIST_FREE(wd->names, na)
1755      {
1756         if (na->address) free(na->address);
1757         if (na->handler) ecore_event_handler_del(na->handler);
1758         if (na->ud.fname)
1759           {
1760              ecore_file_remove(na->ud.fname);
1761              free(na->ud.fname);
1762              na->ud.fname = NULL;
1763           }
1764      }
1765
1766    EINA_LIST_FREE(wd->track, route)
1767      {
1768         evas_object_del(route);
1769      }
1770
1771    if (wd->map) evas_map_free(wd->map);
1772    if (wd->source_names) free(wd->source_names);
1773    if (wd->calc_job) ecore_job_del(wd->calc_job);
1774    if (wd->scr_timer) ecore_timer_del(wd->scr_timer);
1775    if (wd->zoom_animator) ecore_animator_del(wd->zoom_animator);
1776    if (wd->long_timer) ecore_timer_del(wd->long_timer);
1777    if (wd->user_agent) eina_stringshare_del(wd->user_agent);
1778    if (wd->ua) eina_hash_free(wd->ua);
1779    if (wd->markers) free(wd->markers);
1780
1781    free(wd);
1782 }
1783
1784 static void
1785 _del_pre_hook(Evas_Object *obj)
1786 {
1787    ELM_CHECK_WIDTYPE(obj, widtype);
1788    Widget_Data *wd = elm_widget_data_get(obj);
1789    Marker_Group *group;
1790    Elm_Map_Marker *marker;
1791    int i;
1792    Eina_Bool free_marker = EINA_TRUE;
1793    Eina_List *l;
1794
1795    if (!wd) return;
1796    grid_clear_all(obj);
1797    for (i = 0; i <= wd->zoom_max; i++)
1798      {
1799         if (!wd->markers[i]) continue;
1800         Eina_Iterator *it = eina_matrixsparse_iterator_new(wd->markers[i]);
1801         Eina_Matrixsparse_Cell *cell;
1802
1803         EINA_ITERATOR_FOREACH(it, cell)
1804           {
1805              l =  eina_matrixsparse_cell_data_get(cell);
1806              EINA_LIST_FREE(l, group)
1807                {
1808                   EINA_LIST_FREE(group->markers, marker)
1809                     {
1810                        evas_object_event_callback_del_full(group->sc, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1811                                                            _bubble_sc_hints_changed_cb, group);
1812                        if (free_marker) free(marker);
1813                     }
1814                   free(group);
1815                }
1816              free_marker = EINA_FALSE;
1817           }
1818         eina_iterator_free(it);
1819         eina_matrixsparse_free(wd->markers[i]);
1820      }
1821
1822    evas_object_del(wd->sep_maps_markers);
1823    evas_object_del(wd->pan_smart);
1824    wd->pan_smart = NULL;
1825 }
1826
1827 static void
1828 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
1829 {
1830    ELM_CHECK_WIDTYPE(obj, widtype);
1831    Widget_Data *wd = elm_widget_data_get(obj);
1832
1833    if (!wd) return;
1834    if (elm_widget_focus_get(obj))
1835      {
1836         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr), "elm,action,focus", "elm");
1837         evas_object_focus_set(wd->obj, EINA_TRUE);
1838      }
1839    else
1840      {
1841         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr), "elm,action,unfocus", "elm");
1842         evas_object_focus_set(wd->obj, EINA_FALSE);
1843      }
1844 }
1845
1846 static void
1847 _theme_hook(Evas_Object *obj)
1848 {
1849    ELM_CHECK_WIDTYPE(obj, widtype);
1850    Widget_Data *wd = elm_widget_data_get(obj);
1851
1852    if (!wd) return;
1853    elm_smart_scroller_object_theme_set(obj, wd->scr, "map", "base", elm_widget_style_get(obj));
1854    //   edje_object_scale_set(wd->scr, elm_widget_scale_get(obj) * _elm_config->scale);
1855    _sizing_eval(obj);
1856 }
1857
1858 static void
1859 _sizing_eval(Evas_Object *obj)
1860 {
1861    ELM_CHECK_WIDTYPE(obj, widtype);
1862    Widget_Data *wd = elm_widget_data_get(obj);
1863    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
1864
1865    if (!wd) return;
1866    evas_object_size_hint_max_get(wd->scr, &maxw, &maxh);
1867    evas_object_size_hint_min_set(obj, minw, minh);
1868    evas_object_size_hint_max_set(obj, maxw, maxh);
1869 }
1870
1871 static void
1872 _calc_job(void *data)
1873 {
1874    Widget_Data *wd = data;
1875    Evas_Coord minw, minh;
1876
1877    if (!wd) return;
1878    minw = wd->size.w;
1879    minh = wd->size.h;
1880    if (wd->resized)
1881      {
1882         wd->resized = EINA_FALSE;
1883         if (wd->mode != ELM_MAP_ZOOM_MODE_MANUAL)
1884           {
1885              double tz = wd->zoom;
1886              wd->zoom = 0.0;
1887              elm_map_zoom_set(wd->obj, tz);
1888           }
1889      }
1890    if ((minw != wd->minw) || (minh != wd->minh))
1891      {
1892         wd->minw = minw;
1893         wd->minh = minh;
1894         evas_object_smart_callback_call(wd->pan_smart, SIG_CHANGED, NULL);
1895         _sizing_eval(wd->obj);
1896      }
1897    wd->calc_job = NULL;
1898    evas_object_smart_changed(wd->pan_smart);
1899 }
1900
1901 static void
1902 _pan_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
1903 {
1904    Pan *sd = evas_object_smart_data_get(obj);
1905    if (!sd) return;
1906    if ((x == sd->wd->pan_x) && (y == sd->wd->pan_y)) return;
1907    sd->wd->pan_x = x;
1908    sd->wd->pan_y = y;
1909    evas_object_smart_changed(obj);
1910 }
1911
1912 static void
1913 _pan_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1914 {
1915    Pan *sd = evas_object_smart_data_get(obj);
1916    if (!sd) return;
1917    if (x) *x = sd->wd->pan_x;
1918    if (y) *y = sd->wd->pan_y;
1919 }
1920
1921 static void
1922 _pan_max_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1923 {
1924    Pan *sd = evas_object_smart_data_get(obj);
1925    Evas_Coord ow, oh;
1926    if (!sd) return;
1927    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
1928    ow = sd->wd->minw - ow;
1929    if (ow < 0) ow = 0;
1930    oh = sd->wd->minh - oh;
1931    if (oh < 0) oh = 0;
1932    if (x) *x = ow;
1933    if (y) *y = oh;
1934 }
1935
1936 static void
1937 _pan_min_get(Evas_Object *obj __UNUSED__, Evas_Coord *x, Evas_Coord *y)
1938 {
1939    if (x) *x = 0;
1940    if (y) *y = 0;
1941 }
1942
1943 static void
1944 _pan_child_size_get(Evas_Object *obj, Evas_Coord *w, Evas_Coord *h)
1945 {
1946    Pan *sd = evas_object_smart_data_get(obj);
1947    if (!sd) return;
1948    if (w) *w = sd->wd->minw;
1949    if (h) *h = sd->wd->minh;
1950 }
1951
1952 static void
1953 _pan_add(Evas_Object *obj)
1954 {
1955    Pan *sd;
1956    Evas_Object_Smart_Clipped_Data *cd;
1957    _pan_sc.add(obj);
1958    cd = evas_object_smart_data_get(obj);
1959    if (!cd) return;
1960    sd = calloc(1, sizeof(Pan));
1961    if (!sd) return;
1962    sd->__clipped_data = *cd;
1963    free(cd);
1964    evas_object_smart_data_set(obj, sd);
1965 }
1966
1967 static void
1968 _pan_del(Evas_Object *obj)
1969 {
1970    Pan *sd = evas_object_smart_data_get(obj);
1971    if (!sd) return;
1972    _pan_sc.del(obj);
1973 }
1974
1975 static void
1976 _pan_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
1977 {
1978    Pan *sd = evas_object_smart_data_get(obj);
1979    Evas_Coord ow, oh;
1980    if (!sd) return;
1981    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
1982    if ((ow == w) && (oh == h)) return;
1983    sd->wd->resized = EINA_TRUE;
1984    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
1985    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
1986 }
1987
1988 static void
1989 _pan_calculate(Evas_Object *obj)
1990 {
1991    Pan *sd = evas_object_smart_data_get(obj);
1992    Evas_Coord ox, oy, ow, oh;
1993    Eina_List *l;
1994    Grid *g;
1995
1996    EINA_SAFETY_ON_NULL_RETURN(sd);
1997    EINA_SAFETY_ON_NULL_RETURN(sd->wd);
1998
1999    evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
2000    rect_place(sd->wd->obj, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2001    EINA_LIST_FOREACH(sd->wd->grids, l, g)
2002      {
2003         if (sd->wd->zoom == g->zoom) grid_load(sd->wd->obj, g);
2004         else if (sd->wd->zoom-1 != g->zoom && sd->wd->zoom+1 != g->zoom) grid_unload(sd->wd->obj, g); // remain only adjacent grids
2005         grid_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2006         if (sd->wd->zoom == g->zoom) marker_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2007         if (!sd->wd->zoom_animator) route_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2008         if (!sd->wd->zoom_animator) track_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2009      }
2010 }
2011
2012 static void
2013 _pan_move(Evas_Object *obj, Evas_Coord x __UNUSED__, Evas_Coord y __UNUSED__)
2014 {
2015    Pan *sd = evas_object_smart_data_get(obj);
2016    if (!sd) return;
2017    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
2018    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
2019 }
2020
2021 static void
2022 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2023 {
2024    ELM_CHECK_WIDTYPE(obj, widtype);
2025    Widget_Data *wd = elm_widget_data_get(obj);
2026
2027    if (!wd) return;
2028    elm_smart_scroller_hold_set(wd->scr, 1);
2029 }
2030
2031 static void
2032 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2033 {
2034    ELM_CHECK_WIDTYPE(obj, widtype);
2035    Widget_Data *wd = elm_widget_data_get(obj);
2036
2037    if (!wd) return;
2038    elm_smart_scroller_hold_set(wd->scr, 0);
2039 }
2040
2041 static void
2042 _freeze_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2043 {
2044    ELM_CHECK_WIDTYPE(obj, widtype);
2045    Widget_Data *wd = elm_widget_data_get(obj);
2046
2047    if (!wd) return;
2048    elm_smart_scroller_freeze_set(wd->scr, 1);
2049 }
2050
2051 static void
2052 _freeze_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2053 {
2054    ELM_CHECK_WIDTYPE(obj, widtype);
2055    Widget_Data *wd = elm_widget_data_get(obj);
2056
2057    if (!wd) return;
2058    elm_smart_scroller_freeze_set(wd->scr, 0);
2059 }
2060
2061 static void
2062 _scr_anim_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2063 {
2064    evas_object_smart_callback_call(data, "scroll,anim,start", NULL);
2065 }
2066
2067 static void
2068 _scr_anim_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2069 {
2070    evas_object_smart_callback_call(data, "scroll,anim,stop", NULL);
2071 }
2072
2073 static void
2074 _scr_drag_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2075 {
2076    Widget_Data *wd = elm_widget_data_get(data);
2077    EINA_SAFETY_ON_NULL_RETURN(wd);
2078    wd->center_on.enabled = EINA_FALSE;
2079
2080    // FIXME: els_scoller sometimes give start event again & again... it confuses app. (els_scr bug?)
2081    if (!wd->scr_started)
2082      {
2083         wd->scr_started = EINA_TRUE;
2084         evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_START, NULL);
2085      }
2086 }
2087
2088 static void
2089 _scr_drag_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2090 {
2091    Widget_Data *wd = elm_widget_data_get(data);
2092    EINA_SAFETY_ON_NULL_RETURN(wd);
2093    wd->center_on.enabled = EINA_FALSE;
2094
2095    // FIXME: els_scoller sometimes give start event again & again... it confuses app. (els_scr bug?)
2096    if (wd->scr_started)
2097      {
2098         wd->scr_started = EINA_FALSE;
2099         evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_STOP, NULL);
2100      }
2101 }
2102
2103 static void
2104 _scr_scroll(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2105 {
2106    evas_object_smart_callback_call(data, SIG_SCROLL, NULL);
2107 }
2108
2109
2110 static void
2111 _group_object_create(Marker_Group *group)
2112 {
2113    const char *style = "radio";
2114    Evas_Object *icon = NULL;
2115
2116    if (group->obj) return;
2117    if ((!group->clas->priv.objs_notused) || (eina_list_count(group->markers) == 1))
2118      {
2119         //set icon and style
2120         if (eina_list_count(group->markers) == 1)
2121           {
2122              Elm_Map_Marker *m = eina_list_data_get(group->markers);
2123              if (m->clas->style)
2124                style = m->clas->style;
2125
2126              if (m->clas->func.icon_get)
2127                icon = m->clas->func.icon_get(group->wd->obj, m, m->data);
2128
2129              group->delete_object = EINA_TRUE;
2130           }
2131         else
2132           {
2133              if (group->clas->style)
2134                style = group->clas->style;
2135
2136              if (group->clas->func.icon_get)
2137                icon = group->clas->func.icon_get(group->wd->obj, group->clas->data);
2138
2139              group->delete_object = EINA_FALSE;
2140           }
2141
2142         group->obj = elm_layout_add(group->wd->obj);
2143         elm_layout_theme_set(group->obj, "map/marker", style, elm_widget_style_get(group->wd->obj));
2144
2145         if (icon) elm_object_part_content_set(group->obj, "elm.icon", icon);
2146
2147         evas_object_smart_member_add(group->obj, group->wd->pan_smart);
2148         elm_widget_sub_object_add(group->wd->obj, group->obj);
2149         evas_object_stack_above(group->obj, group->wd->sep_maps_markers);
2150
2151         if (!group->delete_object)
2152           group->clas->priv.objs_used = eina_list_append(group->clas->priv.objs_used, group->obj);
2153      }
2154    else
2155      {
2156         group->delete_object = EINA_FALSE;
2157
2158         group->obj = eina_list_data_get(group->clas->priv.objs_notused);
2159         group->clas->priv.objs_used = eina_list_append(group->clas->priv.objs_used, group->obj);
2160         group->clas->priv.objs_notused = eina_list_remove(group->clas->priv.objs_notused, group->obj);
2161         evas_object_show(group->obj);
2162      }
2163
2164    edje_object_signal_callback_add(elm_layout_edje_get(group->obj), "open", "elm", _group_open_cb, group);
2165    edje_object_signal_callback_add(elm_layout_edje_get(group->obj), "bringin", "elm", _group_bringin_cb, group);
2166
2167    group->update_nbelems = EINA_TRUE;
2168    group->update_resize = EINA_TRUE;
2169    group->update_raise = EINA_TRUE;
2170
2171    if (group->open) _group_bubble_create(group);
2172 }
2173
2174 static void
2175 _group_object_free(Marker_Group *group)
2176 {
2177    if (!group->obj) return;
2178    if (!group->delete_object)
2179      {
2180         group->clas->priv.objs_notused = eina_list_append(group->clas->priv.objs_notused, group->obj);
2181         group->clas->priv.objs_used = eina_list_remove(group->clas->priv.objs_used, group->obj);
2182         evas_object_hide(group->obj);
2183
2184         edje_object_signal_callback_del(elm_layout_edje_get(group->obj), "open", "elm", _group_open_cb);
2185         edje_object_signal_callback_del(elm_layout_edje_get(group->obj), "bringin", "elm", _group_bringin_cb);
2186      }
2187    else
2188      evas_object_del(group->obj);
2189
2190    group->obj = NULL;
2191    _group_bubble_free(group);
2192 }
2193
2194 static void
2195 _group_bubble_mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2196 {
2197    Marker_Group *group = data;
2198
2199    if (!evas_object_above_get(group->rect)) return;
2200    evas_object_raise(group->bubble);
2201    evas_object_raise(group->sc);
2202    evas_object_raise(group->rect);
2203 }
2204
2205 static void
2206 _group_bubble_create(Marker_Group *group)
2207 {
2208    if (group->bubble) return;
2209
2210    group->wd->opened_bubbles = eina_list_append(group->wd->opened_bubbles, group);
2211    group->bubble = edje_object_add(evas_object_evas_get(group->obj));
2212    _elm_theme_object_set(group->wd->obj, group->bubble, "map", "marker_bubble",
2213                          elm_widget_style_get(group->wd->obj));
2214    evas_object_smart_member_add(group->bubble,
2215                                 group->wd->obj);
2216    elm_widget_sub_object_add(group->wd->obj, group->bubble);
2217
2218    _group_bubble_content_free(group);
2219    if (!_group_bubble_content_update(group))
2220      {
2221         //no content, we can delete the bubble
2222         _group_bubble_free(group);
2223         return;
2224      }
2225
2226    group->rect = evas_object_rectangle_add(evas_object_evas_get(group->obj));
2227    evas_object_color_set(group->rect, 0, 0, 0, 0);
2228    evas_object_repeat_events_set(group->rect, EINA_TRUE);
2229    evas_object_smart_member_add(group->rect, group->wd->obj);
2230    elm_widget_sub_object_add(group->wd->obj, group->rect);
2231
2232    evas_object_event_callback_add(group->rect, EVAS_CALLBACK_MOUSE_UP, _group_bubble_mouse_up_cb, group);
2233
2234    _group_bubble_place(group);
2235 }
2236
2237 static void _bubble_sc_hints_changed_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2238 {
2239    _group_bubble_place(data);
2240 }
2241
2242 static int
2243 _group_bubble_content_update(Marker_Group *group)
2244 {
2245    Eina_List *l;
2246    Elm_Map_Marker *marker;
2247    int i = 0;
2248
2249    if (!group->bubble) return 1;
2250
2251    if (!group->sc)
2252      {
2253         group->sc = elm_scroller_add(group->bubble);
2254         elm_widget_style_set(group->sc, "map_bubble");
2255         elm_scroller_content_min_limit(group->sc, EINA_FALSE, EINA_TRUE);
2256         elm_scroller_policy_set(group->sc, ELM_SCROLLER_POLICY_AUTO, ELM_SCROLLER_POLICY_OFF);
2257         elm_scroller_bounce_set(group->sc, _elm_config->thumbscroll_bounce_enable, EINA_FALSE);
2258         edje_object_part_swallow(group->bubble, "elm.swallow.content", group->sc);
2259         evas_object_show(group->sc);
2260         evas_object_smart_member_add(group->sc,
2261                                      group->wd->obj);
2262         elm_widget_sub_object_add(group->wd->obj, group->sc);
2263
2264         group->bx = elm_box_add(group->bubble);
2265         evas_object_size_hint_align_set(group->bx, EVAS_HINT_FILL, EVAS_HINT_FILL);
2266         evas_object_size_hint_weight_set(group->bx, 0.5, 0.5);
2267         elm_box_horizontal_set(group->bx, EINA_TRUE);
2268         evas_object_show(group->bx);
2269
2270         elm_object_content_set(group->sc, group->bx);
2271
2272         evas_object_event_callback_add(group->sc, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
2273                                        _bubble_sc_hints_changed_cb, group);
2274      }
2275
2276    EINA_LIST_FOREACH(group->markers, l, marker)
2277      {
2278         if (i >= group->wd->markers_max_num) break;
2279         if ((!marker->content) && (marker->clas->func.get))
2280           marker->content = marker->clas->func.get(group->wd->obj, marker, marker->data);
2281         else if (marker->content)
2282           elm_box_unpack(group->bx, marker->content);
2283         if (marker->content)
2284           {
2285              elm_box_pack_end(group->bx, marker->content);
2286              i++;
2287           }
2288      }
2289    return i;
2290 }
2291
2292 static void
2293 _group_bubble_content_free(Marker_Group *group)
2294 {
2295    Eina_List *l;
2296    Elm_Map_Marker *marker;
2297
2298    if (!group->sc) return;
2299    EINA_LIST_FOREACH(group->markers, l, marker)
2300      {
2301         if ((marker->content) && (marker->clas->func.del))
2302           marker->clas->func.del(group->wd->obj, marker, marker->data, marker->content);
2303         else if (marker->content)
2304           evas_object_del(marker->content);
2305         marker->content = NULL;
2306      }
2307    evas_object_del(group->sc);
2308    group->sc = NULL;
2309 }
2310
2311 static void
2312 _group_bubble_free(Marker_Group *group)
2313 {
2314    if (!group->bubble) return;
2315    group->wd->opened_bubbles = eina_list_remove(group->wd->opened_bubbles, group);
2316    evas_object_event_callback_del_full(group->sc, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
2317                                        _bubble_sc_hints_changed_cb, group);
2318    evas_object_del(group->bubble);
2319    evas_object_del(group->rect);
2320    group->bubble = NULL;
2321    _group_bubble_content_free(group);
2322 }
2323
2324 static void
2325 _group_bubble_place(Marker_Group *group)
2326 {
2327    Evas_Coord x, y, w;
2328    Evas_Coord xx, yy, ww, hh;
2329    const char *s;
2330
2331    if ((!group->bubble) || (!group->obj)) return;
2332
2333    evas_object_geometry_get(group->obj, &x, &y, &w, NULL);
2334    edje_object_size_min_calc(group->bubble, NULL, &hh);
2335
2336    s = edje_object_data_get(group->bubble, "size_w");
2337    if (s) ww = atoi(s);
2338    else ww = 0;
2339    xx = x + w / 2 - ww / 2;
2340    yy = y-hh;
2341
2342    evas_object_move(group->bubble, xx, yy);
2343    evas_object_resize(group->bubble, ww, hh);
2344    obj_rotate_zoom(group->wd, group->bubble);
2345    evas_object_show(group->bubble);
2346
2347    evas_object_move(group->rect, xx, yy);
2348    evas_object_resize(group->rect, ww, hh);
2349    obj_rotate_zoom(group->wd, group->rect);
2350    evas_object_show(group->rect);
2351 }
2352
2353 static void
2354 _group_bringin_cb(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *soure __UNUSED__)
2355 {
2356    Marker_Group *group = data;
2357    Elm_Map_Marker *marker = eina_list_data_get(group->markers);
2358    if (!marker) return;
2359    group->bringin = EINA_TRUE;
2360    elm_map_geo_region_bring_in(group->wd->obj, marker->longitude, marker->latitude);
2361 }
2362
2363 static void
2364 _group_open_cb(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *soure __UNUSED__)
2365 {
2366    Marker_Group *group = data;
2367
2368    if (group->bringin)
2369      {
2370         group->bringin = EINA_FALSE;
2371         return;
2372      }
2373
2374    if (group->bubble)
2375      {
2376         group->open = EINA_FALSE;
2377         _group_bubble_free(group);
2378         return;
2379      }
2380    group->open = EINA_TRUE;
2381    _group_bubble_create(group);
2382 }
2383
2384 static Eina_Bool
2385 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
2386 {
2387    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2388    Widget_Data *wd = elm_widget_data_get(obj);
2389    int zoom;
2390    Evas_Coord x = 0;
2391    Evas_Coord y = 0;
2392    Evas_Coord step_x = 0;
2393    Evas_Coord step_y = 0;
2394    Evas_Coord v_w = 0;
2395    Evas_Coord v_h = 0;
2396    Evas_Coord page_x = 0;
2397    Evas_Coord page_y = 0;
2398
2399    if (!wd) return EINA_FALSE;
2400    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
2401    Evas_Event_Key_Down *ev = event_info;
2402    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
2403
2404    elm_smart_scroller_child_pos_get(wd->scr, &x, &y);
2405    elm_smart_scroller_step_size_get(wd->scr, &step_x, &step_y);
2406    elm_smart_scroller_page_size_get(wd->scr, &page_x, &page_y);
2407    elm_smart_scroller_child_viewport_size_get(wd->scr, &v_w, &v_h);
2408
2409    if ((!strcmp(ev->keyname, "Left")) || (!strcmp(ev->keyname, "KP_Left")))
2410      {
2411         x -= step_x;
2412      }
2413    else if ((!strcmp(ev->keyname, "Right")) || (!strcmp(ev->keyname, "KP_Right")))
2414      {
2415         x += step_x;
2416      }
2417    else if ((!strcmp(ev->keyname, "Up"))  || (!strcmp(ev->keyname, "KP_Up")))
2418      {
2419         y -= step_y;
2420      }
2421    else if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
2422      {
2423         y += step_y;
2424      }
2425    else if ((!strcmp(ev->keyname, "Prior")) || (!strcmp(ev->keyname, "KP_Prior")))
2426      {
2427         if (page_y < 0)
2428           y -= -(page_y * v_h) / 100;
2429         else
2430           y -= page_y;
2431      }
2432    else if ((!strcmp(ev->keyname, "Next")) || (!strcmp(ev->keyname, "KP_Next")))
2433      {
2434         if (page_y < 0)
2435           y += -(page_y * v_h) / 100;
2436         else
2437           y += page_y;
2438      }
2439    else if (!strcmp(ev->keyname, "KP_Add"))
2440      {
2441         zoom = elm_map_zoom_get(obj) + 1;
2442         elm_map_zoom_mode_set(obj, ELM_MAP_ZOOM_MODE_MANUAL);
2443         elm_map_zoom_set(obj, zoom);
2444         return EINA_TRUE;
2445      }
2446    else if (!strcmp(ev->keyname, "KP_Subtract"))
2447      {
2448         zoom = elm_map_zoom_get(obj) - 1;
2449         elm_map_zoom_mode_set(obj, ELM_MAP_ZOOM_MODE_MANUAL);
2450         elm_map_zoom_set(obj, zoom);
2451         return EINA_TRUE;
2452      }
2453    else return EINA_FALSE;
2454
2455    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2456    elm_smart_scroller_child_pos_set(wd->scr, x, y);
2457
2458    return EINA_TRUE;
2459 }
2460
2461 static Eina_Bool
2462 cb_dump_name_attrs(void *data, const char *key, const char *value)
2463 {
2464    Name_Dump *dump = (Name_Dump*)data;
2465    if (!dump) return EINA_FALSE;
2466
2467    if (!strncmp(key, NOMINATIM_ATTR_LON, sizeof(NOMINATIM_ATTR_LON))) dump->lon = atof(value);
2468    else if (!strncmp(key, NOMINATIM_ATTR_LAT, sizeof(NOMINATIM_ATTR_LAT))) dump->lat = atof(value);
2469
2470    return EINA_TRUE;
2471 }
2472
2473
2474 static Eina_Bool
2475 cb_route_dump(void *data, Eina_Simple_XML_Type type, const char *value, unsigned offset __UNUSED__, unsigned length)
2476 {
2477    Route_Dump *dump = data;
2478    if (!dump) return EINA_FALSE;
2479
2480    switch (type)
2481      {
2482       case EINA_SIMPLE_XML_OPEN:
2483       case EINA_SIMPLE_XML_OPEN_EMPTY:
2484         {
2485            const char *attrs;
2486
2487            attrs = eina_simple_xml_tag_attributes_find(value, length);
2488            if (!attrs)
2489              {
2490                 if (!strncmp(value, YOURS_DISTANCE, length)) dump->id = ROUTE_XML_DISTANCE;
2491                 else if (!strncmp(value, YOURS_DESCRIPTION, length)) dump->id = ROUTE_XML_DESCRIPTION;
2492                 else if (!strncmp(value, YOURS_COORDINATES, length)) dump->id = ROUTE_XML_COORDINATES;
2493                 else dump->id = ROUTE_XML_NONE;
2494              }
2495          }
2496         break;
2497       case EINA_SIMPLE_XML_DATA:
2498         {
2499            char *buf = malloc(length);
2500            if (!buf) return EINA_FALSE;
2501            snprintf(buf, length, "%s", value);
2502            if (dump->id == ROUTE_XML_DISTANCE) dump->distance = atof(buf);
2503            else if (!(dump->description) && (dump->id == ROUTE_XML_DESCRIPTION)) dump->description = strdup(buf);
2504            else if (dump->id == ROUTE_XML_COORDINATES) dump->coordinates = strdup(buf);
2505            free(buf);
2506         }
2507         break;
2508       default:
2509         break;
2510      }
2511
2512    return EINA_TRUE;
2513 }
2514
2515 static Eina_Bool
2516 cb_name_dump(void *data, Eina_Simple_XML_Type type, const char *value, unsigned offset __UNUSED__, unsigned length)
2517 {
2518    Name_Dump *dump = data;
2519    if (!dump) return EINA_FALSE;
2520
2521    switch (type)
2522      {
2523       case EINA_SIMPLE_XML_OPEN:
2524       case EINA_SIMPLE_XML_OPEN_EMPTY:
2525         {
2526            const char *attrs;
2527            attrs = eina_simple_xml_tag_attributes_find(value, length);
2528            if (attrs)
2529              {
2530                 if (!strncmp(value, NOMINATIM_RESULT, sizeof(NOMINATIM_RESULT) - 1)) dump->id = NAME_XML_NAME;
2531                 else dump->id = NAME_XML_NONE;
2532
2533                 eina_simple_xml_attributes_parse
2534                   (attrs, length - (attrs - value), cb_dump_name_attrs, dump);
2535              }
2536         }
2537         break;
2538       case EINA_SIMPLE_XML_DATA:
2539         {
2540            char *buf = malloc(length + 1);
2541            if (!buf) return EINA_FALSE;
2542            snprintf(buf, length + 1, "%s", value);
2543            if (dump->id == NAME_XML_NAME) dump->address = strdup(buf);
2544            free(buf);
2545         }
2546         break;
2547       default:
2548         break;
2549      }
2550
2551    return EINA_TRUE;
2552 }
2553
2554 static void
2555 _parse_kml(void *data)
2556 {
2557    Elm_Map_Route *r = (Elm_Map_Route*)data;
2558    if (!r || !r->ud.fname) return;
2559
2560    FILE *f;
2561    char **str;
2562    unsigned int ele, idx;
2563    double lon, lat;
2564    Evas_Object *path;
2565
2566    Route_Dump dump = {0, r->ud.fname, 0.0, NULL, NULL};
2567
2568    f = fopen(r->ud.fname, "rb");
2569    if (f)
2570      {
2571         long sz;
2572
2573         fseek(f, 0, SEEK_END);
2574         sz = ftell(f);
2575         if (sz > 0)
2576           {
2577              char *buf;
2578
2579              fseek(f, 0, SEEK_SET);
2580              buf = malloc(sz);
2581              if (buf)
2582                {
2583                   if (fread(buf, 1, sz, f))
2584                     {
2585                        eina_simple_xml_parse(buf, sz, EINA_TRUE, cb_route_dump, &dump);
2586                        free(buf);
2587                     }
2588                }
2589           }
2590         fclose(f);
2591
2592         if (dump.distance) r->info.distance = dump.distance;
2593         if (dump.description)
2594           {
2595              eina_stringshare_replace(&r->info.waypoints, dump.description);
2596              str = eina_str_split_full(dump.description, "\n", 0, &ele);
2597              r->info.waypoint_count = ele;
2598              for (idx = 0 ; idx < ele ; idx++)
2599                {
2600                   Path_Waypoint *wp = ELM_NEW(Path_Waypoint);
2601                   if (wp)
2602                     {
2603                        wp->wd = r->wd;
2604                        wp->point = eina_stringshare_add(str[idx]);
2605                        DBG("%s", str[idx]);
2606                        r->waypoint = eina_list_append(r->waypoint, wp);
2607                     }
2608                }
2609              if (str && str[0])
2610                {
2611                   free(str[0]);
2612                   free(str);
2613                }
2614           }
2615         else WRN("description is not found !");
2616
2617         if (dump.coordinates)
2618           {
2619              eina_stringshare_replace(&r->info.nodes, dump.coordinates);
2620              str = eina_str_split_full(dump.coordinates, "\n", 0, &ele);
2621              r->info.node_count = ele;
2622              for (idx = 0 ; idx < ele ; idx++)
2623                {
2624                   sscanf(str[idx], "%lf,%lf", &lon, &lat);
2625                   Path_Node *n = ELM_NEW(Path_Node);
2626                   if (n)
2627                     {
2628                        n->wd = r->wd;
2629                        n->pos.lon = lon;
2630                        n->pos.lat = lat;
2631                        n->idx = idx;
2632                        DBG("%lf:%lf", lon, lat);
2633                        n->pos.address = NULL;
2634                        r->nodes = eina_list_append(r->nodes, n);
2635
2636                        path = evas_object_polygon_add(evas_object_evas_get(r->wd->obj));
2637                        evas_object_smart_member_add(path, r->wd->pan_smart);
2638                        r->path = eina_list_append(r->path, path);
2639                     }
2640                }
2641              if (str && str[0])
2642                {
2643                   free(str[0]);
2644                   free(str);
2645                }
2646           }
2647      }
2648 }
2649
2650 static void
2651 _parse_name(void *data)
2652 {
2653    Elm_Map_Name *n = (Elm_Map_Name*)data;
2654    if (!n || !n->ud.fname) return;
2655
2656    FILE *f;
2657
2658    Name_Dump dump = {0, NULL, 0.0, 0.0};
2659
2660    f = fopen(n->ud.fname, "rb");
2661    if (f)
2662      {
2663         long sz;
2664
2665         fseek(f, 0, SEEK_END);
2666         sz = ftell(f);
2667         if (sz > 0)
2668           {
2669              char *buf;
2670
2671              fseek(f, 0, SEEK_SET);
2672              buf = malloc(sz);
2673              if (buf)
2674                {
2675                   if (fread(buf, 1, sz, f))
2676                     {
2677                        eina_simple_xml_parse(buf, sz, EINA_TRUE, cb_name_dump, &dump);
2678                        free(buf);
2679                     }
2680                }
2681           }
2682         fclose(f);
2683
2684         if (dump.address)
2685           {
2686              INF("[%lf : %lf] ADDRESS : %s", n->lon, n->lat, dump.address);
2687              n->address = strdup(dump.address);
2688           }
2689         n->lon = dump.lon;
2690         n->lat = dump.lat;
2691      }
2692 }
2693
2694 Grid *_get_current_grid(Widget_Data *wd)
2695 {
2696    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, NULL);
2697    Eina_List *l;
2698    Grid *g = NULL, *ret = NULL;
2699    EINA_LIST_FOREACH(wd->grids, l, g)
2700      {
2701         if (wd->zoom == g->zoom)
2702           {
2703              ret = g;
2704              break;
2705           }
2706      }
2707    return ret;
2708 }
2709
2710 static Eina_Bool
2711 _route_complete_cb(void *data, int ev_type __UNUSED__, void *event)
2712 {
2713    Ecore_Con_Event_Url_Complete *ev = event;
2714    Elm_Map_Route *r = (Elm_Map_Route*)data;
2715    Widget_Data *wd = r->wd;
2716
2717    if ((!r) || (!ev)) return EINA_TRUE;
2718    Elm_Map_Route *rr = ecore_con_url_data_get(r->con_url);
2719    ecore_con_url_data_set(r->con_url, NULL);
2720    if (r!=rr) return EINA_TRUE;
2721
2722    if (r->ud.fd) fclose(r->ud.fd);
2723    _parse_kml(r);
2724
2725    if (wd->grids)
2726      {
2727         Grid *g;
2728         Evas_Coord ox, oy, ow, oh;
2729         evas_object_geometry_get(wd->obj, &ox, &oy, &ow, &oh);
2730         g = _get_current_grid(wd);
2731         route_place(wd->obj, g, wd->pan_x, wd->pan_y, ox, oy, ow, oh);
2732      }
2733    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
2734                            "elm,state,busy,stop", "elm");
2735    evas_object_smart_callback_call(wd->obj, SIG_ROUTE_LOADED, NULL);
2736    return EINA_TRUE;
2737 }
2738
2739 static Eina_Bool
2740 _name_complete_cb(void *data, int ev_type __UNUSED__, void *event)
2741 {
2742    Ecore_Con_Event_Url_Complete *ev = event;
2743    Elm_Map_Name *n = (Elm_Map_Name*)data;
2744    Widget_Data *wd = n->wd;
2745
2746    if ((!n) || (!ev)) return EINA_TRUE;
2747    Elm_Map_Name *nn = ecore_con_url_data_get(n->con_url);
2748    ecore_con_url_data_set(n->con_url, NULL);
2749    if (n!=nn) return EINA_TRUE;
2750
2751    if (n->ud.fd) fclose(n->ud.fd);
2752    _parse_name(n);
2753
2754    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
2755                            "elm,state,busy,stop", "elm");
2756    evas_object_smart_callback_call(wd->obj, SIG_NAME_LOADED, NULL);
2757    return EINA_TRUE;
2758 }
2759
2760 static Elm_Map_Name *
2761 _utils_convert_name(const Evas_Object *obj, int method, char *address, double lon, double lat)
2762 {
2763    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2764    Widget_Data *wd = elm_widget_data_get(obj);
2765    char buf[PATH_MAX];
2766    char *source;
2767    int fd;
2768
2769    if ((!wd) || (!wd->src)) return NULL;
2770    Elm_Map_Name *name = ELM_NEW(Elm_Map_Name);
2771    if (!name) return NULL;
2772
2773    snprintf(buf, sizeof(buf), DEST_NAME_XML_FILE);
2774    fd = mkstemp(buf);
2775    if (fd < 0)
2776      {
2777         free(name);
2778         return NULL;
2779      }
2780
2781    name->con_url = ecore_con_url_new(NULL);
2782    name->ud.fname = strdup(buf);
2783    INF("xml file : %s", name->ud.fname);
2784
2785    name->ud.fd = fdopen(fd, "w+");
2786    if ((!name->con_url) || (!name->ud.fd))
2787      {
2788         ecore_con_url_free(name->con_url);
2789         free(name);
2790         return NULL;
2791      }
2792
2793    name->wd = wd;
2794    name->handler = ecore_event_handler_add (ECORE_CON_EVENT_URL_COMPLETE, _name_complete_cb, name);
2795    name->method = method;
2796    if (method == ELM_MAP_NAME_METHOD_SEARCH) name->address = strdup(address);
2797    else if (method == ELM_MAP_NAME_METHOD_REVERSE) name->address = NULL;
2798    name->lon = lon;
2799    name->lat = lat;
2800
2801    source = wd->src->name_url_cb(wd->obj, method, address, lon, lat);
2802    INF("name url = %s", source);
2803
2804    wd->names = eina_list_append(wd->names, name);
2805    ecore_con_url_url_set(name->con_url, source);
2806    ecore_con_url_fd_set(name->con_url, fileno(name->ud.fd));
2807    ecore_con_url_data_set(name->con_url, name);
2808
2809    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
2810                            "elm,state,busy,start", "elm");
2811    evas_object_smart_callback_call(wd->obj, SIG_NAME_LOAD, NULL);
2812    ecore_con_url_get(name->con_url);
2813    if (source) free(source);
2814
2815    return name;
2816
2817 }
2818
2819 static Evas_Event_Flags
2820 zoom_start_cb(void *data, void *event_info __UNUSED__)
2821 {
2822    Widget_Data *wd = data;
2823    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EVAS_EVENT_FLAG_NONE);
2824
2825    // FIXME: scroller can be jumping strangely when resizing & scrolling at the sametime (els_scr bug?)
2826    elm_smart_scroller_hold_set(wd->scr, 1);
2827    elm_smart_scroller_freeze_set(wd->scr, 1);
2828    _scr_drag_start(wd->obj, NULL, NULL);
2829
2830    return EVAS_EVENT_FLAG_NONE;
2831 }
2832
2833 static Evas_Event_Flags
2834 zoom_end_cb(void *data, void *event_info __UNUSED__)
2835 {
2836    Widget_Data *wd = data;
2837    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EVAS_EVENT_FLAG_NONE);
2838
2839    // FIXME: scroller can be jumping strangely when resizing & scrolling at the sametime (els_scr bug?)
2840    elm_smart_scroller_hold_set(wd->scr, 0);
2841    elm_smart_scroller_freeze_set(wd->scr, 0);
2842    _scr_drag_stop(wd->obj, NULL, NULL);
2843
2844    wd->pinch.diff = wd->pinch.level;
2845
2846    return EVAS_EVENT_FLAG_NONE;
2847 }
2848
2849 static Evas_Event_Flags
2850 zoom_cb(void *data, void *event_info)
2851 {
2852    Widget_Data *wd = data;
2853    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EVAS_EVENT_FLAG_NONE);
2854
2855    if (!wd->paused)
2856      {
2857         int zoom_diff = 0;
2858         int x, y, w, h;
2859         Elm_Gesture_Zoom_Info *ei = event_info;
2860         evas_object_geometry_get(wd->obj, &x, &y, &w, &h);
2861
2862         wd->pinch.level = wd->pinch.diff * ei->zoom;
2863         wd->pinch.cx = x + ((double)w * 0.5);
2864         wd->pinch.cy = y + ((double)h * 0.5);
2865
2866         if (wd->pinch.level > 2.0 || wd->pinch.level < 1.0)
2867           {
2868              if (wd->pinch.level > 2.0)
2869                {
2870                   zoom_diff = 1;
2871                   wd->pinch.diff = 1.0;
2872                   wd->pinch.level = 1.0;
2873                }
2874              else if (wd->pinch.level < 1.0)
2875                {
2876                   zoom_diff = -1;
2877                   wd->pinch.diff = 2.0;
2878                   wd->pinch.level = 2.0;
2879                }
2880              Elm_Map_Zoom_Mode temp;
2881              elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ZOOM, ELM_GESTURE_STATE_MOVE, NULL, wd);  // ei->zoom is refreshed
2882              temp = wd->mode;
2883              wd->mode = ELM_MAP_ZOOM_MODE_MANUAL;
2884              wd->paused = EINA_TRUE;
2885              elm_map_zoom_set(wd->obj, wd->zoom + zoom_diff);
2886              wd->paused = EINA_FALSE;
2887              wd->mode = temp;
2888              elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ZOOM, ELM_GESTURE_STATE_MOVE, zoom_cb, wd); // ei->zoom is refreshed
2889           }
2890         else
2891           {
2892              if (wd->calc_job) ecore_job_del(wd->calc_job);
2893              wd->calc_job = ecore_job_add(_calc_job, wd);
2894           }
2895         evas_object_smart_callback_call(wd->obj, SIG_ZOOM_CHANGE, NULL);
2896      }
2897
2898    // FIXME: scroller can be jumping strangely when resizing & scrolling at the sametime (els_scr bug?)
2899    _scr_scroll(wd->obj, NULL, NULL);
2900
2901    return EVAS_EVENT_FLAG_NONE;
2902 }
2903
2904 static Evas_Event_Flags
2905 rotate_cb(void *data, void *event_info)
2906 {
2907    Widget_Data *wd = data;
2908    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EVAS_EVENT_FLAG_NONE);
2909
2910    if (!wd->paused)
2911      {
2912         int x, y, w, h;
2913         Elm_Gesture_Rotate_Info *ei = event_info;
2914         evas_object_geometry_get(wd->obj, &x, &y, &w, &h);
2915
2916         wd->rotate.d = wd->rotate.a + (ei->base_angle-ei->angle)*50;
2917         wd->rotate.cx = x + ((double)w * 0.5);
2918         wd->rotate.cy = y + ((double)h * 0.5);
2919
2920         if (wd->calc_job) ecore_job_del(wd->calc_job);
2921         wd->calc_job = ecore_job_add(_calc_job, wd);
2922      }
2923    return EVAS_EVENT_FLAG_NONE;
2924 }
2925
2926 static Evas_Event_Flags
2927 rotate_end_cb(void *data, void *event_info __UNUSED__)
2928 {
2929    Widget_Data *wd = data;
2930    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EVAS_EVENT_FLAG_NONE);
2931
2932    wd->rotate.a = wd->rotate.d;
2933
2934    return EVAS_EVENT_FLAG_NONE;
2935 }
2936
2937 #endif
2938
2939 EAPI Evas_Object *
2940 elm_map_add(Evas_Object *parent)
2941 {
2942 #ifdef HAVE_ELEMENTARY_ECORE_CON
2943    Evas *e;
2944    Widget_Data *wd;
2945    Evas_Coord minw, minh;
2946    Evas_Object *obj;
2947    static Evas_Smart *smart = NULL;
2948    Eina_Bool bounce = _elm_config->thumbscroll_bounce_enable;
2949
2950    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
2951
2952    ELM_SET_WIDTYPE(widtype, "map");
2953    elm_widget_type_set(obj, "map");
2954    elm_widget_sub_object_add(parent, obj);
2955    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
2956    elm_widget_data_set(obj, wd);
2957    elm_widget_del_hook_set(obj, _del_hook);
2958    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
2959    elm_widget_theme_hook_set(obj, _theme_hook);
2960    elm_widget_can_focus_set(obj, EINA_TRUE);
2961    elm_widget_event_hook_set(obj, _event_hook);
2962
2963    wd->scr = elm_smart_scroller_add(e);
2964    elm_smart_scroller_widget_set(wd->scr, obj);
2965    elm_smart_scroller_object_theme_set(obj, wd->scr, "map", "base", "default");
2966    evas_object_smart_callback_add(wd->scr, "scroll", _scr, obj);
2967    evas_object_smart_callback_add(wd->scr, "drag", _scr, obj);
2968    elm_widget_resize_object_set(obj, wd->scr);
2969    elm_smart_scroller_wheel_disabled_set(wd->scr, EINA_TRUE);
2970
2971    evas_object_smart_callback_add(wd->scr, "animate,start", _scr_anim_start, obj);
2972    evas_object_smart_callback_add(wd->scr, "animate,stop", _scr_anim_stop, obj);
2973    evas_object_smart_callback_add(wd->scr, "drag,start", _scr_drag_start, obj);
2974    evas_object_smart_callback_add(wd->scr, "drag,stop", _scr_drag_stop, obj);
2975    evas_object_smart_callback_add(wd->scr, "scroll", _scr_scroll, obj);
2976
2977    elm_smart_scroller_bounce_allow_set(wd->scr, bounce, bounce);
2978
2979    wd->zoom_min = 0xFF;
2980    wd->zoom_max = 0X00;
2981    source_init(obj);
2982
2983    wd->obj = obj;
2984    wd->map = evas_map_new(4);
2985    if (!wd->map) return NULL;
2986
2987    wd->markers_max_num = 30;
2988    wd->pinch.level = 1.0;
2989    wd->pinch.diff = 1.0;
2990    wd->markers = calloc(wd->zoom_max + 1, sizeof(void*));
2991
2992    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
2993    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
2994    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
2995    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
2996
2997    if (!smart)
2998      {
2999         static Evas_Smart_Class sc;
3000
3001         evas_object_smart_clipped_smart_set(&_pan_sc);
3002         sc = _pan_sc;
3003         sc.name = "elm_map_pan";
3004         sc.version = EVAS_SMART_CLASS_VERSION;
3005         sc.add = _pan_add;
3006         sc.del = _pan_del;
3007         sc.resize = _pan_resize;
3008         sc.move = _pan_move;
3009         sc.calculate = _pan_calculate;
3010         smart = evas_smart_class_new(&sc);
3011      }
3012    if (smart)
3013      {
3014         wd->pan_smart = evas_object_smart_add(e, smart);
3015         wd->pan = evas_object_smart_data_get(wd->pan_smart);
3016         wd->pan->wd = wd;
3017      }
3018
3019    elm_smart_scroller_extern_pan_set(wd->scr, wd->pan_smart,
3020                                      _pan_set, _pan_get, _pan_max_get,
3021                                      _pan_min_get, _pan_child_size_get);
3022
3023    wd->rect = evas_object_rectangle_add(e);
3024    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_RESIZE,
3025                                   _rect_resize_cb, obj);
3026    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_DOWN,
3027                                   _mouse_down, obj);
3028    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_UP,
3029                                   _mouse_up, obj);
3030    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_WHEEL,
3031                                   _mouse_wheel_cb, obj);
3032
3033    evas_object_smart_member_add(wd->rect, wd->pan_smart);
3034    elm_widget_sub_object_add(obj, wd->rect);
3035    evas_object_show(wd->rect);
3036    evas_object_color_set(wd->rect, 0, 0, 0, 0);
3037
3038    wd->ges = elm_gesture_layer_add(obj);
3039    if (!wd->ges) ERR("elm_gesture_layer_add() failed");
3040    elm_gesture_layer_attach(wd->ges, wd->rect);
3041    elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ZOOM, ELM_GESTURE_STATE_START,
3042                             zoom_start_cb, wd);
3043    elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ZOOM, ELM_GESTURE_STATE_MOVE,
3044                             zoom_cb, wd);
3045    elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ZOOM, ELM_GESTURE_STATE_END,
3046                             zoom_end_cb, wd);
3047    elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ZOOM, ELM_GESTURE_STATE_ABORT,
3048                             zoom_end_cb, wd);
3049    elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ROTATE, ELM_GESTURE_STATE_MOVE,
3050                             rotate_cb, wd);
3051    elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ROTATE, ELM_GESTURE_STATE_END,
3052                             rotate_end_cb, wd);
3053    elm_gesture_layer_cb_set(wd->ges, ELM_GESTURE_ROTATE, ELM_GESTURE_STATE_ABORT,
3054                             rotate_end_cb, wd);
3055
3056    wd->mode = ELM_MAP_ZOOM_MODE_MANUAL;
3057    wd->id = ((int)getpid() << 16) | idnum;
3058    idnum++;
3059
3060    wd->tsize = 256;
3061    edje_object_size_min_calc(elm_smart_scroller_edje_object_get(wd->scr),
3062                              &minw, &minh);
3063    evas_object_size_hint_min_set(obj, minw, minh);
3064
3065    wd->sep_maps_markers = evas_object_rectangle_add(evas_object_evas_get(obj));
3066    evas_object_smart_member_add(wd->sep_maps_markers, wd->pan_smart);
3067
3068    grid_create_all(obj);
3069
3070    wd->paused = EINA_TRUE;
3071    elm_map_zoom_set(obj, 0);
3072    wd->paused = EINA_FALSE;
3073    _sizing_eval(obj);
3074
3075    // TODO: convert Elementary to subclassing of Evas_Smart_Class
3076    // TODO: and save some bytes, making descriptions per-class and not instance!
3077    evas_object_smart_callbacks_descriptions_set(obj, _signals);
3078
3079    if (!ecore_file_download_protocol_available("http://"))
3080      {
3081         ERR("Ecore must be built with curl support for the map widget!");
3082      }
3083
3084    return obj;
3085 #else
3086    (void) parent;
3087    return NULL;
3088 #endif
3089 }
3090
3091 EAPI void
3092 elm_map_zoom_set(Evas_Object *obj, int zoom)
3093 {
3094 #ifdef HAVE_ELEMENTARY_ECORE_CON
3095    ELM_CHECK_WIDTYPE(obj, widtype);
3096    Widget_Data *wd = elm_widget_data_get(obj);
3097    Eina_List *l;
3098    Evas_Coord rx, ry, rw, rh;
3099    Evas_Object *p;
3100    Elm_Map_Route *r;
3101    Evas_Object *route;
3102    int z = 0;
3103
3104    EINA_SAFETY_ON_NULL_RETURN(wd);
3105    EINA_SAFETY_ON_NULL_RETURN(wd->src);
3106    if (wd->zoom_animator) return;
3107
3108    if (zoom < 0) zoom = 0;
3109    if (zoom > wd->src->zoom_max) zoom = wd->src->zoom_max;
3110    if (zoom < wd->src->zoom_min) zoom = wd->src->zoom_min;
3111
3112    if ((wd->zoom - zoom) > 0) wd->zoom_method = ZOOM_METHOD_OUT;
3113    else if ((wd->zoom - zoom) < 0) wd->zoom_method = ZOOM_METHOD_IN;
3114    else wd->zoom_method = ZOOM_METHOD_NONE;
3115
3116    wd->zoom = zoom;
3117    wd->size.ow = wd->size.w;
3118    wd->size.oh = wd->size.h;
3119    elm_smart_scroller_child_pos_get(wd->scr, &rx, &ry);
3120    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3121
3122    EINA_LIST_FOREACH(wd->route, l, r)
3123      {
3124         if (r)
3125           {
3126              EINA_LIST_FOREACH(r->path, l, p)
3127                {
3128                   evas_object_polygon_points_clear(p);
3129                }
3130           }
3131      }
3132
3133    EINA_LIST_FOREACH(wd->track, l, route)
3134      {
3135        evas_object_hide(route);
3136      }
3137
3138    if (wd->mode != ELM_MAP_ZOOM_MODE_MANUAL)
3139      {
3140         int p2w, p2h;
3141         int cumulw, cumulh;
3142
3143         cumulw = wd->tsize;
3144         p2w = 0;
3145         while (cumulw <= rw)
3146           {
3147              p2w++;
3148              cumulw *= 2;
3149           }
3150         p2w--;
3151
3152         cumulh = wd->tsize;
3153         p2h = 0;
3154         while (cumulh <= rh)
3155           {
3156              p2h++;
3157              cumulh *= 2;
3158           }
3159         p2h--;
3160
3161         if (wd->mode == ELM_MAP_ZOOM_MODE_AUTO_FIT)
3162           {
3163              if (p2w < p2h) z = p2w;
3164              else z = p2h;
3165           }
3166         else if (wd->mode == ELM_MAP_ZOOM_MODE_AUTO_FILL)
3167           {
3168              if (p2w > p2h) z = p2w;
3169              else z = p2h;
3170           }
3171         wd->zoom = z;
3172      }
3173
3174    wd->size.nw = pow(2.0, wd->zoom) * wd->tsize;
3175    wd->size.nh = pow(2.0, wd->zoom) * wd->tsize;
3176    wd->t = 1.0;
3177
3178    if ((wd->size.w > 0) && (wd->size.h > 0))
3179      {
3180         wd->size.spos.x = (double)(rx + (rw / 2)) / (double)wd->size.ow;
3181         wd->size.spos.y = (double)(ry + (rh / 2)) / (double)wd->size.oh;
3182      }
3183    else
3184      {
3185         wd->size.spos.x = 0.5;
3186         wd->size.spos.y = 0.5;
3187      }
3188
3189    if (rw > wd->size.ow) wd->size.spos.x = 0.5;
3190    if (rh > wd->size.oh) wd->size.spos.y = 0.5;
3191    if (wd->size.spos.x > 1.0) wd->size.spos.x = 1.0;
3192    if (wd->size.spos.y > 1.0) wd->size.spos.y = 1.0;
3193
3194    if (wd->paused)
3195      {
3196         zoom_do(obj);
3197      }
3198    else
3199      {
3200         if (!wd->zoom_animator)
3201           {
3202              wd->zoom_animator = ecore_animator_add(_zoom_anim, obj);
3203              wd->nosmooth++;
3204              if (wd->nosmooth == 1) _smooth_update(obj);
3205              evas_object_smart_callback_call(obj, SIG_ZOOM_START, NULL);
3206           }
3207      }
3208
3209    if (wd->zoom_method != ZOOM_METHOD_NONE) evas_object_smart_callback_call(obj, SIG_ZOOM_CHANGE, NULL);
3210 #else
3211    (void) obj;
3212    (void) zoom;
3213 #endif
3214 }
3215
3216 EAPI int
3217 elm_map_zoom_get(const Evas_Object *obj)
3218 {
3219 #ifdef HAVE_ELEMENTARY_ECORE_CON
3220    ELM_CHECK_WIDTYPE(obj, widtype) 0;
3221    Widget_Data *wd = elm_widget_data_get(obj);
3222
3223    if (!wd) return 0;
3224    return wd->zoom;
3225 #else
3226    (void) obj;
3227    return 0;
3228 #endif
3229 }
3230
3231 EAPI void
3232 elm_map_zoom_mode_set(Evas_Object *obj, Elm_Map_Zoom_Mode mode)
3233 {
3234 #ifdef HAVE_ELEMENTARY_ECORE_CON
3235    ELM_CHECK_WIDTYPE(obj, widtype);
3236    Widget_Data *wd = elm_widget_data_get(obj);
3237
3238    if (!wd) return;
3239    if (wd->mode == mode) return;
3240    wd->mode = mode;
3241
3242    if (wd->mode != ELM_MAP_ZOOM_MODE_MANUAL)
3243      {
3244         int tz = wd->zoom;
3245         wd->zoom = 0;
3246         elm_map_zoom_set(wd->obj, tz);
3247      }
3248 #else
3249    (void) obj;
3250    (void) mode;
3251 #endif
3252 }
3253
3254 EAPI Elm_Map_Zoom_Mode
3255 elm_map_zoom_mode_get(const Evas_Object *obj)
3256 {
3257 #ifdef HAVE_ELEMENTARY_ECORE_CON
3258    ELM_CHECK_WIDTYPE(obj, widtype) ELM_MAP_ZOOM_MODE_MANUAL;
3259    Widget_Data *wd = elm_widget_data_get(obj);
3260
3261    if (!wd) return ELM_MAP_ZOOM_MODE_MANUAL;
3262    return wd->mode;
3263 #else
3264    (void) obj;
3265    return ELM_MAP_ZOOM_MODE_MANUAL;
3266 #endif
3267 }
3268
3269 EAPI void
3270 elm_map_geo_region_bring_in(Evas_Object *obj, double lon, double lat)
3271 {
3272 #ifdef HAVE_ELEMENTARY_ECORE_CON
3273    ELM_CHECK_WIDTYPE(obj, widtype);
3274    Widget_Data *wd = elm_widget_data_get(obj);
3275    int rx, ry, rw, rh;
3276
3277    if (!wd) return;
3278    elm_map_utils_convert_geo_into_coord(obj, lon, lat, wd->size.w, &rx, &ry);
3279    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3280
3281    rx = rx - rw / 2;
3282    ry = ry - rh / 2;
3283
3284    if (wd->zoom_animator)
3285      {
3286         wd->nosmooth--;
3287         if (!wd->nosmooth) _smooth_update(obj);
3288         ecore_animator_del(wd->zoom_animator);
3289         wd->zoom_animator = NULL;
3290         zoom_do(obj);
3291         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
3292      }
3293    elm_smart_scroller_region_bring_in(wd->scr, rx, ry, rw, rh);
3294
3295    wd->center_on.enabled = EINA_TRUE;
3296    wd->center_on.lon = lon;
3297    wd->center_on.lat = lat;
3298 #else
3299    (void) obj;
3300    (void) lon;
3301    (void) lat;
3302 #endif
3303 }
3304
3305 EAPI void
3306 elm_map_geo_region_show(Evas_Object *obj, double lon, double lat)
3307 {
3308 #ifdef HAVE_ELEMENTARY_ECORE_CON
3309    ELM_CHECK_WIDTYPE(obj, widtype);
3310    Widget_Data *wd = elm_widget_data_get(obj);
3311    int rx, ry, rw, rh;
3312
3313    if (!wd) return;
3314    elm_map_utils_convert_geo_into_coord(obj, lon, lat, wd->size.w, &rx, &ry);
3315    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3316
3317    rx = rx - rw / 2;
3318    ry = ry - rh / 2;
3319
3320    if (wd->zoom_animator)
3321      {
3322         wd->nosmooth--;
3323         ecore_animator_del(wd->zoom_animator);
3324         wd->zoom_animator = NULL;
3325         zoom_do(obj);
3326         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
3327      }
3328    elm_smart_scroller_child_region_show(wd->scr, rx, ry, rw, rh);
3329
3330    wd->center_on.enabled = EINA_TRUE;
3331    wd->center_on.lon = lon;
3332    wd->center_on.lat = lat;
3333 #else
3334    (void) obj;
3335    (void) lon;
3336    (void) lat;
3337 #endif
3338 }
3339
3340 EAPI void
3341 elm_map_geo_region_get(const Evas_Object *obj, double *lon, double *lat)
3342 {
3343 #ifdef HAVE_ELEMENTARY_ECORE_CON
3344    ELM_CHECK_WIDTYPE(obj, widtype);
3345    Widget_Data *wd = elm_widget_data_get(obj);
3346    Evas_Coord sx, sy, sw, sh;
3347
3348    if (!wd) return;
3349    elm_smart_scroller_child_pos_get(wd->scr, &sx, &sy);
3350    elm_smart_scroller_child_viewport_size_get(wd->scr, &sw, &sh);
3351    sx += sw / 2;
3352    sy += sh / 2;
3353
3354    elm_map_utils_convert_coord_into_geo(obj, sx, sy, wd->size.w, lon, lat);
3355 #else
3356    (void) obj;
3357    (void) lon;
3358    (void) lat;
3359 #endif
3360 }
3361
3362 EAPI void
3363 elm_map_paused_set(Evas_Object *obj, Eina_Bool paused)
3364 {
3365 #ifdef HAVE_ELEMENTARY_ECORE_CON
3366    ELM_CHECK_WIDTYPE(obj, widtype);
3367    Widget_Data *wd = elm_widget_data_get(obj);
3368
3369    if (!wd) return;
3370    if (wd->paused == !!paused) return;
3371    wd->paused = paused;
3372    if (wd->paused)
3373      {
3374         if (wd->zoom_animator)
3375           {
3376              if (wd->zoom_animator) ecore_animator_del(wd->zoom_animator);
3377              wd->zoom_animator = NULL;
3378              zoom_do(obj);
3379              evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
3380           }
3381      }
3382 #else
3383    (void) obj;
3384    (void) paused;
3385 #endif
3386 }
3387
3388 EAPI void
3389 elm_map_paused_markers_set(Evas_Object *obj, Eina_Bool paused)
3390 {
3391 #ifdef HAVE_ELEMENTARY_ECORE_CON
3392    ELM_CHECK_WIDTYPE(obj, widtype);
3393    Widget_Data *wd = elm_widget_data_get(obj);
3394
3395    if (!wd) return;
3396    if (wd->paused_markers == !!paused) return;
3397    wd->paused_markers = paused;
3398 #else
3399    (void) obj;
3400    (void) paused;
3401 #endif
3402 }
3403
3404 EAPI Eina_Bool
3405 elm_map_paused_get(const Evas_Object *obj)
3406 {
3407 #ifdef HAVE_ELEMENTARY_ECORE_CON
3408    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
3409    Widget_Data *wd = elm_widget_data_get(obj);
3410
3411    if (!wd) return EINA_FALSE;
3412    return wd->paused;
3413 #else
3414    (void) obj;
3415    return EINA_FALSE;
3416 #endif
3417 }
3418
3419 EAPI Eina_Bool
3420 elm_map_paused_markers_get(const Evas_Object *obj)
3421 {
3422 #ifdef HAVE_ELEMENTARY_ECORE_CON
3423    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
3424    Widget_Data *wd = elm_widget_data_get(obj);
3425
3426    if (!wd) return EINA_FALSE;
3427    return wd->paused_markers;
3428 #else
3429    (void) obj;
3430    return EINA_FALSE;
3431 #endif
3432 }
3433
3434 EAPI void
3435 elm_map_utils_downloading_status_get(const Evas_Object *obj, int *try_num, int *finish_num)
3436 {
3437 #ifdef HAVE_ELEMENTARY_ECORE_CON
3438    ELM_CHECK_WIDTYPE(obj, widtype);
3439    Widget_Data *wd = elm_widget_data_get(obj);
3440
3441    if (!wd) return;
3442    if (try_num)
3443      {
3444         *try_num = wd->try_num;
3445      }
3446
3447    if (finish_num)
3448      {
3449         *finish_num = wd->finish_num;
3450      }
3451 #else
3452    (void) obj;
3453    (void) try_num;
3454    (void) finish_num;
3455 #endif
3456 }
3457
3458 EAPI void
3459 elm_map_utils_convert_coord_into_geo(const Evas_Object *obj, int x, int y, int size, double *lon, double *lat)
3460 {
3461 #ifdef HAVE_ELEMENTARY_ECORE_CON
3462    ELM_CHECK_WIDTYPE(obj, widtype);
3463    Widget_Data *wd = elm_widget_data_get(obj);
3464
3465    if (!wd) return;
3466    int zoom = floor(log(size / 256) / log(2));
3467    if ((wd->src) && (wd->src->coord_into_geo))
3468      {
3469         if (wd->src->coord_into_geo(obj, zoom, x, y, size, lon, lat)) return;
3470      }
3471
3472    if (lon)
3473      {
3474         *lon = x / (double)size * 360.0 - 180;
3475      }
3476    if (lat)
3477      {
3478         double n = ELM_PI - 2.0 * ELM_PI * y / size;
3479         *lat = 180.0 / ELM_PI * atan(0.5 * (exp(n) - exp(-n)));
3480      }
3481 #else
3482    (void) obj;
3483    (void) x;
3484    (void) y;
3485    (void) size;
3486    (void) lon;
3487    (void) lat;
3488 #endif
3489 }
3490
3491 EAPI void
3492 elm_map_utils_convert_geo_into_coord(const Evas_Object *obj, double lon, double lat, int size, int *x, int *y)
3493 {
3494 #ifdef HAVE_ELEMENTARY_ECORE_CON
3495    ELM_CHECK_WIDTYPE(obj, widtype);
3496    Widget_Data *wd = elm_widget_data_get(obj);
3497
3498    if (!wd) return;
3499    int zoom = floor(log(size / 256) / log(2));
3500    if ((wd->src) && (wd->src->geo_into_coord))
3501      {
3502         if (wd->src->geo_into_coord(obj, zoom, lon, lat, size, x, y)) return;
3503      }
3504
3505    if (x)
3506      *x = floor((lon + 180.0) / 360.0 * size);
3507    if (y)
3508      *y = floor((1.0 - log( tan(lat * ELM_PI / 180.0) + 1.0 / cos(lat * ELM_PI / 180.0)) / ELM_PI) / 2.0 * size);
3509 #else
3510    (void) obj;
3511    (void) lon;
3512    (void) lat;
3513    (void) size;
3514    (void) x;
3515    (void) y;
3516 #endif
3517 }
3518
3519 EAPI Elm_Map_Name *
3520 elm_map_utils_convert_coord_into_name(const Evas_Object *obj, double lon, double lat)
3521 {
3522 #ifdef HAVE_ELEMENTARY_ECORE_CON
3523    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3524    return _utils_convert_name(obj, ELM_MAP_NAME_METHOD_REVERSE, NULL, lon, lat);
3525 #else
3526    (void) obj;
3527    (void) lon;
3528    (void) lat;
3529    return NULL;
3530 #endif
3531 }
3532
3533 EAPI Elm_Map_Name *
3534 elm_map_utils_convert_name_into_coord(const Evas_Object *obj, char *address)
3535 {
3536 #ifdef HAVE_ELEMENTARY_ECORE_CON
3537    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3538    if (!address) return NULL;
3539    return _utils_convert_name(obj, ELM_MAP_NAME_METHOD_SEARCH, address, 0.0, 0.0);
3540 #else
3541    (void) obj;
3542    (void) address;
3543    return NULL;
3544 #endif
3545 }
3546
3547 EAPI void
3548 elm_map_utils_rotate_coord(const Evas_Object *obj __UNUSED__, const Evas_Coord x, const Evas_Coord y, const Evas_Coord cx, const Evas_Coord cy, const double degree, Evas_Coord *xx, Evas_Coord *yy)
3549 {
3550 #ifdef HAVE_ELEMENTARY_ECORE_CON
3551    if ((!xx) || (!yy)) return;
3552
3553    double r = (degree * M_PI) / 180.0;
3554    double tx, ty, ttx, tty;
3555
3556    tx = x - cx;
3557    ty = y - cy;
3558
3559    ttx = tx * cos(r);
3560    tty = tx * sin(r);
3561    tx = ttx + (ty * cos(r + M_PI_2));
3562    ty = tty + (ty * sin(r + M_PI_2));
3563
3564    *xx = tx + cx;
3565    *yy = ty + cy;
3566 #else
3567    (void) x;
3568    (void) y;
3569    (void) cx;
3570    (void) cy;
3571    (void) degree;
3572    (void) xx;
3573    (void) yy;
3574 #endif
3575 }
3576
3577 EAPI Elm_Map_Marker *
3578 elm_map_marker_add(Evas_Object *obj, double lon, double lat, Elm_Map_Marker_Class *clas, Elm_Map_Group_Class *clas_group, void *data)
3579 {
3580 #ifdef HAVE_ELEMENTARY_ECORE_CON
3581    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3582    Widget_Data *wd = elm_widget_data_get(obj);
3583    int i, j;
3584    Eina_List *l;
3585    Marker_Group *group;
3586    int mpi, mpj;
3587    int tabi[9];
3588    int tabj[9];
3589    const char *s;
3590    const char *style;
3591    Evas_Object *o;
3592
3593    if (!wd) return NULL;
3594    EINA_SAFETY_ON_NULL_RETURN_VAL(clas_group, NULL);
3595    EINA_SAFETY_ON_NULL_RETURN_VAL(clas, NULL);
3596
3597    Elm_Map_Marker *marker = ELM_NEW(Elm_Map_Marker);
3598
3599    marker->wd = wd;
3600    marker->clas = clas;
3601    marker->clas_group = clas_group;
3602    marker->longitude = lon;
3603    marker->latitude = lat;
3604    marker->data = data;
3605    marker->x = calloc(wd->zoom_max + 1, sizeof(Evas_Coord));
3606    marker->y = calloc(wd->zoom_max + 1, sizeof(Evas_Coord));
3607    marker->groups = calloc(wd->zoom_max + 1, sizeof(Marker_Group*));
3608
3609    tabi[1] = tabi[4] = tabi[6] = -1;
3610    tabi[2] = tabi[0] = tabi[7] = 0;
3611    tabi[3] = tabi[5] = tabi[8] = 1;
3612
3613    tabj[1] = tabj[2] = tabj[3] = -1;
3614    tabj[4] = tabj[0] = tabj[5] = 0;
3615    tabj[6] = tabj[7] = tabj[8] = 1;
3616
3617    if (!clas_group->priv.set)
3618      {
3619         style = "radio";
3620         if (marker->clas_group && marker->clas_group->style)
3621           style = marker->clas_group->style;
3622
3623         o = edje_object_add(evas_object_evas_get(obj));
3624         _elm_theme_object_set(obj, o, "map/marker", style, elm_widget_style_get(obj));
3625         s = edje_object_data_get(o, "size_w");
3626         if (s) clas_group->priv.edje_w = atoi(s);
3627         else clas_group->priv.edje_w = 0;
3628         s = edje_object_data_get(o, "size_h");
3629         if (s) clas_group->priv.edje_h = atoi(s);
3630         else clas_group->priv.edje_h = 0;
3631         s = edje_object_data_get(o, "size_max_w");
3632         if (s) clas_group->priv.edje_max_w = atoi(s);
3633         else clas_group->priv.edje_max_w = 0;
3634         s = edje_object_data_get(o, "size_max_h");
3635         if (s) clas_group->priv.edje_max_h = atoi(s);
3636         else clas_group->priv.edje_max_h = 0;
3637         evas_object_del(o);
3638
3639         clas_group->priv.set = EINA_TRUE;
3640      }
3641
3642    if (!clas->priv.set)
3643      {
3644         style = "radio";
3645         if (marker->clas && marker->clas->style)
3646           style = marker->clas->style;
3647
3648         o = edje_object_add(evas_object_evas_get(obj));
3649         _elm_theme_object_set(obj, o, "map/marker", style, elm_widget_style_get(obj));
3650         s = edje_object_data_get(o, "size_w");
3651         if (s) clas->priv.edje_w = atoi(s);
3652         else clas->priv.edje_w = 0;
3653         s = edje_object_data_get(o, "size_h");
3654         if (s) clas->priv.edje_h = atoi(s);
3655         else clas->priv.edje_h = 0;
3656         evas_object_del(o);
3657
3658         clas->priv.set = EINA_TRUE;
3659      }
3660
3661    for (i = clas_group->zoom_displayed; i <= wd->zoom_max; i++)
3662      {
3663         elm_map_utils_convert_geo_into_coord(obj, lon, lat, pow(2.0, i)*wd->tsize,
3664                                              &(marker->x[i]), &(marker->y[i]));
3665
3666         //search in the matrixsparse the region where the marker will be
3667         mpi = marker->x[i] / wd->tsize;
3668         mpj = marker->y[i] / wd->tsize;
3669
3670         if (!wd->markers[i])
3671           {
3672              int size =  pow(2.0, i);
3673              wd->markers[i] = eina_matrixsparse_new(size, size, NULL, NULL);
3674           }
3675
3676         group = NULL;
3677         if (i <= clas_group->zoom_grouped)
3678           {
3679              for (j = 0, group = NULL; j < 9 && !group; j++)
3680                {
3681                   EINA_LIST_FOREACH(eina_matrixsparse_data_idx_get(wd->markers[i], mpj + tabj[j], mpi + tabi[j]),
3682                                     l, group)
3683                     {
3684                        if (group->clas == marker->clas_group
3685                            && ELM_RECTS_INTERSECT(marker->x[i]-clas->priv.edje_w/4,
3686                                                   marker->y[i]-clas->priv.edje_h/4, clas->priv.edje_w, clas->priv.edje_h,
3687                                                   group->x-group->w/4, group->y-group->h/4, group->w, group->h))
3688                          {
3689                             group->markers = eina_list_append(group->markers, marker);
3690                             group->update_nbelems = EINA_TRUE;
3691                             group->update_resize = EINA_TRUE;
3692
3693                             group->sum_x += marker->x[i];
3694                             group->sum_y += marker->y[i];
3695                             group->x = group->sum_x / eina_list_count(group->markers);
3696                             group->y = group->sum_y / eina_list_count(group->markers);
3697
3698                             group->w = group->clas->priv.edje_w + group->clas->priv.edje_w/8.
3699                                * eina_list_count(group->markers);
3700                             group->h = group->clas->priv.edje_h + group->clas->priv.edje_h/8.
3701                                * eina_list_count(group->markers);
3702                             if (group->w > group->clas->priv.edje_max_w) group->w = group->clas->priv.edje_max_w;
3703                             if (group->h > group->clas->priv.edje_max_h) group->h = group->clas->priv.edje_max_h;
3704
3705                             if (group->obj && eina_list_count(group->markers) == 2)
3706                               {
3707                                  _group_object_free(group);
3708                                  _group_object_create(group);
3709                               }
3710                             if (group->bubble)
3711                               _group_bubble_content_update(group);
3712
3713                             break;
3714                          }
3715                     }
3716                }
3717           }
3718         if (!group)
3719           {
3720              group = calloc(1, sizeof(Marker_Group));
3721              group->wd = wd;
3722              group->sum_x = marker->x[i];
3723              group->sum_y = marker->y[i];
3724              group->x = marker->x[i];
3725              group->y = marker->y[i];
3726              group->w = clas_group->priv.edje_w;
3727              group->h = clas_group->priv.edje_h;
3728              group->clas = clas_group;
3729
3730              group->markers = eina_list_append(group->markers, marker);
3731              group->update_nbelems = EINA_TRUE;
3732              group->update_resize = EINA_TRUE;
3733
3734              eina_matrixsparse_cell_idx_get(wd->markers[i], mpj, mpi, &(group->cell));
3735
3736              if (!group->cell)
3737                {
3738                   l = eina_list_append(NULL, group);
3739                   eina_matrixsparse_data_idx_set(wd->markers[i], mpj, mpi, l);
3740                   eina_matrixsparse_cell_idx_get(wd->markers[i], mpj, mpi, &(group->cell));
3741                }
3742              else
3743                {
3744                   l = eina_matrixsparse_cell_data_get(group->cell);
3745                   l = eina_list_append(l, group);
3746                   eina_matrixsparse_cell_data_set(group->cell, l);
3747                }
3748           }
3749         marker->groups[i] = group;
3750      }
3751
3752    if (wd->grids)
3753      {
3754         Grid *g;
3755         Evas_Coord ox, oy, ow, oh;
3756         evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
3757         g = _get_current_grid(wd);
3758         marker_place(obj, g, wd->pan_x, wd->pan_y, ox, oy, ow, oh);
3759      }
3760
3761    return marker;
3762 #else
3763    (void) obj;
3764    (void) lon;
3765    (void) lat;
3766    (void) clas;
3767    (void) clas_group;
3768    (void) data;
3769    return NULL;
3770 #endif
3771 }
3772
3773 EAPI void
3774 elm_map_marker_remove(Elm_Map_Marker *marker)
3775 {
3776 #ifdef HAVE_ELEMENTARY_ECORE_CON
3777    int i;
3778    Eina_List *groups;
3779    Widget_Data *wd;
3780
3781    EINA_SAFETY_ON_NULL_RETURN(marker);
3782    wd = marker->wd;
3783    if (!wd) return;
3784    for (i = marker->clas_group->zoom_displayed; i <= wd->zoom_max; i++)
3785      {
3786         marker->groups[i]->markers = eina_list_remove(marker->groups[i]->markers, marker);
3787         if (!eina_list_count(marker->groups[i]->markers))
3788           {
3789              groups = eina_matrixsparse_cell_data_get(marker->groups[i]->cell);
3790              groups = eina_list_remove(groups, marker->groups[i]);
3791              eina_matrixsparse_cell_data_set(marker->groups[i]->cell, groups);
3792
3793              _group_object_free(marker->groups[i]);
3794              _group_bubble_free(marker->groups[i]);
3795              free(marker->groups[i]);
3796           }
3797         else
3798           {
3799              marker->groups[i]->sum_x -= marker->x[i];
3800              marker->groups[i]->sum_y -= marker->y[i];
3801
3802              marker->groups[i]->x = marker->groups[i]->sum_x / eina_list_count(marker->groups[i]->markers);
3803              marker->groups[i]->y = marker->groups[i]->sum_y / eina_list_count(marker->groups[i]->markers);
3804
3805              marker->groups[i]->w = marker->groups[i]->clas->priv.edje_w
3806                 + marker->groups[i]->clas->priv.edje_w/8. * eina_list_count(marker->groups[i]->markers);
3807              marker->groups[i]->h = marker->groups[i]->clas->priv.edje_h
3808                 + marker->groups[i]->clas->priv.edje_h/8. * eina_list_count(marker->groups[i]->markers);
3809              if (marker->groups[i]->w > marker->groups[i]->clas->priv.edje_max_w)
3810                marker->groups[i]->w = marker->groups[i]->clas->priv.edje_max_w;
3811              if (marker->groups[i]->h > marker->groups[i]->clas->priv.edje_max_h)
3812                marker->groups[i]->h = marker->groups[i]->clas->priv.edje_max_h;
3813
3814              if ((marker->groups[i]->obj) && (eina_list_count(marker->groups[i]->markers) == 1))
3815                {
3816                   _group_object_free(marker->groups[i]);
3817                   _group_object_create(marker->groups[i]);
3818                }
3819           }
3820      }
3821
3822    if ((marker->content) && (marker->clas->func.del))
3823      marker->clas->func.del(marker->wd->obj, marker, marker->data, marker->content);
3824    else if (marker->content)
3825      evas_object_del(marker->content);
3826
3827    if (marker->x) free(marker->x);
3828    if (marker->y) free(marker->y);
3829    if (marker->groups) free(marker->groups);
3830
3831    free(marker);
3832
3833    if (wd->grids)
3834      {
3835         Grid *g;
3836         Evas_Coord ox, oy, ow, oh;
3837         evas_object_geometry_get(wd->obj, &ox, &oy, &ow, &oh);
3838         g = _get_current_grid(wd);
3839         marker_place(wd->obj, g, wd->pan_x, wd->pan_y, ox, oy, ow, oh);
3840      }
3841 #else
3842    (void) marker;
3843 #endif
3844 }
3845
3846 EAPI void
3847 elm_map_marker_region_get(const Elm_Map_Marker *marker, double *lon, double *lat)
3848 {
3849 #ifdef HAVE_ELEMENTARY_ECORE_CON
3850    EINA_SAFETY_ON_NULL_RETURN(marker);
3851    if (lon) *lon = marker->longitude;
3852    if (lat) *lat = marker->latitude;
3853 #else
3854    (void) marker;
3855    (void) lon;
3856    (void) lat;
3857 #endif
3858 }
3859
3860 EAPI void
3861 elm_map_marker_bring_in(Elm_Map_Marker *marker)
3862 {
3863 #ifdef HAVE_ELEMENTARY_ECORE_CON
3864    EINA_SAFETY_ON_NULL_RETURN(marker);
3865    elm_map_geo_region_bring_in(marker->wd->obj, marker->longitude, marker->latitude);
3866 #else
3867    (void) marker;
3868 #endif
3869 }
3870
3871 EAPI void
3872 elm_map_marker_show(Elm_Map_Marker *marker)
3873 {
3874 #ifdef HAVE_ELEMENTARY_ECORE_CON
3875    EINA_SAFETY_ON_NULL_RETURN(marker);
3876    elm_map_geo_region_show(marker->wd->obj, marker->longitude, marker->latitude);
3877 #else
3878    (void) marker;
3879 #endif
3880 }
3881
3882 EAPI void
3883 elm_map_markers_list_show(Eina_List *markers)
3884 {
3885 #ifdef HAVE_ELEMENTARY_ECORE_CON
3886    int zoom;
3887    double lon, lat;
3888    Eina_List *l;
3889    Elm_Map_Marker *marker, *m_max_lon = NULL, *m_max_lat = NULL, *m_min_lon = NULL, *m_min_lat = NULL;
3890    Evas_Coord rw, rh, xc, yc;
3891    Widget_Data *wd;
3892
3893    EINA_SAFETY_ON_NULL_RETURN(markers);
3894    EINA_LIST_FOREACH(markers, l, marker)
3895      {
3896         wd = marker->wd;
3897
3898         if ((!m_min_lon) || (marker->longitude < m_min_lon->longitude))
3899           m_min_lon = marker;
3900
3901         if ((!m_max_lon) || (marker->longitude > m_max_lon->longitude))
3902           m_max_lon = marker;
3903
3904         if ((!m_min_lat) || (marker->latitude > m_min_lat->latitude))
3905           m_min_lat = marker;
3906
3907         if ((!m_max_lat) || (marker->latitude < m_max_lat->latitude))
3908           m_max_lat = marker;
3909      }
3910
3911    lon = (m_max_lon->longitude - m_min_lon->longitude) / 2. + m_min_lon->longitude;
3912    lat = (m_max_lat->latitude - m_min_lat->latitude) / 2. + m_min_lat->latitude;
3913
3914    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3915    for (zoom = wd->src->zoom_max; zoom > wd->src->zoom_min; zoom--)
3916      {
3917         Evas_Coord size = pow(2.0, zoom)*wd->tsize;
3918         elm_map_utils_convert_geo_into_coord(wd->obj, lon, lat, size, &xc, &yc);
3919
3920         if ((m_min_lon->x[zoom] - wd->marker_max_w >= xc-rw/2)
3921             && (m_min_lat->y[zoom] - wd->marker_max_h >= yc-rh/2)
3922             && (m_max_lon->x[zoom] + wd->marker_max_w <= xc+rw/2)
3923             && (m_max_lat->y[zoom] + wd->marker_max_h <= yc+rh/2))
3924           break;
3925      }
3926
3927    elm_map_geo_region_show(wd->obj, lon, lat);
3928    elm_map_zoom_set(wd->obj, zoom);
3929 #else
3930    (void) markers;
3931 #endif
3932 }
3933
3934 EAPI void
3935 elm_map_max_marker_per_group_set(Evas_Object *obj, int max)
3936 {
3937 #ifdef HAVE_ELEMENTARY_ECORE_CON
3938    ELM_CHECK_WIDTYPE(obj, widtype);
3939    Widget_Data *wd = elm_widget_data_get(obj);
3940
3941    if (!wd) return;
3942    wd->markers_max_num = max;
3943 #else
3944    (void) obj;
3945    (void) max;
3946 #endif
3947 }
3948
3949 EAPI Evas_Object *
3950 elm_map_marker_object_get(const Elm_Map_Marker *marker)
3951 {
3952 #ifdef HAVE_ELEMENTARY_ECORE_CON
3953    EINA_SAFETY_ON_NULL_RETURN_VAL(marker, NULL);
3954    return marker->content;
3955 #else
3956    (void) marker;
3957    return NULL;
3958 #endif
3959 }
3960
3961 EAPI void
3962 elm_map_marker_update(Elm_Map_Marker *marker)
3963 {
3964 #ifdef HAVE_ELEMENTARY_ECORE_CON
3965    EINA_SAFETY_ON_NULL_RETURN(marker);
3966    if (marker->content)
3967      {
3968         if (marker->clas->func.del)
3969           marker->clas->func.del(marker->wd->obj, marker, marker->data, marker->content);
3970         else
3971           evas_object_del(marker->content);
3972         marker->content = NULL;
3973         _group_bubble_content_update(marker->groups[marker->wd->zoom]);
3974      }
3975 #else
3976    (void) marker;
3977 #endif
3978 }
3979
3980 EAPI void
3981 elm_map_bubbles_close(Evas_Object *obj)
3982 {
3983 #ifdef HAVE_ELEMENTARY_ECORE_CON
3984    ELM_CHECK_WIDTYPE(obj, widtype);
3985    Widget_Data *wd = elm_widget_data_get(obj);
3986    Marker_Group *group;
3987    Eina_List *l, *l_next;
3988
3989    if (!wd) return;
3990    EINA_LIST_FOREACH_SAFE(wd->opened_bubbles, l, l_next, group)
3991       _group_bubble_free(group);
3992 #else
3993    (void) obj;
3994 #endif
3995 }
3996
3997 EAPI Elm_Map_Group_Class *
3998 elm_map_group_class_new(Evas_Object *obj)
3999 {
4000 #ifdef HAVE_ELEMENTARY_ECORE_CON
4001    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4002    Widget_Data *wd = elm_widget_data_get(obj);
4003
4004    if (!wd) return NULL;
4005    Elm_Map_Group_Class *clas = calloc(1, sizeof(Elm_Map_Group_Class));
4006    clas->zoom_grouped = wd->zoom_max;
4007    wd->groups_clas = eina_list_append(wd->groups_clas, clas);
4008    return clas;
4009 #else
4010    (void) obj;
4011    return NULL;
4012 #endif
4013 }
4014
4015 EAPI void
4016 elm_map_group_class_style_set(Elm_Map_Group_Class *clas, const char *style)
4017 {
4018 #ifdef HAVE_ELEMENTARY_ECORE_CON
4019    EINA_SAFETY_ON_NULL_RETURN(clas);
4020    eina_stringshare_replace(&clas->style, style);
4021 #else
4022    (void) clas;
4023    (void) style;
4024 #endif
4025 }
4026
4027 EAPI void
4028 elm_map_group_class_icon_cb_set(Elm_Map_Group_Class *clas, ElmMapGroupIconGetFunc icon_get)
4029 {
4030 #ifdef HAVE_ELEMENTARY_ECORE_CON
4031    EINA_SAFETY_ON_NULL_RETURN(clas);
4032    clas->func.icon_get = icon_get;
4033 #else
4034    (void) clas;
4035    (void) icon_get;
4036 #endif
4037 }
4038
4039 EAPI void
4040 elm_map_group_class_data_set(Elm_Map_Group_Class *clas, void *data)
4041 {
4042 #ifdef HAVE_ELEMENTARY_ECORE_CON
4043    EINA_SAFETY_ON_NULL_RETURN(clas);
4044    clas->data = data;
4045 #else
4046    (void) clas;
4047    (void) data;
4048 #endif
4049 }
4050
4051 EAPI void
4052 elm_map_group_class_zoom_displayed_set(Elm_Map_Group_Class *clas, int zoom)
4053 {
4054 #ifdef HAVE_ELEMENTARY_ECORE_CON
4055    EINA_SAFETY_ON_NULL_RETURN(clas);
4056    clas->zoom_displayed = zoom;
4057 #else
4058    (void) clas;
4059    (void) zoom;
4060 #endif
4061 }
4062
4063 EAPI void
4064 elm_map_group_class_zoom_grouped_set(Elm_Map_Group_Class *clas, int zoom)
4065 {
4066 #ifdef HAVE_ELEMENTARY_ECORE_CON
4067    EINA_SAFETY_ON_NULL_RETURN(clas);
4068    clas->zoom_grouped = zoom;
4069 #else
4070    (void) clas;
4071    (void) zoom;
4072 #endif
4073 }
4074
4075 EAPI void
4076 elm_map_group_class_hide_set(Evas_Object *obj, Elm_Map_Group_Class *clas, Eina_Bool hide)
4077 {
4078 #ifdef HAVE_ELEMENTARY_ECORE_CON
4079    ELM_CHECK_WIDTYPE(obj, widtype);
4080    Widget_Data *wd = elm_widget_data_get(obj);
4081
4082    if (!wd) return;
4083    EINA_SAFETY_ON_NULL_RETURN(clas);
4084    if (clas->hide == hide) return;
4085    clas->hide = hide;
4086    if (wd->grids)
4087      {
4088         Grid *g;
4089         Evas_Coord ox, oy, ow, oh;
4090         evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
4091         g = _get_current_grid(wd);
4092         marker_place(obj, g, wd->pan_x, wd->pan_y, ox, oy, ow, oh);
4093      }
4094 #else
4095    (void) obj;
4096    (void) clas;
4097    (void) hide;
4098 #endif
4099 }
4100
4101 EAPI Elm_Map_Marker_Class *
4102 elm_map_marker_class_new(Evas_Object *obj)
4103 {
4104 #ifdef HAVE_ELEMENTARY_ECORE_CON
4105    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4106    Widget_Data *wd = elm_widget_data_get(obj);
4107
4108    if (!wd) return NULL;
4109    Elm_Map_Marker_Class *clas = calloc(1, sizeof(Elm_Map_Marker_Class));
4110    wd->markers_clas = eina_list_append(wd->markers_clas, clas);
4111    return clas;
4112 #else
4113    (void) obj;
4114    return NULL;
4115 #endif
4116 }
4117
4118 EAPI void
4119 elm_map_marker_class_style_set(Elm_Map_Marker_Class *clas, const char *style)
4120 {
4121 #ifdef HAVE_ELEMENTARY_ECORE_CON
4122    EINA_SAFETY_ON_NULL_RETURN(clas);
4123    eina_stringshare_replace(&clas->style, style);
4124 #else
4125    (void) clas;
4126    (void) style;
4127 #endif
4128 }
4129
4130 EAPI void
4131 elm_map_marker_class_icon_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerIconGetFunc icon_get)
4132 {
4133 #ifdef HAVE_ELEMENTARY_ECORE_CON
4134    EINA_SAFETY_ON_NULL_RETURN(clas);
4135    clas->func.icon_get = icon_get;
4136 #else
4137    (void) clas;
4138    (void) icon_get;
4139 #endif
4140 }
4141
4142 EAPI void
4143 elm_map_marker_class_get_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerGetFunc get)
4144 {
4145 #ifdef HAVE_ELEMENTARY_ECORE_CON
4146    EINA_SAFETY_ON_NULL_RETURN(clas);
4147    clas->func.get = get;
4148 #else
4149    (void) clas;
4150    (void) get;
4151 #endif
4152 }
4153
4154 EAPI void
4155 elm_map_marker_class_del_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerDelFunc del)
4156 {
4157 #ifdef HAVE_ELEMENTARY_ECORE_CON
4158    EINA_SAFETY_ON_NULL_RETURN(clas);
4159    clas->func.del = del;
4160 #else
4161    (void) clas;
4162    (void) del;
4163 #endif
4164 }
4165
4166 EAPI const char **
4167 elm_map_source_names_get(const Evas_Object *obj)
4168 {
4169 #ifdef HAVE_ELEMENTARY_ECORE_CON
4170    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4171    Widget_Data *wd = elm_widget_data_get(obj);
4172
4173    if (!wd) return NULL;
4174    return wd->source_names;
4175 #else
4176    (void) obj;
4177    return NULL;
4178 #endif
4179 }
4180
4181 EAPI void
4182 elm_map_source_name_set(Evas_Object *obj, const char *source_name)
4183 {
4184 #ifdef HAVE_ELEMENTARY_ECORE_CON
4185    ELM_CHECK_WIDTYPE(obj, widtype);
4186    Widget_Data *wd = elm_widget_data_get(obj);
4187    Map_Sources_Tab *s;
4188    Eina_List *l;
4189    int zoom;
4190
4191    if (!wd) return;
4192    if (wd->src)
4193      {
4194         if (!strcmp(wd->src->name, source_name)) return;
4195         if (!wd->src->url_cb) return;
4196      }
4197
4198    grid_clear_all(obj);
4199    EINA_LIST_FOREACH(wd->map_sources_tab, l, s)
4200      {
4201         if (!strcmp(s->name, source_name))
4202           {
4203              wd->src = s;
4204              break;
4205           }
4206      }
4207    zoom = wd->zoom;
4208    wd->zoom = -1;
4209
4210    if (wd->src)
4211      {
4212         if (wd->src->zoom_max < zoom)
4213           zoom = wd->src->zoom_max;
4214         if (wd->src->zoom_min > zoom)
4215           zoom = wd->src->zoom_min;
4216      }
4217    grid_create_all(obj);
4218    elm_map_zoom_set(obj, zoom);
4219 #else
4220    (void) obj;
4221    (void) source_name;
4222 #endif
4223 }
4224
4225 EAPI const char *
4226 elm_map_source_name_get(const Evas_Object *obj)
4227 {
4228 #ifdef HAVE_ELEMENTARY_ECORE_CON
4229    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4230    Widget_Data *wd = elm_widget_data_get(obj);
4231
4232    if ((!wd) || (!wd->src)) return NULL;
4233    return wd->src->name;
4234 #else
4235    (void) obj;
4236    return NULL;
4237 #endif
4238 }
4239
4240 EAPI void
4241 elm_map_route_source_set(Evas_Object *obj, Elm_Map_Route_Sources source)
4242 {
4243 #ifdef HAVE_ELEMENTARY_ECORE_CON
4244    ELM_CHECK_WIDTYPE(obj, widtype);
4245    Widget_Data *wd = elm_widget_data_get(obj);
4246
4247    if (!wd) return;
4248    wd->route_source = source;
4249 #else
4250    (void) obj;
4251    (void) source;
4252 #endif
4253 }
4254
4255 EAPI Elm_Map_Route_Sources
4256 elm_map_route_source_get(const Evas_Object *obj)
4257 {
4258 #ifdef HAVE_ELEMENTARY_ECORE_CON
4259    ELM_CHECK_WIDTYPE(obj, widtype) ELM_MAP_ROUTE_SOURCE_YOURS;
4260    Widget_Data *wd = elm_widget_data_get(obj);
4261
4262    if (!wd) return ELM_MAP_ROUTE_SOURCE_YOURS;
4263    return wd->route_source;
4264 #else
4265    (void) obj;
4266    return ELM_MAP_ROUTE_SOURCE_YOURS;
4267 #endif
4268 }
4269
4270 EAPI void
4271 elm_map_source_zoom_max_set(Evas_Object *obj, int zoom)
4272 {
4273 #ifdef HAVE_ELEMENTARY_ECORE_CON
4274    ELM_CHECK_WIDTYPE(obj, widtype);
4275    Widget_Data *wd = elm_widget_data_get(obj);
4276
4277    if ((!wd) || (!wd->src)) return;
4278    if ((zoom > wd->zoom_max) || (zoom < wd->zoom_min)) return;
4279    wd->src->zoom_max = zoom;
4280 #else
4281    (void) obj;
4282    (void) zoom;
4283 #endif
4284 }
4285
4286 EAPI int
4287 elm_map_source_zoom_max_get(const Evas_Object *obj)
4288 {
4289 #ifdef HAVE_ELEMENTARY_ECORE_CON
4290    ELM_CHECK_WIDTYPE(obj, widtype) 18;
4291    Widget_Data *wd = elm_widget_data_get(obj);
4292
4293    if ((!wd) || (!wd->src)) return 18;
4294    return wd->src->zoom_max;
4295 #else
4296    (void) obj;
4297    return 18;
4298 #endif
4299 }
4300
4301 EAPI void
4302 elm_map_source_zoom_min_set(Evas_Object *obj, int zoom)
4303 {
4304 #ifdef HAVE_ELEMENTARY_ECORE_CON
4305    ELM_CHECK_WIDTYPE(obj, widtype);
4306    Widget_Data *wd = elm_widget_data_get(obj);
4307
4308    if ((!wd) || (!wd->src)) return;
4309    if ((zoom > wd->zoom_max) || (zoom < wd->zoom_min)) return;
4310    wd->src->zoom_min = zoom;
4311 #else
4312    (void) obj;
4313    (void) zoom;
4314 #endif
4315 }
4316
4317 EAPI int
4318 elm_map_source_zoom_min_get(const Evas_Object *obj)
4319 {
4320 #ifdef HAVE_ELEMENTARY_ECORE_CON
4321    ELM_CHECK_WIDTYPE(obj, widtype) 0;
4322    Widget_Data *wd = elm_widget_data_get(obj);
4323
4324    if ((!wd) || (!wd->src)) return 0;
4325    return wd->src->zoom_min;
4326 #else
4327    (void) obj;
4328    return 0;
4329 #endif
4330 }
4331
4332 EAPI void
4333 elm_map_user_agent_set(Evas_Object *obj, const char *user_agent)
4334 {
4335 #ifdef HAVE_ELEMENTARY_ECORE_CON
4336    ELM_CHECK_WIDTYPE(obj, widtype);
4337    Widget_Data *wd = elm_widget_data_get(obj);
4338
4339    if (!wd) return;
4340    if (!wd->user_agent) wd->user_agent = eina_stringshare_add(user_agent);
4341    else eina_stringshare_replace(&wd->user_agent, user_agent);
4342
4343    if (!wd->ua) wd->ua = eina_hash_string_small_new(NULL);
4344    eina_hash_set(wd->ua, "User-Agent", wd->user_agent);
4345 #else
4346    (void) obj;
4347    (void) user_agent;
4348 #endif
4349 }
4350
4351 EAPI const char *
4352 elm_map_user_agent_get(const Evas_Object *obj)
4353 {
4354 #ifdef HAVE_ELEMENTARY_ECORE_CON
4355    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4356    Widget_Data *wd = elm_widget_data_get(obj);
4357
4358    if (!wd) return NULL;
4359    return wd->user_agent;
4360 #else
4361    (void) obj;
4362    return NULL;
4363 #endif
4364 }
4365
4366 EAPI Elm_Map_Route *
4367 elm_map_route_add(Evas_Object *obj,
4368                   Elm_Map_Route_Type type,
4369                   Elm_Map_Route_Method method,
4370                   double flon,
4371                   double flat,
4372                   double tlon,
4373                   double tlat)
4374 {
4375 #ifdef HAVE_ELEMENTARY_ECORE_CON
4376    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4377    Widget_Data *wd = elm_widget_data_get(obj);
4378    char buf[PATH_MAX];
4379    char *source;
4380    char *type_name = NULL;
4381    int fd;
4382
4383    if ((!wd) || (!wd->src)) return NULL;
4384
4385    Elm_Map_Route *route = ELM_NEW(Elm_Map_Route);
4386    if (!route) return NULL;
4387
4388    snprintf(buf, sizeof(buf), DEST_ROUTE_XML_FILE);
4389    fd = mkstemp(buf);
4390    if (fd < 0)
4391      {
4392         free(route);
4393         return NULL;
4394      }
4395
4396    route->con_url = ecore_con_url_new(NULL);
4397    route->ud.fname = strdup(buf);
4398    INF("xml file : %s", route->ud.fname);
4399
4400    route->ud.fd = fdopen(fd, "w+");
4401    if ((!route->con_url) || (!route->ud.fd))
4402      {
4403         ecore_con_url_free(route->con_url);
4404         free(route);
4405         return NULL;
4406      }
4407
4408    route->wd = wd;
4409    route->color.r = 255;
4410    route->color.g = 0;
4411    route->color.b = 0;
4412    route->color.a = 255;
4413    route->handlers = eina_list_append
4414      (route->handlers, (void *)ecore_event_handler_add
4415          (ECORE_CON_EVENT_URL_COMPLETE, _route_complete_cb, route));
4416
4417    route->inbound = EINA_FALSE;
4418    route->type = type;
4419    route->method = method;
4420    route->flon = flon;
4421    route->flat = flat;
4422    route->tlon = tlon;
4423    route->tlat = tlat;
4424
4425    switch (type)
4426      {
4427       case ELM_MAP_ROUTE_TYPE_MOTOCAR:
4428         type_name = strdup(ROUTE_TYPE_MOTORCAR);
4429         break;
4430       case ELM_MAP_ROUTE_TYPE_BICYCLE:
4431         type_name = strdup(ROUTE_TYPE_BICYCLE);
4432         break;
4433       case ELM_MAP_ROUTE_TYPE_FOOT:
4434         type_name = strdup(ROUTE_TYPE_FOOT);
4435         break;
4436       default:
4437         break;
4438      }
4439
4440    source = wd->src->route_url_cb(obj, type_name, method, flon, flat, tlon, tlat);
4441    INF("route url = %s", source);
4442
4443    wd->route = eina_list_append(wd->route, route);
4444
4445    ecore_con_url_url_set(route->con_url, source);
4446    ecore_con_url_fd_set(route->con_url, fileno(route->ud.fd));
4447    ecore_con_url_data_set(route->con_url, route);
4448    ecore_con_url_get(route->con_url);
4449    if (type_name) free(type_name);
4450    if (source) free(source);
4451
4452    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
4453                            "elm,state,busy,start", "elm");
4454    evas_object_smart_callback_call(wd->obj, SIG_ROUTE_LOAD, NULL);
4455    return route;
4456 #else
4457    (void) obj;
4458    (void) type;
4459    (void) method;
4460    (void) flon;
4461    (void) flat;
4462    (void) tlon;
4463    (void) tlat;
4464    return NULL;
4465 #endif
4466 }
4467
4468 EAPI void
4469 elm_map_route_remove(Elm_Map_Route *route)
4470 {
4471 #ifdef HAVE_ELEMENTARY_ECORE_CON
4472    EINA_SAFETY_ON_NULL_RETURN(route);
4473
4474    Path_Waypoint *w;
4475    Path_Node *n;
4476    Evas_Object *p;
4477    Ecore_Event_Handler *h;
4478
4479    EINA_LIST_FREE(route->path, p)
4480      {
4481         evas_object_del(p);
4482      }
4483
4484    EINA_LIST_FREE(route->waypoint, w)
4485      {
4486         if (w->point) eina_stringshare_del(w->point);
4487         free(w);
4488      }
4489
4490    EINA_LIST_FREE(route->nodes, n)
4491      {
4492         if (n->pos.address) eina_stringshare_del(n->pos.address);
4493         free(n);
4494      }
4495
4496    EINA_LIST_FREE(route->handlers, h)
4497      {
4498         ecore_event_handler_del(h);
4499      }
4500
4501    if (route->ud.fname)
4502      {
4503         ecore_file_remove(route->ud.fname);
4504         free(route->ud.fname);
4505         route->ud.fname = NULL;
4506      }
4507 #else
4508    (void) route;
4509 #endif
4510 }
4511
4512 EAPI void
4513 elm_map_route_color_set(Elm_Map_Route *route, int r, int g , int b, int a)
4514 {
4515 #ifdef HAVE_ELEMENTARY_ECORE_CON
4516    EINA_SAFETY_ON_NULL_RETURN(route);
4517    route->color.r = r;
4518    route->color.g = g;
4519    route->color.b = b;
4520    route->color.a = a;
4521 #else
4522    (void) route;
4523    (void) r;
4524    (void) g;
4525    (void) b;
4526    (void) a;
4527 #endif
4528 }
4529
4530 EAPI void
4531 elm_map_route_color_get(const Elm_Map_Route *route, int *r, int *g , int *b, int *a)
4532 {
4533 #ifdef HAVE_ELEMENTARY_ECORE_CON
4534    EINA_SAFETY_ON_NULL_RETURN(route);
4535    if (r) *r = route->color.r;
4536    if (g) *g = route->color.g;
4537    if (b) *b = route->color.b;
4538    if (a) *a = route->color.a;
4539 #else
4540    (void) route;
4541    (void) r;
4542    (void) g;
4543    (void) b;
4544    (void) a;
4545 #endif
4546 }
4547
4548 EAPI double
4549 elm_map_route_distance_get(const Elm_Map_Route *route)
4550 {
4551 #ifdef HAVE_ELEMENTARY_ECORE_CON
4552    EINA_SAFETY_ON_NULL_RETURN_VAL(route, 0.0);
4553    return route->info.distance;
4554 #else
4555    (void) route;
4556    return 0.0;
4557 #endif
4558 }
4559
4560 EAPI const char*
4561 elm_map_route_node_get(const Elm_Map_Route *route)
4562 {
4563 #ifdef HAVE_ELEMENTARY_ECORE_CON
4564    EINA_SAFETY_ON_NULL_RETURN_VAL(route, NULL);
4565    return route->info.nodes;
4566 #else
4567    (void) route;
4568    return NULL;
4569 #endif
4570 }
4571
4572 EAPI const char*
4573 elm_map_route_waypoint_get(const Elm_Map_Route *route)
4574 {
4575 #ifdef HAVE_ELEMENTARY_ECORE_CON
4576    EINA_SAFETY_ON_NULL_RETURN_VAL(route, NULL);
4577    return route->info.waypoints;
4578 #else
4579    (void) route;
4580    return NULL;
4581 #endif
4582 }
4583
4584 EAPI const char *
4585 elm_map_name_address_get(const Elm_Map_Name *name)
4586 {
4587 #ifdef HAVE_ELEMENTARY_ECORE_CON
4588    EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
4589    return name->address;
4590 #else
4591    (void) name;
4592    return NULL;
4593 #endif
4594 }
4595
4596 EAPI void
4597 elm_map_name_region_get(const Elm_Map_Name *name, double *lon, double *lat)
4598 {
4599 #ifdef HAVE_ELEMENTARY_ECORE_CON
4600    EINA_SAFETY_ON_NULL_RETURN(name);
4601    if (lon) *lon = name->lon;
4602    if (lat) *lat = name->lat;
4603 #else
4604    (void) name;
4605    (void) lon;
4606    (void) lat;
4607 #endif
4608 }
4609
4610 EAPI void
4611 elm_map_name_remove(Elm_Map_Name *name)
4612 {
4613 #ifdef HAVE_ELEMENTARY_ECORE_CON
4614    EINA_SAFETY_ON_NULL_RETURN(name);
4615    if (name->address)
4616      {
4617         free(name->address);
4618         name->address = NULL;
4619      }
4620    if (name->handler)
4621      {
4622         ecore_event_handler_del(name->handler);
4623         name->handler = NULL;
4624      }
4625    if (name->ud.fname)
4626      {
4627         ecore_file_remove(name->ud.fname);
4628         free(name->ud.fname);
4629         name->ud.fname = NULL;
4630      }
4631 #else
4632    (void) name;
4633 #endif
4634 }
4635
4636 EAPI void
4637 elm_map_rotate_set(Evas_Object *obj, double degree, Evas_Coord cx, Evas_Coord cy)
4638 {
4639 #ifdef HAVE_ELEMENTARY_ECORE_CON
4640    ELM_CHECK_WIDTYPE(obj, widtype);
4641    Widget_Data *wd = elm_widget_data_get(obj);
4642
4643    if (!wd) return;
4644    wd->rotate.d = degree;
4645    wd->rotate.cx = cx;
4646    wd->rotate.cy = cy;
4647    wd->calc_job = ecore_job_add(_calc_job, wd);
4648 #else
4649    (void) obj;
4650    (void) degree;
4651    (void) cx;
4652    (void) cy;
4653 #endif
4654 }
4655
4656 EAPI void
4657 elm_map_rotate_get(const Evas_Object *obj, double *degree, Evas_Coord *cx, Evas_Coord *cy)
4658 {
4659 #ifdef HAVE_ELEMENTARY_ECORE_CON
4660    ELM_CHECK_WIDTYPE(obj, widtype);
4661    Widget_Data *wd = elm_widget_data_get(obj);
4662
4663    if (!wd) return;
4664    if (degree) *degree = wd->rotate.d;
4665    if (cx) *cx = wd->rotate.cx;
4666    if (cy) *cy = wd->rotate.cy;
4667 #else
4668    (void) obj;
4669    (void) degree;
4670    (void) cx;
4671    (void) cy;
4672 #endif
4673 }
4674
4675 EAPI void
4676 elm_map_wheel_disabled_set(Evas_Object *obj, Eina_Bool disabled)
4677 {
4678 #ifdef HAVE_ELEMENTARY_ECORE_CON
4679    ELM_CHECK_WIDTYPE(obj, widtype);
4680    Widget_Data *wd = elm_widget_data_get(obj);
4681
4682    if (!wd) return;
4683    if ((!wd->wheel_disabled) && (disabled))
4684      evas_object_event_callback_del_full(wd->rect, EVAS_CALLBACK_MOUSE_WHEEL, _mouse_wheel_cb, obj);
4685    else if ((wd->wheel_disabled) && (!disabled))
4686      evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_WHEEL, _mouse_wheel_cb, obj);
4687    wd->wheel_disabled = !!disabled;
4688 #else
4689    (void) obj;
4690    (void) disabled;
4691 #endif
4692 }
4693
4694 EAPI Eina_Bool
4695 elm_map_wheel_disabled_get(const Evas_Object *obj)
4696 {
4697 #ifdef HAVE_ELEMENTARY_ECORE_CON
4698    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
4699    Widget_Data *wd = elm_widget_data_get(obj);
4700
4701    if (!wd) return EINA_FALSE;
4702    return wd->wheel_disabled;
4703 #else
4704    (void) obj;
4705    return EINA_FALSE;
4706 #endif
4707 }
4708
4709 #ifdef ELM_EMAP
4710 EAPI Evas_Object *
4711 elm_map_track_add(Evas_Object *obj, EMap_Route *emap)
4712 {
4713 #ifdef HAVE_ELEMENTARY_ECORE_CON
4714    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4715    Widget_Data *wd = elm_widget_data_get(obj);
4716
4717    if (!wd) return EINA_FALSE;
4718
4719    Evas_Object *route = elm_route_add(obj);
4720    elm_route_emap_set(route, emap);
4721    wd->track = eina_list_append(wd->track, route);
4722
4723    return route;
4724 #else
4725    (void) obj;
4726    (void) emap;
4727    return NULL;
4728 #endif
4729 }
4730 #endif
4731
4732 EAPI void
4733 elm_map_track_remove(Evas_Object *obj, Evas_Object *route)
4734 {
4735 #ifdef HAVE_ELEMENTARY_ECORE_CON
4736    ELM_CHECK_WIDTYPE(obj, widtype) ;
4737    Widget_Data *wd = elm_widget_data_get(obj);
4738
4739    if (!wd) return ;
4740
4741    wd->track = eina_list_remove(wd->track, route);
4742    evas_object_del(route);
4743 #else
4744    (void) obj;
4745    (void) route;
4746 #endif
4747 }
4748
4749 #ifdef HAVE_ELEMENTARY_ECORE_CON
4750
4751 static char *
4752 _mapnik_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4753 {
4754    char buf[PATH_MAX];
4755    // ((x+y+zoom)%3)+'a' is requesting map images from distributed tile servers (eg., a, b, c)
4756    snprintf(buf, sizeof(buf), "http://%c.tile.openstreetmap.org/%d/%d/%d.png", ((x+y+zoom)%3)+'a', zoom, x, y);
4757    return strdup(buf);
4758 }
4759
4760 static char *
4761 _osmarender_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4762 {
4763    char buf[PATH_MAX];
4764    snprintf(buf, sizeof(buf), "http://%c.tah.openstreetmap.org/Tiles/tile/%d/%d/%d.png", ((x+y+zoom)%3)+'a', zoom, x, y);
4765    return strdup(buf);
4766 }
4767
4768 static char *
4769 _cyclemap_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4770 {
4771    char buf[PATH_MAX];
4772    snprintf(buf, sizeof(buf), "http://%c.tile.opencyclemap.org/cycle/%d/%d/%d.png", ((x+y+zoom)%3)+'a', zoom, x, y);
4773    return strdup(buf);
4774 }
4775
4776 static char *
4777 _mapquest_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4778 {
4779    char buf[PATH_MAX];
4780    snprintf(buf, sizeof(buf), "http://otile%d.mqcdn.com/tiles/1.0.0/osm/%d/%d/%d.png", ((x+y+zoom)%4)+1, zoom, x, y);
4781    return strdup(buf);
4782 }
4783
4784 static char *
4785 _mapquest_aerial_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4786 {
4787    char buf[PATH_MAX];
4788    snprintf(buf, sizeof(buf), "http://oatile%d.mqcdn.com/naip/%d/%d/%d.png", ((x+y+zoom)%4)+1, zoom, x, y);
4789    return strdup(buf);
4790 }
4791
4792 static char *_yours_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat)
4793 {
4794    char buf[PATH_MAX];
4795    snprintf(buf, sizeof(buf),
4796             "%s?flat=%lf&flon=%lf&tlat=%lf&tlon=%lf&v=%s&fast=%d&instructions=1",
4797             ROUTE_YOURS_URL, flat, flon, tlat, tlon, type_name, method);
4798
4799    return strdup(buf);
4800 }
4801
4802 // TODO: fix monav api
4803 /*
4804 static char *_monav_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat)
4805 {
4806    char buf[PATH_MAX];
4807    snprintf(buf, sizeof(buf),
4808             "%s?flat=%f&flon=%f&tlat=%f&tlon=%f&v=%s&fast=%d&instructions=1",
4809             ROUTE_MONAV_URL, flat, flon, tlat, tlon, type_name, method);
4810
4811    return strdup(buf);
4812 }
4813 */
4814
4815 // TODO: fix ors api
4816 /*
4817 static char *_ors_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat)
4818 {
4819    char buf[PATH_MAX];
4820    snprintf(buf, sizeof(buf),
4821             "%s?flat=%f&flon=%f&tlat=%f&tlon=%f&v=%s&fast=%d&instructions=1",
4822             ROUTE_ORS_URL, flat, flon, tlat, tlon, type_name, method);
4823
4824    return strdup(buf);
4825 }
4826 */
4827
4828 static char *
4829 _nominatim_url_cb(Evas_Object *obj, int method, char *name, double lon, double lat)
4830 {
4831    ELM_CHECK_WIDTYPE(obj, widtype) strdup("");
4832    Widget_Data *wd = elm_widget_data_get(obj);
4833    char **str;
4834    unsigned int ele, idx;
4835    char search_url[PATH_MAX];
4836    char buf[PATH_MAX];
4837
4838    if (!wd) return strdup("");
4839    if (method == ELM_MAP_NAME_METHOD_SEARCH)
4840      {
4841         search_url[0] = '\0';
4842         str = eina_str_split_full(name, " ", 0, &ele);
4843         for (idx = 0 ; idx < ele ; idx++)
4844           {
4845              eina_strlcat(search_url, str[idx], sizeof(search_url));
4846              if (!(idx == (ele-1))) eina_strlcat(search_url, "+", sizeof(search_url));
4847           }
4848         snprintf(buf, sizeof(buf), "%s/search?q=%s&format=xml&polygon=0&addressdetails=0", NAME_NOMINATIM_URL, search_url);
4849
4850         if (str && str[0])
4851           {
4852              free(str[0]);
4853              free(str);
4854           }
4855      }
4856    else if (method == ELM_MAP_NAME_METHOD_REVERSE) snprintf(buf, sizeof(buf), "%s/reverse?format=xml&lat=%lf&lon=%lf&zoom=%d&addressdetails=0", NAME_NOMINATIM_URL, lat, lon, wd->zoom);
4857    else strcpy(buf, "");
4858
4859    return strdup(buf);
4860 }
4861
4862 #endif