2 * Copyright (c) 2014 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 "shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
26 // Used to produce visually same dimensions on desktop and device builds
27 float ScalePointSize( int pointSize )
29 Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
30 float meanDpi = (dpi.height + dpi.width) * 0.5f;
31 return pointSize * meanDpi / 220.0f;
36 // Define this so that it is interchangeable
37 // "DP" stands for Device independent Pixels
38 #define DP(x) ScalePointSize(x)
44 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
45 const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
47 const char* const TOOLBAR_TITLE = "Buttons";
49 const char* const SMALL_IMAGE_1 = DALI_IMAGE_DIR "gallery-small-14.jpg";
50 const char* const BIG_IMAGE_1 = DALI_IMAGE_DIR "gallery-large-4.jpg";
52 const char* const SMALL_IMAGE_2 = DALI_IMAGE_DIR "gallery-small-20.jpg";
53 const char* const BIG_IMAGE_2 = DALI_IMAGE_DIR "gallery-large-11.jpg";
55 const char* const SMALL_IMAGE_3 = DALI_IMAGE_DIR "gallery-small-25.jpg";
56 const char* const BIG_IMAGE_3 = DALI_IMAGE_DIR "gallery-large-13.jpg";
58 const char* const ENABLED_IMAGE = DALI_IMAGE_DIR "item-select-check.png";
60 const char* const PUSHBUTTON_PRESS_IMAGE = DALI_IMAGE_DIR "button-down.9.png";
61 const char* const PUSHBUTTON_DISABLED_IMAGE = DALI_IMAGE_DIR "button-disabled.9.png";
62 const char* const PUSHBUTTON_BUTTON_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
64 const char* const CHECKBOX_UNSELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-unselected.png";
65 const char* const CHECKBOX_SELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-selected.png";
67 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
70 const int RADIO_LABEL_THUMBNAIL_SIZE = 48;
71 const int RADIO_IMAGE_SPACING = 8;
72 const int BUTTON_HEIGHT = 48;
74 const int MARGIN_SIZE = 10;
75 const int TOP_MARGIN = 85;
76 const int GROUP2_HEIGHT = 238;
77 const int GROUP1_HEIGHT = 120;
78 const int GROUP3_HEIGHT = 190;
79 const int GROUP4_HEIGHT = BUTTON_HEIGHT + MARGIN_SIZE * 2;
83 /** This example shows how to create and use different buttons.
85 * 1. First group of radio buttons with image actor labels selects an image to load
86 * 2. A push button loads the selected thumbnail image into the larger image pane
87 * 3. Second group of radio buttons with a table view label containing a text view and image view, and a normal text view.
88 * Selecting one of these will enable/disable the image loading push button
89 * 4. A group of check boxes
91 class ButtonsController: public ConnectionTracker
95 ButtonsController( Application& application )
96 : mApplication( application )
98 // Connect to the Application's Init signal
99 mApplication.InitSignal().Connect( this, &ButtonsController::Create );
104 // Nothing to do here
107 void Create( Application& application )
109 // The Init signal is received once (only) during the Application lifetime
111 DemoHelper::RequestThemeChange();
113 // Respond to key events
114 Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
116 // Creates a default view with a default tool bar.
117 // The view is added to the stage.
118 mContentLayer = DemoHelper::CreateView( application,
125 int yPos = TOP_MARGIN + MARGIN_SIZE;
127 // Image selector radio group
128 Actor radioGroup2Background = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
129 radioGroup2Background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
130 radioGroup2Background.SetParentOrigin( ParentOrigin::TOP_LEFT );
131 radioGroup2Background.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
132 radioGroup2Background.SetRelayoutEnabled( true );
133 radioGroup2Background.SetSize( DP(348), DP(GROUP2_HEIGHT) );
134 mContentLayer.Add( radioGroup2Background );
136 Actor radioButtonsGroup2 = Actor::New();
137 radioButtonsGroup2.SetParentOrigin( ParentOrigin::TOP_LEFT );
138 radioButtonsGroup2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
139 radioButtonsGroup2.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
140 radioButtonsGroup2.SetRelayoutEnabled( true );
141 radioButtonsGroup2.SetSize( DP(100), DP(160) );
143 radioGroup2Background.Add( radioButtonsGroup2 );
149 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_1 ) );
150 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
151 mRadioButtonImage1 = Dali::Toolkit::RadioButton::New( imageActor );
152 mRadioButtonImage1.SetParentOrigin( ParentOrigin::TOP_LEFT );
153 mRadioButtonImage1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
154 mRadioButtonImage1.SetPosition( 0, DP(radioY) );
155 mRadioButtonImage1.SetSelected( true );
157 radioButtonsGroup2.Add( mRadioButtonImage1 );
162 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
164 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
165 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
167 mRadioButtonImage2 = Dali::Toolkit::RadioButton::New( imageActor );
168 mRadioButtonImage2.SetParentOrigin( ParentOrigin::TOP_LEFT );
169 mRadioButtonImage2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
170 mRadioButtonImage2.SetPosition( 0, DP(radioY) );
172 radioButtonsGroup2.Add( mRadioButtonImage2 );
177 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
179 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
180 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
182 mRadioButtonImage3 = Dali::Toolkit::RadioButton::New( imageActor );
183 mRadioButtonImage3.SetParentOrigin( ParentOrigin::TOP_LEFT );
184 mRadioButtonImage3.SetAnchorPoint( AnchorPoint::TOP_LEFT );
185 mRadioButtonImage3.SetPosition( 0, DP(radioY) );
187 radioButtonsGroup2.Add( mRadioButtonImage3 );
190 // Create select button
191 mUpdateButton = Toolkit::PushButton::New();
192 mUpdateButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
193 mUpdateButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
194 mUpdateButton.SetPosition( 0, DP(MARGIN_SIZE) );
195 mUpdateButton.SetLabel( "Select" );
196 mUpdateButton.SetSize( DP(100), DP(BUTTON_HEIGHT) );
198 mUpdateButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
199 mUpdateButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
200 mUpdateButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
202 mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
204 radioButtonsGroup2.Add(mUpdateButton);
206 // ImageActor to display selected image
207 mBigImage1 = ResourceImage::New( BIG_IMAGE_1 );
208 mBigImage2 = ResourceImage::New( BIG_IMAGE_2 );
209 mBigImage3 = ResourceImage::New( BIG_IMAGE_3 );
211 mImage = ImageActor::New( mBigImage1 );
212 mImage.SetParentOrigin( ParentOrigin::TOP_RIGHT );
213 mImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
214 mImage.SetPosition( DP(MARGIN_SIZE), 0 );
215 mImage.SetSize( DP(218), DP(218) );
216 radioButtonsGroup2.Add( mImage );
218 // The enable/disable radio group
219 yPos += GROUP2_HEIGHT + MARGIN_SIZE;
221 Actor radioGroup1Background = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
222 radioGroup1Background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
223 radioGroup1Background.SetParentOrigin( ParentOrigin::TOP_LEFT );
224 radioGroup1Background.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
225 radioGroup1Background.SetRelayoutEnabled( true );
226 radioGroup1Background.SetSize( DP(348), DP(GROUP1_HEIGHT) );
227 mContentLayer.Add( radioGroup1Background );
230 Actor radioButtonsGroup1 = Actor::New();
231 radioButtonsGroup1.SetParentOrigin( ParentOrigin::TOP_LEFT );
232 radioButtonsGroup1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
233 radioButtonsGroup1.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
235 radioGroup1Background.Add( radioButtonsGroup1 );
237 // First radio button
239 Toolkit::TableView tableView = Toolkit::TableView::New( 1, 2 );
240 tableView.SetSize( DP(260), 0.0f );
241 tableView.SetResizePolicy( USE_NATURAL_SIZE, HEIGHT );
243 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( "Select enabled" );
244 tableView.AddChild( textLabel, Toolkit::TableView::CellPosition( 0, 0 ) );
246 ImageActor imageActor = ImageActor::New( ResourceImage::New( ENABLED_IMAGE ) );
247 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
248 imageActor.SetResizePolicy( FIXED, ALL_DIMENSIONS );
249 imageActor.SetPadding( Padding( DP(20.0f), 0.0f, 0.0f, 0.0f ) );
250 tableView.AddChild( imageActor, Toolkit::TableView::CellPosition( 0, 1 ) );
252 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( tableView );
253 radioButton.SetName( "radio-select-enable" );
254 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
255 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
256 radioButton.SetPosition( 0, 0 );
257 radioButton.SetSelected( true );
259 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
261 radioButtonsGroup1.Add( radioButton );
264 // Second radio button
266 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( "Select disabled" );
267 radioButton.SetName( "radio-select-disable" );
268 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
269 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
270 radioButton.SetPosition( 0, DP(50) );
272 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
274 radioButtonsGroup1.Add( radioButton );
278 yPos += GROUP1_HEIGHT + MARGIN_SIZE;
280 Actor checkBoxBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
281 checkBoxBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
282 checkBoxBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
283 checkBoxBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
284 checkBoxBackground.SetRelayoutEnabled( true );
285 checkBoxBackground.SetSize( DP(430), DP(GROUP3_HEIGHT) );
286 mContentLayer.Add( checkBoxBackground );
288 Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
289 Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
291 int checkYPos = MARGIN_SIZE;
294 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
295 checkBox.SetName( "checkbox1" );
296 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
297 checkBox.SetParentOrigin( ParentOrigin::TOP_LEFT );
298 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
299 checkBox.SetBackgroundImage( unselected );
300 checkBox.SetSelectedImage( selected );
301 checkBox.SetLabel( "CheckBox1 is unselected" );
302 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
304 checkBoxBackground.Add( checkBox );
310 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
311 checkBox.SetName( "checkbox2" );
312 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
313 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
314 checkBox.SetBackgroundImage( unselected );
315 checkBox.SetSelectedImage( selected );
316 checkBox.SetLabel( "CheckBox2 is selected" );
317 checkBox.SetSelected( true );
318 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
320 checkBoxBackground.Add( checkBox );
326 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
327 checkBox.SetName( "checkbox3" );
328 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
329 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
330 checkBox.SetBackgroundImage( unselected );
331 checkBox.SetSelectedImage( selected );
332 checkBox.SetLabel( "CheckBox3 is unselected" );
333 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
335 checkBoxBackground.Add( checkBox );
338 // Create togglabe button
339 yPos += GROUP3_HEIGHT + MARGIN_SIZE;
341 Actor toggleBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
342 toggleBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
343 toggleBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
344 toggleBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
345 toggleBackground.SetRelayoutEnabled( true );
346 toggleBackground.SetSize( DP(150 + MARGIN_SIZE * 2), DP(GROUP4_HEIGHT) );
347 mContentLayer.Add( toggleBackground );
349 Toolkit::PushButton toggleButton = Toolkit::PushButton::New();
350 toggleButton.SetTogglableButton( true );
351 toggleButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
352 toggleButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
353 toggleButton.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
354 toggleButton.SetLabel( "Unselected" );
355 toggleButton.SetSize( DP(150), DP(BUTTON_HEIGHT) );
357 toggleButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
358 toggleButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
359 toggleButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
361 toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
363 toggleBackground.Add( toggleButton );
366 void OnKeyEvent( const KeyEvent& event )
368 if( event.state == KeyEvent::Down )
370 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
372 // Exit application when click back or escape.
378 bool OnButtonSelected( Toolkit::Button button )
380 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
383 if( button.IsSelected() )
385 pushButton.SetLabel( "Selected" );
389 pushButton.SetLabel( "Unselected" );
396 bool EnableSelectButton( Toolkit::Button button )
398 if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
400 mUpdateButton.SetDisabled( false );
402 else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
404 mUpdateButton.SetDisabled( true );
410 bool OnButtonClicked(Toolkit::Button button)
412 if( mRadioButtonImage1.IsSelected() )
414 mImage.SetImage( mBigImage1 );
416 else if( mRadioButtonImage2.IsSelected() )
418 mImage.SetImage( mBigImage2 );
420 else if( mRadioButtonImage3.IsSelected() )
422 mImage.SetImage( mBigImage3 );
427 bool OnCheckBoxesSelected( Toolkit::Button button )
429 if( button.GetName() == "checkbox1" )
431 if( button.IsSelected() )
433 button.SetLabel("CheckBox1 is selected");
437 button.SetLabel("CheckBox1 is unselected");
441 if( button.GetName() == "checkbox2" )
443 if( button.IsSelected() )
445 button.SetLabel("CheckBox2 is selected");
449 button.SetLabel("CheckBox2 is unselected");
453 if( button.GetName() == "checkbox3" )
455 if( button.IsSelected() )
457 button.SetLabel("CheckBox3 is selected");
461 button.SetLabel("CheckBox3 is unselected");
470 Application& mApplication;
471 Toolkit::View mView; ///< The View instance.
472 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
473 Layer mContentLayer; ///< Content layer
475 Toolkit::RadioButton mRadioButtonImage1;
476 Toolkit::RadioButton mRadioButtonImage2;
477 Toolkit::RadioButton mRadioButtonImage3;
479 Toolkit::PushButton mUpdateButton;
487 void RunTest( Application& application )
489 ButtonsController test( application );
491 application.MainLoop();
494 // Entry point for Linux & Tizen applications
496 int main( int argc, char **argv )
498 Application application = Application::New( &argc, &argv );
500 RunTest( application );