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