Merge "(AutomatedTests) Synchronise TestSingletonService with Adaptor" into devel...
[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::RaiseAbove( Layer target )
302 {
303   if( mToolbar )
304   {
305     mToolbar.RaiseAbove( target );
306   }
307 }
308
309 void TextSelectionPopup::OnInitialize()
310 {
311   CreatePopup();
312 }
313
314 void TextSelectionPopup::OnStageConnection( int depth )
315 {
316   // Call the Control::OnStageConnection() to set the depth of the background.
317   Control::OnStageConnection( depth );
318
319   // TextSelectionToolbar::OnStageConnection() will set the depths of all the popup's components.
320 }
321
322 bool TextSelectionPopup::OnCutButtonPressed( Toolkit::Button button )
323 {
324   if( mCallbackInterface )
325   {
326     mCallbackInterface->TextPopupButtonTouched( Toolkit::TextSelectionPopup::CUT );
327   }
328
329   return true;
330 }
331
332 bool TextSelectionPopup::OnCopyButtonPressed( Toolkit::Button button )
333 {
334   if( mCallbackInterface )
335   {
336     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::COPY );
337   }
338
339   return true;
340 }
341
342 bool TextSelectionPopup::OnPasteButtonPressed( Toolkit::Button button )
343 {
344   if( mCallbackInterface )
345   {
346     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::PASTE );
347   }
348
349   return true;
350 }
351
352 bool TextSelectionPopup::OnSelectButtonPressed( Toolkit::Button button )
353 {
354   if( mCallbackInterface )
355   {
356     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::SELECT );
357   }
358
359   return true;
360 }
361
362 bool TextSelectionPopup::OnSelectAllButtonPressed( Toolkit::Button button )
363 {
364   if( mCallbackInterface )
365   {
366     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::SELECT_ALL );
367   }
368
369   return true;
370 }
371
372 bool TextSelectionPopup::OnClipboardButtonPressed( Toolkit::Button button )
373 {
374   if( mCallbackInterface )
375   {
376     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::CLIPBOARD );
377   }
378
379   return true;
380 }
381
382 void TextSelectionPopup::SetDimensionToCustomise( const PopupCustomisations& settingToCustomise, const Size& dimension )
383 {
384   switch( settingToCustomise )
385   {
386     case POPUP_MAXIMUM_SIZE :
387     {
388       Actor self = Self();
389       mMaxSize = dimension;
390       if ( mToolbar )
391       {
392         mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mMaxSize );
393       }
394       break;
395     }
396     case POPUP_MINIMUM_SIZE :
397     {
398       Actor self = Self();
399       mMinSize = dimension;
400       // Option can not be smaller than this if only one.
401       break;
402     }
403     case OPTION_MAXIMUM_SIZE :
404     {
405       mOptionMaxSize = dimension;
406       // Option max size not currently currently supported
407
408       break;
409     }
410     case OPTION_MINIMUM_SIZE :
411     {
412       mOptionMinSize = dimension;
413       // Option min size not currently currently supported
414       break;
415     }
416     case OPTION_DIVIDER_SIZE :
417     {
418       mOptionDividerSize = dimension;
419       if ( mToolbar )
420       {
421         // Resize Dividers not currently supported
422       }
423       break;
424     }
425   } // switch
426 }
427
428 Size TextSelectionPopup::GetDimensionToCustomise( const PopupCustomisations& settingToCustomise )
429 {
430   switch( settingToCustomise )
431   {
432     case POPUP_MAXIMUM_SIZE :
433     {
434       return mMaxSize;
435     }
436     case POPUP_MINIMUM_SIZE :
437     {
438       return mMinSize;
439     }
440     case OPTION_MAXIMUM_SIZE :
441     {
442       return mOptionMaxSize;
443     }
444     case OPTION_MINIMUM_SIZE :
445     {
446       return mOptionMinSize;
447     }
448     case OPTION_DIVIDER_SIZE :
449     {
450       return mOptionDividerSize;
451     }
452   } // switch
453
454   return Size::ZERO;
455 }
456
457 void TextSelectionPopup::SetButtonImage( Toolkit::TextSelectionPopup::Buttons button, Dali::Image image )
458 {
459    switch ( button )
460    {
461    break;
462    case Toolkit::TextSelectionPopup::CLIPBOARD:
463    {
464      mClipboardIconImage  = image;
465    }
466    break;
467    case Toolkit::TextSelectionPopup::CUT :
468    {
469      mCutIconImage = image;
470    }
471    break;
472    case Toolkit::TextSelectionPopup::COPY :
473    {
474      mCopyIconImage = image;
475    }
476    break;
477    case Toolkit::TextSelectionPopup::PASTE :
478    {
479      mPasteIconImage = image;
480    }
481    break;
482    case Toolkit::TextSelectionPopup::SELECT :
483    {
484      mSelectIconImage = image;
485    }
486    break;
487    case Toolkit::TextSelectionPopup::SELECT_ALL :
488    {
489      mSelectAllIconImage = image;
490    }
491    break;
492    default :
493    {
494      DALI_ASSERT_DEBUG( "TextSelectionPopup SetPopupImage Unknown Button" );
495    }
496    } // switch
497 }
498
499 Dali::Image TextSelectionPopup::GetButtonImage( Toolkit::TextSelectionPopup::Buttons button )
500 {
501   switch ( button )
502   {
503   case Toolkit::TextSelectionPopup::CLIPBOARD :
504   {
505     return mClipboardIconImage;
506   }
507   break;
508   case Toolkit::TextSelectionPopup::CUT :
509   {
510     return mCutIconImage;
511   }
512   break;
513   case Toolkit::TextSelectionPopup::COPY :
514   {
515     return mCopyIconImage;
516   }
517   break;
518   case Toolkit::TextSelectionPopup::PASTE :
519   {
520     return mPasteIconImage;
521   }
522   break;
523   case Toolkit::TextSelectionPopup::SELECT :
524   {
525     return mSelectIconImage;
526   }
527   break;
528   case Toolkit::TextSelectionPopup::SELECT_ALL :
529   {
530     return mSelectAllIconImage;
531   }
532   break;
533   default :
534   {
535     DALI_ASSERT_DEBUG( "TextSelectionPopup GetPopupImage Unknown Button" );
536   }
537   } // switch
538
539   return Dali::Image();
540 }
541
542  void TextSelectionPopup::CreateOrderedListOfPopupOptions()
543  {
544    mOrderListOfButtons.clear();
545    mOrderListOfButtons.reserve( 8u );
546
547    // Create button for each possible option using Option priority
548    if ( !mCutIconImage )
549    {
550      mCutIconImage = ResourceImage::New( OPTION_ICON_CUT );
551    }
552    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CUT, mCutOptionPriority, OPTION_CUT, POPUP_CUT_STRING , mCutIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::CUT)  ) );
553
554    if ( !mCopyIconImage )
555    {
556      mCopyIconImage = ResourceImage::New( OPTION_ICON_COPY );
557    }
558    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::COPY, mCopyOptionPriority, OPTION_COPY, POPUP_COPY_STRING, mCopyIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::COPY)  ) );
559
560    if ( !mPasteIconImage )
561    {
562      mPasteIconImage = ResourceImage::New( OPTION_ICON_PASTE );
563    }
564    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::PASTE, mPasteOptionPriority, OPTION_PASTE, POPUP_PASTE_STRING, mPasteIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::PASTE)  ) );
565
566    if ( !mSelectIconImage )
567    mSelectIconImage = ResourceImage::New( OPTION_ICON_SELECT );
568    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT, mSelectOptionPriority, OPTION_SELECT_WORD, POPUP_SELECT_STRING, mSelectIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT)  ) );
569
570    if ( !mSelectAllIconImage )
571    {
572     mSelectAllIconImage = ResourceImage::New( OPTION_ICON_SELECT_ALL );
573    }
574    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT_ALL, mSelectAllOptionPriority, OPTION_SELECT_ALL, POPUP_SELECT_ALL_STRING, mSelectAllIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT_ALL)  ) );
575
576    if ( !mClipboardIconImage )
577    {
578      mClipboardIconImage = ResourceImage::New( OPTION_ICON_CLIPBOARD );
579    }
580    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CLIPBOARD, mClipboardOptionPriority, OPTION_CLIPBOARD, POPUP_CLIPBOARD_STRING, mClipboardIconImage, ( mEnabledButtons & Toolkit::TextSelectionPopup::CLIPBOARD)  ) );
581
582    // Sort the buttons according their priorities.
583    std::sort( mOrderListOfButtons.begin(), mOrderListOfButtons.end(), TextSelectionPopup::ButtonPriorityCompare() );
584  }
585
586  void TextSelectionPopup::AddOption( const ButtonRequirement& button, bool showDivider, bool showIcons, bool showCaption  )
587  {
588    const std::string& name = button.name;
589    const std::string& caption = button.caption;
590    Image iconImage = button.icon;
591
592    // 1. Create the backgrounds for the popup option both normal and pressed.
593    // Both containers will be added to a button.
594
595    Toolkit::TableView optionContainer = Toolkit::TableView::New( (showIcons&showCaption)?2:1 , 1 );
596    optionContainer.SetFitHeight( 0 );
597    optionContainer.SetFitWidth( 0 );
598
599    Toolkit::TableView  optionPressedContainer = Toolkit::TableView::New( (showIcons&showCaption)?2:1 , 1 );
600    optionPressedContainer.SetFitHeight( 0 );
601    optionPressedContainer.SetFitWidth( 0 );
602    optionPressedContainer.SetBackgroundColor( mPressedColor );
603
604 #ifdef DECORATOR_DEBUG
605    optionContainer.SetName("optionContainer");
606    optionPressedContainer.SetName("optionPressedContainer");
607 #endif
608    // 2. Add text.
609
610    if ( showCaption )
611    {
612      Toolkit::TextLabel captionTextLabel = Toolkit::TextLabel::New();
613      captionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
614      captionTextLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
615
616      Toolkit::TextLabel pressedCaptionTextLabel = Toolkit::TextLabel::New();
617      pressedCaptionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
618      pressedCaptionTextLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
619
620      Padding padding;
621      padding.left = 24.0f;
622      padding.right = 24.0f;
623      padding.top = 14.0f;
624      padding.bottom = 14.0f;
625      captionTextLabel.SetPadding( padding );
626      pressedCaptionTextLabel.SetPadding( padding );
627
628      optionContainer.AddChild( captionTextLabel, Toolkit::TableView::CellPosition(( showIcons&showCaption)?1:0, 0 )  );
629      optionPressedContainer.AddChild( pressedCaptionTextLabel, Toolkit::TableView::CellPosition(( showIcons&showCaption)?1:0, 0 ) );
630    }
631
632    int depth = Self().GetHierarchyDepth();
633    // 3. Create the icons
634    if ( showIcons )
635    {
636      ImageActor pressedIcon = ImageActor::New(  iconImage );
637      ImageActor icon = ImageActor::New(  iconImage );
638      icon.SetSortModifier( DECORATION_DEPTH_INDEX + depth - 1 );
639      pressedIcon.SetSortModifier( DECORATION_DEPTH_INDEX + depth - 1 );
640
641      icon.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
642      pressedIcon.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
643      icon.SetColor( mIconColor );
644
645      if ( showCaption & showIcons )
646      {
647        optionContainer.SetFitHeight( 1 );
648        optionContainer.SetFitWidth( 1 );
649        optionPressedContainer.SetFitHeight( 1 );
650        optionPressedContainer.SetFitWidth( 1 );
651      }
652
653      optionContainer.AddChild( icon, Toolkit::TableView::CellPosition( 0, 0 )  );
654      optionPressedContainer.AddChild( pressedIcon, Toolkit::TableView::CellPosition( 0, 0 )  );
655
656      icon.SetPadding( Padding( 10.0f, 10.0f, 10.0f, 10.0f ) );
657      pressedIcon.SetPadding( Padding( 10.0f, 10.0f, 10.0f, 10.0f ) );
658    }
659
660    // 4. Create a option.
661    Toolkit::PushButton option = Toolkit::PushButton::New();
662    option.SetName( name );
663    option.SetAnimationTime( 0.0f );
664    option.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
665
666    switch( button.id )
667    {
668      case Toolkit::TextSelectionPopup::CUT:
669      {
670        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnCutButtonPressed );
671        break;
672      }
673      case Toolkit::TextSelectionPopup::COPY:
674      {
675        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnCopyButtonPressed );
676        break;
677      }
678      case Toolkit::TextSelectionPopup::PASTE:
679      {
680        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnPasteButtonPressed );
681        break;
682      }
683      case Toolkit::TextSelectionPopup::SELECT:
684      {
685        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnSelectButtonPressed );
686        break;
687      }
688      case Toolkit::TextSelectionPopup::SELECT_ALL:
689      {
690        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnSelectAllButtonPressed );
691        break;
692      }
693      case Toolkit::TextSelectionPopup::CLIPBOARD:
694      {
695        option.ClickedSignal().Connect( this, &TextSelectionPopup::OnClipboardButtonPressed );
696        break;
697      }
698      case Toolkit::TextSelectionPopup::NONE:
699      {
700        // Nothing to do:
701        break;
702      }
703    }
704
705    // 5. Set the normal option image.
706    option.SetButtonImage( optionContainer );
707
708    // 6. Set the pressed option image
709    option.SetSelectedImage( optionPressedContainer );
710
711    // 7 Add option to tool bar
712    mToolbar.AddOption( option );
713
714    // 8. Add the divider
715    if( showDivider )
716    {
717      const Size size( mOptionDividerSize.width, 0.0f ); // Height FILL_TO_PARENT
718
719      ImageActor divider = Toolkit::CreateSolidColorActor( Color::WHITE );
720 #ifdef DECORATOR_DEBUG
721      divider.SetName("Text's popup divider");
722 #endif
723      divider.SetSize( size );
724      divider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
725      divider.SetColor( mDividerColor );
726      divider.SetSortModifier( DECORATION_DEPTH_INDEX + depth );
727      mToolbar.AddDivider( divider );
728    }
729  }
730
731  std::size_t TextSelectionPopup::GetNumberOfEnabledOptions()
732  {
733    std::size_t numberOfOptions = 0u;
734    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
735    {
736      const ButtonRequirement& button( *it );
737      if( button.enabled )
738      {
739        ++numberOfOptions;
740      }
741    }
742
743    return numberOfOptions;
744  }
745
746  void TextSelectionPopup::AddPopupOptionsToToolbar( bool showIcons, bool showCaptions )
747  {
748    // Iterate list of buttons and add active ones to Toolbar
749    std::size_t numberOfOptionsRequired =  GetNumberOfEnabledOptions();
750    std::size_t numberOfOptionsAdded = 0u;
751    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
752    {
753      const ButtonRequirement& button( *it );
754      if ( button.enabled )
755      {
756        numberOfOptionsAdded++;
757        AddOption(  button, ( numberOfOptionsAdded < numberOfOptionsRequired ) , showIcons, showCaptions );
758      }
759    }
760  }
761
762  void TextSelectionPopup::CreatePopup()
763  {
764    Actor self = Self();
765    CreateOrderedListOfPopupOptions();  //todo Currently causes all options to be shown
766    self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
767    SetBackgroundImage( NinePatchImage::New( DEFAULT_POPUP_BACKGROUND_IMAGE ) );
768
769    if( !mToolbar )
770    {
771      mToolbar = Toolkit::TextSelectionToolbar::New();
772      mToolbar.SetParentOrigin( ParentOrigin::CENTER );
773      mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mMaxSize );
774      self.Add( mToolbar );
775      AddPopupOptionsToToolbar( mShowIcons, mShowCaptions );
776    }
777  }
778
779 TextSelectionPopup::TextSelectionPopup( TextSelectionPopupCallbackInterface* callbackInterface )
780 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
781   mToolbar(),
782   mMaxSize(),
783   mMinSize(),
784   mOptionDividerSize( Size( 2.0f, 0.0f) ),
785   mEnabledButtons( Toolkit::TextSelectionPopup::NONE ),
786   mCallbackInterface( callbackInterface ),
787   mDividerColor( DEFAULT_POPUP_DIVIDER_COLOR ),
788   mIconColor( DEFAULT_OPTION_ICON ),
789   mPressedColor( DEFAULT_OPTION_PRESSED_COLOR ),
790   mSelectOptionPriority( 1 ),
791   mSelectAllOptionPriority ( 2 ),
792   mCutOptionPriority ( 4 ),
793   mCopyOptionPriority ( 3 ),
794   mPasteOptionPriority ( 5 ),
795   mClipboardOptionPriority( 6 ),
796   mShowIcons( false ),
797   mShowCaptions( true )
798 {
799 }
800
801 TextSelectionPopup::~TextSelectionPopup()
802 {
803 }
804
805
806 } // namespace Internal
807
808 } // namespace Toolkit
809
810 } // namespace Dali
811
812