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