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