1d13f9a6dae2fe2b8db5372479f0928084d24a06
[apps/home/memo.git] / src / memo_edit.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.1 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *    http://floralicense.org/license/
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <appcore-efl.h>
22 #include <Elementary.h>
23 #include <memo.h>
24 #include <memo_text_editor.h>
25 #include <memo_doodle_editor.h>
26 #include <extended-elm.h>
27 #include "memo_log.h"
28
29 typedef struct __edit_view_t {
30     appdata *ad;
31     Evas_Object *navigator;
32     Elm_Object_Item *navi_it;
33     Evas_Object *body_main;    /* content */
34     Evas_Object *l_btn;
35     Evas_Object *r_btn;
36     Eina_Bool drawing;    /* drawing or text mode */
37
38     /* add more variables here */
39     void *h_ed;
40 } edit_view_t;
41
42 extern Eina_Bool launch_by_new;
43 extern Eina_Bool is_previous_edit_view;
44 extern Eina_Bool normal_memo_launch;
45 static void _text_editor_callback(void *data, const char *msg, void *event);
46 static void _doodle_editor_callback(void *data, const char *msg, void *event);
47
48 static void _on_left_btn_clicked(void *data, Evas_Object *obj, void *event_info)
49 {
50     MEMO_FUN_BEG();
51     edit_view_t *ev = (edit_view_t *)data;
52     if (ev->drawing) {
53         memo_doodle_editor_save(ev->h_ed);
54     } else {
55         memo_text_editor_save(ev->h_ed);
56     }
57     MEMO_FUN_END();
58 }
59
60 static void _on_right_btn_clicked(void *data, Evas_Object *obj, void *event_info)
61 {
62     MEMO_FUN_BEG();
63     edit_view_t *ev = (edit_view_t *)data;
64     if (ev->drawing) {
65         memo_doodle_editor_cancel(ev->h_ed);
66     } else {
67         memo_text_editor_cancel(ev->h_ed);
68     }
69     MEMO_FUN_END();
70 }
71
72 static void _text_editor_callback(void *data, const char *msg, void *event)
73 {
74         MEMO_FUN_BEG();
75         edit_view_t *ev = (edit_view_t *)data;
76         if (strcmp(msg, "layout") == 0) {
77                 elm_object_part_content_set(ev->body_main, "elm.swallow.content", (Evas_Object *)event);
78         } else if (strcmp(msg, "title") == 0) {
79                 elm_object_item_text_set(ev->navi_it, (const char *)event);
80         } else if (strcmp(msg, "savable") == 0) {
81                 elm_object_disabled_set(ev->l_btn, (int)event==0? EINA_TRUE:EINA_FALSE);
82         } else if (strcmp(msg, "save") == 0) {
83                 ev->ad->index = ev->ad->index_detail;
84                 memo_navigator_pop(ev->navigator);
85                 if(normal_memo_launch && launch_by_new ) {
86                         elm_win_lower(ev->ad->win_main);
87                         launch_by_new = EINA_FALSE ;
88                 }
89                 if(is_previous_edit_view) {
90                         is_previous_edit_view = EINA_FALSE ;
91                 }
92         } else if (strcmp(msg, "cancel") == 0) {
93                 ev->ad->index = ev->ad->index_detail;
94                 memo_navigator_pop(ev->navigator);
95                 if(normal_memo_launch && launch_by_new ) {
96                         LOGD("lower lower");
97                         elm_win_lower(ev->ad->win_main);
98                         launch_by_new = EINA_FALSE ;
99                 }
100                 if(is_previous_edit_view) {
101                         is_previous_edit_view = EINA_FALSE ;
102                 }
103
104         } else if (strcmp(msg, "drawing") == 0) {
105                 elm_object_part_content_unset(ev->body_main, "elm.swallow.content");
106                 memo_destroy_text_editor(ev->h_ed);
107                 ev->h_ed = memo_load_doodle_editor(ev->ad->win_main, ev->body_main, NULL, _doodle_editor_callback, ev);
108                 ev->drawing = EINA_TRUE;
109         }
110         MEMO_FUN_END();
111 }
112
113 static void _doodle_editor_callback(void *data, const char *msg, void *event)
114 {
115         MEMO_FUN_BEG();
116         edit_view_t *ev = (edit_view_t *)data;
117         if (strcmp(msg, "layout") == 0) {
118                 elm_object_part_content_set(ev->body_main, "elm.swallow.content", (Evas_Object *)event);
119         } else if (strcmp(msg, "title") == 0) {
120                 elm_object_item_text_set(ev->navi_it, (const char *)event);
121         } else if (strcmp(msg, "savable") == 0) {
122                 elm_object_disabled_set(ev->l_btn, (int)event==0? EINA_TRUE:EINA_FALSE);
123         } else if (strcmp(msg, "save") == 0) {
124                 ev->ad->index = ev->ad->index_detail;
125                 memo_navigator_pop(ev->navigator);
126                 if(normal_memo_launch && launch_by_new ) {
127                         elm_win_lower(ev->ad->win_main);
128                         launch_by_new = EINA_FALSE ;
129                 }
130                 if(is_previous_edit_view) {
131                         is_previous_edit_view = EINA_FALSE ;
132                 }
133         } else if (strcmp(msg, "cancel") == 0) {
134                 ev->ad->index = ev->ad->index_detail;
135                 memo_navigator_pop(ev->navigator);
136                 if(normal_memo_launch && launch_by_new ) {
137                         elm_win_lower(ev->ad->win_main);
138                         launch_by_new = EINA_FALSE ;
139                 }
140                 if(is_previous_edit_view) {
141                         is_previous_edit_view = EINA_FALSE ;
142                 }
143         } else if (strcmp(msg, "text") == 0) {
144                 elm_object_part_content_unset(ev->body_main, "elm.swallow.content");
145                 memo_destroy_doodle_editor(ev->h_ed);
146                 ev->h_ed = memo_load_text_editor(ev->ad->win_main, ev->body_main, NULL, _text_editor_callback, ev);
147                 ev->drawing = EINA_FALSE;
148         }
149         MEMO_FUN_END();
150 }
151
152 static void _edit_on_delete(void *data, Evas *e, Evas_Object *obj, void *event_info)
153 {
154     MEMO_FUN_BEG();
155     edit_view_t *ev = (edit_view_t *)data;
156     elm_object_part_content_unset(ev->body_main, "elm.swallow.content");
157     if (ev->drawing) {
158         memo_destroy_doodle_editor(ev->h_ed);
159     } else {
160         memo_destroy_text_editor(ev->h_ed);
161     }
162     evas_object_del(ev->l_btn);
163     evas_object_del(ev->r_btn);
164     SFREE(ev);
165     MEMO_FUN_END();
166 }
167
168 static void _on_update(void *data, Evas_Object *obj, void *event_info)
169 {
170     MEMO_FUN_BEG();
171     edit_view_t *ev = (edit_view_t *)data;
172     if (ev->drawing) {
173         memo_doodle_editor_time_format_update(ev->h_ed);
174     } else {
175         memo_text_editor_time_format_update(ev->h_ed);
176     }
177     MEMO_FUN_END();
178 }
179
180 void *memo_load_edit_view(appdata *ad)
181 {
182         MEMO_FUN_BEG();
183
184         if (ad->index == -1) {
185         int c = 0;
186         if (memo_get_count(&c) == 0) {
187                 RETVIF(c == MEMO_MAX_RECORD, NULL);
188         }
189         }
190
191         edit_view_t *ev = SMALLOC(edit_view_t);
192         RETVIF(ev == NULL, NULL);
193         is_previous_edit_view = EINA_TRUE;
194         ev->ad = ad;
195
196         ev->navigator = ad->navigator;
197         /* body main */
198         ev->body_main = elm_layout_create(ev->navigator, EDJ_FILE, "empty_layout");
199         elm_naviframe_prev_btn_auto_pushed_set(ev->navigator, EINA_FALSE);
200         Elm_Object_Item *navi_it = memo_naviframe_item_push(ev->navigator, "", NULL, NULL, ev->body_main, NULL);
201         ev->navi_it = navi_it;
202         evas_object_event_callback_add(ev->body_main, EVAS_CALLBACK_DEL, _edit_on_delete, ev);
203         evas_object_data_set(ev->body_main, "time_format_update_cb", _on_update);
204         evas_object_data_set(ev->body_main, "time_format_update_data", ev);
205         /* delete "<-" button */
206         Evas_Object *btn = elm_object_item_part_content_get(navi_it, MEMO_NAVIFRAME_ITEM_PREV_BTN);
207         if (btn != NULL) {
208                 evas_object_del(btn);
209         }
210         ev->r_btn = elm_button_create(ev->navigator, MEMO_I18N_CANCEL,
211                                                                         _on_right_btn_clicked, ev);
212     elm_object_style_set(ev->r_btn, "naviframe/back_btn/default");
213         elm_object_item_part_content_set(navi_it, "title_prev_btn", ev->r_btn);
214         ev->l_btn = elm_button_create(ev->navigator, MEMO_I18N_SAVE,
215                                                                                 _on_left_btn_clicked, ev);
216     elm_object_style_set(ev->l_btn, "naviframe/toolbar/default");
217         elm_object_item_part_content_set(navi_it, "title_toolbar_button1", ev->l_btn);
218
219         /*show the toolbar*/
220         elm_object_item_signal_emit(navi_it, "elm,state,sip,shown", "");
221
222         /* load editor */
223         service_h service = NULL;
224         service_create(&service);
225         if (ad->init_str != NULL) {
226                 service_add_extra_data(service, "init_str", ad->init_str);
227                 SFREE(ad->init_str);
228         }
229         if (ad->index == -1) {
230                 ev->h_ed = memo_load_text_editor(ev->ad->win_main, ev->body_main, service, _text_editor_callback, ev);
231                 ev->drawing = EINA_FALSE;
232         } else {
233                 memo_data_t *md = memo_get_data(ad->index);
234                 snprintf(ev->ad->buf, MEMO_BUFFER_SIZE, "%d", ad->index);
235                 service_add_extra_data(service, "index", ev->ad->buf);
236                 if (md->has_doodle == 1) {
237                         ev->h_ed = memo_load_doodle_editor(ev->ad->win_main, ev->body_main, service, _doodle_editor_callback, ev);
238                         ev->drawing = EINA_TRUE;
239                 } else {
240                         ev->h_ed = memo_load_text_editor(ev->ad->win_main, ev->body_main, service, _text_editor_callback, ev);
241                         ev->drawing = EINA_FALSE;
242                 }
243                 memo_free_data(md);
244         }
245         service_destroy(service);
246         MEMO_FUN_END();
247         return (void *)ev;
248 }
249