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