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