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