Merge "Fixed bug when delete first row in the tableView" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-popup-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-selection-popup-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup-callback-interface.h>
23 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
24 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
25 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
26
27 // EXTERNAL INCLUDES
28 #include <dali/public-api/animation/animation.h>
29 #include <dali/public-api/images/nine-patch-image.h>
30 #include <dali/public-api/images/resource-image.h>
31 #include <dali/public-api/math/vector2.h>
32 #include <dali/public-api/math/vector4.h>
33 #include <dali/public-api/object/property-map.h>
34 #include <dali/devel-api/object/type-registry-helper.h>
35 #include <dali/integration-api/debug.h>
36
37 #include <libintl.h>
38 #include <cfloat>
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("sys_string", 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   {
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
392 void TextSelectionPopup::OnStageConnection( int depth )
393 {
394   DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::OnStageConnection\n" );
395   // Call the Control::OnStageConnection() to set the depth of the background.
396   Control::OnStageConnection( depth );
397
398   // TextSelectionToolbar::OnStageConnection() will set the depths of all the popup's components.
399 }
400
401 void TextSelectionPopup::HideAnimationFinished( Animation& animation )
402 {
403   Actor self = Self();
404   if ( !mPopupShowing ) // During the Hide/Fade animation there could be a call to Show the Popup again, mPopupShowing will be true in this case.
405   {
406     DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::HideAnimationFinished\n" );
407     UnparentAndReset( mToolbar );
408   }
409 }
410
411 bool TextSelectionPopup::OnCutButtonPressed( Toolkit::Button button )
412 {
413   if( mCallbackInterface )
414   {
415     mCallbackInterface->TextPopupButtonTouched( Toolkit::TextSelectionPopup::CUT );
416   }
417
418   return true;
419 }
420
421 bool TextSelectionPopup::OnCopyButtonPressed( Toolkit::Button button )
422 {
423   if( mCallbackInterface )
424   {
425     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::COPY );
426   }
427
428   return true;
429 }
430
431 bool TextSelectionPopup::OnPasteButtonPressed( Toolkit::Button button )
432 {
433   if( mCallbackInterface )
434   {
435     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::PASTE );
436   }
437
438   return true;
439 }
440
441 bool TextSelectionPopup::OnSelectButtonPressed( Toolkit::Button button )
442 {
443   if( mCallbackInterface )
444   {
445     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::SELECT );
446   }
447
448   return true;
449 }
450
451 bool TextSelectionPopup::OnSelectAllButtonPressed( Toolkit::Button button )
452 {
453   if( mCallbackInterface )
454   {
455     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::SELECT_ALL );
456   }
457
458   return true;
459 }
460
461 bool TextSelectionPopup::OnClipboardButtonPressed( Toolkit::Button button )
462 {
463   if( mCallbackInterface )
464   {
465     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::CLIPBOARD );
466   }
467
468   return true;
469 }
470
471 void TextSelectionPopup::SetDimensionToCustomise( const PopupCustomisations& settingToCustomise, const Size& dimension )
472 {
473   switch( settingToCustomise )
474   {
475     case POPUP_MAXIMUM_SIZE :
476     {
477       mPopupMaxSize = dimension;
478       if ( mToolbar )
479       {
480         mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, dimension );
481       }
482       break;
483     }
484     case OPTION_MAXIMUM_SIZE :
485     {
486       mOptionMaxSize = dimension;
487       // Option max size not currently currently supported
488       break;
489     }
490     case OPTION_MINIMUM_SIZE :
491     {
492       mOptionMinSize = dimension;
493       // Option min size not currently currently supported
494       break;
495     }
496     case OPTION_DIVIDER_SIZE :
497     {
498       mOptionDividerSize = dimension;
499       if ( mToolbar )
500       {
501         // Resize Dividers not currently supported
502       }
503       break;
504     }
505   } // switch
506 }
507
508 Size TextSelectionPopup::GetDimensionToCustomise( const PopupCustomisations& settingToCustomise )
509 {
510   switch( settingToCustomise )
511   {
512     case POPUP_MAXIMUM_SIZE :
513     {
514       if ( mToolbar )
515       {
516         return mToolbar.GetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE ).Get< Vector2 >();
517       }
518       else
519       {
520         return mPopupMaxSize;
521       }
522     }
523     case OPTION_MAXIMUM_SIZE :
524     {
525       return mOptionMaxSize;
526     }
527     case OPTION_MINIMUM_SIZE :
528     {
529       return mOptionMinSize;
530     }
531     case OPTION_DIVIDER_SIZE :
532     {
533       return mOptionDividerSize;
534     }
535   } // switch
536
537   return Size::ZERO;
538 }
539
540 void TextSelectionPopup::SetButtonImage( Toolkit::TextSelectionPopup::Buttons button, Dali::Image image )
541 {
542    switch ( button )
543    {
544    break;
545    case Toolkit::TextSelectionPopup::CLIPBOARD:
546    {
547      mClipboardIconImage  = image;
548    }
549    break;
550    case Toolkit::TextSelectionPopup::CUT :
551    {
552      mCutIconImage = image;
553    }
554    break;
555    case Toolkit::TextSelectionPopup::COPY :
556    {
557      mCopyIconImage = image;
558    }
559    break;
560    case Toolkit::TextSelectionPopup::PASTE :
561    {
562      mPasteIconImage = image;
563    }
564    break;
565    case Toolkit::TextSelectionPopup::SELECT :
566    {
567      mSelectIconImage = image;
568    }
569    break;
570    case Toolkit::TextSelectionPopup::SELECT_ALL :
571    {
572      mSelectAllIconImage = image;
573    }
574    break;
575    default :
576    {
577      DALI_ASSERT_DEBUG( "TextSelectionPopup SetPopupImage Unknown Button" );
578    }
579    } // switch
580 }
581
582 Dali::Image TextSelectionPopup::GetButtonImage( Toolkit::TextSelectionPopup::Buttons button )
583 {
584   switch ( button )
585   {
586   case Toolkit::TextSelectionPopup::CLIPBOARD :
587   {
588     return mClipboardIconImage;
589   }
590   break;
591   case Toolkit::TextSelectionPopup::CUT :
592   {
593     return mCutIconImage;
594   }
595   break;
596   case Toolkit::TextSelectionPopup::COPY :
597   {
598     return mCopyIconImage;
599   }
600   break;
601   case Toolkit::TextSelectionPopup::PASTE :
602   {
603     return mPasteIconImage;
604   }
605   break;
606   case Toolkit::TextSelectionPopup::SELECT :
607   {
608     return mSelectIconImage;
609   }
610   break;
611   case Toolkit::TextSelectionPopup::SELECT_ALL :
612   {
613     return mSelectAllIconImage;
614   }
615   break;
616   default :
617   {
618     DALI_ASSERT_DEBUG( "TextSelectionPopup GetPopupImage Unknown Button" );
619   }
620   } // switch
621
622   return Dali::Image();
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 , mCutIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::CUT)  ) );
642    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::COPY, mCopyOptionPriority, OPTION_COPY, POPUP_COPY_STRING, mCopyIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::COPY)  ) );
643    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::PASTE, mPasteOptionPriority, OPTION_PASTE, POPUP_PASTE_STRING, mPasteIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::PASTE)  ) );
644    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT, mSelectOptionPriority, OPTION_SELECT_WORD, POPUP_SELECT_STRING, mSelectIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT)  ) );
645    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT_ALL, mSelectAllOptionPriority, OPTION_SELECT_ALL, POPUP_SELECT_ALL_STRING, mSelectAllIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT_ALL)  ) );
646    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CLIPBOARD, mClipboardOptionPriority, OPTION_CLIPBOARD, POPUP_CLIPBOARD_STRING, mClipboardIconImage, ( 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.SetName( button.name );
659    option.SetAnimationTime( 0.0f );
660    option.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
661
662    switch( button.id )
663    {
664      case Toolkit::TextSelectionPopup::CUT:
665      {
666        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnCutButtonPressed );
667        break;
668      }
669      case Toolkit::TextSelectionPopup::COPY:
670      {
671        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnCopyButtonPressed );
672        break;
673      }
674      case Toolkit::TextSelectionPopup::PASTE:
675      {
676        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnPasteButtonPressed );
677        break;
678      }
679      case Toolkit::TextSelectionPopup::SELECT:
680      {
681        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnSelectButtonPressed );
682        break;
683      }
684      case Toolkit::TextSelectionPopup::SELECT_ALL:
685      {
686        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnSelectAllButtonPressed );
687        break;
688      }
689      case Toolkit::TextSelectionPopup::CLIPBOARD:
690      {
691        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnClipboardButtonPressed );
692        break;
693      }
694      case Toolkit::TextSelectionPopup::NONE:
695      {
696        // Nothing to do:
697        break;
698      }
699    }
700
701    // 2. Set the options contents.
702    if( showCaption )
703    {
704      // PushButton layout properties.
705      option.SetProperty( Toolkit::PushButton::Property::LABEL_PADDING, Vector4( 24.0f, 24.0f, 14.0f, 14.0f ) );
706
707      // Label properties.
708      Property::Map buttonLabelProperties;
709      buttonLabelProperties.Insert( "text", button.caption );
710      option.SetProperty( Toolkit::Button::Property::LABEL, buttonLabelProperties );
711    }
712    if( showIcons )
713    {
714      option.SetProperty( Toolkit::PushButton::Property::ICON_PADDING, Vector4( 10.0f, 10.0f, 10.0f, 10.0f ) );
715      option.SetProperty( Toolkit::PushButton::Property::ICON_ALIGNMENT, "TOP" );
716
717      // TODO: This is temporarily disabled until the text-selection-popup image API is changed to strings.
718      //option.SetProperty( Toolkit::PushButton::Property::SELECTED_ICON, button.icon );
719      //option.SetProperty( Toolkit::PushButton::Property::UNSELECTED_ICON, button.icon );
720    }
721
722    // 3. Set the normal option image (blank / Transparent).
723    option.SetUnselectedImage( "" );
724
725    // 4. Set the pressed option image.
726    // The image can be blank, the color can be used regardless.
727    option.SetSelectedImage( mPressedImage );
728    option.SetProperty( Toolkit::Button::Property::SELECTED_COLOR, mPressedColor );
729    option.SetProperty( Toolkit::Control::Property::STYLE_NAME, TEXT_SELECTION_POPUP_BUTTON_STYLE_NAME );
730
731    // 5 Add option to tool bar
732    mToolbar.AddOption( option );
733
734    // 6. Add the divider
735    if( showDivider )
736    {
737      const Size size( mOptionDividerSize.width, 0.0f ); // Height FILL_TO_PARENT
738
739      ImageActor divider = Toolkit::CreateSolidColorActor( Color::WHITE );
740 #ifdef DECORATOR_DEBUG
741      divider.SetName("Text's popup divider");
742 #endif
743      divider.SetSize( size );
744      divider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
745      divider.SetColor( mDividerColor );
746      divider.SetSortModifier( DECORATION_DEPTH_INDEX );
747      mToolbar.AddDivider( divider );
748    }
749  }
750
751  std::size_t TextSelectionPopup::GetNumberOfEnabledOptions()
752  {
753    std::size_t numberOfOptions = 0u;
754    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
755    {
756      const ButtonRequirement& button( *it );
757      if( button.enabled )
758      {
759        ++numberOfOptions;
760      }
761    }
762
763    return numberOfOptions;
764  }
765
766  void TextSelectionPopup::AddPopupOptionsToToolbar( bool showIcons, bool showCaptions )
767  {
768    DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::AddPopupOptionsToToolbar\n" );
769
770    CreateOrderedListOfPopupOptions();
771
772    mButtonsChanged = false;
773    UnparentAndReset( mToolbar);
774
775    if( !mToolbar )
776    {
777      Actor self = Self();
778      mToolbar = Toolkit::TextSelectionToolbar::New();
779      if ( mPopupMaxSize != Vector2::ZERO ) // If PopupMaxSize property set then apply to Toolbar. Toolbar currently is not retriving this from json
780      {
781        mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mPopupMaxSize );
782      }
783      mToolbar.SetParentOrigin( ParentOrigin::CENTER );
784 #ifdef DECORATOR_DEBUG
785      mToolbar.SetName("TextSelectionToolbar");
786 #endif
787      self.Add( mToolbar );
788    }
789
790    // Iterate list of buttons and add active ones to Toolbar
791    std::size_t numberOfOptionsRequired =  GetNumberOfEnabledOptions();
792    std::size_t numberOfOptionsAdded = 0u;
793    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
794    {
795      const ButtonRequirement& button( *it );
796      if ( button.enabled )
797      {
798        numberOfOptionsAdded++;
799        AddOption(  button, ( numberOfOptionsAdded < numberOfOptionsRequired ) , showIcons, showCaptions );
800      }
801    }
802  }
803
804 TextSelectionPopup::TextSelectionPopup( TextSelectionPopupCallbackInterface* callbackInterface )
805 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
806   mToolbar(),
807   mPopupMaxSize(),
808   mOptionMaxSize(),
809   mOptionMinSize(),
810   mOptionDividerSize(),
811   mEnabledButtons( Toolkit::TextSelectionPopup::NONE ),
812   mCallbackInterface( callbackInterface ),
813   mPressedColor( DEFAULT_OPTION_PRESSED_COLOR ),
814   mDividerColor( Color::WHITE ),
815   mIconColor( Color::WHITE ),
816   mSelectOptionPriority( 1 ),
817   mSelectAllOptionPriority ( 2 ),
818   mCutOptionPriority ( 4 ),
819   mCopyOptionPriority ( 3 ),
820   mPasteOptionPriority ( 5 ),
821   mClipboardOptionPriority( 6 ),
822   mFadeInDuration(0.0f),
823   mFadeOutDuration(0.0f),
824   mShowIcons( false ),
825   mShowCaptions( true ),
826   mPopupShowing( false ),
827   mButtonsChanged( false )
828 {
829 }
830
831 TextSelectionPopup::~TextSelectionPopup()
832 {
833 }
834
835
836 } // namespace Internal
837
838 } // namespace Toolkit
839
840 } // namespace Dali
841
842