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