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