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