TextInput & TextView: Klocwork issues fix
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / text-input / text-input-popup-impl.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 // EXTERNAL INCLUDES
19 #include <libintl.h>
20 #include <dali/public-api/animation/constraints.h>
21 #include <dali/integration-api/debug.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-toolkit/internal/controls/text-input/text-input-popup-impl.h>
25 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
26 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
27
28 using namespace std;
29 using namespace Dali;
30
31 #define GET_LOCALE_TEXT(string) dgettext("sys_string", string)
32
33 namespace
34 {
35 const Vector2 DEFAULT_POPUP_INDICATOR_OFFSET(0.0f, 60.0f);
36
37 // 1. For selection: should be above top of highlighted selection, or below bottom of highlighted selection + end handle.
38 // 2. For cursor: should be above top of cursor, or below bottom of cursor + grab handle.
39
40 /**
41  * Image resource paths
42  */
43 const std::string POPUP_BACKGROUND( DALI_IMAGE_DIR "popup_bubble_bg.#.png" );
44 const std::string POPUP_BACKGROUND_EFFECT( DALI_IMAGE_DIR "popup_bubble_bg_ef.#.png" );
45 const std::string POPUP_BACKGROUND_LINE( DALI_IMAGE_DIR "popup_bubble_bg_line.#.png" );
46
47 const std::string POPUP_TAIL_BOTTOM( DALI_IMAGE_DIR "popup_bubble_tail_bottom.png" );
48 const std::string POPUP_TAIL_BOTTOM_EFFECT( DALI_IMAGE_DIR "popup_bubble_tail_bottom_ef.png" );
49 const std::string POPUP_TAIL_BOTTOM_LINE( DALI_IMAGE_DIR "popup_bubble_tail_bottom_line.png" );
50 const std::string POPUP_TAIL_TOP( DALI_IMAGE_DIR "popup_bubble_tail_top.png" );
51 const std::string POPUP_TAIL_TOP_EFFECT( DALI_IMAGE_DIR "popup_bubble_tail_top_ef.png" );
52 const std::string POPUP_TAIL_TOP_LINE( DALI_IMAGE_DIR "popup_bubble_tail_top_line.png" );
53
54 const std::string OPTION_ICON_CLIPBOARD( DALI_IMAGE_DIR "copy_paste_icon_clipboard.png" );
55 const std::string OPTION_ICON_COPY( DALI_IMAGE_DIR "copy_paste_icon_copy.png" );
56 const std::string OPTION_ICON_CUT( DALI_IMAGE_DIR "copy_paste_icon_cut.png" );
57 const std::string OPTION_ICON_PASTE( DALI_IMAGE_DIR "copy_paste_icon_paste.png" );
58 const std::string OPTION_ICON_SELECT( DALI_IMAGE_DIR "copy_paste_icon_select.png" );
59 const std::string OPTION_ICON_SELECT_ALL( DALI_IMAGE_DIR "copy_paste_icon_select_all.png" );
60
61 /**
62  * Constant values for building the GUI
63  */
64 const Vector4 POPUP_MARGIN( 14.0f, 14.0f, 14.0f, 14.0f );  ///< Margin around the popup visible background Image.
65 const Vector4 POPUP_BORDER( 2.0f, 2.0f, 2.0f, 2.0f );  ///< The Border of the popup.
66 const Vector2 POPUP_MIN_SIZE( 0.0f, 126.0f );  ///< The minimum size of the popup.
67 const Vector2 POPUP_MAX_SIZE( 720.0f, 126.0f );  ///< The maximum size of the popup.
68 const float POPUP_TAIL_Y_OFFSET( -2.25f );  ///< The y offset of the tail.
69 const Vector2 POPUP_TAIL_SIZE( 36.0f, 36.0f );  ///< The size of the tail.
70 const Vector2 POPUP_DIVIDER_SIZE( 1.0f, 126.0f );  ///< The size of the divider.
71
72 const Vector4 OPTION_PADDING( 16.0f, 16.0f, 24.0f, 19.0f );  ///< The padding within the option to position icon and text away from the border. The order is left, right, top and bottom
73 const Vector2 OPTION_MAX_SIZE( 220.0f, 126.0f );  ///< The maximum size of the option.
74 const Vector2 OPTION_MIN_SIZE( 128.0f, 126.0f );  ///< The minimum size of the option.
75 const Vector2 OPTION_ICON_SIZE( 45.0f, 45.0f );  ///< The size of the icon.
76 const Vector2 OPTION_TEXT_MIN_SIZE( 128.0f, 30.0f );  ///< The minimum size of the text.
77 const float OPTION_GAP_ICON_TEXT( 8.0f );  ///< The gap between the icon and the text
78
79 const float HIDE_POPUP_ANIMATION_DURATION( 0.2f );                            ///< Duration of popup hide animation in seconds.
80 const float SHOW_POPUP_ANIMATION_DURATION( 0.2f );                            ///< Duration of popup show animation in seconds.
81
82 /**
83  * Default Colors
84  */
85 const Vector4 DEFAULT_POPUP_BACKGROUND( Vector4( .20f, 0.29f, 0.44f, 1.0f ) );
86 const Vector4 DEFAULT_POPUP_BACKGROUND_PRESSED( Vector4( 0.07f, 0.10f, 0.17f, 1.0f ) );
87 const Vector4 DEFAULT_POPUP_LINE_COLOR( Vector4( 0.36f, 0.45f, 0.59f, 1.0f ) );
88 const Vector4 DEFAULT_OPTION_ICON( Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
89 const Vector4 DEFAULT_OPTION_ICON_PRESSED( Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
90 const Vector4 DEFAULT_OPTION_TEXT( Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
91 const Vector4 DEFAULT_OPTION_TEXT_PRESSED( Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
92
93 } // unnamed namespace
94
95 namespace Dali
96 {
97
98 namespace Toolkit
99 {
100
101 namespace Internal
102 {
103
104 const char* const TextInputPopup::SIGNAL_PRESSED = "pressed";
105 const char* const TextInputPopup::SIGNAL_HIDE_FINISHED = "hide-finished";
106 const char* const TextInputPopup::SIGNAL_SHOW_FINISHED = "show-finished";
107
108 const char* const TextInputPopup::OPTION_SELECT_WORD = "option-select_word";                       // "Select Word" popup option.
109 const char* const TextInputPopup::OPTION_SELECT_ALL("option-select_all");                          // "Select All" popup option.
110 const char* const TextInputPopup::OPTION_CUT("option-cut");                                        // "Cut" popup option.
111 const char* const TextInputPopup::OPTION_COPY("option-copy");                                      // "Copy" popup option.
112 const char* const TextInputPopup::OPTION_PASTE("option-paste");                                    // "Paste" popup option.
113 const char* const TextInputPopup::OPTION_CLIPBOARD("option-clipboard");                            // "Clipboard" popup option.
114
115 TextInputPopup::TextInputPopup()
116 : mState(StateHidden),
117   mRoot( Layer::New() ),
118   mButtons(),
119   mVisiblePopUpSize(),
120   mPopupTailXPosition( 0.0f ),
121   mContentSize(),
122   mBackgroundColor( DEFAULT_POPUP_BACKGROUND ),
123   mBackgroundPressedColor( DEFAULT_POPUP_BACKGROUND_PRESSED ),
124   mLineColor( DEFAULT_POPUP_LINE_COLOR ),
125   mIconColor( DEFAULT_OPTION_ICON ),
126   mIconPressedColor( DEFAULT_OPTION_ICON_PRESSED ),
127   mTextColor( DEFAULT_OPTION_TEXT ),
128   mTextPressedColor( DEFAULT_OPTION_TEXT_PRESSED ),
129   mSelectOptionPriority(1),
130   mSelectAllOptionPriority(2),
131   mCutOptionPriority(3),
132   mCopyOptionPriority(4),
133   mPasteOptionPriority(5),
134   mClipboardOptionPriority(6),
135   mPressedSignal(),
136   mHideFinishedSignal(),
137   mShowFinishedSignal()
138 {
139   mRoot.SetParentOrigin( ParentOrigin::TOP_LEFT );
140   mRoot.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
141 }
142
143 void TextInputPopup::AddToParent( Actor parent )
144 {
145   Actor existingParent = mRoot.GetParent();
146
147   if ( !existingParent )
148   {
149     parent.Add( mRoot );
150   }
151 }
152
153 void TextInputPopup::RemoveFromParent()
154 {
155   Actor parent = mRoot.GetParent();
156
157   if ( parent )
158   {
159     parent.Remove( mRoot );
160   }
161 }
162
163 void TextInputPopup::CreateLayer( const Vector2& size )
164 {
165   mLayer = Layer::New();
166   mLayer.SetParentOrigin(ParentOrigin::CENTER);
167   mLayer.SetAnchorPoint(AnchorPoint::CENTER);
168   mLayer.SetSize( size ); // matches stencil size
169   mLayer.SetName("popup-mLayer");
170 }
171
172 void TextInputPopup::CreateStencil( const Vector2& size )
173 {
174   mStencil = CreateSolidColorActor( Color::BLUE );
175   mStencil.SetParentOrigin( ParentOrigin::CENTER );
176   mStencil.SetAnchorPoint( AnchorPoint::CENTER );
177   mStencil.SetDrawMode( DrawMode::STENCIL );
178   mStencil.SetVisible( true );
179   mStencil.SetName( "popup-stencil" );
180   mStencil.SetSize( size );
181 }
182
183 void TextInputPopup::OnScrollStarted( const Vector3& position )
184 {
185   mButtons.SetSensitive( false );
186 }
187
188 void TextInputPopup::OnScrollCompleted( const Vector3& position )
189 {
190   mButtons.SetSensitive( true );
191 }
192
193 void TextInputPopup::CreateScrollView()
194 {
195   mScrollView = Toolkit::ScrollView::New();
196   mScrollView.SetName("popup-scroll-view");
197   mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
198   mScrollView.SetParentOrigin( ParentOrigin::CENTER );
199   mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
200   mScrollView.SetAxisAutoLock( true );
201   mScrollView.ScrollStartedSignal().Connect( this, &TextInputPopup::OnScrollStarted );
202   mScrollView.ScrollCompletedSignal().Connect( this, &TextInputPopup::OnScrollCompleted );
203 }
204
205 void TextInputPopup::UpdateScrollViewRulerAndSize( const Vector2& visibleSize )
206 {
207   mScrollView.SetSize( visibleSize.x, visibleSize.y );
208
209   RulerPtr rulerX = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
210   RulerPtr rulerY = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
211   rulerY->Disable();
212   rulerX->SetDomain( RulerDomain( 0, mContentSize.width, true ) );
213   mScrollView.SetRulerX(rulerX);
214   mScrollView.SetRulerY(rulerY);
215 }
216
217
218 void TextInputPopup::Clear()
219 {
220   if ( mBackground )
221   {
222     UnparentAndReset( mTail );
223     UnparentAndReset( mStencil );
224     UnparentAndReset( mBackground );
225     UnparentAndReset( mButtons );
226     UnparentAndReset( mScrollView );
227     mDividerContainer.clear();
228     RemoveFromParent();
229     mState = StateHidden;
230   }
231 }
232
233 ImageActor TextInputPopup::CreateOptionIcon( Image iconImage, const Vector4& color )
234 {
235   ImageActor icon = ImageActor::New( iconImage );
236   icon.SetSize( OPTION_ICON_SIZE );
237   icon.SetParentOrigin( ParentOrigin::TOP_CENTER );
238   icon.SetAnchorPoint( AnchorPoint::TOP_CENTER );
239   icon.SetColor( color );
240   icon.SetY( OPTION_PADDING.z - POPUP_BORDER.y );
241   return icon;
242 }
243
244 Toolkit::TextView TextInputPopup::CreateOptionCaption( const std::string& caption, const Vector4& color )
245 {
246   TextStyle style;
247   style.SetTextColor( color );
248
249   PointSize pointSize( Font::PixelsToPoints( OPTION_TEXT_MIN_SIZE.y ) );
250   style.SetFontPointSize( pointSize );
251
252   MarkupProcessor::StyledTextArray styledCaption;
253   styledCaption.push_back( MarkupProcessor::StyledText( Text( caption ), style ) );
254
255   Toolkit::TextView textView = Toolkit::TextView::New( styledCaption );
256   textView.SetSizePolicy( Toolkit::Control::Fixed, Toolkit::Control::Fixed );
257   textView.SetWidthExceedPolicy( Toolkit::TextView::EllipsizeEnd );
258   textView.SetHeightExceedPolicy( Toolkit::TextView::EllipsizeEnd );
259   textView.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
260   textView.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
261   textView.SetY( -OPTION_PADDING.w + POPUP_BORDER.w );
262
263   MarkupProcessor::StyledTextArray styledCaptionEllipsize;
264   MarkupProcessor::SetTextStyle( Text("..."), styledCaptionEllipsize, style );
265   textView.SetEllipsizeText( styledCaptionEllipsize );
266
267   const float textWidth = textView.GetWidthForHeight( OPTION_TEXT_MIN_SIZE.y );
268   textView.SetSize( textWidth, OPTION_TEXT_MIN_SIZE.y );
269
270   return textView;
271 }
272
273 void TextInputPopup::CreateBackground()
274 {
275   // Create background-panel if not already created (required if we have at least one option)
276   if ( !mBackground )
277   {
278     Image bgImg = Image::New( POPUP_BACKGROUND );
279     mBackground = ImageActor::New( bgImg );
280     mBackground.SetAnchorPoint( AnchorPoint::CENTER );
281     mBackground.SetParentOrigin( ParentOrigin::CENTER );
282     mBackground.SetName( "text-input-popup-background" );
283     mBackground.SetColor( mBackgroundColor );
284
285     Image bgEffectImg = Image::New( POPUP_BACKGROUND_EFFECT );
286     mBackgroundEffect = ImageActor::New( bgEffectImg );
287     mBackgroundEffect.SetAnchorPoint( AnchorPoint::CENTER );
288     mBackgroundEffect.SetParentOrigin( ParentOrigin::CENTER );
289     mBackgroundEffect.SetName( "text-input-popup-background-effect" );
290     mBackgroundEffect.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
291     mBackgroundEffect.SetZ( 1.0f );
292     mBackground.Add( mBackgroundEffect );
293
294     Image bgLine = Image::New( POPUP_BACKGROUND_LINE );
295     mBackgroundLine = ImageActor::New( bgLine );
296     mBackgroundLine.SetAnchorPoint( AnchorPoint::CENTER);
297     mBackgroundLine.SetParentOrigin( ParentOrigin::CENTER );
298     mBackgroundLine.SetName( "text-input-popup-background-effect" );
299     mBackgroundLine.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
300     mBackgroundLine.SetColor( mLineColor );
301     mBackgroundLine.SetZ( 0.1f );
302     mBackgroundEffect.Add( mBackgroundLine );
303
304     Hide(false);
305   }
306 }
307
308 void TextInputPopup::CreateTail()
309 {
310   if ( !mTail )
311   {
312     Image tail = Image::New( POPUP_TAIL_BOTTOM );
313     mTail = ImageActor::New( tail );
314     mTail.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
315     mTail.SetAnchorPoint( AnchorPoint::TOP_CENTER );
316     mTail.SetName( "text-input-popup-tail" );
317     mTail.SetPosition( 0.0f, POPUP_TAIL_Y_OFFSET - POPUP_BORDER.w, 1.2f );
318     mTail.SetColor( mBackgroundColor );
319
320     Image tailEffect = Image::New( POPUP_TAIL_BOTTOM_EFFECT );
321     mTailEffect = ImageActor::New( tailEffect );
322     mTailEffect.SetParentOrigin( ParentOrigin::CENTER );
323     mTailEffect.SetAnchorPoint( AnchorPoint::CENTER );
324     mTailEffect.SetName( "text-input-popup-tail-effect" );
325     mTailEffect.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
326     mTailEffect.SetZ( 0.1f );
327     mTail.Add( mTailEffect );
328
329     Image tailLine = Image::New( POPUP_TAIL_BOTTOM_LINE );
330     mTailLine = ImageActor::New( tailLine );
331     mTailLine.SetParentOrigin( ParentOrigin::CENTER );
332     mTailLine.SetAnchorPoint( AnchorPoint::CENTER );
333     mTailLine.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
334     mTailLine.SetName( "text-input-popup-tail-line" );
335     mTailLine.SetColor( mLineColor );
336     mTailLine.SetZ( 0.1f );
337     mTailEffect.Add( mTailLine );
338   }
339 }
340
341 ImageActor TextInputPopup::CreateDivider()
342 {
343   ImageActor divider = Toolkit::CreateSolidColorActor( mLineColor );
344   divider.SetParentOrigin( ParentOrigin::TOP_LEFT );
345   divider.SetAnchorPoint( AnchorPoint::TOP_LEFT );
346   divider.SetSize( POPUP_DIVIDER_SIZE.width , mContentSize.height );
347   divider.SetPosition( mContentSize.width - POPUP_DIVIDER_SIZE.width, 0.0f );
348
349   // Keep track of all the dividers. As their height's need to be updated to the max of all
350   // buttons currently added.
351   mDividerContainer.push_back( divider );
352
353   return divider;
354 }
355
356 ImageActor TextInputPopup::CreatePressedBackground( const Vector2& requiredSize )
357 {
358   ImageActor pressedBg = Toolkit::CreateSolidColorActor( mBackgroundPressedColor );
359   pressedBg.SetDrawMode( DrawMode::OVERLAY );
360   pressedBg.SetParentOrigin( ParentOrigin::CENTER );
361   pressedBg.SetAnchorPoint( AnchorPoint::CENTER );
362   pressedBg.SetSize( requiredSize );
363   return pressedBg;
364 }
365
366 TextInputPopup::ButtonRequirement TextInputPopup::CreateRequiredButton( TextInputPopup::Buttons buttonId, std::size_t orderOfPriority,
367                                                                         const std::string& name, const std::string& caption, Image iconImage, bool enabled )
368 {
369   TextInputPopup::ButtonRequirement currentButton;
370   currentButton.buttonId = buttonId;
371   currentButton.orderOfPriority = orderOfPriority;
372   currentButton.name = name;
373   currentButton.caption = caption;
374   currentButton.iconImage = iconImage;
375   currentButton.enabled = enabled;
376
377   return currentButton;
378 }
379
380 void TextInputPopup::CreateOrderedListOfOptions()
381 {
382   mOrderListOfButtons.clear();
383
384   for ( std::size_t index= 0; index < ButtonsEnumEnd; index++ )
385   {
386     TextInputPopup::ButtonRequirement currentButton;
387
388     // Create button for each possible option using Option priority
389     switch ( index )
390     {
391       case ButtonsCut:
392       {
393         Image cutIcon = Image::New( OPTION_ICON_CUT );
394         currentButton = CreateRequiredButton( ButtonsCut, mCutOptionPriority, OPTION_CUT, GET_LOCALE_TEXT("IDS_COM_BODY_CUT"), cutIcon, false );
395         break;
396       }
397       case ButtonsCopy:
398       {
399         Image copyIcon = Image::New( OPTION_ICON_COPY );
400         currentButton = CreateRequiredButton( ButtonsCopy, mCopyOptionPriority, OPTION_COPY, GET_LOCALE_TEXT("IDS_COM_BODY_COPY"), copyIcon, false );
401         break;
402       }
403       case ButtonsPaste:
404       {
405         Image pasteIcon = Image::New( OPTION_ICON_PASTE );
406         currentButton = CreateRequiredButton( ButtonsPaste, mPasteOptionPriority, OPTION_PASTE, GET_LOCALE_TEXT("IDS_COM_BODY_PASTE"), pasteIcon, false );
407         break;
408       }
409       case ButtonsSelect:
410       {
411         Image selectIcon = Image::New( OPTION_ICON_SELECT );
412         currentButton = CreateRequiredButton( ButtonsSelect, mSelectOptionPriority, OPTION_SELECT_WORD, GET_LOCALE_TEXT("IDS_COM_SK_SELECT"), selectIcon, false );
413         break;
414       }
415       case ButtonsSelectAll:
416       {
417         Image selectAllIcon = Image::New( OPTION_ICON_SELECT_ALL );
418         currentButton = CreateRequiredButton( ButtonsSelectAll, mSelectAllOptionPriority, OPTION_SELECT_ALL, GET_LOCALE_TEXT("IDS_COM_BODY_SELECT_ALL"), selectAllIcon, false );
419         break;
420       }
421       case ButtonsClipboard:
422       {
423         Image clipboardIcon = Image::New( OPTION_ICON_CLIPBOARD );
424         currentButton = CreateRequiredButton( ButtonsClipboard, mClipboardOptionPriority, OPTION_CLIPBOARD, GET_LOCALE_TEXT("IDS_COM_BODY_CLIPBOARD"), clipboardIcon, false );
425         break;
426       }
427     }
428
429     bool match = false;
430
431     // Insert button in list of buttons in order of priority setting.
432     for( std::vector<ButtonRequirement>::iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt && !match ); ++it )
433     {
434       const ButtonRequirement& button( *it );
435       if ( currentButton.orderOfPriority < button.orderOfPriority )
436       {
437         if ( currentButton.orderOfPriority != 0 ) // If order priority 0 then do not add button as not required.
438         {
439           mOrderListOfButtons.insert( it, currentButton );
440         }
441         match = true;
442       }
443     }
444
445     if ( !match )
446     {
447       mOrderListOfButtons.push_back( currentButton );
448     }
449   }
450 }
451
452 Vector2 TextInputPopup::GetConstrainedTextSize( const Vector2& textSize )
453 {
454   return  Vector2( std::min( textSize.width, OPTION_MAX_SIZE.width - OPTION_PADDING.x - OPTION_PADDING.y ), textSize.height );
455 }
456
457 void TextInputPopup::AddOption(const std::string& name, const std::string& caption, const Image iconImage, bool finalOption)
458 {
459   // 1. Create container for text and icon when not pressed.
460   Actor optionContainer = Actor::New();
461   optionContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
462   optionContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
463
464   // 2. Add text.
465   Toolkit::TextView captionTextView = CreateOptionCaption( caption, mTextColor );
466   optionContainer.Add( captionTextView );
467
468   // 3. Add icon.
469   ImageActor icon = CreateOptionIcon( iconImage, mIconColor );
470   optionContainer.Add( icon );
471
472   // 4. Calculate the size of option.
473   const Vector2 textSize = Vector2( captionTextView.GetNaturalSize() );
474   captionTextView.SetSize( GetConstrainedTextSize( textSize ) );
475
476
477   const Vector2 optionSize( std::max( textSize.x, OPTION_ICON_SIZE.x ) +  OPTION_PADDING.x + OPTION_PADDING.z,
478                             OPTION_PADDING.z + OPTION_ICON_SIZE.y + OPTION_GAP_ICON_TEXT + textSize.y + OPTION_MAX_SIZE.y );
479
480
481   Vector2 constrainedOptionSize = Min( Max( optionSize, OPTION_MIN_SIZE ), OPTION_MAX_SIZE );
482
483   constrainedOptionSize.height = constrainedOptionSize.height - POPUP_BORDER.y - POPUP_BORDER.z;
484
485   // 5. Create a option.
486   Toolkit::PushButton option = Toolkit::PushButton::New();
487   option.SetSizePolicy( Toolkit::Control::Fixed, Toolkit::Control::Fixed );
488   option.SetParentOrigin( ParentOrigin::TOP_LEFT );
489   option.SetAnchorPoint( AnchorPoint::TOP_LEFT );
490   option.SetSize( constrainedOptionSize );
491   option.SetX( mContentSize.x );
492   option.SetName( name );
493   option.SetAnimationTime( 0.0f );
494   option.ClickedSignal().Connect( this, &TextInputPopup::OnButtonPressed );
495   mButtons.Add( option );
496
497   // 6. Set the normal option image.
498   option.SetButtonImage( optionContainer );
499
500   // 7. Update the content size.
501   mContentSize.x += constrainedOptionSize.x;
502   mContentSize.y = std::max ( constrainedOptionSize.y, mContentSize.y );
503
504   // 8. Create the pressed container.
505   Actor optionPressedContainer = Actor::New();
506
507   // 9. Add option pressed background.
508   Vector2 optionPressedBackgroundSize( constrainedOptionSize.x - POPUP_BORDER.x, mContentSize.y - POPUP_BORDER.y - POPUP_BORDER.w );
509   ImageActor optionPressedBackground = CreatePressedBackground( optionPressedBackgroundSize );
510   optionPressedContainer.Add( optionPressedBackground );
511
512   // 10. Add pressed text
513   Toolkit::TextView pressedCaptionTextView = CreateOptionCaption( caption, mTextPressedColor );
514   pressedCaptionTextView.SetSize( GetConstrainedTextSize( Vector2( pressedCaptionTextView.GetNaturalSize() ) ) );
515   optionPressedBackground.Add( pressedCaptionTextView );
516
517   // 11. Add pressed icon
518   ImageActor pressedIcon = CreateOptionIcon( iconImage, mIconPressedColor );
519   optionPressedBackground.Add( pressedIcon );
520
521   // 12. Set the pressed option image
522   option.SetPressedImage( optionPressedContainer );
523
524   // 13. Add the divider
525   if ( !finalOption )
526   {
527     ImageActor divider = CreateDivider();
528     mButtons.Add( divider );
529   }
530 }
531
532 void TextInputPopup::Hide(bool animate)
533 {
534   if( mRoot )
535   {
536     if(mAnimation)
537     {
538       mAnimation.Clear();
539       mAnimation.Reset();
540     }
541
542     if(animate)
543     {
544       mAnimation = Animation::New( HIDE_POPUP_ANIMATION_DURATION );
545       mAnimation.AnimateTo( Property(mRoot, Actor::SCALE), Vector3::ZERO, AlphaFunctions::EaseOut );
546       mAnimation.AnimateTo( Property(mRoot, Actor::COLOR_ALPHA), 0.0f, AlphaFunctions::EaseOut );
547       mAnimation.Play();
548
549       mAnimation.FinishedSignal().Connect( this, &TextInputPopup::OnHideFinished );
550       mState = StateHiding;
551     }
552     else
553     {
554       mRoot.SetProperty(Actor::SCALE, Vector3::ZERO);
555       mRoot.SetProperty(Actor::COLOR_ALPHA, 0.0f);
556       mState = StateHidden;
557     }
558   }
559 }
560
561 void TextInputPopup::Show( Actor target, bool animate )
562 {
563   if( mRoot )
564   {
565     mRoot.SetSensitive( true );
566
567     if(mAnimation)
568     {
569       mAnimation.Clear();
570       mAnimation.Reset();
571     }
572
573     if ( target )
574     {
575       AddToParent( target );
576     }
577
578     if(animate)
579     {
580       mAnimation = Animation::New( SHOW_POPUP_ANIMATION_DURATION );
581       mAnimation.AnimateTo( Property(mRoot, Actor::SCALE), Vector3::ONE, AlphaFunctions::EaseOut );
582       mAnimation.AnimateTo( Property(mRoot, Actor::COLOR_ALPHA), 1.0f, AlphaFunctions::EaseOut );
583       mAnimation.Play();
584
585       mAnimation.FinishedSignal().Connect( this, &TextInputPopup::OnShowFinished );
586       mState = StateShowing;
587     }
588     else
589     {
590       mRoot.SetProperty(Actor::SCALE, Vector3::ONE);
591       mRoot.SetProperty(Actor::COLOR_ALPHA, 1.0f);
592       mState = StateShown;
593     }
594   }
595 }
596
597 TextInputPopup::State TextInputPopup::GetState(void) const
598 {
599   return mState;
600 }
601
602 Actor TextInputPopup::GetRootActor() const
603 {
604   return mRoot;
605 }
606
607 // Styling
608
609 void TextInputPopup::SetCutPastePopupColor( const Vector4& color )
610 {
611   mBackgroundColor = color;
612 }
613
614 const Vector4& TextInputPopup::GetCutPastePopupColor() const
615 {
616   return mBackgroundColor;
617 }
618
619 void TextInputPopup::SetCutPastePopupPressedColor( const Vector4& color )
620 {
621   mBackgroundPressedColor = color;
622 }
623
624 const Vector4& TextInputPopup::GetCutPastePopupPressedColor() const
625 {
626   return mBackgroundPressedColor;
627 }
628
629 void TextInputPopup::SetCutPastePopupBorderColor( const Vector4& color )
630 {
631   mLineColor = color;
632 }
633
634 const Vector4& TextInputPopup::GetCutPastePopupBorderColor() const
635 {
636   return mLineColor;
637 }
638
639 void TextInputPopup::SetCutPastePopupIconColor( const Vector4& color )
640 {
641   mIconColor = color;
642 }
643
644 const Vector4& TextInputPopup::GetCutPastePopupIconColor() const
645 {
646   return mIconColor;
647 }
648
649 void TextInputPopup::SetCutPastePopupIconPressedColor( const Vector4& color )
650 {
651   mIconPressedColor = color;
652 }
653
654 const Vector4& TextInputPopup::GetCutPastePopupIconPressedColor()
655 {
656   return mIconPressedColor;
657 }
658
659 void TextInputPopup::SetCutPastePopupTextColor( const Vector4& color )
660 {
661   mTextColor = color;
662 }
663
664 const Vector4& TextInputPopup::GetCutPastePopupTextColor()
665 {
666   return mTextColor;
667 }
668
669 void TextInputPopup::SetCutPastePopupTextPressedColor( const Vector4& color )
670 {
671   mTextPressedColor = color;
672 }
673
674 const Vector4& TextInputPopup::GetCutPastePopupTextPressedColor()
675 {
676   return mTextPressedColor;
677 }
678
679 void TextInputPopup::TogglePopupButtonOnOff( TextInputPopup::Buttons requiredButton, bool enable )
680 {
681   bool match ( false );
682   for( std::vector<ButtonRequirement>::iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt && !match ); ++it )
683    {
684      ButtonRequirement& button( *it );
685      if ( requiredButton == button.buttonId )
686      {
687        button.enabled = enable;
688        match = true;
689      }
690    }
691 }
692
693 void TextInputPopup::SetButtonPriorityPosition( TextInputPopup::Buttons button, unsigned int priority )
694 {
695   switch ( button )
696   {
697     case ButtonsCut:
698     {
699       mCutOptionPriority = priority;
700       break;
701     }
702     case ButtonsCopy:
703     {
704       mCopyOptionPriority = priority;
705       break;
706     }
707     case ButtonsPaste:
708     {
709       mPasteOptionPriority = priority;
710       break;
711     }
712     case ButtonsSelect:
713     {
714       mSelectOptionPriority = priority;
715       break;
716     }
717     case ButtonsSelectAll:
718     {
719       mSelectAllOptionPriority = priority;
720       break;
721     }
722     case ButtonsClipboard:
723     {
724       mClipboardOptionPriority = priority;
725       break;
726     }
727     case ButtonsEnumEnd:
728     {
729       DALI_ASSERT_DEBUG( "ButtonsEnumEnd used but an invalid choice");
730       break;
731     }
732   }
733   CreateOrderedListOfOptions(); // Update list of options as priority changed.
734 }
735
736 unsigned int TextInputPopup::GetButtonPriorityPosition( TextInputPopup::Buttons button ) const
737 {
738   unsigned int priority = 0;
739
740   switch ( button )
741   {
742     case ButtonsCut:
743     {
744       priority = mCutOptionPriority;
745       break;
746     }
747     case ButtonsCopy:
748     {
749       priority = mCopyOptionPriority;
750       break;
751     }
752     case ButtonsPaste:
753     {
754       priority = mPasteOptionPriority;
755       break;
756     }
757     case ButtonsSelect:
758     {
759       priority = mSelectOptionPriority;
760       break;
761     }
762     case ButtonsSelectAll:
763     {
764       priority = mSelectAllOptionPriority;
765       break;
766     }
767     case ButtonsClipboard:
768     {
769       priority = mClipboardOptionPriority;
770       break;
771     }
772     case ButtonsEnumEnd:
773     {
774       DALI_ASSERT_DEBUG( "ButtonsEnumEnd used but an invalid choice");
775       break;
776     }
777   }
778
779   return priority;
780 }
781
782 void TextInputPopup::AddPopupOptions()
783 {
784   mContentSize = Vector2( POPUP_MIN_SIZE.width, ( POPUP_BORDER.y + POPUP_BORDER.z ) );
785
786   // 1. Create the background.
787   CreateBackground();
788
789   // 2. Create the tail.
790   CreateTail();
791
792   // 3. Create the scroll view and Actor to hold buttons.
793   CreateScrollView();
794
795   // Clear previous buttons
796   if ( mButtons )
797   {
798     UnparentAndReset( mButtons );
799   }
800
801   mButtons = Actor::New();
802   mButtons.SetParentOrigin( ParentOrigin::CENTER );
803   mButtons.SetAnchorPoint( AnchorPoint::CENTER );
804
805   // 4. Create the options and add into the scroll view.
806   for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
807   {
808     const ButtonRequirement& button( *it );
809     if ( button.enabled )
810     {
811       AddOption( button.name, button.caption, button.iconImage, it != ( endIt - 1 ) ? false : true );
812     }
813   }
814
815   // 5. Calculate size of content and of popup including borders
816   const Vector2 popupSize = Vector2( std::min ( ( POPUP_BORDER.x + mContentSize.width + POPUP_BORDER.z ), POPUP_MAX_SIZE.width ) ,  POPUP_BORDER.y + mContentSize.height + POPUP_BORDER.w );
817
818   mVisiblePopUpSize = Vector3( popupSize.width - POPUP_BORDER.x - POPUP_BORDER.z , mContentSize.height, 1.0f);
819
820   mBackground.SetSize( popupSize.x + 28 - POPUP_BORDER.x - POPUP_BORDER.z, popupSize.y + 28 - POPUP_BORDER.y - POPUP_BORDER.w );
821   mButtons.SetSize( mVisiblePopUpSize.GetVectorXY() );
822
823   // 6. Set the scroll view ruler.
824   UpdateScrollViewRulerAndSize( mVisiblePopUpSize.GetVectorXY() );
825
826   // 7. Create stencil
827   const Vector2 stencilSize = Vector2( mVisiblePopUpSize.GetVectorXY() );
828
829   CreateLayer( stencilSize );
830   CreateStencil( stencilSize );
831
832   mScrollView.Add ( mButtons );
833   mLayer.Add( mScrollView);
834   mLayer.Add( mStencil);
835   mRoot.Add( mTail );
836   mRoot.Add( mBackground );
837   mRoot.Add( mLayer );
838
839   // 8. Set the root size.
840   mRoot.SetSize( popupSize );   // Make Root Actor reflect the size of its content
841 }
842
843 const Vector3& TextInputPopup::GetVisibileSize() const
844 {
845   return mVisiblePopUpSize;
846 }
847
848 void TextInputPopup::SetTailPosition( const Vector3& position, bool yAxisFlip )
849 {
850   mPopupTailXPosition = std::max(  position.x, POPUP_TAIL_SIZE.width*0.5f - mVisiblePopUpSize.width*0.5f + POPUP_BORDER.x );
851
852   std::min( mPopupTailXPosition, mVisiblePopUpSize.width*0.5f - POPUP_BORDER.x - POPUP_TAIL_SIZE.width*0.5f );
853
854   mTail.SetX( mPopupTailXPosition );
855
856   if ( yAxisFlip )
857   {
858     Image tail = Image::New( POPUP_TAIL_TOP );
859     Image tailEffect = Image::New( POPUP_TAIL_TOP_EFFECT );
860     Image tailLine = Image::New( POPUP_TAIL_TOP_LINE );
861
862     mTail.SetImage( tail );
863     mTailEffect.SetImage( tailEffect );
864     mTailLine.SetImage( tailLine );
865
866     mTail.SetParentOrigin( ParentOrigin::TOP_CENTER );
867     mTail.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
868     mTail.SetY( POPUP_BORDER.y - POPUP_TAIL_Y_OFFSET );
869   }
870 }
871
872 bool TextInputPopup::OnButtonPressed( Toolkit::Button button )
873 {
874   mPressedSignal.Emit( button );
875   return false;
876 }
877
878 void TextInputPopup::OnHideFinished(Animation& source)
879 {
880   source.FinishedSignal().Disconnect( this, &TextInputPopup::OnHideFinished );
881   Clear();
882   mState = StateHidden;
883   mHideFinishedSignal.Emit( *this );
884 }
885
886 void TextInputPopup::OnShowFinished(Animation& source)
887 {
888   source.FinishedSignal().Disconnect( this, &TextInputPopup::OnShowFinished );
889   mState = StateShown;
890   mShowFinishedSignal.Emit( *this );
891 }
892
893 TextInputPopup::PressedSignalV2& TextInputPopup::PressedSignal()
894 {
895   return mPressedSignal;
896 }
897
898 TextInputPopup::HideFinishedSignalV2& TextInputPopup::HideFinishedSignal()
899 {
900   return mHideFinishedSignal;
901 }
902
903 TextInputPopup::ShowFinishedSignalV2& TextInputPopup::ShowFinishedSignal()
904 {
905   return mShowFinishedSignal;
906 }
907
908 } // namespace Internal
909
910 } // namespace Toolkit
911
912 } // namespace Dali
913