diff options
author | Xavi Artigas <xavierartigas@yahoo.es> | 2019-10-24 12:44:25 +0200 |
---|---|---|
committer | Xavi Artigas <xavierartigas@yahoo.es> | 2019-10-24 12:44:25 +0200 |
commit | d57c8e2d28df7a6648b85c44115677c2ad565a27 (patch) | |
tree | 4fef4725b59783f1e4efb29d4c7e99f307490fa3 | |
parent | f9486b6bfdb35193ecc9b43bcd22105c966b86a4 (diff) |
Add documentation for C and C# calculator examplesdevs/xartigas/calculator_example
-rw-r--r-- | apps/c/calculator/src/calculator.c | 32 | ||||
-rw-r--r-- | apps/csharp/calculator/src/calculator.cs | 36 |
2 files changed, 54 insertions, 14 deletions
diff --git a/apps/c/calculator/src/calculator.c b/apps/c/calculator/src/calculator.c index 69908207..b42c4ba5 100644 --- a/apps/c/calculator/src/calculator.c +++ b/apps/c/calculator/src/calculator.c | |||
@@ -6,17 +6,21 @@ | |||
6 | #include <efl_ui_text.eo.h> | 6 | #include <efl_ui_text.eo.h> |
7 | #include <efl_text_interactive.eo.h> | 7 | #include <efl_text_interactive.eo.h> |
8 | 8 | ||
9 | static Efl_Ui_Text *_screen = NULL; | 9 | static Efl_Ui_Text *_screen = NULL; // Text widget showing current value |
10 | static int _prev_value = 0, _curr_value = 0; | 10 | static int _prev_value = 0; // Value introduced before an operation (first operand |
11 | static char _operation = '='; | 11 | static int _curr_value = 0; // Value currently being introduced (second operand) |
12 | static Eina_Bool _must_overwrite = EINA_FALSE; | 12 | static char _operation = '='; // Last operation button pressed |
13 | static Eina_Bool _must_overwrite = EINA_FALSE; // Whether next number must be appended to current input | ||
14 | // or overwrite it | ||
13 | 15 | ||
16 | // Quits the application | ||
14 | static void | 17 | static void |
15 | _gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED) | 18 | _gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED) |
16 | { | 19 | { |
17 | efl_exit(0); | 20 | efl_exit(0); |
18 | } | 21 | } |
19 | 22 | ||
23 | // Performs "operation" on "currValue" and "prevValue" and leaves result in "currValue" | ||
20 | static void | 24 | static void |
21 | _operate() | 25 | _operate() |
22 | { | 26 | { |
@@ -39,10 +43,12 @@ _operate() | |||
39 | } | 43 | } |
40 | } | 44 | } |
41 | 45 | ||
46 | // Called every time a button is pressed | ||
42 | static void | 47 | static void |
43 | _button_pressed_cb(void *data, const Efl_Event *event EINA_UNUSED) | 48 | _button_pressed_cb(void *data, const Efl_Event *event EINA_UNUSED) |
44 | { | 49 | { |
45 | char button = ((const char *)data)[0]; | 50 | char button = ((const char *)data)[0]; |
51 | // If it is a number, append it to current input (or replace it) | ||
46 | if (button >= '0' && button <= '9') | 52 | if (button >= '0' && button <= '9') |
47 | { | 53 | { |
48 | char str[2] = { button, '\0' }; | 54 | char str[2] = { button, '\0' }; |
@@ -60,6 +66,7 @@ _button_pressed_cb(void *data, const Efl_Event *event EINA_UNUSED) | |||
60 | switch (button) | 66 | switch (button) |
61 | { | 67 | { |
62 | case 'C': | 68 | case 'C': |
69 | // Clear current input | ||
63 | efl_text_set(_screen, "0"); | 70 | efl_text_set(_screen, "0"); |
64 | break; | 71 | break; |
65 | case '+': | 72 | case '+': |
@@ -67,12 +74,14 @@ _button_pressed_cb(void *data, const Efl_Event *event EINA_UNUSED) | |||
67 | case '*': | 74 | case '*': |
68 | case '/': | 75 | case '/': |
69 | case '=': | 76 | case '=': |
77 | // If there was a pending operation, perform it | ||
70 | if (_operation != '=') | 78 | if (_operation != '=') |
71 | { | 79 | { |
72 | _operate(); | 80 | _operate(); |
73 | snprintf(str, sizeof(str), "%d", _curr_value); | 81 | snprintf(str, sizeof(str), "%d", _curr_value); |
74 | efl_text_set(_screen, str); | 82 | efl_text_set(_screen, str); |
75 | } | 83 | } |
84 | // Store this operation | ||
76 | _operation = button; | 85 | _operation = button; |
77 | _must_overwrite = EINA_TRUE; | 86 | _must_overwrite = EINA_TRUE; |
78 | _prev_value = _curr_value; | 87 | _prev_value = _curr_value; |
@@ -83,6 +92,10 @@ _button_pressed_cb(void *data, const Efl_Event *event EINA_UNUSED) | |||
83 | } | 92 | } |
84 | } | 93 | } |
85 | 94 | ||
95 | // Called every time the content of the screen changes | ||
96 | // We use it to sanitize input (remove heading zeros, for example) | ||
97 | // This makes more sense when the Text widget is editable, since the user | ||
98 | // is free to type anything. | ||
86 | static void | 99 | static void |
87 | _screen_changed_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED) | 100 | _screen_changed_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED) |
88 | { | 101 | { |
@@ -104,8 +117,10 @@ _screen_changed_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED) | |||
104 | } | 117 | } |
105 | } | 118 | } |
106 | 119 | ||
107 | // text is what is drawn on the button, which might be a multi-byte unicode string. | 120 | // Creates an Efl.Ui.Button and positions it in the given position inside the table |
108 | // command is a single-char id for the button. | 121 | // The button text is colored with "r, g, b" |
122 | // "text" is what is drawn on the button, which might be a multi-byte unicode string. | ||
123 | // "command" is a single-char id for the button. | ||
109 | static void | 124 | static void |
110 | _button_add(Efl_Ui_Table *table, const char *text, const char *command, int posx, int posy, int r, int g, int b) | 125 | _button_add(Efl_Ui_Table *table, const char *text, const char *command, int posx, int posy, int r, int g, int b) |
111 | { | 126 | { |
@@ -127,11 +142,13 @@ _button_add(Efl_Ui_Table *table, const char *text, const char *command, int posx | |||
127 | efl_content_set(button, label); | 142 | efl_content_set(button, label); |
128 | } | 143 | } |
129 | 144 | ||
145 | // Creates the UI | ||
130 | static void | 146 | static void |
131 | _gui_setup() | 147 | _gui_setup() |
132 | { | 148 | { |
133 | Eo *win, *table; | 149 | Eo *win, *table; |
134 | 150 | ||
151 | // The window | ||
135 | win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(), | 152 | win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(), |
136 | efl_ui_win_type_set(efl_added, EFL_UI_WIN_TYPE_BASIC), | 153 | efl_ui_win_type_set(efl_added, EFL_UI_WIN_TYPE_BASIC), |
137 | efl_text_set(efl_added, "EFL Calculator"), | 154 | efl_text_set(efl_added, "EFL Calculator"), |
@@ -140,11 +157,13 @@ _gui_setup() | |||
140 | // when the user clicks "close" on a window there is a request to delete | 157 | // 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); | 158 | efl_event_callback_add(win, EFL_UI_WIN_EVENT_DELETE_REQUEST, _gui_quit_cb, NULL); |
142 | 159 | ||
160 | // The table is the main layout | ||
143 | table = efl_add(EFL_UI_TABLE_CLASS, win, | 161 | table = efl_add(EFL_UI_TABLE_CLASS, win, |
144 | efl_content_set(win, efl_added), | 162 | efl_content_set(win, efl_added), |
145 | efl_pack_table_size_set(efl_added, 4, 5), | 163 | efl_pack_table_size_set(efl_added, 4, 5), |
146 | efl_gfx_hint_size_min_set(efl_added, EINA_SIZE2D(300, 400))); | 164 | efl_gfx_hint_size_min_set(efl_added, EINA_SIZE2D(300, 400))); |
147 | 165 | ||
166 | // Create all buttons using the _button_add helper | ||
148 | _button_add(table, "1", "1", 0, 3, 255, 255, 255); | 167 | _button_add(table, "1", "1", 0, 3, 255, 255, 255); |
149 | _button_add(table, "2", "2", 1, 3, 255, 255, 255); | 168 | _button_add(table, "2", "2", 1, 3, 255, 255, 255); |
150 | _button_add(table, "3", "3", 2, 3, 255, 255, 255); | 169 | _button_add(table, "3", "3", 2, 3, 255, 255, 255); |
@@ -162,6 +181,7 @@ _gui_setup() | |||
162 | _button_add(table, "=", "=", 2, 4, 128, 128, 128); | 181 | _button_add(table, "=", "=", 2, 4, 128, 128, 128); |
163 | _button_add(table, "C", "C", 0, 4, 0, 0, 0); | 182 | _button_add(table, "C", "C", 0, 4, 0, 0, 0); |
164 | 183 | ||
184 | // Create a big Efl.Ui.Text screen to display the current input | ||
165 | _screen = efl_add(EFL_UI_TEXT_CLASS, table, | 185 | _screen = efl_add(EFL_UI_TEXT_CLASS, table, |
166 | efl_text_set(efl_added, "0"), | 186 | efl_text_set(efl_added, "0"), |
167 | efl_text_multiline_set(efl_added, EINA_FALSE), | 187 | efl_text_multiline_set(efl_added, EINA_FALSE), |
diff --git a/apps/csharp/calculator/src/calculator.cs b/apps/csharp/calculator/src/calculator.cs index 35e426de..e4711c00 100644 --- a/apps/csharp/calculator/src/calculator.cs +++ b/apps/csharp/calculator/src/calculator.cs | |||
@@ -1,12 +1,15 @@ | |||
1 | /* Simple calculator using an Efl.Ui.Table to place the buttons | ||
2 | */ | ||
1 | using System; | 3 | using System; |
2 | 4 | ||
3 | public class Calculator : Efl.Csharp.Application | 5 | public class Calculator : Efl.Csharp.Application |
4 | { | 6 | { |
5 | private Efl.Ui.Text screen; | 7 | private Efl.Ui.Text screen; // Text widget showing current value |
6 | private int prevValue = 0; | 8 | private int prevValue = 0; // Value introduced before an operation (first operand) |
7 | private int currValue = 0; | 9 | private int currValue = 0; // Value currently being introduced (second operand) |
8 | private char operation = '='; | 10 | private char operation = '='; // Last operation button pressed |
9 | private bool mustOverwrite = false; | 11 | private bool mustOverwrite = false; // Whether next number must be appended to current input |
12 | // or overwrite it | ||
10 | 13 | ||
11 | // Quits the application | 14 | // Quits the application |
12 | private void GUIQuitCb(object sender, Efl.Gfx.EntityVisibilityChangedEventArgs ea) | 15 | private void GUIQuitCb(object sender, Efl.Gfx.EntityVisibilityChangedEventArgs ea) |
@@ -15,6 +18,7 @@ public class Calculator : Efl.Csharp.Application | |||
15 | Efl.App.AppMain.Quit(0); | 18 | Efl.App.AppMain.Quit(0); |
16 | } | 19 | } |
17 | 20 | ||
21 | // Performs "operation" on "currValue" and "prevValue" and leaves result in "currValue" | ||
18 | private void Operate() | 22 | private void Operate() |
19 | { | 23 | { |
20 | switch (operation) | 24 | switch (operation) |
@@ -36,8 +40,10 @@ public class Calculator : Efl.Csharp.Application | |||
36 | } | 40 | } |
37 | } | 41 | } |
38 | 42 | ||
43 | // Called every time a button is pressed | ||
39 | private void ButtonPressedCb(char button) | 44 | private void ButtonPressedCb(char button) |
40 | { | 45 | { |
46 | // If it is a number, append it to current input (or replace it) | ||
41 | if (button >= '0' && button <= '9') | 47 | if (button >= '0' && button <= '9') |
42 | { | 48 | { |
43 | if (mustOverwrite) | 49 | if (mustOverwrite) |
@@ -52,6 +58,7 @@ public class Calculator : Efl.Csharp.Application | |||
52 | switch (button) | 58 | switch (button) |
53 | { | 59 | { |
54 | case 'C': | 60 | case 'C': |
61 | // Clear current input | ||
55 | screen.SetText("0"); | 62 | screen.SetText("0"); |
56 | break; | 63 | break; |
57 | case '+': | 64 | case '+': |
@@ -59,11 +66,13 @@ public class Calculator : Efl.Csharp.Application | |||
59 | case '*': | 66 | case '*': |
60 | case '/': | 67 | case '/': |
61 | case '=': | 68 | case '=': |
69 | // If there was a pending operation, perform it | ||
62 | if (operation != '=') | 70 | if (operation != '=') |
63 | { | 71 | { |
64 | Operate(); | 72 | Operate(); |
65 | screen.SetText(currValue.ToString()); | 73 | screen.SetText(currValue.ToString()); |
66 | } | 74 | } |
75 | // Store this operation | ||
67 | operation = button; | 76 | operation = button; |
68 | mustOverwrite = true; | 77 | mustOverwrite = true; |
69 | prevValue = currValue; | 78 | prevValue = currValue; |
@@ -74,6 +83,10 @@ public class Calculator : Efl.Csharp.Application | |||
74 | } | 83 | } |
75 | } | 84 | } |
76 | 85 | ||
86 | // Called every time the content of the screen changes | ||
87 | // We use it to sanitize input (remove heading zeros, for example) | ||
88 | // This makes more sense when the Text widget is editable, since the user | ||
89 | // is free to type anything. | ||
77 | private void ScreenChangedCb(object sender, EventArgs ea) | 90 | private void ScreenChangedCb(object sender, EventArgs ea) |
78 | { | 91 | { |
79 | string text = ""; | 92 | string text = ""; |
@@ -96,8 +109,10 @@ public class Calculator : Efl.Csharp.Application | |||
96 | if (text != str) screen.SetText(text); | 109 | if (text != str) screen.SetText(text); |
97 | } | 110 | } |
98 | 111 | ||
99 | // text is what is drawn on the button, which might be a multi-byte unicode string. | 112 | // Creates an Efl.Ui.Button and positions it in the given position inside the table |
100 | // command is a single-char id for the button. | 113 | // The button text is colored with "r, g, b" |
114 | // "text" is what is drawn on the button, which might be a multi-byte unicode string. | ||
115 | // "command" is a single-char id for the button. | ||
101 | private void AddButton(Efl.Ui.Table table, string text, char command, int posx, int posy, int r, int g, int b) | 116 | private void AddButton(Efl.Ui.Table table, string text, char command, int posx, int posy, int r, int g, int b) |
102 | { | 117 | { |
103 | var button = new Efl.Ui.Button(table); | 118 | var button = new Efl.Ui.Button(table); |
@@ -107,7 +122,7 @@ public class Calculator : Efl.Csharp.Application | |||
107 | }; | 122 | }; |
108 | // Buttons can only have simple text (no font, styles or markup) but can swallow | 123 | // Buttons can only have simple text (no font, styles or markup) but can swallow |
109 | // any other object we want. | 124 | // any other object we want. |
110 | // Therefore we create a more complex Efl_Ui_Text object and use it as content for the button. | 125 | // Therefore we create a more complex Efl.Ui.Text object and use it as content for the button. |
111 | var label = new Efl.Ui.Text(table); | 126 | var label = new Efl.Ui.Text(table); |
112 | label.Editable = false; | 127 | label.Editable = false; |
113 | label.Halign = 0.5; | 128 | label.Halign = 0.5; |
@@ -118,18 +133,22 @@ public class Calculator : Efl.Csharp.Application | |||
118 | button.Content = label; | 133 | button.Content = label; |
119 | } | 134 | } |
120 | 135 | ||
136 | // Called on start up. We use it to create the UI. | ||
121 | protected override void OnInitialize(string[] args) | 137 | protected override void OnInitialize(string[] args) |
122 | { | 138 | { |
139 | // The window | ||
123 | var win = new Efl.Ui.Win(Efl.App.AppMain); | 140 | var win = new Efl.Ui.Win(Efl.App.AppMain); |
124 | win.SetText("EFL Calculator"); | 141 | win.SetText("EFL Calculator"); |
125 | win.Autohide = true; | 142 | win.Autohide = true; |
126 | win.VisibilityChangedEvent += GUIQuitCb; | 143 | win.VisibilityChangedEvent += GUIQuitCb; |
127 | 144 | ||
145 | // The table is the main layout | ||
128 | var table = new Efl.Ui.Table(win); | 146 | var table = new Efl.Ui.Table(win); |
129 | win.Content = table; | 147 | win.Content = table; |
130 | table.TableSize = (4, 5); | 148 | table.TableSize = (4, 5); |
131 | table.HintSizeMin = new Eina.Size2D(300, 400); | 149 | table.HintSizeMin = new Eina.Size2D(300, 400); |
132 | 150 | ||
151 | // Create all buttons using the AddButton helper | ||
133 | AddButton(table, "1", '1', 0, 3, 255, 255, 255); | 152 | AddButton(table, "1", '1', 0, 3, 255, 255, 255); |
134 | AddButton(table, "2", '2', 1, 3, 255, 255, 255); | 153 | AddButton(table, "2", '2', 1, 3, 255, 255, 255); |
135 | AddButton(table, "3", '3', 2, 3, 255, 255, 255); | 154 | AddButton(table, "3", '3', 2, 3, 255, 255, 255); |
@@ -147,6 +166,7 @@ public class Calculator : Efl.Csharp.Application | |||
147 | AddButton(table, "=", '=', 2, 4, 128, 128, 128); | 166 | AddButton(table, "=", '=', 2, 4, 128, 128, 128); |
148 | AddButton(table, "C", 'C', 0, 4, 0, 0, 0); | 167 | AddButton(table, "C", 'C', 0, 4, 0, 0, 0); |
149 | 168 | ||
169 | // Create a big Efl.Ui.Text screen to display the current input | ||
150 | screen = new Efl.Ui.Text(table); | 170 | screen = new Efl.Ui.Text(table); |
151 | screen.SetText("0"); | 171 | screen.SetText("0"); |
152 | screen.Font = ("Sans", (Efl.Font.Size)48); | 172 | screen.Font = ("Sans", (Efl.Font.Size)48); |