Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-demo.git] / examples / buttons / buttons-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 using namespace Dali;
23
24 namespace
25 {
26 // Used to produce visually same dimensions on desktop and device builds
27 float ScalePointSize( int pointSize )
28 {
29   Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
30   float meanDpi = (dpi.height + dpi.width) * 0.5f;
31   return pointSize * meanDpi / 220.0f;
32 }
33
34 } // namespace
35
36 // Define this so that it is interchangeable
37 // "DP" stands for Device independent Pixels
38 #define DP(x) ScalePointSize(x)
39
40
41 namespace
42 {
43
44 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
45 const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
46
47 const char* const TOOLBAR_TITLE = "Buttons";
48
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";
51
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";
54
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";
57
58 const char* const ENABLED_IMAGE = DALI_IMAGE_DIR "item-select-check.png";
59
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";
63
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";
66
67 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
68
69 // Layout sizes
70 const int RADIO_LABEL_THUMBNAIL_SIZE = 48;
71 const int RADIO_IMAGE_SPACING = 8;
72 const int BUTTON_HEIGHT = 48;
73
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;
80
81 }  // namespace
82
83 /** This example shows how to create and use different buttons.
84  *
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
90  */
91 class ButtonsController: public ConnectionTracker
92 {
93  public:
94
95   ButtonsController( Application& application )
96     : mApplication( application )
97   {
98     // Connect to the Application's Init signal
99     mApplication.InitSignal().Connect( this, &ButtonsController::Create );
100   }
101
102   ~ButtonsController()
103   {
104     // Nothing to do here
105   }
106
107   void Create( Application& application )
108   {
109     // The Init signal is received once (only) during the Application lifetime
110
111     // Respond to key events
112     Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
113
114     // Creates a default view with a default tool bar.
115     // The view is added to the stage.
116     mContentLayer = DemoHelper::CreateView( application,
117                                             mView,
118                                             mToolBar,
119                                             BACKGROUND_IMAGE,
120                                             TOOLBAR_IMAGE,
121                                             TOOLBAR_TITLE );
122
123     int yPos = TOP_MARGIN + MARGIN_SIZE;
124
125     // Image selector radio group
126     Actor radioGroup2Background = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
127     radioGroup2Background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
128     radioGroup2Background.SetParentOrigin( ParentOrigin::TOP_LEFT );
129     radioGroup2Background.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
130     radioGroup2Background.SetRelayoutEnabled( true );
131     radioGroup2Background.SetPreferredSize( Vector2( DP(348), DP(GROUP2_HEIGHT) ) );
132     mContentLayer.Add( radioGroup2Background );
133
134     Actor radioButtonsGroup2 = Actor::New();
135     radioButtonsGroup2.SetParentOrigin( ParentOrigin::TOP_LEFT );
136     radioButtonsGroup2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
137     radioButtonsGroup2.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
138     radioButtonsGroup2.SetRelayoutEnabled( true );
139     radioButtonsGroup2.SetPreferredSize( Vector2( DP(100), DP(160) ) );
140
141     radioGroup2Background.Add( radioButtonsGroup2 );
142
143     int radioY = 0;
144
145     // Radio 1
146     {
147       ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_1 ) );
148       imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
149       mRadioButtonImage1 = Dali::Toolkit::RadioButton::New( imageActor );
150       mRadioButtonImage1.SetParentOrigin( ParentOrigin::TOP_LEFT );
151       mRadioButtonImage1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
152       mRadioButtonImage1.SetPosition( 0, DP(radioY) );
153       mRadioButtonImage1.SetSelected( true );
154
155       radioButtonsGroup2.Add( mRadioButtonImage1 );
156     }
157
158     // Radio 2
159     {
160       radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
161
162       ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
163       imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
164
165       mRadioButtonImage2 = Dali::Toolkit::RadioButton::New( imageActor );
166       mRadioButtonImage2.SetParentOrigin( ParentOrigin::TOP_LEFT );
167       mRadioButtonImage2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
168       mRadioButtonImage2.SetPosition( 0, DP(radioY) );
169
170       radioButtonsGroup2.Add( mRadioButtonImage2 );
171     }
172
173     // Radio 3
174     {
175       radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
176
177       ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
178       imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
179
180       mRadioButtonImage3 = Dali::Toolkit::RadioButton::New( imageActor );
181       mRadioButtonImage3.SetParentOrigin( ParentOrigin::TOP_LEFT );
182       mRadioButtonImage3.SetAnchorPoint( AnchorPoint::TOP_LEFT );
183       mRadioButtonImage3.SetPosition( 0, DP(radioY) );
184
185       radioButtonsGroup2.Add( mRadioButtonImage3 );
186     }
187
188     // Create select button
189     mUpdateButton = Toolkit::PushButton::New();
190     mUpdateButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
191     mUpdateButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
192     mUpdateButton.SetPosition( 0, DP(MARGIN_SIZE) );
193     mUpdateButton.SetLabel( "Select" );
194     mUpdateButton.SetPreferredSize( Vector2( DP(100), DP(BUTTON_HEIGHT) ) );
195
196     mUpdateButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
197     mUpdateButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
198     mUpdateButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
199
200     mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
201
202     radioButtonsGroup2.Add(mUpdateButton);
203
204     // ImageActor to display selected image
205     mBigImage1 = ResourceImage::New( BIG_IMAGE_1 );
206     mBigImage2 = ResourceImage::New( BIG_IMAGE_2 );
207     mBigImage3 = ResourceImage::New( BIG_IMAGE_3 );
208
209     mImage = ImageActor::New( mBigImage1 );
210     mImage.SetParentOrigin( ParentOrigin::TOP_RIGHT );
211     mImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
212     mImage.SetPosition( DP(MARGIN_SIZE), 0 );
213     mImage.SetPreferredSize( Vector2( DP(218), DP(218) ) );
214     radioButtonsGroup2.Add( mImage );
215
216     // The enable/disable radio group
217     yPos += GROUP2_HEIGHT + MARGIN_SIZE;
218
219     Actor radioGroup1Background = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
220     radioGroup1Background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
221     radioGroup1Background.SetParentOrigin( ParentOrigin::TOP_LEFT );
222     radioGroup1Background.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
223     radioGroup1Background.SetRelayoutEnabled( true );
224     radioGroup1Background.SetPreferredSize( Vector2( DP(348), DP(GROUP1_HEIGHT) ) );
225     mContentLayer.Add( radioGroup1Background );
226
227     // Radio group
228     Actor radioButtonsGroup1 = Actor::New();
229     radioButtonsGroup1.SetParentOrigin( ParentOrigin::TOP_LEFT );
230     radioButtonsGroup1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
231     radioButtonsGroup1.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
232
233     radioGroup1Background.Add( radioButtonsGroup1 );
234
235     // First radio button
236     {
237       Toolkit::TableView tableView = Toolkit::TableView::New( 1, 2 );
238       tableView.SetPreferredSize( Vector2( DP(260), 0.0f ) );
239       tableView.SetResizePolicy( USE_NATURAL_SIZE, HEIGHT );
240
241       ImageActor imageActor = ImageActor::New( ResourceImage::New( ENABLED_IMAGE ) );
242       imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
243       imageActor.SetResizePolicy( FIXED, ALL_DIMENSIONS );
244       imageActor.SetPadding( Padding( DP(20.0f), 0.0f, 0.0f, 0.0f ) );
245       tableView.AddChild( imageActor, Toolkit::TableView::CellPosition( 0, 1 ) );
246
247       Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( tableView );
248       radioButton.SetName( "radio-select-enable" );
249       radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
250       radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
251       radioButton.SetPosition( 0, 0 );
252       radioButton.SetSelected( true );
253
254       radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
255
256       radioButtonsGroup1.Add( radioButton );
257     }
258
259     // Second radio button
260     {
261       Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( "Select disabled" );
262       radioButton.SetName( "radio-select-disable" );
263       radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
264       radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
265       radioButton.SetPosition( 0, DP(50) );
266
267       radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
268
269       radioButtonsGroup1.Add( radioButton );
270     }
271
272     // CheckBoxes
273     yPos += GROUP1_HEIGHT + MARGIN_SIZE;
274
275     Actor checkBoxBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
276     checkBoxBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
277     checkBoxBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
278     checkBoxBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
279     checkBoxBackground.SetRelayoutEnabled( true );
280     checkBoxBackground.SetPreferredSize( Vector2( DP(430), DP(GROUP3_HEIGHT) ) );
281     mContentLayer.Add( checkBoxBackground );
282
283     Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
284     Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
285
286     int checkYPos = MARGIN_SIZE;
287
288     {
289       Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
290       checkBox.SetName( "checkbox1" );
291       checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
292       checkBox.SetParentOrigin( ParentOrigin::TOP_LEFT );
293       checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
294       checkBox.SetBackgroundImage( unselected );
295       checkBox.SetSelectedImage( selected );
296       checkBox.SetLabel( "CheckBox1 is unselected" );
297       checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
298
299       checkBoxBackground.Add( checkBox );
300     }
301
302     checkYPos += 60;
303
304     {
305       Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
306       checkBox.SetName( "checkbox2" );
307       checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
308       checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
309       checkBox.SetBackgroundImage( unselected );
310       checkBox.SetSelectedImage( selected );
311       checkBox.SetLabel( "CheckBox2 is selected" );
312       checkBox.SetSelected( true );
313       checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
314
315       checkBoxBackground.Add( checkBox );
316     }
317
318     checkYPos += 60;
319
320     {
321       Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
322       checkBox.SetName( "checkbox3" );
323       checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
324       checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
325       checkBox.SetBackgroundImage( unselected );
326       checkBox.SetSelectedImage( selected );
327       checkBox.SetLabel( "CheckBox3 is unselected" );
328       checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
329
330       checkBoxBackground.Add( checkBox );
331     }
332
333     // Create togglabe button
334     yPos += GROUP3_HEIGHT + MARGIN_SIZE;
335
336     Actor toggleBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
337     toggleBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
338     toggleBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
339     toggleBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
340     toggleBackground.SetRelayoutEnabled( true );
341     toggleBackground.SetPreferredSize( Vector2( DP(150 + MARGIN_SIZE * 2), DP(GROUP4_HEIGHT) ) );
342     mContentLayer.Add( toggleBackground );
343
344     Toolkit::PushButton toggleButton = Toolkit::PushButton::New();
345     toggleButton.SetTogglableButton( true );
346     toggleButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
347     toggleButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
348     toggleButton.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
349     toggleButton.SetLabel( "Unselected" );
350     toggleButton.SetPreferredSize( Vector2( DP(150), DP(BUTTON_HEIGHT) ) );
351
352     toggleButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
353     toggleButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
354     toggleButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
355
356     toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
357
358     toggleBackground.Add( toggleButton );
359   }
360
361   void OnKeyEvent( const KeyEvent& event )
362   {
363     if( event.state == KeyEvent::Down )
364     {
365       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
366       {
367         // Exit application when click back or escape.
368         mApplication.Quit();
369       }
370     }
371   }
372
373   bool OnButtonSelected( Toolkit::Button button )
374   {
375     Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
376     if( pushButton )
377     {
378       if( button.IsSelected() )
379       {
380         pushButton.SetLabel( "Selected" );
381       }
382       else
383       {
384         pushButton.SetLabel( "Unselected" );
385       }
386     }
387
388     return true;
389   }
390
391   bool EnableSelectButton( Toolkit::Button button )
392   {
393     if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
394     {
395       mUpdateButton.SetDisabled( false );
396     }
397     else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
398     {
399       mUpdateButton.SetDisabled( true );
400     }
401
402     return true;
403   }
404
405   bool OnButtonClicked(Toolkit::Button button)
406   {
407     if( mRadioButtonImage1.IsSelected() )
408     {
409       mImage.SetImage( mBigImage1 );
410     }
411     else if( mRadioButtonImage2.IsSelected() )
412     {
413       mImage.SetImage( mBigImage2 );
414     }
415     else if( mRadioButtonImage3.IsSelected() )
416     {
417       mImage.SetImage( mBigImage3 );
418     }
419     return true;
420   }
421
422   bool OnCheckBoxesSelected( Toolkit::Button button )
423   {
424     return true;
425   }
426
427  private:
428
429   Application&      mApplication;
430   Toolkit::View     mView;                              ///< The View instance.
431   Toolkit::ToolBar  mToolBar;                           ///< The View's Toolbar.
432   Layer             mContentLayer;                      ///< Content layer
433
434   Toolkit::RadioButton mRadioButtonImage1;
435   Toolkit::RadioButton mRadioButtonImage2;
436   Toolkit::RadioButton mRadioButtonImage3;
437
438   Toolkit::PushButton mUpdateButton;
439
440   Image mBigImage1;
441   Image mBigImage2;
442   Image mBigImage3;
443   ImageActor mImage;
444 };
445
446 void RunTest( Application& application )
447 {
448   ButtonsController test( application );
449
450   application.MainLoop();
451 }
452
453 // Entry point for Linux & Tizen applications
454 //
455 int main( int argc, char **argv )
456 {
457   Application application = Application::New( &argc, &argv );
458
459   RunTest( application );
460
461   return 0;
462 }