2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/text-controls/text-selection-popup-impl.h>
22 #include <dali-toolkit/public-api/controls/buttons/push-button.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>
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>
46 // todo Move this to adaptor??
47 #define GET_LOCALE_TEXT(string) dgettext("elementary", string)
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.5f, 1.0f, 1.0f, 1.0f ) );
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" );
61 const float OPTION_MARGIN_WIDTH( 10.f ); ///< The margin between the right or lefts edge and the text or icon.
63 #ifdef DGETTEXT_ENABLED
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")
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"
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.
92 return Toolkit::TextSelectionPopup::New();
95 // Setup properties, signals and actions using the type-registry.
97 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionPopup, Toolkit::Control, Create );
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 )
111 DALI_TYPE_REGISTRATION_END()
115 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New()
117 // Create the implementation, temporarily owned by this handle on stack
118 IntrusivePtr< TextSelectionPopup > impl = new TextSelectionPopup();
120 // Pass ownership to CustomActor handle
121 Dali::Toolkit::TextSelectionPopup handle( *impl );
123 // Second-phase init of the implementation
124 // This can only be done after the CustomActor connection has been made...
130 void TextSelectionPopup::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
132 Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
136 TextSelectionPopup& impl( GetImpl( selectionPopup ) );
140 case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
142 impl.SetDimensionToCustomise( POPUP_MAXIMUM_SIZE, value.Get< Vector2 >() );
145 case Toolkit::TextSelectionPopup::Property::POPUP_MIN_SIZE:
147 impl.SetDimensionToCustomise( POPUP_MINIMUM_SIZE, value.Get< Vector2 >() );
150 case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
152 impl.SetDimensionToCustomise( OPTION_MAXIMUM_SIZE, value.Get< Vector2 >() );
155 case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
157 impl.SetDimensionToCustomise( OPTION_MINIMUM_SIZE, value.Get< Vector2>() );
160 case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
162 impl.SetDimensionToCustomise( OPTION_DIVIDER_SIZE, value.Get< Vector2>() );
165 case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
167 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
168 impl.SetButtonImage( CLIPBOARD, image );
171 case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
173 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
174 impl.SetButtonImage( CUT, image );
177 case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
179 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
180 impl.SetButtonImage( COPY, image );
183 case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
185 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
186 impl.SetButtonImage( PASTE, image );
189 case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
191 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
192 impl.SetButtonImage( SELECT, image );
195 case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
197 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
198 impl.SetButtonImage( SELECT_ALL, image );
202 } // TextSelectionPopup
205 Property::Value TextSelectionPopup::GetProperty( BaseObject* object, Property::Index index )
207 Property::Value value;
209 Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
213 TextSelectionPopup& impl( GetImpl( selectionPopup ) );
217 case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
219 value = impl.GetDimensionToCustomise( POPUP_MAXIMUM_SIZE );
222 case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
224 value = impl.GetDimensionToCustomise( OPTION_MAXIMUM_SIZE );
227 case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
229 value = impl.GetDimensionToCustomise( OPTION_MINIMUM_SIZE );
232 case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
234 value = impl.GetDimensionToCustomise( OPTION_DIVIDER_SIZE );
237 case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
239 ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( CLIPBOARD ) );
242 value = image.GetUrl();
246 case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
248 ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( CUT ) );
251 value = image.GetUrl();
255 case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
257 ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( COPY ) );
260 value = image.GetUrl();
264 case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
266 ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( PASTE ) );
269 value = image.GetUrl();
273 case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
275 ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( SELECT ) );
278 value = image.GetUrl();
282 case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
284 ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( SELECT_ALL ) );
287 value = image.GetUrl();
296 void TextSelectionPopup::OnInitialize()
301 void TextSelectionPopup::SetDimensionToCustomise( const PopupCustomisations& settingToCustomise, const Size& dimension )
303 switch( settingToCustomise )
305 case POPUP_MAXIMUM_SIZE :
308 mMaxSize = dimension;
311 mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mMaxSize );
315 case POPUP_MINIMUM_SIZE :
318 mMinSize = dimension;
319 // Option can not be smaller than this if only one.
322 case OPTION_MAXIMUM_SIZE :
324 mOptionMaxSize = dimension;
325 // Option max size not currently currently supported
329 case OPTION_MINIMUM_SIZE :
331 mOptionMinSize = dimension;
332 // Option min size not currently currently supported
335 case OPTION_DIVIDER_SIZE :
337 mOptionDividerSize = dimension;
340 // Resize Dividers not currently supported
347 Size TextSelectionPopup::GetDimensionToCustomise( const PopupCustomisations& settingToCustomise )
349 switch( settingToCustomise )
351 case POPUP_MAXIMUM_SIZE :
355 case POPUP_MINIMUM_SIZE :
359 case OPTION_MAXIMUM_SIZE :
361 return mOptionMaxSize;
363 case OPTION_MINIMUM_SIZE :
365 return mOptionMinSize;
367 case OPTION_DIVIDER_SIZE :
369 return mOptionDividerSize;
376 void TextSelectionPopup::SetButtonImage( Buttons button, Dali::Image image )
383 mClipboardIconImage = image;
388 mCutIconImage = image;
393 mCopyIconImage = image;
398 mPasteIconImage = image;
403 mSelectIconImage = image;
408 mSelectAllIconImage = image;
413 DALI_ASSERT_DEBUG( "TextSelectionPopup SetPopupImage Unknown Button" );
418 Dali::Image TextSelectionPopup::GetButtonImage( Buttons button )
424 return mClipboardIconImage;
429 return mCutIconImage;
434 return mCopyIconImage;
439 return mPasteIconImage;
444 return mSelectIconImage;
449 return mSelectAllIconImage;
454 DALI_ASSERT_DEBUG( "TextSelectionPopup GetPopupImage Unknown Button" );
458 return Dali::Image();
461 void TextSelectionPopup::CreateOrderedListOfPopupOptions()
463 mOrderListOfButtons.clear();
465 // Create button for each possible option using Option priority
466 if ( !mCutIconImage )
468 mCutIconImage = ResourceImage::New( OPTION_ICON_CUT );
470 mOrderListOfButtons.push_back( ButtonRequirement( CUT, mCutOptionPriority, OPTION_CUT, POPUP_CUT_STRING , mCutIconImage, false ) );
472 if ( !mCopyIconImage )
474 mCopyIconImage = ResourceImage::New( OPTION_ICON_COPY );
476 mOrderListOfButtons.push_back( ButtonRequirement( COPY, mCopyOptionPriority, OPTION_COPY, POPUP_COPY_STRING, mCopyIconImage, false ) );
478 if ( !mPasteIconImage )
480 mPasteIconImage = ResourceImage::New( OPTION_ICON_PASTE );
482 mOrderListOfButtons.push_back( ButtonRequirement( PASTE, mPasteOptionPriority, OPTION_PASTE, POPUP_PASTE_STRING, mPasteIconImage, false ) );
484 if ( !mSelectIconImage )
485 mSelectIconImage = ResourceImage::New( OPTION_ICON_SELECT );
486 mOrderListOfButtons.push_back( ButtonRequirement( SELECT, mSelectOptionPriority, OPTION_SELECT_WORD, POPUP_SELECT_STRING, mSelectIconImage, true ) );
488 if ( !mSelectAllIconImage )
490 mSelectAllIconImage = ResourceImage::New( OPTION_ICON_SELECT_ALL );
492 mOrderListOfButtons.push_back( ButtonRequirement( SELECT_ALL, mSelectAllOptionPriority, OPTION_SELECT_ALL, POPUP_SELECT_ALL_STRING, mSelectAllIconImage, true ) );
494 if ( !mClipboardIconImage )
496 mClipboardIconImage = ResourceImage::New( OPTION_ICON_CLIPBOARD );
498 mOrderListOfButtons.push_back( ButtonRequirement( CLIPBOARD, mClipboardOptionPriority, OPTION_CLIPBOARD, POPUP_CLIPBOARD_STRING, mClipboardIconImage, false ) );
500 // Sort the buttons according their priorities.
501 std::sort( mOrderListOfButtons.begin(), mOrderListOfButtons.end(), TextSelectionPopup::ButtonPriorityCompare() );
504 void TextSelectionPopup::AddOption( const std::string& name, const std::string& caption, const Image iconImage, bool showDivider, bool showIcons, bool showCaption )
506 // 1. Create the backgrounds for the popup option both normal and pressed.
507 // Both containers will be added to a button.
509 Toolkit::TableView optionContainer = Toolkit::TableView::New( (showIcons&showCaption)?2:1 , 1 );
510 optionContainer.SetDrawMode( DrawMode::OVERLAY );
511 optionContainer.SetFitHeight( 0 );
512 optionContainer.SetFitWidth( 0 );
514 Toolkit::TableView optionPressedContainer = Toolkit::TableView::New( (showIcons&showCaption)?2:1 , 1 );
515 optionPressedContainer.SetDrawMode( DrawMode::OVERLAY );
516 optionPressedContainer.SetFitHeight( 0 );
517 optionPressedContainer.SetFitWidth( 0 );
518 optionPressedContainer.SetBackgroundColor(Color::RED); //todo member variable
520 #ifdef DECORATOR_DEBUG
521 optionContainer.SetName("optionContainer");
522 optionPressedContainer.SetName("optionPressedContainer");
528 Toolkit::TextLabel captionTextLabel = Toolkit::TextLabel::New();
529 captionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
530 captionTextLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
532 Toolkit::TextLabel pressedCaptionTextLabel = Toolkit::TextLabel::New();
533 pressedCaptionTextLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, caption );
534 pressedCaptionTextLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
537 padding.left = 24.0f;
538 padding.right = 24.0f;
540 padding.bottom = 14.0f;
541 captionTextLabel.SetPadding( padding );
542 pressedCaptionTextLabel.SetPadding( padding );
544 optionContainer.AddChild( captionTextLabel, Toolkit::TableView::CellPosition(( showIcons&showCaption)?1:0, 0 ) );
545 optionPressedContainer.AddChild( pressedCaptionTextLabel, Toolkit::TableView::CellPosition(( showIcons&showCaption)?1:0, 0 ) );
548 // 3. Create the icons
551 ImageActor pressedIcon = ImageActor::New( iconImage );
552 ImageActor icon = ImageActor::New( iconImage );
554 icon.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
555 pressedIcon.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
556 icon.SetColor( mIconColor );
557 pressedIcon.SetColor( mIconPressedColor );
559 if ( showCaption & showIcons )
561 optionContainer.SetFitHeight( 1 );
562 optionContainer.SetFitWidth( 1 );
563 optionPressedContainer.SetFitHeight( 1 );
564 optionPressedContainer.SetFitWidth( 1 );
567 optionContainer.AddChild( icon, Toolkit::TableView::CellPosition( 0, 0 ) );
568 optionPressedContainer.AddChild( pressedIcon, Toolkit::TableView::CellPosition( 0, 0 ) );
570 icon.SetPadding( Padding( 10.0f, 10.0f, 10.0f, 10.0f ) );
571 pressedIcon.SetPadding( Padding( 10.0f, 10.0f, 10.0f, 10.0f ) );
574 // 4. Create a option.
575 Toolkit::PushButton option = Toolkit::PushButton::New();
576 option.SetName( name );
577 option.SetAnimationTime( 0.0f );
578 option.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
579 //option.ClickedSignal().Connect( this, &TextInputPopup::OnButtonPressed );
581 // 5. Set the normal option image.
582 option.SetButtonImage( optionContainer );
584 // 6. Set the pressed option image
585 option.SetSelectedImage( optionPressedContainer );
587 // 7 Add option to tool bar
588 mToolbar.AddOption( option );
590 // 8. Add the divider
593 const Size size( mOptionDividerSize.width, 0.0f ); // Height FILL_TO_PARENT
595 ImageActor divider = Toolkit::CreateSolidColorActor( Color::WHITE );
596 divider.SetSize( size );
597 divider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
598 divider.SetColor( mLineColor );
599 mToolbar.AddDivider( divider );
603 std::size_t TextSelectionPopup::GetNumberOfEnabledOptions()
605 std::size_t numberOfOptions = 0u;
606 for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
608 const ButtonRequirement& button( *it );
615 return numberOfOptions;
618 void TextSelectionPopup::AddPopupOptionsToToolbar( bool showIcons, bool showCaptions )
620 // Iterate list of buttons and add active ones to Toolbar
621 std::size_t numberOfOptionsRequired = GetNumberOfEnabledOptions();
622 std::size_t numberOfOptionsAdded = 0u;
623 for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
625 const ButtonRequirement& button( *it );
626 if ( button.enabled )
628 numberOfOptionsAdded++;
629 AddOption( button.name, button.caption, button.icon, ( numberOfOptionsAdded < numberOfOptionsRequired ) , showIcons, showCaptions );
634 void TextSelectionPopup::CreatePopup()
637 CreateOrderedListOfPopupOptions(); //todo Currently causes all options to be shown
638 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
639 SetBackgroundImage( NinePatchImage::New( DEFAULT_POPUP_BACKGROUND_IMAGE ) );
643 mToolbar = Toolkit::TextSelectionToolbar::New();
644 mToolbar.SetParentOrigin( ParentOrigin::CENTER );
645 mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mMaxSize );
646 self.Add( mToolbar );
647 AddPopupOptionsToToolbar( mShowIcons, mShowCaptions );
651 TextSelectionPopup::TextSelectionPopup()
652 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
656 mOptionDividerSize( Size( 2.0f, 0.0f) ),
657 mLineColor( DEFAULT_POPUP_LINE_COLOR ),
658 mIconColor( DEFAULT_OPTION_ICON ),
659 mIconPressedColor( DEFAULT_OPTION_ICON_PRESSED ),
660 mSelectOptionPriority( 1 ),
661 mSelectAllOptionPriority ( 2 ),
662 mCutOptionPriority ( 3 ),
663 mCopyOptionPriority ( 4 ),
664 mPasteOptionPriority ( 5 ),
665 mClipboardOptionPriority( 6 ),
667 mShowCaptions( true )
671 TextSelectionPopup::~TextSelectionPopup()
676 } // namespace Internal
678 } // namespace Toolkit