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