Add OPTION_DIVIDER_PADDING property to TextSelectionPopup
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-popup-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_SELECTION_POPUP_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_SELECTION_POPUP_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/layer.h>
23 #include <dali/public-api/object/property-map.h>
24 #include <string>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
28 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
29 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-toolbar.h>
30 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
31 #include <dali-toolkit/public-api/controls/control-impl.h>
32 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
33
34 namespace Dali
35 {
36 namespace Toolkit
37 {
38 namespace Internal
39 {
40 enum PopupCustomisations
41 {
42   POPUP_MAXIMUM_SIZE,
43   OPTION_MAXIMUM_SIZE,
44   OPTION_MINIMUM_SIZE,
45   OPTION_DIVIDER_SIZE
46 };
47
48 class TextSelectionPopup : public Control
49 {
50 public:
51   struct ButtonRequirement
52   {
53     ButtonRequirement()
54     : id(Toolkit::TextSelectionPopup::NONE),
55       priority(0u),
56       name(),
57       caption(),
58       enabled(false)
59     {
60     }
61
62     ButtonRequirement(Toolkit::TextSelectionPopup::Buttons buttonId,
63                       std::size_t                          buttonPriority,
64                       const std::string&                   buttonName,
65                       const std::string&                   buttonCaption,
66                       bool                                 buttonEnabled)
67     : id(buttonId),
68       priority(buttonPriority),
69       name(buttonName),
70       caption(buttonCaption),
71       enabled(buttonEnabled)
72     {
73     }
74
75     Toolkit::TextSelectionPopup::Buttons id;
76     std::size_t                          priority;
77     std::string                          name;
78     std::string                          caption;
79     bool                                 enabled;
80   };
81
82   struct ButtonPriorityCompare
83   {
84     bool operator()(const ButtonRequirement& lhs, const ButtonRequirement& rhs) const
85     {
86       return lhs.priority < rhs.priority;
87     }
88   };
89
90   /**
91    * @brief New constructor with provided buttons to enable.
92    * @param[in] callbackInterface The text popup callback interface which receives the button click callbacks.
93    * @return A handle to the TextSelectionPopup control.
94    */
95   static Toolkit::TextSelectionPopup New(TextSelectionPopupCallbackInterface* callbackInterface);
96
97   // Properties
98
99   /**
100    * @brief Called when a property of an object of this type is set.
101    * @param[in] object The object whose property is set.
102    * @param[in] index The property index.
103    * @param[in] value The new property value.
104    */
105   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
106
107   /**
108    * @brief Called to retrieve a property of an object of this type.
109    *
110    * @param[in] object The object whose property is to be retrieved.
111    * @param[in] index The property index.
112    * @return The current value of the property.
113    */
114   static Property::Value GetProperty(BaseObject* object, Property::Index index);
115
116   /**
117    * @copydoc Toolkit::EnableButtons
118    */
119   void EnableButtons(Toolkit::TextSelectionPopup::Buttons buttonsToEnable);
120
121   /**
122    * @copydoc Toolkit::TextSelectionPopup::RaiseAbove()
123    */
124   void RaiseAbove(Actor target);
125
126   /**
127    * @copydoc Toolkit::TextSelectionPopup::ShowPopup()
128    */
129   void ShowPopup();
130
131   /**
132    * @copydoc Toolkiut::TextSelectionPopup::HidePopup()
133    */
134   void HidePopup();
135
136 private: // From Control
137   /**
138    * @copydoc Control::OnInitialize()
139    */
140   void OnInitialize() override;
141
142 private: // Implementation
143   void HideAnimationFinished(Animation& animation);
144
145   /**
146    * @brief When the cut button is pressed.
147    * @param[in] button the button pressed
148    * @return @e true to consume the event.
149    */
150   bool OnCutButtonPressed(Toolkit::Button button);
151
152   /**
153    * @brief When the copy button is pressed.
154    * @param[in] button the button pressed
155    * @return @e true to consume the event.
156    */
157   bool OnCopyButtonPressed(Toolkit::Button button);
158
159   /**
160    * @brief When the paste button is pressed.
161    * @param[in] button the button pressed
162    * @return @e true to consume the event.
163    */
164   bool OnPasteButtonPressed(Toolkit::Button button);
165
166   /**
167    * @brief When the select button is pressed.
168    * @param[in] button the button pressed
169    * @return @e true to consume the event.
170    */
171   bool OnSelectButtonPressed(Toolkit::Button button);
172
173   /**
174    * @brief When the select all button is pressed.
175    * @param[in] button the button pressed
176    * @return @e true to consume the event.
177    */
178   bool OnSelectAllButtonPressed(Toolkit::Button button);
179
180   /**
181    * @brief When the clipboard button is pressed.
182    * @param[in] button the button pressed
183    * @return @e true to consume the event.
184    */
185   bool OnClipboardButtonPressed(Toolkit::Button button);
186
187   /**
188    * @brief Method to set the dimension or dimension constraint on certain aspects of the Popup.
189    *
190    * @param[in] settingToCustomise The setting for the PopupCustomisations enum that can be customised
191    * @param[in] dimension The size to customise with
192    */
193   void SetDimensionToCustomise(const PopupCustomisations& settingToCustomise, const Size& dimension);
194
195   /**
196    * @brief Method to get the dimension or dimension constraint on certain aspects of the Popup that was previously customised
197    *
198    * @param[in] setting The setting from the PopupCustomisations enum
199    */
200   Size GetDimensionToCustomise(const PopupCustomisations& setting) const;
201
202   /**
203    * @brief Sets the image for the given button of the Popup.
204    *
205    * @param[in] button  The button the image should be used for from the Buttons Enum.
206    * @param[in] image The image to use.
207    */
208   void SetButtonImage(Toolkit::TextSelectionPopup::Buttons button, const std::string& image);
209
210   /**
211    * @brief Retrieves the image of the given button used by the popup
212    *
213    * @param[in] button The button to get the image from
214    * @return The image used for that button.
215    */
216   const std::string& GetButtonImage(Toolkit::TextSelectionPopup::Buttons button) const;
217
218   /**
219    * @brief Sets the image for the pressed state of a popup option.
220    *
221    * @param[in]  filename The image filename to use.
222    */
223   void SetPressedImage(const std::string& filename);
224
225   /**
226    * @brief Gets the image used for the pressed state of a popup option.
227    *
228    * @return     The image filename used.
229    */
230   std::string GetPressedImage() const;
231
232   /**
233    * Set option divider padding
234    * @param[in] padding BEGIN END BOTTOM TOP
235    */
236   void SetOptionDividerPadding(const Padding& padding);
237
238   /**
239    * Get option divider padding
240    * @return Padding
241    */
242   Padding GetOptionDividerPadding() const;
243
244   void CreateOrderedListOfPopupOptions();
245
246   void AddOption(const ButtonRequirement& button, bool showDivider, bool showIcons, bool showCaption);
247
248   std::size_t GetNumberOfEnabledOptions() const;
249
250   void AddPopupOptionsToToolbar(bool showIcons, bool showCaptions);
251
252   /**
253    * Creates the background-border image
254    *
255    * @param[in] propertyMap The properties describing the background-border
256    */
257   void CreateBackgroundBorder(Property::Map& propertyMap);
258
259   /**
260    * Construct a new TextField.
261    */
262   TextSelectionPopup(TextSelectionPopupCallbackInterface* callbackInterface);
263
264   /**
265    * A reference counted object may only be deleted by calling Unreference()
266    */
267   virtual ~TextSelectionPopup();
268
269 private:
270   // Undefined copy constructor and assignment operators
271   TextSelectionPopup(const TextSelectionPopup&);
272   TextSelectionPopup& operator=(const TextSelectionPopup& rhs);
273
274 private: // Data
275   Dali::Toolkit::TextSelectionToolbar mToolbar;
276
277   Dali::Toolkit::TableView mTableOfButtons; // Actor which holds all the buttons, sensitivity can be set on buttons via this actor
278
279   // Images paths to be used by the Popup buttons
280   std::string mCutIconImage;
281   std::string mCopyIconImage;
282   std::string mPasteIconImage;
283   std::string mClipboardIconImage;
284   std::string mSelectIconImage;
285   std::string mSelectAllIconImage;
286
287   Size    mPopupMaxSize;         // Maximum size of the Popup
288   Size    mOptionMaxSize;        // Maximum size of an Option button
289   Size    mOptionMinSize;        // Minimum size of an Option button
290   Size    mOptionDividerSize;    // Size of divider line
291   Padding mOptionDividerPadding; // Padding of divider line
292
293   std::vector<ButtonRequirement> mOrderListOfButtons; // List of buttons in the order to be displayed and a flag to indicate if needed.
294
295   Toolkit::TextSelectionPopup::Buttons          mEnabledButtons; // stores enabled buttons
296   Toolkit::TextSelectionPopupCallbackInterface* mCallbackInterface;
297
298   std::string mPressedImage; // Image used for the popup option when pressed.
299   Vector4     mPressedColor; // Color of the popup option when pressed.
300   Vector4     mDividerColor; // Color of the divider between buttons
301   Vector4     mIconColor;    // Color of the popup icon.
302
303   // Priority of Options/Buttons in the Cut and Paste pop-up, higher priority buttons are displayed first, left to right.
304   std::size_t mSelectOptionPriority;    // Position of Select Button
305   std::size_t mSelectAllOptionPriority; // Position of Select All button
306   std::size_t mCutOptionPriority;       // Position of Cut button
307   std::size_t mCopyOptionPriority;      // Position of Copy button
308   std::size_t mPasteOptionPriority;     // Position of Paste button
309   std::size_t mClipboardOptionPriority; // Position of Clipboard button
310   float       mFadeInDuration;          // Duration of the animation to fade in the Popup
311   float       mFadeOutDuration;         // Duration of the animation to fade out the Popup
312
313   bool mShowIcons : 1;      // Flag to show icons
314   bool mShowCaptions : 1;   // Flag to show text captions
315   bool mPopupShowing : 1;   // Flag to indicate Popup showing
316   bool mButtonsChanged : 1; // Flag to indicate the Popup Buttons have changed
317 };
318
319 } // namespace Internal
320
321 // Helpers for public-api forwarding methods
322
323 inline Toolkit::Internal::TextSelectionPopup& GetImpl(Toolkit::TextSelectionPopup& textSelectionPopup)
324 {
325   DALI_ASSERT_ALWAYS(textSelectionPopup);
326
327   Dali::RefObject& handle = textSelectionPopup.GetImplementation();
328
329   return static_cast<Toolkit::Internal::TextSelectionPopup&>(handle);
330 }
331
332 inline const Toolkit::Internal::TextSelectionPopup& GetImpl(const Toolkit::TextSelectionPopup& textSelectionPopup)
333 {
334   DALI_ASSERT_ALWAYS(textSelectionPopup);
335
336   const Dali::RefObject& handle = textSelectionPopup.GetImplementation();
337
338   return static_cast<const Toolkit::Internal::TextSelectionPopup&>(handle);
339 }
340
341 } // namespace Toolkit
342
343 } // namespace Dali
344
345 #endif // DALI_TOOLKIT_INTERNAL_TEXT_SELECTION_POPUP_H