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