(TextInput) Cursor is not moved to start of word when selecting text.
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / text-input / text-input-popup-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_TEXT_INPUT_POPUP_H__
2 #define __DALI_TOOLKIT_INTERNAL_TEXT_INPUT_POPUP_H__
3
4 /*
5  * Copyright (c) 2014 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 // INTERNAL INCLUDES
22 #include <dali/dali.h>
23
24 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
25 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 class Button;
34
35 namespace Internal
36 {
37
38 class TextInputPopup : public ConnectionTracker
39 {
40
41 public:
42
43   enum State
44   {
45     StateHidden,
46     StateHiding,
47     StateShowing,
48     StateShown
49   };
50
51   enum Buttons
52   {
53     ButtonsCut,
54     ButtonsCopy,
55     ButtonsPaste,
56     ButtonsSelect,
57     ButtonsSelectAll,
58     ButtonsClipboard,
59     ButtonsEnumEnd
60   };
61
62   struct ButtonRequirement
63   {
64     TextInputPopup::Buttons buttonId;
65     std::size_t orderOfPriority;
66     std::string name;
67     std::string caption;
68     Image iconImage;
69     bool enabled;
70   };
71
72   static const char* const OPTION_SELECT_WORD;
73   static const char* const OPTION_SELECT_ALL;
74   static const char* const OPTION_CUT;
75   static const char* const OPTION_COPY;
76   static const char* const OPTION_PASTE;
77   static const char* const OPTION_CLIPBOARD;
78
79   // Signal names
80   static const char* const SIGNAL_PRESSED;
81   static const char* const SIGNAL_HIDE_FINISHED;
82   static const char* const SIGNAL_SHOW_FINISHED;
83
84   // Popup Button Pressed
85   typedef SignalV2< bool( Toolkit::Button ) > PressedSignalV2;
86
87   // Popup Hide Finished
88   typedef SignalV2< void( TextInputPopup& ) > HideFinishedSignalV2;
89
90   // Popup Show Finished
91   typedef SignalV2< void( TextInputPopup& ) > ShowFinishedSignalV2;
92
93   /**
94    * Signal emitted when the button is touched.
95    */
96   PressedSignalV2& PressedSignal();
97
98   /**
99    * Signal emitted when popup is completely hidden
100    * @note Only occurs after a Show() call with animation enabled.
101    */
102   HideFinishedSignalV2& HideFinishedSignal();
103
104   /**
105    * Signal emitted when popup is completely shown
106    * @note Only occurs after a Hide() call with animation enabled.
107    */
108   ShowFinishedSignalV2& ShowFinishedSignal();
109
110 public:
111
112   /**
113    * Default constructor
114    * Creates an empty popup base actor (no content i.e. invisible)
115    */
116   TextInputPopup();
117
118   /**
119    * Clears popup options (popup no longer exists)
120    */
121   void Clear();
122
123   /**
124    * Create the label
125    * @return the newly created Image actor to be used as the icon
126    */
127   ImageActor CreateOptionIcon( Image iconImage, const Vector4& color );
128
129   /**
130    * Create the caption
131    * @param[in] styledCaption The text to be displayed
132    * @return the newly created label
133    */
134   Toolkit::TextView CreateOptionCaption( const std::string& caption, const Vector4& color );
135
136   /**
137    * Creates and sets up the background
138    */
139   void CreateBackground();
140
141   /**
142    * Create and set up the tail
143    */
144   void CreateTail();
145
146   /**
147    * Create divider if multiple options
148    * @return Return a divider image actor
149    */
150   ImageActor CreateDivider();
151
152   /**
153    * Create a background to be used when option pressed
154    * @param[in] requiredSize size Image actor should be
155    * @return Returns an Image Actor to be used a pressed background
156    */
157   ImageActor CreatePressedBackground( const Vector2& requiredSize );
158
159   /**
160    * Creates a ordered vector of button options
161    */
162   void CreateOrderedListOfOptions();
163
164   /**
165    * Get the TextSize after constrained by the Popup margins.
166    * @param[in] textSize Natural size of text
167    * @return Vector2 constrained text size.
168    *
169    */
170   Vector2 GetConstrainedTextSize( const Vector2& textSize );
171
172   /**
173    * Adds a popup option.
174    * @note Creates popup frame if not already created.
175    * @param[in] name The unique name for this option.
176    * @param[in] caption The caption (label) for this option
177    * @param[in] iconImage Image to displayed with text.
178    * @param[in] finalOption Flag to indicate that this is the final option.
179    * (set to true on the last option you add)
180    */
181   void AddOption(const std::string& name, const std::string& caption, const Image iconImage, bool finalOption);
182
183   /**
184    * Hides the popup
185    * @param[in] animate (optional) whether to animate popup to hide state over time (i.e. tween).
186    */
187   void Hide(bool animate = true);
188
189   /**
190    * Shows the popup
191    * @param[in] animate (optional) whether to animate popup to show state over time (i.e. tween).
192    * @param[in] target Actor to parent popup.
193    */
194   void Show( Actor target, bool animate = true );
195
196   /**
197    * Returns the current state of the popup.
198    * @return The state of the popup see enum State
199    */
200   State GetState(void) const;
201
202   /**
203    * Get the root actor which the buttons are added to.
204    * @return the root actor
205    */
206   Actor GetRootActor() const;
207
208   /**
209    * Set the Cut and Paste buttons color when in normal state
210    * @param[in] color color to use
211    */
212   void SetCutPastePopupColor( const Vector4& color );
213
214   /**
215    * Get the set color of the Copy and Paste Popup buttons
216    * @return color
217    */
218   const Vector4& GetCutPastePopupColor() const;
219
220   /**
221    * Set the Cut and Paste button color when pressed.
222    * @param[in] color color to use
223    */
224   void SetCutPastePopupPressedColor( const Vector4& color );
225
226   /**
227    * Get the Cut and Paste pressed button color.
228    * @return color
229    */
230   const Vector4& GetCutPastePopupPressedColor() const;
231
232   /**
233    * Set the border color of the popup
234    * @param[in] color required color
235    */
236   void SetCutPastePopupBorderColor( const Vector4& color );
237
238   /**
239    * Get the border color
240    * @return Vector4 the color of the border
241    */
242   const Vector4& GetCutPastePopupBorderColor() const;
243
244   /**
245    * Toggle if a popup button should be enabled (shown) or not
246    * @param[in]  requiredButton Button Id to enable or disable
247    * @param[in]  enable toggle to enable (true) or disable (false)
248    */
249   void TogglePopupButtonOnOff( TextInputPopup::Buttons requiredButton, bool enable );
250
251   /**
252    * Set the Button Priority Position
253    * @param[in] button Button id for priority to be set on
254    * @param[in] priority Priority level, 1 is highest so will appear first.  0 priority will not show the button.
255    */
256   void SetButtonPriorityPosition( TextInputPopup::Buttons button, unsigned int priority );
257
258   /**
259    * Set the icon color of the popup
260    * @param[in] color required color
261    */
262   void SetCutPastePopupIconColor( const Vector4& color );
263
264   /**
265    * Get the popup icon color
266    * @return Vector4 the color of the popup icon
267    */
268   const Vector4& GetCutPastePopupIconColor() const;
269
270   /**
271    * Set the pressed icon color of the popup
272    * @param[in] color required color
273    */
274   void SetCutPastePopupIconPressedColor( const Vector4& color );
275
276   /**
277    * Get the popup pressed icon color
278    * @return Vector4 the color of the popup pressed icon
279    */
280   const Vector4& GetCutPastePopupIconPressedColor();
281
282   /**
283    * Set the text color of the popup
284    * @param[in] color required color
285    */
286   void SetCutPastePopupTextColor( const Vector4& color );
287
288   /**
289    * Get the popup text color
290    * @return Vector4 the color of the popup text
291    */
292   const Vector4& GetCutPastePopupTextColor();
293
294   /**
295    * Set the pressed text color of the popup
296    * @param[in] color required color
297    */
298   void SetCutPastePopupTextPressedColor( const Vector4& color );
299
300   /**
301    * Get the popup pressed text color
302    * @return Vector4 the color of the popup pressed text
303    */
304   const Vector4& GetCutPastePopupTextPressedColor();
305
306   /**
307    * Get the Button Priority Position
308    * @param[in] button Button id to get priority of
309    * @return the button priority, 1 is highest, 0 is not shown.
310    */
311   unsigned int GetButtonPriorityPosition( TextInputPopup::Buttons button ) const;
312
313   /**
314    * Adds Popup options which have been enabled.
315    */
316   void AddPopupOptions();
317
318   /**
319    * Get Visible size of the Popup, excludes content that needs scrolling
320    * @return Vector3 size of Popup
321    */
322   const Vector3& GetVisibileSize() const;
323
324   /**
325    * Sets the positon of the PopUp tail relative to TextInput
326    * @param[in] position Position to set
327    * @param[in] yAxisFlip If tail should be flipped in y axis
328    */
329   void SetTailPosition( const Vector3& position, const bool yAxisFlip );
330
331 private:
332
333   /**
334    * Creates a Button with the required parameters.
335    * @param[in] buttonId enum representing the button
336    * @param[in] orderOfPriority Position in toolbar button should be position, 1 is first from left to right.
337    * @param[in] name Given name for Button actor
338    * @param[in] caption Text to display in button
339    * @param[in] iconImage Icon to display in button
340    * @param[in] enabled Toggle if button should be used or not, this is decided by the current state/conditions.
341    */
342   TextInputPopup::ButtonRequirement CreateRequiredButton( TextInputPopup::Buttons buttonId, std::size_t orderOfPriority,
343                                                                           const std::string& name, const std::string& caption, Image iconImage, bool enabled );
344
345   /**
346    * @brief Adds popup to the given parent
347    * @paran[in] parent target to add Popup to
348    */
349   void AddToParent( Actor parent );
350
351   /**
352    * @brief Removes Popup from Parent
353    */
354   void RemoveFromParent();
355
356   /**
357    * Applies constraint to keep Popup in view within the desired area.
358    */
359   void ApplyConfinementConstraint();
360
361   /**
362    * Applies constraint to keep the Tail attached to Popup
363    */
364   void ApplyTailConstraint();
365
366   /**
367    * Create Layer to be used with stencil to allow scrolling of buttons which do not fit in visible popup
368    * @param[in] size of the layer.
369    */
370   void CreateLayer( const Vector2& size );
371
372   /**
373    * Create a stencil to clip the scroll view content
374    * @param[in] size of the stencil.
375    */
376   void CreateStencil( const Vector2& size );
377
378   /**
379    * Popup has started to scroll
380    * @param[in] position current scroll view position
381    */
382   void OnScrollStarted( const Vector3& position );
383
384   /**
385    * Popup has stopped scrolling
386    * @param[in] position current scroll view position
387    */
388   void OnScrollCompleted( const Vector3& position );
389
390   /**
391    * Create a scroll view to hold the popup buttons and allow scrolling if too many buttons to fit within the visible boundary
392    */
393   void CreateScrollView();
394
395   /**
396    * Set the scroll view size and ruler.
397    * @param[in] visibleSize size of the visible scroll view
398    */
399   void UpdateScrollViewRulerAndSize( const Vector2& visibleSize );
400
401   /**
402    * Called when a button is pressed in the Popup
403    * @param[in] button The button pressed.
404    */
405   bool OnButtonPressed( Toolkit::Button button );
406
407   /**
408    * Invoked upon popup Hide animation completing.
409    * @note Only called for animating hide, not called for instantaneous (animate = false)
410    * @param[in] source The animation which completed.
411    */
412   void OnHideFinished(Animation& source);
413
414   /**
415    * Invoked upon popup Show animation completing.
416    * @note Only called for animating show, not called for instantaneous (animate = false)
417    * @param[in] source The animation which completed.
418    */
419   void OnShowFinished(Animation& source);
420
421 private:
422
423   State mState;                                       ///< Popup State.
424   Layer mRoot;                                        ///< The actor which all popup content is added to (i.e. panel and buttons)
425   Actor mButtons;                                     ///< Actor which holds all the buttons, sensitivity can be set on all buttons via this actor
426   ImageActor mBackground;                             ///< The background popup panel
427   ImageActor mBackgroundEffect;                       ///< The background effect
428   ImageActor mBackgroundLine;                         ///< The background line
429   ImageActor mTail;                                   ///< The tail for the popup
430   ImageActor mTailEffect;                             ///< the tail effect
431   ImageActor mTailLine;                               ///< The border/outline around the tail
432
433   Vector3 mVisiblePopUpSize;                          ///< Visible Size of Popup excluding content that needs scrolling.
434   float mPopupTailXPosition;                          ///< X position of PopUp tail.
435
436   Vector2 mContentSize;                               ///< Size of Content (i.e. Buttons)
437   ActorContainer mDividerContainer;                   ///< List of dividers added to popup.
438   Animation mAnimation;                               ///< Popup Hide/Show animation.
439
440   Layer mLayer;                                       ///< Layer to be used with Stencil
441   Actor mStencil;                                     ///< Stencil to clip scrollview
442   Toolkit::ScrollView mScrollView;                    ///< Scrollview to house the popup
443
444   std::vector<ButtonRequirement> mOrderListOfButtons;        // List of buttons in the order to be displayed and a flag to indicate if needed.
445
446   Vector4 mBackgroundColor;              // Color of the background of the text input popup
447   Vector4 mBackgroundPressedColor;       // Color of the option background.
448   Vector4 mLineColor;                    // Color of the line around the text input popup
449   Vector4 mIconColor;                    // Color of the popup icon.
450   Vector4 mIconPressedColor;             // Color of the popup icon when pressed.
451   Vector4 mTextColor;                    // Color of the popup text.
452   Vector4 mTextPressedColor;             // Color of the popup text when pressed.
453
454   // Priority of Options/Buttons in the Cut and Paste pop-up, higher priority buttons are displayed first, left to right.
455   std::size_t mSelectOptionPriority;  // Position of Select Button
456   std::size_t mSelectAllOptionPriority; // Position of Select All button
457   std::size_t mCutOptionPriority; // Position of Cut button
458   std::size_t mCopyOptionPriority; // Position of Copy button
459   std::size_t mPasteOptionPriority;  // Position of Paste button
460   std::size_t mClipboardOptionPriority;  // Position of Clipboard button
461
462   PressedSignalV2 mPressedSignal;              ///< Signal emitted when a button within the popup is pressed.
463   HideFinishedSignalV2 mHideFinishedSignal;    ///< Signal emitted when popup is completely hidden
464   ShowFinishedSignalV2 mShowFinishedSignal;    ///< Signal emitted when popup is completely shown
465
466 };
467
468 } // namespace Internal
469
470 } // namespace Toolkit
471
472 } // namespace Dali
473
474 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__