[dali_1.0.38] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-popup-impl.cpp
1 /*
2  * Copyright (c) 2015 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 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-selection-popup-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
23 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
24 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
25
26 // EXTERNAL INCLUDES
27 #include <dali/public-api/images/nine-patch-image.h>
28 #include <dali/public-api/images/resource-image.h>
29 #include <dali/public-api/math/vector2.h>
30 #include <dali/public-api/math/vector4.h>
31 #include <libintl.h>
32
33 // todo Move this to adaptor??
34 #define GET_LOCALE_TEXT(string) dgettext("elementary", string)
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 namespace
46 {
47 const Dali::Vector4 DEFAULT_POPUP_BACKGROUND( Dali::Vector4( .20f, 0.29f, 0.44f, 1.0f ) );
48 const Dali::Vector4 DEFAULT_POPUP_BACKGROUND_PRESSED( Dali::Vector4( 0.07f, 0.10f, 0.17f, 1.0f ) );
49 const Dali::Vector4 DEFAULT_POPUP_LINE_COLOR( Dali::Vector4( 0.36f, 0.45f, 0.59f, 1.0f ) );
50 const Dali::Vector4 DEFAULT_OPTION_ICON( Dali::Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
51 const Dali::Vector4 DEFAULT_OPTION_ICON_PRESSED( Dali::Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
52 const Dali::Vector4 DEFAULT_OPTION_TEXT( Dali::Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
53 const Dali::Vector4 DEFAULT_OPTION_TEXT_PRESSED( Dali::Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
54
55 const std::string DEFAULT_POPUP_BACKGROUND_IMAGE( DALI_IMAGE_DIR "popup_bubble_bg.#.png" );
56 const std::string OPTION_ICON_CLIPBOARD( DALI_IMAGE_DIR "copy_paste_icon_clipboard.png" );
57 const std::string OPTION_ICON_COPY( DALI_IMAGE_DIR "copy_paste_icon_copy.png" );
58 const std::string OPTION_ICON_CUT( DALI_IMAGE_DIR "copy_paste_icon_cut.png" );
59 const std::string OPTION_ICON_PASTE( DALI_IMAGE_DIR "copy_paste_icon_paste.png" );
60 const std::string OPTION_ICON_SELECT( DALI_IMAGE_DIR "copy_paste_icon_select.png" );
61 const std::string OPTION_ICON_SELECT_ALL( DALI_IMAGE_DIR "copy_paste_icon_select_all.png" );
62
63 const Dali::Vector2 DEFAULT_POPUP_MAX_SIZE( 470.0f, 120.0f ); ///< The maximum size of the popup.
64
65 const float OPTION_TEXT_LINE_HEIGHT( 32.0f );     ///< The line height of the text.
66 const Dali::Vector2 OPTION_ICON_SIZE( 0.f, 0.f );       ///< The size of the icon.
67 const float OPTION_GAP_ICON_TEXT( 6.f );          ///< The gap between the icon and the text
68 const float OPTION_MARGIN_WIDTH( 10.f );          ///< The margin between the right or lefts edge and the text or icon.
69 const float OPTION_MAX_WIDTH( DEFAULT_POPUP_MAX_SIZE.width / 6 ); ///< The maximum width of the option (currently set to the max)
70 const float OPTION_MIN_WIDTH( 86.0f );           ///< The minimum width of the option.
71
72 const float POPUP_DIVIDER_WIDTH( 1.f );        ///< The size of the divider.
73
74 const Dali::Vector2 POPUP_TAIL_SIZE( 20.0f, 16.0f ); ///< The size of the tail.
75 const float POPUP_TAIL_Y_OFFSET( 5.f );        ///< The y offset of the tail (when its position is on the bottom).
76 const float POPUP_TAIL_TOP_Y_OFFSET( 3.f );    ///< The y offset of the tail (when its position is on the top).
77
78 const float HIDE_POPUP_ANIMATION_DURATION( 0.2f ); ///< Duration of popup hide animation in seconds.
79 const float SHOW_POPUP_ANIMATION_DURATION( 0.2f ); ///< Duration of popup show animation in seconds.
80
81 const char* const OPTION_SELECT_WORD = "option-select_word";                       // "Select Word" popup option.
82 const char* const OPTION_SELECT_ALL("option-select_all");                          // "Select All" popup option.
83 const char* const OPTION_CUT("option-cut");                                        // "Cut" popup option.
84 const char* const OPTION_COPY("option-copy");                                      // "Copy" popup option.
85 const char* const OPTION_PASTE("option-paste");                                    // "Paste" popup option.
86 const char* const OPTION_CLIPBOARD("option-clipboard");                            // "Clipboard" popup option.
87
88 } // namespace
89
90 //// Comparison function for ButtonRequirement Priority
91 //bool TextSelectionPopup::PriorityCompare( ButtonRequirement const& a, ButtonRequirement const& b )
92 //{
93 //  return a.priority < b.priority;
94 //}
95
96
97 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New()
98 {
99   // Create the implementation, temporarily owned by this handle on stack
100   IntrusivePtr< TextSelectionPopup > impl = new TextSelectionPopup();
101
102   // Pass ownership to CustomActor handle
103   Dali::Toolkit::TextSelectionPopup handle( *impl );
104
105   // Second-phase init of the implementation
106   // This can only be done after the CustomActor connection has been made...
107   impl->Initialize();
108
109   return handle;
110 }
111
112 void TextSelectionPopup::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
113 {
114   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
115
116   if( selectionPopup )
117   {
118     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
119
120     switch( index )
121     {
122       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
123       {
124        impl.SetPopupMaxSize( value.Get< Vector2 >() );
125        break;
126       }
127       case Toolkit::TextSelectionPopup::Property::POPUP_BACKGROUND_IMAGE:
128       {
129         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
130         impl.SetPopupImage( POPUP_BACKGROUND, image );
131         break;
132       }
133       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
134       {
135         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
136         impl.SetPopupImage( POPUP_CLIPBOARD_BUTTON, image );
137         break;
138       }
139       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
140       {
141         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
142         impl.SetPopupImage( POPUP_CUT_BUTTON_ICON, image );
143         break;
144       }
145       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
146       {
147         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
148         impl.SetPopupImage( POPUP_COPY_BUTTON_ICON, image );
149         break;
150       }
151       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
152       {
153         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
154         impl.SetPopupImage( POPUP_PASTE_BUTTON_ICON, image );
155         break;
156       }
157       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
158       {
159         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
160         impl.SetPopupImage( POPUP_SELECT_BUTTON_ICON, image );
161         break;
162       }
163       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
164       {
165         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
166         impl.SetPopupImage( POPUP_SELECT_ALL_BUTTON_ICON, image );
167         break;
168       }
169     } // switch
170   } // TextSelectionPopup
171 }
172
173 Property::Value TextSelectionPopup::GetProperty( BaseObject* object, Property::Index index )
174 {
175   Property::Value value;
176
177   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
178
179   if( selectionPopup )
180   {
181     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
182
183     switch( index )
184     {
185       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
186       {
187         value = impl.GetPopupMaxSize();
188         break;
189       }
190       case Toolkit::TextSelectionPopup::Property::POPUP_BACKGROUND_IMAGE:
191       {
192         ResourceImage image = ResourceImage::DownCast( impl.GetPopupImage( POPUP_BACKGROUND ) );
193         if( image )
194         {
195           value = image.GetUrl();
196         }
197         break;
198       }
199       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
200       {
201         ResourceImage image = ResourceImage::DownCast( impl.GetPopupImage( POPUP_CLIPBOARD_BUTTON ) );
202         if( image )
203         {
204           value = image.GetUrl();
205         }
206         break;
207       }
208       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
209       {
210         ResourceImage image = ResourceImage::DownCast( impl.GetPopupImage( POPUP_CUT_BUTTON_ICON ) );
211         if( image )
212         {
213           value = image.GetUrl();
214         }
215         break;
216       }
217       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
218       {
219         ResourceImage image = ResourceImage::DownCast( impl.GetPopupImage( POPUP_COPY_BUTTON_ICON ) );
220         if( image )
221         {
222           value = image.GetUrl();
223         }
224         break;
225       }
226       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
227       {
228         ResourceImage image = ResourceImage::DownCast( impl.GetPopupImage( POPUP_PASTE_BUTTON_ICON ) );
229         if( image )
230         {
231           value = image.GetUrl();
232         }
233         break;
234       }
235       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
236       {
237         ResourceImage image = ResourceImage::DownCast( impl.GetPopupImage( POPUP_SELECT_BUTTON_ICON ) );
238         if( image )
239         {
240           value = image.GetUrl();
241         }
242         break;
243       }
244       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
245       {
246         ResourceImage image = ResourceImage::DownCast( impl.GetPopupImage( POPUP_SELECT_ALL_BUTTON_ICON ) );
247         if( image )
248         {
249           value = image.GetUrl();
250         }
251         break;
252       }
253     } // switch
254   }
255   return value;
256 }
257
258 void TextSelectionPopup::OnInitialize()
259 {
260   CreatePopup();
261 }
262
263 void TextSelectionPopup::OnRelayout( const Vector2& size, RelayoutContainer& container )
264 {
265
266 }
267
268 void TextSelectionPopup::SetPopupMaxSize( const Size& maxSize )
269 {
270   mMaxSize = maxSize;
271 }
272
273 const Dali::Vector2& TextSelectionPopup::GetPopupMaxSize() const
274 {
275   return mMaxSize;
276 }
277
278 void TextSelectionPopup::SetPopupImage( PopupParts part, Dali::Image image )
279 {
280    switch ( part )
281    {
282    case POPUP_BACKGROUND :
283    {
284      mBackgroundImage = image;
285    }
286    break;
287    case POPUP_CLIPBOARD_BUTTON :
288    {
289      mClipboardIconImage  = image;
290    }
291    break;
292    case POPUP_CUT_BUTTON_ICON :
293    {
294      mCutIconImage = image;
295    }
296    break;
297    case POPUP_COPY_BUTTON_ICON :
298    {
299      mCopyIconImage = image;
300    }
301    break;
302    case POPUP_PASTE_BUTTON_ICON :
303    {
304      mPasteIconImage = image;
305    }
306    break;
307    case POPUP_SELECT_BUTTON_ICON :
308    {
309      mSelectIconImage = image;
310    }
311    break;
312    case POPUP_SELECT_ALL_BUTTON_ICON :
313    {
314      mSelectAllIconImage = image;
315    }
316    break;
317
318    } // switch
319 }
320
321 Dali::Image TextSelectionPopup::GetPopupImage( PopupParts part )
322 {
323   switch ( part )
324   {
325   case POPUP_BACKGROUND :
326   {
327     return mBackgroundImage;
328   }
329   break;
330   case POPUP_CLIPBOARD_BUTTON :
331   {
332     return mClipboardIconImage;
333   }
334   break;
335   case POPUP_CUT_BUTTON_ICON :
336   {
337     return mCutIconImage;
338   }
339   break;
340   case POPUP_COPY_BUTTON_ICON :
341   {
342     return mCopyIconImage;
343   }
344   break;
345   case POPUP_PASTE_BUTTON_ICON :
346   {
347     return mPasteIconImage;
348   }
349   break;
350   case POPUP_SELECT_BUTTON_ICON :
351   {
352     return mSelectIconImage;
353   }
354   break;
355   case POPUP_SELECT_ALL_BUTTON_ICON :
356   {
357     return mSelectAllIconImage;
358   }
359   break;
360   default :
361   {
362     DALI_ASSERT_DEBUG( "Unknown Popup Part" );
363   }
364   } // switch
365
366   return Dali::Image();
367 }
368
369  void TextSelectionPopup::CreateOrderedListOfPopupOptions()
370  {
371    mOrderListOfButtons.clear();
372
373    // Create button for each possible option using Option priority
374    if ( !mCutIconImage )
375    {
376      mCutIconImage = ResourceImage::New( OPTION_ICON_CUT );
377    }
378    mOrderListOfButtons.push_back( ButtonRequirement( ButtonsCut, mCutOptionPriority, OPTION_CUT, GET_LOCALE_TEXT("IDS_COM_BODY_CUT"), mCutIconImage, true ) );
379
380    if ( !mCopyIconImage )
381    {
382      mCopyIconImage = ResourceImage::New( OPTION_ICON_COPY );
383    }
384    mOrderListOfButtons.push_back( ButtonRequirement( ButtonsCopy, mCopyOptionPriority, OPTION_COPY, GET_LOCALE_TEXT("IDS_COM_BODY_COPY"), mCopyIconImage, true ) );
385
386    if ( !mPasteIconImage )
387    {
388      mPasteIconImage = ResourceImage::New( OPTION_ICON_PASTE );
389    }
390    mOrderListOfButtons.push_back( ButtonRequirement( ButtonsPaste, mPasteOptionPriority, OPTION_PASTE, GET_LOCALE_TEXT("IDS_COM_BODY_PASTE"), mPasteIconImage, true ) );
391
392    if ( !mSelectIconImage )
393    mSelectIconImage = ResourceImage::New( OPTION_ICON_SELECT );
394    mOrderListOfButtons.push_back( ButtonRequirement( ButtonsSelect, mSelectOptionPriority, OPTION_SELECT_WORD, GET_LOCALE_TEXT("IDS_COM_SK_SELECT"), mSelectIconImage, true ) );
395
396    if ( !mSelectAllIconImage )
397    {
398     mSelectAllIconImage = ResourceImage::New( OPTION_ICON_SELECT_ALL );
399    }
400    mOrderListOfButtons.push_back( ButtonRequirement( ButtonsSelectAll, mSelectAllOptionPriority, OPTION_SELECT_ALL, GET_LOCALE_TEXT("IDS_COM_BODY_SELECT_ALL"), mSelectAllIconImage, true ) );
401
402    if ( !mClipboardIconImage )
403    {
404      mClipboardIconImage = ResourceImage::New( OPTION_ICON_CLIPBOARD );
405    }
406    mOrderListOfButtons.push_back( ButtonRequirement( ButtonsClipboard, mClipboardOptionPriority, OPTION_CLIPBOARD, GET_LOCALE_TEXT("IDS_COM_BODY_CLIPBOARD"), mClipboardIconImage, true ) );
407
408    // Sort the buttons according their priorities.
409    std::sort( mOrderListOfButtons.begin(), mOrderListOfButtons.end(), TextSelectionPopup::ButtonPriorityCompare() );
410  }
411
412  void TextSelectionPopup::CreateBackground()
413  {
414    if ( !mBackgroundImage )
415    {
416      mBackgroundImage = ResourceImage::New( DEFAULT_POPUP_BACKGROUND_IMAGE );
417    }
418
419    NinePatchImage backgroundImageNinePatch = NinePatchImage::DownCast( mBackgroundImage );
420    if( backgroundImageNinePatch )
421    {
422      const Size ninePatchImageSize = Size( static_cast<float>( mBackgroundImage.GetWidth() ), static_cast<float>( mBackgroundImage.GetHeight() ) );
423      Rect<int> childRect = backgroundImageNinePatch.GetChildRectangle();
424
425      // -1u because of the cropping.
426      mNinePatchMargins.x = childRect.x - 1u;
427      mNinePatchMargins.y = ninePatchImageSize.width - ( childRect.x + childRect.width ) - 1u;
428      mNinePatchMargins.z = childRect.y - 1u;
429      mNinePatchMargins.w = ninePatchImageSize.height - ( childRect.y + childRect.height ) - 1u;
430    }
431
432    SetBackgroundImage( mBackgroundImage );
433    SetBackgroundColor( mBackgroundColor );
434  }
435
436  void TextSelectionPopup::AddOption( Actor& parent, const std::string& name, const std::string& caption, const Image iconImage, bool finalOption, bool showIcons )
437  {
438    // 1. Create the backgrounds for the popup option both normal and pressed.
439    // Both containers will be added to a button.
440    Actor optionContainer = Actor::New();
441    optionContainer.SetRelayoutEnabled( true );
442    optionContainer.SetDrawMode( DrawMode::OVERLAY );
443    optionContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
444
445    ImageActor optionPressedContainer = Toolkit::CreateSolidColorActor( mBackgroundPressedColor );
446    optionPressedContainer.SetDrawMode( DrawMode::OVERLAY );
447    optionPressedContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
448
449    // 2. Add text.
450    Toolkit::TextLabel captionTextLabel = Toolkit::TextLabel::New();
451    captionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
452    // optionContainer.Add( captionTextLabel ); Temporary removed.
453
454    Toolkit::TextLabel pressedCaptionTextLabel = Toolkit::TextLabel::New();
455    pressedCaptionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
456    // optionPressedContainer.Add( pressedCaptionTextLabel ); Temporary removed.
457
458    // Calculates the icon/text position.
459    float iconTextOffsetY = 0.0f;
460
461    if ( showIcons )
462    {
463      // 3. Create the icons
464      ImageActor pressedIcon = ImageActor::New(  iconImage );
465      ImageActor icon = ImageActor::New(  iconImage );
466
467      optionContainer.Add( icon );
468      optionPressedContainer.Add( pressedIcon );
469
470      iconTextOffsetY = 0.5f * ( ( DEFAULT_POPUP_MAX_SIZE.height - mNinePatchMargins.z - mNinePatchMargins.w ) - ( OPTION_ICON_SIZE.height + OPTION_GAP_ICON_TEXT + OPTION_TEXT_LINE_HEIGHT ) );
471
472      icon.SetParentOrigin( ParentOrigin::TOP_CENTER );
473      icon.SetAnchorPoint( AnchorPoint::TOP_CENTER );
474      icon.SetY( iconTextOffsetY );
475
476      pressedIcon.SetParentOrigin( ParentOrigin::TOP_CENTER );
477      pressedIcon.SetAnchorPoint( AnchorPoint::TOP_CENTER );
478      pressedIcon.SetY( iconTextOffsetY );
479
480      // Layout icon + gap + text
481      captionTextLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
482      pressedCaptionTextLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
483      pressedCaptionTextLabel.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
484      captionTextLabel.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
485      pressedCaptionTextLabel.SetY( -iconTextOffsetY );
486      captionTextLabel.SetY( -iconTextOffsetY );
487    }
488    else
489    {
490      // Centre option text
491      captionTextLabel.SetAnchorPoint( AnchorPoint::CENTER );
492      captionTextLabel.SetParentOrigin( ParentOrigin::CENTER );
493      pressedCaptionTextLabel.SetAnchorPoint( AnchorPoint::CENTER );
494      pressedCaptionTextLabel.SetParentOrigin( ParentOrigin::CENTER );
495    }
496
497    // Calculate the size of the text.
498    Vector3 textSize = captionTextLabel.GetNaturalSize();
499    textSize.width = std::min( textSize.width, OPTION_MAX_WIDTH - 2.f * OPTION_MARGIN_WIDTH );
500
501    // Set the size to the text. Text will be ellipsized if exceeds the max width.
502    captionTextLabel.SetSize( textSize.GetVectorXY() );
503    pressedCaptionTextLabel.SetSize( textSize.GetVectorXY() );
504
505    // 4. Calculate the size of option.
506
507    // The width is the max size of the text or the icon plus the margins clamped between the option min and max size.
508    // The height is the whole popup height minus the ninepatch margins.
509    const Vector2 optionSize( std::min( OPTION_MAX_WIDTH, std::max( OPTION_MIN_WIDTH, std::max( textSize.width, OPTION_ICON_SIZE.width ) + 2.f * OPTION_MARGIN_WIDTH ) ),
510                              DEFAULT_POPUP_MAX_SIZE.height - mNinePatchMargins.z - mNinePatchMargins.w );
511
512    optionContainer.SetSize( optionSize );
513    optionPressedContainer.SetSize( optionSize );
514
515    // 5. Create a option.
516    Toolkit::PushButton option = Toolkit::PushButton::New();
517    option.SetSize( optionSize );
518    option.SetAnchorPoint( AnchorPoint::TOP_LEFT );
519    option.SetX( mContentSize.width );
520    option.SetName( name );
521    option.SetAnimationTime( 0.0f );
522    //option.ClickedSignal().Connect( this, &TextInputPopup::OnButtonPressed );
523
524    parent.Add( option );
525
526    // 6. Set the normal option image.
527    option.SetButtonImage( optionContainer );
528
529    // 7. Set the pressed option image
530    option.SetSelectedImage( optionPressedContainer );
531
532    // 8. Update the content size.
533    mContentSize.width += optionSize.width;
534    mContentSize.height = std::max ( optionSize.height, mContentSize.height );
535
536    // 9. Add the divider
537    if( !finalOption )
538    {
539      const Size size( POPUP_DIVIDER_WIDTH, mContentSize.height );
540
541      ImageActor divider =  Toolkit::CreateSolidColorActor( Color::WHITE );
542      divider.SetSize( size );
543      divider.SetParentOrigin( ParentOrigin::TOP_LEFT );
544      divider.SetAnchorPoint( AnchorPoint::TOP_LEFT );
545      divider.SetPosition( mContentSize.width - POPUP_DIVIDER_WIDTH, 0.0f );
546      parent.Add( divider );
547    }
548  }
549
550  void TextSelectionPopup::SetUpPopup( Size& size )
551  {
552    Actor self = Self();
553
554    // Create Layer and Stencil.
555    mStencilLayer = Layer::New();
556    ImageActor stencil = CreateSolidColorActor( Color::RED );
557    stencil.SetDrawMode( DrawMode::STENCIL );
558    stencil.SetVisible( true );
559    Actor scrollview = Actor::New(); //todo make a scrollview
560    stencil.SetRelayoutEnabled( true );
561
562    self.SetSize( mRequiredPopUpSize ); // control matches stencil size
563
564    mStencilLayer.SetSize( size ); // matches stencil size
565
566    stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
567    scrollview.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
568    mButtons.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
569
570    mStencilLayer.SetAnchorPoint(AnchorPoint::TOP_LEFT);
571    scrollview.SetAnchorPoint(AnchorPoint::TOP_LEFT);
572    mButtons.SetAnchorPoint( AnchorPoint::TOP_LEFT );
573
574    mStencilLayer.SetPosition( mNinePatchMargins.x,  mNinePatchMargins.y );
575
576    self.Add( mStencilLayer );
577    mStencilLayer.Add( stencil );
578    mStencilLayer.Add( scrollview );
579    scrollview.Add( mButtons );
580  }
581
582  void TextSelectionPopup::AddPopupOptions( bool createTail, bool showIcons )
583  {
584    mShowIcons = showIcons;
585
586    mContentSize = Vector2::ZERO;
587
588    mButtons = Actor::New();
589    mButtons.SetRelayoutEnabled( true );
590
591    // Add the options into the buttons container.
592
593    // 1. Determine how many buttons are active and should be added to container.
594    std::size_t numberOfOptions = 0u;
595    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
596    {
597      const ButtonRequirement& button( *it );
598      if( button.enabled )
599      {
600        ++numberOfOptions;
601      }
602    }
603
604    // 2. Iterate list of buttons and add active ones.
605    std::size_t optionsAdded = 0u;
606    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
607    {
608      const ButtonRequirement& button( *it );
609      if ( button.enabled )
610      {
611        ++optionsAdded;
612        AddOption( mButtons, button.name, button.caption, button.icon, optionsAdded == numberOfOptions, mShowIcons );
613      }
614    }
615
616    // Calculate the size of the whole popup which may not be all visible.
617    mRequiredPopUpSize = Size( std::min( mMaxSize.width, mContentSize.width + mNinePatchMargins.x + mNinePatchMargins.y ), DEFAULT_POPUP_MAX_SIZE.height );
618
619    // Size of the contents within the popup
620    mVisiblePopUpSize = Size( mRequiredPopUpSize.width - mNinePatchMargins.x - mNinePatchMargins.y, mRequiredPopUpSize.height - mNinePatchMargins.z - mNinePatchMargins.w );
621  }
622
623  void TextSelectionPopup::CreatePopup()
624  {
625    if ( !mStencilLayer )
626    {
627      CreateOrderedListOfPopupOptions();  //todo Currently causes all options to be shown
628      CreateBackground();
629      AddPopupOptions( true, true );
630      SetUpPopup( mVisiblePopUpSize );
631    }
632
633    mStencilLayer.RaiseToTop();
634  }
635
636 TextSelectionPopup::TextSelectionPopup()
637 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
638   mMaxSize ( DEFAULT_POPUP_MAX_SIZE ),
639   mVisiblePopUpSize( DEFAULT_POPUP_MAX_SIZE ),
640   mRequiredPopUpSize( DEFAULT_POPUP_MAX_SIZE ),
641   mBackgroundColor( DEFAULT_POPUP_BACKGROUND ),
642   mBackgroundPressedColor( DEFAULT_POPUP_BACKGROUND_PRESSED ),
643   mLineColor( DEFAULT_POPUP_LINE_COLOR ),
644   mIconColor( DEFAULT_OPTION_ICON ),
645   mIconPressedColor( DEFAULT_OPTION_ICON_PRESSED ),
646   mTextColor( DEFAULT_OPTION_TEXT ),
647   mTextPressedColor( DEFAULT_OPTION_TEXT_PRESSED ),
648   mSelectOptionPriority( 1 ),
649   mSelectAllOptionPriority ( 2 ),
650   mCutOptionPriority ( 3 ),
651   mCopyOptionPriority ( 4 ),
652   mPasteOptionPriority ( 5 ),
653   mClipboardOptionPriority( 6 ),
654   mShowIcons( true )
655 {
656 }
657
658 TextSelectionPopup::~TextSelectionPopup()
659 {
660 }
661
662
663 } // namespace Internal
664
665 } // namespace Toolkit
666
667 } // namespace Dali