Reposition the grab/selection handles position.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.h
1 #ifndef __DALI_TOOLKIT_TEXT_CONTROLLER_H__
2 #define __DALI_TOOLKIT_TEXT_CONTROLLER_H__
3
4 /*
5  * Copyright (c) 2015 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/devel-api/adaptor-framework/imf-manager.h>
23 #include <dali/public-api/events/gesture.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup-callback-interface.h>
27 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
28 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
29 #include <dali-toolkit/internal/text/text-control-interface.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Text
38 {
39
40 class Controller;
41 class View;
42
43 typedef IntrusivePtr<Controller> ControllerPtr;
44 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
45
46 /**
47  * @brief Different placeholder-text can be shown when the control is active/inactive.
48  */
49 enum PlaceholderType
50 {
51   PLACEHOLDER_TYPE_ACTIVE,
52   PLACEHOLDER_TYPE_INACTIVE,
53 };
54
55 /**
56  * @brief A Text Controller is used by UI Controls which display text.
57  *
58  * It manipulates the Logical & Visual text models on behalf of the UI Controls.
59  * It provides a view of the text that can be used by rendering back-ends.
60  *
61  * For selectable/editable UI controls, the controller handles input events from the UI control
62  * and decorations (grab handles etc) via the Decorator::ControllerInterface interface.
63  *
64  * The text selection popup button callbacks are as well handled via the TextSelectionPopupCallbackInterface interface.
65  */
66 class Controller : public RefObject, public Decorator::ControllerInterface, public TextSelectionPopupCallbackInterface
67 {
68 public:
69
70   /**
71    * @brief Text related operations to be done in the relayout process.
72    */
73   enum OperationsMask
74   {
75     NO_OPERATION       = 0x0000,
76     CONVERT_TO_UTF32   = 0x0001,
77     GET_SCRIPTS        = 0x0002,
78     VALIDATE_FONTS     = 0x0004,
79     GET_LINE_BREAKS    = 0x0008,
80     GET_WORD_BREAKS    = 0x0010,
81     BIDI_INFO          = 0x0020,
82     SHAPE_TEXT         = 0x0040,
83     GET_GLYPH_METRICS  = 0x0080,
84     LAYOUT             = 0x0100,
85     UPDATE_ACTUAL_SIZE = 0x0200,
86     REORDER            = 0x0400,
87     ALIGN              = 0x0800,
88     ALL_OPERATIONS     = 0xFFFF
89   };
90
91   /**
92    * @brief Used to distinguish between regular key events and IMF events
93    */
94   enum InsertType
95   {
96     COMMIT,
97     PRE_EDIT
98   };
99
100   /**
101    * @brief Create a new instance of a Controller.
102    *
103    * @param[in] controlInterface An interface used to request a text relayout.
104    * @return A pointer to a new Controller.
105    */
106   static ControllerPtr New( ControlInterface& controlInterface );
107
108   /**
109    * @brief Called to enable text input.
110    *
111    * @note Selectable or editable controls should call this once after Controller::New().
112    * @param[in] decorator Used to create cursor, selection handle decorations etc.
113    */
114   void EnableTextInput( DecoratorPtr decorator );
115
116   /**
117    * @brief Replaces any text previously set.
118    *
119    * @note This will be converted into UTF-32 when stored in the text model.
120    * @param[in] text A string of UTF-8 characters.
121    */
122   void SetText( const std::string& text );
123
124   /**
125    * @brief Retrieve any text previously set.
126    *
127    * @return A string of UTF-8 characters.
128    */
129   void GetText( std::string& text ) const;
130
131   /**
132    * @brief Remove a given number of characters
133    *
134    * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
135    * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
136    * @return True if the remove was successful.
137    */
138   bool RemoveText( int cursorOffset, int numberOfChars );
139
140   /**
141    * @brief Retrieve the current cursor position.
142    *
143    * @return The cursor position.
144    */
145   unsigned int GetLogicalCursorPosition() const;
146
147   /**
148    * @brief Replaces any placeholder text previously set.
149    *
150    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
151    * @param[in] text A string of UTF-8 characters.
152    */
153   void SetPlaceholderText( PlaceholderType type, const std::string& text );
154
155   /**
156    * @brief Retrieve any placeholder text previously set.
157    *
158    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
159    * @param[out] A string of UTF-8 characters.
160    */
161   void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
162
163   /**
164    * @brief Sets the maximum number of characters that can be inserted into the TextModel
165    *
166    * @param[in] maxCharacters maximum number of characters to be accepted
167    */
168   void SetMaximumNumberOfCharacters( int maxCharacters );
169
170   /**
171    * @brief Sets the maximum number of characters that can be inserted into the TextModel
172    *
173    * @param[in] maxCharacters maximum number of characters to be accepted
174    */
175   int GetMaximumNumberOfCharacters();
176
177   /**
178    * @brief Set the default font family.
179    *
180    * @param[in] defaultFontFamily The default font family.
181    */
182   void SetDefaultFontFamily( const std::string& defaultFontFamily );
183
184   /**
185    * @brief Retrieve the default font family.
186    *
187    * @return The default font family.
188    */
189   const std::string& GetDefaultFontFamily() const;
190
191   /**
192    * @brief Sets the font's style string.
193    *
194    * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
195    *
196    * @param[in] style The font's style string.
197    */
198   void SetDefaultFontStyle( const std::string& style );
199
200   /**
201    * @brief Retrieves the font's style.
202    *
203    * @return The font's style.
204    */
205   const std::string& GetDefaultFontStyle() const;
206
207   /**
208    * @brief Sets the default font width.
209    *
210    * @param[in] width The font width.
211    */
212   void SetDefaultFontWidth( FontWidth width );
213
214   /**
215    * @brief Retrieves the default font width.
216    *
217    * @return The default font width.
218    */
219   FontWidth GetDefaultFontWidth() const;
220
221   /**
222    * @brief Sets the default font weight.
223    *
224    * @param[in] weight The font weight.
225    */
226   void SetDefaultFontWeight( FontWeight weight );
227
228   /**
229    * @brief Retrieves the default font weight.
230    *
231    * @return The default font weight.
232    */
233   FontWeight GetDefaultFontWeight() const;
234
235   /**
236    * @brief Sets the default font slant.
237    *
238    * @param[in] slant The font slant.
239    */
240   void SetDefaultFontSlant( FontSlant slant );
241
242   /**
243    * @brief Retrieves the default font slant.
244    *
245    * @return The default font slant.
246    */
247   FontSlant GetDefaultFontSlant() const;
248
249   /**
250    * @brief Set the default point size.
251    *
252    * @param[in] pointSize The default point size.
253    */
254   void SetDefaultPointSize( float pointSize );
255
256   /**
257    * @brief Retrieve the default point size.
258    *
259    * @return The default point size.
260    */
261   float GetDefaultPointSize() const;
262
263   /**
264    * @ brief Update the text after a font change
265    * @param[in] newDefaultFont The new font to change to
266    */
267   void UpdateAfterFontChange( std::string& newDefaultFont );
268
269   /**
270    * @brief Set the text color
271    *
272    * @param textColor The text color
273    */
274   void SetTextColor( const Vector4& textColor );
275
276   /**
277    * @brief Retrieve the text color
278    *
279    * @return The text color
280    */
281   const Vector4& GetTextColor() const;
282
283   /**
284    * @brief Set the text color
285    *
286    * @param textColor The text color
287    */
288   void SetPlaceholderTextColor( const Vector4& textColor );
289
290   /**
291    * @brief Retrieve the text color
292    *
293    * @return The text color
294    */
295   const Vector4& GetPlaceholderTextColor() const;
296
297   /**
298    * @brief Set the shadow offset.
299    *
300    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
301    */
302   void SetShadowOffset( const Vector2& shadowOffset );
303
304   /**
305    * @brief Retrieve the shadow offset.
306    *
307    * @return The shadow offset.
308    */
309   const Vector2& GetShadowOffset() const;
310
311   /**
312    * @brief Set the shadow color.
313    *
314    * @param[in] shadowColor The shadow color.
315    */
316   void SetShadowColor( const Vector4& shadowColor );
317
318   /**
319    * @brief Retrieve the shadow color.
320    *
321    * @return The shadow color.
322    */
323   const Vector4& GetShadowColor() const;
324
325   /**
326    * @brief Set the underline color.
327    *
328    * @param[in] color color of underline.
329    */
330   void SetUnderlineColor( const Vector4& color );
331
332   /**
333    * @brief Retrieve the underline color.
334    *
335    * @return The underline color.
336    */
337   const Vector4& GetUnderlineColor() const;
338
339   /**
340    * @brief Set the underline enabled flag.
341    *
342    * @param[in] enabled The underline enabled flag.
343    */
344   void SetUnderlineEnabled( bool enabled );
345
346   /**
347    * @brief Returns whether the text is underlined or not.
348    *
349    * @return The underline state.
350    */
351   bool IsUnderlineEnabled() const;
352
353   /**
354    * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
355    *
356    * @param[in] height The height in pixels of the underline
357    */
358   void SetUnderlineHeight( float height );
359
360   /**
361    * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
362    *
363    * @return The height of the underline, or 0 if height is not overrided.
364    */
365   float GetUnderlineHeight() const;
366
367   /**
368    * @brief Called to enable/disable cursor blink.
369    *
370    * @note Only editable controls should calls this.
371    * @param[in] enabled Whether the cursor should blink or not.
372    */
373   void SetEnableCursorBlink( bool enable );
374
375   /**
376    * @brief Query whether cursor blink is enabled.
377    *
378    * @return Whether the cursor should blink or not.
379    */
380   bool GetEnableCursorBlink() const;
381
382   /**
383    * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
384    *
385    * @return The scroll position.
386    */
387   const Vector2& GetScrollPosition() const;
388
389   /**
390    * @brief Query the alignment offset.
391    *
392    * @return The alignmnet offset.
393    */
394   const Vector2& GetAlignmentOffset() const;
395
396   /**
397    * @copydoc Control::GetNaturalSize()
398    */
399   Vector3 GetNaturalSize();
400
401   /**
402    * @copydoc Control::GetHeightForWidth()
403    */
404   float GetHeightForWidth( float width );
405
406   /**
407    * @brief Triggers a relayout which updates View (if necessary).
408    *
409    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
410    * @param[in] size A the size of a bounding box to layout text within.
411    * @return True if the text model or decorations were updated.
412    */
413   bool Relayout( const Size& size );
414
415   /**
416    * @brief Process queued events which modify the model.
417    */
418   void ProcessModifyEvents();
419
420   /**
421    * @brief Used to remove placeholder text.
422    */
423   void ResetText();
424
425   /**
426    * @brief Used to reset the cursor position after setting a new text.
427    *
428    * @param[in] cursorIndex Where to place the cursor.
429    */
430   void ResetCursorPosition( CharacterIndex cursorIndex );
431
432   /**
433    * @brief Used to reset the scroll position after setting a new text.
434    */
435   void ResetScrollPosition();
436
437   /**
438    * @brief Used to process an event queued from SetText()
439    */
440   void TextReplacedEvent();
441
442   /**
443    * @brief Used to process an event queued from key events etc.
444    */
445   void TextInsertedEvent();
446
447   /**
448    * @brief Used to process an event queued from backspace key etc.
449    */
450   void TextDeletedEvent();
451
452   /**
453    * @brief Lays-out the text.
454    *
455    * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
456    *
457    * @param[in] size A the size of a bounding box to layout text within.
458    * @param[in] operations The layout operations which need to be done.
459    * @param[out] layoutSize The size of the laid-out text.
460    */
461   bool DoRelayout( const Size& size,
462                    OperationsMask operations,
463                    Size& layoutSize );
464
465   /**
466    * @brief Whether to enable the multi-line layout.
467    *
468    * @param[in] enable \e true enables the multi-line (by default)
469    */
470   void SetMultiLineEnabled( bool enable );
471
472   /**
473    * @return Whether the multi-line layout is enabled.
474    */
475   bool IsMultiLineEnabled() const;
476
477   /**
478    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
479    */
480   void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
481
482   /**
483    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
484    */
485   LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
486
487   /**
488    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
489    */
490   void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
491
492   /**
493    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
494    */
495   LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
496
497   /**
498    * @brief Calulates the alignment of the whole text inside the bounding box.
499    *
500    * @param[in] size The size of the bounding box.
501    */
502   void CalculateTextAlignment( const Size& size );
503
504   /**
505    * @brief Return the layout engine.
506    *
507    * @return A reference to the layout engine.
508    */
509   LayoutEngine& GetLayoutEngine();
510
511   /**
512    * @brief Return a view of the text.
513    *
514    * @return A reference to the view.
515    */
516   View& GetView();
517
518   // Text-input Event Queuing
519
520   /**
521    * @brief Called by editable UI controls when keyboard focus is gained.
522    */
523   void KeyboardFocusGainEvent();
524
525   /**
526    * @brief Called by editable UI controls when focus is lost.
527    */
528   void KeyboardFocusLostEvent();
529
530   /**
531    * @brief Called by editable UI controls when key events are received.
532    *
533    * @param[in] event The key event.
534    * @param[in] type Used to distinguish between regular key events and IMF events.
535    */
536   bool KeyEvent( const Dali::KeyEvent& event );
537
538   /**
539    * @brief Called by editable UI controls when key events are received.
540    *
541    * @param[in] text The text to insert.
542    * @param[in] type Used to distinguish between regular key events and IMF events.
543    */
544   void InsertText( const std::string& text, InsertType type );
545
546   /**
547    * @brief Checks if text is selected and if so removes it.
548    * @return true if text was removed
549    */
550   bool RemoveSelectedText();
551
552   /**
553    * @brief Called by editable UI controls when a tap gesture occurs.
554    * @param[in] tapCount The number of taps.
555    * @param[in] x The x position relative to the top-left of the parent control.
556    * @param[in] y The y position relative to the top-left of the parent control.
557    */
558   void TapEvent( unsigned int tapCount, float x, float y );
559
560   /**
561    * @brief Called by editable UI controls when a pan gesture occurs.
562    *
563    * @param[in] state The state of the gesture.
564    * @param[in] displacement This distance panned since the last pan gesture.
565    */
566   void PanEvent( Gesture::State state, const Vector2& displacement );
567
568   /**
569    * @brief Called by editable UI controls when a long press gesture occurs.
570    *
571    * @param[in] state The state of the gesture.
572    * @param[in] x The x position relative to the top-left of the parent control.
573    * @param[in] y The y position relative to the top-left of the parent control.
574    */
575   void LongPressEvent( Gesture::State state, float x, float y );
576
577   /**
578    * @brief Creates a selection event.
579    *
580    * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
581    *
582    * @param[in] x The x position relative to the top-left of the parent control.
583    * @param[in] y The y position relative to the top-left of the parent control.
584    * @param[in] selectAll Whether the whole text is selected.
585    */
586   void SelectEvent( float x, float y, bool selectAll );
587
588   /**
589    * @brief Event received from IMF manager
590    *
591    * @param[in] imfManager The IMF manager.
592    * @param[in] imfEvent The event received.
593    * @return A data struture indicating if update is needed, cursor position and current text.
594    */
595   ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
596
597   /**
598    * @brief Paste given string into Text model
599    * @param[in] stringToPaste this string will be inserted into the text model
600    */
601   void PasteText( const std::string& stringToPaste );
602
603   /**
604    * @brief Event from Clipboard notifying an Item has been selected for pasting
605    */
606   void PasteClipboardItemEvent();
607
608   /**
609    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
610    */
611   virtual void GetTargetSize( Vector2& targetSize );
612
613   /**
614    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
615    */
616   virtual void AddDecoration( Actor& actor, bool needsClipping );
617
618   /**
619    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
620    */
621   virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
622
623   /**
624    * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
625    */
626   virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
627
628 protected:
629
630   /**
631    * @brief A reference counted object may only be deleted by calling Unreference().
632    */
633   virtual ~Controller();
634
635 private:
636
637   /**
638    * @brief Helper to KeyEvent() to handle the backspace case.
639    *
640    * @return True if a character was deleted.
641    */
642   bool BackspaceKeyEvent();
643
644   /**
645    * @brief Helper to notify IMF manager with surrounding text & cursor changes.
646    */
647   void NotifyImfManager();
648
649   /**
650    * @brief Helper to clear font-specific data.
651    */
652   void ShowPlaceholderText();
653
654   /**
655    * @brief Helper to clear all the model data except for LogicalModel::mText.
656    */
657   void ClearModelData();
658
659   /**
660    * @brief Helper to clear font-specific data (only).
661    */
662   void ClearFontData();
663
664   /**
665    * @brief Private constructor.
666    */
667   Controller( ControlInterface& controlInterface );
668
669   // Undefined
670   Controller( const Controller& handle );
671
672   // Undefined
673   Controller& operator=( const Controller& handle );
674
675 private:
676
677   struct Impl;
678   Impl* mImpl;
679 };
680
681 } // namespace Text
682
683 } // namespace Toolkit
684
685 } // namespace Dali
686
687 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__