2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
20 #include <dali/dali.h>
21 #include "shared/view.h"
25 // Define this so that it is interchangeable
26 // "DP" stands for Device independent Pixels
31 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "background-default.png";
32 const char* const TOOLBAR_IMAGE = DEMO_IMAGE_DIR "top-bar.png";
34 const char* const TOOLBAR_TITLE = "Buttons";
36 const char* const SMALL_IMAGE_1 = DEMO_IMAGE_DIR "gallery-small-14.jpg";
37 const char* const BIG_IMAGE_1 = DEMO_IMAGE_DIR "gallery-large-4.jpg";
39 const char* const SMALL_IMAGE_2 = DEMO_IMAGE_DIR "gallery-small-20.jpg";
40 const char* const BIG_IMAGE_2 = DEMO_IMAGE_DIR "gallery-large-11.jpg";
42 const char* const SMALL_IMAGE_3 = DEMO_IMAGE_DIR "gallery-small-25.jpg";
43 const char* const BIG_IMAGE_3 = DEMO_IMAGE_DIR "gallery-large-13.jpg";
45 const char* const ENABLED_IMAGE = DEMO_IMAGE_DIR "item-select-check.png";
47 const Vector4 BACKGROUND_COLOUR(0.25f, 0.25f, 0.25f, 0.15f);
50 const int RADIO_LABEL_THUMBNAIL_SIZE = 50;
51 const int RADIO_LABEL_THUMBNAIL_SIZE_SMALL = 40;
52 const int RADIO_IMAGE_SPACING = 8;
53 const int BUTTON_HEIGHT = 48;
55 const int MARGIN_SIZE = 10;
56 const int TOP_MARGIN = 85;
60 /** This example shows how to create and use different buttons.
62 * 1. First group of radio buttons with image actor labels selects an image to load
63 * 2. A push button loads the selected thumbnail image into the larger image pane
64 * 3. Second group of radio buttons with a table view label containing a text view and image view, and a normal text view.
65 * Selecting one of these will enable/disable the image loading push button
66 * 4. A group of check boxes
68 class ButtonsController : public ConnectionTracker
71 ButtonsController(Application& application)
72 : mApplication(application)
74 // Connect to the Application's Init signal
75 mApplication.InitSignal().Connect(this, &ButtonsController::Create);
83 void Create(Application& application)
85 // The Init signal is received once (only) during the Application lifetime
87 // Respond to key events
88 application.GetWindow().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
90 // Creates a default view with a default tool bar.
91 // The view is added to the window.
92 mContentLayer = DemoHelper::CreateView(application,
99 Toolkit::TableView contentTable = Toolkit::TableView::New(4, 1);
100 contentTable.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
101 contentTable.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
102 contentTable.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
103 contentTable.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
104 contentTable.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5f));
106 for(unsigned int i = 0; i < contentTable.GetRows(); ++i)
108 contentTable.SetFitHeight(i);
111 contentTable.SetProperty(Actor::Property::POSITION, Vector2(0.0f, TOP_MARGIN));
113 mContentLayer.Add(contentTable);
115 // Image selector radio group
116 Toolkit::TableView radioGroup2Background = Toolkit::TableView::New(2, 2);
117 radioGroup2Background.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
118 radioGroup2Background.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
119 radioGroup2Background.SetBackgroundColor(BACKGROUND_COLOUR);
120 radioGroup2Background.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE));
121 radioGroup2Background.SetFitHeight(0);
122 radioGroup2Background.SetFitHeight(1);
123 radioGroup2Background.SetFitWidth(0);
125 contentTable.Add(radioGroup2Background);
127 Toolkit::TableView radioButtonsGroup2 = Toolkit::TableView::New(3, 1);
128 radioButtonsGroup2.SetCellPadding(Size(0.0f, MARGIN_SIZE * 0.5f));
129 radioButtonsGroup2.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
130 for(unsigned int i = 0; i < radioButtonsGroup2.GetRows(); ++i)
132 radioButtonsGroup2.SetFitHeight(i);
134 radioButtonsGroup2.SetFitWidth(0);
136 radioGroup2Background.AddChild(radioButtonsGroup2, Toolkit::TableView::CellPosition(0, 0));
138 // TableView to lay out 3x Radio buttons on the left, and 3x Image thumbnails on the right.
139 Toolkit::TableView imageSelectTableView = Toolkit::TableView::New(3, 2);
140 imageSelectTableView.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
141 imageSelectTableView.SetFitHeight(0);
142 imageSelectTableView.SetFitHeight(1);
143 imageSelectTableView.SetFitHeight(2);
144 imageSelectTableView.SetFitWidth(0);
145 imageSelectTableView.SetFitWidth(1);
146 imageSelectTableView.SetCellPadding(Vector2(6.0f, 0.0f));
148 radioButtonsGroup2.Add(imageSelectTableView);
154 Toolkit::ImageView image = Toolkit::ImageView::New(SMALL_IMAGE_1);
155 image.SetProperty(Actor::Property::SIZE, Vector2(DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE)));
157 mRadioButtonImage1 = Dali::Toolkit::RadioButton::New("1");
158 mRadioButtonImage1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
159 mRadioButtonImage1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
160 mRadioButtonImage1.SetProperty(Actor::Property::POSITION, Vector2(0, DP(radioY)));
161 mRadioButtonImage1.SetProperty(Toolkit::Button::Property::SELECTED, true);
163 imageSelectTableView.AddChild(mRadioButtonImage1, Toolkit::TableView::CellPosition(0, 0));
164 imageSelectTableView.AddChild(image, Toolkit::TableView::CellPosition(0, 1));
169 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
171 Toolkit::ImageView image = Toolkit::ImageView::New(SMALL_IMAGE_2);
172 image.SetProperty(Actor::Property::SIZE, Vector2(DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE)));
174 mRadioButtonImage2 = Dali::Toolkit::RadioButton::New("2");
175 mRadioButtonImage2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
176 mRadioButtonImage2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
177 mRadioButtonImage2.SetProperty(Actor::Property::POSITION, Vector2(0, DP(radioY)));
179 imageSelectTableView.AddChild(mRadioButtonImage2, Toolkit::TableView::CellPosition(1, 0));
180 imageSelectTableView.AddChild(image, Toolkit::TableView::CellPosition(1, 1));
185 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
187 Toolkit::ImageView image = Toolkit::ImageView::New(SMALL_IMAGE_3);
188 image.SetProperty(Actor::Property::SIZE, Vector2(DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE)));
190 mRadioButtonImage3 = Dali::Toolkit::RadioButton::New("3");
191 mRadioButtonImage3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
192 mRadioButtonImage3.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
193 mRadioButtonImage3.SetProperty(Actor::Property::POSITION, Vector2(0, DP(radioY)));
195 imageSelectTableView.AddChild(mRadioButtonImage3, Toolkit::TableView::CellPosition(2, 0));
196 imageSelectTableView.AddChild(image, Toolkit::TableView::CellPosition(2, 1));
199 // Create select button
200 mUpdateButton = Toolkit::PushButton::New();
201 mUpdateButton.SetProperty(Toolkit::Button::Property::LABEL, "Select");
202 mUpdateButton.SetProperty(Dali::Actor::Property::NAME, "selectButton");
203 mUpdateButton.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
205 mUpdateButton.ClickedSignal().Connect(this, &ButtonsController::OnButtonClicked);
207 radioGroup2Background.AddChild(mUpdateButton, Toolkit::TableView::CellPosition(1, 0));
209 // ImageView to display selected image
210 mImage = Toolkit::ImageView::New(BIG_IMAGE_1);
211 mImage.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
212 mImage.SetProperty(Actor::Property::SIZE_SCALE_POLICY, SizeScalePolicy::FIT_WITH_ASPECT_RATIO);
213 radioGroup2Background.AddChild(mImage, Toolkit::TableView::CellPosition(0, 1, 2, 1));
215 // The enable/disable radio group
216 Toolkit::TableView radioGroup1Background = Toolkit::TableView::New(1, 1);
217 radioGroup1Background.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
218 radioGroup1Background.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
219 radioGroup1Background.SetBackgroundColor(BACKGROUND_COLOUR);
220 radioGroup1Background.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE));
221 radioGroup1Background.SetFitHeight(0);
223 contentTable.Add(radioGroup1Background);
226 Toolkit::TableView radioButtonsGroup1 = Toolkit::TableView::New(2, 1);
227 radioButtonsGroup1.SetCellPadding(Size(0.0f, MARGIN_SIZE * 0.5f));
228 radioButtonsGroup1.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
229 for(unsigned int i = 0; i < radioButtonsGroup1.GetRows(); ++i)
231 radioButtonsGroup1.SetFitHeight(i);
233 radioButtonsGroup1.SetFitWidth(0);
235 radioGroup1Background.Add(radioButtonsGroup1);
237 // TableView to lay out 2x Radio buttons on the left, and 1x Tick image on the right.
238 Toolkit::TableView tableView = Toolkit::TableView::New(2, 2);
239 tableView.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
240 tableView.SetFitHeight(0);
241 tableView.SetFitHeight(1);
242 tableView.SetFitWidth(0);
243 tableView.SetFitWidth(1);
245 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New("Select enabled");
246 textLabel.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH);
247 textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
248 textLabel.SetProperty(Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
250 Toolkit::ImageView image = Toolkit::ImageView::New(ENABLED_IMAGE);
251 image.SetProperty(Actor::Property::SIZE, Vector2(DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE)));
252 image.SetProperty(Actor::Property::PADDING, Padding(DP(20.0f), 0.0f, 0.0f, 0.0f));
253 tableView.AddChild(image, Toolkit::TableView::CellPosition(0, 1));
255 radioButtonsGroup1.Add(tableView);
257 // First radio button
259 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New("Select enabled");
260 radioButton.SetProperty(Dali::Actor::Property::NAME, "radioSelectEnable");
261 radioButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
262 radioButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
263 radioButton.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
264 radioButton.SetProperty(Toolkit::Button::Property::SELECTED, true);
266 radioButton.StateChangedSignal().Connect(this, &ButtonsController::EnableSelectButton);
268 tableView.AddChild(radioButton, Toolkit::TableView::CellPosition(0, 0));
271 // Second radio button
273 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New("Select disabled");
274 radioButton.SetProperty(Dali::Actor::Property::NAME, "radioSelectDisable");
275 radioButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
276 radioButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
277 radioButton.SetProperty(Actor::Property::POSITION, Vector2(0, DP(50)));
279 radioButton.StateChangedSignal().Connect(this, &ButtonsController::EnableSelectButton);
281 tableView.AddChild(radioButton, Toolkit::TableView::CellPosition(1, 0));
285 Toolkit::TableView checkBoxBackground = Toolkit::TableView::New(3, 1);
286 checkBoxBackground.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
287 checkBoxBackground.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
288 checkBoxBackground.SetBackgroundColor(BACKGROUND_COLOUR);
289 checkBoxBackground.SetCellPadding(Size(MARGIN_SIZE / 2.0f, MARGIN_SIZE / 2.0f));
291 for(unsigned int i = 0; i < checkBoxBackground.GetRows(); ++i)
293 checkBoxBackground.SetFitHeight(i);
296 contentTable.Add(checkBoxBackground);
299 mCheckboxButton1 = Toolkit::CheckBoxButton::New();
300 mCheckboxButton1.SetProperty(Dali::Actor::Property::NAME, "checkbox1");
301 mCheckboxButton1.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox1 is unselected");
302 mCheckboxButton1.StateChangedSignal().Connect(this, &ButtonsController::OnCheckBoxesSelected);
303 mCheckboxButton1.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
305 checkBoxBackground.Add(mCheckboxButton1);
309 mCheckboxButton2 = Toolkit::CheckBoxButton::New();
310 mCheckboxButton2.SetProperty(Dali::Actor::Property::NAME, "checkbox2");
311 mCheckboxButton2.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox2 is selected");
312 mCheckboxButton2.SetProperty(Toolkit::Button::Property::SELECTED, true);
313 mCheckboxButton2.StateChangedSignal().Connect(this, &ButtonsController::OnCheckBoxesSelected);
314 mCheckboxButton2.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
316 checkBoxBackground.Add(mCheckboxButton2);
320 mCheckboxButton3 = Toolkit::CheckBoxButton::New();
321 mCheckboxButton3.SetProperty(Dali::Actor::Property::NAME, "checkbox3");
322 mCheckboxButton3.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox3 is unselected");
323 mCheckboxButton3.StateChangedSignal().Connect(this, &ButtonsController::OnCheckBoxesSelected);
324 mCheckboxButton3.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
326 checkBoxBackground.Add(mCheckboxButton3);
329 // Create togglabe button
330 Toolkit::TableView toggleBackground = Toolkit::TableView::New(3, 1);
331 toggleBackground.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
332 toggleBackground.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
333 toggleBackground.SetBackgroundColor(BACKGROUND_COLOUR);
334 toggleBackground.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE));
336 for(unsigned int i = 0; i < toggleBackground.GetRows(); ++i)
338 toggleBackground.SetFitHeight(i);
341 contentTable.Add(toggleBackground);
343 mToggleButton = Toolkit::PushButton::New();
344 mToggleButton.SetProperty(Toolkit::Button::Property::TOGGLABLE, true);
345 mToggleButton.SetProperty(Toolkit::Button::Property::LABEL, "Unselected");
346 mToggleButton.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
347 mToggleButton.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
348 mToggleButton.StateChangedSignal().Connect(this, &ButtonsController::OnButtonSelected);
350 toggleBackground.Add(mToggleButton);
353 void OnKeyEvent(const KeyEvent& event)
355 if(event.GetState() == KeyEvent::DOWN)
357 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
359 // Exit application when click back or escape.
365 bool OnButtonSelected(Toolkit::Button button)
367 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(button);
370 bool isSelected = button.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>();
373 pushButton.SetProperty(Toolkit::Button::Property::LABEL, "Selected");
377 pushButton.SetProperty(Toolkit::Button::Property::LABEL, "Unselected");
384 bool EnableSelectButton(Toolkit::Button button)
386 bool isSelected = button.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>();
392 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == "radioSelectEnable")
394 mUpdateButton.SetProperty(Toolkit::Button::Property::DISABLED, false);
396 mRadioButtonImage1.SetProperty(Toolkit::Button::Property::DISABLED, false);
397 mRadioButtonImage2.SetProperty(Toolkit::Button::Property::DISABLED, false);
398 mRadioButtonImage3.SetProperty(Toolkit::Button::Property::DISABLED, false);
400 mCheckboxButton1.SetProperty(Toolkit::Button::Property::DISABLED, false);
401 mCheckboxButton2.SetProperty(Toolkit::Button::Property::DISABLED, false);
402 mCheckboxButton3.SetProperty(Toolkit::Button::Property::DISABLED, false);
404 mToggleButton.SetProperty(Toolkit::Button::Property::DISABLED, false);
406 else if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == "radioSelectDisable")
408 mUpdateButton.SetProperty(Toolkit::Button::Property::DISABLED, true);
410 mRadioButtonImage1.SetProperty(Toolkit::Button::Property::DISABLED, true);
411 mRadioButtonImage2.SetProperty(Toolkit::Button::Property::DISABLED, true);
412 mRadioButtonImage3.SetProperty(Toolkit::Button::Property::DISABLED, true);
414 mCheckboxButton1.SetProperty(Toolkit::Button::Property::DISABLED, true);
415 mCheckboxButton2.SetProperty(Toolkit::Button::Property::DISABLED, true);
416 mCheckboxButton3.SetProperty(Toolkit::Button::Property::DISABLED, true);
418 mToggleButton.SetProperty(Toolkit::Button::Property::DISABLED, true);
424 bool OnButtonClicked(Toolkit::Button button)
426 if(mRadioButtonImage1.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>())
428 mImage.SetImage(BIG_IMAGE_1);
430 else if(mRadioButtonImage2.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>())
432 mImage.SetImage(BIG_IMAGE_2);
434 else if(mRadioButtonImage3.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>())
436 mImage.SetImage(BIG_IMAGE_3);
441 bool OnCheckBoxesSelected(Toolkit::Button button)
443 bool isSelected = button.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>();
444 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == "checkbox1")
448 button.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox1 is selected");
452 button.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox1 is unselected");
456 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == "checkbox2")
460 button.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox2 is selected");
464 button.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox2 is unselected");
468 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == "checkbox3")
472 button.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox3 is selected");
476 button.SetProperty(Toolkit::Button::Property::LABEL, "CheckBox3 is unselected");
484 Application& mApplication;
485 Toolkit::Control mView; ///< The View instance.
486 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
487 Layer mContentLayer; ///< Content layer
489 Toolkit::RadioButton mRadioButtonImage1;
490 Toolkit::RadioButton mRadioButtonImage2;
491 Toolkit::RadioButton mRadioButtonImage3;
493 Toolkit::PushButton mUpdateButton;
494 Toolkit::PushButton mToggleButton;
496 Toolkit::CheckBoxButton mCheckboxButton1;
497 Toolkit::CheckBoxButton mCheckboxButton2;
498 Toolkit::CheckBoxButton mCheckboxButton3;
500 Animation mAnimation;
502 Toolkit::ImageView mImage;
505 int DALI_EXPORT_API main(int argc, char** argv)
507 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
508 ButtonsController test(application);
509 application.MainLoop();