elm_map: fix pinch zoom 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         wd->pinch.level = 1.0;
1465         zoom_do(obj);
1466         wd->zoom_animator = NULL;
1467         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
1468         return ECORE_CALLBACK_CANCEL;
1469      }
1470    else
1471      {
1472         Evas_Coord x, y, w, h;
1473         float half_w, half_h;
1474
1475         evas_object_geometry_get(data, &x, &y, &w, &h);
1476         half_w = (float)w * 0.5;
1477         half_h = (float)h * 0.5;
1478
1479         wd->pinch.level = t;
1480         wd->pinch.cx = x + half_w;
1481         wd->pinch.cy = y + half_h;
1482         if (wd->calc_job) ecore_job_del(wd->calc_job);
1483         wd->calc_job = ecore_job_add(_calc_job, wd);
1484         return ECORE_CALLBACK_RENEW;
1485      }
1486 }
1487
1488 static Eina_Bool
1489 _long_press(void *data)
1490 {
1491    Widget_Data *wd = elm_widget_data_get(data);
1492    if (!wd) return ECORE_CALLBACK_CANCEL;
1493    wd->long_timer = NULL;
1494    evas_object_smart_callback_call(data, SIG_LONGPRESSED, &wd->ev);
1495    return ECORE_CALLBACK_CANCEL;
1496 }
1497
1498 static void
1499 _mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
1500 {
1501    ELM_CHECK_WIDTYPE(data, widtype);
1502    Widget_Data *wd = elm_widget_data_get(data);
1503    Evas_Event_Mouse_Down *ev = event_info;
1504    Event *ev0;
1505
1506    ev0 = get_event_object(data, 0);
1507    if (ev0) return;
1508    ev0 = create_event_object(data, obj, 0);
1509    if (!ev0) return;
1510
1511    ev0->hold_timer = NULL;
1512    ev0->prev.x = ev->output.x;
1513    ev0->prev.y = ev->output.y;
1514
1515    if (!wd) return;
1516    if (ev->button != 1) return;
1517    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
1518    else wd->on_hold = EINA_FALSE;
1519    if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
1520      evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, ev);
1521    else
1522      evas_object_smart_callback_call(data, SIG_PRESS, ev);
1523    if (wd->long_timer) ecore_timer_del(wd->long_timer);
1524    wd->ev.output.x = ev->output.x;
1525    wd->ev.output.y = ev->output.y;
1526    wd->long_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press, data);
1527 }
1528
1529 static void
1530 _mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1531 {
1532    Evas_Event_Mouse_Move *move = event_info;
1533    Event *ev0;
1534
1535    ev0 = get_event_object(data, 0);
1536    if (!ev0) return;
1537    ev0->prev.x = move->cur.output.x;
1538    ev0->prev.y = move->cur.output.y;
1539 }
1540
1541 static void
1542 _mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1543 {
1544    ELM_CHECK_WIDTYPE(data, widtype);
1545    Widget_Data *wd = elm_widget_data_get(data);
1546    Evas_Event_Mouse_Up *ev = event_info;
1547    int mdevice;
1548    Event *ev0;
1549    Event *ev1;
1550
1551    ev0 = get_event_object(data, 0);
1552    if (ev0)
1553      {
1554         mdevice = get_multi_device(data);
1555         if (mdevice == 0)
1556           {
1557              if (ev0->hold_timer)
1558                {
1559                   ecore_timer_del(ev0->hold_timer);
1560                   ev0->hold_timer = NULL;
1561                }
1562              elm_smart_scroller_hold_set(wd->scr, 0);
1563              elm_smart_scroller_freeze_set(wd->scr, 0);
1564           }
1565         else
1566           {
1567              ev1 = get_event_object(data, mdevice);
1568              if (ev1)
1569                ev1->hold_timer = ecore_timer_add(0.35, _hold_timer_cb, ev1);
1570           }
1571         destroy_event_object(data, ev0);
1572      }
1573
1574    if (!wd) return;
1575    if (ev->button != 1) return;
1576    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
1577    else wd->on_hold = EINA_FALSE;
1578    if (wd->long_timer)
1579      {
1580         ecore_timer_del(wd->long_timer);
1581         wd->long_timer = NULL;
1582      }
1583    if (!wd->on_hold) evas_object_smart_callback_call(data, SIG_CLICKED, ev);
1584    wd->on_hold = EINA_FALSE;
1585 }
1586
1587 static void
1588 _mouse_multi_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
1589 {
1590    ELM_CHECK_WIDTYPE(data, widtype);
1591    Widget_Data *wd = elm_widget_data_get(data);
1592    Event *ev;
1593    Evas_Event_Multi_Down *down = event_info;
1594
1595    elm_smart_scroller_hold_set(wd->scr, 1);
1596    elm_smart_scroller_freeze_set(wd->scr, 1);
1597
1598    ev = create_event_object(data, obj, down->device);
1599    if (!ev)
1600      {
1601         DBG("Failed : create_event_object");
1602         goto done;
1603      }
1604    wd->multi_count++;
1605
1606    ev->hold_timer = NULL;
1607    ev->start.x = ev->prev.x = down->output.x;
1608    ev->start.y = ev->prev.y = down->output.y;
1609    ev->pinch_start_dis = 0;
1610    wd->pinch.level = 1.0;
1611    wd->pinch.diff = 1.0;
1612
1613 done:
1614    if (wd->long_timer)
1615      {
1616         ecore_timer_del(wd->long_timer);
1617         wd->long_timer = NULL;
1618      }
1619    return;
1620 }
1621
1622 static void
1623 _mouse_multi_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1624 {
1625    ELM_CHECK_WIDTYPE(data, widtype);
1626    Widget_Data *wd = elm_widget_data_get(data);
1627    Evas_Event_Multi_Move *move = event_info;
1628    int dis_new;
1629    double t, tt, a, a_diff;
1630    Event *ev0;
1631    Event *ev;
1632
1633    ev = get_event_object(data, move->device);
1634    if (!ev) return;
1635
1636    ev0 = get_event_object(data, 0);
1637    if (!ev0) return;
1638
1639    if (wd->multi_count >= 1)
1640      {
1641         Evas_Coord x, y, w, h;
1642         float half_w, half_h;
1643
1644         evas_object_geometry_get(data, &x, &y, &w, &h);
1645         half_w = (float)w * 0.5;
1646         half_h = (float)h * 0.5;
1647         dis_new = get_distance(ev0->prev.x, ev0->prev.y, ev->prev.x, ev->prev.y);
1648
1649         if (!ev->pinch_start_dis) ev->pinch_start_dis = dis_new;
1650         else
1651           {
1652              ev->pinch_dis = dis_new;
1653              tt = wd->pinch.diff;
1654              wd->pinch.diff = (double)(ev->pinch_dis - ev->pinch_start_dis);
1655              t = (wd->pinch.diff * 0.01) + 1.0;
1656              if ((!wd->zoom) || ((wd->zoom + (int)t - 1) <= map_sources_tab[wd->source].zoom_min) ||
1657                  ((wd->zoom + (int)t - 1) >= map_sources_tab[wd->source].zoom_max) ||
1658                  (t > PINCH_ZOOM_MAX) || (t < PINCH_ZOOM_MIN))
1659                {
1660                   wd->pinch.diff = tt;
1661                   goto do_nothing;
1662                }
1663              else
1664                {
1665                   wd->pinch.level = (wd->pinch.diff * 0.01) + 1.0;
1666                   wd->pinch.cx = x + half_w;
1667                   wd->pinch.cy = y + half_h;
1668                }
1669
1670              a = (double)(ev->prev.y - ev0->prev.y) / (double)(ev->prev.x - ev0->prev.x);
1671              if (!wd->rotate.a) wd->rotate.a = a;
1672              else
1673                {
1674
1675                   a_diff = wd->rotate.a - a;
1676                   if (a_diff > 0) wd->rotate.d -= 1.0;
1677                   else if (a_diff < 0) wd->rotate.d += 1.0;
1678                   wd->rotate.a = a;
1679                   wd->rotate.cx = x + half_w;
1680                   wd->rotate.cy = y + half_h;
1681                }
1682
1683              if (wd->calc_job) ecore_job_del(wd->calc_job);
1684              wd->calc_job = ecore_job_add(_calc_job, wd);
1685           }
1686      }
1687 do_nothing:
1688    ev->prev.x = move->cur.output.x;
1689    ev->prev.y = move->cur.output.y;
1690 }
1691
1692 static void
1693 _mouse_multi_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1694 {
1695    ELM_CHECK_WIDTYPE(data, widtype);
1696    Widget_Data *wd = elm_widget_data_get(data);
1697    Evas_Event_Multi_Up *up = event_info;
1698    Event *ev0;
1699    Event *ev;
1700    Eina_Bool tp;
1701    double t = 0.0;
1702
1703    wd->multi_count--;
1704    if (wd->calc_job) ecore_job_del(wd->calc_job);
1705    if (wd->zoom_animator)
1706      {
1707         ecore_animator_del(wd->zoom_animator);
1708         wd->zoom_animator = NULL;
1709      }
1710    tp = wd->paused;
1711    wd->paused = EINA_TRUE;
1712    if (wd->pinch.diff >= 0.0) t = wd->pinch.diff * 0.01;
1713    else if (wd->pinch.diff < 0.0) t = -1.0 / ((wd->pinch.diff * 0.01) + 1.0);
1714    elm_map_zoom_set(data, wd->zoom + (int)ceil(t));
1715    wd->pinch.level = 1.0;
1716    wd->paused = tp;
1717    wd->rotate.a = 0.0;
1718
1719    ev = get_event_object(data, up->device);
1720    if (!ev)
1721      {
1722         DBG("Cannot get multi device");
1723         return;
1724      }
1725
1726    ev0 = get_event_object(data, 0);
1727    if (ev0)
1728      ev0->hold_timer = ecore_timer_add(0.35, _hold_timer_cb, ev0);
1729    else
1730      {
1731         if (ev->hold_timer)
1732           {
1733              ecore_timer_del(ev->hold_timer);
1734              ev->hold_timer = NULL;
1735           }
1736      }
1737    destroy_event_object(data, ev);
1738 }
1739
1740 static Evas_Smart_Class _pan_sc = EVAS_SMART_CLASS_INIT_NULL;
1741
1742 static Eina_Bool
1743 _hold_timer_cb(void *data)
1744 {
1745    Event *ev0 = data;
1746
1747    ev0->hold_timer = NULL;
1748    return ECORE_CALLBACK_CANCEL;
1749 }
1750
1751 static void
1752 _rect_resize_cb(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1753 {
1754    Widget_Data *wd = elm_widget_data_get(data);
1755    int x, y, w, h;
1756
1757    evas_object_geometry_get(wd->rect, &x, &y, &w, &h);
1758    evas_object_geometry_get(wd->pan_smart, &x, &y, &w, &h);
1759    evas_object_resize(wd->rect, w, h);
1760    evas_object_move(wd->rect, x, y);
1761 }
1762
1763 static void
1764 _del_hook(Evas_Object *obj)
1765 {
1766    Elm_Map_Group_Class *group_clas;
1767    Elm_Map_Marker_Class *marker_clas;
1768    Widget_Data *wd = elm_widget_data_get(obj);
1769    Eina_List *l;
1770    Event *ev;
1771    Evas_Object *p;
1772    Route_Node *n;
1773    Route_Waypoint *w;
1774    Ecore_Event_Handler *h;
1775    Elm_Map_Route *r;
1776    Elm_Map_Name *na;
1777
1778    if (!wd) return;
1779
1780    EINA_LIST_FREE(wd->groups_clas, group_clas)
1781      {
1782         if (group_clas->style)
1783           eina_stringshare_del(group_clas->style);
1784         free(group_clas);
1785      }
1786
1787    EINA_LIST_FREE(wd->markers_clas, marker_clas)
1788      {
1789         if (marker_clas->style)
1790           eina_stringshare_del(marker_clas->style);
1791         free(marker_clas);
1792      }
1793
1794    EINA_LIST_FOREACH(wd->s_event_list, l, ev)
1795      {
1796         destroy_event_object(obj, ev);
1797      }
1798
1799    EINA_LIST_FOREACH(wd->route, l, r)
1800      {
1801         EINA_LIST_FREE(r->path, p)
1802           {
1803              evas_object_del(p);
1804           }
1805
1806         EINA_LIST_FREE(r->waypoint, w)
1807           {
1808              if (w->point) eina_stringshare_del(w->point);
1809              free(w);
1810           }
1811
1812         EINA_LIST_FREE(r->nodes, n)
1813           {
1814              if (n->pos.address) eina_stringshare_del(n->pos.address);
1815              free(n);
1816           }
1817
1818         EINA_LIST_FREE(r->handlers, h)
1819           {
1820              ecore_event_handler_del(h);
1821           }
1822
1823         if (r->con_url) ecore_con_url_free(r->con_url);
1824         if (r->info.nodes) eina_stringshare_del(r->info.nodes);
1825         if (r->info.waypoints) eina_stringshare_del(r->info.waypoints);
1826      }
1827
1828    EINA_LIST_FREE(wd->names, na)
1829      {
1830         if (na->address) free(na->address);
1831         if (na->handler) ecore_event_handler_del(na->handler);
1832         if (na->ud.fname)
1833           {
1834              ecore_file_remove(na->ud.fname);
1835              free(na->ud.fname);
1836              na->ud.fname = NULL;
1837           }
1838      }
1839
1840    if (wd->calc_job) ecore_job_del(wd->calc_job);
1841    if (wd->scr_timer) ecore_timer_del(wd->scr_timer);
1842    if (wd->zoom_animator) ecore_animator_del(wd->zoom_animator);
1843    if (wd->long_timer) ecore_timer_del(wd->long_timer);
1844    if ((wd->api) && (wd->api->obj_unhook)) wd->api->obj_unhook(obj);
1845    if (wd->user_agent) eina_stringshare_del(wd->user_agent);
1846    if (wd->ua) eina_hash_free(wd->ua);
1847
1848    free(wd);
1849 }
1850
1851 static void
1852 _del_pre_hook(Evas_Object *obj)
1853 {
1854    Marker_Group *group;
1855    Elm_Map_Marker *marker;
1856    int i;
1857    Eina_Bool free_marker = EINA_TRUE;
1858    Eina_List *l;
1859    Widget_Data *wd = elm_widget_data_get(obj);
1860    grid_clearall(obj);
1861
1862    if (!wd) return;
1863    for (i = 0; i < ZOOM_MAX + 1; i++)
1864      {
1865         if (!wd->markers[i]) continue;
1866         Eina_Iterator *it = eina_matrixsparse_iterator_new(wd->markers[i]);
1867         Eina_Matrixsparse_Cell *cell;
1868
1869         EINA_ITERATOR_FOREACH(it, cell)
1870           {
1871              l =  eina_matrixsparse_cell_data_get(cell);
1872              EINA_LIST_FREE(l, group)
1873                {
1874                   EINA_LIST_FREE(group->markers, marker)
1875                     {
1876                        evas_object_event_callback_del_full(group->sc, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1877                                                            _bubble_sc_hits_changed_cb, group);
1878                        if (free_marker) free(marker);
1879                     }
1880                   free(group);
1881                }
1882              free_marker = EINA_FALSE;
1883           }
1884         eina_iterator_free(it);
1885         eina_matrixsparse_free(wd->markers[i]);
1886      }
1887
1888    evas_object_del(wd->sep_maps_markers);
1889    evas_object_del(wd->pan_smart);
1890    wd->pan_smart = NULL;
1891 }
1892
1893 static void
1894 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
1895 {
1896    Widget_Data *wd = elm_widget_data_get(obj);
1897    if (!wd) return;
1898    if (elm_widget_focus_get(obj))
1899      {
1900         edje_object_signal_emit(wd->obj, "elm,action,focus", "elm");
1901         evas_object_focus_set(wd->obj, EINA_TRUE);
1902      }
1903    else
1904      {
1905         edje_object_signal_emit(wd->obj, "elm,action,unfocus", "elm");
1906         evas_object_focus_set(wd->obj, EINA_FALSE);
1907      }
1908 }
1909
1910 static void
1911 _theme_hook(Evas_Object *obj)
1912 {
1913    Widget_Data *wd = elm_widget_data_get(obj);
1914    if (!wd) return;
1915    elm_smart_scroller_object_theme_set(obj, wd->scr, "map", "base", elm_widget_style_get(obj));
1916    //   edje_object_scale_set(wd->scr, elm_widget_scale_get(obj) * _elm_config->scale);
1917    _sizing_eval(obj);
1918 }
1919
1920 static void
1921 _sizing_eval(Evas_Object *obj)
1922 {
1923    Widget_Data *wd = elm_widget_data_get(obj);
1924    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
1925    if (!wd) return;
1926    evas_object_size_hint_max_get(wd->scr, &maxw, &maxh);
1927    evas_object_size_hint_min_set(obj, minw, minh);
1928    evas_object_size_hint_max_set(obj, maxw, maxh);
1929 }
1930
1931 static void
1932 _calc_job(void *data)
1933 {
1934    Widget_Data *wd = data;
1935    Evas_Coord minw, minh;
1936
1937    minw = wd->size.w;
1938    minh = wd->size.h;
1939    if (wd->resized)
1940      {
1941         wd->resized = 0;
1942         if (wd->mode != ELM_MAP_ZOOM_MODE_MANUAL)
1943           {
1944              double tz = wd->zoom;
1945              wd->zoom = 0.0;
1946              elm_map_zoom_set(wd->obj, tz);
1947           }
1948      }
1949    if ((minw != wd->minw) || (minh != wd->minh))
1950      {
1951         wd->minw = minw;
1952         wd->minh = minh;
1953         evas_object_smart_callback_call(wd->pan_smart, SIG_CHANGED, NULL);
1954         _sizing_eval(wd->obj);
1955      }
1956    wd->calc_job = NULL;
1957    evas_object_smart_changed(wd->pan_smart);
1958 }
1959
1960 static void
1961 _pan_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
1962 {
1963    Pan *sd = evas_object_smart_data_get(obj);
1964    if (!sd) return;
1965    if ((x == sd->wd->pan_x) && (y == sd->wd->pan_y)) return;
1966    sd->wd->pan_x = x;
1967    sd->wd->pan_y = y;
1968    evas_object_smart_changed(obj);
1969 }
1970
1971 static void
1972 _pan_get(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) *x = sd->wd->pan_x;
1977    if (y) *y = sd->wd->pan_y;
1978 }
1979
1980 static void
1981 _pan_max_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1982 {
1983    Pan *sd = evas_object_smart_data_get(obj);
1984    Evas_Coord ow, oh;
1985    if (!sd) return;
1986    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
1987    ow = sd->wd->minw - ow;
1988    if (ow < 0) ow = 0;
1989    oh = sd->wd->minh - oh;
1990    if (oh < 0) oh = 0;
1991    if (x) *x = ow;
1992    if (y) *y = oh;
1993 }
1994
1995 static void
1996 _pan_min_get(Evas_Object *obj __UNUSED__, Evas_Coord *x, Evas_Coord *y)
1997 {
1998    if (x) *x = 0;
1999    if (y) *y = 0;
2000 }
2001
2002 static void
2003 _pan_child_size_get(Evas_Object *obj, Evas_Coord *w, Evas_Coord *h)
2004 {
2005    Pan *sd = evas_object_smart_data_get(obj);
2006    if (!sd) return;
2007    if (w) *w = sd->wd->minw;
2008    if (h) *h = sd->wd->minh;
2009 }
2010
2011 static void
2012 _pan_add(Evas_Object *obj)
2013 {
2014    Pan *sd;
2015    Evas_Object_Smart_Clipped_Data *cd;
2016    _pan_sc.add(obj);
2017    cd = evas_object_smart_data_get(obj);
2018    if (!cd) return;
2019    sd = calloc(1, sizeof(Pan));
2020    if (!sd) return;
2021    sd->__clipped_data = *cd;
2022    free(cd);
2023    evas_object_smart_data_set(obj, sd);
2024 }
2025
2026 static void
2027 _pan_del(Evas_Object *obj)
2028 {
2029    Pan *sd = evas_object_smart_data_get(obj);
2030    if (!sd) return;
2031    _pan_sc.del(obj);
2032 }
2033
2034 static void
2035 _pan_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
2036 {
2037    Pan *sd = evas_object_smart_data_get(obj);
2038    Evas_Coord ow, oh;
2039    if (!sd) return;
2040    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
2041    if ((ow == w) && (oh == h)) return;
2042    sd->wd->resized = 1;
2043    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
2044    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
2045 }
2046
2047 static void
2048 _pan_calculate(Evas_Object *obj)
2049 {
2050    Pan *sd = evas_object_smart_data_get(obj);
2051    Evas_Coord ox, oy, ow, oh;
2052    Eina_List *l;
2053    Grid *g;
2054    if (!sd) return;
2055    evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
2056    rect_place(sd->wd->obj, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2057    EINA_LIST_FOREACH(sd->wd->grids, l, g)
2058      {
2059         if (sd->wd->pinch.level == 1.0) grid_load(sd->wd->obj, g);
2060         grid_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2061         marker_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2062         if (!sd->wd->zoom_animator) route_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
2063      }
2064 }
2065
2066 static void
2067 _pan_move(Evas_Object *obj, Evas_Coord x __UNUSED__, Evas_Coord y __UNUSED__)
2068 {
2069    Pan *sd = evas_object_smart_data_get(obj);
2070    if (!sd) return;
2071    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
2072    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
2073 }
2074
2075 static void
2076 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2077 {
2078    Widget_Data *wd = elm_widget_data_get(obj);
2079    if (!wd) return;
2080    elm_smart_scroller_hold_set(wd->scr, 1);
2081 }
2082
2083 static void
2084 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2085 {
2086    Widget_Data *wd = elm_widget_data_get(obj);
2087    if (!wd) return;
2088    elm_smart_scroller_hold_set(wd->scr, 0);
2089 }
2090
2091 static void
2092 _freeze_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2093 {
2094    Widget_Data *wd = elm_widget_data_get(obj);
2095    if (!wd) return;
2096    elm_smart_scroller_freeze_set(wd->scr, 1);
2097 }
2098
2099 static void
2100 _freeze_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
2101 {
2102    Widget_Data *wd = elm_widget_data_get(obj);
2103    if (!wd) return;
2104    elm_smart_scroller_freeze_set(wd->scr, 0);
2105 }
2106
2107 static void
2108 _scr_anim_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2109 {
2110    evas_object_smart_callback_call(data, "scroll,anim,start", NULL);
2111 }
2112
2113 static void
2114 _scr_anim_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2115 {
2116    evas_object_smart_callback_call(data, "scroll,anim,stop", NULL);
2117 }
2118
2119 static void
2120 _scr_drag_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2121 {
2122    Widget_Data *wd = elm_widget_data_get(data);
2123    wd->center_on.enabled = EINA_FALSE;
2124    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_START, NULL);
2125 }
2126
2127 static void
2128 _scr_drag_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2129 {
2130    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_STOP, NULL);
2131 }
2132
2133 static void
2134 _scr_scroll(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2135 {
2136    evas_object_smart_callback_call(data, SIG_SCROLL, NULL);
2137 }
2138
2139
2140 static void
2141 _group_object_create(Marker_Group *group)
2142 {
2143    const char *style = "radio";
2144    Evas_Object *icon = NULL;
2145
2146    if (group->obj) return;
2147    if ((!group->clas->priv.objs_notused) || (eina_list_count(group->markers) == 1))
2148      {
2149         //set icon and style
2150         if (eina_list_count(group->markers) == 1)
2151           {
2152              Elm_Map_Marker *m = eina_list_data_get(group->markers);
2153              if (m->clas->style)
2154                style = m->clas->style;
2155
2156              if (m->clas->func.icon_get)
2157                icon = m->clas->func.icon_get(group->wd->obj, m, m->data);
2158
2159              group->delete_object = EINA_TRUE;
2160           }
2161         else
2162           {
2163              if (group->clas->style)
2164                style = group->clas->style;
2165
2166              if (group->clas->func.icon_get)
2167                icon = group->clas->func.icon_get(group->wd->obj, group->clas->data);
2168
2169              group->delete_object = EINA_FALSE;
2170           }
2171
2172         group->obj = elm_layout_add(group->wd->obj);
2173         elm_layout_theme_set(group->obj, "map/marker", style, elm_widget_style_get(group->wd->obj));
2174
2175         if (icon) elm_layout_content_set(group->obj, "elm.icon", icon);
2176
2177         evas_object_smart_member_add(group->obj, group->wd->pan_smart);
2178         elm_widget_sub_object_add(group->wd->obj, group->obj);
2179         evas_object_stack_above(group->obj, group->wd->sep_maps_markers);
2180
2181         if (!group->delete_object)
2182           group->clas->priv.objs_used = eina_list_append(group->clas->priv.objs_used, group->obj);
2183      }
2184    else
2185      {
2186         group->delete_object = EINA_FALSE;
2187
2188         group->obj = eina_list_data_get(group->clas->priv.objs_notused);
2189         group->clas->priv.objs_used = eina_list_append(group->clas->priv.objs_used, group->obj);
2190         group->clas->priv.objs_notused = eina_list_remove(group->clas->priv.objs_notused, group->obj);
2191         evas_object_show(group->obj);
2192      }
2193
2194    edje_object_signal_callback_add(elm_layout_edje_get(group->obj), "open", "elm", _group_open_cb, group);
2195    edje_object_signal_callback_add(elm_layout_edje_get(group->obj), "bringin", "elm", _group_bringin_cb, group);
2196
2197    group->update_nbelems = EINA_TRUE;
2198    group->update_resize = EINA_TRUE;
2199    group->update_raise = EINA_TRUE;
2200
2201    if (group->open) _group_bubble_create(group);
2202 }
2203
2204 static void
2205 _group_object_free(Marker_Group *group)
2206 {
2207    if (!group->obj) return;
2208    if (!group->delete_object)
2209      {
2210         group->clas->priv.objs_notused = eina_list_append(group->clas->priv.objs_notused, group->obj);
2211         group->clas->priv.objs_used = eina_list_remove(group->clas->priv.objs_used, group->obj);
2212         evas_object_hide(group->obj);
2213
2214         edje_object_signal_callback_del(elm_layout_edje_get(group->obj), "open", "elm", _group_open_cb);
2215         edje_object_signal_callback_del(elm_layout_edje_get(group->obj), "bringin", "elm", _group_bringin_cb);
2216      }
2217    else
2218      evas_object_del(group->obj);
2219
2220    group->obj = NULL;
2221    _group_bubble_free(group);
2222 }
2223
2224 static void
2225 _group_bubble_mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2226 {
2227    Marker_Group *group = data;
2228
2229    if (!evas_object_above_get(group->rect)) return;
2230    evas_object_raise(group->bubble);
2231    evas_object_raise(group->sc);
2232    evas_object_raise(group->rect);
2233 }
2234
2235 static void
2236 _group_bubble_create(Marker_Group *group)
2237 {
2238    if (group->bubble) return;
2239
2240    group->wd->opened_bubbles = eina_list_append(group->wd->opened_bubbles, group);
2241    group->bubble = edje_object_add(evas_object_evas_get(group->obj));
2242    _elm_theme_object_set(group->wd->obj, group->bubble, "map", "marker_bubble",
2243                          elm_widget_style_get(group->wd->obj));
2244    evas_object_smart_member_add(group->bubble,
2245                                 group->wd->obj);
2246    elm_widget_sub_object_add(group->wd->obj, group->bubble);
2247
2248    _group_bubble_content_free(group);
2249    if (!_group_bubble_content_update(group))
2250      {
2251         //no content, we can delete the bubble
2252         _group_bubble_free(group);
2253         return;
2254      }
2255
2256    group->rect = evas_object_rectangle_add(evas_object_evas_get(group->obj));
2257    evas_object_color_set(group->rect, 0, 0, 0, 0);
2258    evas_object_repeat_events_set(group->rect, EINA_TRUE);
2259    evas_object_smart_member_add(group->rect, group->wd->obj);
2260    elm_widget_sub_object_add(group->wd->obj, group->rect);
2261
2262    evas_object_event_callback_add(group->rect, EVAS_CALLBACK_MOUSE_UP, _group_bubble_mouse_up_cb, group);
2263
2264    _group_bubble_place(group);
2265 }
2266
2267 static void _bubble_sc_hits_changed_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2268 {
2269    _group_bubble_place(data);
2270 }
2271
2272 static int
2273 _group_bubble_content_update(Marker_Group *group)
2274 {
2275    Eina_List *l;
2276    Elm_Map_Marker *marker;
2277    int i = 0;
2278
2279    if (!group->bubble) return 1;
2280
2281    if (!group->sc)
2282      {
2283         group->sc = elm_scroller_add(group->bubble);
2284         elm_widget_style_set(group->sc, "map_bubble");
2285         elm_scroller_content_min_limit(group->sc, EINA_FALSE, EINA_TRUE);
2286         elm_scroller_policy_set(group->sc, ELM_SCROLLER_POLICY_AUTO, ELM_SCROLLER_POLICY_OFF);
2287         elm_scroller_bounce_set(group->sc, _elm_config->thumbscroll_bounce_enable, EINA_FALSE);
2288         edje_object_part_swallow(group->bubble, "elm.swallow.content", group->sc);
2289         evas_object_show(group->sc);
2290         evas_object_smart_member_add(group->sc,
2291                                      group->wd->obj);
2292         elm_widget_sub_object_add(group->wd->obj, group->sc);
2293
2294         group->bx = elm_box_add(group->bubble);
2295         evas_object_size_hint_align_set(group->bx, EVAS_HINT_FILL, EVAS_HINT_FILL);
2296         evas_object_size_hint_weight_set(group->bx, 0.5, 0.5);
2297         elm_box_horizontal_set(group->bx, EINA_TRUE);
2298         evas_object_show(group->bx);
2299
2300         elm_scroller_content_set(group->sc, group->bx);
2301
2302         evas_object_event_callback_add(group->sc, EVAS_CALLBACK_RESIZE,
2303                                        _bubble_sc_hits_changed_cb, group);
2304      }
2305
2306    EINA_LIST_FOREACH(group->markers, l, marker)
2307      {
2308         if (i >= group->wd->markers_max_num) break;
2309         if ((!marker->content) && (marker->clas->func.get))
2310           marker->content = marker->clas->func.get(group->wd->obj, marker, marker->data);
2311         else if (marker->content)
2312           elm_box_unpack(group->bx, marker->content);
2313         if (marker->content)
2314           {
2315              elm_box_pack_end(group->bx, marker->content);
2316              i++;
2317           }
2318      }
2319    return i;
2320 }
2321
2322 static void
2323 _group_bubble_content_free(Marker_Group *group)
2324 {
2325    Eina_List *l;
2326    Elm_Map_Marker *marker;
2327
2328    if (!group->sc) return;
2329    EINA_LIST_FOREACH(group->markers, l, marker)
2330      {
2331         if ((marker->content) && (marker->clas->func.del))
2332           marker->clas->func.del(group->wd->obj, marker, marker->data, marker->content);
2333         else if (marker->content)
2334           evas_object_del(marker->content);
2335         marker->content = NULL;
2336      }
2337    evas_object_del(group->sc);
2338    group->sc = NULL;
2339 }
2340
2341 static void
2342 _group_bubble_free(Marker_Group *group)
2343 {
2344    if (!group->bubble) return;
2345    group->wd->opened_bubbles = eina_list_remove(group->wd->opened_bubbles, group);
2346    evas_object_event_callback_del_full(group->sc, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
2347                                        _bubble_sc_hits_changed_cb, group);
2348    evas_object_del(group->bubble);
2349    evas_object_del(group->rect);
2350    group->bubble = NULL;
2351    _group_bubble_content_free(group);
2352 }
2353
2354 static void
2355 _group_bubble_place(Marker_Group *group)
2356 {
2357    Evas_Coord x, y, w;
2358    Evas_Coord xx, yy, ww, hh;
2359    const char *s;
2360
2361    if ((!group->bubble) || (!group->obj)) return;
2362
2363    evas_object_geometry_get(group->obj, &x, &y, &w, NULL);
2364    edje_object_size_min_calc(group->bubble, NULL, &hh);
2365
2366    s = edje_object_data_get(group->bubble, "size_w");
2367    if (s) ww = atoi(s);
2368    else ww = 0;
2369    xx = x + w / 2 - ww / 2;
2370    yy = y-hh;
2371
2372    evas_object_move(group->bubble, xx, yy);
2373    evas_object_resize(group->bubble, ww, hh);
2374    obj_rotate_zoom(group->wd, group->bubble);
2375    evas_object_show(group->bubble);
2376
2377    evas_object_move(group->rect, xx, yy);
2378    evas_object_resize(group->rect, ww, hh);
2379    obj_rotate_zoom(group->wd, group->rect);
2380    evas_object_show(group->rect);
2381 }
2382
2383 static void
2384 _group_bringin_cb(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *soure __UNUSED__)
2385 {
2386    Marker_Group *group = data;
2387    Elm_Map_Marker *marker = eina_list_data_get(group->markers);
2388    if (!marker) return;
2389    group->bringin = EINA_TRUE;
2390    elm_map_geo_region_bring_in(group->wd->obj, marker->longitude, marker->latitude);
2391 }
2392
2393 static void
2394 _group_open_cb(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *soure __UNUSED__)
2395 {
2396    Marker_Group *group = data;
2397
2398    if (group->bringin)
2399      {
2400         group->bringin = EINA_FALSE;
2401         return;
2402      }
2403
2404    if (group->bubble)
2405      {
2406         group->open = EINA_FALSE;
2407         _group_bubble_free(group);
2408         return;
2409      }
2410    group->open = EINA_TRUE;
2411    _group_bubble_create(group);
2412 }
2413
2414 static Eina_Bool
2415 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
2416 {
2417    double zoom;
2418    Evas_Coord x = 0;
2419    Evas_Coord y = 0;
2420    Evas_Coord step_x = 0;
2421    Evas_Coord step_y = 0;
2422    Evas_Coord v_w = 0;
2423    Evas_Coord v_h = 0;
2424    Evas_Coord page_x = 0;
2425    Evas_Coord page_y = 0;
2426
2427    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
2428    Evas_Event_Key_Down *ev = event_info;
2429    Widget_Data *wd = elm_widget_data_get(obj);
2430    if (!wd) return EINA_FALSE;
2431    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
2432
2433    elm_smart_scroller_child_pos_get(wd->scr, &x, &y);
2434    elm_smart_scroller_step_size_get(wd->scr, &step_x, &step_y);
2435    elm_smart_scroller_page_size_get(wd->scr, &page_x, &page_y);
2436    elm_smart_scroller_child_viewport_size_get(wd->scr, &v_w, &v_h);
2437
2438    if ((!strcmp(ev->keyname, "Left")) || (!strcmp(ev->keyname, "KP_Left")))
2439      {
2440         x -= step_x;
2441      }
2442    else if ((!strcmp(ev->keyname, "Right")) || (!strcmp(ev->keyname, "KP_Right")))
2443      {
2444         x += step_x;
2445      }
2446    else if ((!strcmp(ev->keyname, "Up"))  || (!strcmp(ev->keyname, "KP_Up")))
2447      {
2448         y -= step_y;
2449      }
2450    else if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
2451      {
2452         y += step_y;
2453      }
2454    else if ((!strcmp(ev->keyname, "Prior")) || (!strcmp(ev->keyname, "KP_Prior")))
2455      {
2456         if (page_y < 0)
2457           y -= -(page_y * v_h) / 100;
2458         else
2459           y -= page_y;
2460      }
2461    else if ((!strcmp(ev->keyname, "Next")) || (!strcmp(ev->keyname, "KP_Next")))
2462      {
2463         if (page_y < 0)
2464           y += -(page_y * v_h) / 100;
2465         else
2466           y += page_y;
2467      }
2468    else if (!strcmp(ev->keyname, "KP_Add"))
2469      {
2470         zoom = elm_map_zoom_get(obj);
2471         zoom += 1;
2472         elm_map_zoom_mode_set(obj, ELM_MAP_ZOOM_MODE_MANUAL);
2473         elm_map_zoom_set(obj, zoom);
2474         return EINA_TRUE;
2475      }
2476    else if (!strcmp(ev->keyname, "KP_Subtract"))
2477      {
2478         zoom = elm_map_zoom_get(obj);
2479         zoom -= 1;
2480         elm_map_zoom_mode_set(obj, ELM_MAP_ZOOM_MODE_MANUAL);
2481         elm_map_zoom_set(obj, zoom);
2482         return EINA_TRUE;
2483      }
2484    else return EINA_FALSE;
2485
2486    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2487    elm_smart_scroller_child_pos_set(wd->scr, x, y);
2488
2489    return EINA_TRUE;
2490 }
2491
2492 static Eina_Bool
2493 cb_dump_name_attrs(void *data, const char *key, const char *value)
2494 {
2495    Name_Dump *dump = (Name_Dump*)data;
2496    if (!dump) return EINA_FALSE;
2497
2498    if (!strncmp(key, NOMINATIM_ATTR_LON, sizeof(NOMINATIM_ATTR_LON))) dump->lon = atof(value);
2499    else if (!strncmp(key, NOMINATIM_ATTR_LAT, sizeof(NOMINATIM_ATTR_LAT))) dump->lat = atof(value);
2500
2501    return EINA_TRUE;
2502 }
2503
2504
2505 static Eina_Bool
2506 cb_route_dump(void *data, Eina_Simple_XML_Type type, const char *value, unsigned offset __UNUSED__, unsigned length)
2507 {
2508    Route_Dump *dump = data;
2509    if (!dump) return EINA_FALSE;
2510
2511    switch (type)
2512      {
2513       case EINA_SIMPLE_XML_OPEN:
2514       case EINA_SIMPLE_XML_OPEN_EMPTY:
2515         {
2516            const char *attrs;
2517
2518            attrs = eina_simple_xml_tag_attributes_find(value, length);
2519            if (!attrs)
2520              {
2521                 if (!strncmp(value, YOURS_DISTANCE, length)) dump->id = ROUTE_XML_DISTANCE;
2522                 else if (!strncmp(value, YOURS_DESCRIPTION, length)) dump->id = ROUTE_XML_DESCRIPTION;
2523                 else if (!strncmp(value, YOURS_COORDINATES, length)) dump->id = ROUTE_XML_COORDINATES;
2524                 else dump->id = ROUTE_XML_NONE;
2525              }
2526          }
2527         break;
2528       case EINA_SIMPLE_XML_DATA:
2529         {
2530            char *buf = malloc(length);
2531            if (!buf) return EINA_FALSE;
2532            snprintf(buf, length, "%s", value);
2533            if (dump->id == ROUTE_XML_DISTANCE) dump->distance = atof(buf);
2534            else if (!(dump->description) && (dump->id == ROUTE_XML_DESCRIPTION)) dump->description = strdup(buf);
2535            else if (dump->id == ROUTE_XML_COORDINATES) dump->coordinates = strdup(buf);
2536            free(buf);
2537         }
2538         break;
2539       default:
2540         break;
2541      }
2542
2543    return EINA_TRUE;
2544 }
2545
2546 static Eina_Bool
2547 cb_name_dump(void *data, Eina_Simple_XML_Type type, const char *value, unsigned offset __UNUSED__, unsigned length)
2548 {
2549    Name_Dump *dump = data;
2550    if (!dump) return EINA_FALSE;
2551
2552    switch (type)
2553      {
2554       case EINA_SIMPLE_XML_OPEN:
2555       case EINA_SIMPLE_XML_OPEN_EMPTY:
2556         {
2557            const char *attrs;
2558            attrs = eina_simple_xml_tag_attributes_find(value, length);
2559            if (attrs)
2560              {
2561                 if (!strncmp(value, NOMINATIM_RESULT, sizeof(NOMINATIM_RESULT)-1)) dump->id = NAME_XML_NAME;
2562                 else dump->id = NAME_XML_NONE;
2563
2564                 eina_simple_xml_attributes_parse
2565                   (attrs, length - (attrs - value), cb_dump_name_attrs, dump);
2566              }
2567         }
2568         break;
2569       case EINA_SIMPLE_XML_DATA:
2570         {
2571            char *buf = malloc(length + 1);
2572            if (!buf) return EINA_FALSE;
2573            snprintf(buf, length + 1, "%s", value);
2574            if (dump->id == NAME_XML_NAME) dump->address = strdup(buf);
2575            free(buf);
2576         }
2577         break;
2578       default:
2579         break;
2580      }
2581
2582    return EINA_TRUE;
2583 }
2584
2585 static void
2586 _parse_kml(void *data)
2587 {
2588    Elm_Map_Route *r = (Elm_Map_Route*)data;
2589    if (!r && !r->ud.fname) return;
2590
2591    FILE *f;
2592    char **str;
2593    unsigned int ele, idx;
2594    double lon, lat;
2595    Evas_Object *path;
2596
2597    Route_Dump dump = {0, r->ud.fname, 0.0, NULL, NULL};
2598
2599    f = fopen(r->ud.fname, "rb");
2600    if (f)
2601      {
2602         long sz;
2603
2604         fseek(f, 0, SEEK_END);
2605         sz = ftell(f);
2606         if (sz > 0)
2607           {
2608              char *buf;
2609
2610              fseek(f, 0, SEEK_SET);
2611              buf = malloc(sz);
2612              if (buf)
2613                {
2614                   if (fread(buf, 1, sz, f))
2615                     {
2616                        eina_simple_xml_parse(buf, sz, EINA_TRUE, cb_route_dump, &dump);
2617                        free(buf);
2618                     }
2619                }
2620           }
2621         fclose(f);
2622
2623         if (dump.distance) r->info.distance = dump.distance;
2624         if (dump.description)
2625           {
2626              eina_stringshare_replace(&r->info.waypoints, dump.description);
2627              str = eina_str_split_full(dump.description, "\n", 0, &ele);
2628              r->info.waypoint_count = ele;
2629              for (idx = 0 ; idx < ele ; idx++)
2630                {
2631                   Route_Waypoint *wp = ELM_NEW(Route_Waypoint);
2632                   if (wp)
2633                     {
2634                        wp->wd = r->wd;
2635                        wp->point = eina_stringshare_add(str[idx]);
2636                        DBG("%s", str[idx]);
2637                        r->waypoint = eina_list_append(r->waypoint, wp);
2638                     }
2639                }
2640              if (str && str[0])
2641                {
2642                   free(str[0]);
2643                   free(str);
2644                }
2645           }
2646         else WRN("description is not found !");
2647
2648         if (dump.coordinates)
2649           {
2650              eina_stringshare_replace(&r->info.nodes, dump.coordinates);
2651              str = eina_str_split_full(dump.coordinates, "\n", 0, &ele);
2652              r->info.node_count = ele;
2653              for (idx = 0 ; idx < ele ; idx++)
2654                {
2655                   sscanf(str[idx], "%lf,%lf", &lon, &lat);
2656                   Route_Node *n = ELM_NEW(Route_Node);
2657                   if (n)
2658                     {
2659                        n->wd = r->wd;
2660                        n->pos.lon = lon;
2661                        n->pos.lat = lat;
2662                        n->idx = idx;
2663                        DBG("%lf:%lf", lon, lat);
2664                        n->pos.address = NULL;
2665                        r->nodes = eina_list_append(r->nodes, n);
2666
2667                        path = evas_object_line_add(evas_object_evas_get(r->wd->obj));
2668                        evas_object_smart_member_add(path, r->wd->pan_smart);
2669                        r->path = eina_list_append(r->path, path);
2670                     }
2671                }
2672              if (str && str[0])
2673                {
2674                   free(str[0]);
2675                   free(str);
2676                }
2677           }
2678      }
2679 }
2680
2681 static void
2682 _parse_name(void *data)
2683 {
2684    Elm_Map_Name *n = (Elm_Map_Name*)data;
2685    if (!n && !n->ud.fname) return;
2686
2687    FILE *f;
2688
2689    Name_Dump dump = {0, NULL, 0.0, 0.0};
2690
2691    f = fopen(n->ud.fname, "rb");
2692    if (f)
2693      {
2694         long sz;
2695
2696         fseek(f, 0, SEEK_END);
2697         sz = ftell(f);
2698         if (sz > 0)
2699           {
2700              char *buf;
2701
2702              fseek(f, 0, SEEK_SET);
2703              buf = malloc(sz);
2704              if (buf)
2705                {
2706                   if (fread(buf, 1, sz, f))
2707                     {
2708                        eina_simple_xml_parse(buf, sz, EINA_TRUE, cb_name_dump, &dump);
2709                        free(buf);
2710                     }
2711                }
2712           }
2713         fclose(f);
2714
2715         if (dump.address)
2716           {
2717              INF("[%lf : %lf] ADDRESS : %s", n->lon, n->lat, dump.address);
2718              n->address = strdup(dump.address);
2719           }
2720         n->lon = dump.lon;
2721         n->lat = dump.lat;
2722      }
2723 }
2724
2725 static Eina_Bool
2726 _route_complete_cb(void *data, int ev_type __UNUSED__, void *event)
2727 {
2728    Ecore_Con_Event_Url_Complete *ev = event;
2729    Elm_Map_Route *r = (Elm_Map_Route*)data;
2730    Widget_Data *wd = r->wd;
2731
2732    if ((!r) || (!ev)) return EINA_TRUE;
2733    Elm_Map_Route *rr = ecore_con_url_data_get(r->con_url);
2734    ecore_con_url_data_set(r->con_url, NULL);
2735    if (r!=rr) return EINA_TRUE;
2736
2737    if (r->ud.fd) fclose(r->ud.fd);
2738    _parse_kml(r);
2739
2740    if (wd->grids)
2741      {
2742         Evas_Coord ox, oy, ow, oh;
2743         evas_object_geometry_get(wd->obj, &ox, &oy, &ow, &oh);
2744         route_place(wd->obj, eina_list_data_get(wd->grids), wd->pan_x, wd->pan_y, ox, oy, ow, oh);
2745      }
2746    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
2747                            "elm,state,busy,stop", "elm");
2748    evas_object_smart_callback_call(wd->obj, SIG_ROUTE_LOADED, NULL);
2749    return EINA_TRUE;
2750 }
2751
2752 static Eina_Bool
2753 _name_complete_cb(void *data, int ev_type __UNUSED__, void *event)
2754 {
2755    Ecore_Con_Event_Url_Complete *ev = event;
2756    Elm_Map_Name *n = (Elm_Map_Name*)data;
2757    Widget_Data *wd = n->wd;
2758
2759    if ((!n) || (!ev)) return EINA_TRUE;
2760    Elm_Map_Name *nn = ecore_con_url_data_get(n->con_url);
2761    ecore_con_url_data_set(n->con_url, NULL);
2762    if (n!=nn) return EINA_TRUE;
2763
2764    if (n->ud.fd) fclose(n->ud.fd);
2765    _parse_name(n);
2766
2767    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
2768                            "elm,state,busy,stop", "elm");
2769    evas_object_smart_callback_call(wd->obj, SIG_NAME_LOADED, NULL);
2770    return EINA_TRUE;
2771 }
2772
2773 static Elm_Map_Name *
2774 _utils_convert_name(const Evas_Object *obj, int method, char *address, double lon, double lat)
2775 {
2776    Widget_Data *wd = elm_widget_data_get(obj);
2777    if (!wd) return NULL;
2778
2779    char buf[PATH_MAX];
2780    char *source;
2781    int fd;
2782    Elm_Map_Name *name = ELM_NEW(Elm_Map_Name);
2783    if (!name) return NULL;
2784
2785    snprintf(buf, sizeof(buf), DEST_NAME_XML_FILE);
2786    fd = mkstemp(buf);
2787    if (fd < 0)
2788      {
2789         free(name);
2790         return NULL;
2791      }
2792
2793    name->con_url = ecore_con_url_new(NULL);
2794    name->ud.fname = strdup(buf);
2795    INF("xml file : %s", name->ud.fname);
2796
2797    name->ud.fd = fdopen(fd, "w+");
2798    if ((!name->con_url) || (!name->ud.fd))
2799      {
2800         ecore_con_url_free(name->con_url);
2801         free(name);
2802         return NULL;
2803      }
2804
2805    name->wd = wd;
2806    name->handler = ecore_event_handler_add (ECORE_CON_EVENT_URL_COMPLETE, _name_complete_cb, name);
2807    name->method = method;
2808    if (method == ELM_MAP_NAME_METHOD_SEARCH) name->address = strdup(address);
2809    else if (method == ELM_MAP_NAME_METHOD_REVERSE) name->address = NULL;
2810    name->lon = lon;
2811    name->lat = lat;
2812
2813    source = map_sources_tab[wd->source].name_url_cb(wd->obj, method, address, lon, lat);
2814    INF("name url = %s", source);
2815
2816    wd->names = eina_list_append(wd->names, name);
2817    ecore_con_url_url_set(name->con_url, source);
2818    ecore_con_url_fd_set(name->con_url, fileno(name->ud.fd));
2819    ecore_con_url_data_set(name->con_url, name);
2820
2821    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
2822                            "elm,state,busy,start", "elm");
2823    evas_object_smart_callback_call(wd->obj, SIG_NAME_LOAD, NULL);
2824    ecore_con_url_get(name->con_url);
2825    if (source) free(source);
2826
2827    return name;
2828
2829 }
2830
2831 static int idnum = 1;
2832
2833 /**
2834  * Add a new Map object
2835  *
2836  * @param parent The parent object
2837  * @return The new object or NULL if it cannot be created
2838  *
2839  * @ingroup Map
2840  */
2841 EAPI Evas_Object *
2842 elm_map_add(Evas_Object *parent)
2843 {
2844    Evas *e;
2845    Widget_Data *wd;
2846    Evas_Coord minw, minh;
2847    Evas_Object *obj;
2848    static Evas_Smart *smart = NULL;
2849    Eina_Bool bounce = _elm_config->thumbscroll_bounce_enable;
2850
2851    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
2852
2853    ELM_SET_WIDTYPE(widtype, "map");
2854    elm_widget_type_set(obj, "map");
2855    elm_widget_sub_object_add(parent, obj);
2856    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
2857    elm_widget_data_set(obj, wd);
2858    elm_widget_del_hook_set(obj, _del_hook);
2859    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
2860    elm_widget_theme_hook_set(obj, _theme_hook);
2861    elm_widget_can_focus_set(obj, EINA_TRUE);
2862    elm_widget_event_hook_set(obj, _event_hook);
2863
2864    wd->scr = elm_smart_scroller_add(e);
2865    elm_smart_scroller_widget_set(wd->scr, obj);
2866    elm_smart_scroller_object_theme_set(obj, wd->scr, "map", "base", "default");
2867    evas_object_smart_callback_add(wd->scr, "scroll", _scr, obj);
2868    evas_object_smart_callback_add(wd->scr, "drag", _scr, obj);
2869    elm_widget_resize_object_set(obj, wd->scr);
2870
2871    evas_object_smart_callback_add(wd->scr, "animate,start", _scr_anim_start, obj);
2872    evas_object_smart_callback_add(wd->scr, "animate,stop", _scr_anim_stop, obj);
2873    evas_object_smart_callback_add(wd->scr, "drag,start", _scr_drag_start, obj);
2874    evas_object_smart_callback_add(wd->scr, "drag,stop", _scr_drag_stop, obj);
2875    evas_object_smart_callback_add(wd->scr, "scroll", _scr_scroll, obj);
2876
2877    elm_smart_scroller_bounce_allow_set(wd->scr, bounce, bounce);
2878
2879    wd->api = module(obj);
2880    if ((wd->api) && (wd->api->obj_hook)) wd->api->obj_hook(obj);
2881
2882    wd->obj = obj;
2883
2884    wd->markers_max_num = 30;
2885    wd->source = ELM_MAP_SOURCE_MAPNIK;
2886    wd->pinch.level = 1.0;
2887
2888    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
2889    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
2890    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
2891    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
2892
2893    if (!smart)
2894      {
2895         static Evas_Smart_Class sc;
2896
2897         evas_object_smart_clipped_smart_set(&_pan_sc);
2898         sc = _pan_sc;
2899         sc.name = "elm_map_pan";
2900         sc.version = EVAS_SMART_CLASS_VERSION;
2901         sc.add = _pan_add;
2902         sc.del = _pan_del;
2903         sc.resize = _pan_resize;
2904         sc.move = _pan_move;
2905         sc.calculate = _pan_calculate;
2906         smart = evas_smart_class_new(&sc);
2907      }
2908    if (smart)
2909      {
2910         wd->pan_smart = evas_object_smart_add(e, smart);
2911         wd->pan = evas_object_smart_data_get(wd->pan_smart);
2912         wd->pan->wd = wd;
2913      }
2914
2915    elm_smart_scroller_extern_pan_set(wd->scr, wd->pan_smart,
2916                                      _pan_set, _pan_get, _pan_max_get,
2917                                      _pan_min_get, _pan_child_size_get);
2918
2919    wd->rect = evas_object_rectangle_add(e);
2920    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_RESIZE,
2921                                   _rect_resize_cb, obj);
2922    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_DOWN,
2923                                   _mouse_down, obj);
2924    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_MOVE,
2925                                   _mouse_move, obj);
2926    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_UP,
2927                                   _mouse_up, obj);
2928    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MULTI_DOWN,
2929                                   _mouse_multi_down, obj);
2930    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MULTI_MOVE,
2931                                   _mouse_multi_move, obj);
2932    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MULTI_UP,
2933                                   _mouse_multi_up, obj);
2934
2935    evas_object_smart_member_add(wd->rect, wd->pan_smart);
2936    elm_widget_sub_object_add(obj, wd->rect);
2937    evas_object_show(wd->rect);
2938    evas_object_color_set(wd->rect, 0, 0, 0, 0);
2939
2940    wd->zoom = -1;
2941    wd->mode = ELM_MAP_ZOOM_MODE_MANUAL;
2942    wd->id = ((int)getpid() << 16) | idnum;
2943    idnum++;
2944
2945    wd->tsize = 256;
2946
2947    edje_object_size_min_calc(elm_smart_scroller_edje_object_get(wd->scr),
2948                              &minw, &minh);
2949    evas_object_size_hint_min_set(obj, minw, minh);
2950
2951    wd->paused = EINA_TRUE;
2952    elm_map_zoom_set(obj, 0);
2953    wd->paused = EINA_FALSE;
2954
2955    _sizing_eval(obj);
2956
2957    wd->calc_job = ecore_job_add(_calc_job, wd);
2958
2959    wd->sep_maps_markers = evas_object_rectangle_add(evas_object_evas_get(obj));
2960    evas_object_smart_member_add(wd->sep_maps_markers, wd->pan_smart);
2961
2962    // TODO: convert Elementary to subclassing of Evas_Smart_Class
2963    // TODO: and save some bytes, making descriptions per-class and not instance!
2964    evas_object_smart_callbacks_descriptions_set(obj, _signals);
2965
2966    if (!ecore_file_download_protocol_available("http://"))
2967      {
2968         ERR("Ecore must be built with curl support for the map widget!");
2969      }
2970    return obj;
2971 }
2972
2973 /**
2974  * Set the zoom level of the map
2975  *
2976  * This sets the zoom level. 0 is the world map and 18 is the maximum zoom.
2977  *
2978  * @param obj The map object
2979  * @param zoom The zoom level to set
2980  *
2981  * @ingroup Map
2982  */
2983 EAPI void
2984 elm_map_zoom_set(Evas_Object *obj, int zoom)
2985 {
2986    ELM_CHECK_WIDTYPE(obj, widtype);
2987    Widget_Data *wd = elm_widget_data_get(obj);
2988    Eina_List *l, *lr;
2989    Grid *g, *g_zoom = NULL;
2990    Evas_Coord rx, ry, rw, rh;
2991    Evas_Object *p;
2992    Elm_Map_Route *r;
2993    int z;
2994    int zoom_changed = 0, started = 0;
2995
2996    if ((!wd) || (wd->zoom_animator)) return;
2997    if (zoom < 0 ) zoom = 0;
2998    if (zoom > map_sources_tab[wd->source].zoom_max)
2999      zoom = map_sources_tab[wd->source].zoom_max;
3000    if (zoom < map_sources_tab[wd->source].zoom_min)
3001      zoom = map_sources_tab[wd->source].zoom_min;
3002
3003    if ((wd->zoom - zoom) > 0) wd->zoom_method = ZOOM_METHOD_OUT;
3004    else if ((wd->zoom - zoom) < 0) wd->zoom_method = ZOOM_METHOD_IN;
3005    else wd->zoom_method = ZOOM_METHOD_NONE;
3006    wd->zoom = zoom;
3007    wd->size.ow = wd->size.w;
3008    wd->size.oh = wd->size.h;
3009    elm_smart_scroller_child_pos_get(wd->scr, &rx, &ry);
3010    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3011
3012    EINA_LIST_FOREACH(wd->route, lr, r)
3013      {
3014         if (r)
3015           {
3016              EINA_LIST_FOREACH(r->path, l, p)
3017                {
3018                   evas_object_hide(p);
3019                }
3020           }
3021      }
3022
3023    if (wd->mode == ELM_MAP_ZOOM_MODE_MANUAL)
3024      {
3025         wd->size.nw = pow(2.0, wd->zoom) * wd->tsize;
3026         wd->size.nh = pow(2.0, wd->zoom) * wd->tsize;
3027      }
3028    else if (wd->mode == ELM_MAP_ZOOM_MODE_AUTO_FIT)
3029      {
3030         int p2w, p2h;
3031         int cumulw, cumulh;
3032
3033         cumulw = wd->tsize;
3034         p2w = 0;
3035         while (cumulw <= rw)
3036           {
3037              p2w++;
3038              cumulw *= 2;
3039           }
3040         p2w--;
3041
3042         cumulh = wd->tsize;
3043         p2h = 0;
3044         while (cumulh <= rh)
3045           {
3046              p2h++;
3047              cumulh *= 2;
3048           }
3049         p2h--;
3050
3051         if (p2w < p2h)
3052           z = p2w;
3053         else
3054           z = p2h;
3055
3056         wd->zoom = z;
3057         wd->size.nw = pow(2.0, wd->zoom) * wd->tsize;
3058         wd->size.nh = pow(2.0, wd->zoom) * wd->tsize;
3059      }
3060    else if (wd->mode == ELM_MAP_ZOOM_MODE_AUTO_FILL)
3061      {
3062         int p2w, p2h;
3063         int cumulw, cumulh;
3064
3065         cumulw = wd->tsize;
3066         p2w = 0;
3067         while (cumulw <= rw)
3068           {
3069              p2w++;
3070              cumulw *= 2;
3071           }
3072         p2w--;
3073
3074         cumulh = wd->tsize;
3075         p2h = 0;
3076         while (cumulh <= rh)
3077           {
3078              p2h++;
3079              cumulh *= 2;
3080           }
3081         p2h--;
3082
3083         if (p2w > p2h)
3084           z = p2w;
3085         else
3086           z = p2h;
3087
3088         wd->zoom = z;
3089         wd->size.nw = pow(2.0, wd->zoom) * wd->tsize;
3090         wd->size.nh = pow(2.0, wd->zoom) * wd->tsize;
3091      }
3092
3093    EINA_LIST_FOREACH(wd->grids, l, g)
3094      {
3095         if (g->zoom == wd->zoom)
3096           {
3097              _grid_raise(g);
3098              goto done;
3099           }
3100      }
3101    g = grid_create(obj);
3102    if (g)
3103      {
3104         if (eina_list_count(wd->grids) > 1)
3105           {
3106              g_zoom = eina_list_last(wd->grids)->data;
3107              wd->grids = eina_list_remove(wd->grids, g_zoom);
3108              grid_clear(obj, g_zoom);
3109              free(g_zoom);
3110           }
3111         wd->grids = eina_list_prepend(wd->grids, g);
3112      }
3113    else
3114      {
3115         EINA_LIST_FREE(wd->grids, g)
3116           {
3117              grid_clear(obj, g);
3118              free(g);
3119           }
3120      }
3121 done:
3122
3123    wd->t_start = ecore_loop_time_get();
3124    wd->t_end = wd->t_start + _elm_config->zoom_friction;
3125    if (wd->paused)
3126      {
3127         zoom_do(obj);
3128      }
3129    else
3130      {
3131         if (!wd->zoom_animator)
3132           {
3133              wd->zoom_animator = ecore_animator_add(_zoom_anim, obj);
3134              wd->nosmooth++;
3135              if (wd->nosmooth == 1) _smooth_update(obj);
3136              started = 1;
3137           }
3138      }
3139    if (wd->zoom_animator)
3140      {
3141         if (!_zoom_anim(obj))
3142           {
3143              ecore_animator_del(wd->zoom_animator);
3144              wd->zoom_animator = NULL;
3145           }
3146      }
3147    if (wd->calc_job) ecore_job_del(wd->calc_job);
3148    wd->calc_job = ecore_job_add(_calc_job, wd);
3149    if (!wd->paused)
3150      {
3151         if (started)
3152           evas_object_smart_callback_call(obj, SIG_ZOOM_START, NULL);
3153         if (!wd->zoom_animator)
3154           evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
3155      }
3156
3157    if (zoom_changed)
3158      evas_object_smart_callback_call(obj, SIG_ZOOM_CHANGE, NULL);
3159 }
3160
3161 /**
3162  * Get the zoom level of the map
3163  *
3164  * This returns the current zoom level of the map object. Note that if
3165  * you set the fill mode to other than ELM_MAP_ZOOM_MODE_MANUAL
3166  * (which is the default), the zoom level may be changed at any time by the
3167  * map object itself to account for map size and map viewpoer size
3168  *
3169  * @param obj The map object
3170  * @return The current zoom level
3171  *
3172  * @ingroup Map
3173  */
3174 EAPI double
3175 elm_map_zoom_get(const Evas_Object *obj)
3176 {
3177    ELM_CHECK_WIDTYPE(obj, widtype) 1.0;
3178    Widget_Data *wd = elm_widget_data_get(obj);
3179    if (!wd) return 1.0;
3180    return wd->zoom;
3181 }
3182
3183 /**
3184  * Set the zoom mode
3185  *
3186  * This sets the zoom mode to manual or one of several automatic levels.
3187  * Manual (ELM_MAP_ZOOM_MODE_MANUAL) means that zoom is set manually by
3188  * elm_map_zoom_set() and will stay at that level until changed by code
3189  * or until zoom mode is changed. This is the default mode.
3190  * The Automatic modes will allow the map object to automatically
3191  * adjust zoom mode based on properties. ELM_MAP_ZOOM_MODE_AUTO_FIT) will
3192  * adjust zoom so the map fits inside the scroll frame with no pixels
3193  * outside this area. ELM_MAP_ZOOM_MODE_AUTO_FILL will be similar but
3194  * 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.
3195  *
3196  * @param obj The map object
3197  * @param mode The desired mode
3198  *
3199  * @ingroup Map
3200  */
3201 EAPI void
3202 elm_map_zoom_mode_set(Evas_Object *obj, Elm_Map_Zoom_Mode mode)
3203 {
3204    ELM_CHECK_WIDTYPE(obj, widtype);
3205    Widget_Data *wd = elm_widget_data_get(obj);
3206    if (!wd) return;
3207    if (wd->mode == mode) return;
3208    wd->mode = mode;
3209      {
3210         double tz = wd->zoom;
3211         wd->zoom = 0.0;
3212         elm_map_zoom_set(wd->obj, tz);
3213      }
3214 }
3215
3216 /**
3217  * Get the zoom mode
3218  *
3219  * This gets the current zoom mode of the map object
3220  *
3221  * @param obj The map object
3222  * @return The current zoom mode
3223  *
3224  * @ingroup Map
3225  */
3226 EAPI Elm_Map_Zoom_Mode
3227 elm_map_zoom_mode_get(const Evas_Object *obj)
3228 {
3229    ELM_CHECK_WIDTYPE(obj, widtype) ELM_MAP_ZOOM_MODE_MANUAL;
3230    Widget_Data *wd = elm_widget_data_get(obj);
3231    if (!wd) return ELM_MAP_ZOOM_MODE_MANUAL;
3232    return wd->mode;
3233 }
3234
3235 /**
3236  * Centers the map at @p lon @p lat using an animation to scroll.
3237  *
3238  * @param obj The map object
3239  * @param lon Longitude to center at
3240  * @param lon Latitude to center at
3241  *
3242  * @ingroup Map
3243  */
3244 EAPI void
3245 elm_map_geo_region_bring_in(Evas_Object *obj, double lon, double lat)
3246 {
3247    ELM_CHECK_WIDTYPE(obj, widtype);
3248    Widget_Data *wd = elm_widget_data_get(obj);
3249    int rx, ry, rw, rh;
3250
3251    if (!wd) return;
3252    elm_map_utils_convert_geo_into_coord(obj, lon, lat, wd->size.w, &rx, &ry);
3253    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3254
3255    rx = rx - rw / 2;
3256    ry = ry - rh / 2;
3257
3258    if (wd->zoom_animator)
3259      {
3260         wd->nosmooth--;
3261         if (!wd->nosmooth) _smooth_update(obj);
3262         ecore_animator_del(wd->zoom_animator);
3263         wd->zoom_animator = NULL;
3264         zoom_do(obj);
3265         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
3266      }
3267    elm_smart_scroller_region_bring_in(wd->scr, rx, ry, rw, rh);
3268
3269    wd->center_on.enabled = EINA_TRUE;
3270    wd->center_on.lon = lon;
3271    wd->center_on.lat = lat;
3272 }
3273
3274 /**
3275  * Move the map to the current coordinates.
3276  *
3277  * This move the map to the current coordinates. The map will be centered on these coordinates.
3278  *
3279  * @param obj The map object
3280  * @param lat The latitude.
3281  * @param lon The longitude.
3282  *
3283  * @ingroup Map
3284  */
3285 EAPI void
3286 elm_map_geo_region_show(Evas_Object *obj, double lon, double lat)
3287 {
3288    ELM_CHECK_WIDTYPE(obj, widtype);
3289    Widget_Data *wd = elm_widget_data_get(obj);
3290    int rx, ry, rw, rh;
3291
3292    if (!wd) return;
3293    elm_map_utils_convert_geo_into_coord(obj, lon, lat, wd->size.w, &rx, &ry);
3294    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3295
3296    rx = rx - rw / 2;
3297    ry = ry - rh / 2;
3298
3299    if (wd->zoom_animator)
3300      {
3301         wd->nosmooth--;
3302         ecore_animator_del(wd->zoom_animator);
3303         wd->zoom_animator = NULL;
3304         zoom_do(obj);
3305         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
3306      }
3307    elm_smart_scroller_child_region_show(wd->scr, rx, ry, rw, rh);
3308
3309    wd->center_on.enabled = EINA_TRUE;
3310    wd->center_on.lon = lon;
3311    wd->center_on.lat = lat;
3312 }
3313
3314 /**
3315  * Get the current coordinates of the map.
3316  *
3317  * This gets the current coordinates of the map object.
3318  *
3319  * @param obj The map object
3320  * @param lat The latitude.
3321  * @param lon The longitude.
3322  *
3323  * @ingroup Map
3324  */
3325 EAPI void
3326 elm_map_geo_region_get(const Evas_Object *obj, double *lon, double *lat)
3327 {
3328    ELM_CHECK_WIDTYPE(obj, widtype);
3329    Widget_Data *wd = elm_widget_data_get(obj);
3330    Evas_Coord sx, sy, sw, sh;
3331
3332    if (!wd) return;
3333    elm_smart_scroller_child_pos_get(wd->scr, &sx, &sy);
3334    elm_smart_scroller_child_viewport_size_get(wd->scr, &sw, &sh);
3335    sx += sw / 2;
3336    sy += sh / 2;
3337
3338    elm_map_utils_convert_coord_into_geo(obj, sx, sy, wd->size.w, lon, lat);
3339 }
3340
3341 /**
3342  * Set the paused state for map
3343  *
3344  * This sets the paused state to on (1) or off (0) for map. The default
3345  * is off. This will stop zooming using animation change zoom levels and
3346  * change instantly. This will stop any existing animations that are running.
3347  *
3348  * @param obj The map object
3349  * @param paused The pause state to set
3350  *
3351  * @ingroup Map
3352  */
3353 EAPI void
3354 elm_map_paused_set(Evas_Object *obj, Eina_Bool paused)
3355 {
3356    ELM_CHECK_WIDTYPE(obj, widtype);
3357    Widget_Data *wd = elm_widget_data_get(obj);
3358    if (!wd) return;
3359    if (wd->paused == !!paused) return;
3360    wd->paused = paused;
3361    if (wd->paused)
3362      {
3363         if (wd->zoom_animator)
3364           {
3365              ecore_animator_del(wd->zoom_animator);
3366              wd->zoom_animator = NULL;
3367              zoom_do(obj);
3368              evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
3369           }
3370      }
3371 }
3372
3373 /**
3374  * Set the paused state for the markers
3375  *
3376  * This sets the paused state to on (1) or off (0) for the markers. The default
3377  * is off. This will stop displaying the markers during change zoom levels. Set
3378  * to on if you have a large number of markers.
3379  *
3380  * @param obj The map object
3381  * @param paused The pause state to set
3382  *
3383  * @ingroup Map
3384  */
3385 EAPI void
3386 elm_map_paused_markers_set(Evas_Object *obj, Eina_Bool paused)
3387 {
3388    ELM_CHECK_WIDTYPE(obj, widtype);
3389    Widget_Data *wd = elm_widget_data_get(obj);
3390    if (!wd) return;
3391    if (wd->paused_markers == !!paused) return;
3392    wd->paused_markers = paused;
3393 }
3394
3395 /**
3396  * Get the paused state for map
3397  *
3398  * This gets the current paused state for the map object.
3399  *
3400  * @param obj The map object
3401  * @return The current paused state
3402  *
3403  * @ingroup Map
3404  */
3405 EAPI Eina_Bool
3406 elm_map_paused_get(const Evas_Object *obj)
3407 {
3408    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
3409    Widget_Data *wd = elm_widget_data_get(obj);
3410    if (!wd) return EINA_FALSE;
3411    return wd->paused;
3412 }
3413
3414 /**
3415  * Get the paused state for the markers
3416  *
3417  * This gets the current paused state for the markers object.
3418  *
3419  * @param obj The map object
3420  * @return The current paused state
3421  *
3422  * @ingroup Map
3423  */
3424 EAPI Eina_Bool
3425 elm_map_paused_markers_get(const Evas_Object *obj)
3426 {
3427    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
3428    Widget_Data *wd = elm_widget_data_get(obj);
3429    if (!wd) return EINA_FALSE;
3430    return wd->paused_markers;
3431 }
3432
3433 /**
3434  * Get the information of downloading status
3435  *
3436  * This gets the current downloading status for the map object.
3437  *
3438  * @param obj The map object
3439  * @param try_num the number of download trying map
3440  * @param finish_num the number of downloaded map
3441  *
3442  * @ingroup Map
3443  */
3444
3445 EAPI void
3446 elm_map_utils_downloading_status_get(const Evas_Object *obj, int *try_num, int *finish_num)
3447 {
3448    ELM_CHECK_WIDTYPE(obj, widtype);
3449    Widget_Data *wd = elm_widget_data_get(obj);
3450    if (!wd) return;
3451
3452    if (try_num)
3453      {
3454         *try_num = wd->try_num;
3455      }
3456
3457    if (finish_num)
3458      {
3459         *finish_num = wd->finish_num;
3460      }
3461 }
3462 /**
3463  * Convert a pixel coordinate (x,y) into a geographic coordinate (longitude, latitude).
3464  *
3465  * @param obj The map object
3466  * @param x the coordinate
3467  * @param y the coordinate
3468  * @param size the size in pixels of the map. The map is a square and generally his size is : pow(2.0, zoom)*256.
3469  * @param lon the longitude correspond to x
3470  * @param lat the latitude correspond to y
3471  *
3472  * @ingroup Map
3473  */
3474 EAPI void
3475 elm_map_utils_convert_coord_into_geo(const Evas_Object *obj, int x, int y, int size, double *lon, double *lat)
3476 {
3477    Widget_Data *wd = elm_widget_data_get(obj);
3478    int zoom = floor(log(size/256) / log(2));
3479
3480    if (elm_map_source_get(obj) == ELM_MAP_SOURCE_MODULE)
3481      if ((wd->api) && (wd->api->obj_convert_coord_into_geo))
3482        if (wd->api->obj_convert_coord_into_geo(obj, zoom, x, y, size, lon, lat)) return;
3483
3484    if (lon)
3485      {
3486         *lon = x / (double)size * 360.0 - 180;
3487      }
3488    if (lat)
3489      {
3490         double n = ELM_PI - 2.0 * ELM_PI * y / size;
3491         *lat = 180.0 / ELM_PI * atan(0.5 * (exp(n) - exp(-n)));
3492      }
3493 }
3494
3495 /**
3496  * Convert a geographic coordinate (longitude, latitude) into a pixel coordinate (x, y).
3497  *
3498  * @param obj The map object
3499  * @param lon the longitude
3500  * @param lat the latitude
3501  * @param size the size in pixels of the map. The map is a square and generally his size is : pow(2.0, zoom)*256.
3502  * @param x the coordinate correspond to the longitude
3503  * @param y the coordinate correspond to the latitude
3504  *
3505  * @ingroup Map
3506  */
3507 EAPI void
3508 elm_map_utils_convert_geo_into_coord(const Evas_Object *obj, double lon, double lat, int size, int *x, int *y)
3509 {
3510    Widget_Data *wd = elm_widget_data_get(obj);
3511    int zoom = floor(log(size/256) / log(2));
3512
3513    if (elm_map_source_get(obj) == ELM_MAP_SOURCE_MODULE)
3514      if ((wd->api) && (wd->api->obj_convert_geo_into_coord))
3515        if (wd->api->obj_convert_geo_into_coord(obj, zoom, lon, lat, size, x, y)) return;
3516
3517    if (x)
3518      *x = floor((lon + 180.0) / 360.0 * size);
3519    if (y)
3520      *y = floor((1.0 - log( tan(lat * ELM_PI/180.0) + 1.0 / cos(lat * ELM_PI/180.0)) / ELM_PI) / 2.0 * size);
3521 }
3522
3523 /**
3524  * Convert a geographic coordinate (longitude, latitude) into a name (address).
3525  *
3526  * @param obj The map object
3527  * @param lon the longitude
3528  * @param lat the latitude
3529  *
3530  * @return name the address
3531  *
3532  * @ingroup Map
3533  */
3534 EAPI Elm_Map_Name *
3535 elm_map_utils_convert_coord_into_name(const Evas_Object *obj, double lon, double lat)
3536 {
3537    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3538    return _utils_convert_name(obj, ELM_MAP_NAME_METHOD_REVERSE, NULL, lon, lat);
3539 }
3540
3541 /**
3542  * Convert a name (address) into a geographic coordinate (longitude, latitude).
3543  *
3544  * @param obj The map object
3545  * @param name the address
3546  * @param lat the latitude correspond to y
3547  *
3548  * @return name the address
3549  *
3550  * @ingroup Map
3551  */
3552 EAPI Elm_Map_Name *
3553 elm_map_utils_convert_name_into_coord(const Evas_Object *obj, char *address)
3554 {
3555    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3556    if (!address) return NULL;
3557    return _utils_convert_name(obj, ELM_MAP_NAME_METHOD_SEARCH, address, 0.0, 0.0);
3558 }
3559
3560 /**
3561  * Convert a pixel coordinate into a roated pixcel coordinate.
3562  *
3563  * @param obj The map object
3564  * @param x x to rotate.
3565  * @param y y to rotate.
3566  * @param cx rotation's center horizontal position.
3567  * @param cy rotation's center vertical position.
3568  * @param degree amount of degrees from 0.0 to 360.0 to rotate arount Z axis.
3569  * @param xx rotated x.
3570  * @param yy rotated y.
3571  *
3572  * @ingroup Map
3573  */
3574 EAPI void
3575 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)
3576 {
3577    if ((!xx) || (!yy)) return;
3578
3579    double r = (degree * M_PI) / 180.0;
3580    double tx, ty, ttx, tty;
3581
3582    tx = x - cx;
3583    ty = y - cy;
3584
3585    ttx = tx * cos(r);
3586    tty = tx * sin(r);
3587    tx = ttx + (ty * cos(r + M_PI_2));
3588    ty = tty + (ty * sin(r + M_PI_2));
3589
3590    *xx = tx + cx;
3591    *yy = ty + cy;
3592 }
3593
3594 /**
3595  * Add a marker on the map
3596  *
3597  * @param obj The map object
3598  * @param lon the longitude
3599  * @param lat the latitude
3600  * @param clas the class to use
3601  * @param clas_group the class group
3602  * @param data the data passed to the callbacks
3603  *
3604  * @return The marker object
3605  *
3606  * @ingroup Map
3607  */
3608 EAPI Elm_Map_Marker *
3609 elm_map_marker_add(Evas_Object *obj, double lon, double lat, Elm_Map_Marker_Class *clas, Elm_Map_Group_Class *clas_group, void *data)
3610 {
3611    int i, j;
3612    Eina_List *l;
3613    Marker_Group *group;
3614    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3615    Widget_Data *wd = elm_widget_data_get(obj);
3616    int mpi, mpj;
3617    int tabi[9];
3618    int tabj[9];
3619    const char *s;
3620    const char *style;
3621    Evas_Object *o;
3622
3623    if (!wd) return NULL;
3624    EINA_SAFETY_ON_NULL_RETURN_VAL(clas_group, NULL);
3625    EINA_SAFETY_ON_NULL_RETURN_VAL(clas, NULL);
3626
3627    Elm_Map_Marker *marker = ELM_NEW(Elm_Map_Marker);
3628
3629    marker->wd = wd;
3630    marker->clas = clas;
3631    marker->clas_group = clas_group;
3632    marker->longitude = lon;
3633    marker->latitude = lat;
3634    marker->data = data;
3635
3636    tabi[1] = tabi[4] = tabi[6] = -1;
3637    tabi[2] = tabi[0] = tabi[7] = 0;
3638    tabi[3] = tabi[5] = tabi[8] = 1;
3639
3640    tabj[1] = tabj[2] = tabj[3] = -1;
3641    tabj[4] = tabj[0] = tabj[5] = 0;
3642    tabj[6] = tabj[7] = tabj[8] = 1;
3643
3644    if (!clas_group->priv.set)
3645      {
3646         style = "radio";
3647         if (marker->clas_group && marker->clas_group->style)
3648           style = marker->clas_group->style;
3649
3650         o = edje_object_add(evas_object_evas_get(obj));
3651         _elm_theme_object_set(obj, o, "map/marker", style, elm_widget_style_get(obj));
3652         s = edje_object_data_get(o, "size_w");
3653         if (s) clas_group->priv.edje_w = atoi(s);
3654         else clas_group->priv.edje_w = 0;
3655         s = edje_object_data_get(o, "size_h");
3656         if (s) clas_group->priv.edje_h = atoi(s);
3657         else clas_group->priv.edje_h = 0;
3658         s = edje_object_data_get(o, "size_max_w");
3659         if (s) clas_group->priv.edje_max_w = atoi(s);
3660         else clas_group->priv.edje_max_w = 0;
3661         s = edje_object_data_get(o, "size_max_h");
3662         if (s) clas_group->priv.edje_max_h = atoi(s);
3663         else clas_group->priv.edje_max_h = 0;
3664         evas_object_del(o);
3665
3666         clas_group->priv.set = EINA_TRUE;
3667      }
3668
3669    if (!clas->priv.set)
3670      {
3671         style = "radio";
3672         if (marker->clas && marker->clas->style)
3673           style = marker->clas->style;
3674
3675         o = edje_object_add(evas_object_evas_get(obj));
3676         _elm_theme_object_set(obj, o, "map/marker", style, elm_widget_style_get(obj));
3677         s = edje_object_data_get(o, "size_w");
3678         if (s) clas->priv.edje_w = atoi(s);
3679         else clas->priv.edje_w = 0;
3680         s = edje_object_data_get(o, "size_h");
3681         if (s) clas->priv.edje_h = atoi(s);
3682         else clas->priv.edje_h = 0;
3683         evas_object_del(o);
3684
3685         clas->priv.set = EINA_TRUE;
3686      }
3687
3688    for (i = clas_group->zoom_displayed; i <= ZOOM_MAX; i++)
3689      {
3690         elm_map_utils_convert_geo_into_coord(obj, lon, lat, pow(2.0, i)*wd->tsize,
3691                                              &(marker->x[i]), &(marker->y[i]));
3692
3693         //search in the matrixsparse the region where the marker will be
3694         mpi = marker->x[i] / wd->tsize;
3695         mpj = marker->y[i] / wd->tsize;
3696
3697         if (!wd->markers[i])
3698           {
3699              int size =  pow(2.0, i);
3700              wd->markers[i] = eina_matrixsparse_new(size, size, NULL, NULL);
3701           }
3702
3703         group = NULL;
3704         if (i <= clas_group->zoom_grouped)
3705           {
3706              for (j = 0, group = NULL; j < 9 && !group; j++)
3707                {
3708                   EINA_LIST_FOREACH(eina_matrixsparse_data_idx_get(wd->markers[i], mpj + tabj[j], mpi + tabi[j]),
3709                                     l, group)
3710                     {
3711                        if (group->clas == marker->clas_group
3712                            && ELM_RECTS_INTERSECT(marker->x[i]-clas->priv.edje_w/4,
3713                                                   marker->y[i]-clas->priv.edje_h/4, clas->priv.edje_w, clas->priv.edje_h,
3714                                                   group->x-group->w/4, group->y-group->h/4, group->w, group->h))
3715                          {
3716                             group->markers = eina_list_append(group->markers, marker);
3717                             group->update_nbelems = EINA_TRUE;
3718                             group->update_resize = EINA_TRUE;
3719
3720                             group->sum_x += marker->x[i];
3721                             group->sum_y += marker->y[i];
3722                             group->x = group->sum_x / eina_list_count(group->markers);
3723                             group->y = group->sum_y / eina_list_count(group->markers);
3724
3725                             group->w = group->clas->priv.edje_w + group->clas->priv.edje_w/8.
3726                                * eina_list_count(group->markers);
3727                             group->h = group->clas->priv.edje_h + group->clas->priv.edje_h/8.
3728                                * eina_list_count(group->markers);
3729                             if (group->w > group->clas->priv.edje_max_w) group->w = group->clas->priv.edje_max_w;
3730                             if (group->h > group->clas->priv.edje_max_h) group->h = group->clas->priv.edje_max_h;
3731
3732                             if (group->obj && eina_list_count(group->markers) == 2)
3733                               {
3734                                  _group_object_free(group);
3735                                  _group_object_create(group);
3736                               }
3737                             if (group->bubble)
3738                               _group_bubble_content_update(group);
3739
3740                             break;
3741                          }
3742                     }
3743                }
3744           }
3745         if (!group)
3746           {
3747              group = calloc(1, sizeof(Marker_Group));
3748              group->wd = wd;
3749              group->sum_x = marker->x[i];
3750              group->sum_y = marker->y[i];
3751              group->x = marker->x[i];
3752              group->y = marker->y[i];
3753              group->w = clas_group->priv.edje_w;
3754              group->h = clas_group->priv.edje_h;
3755              group->clas = clas_group;
3756
3757              group->markers = eina_list_append(group->markers, marker);
3758              group->update_nbelems = EINA_TRUE;
3759              group->update_resize = EINA_TRUE;
3760
3761              eina_matrixsparse_cell_idx_get(wd->markers[i], mpj, mpi, &(group->cell));
3762
3763              if (!group->cell)
3764                {
3765                   l = eina_list_append(NULL, group);
3766                   eina_matrixsparse_data_idx_set(wd->markers[i], mpj, mpi, l);
3767                   eina_matrixsparse_cell_idx_get(wd->markers[i], mpj, mpi, &(group->cell));
3768                }
3769              else
3770                {
3771                   l = eina_matrixsparse_cell_data_get(group->cell);
3772                   l = eina_list_append(l, group);
3773                   eina_matrixsparse_cell_data_set(group->cell, l);
3774                }
3775           }
3776         marker->groups[i] = group;
3777      }
3778
3779    if (wd->grids)
3780      {
3781         Evas_Coord ox, oy, ow, oh;
3782         evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
3783         marker_place(obj, eina_list_data_get(wd->grids), wd->pan_x, wd->pan_y, ox, oy, ow, oh);
3784      }
3785
3786    return marker;
3787 }
3788
3789 /**
3790  * Remove a marker from the map
3791  *
3792  * @param marker The marker to remove
3793  *
3794  * @ingroup Map
3795  */
3796 EAPI void
3797 elm_map_marker_remove(Elm_Map_Marker *marker)
3798 {
3799    int i;
3800    Eina_List *groups;
3801    Widget_Data *wd;
3802
3803    EINA_SAFETY_ON_NULL_RETURN(marker);
3804    wd = marker->wd;
3805    if (!wd) return;
3806    for (i = marker->clas_group->zoom_displayed; i <= ZOOM_MAX; i++)
3807      {
3808         marker->groups[i]->markers = eina_list_remove(marker->groups[i]->markers, marker);
3809         if (!eina_list_count(marker->groups[i]->markers))
3810           {
3811              groups = eina_matrixsparse_cell_data_get(marker->groups[i]->cell);
3812              groups = eina_list_remove(groups, marker->groups[i]);
3813              eina_matrixsparse_cell_data_set(marker->groups[i]->cell, groups);
3814
3815              _group_object_free(marker->groups[i]);
3816              _group_bubble_free(marker->groups[i]);
3817              free(marker->groups[i]);
3818           }
3819         else
3820           {
3821              marker->groups[i]->sum_x -= marker->x[i];
3822              marker->groups[i]->sum_y -= marker->y[i];
3823
3824              marker->groups[i]->x = marker->groups[i]->sum_x / eina_list_count(marker->groups[i]->markers);
3825              marker->groups[i]->y = marker->groups[i]->sum_y / eina_list_count(marker->groups[i]->markers);
3826
3827              marker->groups[i]->w = marker->groups[i]->clas->priv.edje_w
3828                 + marker->groups[i]->clas->priv.edje_w/8. * eina_list_count(marker->groups[i]->markers);
3829              marker->groups[i]->h = marker->groups[i]->clas->priv.edje_h
3830                 + marker->groups[i]->clas->priv.edje_h/8. * eina_list_count(marker->groups[i]->markers);
3831              if (marker->groups[i]->w > marker->groups[i]->clas->priv.edje_max_w)
3832                marker->groups[i]->w = marker->groups[i]->clas->priv.edje_max_w;
3833              if (marker->groups[i]->h > marker->groups[i]->clas->priv.edje_max_h)
3834                marker->groups[i]->h = marker->groups[i]->clas->priv.edje_max_h;
3835
3836              if ((marker->groups[i]->obj) && (eina_list_count(marker->groups[i]->markers) == 1))
3837                {
3838                   _group_object_free(marker->groups[i]);
3839                   _group_object_create(marker->groups[i]);
3840                }
3841           }
3842      }
3843
3844    if ((marker->content) && (marker->clas->func.del))
3845      marker->clas->func.del(marker->wd->obj, marker, marker->data, marker->content);
3846    else if (marker->content)
3847      evas_object_del(marker->content);
3848
3849    free(marker);
3850
3851    if (wd->grids)
3852      {
3853         Evas_Coord ox, oy, ow, oh;
3854         evas_object_geometry_get(wd->obj, &ox, &oy, &ow, &oh);
3855         marker_place(wd->obj, eina_list_data_get(wd->grids), wd->pan_x, wd->pan_y, ox, oy, ow, oh);
3856      }
3857 }
3858
3859 /**
3860  * Get the current coordinates of the marker.
3861  *
3862  * @param marker marker.
3863  * @param lat The latitude.
3864  * @param lon The longitude.
3865  *
3866  * @ingroup Map
3867  */
3868 EAPI void
3869 elm_map_marker_region_get(const Elm_Map_Marker *marker, double *lon, double *lat)
3870 {
3871    EINA_SAFETY_ON_NULL_RETURN(marker);
3872    if (lon) *lon = marker->longitude;
3873    if (lat) *lat = marker->latitude;
3874 }
3875
3876 /**
3877  * Move the map to the coordinate of the marker.
3878  *
3879  * @param marker The marker where the map will be center.
3880  *
3881  * @ingroup Map
3882  */
3883 EAPI void
3884 elm_map_marker_bring_in(Elm_Map_Marker *marker)
3885 {
3886    EINA_SAFETY_ON_NULL_RETURN(marker);
3887    elm_map_geo_region_bring_in(marker->wd->obj, marker->longitude, marker->latitude);
3888 }
3889
3890 /**
3891  * Move the map to the coordinate of the marker.
3892  *
3893  * @param marker The marker where the map will be center.
3894  *
3895  * @ingroup Map
3896  */
3897 EAPI void
3898 elm_map_marker_show(Elm_Map_Marker *marker)
3899 {
3900    EINA_SAFETY_ON_NULL_RETURN(marker);
3901    elm_map_geo_region_show(marker->wd->obj, marker->longitude, marker->latitude);
3902 }
3903
3904 /**
3905  * Move and zoom the map to display a list of markers.
3906  *
3907  * The map will be centered on the center point of the markers in the list. Then
3908  * the map will be zoomed in order to fit the markers using the maximum zoom which
3909  * allows display of all the markers.
3910  *
3911  * @param markers The list of markers (list of Elm_Map_Marker *)
3912  *
3913  * @ingroup Map
3914  */
3915 EAPI void
3916 elm_map_markers_list_show(Eina_List *markers)
3917 {
3918    int zoom;
3919    double lon, lat;
3920    Eina_List *l;
3921    Elm_Map_Marker *marker, *m_max_lon = NULL, *m_max_lat = NULL, *m_min_lon = NULL, *m_min_lat = NULL;
3922    Evas_Coord rw, rh, xc, yc;
3923    Widget_Data *wd;
3924
3925    EINA_SAFETY_ON_NULL_RETURN(markers);
3926
3927    EINA_LIST_FOREACH(markers, l, marker)
3928      {
3929         wd = marker->wd;
3930
3931         if ((!m_min_lon) || (marker->longitude < m_min_lon->longitude))
3932           m_min_lon = marker;
3933
3934         if ((!m_max_lon) || (marker->longitude > m_max_lon->longitude))
3935           m_max_lon = marker;
3936
3937         if ((!m_min_lat) || (marker->latitude > m_min_lat->latitude))
3938           m_min_lat = marker;
3939
3940         if ((!m_max_lat) || (marker->latitude < m_max_lat->latitude))
3941           m_max_lat = marker;
3942      }
3943
3944    lon = (m_max_lon->longitude - m_min_lon->longitude) / 2. + m_min_lon->longitude;
3945    lat = (m_max_lat->latitude - m_min_lat->latitude) / 2. + m_min_lat->latitude;
3946
3947    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
3948    for (zoom = map_sources_tab[wd->source].zoom_max; zoom>map_sources_tab[wd->source].zoom_min; zoom--)
3949      {
3950         Evas_Coord size = pow(2.0, zoom)*wd->tsize;
3951         elm_map_utils_convert_geo_into_coord(wd->obj, lon, lat, size, &xc, &yc);
3952
3953         if ((m_min_lon->x[zoom] - wd->marker_max_w >= xc-rw/2)
3954             && (m_min_lat->y[zoom] - wd->marker_max_h >= yc-rh/2)
3955             && (m_max_lon->x[zoom] + wd->marker_max_w <= xc+rw/2)
3956             && (m_max_lat->y[zoom] + wd->marker_max_h <= yc+rh/2))
3957           break;
3958      }
3959
3960    elm_map_geo_region_show(wd->obj, lon, lat);
3961    elm_map_zoom_set(wd->obj, zoom);
3962 }
3963
3964 /**
3965  * Set the maximum numbers of markers display in a group.
3966  *
3967  * A group can have a long list of markers, consequently the creation of the content
3968  * of the bubble can be very slow. In order to avoid this, a maximum number of items
3969  * is displayed in a bubble. By default this number is 30.
3970  *
3971  * @param obj The map object.
3972  * @param max The maximum numbers of items displayed in a bubble.
3973  *
3974  * @ingroup Map
3975  */
3976 EAPI void
3977 elm_map_max_marker_per_group_set(Evas_Object *obj, int max)
3978 {
3979    ELM_CHECK_WIDTYPE(obj, widtype);
3980    Widget_Data *wd = elm_widget_data_get(obj);
3981    if (!wd) return;
3982    wd->markers_max_num = max;
3983 }
3984
3985 /**
3986  * Return the evas object getting from the ElmMapMarkerGetFunc callback
3987  *
3988  * @param marker The marker.
3989  * @return Return the evas object if it exists, else NULL.
3990  *
3991  * @ingroup Map
3992  */
3993 EAPI Evas_Object *
3994 elm_map_marker_object_get(const Elm_Map_Marker *marker)
3995 {
3996    EINA_SAFETY_ON_NULL_RETURN_VAL(marker, NULL);
3997    return marker->content;
3998 }
3999
4000 /**
4001  * Update the marker
4002  *
4003  * @param marker The marker.
4004  *
4005  * @ingroup Map
4006  */
4007 EAPI void
4008 elm_map_marker_update(Elm_Map_Marker *marker)
4009 {
4010    EINA_SAFETY_ON_NULL_RETURN(marker);
4011    if (marker->content)
4012      {
4013         if (marker->clas->func.del)
4014           marker->clas->func.del(marker->wd->obj, marker, marker->data, marker->content);
4015         else
4016           evas_object_del(marker->content);
4017         marker->content = NULL;
4018         _group_bubble_content_update(marker->groups[marker->wd->zoom]);
4019      }
4020 }
4021
4022 /**
4023  * Close all opened bubbles
4024  *
4025  * @param obj The map object
4026  *
4027  * @ingroup Map
4028  */
4029 EAPI void
4030 elm_map_bubbles_close(Evas_Object *obj)
4031 {
4032    ELM_CHECK_WIDTYPE(obj, widtype);
4033    Widget_Data *wd = elm_widget_data_get(obj);
4034    Marker_Group *group;
4035    Eina_List *l, *l_next;
4036    if (!wd) return;
4037    EINA_LIST_FOREACH_SAFE(wd->opened_bubbles, l, l_next, group)
4038       _group_bubble_free(group);
4039 }
4040
4041
4042 /**
4043  * Create a group class.
4044  *
4045  * Each marker must be associated to a group class. Marker with the same group are grouped if they are close.
4046  * The group class defines the style of the marker when a marker is grouped to others markers.
4047  *
4048  * @param obj The map object
4049  * @return Returns the new group class
4050  *
4051  * @ingroup Map
4052  */
4053 EAPI Elm_Map_Group_Class *
4054 elm_map_group_class_new(Evas_Object *obj)
4055 {
4056    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4057    Widget_Data *wd = elm_widget_data_get(obj);
4058    if (!wd) return NULL;
4059    Elm_Map_Group_Class *clas = calloc(1, sizeof(Elm_Map_Group_Class));
4060    clas->zoom_grouped = ZOOM_MAX;
4061    wd->groups_clas = eina_list_append(wd->groups_clas, clas);
4062    return clas;
4063 }
4064
4065 /**
4066  * Set the style of a group class (radio, radio2 or empty)
4067  *
4068  * @param clas the group class
4069  * @param style the new style
4070  *
4071  * @ingroup Map
4072  */
4073 EAPI void
4074 elm_map_group_class_style_set(Elm_Map_Group_Class *clas, const char *style)
4075 {
4076    EINA_SAFETY_ON_NULL_RETURN(clas);
4077    eina_stringshare_replace(&clas->style, style);
4078 }
4079
4080 /**
4081  * Set the icon callback of a group class.
4082  *
4083  * A custom icon can be displayed in a marker. The function @ref icon_get must return this icon.
4084  *
4085  * @param clas the group class
4086  * @param icon_get the callback to create the icon
4087  *
4088  * @ingroup Map
4089  */
4090 EAPI void
4091 elm_map_group_class_icon_cb_set(Elm_Map_Group_Class *clas, ElmMapGroupIconGetFunc icon_get)
4092 {
4093    EINA_SAFETY_ON_NULL_RETURN(clas);
4094    clas->func.icon_get = icon_get;
4095 }
4096
4097 /**
4098  * Set the data associated to the group class (radio, radio2 or empty)
4099  *
4100  * @param clas the group class
4101  * @param data the new user data
4102  *
4103  * @ingroup Map
4104  */
4105 EAPI void
4106 elm_map_group_class_data_set(Elm_Map_Group_Class *clas, void *data)
4107 {
4108    EINA_SAFETY_ON_NULL_RETURN(clas);
4109    clas->data = data;
4110 }
4111
4112 /**
4113  * Set the zoom from where the markers are displayed.
4114  *
4115  * Markers will not be displayed for a zoom less than @ref zoom
4116  *
4117  * @param clas the group class
4118  * @param zoom the zoom
4119  *
4120  * @ingroup Map
4121  */
4122 EAPI void
4123 elm_map_group_class_zoom_displayed_set(Elm_Map_Group_Class *clas, int zoom)
4124 {
4125    EINA_SAFETY_ON_NULL_RETURN(clas);
4126    clas->zoom_displayed = zoom;
4127 }
4128
4129 /**
4130  * Set the zoom from where the markers are no more grouped.
4131  *
4132  * @param clas the group class
4133  * @param zoom the zoom
4134  *
4135  * @ingroup Map
4136  */
4137 EAPI void
4138 elm_map_group_class_zoom_grouped_set(Elm_Map_Group_Class *clas, int zoom)
4139 {
4140    EINA_SAFETY_ON_NULL_RETURN(clas);
4141    clas->zoom_grouped = zoom;
4142 }
4143
4144 /**
4145  * Set if the markers associated to the group class @clas are hidden or not.
4146  * If @ref hide is true the markers will be hidden.
4147  *
4148  * @param clas the group class
4149  * @param hide if true the markers will be hidden, else they will be displayed.
4150  *
4151  * @ingroup Map
4152  */
4153 EAPI void
4154 elm_map_group_class_hide_set(Evas_Object *obj, Elm_Map_Group_Class *clas, Eina_Bool hide)
4155 {
4156    ELM_CHECK_WIDTYPE(obj, widtype);
4157    Widget_Data *wd = elm_widget_data_get(obj);
4158    if (!wd) return;
4159    EINA_SAFETY_ON_NULL_RETURN(clas);
4160    if (clas->hide == hide) return;
4161    clas->hide = hide;
4162    if (wd->grids)
4163      {
4164         Evas_Coord ox, oy, ow, oh;
4165         evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
4166         marker_place(obj, eina_list_data_get(wd->grids), wd->pan_x, wd->pan_y, ox, oy, ow, oh);
4167      }
4168 }
4169
4170
4171 /**
4172  * Create a marker class.
4173  *
4174  * Each marker must be associated to a class.
4175  * The class defines the style of the marker when a marker is displayed alone (not grouped).
4176  *
4177  * @param obj The map object
4178  * @return Returns the new class
4179  *
4180  * @ingroup Map
4181  */
4182 EAPI Elm_Map_Marker_Class *
4183 elm_map_marker_class_new(Evas_Object *obj)
4184 {
4185    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4186    Widget_Data *wd = elm_widget_data_get(obj);
4187    if (!wd) return NULL;
4188    Elm_Map_Marker_Class *clas = calloc(1, sizeof(Elm_Map_Marker_Class));
4189    wd->markers_clas = eina_list_append(wd->markers_clas, clas);
4190    return clas;
4191 }
4192
4193 /**
4194  * Set the style of a class (radio, radio2 or empty)
4195  *
4196  * @param clas the group class
4197  * @param style the new style
4198  *
4199  * @ingroup Map
4200  */
4201 EAPI void
4202 elm_map_marker_class_style_set(Elm_Map_Marker_Class *clas, const char *style)
4203 {
4204    EINA_SAFETY_ON_NULL_RETURN(clas);
4205    eina_stringshare_replace(&clas->style, style);
4206 }
4207
4208 /**
4209  * Set the icon callback of a class.
4210  *
4211  * A custom icon can be displayed in a marker. The function @ref icon_get must return this icon.
4212  *
4213  * @param clas the group class
4214  * @param icon_get the callback to create the icon
4215  *
4216  * @ingroup Map
4217  */
4218 EAPI void
4219 elm_map_marker_class_icon_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerIconGetFunc icon_get)
4220 {
4221    EINA_SAFETY_ON_NULL_RETURN(clas);
4222    clas->func.icon_get = icon_get;
4223 }
4224
4225 /**
4226  *
4227  * Set the callback of the content of the bubble.
4228  *
4229  * When the user click on a marker, a bubble is displayed with a content.
4230  * The callback @ref get musst return this content. It can be NULL.
4231  *
4232  * @param clas the group class
4233  * @param get the callback to create the content
4234  *
4235  * @ingroup Map
4236  */
4237 EAPI void
4238 elm_map_marker_class_get_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerGetFunc get)
4239 {
4240    EINA_SAFETY_ON_NULL_RETURN(clas);
4241    clas->func.get = get;
4242 }
4243
4244 /**
4245  * Set the callback of the content of delete the object created by the callback "get".
4246  *
4247  * If this callback is defined the user will have to delete (or not) the object inside.
4248  * If the callback is not defined the object will be destroyed with evas_object_del()
4249  *
4250  * @param clas the group class
4251  * @param del the callback to delete the content
4252  *
4253  * @ingroup Map
4254  */
4255 EAPI void
4256 elm_map_marker_class_del_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerDelFunc del)
4257 {
4258    EINA_SAFETY_ON_NULL_RETURN(clas);
4259    clas->func.del = del;
4260 }
4261
4262 /**
4263  * Set the source of the map.
4264  *
4265  * Elm_Map retrieves the image which composed the map from a web service. This web service can
4266  * be set with this method. A different service can return a different maps with different
4267  * information and it can use different zoom value.
4268  *
4269  * @param clas the group class
4270  * @param source the new source
4271  *
4272  * @ingroup Map
4273  */
4274 EAPI void
4275 elm_map_source_set(Evas_Object *obj, Elm_Map_Sources source)
4276 {
4277    ELM_CHECK_WIDTYPE(obj, widtype);
4278    Widget_Data *wd = elm_widget_data_get(obj);
4279    Grid *grid;
4280    int zoom;
4281    if (!wd) return;
4282    if (wd->source == source ) return;
4283    if (!map_sources_tab[source].url_cb) return;
4284    _elm_config_sub_init();
4285
4286    EINA_LIST_FREE(wd->grids, grid) grid_clear(obj, grid);
4287
4288    wd->source = source;
4289    zoom = wd->zoom;
4290    wd->zoom = -1;
4291
4292    if (map_sources_tab[wd->source].zoom_max < zoom)
4293      zoom = map_sources_tab[wd->source].zoom_max;
4294    if (map_sources_tab[wd->source].zoom_min > zoom)
4295      zoom = map_sources_tab[wd->source].zoom_min;
4296
4297    elm_map_zoom_set(obj, zoom);
4298 }
4299
4300 /**
4301  * Set the source of the route.
4302  *
4303  * @param clas the group class
4304  * @param source the new source
4305  *
4306  * @ingroup Map
4307  */
4308 EAPI void
4309 elm_map_route_source_set(Evas_Object *obj, Elm_Map_Route_Sources source)
4310 {
4311    ELM_CHECK_WIDTYPE(obj, widtype);
4312    Widget_Data *wd = elm_widget_data_get(obj);
4313    if (!wd) return;
4314    wd->source = source;
4315 }
4316
4317 /**
4318  * Get the current source
4319  *
4320  * @param obj the map object
4321  * @return Returns the maximum zoom of the source
4322  *
4323  * @ingroup Map
4324  */
4325 EAPI Elm_Map_Sources
4326 elm_map_source_get(const Evas_Object *obj)
4327 {
4328    ELM_CHECK_WIDTYPE(obj, widtype) ELM_MAP_SOURCE_MAPNIK;
4329    Widget_Data *wd = elm_widget_data_get(obj);
4330    if (!wd) return ELM_MAP_SOURCE_MAPNIK;
4331    return wd->source;
4332 }
4333
4334 /**
4335  * Get the current route source
4336  *
4337  * @param obj the map object
4338  * @return Returns the source of the route
4339  *
4340  * @ingroup Map
4341  */
4342 EAPI Elm_Map_Route_Sources
4343 elm_map_route_source_get(const Evas_Object *obj)
4344 {
4345    ELM_CHECK_WIDTYPE(obj, widtype) ELM_MAP_ROUTE_SOURCE_YOURS;
4346    Widget_Data *wd = elm_widget_data_get(obj);
4347    if (!wd) return ELM_MAP_ROUTE_SOURCE_YOURS;
4348    return wd->route_source;
4349 }
4350
4351 /**
4352  * Set the API of a custom source.
4353  *
4354  * A custom web service can be associated to the source ELM_MAP_SOURCE_CUSTOM_(1..7).
4355  *
4356  * @param source the source ID (ELM_MAP_SOURCE_CUSTOM_(1..7))
4357  * @param name the name of the source
4358  * @param zoom_min the minimum zoom of the source, must be >= 0
4359  * @param zoom_max the maximum zoom of the source, must be <= ZOOM_MAX
4360  * @param url_cb the callback used to create the url from where a tile (png or jpeg file) is downloaded.
4361  *
4362  * @ingroup Map
4363  */
4364 EAPI void
4365 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)
4366 {
4367    EINA_SAFETY_ON_NULL_RETURN(name);
4368    EINA_SAFETY_ON_NULL_RETURN(url_cb);
4369    map_sources_tab[source].name = name;
4370    map_sources_tab[source].zoom_min = zoom_min;
4371    map_sources_tab[source].zoom_max = zoom_max;
4372    map_sources_tab[source].url_cb = url_cb;
4373    map_sources_tab[source].route_url_cb = route_url_cb;
4374    map_sources_tab[source].name_url_cb = name_url_cb;
4375 }
4376
4377 /**
4378  * Get the maximum zoom of the source.
4379  *
4380  * @param source the source
4381  * @return Returns the maximum zoom of the source
4382  *
4383  * @ingroup Map
4384  */
4385 EAPI int
4386 elm_map_source_zoom_max_get(Elm_Map_Sources source)
4387 {
4388    return map_sources_tab[source].zoom_max;
4389 }
4390
4391 /**
4392  * Get the minimum zoom of the source.
4393  *
4394  * @param source the source
4395  * @return Returns the minimum zoom of the source
4396  *
4397  * @ingroup Map
4398  */
4399 EAPI int
4400 elm_map_source_zoom_min_get(Elm_Map_Sources source)
4401 {
4402    return map_sources_tab[source].zoom_min;
4403 }
4404
4405 /**
4406  * Get the name of a source.
4407  *
4408  * @param source the source
4409  * @return Returns the name of the source
4410  *
4411  * @ingroup Map
4412  */
4413 EAPI const char *
4414 elm_map_source_name_get(Elm_Map_Sources source)
4415 {
4416    return map_sources_tab[source].name;
4417 }
4418
4419 /**
4420  * Set the user agent of the widget map.
4421  *
4422  * @param obj The map object
4423  * @param user_agent the user agent of the widget map
4424  *
4425  * @ingroup Map
4426  */
4427 EAPI void
4428 elm_map_user_agent_set(Evas_Object *obj, const char *user_agent)
4429 {
4430    ELM_CHECK_WIDTYPE(obj, widtype);
4431    Widget_Data *wd = elm_widget_data_get(obj);
4432
4433    if (!wd) return;
4434    if (!wd->user_agent) wd->user_agent = eina_stringshare_add(user_agent);
4435    else eina_stringshare_replace(&wd->user_agent, user_agent);
4436
4437    if (!wd->ua) wd->ua = eina_hash_string_small_new(NULL);
4438    eina_hash_set(wd->ua, "User-Agent", wd->user_agent);
4439 }
4440
4441 /**
4442  * Get the user agent of the widget map.
4443  *
4444  * @param obj The map object
4445  * @return The user agent of the widget map
4446  *
4447  * @ingroup Map
4448  */
4449 EAPI const char *
4450 elm_map_user_agent_get(Evas_Object *obj)
4451 {
4452    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4453    Widget_Data *wd = elm_widget_data_get(obj);
4454
4455    if (!wd) return NULL;
4456    return wd->user_agent;
4457 }
4458
4459 /**
4460  * Add a route on the map
4461  *
4462  * @param obj The map object
4463  * @param type the type if transport
4464  * @param method the routing method
4465  * @param flon the start longitude
4466  * @param flat the start latitude
4467  * @param tlon the destination longitude
4468  * @param tlat the destination latitude
4469  *
4470  * @return The Route object
4471  *
4472  * @ingroup Map
4473  */
4474 EAPI Elm_Map_Route *
4475 elm_map_route_add(Evas_Object *obj,
4476                   Elm_Map_Route_Type type,
4477                   Elm_Map_Route_Method method,
4478                   double flon,
4479                   double flat,
4480                   double tlon,
4481                   double tlat)
4482 {
4483    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
4484    Widget_Data *wd = elm_widget_data_get(obj);
4485    if (!wd) return EINA_FALSE;
4486    char buf[PATH_MAX];
4487    char *source;
4488    char *type_name = NULL;
4489    int fd;
4490    Elm_Map_Route *route = ELM_NEW(Elm_Map_Route);
4491    if (!route) return NULL;
4492
4493    snprintf(buf, sizeof(buf), DEST_ROUTE_XML_FILE);
4494    fd = mkstemp(buf);
4495    if (fd < 0)
4496      {
4497         free(route);
4498         return NULL;
4499      }
4500
4501    route->con_url = ecore_con_url_new(NULL);
4502    route->ud.fname = strdup(buf);
4503    INF("xml file : %s", route->ud.fname);
4504
4505    route->ud.fd = fdopen(fd, "w+");
4506    if ((!route->con_url) || (!route->ud.fd))
4507      {
4508         ecore_con_url_free(route->con_url);
4509         free(route);
4510         return NULL;
4511      }
4512
4513    route->wd = wd;
4514    route->color.r = 255;
4515    route->color.g = 0;
4516    route->color.b = 0;
4517    route->color.a = 255;
4518    route->handlers = eina_list_append
4519      (route->handlers, (void *)ecore_event_handler_add
4520          (ECORE_CON_EVENT_URL_COMPLETE, _route_complete_cb, route));
4521
4522    route->inbound = EINA_FALSE;
4523    route->type = type;
4524    route->method = method;
4525    route->flon = flon;
4526    route->flat = flat;
4527    route->tlon = tlon;
4528    route->tlat = tlat;
4529
4530    switch (type)
4531      {
4532       case ELM_MAP_ROUTE_TYPE_MOTOCAR:
4533         type_name = strdup(ROUTE_TYPE_MOTORCAR);
4534         break;
4535       case ELM_MAP_ROUTE_TYPE_BICYCLE:
4536         type_name = strdup(ROUTE_TYPE_BICYCLE);
4537         break;
4538       case ELM_MAP_ROUTE_TYPE_FOOT:
4539         type_name = strdup(ROUTE_TYPE_FOOT);
4540         break;
4541       default:
4542         break;
4543      }
4544
4545    source = map_sources_tab[wd->source].route_url_cb(obj, type_name, method, flon, flat, tlon, tlat);
4546    INF("route url = %s", source);
4547
4548    wd->route = eina_list_append(wd->route, route);
4549
4550    ecore_con_url_url_set(route->con_url, source);
4551    ecore_con_url_fd_set(route->con_url, fileno(route->ud.fd));
4552    ecore_con_url_data_set(route->con_url, route);
4553    ecore_con_url_get(route->con_url);
4554    if (type_name) free(type_name);
4555    if (source) free(source);
4556
4557    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
4558                            "elm,state,busy,start", "elm");
4559    evas_object_smart_callback_call(wd->obj, SIG_ROUTE_LOAD, NULL);
4560    return route;
4561 }
4562
4563 /**
4564  * Remove a route from the map
4565  *
4566  * @param route The route to remove
4567  *
4568  * @ingroup Map
4569  */
4570
4571 EAPI void
4572 elm_map_route_remove(Elm_Map_Route *route)
4573 {
4574    EINA_SAFETY_ON_NULL_RETURN(route);
4575
4576    Route_Waypoint *w;
4577    Route_Node *n;
4578    Evas_Object *p;
4579    Ecore_Event_Handler *h;
4580
4581    EINA_LIST_FREE(route->path, p)
4582      {
4583         evas_object_del(p);
4584      }
4585
4586    EINA_LIST_FREE(route->waypoint, w)
4587      {
4588         if (w->point) eina_stringshare_del(w->point);
4589         free(w);
4590      }
4591
4592    EINA_LIST_FREE(route->nodes, n)
4593      {
4594         if (n->pos.address) eina_stringshare_del(n->pos.address);
4595         free(n);
4596      }
4597
4598    EINA_LIST_FREE(route->handlers, h)
4599      {
4600         ecore_event_handler_del(h);
4601      }
4602
4603    if (route->ud.fname)
4604      {
4605         ecore_file_remove(route->ud.fname);
4606         free(route->ud.fname);
4607         route->ud.fname = NULL;
4608      }
4609 }
4610
4611 /**
4612  * Set the option used for the background color
4613  *
4614  * @param route The route object
4615  * @param r
4616  * @param g
4617  * @param b
4618  * @param a
4619  *
4620  * This sets the color used for the route
4621  *
4622  * @ingroup Map
4623  */
4624 EAPI void
4625 elm_map_route_color_set(Elm_Map_Route *route, int r, int g , int b, int a)
4626 {
4627    EINA_SAFETY_ON_NULL_RETURN(route);
4628    route->color.r = r;
4629    route->color.g = g;
4630    route->color.b = b;
4631    route->color.a = a;
4632 }
4633
4634 /**
4635  * Get the option used for the background color
4636  *
4637  * @param route The route object
4638  * @param r
4639  * @param g
4640  * @param b
4641  * @param a
4642  *
4643  * @ingroup Map
4644  */
4645 EAPI void
4646 elm_map_route_color_get(Elm_Map_Route *route, int *r, int *g , int *b, int *a)
4647 {
4648    EINA_SAFETY_ON_NULL_RETURN(route);
4649    if (r) *r = route->color.r;
4650    if (g) *g = route->color.g;
4651    if (b) *b = route->color.b;
4652    if (a) *a = route->color.a;
4653 }
4654
4655 /**
4656  * Get the information of route distance
4657  *
4658  * @param route the route object
4659  * @return Returns the distance of route (unit : km)
4660  *
4661  * @ingroup Map
4662  */
4663 EAPI double
4664 elm_map_route_distance_get(Elm_Map_Route *route)
4665 {
4666    EINA_SAFETY_ON_NULL_RETURN_VAL(route, 0.0);
4667    return route->info.distance;
4668 }
4669
4670 /**
4671  * Get the information of route nodes
4672  *
4673  * @param route the route object
4674  * @return Returns the nodes of route
4675  *
4676  * @ingroup Map
4677  */
4678
4679 EAPI const char*
4680 elm_map_route_node_get(Elm_Map_Route *route)
4681 {
4682    EINA_SAFETY_ON_NULL_RETURN_VAL(route, NULL);
4683    return route->info.nodes;
4684 }
4685
4686 /**
4687  * Get the information of route waypoint
4688  *
4689  * @param route the route object
4690  * @return Returns the waypoint of route
4691  *
4692  * @ingroup Map
4693  */
4694
4695 EAPI const char*
4696 elm_map_route_waypoint_get(Elm_Map_Route *route)
4697 {
4698    EINA_SAFETY_ON_NULL_RETURN_VAL(route, NULL);
4699    return route->info.waypoints;
4700 }
4701
4702 /**
4703  * Get the information of address
4704  *
4705  * @param name the name object
4706  * @return Returns the address of name
4707  *
4708  * @ingroup Map
4709  */
4710 EAPI const char *
4711 elm_map_name_address_get(Elm_Map_Name *name)
4712 {
4713    EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
4714    return name->address;
4715 }
4716
4717 /**
4718  * Get the current coordinates of the name.
4719  *
4720  * This gets the current coordinates of the name object.
4721  *
4722  * @param obj The name object
4723  * @param lat The latitude.
4724  * @param lon The longitude.
4725  *
4726  * @ingroup Map
4727  */
4728 EAPI void
4729 elm_map_name_region_get(Elm_Map_Name *name, double *lon, double *lat)
4730 {
4731    EINA_SAFETY_ON_NULL_RETURN(name);
4732    if (lon) *lon = name->lon;
4733    if (lat) *lat = name->lat;
4734 }
4735
4736 /**
4737  * Remove a name from the map
4738  *
4739  * @param name The name to remove
4740  *
4741  * @ingroup Map
4742  */
4743 EAPI void
4744 elm_map_name_remove(Elm_Map_Name *name)
4745 {
4746    EINA_SAFETY_ON_NULL_RETURN(name);
4747    if (name->address)
4748      {
4749         free(name->address);
4750         name->address = NULL;
4751      }
4752    if (name->handler)
4753      {
4754         ecore_event_handler_del(name->handler);
4755         name->handler = NULL;
4756      }
4757    if (name->ud.fname)
4758      {
4759         ecore_file_remove(name->ud.fname);
4760         free(name->ud.fname);
4761         name->ud.fname = NULL;
4762      }
4763 }
4764
4765 /**
4766  * Set the rotate degree of the map
4767  *
4768  * @param obj The map object
4769  * @param angle amount of degrees from 0.0 to 360.0 to rotate arount Z axis.
4770  * @param cx rotation's center horizontal position.
4771  * @param cy rotation's center vertical position.
4772  *
4773  * @ingroup Map
4774  */
4775 EAPI void
4776 elm_map_rotate_set(Evas_Object *obj, double degree, Evas_Coord cx, Evas_Coord cy)
4777 {
4778    ELM_CHECK_WIDTYPE(obj, widtype);
4779    Widget_Data *wd = elm_widget_data_get(obj);
4780
4781    wd->rotate.d = degree;
4782    wd->rotate.cx = cx;
4783    wd->rotate.cy = cy;
4784    wd->calc_job = ecore_job_add(_calc_job, wd);
4785 }
4786
4787 /**
4788  * Get the rotate degree of the map
4789  *
4790  * @param obj The map object
4791  * @return amount of degrees from 0.0 to 360.0 to rotate arount Z axis.
4792  * @param cx rotation's center horizontal position.
4793  * @param cy rotation's center vertical position.
4794  *
4795  * @ingroup Map
4796  */
4797 EAPI void
4798 elm_map_rotate_get(const Evas_Object *obj, double *degree, Evas_Coord *cx, Evas_Coord *cy)
4799 {
4800    ELM_CHECK_WIDTYPE(obj, widtype);
4801    Widget_Data *wd = elm_widget_data_get(obj);
4802
4803    if (degree) *degree = wd->rotate.d;
4804    if (cx) *cx = wd->rotate.cx;
4805    if (cy) *cy = wd->rotate.cy;
4806 }
4807
4808 static char *
4809 _mapnik_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4810 {
4811    char buf[PATH_MAX];
4812    snprintf(buf, sizeof(buf), "http://tile.openstreetmap.org/%d/%d/%d.png",
4813             zoom, x, y);
4814    return strdup(buf);
4815 }
4816
4817 static char *
4818 _osmarender_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4819 {
4820    char buf[PATH_MAX];
4821    snprintf(buf, sizeof(buf),
4822             "http://tah.openstreetmap.org/Tiles/tile/%d/%d/%d.png",
4823             zoom, x, y);
4824    return strdup(buf);
4825 }
4826
4827 static char *
4828 _cyclemap_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4829 {
4830    char buf[PATH_MAX];
4831    snprintf(buf, sizeof(buf),
4832             "http://andy.sandbox.cloudmade.com/tiles/cycle/%d/%d/%d.png",
4833             zoom, x, y);
4834    return strdup(buf);
4835 }
4836
4837 static char *
4838 _maplint_url_cb(Evas_Object *obj __UNUSED__, int x, int y, int zoom)
4839 {
4840    char buf[PATH_MAX];
4841    snprintf(buf, sizeof(buf),
4842             "http://tah.openstreetmap.org/Tiles/maplint/%d/%d/%d.png",
4843             zoom, x, y);
4844    return strdup(buf);
4845 }
4846
4847 static char *
4848 _custom1_url_cb(Evas_Object *obj __UNUSED__, int x __UNUSED__, int y __UNUSED__, int zoom __UNUSED__)
4849 {
4850    return strdup("");
4851 }
4852
4853 static char *
4854 _custom2_url_cb(Evas_Object *obj __UNUSED__, int x __UNUSED__, int y __UNUSED__, int zoom __UNUSED__)
4855 {
4856    return strdup("");
4857 }
4858
4859 static char *
4860 _custom3_url_cb(Evas_Object *obj __UNUSED__, int x __UNUSED__, int y __UNUSED__, int zoom __UNUSED__)
4861 {
4862    return strdup("");
4863 }
4864
4865 static char *
4866 _custom4_url_cb(Evas_Object *obj __UNUSED__, int x __UNUSED__, int y __UNUSED__, int zoom __UNUSED__)
4867 {
4868    return strdup("");
4869 }
4870
4871 static char *
4872 _custom5_url_cb(Evas_Object *obj __UNUSED__, int x __UNUSED__, int y __UNUSED__, int zoom __UNUSED__)
4873 {
4874    return strdup("");
4875 }
4876
4877 static char *
4878 _custom6_url_cb(Evas_Object *obj __UNUSED__, int x __UNUSED__, int y __UNUSED__, int zoom __UNUSED__)
4879 {
4880    return strdup("");
4881 }
4882
4883 static char *
4884 _module_url_cb(Evas_Object *obj, int x, int y, int zoom)
4885 {
4886    char *buf = NULL;
4887    Widget_Data *wd = elm_widget_data_get(obj);
4888    if (elm_map_source_get(obj) == ELM_MAP_SOURCE_MODULE)
4889      if ((wd->api) && (wd->api->obj_url_request))
4890        buf = wd->api->obj_url_request(obj, x, y, zoom);
4891
4892    if (!buf) buf = strdup("");
4893
4894    return buf;
4895 }
4896
4897 static char *_yours_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat)
4898 {
4899    char buf[PATH_MAX];
4900    snprintf(buf, sizeof(buf),
4901             "%s?flat=%lf&flon=%lf&tlat=%lf&tlon=%lf&v=%s&fast=%d&instructions=1",
4902             ROUTE_YOURS_URL, flat, flon, tlat, tlon, type_name, method);
4903
4904    return strdup(buf);
4905 }
4906
4907 // TODO: fix monav api
4908 /*
4909 static char *_monav_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat)
4910 {
4911    char buf[PATH_MAX];
4912    snprintf(buf, sizeof(buf),
4913             "%s?flat=%f&flon=%f&tlat=%f&tlon=%f&v=%s&fast=%d&instructions=1",
4914             ROUTE_MONAV_URL, flat, flon, tlat, tlon, type_name, method);
4915
4916    return strdup(buf);
4917 }
4918 */
4919
4920 // TODO: fix ors api
4921 /*
4922 static char *_ors_url_cb(Evas_Object *obj __UNUSED__, char *type_name, int method, double flon, double flat, double tlon, double tlat)
4923 {
4924    char buf[PATH_MAX];
4925    snprintf(buf, sizeof(buf),
4926             "%s?flat=%f&flon=%f&tlat=%f&tlon=%f&v=%s&fast=%d&instructions=1",
4927             ROUTE_ORS_URL, flat, flon, tlat, tlon, type_name, method);
4928
4929    return strdup(buf);
4930 }
4931 */
4932
4933 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__)
4934 {
4935    return strdup("");
4936 }
4937
4938 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__)
4939 {
4940    return strdup("");
4941 }
4942
4943 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__)
4944 {
4945    return strdup("");
4946 }
4947
4948 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__)
4949 {
4950    return strdup("");
4951 }
4952
4953 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__)
4954 {
4955    return strdup("");
4956 }
4957
4958 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__)
4959 {
4960    return strdup("");
4961 }
4962 /*
4963 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__)
4964 {
4965    // TODO: make example route module
4966    return strdup("");
4967 }
4968 */
4969
4970 static char *
4971 _nominatim_url_cb(Evas_Object *obj, int method, char *name, double lon, double lat)
4972 {
4973    Widget_Data *wd = elm_widget_data_get(obj);
4974    if (!wd) return strdup("");
4975    char **str;
4976    unsigned int ele, idx;
4977
4978    char search_url[PATH_MAX];
4979    char buf[PATH_MAX];
4980    if (method == ELM_MAP_NAME_METHOD_SEARCH)
4981      {
4982         search_url[0] = '\0';
4983         str = eina_str_split_full(name, " ", 0, &ele);
4984         for (idx = 0 ; idx < ele ; idx++)
4985           {
4986              eina_strlcat(search_url, str[idx], sizeof(search_url));
4987              if (!(idx == (ele-1))) eina_strlcat(search_url, "+", sizeof(search_url));
4988           }
4989         snprintf(buf, sizeof(buf), "%s/search?q=%s&format=xml&polygon=0&addressdetails=0", NAME_NOMINATIM_URL, search_url);
4990      }
4991    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);
4992    else strcpy(buf, "");
4993
4994    return strdup(buf);
4995 }
4996
4997 static char *
4998 _name_custom1_url_cb(Evas_Object *obj __UNUSED__, int method __UNUSED__, char *name __UNUSED__, double lon __UNUSED__, double lat __UNUSED__)
4999 {
5000    return strdup("");
5001 }
5002
5003 static char *
5004 _name_custom2_url_cb(Evas_Object *obj __UNUSED__, int method __UNUSED__, char *name __UNUSED__, double lon __UNUSED__, double lat __UNUSED__)
5005 {
5006    return strdup("");
5007 }
5008
5009 static char *
5010 _name_custom3_url_cb(Evas_Object *obj __UNUSED__, int method __UNUSED__, char *name __UNUSED__, double lon __UNUSED__, double lat __UNUSED__)
5011 {
5012    return strdup("");
5013 }
5014
5015 static char *
5016 _name_custom4_url_cb(Evas_Object *obj __UNUSED__, int method __UNUSED__, char *name __UNUSED__, double lon __UNUSED__, double lat __UNUSED__)
5017 {
5018    return strdup("");
5019 }
5020
5021 static char *
5022 _name_custom5_url_cb(Evas_Object *obj __UNUSED__, int method __UNUSED__, char *name __UNUSED__, double lon __UNUSED__, double lat __UNUSED__)
5023 {
5024    return strdup("");
5025 }
5026
5027 static char *
5028 _name_custom6_url_cb(Evas_Object *obj __UNUSED__, int method __UNUSED__, char *name __UNUSED__, double lon __UNUSED__, double lat __UNUSED__)
5029 {
5030    return strdup("");
5031 }
5032