7893cd3db4784dbc73d636de198b3cbf4acf9453
[apps/core/preloaded/calendar.git] / src / view-main-year.c
1 /*
2  * Calendar
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngjae Shin <yj99.shin@samsung.com>
7  *          Taeho Kang <taeho84.kang@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 #include <Ecore_X.h>
23
24 #include "view.h"
25
26 #define ANIMATION_TIME 0.2
27
28 typedef enum year_index {
29         CAL_YEAR_PREV_YEAR = -1,
30         CAL_YEAR_CURR_YEAR,
31         CAL_YEAR_NEXT_YEAR,
32 }year_index;
33
34 static const char *_name = "main/year/months";
35
36 typedef struct {
37         const char *name;
38         struct appdata *ad;
39         Evas_Object *parent;
40         Evas_Object *ly; // self
41         Evas_Object *handle_base;
42
43         struct _year* prev_year;
44         struct _year* cur_year;
45         struct _year* next_year;
46
47         Eina_Bool ignore_mouse_down; // block mouse down event during flick
48         int downed;
49         Evas_Coord_Rectangle rect;
50         Evas_Coord_Point down1,down2;
51         Evas_Coord_Point move1,move2;
52
53         int pos_selected;
54         int pos_down;
55
56         Evas_Coord_Rectangle mouse_down_position;
57         Evas_Coord_Rectangle mouse_up_position;
58         Evas_Coord_Rectangle pos_to;
59         Ecore_Animator *ani;
60         double ani_start;
61         struct tm tm_to;
62
63         char wday[8][7];//To reduce get text time
64         char month[12][32];//To reduce get text time
65
66         Ecore_Idler* idler; //To make prev & next year object
67
68         Evas_Object* edje_handle[12];
69 } cal_view_main_year_data;
70
71 struct _year{
72         int year;
73         Evas_Object* image;
74         Evas* evas;
75         Evas_Object* edj;
76         Evas_Object* ly;
77         cal_view_main_year_data *p;
78 };
79
80 static char signal_source[32];
81 static struct _year* __cal_year_create_year_object(Evas_Object* parent, cal_view_main_year_data* p, enum year_index);
82 static void __cal_year_delete_year_object(struct _year* y);
83 static void __cal_year_mouse_up(void *data, Evas *e, Evas_Object *ly, void *ei);
84 static void __cal_year_mouse_down(void *data, Evas *e, Evas_Object *ly, void *ei);
85 static void __cal_year_mouse_move(void *data, Evas *e, Evas_Object *ly, void *ei);
86 static void __cal_year_update_select(struct _year *y, int tm_mon);
87 static void __cal_year_delete_edje_handle(cal_view_main_year_data* p);
88
89 static inline void __cal_year_stop_animation(cal_view_main_year_data *p)
90 {
91         c_retm_if(!p, "Input parameter is null.");
92
93         if (p->ani)
94                 ecore_animator_del(p->ani);
95
96         p->ani = NULL;
97 }
98
99 static void __cal_year_delete_layout(void *data, Evas *e, Evas_Object *obj, void *ei)
100 {
101         CAL_FN_START;
102
103         c_retm_if(!data, "Input parameter is null.");
104
105         cal_view_main_year_data *p = data;
106
107         __cal_year_delete_edje_handle(p);
108
109         __cal_year_stop_animation(p);
110
111         if (p->prev_year)
112                 __cal_year_delete_year_object(p->prev_year);
113         if (p->cur_year)
114                 __cal_year_delete_year_object(p->cur_year);
115         if (p->next_year)
116                 __cal_year_delete_year_object(p->next_year);
117
118         if (p->idler)
119                 ecore_idler_del(p->idler);
120
121         free(p);
122 }
123
124 static void __cal_year_set_month_text(struct _year* y)
125 {
126         c_retm_if(!y, "Input parameter is null.");
127
128         int i;
129         cal_view_main_year_data* p = y->p;
130         c_retm_if(!p, "");
131
132
133         for (i = 0; i < 12; i++)
134                 edje_object_part_text_set(y->edj, cal_util_get_part_text("mb/%d/text", i), p->month[i]);
135 }
136
137 static void __cal_year_make_month_text(cal_view_main_year_data* p)
138 {
139         c_retm_if(!p, "Input parameter is null.");
140
141         int i;
142         struct tm tm;
143
144         tm = p->ad->base_tm;
145         cal_util_update_tm_month(&tm, tm.tm_mon*(-1));
146
147         for(i = 0; i < 12 ; i++)
148         {
149                 cal_util_get_time_text(p->month[i], 31, CAL_UTIL_DATE_FORMAT_4, NULL, &tm);
150                 cal_util_update_tm_month(&tm, 1);
151         }
152 }
153
154 static void __cal_year_make_wday_text(cal_view_main_year_data* p)
155 {
156         c_retm_if(!p, "Input parameter is null.");
157
158         int i;
159         struct tm tm;
160
161         tm = p->ad->base_tm;
162         cal_util_update_tm_day(&tm, tm.tm_wday*(-1));
163
164         for(i = 0; i < 7 ; i++)
165         {
166                 cal_util_get_time_text(p->wday[i], 7, CAL_UTIL_DATE_FORMAT_2, NULL, &tm);
167                 cal_util_update_tm_day(&tm, 1);
168         }
169 }
170
171 static void __cal_year_set_wday_text(Evas_Object *month, int start)
172 {
173         c_retm_if(!month, "Input parameter is null.");
174
175         int i;
176
177         cal_view_main_year_data* p = CAL_UTIL_GET_PRIV_DATA(month);
178
179         for (i = 0; i < 7; i++)
180         {
181                 edje_object_part_text_set(month, cal_util_get_part_text("sdh/%d/text", i), p->wday[(i+start)%7]);
182                 if((i+start)%7 == 6)
183                 {
184                         cal_util_emit_signal(month, "satday,%d", i);
185                         continue;
186                 }
187
188                 if((i+start)%7 == 0)
189                         cal_util_emit_signal(month, "sunday,%d", i);
190         }
191 }
192
193 static void __cal_year_update_today(Evas_Object *ly, int tm_year, int tm_mon, int tm_mday, int pos)
194 {
195         c_retm_if(!ly, "Input parameter is null.");
196
197         cal_view_main_year_data *p = CAL_UTIL_GET_PRIV_DATA(ly);
198         c_retm_if(!p, "CAL_UTIL_GET_PRIV_DATA returned null.");
199
200         struct appdata *ad = p->ad;
201         c_retm_if(!ad, "p->ad is null.");
202
203         if (tm_year == ad->today_tm.tm_year && tm_mon == ad->today_tm.tm_mon && tm_mday == ad->today_tm.tm_mday)
204                 cal_util_emit_signal(ly, "today,%d", pos-1);
205 }
206
207
208 static void __cal_year_fill_month(Evas_Object *month, int tm_year, int tm_mon, int *first_wday, int start_wday)
209 {
210         c_retm_if(!month, "Input parameter is null.");
211
212         int i, day, wday;
213         int max;
214
215         wday = *first_wday;
216         max = CAL_UTIL_GET_WDAY(wday - start_wday);
217
218         for (i = 0; i < max; i++)
219         {
220                 cal_util_set_text(month, cal_util_get_part_text("sdb/%d/text", i), "");
221         }
222
223         day = 1;
224         max = i + cal_util_get_max_days(tm_year, tm_mon);
225         for (; i < max; i++)
226         {
227                 cal_util_set_text(month, cal_util_get_part_text("sdb/%d/text", i), "%d", day);
228
229                 if (wday == 6)
230                         cal_util_emit_signal(month, "satday,%d", i);
231                 else if (wday == 0)
232                         cal_util_emit_signal(month, "sunday,%d", i);
233
234                 wday = CAL_UTIL_GET_WDAY(wday + 1);
235
236                 __cal_year_update_today(month, tm_year, tm_mon, day-1, i);
237
238                 day++;
239         }
240
241         for (; i < 42; i++)
242         {
243                 cal_util_set_text(month, cal_util_get_part_text("sdb/%d/text", i), "");
244         }
245
246         *first_wday = wday;
247 }
248
249 static Evas_Object* __cal_year_create_month(struct _year *y, int tm_mon, int *wday, int start)
250 {
251         c_retvm_if(!y, NULL, "Input parameter is null.");
252
253         Evas_Object *month;
254
255         month = edje_object_add(y->evas);
256
257         if(cal_util_black_theme_check()){
258                 edje_object_file_set(month, EDJ_FILE, "main/year/cal");
259         }
260         else{
261                 edje_object_file_set(month, EDJ_FILE_WHITE, "main/year/cal");
262         }
263
264         evas_object_data_set(month,"priv",y->p);
265
266         __cal_year_set_wday_text(month, start);
267         __cal_year_fill_month(month, y->year, tm_mon, wday, start);
268
269         return month;
270 }
271
272 static int __cal_year_get_first_wday(int tm_year)
273 {
274         struct tm tm;
275         time_t t;
276
277         t = time(NULL);
278         localtime_r(&t, &tm);
279
280         tm.tm_year = tm_year;
281         tm.tm_mon = 0;
282         tm.tm_mday = 1;
283
284         t = mktime(&tm);
285         c_retvm_if(t == -1, 0, "mktime() returned -1.");
286
287         return tm.tm_wday;
288 }
289
290 static void __cal_year_fill_months(struct _year* y)
291 {
292         c_retm_if(!y, "Input parameter is null.");
293
294         int i;
295         Evas_Object *month;
296         int first_wday;
297         cal_view_main_year_data* p = y->p;
298
299         first_wday = __cal_year_get_first_wday(y->year);
300
301         __cal_year_make_wday_text(p);
302
303         for (i = 0; i < 12; i++)
304         {
305                 month = __cal_year_create_month(y, i, &first_wday, p->ad->wday_start);
306                 c_retm_if(!month, "");
307
308                 edje_object_part_swallow(y->edj, cal_util_get_part_text("mb/%d/sw", i), month);
309
310                 evas_object_data_set(y->ly, cal_util_get_part_text("mb/%d/sw", i), month);
311         }
312 }
313
314 static inline void __cal_year_move_year_objects(cal_view_main_year_data *p, Evas_Coord x)
315 {
316         c_retm_if(!p, "Input parameter is null.");
317
318         if (x < 0) {
319
320                 if (!p->next_year) {
321
322                         p->next_year = __cal_year_create_year_object(p->ly, p, CAL_YEAR_NEXT_YEAR);
323                         c_retm_if(!p->next_year, "__cal_year_create_year_object(p->ly, p, CAL_YEAR_NEXT_YEAR) returned null");
324                 }
325
326                 evas_object_move(p->cur_year->image, x, p->rect.y);
327
328                 evas_object_move(p->next_year->image, x + p->rect.w, p->rect.y);
329         } else {
330
331                 if (!p->prev_year) {
332
333                         p->prev_year = __cal_year_create_year_object(p->ly, p, CAL_YEAR_PREV_YEAR);
334                         c_retm_if(!p->prev_year, "__cal_year_create_year_object(p->ly, p, CAL_YEAR_PREV_YEAR) returned null");
335                 }
336
337                 evas_object_move(p->cur_year->image, x, p->rect.y);
338
339                 evas_object_move(p->prev_year->image, x -p->rect.w, p->rect.y);
340         }
341
342         evas_object_move(p->handle_base, x, p->rect.y);
343 }
344
345 static void __cal_year_image_object_mouse_down_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
346 {
347         c_retm_if(!data, "Input parameter is null.");
348
349         cal_view_main_year_data *p = data;
350         struct _year *y = p->cur_year;
351
352         int index = (int)evas_object_data_get(obj, "index");
353
354         snprintf(signal_source, 31, "mb/%d/base", index);
355         edje_object_signal_emit(y->edj, "mouse,down,1", signal_source);
356
357         p->pos_down = index;
358 }
359
360 static void __cal_year_image_object_mouse_up_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
361 {
362         c_retm_if(!data, "Input parameter is null.");
363
364         cal_view_main_year_data *p = data;
365         struct _year *y = p->cur_year;
366         struct appdata* ad = p->ad;
367
368         snprintf(signal_source, 31, "mb/%d/base", p->pos_down);
369         edje_object_signal_emit(y->edj, "mouse,up,1", signal_source);
370
371         if (p->ignore_mouse_down)
372         {
373                 p->ignore_mouse_down = EINA_FALSE;
374                 return;
375         }
376
377         cal_util_update_tm_month(&ad->base_tm, p->pos_down - ad->base_tm.tm_mon);
378
379         if (p->pos_down != p->pos_selected)
380         {
381                 cal_util_emit_signal(y->edj, "unselect,%d", p->pos_selected);
382
383                 cal_util_emit_signal(y->edj, "select,%d", p->pos_down);
384
385                 p->pos_selected = p->pos_down;
386         }
387         else
388         {
389                 cal_main_change_view(p->parent, CV_MONTH);
390         }
391 }
392
393 static void __cal_year_delete_edje_handle(cal_view_main_year_data* p)
394 {
395         c_retm_if(!p, "p is null.");
396
397         int i = 0;
398
399         for (i = 0 ; i < 12 ; i++)
400         {
401                 c_retm_if(!p->edje_handle[i], "p->edje_handle[%d] is null.", i);
402
403                 evas_object_event_callback_del(p->edje_handle[i], EVAS_CALLBACK_MOUSE_DOWN, __cal_year_image_object_mouse_down_callback);
404                 evas_object_event_callback_del(p->edje_handle[i], EVAS_CALLBACK_MOUSE_UP, __cal_year_image_object_mouse_up_callback);
405
406                 evas_object_del(p->edje_handle[i]);
407         }
408
409         c_retm_if(!p->handle_base, "p->handle_base is null.");
410         evas_object_del(p->handle_base);
411 }
412
413 static inline int __cal_year_update_view(cal_view_main_year_data *p)
414 {
415         CAL_FN_START;
416
417         c_retvm_if(!p, -1, "Input parameter is null.");
418         c_retvm_if(!p->ad, -1, "p->ad is null.");
419
420         struct appdata* ad = p->ad;
421
422         int d = p->tm_to.tm_year - p->ad->base_tm.tm_year;
423
424         ad->base_tm = p->tm_to;
425
426         cal_util_emit_signal(p->cur_year->edj, "unselect,%d", p->pos_selected);
427
428         elm_object_part_content_unset(p->ly, "content");
429         elm_object_part_content_unset(p->ly, "content/left");
430         elm_object_part_content_unset(p->ly, "content/right");
431
432         if (d > 0) {
433
434                 if (p->prev_year)
435                         __cal_year_delete_year_object(p->prev_year);
436
437                 p->prev_year = p->cur_year;
438                 p->cur_year = p->next_year;
439                 p->next_year = __cal_year_create_year_object(p->ly, p, CAL_YEAR_NEXT_YEAR);
440
441         } else if ( d < 0) {
442
443                 if (p->next_year)
444                         __cal_year_delete_year_object(p->next_year);
445
446                 p->next_year = p->cur_year;
447                 p->cur_year = p->prev_year;
448                 p->prev_year = __cal_year_create_year_object(p->ly, p, CAL_YEAR_PREV_YEAR);
449         }
450
451         elm_object_part_content_set(p->ly, "content", p->cur_year->image);
452
453         if (p->prev_year)
454                 elm_object_part_content_set(p->ly, "content/left", p->prev_year->image);
455
456         if (p->next_year)
457                 elm_object_part_content_set(p->ly, "content/right", p->next_year->image);
458
459         cal_main_update_title_text(ad->title, CV_YEAR, &ad->base_tm, ad->wday_start);
460
461         __cal_year_update_select(p->cur_year, ad->base_tm.tm_mon);
462
463         __cal_year_move_year_objects(p, 0);
464
465         if (p->ignore_mouse_down)
466                 p->ignore_mouse_down = EINA_FALSE;
467
468         return 0;
469 }
470
471 static Eina_Bool __cal_year_animation(void *data, double pos)
472 {
473         c_retvm_if(!data, ECORE_CALLBACK_CANCEL, "Input parameter is null.");
474
475         cal_view_main_year_data *p = data;
476         Evas_Coord dx;
477
478         dx = (p->mouse_up_position.x - p->pos_to.x) * cal_util_nsin(1.0 - pos);
479         __cal_year_move_year_objects(p, p->pos_to.x + dx);
480
481         if ((dx + p->pos_to.x)%p->ad->win_w == 0)
482         {
483                 p->ani = NULL;
484                 __cal_year_update_view(p);
485                 return ECORE_CALLBACK_CANCEL;
486         }
487
488         return ECORE_CALLBACK_RENEW;
489 }
490
491 static inline void __cal_year_start_animation(cal_view_main_year_data *p)
492 {
493         c_retm_if(!p, "Input parameter is null.");
494
495         if (p->mouse_up_position.x == p->pos_to.x)
496                 return;
497
498         if (!p->ani)
499                 p->ani = ecore_animator_timeline_add(ANIMATION_TIME, __cal_year_animation, p);
500
501         p->ani_start = ecore_time_get();
502 }
503
504 static void inline __cal_year_set_go(cal_view_main_year_data *p, int d)
505 {
506         c_retm_if(!p, "Input parameter is null.");
507
508         int r = 0;
509
510         p->tm_to = p->ad->base_tm;
511         p->pos_to = p->rect;
512
513         if(d != 0)
514                 r = cal_util_update_tm_year(&p->tm_to, d);
515         if (r == -1)
516                 return;
517
518         p->pos_to.x = p->rect.w * (d * -1);
519 }
520
521 static void __cal_year_mouse_up(void *data, Evas *e, Evas_Object *ly, void *ei)
522 {
523         c_retm_if(!data, "Input parameter is null.");
524
525         Evas_Event_Mouse_Up *ev = ei;
526         cal_view_main_year_data *p = data;
527
528         if (!p->downed)
529                 return;
530
531         p->downed = 0;
532
533         cal_util_get_geometry(&p->mouse_up_position, p->cur_year->image);
534
535         if (p->mouse_up_position.x > (p->rect.w >> 2))
536                 __cal_year_set_go(p, -1);
537         else if (p->mouse_up_position.x < (p->rect.w >> 2) * -1)
538                 __cal_year_set_go(p, 1);
539         else
540         {
541                 Evas_Coord x = ev->canvas.x - p->down1.x;
542
543                 if (x < 3 && x > -3 && p->mouse_up_position.x ==0)
544                         return;
545
546                 __cal_year_set_go(p, 0);
547         }
548
549         __cal_year_start_animation(p);
550 }
551
552 static void __cal_year_mouse_move(void *data, Evas *e, Evas_Object *ly, void *ei)
553 {
554         c_retm_if(!data, "Input parameter is null.");
555
556         cal_view_main_year_data *p = data;
557
558         Evas_Event_Mouse_Move *ev = ei;
559         Evas_Coord x;
560         static int cnt = 0;
561
562         if (!p->downed)
563                 return;
564
565         cnt++;
566         if ((cnt & 0x7) != 0) // Workaround
567                 return;
568
569         p->move2.x = ev->cur.canvas.x;
570         p->move2.y = ev->cur.canvas.y;
571
572         x = p->mouse_down_position.x + p->move2.x - p->down1.x;
573
574         __cal_year_move_year_objects(p, x);
575
576         p->ignore_mouse_down = EINA_TRUE;
577 }
578
579 static void __cal_year_mouse_down(void *data, Evas *e, Evas_Object *ly, void *ei)
580 {
581         c_retm_if(!data, "Input parameter is null.");
582
583         cal_view_main_year_data *p = data;
584         struct _year *y = p->cur_year;
585
586         Evas_Event_Mouse_Down *ev = ei;
587
588         p->down1.x = p->move1.x = ev->canvas.x;
589         p->down1.y = p->move1.y = ev->canvas.y;
590
591         cal_util_get_geometry(&p->mouse_down_position, y->image);
592
593         __cal_year_stop_animation(p);
594
595         p->downed = 1;
596 }
597
598 static void __cal_year_update_select(struct _year *y, int tm_mon)
599 {
600         c_retm_if(!y, "Input parameter is null.");
601
602         cal_view_main_year_data* p = y->p;
603
604         if (p->pos_selected != -1)
605                 cal_util_emit_signal(y->edj, "unselect,%d", p->pos_selected);
606
607         cal_util_emit_signal(y->edj, "select,%d", tm_mon);
608
609         p->pos_selected = tm_mon;
610 }
611
612 static Evas_Object *__cal_year_create_image(Evas_Object *parent)
613 {
614         c_retvm_if(!parent, NULL, "Input parameter is null.");
615
616         Evas_Object *img;
617         Evas *e;
618         Ecore_Evas *ee;
619         Evas_Coord w, h;
620
621         ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
622
623         e = evas_object_evas_get(parent);
624         ee = ecore_evas_ecore_evas_get(e);
625
626         img = ecore_evas_object_image_new(ee);
627         if (!img)
628                 return NULL;
629
630         evas_object_image_alpha_set(img, EINA_TRUE);
631         evas_object_image_size_set(img, w, h);
632         evas_object_image_fill_set(img, 0, 0, w, h);
633
634         evas_object_show(img);
635
636         return img;
637 }
638
639 static Evas_Object *__cal_year_add_edje(Evas *e, const char *grp, Evas_Coord w, Evas_Coord h)
640 {
641         c_retvm_if(!e, NULL, "Input parameter is null.");
642
643         Evas_Object *eo;
644         Eina_Bool r;
645
646         eo = edje_object_add(e);
647         if (!eo)
648                 return NULL;
649
650         if(cal_util_black_theme_check()){
651                 r = edje_object_file_set(eo, EDJ_FILE, grp);
652         }
653         else{
654                 r = edje_object_file_set(eo, EDJ_FILE_WHITE, grp);
655         }
656
657         if (!r)
658         {
659                 evas_object_del(eo);
660                 return NULL;
661         }
662
663         evas_object_freeze_events_set(eo, EINA_TRUE);
664
665         evas_object_resize(eo, w, h);
666         evas_object_move(eo, 0, 0);
667         evas_object_show(eo);
668
669         return eo;
670 }
671
672 static void __cal_year_image_object_resize_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
673 {
674         c_retm_if(!data, "Input parameter is null.");
675
676         struct _year *y = data;
677         c_retm_if(!y->edj, "y->edj is null.");
678         c_retm_if(!y->p, "y->p is null.");
679
680         cal_view_main_year_data *p = y->p;
681
682         cal_util_get_geometry(&p->rect, obj);
683
684         evas_object_resize(y->edj, p->rect.w, p->rect.h);
685         evas_object_move(y->edj, 0, 0);
686 }
687
688 static void __cal_year_image_object_move_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
689 {
690         c_retm_if(!data, "Input parameter is null.");
691
692         struct _year *y = data;
693
694         evas_object_move(y->edj, 0, 0);
695 }
696
697 static void __cal_year_delete_year_object(struct _year* y)
698 {
699         CAL_FN_START;
700
701         c_retm_if(!y, "Input parameter is null.");
702
703         evas_object_del(y->image);
704         evas_object_del(y->edj);
705
706         free(y);
707 }
708
709 static struct _year* __cal_year_create_year_object(Evas_Object* ly, cal_view_main_year_data* p, enum year_index year)
710 {
711         c_retvm_if(!ly, NULL, "Input parameter is null.");
712         c_retvm_if(!p, NULL, "Input parameter is null.");
713         c_retvm_if(!p->ad, NULL, "p->ad is null.");
714
715         struct appdata* ad = p->ad;
716         c_retv_if(!ad, NULL);
717
718         struct tm tm = ad->base_tm;
719
720         int r = cal_util_update_tm_year(&tm, year);
721         c_retvm_if(r < 0, NULL, "cal_util_update_tm_year(%d, %d is failed", tm.tm_year, year);
722
723         struct _year* y = calloc(1, sizeof(struct _year));
724         c_retv_if(!y, NULL);
725
726         y->image = __cal_year_create_image(ly);
727
728         y->evas = ecore_evas_object_evas_get(y->image);
729
730         y->edj = __cal_year_add_edje(y->evas, "main/year/months", p->rect.w, p->rect.h);
731
732         y->p = p;
733
734         if(year > 0)
735                 elm_object_part_content_set(ly, "content/right", y->image);
736         else if( year < 0)
737                 elm_object_part_content_set(ly, "content/left", y->image);
738         else
739                 elm_object_part_content_set(ly, "content", y->image);
740
741         y->ly = ly;
742         y->year = tm.tm_year;
743
744         evas_object_event_callback_add(y->image, EVAS_CALLBACK_RESIZE, __cal_year_image_object_resize_callback, y);
745         evas_object_event_callback_add(y->image, EVAS_CALLBACK_MOVE, __cal_year_image_object_move_callback, y);
746
747         __cal_year_set_month_text(y);
748         __cal_year_fill_months(y);
749
750         return y;
751 }
752
753 static void __cal_year_create_edje_handle(cal_view_main_year_data* p)
754 {
755         c_retm_if(!p, "p is null.");
756
757         Evas * e = evas_object_evas_get(p->ly);
758         int i = 0;
759         Evas_Object *ly = NULL;
760         Evas_Object *handle = NULL;
761         char part[16];
762
763         ly = cal_util_add_layout(p->ly, "year/edje/handle");
764         c_retm_if(!ly, "ly is null.");
765
766         for (i = 0 ; i < 12 ; i++)
767         {
768                 handle = evas_object_rectangle_add(e);
769                 c_retm_if(!handle, "evas_object_rectangle_add returned null.");
770
771                 evas_object_color_set(handle, 0, 0, 0, 0);
772                 evas_object_data_set(handle, "index", (void*)i);
773                 snprintf(part, sizeof(part), "handle/%d/sw", i);
774                 elm_object_part_content_set(ly, part, handle);
775
776                 evas_object_event_callback_add(handle, EVAS_CALLBACK_MOUSE_DOWN, __cal_year_image_object_mouse_down_callback, p);
777                 evas_object_event_callback_add(handle, EVAS_CALLBACK_MOUSE_UP, __cal_year_image_object_mouse_up_callback, p);
778
779                 p->edje_handle[i] = handle;
780         }
781
782         elm_object_part_content_set(p->ly, "edje/handle", ly);
783
784         p->handle_base = ly;
785
786 }
787 Evas_Object* cal_year_create_view(struct appdata *ad, Evas_Object *main)
788 {
789         c_retvm_if(!ad, NULL, "Input parameter is null.");
790         c_retvm_if(!main, NULL, "Input parameter is null.");
791
792         cal_view_main_year_data *p;
793         Evas_Object* ly = NULL;
794
795         CAL_CALLOC(p, 1, cal_view_main_year_data);
796
797         p->name = _name;
798         p->ad = ad;
799         p->parent = main;
800
801         edje_object_part_geometry_get(CAL_UTIL_GET_EDJ_DATA(ad->main), "cont", NULL, NULL, &p->rect.w, &p->rect.h);
802
803         memset(p->wday, 0, 8*7);
804         memset(p->month, 0, 32*12);
805
806         __cal_year_make_month_text(p);
807
808         ly = cal_util_add_layout(p->ad->nv, "year/layout");
809         if (!ly)
810         {
811                 free(p);
812                 return NULL;
813         }
814
815         evas_object_data_set(ly, "priv", p);
816         p->ly = ly;
817
818         p->pos_selected = -1;
819
820         __cal_year_create_edje_handle(p);
821
822         evas_object_event_callback_add(ly, EVAS_CALLBACK_DEL, __cal_year_delete_layout, p);
823         evas_object_event_callback_add(ly, EVAS_CALLBACK_MOUSE_DOWN, __cal_year_mouse_down, p);
824         evas_object_event_callback_add(ly, EVAS_CALLBACK_MOUSE_UP, __cal_year_mouse_up, p);
825         evas_object_event_callback_add(ly, EVAS_CALLBACK_MOUSE_MOVE, __cal_year_mouse_move, p);
826
827         return p->ly;
828 }
829
830 static Eina_Bool __cal_year_create_year_with_idler(void* data)
831 {
832         CAL_FN_START;
833
834         c_retvm_if(!data, ECORE_CALLBACK_CANCEL, "Input param is null.");
835
836         cal_view_main_year_data* p = data;
837
838         c_retvm_if(!p->ad, ECORE_CALLBACK_CANCEL, "p->ad is null.");
839
840         if(p->prev_year)
841                 __cal_year_delete_year_object(p->prev_year);
842
843         p->prev_year = __cal_year_create_year_object(p->ly, p, CAL_YEAR_PREV_YEAR);
844         c_retvm_if(!p->prev_year, ECORE_CALLBACK_CANCEL, "__cal_year_create_year_object returned null.");
845
846         if(p->next_year)
847                 __cal_year_delete_year_object(p->next_year);
848
849         p->next_year = __cal_year_create_year_object(p->ly, p, CAL_YEAR_NEXT_YEAR);
850         c_retvm_if(!p->next_year, ECORE_CALLBACK_CANCEL, "__cal_year_create_year_object returned null.");
851
852         p->idler = NULL;
853
854         return ECORE_CALLBACK_CANCEL;
855 }
856
857 static void __cal_year_update_current_year(cal_view_main_year_data* p)
858 {
859         CAL_FN_START;
860
861         c_retm_if(!p, "Input parameter is null.");
862
863         p->cur_year = __cal_year_create_year_object(p->ly, p, CAL_YEAR_CURR_YEAR);
864         c_retm_if(!p->cur_year, "__cal_year_create_year_object returned null.");
865
866         elm_object_part_content_set(p->ly, "content", p->cur_year->image);
867 }
868
869 void cal_year_update_view(Evas_Object *ly)
870 {
871         CAL_FN_START;
872
873         c_retm_if(!ly, "Input parameter is null.");
874
875         cal_view_main_year_data *p;
876         struct appdata *ad;
877
878         p = CAL_UTIL_GET_PRIV_DATA(ly);
879         if (!p || CAL_STRCMP(p->name, _name))
880         {
881                 ERR("update year view: Invalid object");
882                 return;
883         }
884
885         ad = p->ad;
886         c_retm_if(!ad, "p->ad is null.");
887
888         __cal_year_update_current_year(p);
889
890         if(p->idler)
891         {
892                 ecore_idler_del(p->idler);
893                 p->idler = NULL;
894         }
895
896         p->idler = ecore_idler_add(__cal_year_create_year_with_idler, (void*)p);
897
898         __cal_year_update_select(p->cur_year, ad->base_tm.tm_mon);
899 }
900