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