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