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