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