TextSelectionPopup buttons can be enabled from TextController
[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/default-controls/solid-color-actor.h>
23 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
24
25 // EXTERNAL INCLUDES
26 #include <dali/public-api/images/nine-patch-image.h>
27 #include <dali/public-api/images/resource-image.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/public-api/math/vector4.h>
30 #include <dali/devel-api/object/type-registry-helper.h>
31 #include <libintl.h>
32 #include <cfloat>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45 // todo Move this to adaptor??
46 #define GET_LOCALE_TEXT(string) dgettext("elementary", string)
47
48 const Dali::Vector4 DEFAULT_POPUP_LINE_COLOR( Dali::Vector4( 0.69f, 0.93f, 0.93f, 1.0f ) );
49 const Dali::Vector4 DEFAULT_OPTION_ICON( Dali::Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
50 const Dali::Vector4 DEFAULT_OPTION_ICON_PRESSED( Dali::Vector4( 0.5f, 1.0f, 1.0f, 1.0f ) );
51
52 const std::string DEFAULT_POPUP_BACKGROUND_IMAGE( DALI_IMAGE_DIR "selection-popup-bg#.png" );
53 const std::string OPTION_ICON_CLIPBOARD( DALI_IMAGE_DIR "copy_paste_icon_clipboard.png" );
54 const std::string OPTION_ICON_COPY( DALI_IMAGE_DIR "copy_paste_icon_copy.png" );
55 const std::string OPTION_ICON_CUT( DALI_IMAGE_DIR "copy_paste_icon_cut.png" );
56 const std::string OPTION_ICON_PASTE( DALI_IMAGE_DIR "copy_paste_icon_paste.png" );
57 const std::string OPTION_ICON_SELECT( DALI_IMAGE_DIR "copy_paste_icon_select.png" );
58 const std::string OPTION_ICON_SELECT_ALL( DALI_IMAGE_DIR "copy_paste_icon_select_all.png" );
59
60 const float OPTION_MARGIN_WIDTH( 10.f );          ///< The margin between the right or lefts edge and the text or icon.
61
62 #ifdef DGETTEXT_ENABLED
63
64 #define POPUP_CUT_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CUT")
65 #define POPUP_COPY_STRING GET_LOCALE_TEXT("IDS_COM_BODY_COPY")
66 #define POPUP_PASTE_STRING GET_LOCALE_TEXT("IDS_COM_BODY_PASTE")
67 #define POPUP_SELECT_STRING GET_LOCALE_TEXT("IDS_COM_SK_SELECT")
68 #define POPUP_SELECT_ALL_STRING GET_LOCALE_TEXT("IDS_COM_BODY_SELECT_ALL")
69 #define POPUP_CLIPBOARD_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CLIPBOARD")
70
71 #else
72
73 #define POPUP_CUT_STRING  "Cut"
74 #define POPUP_COPY_STRING  "Copy"
75 #define POPUP_PASTE_STRING  "Paste"
76 #define POPUP_SELECT_STRING  "Select"
77 #define POPUP_SELECT_ALL_STRING  "Select All"
78 #define POPUP_CLIPBOARD_STRING  "Clipboard"
79
80 #endif
81
82 const char* const OPTION_SELECT_WORD = "option-select_word";                       // "Select Word" popup option.
83 const char* const OPTION_SELECT_ALL("option-select_all");                          // "Select All" popup option.
84 const char* const OPTION_CUT("option-cut");                                        // "Cut" popup option.
85 const char* const OPTION_COPY("option-copy");                                      // "Copy" popup option.
86 const char* const OPTION_PASTE("option-paste");                                    // "Paste" popup option.
87 const char* const OPTION_CLIPBOARD("option-clipboard");                            // "Clipboard" popup option.
88
89 BaseHandle Create()
90 {
91   return Toolkit::TextSelectionPopup::New();
92 }
93
94 // Setup properties, signals and actions using the type-registry.
95
96 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionPopup, Toolkit::Control, Create );
97
98 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-max-size", VECTOR2,   POPUP_MAX_SIZE )
99 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-min-size", VECTOR2,   POPUP_MIN_SIZE )
100 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "option-max-size", VECTOR2,   OPTION_MAX_SIZE )
101 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "option-min-size", VECTOR2,   OPTION_MIN_SIZE )
102 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "option-divider-size", VECTOR2,   OPTION_DIVIDER_SIZE )
103 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-clipboard-button-image", STRING, POPUP_CLIPBOARD_BUTTON_ICON_IMAGE )
104 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-cut-button-image", STRING, POPUP_CUT_BUTTON_ICON_IMAGE )
105 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-copy-button-image", STRING, POPUP_COPY_BUTTON_ICON_IMAGE )
106 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-paste-button-image", STRING, POPUP_PASTE_BUTTON_ICON_IMAGE )
107 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-select-button-image", STRING, POPUP_SELECT_BUTTON_ICON_IMAGE )
108 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popup-select-all-button-image", STRING, POPUP_SELECT_ALL_BUTTON_ICON_IMAGE )
109
110 DALI_TYPE_REGISTRATION_END()
111
112 } // namespace
113
114 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New()
115 {
116   // Create the implementation, temporarily owned by this handle on stack
117   IntrusivePtr< TextSelectionPopup > impl = new TextSelectionPopup();
118
119   // Pass ownership to CustomActor handle
120   Dali::Toolkit::TextSelectionPopup handle( *impl );
121
122   // Second-phase init of the implementation
123   // This can only be done after the CustomActor connection has been made...
124   impl->Initialize();
125
126   return handle;
127 }
128
129 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New( Toolkit::TextSelectionPopup::Buttons buttonsToEnable )
130 {
131    // Create the implementation, temporarily owned by this handle on stack
132   IntrusivePtr< TextSelectionPopup > impl = new TextSelectionPopup();
133
134   // Pass ownership to CustomActor handle
135   Dali::Toolkit::TextSelectionPopup handle( *impl );
136
137   impl->mEnabledButtons = buttonsToEnable;
138
139   // Second-phase init of the implementation
140   // This can only be done after the CustomActor connection has been made...
141   impl->Initialize();
142
143   return handle;
144 }
145 void TextSelectionPopup::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
146 {
147   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
148
149   if( selectionPopup )
150   {
151     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
152
153     switch( index )
154     {
155       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
156       {
157        impl.SetDimensionToCustomise( POPUP_MAXIMUM_SIZE, value.Get< Vector2 >() );
158        break;
159       }
160       case Toolkit::TextSelectionPopup::Property::POPUP_MIN_SIZE:
161       {
162         impl.SetDimensionToCustomise( POPUP_MINIMUM_SIZE, value.Get< Vector2 >() );
163         break;
164       }
165       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
166       {
167         impl.SetDimensionToCustomise( OPTION_MAXIMUM_SIZE, value.Get< Vector2 >() );
168         break;
169       }
170       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
171       {
172         impl.SetDimensionToCustomise( OPTION_MINIMUM_SIZE, value.Get< Vector2>() );
173         break;
174       }
175       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
176       {
177         impl.SetDimensionToCustomise( OPTION_DIVIDER_SIZE, value.Get< Vector2>() );
178         break;
179       }
180       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
181       {
182         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
183         impl.SetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD, image );
184         break;
185       }
186       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
187       {
188         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
189         impl.SetButtonImage( Toolkit::TextSelectionPopup::CUT, image );
190         break;
191       }
192       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
193       {
194         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
195         impl.SetButtonImage( Toolkit::TextSelectionPopup::COPY, image );
196         break;
197       }
198       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
199       {
200         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
201         impl.SetButtonImage( Toolkit::TextSelectionPopup::PASTE, image );
202         break;
203       }
204       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
205       {
206         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
207         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT, image );
208         break;
209       }
210       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
211       {
212         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
213         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL, image );
214         break;
215       }
216     } // switch
217   } // TextSelectionPopup
218 }
219
220 Property::Value TextSelectionPopup::GetProperty( BaseObject* object, Property::Index index )
221 {
222   Property::Value value;
223
224   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
225
226   if( selectionPopup )
227   {
228     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
229
230     switch( index )
231     {
232       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
233       {
234         value = impl.GetDimensionToCustomise( POPUP_MAXIMUM_SIZE );
235         break;
236       }
237       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
238       {
239         value = impl.GetDimensionToCustomise( OPTION_MAXIMUM_SIZE );
240         break;
241       }
242       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
243       {
244         value = impl.GetDimensionToCustomise( OPTION_MINIMUM_SIZE );
245         break;
246       }
247       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
248       {
249         value = impl.GetDimensionToCustomise( OPTION_DIVIDER_SIZE );
250         break;
251       }
252       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
253       {
254         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD ) );
255         if( image )
256         {
257           value = image.GetUrl();
258         }
259         break;
260       }
261       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
262       {
263         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CUT ) );
264         if( image )
265         {
266           value = image.GetUrl();
267         }
268         break;
269       }
270       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
271       {
272         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::COPY ) );
273         if( image )
274         {
275           value = image.GetUrl();
276         }
277         break;
278       }
279       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
280       {
281         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::PASTE ) );
282         if( image )
283         {
284           value = image.GetUrl();
285         }
286         break;
287       }
288       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
289       {
290         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT ) );
291         if( image )
292         {
293           value = image.GetUrl();
294         }
295         break;
296       }
297       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
298       {
299         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL ) );
300         if( image )
301         {
302           value = image.GetUrl();
303         }
304         break;
305       }
306     } // switch
307   }
308   return value;
309 }
310
311 void TextSelectionPopup::OnInitialize()
312 {
313   CreatePopup();
314 }
315
316 bool TextSelectionPopup::OnButtonPressed( Toolkit::Button button )
317 {
318   return true;
319 }
320
321 void TextSelectionPopup::SetDimensionToCustomise( const PopupCustomisations& settingToCustomise, const Size& dimension )
322 {
323   switch( settingToCustomise )
324   {
325     case POPUP_MAXIMUM_SIZE :
326     {
327       Actor self = Self();
328       mMaxSize = dimension;
329       if ( mToolbar )
330       {
331         mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mMaxSize );
332       }
333       break;
334     }
335     case POPUP_MINIMUM_SIZE :
336     {
337       Actor self = Self();
338       mMinSize = dimension;
339       // Option can not be smaller than this if only one.
340       break;
341     }
342     case OPTION_MAXIMUM_SIZE :
343     {
344       mOptionMaxSize = dimension;
345       // Option max size not currently currently supported
346
347       break;
348     }
349     case OPTION_MINIMUM_SIZE :
350     {
351       mOptionMinSize = dimension;
352       // Option min size not currently currently supported
353       break;
354     }
355     case OPTION_DIVIDER_SIZE :
356     {
357       mOptionDividerSize = dimension;
358       if ( mToolbar )
359       {
360         // Resize Dividers not currently supported
361       }
362       break;
363     }
364   } // switch
365 }
366
367 Size TextSelectionPopup::GetDimensionToCustomise( const PopupCustomisations& settingToCustomise )
368 {
369   switch( settingToCustomise )
370   {
371     case POPUP_MAXIMUM_SIZE :
372     {
373       return mMaxSize;
374     }
375     case POPUP_MINIMUM_SIZE :
376     {
377       return mMinSize;
378     }
379     case OPTION_MAXIMUM_SIZE :
380     {
381       return mOptionMaxSize;
382     }
383     case OPTION_MINIMUM_SIZE :
384     {
385       return mOptionMinSize;
386     }
387     case OPTION_DIVIDER_SIZE :
388     {
389       return mOptionDividerSize;
390     }
391   } // switch
392
393   return Size::ZERO;
394 }
395
396 void TextSelectionPopup::SetButtonImage( Toolkit::TextSelectionPopup::Buttons button, Dali::Image image )
397 {
398    switch ( button )
399    {
400    break;
401    case Toolkit::TextSelectionPopup::CLIPBOARD:
402    {
403      mClipboardIconImage  = image;
404    }
405    break;
406    case Toolkit::TextSelectionPopup::CUT :
407    {
408      mCutIconImage = image;
409    }
410    break;
411    case Toolkit::TextSelectionPopup::COPY :
412    {
413      mCopyIconImage = image;
414    }
415    break;
416    case Toolkit::TextSelectionPopup::PASTE :
417    {
418      mPasteIconImage = image;
419    }
420    break;
421    case Toolkit::TextSelectionPopup::SELECT :
422    {
423      mSelectIconImage = image;
424    }
425    break;
426    case Toolkit::TextSelectionPopup::SELECT_ALL :
427    {
428      mSelectAllIconImage = image;
429    }
430    break;
431    default :
432    {
433      DALI_ASSERT_DEBUG( "TextSelectionPopup SetPopupImage Unknown Button" );
434    }
435    } // switch
436 }
437
438 Dali::Image TextSelectionPopup::GetButtonImage( Toolkit::TextSelectionPopup::Buttons button )
439 {
440   switch ( button )
441   {
442   case Toolkit::TextSelectionPopup::CLIPBOARD :
443   {
444     return mClipboardIconImage;
445   }
446   break;
447   case Toolkit::TextSelectionPopup::CUT :
448   {
449     return mCutIconImage;
450   }
451   break;
452   case Toolkit::TextSelectionPopup::COPY :
453   {
454     return mCopyIconImage;
455   }
456   break;
457   case Toolkit::TextSelectionPopup::PASTE :
458   {
459     return mPasteIconImage;
460   }
461   break;
462   case Toolkit::TextSelectionPopup::SELECT :
463   {
464     return mSelectIconImage;
465   }
466   break;
467   case Toolkit::TextSelectionPopup::SELECT_ALL :
468   {
469     return mSelectAllIconImage;
470   }
471   break;
472   default :
473   {
474     DALI_ASSERT_DEBUG( "TextSelectionPopup GetPopupImage Unknown Button" );
475   }
476   } // switch
477
478   return Dali::Image();
479 }
480
481  void TextSelectionPopup::CreateOrderedListOfPopupOptions()
482  {
483    mOrderListOfButtons.clear();
484
485    // Create button for each possible option using Option priority
486    if ( !mCutIconImage )
487    {
488      mCutIconImage = ResourceImage::New( OPTION_ICON_CUT );
489    }
490    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CUT, mCutOptionPriority, OPTION_CUT, POPUP_CUT_STRING , mCutIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::CUT)  ) );
491
492    if ( !mCopyIconImage )
493    {
494      mCopyIconImage = ResourceImage::New( OPTION_ICON_COPY );
495    }
496    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::COPY, mCopyOptionPriority, OPTION_COPY, POPUP_COPY_STRING, mCopyIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::COPY)  ) );
497
498    if ( !mPasteIconImage )
499    {
500      mPasteIconImage = ResourceImage::New( OPTION_ICON_PASTE );
501    }
502    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::PASTE, mPasteOptionPriority, OPTION_PASTE, POPUP_PASTE_STRING, mPasteIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::PASTE)  ) );
503
504    if ( !mSelectIconImage )
505    mSelectIconImage = ResourceImage::New( OPTION_ICON_SELECT );
506    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT, mSelectOptionPriority, OPTION_SELECT_WORD, POPUP_SELECT_STRING, mSelectIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT)  ) );
507
508    if ( !mSelectAllIconImage )
509    {
510     mSelectAllIconImage = ResourceImage::New( OPTION_ICON_SELECT_ALL );
511    }
512    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT_ALL, mSelectAllOptionPriority, OPTION_SELECT_ALL, POPUP_SELECT_ALL_STRING, mSelectAllIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT_ALL)  ) );
513
514    if ( !mClipboardIconImage )
515    {
516      mClipboardIconImage = ResourceImage::New( OPTION_ICON_CLIPBOARD );
517    }
518    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CLIPBOARD, mClipboardOptionPriority, OPTION_CLIPBOARD, POPUP_CLIPBOARD_STRING, mClipboardIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::CLIPBOARD)  ) );
519
520    // Sort the buttons according their priorities.
521    std::sort( mOrderListOfButtons.begin(), mOrderListOfButtons.end(), TextSelectionPopup::ButtonPriorityCompare() );
522  }
523
524  void TextSelectionPopup::AddOption( const std::string& name, const std::string& caption, const Image iconImage, bool showDivider, bool showIcons, bool showCaption  )
525  {
526    // 1. Create the backgrounds for the popup option both normal and pressed.
527    // Both containers will be added to a button.
528
529    Toolkit::TableView optionContainer = Toolkit::TableView::New( (showIcons&showCaption)?2:1 , 1 );
530    optionContainer.SetDrawMode( DrawMode::OVERLAY );
531    optionContainer.SetFitHeight( 0 );
532    optionContainer.SetFitWidth( 0 );
533
534    Toolkit::TableView  optionPressedContainer = Toolkit::TableView::New( (showIcons&showCaption)?2:1 , 1 );
535    optionPressedContainer.SetDrawMode( DrawMode::OVERLAY );
536    optionPressedContainer.SetFitHeight( 0 );
537    optionPressedContainer.SetFitWidth( 0 );
538    optionPressedContainer.SetBackgroundColor(Color::RED); //todo member variable
539
540 #ifdef DECORATOR_DEBUG
541    optionContainer.SetName("optionContainer");
542    optionPressedContainer.SetName("optionPressedContainer");
543 #endif
544    // 2. Add text.
545
546    if ( showCaption )
547    {
548      Toolkit::TextLabel captionTextLabel = Toolkit::TextLabel::New();
549      captionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
550      captionTextLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
551
552      Toolkit::TextLabel pressedCaptionTextLabel = Toolkit::TextLabel::New();
553      pressedCaptionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
554      pressedCaptionTextLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
555
556      Padding padding;
557      padding.left = 24.0f;
558      padding.right = 24.0f;
559      padding.top = 13.0f;
560      padding.bottom = 14.0f;
561      captionTextLabel.SetPadding( padding );
562      pressedCaptionTextLabel.SetPadding( padding );
563
564      optionContainer.AddChild( captionTextLabel, Toolkit::TableView::CellPosition(( showIcons&showCaption)?1:0, 0 )  );
565      optionPressedContainer.AddChild( pressedCaptionTextLabel, Toolkit::TableView::CellPosition(( showIcons&showCaption)?1:0, 0 ) );
566    }
567
568    // 3. Create the icons
569    if ( showIcons )
570    {
571      ImageActor pressedIcon = ImageActor::New(  iconImage );
572      ImageActor icon = ImageActor::New(  iconImage );
573
574      icon.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
575      pressedIcon.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
576      icon.SetColor( mIconColor );
577      pressedIcon.SetColor( mIconPressedColor );
578
579      if ( showCaption & showIcons )
580      {
581        optionContainer.SetFitHeight( 1 );
582        optionContainer.SetFitWidth( 1 );
583        optionPressedContainer.SetFitHeight( 1 );
584        optionPressedContainer.SetFitWidth( 1 );
585      }
586
587      optionContainer.AddChild( icon, Toolkit::TableView::CellPosition( 0, 0 )  );
588      optionPressedContainer.AddChild( pressedIcon, Toolkit::TableView::CellPosition( 0, 0 )  );
589
590      icon.SetPadding( Padding( 10.0f, 10.0f, 10.0f, 10.0f ) );
591      pressedIcon.SetPadding( Padding( 10.0f, 10.0f, 10.0f, 10.0f ) );
592    }
593
594    // 4. Create a option.
595    Toolkit::PushButton option = Toolkit::PushButton::New();
596    option.SetName( name );
597    option.SetAnimationTime( 0.0f );
598    option.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
599    option.ClickedSignal().Connect( this, &TextSelectionPopup::OnButtonPressed );
600
601    // 5. Set the normal option image.
602    option.SetButtonImage( optionContainer );
603
604    // 6. Set the pressed option image
605    option.SetSelectedImage( optionPressedContainer );
606
607    // 7 Add option to tool bar
608    mToolbar.AddOption( option );
609
610    // 8. Add the divider
611    if( showDivider )
612    {
613      const Size size( mOptionDividerSize.width, 0.0f ); // Height FILL_TO_PARENT
614
615      ImageActor divider = Toolkit::CreateSolidColorActor( Color::WHITE );
616      divider.SetSize( size );
617      divider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
618      divider.SetColor( mLineColor );
619      mToolbar.AddDivider( divider );
620    }
621  }
622
623  std::size_t TextSelectionPopup::GetNumberOfEnabledOptions()
624  {
625    std::size_t numberOfOptions = 0u;
626    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
627    {
628      const ButtonRequirement& button( *it );
629      if( button.enabled )
630      {
631        ++numberOfOptions;
632      }
633    }
634
635    return numberOfOptions;
636  }
637
638  void TextSelectionPopup::AddPopupOptionsToToolbar( bool showIcons, bool showCaptions )
639  {
640    // Iterate list of buttons and add active ones to Toolbar
641    std::size_t numberOfOptionsRequired =  GetNumberOfEnabledOptions();
642    std::size_t numberOfOptionsAdded = 0u;
643    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
644    {
645      const ButtonRequirement& button( *it );
646      if ( button.enabled )
647      {
648        numberOfOptionsAdded++;
649        AddOption(  button.name, button.caption, button.icon, ( numberOfOptionsAdded < numberOfOptionsRequired ) , showIcons, showCaptions );
650      }
651    }
652  }
653
654  void TextSelectionPopup::CreatePopup()
655  {
656    Actor self = Self();
657    CreateOrderedListOfPopupOptions();  //todo Currently causes all options to be shown
658    self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
659    SetBackgroundImage( NinePatchImage::New( DEFAULT_POPUP_BACKGROUND_IMAGE ) );
660
661    if ( !mToolbar )
662    {
663      mToolbar = Toolkit::TextSelectionToolbar::New();
664      mToolbar.SetParentOrigin( ParentOrigin::CENTER );
665      mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mMaxSize );
666      self.Add( mToolbar );
667      AddPopupOptionsToToolbar( mShowIcons, mShowCaptions );
668    }
669  }
670
671 TextSelectionPopup::TextSelectionPopup()
672 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
673   mToolbar(),
674   mMaxSize(),
675   mMinSize(),
676   mOptionDividerSize( Size( 2.0f, 0.0f) ),
677   mEnabledButtons( Toolkit::TextSelectionPopup::NONE ),
678   mLineColor( DEFAULT_POPUP_LINE_COLOR ),
679   mIconColor( DEFAULT_OPTION_ICON ),
680   mIconPressedColor( DEFAULT_OPTION_ICON_PRESSED ),
681   mSelectOptionPriority( 1 ),
682   mSelectAllOptionPriority ( 2 ),
683   mCutOptionPriority ( 3 ),
684   mCopyOptionPriority ( 4 ),
685   mPasteOptionPriority ( 5 ),
686   mClipboardOptionPriority( 6 ),
687   mShowIcons( false ),
688   mShowCaptions( true )
689 {
690 }
691
692 TextSelectionPopup::~TextSelectionPopup()
693 {
694 }
695
696
697 } // namespace Internal
698
699 } // namespace Toolkit
700
701 } // namespace Dali
702
703