also support efl-1.7 with tizen patches
[apps/core/preloaded/calculator.git] / src / calc-main.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 #include <dlog.h>
19 #include <feedback.h>
20 #include "calc-main.h"
21 #include "calc-view.h"
22 #include <vconf.h>
23
24 extern char calculator_input_str[];
25 extern int calculator_cursor_pos;
26
27 extern int cur_fontsize;
28 extern int default_fontsize;
29 extern int min_len;
30 extern int max_len;
31 extern int small_fontsize;
32 extern int scientific_result_len;
33
34 extern void calc_xxx(struct appdata *ap);       /* will be removed */
35 extern void _calc_entry_text_set_rotate(struct appdata *ad);
36 extern void calc_por_pannel_load(struct appdata *ad);
37 extern void calc_lans_pannel_load(struct appdata *ad);
38 Evas_Object *load_edj(Evas_Object * parent, const char *file, const char *group)
39 {
40         CALC_FUN_BEG();
41         Evas_Object *eo;
42         int r;
43         eo = elm_layout_add(parent);
44         if (eo) {
45                 r = elm_layout_file_set(eo, file, group);
46                 if (!r) {
47                         evas_object_del(eo);
48                         return NULL;
49                 }
50                 evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND,
51                                                  EVAS_HINT_EXPAND);
52         }
53         CALC_FUN_END();
54         return eo;
55 }
56
57 /**
58 * @describe
59 * @When rotate to landscape view (Scientific calculation)
60 * @param[in]    ad      The appdata
61 * @param[in]    angle   The rotate angle 90 or 270
62 * @return       void
63 * @exception
64 */
65 static void __to_landscape(struct appdata *ad, int angle)
66 {
67         CALC_FUN_BEG();
68         if (ad == NULL) {
69                 return;
70         }
71         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
72         calc_lans_pannel_load(ad);
73         edje_object_signal_emit(_EDJ(ad->edje), "landscape", "");
74         elm_win_rotation_with_resize_set(ad->win, angle);
75         //elm_win_rotation_with_resize_set(ad->eo, angle);/*when rotating, resize the window eo*/
76         default_fontsize = 98;
77         cur_fontsize = 98;
78         small_fontsize = 98;
79         min_len = 30;
80         max_len = 37;
81         scientific_result_len = 13;
82         _calc_entry_text_set_rotate(ad);
83         if (!ad->panel_show) {
84                 calc_view_show_histroy(ad->hist_area);
85                 edje_object_signal_emit(_EDJ(ad->edje), "show,hist", "");
86         }
87         CALC_FUN_END();
88 }
89
90 /**
91 * @describe
92 * @When rotate to protrait view (Basic calculation)
93 * @param[in]    ad      The appdata
94 * @param[in]    angle   The rotate angle 0 or 180
95 * @return       void
96 * @exception
97 */
98 static void __to_portrait(struct appdata *ad, int angle)
99 {
100     CALC_FUN_BEG();
101         if (ad == NULL) {
102                 return;
103         }
104         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
105         calc_por_pannel_load(ad);
106         edje_object_signal_emit(_EDJ(ad->edje), "portrait", "");
107         elm_win_rotation_with_resize_set(ad->win, angle);
108         //elm_win_rotation_with_resize_set(ad->eo, angle);/*when rotating, resize the window eo*/
109         default_fontsize = 70;
110         cur_fontsize = 70;
111         small_fontsize = 58;
112         min_len = 13;
113         max_len = 16;
114         scientific_result_len = 8;
115         _calc_entry_text_set_rotate(ad);
116         if (!ad->panel_show) {
117                 calc_view_show_histroy(ad->hist_area);
118                 edje_object_signal_emit(_EDJ(ad->edje), "show,hist", "");
119         }
120         CALC_FUN_END();
121 }
122
123 /**
124 * @describe
125 *
126 *
127 * @param    mode
128 * @param    data
129 * @return    int
130 * @exception
131 */
132 static  void _rotate(app_device_orientation_e  mode, void *data)
133 {
134         CALC_FUN_BEG();
135         if (data == NULL) {
136                 return;
137         }
138         struct appdata *ad = (struct appdata *)data;
139
140         switch (mode) {
141         case APP_DEVICE_ORIENTATION_0:  //[1]0
142                 __to_portrait(ad, 0);
143                 break;
144
145         case APP_DEVICE_ORIENTATION_180:        //[2]180
146                 __to_portrait(ad, 180);
147                 break;
148
149         case APP_DEVICE_ORIENTATION_270:        //[3]-90
150                 __to_landscape(ad, 270);
151                 break;
152
153         case APP_DEVICE_ORIENTATION_90: //[4]90
154                 __to_landscape(ad, 90);
155                 break;
156
157         default:
158                 break;
159         }
160                 if (!ad->panel_show) {
161                         calc_view_revise_history_scroller(ad);
162                 } else {
163                         calc_view_revise_input_scroller(ad);
164                 }
165
166         /* When rotate, the size of input scroller will be changed. So it should adjust to cursor position. */
167         calc_view_revise_input_scroller(ad);
168         CALC_FUN_END();
169 }
170
171 /**
172 * @describe
173 *
174 *
175 * @param    data
176 * @param    obj
177 * @param    event_info
178 * @return    void
179 * @exception
180 */
181 static void _win_del(void *data, Evas_Object * obj, void *event_info)
182 {
183         elm_exit();
184 }
185
186 /**
187 * @describe
188 *
189 *
190 * @param    data
191 * @return    int
192 * @exception
193 */
194 static int _set_input_entry_focus(void *data)
195 {
196         CALC_FUN_BEG();
197         struct appdata *ad = (struct appdata *)data;
198         elm_object_focus_set(ad->input_entry, EINA_TRUE);       //set focus
199         _calc_entry_clear(ad->input_entry);
200         CALC_FUN_END();
201         return ECORE_CALLBACK_CANCEL;   //0
202 }
203
204 /**
205 * @describe
206 *
207 *
208 * @param    data
209 * @return    int
210 * @exception
211 */
212 static int _load_idle_view(void *data)
213 {
214         CALC_FUN_BEG();
215         struct appdata *ad = (struct appdata *)data;
216         calc_view_load_in_idle(ad);
217         CALC_FUN_END();
218         return ECORE_CALLBACK_CANCEL;   //0
219 }
220
221 /**
222 * @describe
223 *
224 *
225 * @param    name
226 * @return    Evas_Object*
227 * @exception
228 */
229 static Evas_Object *_create_win(const char *name)
230 {
231     CALC_FUN_BEG();
232         Evas_Object *eo;
233         int w,h;
234         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
235         if (eo) {
236                 elm_win_title_set(eo, name);
237                 evas_object_smart_callback_add(eo, "delete,request", _win_del,
238                                                NULL);
239         elm_win_screen_size_get(eo, NULL, NULL, &w, &h);
240         evas_object_resize(eo, w, h);
241         }
242         CALC_FUN_END();
243         return eo;
244 }
245
246 /**
247 * @describe
248 *
249 *
250 * @param    ad
251 * @return    void
252 * @exception
253 */
254 static void _on_exit(struct appdata *ad)
255 {
256         CALC_FUN_BEG();
257         if (ad->por_pannel) {
258                 evas_object_del(ad->por_pannel);
259                 ad->por_pannel = NULL;
260         }
261
262         if (ad->lan_pannel) {
263                 evas_object_del(ad->lan_pannel);
264                 ad->lan_pannel = NULL;
265         }
266
267         if (ad->input_entry) {
268                 evas_object_del(ad->input_entry);
269                 ad->input_entry = NULL;
270         }
271         if (ad->clear_item) {
272                 elm_object_item_del(ad->clear_item);
273                 ad->clear_item = NULL;
274         }
275         if (ad->more_btn_popup) {
276                 evas_object_del(ad->more_btn_popup);
277                 ad->more_btn_popup = NULL;
278         }
279         if (ad->more_btn) {
280                 evas_object_del(ad->more_btn);
281                 ad->more_btn = NULL;
282         }
283         if (ad->navi_it) {
284                 elm_object_item_del(ad->navi_it);
285                 ad->navi_it = NULL;
286         }
287
288         if (ad->nf) {
289                 evas_object_del(ad->nf);
290                 ad->nf = NULL;
291         }
292          if (ad->window_icon) {
293                 evas_object_del(ad->window_icon);
294                 ad->window_icon = NULL;
295         }
296 #ifdef SAVE_HISTORY
297         if (ad->hist_area) {
298                 evas_object_del(ad->hist_area);
299                 ad->hist_area = NULL;
300         }
301
302         if (ad->hist_scroll) {
303                 evas_object_del(ad->hist_scroll);
304                 ad->hist_scroll = NULL;
305         }
306 #endif
307
308         if (ad->edje) {
309                 evas_object_del(ad->edje);
310                 ad->edje = NULL;
311         }
312
313         if (ad->layout) {
314                 evas_object_del(ad->layout);
315                 ad->layout = NULL;
316         }
317
318         if (ad->bg) {
319                 evas_object_del(ad->bg);
320                 ad->bg = NULL;
321         }
322
323         if (ad->win) {
324                 evas_object_del(ad->win);
325                 ad->win = NULL;
326         }
327
328         /* delete timer */
329         if (ad->calc_timer) {
330                 ecore_timer_del(ad->calc_timer);
331                 ad->calc_timer = NULL;
332         }
333         if (ad->wrong_timer) {
334                 ecore_timer_del(ad->wrong_timer);
335                 ad->wrong_timer = NULL;
336         }
337
338         feedback_deinitialize();
339
340         CALC_FUN_END();
341 }
342
343 /**
344 * @describe
345 *
346 *
347 * @param    data
348 * @return    int
349 * @exception
350 */
351 static bool app_create(void *data)
352 {
353         CALC_FUN_BEG();
354         struct appdata *ad = (struct appdata *)data;
355
356         /*  Use elm_theme_extension_add() API before creating any widgets */
357         elm_theme_extension_add(NULL, CALCULATOR_THEME);
358
359         /* main widnow */
360         ad->win = _create_win(PACKAGE);
361         if (ad->win == NULL) {
362                 return EINA_FALSE;
363         }
364         evas_object_smart_callback_add(ad->win, "profile,changed", win_profile_changed_cb, ad);
365         evas_object_show(ad->win);
366
367         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
368
369         /* will be removed */
370         calc_xxx(ad);
371
372         /* load main view */
373         calc_view_load(ad);
374         ad->panel_show = EINA_TRUE;
375
376         int  value = 1;
377         int ret = vconf_get_bool(VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL, &value);
378         if (ret == 0) {
379                 if (value == 1) {
380                         _rotate(APP_DEVICE_ORIENTATION_0, ad);
381                 } else {
382                         app_device_orientation_e curr = app_get_device_orientation();
383                         _rotate(curr, ad);
384                 }
385         } else {
386                 app_device_orientation_e curr = app_get_device_orientation();
387                 _rotate(curr, ad);
388         }
389         /* register callback */
390         ecore_idler_add((Ecore_Task_Cb) _set_input_entry_focus, ad);
391         ecore_idler_add((Ecore_Task_Cb) _load_idle_view, ad);
392         CALC_FUN_END();
393         return EINA_TRUE;               //EXIT_SUCCESS
394
395 }
396
397 /**
398 * @describe
399 *
400 *
401 * @param    data
402 * @return    int
403 * @exception
404 */
405 static void app_terminate(void *data)
406 {
407         // Release all resources
408         struct appdata *ad = (struct appdata *)data;
409         _on_exit(ad);
410 }
411
412 /**
413 * @describe
414 *
415 *
416 * @param    data
417 * @return    int
418 * @exception
419 */
420 static void app_pause(void *data)
421 {
422         // Take necessary actions when application becomes invisible
423 }
424
425 /**
426 * @describe
427 *
428 *
429 * @param    data
430 * @return    int
431 * @exception
432 */
433 static void app_resume(void *data)
434 {
435         // Take necessary actions when application becomes visible.
436         struct appdata *ad = (struct appdata *)data;
437         elm_object_focus_set(ad->input_entry, EINA_TRUE);
438 }
439
440 /**
441 * @describe
442 *   The entry of the program
443 *
444 * @param    argc
445 * @param    argv
446 * @param    int
447 * @exception
448 */
449 int main(int argc, char *argv[])
450 {
451     CALC_FUN_BEG();
452         struct appdata ad;
453
454         app_event_callback_s event_callback;
455
456         event_callback.create = app_create;
457         event_callback.terminate = app_terminate;
458         event_callback.pause = app_pause;
459         event_callback.resume = app_resume;
460         event_callback.service = NULL;
461         event_callback.low_memory = NULL;
462         event_callback.low_battery = NULL;
463         event_callback.device_orientation = _rotate;
464         event_callback.language_changed = NULL;
465         event_callback.region_format_changed = NULL;
466
467         memset(&ad, 0x0, sizeof(struct appdata));
468         CALC_FUN_END();
469         return app_efl_main(&argc, &argv, &event_callback, &ad);
470 }
471