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