Fix boost includes
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / text-input / text-input-decorator-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_TEXT_INPUT_DECORATOR_H__
2 #define __DALI_TOOLKIT_INTERNAL_TEXT_INPUT_DECORATOR_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <dali/dali.h>
21
22 // INTERNAL INCLUDES
23
24 #include <dali-toolkit/internal/controls/text-input/textview-character-positions-impl.h>
25 #include <dali-toolkit/internal/controls/text-input/text-input-handles-impl.h>
26 #include <dali-toolkit/internal/controls/text-input/text-input-text-highlight-impl.h>
27 #include <dali-toolkit/internal/controls/text-input/text-input-popup-new-impl.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 class Decorator;
39
40 typedef IntrusivePtr<Decorator> DecoratorPtr;
41
42 /**
43  *  @brief Decorator Class
44  *
45  *  Decorations are Selection Handles, cursor, grab handle, magnifier the "cut copy paste" PopUp and Selection highlight.
46  *  The Decorator triggers creation of these decorations and positions them.
47  *  Decoration positions can be dependent on other decorations like the PopUp on the Selection handles.
48  *  The decorator maintains a Bounding Box which the decorations have to be positioned within, decorations can be flipped or hidden to obey this Bounding Box.
49  *  Scrolling of Text can effect positioning of decorations, the decorator repositions decorations in this case.
50  */
51
52 class Decorator : public ConnectionTracker
53 {
54
55 public:
56
57   /**
58    * @brief Constructor
59    *
60    * @param[in] textviewManager TextViewManager to be used
61    */
62   Decorator( TextViewCharacterPositioning& textviewManager, TextInputTextStyle& textStyle);
63
64   /**
65    * @brief Default destructor
66    */
67   ~Decorator();
68
69   /**
70    * @brief Set the dimensions of the bounding rectangle for decorations to obey.
71    *
72    * @param[in] boundingRectangle
73    */
74   void SetBoundingBox( const Rect<float>& boundingRectangle );
75
76   /**
77    * @brief Get the bounding dimensions of the bounding box
78    *
79    * @return dimensions of the bounding box from world origin. (x, y, w, z )
80    *
81    *  -----------------
82    * |        ^        |
83    * |        |        |
84    * |        y        |
85    * |        |        |
86    * |        v        |
87    * |<--x--> o <--z-->|
88    * |        ^        |
89    * |        |        |
90    * |        w        |
91    * |        |        |
92    * |        v        |
93    *  -----------------
94    */
95   Vector4 GetBoundingBox() const;
96
97   /**
98    * @brief Callback when a handle is panned/moved, either selection handles or grab handle
99    *
100    * @param actor Handle of the selection or grab handle.
101    * @param gesture Data structure with the parameters of the gesture.
102    */
103   void OnHandlePan(Actor actor, PanGesture gesture);
104
105   // Selection Handles
106
107   /**
108    * @brief Create a left and right selection handle and parent both to the provided actor
109    * @param[in] parent actor in which the handles should be added to.
110    */
111   void CreateSelectionHandles( Actor parent );
112
113   /**
114    * @brief Remove selection handles from their parent
115    */
116   void RemoveSelectionHandles();
117
118   /**
119    * @brief Get size of Selection handles
120    *
121    * @return size of a selection handle
122    */
123   Vector3 GetSelectionHandleSize();
124
125   /**
126    * @brief Get position of Selection handle within text
127    *
128    * @return character position of a selection handle one
129    */
130   std::size_t GetHandleOnePosition() const;
131
132   /**
133    * @brief Get position of Selection handle within text
134    *
135    * @return character position of a selection handle two
136    */
137   std::size_t GetHandleTwoPosition() const;
138
139   /**
140    * @brief Position Selection a single handle at given positions within the text string.
141    *
142    * @param[in] selectionHandle handle to be positioned
143    * @param[in] position where to place handle
144    * @return Vector3 Position of handle as a coordinate.
145    */
146   Vector3 PositionSelectionHandle( Actor selectionHandle, std::size_t position );
147
148   /**
149    * @brief Position Selection a single handle at given coordinates
150    *
151    * @param[in] selectionHandle handle to be positioned
152    * @param[in] actualPosition coordinates to position handle
153    * @param[in] position where to place handle
154    * @return Vector3 Position of handle as a coordinate.
155    */
156   Vector3 PositionSelectionHandle( Actor selectionHandle, Vector3& actualPosition, std::size_t position );
157
158   /**
159    * @brief Make both selection handle visible or invisible
160    * @param[in] visible true to make visible, false to fine
161    */
162   void SetSelectionHandlesVisibility( bool visible );
163
164   /**
165    * @brief Position Selection handles at given positions within the text string.
166    *
167    * @param[in] start where to place first handle
168    * @param[in] end  where to place second handle
169    */
170   void PositionSelectionHandles( std::size_t start, std::size_t end );
171
172   /**
173    * @brief Move selection handle by the given displacement.
174    *
175    * @param[in] selectionHandle Actor to move
176    * @param[in] actualSelectionHandlePosition actual current position of the handle in x y z
177    * @param[in] currentSelectionHandlePosition current position along the string
178    * @param[in] displacement the x y displacement
179    */
180   Vector3 MoveSelectionHandle( Actor selectionHandle,
181                                Vector3& actualSelectionHandlePosition,
182                                std::size_t& currentSelectionHandlePosition,
183                                const Vector2& displacement );
184
185   /* Grab Handle */
186
187   /**
188    * @brief Position GrabHandlewith depending on the the character in the text it should be placed at
189    * @param[in] positonInText the character position within the text the handle should be at
190    */
191   void PositionGrabHandle( std::size_t positionInText );
192
193   /**
194    * @brief Move grab handle to the required position within the text
195    *
196    * @param[in] displacement Displacement of the grab handle in actor coordinates.
197    */
198   void MoveGrabHandle( const Vector2& displacement );
199
200   /**
201    * @brief Show or hide the GrabHandle is visibility is true
202    *
203    * @param[in] visible flag to show or not show the grab handle
204    */
205   void ShowGrabHandle( bool visible );
206
207   /**
208    * @brief Create the GrabHandle used to position cursor
209    * @param[in] targetParent the Actor to parent the GrabHandle
210    */
211   void CreateGrabHandle( Actor targetParent );
212
213   /**
214    * @brief Set the image to be used as the cursor grab hander
215    * @pre The text input actor has been initialised.
216    * @param[in] image The image to be used.
217    */
218   void SetGrabHandleImage( Image image );
219
220   /**
221    * @brief Toggle to enable the grab handle, used to position cursor when magnifier not being used.
222    * Default behaviour is to use the magnifier to position the cursor, enabling this prevents the magnifier from being shown.
223    * @param[in] toggle true to enable, false to disable grab handle
224    */
225   void EnableGrabHandle(bool toggle);
226
227   /**
228    * @brief Method to check if grab handle is enabled, if false then the magnifier will be used to position cursor.
229    * @return bool returns true is grab handle enabled.
230    */
231   bool IsGrabHandleEnabled();
232
233   /* Cursor */
234
235   /**
236    * @brief Get the current Cursor position
237    * @return current cursor position
238    */
239   std::size_t GetCurrentCursorPosition() const;
240
241   /**
242    * @brief Set the Cursor position
243    * @param[in] the position the cursor should be set to
244    */
245   void SetCurrentCursorPosition( std::size_t newCursorPosition );
246
247   /**
248    * @brief Set if the cursors are visible or not.
249    * @param[in] visible flag true for visible
250    */
251   void SetCursorVisibility( bool visible );
252
253   /**
254    * @brief Display cursor
255    * @param[in] nthChar position in text string to display cursor
256    */
257   void DrawCursor( const std::size_t nthChar = 0 );
258
259   /**
260    * Sets alternate cursor enable state
261    * @see SetCursorVisibility
262    * alternate cursor will only be visible if both SetCursorVisiblity
263    * and cursor enabled have been set to true.
264    */
265   void SetAltCursorEnabled( bool enabled );
266
267   /**
268    * @brief Set the image to be used for the regular left to right cursor
269    * @pre The text input actor has been initialised.
270    * @param[in] image The image to be used.
271    * @param[in] border The nine patch border for the image.
272    */
273   void SetCursorImage( Image image, const Vector4& border );
274
275   /**
276    * @brief Set the image to be used for the Right to Left cursor
277    * @pre The text input actor has been initialised.
278    * @param[in] image The image to be used.
279    * @param[in] border The nine patch border for the image.
280    */
281   void SetRTLCursorImage( Image image, const Vector4& border );
282
283   /**
284    * @brief Creates a cursor from the supplied image and nine patch border.
285    * @param[in] cursorImage the image to be used for the cursor.
286    * @param[in] border the nine patch border corresponding to the supplied image.
287    * @paran[in] cursorName actor name for cursor
288    * @return the image actor to be used as the cursor.
289    */
290   ImageActor CreateCursor( Image cursorImage, const Vector4& border,  const std::string& cursorName );
291
292   /**
293    * @brief Creates a regular and Right-To-Left cursor and parents them to give target Actor
294    * @param[in] targetParent target Actor
295    */
296   void CreateCursors( Actor targetParent );
297
298   /**
299    * @Brief Returns the cursor size at a given position in the text.
300    * @return Size the size of the cursor
301    */
302   Size GetCursorSizeAt( std::size_t positionWithinTextToGetCursorSize );
303
304   /**
305    * @brief  Start a timer to signal cursor to blink.
306    */
307   void StartCursorBlinkTimer();
308
309   /**
310    * @brief  Stop the timer signalling the cursor to blink.
311    */
312   void StopCursorBlinkTimer();
313
314   /**
315    * @brief Callback when handle timer ticks.
316    *
317    * Cursor should become visible/invisible to simulate blinking.
318    *
319    * @return True if the timer should be keep running.
320    */
321   bool OnCursorBlinkTimerTick();
322
323   /* Selection Highlight */
324
325   /**
326    * @brief Updates mesh data for selection highlight depending on handle positions and displays it.
327    */
328   void ShowUpdatedHighlight();
329
330   /**
331    * @brief Creates the Highlight used for selection
332    *
333    * @param[in] parent target actor in which the handles should be added to.
334    */
335   void CreateHighlight( Actor parent );
336
337   /**
338    * @brief Remove Highlight actor from it's parent
339    */
340   void RemoveHighlight();
341
342   /**
343    * @brief Set the visibility of the Highlight
344    *
345    * @param[in] visibility True to show and False to hide.
346    */
347   void HighlightVisibility( bool visiblility );
348
349   /* Boundary Property Notifications when handle exceed bounding box*/
350
351   /**
352    * @brief PropertyNotification Callback when left boundary exceeded so handle can be flipped.
353    *
354    * @param[in] source PropertyNotification
355    */
356   void OnLeftBoundaryExceeded( PropertyNotification& source );
357   /**
358    * @brief PropertyNotification Callback when within left boundary so handle can be flipped back.
359    *
360    * @param[in] source PropertyNotification
361    */
362   void OnReturnToLeftBoundary( PropertyNotification& source );
363   /**
364    * @brief PropertyNotification Callback when right boundary exceeded so handle can be flipped.
365    *
366    * @param[in] source PropertyNotification
367    */
368   void OnRightBoundaryExceeded( PropertyNotification& source );
369   /**
370    * @brief PropertyNotification Callback when within right boundary so handle can be flipped back.
371    *
372    * @param[in] source PropertyNotification
373    */
374   void OnReturnToRightBoundary( PropertyNotification& source );
375
376   /**
377    * @brief PropertyNotification Callbacks for hiding handle one when it exceeds boundary.
378    *
379    * @param[in] source PropertyNotification
380    */
381   void OnHandleOneLeavesBoundary( PropertyNotification& source );
382   /**
383    * @brief PropertyNotification Callbacks for showing hidden handle one when returns within boundary
384    *
385    * @param[in] source PropertyNotification
386    */
387   void OnHandleOneWithinBoundary( PropertyNotification& source );
388   /**
389    * @brief PropertyNotification Callbacks for hiding handle two it  when exceeds boundary.
390    *
391    * @param[in] source PropertyNotification
392    */
393   void OnHandleTwoLeavesBoundary( PropertyNotification& source );
394   /**
395    * @brief PropertyNotification Callbacks for showing hidden handle two when returns within boundary
396    *
397    * @param[in] source PropertyNotification
398    */
399   void OnHandleTwoWithinBoundary( PropertyNotification& source );
400
401   /**
402    * @brief Set up property notifications on the position of the handles to facilitate flipping and hiding when at screen boundary.
403    */
404   void SetUpHandlePropertyNotifications();
405
406   // Cut & Paste Pop-up
407
408   /**
409    * @brief Calculate positioning of PopUp relative to handles
410    * @return Actual position of PopUp
411    */
412   Vector3 PositionOfPopUpRelativeToSelectionHandles( );
413
414   /**
415    * @brief Calculate alternative position of PopUp relative to handles  when can it not be displayed in the default upper position.
416    * @return Actual position of PopUp
417    */
418   Vector3 AlternatePopUpPositionRelativeToSelectionHandles();
419
420   /**
421    * @brief Calculate positioning of PopUp relative to cursor
422    * @return Actual position of PopUp
423    */
424   Vector3 PositionOfPopUpRelativeToCursor();
425
426   /**
427    * @brief Calculate alternative position of PopUp relative to cursor when can not be displayed in normal upper position.
428    * @return Actual position of PopUp
429    */
430   Vector3 AlternatePopUpPositionRelativeToCursor();
431
432   /**
433    * @brief Calculate positioning of PopUp relative to GrabHandle
434    * @return Actual position of PopUp
435    */
436   Vector3 PositionOfPopUpRelativeToGrabHandle();
437
438  /**
439   * @brief Show the PopUp in the provided target
440   * @param[in] target target actor in which the PopUp should be added to.
441   */
442   void ShowPopUp( Actor target );
443
444   /**
445    * @brief Show PopUp in previously set Target.
446    * @pre Must have previously called ShopPopUp( Actor target ) otherwise PopUp will not be shown.
447    */
448   void ShowPopUp();
449
450   /**
451    * @brief Create and Show Cut Copy Paste PopUp
452    */
453   void ShowPopupCutCopyPaste();
454
455   /**
456    * @brief Hide PopUp
457    * @param[in] animate Animate or just hide instantly, default is true
458    * @param[in] signalFinished Signal when finished, default is true
459    */
460   void HidePopUp( bool animate=true, bool signalFinished=true );
461
462   /**
463    * @brief Adds a popup option.
464    * @brief Creates popup frame if not already created.
465    * @param[in] name The unique name for this option.
466    * @param[in] caption The caption (label) for this option
467    * @param[in] icon the image icon to be displayed for this option
468    * @param[in] finalOption Flag to indicate that this is the final option.
469    * (set to true on the last option you add)
470    */
471   void AddPopupOption(const std::string& name, const std::string& caption, const Image icon, bool finalOption = false);
472
473   /**
474    * @brief Removes popup, and its options.
475    */
476   void ClearPopup();
477
478   /**
479    * @brief PropertyNotification Callbacks for flipping PopUp when exceeds boundary.
480    * @param[in] source PropertyNotification
481    */
482   void PopUpLeavesVerticalBoundary( PropertyNotification& source );
483
484   /**
485    * @brief Setup position notifications when PopUp exceeds boundary
486    */
487   void SetUpPopUpPositionNotifications( );
488
489   /**
490    * @brief Callback for when a button is pressed in popup panel
491    * @param[in] button handle to the button pressed.
492    * @return bool consummation
493    */
494   bool OnPopupButtonPressed( Toolkit::Button button );
495
496   // Decoration positioning during scrolling
497
498   /**
499    * @brief Updates the position of the decorations when Text is scrolled.
500    *
501    * @param[in] textView Handle of the text-view.
502    * @param[in] scrollPosition The difference with the previous scroll position.
503    */
504   void TextViewScrolled( Toolkit::TextView textView, Vector2 scrollPosition );
505
506   /**
507    * @brief  Creates and starts a timer to scroll the text when handles are close to the edges of the text-input.
508    *
509    * It only starts the timer if it's already created.
510    */
511   void StartScrollTimer();
512
513   /**
514    * @brief  Stops the timer used to scroll the text.
515    */
516   void StopScrollTimer();
517
518   /**
519    * @brief Scroll Text according to handle position
520    * @param[in out] handlePosition handle position within character string
521    * @param[in] actual vector position of handle
522    * @return updated actual vector position of handle
523    */
524   Vector3 ScrollRelativeToHandle( std::size_t& handlePosition, Vector3& actualHandlePosition );
525
526   /**
527    * @brief Callback called by the timer used to scroll the text.
528    *
529    * It calculates and sets a new scroll position.
530    */
531   bool OnScrollTimerTick();
532
533   // Text Selection
534
535   /**
536    * @brief Function to get Text selected between the 2 selection handles.
537    * @return StyledTextArray an array of
538    */
539   MarkupProcessor::StyledTextArray GetSelectedText();
540
541 private:
542
543   /**
544    * @brief Copy Constructor
545    * @param[in] decorator
546    * Undefined/Hidden.
547    */
548   Decorator(const Decorator& decorator);
549
550   /**
551    * @Assignment Constructor
552    * @param[in] rhs
553    * Undefined/Hidden.
554    */
555   Decorator& operator=(const Decorator& rhs);
556
557 public:
558
559   typedef SignalV2< bool( Toolkit::Button ) > PressedSignal;
560   typedef SignalV2< void () > CursorPositionedSignal;
561   /**
562    * @brief Signal emitted when the button is touched.
563    * This is relayed from the PopUp class.  It enables the owner of the Decorator to act on the PopUp button press.
564    */
565   PressedSignal& PopUpButtonPressedSignal();
566
567   /**
568    * @brief Signal emitted when the cursor is repositioned
569    * @param[in] cursor the new cursor position
570    */
571   CursorPositionedSignal& CursorRePositionedSignal();
572
573 private:
574
575   Vector4 mBoundingRectangleWorldCoordinates;
576
577   TextViewCharacterPositioning& mTextViewCharacterPositioning;
578
579   TextInputHandles mTextInputHandles;
580
581   TextInputTextStyle& mTextStyle;
582
583   Vector3 mSelectionHandleOneActualPosition;    // Actual x y position of handle
584   Vector3 mSelectionHandleTwoActualPosition;    // Actual x y position of handle
585   std::size_t mSelectionHandleOnePosition;      // Position of handle along the string of text
586   std::size_t mSelectionHandleTwoPosition;      // Position of handle along the string of text
587
588   TextInputPopupNew mPopUpPanel;                // PopUp used for Cut Cpoy and Paste
589   Actor mPopUpTarget;                           // Target Actor to parent PopUp
590
591   Vector3 mActualGrabHandlePosition;            // Actual position of grab handle, this might not be snapped to a character
592   std::size_t mGrabHandlePosition;              // Position of grab handle along the string of text
593   Vector3 mCurrentHandlePosition;
594
595   std::size_t mCursorPosition;                  // Current cursor position within the text string
596   ImageActor mCursor;                           // Cursor overlayed on Text to show where new text will be inserted
597   ImageActor mCursorRTL;                        // Right To Left Cursor overlayed on Text (where new RTL text would be inserted)
598   Animation mCursorAnimation;                   // Animation for cursor blinking.
599   Timer mCursorBlinkTimer;                      // Timer to signal cursor to blink
600
601   Vector2 mScrollDisplacement;                  // How much to scroll by
602   Timer mScrollTimer;                           // Timer to scroll text over a period of time not all in one update.
603
604   TextHighlight mTextHighlight;                 // Holds data required to construct the highlight
605   MeshActor mHighlightMeshActor;                // Mesh Actor to display highlight
606
607   PanGestureDetector mPanGestureDetector;
608
609   PressedSignal mPopUpButtonPressedSignal;           // Signal emitted when a button within the popup is pressed.
610   CursorPositionedSignal mCursorRePositionedSignal;  // Signal emitted when a button when cursor position is changed.
611
612   bool mCursorBlinkStatus:1;                    // \e true shows the cursor, \e false hides it.
613   bool mCursorVisibility:1;                     // Should cursor be visible
614   bool mCursorRTLEnabled:1;                     // Enable state of Alternate RTL Cursor (need to keep track of this as it's not always enabled)
615   bool mIsGrabHandleInScrollArea:1;             // Whether the grab handle is inside the boundaries of the text-input.
616   bool mIsCursorInScrollArea:1;                 // Whether the cursor is inside the boundaries of the text-input.
617   bool mGrabHandleVisibility:1;                 // Should grab handle be visible
618   bool mGrabHandleEnabled:1;                    // Flag to enable the grab handle instead of the default magnifier.
619 };
620
621 } // namespace Internal
622
623 } // namespace Toolkit
624
625 } // namespace Dali
626
627 #endif // __DALI_TOOLKIT_INTERNAL_TEXT_INPUT_DECORATOR_H__
628
629