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