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