Revert "[Tizen] Add codes for Dali Windows Backend"
[platform/core/uifw/dali-demo.git] / examples / text-label / text-label-example.cpp
1 /*
2  * Copyright (c) 2018 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 /**
19  * @file text-label-example.cpp
20  * @brief Usage of TextLabel control with style application.
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali/devel-api/actors/actor-devel.h>
26 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
27 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
28 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
29 #include <dali-toolkit/dali-toolkit.h>
30 #include <iostream>
31
32 // INTERNAL INCLUDES
33 #include "shared/multi-language-strings.h"
34 #include "shared/view.h"
35 #include "expanding-buttons.h"
36
37 using namespace Dali;
38 using namespace Dali::Toolkit;
39 using namespace MultiLanguageStrings;
40
41 namespace
42 {
43 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
44 const char* const STYLE_SELECTED_IMAGE = DEMO_IMAGE_DIR "FontStyleButton_OK_03.png";
45
46 const char* BUTTON_IMAGES[] =
47 {
48   DEMO_IMAGE_DIR "FontStyleButton_Colour.png",
49   DEMO_IMAGE_DIR "FontStyleButton_Outline.png",
50   DEMO_IMAGE_DIR "FontStyleButton_Shadow.png",
51   DEMO_IMAGE_DIR "FontStyleButton_Background.png"
52 };
53
54 const unsigned int KEY_ZERO = 10;
55 const unsigned int KEY_ONE = 11;
56 const unsigned int KEY_A = 38;
57 const unsigned int KEY_F = 41;
58 const unsigned int KEY_H = 43;
59 const unsigned int KEY_U = 30;
60 const unsigned int KEY_V = 55;
61 const unsigned int KEY_M = 58;
62 const unsigned int KEY_L = 46;
63 const unsigned int KEY_S = 39;
64 const unsigned int KEY_PLUS = 21;
65 const unsigned int KEY_MINUS = 20;
66
67 const char* H_ALIGNMENT_STRING_TABLE[] =
68 {
69   "BEGIN",
70   "CENTER",
71   "END"
72 };
73
74 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
75
76 const char* V_ALIGNMENT_STRING_TABLE[] =
77 {
78   "TOP",
79   "CENTER",
80   "BOTTOM"
81 };
82
83 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
84
85 enum StyleType
86 {
87   TEXT_COLOR = 0,
88   OUTLINE,
89   SHADOW,
90   BACKGROUND,
91   NUMBER_OF_STYLES
92 };
93
94 const Vector4 AVAILABLE_COLORS[] =
95 {
96   Color::GREEN,
97   Color::BLUE,
98   Color::RED,
99   Color::CYAN,
100   Color::WHITE // Used as clear
101 };
102
103 const unsigned int NUMBER_OF_COLORS = sizeof( AVAILABLE_COLORS ) / sizeof( AVAILABLE_COLORS[0u] );
104
105 int ConvertToEven(int value)
106 {
107   return (value % 2 == 0) ? value : (value + 1);
108 }
109
110 struct HSVColorConstraint
111 {
112   HSVColorConstraint(float hue, float saturation, float value)
113   : hue(hue),
114     saturation(saturation),
115     value(value)
116   {
117   }
118
119   void operator()(Vector3& current, const PropertyInputContainer& inputs )
120   {
121     current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
122   }
123
124   Vector3 hsv2rgb(Vector3 colorHSV)
125   {
126     float r=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x)-1));
127     float g=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x-2.09439)-1));
128     float b=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x+2.09439)-1));
129     return Vector3(r, g, b);
130   }
131   float hue;
132   float saturation;
133   float value;
134 };
135
136 const float STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE = 0.9f;
137 const float BUTTON_SIZE_RATIO_TO_STAGE = 0.1f;
138 const float OUTLINE_WIDTH = 2.0f;
139 const Vector2 SHADOW_OFFSET = Vector2( 2.0f, 2.0f );
140 const int GAP_BETWEEN_BUTTONS = 3;
141
142
143 } // anonymous namespace
144
145 /**
146  * @brief The main class of the demo.
147  */
148 class TextLabelExample : public ConnectionTracker
149 {
150 public:
151
152   TextLabelExample( Application& application )
153   : mApplication( application ),
154     mLabel(),
155     mSelectedColor(AVAILABLE_COLORS[0]),
156     mStyleActivatedForColor( NUMBER_OF_STYLES ),
157     mContainer(),
158     mGrabCorner(),
159     mBorder(),
160     mPanGestureDetector(),
161     mLayoutSize(),
162     mLanguageId( 0u ),
163     mAlignment( 0u ),
164     mHueAngleIndex( Property::INVALID_INDEX ),
165     mOverrideMixColorIndex( Property::INVALID_INDEX ),
166     mColorButtonsHidden( true ),
167     mCollapseColorsAndStyles( false )
168   {
169     // Connect to the Application's Init signal
170     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
171
172     // Set Style flags to inactive
173     for ( unsigned int i = TEXT_COLOR; i < NUMBER_OF_STYLES; i++ )
174     {
175       mStyleActiveState[ i ] = false;
176       mCurrentStyleColor[i] = AVAILABLE_COLORS[ NUMBER_OF_COLORS - 1 ];
177     }
178   }
179
180   ~TextLabelExample()
181   {
182     // Nothing to do here.
183   }
184
185   // Clicking the expanding button shows the registered style buttons.
186   void SetUpExpandingStyleButtons( Vector2 position )
187   {
188     mExpandingButtons = Demo::ExpandingButtons::New();
189     mExpandingButtons.SetPosition( mButtonSize.width, mStageSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE );
190     mExpandingButtons.CollapsingSignal().Connect( this, &TextLabelExample::OnExpandingButtonCollapsing );
191     mExpandingButtons.SetSize( mButtonSize );
192     // Creates the buttons to be expanded
193     CreateStyleButtons();
194
195     // Register the created buttons with the ExpandingButtons.
196     for ( unsigned int index = 0; index < NUMBER_OF_STYLES; index++ )
197     {
198       mExpandingButtons.RegisterButton( mStyleButtons[index] );
199     }
200   }
201
202   /**
203    * One-time setup in response to Application InitSignal.
204    */
205   void Create( Application& application )
206   {
207     Stage stage = Stage::GetCurrent();
208
209     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
210     mStageSize = stage.GetSize();
211     mButtonSize = Size( mStageSize.height * 0.1, mStageSize.height * 0.1 ); // Button size 1/10 of stage height
212
213     mContainer = Control::New();
214     mContainer.SetName( "Container" );
215     mContainer.SetParentOrigin( ParentOrigin::CENTER );
216     mLayoutSize = Vector2(mStageSize.width*0.6f, mStageSize.width*0.6f);
217     mContainer.SetSize( mLayoutSize );
218     stage.Add( mContainer );
219
220     // Resize the center layout when the corner is grabbed
221     mGrabCorner = ImageView::New( BACKGROUND_IMAGE );
222     mGrabCorner.SetName( "GrabCorner" );
223     mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
224     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
225     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
226     mContainer.Add( mGrabCorner );
227
228     mPanGestureDetector = PanGestureDetector::New();
229     mPanGestureDetector.Attach( mGrabCorner );
230     mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
231
232     mLabel = TextLabel::New( "\xF0\x9F\x98\x89 A Quick Brown Fox Jumps Over The Lazy Dog" );
233
234     mLabel.SetName( "TextLabel" );
235     mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
236     mLabel.SetSize(mLayoutSize);
237     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
238     mLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::GREEN );
239     mLabel.SetBackgroundColor( Color::WHITE );
240     mContainer.Add( mLabel );
241
242     // Clicking ExpandingButton shows the Registered Style buttons, clicking again hides them.
243     Vector2 expandingButtonPosition( mButtonSize.width, mStageSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE );
244     SetUpExpandingStyleButtons( expandingButtonPosition );
245     stage.Add( mExpandingButtons );
246
247     // Add a border for the container so you can see the container is being resized while grabbing the handle.
248     mBorder = Control::New();
249     mBorder.SetAnchorPoint( AnchorPoint::TOP_LEFT );
250     mBorder.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
251     mBorder.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
252
253     Dali::Property::Map border;
254     border.Insert( Toolkit::Visual::Property::TYPE,  Visual::BORDER );
255     border.Insert( BorderVisual::Property::COLOR,  Color::WHITE );
256     border.Insert( BorderVisual::Property::SIZE,  3.f );
257     mBorder.SetProperty( Control::Property::BACKGROUND, border );
258     mContainer.Add( mBorder );
259     mBorder.SetVisible(false);
260     mGrabCorner.RaiseToTop();
261
262     mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
263     Renderer bgRenderer = mLabel.GetRendererAt(0);
264     mOverrideMixColorIndex = DevelHandle::GetPropertyIndex( bgRenderer, ColorVisual::Property::MIX_COLOR );
265
266     Constraint constraint = Constraint::New<Vector3>( bgRenderer, mOverrideMixColorIndex, HSVColorConstraint(0.0f, 0.5f, 0.8f));
267     constraint.AddSource( Source( mLabel, mHueAngleIndex ) );
268     constraint.SetRemoveAction( Constraint::Discard );
269     constraint.Apply();
270
271     Animation anim = Animation::New(50.0f);
272     anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
273     anim.SetLooping(true);
274     anim.Play();
275
276     mContainer.RaiseToTop();
277     mGrabCorner.RaiseToTop();
278
279     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
280     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
281   }
282
283   // If the styling buttons should colapse (hide) then the color buttons should also hide.
284   bool OnExpandingButtonCollapsing( Demo::ExpandingButtons button )
285   {
286     mCollapseColorsAndStyles = true;
287     HideColorButtons();
288     return true;
289   }
290
291   // Get the style type from the given button
292   StyleType GetStyleTypeFromButton( Toolkit::Button button )
293   {
294     StyleType style = StyleType::TEXT_COLOR;
295
296     if( button == mStyleButtons[ StyleType::OUTLINE ] )
297     {
298       style = StyleType::OUTLINE;
299     }
300     else if( button == mStyleButtons[ StyleType::SHADOW ] )
301     {
302       style = StyleType::SHADOW;
303     }
304     else if( button == mStyleButtons[ StyleType::BACKGROUND ] )
305     {
306       style = StyleType::BACKGROUND;
307     }
308     return style;
309   }
310
311   // Style selected, show color buttons
312   bool OnStyleButtonClicked( Toolkit::Button button )
313   {
314     StyleType selectedStyle = GetStyleTypeFromButton( button );
315     if ( mStyleActivatedForColor == selectedStyle )
316     {
317       HideColorButtons();
318     }
319     else
320     {
321       ResetColorButtons( mColorButtons, NUMBER_OF_COLORS );
322       ShowColorButtons( selectedStyle);
323     }
324     return true;
325   }
326
327   // Set style to selected color
328   bool OnColorSelected( Toolkit::Button button )
329   {
330     for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
331     {
332       if ( mColorButtons[index] == button )
333       {
334         mSelectedColor = AVAILABLE_COLORS[ index ];
335       }
336     }
337
338     switch ( mStyleActivatedForColor )
339     {
340       case TEXT_COLOR :
341       {
342         Animation animation = Animation::New( 1.f );
343         animation.AnimateTo( Property( mLabel, TextLabel::Property::TEXT_COLOR ), mSelectedColor, AlphaFunction::LINEAR );
344         mCurrentStyleColor[ TEXT_COLOR ] = mSelectedColor;
345         animation.Play();
346         break;
347       }
348       case OUTLINE :
349       {
350         Property::Map outlineMap;
351         float outlineWidth = OUTLINE_WIDTH;
352
353         if( mStyleActiveState[ OUTLINE ] )
354         {
355           outlineWidth = ( Color::WHITE == mSelectedColor ) ? 0.0f : OUTLINE_WIDTH ;  // toggles outline on/off
356         }
357         mStyleActiveState[ OUTLINE ] = ( outlineWidth > 0.0f ) ? true : false;
358
359         outlineMap["color"] = mSelectedColor;
360         outlineMap["width"] = outlineWidth;
361         mCurrentStyleColor[ OUTLINE ] = mSelectedColor;
362         mLabel.SetProperty( TextLabel::Property::OUTLINE, outlineMap );
363         break;
364       }
365       case SHADOW :
366       {
367         Vector2 shadowOffset( SHADOW_OFFSET ); // Will be set to zeros if color already set
368         Property::Value value = mLabel.GetProperty( TextLabel::Property::SHADOW_COLOR  );
369         Vector4 currentShadowColor;
370         value.Get( currentShadowColor );
371
372         if ( mStyleActiveState[ SHADOW ] )
373         {
374           // toggle shadow off ( zero offset ) if color is already set
375           shadowOffset = (  Color::WHITE == mSelectedColor ) ? Vector2::ZERO : Vector2( SHADOW_OFFSET );
376         }
377
378         mStyleActiveState[ SHADOW ] = ( shadowOffset == Vector2::ZERO ) ? false : true;
379         mCurrentStyleColor[ SHADOW ] = mSelectedColor;
380
381         Property::Map shadowMap;
382         shadowMap.Insert( "offset", shadowOffset );
383         shadowMap.Insert( "color", mSelectedColor );
384         mLabel.SetProperty( TextLabel::Property::SHADOW, shadowMap );
385
386         break;
387       }
388       case BACKGROUND :
389       {
390         Property::Map backgroundMap;
391         auto backgroundEnabled(true);
392
393         if( mStyleActiveState[ BACKGROUND ] )
394         {
395           backgroundEnabled = ( Color::WHITE != mSelectedColor );  // toggles background on/off
396         }
397         mStyleActiveState[ BACKGROUND ] = backgroundEnabled;
398
399         backgroundMap["color"] = mSelectedColor;
400         backgroundMap["enable"] = backgroundEnabled;
401         mCurrentStyleColor[ BACKGROUND ] = mSelectedColor;
402         mLabel.SetProperty( DevelTextLabel::Property::BACKGROUND, backgroundMap );
403
404         break;
405       }
406       default :
407         break;
408     }
409
410     return true;
411   }
412
413   // Set the inital color button that should be be selected.
414   // If the style already has a color set then that should be used
415   void SetInitialSelectedColorButton( StyleType styleButtonIndex )
416   {
417     Vector4 selectedColor = mCurrentStyleColor[ styleButtonIndex ];
418
419     for ( unsigned int i = 0; i < NUMBER_OF_COLORS; i++ )
420     {
421       if ( AVAILABLE_COLORS[i] == selectedColor )
422       {
423         if ( mColorButtons[i] )
424         {
425           mColorButtons[ i ].SetProperty( Toolkit::DevelButton::Property::SELECTED, true );
426         }
427         break;
428       }
429     }
430   }
431
432   // Create a bar of color buttons that the user can select.
433   void ShowColorButtons( StyleType styleButtonIndex )
434   {
435     mCollapseColorsAndStyles = false; // Request to show colors so reset flag
436     mStyleActivatedForColor = styleButtonIndex;
437
438     for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
439     {
440       if( !mColorButtonsAnimation )
441       {
442         mColorButtonsAnimation = Animation::New( 0.15f );
443         mColorButtonsAnimation.FinishedSignal().Connect( this, &TextLabelExample::OnColorButtonAnimationFinished );
444       }
445
446       // Create a color button
447       if ( ! mColorButtons[index] )
448       {
449         mColorButtons[index] = RadioButton::New();
450         mColorButtons[index].SetSize( mButtonSize );
451         mColorButtons[index].ClickedSignal().Connect( this, &TextLabelExample::OnColorSelected );
452         mColorButtons[index].SetProperty( Button::Property::TOGGLABLE, true );
453         Property::Map propertyMap;
454         propertyMap.Insert(Visual::Property::TYPE,  Visual::COLOR);
455         propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[ index ]);
456         mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, propertyMap );
457         mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_VISUAL, propertyMap );
458         mColorButtons[index].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
459         mColorButtons[index].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
460
461
462         propertyMap.Insert(Visual::Property::TYPE,  Visual::COLOR);
463         propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[ index ]);
464         mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, propertyMap );
465
466         mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_VISUAL,
467                             Property::Map().Add( Visual::Property::TYPE, Visual::BORDER )
468                                            .Add( BorderVisual::Property::COLOR, Color::WHITE )
469                                            .Add( BorderVisual::Property::SIZE, 4.0f )
470                                            .Add( BorderVisual::Property::ANTI_ALIASING, true ) );
471
472         // Use a white button with 50% transparency as a clear color button
473         if ( Color::WHITE == AVAILABLE_COLORS[ index ] && styleButtonIndex != StyleType::TEXT_COLOR )
474         {
475           mColorButtons[index].SetOpacity(0.5f);
476
477           mColorButtons[index].SetProperty( Toolkit::Button::Property::LABEL,
478                                             Property::Map().Add( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT )
479                                                            .Add( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::CENTER )
480                                                            .Add( Toolkit::TextVisual::Property::TEXT, "off") );
481
482         }
483       }
484
485       SetInitialSelectedColorButton( mStyleActivatedForColor );
486
487       mColorButtons[index].Unparent();
488
489       mStyleButtons[styleButtonIndex].Add( mColorButtons[index] );
490       mColorButtons[index].Lower();
491
492       // Position button using nice animation
493       mColorButtons[index].SetY( -GAP_BETWEEN_BUTTONS );
494       float desiredPosition = -( mButtonSize.height + GAP_BETWEEN_BUTTONS ) * (index);
495       AlphaFunction focusedAlphaFunction = AlphaFunction( Vector2 ( 0.32f, 0.08f ), Vector2( 0.38f, 1.72f ) );
496       mColorButtonsAnimation.AnimateBy( Property( mColorButtons[index], Actor::Property::POSITION_Y ), desiredPosition, focusedAlphaFunction );
497     }
498
499     mColorButtonsHidden = false;
500     mColorButtonsAnimation.Play();
501   }
502
503   // Remove the color buttons when not being shown.
504   void ResetColorButtons( Button buttons[], unsigned int numberOfButtons )
505   {
506     for( unsigned int index = 0; index < numberOfButtons; index++)
507     {
508       UnparentAndReset( buttons[index] );
509     }
510   }
511
512   void OnColorButtonAnimationFinished( Animation& animation )
513   {
514     animation.Clear();
515     if ( mColorButtonsHidden )
516     {
517       ResetColorButtons( mColorButtons, NUMBER_OF_COLORS );
518       animation.Reset(); // Handle reset
519       if ( mCollapseColorsAndStyles )
520       {
521         mExpandingButtons.Collapse();
522       }
523     }
524   }
525
526   // Create the style buttons that will expand from the expanding button.
527   void CreateStyleButtons()
528   {
529     for ( unsigned int index = 0; index < NUMBER_OF_STYLES; index++ )
530     {
531       if ( ! mStyleButtons[index] )
532       {
533         mStyleButtons[index] = PushButton::New();
534         mStyleButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, BUTTON_IMAGES[ index ] );
535         mStyleButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, STYLE_SELECTED_IMAGE );
536         mStyleButtons[index].SetProperty( Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
537         mStyleButtons[index].SetSize( mButtonSize );
538         mStyleButtons[index].ClickedSignal().Connect( this, &TextLabelExample::OnStyleButtonClicked );
539       }
540     }
541   }
542
543   // Animate away the color bar.
544   void HideColorButtons()
545   {
546     if ( ! mColorButtonsHidden )
547     {
548       for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
549       {
550         mColorButtonsAnimation.AnimateTo( Property( mColorButtons[index], Actor::Property::POSITION_Y ), 0.0f );
551       }
552       mColorButtonsHidden = true;
553       mColorButtonsAnimation.Play();
554     }
555     mStyleActivatedForColor = NUMBER_OF_STYLES;
556   }
557
558   //  Request the expanding button to collapse.
559   void HideStyleAndColorButtons()
560   {
561     mCollapseColorsAndStyles = true;
562     if ( mColorButtonsHidden )
563     {
564       mExpandingButtons.Collapse();
565     }
566     else
567     {
568       HideColorButtons();
569     }
570   }
571
572   // Resize the text-label with pan gesture
573   void OnPan( Actor actor, const PanGesture& gesture )
574   {
575     // Reset mLayoutSize when the pan starts
576     if( gesture.state == Gesture::Started )
577     {
578       if( mLayoutSize.x < 2.0f )
579       {
580         mLayoutSize.x = 2.0f;
581       }
582
583       if( mLayoutSize.y < 2.0f )
584       {
585         mLayoutSize.y = 2.0f;
586       }
587
588       // Only show the border during the panning
589       mBorder.SetVisible(true);
590
591       HideStyleAndColorButtons();
592     }
593
594     mLayoutSize.x += gesture.displacement.x * 2.0f;
595     mLayoutSize.y += gesture.displacement.y * 2.0f;
596
597     if( mLayoutSize.x >= 2.0f ||
598         mLayoutSize.y >= 2.0f )
599     {
600       mLayoutSize.x = std::min ( mLayoutSize.x, mStageSize.width );
601       mLayoutSize.y = std::min ( mLayoutSize.y, mStageSize.height*.9f );
602
603       // Avoid pixel mis-alignment issue
604       Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
605                                      std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
606
607       mContainer.SetSize( clampedSize );
608     }
609
610     if( gesture.state == Gesture::Cancelled || gesture.state == Gesture::Finished )
611     {
612       // Resize the text label to match the container size when panning is finished
613       mLabel.SetSize(mLayoutSize);
614       mBorder.SetVisible(false);
615     }
616   }
617
618   /**
619    * Main key event handler
620    */
621   void OnKeyEvent(const KeyEvent& event)
622   {
623     if(event.state == KeyEvent::Down)
624     {
625       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
626       {
627         mApplication.Quit();
628       }
629       else if( event.IsCtrlModifier() )
630       {
631         switch( event.keyCode )
632         {
633           // Select rendering back-end
634           case KEY_ZERO: // fall through
635           case KEY_ONE:
636           {
637             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
638             break;
639           }
640           case KEY_A: // Animate text colour
641           {
642             Animation animation = Animation::New( 2.f );
643             animation.AnimateTo( Property( mLabel, TextLabel::Property::TEXT_COLOR ), Color::RED, AlphaFunction::SIN );
644             animation.SetLoopCount( 3 );
645             animation.Play();
646             break;
647           }
648           case KEY_F: // Fill vertically
649           {
650             if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
651             {
652               mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
653             }
654             else
655             {
656               mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
657             }
658             break;
659           }
660           case KEY_H: // Horizontal alignment
661           {
662             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
663             {
664               mAlignment = 0u;
665             }
666
667             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
668             break;
669           }
670           case KEY_V: // Vertical alignment
671           {
672             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
673             {
674               mAlignment = 0u;
675             }
676
677             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
678             break;
679           }
680           case KEY_M: // Multi-line
681           {
682             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
683             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
684             break;
685           }
686           case KEY_L: // Language
687           {
688             const Language& language = LANGUAGES[ mLanguageId ];
689
690             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
691
692             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
693             {
694               mLanguageId = 0u;
695             }
696             break;
697           }
698           case KEY_S: // Shadow color
699           {
700             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
701             {
702               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
703             }
704             else
705             {
706               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
707             }
708             break;
709           }
710           case KEY_U: // Markup
711           {
712             mLabel.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
713             mLabel.SetProperty( TextLabel::Property::TEXT, "<font family='DejaVuSerif' size='18'>H<color value='blue'>ello</color> <font weight='bold'>world</font> demo</font>" );
714             break;
715           }
716           case KEY_PLUS: // Increase shadow offset
717           {
718             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
719             break;
720           }
721           case KEY_MINUS: // Decrease shadow offset
722           {
723             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
724             break;
725           }
726
727         }
728       }
729     }
730   }
731
732 private:
733
734   Application& mApplication;
735
736   TextLabel mLabel;
737
738   Demo::ExpandingButtons mExpandingButtons;
739   PushButton mStyleButtons[ NUMBER_OF_STYLES ];
740   bool mStyleActiveState[ NUMBER_OF_STYLES ];
741
742   Vector4 mCurrentStyleColor[NUMBER_OF_STYLES ];
743
744   Vector4 mSelectedColor;
745
746   Button mColorButtons[ NUMBER_OF_COLORS ];
747
748   StyleType mStyleActivatedForColor; // The style that the color bar is connected to
749
750   Control mContainer;
751   Control mGrabCorner;
752   Control mBorder;
753
754   PanGestureDetector mPanGestureDetector;
755
756   Vector2 mLayoutSize;
757
758   Animation mColorButtonsAnimation;
759
760   Size mStageSize;
761   Size mButtonSize;
762
763   unsigned int mLanguageId;
764   unsigned int mAlignment;
765   Property::Index mHueAngleIndex;
766   Property::Index mOverrideMixColorIndex;
767
768   bool mColorButtonsHidden;
769   bool mCollapseColorsAndStyles;
770 };
771
772 int DALI_EXPORT_API main( int argc, char **argv )
773 {
774   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
775   TextLabelExample test( application );
776   application.MainLoop();
777   return 0;
778 }