e95cdadbfcd0c66ead5e9152e609a95400751243
[framework/uifw/elementary.git] / src / lib / elm_map.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "elm_module_priv.h"
4 #include "els_scroller.h"
5
6 /**
7  * @defgroup Map Map
8  * @ingroup Elementary
9  *
10  * This is a widget specifically for displaying the free map OpenStreetMap.
11  *
12  * Signals that you can add callbacks for are:
13  *
14  * clicked - This is called when a user has clicked the map without dragging
15  * around.
16  *
17  * press - This is called when a user has pressed down on the map.
18  *
19  * longpressed - This is called when a user has pressed down on the map for
20  * a long time without dragging around.
21  *
22  * clicked,double - This is called when a user has double-clicked the photo.
23  *
24  * load,details - Map detailed data load begins.
25  *
26  * loaded,details - This is called when all parts of the map are loaded.
27  *
28  * zoom,start - Zoom animation started.
29  *
30  * zoom,stop - Zoom animation stopped.
31  *
32  * zoom,change - Zoom changed when using an auto zoom mode.
33  *
34  * scroll - the content has been scrolled (moved)
35  *
36  * scroll,anim,start - scrolling animation has started
37  *
38  * scroll,anim,stop - scrolling animation has stopped
39  *
40  * scroll,drag,start - dragging the contents around has started
41  *
42  * scroll,drag,stop - dragging the contents around has stopped
43  *
44  * TODO : doxygen
45  */
46
47
48 typedef struct _Widget_Data Widget_Data;
49 typedef struct _Pan Pan;
50 typedef struct _Grid Grid;
51 typedef struct _Grid_Item Grid_Item;
52 typedef struct _Marker_Group Marker_Group;
53 typedef struct _Mod_Api Mod_Api;
54
55 #define DEST_DIR_ZOOM_PATH "/tmp/elm_map/%d/%d/"
56 #define DEST_DIR_PATH DEST_DIR_ZOOM_PATH"%d/"
57 #define DEST_FILE_PATH "%s%d.png"
58
59 // Map sources
60 // Currently the size of a tile must be 256*256
61 // and the size of the map must be pow(2.0, z)*tile_size
62 typedef struct _Map_Sources_Tab
63 {
64    Elm_Map_Sources source;
65    const char *name;
66    int zoom_min;
67    int zoom_max;
68    ElmMapSourceURLFunc url_cb;
69    Eina_Bool use_module : 1;
70 } Map_Sources_Tab;
71
72 #define ZOOM_MAX 20
73 #define TOUCH_HOLD_RANGE 40
74 //Zemm min is supposed to be 0
75 static char * _mapnik_url_cb(void *data ,int x, int y, int zoom);
76 static char * _osmarender_url_cb(void *data ,int x, int y, int zoom);
77 static char * _cyclemap_url_cb(void *data ,int x, int y, int zoom);
78 static char * _maplint_url_cb(void *data ,int x, int y, int zoom);
79 static char * _decarta_url_cb(void *data ,int x, int y, int zoom);
80 static Map_Sources_Tab map_sources_tab[] =
81 {
82      {ELM_MAP_SOURCE_MAPNIK, "Mapnik", 0, 18, _mapnik_url_cb, EINA_FALSE},
83      {ELM_MAP_SOURCE_OSMARENDER, "Osmarender", 0, 17, _osmarender_url_cb, EINA_FALSE},
84      {ELM_MAP_SOURCE_CYCLEMAP, "Cycle Map", 0, 17, _cyclemap_url_cb, EINA_FALSE},
85      {ELM_MAP_SOURCE_MAPLINT, "Maplint", 12, 16, _maplint_url_cb, EINA_FALSE},
86      {ELM_MAP_SOURCE_DECARTA, "Decarta", 1, 20, _decarta_url_cb, EINA_TRUE},
87      {ELM_MAP_SOURCE_CUSTOM_1, "Custom 1", 0, 18, NULL, EINA_FALSE},
88      {ELM_MAP_SOURCE_CUSTOM_2, "Custom 2", 0, 18, NULL, EINA_FALSE},
89      {ELM_MAP_SOURCE_CUSTOM_3, "Custom 3", 0, 18, NULL, EINA_FALSE},
90      {ELM_MAP_SOURCE_CUSTOM_4, "Custom 4", 0, 18, NULL, EINA_FALSE},
91      {ELM_MAP_SOURCE_CUSTOM_5, "Custom 5", 0, 18, NULL, EINA_FALSE},
92      {ELM_MAP_SOURCE_CUSTOM_6, "Custom 6", 0, 18, NULL, EINA_FALSE}
93 };
94
95 struct _Elm_Map_Marker_Class
96 {
97    const char *style;
98    int zoom_displayed;
99
100    struct _Elm_Map_Marker_Class_Func {
101       ElmMapMarkerGetFunc get;
102       ElmMapMarkerDelFunc del; //if NULL the object will be destroyed with evas_object_del()
103       ElmMapMarkerIconGetFunc icon_get;
104    } func;
105
106    struct { //this part is private, do not modify these values
107       Eina_Bool set : 1;
108       Evas_Coord edje_w, edje_h;
109    } priv;
110 };
111
112 struct _Elm_Map_Marker
113 {
114    Widget_Data *wd;
115    Elm_Map_Marker_Class *clas;
116    Elm_Map_Group_Class *clas_group;
117    double longitude, latitude;
118
119    Evas_Coord map_size;
120    Evas_Coord x[ZOOM_MAX+1], y[ZOOM_MAX+1];
121    void *data;
122
123    Marker_Group *groups[ZOOM_MAX+1];
124
125    Evas_Object *content;
126 };
127
128 struct _Elm_Map_Group_Class
129 {
130    const char *style;
131    void *data;
132    int zoom_displayed; // display the group if the zoom is >= to zoom_display
133    int zoom_grouped; // group the markers only if the zoom is <= to zoom_groups
134    Eina_Bool hide : 1;
135
136    struct {
137       ElmMapGroupIconGetFunc icon_get;
138    } func;
139    
140    struct { //this part is private, do not modify these values
141       Eina_Bool set : 1;
142       Evas_Coord edje_w, edje_h;
143       Evas_Coord edje_max_w, edje_max_h;
144       
145       Eina_List *objs_used;
146       Eina_List *objs_notused;
147    } priv;
148 };
149
150 struct _Marker_Group
151 {
152    Widget_Data *wd;
153    Eina_Matrixsparse_Cell *cell;
154    Elm_Map_Group_Class *clas;
155
156    Eina_List *markers;
157    long long sum_x, sum_y;
158    Evas_Coord x, y;
159    Evas_Coord w, h;
160
161    Evas_Object *obj, *bubble, *sc, *bx, *rect;
162    Eina_Bool open : 1;
163    Eina_Bool bringin : 1;
164    Eina_Bool update_nbelems : 1;
165    Eina_Bool update_resize : 1;
166    Eina_Bool update_raise : 1;
167    Eina_Bool delete_object : 1;
168 };
169
170 struct _Grid_Item
171 {
172    Widget_Data *wd;
173    Evas_Object *img;
174    //Evas_Object *txt;
175    const char *file;
176    struct {
177       int x, y, w, h;
178    } src, out;
179    Eina_Bool want : 1;
180    Eina_Bool download : 1;
181    Eina_Bool have : 1;
182    Ecore_File_Download_Job *job;
183    int try_num;
184 };
185
186 struct _Grid
187 {
188    Widget_Data *wd;
189    int tsize; // size of tile (tsize x tsize pixels)
190    int zoom; // zoom level tiles want for optimal display (1, 2, 4, 8)
191    int iw, ih; // size of image in pixels
192    int w, h; // size of grid image in pixels (represented by grid)
193    int gw, gh; // size of grid in tiles
194    Eina_Matrixsparse *grid;
195 };
196
197 struct _Widget_Data
198 {
199    Evas_Object *obj;
200    Evas_Object *scr;
201    Evas_Object *pan_smart;
202    Evas_Object *rect;
203    Evas_Object *sep_maps_markers; //map objects are below this object and marker objects are on top
204    Pan *pan;
205    Evas_Coord pan_x, pan_y, minw, minh;
206
207    int id;
208    int zoom;
209    Elm_Map_Zoom_Mode mode;
210
211    Ecore_Job *calc_job;
212    Ecore_Timer *scr_timer;
213    Ecore_Timer *long_timer;
214    Ecore_Animator *zoom_animator;
215    double t_start, t_end;
216    struct {
217       int w, h;
218       int ow, oh, nw, nh;
219       struct {
220          double x, y;
221       } spos;
222    } size;
223    struct {
224       Eina_Bool show : 1;
225       Evas_Coord x, y ,w ,h;
226    } show;
227    int tsize;
228    int nosmooth;
229    int preload_num;
230    Eina_List *grids;
231    Eina_Bool resized : 1;
232    Eina_Bool longpressed : 1;
233    Eina_Bool on_hold : 1;
234    Eina_Bool paused : 1;
235    Eina_Bool paused_markers : 1;
236    Eina_Bool pinch_zoom : 1;
237    
238    struct {
239       Eina_Bool enabled;
240       double lon, lat;
241    } center_on;
242
243    Ecore_Job *markers_place_job;
244    Eina_Matrixsparse *markers[ZOOM_MAX+1];
245    Eina_List *cells_displayed; // list of Eina_Matrixsparse_Cell
246    Evas_Coord markers_max_num;
247    int marker_max_w, marker_max_h;
248    int marker_zoom;
249    Eina_List *opened_bubbles; //opened bubbles, list of Map_Group *
250
251    Eina_List *groups_clas; // list of Elm_Map_Group_Class*
252    Eina_List *markers_clas; // list of Elm_Map_Markers_Class*
253
254    Elm_Map_Sources source;
255    Mod_Api *api;
256 };
257
258 struct _Pan
259 {
260    Evas_Object_Smart_Clipped_Data __clipped_data;
261    Widget_Data *wd;
262 };
263
264 struct _Mod_Api
265 {
266    Eina_Bool (*obj_hook) (Evas_Object *obj);
267    Eina_Bool (*obj_unhook) (Evas_Object *obj);
268    char * (*obj_url_request) (Evas_Object *obj, int x, int y, int zoom);
269    Eina_Bool (*obj_convert_coord_into_geo) (const Evas_Object *obj, int zoom, int x, int y, int size, double *lon, double *lat);
270    Eina_Bool (*obj_convert_geo_into_coord) (const Evas_Object *obj, int zoom, double lon, double lat, int size, int *x, int *y);
271 };
272
273 struct event_t
274 {
275    int device;
276    
277    struct prev {
278    Evas_Coord x, y;
279    } prev;
280    
281    Evas_Coord x, y, w, h;
282    
283    Evas_Object *object;
284    Ecore_Timer *hold_timer;
285    
286    int ts;
287    int v;
288    
289    int pinch_dis;
290    Evas_Object *pinch_obj;
291    Evas_Object *test;
292 };
293 static int dis_old = 0;
294 static Eina_List *s_event_list;
295
296 static const char *widtype = NULL;
297
298 static const char SIG_CHANGED[] = "changed";
299 static const char SIG_CLICKED[] = "clicked";
300 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
301 static const char SIG_LOADED_DETAIL[] = "loaded,detail";
302 static const char SIG_LOAD_DETAIL[] = "load,detail";
303 static const char SIG_LONGPRESSED[] = "longpressed";
304 static const char SIG_PRESS[] = "press";
305 static const char SIG_SCROLL[] = "scroll";
306 static const char SIG_SCROLL_DRAG_START[] = "scroll,drag,start";
307 static const char SIG_SCROLL_DRAG_STOP[] = "scroll,drag,stop";
308 static const char SIG_ZOOM_CHANGE[] = "zoom,change";
309 static const char SIG_ZOOM_START[] = "zoom,start";
310 static const char SIG_ZOOM_STOP[] = "zoom,stop";
311 static const Evas_Smart_Cb_Description _signals[] = {
312   {SIG_CHANGED, ""},
313   {SIG_CLICKED, ""},
314   {SIG_CLICKED_DOUBLE, ""},
315   {SIG_LOADED_DETAIL, ""},
316   {SIG_LOAD_DETAIL, ""},
317   {SIG_LONGPRESSED, ""},
318   {SIG_PRESS, ""},
319   {SIG_SCROLL, ""},
320   {SIG_SCROLL_DRAG_START, ""},
321   {SIG_SCROLL_DRAG_STOP, ""},
322   {SIG_ZOOM_CHANGE, ""},
323   {SIG_ZOOM_START, ""},
324   {SIG_ZOOM_STOP, ""},
325   {NULL, NULL}
326 };
327
328 static void _pan_calculate(Evas_Object *obj);
329
330 static void _del_hook(Evas_Object *obj);
331 static void _theme_hook(Evas_Object *obj);
332 static void _sizing_eval(Evas_Object *obj);
333 static void _calc_job(void *data);
334 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);
335 static void grid_clear(Evas_Object *obj, Grid *g);
336 static Grid *grid_create(Evas_Object *obj);
337 static void grid_load(Evas_Object *obj, Grid *g);
338
339
340 static void _group_object_create(Marker_Group *group);
341 static void _group_object_free(Marker_Group *group);
342 static void _group_open_cb(void *data, Evas_Object *obj, const char *emission, const char *soure);
343 static void _group_bringin_cb(void *data, Evas_Object *obj, const char *emission, const char *soure);
344 static void _group_bubble_create(Marker_Group *group);
345 static void _group_bubble_free(Marker_Group *group);
346 static void _group_bubble_place(Marker_Group *group);
347
348 static int _group_bubble_content_update(Marker_Group *group);
349 static void _group_bubble_content_free(Marker_Group *group);
350 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);
351 static void _bubble_sc_hits_changed_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
352
353 static Eina_Bool
354 hold_timer_cb(void *data)
355 {
356   struct event_t *ev0 = (struct event_t *)data;
357   ev0->hold_timer = NULL;
358
359   return ECORE_CALLBACK_CANCEL;
360 }
361
362 static int
363 get_multi_device(void)
364 {
365    Eina_List *l;
366    struct event_t *ev = NULL;
367    
368    EINA_LIST_FOREACH(s_event_list, l, ev) {
369       if(ev->device != 0) return ev->device;                            
370    }
371    return 0;
372 }
373
374 static int
375 get_distance(Evas_Coord x1, Evas_Coord y1, Evas_Coord x2, Evas_Coord y2)
376 {
377    int dis, dx, dy;
378    
379    dx = x1 - x2;
380    dy = y1 - y2;
381    
382    dis = (int)sqrt(dx * dx + dy  * dy);
383    return dis;
384 }
385
386 static struct event_t*
387 get_event_object(int device)
388 {
389    Eina_List *l;
390    struct event_t *ev = NULL;
391    
392    EINA_LIST_FOREACH(s_event_list, l, ev) {
393       if(ev->device == device) break;
394       ev = NULL;
395    }
396    return ev;
397 }
398
399 static struct event_t *
400 create_event_object(Evas_Object *object, int device)
401 {
402    struct event_t *ev;
403
404    ev = calloc(1, sizeof(struct event_t));
405    if(ev == NULL) DBG("Cannot allocate event_t");
406    
407    ev->object = object;
408    ev->device = device;
409    
410    evas_object_geometry_get(object, &ev->x, &ev->y, &ev->w, &ev->h);
411    
412    s_event_list = eina_list_append(s_event_list, ev);
413    
414    return ev;
415 }
416
417 static void
418 destroy_event_object(struct event_t *ev)
419 {
420    if(ev == NULL) return;
421    ev->pinch_obj = NULL;
422    ev->pinch_dis = 0;
423    
424    s_event_list = eina_list_remove(s_event_list, ev);
425    
426    if (ev->hold_timer) {
427       ecore_timer_del(ev->hold_timer);
428       ev->hold_timer = NULL;
429    }
430    free(ev);
431 }
432
433 static Mod_Api *
434 module(Evas_Object *obj __UNUSED__)
435 {
436    static Elm_Module *m = NULL;
437    if (m) goto ok; // already found - just use
438    if (!(m = _elm_module_find_as("map/api"))) return NULL;
439
440    // get module api
441    m->api = malloc(sizeof(Mod_Api));
442    if (!m->api) return NULL;
443    ((Mod_Api *)(m->api)      )->obj_hook = // called on creation
444      _elm_module_symbol_get(m, "obj_hook");
445    ((Mod_Api *)(m->api)      )->obj_unhook = // called on deletion
446      _elm_module_symbol_get(m, "obj_unhook");
447    ((Mod_Api *)(m->api)      )->obj_url_request =
448      _elm_module_symbol_get(m, "obj_url_request");
449    ((Mod_Api *)(m->api)      )->obj_convert_coord_into_geo =
450      _elm_module_symbol_get(m, "obj_convert_coord_into_geo");
451    ((Mod_Api *)(m->api)      )->obj_convert_geo_into_coord =
452      _elm_module_symbol_get(m, "obj_convert_geo_into_coord");
453    ok: // ok - return api
454    return m->api;
455 }
456
457 static void
458 rect_place(Evas_Object *obj, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
459 {
460    Widget_Data *wd = elm_widget_data_get(obj);
461    Evas_Coord ax, ay, gw, gh, hh, ww;
462    if (!wd) return;
463    evas_object_geometry_get(wd->rect, NULL, NULL, &ww, &hh);
464
465    ax = 0;
466    ay = 0;
467    gw = wd->size.w;
468    gh = wd->size.h;
469
470    if (ww == gw && hh == gh) return;
471
472    if (ow > gw) ax = (ow - gw) / 2;
473    if (oh > gh) ay = (oh - gh) / 2;
474    evas_object_move(wd->rect,
475                     ox + 0 - px + ax,
476                     oy + 0 - py + ay);
477    evas_object_resize(wd->rect, gw, gh);
478    
479    if (wd->show.show)
480      {
481         wd->show.show = EINA_FALSE;
482         elm_smart_scroller_child_region_show(wd->scr, wd->show.x, wd->show.y, wd->show.w, wd->show.h);
483      }
484 }
485
486 static void
487 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)
488 {
489    Widget_Data *wd = elm_widget_data_get(obj);
490    Evas_Coord ax, ay, gw, gh, tx, ty;
491    Eina_List *l, *markers;
492    Eina_Matrixsparse_Cell *cell;
493    Marker_Group *group;
494    int xx, yy, ww, hh;
495    char buf[PATH_MAX];
496    int y, x;
497    int g_xx, g_yy, g_hh, g_ww;
498
499    if (!wd) return;
500    if (g != eina_list_data_get(wd->grids)) return;
501
502    ax = 0;
503    ay = 0;
504    gw = wd->size.w;
505    gh = wd->size.h;
506    if (ow > gw) ax = (ow - gw) / 2;
507    if (oh > gh) ay = (oh - gh) / 2;
508
509    if (wd->zoom != wd->marker_zoom)
510      {
511         EINA_LIST_FREE(wd->cells_displayed, cell)
512           {
513              EINA_LIST_FOREACH(eina_matrixsparse_cell_data_get(cell), l, group)
514                {
515                   if (group->obj) _group_object_free(group);
516                }
517           }
518      }
519    wd->marker_zoom = wd->zoom;
520
521    if (wd->paused_markers
522        && (wd->size.nw != wd->size.w || wd->size.nh != wd->size.h) )
523      return;
524
525    g_xx = wd->pan_x / wd->tsize;
526    if (g_xx < 0) g_xx = 0;
527    g_yy = wd->pan_y / wd->tsize;
528    if (g_yy < 0) g_yy = 0;
529    g_ww =  ow / wd->tsize + 1;
530    if (g_xx + g_ww >= g->gw) g_ww = g->gw - g_xx - 1;
531    g_hh =  oh / wd->tsize + 1;
532    if (g_yy + g_hh >= g->gh) g_hh = g->gh - g_yy - 1;
533
534    //hide groups no more displayed
535    EINA_LIST_FREE(wd->cells_displayed, cell)
536      {
537         eina_matrixsparse_cell_position_get(cell, (unsigned long *)&y, (unsigned long *)&x);
538         if (y < g_yy || y > g_yy + g_hh || x < g_xx || x > g_xx + g_ww)
539           {
540              EINA_LIST_FOREACH(eina_matrixsparse_cell_data_get(cell), l, group)
541                {
542                   if (group->obj) _group_object_free(group);
543                }
544           }
545      }
546
547    for (y = g_yy; y <= g_yy + g_hh; y++)
548      {
549         for (x = g_xx; x <= g_xx + g_ww; x++)
550           {
551              if (!wd->markers[wd->zoom]) continue;
552              eina_matrixsparse_cell_idx_get(wd->markers[wd->zoom], y, x, &cell);
553              if (!cell) continue;
554              wd->cells_displayed = eina_list_append(wd->cells_displayed, cell);
555              markers = eina_matrixsparse_cell_data_get(cell);
556              EINA_LIST_FOREACH(markers, l, group)
557                {
558                   if (!group->markers) continue;
559                   if (group->clas->zoom_displayed > wd->zoom) continue;
560
561                   xx = group->x;
562                   yy = group->y;
563                   ww = group->w;
564                   hh = group->h;
565
566                   if (eina_list_count(group->markers) == 1)
567                     {
568                        Elm_Map_Marker *m = eina_list_data_get(group->markers);
569                        ww = m->clas->priv.edje_w;
570                        hh = m->clas->priv.edje_h;
571                     }
572
573                   if (ww <= 0) ww = 1;
574                   if (hh <= 0) hh = 1;
575
576                   if ((gw != g->w) && (g->w > 0))
577                     {
578                        tx = xx;
579                        xx = ((long long )gw * xx) / g->w;
580                        ww = (((long long)gw * (tx + ww)) / g->w) - xx;
581                     }
582                   if ((gh != g->h) && (g->h > 0))
583                     {
584                        ty = yy;
585                        yy = ((long long)gh * yy) / g->h;
586                        hh = (((long long)gh * (ty + hh)) / g->h) - yy;
587                     }
588
589                   if (!group->clas->hide
590                       && xx-px+ax+ox >= ox && xx-px+ax+ox<= ox+ow
591                       && yy-py+ay+oy >= oy && yy-py+ay+oy<= oy+oh)
592                     {
593                        if (!group->obj) _group_object_create(group);
594
595                        if (group->update_nbelems)
596                          {
597                             group->update_nbelems = EINA_FALSE;
598                             if (eina_list_count(group->markers) > 1)
599                               {
600                                  snprintf(buf, sizeof(buf), "%d", eina_list_count(group->markers));
601                                  edje_object_part_text_set(elm_layout_edje_get(group->obj), "elm.text", buf);
602                               }
603                             else
604                               edje_object_part_text_set(elm_layout_edje_get(group->obj), "elm.text", "");
605                          }
606                        evas_object_move(group->obj,
607                                         xx - px + ax + ox - ww/2,
608                                         yy - py + ay + oy - hh/2);
609                        if (!wd->paused_markers || group->update_resize)
610                          {
611                             group->update_resize = EINA_FALSE;
612                             evas_object_resize(group->obj, ww, hh);
613                          }
614                        if (group->update_raise)
615                          {
616                             group->update_raise = EINA_FALSE;
617                             evas_object_raise(group->obj);
618                             evas_object_show(group->obj);
619                          }
620                        if (group->bubble) _group_bubble_place(group);
621                     }
622                   else if (group->obj)
623                     {
624                        _group_object_free(group);
625                     }
626                }
627           }
628      }
629 }
630
631 static void
632 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)
633 {
634    Widget_Data *wd = elm_widget_data_get(obj);
635    Evas_Coord ax, ay, gw, gh, tx, ty;
636    int xx, yy, ww, hh;
637
638    if (!wd) return;
639    
640    ax = 0;
641    ay = 0;
642    gw = wd->size.w;
643    gh = wd->size.h;
644    if (ow > gw) ax = (ow - gw) / 2;
645    if (oh > gh) ay = (oh - gh) / 2;
646
647    Eina_Iterator *it = eina_matrixsparse_iterator_new(g->grid);
648    Eina_Matrixsparse_Cell *cell;
649
650    EINA_ITERATOR_FOREACH(it, cell)
651      {
652         Grid_Item *gi = eina_matrixsparse_cell_data_get(cell);
653
654         xx = gi->out.x;
655         yy = gi->out.y;
656         ww = gi->out.w;
657         hh = gi->out.h;
658         if ((gw != g->w) && (g->w > 0))
659           {
660              tx = xx;
661              xx = ((long long )gw * xx) / g->w;
662              ww = (((long long)gw * (tx + ww)) / g->w) - xx;
663           }
664         if ((gh != g->h) && (g->h > 0))
665           {
666              ty = yy;
667              yy = ((long long)gh * yy) / g->h;
668              hh = (((long long)gh * (ty + hh)) / g->h) - yy;
669           }
670         evas_object_move(gi->img,
671                          xx - px + ax + ox,
672                          yy - py + ay + oy);
673         
674         evas_object_resize(gi->img, ww, hh);
675
676         /*evas_object_move(gi->txt,
677           xx - px + ax + ox,
678           yy - py + ay + oy);
679
680           evas_object_resize(gi->txt, ww, hh);
681           */
682      }
683    eina_iterator_free(it);
684 }
685
686 static void
687 grid_clear(Evas_Object *obj, Grid *g)
688 {
689    Widget_Data *wd = elm_widget_data_get(obj);
690    char buf[PATH_MAX];
691
692    if (!wd) return;
693    if (!g->grid) return;
694
695    Eina_Iterator *it = eina_matrixsparse_iterator_new(g->grid);
696    Eina_Matrixsparse_Cell *cell;
697
698    snprintf(buf, sizeof(buf), DEST_DIR_ZOOM_PATH, wd->id, g->zoom);
699    ecore_file_recursive_rm(buf);
700
701    EINA_ITERATOR_FOREACH(it, cell)
702      {
703         Grid_Item *gi = eina_matrixsparse_cell_data_get(cell);
704         evas_object_del(gi->img);
705         //evas_object_del(gi->txt);
706
707         if (gi->want)
708           {
709              gi->want = EINA_FALSE;
710              wd->preload_num--;
711              if (wd->preload_num == 0)
712                {
713                   edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
714                         "elm,state,busy,stop", "elm");
715                   evas_object_smart_callback_call(obj, SIG_LOADED_DETAIL, NULL);
716                }
717           }
718
719         if (gi->job)
720           {
721              DBG("DOWNLOAD abort %s", gi->file);
722              ecore_file_download_abort(gi->job);
723              ecore_file_remove(gi->file);
724              gi->job = NULL;
725           }
726         if (gi->file)
727           eina_stringshare_del(gi->file);
728         
729         free(gi);
730      }
731    eina_matrixsparse_free(g->grid);
732    eina_iterator_free(it);
733    g->grid = NULL;
734    g->gw = 0;
735    g->gh = 0;
736 }
737
738 static void
739 _tile_update(Grid_Item *gi)
740 {
741    gi->want = EINA_FALSE;
742    gi->download = EINA_FALSE;
743    evas_object_image_file_set(gi->img, gi->file, NULL);
744    if (evas_object_image_load_error_get(gi->img) != EVAS_LOAD_ERROR_NONE)
745      ecore_file_remove(gi->file);
746    
747    evas_object_show(gi->img);
748
749    //evas_object_text_text_set(gi->txt, gi->file);
750    //evas_object_show(gi->txt);
751
752    gi->have = EINA_TRUE;
753    gi->wd->preload_num--;
754    if (gi->wd->preload_num == 0)
755      {
756         edje_object_signal_emit(elm_smart_scroller_edje_object_get(gi->wd->scr),
757                                 "elm,state,busy,stop", "elm");
758         evas_object_smart_callback_call(gi->wd->obj, SIG_LOADED_DETAIL, NULL);
759      }
760 }
761
762
763 static void
764 _tile_downloaded(void *data, const char *file __UNUSED__, int status)
765 {
766    Grid_Item *gi = data;
767
768    gi->download = EINA_FALSE;
769    gi->job = NULL;
770
771    DBG("DOWNLOAD done %s", gi->file);
772    if (gi->want && !status) _tile_update(gi);
773
774    if (status)
775      {
776         DBG("Download failed %s (%d) ", gi->file, status);
777         ecore_file_remove(gi->file);
778      }
779 }
780
781 static Grid *
782 grid_create(Evas_Object *obj)
783 {
784    Widget_Data *wd = elm_widget_data_get(obj);
785    Grid *g;
786
787    g = calloc(1, sizeof(Grid));
788
789    g->zoom = wd->zoom;
790    g->tsize = wd->tsize;
791    g->wd = wd;
792
793    if (g->zoom > map_sources_tab[wd->source].zoom_max) return NULL;
794    if (g->zoom < map_sources_tab[wd->source].zoom_min) return NULL;
795
796    int size =  pow(2.0, wd->zoom);
797    g->gw = size;
798    g->gh = size;
799
800    g->w = g->tsize * g->gw;
801    g->h = g->tsize * g->gh;
802
803    g->grid = eina_matrixsparse_new(g->gh, g->gw, NULL, NULL);
804
805    return g;
806 }
807
808 static void
809 grid_load(Evas_Object *obj, Grid *g)
810 {
811    Widget_Data *wd = elm_widget_data_get(obj);
812    int x, y;
813    int size;
814    Evas_Coord ox, oy, ow, oh, cvx, cvy, cvw, cvh, tx, ty, gw, gh, xx, yy, ww, hh;
815    Eina_Iterator *it;
816    Eina_Matrixsparse_Cell *cell;
817    Grid_Item *gi;
818
819    if (!wd) return;
820    evas_object_geometry_get(wd->pan_smart, &ox, &oy, &ow, &oh);
821    evas_output_viewport_get(evas_object_evas_get(wd->obj), &cvx, &cvy, &cvw, &cvh);
822                                
823    gw = wd->size.w;
824    gh = wd->size.h;
825
826    if ((gw <= 0) || (gh <= 0)) return;
827
828    size = g->tsize;
829    if ((gw != g->w) && (g->w > 0))
830      size = ((long long)gw * size) / g->w;
831    if (size < (g->tsize / 2)) return; // else we will load to much tiles
832
833    it = eina_matrixsparse_iterator_new(g->grid);
834    
835    EINA_ITERATOR_FOREACH(it, cell)
836      {
837         gi = eina_matrixsparse_cell_data_get(cell);
838
839         xx = gi->out.x;
840         yy = gi->out.y;
841         ww = gi->out.w;
842         hh = gi->out.h;
843
844         if ((gw != g->w) && (g->w > 0))
845           {
846              tx = xx;
847              xx = ((long long )gw * xx) / g->w;
848              ww = (((long long)gw * (tx + ww)) / g->w) - xx;
849           }
850         if ((gh != g->h) && (g->h > 0))
851           {
852              ty = yy;
853              yy = ((long long)gh * yy) / g->h;
854              hh = (((long long)gh * (ty + hh)) / g->h) - yy;
855           }
856
857         if (!ELM_RECTS_INTERSECT(xx - wd->pan_x + ox, 
858                                  yy  - wd->pan_y + oy,
859                                  ww, hh,
860                                  cvx, cvy, cvw, cvh))
861           {
862              if (gi->want)
863                {
864                   wd->preload_num--;
865                   if (wd->preload_num == 0)
866                     {
867                        edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
868                                                "elm,state,busy,stop", "elm");
869                        evas_object_smart_callback_call(obj, SIG_LOADED_DETAIL,
870                                                        NULL);
871                     }
872                   evas_object_hide(gi->img);
873                   //evas_object_hide(gi->txt);
874                   evas_object_image_file_set(gi->img, NULL, NULL);
875                   gi->want = EINA_FALSE;
876                   gi->have = EINA_FALSE;
877                   
878                   if (gi->job)
879                     {
880                        DBG("DOWNLOAD abort %s", gi->file);
881                        ecore_file_download_abort(gi->job);
882                        ecore_file_remove(gi->file);
883                        gi->job = NULL;
884                     }
885                   gi->download = EINA_FALSE;
886                }
887              else if (gi->have)
888                {
889                   evas_object_hide(gi->img);
890                   //evas_object_hide(gi->txt);
891                   evas_object_image_preload(gi->img, 1);
892                   evas_object_image_file_set(gi->img, NULL, NULL);
893                   gi->have = EINA_FALSE;
894                   gi->want = EINA_FALSE;
895                }
896           }
897      }
898    eina_iterator_free(it);
899
900    xx = wd->pan_x / size;
901    if (xx < 0) xx = 0;
902
903    yy = wd->pan_y / size;
904    if (yy < 0) yy = 0;
905
906    ww = ow / size + 1;
907    if (xx + ww >= g->gw) ww = g->gw - xx - 1;
908
909    hh = oh / size + 1;
910    if (yy + hh >= g->gh) hh = g->gh - yy - 1;
911
912    for (y = yy; y <= yy + hh; y++)
913      {
914         for (x = xx; x <= xx + ww; x++)
915           {
916              gi = eina_matrixsparse_data_idx_get(g->grid, y, x);
917
918              if ((!gi) && (g != eina_list_data_get(wd->grids)))
919                continue;
920
921              if (!gi)
922                {
923                   gi = calloc(1, sizeof(Grid_Item));
924                   gi->src.x = x * g->tsize;
925                   gi->src.y = y * g->tsize;
926                   gi->src.w = g->tsize;
927                   gi->src.h = g->tsize;
928
929                   gi->out.x = gi->src.x;
930                   gi->out.y = gi->src.y;
931                   gi->out.w = gi->src.w;
932                   gi->out.h = gi->src.h;
933
934                   gi->wd = wd;
935                   
936                   gi->img = evas_object_image_add(evas_object_evas_get(obj));
937                   evas_object_image_scale_hint_set
938                     (gi->img, EVAS_IMAGE_SCALE_HINT_DYNAMIC);
939                   evas_object_image_filled_set(gi->img, 1);
940                   
941                   evas_object_smart_member_add(gi->img, wd->pan_smart);
942                   elm_widget_sub_object_add(obj, gi->img);
943                   evas_object_pass_events_set(gi->img, EINA_TRUE);
944                   evas_object_stack_below(gi->img, wd->sep_maps_markers);
945                   
946                   /*gi->txt = evas_object_text_add(evas_object_evas_get(obj));
947                    evas_object_text_font_set(gi->txt, "Vera", 12);
948                    evas_object_color_set(gi->txt, 100, 100, 100, 255);
949                    evas_object_smart_member_add(gi->txt,
950                    wd->pan_smart);
951                    elm_widget_sub_object_add(obj, gi->txt);
952                    evas_object_pass_events_set(gi->txt, EINA_TRUE);
953                    */
954                   eina_matrixsparse_data_idx_set(g->grid, y, x, gi);
955                }
956              
957              if (!gi->have && !gi->download)
958                {
959                   char buf[PATH_MAX], buf2[PATH_MAX];
960                   char *source;
961                   
962                   gi->want = EINA_TRUE;
963                   
964                   snprintf(buf, sizeof(buf), DEST_DIR_PATH, wd->id, g->zoom, x);
965                   if (!ecore_file_exists(buf))
966                     ecore_file_mkpath(buf);
967                   
968                   snprintf(buf2, sizeof(buf2), DEST_FILE_PATH, buf, y);
969                   
970                   source = map_sources_tab[wd->source].url_cb(obj, x, y, g->zoom);
971                   if(strlen(source)==0) continue;
972                   
973                   eina_stringshare_replace(&gi->file, buf2);
974
975                   if (ecore_file_exists(buf2) || g == eina_list_data_get(wd->grids))
976                     {
977                        gi->download = EINA_TRUE;
978                        wd->preload_num++;
979                        if (wd->preload_num == 1)
980                          {
981                             edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
982                                                     "elm,state,busy,start", "elm");
983                             evas_object_smart_callback_call(obj,
984                                                             SIG_LOAD_DETAIL,
985                                                             NULL);
986                          }
987                        
988                        if (ecore_file_exists(buf2))
989                          _tile_update(gi);
990                        else
991                          {
992                             DBG("DOWNLOAD %s \t in %s", source, buf2);
993                             ecore_file_download(source, buf2, _tile_downloaded, NULL, gi, &(gi->job));
994                             if (!gi->job)
995                               DBG("Can't start to download %s", buf);
996                          }
997                     }
998                   if (source) free(source);
999                }
1000              else if (gi->have)
1001                evas_object_show(gi->img);
1002           }
1003      }
1004 }
1005
1006 static void
1007 grid_clearall(Evas_Object *obj)
1008 {
1009    Widget_Data *wd = elm_widget_data_get(obj);
1010    Grid *g;
1011
1012    if (!wd) return;
1013    EINA_LIST_FREE(wd->grids, g)
1014      {
1015         grid_clear(obj, g);
1016         free(g);
1017      }
1018 }
1019
1020 static void
1021 _smooth_update(Evas_Object *obj)
1022 {
1023    Widget_Data *wd = elm_widget_data_get(obj);
1024    Eina_List *l;
1025    Grid *g;
1026
1027    if (!wd) return;
1028    EINA_LIST_FOREACH(wd->grids, l, g)
1029      {
1030         Eina_Iterator *it = eina_matrixsparse_iterator_new(g->grid);
1031         Eina_Matrixsparse_Cell *cell;
1032         
1033         EINA_ITERATOR_FOREACH(it, cell)
1034           {
1035              Grid_Item *gi = eina_matrixsparse_cell_data_get(cell);
1036              evas_object_image_smooth_scale_set(gi->img, (wd->nosmooth == 0));
1037           }
1038         eina_iterator_free(it);
1039      }
1040 }
1041
1042 static void
1043 _grid_raise(Grid *g)
1044 {
1045    Eina_Iterator *it = eina_matrixsparse_iterator_new(g->grid);
1046    Eina_Matrixsparse_Cell *cell;
1047
1048    g->wd->size.w = g->w;
1049    g->wd->size.h = g->h;
1050
1051    EINA_ITERATOR_FOREACH(it, cell)
1052      {
1053         Grid_Item *gi = eina_matrixsparse_cell_data_get(cell);
1054         evas_object_raise(gi->img);
1055         //evas_object_raise(gi->txt);
1056      }
1057    eina_iterator_free(it);
1058 }
1059
1060 static Eina_Bool
1061 _scr_timeout(void *data)
1062 {
1063    Widget_Data *wd = elm_widget_data_get(data);
1064    if (!wd) return ECORE_CALLBACK_CANCEL;
1065    wd->nosmooth--;
1066    if (wd->nosmooth == 0) _smooth_update(data);
1067    wd->scr_timer = NULL;
1068    return ECORE_CALLBACK_CANCEL;
1069 }
1070
1071 static void
1072 _scr(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1073 {
1074    Widget_Data *wd = elm_widget_data_get(data);
1075    if (!wd) return;
1076    if (!wd->scr_timer)
1077      {
1078         wd->nosmooth++;
1079         if (wd->nosmooth == 1) _smooth_update(data);
1080      }
1081    if (wd->scr_timer) ecore_timer_del(wd->scr_timer);
1082    wd->scr_timer = ecore_timer_add(0.5, _scr_timeout, data);
1083 }
1084
1085 static Eina_Bool
1086 zoom_do(Evas_Object *obj, double t)
1087 {
1088    Widget_Data *wd = elm_widget_data_get(obj);
1089    Evas_Coord xx, yy, ow, oh;
1090
1091    if (!wd) return 0;
1092    if (t > 1.0) t = 1.0;
1093
1094    wd->size.w = (wd->size.ow * (1.0 - t)) + (wd->size.nw * t);
1095    wd->size.h = (wd->size.oh * (1.0 - t)) + (wd->size.nh * t);
1096
1097    elm_smart_scroller_child_viewport_size_get(wd->scr, &ow, &oh);
1098
1099    if (wd->center_on.enabled)
1100      {
1101         elm_map_utils_convert_geo_into_coord(obj, wd->center_on.lon, wd->center_on.lat, wd->size.w, &xx, &yy);
1102         xx -= ow / 2;
1103         yy -= oh / 2;
1104      }
1105    else
1106      {
1107         xx = (wd->size.spos.x * wd->size.w) - (ow / 2);
1108         yy = (wd->size.spos.y * wd->size.h) - (oh / 2);
1109      }
1110
1111    if (xx < 0) xx = 0;
1112    else if (xx > (wd->size.w - ow)) xx = wd->size.w - ow;
1113    if (yy < 0) yy = 0;
1114    else if (yy > (wd->size.h - oh)) yy = wd->size.h - oh;
1115
1116    wd->show.show = EINA_TRUE;
1117    wd->show.x = xx;
1118    wd->show.y = yy;
1119    wd->show.w = ow;
1120    wd->show.h = oh;
1121
1122    if (wd->calc_job) ecore_job_del(wd->calc_job);
1123    wd->calc_job = ecore_job_add(_calc_job, wd);
1124    if (t >= 1.0)
1125      {
1126         return ECORE_CALLBACK_CANCEL;
1127      }
1128    return ECORE_CALLBACK_RENEW;
1129 }
1130
1131 static Eina_Bool
1132 _zoom_anim(void *data)
1133 {
1134    Evas_Object *obj = data;
1135    Widget_Data *wd = elm_widget_data_get(obj);
1136    double t;
1137    int go;
1138
1139    if (!wd) return ECORE_CALLBACK_CANCEL;
1140    t = ecore_loop_time_get();
1141    if (t >= wd->t_end)
1142      t = 1.0;
1143    else if (wd->t_end > wd->t_start)
1144      t = (t - wd->t_start) / (wd->t_end - wd->t_start);
1145    else
1146      t = 1.0;
1147    t = 1.0 - t;
1148    t = 1.0 - (t * t);
1149    go = zoom_do(obj, t);
1150    if (!go)
1151      {
1152         wd->nosmooth--;
1153         if (wd->nosmooth == 0) _smooth_update(data);
1154         wd->zoom_animator = NULL;
1155         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
1156      }
1157    return go;
1158 }
1159
1160 static Eina_Bool
1161 _long_press(void *data)
1162 {
1163    Widget_Data *wd = elm_widget_data_get(data);
1164    if (!wd) return ECORE_CALLBACK_CANCEL;
1165    wd->long_timer = NULL;
1166    wd->longpressed = EINA_TRUE;
1167    evas_object_smart_callback_call(data, SIG_LONGPRESSED, NULL);
1168    return ECORE_CALLBACK_CANCEL;
1169 }
1170
1171 static void
1172 _mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1173 {
1174    Widget_Data *wd = elm_widget_data_get(data);
1175    Evas_Event_Mouse_Down *ev = (Evas_Event_Mouse_Down*)event_info;
1176    struct event_t *ev0;
1177    
1178    ev0 = get_event_object(0);
1179    if(!ev0){
1180       ev0 = create_event_object(obj, 0);
1181       if(ev0){
1182          ev0->hold_timer = NULL;
1183          ev0->prev.x = ev->output.x;
1184          ev0->prev.y = ev->output.y;
1185       }
1186    }
1187
1188    if (!wd) return;
1189    if (ev->button != 1) return;
1190    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
1191    else wd->on_hold = EINA_FALSE;
1192    if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
1193      evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
1194    else
1195      evas_object_smart_callback_call(data, SIG_PRESS, NULL);
1196    wd->longpressed = EINA_FALSE;
1197    if (wd->long_timer) ecore_timer_del(wd->long_timer);
1198    wd->long_timer = ecore_timer_add(1.0, _long_press, data);
1199 }
1200
1201 static void
1202 _mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1203 {
1204    Evas_Event_Mouse_Move *move = (Evas_Event_Mouse_Move *)event_info;
1205    struct event_t *ev0;
1206    ev0 = get_event_object(0);
1207    if(ev0 == NULL) return;
1208         
1209    ev0->prev.x = move->cur.output.x;
1210    ev0->prev.y = move->cur.output.y;
1211 }
1212
1213 static void
1214 _mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1215 {
1216    Widget_Data *wd = elm_widget_data_get(data);
1217    Evas_Event_Mouse_Up *ev = event_info;
1218
1219    int mdevice;
1220    struct event_t *ev0;
1221    struct event_t *ev1 = NULL;
1222    
1223    ev0 = get_event_object(0);
1224    if(ev0 != NULL) {
1225       mdevice = get_multi_device();
1226       if(mdevice == 0) {
1227          if(ev0->hold_timer == NULL){
1228          }else{
1229             ecore_timer_del(ev0->hold_timer);
1230             ev0->hold_timer = NULL;
1231          }
1232       }else{
1233          ev1 = get_event_object(mdevice);
1234          if(ev1 != NULL){
1235             ev1->hold_timer = ecore_timer_add(0.35f, hold_timer_cb, ev1);
1236          }
1237       }
1238       destroy_event_object(ev0);
1239    }else{
1240       DBG("Cannot get event0");
1241    }
1242    if (!wd) return;
1243    if (ev->button != 1) return;
1244    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
1245    else wd->on_hold = EINA_FALSE;
1246    if (wd->long_timer){
1247       ecore_timer_del(wd->long_timer);
1248       wd->long_timer = NULL;
1249    }
1250    if (!wd->on_hold)
1251    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
1252    wd->on_hold = EINA_FALSE;
1253 }
1254
1255 static void
1256 _mouse_multi_down(void *data, Evas *evas, Evas_Object *obj, void *event_info)
1257 {
1258    Widget_Data *wd = elm_widget_data_get(data);
1259    struct event_t *ev;
1260    Evas_Event_Multi_Down *down = event_info;
1261
1262    ev = get_event_object(down->device);
1263    if(ev) return;
1264
1265    ev = create_event_object(obj, down->device);
1266    if(!ev){
1267       DBG("Failed : create_event_object");
1268       return;
1269    }
1270
1271    elm_smart_scroller_hold_set(wd->scr, 1);
1272    elm_smart_scroller_freeze_set(wd->scr, 1);
1273    elm_smart_scroller_freeze_momentum_animator_set(wd->scr, 1);
1274    elm_smart_scroller_freeze_bounce_animator_set(wd->scr, 1);
1275
1276    wd->pinch_zoom = EINA_FALSE;
1277
1278    ev->hold_timer = NULL;
1279    ev->prev.x = down->output.x;
1280    ev->prev.y = down->output.y;
1281 }
1282
1283 static void
1284 _mouse_multi_move(void *data, Evas *evas, Evas_Object *obj, void *event_info)
1285 {
1286    Widget_Data *wd = elm_widget_data_get(data);
1287    Evas_Event_Multi_Move *move = (Evas_Event_Multi_Move *)event_info;
1288    int dis_new;
1289    struct event_t *ev0;
1290    struct event_t *ev;
1291
1292    ev = get_event_object(move->device);
1293    if(ev == NULL) {
1294       DBG("Cannot get multi device");
1295       return;
1296    }
1297
1298    ev->prev.x = move->cur.output.x;
1299    ev->prev.y = move->cur.output.y;
1300
1301    ev0 = get_event_object(0);
1302    if(ev0 == NULL) {
1303       DBG("Cannot get device0");
1304       return;
1305    }
1306
1307    dis_new = get_distance(ev0->prev.x, ev0->prev.y, ev->prev.x, ev->prev.y);
1308    int zoom = wd->zoom;
1309    
1310    if(wd->pinch_zoom) return;
1311    if(dis_old != 0) {
1312       if(dis_old - dis_new > 0 && ev->pinch_dis > TOUCH_HOLD_RANGE){
1313          wd->pinch_zoom = EINA_TRUE;
1314          --zoom;
1315          elm_map_zoom_set(data, zoom);
1316          ev->pinch_dis = 0;
1317       }else if(dis_old - dis_new < 0 && ev->pinch_dis < -TOUCH_HOLD_RANGE){
1318          wd->pinch_zoom = EINA_TRUE;
1319          ++zoom;
1320          elm_map_zoom_set(data, zoom);
1321          ev->pinch_dis = 0;
1322       }
1323
1324       ev->pinch_dis += (dis_old - dis_new);
1325    }
1326    dis_old = dis_new;
1327 }
1328
1329 static void
1330 _mouse_multi_up(void *data, Evas *evas, Evas_Object *obj, void *event_info)
1331 {
1332    Widget_Data *wd = elm_widget_data_get(data);
1333    Evas_Event_Multi_Up *up = (Evas_Event_Multi_Up *)event_info;
1334    struct event_t *ev0;
1335    struct event_t *ev;
1336
1337    elm_smart_scroller_hold_set(wd->scr, 0);
1338    elm_smart_scroller_freeze_set(wd->scr, 0);
1339    elm_smart_scroller_freeze_momentum_animator_set(wd->scr, 0);
1340    elm_smart_scroller_freeze_bounce_animator_set(wd->scr, 0);
1341
1342    ev = get_event_object(up->device);
1343    if(ev == NULL){
1344       DBG("Cannot get multi device");
1345       return;
1346    }
1347
1348    wd->pinch_zoom = EINA_FALSE;
1349    dis_old = 0;
1350    
1351    ev0 = get_event_object(0);
1352    if(ev0){
1353       ev0->hold_timer = ecore_timer_add(0.35f, hold_timer_cb, ev0);
1354    }else{
1355       if(ev->hold_timer == NULL) {
1356       } else {
1357          ecore_timer_del(ev->hold_timer);
1358          ev->hold_timer = NULL;
1359       }   
1360    }
1361    destroy_event_object(ev);
1362 }
1363
1364 static Evas_Smart_Class _pan_sc = EVAS_SMART_CLASS_INIT_NULL;
1365
1366 static void
1367 _del_hook(Evas_Object *obj)
1368 {
1369    Elm_Map_Group_Class *group_clas;
1370    Elm_Map_Marker_Class *marker_clas;
1371    Widget_Data *wd = elm_widget_data_get(obj);
1372
1373    if (!wd) return;
1374
1375    EINA_LIST_FREE(wd->groups_clas, group_clas)
1376      {
1377         if (group_clas->style)
1378           eina_stringshare_del(group_clas->style);
1379         free(group_clas);
1380      }
1381
1382    EINA_LIST_FREE(wd->markers_clas, marker_clas)
1383      {
1384         if (marker_clas->style)
1385           eina_stringshare_del(marker_clas->style);
1386         free(marker_clas);
1387      }
1388
1389    if (wd->calc_job) ecore_job_del(wd->calc_job);
1390    if (wd->scr_timer) ecore_timer_del(wd->scr_timer);
1391    if (wd->zoom_animator) ecore_animator_del(wd->zoom_animator);
1392    if (wd->long_timer) ecore_timer_del(wd->long_timer);
1393
1394    if ((wd->api) && (wd->api->obj_unhook)) wd->api->obj_unhook(obj);
1395    free(wd);
1396 }
1397
1398 static void
1399 _del_pre_hook(Evas_Object *obj)
1400 {
1401    Marker_Group *group;
1402    Elm_Map_Marker *marker;
1403    int i;
1404    Eina_Bool free_marker = EINA_TRUE;
1405    Eina_List *l;
1406    Widget_Data *wd = elm_widget_data_get(obj);
1407    grid_clearall(obj);
1408
1409    if (!wd) return;
1410    for (i = 0; i < ZOOM_MAX + 1; i++)
1411      {
1412         if (!wd->markers[i]) continue;
1413         Eina_Iterator *it = eina_matrixsparse_iterator_new(wd->markers[i]);
1414         Eina_Matrixsparse_Cell *cell;
1415         
1416         EINA_ITERATOR_FOREACH(it, cell)
1417           {
1418              l =  eina_matrixsparse_cell_data_get(cell);
1419              EINA_LIST_FREE(l, group)
1420                {
1421                   EINA_LIST_FREE(group->markers, marker)
1422                     {
1423                        evas_object_event_callback_del_full(group->sc, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1424                                                            _bubble_sc_hits_changed_cb, group);
1425                        if (free_marker) free(marker);
1426                     }
1427                   free(group);
1428                }
1429              free_marker = EINA_FALSE;
1430           }
1431         eina_iterator_free(it);
1432         eina_matrixsparse_free(wd->markers[i]);
1433      }
1434
1435    evas_object_del(wd->sep_maps_markers);
1436    evas_object_del(wd->pan_smart);
1437    wd->pan_smart = NULL;
1438 }
1439
1440 static void
1441 _theme_hook(Evas_Object *obj)
1442 {
1443    Widget_Data *wd = elm_widget_data_get(obj);
1444    if (!wd) return;
1445    elm_smart_scroller_object_theme_set(obj, wd->scr, "map", "base", elm_widget_style_get(obj));
1446 //   edje_object_scale_set(wd->scr, elm_widget_scale_get(obj) * _elm_config->scale);
1447    _sizing_eval(obj);
1448 }
1449
1450 static void
1451 _sizing_eval(Evas_Object *obj)
1452 {
1453    Widget_Data *wd = elm_widget_data_get(obj);
1454    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
1455    if (!wd) return;
1456    evas_object_size_hint_max_get(wd->scr, &maxw, &maxh);
1457    evas_object_size_hint_min_set(obj, minw, minh);
1458    evas_object_size_hint_max_set(obj, maxw, maxh);
1459 }
1460
1461 static void
1462 _calc_job(void *data)
1463 {
1464    Widget_Data *wd = data;
1465    Evas_Coord minw, minh;
1466
1467    minw = wd->size.w;
1468    minh = wd->size.h;
1469    if (wd->resized)
1470      {
1471         wd->resized = 0;
1472         if (wd->mode != ELM_MAP_ZOOM_MODE_MANUAL)
1473           {
1474              double tz = wd->zoom;
1475              wd->zoom = 0.0;
1476              elm_map_zoom_set(wd->obj, tz);
1477           }
1478      }
1479    if ((minw != wd->minw) || (minh != wd->minh))
1480      {
1481         wd->minw = minw;
1482         wd->minh = minh;
1483         evas_object_smart_callback_call(wd->pan_smart, SIG_CHANGED, NULL);
1484         _sizing_eval(wd->obj);
1485      }
1486    wd->calc_job = NULL;
1487    evas_object_smart_changed(wd->pan_smart);
1488 }
1489
1490 static void
1491 _pan_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
1492 {
1493    Pan *sd = evas_object_smart_data_get(obj);
1494    if (!sd) return;
1495    if ((x == sd->wd->pan_x) && (y == sd->wd->pan_y)) return;
1496    sd->wd->pan_x = x;
1497    sd->wd->pan_y = y;
1498    evas_object_smart_changed(obj);
1499 }
1500
1501 static void
1502 _pan_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1503 {
1504    Pan *sd = evas_object_smart_data_get(obj);
1505    if (!sd) return;
1506    if (x) *x = sd->wd->pan_x;
1507    if (y) *y = sd->wd->pan_y;
1508 }
1509
1510 static void
1511 _pan_max_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1512 {
1513    Pan *sd = evas_object_smart_data_get(obj);
1514    Evas_Coord ow, oh;
1515    if (!sd) return;
1516    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
1517    ow = sd->wd->minw - ow;
1518    if (ow < 0) ow = 0;
1519    oh = sd->wd->minh - oh;
1520    if (oh < 0) oh = 0;
1521    if (x) *x = ow;
1522    if (y) *y = oh;
1523 }
1524
1525 static void
1526 _pan_child_size_get(Evas_Object *obj, Evas_Coord *w, Evas_Coord *h)
1527 {
1528    Pan *sd = evas_object_smart_data_get(obj);
1529    if (!sd) return;
1530    if (w) *w = sd->wd->minw;
1531    if (h) *h = sd->wd->minh;
1532 }
1533
1534 static void
1535 _pan_add(Evas_Object *obj)
1536 {
1537    Pan *sd;
1538    Evas_Object_Smart_Clipped_Data *cd;
1539    _pan_sc.add(obj);
1540    cd = evas_object_smart_data_get(obj);
1541    if (!cd) return;
1542    sd = calloc(1, sizeof(Pan));
1543    if (!sd) return;
1544    sd->__clipped_data = *cd;
1545    free(cd);
1546    evas_object_smart_data_set(obj, sd);
1547 }
1548
1549 static void
1550 _pan_del(Evas_Object *obj)
1551 {
1552    Pan *sd = evas_object_smart_data_get(obj);
1553    if (!sd) return;
1554    _pan_sc.del(obj);
1555 }
1556
1557 static void
1558 _pan_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
1559 {
1560    Pan *sd = evas_object_smart_data_get(obj);
1561    Evas_Coord ow, oh;
1562    if (!sd) return;
1563    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
1564    if ((ow == w) && (oh == h)) return;
1565    sd->wd->resized = 1;
1566    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
1567    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
1568 }
1569
1570 static void
1571 _pan_calculate(Evas_Object *obj)
1572 {
1573    Pan *sd = evas_object_smart_data_get(obj);
1574    Evas_Coord ox, oy, ow, oh;
1575    Eina_List *l;
1576    Grid *g;
1577    if (!sd) return;
1578    evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
1579    rect_place(sd->wd->obj, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
1580    EINA_LIST_FOREACH(sd->wd->grids, l, g)
1581      {
1582         grid_load(sd->wd->obj, g);
1583         grid_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
1584         marker_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
1585      }
1586 }
1587
1588 static void
1589 _pan_move(Evas_Object *obj, Evas_Coord x __UNUSED__, Evas_Coord y __UNUSED__)
1590 {
1591    Pan *sd = evas_object_smart_data_get(obj);
1592    if (!sd) return;
1593    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
1594    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
1595 }
1596
1597 static void
1598 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
1599 {
1600    Widget_Data *wd = elm_widget_data_get(obj);
1601    if (!wd) return;
1602    elm_smart_scroller_hold_set(wd->scr, 1);
1603 }
1604
1605 static void
1606 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
1607 {
1608    Widget_Data *wd = elm_widget_data_get(obj);
1609    if (!wd) return;
1610    elm_smart_scroller_hold_set(wd->scr, 0);
1611 }
1612
1613 static void
1614 _freeze_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
1615 {
1616    Widget_Data *wd = elm_widget_data_get(obj);
1617    if (!wd) return;
1618    elm_smart_scroller_freeze_set(wd->scr, 1);
1619 }
1620
1621 static void
1622 _freeze_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
1623 {
1624    Widget_Data *wd = elm_widget_data_get(obj);
1625    if (!wd) return;
1626    elm_smart_scroller_freeze_set(wd->scr, 0);
1627 }
1628
1629 static void
1630 _scr_anim_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1631 {
1632    evas_object_smart_callback_call(data, "scroll,anim,start", NULL);
1633 }
1634
1635 static void
1636 _scr_anim_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1637 {
1638    evas_object_smart_callback_call(data, "scroll,anim,stop", NULL);
1639 }
1640
1641 static void
1642 _scr_drag_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1643 {
1644    Widget_Data *wd = elm_widget_data_get(data);
1645    wd->center_on.enabled = EINA_FALSE;
1646    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_START, NULL);
1647 }
1648
1649 static void
1650 _scr_drag_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1651 {
1652    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_STOP, NULL);
1653 }
1654
1655 static void
1656 _scr_scroll(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1657 {
1658    evas_object_smart_callback_call(data, SIG_SCROLL, NULL);
1659 }
1660
1661
1662 static void
1663 _group_object_create(Marker_Group *group)
1664 {
1665    const char *style = "radio";
1666    Evas_Object *icon = NULL;
1667
1668    if (group->obj) return;
1669    if (!group->clas->priv.objs_notused || eina_list_count(group->markers) == 1)
1670      {
1671         //set icon and style
1672         if (eina_list_count(group->markers) == 1)
1673           {
1674              Elm_Map_Marker *m = eina_list_data_get(group->markers);
1675              if (m->clas->style)
1676                style = m->clas->style;
1677              
1678              if (m->clas->func.icon_get)
1679                icon = m->clas->func.icon_get(group->wd->obj, m, m->data);
1680              
1681              group->delete_object = EINA_TRUE;
1682           }
1683         else
1684           {
1685              if (group->clas->style)
1686                style = group->clas->style;
1687              
1688              if (group->clas->func.icon_get)
1689                icon = group->clas->func.icon_get(group->wd->obj, group->clas->data);
1690              
1691              group->delete_object = EINA_FALSE;
1692           }
1693         
1694         group->obj = elm_layout_add(group->wd->obj);
1695         elm_layout_theme_set(group->obj, "map/marker", style, elm_widget_style_get(group->wd->obj));
1696
1697         if (icon) elm_layout_content_set(group->obj, "elm.icon", icon);
1698         
1699         evas_object_smart_member_add(group->obj, group->wd->pan_smart);
1700         elm_widget_sub_object_add(group->wd->obj, group->obj);
1701         evas_object_stack_above(group->obj, group->wd->sep_maps_markers);
1702
1703         if (!group->delete_object)
1704           group->clas->priv.objs_used = eina_list_append(group->clas->priv.objs_used, group->obj);
1705      }
1706    else
1707      {
1708         group->delete_object = EINA_FALSE;
1709         
1710         group->obj = eina_list_data_get(group->clas->priv.objs_notused);
1711         group->clas->priv.objs_used = eina_list_append(group->clas->priv.objs_used, group->obj);
1712         group->clas->priv.objs_notused = eina_list_remove(group->clas->priv.objs_notused, group->obj);
1713         evas_object_show(group->obj);
1714      }
1715
1716    edje_object_signal_callback_add(elm_layout_edje_get(group->obj), "open", "elm", _group_open_cb, group);
1717    edje_object_signal_callback_add(elm_layout_edje_get(group->obj), "bringin", "elm", _group_bringin_cb, group);
1718
1719    group->update_nbelems = EINA_TRUE;
1720    group->update_resize = EINA_TRUE;
1721    group->update_raise = EINA_TRUE;
1722
1723    if (group->open) _group_bubble_create(group);
1724 }
1725
1726 static void
1727 _group_object_free(Marker_Group *group)
1728 {
1729    if (!group->obj) return;
1730    if (!group->delete_object)
1731      {
1732         group->clas->priv.objs_notused = eina_list_append(group->clas->priv.objs_notused, group->obj);
1733         group->clas->priv.objs_used = eina_list_remove(group->clas->priv.objs_used, group->obj);
1734         evas_object_hide(group->obj);
1735
1736         edje_object_signal_callback_del(elm_layout_edje_get(group->obj), "open", "elm", _group_open_cb);
1737         edje_object_signal_callback_del(elm_layout_edje_get(group->obj), "bringin", "elm", _group_bringin_cb);
1738      }
1739    else
1740      evas_object_del(group->obj);
1741
1742    group->obj = NULL;
1743    _group_bubble_free(group);
1744 }
1745
1746 static void
1747 _group_bubble_mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1748 {
1749    Marker_Group *group = data;
1750
1751    if (!evas_object_above_get(group->rect)) return;
1752    evas_object_raise(group->bubble);
1753    evas_object_raise(group->sc);
1754    evas_object_raise(group->rect);
1755 }
1756
1757 static void
1758 _group_bubble_create(Marker_Group *group)
1759 {
1760    if (group->bubble) return;
1761
1762    group->wd->opened_bubbles = eina_list_append(group->wd->opened_bubbles, group);
1763    group->bubble = edje_object_add(evas_object_evas_get(group->obj));
1764    _elm_theme_object_set(group->wd->obj, group->bubble, "map", "marker_bubble",
1765                          elm_widget_style_get(group->wd->obj));
1766    evas_object_smart_member_add(group->bubble,
1767                                 group->wd->obj);
1768    elm_widget_sub_object_add(group->wd->obj, group->bubble);
1769    
1770    _group_bubble_content_free(group);
1771    if (!_group_bubble_content_update(group))
1772      {
1773         //no content, we can delete the bubble
1774         _group_bubble_free(group);
1775         return;
1776      }
1777    
1778    group->rect = evas_object_rectangle_add(evas_object_evas_get(group->obj));
1779    evas_object_color_set(group->rect, 0, 0, 0, 0);
1780    evas_object_repeat_events_set(group->rect, EINA_TRUE);
1781    evas_object_smart_member_add(group->rect, group->wd->obj);
1782    elm_widget_sub_object_add(group->wd->obj, group->rect);
1783    
1784    evas_object_event_callback_add(group->rect, EVAS_CALLBACK_MOUSE_UP, _group_bubble_mouse_up_cb, group);
1785    
1786    _group_bubble_place(group);
1787 }
1788
1789 static void _bubble_sc_hits_changed_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1790 {
1791    _group_bubble_place(data);
1792 }
1793
1794 static int
1795 _group_bubble_content_update(Marker_Group *group)
1796 {
1797    Eina_List *l;
1798    Elm_Map_Marker *marker;
1799    int i = 0;
1800
1801    if (!group->bubble) return 1;
1802
1803    if (!group->sc)
1804      {
1805         group->sc = elm_scroller_add(group->bubble);
1806         elm_scroller_content_min_limit(group->sc, EINA_FALSE, EINA_TRUE);
1807         elm_scroller_policy_set(group->sc, ELM_SCROLLER_POLICY_AUTO, ELM_SCROLLER_POLICY_OFF);
1808         elm_scroller_bounce_set(group->sc, EINA_TRUE, EINA_FALSE);
1809         edje_object_part_swallow(group->bubble, "elm.swallow.content", group->sc);
1810         evas_object_show(group->sc);
1811         evas_object_smart_member_add(group->sc,
1812                                      group->wd->obj);
1813         elm_widget_sub_object_add(group->wd->obj, group->sc);
1814         
1815         group->bx = elm_box_add(group->bubble);
1816         evas_object_size_hint_align_set(group->bx, EVAS_HINT_FILL, EVAS_HINT_FILL);
1817         evas_object_size_hint_weight_set(group->bx, 0.5, 0.5);
1818         elm_box_horizontal_set(group->bx, EINA_TRUE);
1819         evas_object_show(group->bx);
1820         
1821         elm_scroller_content_set(group->sc, group->bx);
1822         
1823         evas_object_event_callback_add(group->sc, EVAS_CALLBACK_RESIZE,
1824                                        _bubble_sc_hits_changed_cb, group);
1825      }
1826    
1827    EINA_LIST_FOREACH(group->markers, l, marker)
1828      {
1829         if (i >= group->wd->markers_max_num) break;
1830         if (!marker->content && marker->clas->func.get)
1831           marker->content = marker->clas->func.get(group->wd->obj, marker, marker->data);
1832         else if (marker->content)
1833           elm_box_unpack(group->bx, marker->content);
1834         if (marker->content)
1835           {
1836              elm_box_pack_end(group->bx, marker->content);
1837              i++;
1838           }
1839      }
1840    return i;
1841 }
1842
1843 static void
1844 _group_bubble_content_free(Marker_Group *group)
1845 {
1846    Eina_List *l;
1847    Elm_Map_Marker *marker;
1848
1849    if (!group->sc) return;
1850    EINA_LIST_FOREACH(group->markers, l, marker)
1851      {
1852         if (marker->content && marker->clas->func.del)
1853           marker->clas->func.del(group->wd->obj, marker, marker->data, marker->content);
1854         else if (marker->content)
1855           evas_object_del(marker->content);
1856         marker->content = NULL;
1857      }
1858    evas_object_del(group->sc);
1859    group->sc = NULL;
1860 }
1861
1862 static void
1863 _group_bubble_free(Marker_Group *group)
1864 {
1865    if (!group->bubble) return;
1866    group->wd->opened_bubbles = eina_list_remove(group->wd->opened_bubbles, group);
1867    evas_object_event_callback_del_full(group->sc, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1868                                        _bubble_sc_hits_changed_cb, group);
1869    evas_object_del(group->bubble);
1870    evas_object_del(group->rect);
1871    group->bubble = NULL;
1872    _group_bubble_content_free(group);
1873 }
1874
1875 static void
1876 _group_bubble_place(Marker_Group *group)
1877 {
1878    Evas_Coord x, y, w;
1879    Evas_Coord xx, yy, ww, hh;
1880    const char *s;
1881
1882    if (!group->bubble || !group->obj) return;
1883
1884    evas_object_geometry_get(group->obj, &x, &y, &w, NULL);
1885    edje_object_size_min_calc(group->bubble, NULL, &hh);
1886
1887    s = edje_object_data_get(group->bubble, "size_w");
1888    ww = atoi(s);
1889    xx = x + w / 2 - ww / 2;
1890    yy = y-hh;
1891
1892    evas_object_move(group->bubble, xx, yy);
1893    evas_object_resize(group->bubble, ww, hh);
1894    evas_object_show(group->bubble);
1895
1896    evas_object_move(group->rect, xx, yy);
1897    evas_object_resize(group->rect, ww, hh);
1898    evas_object_show(group->rect);
1899 }
1900
1901 static void
1902 _group_bringin_cb(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *soure __UNUSED__)
1903 {
1904    Marker_Group *group = data;
1905    Elm_Map_Marker *marker = eina_list_data_get(group->markers);
1906    if (!marker) return;
1907    group->bringin = EINA_TRUE;
1908    elm_map_geo_region_bring_in(group->wd->obj, marker->longitude, marker->latitude);
1909 }
1910
1911 static void
1912 _group_open_cb(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *soure __UNUSED__)
1913 {
1914    Marker_Group *group = data;
1915
1916    if (group->bringin)
1917      {
1918         group->bringin = EINA_FALSE;
1919         return;
1920      }
1921
1922    if (group->bubble)
1923      {
1924         group->open = EINA_FALSE;
1925         _group_bubble_free(group);
1926         return;
1927      }
1928    group->open = EINA_TRUE;
1929    _group_bubble_create(group);
1930 }
1931
1932 static int idnum = 1;
1933
1934 /**
1935  * Add a new Map object
1936  *
1937  * @param parent The parent object
1938  * @return The new object or NULL if it cannot be created
1939  *
1940  * @ingroup Map
1941  */
1942 EAPI Evas_Object *
1943 elm_map_add(Evas_Object *parent)
1944 {
1945    Evas *e;
1946    Widget_Data *wd;
1947    Evas_Coord minw, minh;
1948    Evas_Object *obj;
1949    Eina_Bool ret = EINA_FALSE;
1950    int idx;
1951    static Evas_Smart *smart = NULL;
1952
1953    if (!ecore_file_download_protocol_available("http://"))
1954      {
1955         ERR("Ecore must be built with the support of HTTP for the widget map !");
1956         return NULL;
1957      }
1958
1959    wd = ELM_NEW(Widget_Data);
1960    wd->api = NULL;
1961    e = evas_object_evas_get(parent);
1962    obj = elm_widget_add(e);
1963    ELM_SET_WIDTYPE(widtype, "map");
1964    elm_widget_type_set(obj, "map");
1965    elm_widget_sub_object_add(parent, obj);
1966    elm_widget_data_set(obj, wd);
1967    elm_widget_del_hook_set(obj, _del_hook);
1968    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1969    elm_widget_theme_hook_set(obj, _theme_hook);
1970
1971    wd->scr = elm_smart_scroller_add(e);
1972    elm_smart_scroller_widget_set(wd->scr, obj);
1973    elm_smart_scroller_object_theme_set(obj, wd->scr, "map", "base", "default");
1974    evas_object_smart_callback_add(wd->scr, "scroll", _scr, obj);
1975    evas_object_smart_callback_add(wd->scr, "drag", _scr, obj);
1976    elm_widget_resize_object_set(obj, wd->scr);
1977
1978    evas_object_smart_callback_add(wd->scr, "animate,start", _scr_anim_start, obj);
1979    evas_object_smart_callback_add(wd->scr, "animate,stop", _scr_anim_stop, obj);
1980    evas_object_smart_callback_add(wd->scr, "drag,start", _scr_drag_start, obj);
1981    evas_object_smart_callback_add(wd->scr, "drag,stop", _scr_drag_stop, obj);
1982    evas_object_smart_callback_add(wd->scr, "scroll", _scr_scroll, obj);
1983
1984    elm_smart_scroller_bounce_allow_set(wd->scr, EINA_TRUE, EINA_TRUE);
1985
1986    wd->obj = obj;
1987
1988    wd->markers_max_num = 30;
1989    wd->source = ELM_MAP_SOURCE_MAPNIK;
1990
1991    for(idx=ELM_MAP_SOURCE_MAPNIK; idx<=ELM_MAP_SOURCE_CUSTOM_6; idx++)
1992    if(map_sources_tab[idx].use_module == EINA_TRUE){
1993       if(!wd->api){
1994          wd->api = module(obj);
1995          if ((wd->api) && (wd->api->obj_hook)) ret = wd->api->obj_hook(obj);
1996          if (!ret) DBG("Failed : loading module [%s]", elm_map_source_name_get(idx));
1997       }
1998    }
1999
2000    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
2001    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
2002    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
2003    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
2004
2005    if (!smart)
2006      {
2007         static Evas_Smart_Class sc;
2008
2009         evas_object_smart_clipped_smart_set(&_pan_sc);
2010         sc = _pan_sc;
2011         sc.name = "elm_map_pan";
2012         sc.version = EVAS_SMART_CLASS_VERSION;
2013         sc.add = _pan_add;
2014         sc.del = _pan_del;
2015         sc.resize = _pan_resize;
2016         sc.move = _pan_move;
2017         sc.calculate = _pan_calculate;
2018         smart = evas_smart_class_new(&sc);
2019      }
2020    if (smart)
2021      {
2022         wd->pan_smart = evas_object_smart_add(e, smart);
2023         wd->pan = evas_object_smart_data_get(wd->pan_smart);
2024         wd->pan->wd = wd;
2025      }
2026
2027    elm_smart_scroller_extern_pan_set(wd->scr, wd->pan_smart,
2028          _pan_set, _pan_get,
2029          _pan_max_get, _pan_child_size_get);
2030
2031    wd->rect = evas_object_rectangle_add(e);
2032    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_DOWN,
2033          _mouse_down, obj);
2034    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_UP,
2035          _mouse_up, obj);
2036    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MOUSE_MOVE,
2037          _mouse_move, obj);
2038    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MULTI_DOWN, 
2039          _mouse_multi_down, obj);
2040    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MULTI_MOVE,
2041          _mouse_multi_move, obj);
2042    evas_object_event_callback_add(wd->rect, EVAS_CALLBACK_MULTI_UP,
2043          _mouse_multi_up, obj);
2044
2045    evas_object_smart_member_add(wd->rect, wd->pan_smart);
2046    elm_widget_sub_object_add(obj, wd->rect);
2047    evas_object_show(wd->rect);
2048    evas_object_color_set(wd->rect, 0, 0, 0, 0);
2049
2050    wd->zoom = -1;
2051    wd->mode = ELM_MAP_ZOOM_MODE_MANUAL;
2052    wd->id = ((int)getpid() << 16) | idnum;
2053    idnum++;
2054
2055    wd->tsize = 256;
2056
2057    edje_object_size_min_calc(elm_smart_scroller_edje_object_get(wd->scr),
2058          &minw, &minh);
2059    evas_object_size_hint_min_set(obj, minw, minh);
2060
2061    wd->paused = EINA_TRUE;
2062    elm_map_zoom_set(obj, 0);
2063    wd->paused = EINA_FALSE;
2064
2065    _sizing_eval(obj);
2066
2067    wd->calc_job = ecore_job_add(_calc_job, wd);
2068
2069    wd->sep_maps_markers = evas_object_rectangle_add(evas_object_evas_get(obj));
2070    evas_object_smart_member_add(wd->sep_maps_markers, wd->pan_smart);
2071
2072    // TODO: convert Elementary to subclassing of Evas_Smart_Class
2073    // TODO: and save some bytes, making descriptions per-class and not instance!
2074    evas_object_smart_callbacks_descriptions_set(obj, _signals);
2075    return obj;
2076 }
2077
2078 /**
2079  * Set the zoom level of the map
2080  *
2081  * This sets the zoom level. 0 is the world map and 18 is the maximum zoom.
2082  *
2083  * @param obj The map object
2084  * @param zoom The zoom level to set
2085  *
2086  * @ingroup Map
2087  */
2088 EAPI void
2089 elm_map_zoom_set(Evas_Object *obj, int zoom)
2090 {
2091    ELM_CHECK_WIDTYPE(obj, widtype);
2092    Widget_Data *wd = elm_widget_data_get(obj);
2093    Eina_List *l;
2094    Grid *g, *g_zoom = NULL;
2095    Evas_Coord rx, ry, rw, rh;
2096    int z;
2097    int zoom_changed = 0, started = 0;
2098    
2099    if (!wd) return;
2100    if (zoom < 0 ) zoom = 0;
2101    if (zoom > map_sources_tab[wd->source].zoom_max)
2102      zoom = map_sources_tab[wd->source].zoom_max;
2103    if (zoom < map_sources_tab[wd->source].zoom_min)
2104      zoom = map_sources_tab[wd->source].zoom_min;
2105    if (zoom == wd->zoom) return;
2106    if (wd->zoom_animator) return;
2107
2108    wd->zoom = zoom;
2109    wd->size.ow = wd->size.w;
2110    wd->size.oh = wd->size.h;
2111    elm_smart_scroller_child_pos_get(wd->scr, &rx, &ry);
2112    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
2113
2114    if (wd->mode == ELM_MAP_ZOOM_MODE_MANUAL)
2115      {
2116         wd->size.nw = pow(2.0, wd->zoom) * wd->tsize;
2117         wd->size.nh = pow(2.0, wd->zoom) * wd->tsize;
2118      }
2119    else if (wd->mode == ELM_MAP_ZOOM_MODE_AUTO_FIT)
2120      {
2121         int p2w, p2h;
2122         int cumulw, cumulh;
2123
2124         cumulw = wd->tsize;
2125         p2w = 0;
2126         while (cumulw <= rw)
2127           {
2128              p2w++;
2129              cumulw *= 2;
2130           }
2131         p2w--;
2132
2133         cumulh = wd->tsize;
2134         p2h = 0;
2135         while (cumulh <= rh)
2136           {
2137              p2h++;
2138              cumulh *= 2;
2139           }
2140         p2h--;
2141         
2142         if (p2w < p2h)
2143           z = p2w;
2144         else
2145           z = p2h;
2146         
2147         wd->zoom = z;
2148         wd->size.nw = pow(2.0, wd->zoom) * wd->tsize;
2149         wd->size.nh = pow(2.0, wd->zoom) * wd->tsize;
2150      }
2151    else if (wd->mode == ELM_MAP_ZOOM_MODE_AUTO_FILL)
2152      {
2153         int p2w, p2h;
2154         int cumulw, cumulh;
2155         
2156         cumulw = wd->tsize;
2157         p2w = 0;
2158         while (cumulw <= rw)
2159           {
2160              p2w++;
2161              cumulw *= 2;
2162           }
2163         p2w--;
2164
2165         cumulh = wd->tsize;
2166         p2h = 0;
2167         while (cumulh <= rh)
2168           {
2169              p2h++;
2170              cumulh *= 2;
2171           }
2172         p2h--;
2173
2174         if (p2w > p2h)
2175           z = p2w;
2176         else
2177           z = p2h;
2178         
2179         wd->zoom = z;
2180         wd->size.nw = pow(2.0, wd->zoom) * wd->tsize;
2181         wd->size.nh = pow(2.0, wd->zoom) * wd->tsize;
2182      }
2183
2184    EINA_LIST_FOREACH(wd->grids, l, g)
2185      {
2186         if (g->zoom == wd->zoom)
2187           {
2188              wd->grids = eina_list_remove(wd->grids, g);
2189              wd->grids = eina_list_prepend(wd->grids, g);
2190              _grid_raise(g);
2191              goto done;
2192           }
2193      }
2194    g = grid_create(obj);
2195    if (g)
2196      {
2197         if (eina_list_count(wd->grids) > 1)
2198           {
2199              g_zoom = eina_list_last(wd->grids)->data;
2200              wd->grids = eina_list_remove(wd->grids, g_zoom);
2201              grid_clear(obj, g_zoom);
2202              free(g_zoom);
2203           }
2204         wd->grids = eina_list_prepend(wd->grids, g);
2205      }
2206    else
2207      {
2208         EINA_LIST_FREE(wd->grids, g)
2209           {
2210              grid_clear(obj, g);
2211              free(g);
2212           }
2213      }
2214 done:
2215
2216    wd->t_start = ecore_loop_time_get();
2217    wd->t_end = wd->t_start + _elm_config->zoom_friction;
2218    if ((wd->size.w > 0) && (wd->size.h > 0))
2219      {
2220         wd->size.spos.x = (double)(rx + (rw / 2)) / (double)wd->size.ow;
2221         wd->size.spos.y = (double)(ry + (rh / 2)) / (double)wd->size.oh;
2222      }
2223    else
2224      {
2225         wd->size.spos.x = 0.5;
2226         wd->size.spos.y = 0.5;
2227      }
2228    if (rw > wd->size.ow) wd->size.spos.x = 0.5;
2229    if (rh > wd->size.oh) wd->size.spos.y = 0.5;
2230    if (wd->size.spos.x > 1.0) wd->size.spos.x = 1.0;
2231    if (wd->size.spos.y > 1.0) wd->size.spos.y = 1.0;
2232    if (wd->paused)
2233      {
2234         zoom_do(obj, 1.0);
2235      }
2236    else
2237      {
2238         if (!wd->zoom_animator)
2239           {
2240              wd->zoom_animator = ecore_animator_add(_zoom_anim, obj);
2241              wd->nosmooth++;
2242              if (wd->nosmooth == 1) _smooth_update(obj);
2243              started = 1;
2244           }
2245      }
2246    if (wd->zoom_animator)
2247      {
2248         if (!_zoom_anim(obj))
2249           {
2250              ecore_animator_del(wd->zoom_animator);
2251              wd->zoom_animator = NULL;
2252           }
2253      }
2254    if (wd->calc_job) ecore_job_del(wd->calc_job);
2255    wd->calc_job = ecore_job_add(_calc_job, wd);
2256    if (!wd->paused)
2257      {
2258         if (started)
2259           evas_object_smart_callback_call(obj, SIG_ZOOM_START, NULL);
2260         if (!wd->zoom_animator)
2261           evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
2262      }
2263
2264    if (zoom_changed)
2265      evas_object_smart_callback_call(obj, SIG_ZOOM_CHANGE, NULL);
2266 }
2267
2268 /**
2269  * Get the zoom level of the photo
2270  *
2271  * This returns the current zoom level of the map object. Note that if
2272  * you set the fill mode to other than ELM_MAP_ZOOM_MODE_MANUAL
2273  * (which is the default), the zoom level may be changed at any time by the
2274  * map object itself to account for map size and map viewpoer size
2275  *
2276  * @param obj The map object
2277  * @return The current zoom level
2278  *
2279  * @ingroup Map
2280  */
2281 EAPI double
2282 elm_map_zoom_get(const Evas_Object *obj)
2283 {
2284    ELM_CHECK_WIDTYPE(obj, widtype) 1.0;
2285    Widget_Data *wd = elm_widget_data_get(obj);
2286    if (!wd) return 1.0;
2287    return wd->zoom;
2288 }
2289
2290 /**
2291  * Set the zoom mode
2292  *
2293  * This sets the zoom mode to manual or one of several automatic levels.
2294  * Manual (ELM_MAP_ZOOM_MODE_MANUAL) means that zoom is set manually by
2295  * elm_map_zoom_set() and will stay at that level until changed by code
2296  * or until zoom mode is changed. This is the default mode.
2297  * The Automatic modes will allow the map object to automatically
2298  * adjust zoom mode based on properties. ELM_MAP_ZOOM_MODE_AUTO_FIT) will
2299  * adjust zoom so the photo fits inside the scroll frame with no pixels
2300  * outside this area. ELM_MAP_ZOOM_MODE_AUTO_FILL will be similar but
2301  * 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.
2302  *
2303  * @param obj The map object
2304  * @param mode The desired mode
2305  *
2306  * @ingroup Map
2307  */
2308 EAPI void
2309 elm_map_zoom_mode_set(Evas_Object *obj, Elm_Map_Zoom_Mode mode)
2310 {
2311    ELM_CHECK_WIDTYPE(obj, widtype);
2312    Widget_Data *wd = elm_widget_data_get(obj);
2313    if (!wd) return;
2314    if (wd->mode == mode) return;
2315    wd->mode = mode;
2316      {
2317         double tz = wd->zoom;
2318         wd->zoom = 0.0;
2319         elm_map_zoom_set(wd->obj, tz);
2320      }
2321 }
2322
2323 /**
2324  * Get the zoom mode
2325  *
2326  * This gets the current zoom mode of the map object
2327  *
2328  * @param obj The map object
2329  * @return The current zoom mode
2330  *
2331  * @ingroup Map
2332  */
2333 EAPI Elm_Map_Zoom_Mode
2334 elm_map_zoom_mode_get(const Evas_Object *obj)
2335 {
2336    ELM_CHECK_WIDTYPE(obj, widtype) ELM_MAP_ZOOM_MODE_MANUAL;
2337    Widget_Data *wd = elm_widget_data_get(obj);
2338    if (!wd) return ELM_MAP_ZOOM_MODE_MANUAL;
2339    return wd->mode;
2340 }
2341
2342 /**
2343  * Centers the map at @p lon @p lat using an animation to scroll.
2344  *
2345  * @param obj The map object
2346  * @param lon Longitude to center at
2347  * @param lon Latitude to center at
2348  *
2349  * @ingroup Map
2350  */
2351 EAPI void
2352 elm_map_geo_region_bring_in(Evas_Object *obj, double lon, double lat)
2353 {
2354    ELM_CHECK_WIDTYPE(obj, widtype);
2355    Widget_Data *wd = elm_widget_data_get(obj);
2356    int rx, ry, rw, rh;
2357
2358    if (!wd) return;
2359    elm_map_utils_convert_geo_into_coord(obj, lon, lat, wd->size.w, &rx, &ry);
2360    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
2361
2362    rx = rx - rw / 2;
2363    ry = ry - rh / 2;
2364
2365    if (wd->zoom_animator)
2366      {
2367         wd->nosmooth--;
2368         if (wd->nosmooth == 0) _smooth_update(obj);
2369         ecore_animator_del(wd->zoom_animator);
2370         wd->zoom_animator = NULL;
2371         zoom_do(obj, 1.0);
2372         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
2373      }
2374    elm_smart_scroller_region_bring_in(wd->scr, rx, ry, rw, rh);
2375
2376    wd->center_on.enabled = EINA_TRUE;
2377    wd->center_on.lon = lon;
2378    wd->center_on.lat = lat;
2379 }
2380
2381 /**
2382  * Move the map to the current coordinates.
2383  *
2384  * This move the map to the current coordinates. The map will be centered on these coordinates.
2385  *
2386  * @param obj The map object
2387  * @param lat The latitude.
2388  * @param lon The longitude.
2389  *
2390  * @ingroup Map
2391  */
2392 EAPI void
2393 elm_map_geo_region_show(Evas_Object *obj, double lon, double lat)
2394 {
2395    ELM_CHECK_WIDTYPE(obj, widtype);
2396    Widget_Data *wd = elm_widget_data_get(obj);
2397    int rx, ry, rw, rh;
2398
2399    if (!wd) return;
2400    elm_map_utils_convert_geo_into_coord(obj, lon, lat, wd->size.w, &rx, &ry);
2401    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
2402
2403    rx = rx - rw / 2;
2404    ry = ry - rh / 2;
2405
2406    if (wd->zoom_animator)
2407      {
2408         wd->nosmooth--;
2409         ecore_animator_del(wd->zoom_animator);
2410         wd->zoom_animator = NULL;
2411         zoom_do(obj, 1.0);
2412         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
2413      }
2414    elm_smart_scroller_child_region_show(wd->scr, rx, ry, rw, rh);
2415
2416    wd->center_on.enabled = EINA_TRUE;
2417    wd->center_on.lon = lon;
2418    wd->center_on.lat = lat;
2419 }
2420
2421 /**
2422  * Get the current coordinates of the map.
2423  *
2424  * This gets the current coordinates of the map object.
2425  *
2426  * @param obj The map object
2427  * @param lat The latitude.
2428  * @param lon The longitude.
2429  *
2430  * @ingroup Map
2431  */
2432 EAPI void
2433 elm_map_geo_region_get(const Evas_Object *obj, double *lon, double *lat)
2434 {
2435    ELM_CHECK_WIDTYPE(obj, widtype);
2436    Widget_Data *wd = elm_widget_data_get(obj);
2437    Evas_Coord sx, sy, sw, sh;
2438
2439    if (!wd) return;
2440    elm_smart_scroller_child_pos_get(wd->scr, &sx, &sy);
2441    elm_smart_scroller_child_viewport_size_get(wd->scr, &sw, &sh);
2442    sx += sw / 2;
2443    sy += sh / 2;
2444
2445    elm_map_utils_convert_coord_into_geo(obj, sx, sy, wd->size.w, lon, lat);
2446 }
2447
2448 /**
2449  * Set the paused state for map
2450  *
2451  * This sets the paused state to on (1) or off (0) for map. The default
2452  * is off. This will stop zooming using animation change zoom levels and
2453  * change instantly. This will stop any existing animations that are running.
2454  *
2455  * @param obj The map object
2456  * @param paused The pause state to set
2457  *
2458  * @ingroup Map
2459  */
2460 EAPI void
2461 elm_map_paused_set(Evas_Object *obj, Eina_Bool paused)
2462 {
2463    ELM_CHECK_WIDTYPE(obj, widtype);
2464    Widget_Data *wd = elm_widget_data_get(obj);
2465    if (!wd) return;
2466    if (wd->paused == !!paused) return;
2467    wd->paused = paused;
2468    if (wd->paused)
2469      {
2470         if (wd->zoom_animator)
2471           {
2472              ecore_animator_del(wd->zoom_animator);
2473              wd->zoom_animator = NULL;
2474              zoom_do(obj, 1.0);
2475              evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
2476           }
2477      }
2478 }
2479
2480 /**
2481  * Set the paused state for the markers
2482  *
2483  * This sets the paused state to on (1) or off (0) for the markers. The default
2484  * is off. This will stop displaying the markers during change zoom levels. Set
2485  * to on if you have a large number of markers.
2486  *
2487  * @param obj The map object
2488  * @param paused The pause state to set
2489  *
2490  * @ingroup Map
2491  */
2492 EAPI void
2493 elm_map_paused_markers_set(Evas_Object *obj, Eina_Bool paused)
2494 {
2495    ELM_CHECK_WIDTYPE(obj, widtype);
2496    Widget_Data *wd = elm_widget_data_get(obj);
2497    if (!wd) return;
2498    if (wd->paused_markers == !!paused) return;
2499    wd->paused_markers = paused;
2500 }
2501
2502 /**
2503  * Get the paused state for map
2504  *
2505  * This gets the current paused state for the map object.
2506  *
2507  * @param obj The map object
2508  * @return The current paused state
2509  *
2510  * @ingroup Map
2511  */
2512 EAPI Eina_Bool
2513 elm_map_paused_get(const Evas_Object *obj)
2514 {
2515    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2516    Widget_Data *wd = elm_widget_data_get(obj);
2517    if (!wd) return EINA_FALSE;
2518    return wd->paused;
2519 }
2520
2521 /**
2522  * Get the paused state for the markers
2523  *
2524  * This gets the current paused state for the markers object.
2525  *
2526  * @param obj The map object
2527  * @return The current paused state
2528  *
2529  * @ingroup Map
2530  */
2531 EAPI Eina_Bool
2532 elm_map_paused_markers_get(const Evas_Object *obj)
2533 {
2534    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2535    Widget_Data *wd = elm_widget_data_get(obj);
2536    if (!wd) return EINA_FALSE;
2537    return wd->paused_markers;
2538 }
2539
2540 /**
2541  * Convert a pixel coordinate (x,y) into a geographic coordinate (longitude, latitude).
2542  *
2543  * @param obj The map object
2544  * @param x the coordinate
2545  * @param y the coordinate
2546  * @param size the size in pixels of the map. The map is a square and generally his size is : pow(2.0, zoom)*256.
2547  * @param lon the longitude correspond to x
2548  * @param lat the latitude correspond to y
2549  *
2550  * @ingroup Map
2551  */
2552 EAPI void
2553 elm_map_utils_convert_coord_into_geo(const Evas_Object *obj, int x, int y, int size, double *lon, double *lat)
2554 {
2555    Widget_Data *wd = elm_widget_data_get(obj);
2556    int zoom = floor(log2(size/256));
2557
2558    if(map_sources_tab[elm_map_source_get(obj)].use_module == EINA_TRUE)
2559    if(wd->api) if ((wd->api) && (wd->api->obj_convert_coord_into_geo)){
2560       if(wd->api->obj_convert_coord_into_geo(obj, zoom, x, y, size, lon, lat)==EINA_TRUE)
2561          return;
2562    }
2563
2564    if (lon)
2565      {
2566         *lon = x / (double)size * 360.0 - 180;
2567      }
2568    if (lat)
2569      {
2570         double n = ELM_PI - 2.0 * ELM_PI * y / size;
2571         *lat = 180.0 / ELM_PI * atan(0.5 * (exp(n) - exp(-n)));
2572      }
2573 }
2574
2575 /**
2576  * Convert a geographic coordinate (longitude, latitude) into a pixel coordinate (x, y).
2577  *
2578  * @param obj The map object
2579  * @param lon the longitude
2580  * @param lat the latitude
2581  * @param size the size in pixels of the map. The map is a square and generally his size is : pow(2.0, zoom)*256.
2582  * @param x the coordinate correspond to the longitude
2583  * @param y the coordinate correspond to the latitude
2584  *
2585  * @ingroup Map
2586  */
2587 EAPI void
2588 elm_map_utils_convert_geo_into_coord(const Evas_Object *obj, double lon, double lat, int size, int *x, int *y)
2589 {
2590    Widget_Data *wd = elm_widget_data_get(obj);
2591    int zoom = floor(log2(size/256));
2592
2593    if(map_sources_tab[elm_map_source_get(obj)].use_module == EINA_TRUE)
2594    if(wd->api) if ((wd->api) && (wd->api->obj_convert_geo_into_coord)){
2595       if(wd->api->obj_convert_geo_into_coord(obj, zoom, lon, lat, size, x, y)==EINA_TRUE)
2596          return;
2597    }
2598
2599    if (x)
2600      *x = floor((lon + 180.0) / 360.0 * size);
2601    if (y)
2602      *y = floor((1.0 - log( tan(lat * ELM_PI/180.0) + 1.0 / cos(lat * ELM_PI/180.0)) / ELM_PI) / 2.0 * size);
2603 }
2604
2605
2606
2607 /**
2608  * Add a marker on the map
2609  *
2610  * @param obj The map object
2611  * @param lon the longitude
2612  * @param lat the latitude
2613  * @param clas the class to use
2614  * @param data the data passed to the callbacks
2615  *
2616  * @return The marker object
2617  *
2618  * @ingroup Map
2619  */
2620 EAPI Elm_Map_Marker *
2621 elm_map_marker_add(Evas_Object *obj, double lon, double lat, Elm_Map_Marker_Class *clas, Elm_Map_Group_Class *clas_group, void *data)
2622 {
2623    int i, j;
2624    Eina_List *l;
2625    Marker_Group *group;
2626    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2627    Widget_Data *wd = elm_widget_data_get(obj);
2628    int mpi, mpj;
2629    int tabi[9];
2630    int tabj[9];
2631    const char *s;
2632    const char *style;
2633    Evas_Object *o;
2634
2635    if (!wd) return NULL;
2636    if (!clas_group || !clas) return NULL;
2637
2638    Elm_Map_Marker *marker = ELM_NEW(Elm_Map_Marker);
2639
2640    marker->wd = wd;
2641    marker->clas = clas;
2642    marker->clas_group = clas_group;
2643    marker->longitude = lon;
2644    marker->latitude = lat;
2645    marker->data = data;
2646
2647    tabi[1] = tabi[4] = tabi[6] = -1;
2648    tabi[2] = tabi[0] = tabi[7] = 0;
2649    tabi[3] = tabi[5] = tabi[8] = 1;
2650
2651    tabj[1] = tabj[2] = tabj[3] = -1;
2652    tabj[4] = tabj[0] = tabj[5] = 0;
2653    tabj[6] = tabj[7] = tabj[8] = 1;
2654
2655    if (!clas_group->priv.set)
2656      {
2657         style = "radio";
2658         if (marker->clas_group && marker->clas_group->style)
2659           style = marker->clas_group->style;
2660
2661         o = edje_object_add(evas_object_evas_get(obj));
2662         _elm_theme_object_set(obj, o, "map/marker", style, elm_widget_style_get(obj));
2663         s = edje_object_data_get(o, "size_w");
2664         clas_group->priv.edje_w = atoi(s);
2665         s = edje_object_data_get(o, "size_h");
2666         clas_group->priv.edje_h = atoi(s);
2667         s = edje_object_data_get(o, "size_max_w");
2668         clas_group->priv.edje_max_w = atoi(s);
2669         s = edje_object_data_get(o, "size_max_h");
2670         clas_group->priv.edje_max_h = atoi(s);
2671         evas_object_del(o);
2672
2673         clas_group->priv.set = EINA_TRUE;
2674      }
2675
2676    if (!clas->priv.set)
2677      {
2678         style = "radio";
2679         if (marker->clas && marker->clas->style)
2680           style = marker->clas->style;
2681         
2682         o = edje_object_add(evas_object_evas_get(obj));
2683         _elm_theme_object_set(obj, o, "map/marker", style, elm_widget_style_get(obj));
2684         s = edje_object_data_get(o, "size_w");
2685         clas->priv.edje_w = atoi(s);
2686         s = edje_object_data_get(o, "size_h");
2687         clas->priv.edje_h = atoi(s);
2688         evas_object_del(o);
2689
2690         clas->priv.set = EINA_TRUE;
2691      }
2692
2693    for (i = clas_group->zoom_displayed; i <= ZOOM_MAX; i++)
2694      {
2695         elm_map_utils_convert_geo_into_coord(obj, lon, lat, pow(2.0, i)*wd->tsize,
2696                                              &(marker->x[i]), &(marker->y[i]));
2697         
2698         //search in the matrixsparse the region where the marker will be
2699         mpi = marker->x[i] / wd->tsize;
2700         mpj = marker->y[i] / wd->tsize;
2701
2702         if (!wd->markers[i])
2703           {
2704              int size =  pow(2.0, i);
2705              wd->markers[i] = eina_matrixsparse_new(size, size, NULL, NULL);
2706           }
2707
2708         group = NULL;
2709         if (i <= clas_group->zoom_grouped)
2710           {
2711              for (j = 0, group = NULL; j < 9 && !group; j++)
2712                {
2713                   EINA_LIST_FOREACH(eina_matrixsparse_data_idx_get(wd->markers[i], mpj + tabj[j], mpi + tabi[j]),
2714                                     l, group)
2715                     {
2716                        if (group->clas == marker->clas_group
2717                            && ELM_RECTS_INTERSECT(marker->x[i]-clas->priv.edje_w/4,
2718                                                   marker->y[i]-clas->priv.edje_h/4, clas->priv.edje_w, clas->priv.edje_h,
2719                                                   group->x-group->w/4, group->y-group->h/4, group->w, group->h))
2720                          {
2721                             group->markers = eina_list_append(group->markers, marker);
2722                             group->update_nbelems = EINA_TRUE;
2723                             group->update_resize = EINA_TRUE;
2724
2725                             group->sum_x += marker->x[i];
2726                             group->sum_y += marker->y[i];
2727                             group->x = group->sum_x / eina_list_count(group->markers);
2728                             group->y = group->sum_y / eina_list_count(group->markers);
2729
2730                             group->w = group->clas->priv.edje_w + group->clas->priv.edje_w/8.
2731                               * eina_list_count(group->markers);
2732                             group->h = group->clas->priv.edje_h + group->clas->priv.edje_h/8.
2733                               * eina_list_count(group->markers);
2734                             if (group->w > group->clas->priv.edje_max_w) group->w = group->clas->priv.edje_max_w;
2735                             if (group->h > group->clas->priv.edje_max_h) group->h = group->clas->priv.edje_max_h;
2736
2737                             if (group->obj && eina_list_count(group->markers) == 2)
2738                               {
2739                                  _group_object_free(group);
2740                                  _group_object_create(group);
2741                               }
2742                             if (group->bubble)
2743                               _group_bubble_content_update(group);
2744                             
2745                             break;
2746                          }
2747                     }
2748                }
2749           }
2750         if (!group)
2751           {
2752              group = calloc(1, sizeof(Marker_Group));
2753              group->wd = wd;
2754              group->sum_x = marker->x[i];
2755              group->sum_y = marker->y[i];
2756              group->x = marker->x[i];
2757              group->y = marker->y[i];
2758              group->w = clas_group->priv.edje_w;
2759              group->h = clas_group->priv.edje_h;
2760              group->clas = clas_group;
2761
2762              group->markers = eina_list_append(group->markers, marker);
2763              group->update_nbelems = EINA_TRUE;
2764              group->update_resize = EINA_TRUE;
2765
2766              eina_matrixsparse_cell_idx_get(wd->markers[i], mpj, mpi, &(group->cell));
2767
2768              if (!group->cell)
2769                {
2770                   l = eina_list_append(NULL, group);
2771                   eina_matrixsparse_data_idx_set(wd->markers[i], mpj, mpi, l);
2772                   eina_matrixsparse_cell_idx_get(wd->markers[i], mpj, mpi, &(group->cell));
2773                }
2774              else
2775                {
2776                   l = eina_matrixsparse_cell_data_get(group->cell);
2777                   l = eina_list_append(l, group);
2778                   eina_matrixsparse_cell_data_set(group->cell, l);
2779                }
2780           }
2781         marker->groups[i] = group;
2782      }
2783
2784    if (wd->grids)
2785      {
2786         Evas_Coord ox, oy, ow, oh;
2787         evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
2788         marker_place(obj, eina_list_data_get(wd->grids), wd->pan_x, wd->pan_y, ox, oy, ow, oh);
2789      }
2790
2791    return marker;
2792 }
2793
2794 /**
2795  * Remove a marker from the map
2796  *
2797  * @param marker The marker to remove
2798  *
2799  * @ingroup Map
2800  */
2801 EAPI void
2802 elm_map_marker_remove(Elm_Map_Marker *marker)
2803 {
2804    int i;
2805    Eina_List *groups;
2806    Widget_Data *wd;
2807
2808    if (!marker) return;
2809    wd = marker->wd;
2810    if (!wd) return;
2811    for (i = 0; i <= ZOOM_MAX; i++)
2812      {
2813         marker->groups[i]->markers = eina_list_remove(marker->groups[i]->markers, marker);
2814         if (eina_list_count(marker->groups[i]->markers) == 0)
2815           {
2816              groups = eina_matrixsparse_cell_data_get(marker->groups[i]->cell);
2817              groups = eina_list_remove(groups, marker->groups[i]);
2818              eina_matrixsparse_cell_data_set(marker->groups[i]->cell, groups);
2819              
2820              _group_object_free(marker->groups[i]);
2821              _group_bubble_free(marker->groups[i]);
2822              free(marker->groups[i]);
2823           }
2824         else
2825           {
2826              marker->groups[i]->sum_x -= marker->x[i];
2827              marker->groups[i]->sum_y -= marker->y[i];
2828              
2829              marker->groups[i]->x = marker->groups[i]->sum_x / eina_list_count(marker->groups[i]->markers);
2830              marker->groups[i]->y = marker->groups[i]->sum_y / eina_list_count(marker->groups[i]->markers);
2831              
2832              marker->groups[i]->w = marker->groups[i]->clas->priv.edje_w
2833                + marker->groups[i]->clas->priv.edje_w/8. * eina_list_count(marker->groups[i]->markers);
2834              marker->groups[i]->h = marker->groups[i]->clas->priv.edje_h
2835                + marker->groups[i]->clas->priv.edje_h/8. * eina_list_count(marker->groups[i]->markers);
2836              if (marker->groups[i]->w > marker->groups[i]->clas->priv.edje_max_w)
2837                marker->groups[i]->w = marker->groups[i]->clas->priv.edje_max_w;
2838              if (marker->groups[i]->h > marker->groups[i]->clas->priv.edje_max_h)
2839                marker->groups[i]->h = marker->groups[i]->clas->priv.edje_max_h;
2840           }
2841         if (marker->groups[i]->obj && eina_list_count(marker->groups[i]->markers) == 1)
2842           {
2843              _group_object_free(marker->groups[i]);
2844              _group_object_create(marker->groups[i]);
2845           }
2846      }
2847
2848    if (marker->content && marker->clas->func.del)
2849      marker->clas->func.del(marker->wd->obj, marker, marker->data, marker->content);
2850    else if (marker->content)
2851      evas_object_del(marker->content);
2852    
2853    free(marker);
2854    
2855    if (wd->grids)
2856      {
2857         Evas_Coord ox, oy, ow, oh;
2858         evas_object_geometry_get(wd->obj, &ox, &oy, &ow, &oh);
2859         marker_place(wd->obj, eina_list_data_get(wd->grids), wd->pan_x, wd->pan_y, ox, oy, ow, oh);
2860      }
2861 }
2862
2863 /**
2864  * Move the map to the coordinate of the marker.
2865  *
2866  * @param marker The marker where the map will be center.
2867  *
2868  * @ingroup Map
2869  */
2870 EAPI void
2871 elm_map_marker_bring_in(Elm_Map_Marker *marker)
2872 {
2873    if (!marker) return;
2874    elm_map_geo_region_bring_in(marker->wd->obj, marker->longitude, marker->latitude);
2875 }
2876
2877
2878 /**
2879  * Move the map to the coordinate of the marker.
2880  *
2881  * @param marker The marker where the map will be center.
2882  *
2883  * @ingroup Map
2884  */
2885 EAPI void
2886 elm_map_marker_show(Elm_Map_Marker *marker)
2887 {
2888    if (!marker) return;
2889    elm_map_geo_region_show(marker->wd->obj, marker->longitude, marker->latitude);
2890 }
2891
2892 /**
2893  * Move and zoom the map to display a list of markers.
2894  *
2895  * The map will be centered on the center point of the markers in the list. Then
2896  * the map will be zoomed in order to fit the markers using the maximum zoom which
2897  * allows display of all the markers.
2898  *
2899  * @param markers The list of markers (list of Elm_Map_Marker *)
2900  *
2901  * @ingroup Map
2902  */
2903 EAPI void
2904 elm_map_markers_list_show(Eina_List *markers)
2905 {
2906    int zoom;
2907    double lon, lat;
2908    Eina_List *l;
2909    Elm_Map_Marker *marker, *m_max_lon = NULL, *m_max_lat = NULL, *m_min_lon = NULL, *m_min_lat = NULL;
2910    Evas_Coord rw, rh, xc, yc;
2911    Widget_Data *wd;
2912
2913    if (!markers) return;
2914
2915    EINA_LIST_FOREACH(markers, l, marker)
2916      {
2917         wd = marker->wd;
2918
2919         if (!m_min_lon || marker->longitude < m_min_lon->longitude)
2920           m_min_lon = marker;
2921         
2922         if (!m_max_lon || marker->longitude > m_max_lon->longitude)
2923           m_max_lon = marker;
2924         
2925         if (!m_min_lat || marker->latitude > m_min_lat->latitude)
2926           m_min_lat = marker;
2927         
2928         if (!m_max_lat || marker->latitude < m_max_lat->latitude)
2929           m_max_lat = marker;
2930      }
2931    
2932    lon = (m_max_lon->longitude - m_min_lon->longitude) / 2. + m_min_lon->longitude;
2933    lat = (m_max_lat->latitude - m_min_lat->latitude) / 2. + m_min_lat->latitude;
2934    
2935    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
2936    for (zoom = map_sources_tab[wd->source].zoom_max; zoom>map_sources_tab[wd->source].zoom_min; zoom--)
2937      {
2938         Evas_Coord size = pow(2.0, zoom)*wd->tsize;
2939         elm_map_utils_convert_geo_into_coord(wd->obj, lon, lat, size, &xc, &yc);
2940         
2941         if (m_min_lon->x[zoom] - wd->marker_max_w >= xc-rw/2
2942             && m_min_lat->y[zoom] - wd->marker_max_h >= yc-rh/2
2943             && m_max_lon->x[zoom] + wd->marker_max_w <= xc+rw/2
2944             && m_max_lat->y[zoom] + wd->marker_max_h <= yc+rh/2)
2945           break;
2946      }
2947
2948    elm_map_geo_region_show(wd->obj, lon, lat);
2949    elm_map_zoom_set(wd->obj, zoom);
2950 }
2951
2952 /**
2953  * Set the maximum numbers of markers display in a group.
2954  *
2955  * A group can have a long list of markers, consequently the creation of the content
2956  * of the bubble can be very slow. In order to avoid this, a maximum number of items
2957  * is displayed in a bubble. By default this number is 30.
2958  *
2959  * @param obj The map object.
2960  * @param max The maximum numbers of items displayed in a bubble.
2961  *
2962  * @ingroup Map
2963  */
2964 EAPI void
2965 elm_map_max_marker_per_group_set(Evas_Object *obj, int max)
2966 {
2967    ELM_CHECK_WIDTYPE(obj, widtype);
2968    Widget_Data *wd = elm_widget_data_get(obj);
2969    if (!wd) return;
2970    wd->markers_max_num = max;
2971 }
2972
2973 /**
2974  * Return the evas object getting from the ElmMapMarkerGetFunc callback
2975  *
2976  * @param marker The marker.
2977  * @return Return the evas object if it exists, else NULL.
2978  *
2979  * @ingroup Map
2980  */
2981 EAPI Evas_Object *
2982 elm_map_marker_object_get(Elm_Map_Marker *marker)
2983 {
2984    if (!marker) return NULL;
2985    return marker->content;
2986 }
2987
2988 /**
2989  * Update the marker
2990  *
2991  * @param marker The marker.
2992  *
2993  * @ingroup Map
2994  */
2995 EAPI void
2996 elm_map_marker_update(Elm_Map_Marker *marker)
2997 {
2998    if (!marker) return;
2999    if (marker->content)
3000      {
3001         if (marker->clas->func.del)
3002           marker->clas->func.del(marker->wd->obj, marker, marker->data, marker->content);
3003         else
3004           evas_object_del(marker->content);
3005         marker->content = NULL;
3006         _group_bubble_content_update(marker->groups[marker->wd->zoom]);
3007      }
3008 }
3009
3010 /**
3011  * Close all opened bubbles
3012  *
3013  * @param obj The map object
3014  *
3015  * @ingroup Map
3016  */
3017 EAPI void
3018 elm_map_bubbles_close(Evas_Object *obj)
3019 {
3020    ELM_CHECK_WIDTYPE(obj, widtype);
3021    Widget_Data *wd = elm_widget_data_get(obj);
3022    Marker_Group *group;
3023    Eina_List *l, *l_next;
3024    if (!wd) return;
3025    EINA_LIST_FOREACH_SAFE(wd->opened_bubbles, l, l_next, group)
3026      _group_bubble_free(group);
3027 }
3028
3029
3030 /**
3031  * Create a group class.
3032  *
3033  * Each marker must be associated to a group class. Marker with the same group are grouped if they are close.
3034  * The group class defines the style of the marker when a marker is grouped to others markers.
3035  *
3036  * @param obj The map object
3037  * @return Returns the new group class
3038  *
3039  * @ingroup Map
3040  */
3041 EAPI Elm_Map_Group_Class *
3042 elm_map_group_class_new(Evas_Object *obj)
3043 {
3044    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3045    Widget_Data *wd = elm_widget_data_get(obj);
3046    if (!wd) return NULL;
3047    Elm_Map_Group_Class *clas = calloc(1, sizeof(Elm_Map_Group_Class));
3048    clas->zoom_grouped = ZOOM_MAX;
3049    wd->groups_clas = eina_list_append(wd->groups_clas, clas);
3050    return clas;
3051 }
3052
3053 /**
3054  * Set the style of a group class (radio, radio2 or empty)
3055  *
3056  * @param clas the group class
3057  * @param style the new style
3058  *
3059  * @ingroup Map
3060  */
3061 EAPI void
3062 elm_map_group_class_style_set(Elm_Map_Group_Class *clas, const char *style)
3063 {
3064    if (!clas) return;
3065    eina_stringshare_replace(&clas->style, style);
3066 }
3067
3068 /**
3069  * Set the icon callback of a group class.
3070  *
3071  * A custom icon can be displayed in a marker. The function @ref icon_get must return this icon.
3072  *
3073  * @param clas the group class
3074  * @param icon_get the callback to create the icon
3075  *
3076  * @ingroup Map
3077  */
3078 EAPI void
3079 elm_map_group_class_icon_cb_set(Elm_Map_Group_Class *clas, ElmMapGroupIconGetFunc icon_get)
3080 {
3081    if (!clas) return;
3082    clas->func.icon_get = icon_get;
3083 }
3084
3085 /**
3086  * Set the data associated to the group class (radio, radio2 or empty)
3087  *
3088  * @param clas the group class
3089  * @param data the new user data
3090  *
3091  * @ingroup Map
3092  */
3093 EAPI void
3094 elm_map_group_class_data_set(Elm_Map_Group_Class *clas, void *data)
3095 {
3096    if (!clas) return;
3097    clas->data = data;
3098 }
3099
3100 /**
3101  * Set the zoom from where the markers are displayed.
3102  *
3103  * Markers will not be displayed for a zoom less than @ref zoom
3104  *
3105  * @param clas the group class
3106  * @param zoom the zoom
3107  *
3108  * @ingroup Map
3109  */
3110 EAPI void
3111 elm_map_group_class_zoom_displayed_set(Elm_Map_Group_Class *clas, int zoom)
3112 {
3113    if (!clas) return;
3114    clas->zoom_displayed = zoom;
3115 }
3116
3117 /**
3118  * Set the zoom from where the markers are no more grouped.
3119  *
3120  * @param clas the group class
3121  * @param zoom the zoom
3122  *
3123  * @ingroup Map
3124  */
3125 EAPI void
3126 elm_map_group_class_zoom_grouped_set(Elm_Map_Group_Class *clas, int zoom)
3127 {
3128    if (!clas) return;
3129    clas->zoom_grouped = zoom;
3130 }
3131
3132 /**
3133  * Set if the markers associated to the group class @clas are hidden or not.
3134  * If @ref hide is true the markers will be hidden.
3135  *
3136  * @param clas the group class
3137  * @param hide if true the markers will be hidden, else they will be displayed.
3138  *
3139  * @ingroup Map
3140  */
3141 EAPI void
3142 elm_map_group_class_hide_set(Evas_Object *obj, Elm_Map_Group_Class *clas, Eina_Bool hide)
3143 {
3144    ELM_CHECK_WIDTYPE(obj, widtype);
3145    Widget_Data *wd = elm_widget_data_get(obj);
3146    if (!wd) return;
3147    if (!clas) return;
3148    if (clas->hide == hide) return;
3149    clas->hide = hide;
3150    if (wd->grids)
3151      {
3152         Evas_Coord ox, oy, ow, oh;
3153         evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
3154         marker_place(obj, eina_list_data_get(wd->grids), wd->pan_x, wd->pan_y, ox, oy, ow, oh);
3155      }
3156 }
3157
3158
3159 /**
3160  * Create a marker class.
3161  *
3162  * Each marker must be associated to a class.
3163  * The class defines the style of the marker when a marker is displayed alone (not grouped).
3164  *
3165  * @param obj The map object
3166  * @return Returns the new class
3167  *
3168  * @ingroup Map
3169  */
3170 EAPI Elm_Map_Marker_Class *
3171 elm_map_marker_class_new(Evas_Object *obj)
3172 {
3173    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
3174    Widget_Data *wd = elm_widget_data_get(obj);
3175    if (!wd) return NULL;
3176    Elm_Map_Marker_Class *clas = calloc(1, sizeof(Elm_Map_Marker_Class));
3177    wd->markers_clas = eina_list_append(wd->markers_clas, clas);
3178    return clas;
3179 }
3180
3181 /**
3182  * Set the style of a class (radio, radio2 or empty)
3183  *
3184  * @param clas the group class
3185  * @param style the new style
3186  *
3187  * @ingroup Map
3188  */
3189 EAPI void
3190 elm_map_marker_class_style_set(Elm_Map_Marker_Class *clas, const char *style)
3191 {
3192    if (!clas) return;
3193    eina_stringshare_replace(&clas->style, style);
3194 }
3195
3196 /**
3197  * Set the icon callback of a class.
3198  *
3199  * A custom icon can be displayed in a marker. The function @ref icon_get must return this icon.
3200  *
3201  * @param clas the group class
3202  * @param icon_get the callback to create the icon
3203  *
3204  * @ingroup Map
3205  */
3206 EAPI void
3207 elm_map_marker_class_icon_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerIconGetFunc icon_get)
3208 {
3209    if (!clas) return;
3210    clas->func.icon_get = icon_get;
3211 }
3212
3213 /**
3214  *
3215  * Set the callback of the content of the bubble.
3216  *
3217  * When the user click on a marker, a bubble is displayed with a content.
3218  * The callback @ref get musst return this content. It can be NULL.
3219  *
3220  * @param clas the group class
3221  * @param get the callback to create the content
3222  *
3223  * @ingroup Map
3224  */
3225 EAPI void
3226 elm_map_marker_class_get_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerGetFunc get)
3227 {
3228    if (!clas) return;
3229    clas->func.get = get;
3230 }
3231
3232 /**
3233  * Set the callback of the content of delete the object created by the callback "get".
3234  *
3235  * If this callback is defined the user will have to delete (or not) the object inside.
3236  * If the callback is not defined the object will be destroyed with evas_object_del()
3237  *
3238  * @param clas the group class
3239  * @param del the callback to delete the content
3240  *
3241  * @ingroup Map
3242  */
3243 EAPI void
3244 elm_map_marker_class_del_cb_set(Elm_Map_Marker_Class *clas, ElmMapMarkerDelFunc del)
3245 {
3246    if (!clas) return;
3247    clas->func.del = del;
3248 }
3249
3250 /**
3251  * Set the source of the map.
3252  *
3253  * Elm_Map retrieves the image which composed the map from a web service. This web service can
3254  * be set with this method. A different service can return a different maps with different
3255  * information and it can use different zoom value.
3256  *
3257  * @param clas the group class
3258  * @param source the new source
3259  *
3260  * @ingroup Map
3261  */
3262 EAPI void
3263 elm_map_source_set(Evas_Object *obj, Elm_Map_Sources source)
3264 {
3265    ELM_CHECK_WIDTYPE(obj, widtype);
3266    Widget_Data *wd = elm_widget_data_get(obj);
3267    Grid *grid;
3268    int zoom;
3269    if (!wd) return;
3270    if (wd->source == source ) return;
3271    if (!map_sources_tab[source].url_cb) return;
3272
3273    EINA_LIST_FREE(wd->grids, grid) grid_clear(obj, grid);
3274
3275    wd->source = source;
3276    zoom = wd->zoom;
3277    wd->zoom = -1;
3278
3279    if (map_sources_tab[wd->source].zoom_max < zoom)
3280      zoom = map_sources_tab[wd->source].zoom_max;
3281    if (map_sources_tab[wd->source].zoom_min > zoom)
3282      zoom = map_sources_tab[wd->source].zoom_min;
3283    
3284    elm_map_zoom_set(obj, zoom);
3285 }
3286
3287 /**
3288  * Get the current source
3289  *
3290  * @param obj the map object
3291  * @return Returns the maximum zoom of the source
3292  *
3293  * @ingroup Map
3294  */
3295 EAPI Elm_Map_Sources
3296 elm_map_source_get(const Evas_Object *obj)
3297 {
3298    ELM_CHECK_WIDTYPE(obj, widtype) ELM_MAP_SOURCE_MAPNIK;
3299    Widget_Data *wd = elm_widget_data_get(obj);
3300    if (!wd) return ELM_MAP_SOURCE_MAPNIK;
3301    return wd->source;
3302 }
3303
3304 /**
3305  * Set the API of a custom source.
3306  *
3307  * A custom web service can be associated to the source ELM_MAP_SOURCE_CUSTOM_(1..7).
3308  *
3309  * @param source the source ID (ELM_MAP_SOURCE_CUSTOM_(1..7))
3310  * @param name the name of the source
3311  * @param zoom_min the minimum zoom of the source, must be >= 0
3312  * @param zoom_max the maximum zoom of the source, must be <= ZOOM_MAX
3313  * @param url_cb the callback used to create the url from where a tile (png or jpeg file) is downloaded.
3314  *
3315  * @ingroup Map
3316  */
3317 EAPI void
3318 elm_map_source_custom_api_set(Elm_Map_Sources source, const char *name, int zoom_min, int zoom_max, ElmMapSourceURLFunc url_cb)
3319 {
3320    if (!name || !url_cb) return;
3321    map_sources_tab[source].name = name;
3322    map_sources_tab[source].zoom_min = zoom_min;
3323    map_sources_tab[source].zoom_max = zoom_max;
3324    map_sources_tab[source].url_cb = url_cb;
3325 }
3326
3327 /**
3328  * Get the maximum zoom of the source.
3329  *
3330  * @param source the source
3331  * @return Returns the maximum zoom of the source
3332  *
3333  * @ingroup Map
3334  */
3335 EAPI int
3336 elm_map_source_zoom_max_get(Elm_Map_Sources source)
3337 {
3338    return map_sources_tab[source].zoom_max;
3339 }
3340
3341 /**
3342  * Get the minimum zoom of the source.
3343  *
3344  * @param source the source
3345  * @return Returns the minimum zoom of the source
3346  *
3347  * @ingroup Map
3348  */
3349 EAPI int
3350 elm_map_source_zoom_min_get(Elm_Map_Sources source)
3351 {
3352    return map_sources_tab[source].zoom_min;
3353 }
3354
3355 /**
3356  * Get the name of a source.
3357  *
3358  * @param source the source
3359  * @return Returns the name of the source
3360  *
3361  * @ingroup Map
3362  */
3363 EAPI const char *
3364 elm_map_source_name_get(Elm_Map_Sources source)
3365 {
3366    return map_sources_tab[source].name;
3367 }
3368
3369
3370 static char *
3371 _mapnik_url_cb(void *data, int x, int y, int zoom)
3372 {
3373    char buf[PATH_MAX];
3374    snprintf(buf, sizeof(buf), "http://tile.openstreetmap.org/%d/%d/%d.png",
3375             zoom, x, y);
3376    return strdup(buf);
3377 }
3378
3379 static char *
3380 _osmarender_url_cb(void * data, int x, int y, int zoom)
3381 {
3382    char buf[PATH_MAX];
3383    snprintf(buf, sizeof(buf), "http://tah.openstreetmap.org/Tiles/tile/%d/%d/%d.png",
3384             zoom, x, y);
3385    return strdup(buf);
3386 }
3387
3388 static char *
3389 _cyclemap_url_cb(void *data, int x, int y, int zoom)
3390 {
3391    char buf[PATH_MAX];
3392    snprintf(buf, sizeof(buf), "http://andy.sandbox.cloudmade.com/tiles/cycle/%d/%d/%d.png",
3393             zoom, x, y);
3394    return strdup(buf);
3395 }
3396
3397 static char *
3398 _maplint_url_cb(void *data, int x, int y, int zoom)
3399 {
3400    char buf[PATH_MAX];
3401    snprintf(buf, sizeof(buf), "http://tah.openstreetmap.org/Tiles/maplint/%d/%d/%d.png",
3402             zoom, x, y);
3403    return strdup(buf);
3404 }
3405
3406 static char *
3407 _decarta_url_cb(void *data, int x, int y, int zoom)
3408 {
3409    char *url = NULL;
3410    Widget_Data *wd = elm_widget_data_get(data);
3411    if(map_sources_tab[elm_map_source_get(data)].use_module == EINA_TRUE)
3412    if(wd->api) if ((wd->api) && (wd->api->obj_url_request))
3413       url = wd->api->obj_url_request(data, x, y, zoom);
3414
3415    if(!url) url = strdup("");
3416
3417    return url;
3418 }
3419