cxx: Added examples and tutorial for C++ binding
[platform/upstream/elementary.git] / src / examples / button_cxx_example_01.cc
1 #include <Elementary.hh>
2
3 EAPI_MAIN int
4 elm_main(int argc, char *argv[])
5 {
6    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_HIDDEN);
7
8    ::elm::win_standard win;
9    win.title_set("Button example");
10    win.autohide_set(true);
11
12    ::evas::object icon_still(nullptr);
13    ::elm::button mid(efl::eo::parent = win);
14    ::elm::button up(efl::eo::parent = win);
15    ::elm::button down(efl::eo::parent = win);
16    ::elm::button left(efl::eo::parent = win);
17    ::elm::button right(efl::eo::parent = win);
18
19    ::elm::box box(efl::eo::parent = win);
20    box.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
21    win.resize_object_add(box);
22    box.visible_set(true);
23
24    ::elm::box box_initial(efl::eo::parent = win);
25    box_initial.horizontal_set(true);
26    box_initial.size_hint_weight_set(EVAS_HINT_EXPAND, 0.0);
27    box.pack_end(box_initial);
28    box_initial.visible_set(true);
29
30    ::elm::button btn(efl::eo::parent = win);
31    btn.text_set("elm.text", "Initial: 0.0");
32    box_initial.pack_end(btn);
33    btn.visible_set(true);
34
35    auto btn_options = std::bind([&] (evas::clickable_interface ci)
36                       {
37                          ::elm::button b(eo_ref(ci._eo_ptr()));
38                          std::string::size_type ptr;
39                          efl::eina::optional<std::string> lbl = b.text_get("elm.text");
40
41                          ptr = lbl->find(":");
42                          ptr += 2;
43                          double t = std::stod(lbl->substr(ptr));
44
45                          if (lbl->compare(0,7,"Initial") == 0)
46                            {
47                               up.autorepeat_initial_timeout_set(t);
48                               down.autorepeat_initial_timeout_set(t);
49                               left.autorepeat_initial_timeout_set(t);
50                               right.autorepeat_initial_timeout_set(t);
51                            }
52                          else if (lbl->compare(0,3,"Gap") == 0)
53                            {
54                               up.autorepeat_gap_timeout_set(t);
55                               down.autorepeat_gap_timeout_set(t);
56                               left.autorepeat_gap_timeout_set(t);
57                               right.autorepeat_gap_timeout_set(t);
58                            }
59                       }, std::placeholders::_1);
60
61    btn.callback_clicked_add(btn_options);
62
63    ::elm::button btn2(efl::eo::parent = win);
64    btn2.text_set("elm.text", "Initial: 1.0");
65    box_initial.pack_end(btn2);
66    btn2.visible_set(true);
67    btn2.callback_clicked_add(btn_options);
68
69    ::elm::button btn3(efl::eo::parent = win);
70    btn3.text_set("elm.text", "Initial: 5.0");
71    box_initial.pack_end(btn3);
72    btn3.visible_set(true);
73    btn3.callback_clicked_add(btn_options);
74
75    ::elm::box box_gap(efl::eo::parent = win);
76    box_gap.horizontal_set(true);
77    box_gap.size_hint_weight_set(EVAS_HINT_EXPAND, 0.0);
78    box.pack_end(box_gap);
79    box_gap.visible_set(true);
80
81    ::elm::button btn4(efl::eo::parent = win);
82    btn4.text_set("elm.text", "Gap: 0.1");
83    box_gap.pack_end(btn4);
84    btn4.visible_set(true);
85    btn4.callback_clicked_add(btn_options);
86
87    ::elm::button btn5(efl::eo::parent = win);
88    btn5.text_set("elm.text", "Gap: 0.5");
89    box_gap.pack_end(btn5);
90    btn5.visible_set(true);
91    btn5.callback_clicked_add(btn_options);
92
93    ::elm::button btn6(efl::eo::parent = win);
94    btn6.text_set("elm.text", "Gap: 1.0");
95    box_gap.pack_end(btn6);
96    btn6.visible_set(true);
97    btn6.callback_clicked_add(btn_options);
98
99    up.autorepeat_set(true);
100    up.autorepeat_initial_timeout_set(1.0);
101    up.autorepeat_gap_timeout_set(0.5);
102    up.size_hint_weight_set(EVAS_HINT_EXPAND, 0.0);
103    up.size_hint_align_set(EVAS_HINT_FILL, 0.0);
104    box.pack_end(up);
105    up.visible_set(true);
106
107    auto btn_cursors_move = std::bind([&] (evas::clickable_interface ci2)
108                            {
109                               ::elm::button button = efl::eo::downcast<elm::button>(ci2);
110                               double ax, ay;
111
112                               if (!icon_still)
113                                 {
114                                    ::elm::icon obj(efl::eo::parent = mid);
115                                    efl::eina::optional<evas::object> icon_still_p = mid.content_unset("icon");
116                                    if(icon_still_p)
117                                      {
118                                         icon_still = *icon_still_p;
119                                         icon_still.visible_set(false);
120                                      }
121                                    obj.standard_set("chat");
122                                    mid.content_set("icon", obj);
123                                 }
124
125                               mid.size_hint_align_get(&ax, &ay);
126                               if (button == up)
127                                 {
128                                    ay -= 0.05;
129                                    if (ay < 0.0)
130                                      ay = 0.0;
131                                 }
132                               else if (button == down)
133                                 {
134                                    ay += 0.05;
135                                    if (ay > 1.0)
136                                      ay = 1.0;
137                                 }
138                               else if (button == left)
139                                 {
140                                    ax -= 0.05;
141                                    if (ax < 0.0)
142                                      ax = 0.0;
143                                 }
144                               else if (button == right)
145                                 {
146                                    ax += 0.05;
147                                    if (ax > 1.0)
148                                      ax = 1.0;
149                                 }
150                               mid.size_hint_align_set(ax, ay);
151                            }, std::placeholders::_1);
152
153    up.callback_repeated_add(btn_cursors_move);
154
155    auto btn_cursors_release = std::bind([&] ()
156                               {
157                                  if (icon_still)
158                                    {
159                                       mid.content_set("icon", icon_still);
160                                       icon_still = evas::object(nullptr);
161                                    }
162                               });
163
164    up.callback_unpressed_add(btn_cursors_release);
165
166    ::elm::icon icon_up(efl::eo::parent = win);
167    icon_up.standard_set("arrow_up");
168    up.content_set("icon", icon_up);
169
170    ::elm::box box_inferior(efl::eo::parent = win);
171    box_inferior.horizontal_set(true);
172    box_inferior.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
173    box_inferior.size_hint_align_set(EVAS_HINT_FILL, EVAS_HINT_FILL);
174    box.pack_end(box_inferior);
175    box_inferior.visible_set(true);
176
177    left.autorepeat_set(true);
178    left.autorepeat_initial_timeout_set(1.0);
179    left.autorepeat_gap_timeout_set(0.5);
180    left.size_hint_weight_set(0.0, EVAS_HINT_EXPAND);
181    left.size_hint_align_set(0.0, EVAS_HINT_FILL);
182    box_inferior.pack_end(left);
183    left.visible_set(true);
184    left.callback_repeated_add(btn_cursors_move);
185    left.callback_unpressed_add(btn_cursors_release);
186
187    ::elm::icon icon_left(efl::eo::parent = win);
188    icon_left.standard_set("arrow_left");
189    left.content_set("icon", icon_left);
190
191    mid.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
192    box_inferior.pack_end(mid);
193    mid.visible_set(true);
194
195    ::elm::icon icon_mid(efl::eo::parent = win);
196    icon_mid.standard_set("close");
197    mid.content_set("icon", icon_mid);
198
199    right.autorepeat_set(true);
200    right.autorepeat_initial_timeout_set(1.0);
201    right.autorepeat_gap_timeout_set(0.5);
202    right.size_hint_weight_set(0.0, EVAS_HINT_EXPAND);
203    right.size_hint_align_set(0.0, EVAS_HINT_FILL);
204    box_inferior.pack_end(right);
205    right.visible_set(true);
206    right.callback_repeated_add(btn_cursors_move);
207    right.callback_unpressed_add(btn_cursors_release);
208
209    ::elm::icon icon_right(efl::eo::parent = win);
210    icon_right.standard_set("arrow_right");
211    right.content_set("icon", icon_right);
212
213    down.autorepeat_set(true);
214    down.autorepeat_initial_timeout_set(1.0);
215    down.autorepeat_gap_timeout_set(0.5);
216    down.size_hint_weight_set(EVAS_HINT_EXPAND, 0.0);
217    down.size_hint_align_set(EVAS_HINT_FILL, 0.0);
218    box.pack_end(down);
219    down.visible_set(true);
220    down.callback_repeated_add(btn_cursors_move);
221    down.callback_unpressed_add(btn_cursors_release);
222
223    ::elm::icon icon_down(efl::eo::parent = win);
224    icon_down.standard_set("arrow_down");
225    down.content_set("icon", icon_down);
226
227    win.size_set(300, 320);
228    win.visible_set(true);
229
230    elm_run();
231    return 0;
232 }
233 ELM_MAIN()