PlaceholderText + IMF improvements
[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 Replaces any text previously set.
114    *
115    * @note This will be converted into UTF-32 when stored in the text model.
116    * @param[in] text A string of UTF-8 characters.
117    */
118   void SetText( const std::string& text );
119
120   /**
121    * @brief Retrieve any text previously set.
122    *
123    * @return A string of UTF-8 characters.
124    */
125   void GetText( std::string& text ) const;
126
127   /**
128    * @brief Replaces any placeholder text previously set.
129    *
130    * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
131    * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
132    * @return True if the remove was successful.
133    */
134   bool RemoveText( int cursorOffset, int numberOfChars );
135
136   /**
137    * @brief Retrieve the current cursor position.
138    *
139    * @return The cursor position.
140    */
141   unsigned int GetLogicalCursorPosition() const;
142
143   /**
144    * @brief Replaces any placeholder text previously set.
145    *
146    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
147    * @param[in] text A string of UTF-8 characters.
148    */
149   void SetPlaceholderText( PlaceholderType type, const std::string& text );
150
151   /**
152    * @brief Retrieve any placeholder text previously set.
153    *
154    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
155    * @param[out] A string of UTF-8 characters.
156    */
157   void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
158
159   /**
160    * @brief Sets the maximum number of characters that can be inserted into the TextModel
161    *
162    * @param[in] maxCharacters maximum number of characters to be accepted
163    */
164   void SetMaximumNumberOfCharacters( int maxCharacters );
165
166   /**
167    * @brief Sets the maximum number of characters that can be inserted into the TextModel
168    *
169    * @param[in] maxCharacters maximum number of characters to be accepted
170    */
171   int GetMaximumNumberOfCharacters();
172
173   /**
174    * @brief Set the default font family.
175    *
176    * @param[in] defaultFontFamily The default font family.
177    */
178   void SetDefaultFontFamily( const std::string& defaultFontFamily );
179
180   /**
181    * @brief Retrieve the default font family.
182    *
183    * @return The default font family.
184    */
185   const std::string& GetDefaultFontFamily() const;
186
187   /**
188    * @brief Set the default font style.
189    *
190    * @param[in] defaultFontStyle The default font style.
191    */
192   void SetDefaultFontStyle( const std::string& defaultFontStyle );
193
194   /**
195    * @brief Retrieve the default font style.
196    *
197    * @return The default font style.
198    */
199   const std::string& GetDefaultFontStyle() const;
200
201   /**
202    * @brief Set the default point size.
203    *
204    * @param[in] defaultFontStyle The default point size.
205    */
206   void SetDefaultPointSize( float pointSize );
207
208   /**
209    * @brief Retrieve the default point size.
210    *
211    * @return The default point size.
212    */
213   float GetDefaultPointSize() const;
214
215   /**
216    * @brief Set the text color
217    *
218    * @param textColor The text color
219    */
220   void SetTextColor( const Vector4& textColor );
221
222   /**
223    * @brief Retrieve the text color
224    *
225    * @return The text color
226    */
227   const Vector4& GetTextColor() const;
228
229   /**
230    * @brief Set the text color
231    *
232    * @param textColor The text color
233    */
234   void SetPlaceholderTextColor( const Vector4& textColor );
235
236   /**
237    * @brief Retrieve the text color
238    *
239    * @return The text color
240    */
241   const Vector4& GetPlaceholderTextColor() const;
242
243   /**
244    * @brief Set the shadow offset.
245    *
246    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
247    */
248   void SetShadowOffset( const Vector2& shadowOffset );
249
250   /**
251    * @brief Retrieve the shadow offset.
252    *
253    * @return The shadow offset.
254    */
255   const Vector2& GetShadowOffset() const;
256
257   /**
258    * @brief Set the shadow color.
259    *
260    * @param[in] shadowColor The shadow color.
261    */
262   void SetShadowColor( const Vector4& shadowColor );
263
264   /**
265    * @brief Retrieve the shadow color.
266    *
267    * @return The shadow color.
268    */
269   const Vector4& GetShadowColor() const;
270
271   /**
272    * @brief Set the underline color.
273    *
274    * @param[in] color color of underline.
275    */
276   void SetUnderlineColor( const Vector4& color );
277
278   /**
279    * @brief Retrieve the underline color.
280    *
281    * @return The underline color.
282    */
283   const Vector4& GetUnderlineColor() const;
284
285   /**
286    * @brief Set the underline enabled flag.
287    *
288    * @param[in] enabled The underline enabled flag.
289    */
290   void SetUnderlineEnabled( bool enabled );
291
292   /**
293    * @brief Returns whether the text is underlined or not.
294    *
295    * @return The underline state.
296    */
297   bool IsUnderlineEnabled() const;
298
299   /**
300    * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
301    *
302    * @param[in] height The height in pixels of the underline
303    */
304   void SetUnderlineHeight( float height );
305
306   /**
307    * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
308    *
309    * @return The height of the underline, or 0 if height is not overrided.
310    */
311   float GetUnderlineHeight() const;
312
313   /**
314    * @brief Called to enable text input.
315    *
316    * @note Only selectable or editable controls should calls this.
317    * @param[in] decorator Used to create cursor, selection handle decorations etc.
318    */
319   void EnableTextInput( DecoratorPtr decorator );
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 Private constructor.
488    */
489   Controller( ControlInterface& controlInterface );
490
491   // Undefined
492   Controller( const Controller& handle );
493
494   // Undefined
495   Controller& operator=( const Controller& handle );
496
497 private:
498
499   struct Impl;
500   Impl* mImpl;
501 };
502
503 } // namespace Text
504
505 } // namespace Toolkit
506
507 } // namespace Dali
508
509 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__