summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavi Artigas <xavierartigas@yahoo.es>2019-10-23 13:39:47 +0200
committerXavi Artigas <xavierartigas@yahoo.es>2019-10-23 17:11:33 +0200
commit8caeaaf071150cb6d6b51e94fddf1c3141cc9f81 (patch)
tree9e6571f39dec69684dba0f17ec91ee3b42a3b157
parent95c373e8e3f89e2cb530406fd81aab340619fc9b (diff)
Example C Calculator app
For simplicity, only integer operations and no direct editing of the numbers. This is meant as a simple UI example, not as a CS exercise.
-rw-r--r--apps/c/calculator/meson.build13
-rw-r--r--apps/c/calculator/src/calculator.c186
-rw-r--r--apps/c/calculator/src/meson.build12
3 files changed, 211 insertions, 0 deletions
diff --git a/apps/c/calculator/meson.build b/apps/c/calculator/meson.build
new file mode 100644
index 00000000..16daf601
--- /dev/null
+++ b/apps/c/calculator/meson.build
@@ -0,0 +1,13 @@
1project(
2 'efl-example-calculator', 'c',
3 version : '0.0.1',
4 default_options: [ 'c_std=gnu99', 'warning_level=2' ],
5 meson_version : '>= 0.38.0')
6
7eina = dependency('eina', version : '>=1.20.99')
8efl = dependency('efl-ui', version : '>=1.20.99')
9elm = dependency('elementary', version : '>=1.20.99')
10
11inc = include_directories('.')
12subdir('src')
13
diff --git a/apps/c/calculator/src/calculator.c b/apps/c/calculator/src/calculator.c
new file mode 100644
index 00000000..69908207
--- /dev/null
+++ b/apps/c/calculator/src/calculator.c
@@ -0,0 +1,186 @@
1#define EFL_BETA_API_SUPPORT 1
2
3#include <Elementary.h>
4#include <Efl_Ui.h>
5// Temporary workaround until Unified Text stops using Legacy classes internally
6#include <efl_ui_text.eo.h>
7#include <efl_text_interactive.eo.h>
8
9static Efl_Ui_Text *_screen = NULL;
10static int _prev_value = 0, _curr_value = 0;
11static char _operation = '=';
12static Eina_Bool _must_overwrite = EINA_FALSE;
13
14static void
15_gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
16{
17 efl_exit(0);
18}
19
20static void
21_operate()
22{
23 switch (_operation)
24 {
25 case '+':
26 _curr_value += _prev_value;
27 break;
28 case '-':
29 _curr_value = _prev_value - _curr_value;
30 break;
31 case '*':
32 _curr_value *= _prev_value;
33 break;
34 case '/':
35 _curr_value = _prev_value / _curr_value;
36 break;
37 default:
38 break;
39 }
40}
41
42static void
43_button_pressed_cb(void *data, const Efl_Event *event EINA_UNUSED)
44{
45 char button = ((const char *)data)[0];
46 if (button >= '0' && button <= '9')
47 {
48 char str[2] = { button, '\0' };
49 if (_must_overwrite)
50 {
51 efl_text_set(_screen, "");
52 _must_overwrite = EINA_FALSE;
53 }
54 Efl_Text_Cursor_Cursor *cursor = efl_text_cursor_get(_screen, EFL_TEXT_CURSOR_GET_TYPE_MAIN);
55 efl_text_cursor_text_insert(_screen, cursor, str);
56 }
57 else
58 {
59 char str[32] = "";
60 switch (button)
61 {
62 case 'C':
63 efl_text_set(_screen, "0");
64 break;
65 case '+':
66 case '-':
67 case '*':
68 case '/':
69 case '=':
70 if (_operation != '=')
71 {
72 _operate();
73 snprintf(str, sizeof(str), "%d", _curr_value);
74 efl_text_set(_screen, str);
75 }
76 _operation = button;
77 _must_overwrite = EINA_TRUE;
78 _prev_value = _curr_value;
79 break;
80 default:
81 break;
82 }
83 }
84}
85
86static void
87_screen_changed_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
88{
89 char _text[32] = "";
90 const char *str = efl_text_get(_screen);
91 int d;
92 if ((strcmp(str, "") == 0) || (strcmp(str, "-") == 0))
93 {
94 snprintf(_text, sizeof(_text), "0");
95 }
96 else if (sscanf(str, "%d", &d) == 1)
97 {
98 snprintf(_text, sizeof(_text), "%d", d);
99 _curr_value = d;
100 }
101 if (strncmp(_text, str, sizeof(_text)))
102 {
103 efl_text_set(_screen, _text);
104 }
105}
106
107// text is what is drawn on the button, which might be a multi-byte unicode string.
108// command is a single-char id for the button.
109static void
110_button_add(Efl_Ui_Table *table, const char *text, const char *command, int posx, int posy, int r, int g, int b)
111{
112 Efl_Ui_Button *button =
113 efl_add(EFL_UI_BUTTON_CLASS, table,
114 efl_pack_table(table, efl_added, posx, posy, 1, 1),
115 efl_event_callback_add(efl_added, EFL_INPUT_EVENT_CLICKED, _button_pressed_cb, command));
116 // Buttons can only have simple text (no font, styles or markup) but can swallow
117 // any other object we want.
118 // Therefore we create a more complex Efl_Ui_Text object and use it as content for the button.
119 Efl_Ui_Text *label =
120 efl_add(EFL_UI_TEXT_CLASS, button,
121 efl_text_interactive_editable_set(efl_added, EINA_FALSE),
122 efl_text_halign_set(efl_added, 0.5),
123 efl_text_valign_set(efl_added, 0.5),
124 efl_gfx_color_set(efl_added, r, g, b, 255),
125 efl_text_set(efl_added, text));
126 efl_text_font_set(label, "Sans", 36);
127 efl_content_set(button, label);
128}
129
130static void
131_gui_setup()
132{
133 Eo *win, *table;
134
135 win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(),
136 efl_ui_win_type_set(efl_added, EFL_UI_WIN_TYPE_BASIC),
137 efl_text_set(efl_added, "EFL Calculator"),
138 efl_ui_win_autodel_set(efl_added, EINA_TRUE));
139
140 // when the user clicks "close" on a window there is a request to delete
141 efl_event_callback_add(win, EFL_UI_WIN_EVENT_DELETE_REQUEST, _gui_quit_cb, NULL);
142
143 table = efl_add(EFL_UI_TABLE_CLASS, win,
144 efl_content_set(win, efl_added),
145 efl_pack_table_size_set(efl_added, 4, 5),
146 efl_gfx_hint_size_min_set(efl_added, EINA_SIZE2D(300, 400)));
147
148 _button_add(table, "1", "1", 0, 3, 255, 255, 255);
149 _button_add(table, "2", "2", 1, 3, 255, 255, 255);
150 _button_add(table, "3", "3", 2, 3, 255, 255, 255);
151 _button_add(table, "4", "4", 0, 2, 255, 255, 255);
152 _button_add(table, "5", "5", 1, 2, 255, 255, 255);
153 _button_add(table, "6", "6", 2, 2, 255, 255, 255);
154 _button_add(table, "7", "7", 0, 1, 255, 255, 255);
155 _button_add(table, "8", "8", 1, 1, 255, 255, 255);
156 _button_add(table, "9", "9", 2, 1, 255, 255, 255);
157 _button_add(table, "0", "0", 1, 4, 255, 255, 255);
158 _button_add(table, "+", "+", 3, 1, 128, 128, 128);
159 _button_add(table, "−", "-", 3, 2, 128, 128, 128);
160 _button_add(table, "×", "*", 3, 3, 128, 128, 128);
161 _button_add(table, "÷", "/", 3, 4, 128, 128, 128);
162 _button_add(table, "=", "=", 2, 4, 128, 128, 128);
163 _button_add(table, "C", "C", 0, 4, 0, 0, 0);
164
165 _screen = efl_add(EFL_UI_TEXT_CLASS, table,
166 efl_text_set(efl_added, "0"),
167 efl_text_multiline_set(efl_added, EINA_FALSE),
168 efl_text_interactive_editable_set(efl_added, EINA_FALSE),
169 efl_text_interactive_selection_allowed_set(efl_added, EINA_FALSE),
170 efl_pack_table(table, efl_added, 0, 0, 4, 1),
171 efl_text_halign_set(efl_added, 0.9),
172 efl_text_valign_set(efl_added, 0.5),
173 efl_text_effect_type_set(efl_added, EFL_TEXT_STYLE_EFFECT_TYPE_GLOW),
174 efl_text_glow_color_set(efl_added, 128, 128, 128, 128),
175 efl_event_callback_add(efl_added, EFL_UI_TEXT_EVENT_CHANGED,
176 _screen_changed_cb, NULL));
177 efl_text_font_set(_screen, "Sans", 48);
178}
179
180EAPI_MAIN void
181efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
182{
183 _gui_setup();
184}
185EFL_MAIN()
186
diff --git a/apps/c/calculator/src/meson.build b/apps/c/calculator/src/meson.build
new file mode 100644
index 00000000..d01a6049
--- /dev/null
+++ b/apps/c/calculator/src/meson.build
@@ -0,0 +1,12 @@
1src = files([
2 'calculator.c',
3])
4
5deps = [eina, efl, elm]
6
7executable('efl_example_calculator', src,
8 dependencies : deps,
9 include_directories : inc,
10 install : true
11)
12