3c6125dd77694f13c813a34ef15f28cc7defa3b3
[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    * @param[in] userDefined If set by the user
182    */
183   void SetDefaultFontFamily( const std::string& defaultFontFamily, bool userDefined );
184
185   /**
186    * @brief Retrieve the default font family.
187    *
188    * @return The default font family.
189    */
190   const std::string& GetDefaultFontFamily() const;
191
192   /**
193    * @brief Sets the font's style string.
194    *
195    * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
196    *
197    * @param[in] style The font's style string.
198    */
199   void SetDefaultFontStyle( const std::string& style );
200
201   /**
202    * @brief Retrieves the font's style.
203    *
204    * @return The font's style.
205    */
206   const std::string& GetDefaultFontStyle() const;
207
208   /**
209    * @brief Sets the default font width.
210    *
211    * @param[in] width The font width.
212    */
213   void SetDefaultFontWidth( FontWidth width );
214
215   /**
216    * @brief Retrieves the default font width.
217    *
218    * @return The default font width.
219    */
220   FontWidth GetDefaultFontWidth() const;
221
222   /**
223    * @brief Sets the default font weight.
224    *
225    * @param[in] weight The font weight.
226    */
227   void SetDefaultFontWeight( FontWeight weight );
228
229   /**
230    * @brief Retrieves the default font weight.
231    *
232    * @return The default font weight.
233    */
234   FontWeight GetDefaultFontWeight() const;
235
236   /**
237    * @brief Sets the default font slant.
238    *
239    * @param[in] slant The font slant.
240    */
241   void SetDefaultFontSlant( FontSlant slant );
242
243   /**
244    * @brief Retrieves the default font slant.
245    *
246    * @return The default font slant.
247    */
248   FontSlant GetDefaultFontSlant() const;
249
250   /**
251    * @brief Set the default point size.
252    *
253    * @param[in] pointSize The default point size.
254    */
255   void SetDefaultPointSize( float pointSize );
256
257   /**
258    * @brief Retrieve the default point size.
259    *
260    * @return The default point size.
261    */
262   float GetDefaultPointSize() const;
263
264   /**
265    * @ brief Update the text after a font change
266    * @param[in] newDefaultFont The new font to change to
267    */
268   void UpdateAfterFontChange( std::string& newDefaultFont );
269
270   /**
271    * @brief Set the text color
272    *
273    * @param textColor The text color
274    */
275   void SetTextColor( const Vector4& textColor );
276
277   /**
278    * @brief Retrieve the text color
279    *
280    * @return The text color
281    */
282   const Vector4& GetTextColor() const;
283
284   /**
285    * @brief Set the text color
286    *
287    * @param textColor The text color
288    */
289   void SetPlaceholderTextColor( const Vector4& textColor );
290
291   /**
292    * @brief Retrieve the text color
293    *
294    * @return The text color
295    */
296   const Vector4& GetPlaceholderTextColor() const;
297
298   /**
299    * @brief Set the shadow offset.
300    *
301    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
302    */
303   void SetShadowOffset( const Vector2& shadowOffset );
304
305   /**
306    * @brief Retrieve the shadow offset.
307    *
308    * @return The shadow offset.
309    */
310   const Vector2& GetShadowOffset() const;
311
312   /**
313    * @brief Set the shadow color.
314    *
315    * @param[in] shadowColor The shadow color.
316    */
317   void SetShadowColor( const Vector4& shadowColor );
318
319   /**
320    * @brief Retrieve the shadow color.
321    *
322    * @return The shadow color.
323    */
324   const Vector4& GetShadowColor() const;
325
326   /**
327    * @brief Set the underline color.
328    *
329    * @param[in] color color of underline.
330    */
331   void SetUnderlineColor( const Vector4& color );
332
333   /**
334    * @brief Retrieve the underline color.
335    *
336    * @return The underline color.
337    */
338   const Vector4& GetUnderlineColor() const;
339
340   /**
341    * @brief Set the underline enabled flag.
342    *
343    * @param[in] enabled The underline enabled flag.
344    */
345   void SetUnderlineEnabled( bool enabled );
346
347   /**
348    * @brief Returns whether the text is underlined or not.
349    *
350    * @return The underline state.
351    */
352   bool IsUnderlineEnabled() const;
353
354   /**
355    * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
356    *
357    * @param[in] height The height in pixels of the underline
358    */
359   void SetUnderlineHeight( float height );
360
361   /**
362    * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
363    *
364    * @return The height of the underline, or 0 if height is not overrided.
365    */
366   float GetUnderlineHeight() const;
367
368   /**
369    * @brief Called to enable/disable cursor blink.
370    *
371    * @note Only editable controls should calls this.
372    * @param[in] enabled Whether the cursor should blink or not.
373    */
374   void SetEnableCursorBlink( bool enable );
375
376   /**
377    * @brief Query whether cursor blink is enabled.
378    *
379    * @return Whether the cursor should blink or not.
380    */
381   bool GetEnableCursorBlink() const;
382
383   /**
384    * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
385    *
386    * @return The scroll position.
387    */
388   const Vector2& GetScrollPosition() const;
389
390   /**
391    * @brief Query the alignment offset.
392    *
393    * @return The alignmnet offset.
394    */
395   const Vector2& GetAlignmentOffset() const;
396
397   /**
398    * @copydoc Control::GetNaturalSize()
399    */
400   Vector3 GetNaturalSize();
401
402   /**
403    * @copydoc Control::GetHeightForWidth()
404    */
405   float GetHeightForWidth( float width );
406
407   /**
408    * @brief Triggers a relayout which updates View (if necessary).
409    *
410    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
411    * @param[in] size A the size of a bounding box to layout text within.
412    * @return True if the text model or decorations were updated.
413    */
414   bool Relayout( const Size& size );
415
416   /**
417    * @brief Process queued events which modify the model.
418    */
419   void ProcessModifyEvents();
420
421   /**
422    * @brief Used to remove placeholder text.
423    */
424   void ResetText();
425
426   /**
427    * @brief Used to reset the cursor position after setting a new text.
428    *
429    * @param[in] cursorIndex Where to place the cursor.
430    */
431   void ResetCursorPosition( CharacterIndex cursorIndex );
432
433   /**
434    * @brief Used to reset the scroll position after setting a new text.
435    */
436   void ResetScrollPosition();
437
438   /**
439    * @brief Used to process an event queued from SetText()
440    */
441   void TextReplacedEvent();
442
443   /**
444    * @brief Used to process an event queued from key events etc.
445    */
446   void TextInsertedEvent();
447
448   /**
449    * @brief Used to process an event queued from backspace key etc.
450    */
451   void TextDeletedEvent();
452
453   /**
454    * @brief Lays-out the text.
455    *
456    * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
457    *
458    * @param[in] size A the size of a bounding box to layout text within.
459    * @param[in] operations The layout operations which need to be done.
460    * @param[out] layoutSize The size of the laid-out text.
461    */
462   bool DoRelayout( const Size& size,
463                    OperationsMask operations,
464                    Size& layoutSize );
465
466   /**
467    * @brief Whether to enable the multi-line layout.
468    *
469    * @param[in] enable \e true enables the multi-line (by default)
470    */
471   void SetMultiLineEnabled( bool enable );
472
473   /**
474    * @return Whether the multi-line layout is enabled.
475    */
476   bool IsMultiLineEnabled() const;
477
478   /**
479    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
480    */
481   void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
482
483   /**
484    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
485    */
486   LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
487
488   /**
489    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
490    */
491   void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
492
493   /**
494    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
495    */
496   LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
497
498   /**
499    * @brief Calulates the alignment of the whole text inside the bounding box.
500    *
501    * @param[in] size The size of the bounding box.
502    */
503   void CalculateTextAlignment( const Size& size );
504
505   /**
506    * @brief Return the layout engine.
507    *
508    * @return A reference to the layout engine.
509    */
510   LayoutEngine& GetLayoutEngine();
511
512   /**
513    * @brief Return a view of the text.
514    *
515    * @return A reference to the view.
516    */
517   View& GetView();
518
519   // Text-input Event Queuing
520
521   /**
522    * @brief Called by editable UI controls when keyboard focus is gained.
523    */
524   void KeyboardFocusGainEvent();
525
526   /**
527    * @brief Called by editable UI controls when focus is lost.
528    */
529   void KeyboardFocusLostEvent();
530
531   /**
532    * @brief Called by editable UI controls when key events are received.
533    *
534    * @param[in] event The key event.
535    * @param[in] type Used to distinguish between regular key events and IMF events.
536    */
537   bool KeyEvent( const Dali::KeyEvent& event );
538
539   /**
540    * @brief Called by editable UI controls when key events are received.
541    *
542    * @param[in] text The text to insert.
543    * @param[in] type Used to distinguish between regular key events and IMF events.
544    */
545   void InsertText( const std::string& text, InsertType type );
546
547   /**
548    * @brief Checks if text is selected and if so removes it.
549    * @return true if text was removed
550    */
551   bool RemoveSelectedText();
552
553   /**
554    * @brief Called by editable UI controls when a tap gesture occurs.
555    * @param[in] tapCount The number of taps.
556    * @param[in] x The x position relative to the top-left of the parent control.
557    * @param[in] y The y position relative to the top-left of the parent control.
558    */
559   void TapEvent( unsigned int tapCount, float x, float y );
560
561   /**
562    * @brief Called by editable UI controls when a pan gesture occurs.
563    *
564    * @param[in] state The state of the gesture.
565    * @param[in] displacement This distance panned since the last pan gesture.
566    */
567   void PanEvent( Gesture::State state, const Vector2& displacement );
568
569   /**
570    * @brief Called by editable UI controls when a long press gesture occurs.
571    *
572    * @param[in] state The state of the gesture.
573    * @param[in] x The x position relative to the top-left of the parent control.
574    * @param[in] y The y position relative to the top-left of the parent control.
575    */
576   void LongPressEvent( Gesture::State state, float x, float y );
577
578   /**
579    * @brief Creates a selection event.
580    *
581    * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
582    *
583    * @param[in] x The x position relative to the top-left of the parent control.
584    * @param[in] y The y position relative to the top-left of the parent control.
585    * @param[in] selectAll Whether the whole text is selected.
586    */
587   void SelectEvent( float x, float y, bool selectAll );
588
589   /**
590    * @brief Event received from IMF manager
591    *
592    * @param[in] imfManager The IMF manager.
593    * @param[in] imfEvent The event received.
594    * @return A data struture indicating if update is needed, cursor position and current text.
595    */
596   ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
597
598   /**
599    * @brief Paste given string into Text model
600    * @param[in] stringToPaste this string will be inserted into the text model
601    */
602   void PasteText( const std::string& stringToPaste );
603
604   /**
605    * @brief Event from Clipboard notifying an Item has been selected for pasting
606    */
607   void PasteClipboardItemEvent();
608
609   /**
610    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
611    */
612   virtual void GetTargetSize( Vector2& targetSize );
613
614   /**
615    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
616    */
617   virtual void AddDecoration( Actor& actor, bool needsClipping );
618
619   /**
620    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
621    */
622   virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
623
624   /**
625    * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
626    */
627   virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
628
629 protected:
630
631   /**
632    * @brief A reference counted object may only be deleted by calling Unreference().
633    */
634   virtual ~Controller();
635
636 private:
637
638   /**
639    * @brief Helper to KeyEvent() to handle the backspace case.
640    *
641    * @return True if a character was deleted.
642    */
643   bool BackspaceKeyEvent();
644
645   /**
646    * @brief Helper to notify IMF manager with surrounding text & cursor changes.
647    */
648   void NotifyImfManager();
649
650   /**
651    * @brief Helper to clear font-specific data.
652    */
653   void ShowPlaceholderText();
654
655   /**
656    * @brief Helper to clear all the model data except for LogicalModel::mText.
657    */
658   void ClearModelData();
659
660   /**
661    * @brief Helper to clear font-specific data (only).
662    */
663   void ClearFontData();
664
665   /**
666    * @brief Private constructor.
667    */
668   Controller( ControlInterface& controlInterface );
669
670   // Undefined
671   Controller( const Controller& handle );
672
673   // Undefined
674   Controller& operator=( const Controller& handle );
675
676 private:
677
678   struct Impl;
679   Impl* mImpl;
680 };
681
682 } // namespace Text
683
684 } // namespace Toolkit
685
686 } // namespace Dali
687
688 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__