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