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