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