c19b9207d7422153d0364499664930da6ce3f075
[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 // EXTERNAL INCLUDES
22 #include <libintl.h>
23 #include <cfloat>
24 #include <dali/public-api/animation/animation.h>
25 #include <dali/public-api/images/nine-patch-image.h>
26 #include <dali/public-api/images/resource-image.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/math/vector4.h>
29 #include <dali/public-api/object/property-map.h>
30 #include <dali/devel-api/object/type-registry-helper.h>
31 #include <dali/integration-api/debug.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
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("sys_string", 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 BaseHandle Create()
88 {
89   return Toolkit::TextSelectionPopup::New( NULL );
90 }
91
92 // Setup properties, signals and actions using the type-registry.
93
94 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionPopup, Toolkit::Control, Create );
95
96 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupMaxSize", VECTOR2,   POPUP_MAX_SIZE )
97 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupMinSize", VECTOR2,   POPUP_MIN_SIZE )
98 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionMaxSize", VECTOR2,   OPTION_MAX_SIZE )
99 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionMinSize", VECTOR2,   OPTION_MIN_SIZE )
100 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionDividerSize", VECTOR2,   OPTION_DIVIDER_SIZE )
101 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupClipboardButtonImage", STRING, POPUP_CLIPBOARD_BUTTON_ICON_IMAGE )
102 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupCutButtonImage", STRING, POPUP_CUT_BUTTON_ICON_IMAGE )
103 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupCopyButtonImage", STRING, POPUP_COPY_BUTTON_ICON_IMAGE )
104 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPasteButtonImage", STRING, POPUP_PASTE_BUTTON_ICON_IMAGE )
105 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupSelectButtonImage", STRING, POPUP_SELECT_BUTTON_ICON_IMAGE )
106 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupSelectAllButtonImage", STRING, POPUP_SELECT_ALL_BUTTON_ICON_IMAGE )
107 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupDividerColor", VECTOR4, POPUP_DIVIDER_COLOR )
108 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupIconColor", VECTOR4, POPUP_ICON_COLOR )
109 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPressedColor", VECTOR4, POPUP_PRESSED_COLOR )
110 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPressedImage", STRING, POPUP_PRESSED_IMAGE )
111 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupFadeInDuration", FLOAT, POPUP_FADE_IN_DURATION )
112 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupFadeOutDuration", FLOAT, POPUP_FADE_OUT_DURATION )
113
114 DALI_TYPE_REGISTRATION_END()
115
116 } // namespace
117
118
119 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New( TextSelectionPopupCallbackInterface* callbackInterface )
120 {
121   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextSelectionPopup::New\n" );
122
123    // Create the implementation, temporarily owned by this handle on stack
124   IntrusivePtr< TextSelectionPopup > impl = new TextSelectionPopup( callbackInterface );
125
126   // Pass ownership to CustomActor handle
127   Dali::Toolkit::TextSelectionPopup handle( *impl );
128
129   // Second-phase init of the implementation
130   // This can only be done after the CustomActor connection has been made...
131   impl->Initialize();
132
133   return handle;
134 }
135
136 void TextSelectionPopup::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
137 {
138   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
139
140   if( selectionPopup )
141   {
142     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
143
144     switch( index )
145     {
146       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
147       {
148        impl.SetDimensionToCustomise( POPUP_MAXIMUM_SIZE, value.Get< Vector2 >() );
149        break;
150       }
151       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
152       {
153         impl.SetDimensionToCustomise( OPTION_MAXIMUM_SIZE, value.Get< Vector2 >() );
154         break;
155       }
156       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
157       {
158         impl.SetDimensionToCustomise( OPTION_MINIMUM_SIZE, value.Get< Vector2>() );
159         break;
160       }
161       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
162       {
163         impl.SetDimensionToCustomise( OPTION_DIVIDER_SIZE, value.Get< Vector2>() );
164         break;
165       }
166       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
167       {
168         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
169         impl.SetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD, image );
170         break;
171       }
172       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
173       {
174         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
175         impl.SetButtonImage( Toolkit::TextSelectionPopup::CUT, image );
176         break;
177       }
178       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
179       {
180         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
181         impl.SetButtonImage( Toolkit::TextSelectionPopup::COPY, image );
182         break;
183       }
184       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
185       {
186         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
187         impl.SetButtonImage( Toolkit::TextSelectionPopup::PASTE, image );
188         break;
189       }
190       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
191       {
192         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
193         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT, image );
194         break;
195       }
196       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
197       {
198         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
199         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL, image );
200         break;
201       }
202       case Toolkit::TextSelectionPopup::Property::POPUP_DIVIDER_COLOR:
203       {
204         impl.mDividerColor = value.Get< Vector4 >();
205         break;
206       }
207       case Toolkit::TextSelectionPopup::Property::POPUP_ICON_COLOR:
208       {
209         impl.mIconColor = value.Get< Vector4 >();
210         break;
211       }
212       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_COLOR:
213       {
214         impl.mPressedColor = value.Get< Vector4 >();
215         break;
216       }
217       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_IMAGE:
218       {
219         impl.SetPressedImage( value.Get< std::string >() );
220         break;
221       }
222       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_IN_DURATION:
223       {
224         impl.mFadeInDuration = value.Get < float >();
225         break;
226       }
227       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_OUT_DURATION:
228       {
229         impl.mFadeOutDuration = value.Get < float >();
230         break;
231       }
232     } // switch
233   } // TextSelectionPopup
234 }
235
236 Property::Value TextSelectionPopup::GetProperty( BaseObject* object, Property::Index index )
237 {
238   Property::Value value;
239
240   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
241
242   if( selectionPopup )
243   {
244     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
245
246     switch( index )
247     {
248       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
249       {
250         value = impl.GetDimensionToCustomise( POPUP_MAXIMUM_SIZE );
251         break;
252       }
253       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
254       {
255         value = impl.GetDimensionToCustomise( OPTION_MAXIMUM_SIZE );
256         break;
257       }
258       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
259       {
260         value = impl.GetDimensionToCustomise( OPTION_MINIMUM_SIZE );
261         break;
262       }
263       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
264       {
265         value = impl.GetDimensionToCustomise( OPTION_DIVIDER_SIZE );
266         break;
267       }
268       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
269       {
270         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD ) );
271         if( image )
272         {
273           value = image.GetUrl();
274         }
275         break;
276       }
277       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
278       {
279         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CUT ) );
280         if( image )
281         {
282           value = image.GetUrl();
283         }
284         break;
285       }
286       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
287       {
288         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::COPY ) );
289         if( image )
290         {
291           value = image.GetUrl();
292         }
293         break;
294       }
295       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
296       {
297         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::PASTE ) );
298         if( image )
299         {
300           value = image.GetUrl();
301         }
302         break;
303       }
304       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
305       {
306         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT ) );
307         if( image )
308         {
309           value = image.GetUrl();
310         }
311         break;
312       }
313       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
314       {
315         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL ) );
316         if( image )
317         {
318           value = image.GetUrl();
319         }
320         break;
321       }
322       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_IMAGE:
323       {
324         value = impl.GetPressedImage();
325         break;
326       }
327       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_IN_DURATION:
328       {
329         value = impl.mFadeInDuration;
330         break;
331       }
332       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_OUT_DURATION:
333       {
334         value = impl.mFadeOutDuration;
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( Layer target )
349 {
350   if( mToolbar )
351   {
352     mToolbar.RaiseAbove( target );
353   }
354 }
355
356 void TextSelectionPopup::ShowPopup()
357 {
358   if ( !mPopupShowing || mButtonsChanged )
359   {
360     Actor self = Self();
361     AddPopupOptionsToToolbar( mShowIcons, mShowCaptions );
362
363     Animation animation = Animation::New( mFadeInDuration );
364     animation.AnimateTo( Property(self, Actor::Property::COLOR_ALPHA), 1.0f  );
365     animation.Play();
366     mPopupShowing = true;
367   }
368 }
369
370 void TextSelectionPopup::HidePopup()
371 {
372   if ( mPopupShowing )
373   {
374     mPopupShowing = false;
375     Actor self = Self();
376     Animation animation = Animation::New( mFadeOutDuration );
377     animation.AnimateTo( Property(self, Actor::Property::COLOR_ALPHA), 0.0f  );
378     animation.FinishedSignal().Connect( this, &TextSelectionPopup::HideAnimationFinished );
379     animation.Play();
380   }
381 }
382
383 void TextSelectionPopup::OnInitialize()
384 {
385   DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::OnInitialize\n" );
386   Actor self = Self();
387   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
388   self.SetProperty( Actor::Property::COLOR_ALPHA, 0.0f );
389 }
390
391 void TextSelectionPopup::OnStageConnection( int depth )
392 {
393   DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::OnStageConnection\n" );
394   // Call the Control::OnStageConnection() to set the depth of the background.
395   Control::OnStageConnection( depth );
396
397   // TextSelectionToolbar::OnStageConnection() will set the depths of all the popup's components.
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 )
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, Dali::Image image )
540 {
541    switch ( button )
542    {
543    break;
544    case Toolkit::TextSelectionPopup::CLIPBOARD:
545    {
546      mClipboardIconImage  = image;
547    }
548    break;
549    case Toolkit::TextSelectionPopup::CUT :
550    {
551      mCutIconImage = image;
552    }
553    break;
554    case Toolkit::TextSelectionPopup::COPY :
555    {
556      mCopyIconImage = image;
557    }
558    break;
559    case Toolkit::TextSelectionPopup::PASTE :
560    {
561      mPasteIconImage = image;
562    }
563    break;
564    case Toolkit::TextSelectionPopup::SELECT :
565    {
566      mSelectIconImage = image;
567    }
568    break;
569    case Toolkit::TextSelectionPopup::SELECT_ALL :
570    {
571      mSelectAllIconImage = image;
572    }
573    break;
574    default :
575    {
576      DALI_ASSERT_DEBUG( "TextSelectionPopup SetPopupImage Unknown Button" );
577    }
578    } // switch
579 }
580
581 Dali::Image TextSelectionPopup::GetButtonImage( Toolkit::TextSelectionPopup::Buttons button )
582 {
583   switch ( button )
584   {
585   case Toolkit::TextSelectionPopup::CLIPBOARD :
586   {
587     return mClipboardIconImage;
588   }
589   break;
590   case Toolkit::TextSelectionPopup::CUT :
591   {
592     return mCutIconImage;
593   }
594   break;
595   case Toolkit::TextSelectionPopup::COPY :
596   {
597     return mCopyIconImage;
598   }
599   break;
600   case Toolkit::TextSelectionPopup::PASTE :
601   {
602     return mPasteIconImage;
603   }
604   break;
605   case Toolkit::TextSelectionPopup::SELECT :
606   {
607     return mSelectIconImage;
608   }
609   break;
610   case Toolkit::TextSelectionPopup::SELECT_ALL :
611   {
612     return mSelectAllIconImage;
613   }
614   break;
615   default :
616   {
617     DALI_ASSERT_DEBUG( "TextSelectionPopup GetPopupImage Unknown Button" );
618   }
619   } // switch
620
621   return Dali::Image();
622 }
623
624 void TextSelectionPopup::SetPressedImage( const std::string& filename )
625 {
626   mPressedImage = filename;
627 }
628
629 std::string TextSelectionPopup::GetPressedImage() const
630 {
631   return mPressedImage;
632 }
633
634  void TextSelectionPopup::CreateOrderedListOfPopupOptions()
635  {
636    mOrderListOfButtons.clear();
637    mOrderListOfButtons.reserve( 8u );
638
639    // Create button for each possible option using Option priority
640    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CUT, mCutOptionPriority, OPTION_CUT, POPUP_CUT_STRING , mCutIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::CUT)  ) );
641    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::COPY, mCopyOptionPriority, OPTION_COPY, POPUP_COPY_STRING, mCopyIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::COPY)  ) );
642    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::PASTE, mPasteOptionPriority, OPTION_PASTE, POPUP_PASTE_STRING, mPasteIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::PASTE)  ) );
643    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT, mSelectOptionPriority, OPTION_SELECT_WORD, POPUP_SELECT_STRING, mSelectIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT)  ) );
644    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT_ALL, mSelectAllOptionPriority, OPTION_SELECT_ALL, POPUP_SELECT_ALL_STRING, mSelectAllIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT_ALL)  ) );
645    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CLIPBOARD, mClipboardOptionPriority, OPTION_CLIPBOARD, POPUP_CLIPBOARD_STRING, mClipboardIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::CLIPBOARD)  ) );
646
647    // Sort the buttons according their priorities.
648    std::sort( mOrderListOfButtons.begin(), mOrderListOfButtons.end(), TextSelectionPopup::ButtonPriorityCompare() );
649  }
650
651  void TextSelectionPopup::AddOption( const ButtonRequirement& button, bool showDivider, bool showIcons, bool showCaption  )
652  {
653    // 1. Create a option.
654    DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::AddOption\n" );
655
656    Toolkit::PushButton option = Toolkit::PushButton::New();
657    option.SetName( button.name );
658    option.SetAnimationTime( 0.0f );
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( "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::PushButton::Property::ICON_ALIGNMENT, "TOP" );
715
716      // TODO: This is temporarily disabled until the text-selection-popup image API is changed to strings.
717      //option.SetProperty( Toolkit::PushButton::Property::SELECTED_ICON, button.icon );
718      //option.SetProperty( Toolkit::PushButton::Property::UNSELECTED_ICON, button.icon );
719    }
720
721    // 3. Set the normal option image (blank / Transparent).
722    option.SetUnselectedImage( "" );
723
724    // 4. Set the pressed option image.
725    // The image can be blank, the color can be used regardless.
726    option.SetSelectedImage( mPressedImage );
727    option.SetProperty( Toolkit::Button::Property::SELECTED_COLOR, mPressedColor );
728    option.SetProperty( Toolkit::Control::Property::STYLE_NAME, TEXT_SELECTION_POPUP_BUTTON_STYLE_NAME );
729
730    // 5 Add option to tool bar
731    mToolbar.AddOption( option );
732
733    // 6. Add the divider
734    if( showDivider )
735    {
736      const Size size( mOptionDividerSize.width, 0.0f ); // Height FILL_TO_PARENT
737
738      Toolkit::Control divider = Toolkit::Control::New();
739 #ifdef DECORATOR_DEBUG
740      divider.SetName("Text's popup divider");
741 #endif
742      divider.SetSize( size );
743      divider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
744      divider.SetBackgroundColor( mDividerColor  );
745      mToolbar.AddDivider( divider );
746    }
747  }
748
749  std::size_t TextSelectionPopup::GetNumberOfEnabledOptions()
750  {
751    std::size_t numberOfOptions = 0u;
752    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
753    {
754      const ButtonRequirement& button( *it );
755      if( button.enabled )
756      {
757        ++numberOfOptions;
758      }
759    }
760
761    return numberOfOptions;
762  }
763
764  void TextSelectionPopup::AddPopupOptionsToToolbar( bool showIcons, bool showCaptions )
765  {
766    DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::AddPopupOptionsToToolbar\n" );
767
768    CreateOrderedListOfPopupOptions();
769
770    mButtonsChanged = false;
771    UnparentAndReset( mToolbar);
772
773    if( !mToolbar )
774    {
775      Actor self = Self();
776      mToolbar = Toolkit::TextSelectionToolbar::New();
777      if ( mPopupMaxSize != Vector2::ZERO ) // If PopupMaxSize property set then apply to Toolbar. Toolbar currently is not retriving this from json
778      {
779        mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mPopupMaxSize );
780      }
781      mToolbar.SetParentOrigin( ParentOrigin::CENTER );
782 #ifdef DECORATOR_DEBUG
783      mToolbar.SetName("TextSelectionToolbar");
784 #endif
785      self.Add( mToolbar );
786    }
787
788    // Iterate list of buttons and add active ones to Toolbar
789    std::size_t numberOfOptionsRequired =  GetNumberOfEnabledOptions();
790    std::size_t numberOfOptionsAdded = 0u;
791    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
792    {
793      const ButtonRequirement& button( *it );
794      if ( button.enabled )
795      {
796        numberOfOptionsAdded++;
797        AddOption(  button, ( numberOfOptionsAdded < numberOfOptionsRequired ) , showIcons, showCaptions );
798      }
799    }
800  }
801
802 TextSelectionPopup::TextSelectionPopup( TextSelectionPopupCallbackInterface* callbackInterface )
803 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
804   mToolbar(),
805   mPopupMaxSize(),
806   mOptionMaxSize(),
807   mOptionMinSize(),
808   mOptionDividerSize(),
809   mEnabledButtons( Toolkit::TextSelectionPopup::NONE ),
810   mCallbackInterface( callbackInterface ),
811   mPressedColor( DEFAULT_OPTION_PRESSED_COLOR ),
812   mDividerColor( Color::WHITE ),
813   mIconColor( Color::WHITE ),
814   mSelectOptionPriority( 1 ),
815   mSelectAllOptionPriority ( 2 ),
816   mCutOptionPriority ( 4 ),
817   mCopyOptionPriority ( 3 ),
818   mPasteOptionPriority ( 5 ),
819   mClipboardOptionPriority( 6 ),
820   mFadeInDuration(0.0f),
821   mFadeOutDuration(0.0f),
822   mShowIcons( false ),
823   mShowCaptions( true ),
824   mPopupShowing( false ),
825   mButtonsChanged( false )
826 {
827 }
828
829 TextSelectionPopup::~TextSelectionPopup()
830 {
831 }
832
833
834 } // namespace Internal
835
836 } // namespace Toolkit
837
838 } // namespace Dali
839
840