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