fix: Remove a strong theme dependency for the application layout.
[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->bg) {
323                 evas_object_del(ad->bg);
324                 ad->bg = NULL;
325         }
326
327         if (ad->win) {
328                 evas_object_del(ad->win);
329                 ad->win = NULL;
330         }
331
332         /* delete timer */
333         if (ad->calc_timer) {
334                 ecore_timer_del(ad->calc_timer);
335                 ad->calc_timer = NULL;
336         }
337         if (ad->wrong_timer) {
338                 ecore_timer_del(ad->wrong_timer);
339                 ad->wrong_timer = NULL;
340         }
341
342         feedback_deinitialize();
343
344         CALC_FUN_END();
345 }
346
347 /**
348 * @describe
349 *
350 *
351 * @param    data
352 * @return    int
353 * @exception
354 */
355 static bool app_create(void *data)
356 {
357         CALC_FUN_BEG();
358         struct appdata *ad = (struct appdata *)data;
359
360         /*  Use elm_theme_extension_add() API before creating any widgets */
361         elm_theme_extension_add(NULL, CALCULATOR_THEME);
362
363         /* main widnow */
364         ad->win = _create_win(PACKAGE);
365         if (ad->win == NULL) {
366                 return EINA_FALSE;
367         }
368         evas_object_smart_callback_add(ad->win, "profile,changed", win_profile_changed_cb, ad);
369         evas_object_show(ad->win);
370
371         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
372
373         /* will be removed */
374         calc_xxx(ad);
375
376         /* load main view */
377         calc_view_load(ad);
378         ad->panel_show = EINA_TRUE;
379
380         int  value = 1;
381         int ret = vconf_get_bool(VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL, &value);
382         if (ret == 0) {
383                 if (value == 1) {
384                         _rotate(APP_DEVICE_ORIENTATION_0, ad);
385                 } else {
386                         app_device_orientation_e curr = app_get_device_orientation();
387                         _rotate(curr, ad);
388                 }
389         } else {
390                 app_device_orientation_e curr = app_get_device_orientation();
391                 _rotate(curr, ad);
392         }
393         /* register callback */
394         ecore_idler_add((Ecore_Task_Cb) _set_input_entry_focus, ad);
395         ecore_idler_add((Ecore_Task_Cb) _load_idle_view, ad);
396         CALC_FUN_END();
397         return EINA_TRUE;               //EXIT_SUCCESS
398
399 }
400
401 /**
402 * @describe
403 *
404 *
405 * @param    data
406 * @return    int
407 * @exception
408 */
409 static void app_terminate(void *data)
410 {
411         // Release all resources
412         struct appdata *ad = (struct appdata *)data;
413         _on_exit(ad);
414 }
415
416 /**
417 * @describe
418 *
419 *
420 * @param    data
421 * @return    int
422 * @exception
423 */
424 static void app_pause(void *data)
425 {
426         // Take necessary actions when application becomes invisible
427 }
428
429 /**
430 * @describe
431 *
432 *
433 * @param    data
434 * @return    int
435 * @exception
436 */
437 static void app_resume(void *data)
438 {
439         // Take necessary actions when application becomes visible.
440         struct appdata *ad = (struct appdata *)data;
441         elm_object_focus_set(ad->input_entry, EINA_TRUE);
442 }
443
444 /**
445 * @describe
446 *   The entry of the program
447 *
448 * @param    argc
449 * @param    argv
450 * @param    int
451 * @exception
452 */
453 int main(int argc, char *argv[])
454 {
455     CALC_FUN_BEG();
456         struct appdata ad;
457
458         app_event_callback_s event_callback;
459
460         event_callback.create = app_create;
461         event_callback.terminate = app_terminate;
462         event_callback.pause = app_pause;
463         event_callback.resume = app_resume;
464         event_callback.service = NULL;
465         event_callback.low_memory = NULL;
466         event_callback.low_battery = NULL;
467         event_callback.device_orientation = _rotate;
468         event_callback.language_changed = NULL;
469         event_callback.region_format_changed = NULL;
470
471         memset(&ad, 0x0, sizeof(struct appdata));
472         CALC_FUN_END();
473         return app_efl_main(&argc, &argv, &event_callback, &ad);
474 }
475