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