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