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