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