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