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