[dali_1.9.26] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/text-controller.h>
20
21 // EXTERNAL INCLUDES
22 #include <limits>
23 #include <cmath>
24 #include <memory.h>
25 #include <dali/public-api/adaptor-framework/key.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
28 #include <dali/devel-api/text-abstraction/font-client.h>
29 #include <dali/devel-api/adaptor-framework/key-devel.h>
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/public-api/controls/text-controls/placeholder-properties.h>
33 #include <dali-toolkit/internal/text/bidirectional-support.h>
34 #include <dali-toolkit/internal/text/character-set-conversion.h>
35 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
36 #include <dali-toolkit/internal/text/markup-processor.h>
37 #include <dali-toolkit/internal/text/multi-language-support.h>
38 #include <dali-toolkit/internal/text/text-controller-impl.h>
39 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
40 #include <dali-toolkit/internal/text/text-font-style.h>
41
42 namespace
43 {
44
45 #if defined(DEBUG_ENABLED)
46   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
47 #endif
48
49 const float MAX_FLOAT = std::numeric_limits<float>::max();
50
51 const std::string EMPTY_STRING("");
52
53 const std::string KEY_C_NAME = "c";
54 const std::string KEY_V_NAME = "v";
55 const std::string KEY_X_NAME = "x";
56
57 const char * const PLACEHOLDER_TEXT = "text";
58 const char * const PLACEHOLDER_TEXT_FOCUSED = "textFocused";
59 const char * const PLACEHOLDER_COLOR = "color";
60 const char * const PLACEHOLDER_FONT_FAMILY = "fontFamily";
61 const char * const PLACEHOLDER_FONT_STYLE = "fontStyle";
62 const char * const PLACEHOLDER_POINT_SIZE = "pointSize";
63 const char * const PLACEHOLDER_PIXEL_SIZE = "pixelSize";
64 const char * const PLACEHOLDER_ELLIPSIS = "ellipsis";
65
66 float ConvertToEven( float value )
67 {
68   int intValue(static_cast<int>( value ));
69   return static_cast<float>( intValue + ( intValue & 1 ) );
70 }
71
72 int ConvertPixelToPint( float pixel )
73 {
74   unsigned int horizontalDpi = 0u;
75   unsigned int verticalDpi = 0u;
76   Dali::TextAbstraction::FontClient fontClient = Dali::TextAbstraction::FontClient::Get();
77   fontClient.GetDpi( horizontalDpi, verticalDpi );
78
79   return ( pixel * 72.f ) / static_cast< float >( horizontalDpi );
80 }
81
82 } // namespace
83
84 namespace Dali
85 {
86
87 namespace Toolkit
88 {
89
90 namespace Text
91 {
92
93 /**
94  * @brief Adds a new font description run for the selected text.
95  *
96  * The new font parameters are added after the call to this method.
97  *
98  * @param[in] eventData The event data pointer.
99  * @param[in] logicalModel The logical model where to add the new font description run.
100  * @param[out] startOfSelectedText Index to the first selected character.
101  * @param[out] lengthOfSelectedText Number of selected characters.
102  */
103 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
104                                                  LogicalModelPtr logicalModel,
105                                                  CharacterIndex& startOfSelectedText,
106                                                  Length& lengthOfSelectedText )
107 {
108   const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
109
110   // Get start and end position of selection
111   startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
112   lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
113
114   // Add the font run.
115   const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
116   logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
117
118   FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
119
120   fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
121   fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
122
123   // Recalculate the selection highlight as the metrics may have changed.
124   eventData->mUpdateLeftSelectionPosition = true;
125   eventData->mUpdateRightSelectionPosition = true;
126   eventData->mUpdateHighlightBox = true;
127
128   return fontDescriptionRun;
129 }
130
131 // public : Constructor.
132
133 ControllerPtr Controller::New()
134 {
135   return ControllerPtr( new Controller() );
136 }
137
138 ControllerPtr Controller::New( ControlInterface* controlInterface )
139 {
140   return ControllerPtr( new Controller( controlInterface ) );
141 }
142
143 ControllerPtr Controller::New( ControlInterface* controlInterface,
144                                EditableControlInterface* editableControlInterface )
145 {
146   return ControllerPtr( new Controller( controlInterface,
147                                         editableControlInterface ) );
148 }
149
150 // public : Configure the text controller.
151
152 void Controller::EnableTextInput( DecoratorPtr decorator, InputMethodContext& inputMethodContext )
153 {
154   if( !decorator )
155   {
156     delete mImpl->mEventData;
157     mImpl->mEventData = NULL;
158
159     // Nothing else to do.
160     return;
161   }
162
163   if( NULL == mImpl->mEventData )
164   {
165     mImpl->mEventData = new EventData( decorator, inputMethodContext );
166   }
167 }
168
169 void Controller::SetGlyphType( TextAbstraction::GlyphType glyphType )
170 {
171   // Metrics for bitmap & vector based glyphs are different
172   mImpl->mMetrics->SetGlyphType( glyphType );
173
174   // Clear the font-specific data
175   ClearFontData();
176
177   mImpl->RequestRelayout();
178 }
179
180 void Controller::SetMarkupProcessorEnabled( bool enable )
181 {
182   if( enable != mImpl->mMarkupProcessorEnabled )
183   {
184     //If Text was already set, call the SetText again for enabling or disabling markup
185     mImpl->mMarkupProcessorEnabled = enable;
186     std::string text;
187     GetText( text );
188     SetText( text );
189   }
190 }
191
192 bool Controller::IsMarkupProcessorEnabled() const
193 {
194   return mImpl->mMarkupProcessorEnabled;
195 }
196
197 void Controller::SetAutoScrollEnabled( bool enable )
198 {
199   DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled[%s] SingleBox[%s]-> [%p]\n", (enable)?"true":"false", ( mImpl->mLayoutEngine.GetLayout() == Layout::Engine::SINGLE_LINE_BOX)?"true":"false", this );
200
201   if( mImpl->mLayoutEngine.GetLayout() == Layout::Engine::SINGLE_LINE_BOX )
202   {
203     if( enable )
204     {
205       DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled for SINGLE_LINE_BOX\n" );
206       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
207                                                                LAYOUT                    |
208                                                                ALIGN                     |
209                                                                UPDATE_LAYOUT_SIZE        |
210                                                                UPDATE_DIRECTION          |
211                                                                REORDER );
212
213     }
214     else
215     {
216       DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetAutoScrollEnabled Disabling autoscroll\n");
217       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
218                                                                LAYOUT                    |
219                                                                ALIGN                     |
220                                                                UPDATE_LAYOUT_SIZE        |
221                                                                REORDER );
222     }
223
224     mImpl->mIsAutoScrollEnabled = enable;
225     mImpl->RequestRelayout();
226   }
227   else
228   {
229     DALI_LOG_WARNING( "Attempted AutoScrolling on a non SINGLE_LINE_BOX, request ignored\n" );
230     mImpl->mIsAutoScrollEnabled = false;
231   }
232 }
233
234 bool Controller::IsAutoScrollEnabled() const
235 {
236   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::IsAutoScrollEnabled[%s]\n", mImpl->mIsAutoScrollEnabled?"true":"false" );
237
238   return mImpl->mIsAutoScrollEnabled;
239 }
240
241 CharacterDirection Controller::GetAutoScrollDirection() const
242 {
243   return mImpl->mIsTextDirectionRTL;
244 }
245
246 float Controller::GetAutoScrollLineAlignment() const
247 {
248   float offset = 0.f;
249
250   if( mImpl->mModel->mVisualModel &&
251       ( 0u != mImpl->mModel->mVisualModel->mLines.Count() ) )
252   {
253     offset = ( *mImpl->mModel->mVisualModel->mLines.Begin() ).alignmentOffset;
254   }
255
256   return offset;
257 }
258
259 void Controller::SetHorizontalScrollEnabled( bool enable )
260 {
261   if( ( NULL != mImpl->mEventData ) &&
262       mImpl->mEventData->mDecorator )
263   {
264     mImpl->mEventData->mDecorator->SetHorizontalScrollEnabled( enable );
265   }
266 }
267 bool Controller::IsHorizontalScrollEnabled() const
268 {
269   if( ( NULL != mImpl->mEventData ) &&
270       mImpl->mEventData->mDecorator )
271   {
272     return mImpl->mEventData->mDecorator->IsHorizontalScrollEnabled();
273   }
274
275   return false;
276 }
277
278 void Controller::SetVerticalScrollEnabled( bool enable )
279 {
280   if( ( NULL != mImpl->mEventData ) &&
281       mImpl->mEventData->mDecorator )
282   {
283     if( mImpl->mEventData->mDecorator )
284     {
285       mImpl->mEventData->mDecorator->SetVerticalScrollEnabled( enable );
286     }
287   }
288 }
289
290 bool Controller::IsVerticalScrollEnabled() const
291 {
292   if( ( NULL != mImpl->mEventData ) &&
293       mImpl->mEventData->mDecorator )
294   {
295     return mImpl->mEventData->mDecorator->IsVerticalScrollEnabled();
296   }
297
298   return false;
299 }
300
301 void Controller::SetSmoothHandlePanEnabled( bool enable )
302 {
303   if( ( NULL != mImpl->mEventData ) &&
304       mImpl->mEventData->mDecorator )
305   {
306     mImpl->mEventData->mDecorator->SetSmoothHandlePanEnabled( enable );
307   }
308 }
309
310 bool Controller::IsSmoothHandlePanEnabled() const
311 {
312   if( ( NULL != mImpl->mEventData ) &&
313       mImpl->mEventData->mDecorator )
314   {
315     return mImpl->mEventData->mDecorator->IsSmoothHandlePanEnabled();
316   }
317
318   return false;
319 }
320
321 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
322 {
323   mImpl->mMaximumNumberOfCharacters = maxCharacters;
324 }
325
326 int Controller::GetMaximumNumberOfCharacters()
327 {
328   return mImpl->mMaximumNumberOfCharacters;
329 }
330
331 void Controller::SetEnableCursorBlink( bool enable )
332 {
333   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
334
335   if( NULL != mImpl->mEventData )
336   {
337     mImpl->mEventData->mCursorBlinkEnabled = enable;
338
339     if( !enable &&
340         mImpl->mEventData->mDecorator )
341     {
342       mImpl->mEventData->mDecorator->StopCursorBlink();
343     }
344   }
345 }
346
347 bool Controller::GetEnableCursorBlink() const
348 {
349   if( NULL != mImpl->mEventData )
350   {
351     return mImpl->mEventData->mCursorBlinkEnabled;
352   }
353
354   return false;
355 }
356
357 void Controller::SetMultiLineEnabled( bool enable )
358 {
359   const Layout::Engine::Type layout = enable ? Layout::Engine::MULTI_LINE_BOX : Layout::Engine::SINGLE_LINE_BOX;
360
361   if( layout != mImpl->mLayoutEngine.GetLayout() )
362   {
363     // Set the layout type.
364     mImpl->mLayoutEngine.SetLayout( layout );
365
366     // Set the flags to redo the layout operations
367     const OperationsMask layoutOperations =  static_cast<OperationsMask>( LAYOUT             |
368                                                                           UPDATE_LAYOUT_SIZE |
369                                                                           ALIGN              |
370                                                                           REORDER );
371
372     mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
373     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
374
375     // Need to recalculate natural size
376     mImpl->mRecalculateNaturalSize = true;
377
378     mImpl->RequestRelayout();
379   }
380 }
381
382 bool Controller::IsMultiLineEnabled() const
383 {
384   return Layout::Engine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
385 }
386
387 void Controller::SetHorizontalAlignment( Text::HorizontalAlignment::Type alignment )
388 {
389   if( alignment != mImpl->mModel->mHorizontalAlignment )
390   {
391     // Set the alignment.
392     mImpl->mModel->mHorizontalAlignment = alignment;
393
394     // Set the flag to redo the alignment operation.
395     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
396
397     if( mImpl->mEventData )
398     {
399       mImpl->mEventData->mUpdateAlignment = true;
400
401       // Update the cursor if it's in editing mode
402       if( EventData::IsEditingState( mImpl->mEventData->mState ) )
403       {
404         mImpl->ChangeState( EventData::EDITING );
405         mImpl->mEventData->mUpdateCursorPosition = true;
406       }
407     }
408
409     mImpl->RequestRelayout();
410   }
411 }
412
413 Text::HorizontalAlignment::Type Controller::GetHorizontalAlignment() const
414 {
415   return mImpl->mModel->mHorizontalAlignment;
416 }
417
418 void Controller::SetVerticalAlignment( VerticalAlignment::Type alignment )
419 {
420   if( alignment != mImpl->mModel->mVerticalAlignment )
421   {
422     // Set the alignment.
423     mImpl->mModel->mVerticalAlignment = alignment;
424
425     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
426
427     mImpl->RequestRelayout();
428   }
429 }
430
431 VerticalAlignment::Type Controller::GetVerticalAlignment() const
432 {
433   return mImpl->mModel->mVerticalAlignment;
434 }
435
436 bool Controller::IsIgnoreSpacesAfterText() const
437 {
438   return mImpl->mModel->mIgnoreSpacesAfterText;
439 }
440
441 void Controller::SetIgnoreSpacesAfterText( bool ignore )
442 {
443   mImpl->mModel->mIgnoreSpacesAfterText = ignore;
444 }
445
446 bool Controller::IsMatchSystemLanguageDirection() const
447 {
448   return mImpl->mModel->mMatchSystemLanguageDirection;
449 }
450
451 void Controller::SetMatchSystemLanguageDirection( bool match )
452 {
453   mImpl->mModel->mMatchSystemLanguageDirection = match;
454 }
455
456 void Controller::SetLayoutDirection( Dali::LayoutDirection::Type layoutDirection )
457 {
458   mImpl->mLayoutDirection = layoutDirection;
459 }
460
461 bool Controller::IsShowingRealText() const
462 {
463   return mImpl->IsShowingRealText();
464 }
465
466
467 void Controller::SetLineWrapMode( Text::LineWrap::Mode lineWrapMode )
468 {
469   if( lineWrapMode != mImpl->mModel->mLineWrapMode )
470   {
471     // Set the text wrap mode.
472     mImpl->mModel->mLineWrapMode = lineWrapMode;
473
474
475     // Update Text layout for applying wrap mode
476     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
477                                                              ALIGN                     |
478                                                              LAYOUT                    |
479                                                              UPDATE_LAYOUT_SIZE        |
480                                                              REORDER                   );
481     mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
482     mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
483     mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
484
485     // Request relayout
486     mImpl->RequestRelayout();
487   }
488 }
489
490 Text::LineWrap::Mode Controller::GetLineWrapMode() const
491 {
492   return mImpl->mModel->mLineWrapMode;
493 }
494
495 void Controller::SetTextElideEnabled( bool enabled )
496 {
497   mImpl->mModel->mElideEnabled = enabled;
498 }
499
500 bool Controller::IsTextElideEnabled() const
501 {
502   return mImpl->mModel->mElideEnabled;
503 }
504
505 void Controller::SetTextFitEnabled(bool enabled)
506 {
507   mImpl->mTextFitEnabled = enabled;
508 }
509
510 bool Controller::IsTextFitEnabled() const
511 {
512   return mImpl->mTextFitEnabled;
513 }
514
515 void Controller::SetTextFitMinSize( float minSize, FontSizeType type )
516 {
517   switch( type )
518   {
519     case POINT_SIZE:
520     {
521       mImpl->mTextFitMinSize = minSize;
522       break;
523     }
524     case PIXEL_SIZE:
525     {
526       mImpl->mTextFitMinSize = ConvertPixelToPint( minSize );
527       break;
528     }
529   }
530 }
531
532 float Controller::GetTextFitMinSize() const
533 {
534   return mImpl->mTextFitMinSize;
535 }
536
537 void Controller::SetTextFitMaxSize( float maxSize, FontSizeType type )
538 {
539   switch( type )
540   {
541     case POINT_SIZE:
542     {
543       mImpl->mTextFitMaxSize = maxSize;
544       break;
545     }
546     case PIXEL_SIZE:
547     {
548       mImpl->mTextFitMaxSize = ConvertPixelToPint( maxSize );
549       break;
550     }
551   }
552 }
553
554 float Controller::GetTextFitMaxSize() const
555 {
556   return mImpl->mTextFitMaxSize;
557 }
558
559 void Controller::SetTextFitStepSize( float step, FontSizeType type )
560 {
561   switch( type )
562   {
563     case POINT_SIZE:
564     {
565       mImpl->mTextFitStepSize = step;
566       break;
567     }
568     case PIXEL_SIZE:
569     {
570       mImpl->mTextFitStepSize = ConvertPixelToPint( step );
571       break;
572     }
573   }
574 }
575
576 float Controller::GetTextFitStepSize() const
577 {
578   return mImpl->mTextFitStepSize;
579 }
580
581 void Controller::SetTextFitContentSize(Vector2 size)
582 {
583   mImpl->mTextFitContentSize = size;
584 }
585
586 Vector2 Controller::GetTextFitContentSize() const
587 {
588   return mImpl->mTextFitContentSize;
589 }
590
591 void Controller::SetPlaceholderTextElideEnabled( bool enabled )
592 {
593   mImpl->mEventData->mIsPlaceholderElideEnabled = enabled;
594   mImpl->mEventData->mPlaceholderEllipsisFlag = true;
595
596   // Update placeholder if there is no text
597   if( mImpl->IsShowingPlaceholderText() ||
598       ( 0u == mImpl->mModel->mLogicalModel->mText.Count() ) )
599   {
600     ShowPlaceholderText();
601   }
602 }
603
604 bool Controller::IsPlaceholderTextElideEnabled() const
605 {
606   return mImpl->mEventData->mIsPlaceholderElideEnabled;
607 }
608
609 void Controller::SetSelectionEnabled( bool enabled )
610 {
611   mImpl->mEventData->mSelectionEnabled = enabled;
612 }
613
614 bool Controller::IsSelectionEnabled() const
615 {
616   return mImpl->mEventData->mSelectionEnabled;
617 }
618
619 void Controller::SetShiftSelectionEnabled( bool enabled )
620 {
621   mImpl->mEventData->mShiftSelectionFlag = enabled;
622 }
623
624 bool Controller::IsShiftSelectionEnabled() const
625 {
626   return mImpl->mEventData->mShiftSelectionFlag;
627 }
628
629 void Controller::SetGrabHandleEnabled( bool enabled )
630 {
631   mImpl->mEventData->mGrabHandleEnabled = enabled;
632 }
633
634 bool Controller::IsGrabHandleEnabled() const
635 {
636   return mImpl->mEventData->mGrabHandleEnabled;
637 }
638
639 void Controller::SetGrabHandlePopupEnabled(bool enabled)
640 {
641   mImpl->mEventData->mGrabHandlePopupEnabled = enabled;
642 }
643
644 bool Controller::IsGrabHandlePopupEnabled() const
645 {
646   return mImpl->mEventData->mGrabHandlePopupEnabled;
647 }
648
649 // public : Update
650
651 void Controller::SetText( const std::string& text )
652 {
653   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
654
655   // Reset keyboard as text changed
656   mImpl->ResetInputMethodContext();
657
658   // Remove the previously set text and style.
659   ResetText();
660
661   // Remove the style.
662   ClearStyleData();
663
664   CharacterIndex lastCursorIndex = 0u;
665
666   if( NULL != mImpl->mEventData )
667   {
668     // If popup shown then hide it by switching to Editing state
669     if( ( EventData::SELECTING == mImpl->mEventData->mState )          ||
670         ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
671         ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
672         ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
673     {
674       mImpl->ChangeState( EventData::EDITING );
675     }
676   }
677
678   if( !text.empty() )
679   {
680     mImpl->mModel->mVisualModel->SetTextColor( mImpl->mTextColor );
681
682     MarkupProcessData markupProcessData( mImpl->mModel->mLogicalModel->mColorRuns,
683                                          mImpl->mModel->mLogicalModel->mFontDescriptionRuns,
684                                          mImpl->mModel->mLogicalModel->mEmbeddedItems );
685
686     Length textSize = 0u;
687     const uint8_t* utf8 = NULL;
688     if( mImpl->mMarkupProcessorEnabled )
689     {
690       ProcessMarkupString( text, markupProcessData );
691       textSize = markupProcessData.markupProcessedText.size();
692
693       // This is a bit horrible but std::string returns a (signed) char*
694       utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
695     }
696     else
697     {
698       textSize = text.size();
699
700       // This is a bit horrible but std::string returns a (signed) char*
701       utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
702     }
703
704     //  Convert text into UTF-32
705     Vector<Character>& utf32Characters = mImpl->mModel->mLogicalModel->mText;
706     utf32Characters.Resize( textSize );
707
708     // Transform a text array encoded in utf8 into an array encoded in utf32.
709     // It returns the actual number of characters.
710     Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
711     utf32Characters.Resize( characterCount );
712
713     DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
714     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mModel->mLogicalModel->mText.Count() );
715
716     // The characters to be added.
717     mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
718
719     // To reset the cursor position
720     lastCursorIndex = characterCount;
721
722     // Update the rest of the model during size negotiation
723     mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
724
725     // The natural size needs to be re-calculated.
726     mImpl->mRecalculateNaturalSize = true;
727
728     // The text direction needs to be updated.
729     mImpl->mUpdateTextDirection = true;
730
731     // Apply modifications to the model
732     mImpl->mOperationsPending = ALL_OPERATIONS;
733   }
734   else
735   {
736     ShowPlaceholderText();
737   }
738
739   // Resets the cursor position.
740   ResetCursorPosition( lastCursorIndex );
741
742   // Scrolls the text to make the cursor visible.
743   ResetScrollPosition();
744
745   mImpl->RequestRelayout();
746
747   if( NULL != mImpl->mEventData )
748   {
749     // Cancel previously queued events
750     mImpl->mEventData->mEventQueue.clear();
751   }
752
753   // Do this last since it provides callbacks into application code.
754   if( NULL != mImpl->mEditableControlInterface )
755   {
756     mImpl->mEditableControlInterface->TextChanged();
757   }
758 }
759
760 void Controller::GetText( std::string& text ) const
761 {
762   if( !mImpl->IsShowingPlaceholderText() )
763   {
764     // Retrieves the text string.
765     mImpl->GetText( 0u, text );
766   }
767   else
768   {
769     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
770   }
771 }
772
773 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
774 {
775   if( NULL != mImpl->mEventData )
776   {
777     if( PLACEHOLDER_TYPE_INACTIVE == type )
778     {
779       mImpl->mEventData->mPlaceholderTextInactive = text;
780     }
781     else
782     {
783       mImpl->mEventData->mPlaceholderTextActive = text;
784     }
785
786     // Update placeholder if there is no text
787     if( mImpl->IsShowingPlaceholderText() ||
788         ( 0u == mImpl->mModel->mLogicalModel->mText.Count() ) )
789     {
790       ShowPlaceholderText();
791     }
792   }
793 }
794
795 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
796 {
797   if( NULL != mImpl->mEventData )
798   {
799     if( PLACEHOLDER_TYPE_INACTIVE == type )
800     {
801       text = mImpl->mEventData->mPlaceholderTextInactive;
802     }
803     else
804     {
805       text = mImpl->mEventData->mPlaceholderTextActive;
806     }
807   }
808 }
809
810 void Controller::UpdateAfterFontChange( const std::string& newDefaultFont )
811 {
812   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::UpdateAfterFontChange\n");
813
814   if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
815   {
816     DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
817     mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
818
819     ClearFontData();
820
821     mImpl->RequestRelayout();
822   }
823 }
824
825 // public : Default style & Input style
826
827 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
828 {
829   if( NULL == mImpl->mFontDefaults )
830   {
831     mImpl->mFontDefaults = new FontDefaults();
832   }
833
834   mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
835   DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
836   mImpl->mFontDefaults->familyDefined = !defaultFontFamily.empty();
837
838   if( mImpl->mEventData )
839   {
840     // Update the cursor position if it's in editing mode
841     if( EventData::IsEditingState( mImpl->mEventData->mState ) )
842     {
843       mImpl->mEventData->mDecoratorUpdated = true;
844       mImpl->mEventData->mUpdateCursorPosition = true; // Cursor position should be updated when the font family is updated.
845     }
846   }
847
848   // Clear the font-specific data
849   ClearFontData();
850
851   mImpl->RequestRelayout();
852 }
853
854 const std::string& Controller::GetDefaultFontFamily() const
855 {
856   if( NULL != mImpl->mFontDefaults )
857   {
858     return mImpl->mFontDefaults->mFontDescription.family;
859   }
860
861   return EMPTY_STRING;
862 }
863
864 void Controller::SetPlaceholderFontFamily( const std::string& placeholderTextFontFamily )
865 {
866   if( NULL != mImpl->mEventData )
867   {
868     if( NULL == mImpl->mEventData->mPlaceholderFont )
869     {
870       mImpl->mEventData->mPlaceholderFont = new FontDefaults();
871     }
872
873     mImpl->mEventData->mPlaceholderFont->mFontDescription.family = placeholderTextFontFamily;
874     DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetPlaceholderFontFamily %s\n", placeholderTextFontFamily.c_str());
875     mImpl->mEventData->mPlaceholderFont->familyDefined = !placeholderTextFontFamily.empty();
876
877     mImpl->RequestRelayout();
878   }
879 }
880
881 const std::string& Controller::GetPlaceholderFontFamily() const
882 {
883   if( ( NULL != mImpl->mEventData ) && ( NULL != mImpl->mEventData->mPlaceholderFont ) )
884   {
885     return mImpl->mEventData->mPlaceholderFont->mFontDescription.family;
886   }
887
888   return EMPTY_STRING;
889 }
890
891 void Controller::SetDefaultFontWeight( FontWeight weight )
892 {
893   if( NULL == mImpl->mFontDefaults )
894   {
895     mImpl->mFontDefaults = new FontDefaults();
896   }
897
898   mImpl->mFontDefaults->mFontDescription.weight = weight;
899   mImpl->mFontDefaults->weightDefined = true;
900
901   if( mImpl->mEventData )
902   {
903     // Update the cursor position if it's in editing mode
904     if( EventData::IsEditingState( mImpl->mEventData->mState ) )
905     {
906       mImpl->mEventData->mDecoratorUpdated = true;
907       mImpl->mEventData->mUpdateCursorPosition = true; // Cursor position should be updated when the font weight is updated.
908     }
909   }
910
911   // Clear the font-specific data
912   ClearFontData();
913
914   mImpl->RequestRelayout();
915 }
916
917 bool Controller::IsDefaultFontWeightDefined() const
918 {
919   if( NULL != mImpl->mFontDefaults )
920   {
921     return mImpl->mFontDefaults->weightDefined;
922   }
923
924   return false;
925 }
926
927 FontWeight Controller::GetDefaultFontWeight() const
928 {
929   if( NULL != mImpl->mFontDefaults )
930   {
931     return mImpl->mFontDefaults->mFontDescription.weight;
932   }
933
934   return TextAbstraction::FontWeight::NORMAL;
935 }
936
937 void Controller::SetPlaceholderTextFontWeight( FontWeight weight )
938 {
939   if( NULL != mImpl->mEventData )
940   {
941     if( NULL == mImpl->mEventData->mPlaceholderFont )
942     {
943       mImpl->mEventData->mPlaceholderFont = new FontDefaults();
944     }
945
946     mImpl->mEventData->mPlaceholderFont->mFontDescription.weight = weight;
947     mImpl->mEventData->mPlaceholderFont->weightDefined = true;
948
949     mImpl->RequestRelayout();
950   }
951 }
952
953 bool Controller::IsPlaceholderTextFontWeightDefined() const
954 {
955   if( ( NULL != mImpl->mEventData ) && ( NULL != mImpl->mEventData->mPlaceholderFont ) )
956   {
957     return mImpl->mEventData->mPlaceholderFont->weightDefined;
958   }
959   return false;
960 }
961
962 FontWeight Controller::GetPlaceholderTextFontWeight() const
963 {
964   if( ( NULL != mImpl->mEventData ) && ( NULL != mImpl->mEventData->mPlaceholderFont ) )
965   {
966     return mImpl->mEventData->mPlaceholderFont->mFontDescription.weight;
967   }
968
969   return TextAbstraction::FontWeight::NORMAL;
970 }
971
972 void Controller::SetDefaultFontWidth( FontWidth width )
973 {
974   if( NULL == mImpl->mFontDefaults )
975   {
976     mImpl->mFontDefaults = new FontDefaults();
977   }
978
979   mImpl->mFontDefaults->mFontDescription.width = width;
980   mImpl->mFontDefaults->widthDefined = true;
981
982   if( mImpl->mEventData )
983   {
984     // Update the cursor position if it's in editing mode
985     if( EventData::IsEditingState( mImpl->mEventData->mState ) )
986     {
987       mImpl->mEventData->mDecoratorUpdated = true;
988       mImpl->mEventData->mUpdateCursorPosition = true; // Cursor position should be updated when the font width is updated.
989     }
990   }
991
992   // Clear the font-specific data
993   ClearFontData();
994
995   mImpl->RequestRelayout();
996 }
997
998 bool Controller::IsDefaultFontWidthDefined() const
999 {
1000   if( NULL != mImpl->mFontDefaults )
1001   {
1002     return mImpl->mFontDefaults->widthDefined;
1003   }
1004
1005   return false;
1006 }
1007
1008 FontWidth Controller::GetDefaultFontWidth() const
1009 {
1010   if( NULL != mImpl->mFontDefaults )
1011   {
1012     return mImpl->mFontDefaults->mFontDescription.width;
1013   }
1014
1015   return TextAbstraction::FontWidth::NORMAL;
1016 }
1017
1018 void Controller::SetPlaceholderTextFontWidth( FontWidth width )
1019 {
1020   if( NULL != mImpl->mEventData )
1021   {
1022     if( NULL == mImpl->mEventData->mPlaceholderFont )
1023     {
1024       mImpl->mEventData->mPlaceholderFont = new FontDefaults();
1025     }
1026
1027     mImpl->mEventData->mPlaceholderFont->mFontDescription.width = width;
1028     mImpl->mEventData->mPlaceholderFont->widthDefined = true;
1029
1030     mImpl->RequestRelayout();
1031   }
1032 }
1033
1034 bool Controller::IsPlaceholderTextFontWidthDefined() const
1035 {
1036   if( ( NULL != mImpl->mEventData ) && ( NULL != mImpl->mEventData->mPlaceholderFont ) )
1037   {
1038     return mImpl->mEventData->mPlaceholderFont->widthDefined;
1039   }
1040   return false;
1041 }
1042
1043 FontWidth Controller::GetPlaceholderTextFontWidth() const
1044 {
1045   if( ( NULL != mImpl->mEventData ) && ( NULL != mImpl->mEventData->mPlaceholderFont ) )
1046   {
1047     return mImpl->mEventData->mPlaceholderFont->mFontDescription.width;
1048   }
1049
1050   return TextAbstraction::FontWidth::NORMAL;
1051 }
1052
1053 void Controller::SetDefaultFontSlant( FontSlant slant )
1054 {
1055   if( NULL == mImpl->mFontDefaults )
1056   {
1057     mImpl->mFontDefaults = new FontDefaults();
1058   }
1059
1060   mImpl->mFontDefaults->mFontDescription.slant = slant;
1061   mImpl->mFontDefaults->slantDefined = true;
1062
1063   if( mImpl->mEventData )
1064   {
1065     // Update the cursor position if it's in editing mode
1066     if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1067     {
1068       mImpl->mEventData->mDecoratorUpdated = true;
1069       mImpl->mEventData->mUpdateCursorPosition = true; // Cursor position should be updated when the font slant is updated.
1070     }
1071   }
1072
1073   // Clear the font-specific data
1074   ClearFontData();
1075
1076   mImpl->RequestRelayout();
1077 }
1078
1079 bool Controller::IsDefaultFontSlantDefined() const
1080 {
1081   if( NULL != mImpl->mFontDefaults )
1082   {
1083     return mImpl->mFontDefaults->slantDefined;
1084   }
1085   return false;
1086 }
1087
1088 FontSlant Controller::GetDefaultFontSlant() const
1089 {
1090   if( NULL != mImpl->mFontDefaults )
1091   {
1092     return mImpl->mFontDefaults->mFontDescription.slant;
1093   }
1094
1095   return TextAbstraction::FontSlant::NORMAL;
1096 }
1097
1098 void Controller::SetPlaceholderTextFontSlant( FontSlant slant )
1099 {
1100   if( NULL != mImpl->mEventData )
1101   {
1102     if( NULL == mImpl->mEventData->mPlaceholderFont )
1103     {
1104       mImpl->mEventData->mPlaceholderFont = new FontDefaults();
1105     }
1106
1107     mImpl->mEventData->mPlaceholderFont->mFontDescription.slant = slant;
1108     mImpl->mEventData->mPlaceholderFont->slantDefined = true;
1109
1110     mImpl->RequestRelayout();
1111   }
1112 }
1113
1114 bool Controller::IsPlaceholderTextFontSlantDefined() const
1115 {
1116   if( ( NULL != mImpl->mEventData ) && ( NULL != mImpl->mEventData->mPlaceholderFont ) )
1117   {
1118     return mImpl->mEventData->mPlaceholderFont->slantDefined;
1119   }
1120   return false;
1121 }
1122
1123 FontSlant Controller::GetPlaceholderTextFontSlant() const
1124 {
1125   if( ( NULL != mImpl->mEventData ) && ( NULL != mImpl->mEventData->mPlaceholderFont ) )
1126   {
1127     return mImpl->mEventData->mPlaceholderFont->mFontDescription.slant;
1128   }
1129
1130   return TextAbstraction::FontSlant::NORMAL;
1131 }
1132
1133 void Controller::SetDefaultFontSize( float fontSize, FontSizeType type )
1134 {
1135   if( NULL == mImpl->mFontDefaults )
1136   {
1137     mImpl->mFontDefaults = new FontDefaults();
1138   }
1139
1140   switch( type )
1141   {
1142     case POINT_SIZE:
1143     {
1144       mImpl->mFontDefaults->mDefaultPointSize = fontSize;
1145       mImpl->mFontDefaults->sizeDefined = true;
1146       break;
1147     }
1148     case PIXEL_SIZE:
1149     {
1150       // Point size = Pixel size * 72.f / DPI
1151       unsigned int horizontalDpi = 0u;
1152       unsigned int verticalDpi = 0u;
1153       TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
1154       fontClient.GetDpi( horizontalDpi, verticalDpi );
1155
1156       mImpl->mFontDefaults->mDefaultPointSize = ( fontSize * 72.f ) / static_cast< float >( horizontalDpi );
1157       mImpl->mFontDefaults->sizeDefined = true;
1158       break;
1159     }
1160   }
1161
1162   if( mImpl->mEventData )
1163   {
1164     // Update the cursor position if it's in editing mode
1165     if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1166     {
1167       mImpl->mEventData->mDecoratorUpdated = true;
1168       mImpl->mEventData->mUpdateCursorPosition = true; // Cursor position should be updated when the font size is updated.
1169     }
1170   }
1171
1172   // Clear the font-specific data
1173   ClearFontData();
1174
1175   mImpl->RequestRelayout();
1176 }
1177
1178 float Controller::GetDefaultFontSize( FontSizeType type ) const
1179 {
1180   float value = 0.0f;
1181   if( NULL != mImpl->mFontDefaults )
1182   {
1183     switch( type )
1184     {
1185       case POINT_SIZE:
1186       {
1187         value = mImpl->mFontDefaults->mDefaultPointSize;
1188         break;
1189       }
1190       case PIXEL_SIZE:
1191       {
1192         // Pixel size = Point size * DPI / 72.f
1193         unsigned int horizontalDpi = 0u;
1194         unsigned int verticalDpi = 0u;
1195         TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
1196         fontClient.GetDpi( horizontalDpi, verticalDpi );
1197
1198         value = mImpl->mFontDefaults->mDefaultPointSize * static_cast< float >( horizontalDpi ) / 72.f;
1199         break;
1200       }
1201     }
1202     return value;
1203   }
1204
1205   return value;
1206 }
1207
1208 void Controller::SetPlaceholderTextFontSize( float fontSize, FontSizeType type )
1209 {
1210   if( NULL != mImpl->mEventData )
1211   {
1212     if( NULL == mImpl->mEventData->mPlaceholderFont )
1213     {
1214       mImpl->mEventData->mPlaceholderFont = new FontDefaults();
1215     }
1216
1217     switch( type )
1218     {
1219       case POINT_SIZE:
1220       {
1221         mImpl->mEventData->mPlaceholderFont->mDefaultPointSize = fontSize;
1222         mImpl->mEventData->mPlaceholderFont->sizeDefined = true;
1223         mImpl->mEventData->mIsPlaceholderPixelSize = false; // Font size flag
1224         break;
1225       }
1226       case PIXEL_SIZE:
1227       {
1228         // Point size = Pixel size * 72.f / DPI
1229         unsigned int horizontalDpi = 0u;
1230         unsigned int verticalDpi = 0u;
1231         TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
1232         fontClient.GetDpi( horizontalDpi, verticalDpi );
1233
1234         mImpl->mEventData->mPlaceholderFont->mDefaultPointSize = ( fontSize * 72.f ) / static_cast< float >( horizontalDpi );
1235         mImpl->mEventData->mPlaceholderFont->sizeDefined = true;
1236         mImpl->mEventData->mIsPlaceholderPixelSize = true; // Font size flag
1237         break;
1238       }
1239     }
1240
1241     mImpl->RequestRelayout();
1242   }
1243 }
1244
1245 float Controller::GetPlaceholderTextFontSize( FontSizeType type ) const
1246 {
1247   float value = 0.0f;
1248   if( NULL != mImpl->mEventData )
1249   {
1250     switch( type )
1251     {
1252       case POINT_SIZE:
1253       {
1254         if( NULL != mImpl->mEventData->mPlaceholderFont )
1255         {
1256           value = mImpl->mEventData->mPlaceholderFont->mDefaultPointSize;
1257         }
1258         else
1259         {
1260           // If the placeholder text font size is not set, then return the default font size.
1261           value = GetDefaultFontSize( POINT_SIZE );
1262         }
1263         break;
1264       }
1265       case PIXEL_SIZE:
1266       {
1267         if( NULL != mImpl->mEventData->mPlaceholderFont )
1268         {
1269           // Pixel size = Point size * DPI / 72.f
1270           unsigned int horizontalDpi = 0u;
1271           unsigned int verticalDpi = 0u;
1272           TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
1273           fontClient.GetDpi( horizontalDpi, verticalDpi );
1274
1275           value = mImpl->mEventData->mPlaceholderFont->mDefaultPointSize * static_cast< float >( horizontalDpi ) / 72.f;
1276         }
1277         else
1278         {
1279           // If the placeholder text font size is not set, then return the default font size.
1280           value = GetDefaultFontSize( PIXEL_SIZE );
1281         }
1282         break;
1283       }
1284     }
1285     return value;
1286   }
1287
1288   return value;
1289 }
1290
1291 void Controller::SetDefaultColor( const Vector4& color )
1292 {
1293   mImpl->mTextColor = color;
1294
1295   if( !mImpl->IsShowingPlaceholderText() )
1296   {
1297     mImpl->mModel->mVisualModel->SetTextColor( color );
1298
1299     mImpl->mModel->mLogicalModel->mColorRuns.Clear();
1300
1301     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
1302
1303     mImpl->RequestRelayout();
1304   }
1305 }
1306
1307 const Vector4& Controller::GetDefaultColor() const
1308 {
1309   return mImpl->mTextColor;
1310 }
1311
1312 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
1313 {
1314   if( NULL != mImpl->mEventData )
1315   {
1316     mImpl->mEventData->mPlaceholderTextColor = textColor;
1317   }
1318
1319   if( mImpl->IsShowingPlaceholderText() )
1320   {
1321     mImpl->mModel->mVisualModel->SetTextColor( textColor );
1322     mImpl->RequestRelayout();
1323   }
1324 }
1325
1326 const Vector4& Controller::GetPlaceholderTextColor() const
1327 {
1328   if( NULL != mImpl->mEventData )
1329   {
1330     return mImpl->mEventData->mPlaceholderTextColor;
1331   }
1332
1333   return Color::BLACK;
1334 }
1335
1336 void Controller::SetShadowOffset( const Vector2& shadowOffset )
1337 {
1338   mImpl->mModel->mVisualModel->SetShadowOffset( shadowOffset );
1339
1340   mImpl->RequestRelayout();
1341 }
1342
1343 const Vector2& Controller::GetShadowOffset() const
1344 {
1345   return mImpl->mModel->mVisualModel->GetShadowOffset();
1346 }
1347
1348 void Controller::SetShadowColor( const Vector4& shadowColor )
1349 {
1350   mImpl->mModel->mVisualModel->SetShadowColor( shadowColor );
1351
1352   mImpl->RequestRelayout();
1353 }
1354
1355 const Vector4& Controller::GetShadowColor() const
1356 {
1357   return mImpl->mModel->mVisualModel->GetShadowColor();
1358 }
1359
1360 void Controller::SetShadowBlurRadius( const float& shadowBlurRadius )
1361 {
1362   if ( fabsf( GetShadowBlurRadius() - shadowBlurRadius ) > Math::MACHINE_EPSILON_1 )
1363   {
1364     mImpl->mModel->mVisualModel->SetShadowBlurRadius( shadowBlurRadius );
1365
1366     mImpl->RequestRelayout();
1367   }
1368 }
1369
1370 const float& Controller::GetShadowBlurRadius() const
1371 {
1372   return mImpl->mModel->mVisualModel->GetShadowBlurRadius();
1373 }
1374
1375 void Controller::SetUnderlineColor( const Vector4& color )
1376 {
1377   mImpl->mModel->mVisualModel->SetUnderlineColor( color );
1378
1379   mImpl->RequestRelayout();
1380 }
1381
1382 const Vector4& Controller::GetUnderlineColor() const
1383 {
1384   return mImpl->mModel->mVisualModel->GetUnderlineColor();
1385 }
1386
1387 void Controller::SetUnderlineEnabled( bool enabled )
1388 {
1389   mImpl->mModel->mVisualModel->SetUnderlineEnabled( enabled );
1390
1391   mImpl->RequestRelayout();
1392 }
1393
1394 bool Controller::IsUnderlineEnabled() const
1395 {
1396   return mImpl->mModel->mVisualModel->IsUnderlineEnabled();
1397 }
1398
1399 void Controller::SetUnderlineHeight( float height )
1400 {
1401   mImpl->mModel->mVisualModel->SetUnderlineHeight( height );
1402
1403   mImpl->RequestRelayout();
1404 }
1405
1406 float Controller::GetUnderlineHeight() const
1407 {
1408   return mImpl->mModel->mVisualModel->GetUnderlineHeight();
1409 }
1410
1411 void Controller::SetOutlineColor( const Vector4& color )
1412 {
1413   mImpl->mModel->mVisualModel->SetOutlineColor( color );
1414
1415   mImpl->RequestRelayout();
1416 }
1417
1418 const Vector4& Controller::GetOutlineColor() const
1419 {
1420   return mImpl->mModel->mVisualModel->GetOutlineColor();
1421 }
1422
1423 void Controller::SetOutlineWidth( uint16_t width )
1424 {
1425   mImpl->mModel->mVisualModel->SetOutlineWidth( width );
1426
1427   mImpl->RequestRelayout();
1428 }
1429
1430 uint16_t Controller::GetOutlineWidth() const
1431 {
1432   return mImpl->mModel->mVisualModel->GetOutlineWidth();
1433 }
1434
1435 void Controller::SetBackgroundColor( const Vector4& color )
1436 {
1437   mImpl->mModel->mVisualModel->SetBackgroundColor( color );
1438
1439   mImpl->RequestRelayout();
1440 }
1441
1442 const Vector4& Controller::GetBackgroundColor() const
1443 {
1444   return mImpl->mModel->mVisualModel->GetBackgroundColor();
1445 }
1446
1447 void Controller::SetBackgroundEnabled( bool enabled )
1448 {
1449   mImpl->mModel->mVisualModel->SetBackgroundEnabled( enabled );
1450
1451   mImpl->RequestRelayout();
1452 }
1453
1454 bool Controller::IsBackgroundEnabled() const
1455 {
1456   return mImpl->mModel->mVisualModel->IsBackgroundEnabled();
1457 }
1458
1459 void Controller::SetDefaultEmbossProperties( const std::string& embossProperties )
1460 {
1461   if( NULL == mImpl->mEmbossDefaults )
1462   {
1463     mImpl->mEmbossDefaults = new EmbossDefaults();
1464   }
1465
1466   mImpl->mEmbossDefaults->properties = embossProperties;
1467 }
1468
1469 const std::string& Controller::GetDefaultEmbossProperties() const
1470 {
1471   if( NULL != mImpl->mEmbossDefaults )
1472   {
1473     return mImpl->mEmbossDefaults->properties;
1474   }
1475
1476   return EMPTY_STRING;
1477 }
1478
1479 void Controller::SetDefaultOutlineProperties( const std::string& outlineProperties )
1480 {
1481   if( NULL == mImpl->mOutlineDefaults )
1482   {
1483     mImpl->mOutlineDefaults = new OutlineDefaults();
1484   }
1485
1486   mImpl->mOutlineDefaults->properties = outlineProperties;
1487 }
1488
1489 const std::string& Controller::GetDefaultOutlineProperties() const
1490 {
1491   if( NULL != mImpl->mOutlineDefaults )
1492   {
1493     return mImpl->mOutlineDefaults->properties;
1494   }
1495
1496   return EMPTY_STRING;
1497 }
1498
1499 bool Controller::SetDefaultLineSpacing( float lineSpacing )
1500 {
1501   if( std::fabs( lineSpacing - mImpl->mLayoutEngine.GetDefaultLineSpacing() ) > Math::MACHINE_EPSILON_1000 )
1502   {
1503     mImpl->mLayoutEngine.SetDefaultLineSpacing(lineSpacing);
1504     mImpl->mRecalculateNaturalSize = true;
1505     return true;
1506   }
1507   return false;
1508 }
1509
1510 float Controller::GetDefaultLineSpacing() const
1511 {
1512   return mImpl->mLayoutEngine.GetDefaultLineSpacing();
1513 }
1514
1515 bool Controller::SetDefaultLineSize( float lineSize )
1516 {
1517   if( std::fabs( lineSize - mImpl->mLayoutEngine.GetDefaultLineSize() ) > Math::MACHINE_EPSILON_1000 )
1518   {
1519     mImpl->mLayoutEngine.SetDefaultLineSize(lineSize);
1520     mImpl->mRecalculateNaturalSize = true;
1521     return true;
1522   }
1523   return false;
1524 }
1525
1526 float Controller::GetDefaultLineSize() const
1527 {
1528   return mImpl->mLayoutEngine.GetDefaultLineSize();
1529 }
1530
1531 void Controller::SetInputColor( const Vector4& color )
1532 {
1533   if( NULL != mImpl->mEventData )
1534   {
1535     mImpl->mEventData->mInputStyle.textColor = color;
1536     mImpl->mEventData->mInputStyle.isDefaultColor = false;
1537
1538     if( EventData::SELECTING == mImpl->mEventData->mState || EventData::EDITING == mImpl->mEventData->mState || EventData::INACTIVE == mImpl->mEventData->mState )
1539     {
1540       const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
1541
1542       // Get start and end position of selection
1543       const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
1544       const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
1545
1546       // Add the color run.
1547       const VectorBase::SizeType numberOfRuns = mImpl->mModel->mLogicalModel->mColorRuns.Count();
1548       mImpl->mModel->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1549
1550       ColorRun& colorRun = *( mImpl->mModel->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1551       colorRun.color = color;
1552       colorRun.characterRun.characterIndex = startOfSelectedText;
1553       colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
1554
1555       // Request to relayout.
1556       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
1557       mImpl->RequestRelayout();
1558
1559       mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1560       mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1561       mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1562     }
1563   }
1564 }
1565
1566 const Vector4& Controller::GetInputColor() const
1567 {
1568   if( NULL != mImpl->mEventData )
1569   {
1570     return mImpl->mEventData->mInputStyle.textColor;
1571   }
1572
1573   // Return the default text's color if there is no EventData.
1574   return mImpl->mTextColor;
1575
1576 }
1577
1578 void Controller::SetInputFontFamily( const std::string& fontFamily )
1579 {
1580   if( NULL != mImpl->mEventData )
1581   {
1582     mImpl->mEventData->mInputStyle.familyName = fontFamily;
1583     mImpl->mEventData->mInputStyle.isFamilyDefined = true;
1584
1585     if( EventData::SELECTING == mImpl->mEventData->mState || EventData::EDITING == mImpl->mEventData->mState || EventData::INACTIVE == mImpl->mEventData->mState )
1586     {
1587       CharacterIndex startOfSelectedText = 0u;
1588       Length lengthOfSelectedText = 0u;
1589
1590       if( EventData::SELECTING == mImpl->mEventData->mState )
1591       {
1592         // Update a font description run for the selecting state.
1593         FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1594                                                                               mImpl->mModel->mLogicalModel,
1595                                                                               startOfSelectedText,
1596                                                                               lengthOfSelectedText );
1597
1598         fontDescriptionRun.familyLength = fontFamily.size();
1599         fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1600         memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
1601         fontDescriptionRun.familyDefined = true;
1602
1603         // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1604
1605         mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1606         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1607         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1608       }
1609       else
1610       {
1611         mImpl->mTextUpdateInfo.mCharacterIndex = 0;
1612         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1613         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
1614       }
1615
1616       // Request to relayout.
1617       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1618                                                                VALIDATE_FONTS            |
1619                                                                SHAPE_TEXT                |
1620                                                                GET_GLYPH_METRICS         |
1621                                                                LAYOUT                    |
1622                                                                UPDATE_LAYOUT_SIZE        |
1623                                                                REORDER                   |
1624                                                                ALIGN );
1625       mImpl->mRecalculateNaturalSize = true;
1626       mImpl->RequestRelayout();
1627
1628       // As the font changes, recalculate the handle positions is needed.
1629       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1630       mImpl->mEventData->mUpdateRightSelectionPosition = true;
1631       mImpl->mEventData->mUpdateHighlightBox = true;
1632       mImpl->mEventData->mScrollAfterUpdatePosition = true;
1633     }
1634   }
1635 }
1636
1637 const std::string& Controller::GetInputFontFamily() const
1638 {
1639   if( NULL != mImpl->mEventData )
1640   {
1641     return mImpl->mEventData->mInputStyle.familyName;
1642   }
1643
1644   // Return the default font's family if there is no EventData.
1645   return GetDefaultFontFamily();
1646 }
1647
1648 void Controller::SetInputFontWeight( FontWeight weight )
1649 {
1650   if( NULL != mImpl->mEventData )
1651   {
1652     mImpl->mEventData->mInputStyle.weight = weight;
1653     mImpl->mEventData->mInputStyle.isWeightDefined = true;
1654
1655     if( EventData::SELECTING == mImpl->mEventData->mState || EventData::EDITING == mImpl->mEventData->mState || EventData::INACTIVE == mImpl->mEventData->mState )
1656     {
1657       CharacterIndex startOfSelectedText = 0u;
1658       Length lengthOfSelectedText = 0u;
1659
1660       if( EventData::SELECTING == mImpl->mEventData->mState )
1661       {
1662         // Update a font description run for the selecting state.
1663         FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1664                                                                               mImpl->mModel->mLogicalModel,
1665                                                                               startOfSelectedText,
1666                                                                               lengthOfSelectedText );
1667
1668         fontDescriptionRun.weight = weight;
1669         fontDescriptionRun.weightDefined = true;
1670
1671         mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1672         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1673         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1674       }
1675       else
1676       {
1677         mImpl->mTextUpdateInfo.mCharacterIndex = 0;
1678         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1679         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
1680       }
1681
1682       // Request to relayout.
1683       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1684                                                                VALIDATE_FONTS            |
1685                                                                SHAPE_TEXT                |
1686                                                                GET_GLYPH_METRICS         |
1687                                                                LAYOUT                    |
1688                                                                UPDATE_LAYOUT_SIZE        |
1689                                                                REORDER                   |
1690                                                                ALIGN );
1691       mImpl->mRecalculateNaturalSize = true;
1692       mImpl->RequestRelayout();
1693
1694       // As the font might change, recalculate the handle positions is needed.
1695       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1696       mImpl->mEventData->mUpdateRightSelectionPosition = true;
1697       mImpl->mEventData->mUpdateHighlightBox = true;
1698       mImpl->mEventData->mScrollAfterUpdatePosition = true;
1699     }
1700   }
1701 }
1702
1703 bool Controller::IsInputFontWeightDefined() const
1704 {
1705   bool defined = false;
1706
1707   if( NULL != mImpl->mEventData )
1708   {
1709     defined = mImpl->mEventData->mInputStyle.isWeightDefined;
1710   }
1711
1712   return defined;
1713 }
1714
1715 FontWeight Controller::GetInputFontWeight() const
1716 {
1717   if( NULL != mImpl->mEventData )
1718   {
1719     return mImpl->mEventData->mInputStyle.weight;
1720   }
1721
1722   return GetDefaultFontWeight();
1723 }
1724
1725 void Controller::SetInputFontWidth( FontWidth width )
1726 {
1727   if( NULL != mImpl->mEventData )
1728   {
1729     mImpl->mEventData->mInputStyle.width = width;
1730     mImpl->mEventData->mInputStyle.isWidthDefined = true;
1731
1732     if( EventData::SELECTING == mImpl->mEventData->mState || EventData::EDITING == mImpl->mEventData->mState || EventData::INACTIVE == mImpl->mEventData->mState )
1733     {
1734       CharacterIndex startOfSelectedText = 0u;
1735       Length lengthOfSelectedText = 0u;
1736
1737       if( EventData::SELECTING == mImpl->mEventData->mState )
1738       {
1739         // Update a font description run for the selecting state.
1740         FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1741                                                                               mImpl->mModel->mLogicalModel,
1742                                                                               startOfSelectedText,
1743                                                                               lengthOfSelectedText );
1744
1745         fontDescriptionRun.width = width;
1746         fontDescriptionRun.widthDefined = true;
1747
1748         mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1749         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1750         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1751       }
1752       else
1753       {
1754         mImpl->mTextUpdateInfo.mCharacterIndex = 0;
1755         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1756         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
1757       }
1758
1759       // Request to relayout.
1760       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1761                                                                VALIDATE_FONTS            |
1762                                                                SHAPE_TEXT                |
1763                                                                GET_GLYPH_METRICS         |
1764                                                                LAYOUT                    |
1765                                                                UPDATE_LAYOUT_SIZE        |
1766                                                                REORDER                   |
1767                                                                ALIGN );
1768       mImpl->mRecalculateNaturalSize = true;
1769       mImpl->RequestRelayout();
1770
1771       // As the font might change, recalculate the handle positions is needed.
1772       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1773       mImpl->mEventData->mUpdateRightSelectionPosition = true;
1774       mImpl->mEventData->mUpdateHighlightBox = true;
1775       mImpl->mEventData->mScrollAfterUpdatePosition = true;
1776     }
1777   }
1778 }
1779
1780 bool Controller::IsInputFontWidthDefined() const
1781 {
1782   bool defined = false;
1783
1784   if( NULL != mImpl->mEventData )
1785   {
1786     defined = mImpl->mEventData->mInputStyle.isWidthDefined;
1787   }
1788
1789   return defined;
1790 }
1791
1792 FontWidth Controller::GetInputFontWidth() const
1793 {
1794   if( NULL != mImpl->mEventData )
1795   {
1796     return mImpl->mEventData->mInputStyle.width;
1797   }
1798
1799   return GetDefaultFontWidth();
1800 }
1801
1802 void Controller::SetInputFontSlant( FontSlant slant )
1803 {
1804   if( NULL != mImpl->mEventData )
1805   {
1806     mImpl->mEventData->mInputStyle.slant = slant;
1807     mImpl->mEventData->mInputStyle.isSlantDefined = true;
1808
1809     if( EventData::SELECTING == mImpl->mEventData->mState || EventData::EDITING == mImpl->mEventData->mState || EventData::INACTIVE == mImpl->mEventData->mState )
1810     {
1811       CharacterIndex startOfSelectedText = 0u;
1812       Length lengthOfSelectedText = 0u;
1813
1814       if( EventData::SELECTING == mImpl->mEventData->mState )
1815       {
1816         // Update a font description run for the selecting state.
1817         FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1818                                                                               mImpl->mModel->mLogicalModel,
1819                                                                               startOfSelectedText,
1820                                                                               lengthOfSelectedText );
1821
1822         fontDescriptionRun.slant = slant;
1823         fontDescriptionRun.slantDefined = true;
1824
1825         mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1826         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1827         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1828       }
1829       else
1830       {
1831         mImpl->mTextUpdateInfo.mCharacterIndex = 0;
1832         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1833         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
1834       }
1835
1836       // Request to relayout.
1837       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1838                                                                VALIDATE_FONTS            |
1839                                                                SHAPE_TEXT                |
1840                                                                GET_GLYPH_METRICS         |
1841                                                                LAYOUT                    |
1842                                                                UPDATE_LAYOUT_SIZE        |
1843                                                                REORDER                   |
1844                                                                ALIGN );
1845       mImpl->mRecalculateNaturalSize = true;
1846       mImpl->RequestRelayout();
1847
1848       // As the font might change, recalculate the handle positions is needed.
1849       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1850       mImpl->mEventData->mUpdateRightSelectionPosition = true;
1851       mImpl->mEventData->mUpdateHighlightBox = true;
1852       mImpl->mEventData->mScrollAfterUpdatePosition = true;
1853     }
1854   }
1855 }
1856
1857 bool Controller::IsInputFontSlantDefined() const
1858 {
1859   bool defined = false;
1860
1861   if( NULL != mImpl->mEventData )
1862   {
1863     defined = mImpl->mEventData->mInputStyle.isSlantDefined;
1864   }
1865
1866   return defined;
1867 }
1868
1869 FontSlant Controller::GetInputFontSlant() const
1870 {
1871   if( NULL != mImpl->mEventData )
1872   {
1873     return mImpl->mEventData->mInputStyle.slant;
1874   }
1875
1876   return GetDefaultFontSlant();
1877 }
1878
1879 void Controller::SetInputFontPointSize( float size )
1880 {
1881   if( NULL != mImpl->mEventData )
1882   {
1883     mImpl->mEventData->mInputStyle.size = size;
1884     mImpl->mEventData->mInputStyle.isSizeDefined = true;
1885
1886     if( EventData::SELECTING == mImpl->mEventData->mState || EventData::EDITING == mImpl->mEventData->mState || EventData::INACTIVE == mImpl->mEventData->mState )
1887     {
1888       CharacterIndex startOfSelectedText = 0u;
1889       Length lengthOfSelectedText = 0u;
1890
1891       if( EventData::SELECTING == mImpl->mEventData->mState )
1892       {
1893         // Update a font description run for the selecting state.
1894         FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
1895                                                                               mImpl->mModel->mLogicalModel,
1896                                                                               startOfSelectedText,
1897                                                                               lengthOfSelectedText );
1898
1899         fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
1900         fontDescriptionRun.sizeDefined = true;
1901
1902         mImpl->mTextUpdateInfo.mCharacterIndex = startOfSelectedText;
1903         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = lengthOfSelectedText;
1904         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = lengthOfSelectedText;
1905       }
1906       else
1907       {
1908         mImpl->mTextUpdateInfo.mCharacterIndex = 0;
1909         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
1910         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
1911       }
1912
1913       // Request to relayout.
1914       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1915                                                                VALIDATE_FONTS            |
1916                                                                SHAPE_TEXT                |
1917                                                                GET_GLYPH_METRICS         |
1918                                                                LAYOUT                    |
1919                                                                UPDATE_LAYOUT_SIZE        |
1920                                                                REORDER                   |
1921                                                                ALIGN );
1922       mImpl->mRecalculateNaturalSize = true;
1923       mImpl->RequestRelayout();
1924
1925       // As the font might change, recalculate the handle positions is needed.
1926       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
1927       mImpl->mEventData->mUpdateRightSelectionPosition = true;
1928       mImpl->mEventData->mUpdateHighlightBox = true;
1929       mImpl->mEventData->mScrollAfterUpdatePosition = true;
1930     }
1931   }
1932 }
1933
1934 float Controller::GetInputFontPointSize() const
1935 {
1936   if( NULL != mImpl->mEventData )
1937   {
1938     return mImpl->mEventData->mInputStyle.size;
1939   }
1940
1941   // Return the default font's point size if there is no EventData.
1942   return GetDefaultFontSize( Text::Controller::POINT_SIZE );
1943 }
1944
1945 void Controller::SetInputLineSpacing( float lineSpacing )
1946 {
1947   if( NULL != mImpl->mEventData )
1948   {
1949     mImpl->mEventData->mInputStyle.lineSpacing = lineSpacing;
1950     mImpl->mEventData->mInputStyle.isLineSpacingDefined = true;
1951   }
1952 }
1953
1954 float Controller::GetInputLineSpacing() const
1955 {
1956   if( NULL != mImpl->mEventData )
1957   {
1958     return mImpl->mEventData->mInputStyle.lineSpacing;
1959   }
1960
1961   return 0.f;
1962 }
1963
1964 void Controller::SetInputShadowProperties( const std::string& shadowProperties )
1965 {
1966   if( NULL != mImpl->mEventData )
1967   {
1968     mImpl->mEventData->mInputStyle.shadowProperties = shadowProperties;
1969   }
1970 }
1971
1972 const std::string& Controller::GetInputShadowProperties() const
1973 {
1974   if( NULL != mImpl->mEventData )
1975   {
1976     return mImpl->mEventData->mInputStyle.shadowProperties;
1977   }
1978
1979   return EMPTY_STRING;
1980 }
1981
1982 void Controller::SetInputUnderlineProperties( const std::string& underlineProperties )
1983 {
1984   if( NULL != mImpl->mEventData )
1985   {
1986     mImpl->mEventData->mInputStyle.underlineProperties = underlineProperties;
1987   }
1988 }
1989
1990 const std::string& Controller::GetInputUnderlineProperties() const
1991 {
1992   if( NULL != mImpl->mEventData )
1993   {
1994     return mImpl->mEventData->mInputStyle.underlineProperties;
1995   }
1996
1997   return EMPTY_STRING;
1998 }
1999
2000 void Controller::SetInputEmbossProperties( const std::string& embossProperties )
2001 {
2002   if( NULL != mImpl->mEventData )
2003   {
2004     mImpl->mEventData->mInputStyle.embossProperties = embossProperties;
2005   }
2006 }
2007
2008 const std::string& Controller::GetInputEmbossProperties() const
2009 {
2010   if( NULL != mImpl->mEventData )
2011   {
2012     return mImpl->mEventData->mInputStyle.embossProperties;
2013   }
2014
2015   return GetDefaultEmbossProperties();
2016 }
2017
2018 void Controller::SetInputOutlineProperties( const std::string& outlineProperties )
2019 {
2020   if( NULL != mImpl->mEventData )
2021   {
2022     mImpl->mEventData->mInputStyle.outlineProperties = outlineProperties;
2023   }
2024 }
2025
2026 const std::string& Controller::GetInputOutlineProperties() const
2027 {
2028   if( NULL != mImpl->mEventData )
2029   {
2030     return mImpl->mEventData->mInputStyle.outlineProperties;
2031   }
2032
2033   return GetDefaultOutlineProperties();
2034 }
2035
2036 void Controller::SetInputModePassword( bool passwordInput )
2037 {
2038   if( NULL != mImpl->mEventData )
2039   {
2040     mImpl->mEventData->mPasswordInput = passwordInput;
2041   }
2042 }
2043
2044 bool Controller::IsInputModePassword()
2045 {
2046   if( NULL != mImpl->mEventData )
2047   {
2048     return mImpl->mEventData->mPasswordInput;
2049   }
2050   return false;
2051 }
2052
2053 void Controller::SetNoTextDoubleTapAction( NoTextTap::Action action )
2054 {
2055   if( NULL != mImpl->mEventData )
2056   {
2057     mImpl->mEventData->mDoubleTapAction = action;
2058   }
2059 }
2060
2061 Controller::NoTextTap::Action Controller::GetNoTextDoubleTapAction() const
2062 {
2063   NoTextTap::Action action = NoTextTap::NO_ACTION;
2064
2065   if( NULL != mImpl->mEventData )
2066   {
2067     action = mImpl->mEventData->mDoubleTapAction;
2068   }
2069
2070   return action;
2071 }
2072
2073 void Controller::SetNoTextLongPressAction( NoTextTap::Action action )
2074 {
2075   if( NULL != mImpl->mEventData )
2076   {
2077     mImpl->mEventData->mLongPressAction = action;
2078   }
2079 }
2080
2081 Controller::NoTextTap::Action Controller::GetNoTextLongPressAction() const
2082 {
2083   NoTextTap::Action action = NoTextTap::NO_ACTION;
2084
2085   if( NULL != mImpl->mEventData )
2086   {
2087     action = mImpl->mEventData->mLongPressAction;
2088   }
2089
2090   return action;
2091 }
2092
2093 bool Controller::IsUnderlineSetByString()
2094 {
2095   return mImpl->mUnderlineSetByString;
2096 }
2097
2098 void Controller::UnderlineSetByString( bool setByString )
2099 {
2100   mImpl->mUnderlineSetByString = setByString;
2101 }
2102
2103 bool Controller::IsShadowSetByString()
2104 {
2105   return mImpl->mShadowSetByString;
2106 }
2107
2108 void Controller::ShadowSetByString( bool setByString )
2109 {
2110   mImpl->mShadowSetByString = setByString;
2111 }
2112
2113 bool Controller::IsOutlineSetByString()
2114 {
2115   return mImpl->mOutlineSetByString;
2116 }
2117
2118 void Controller::OutlineSetByString( bool setByString )
2119 {
2120   mImpl->mOutlineSetByString = setByString;
2121 }
2122
2123 bool Controller::IsFontStyleSetByString()
2124 {
2125   return mImpl->mFontStyleSetByString;
2126 }
2127
2128 void Controller::FontStyleSetByString( bool setByString )
2129 {
2130   mImpl->mFontStyleSetByString = setByString;
2131 }
2132
2133 // public : Queries & retrieves.
2134
2135 Layout::Engine& Controller::GetLayoutEngine()
2136 {
2137   return mImpl->mLayoutEngine;
2138 }
2139
2140 View& Controller::GetView()
2141 {
2142   return mImpl->mView;
2143 }
2144
2145 Vector3 Controller::GetNaturalSize()
2146 {
2147   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
2148   Vector3 naturalSize;
2149
2150   // Make sure the model is up-to-date before layouting
2151   ProcessModifyEvents();
2152
2153   if( mImpl->mRecalculateNaturalSize )
2154   {
2155     // Operations that can be done only once until the text changes.
2156     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
2157                                                                            GET_SCRIPTS       |
2158                                                                            VALIDATE_FONTS    |
2159                                                                            GET_LINE_BREAKS   |
2160                                                                            BIDI_INFO         |
2161                                                                            SHAPE_TEXT        |
2162                                                                            GET_GLYPH_METRICS );
2163
2164     // Set the update info to relayout the whole text.
2165     mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
2166     mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mModel->mLogicalModel->mText.Count();
2167
2168     // Make sure the model is up-to-date before layouting
2169     mImpl->UpdateModel( onlyOnceOperations );
2170
2171     // Layout the text for the new width.
2172     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT | REORDER );
2173
2174     // Store the actual control's size to restore later.
2175     const Size actualControlSize = mImpl->mModel->mVisualModel->mControlSize;
2176
2177     DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
2178                 static_cast<OperationsMask>( onlyOnceOperations |
2179                                              LAYOUT | REORDER ),
2180                 naturalSize.GetVectorXY() );
2181
2182     // Do not do again the only once operations.
2183     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
2184
2185     // Do the size related operations again.
2186     const OperationsMask sizeOperations =  static_cast<OperationsMask>( LAYOUT |
2187                                                                         ALIGN  |
2188                                                                         REORDER );
2189     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
2190
2191     // Stores the natural size to avoid recalculate it again
2192     // unless the text/style changes.
2193     mImpl->mModel->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
2194
2195     mImpl->mRecalculateNaturalSize = false;
2196
2197     // Clear the update info. This info will be set the next time the text is updated.
2198     mImpl->mTextUpdateInfo.Clear();
2199     mImpl->mTextUpdateInfo.mClearAll = true;
2200
2201     // Restore the actual control's size.
2202     mImpl->mModel->mVisualModel->mControlSize = actualControlSize;
2203
2204     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
2205   }
2206   else
2207   {
2208     naturalSize = mImpl->mModel->mVisualModel->GetNaturalSize();
2209
2210     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
2211   }
2212
2213   naturalSize.x = ConvertToEven( naturalSize.x );
2214   naturalSize.y = ConvertToEven( naturalSize.y );
2215
2216   return naturalSize;
2217 }
2218
2219 bool Controller::CheckForTextFit( float pointSize, Size& layoutSize )
2220 {
2221   Size textSize;
2222   mImpl->mFontDefaults->mFitPointSize = pointSize;
2223   mImpl->mFontDefaults->sizeDefined = true;
2224   ClearFontData();
2225
2226   // Operations that can be done only once until the text changes.
2227   const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
2228                                                                               GET_SCRIPTS |
2229                                                                            VALIDATE_FONTS |
2230                                                                           GET_LINE_BREAKS |
2231                                                                                 BIDI_INFO |
2232                                                                                 SHAPE_TEXT|
2233                                                                          GET_GLYPH_METRICS );
2234
2235   mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
2236   mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mModel->mLogicalModel->mText.Count();
2237
2238   // Make sure the model is up-to-date before layouting
2239   mImpl->UpdateModel( onlyOnceOperations );
2240
2241   DoRelayout( Size( layoutSize.width, MAX_FLOAT ),
2242               static_cast<OperationsMask>( onlyOnceOperations | LAYOUT),
2243               textSize);
2244
2245   // Clear the update info. This info will be set the next time the text is updated.
2246   mImpl->mTextUpdateInfo.Clear();
2247   mImpl->mTextUpdateInfo.mClearAll = true;
2248
2249   if( textSize.width > layoutSize.width || textSize.height > layoutSize.height )
2250   {
2251     return false;
2252   }
2253   return true;
2254 }
2255
2256 void Controller::FitPointSizeforLayout( Size layoutSize )
2257 {
2258   const OperationsMask operations  = mImpl->mOperationsPending;
2259   if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) || mImpl->mTextFitContentSize != layoutSize )
2260   {
2261     bool actualellipsis = mImpl->mModel->mElideEnabled;
2262     float minPointSize = mImpl->mTextFitMinSize;
2263     float maxPointSize = mImpl->mTextFitMaxSize;
2264     float pointInterval = mImpl->mTextFitStepSize;
2265
2266     mImpl->mModel->mElideEnabled = false;
2267     Vector<float> pointSizeArray;
2268
2269     // check zero value
2270     if( pointInterval < 1.f )
2271     {
2272       mImpl->mTextFitStepSize = pointInterval = 1.0f;
2273     }
2274
2275     pointSizeArray.Reserve( static_cast< unsigned int >( ceil( ( maxPointSize - minPointSize ) / pointInterval ) ) );
2276
2277     for( float i = minPointSize; i < maxPointSize; i += pointInterval )
2278     {
2279       pointSizeArray.PushBack( i );
2280     }
2281
2282     pointSizeArray.PushBack( maxPointSize );
2283
2284     int bestSizeIndex = 0;
2285     int min = bestSizeIndex + 1;
2286     int max = pointSizeArray.Size() - 1;
2287     while( min <= max )
2288     {
2289       int destI = ( min + max ) / 2;
2290
2291       if( CheckForTextFit( pointSizeArray[destI], layoutSize ) )
2292       {
2293         bestSizeIndex = min;
2294         min = destI + 1;
2295       }
2296       else
2297       {
2298         max = destI - 1;
2299         bestSizeIndex = max;
2300       }
2301     }
2302
2303     mImpl->mModel->mElideEnabled = actualellipsis;
2304     mImpl->mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
2305     mImpl->mFontDefaults->sizeDefined = true;
2306     ClearFontData();
2307   }
2308 }
2309
2310 float Controller::GetHeightForWidth( float width )
2311 {
2312   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
2313   // Make sure the model is up-to-date before layouting
2314   ProcessModifyEvents();
2315
2316   Size layoutSize;
2317   if( fabsf( width - mImpl->mModel->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 ||
2318                                                          mImpl->mTextUpdateInfo.mFullRelayoutNeeded ||
2319                                                          mImpl->mTextUpdateInfo.mClearAll            )
2320   {
2321     // Operations that can be done only once until the text changes.
2322     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
2323                                                                            GET_SCRIPTS       |
2324                                                                            VALIDATE_FONTS    |
2325                                                                            GET_LINE_BREAKS   |
2326                                                                            BIDI_INFO         |
2327                                                                            SHAPE_TEXT        |
2328                                                                            GET_GLYPH_METRICS );
2329
2330     // Set the update info to relayout the whole text.
2331     mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
2332     mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mModel->mLogicalModel->mText.Count();
2333
2334     // Make sure the model is up-to-date before layouting
2335     mImpl->UpdateModel( onlyOnceOperations );
2336
2337
2338     // Layout the text for the new width.
2339     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | LAYOUT );
2340
2341     // Store the actual control's width.
2342     const float actualControlWidth = mImpl->mModel->mVisualModel->mControlSize.width;
2343
2344     DoRelayout( Size( width, MAX_FLOAT ),
2345                 static_cast<OperationsMask>( onlyOnceOperations |
2346                                              LAYOUT ),
2347                 layoutSize );
2348
2349     // Do not do again the only once operations.
2350     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
2351
2352     // Do the size related operations again.
2353     const OperationsMask sizeOperations =  static_cast<OperationsMask>( LAYOUT |
2354                                                                         ALIGN  |
2355                                                                         REORDER );
2356
2357     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
2358
2359     // Clear the update info. This info will be set the next time the text is updated.
2360     mImpl->mTextUpdateInfo.Clear();
2361     mImpl->mTextUpdateInfo.mClearAll = true;
2362
2363     // Restore the actual control's width.
2364     mImpl->mModel->mVisualModel->mControlSize.width = actualControlWidth;
2365
2366     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
2367   }
2368   else
2369   {
2370     layoutSize = mImpl->mModel->mVisualModel->GetLayoutSize();
2371     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
2372   }
2373
2374   return layoutSize.height;
2375 }
2376
2377 int Controller::GetLineCount( float width )
2378 {
2379   GetHeightForWidth( width );
2380   int numberofLines = mImpl->mModel->GetNumberOfLines();
2381   return numberofLines;
2382 }
2383
2384 const ModelInterface* const Controller::GetTextModel() const
2385 {
2386   return mImpl->mModel.Get();
2387 }
2388
2389 float Controller::GetScrollAmountByUserInput()
2390 {
2391   float scrollAmount = 0.0f;
2392
2393   if (NULL != mImpl->mEventData && mImpl->mEventData->mCheckScrollAmount)
2394   {
2395     scrollAmount = mImpl->mModel->mScrollPosition.y -  mImpl->mModel->mScrollPositionLast.y;
2396     mImpl->mEventData->mCheckScrollAmount = false;
2397   }
2398   return scrollAmount;
2399 }
2400
2401 bool Controller::GetTextScrollInfo( float& scrollPosition, float& controlHeight, float& layoutHeight )
2402 {
2403   const Vector2& layout = mImpl->mModel->mVisualModel->GetLayoutSize();
2404   bool isScrolled;
2405
2406   controlHeight = mImpl->mModel->mVisualModel->mControlSize.height;
2407   layoutHeight = layout.height;
2408   scrollPosition = mImpl->mModel->mScrollPosition.y;
2409   isScrolled = !Equals( mImpl->mModel->mScrollPosition.y, mImpl->mModel->mScrollPositionLast.y, Math::MACHINE_EPSILON_1 );
2410   return isScrolled;
2411 }
2412
2413 void Controller::SetHiddenInputOption(const Property::Map& options )
2414 {
2415   if( NULL == mImpl->mHiddenInput )
2416   {
2417     mImpl->mHiddenInput = new HiddenText( this );
2418   }
2419   mImpl->mHiddenInput->SetProperties(options);
2420 }
2421
2422 void Controller::GetHiddenInputOption(Property::Map& options )
2423 {
2424   if( NULL != mImpl->mHiddenInput )
2425   {
2426     mImpl->mHiddenInput->GetProperties(options);
2427   }
2428 }
2429
2430 void Controller::SetPlaceholderProperty( const Property::Map& map )
2431 {
2432   const Property::Map::SizeType count = map.Count();
2433
2434   for( Property::Map::SizeType position = 0; position < count; ++position )
2435   {
2436     KeyValuePair keyValue = map.GetKeyValue( position );
2437     Property::Key& key = keyValue.first;
2438     Property::Value& value = keyValue.second;
2439
2440     if( key == Toolkit::Text::PlaceHolder::Property::TEXT  || key == PLACEHOLDER_TEXT )
2441     {
2442       std::string text = "";
2443       value.Get( text );
2444       SetPlaceholderText( Controller::PLACEHOLDER_TYPE_INACTIVE, text );
2445     }
2446     else if( key == Toolkit::Text::PlaceHolder::Property::TEXT_FOCUSED || key == PLACEHOLDER_TEXT_FOCUSED )
2447     {
2448       std::string text = "";
2449       value.Get( text );
2450       SetPlaceholderText( Controller::PLACEHOLDER_TYPE_ACTIVE, text );
2451     }
2452     else if( key == Toolkit::Text::PlaceHolder::Property::COLOR || key == PLACEHOLDER_COLOR )
2453     {
2454       Vector4 textColor;
2455       value.Get( textColor );
2456       if( GetPlaceholderTextColor() != textColor )
2457       {
2458         SetPlaceholderTextColor( textColor );
2459       }
2460     }
2461     else if( key == Toolkit::Text::PlaceHolder::Property::FONT_FAMILY || key == PLACEHOLDER_FONT_FAMILY )
2462     {
2463       std::string fontFamily = "";
2464       value.Get( fontFamily );
2465       SetPlaceholderFontFamily( fontFamily );
2466     }
2467     else if( key == Toolkit::Text::PlaceHolder::Property::FONT_STYLE || key == PLACEHOLDER_FONT_STYLE )
2468     {
2469       SetFontStyleProperty( this, value, Text::FontStyle::PLACEHOLDER );
2470     }
2471     else if( key == Toolkit::Text::PlaceHolder::Property::POINT_SIZE || key == PLACEHOLDER_POINT_SIZE )
2472     {
2473       float pointSize;
2474       value.Get( pointSize );
2475       if( !Equals( GetPlaceholderTextFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
2476       {
2477         SetPlaceholderTextFontSize( pointSize, Text::Controller::POINT_SIZE );
2478       }
2479     }
2480     else if( key == Toolkit::Text::PlaceHolder::Property::PIXEL_SIZE || key == PLACEHOLDER_PIXEL_SIZE )
2481     {
2482       float pixelSize;
2483       value.Get( pixelSize );
2484       if( !Equals( GetPlaceholderTextFontSize( Text::Controller::PIXEL_SIZE ), pixelSize ) )
2485       {
2486         SetPlaceholderTextFontSize( pixelSize, Text::Controller::PIXEL_SIZE );
2487       }
2488     }
2489     else if( key == Toolkit::Text::PlaceHolder::Property::ELLIPSIS || key == PLACEHOLDER_ELLIPSIS )
2490     {
2491       bool ellipsis;
2492       value.Get( ellipsis );
2493       SetPlaceholderTextElideEnabled( ellipsis );
2494     }
2495   }
2496 }
2497
2498 void Controller::GetPlaceholderProperty( Property::Map& map )
2499 {
2500   if( NULL != mImpl->mEventData )
2501   {
2502     if( !mImpl->mEventData->mPlaceholderTextActive.empty() )
2503     {
2504       map[ Text::PlaceHolder::Property::TEXT_FOCUSED ] = mImpl->mEventData->mPlaceholderTextActive;
2505     }
2506     if( !mImpl->mEventData->mPlaceholderTextInactive.empty() )
2507     {
2508       map[ Text::PlaceHolder::Property::TEXT ] = mImpl->mEventData->mPlaceholderTextInactive;
2509     }
2510
2511     map[ Text::PlaceHolder::Property::COLOR ] = mImpl->mEventData->mPlaceholderTextColor;
2512     map[ Text::PlaceHolder::Property::FONT_FAMILY ] = GetPlaceholderFontFamily();
2513
2514     Property::Value fontStyleMapGet;
2515     GetFontStyleProperty( this, fontStyleMapGet, Text::FontStyle::PLACEHOLDER );
2516     map[ Text::PlaceHolder::Property::FONT_STYLE ] = fontStyleMapGet;
2517
2518     // Choose font size : POINT_SIZE or PIXEL_SIZE
2519     if( !mImpl->mEventData->mIsPlaceholderPixelSize )
2520     {
2521       map[ Text::PlaceHolder::Property::POINT_SIZE ] = GetPlaceholderTextFontSize( Text::Controller::POINT_SIZE );
2522     }
2523     else
2524     {
2525       map[ Text::PlaceHolder::Property::PIXEL_SIZE ] = GetPlaceholderTextFontSize( Text::Controller::PIXEL_SIZE );
2526     }
2527
2528     if( mImpl->mEventData->mPlaceholderEllipsisFlag )
2529     {
2530       map[ Text::PlaceHolder::Property::ELLIPSIS ] = IsPlaceholderTextElideEnabled();
2531     }
2532   }
2533 }
2534
2535 Toolkit::DevelText::TextDirection::Type Controller::GetTextDirection()
2536 {
2537   // Make sure the model is up-to-date before layouting
2538   ProcessModifyEvents();
2539
2540   if ( mImpl->mUpdateTextDirection )
2541   {
2542     // Operations that can be done only once until the text changes.
2543     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
2544                                                                            GET_SCRIPTS       |
2545                                                                            VALIDATE_FONTS    |
2546                                                                            GET_LINE_BREAKS   |
2547                                                                            BIDI_INFO         |
2548                                                                            SHAPE_TEXT        |
2549                                                                            GET_GLYPH_METRICS );
2550
2551     // Set the update info to relayout the whole text.
2552     mImpl->mTextUpdateInfo.mParagraphCharacterIndex = 0u;
2553     mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters = mImpl->mModel->mLogicalModel->mText.Count();
2554
2555     // Make sure the model is up-to-date before layouting
2556     mImpl->UpdateModel( onlyOnceOperations );
2557
2558     Vector3 naturalSize;
2559     DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
2560                 static_cast<OperationsMask>( onlyOnceOperations |
2561                                              LAYOUT | REORDER | UPDATE_DIRECTION ),
2562                 naturalSize.GetVectorXY() );
2563
2564     // Do not do again the only once operations.
2565     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
2566
2567     // Clear the update info. This info will be set the next time the text is updated.
2568     mImpl->mTextUpdateInfo.Clear();
2569
2570     // FullRelayoutNeeded should be true because DoRelayout is MAX_FLOAT, MAX_FLOAT.
2571     mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2572
2573     mImpl->mUpdateTextDirection = false;
2574   }
2575
2576   return mImpl->mIsTextDirectionRTL ? Toolkit::DevelText::TextDirection::RIGHT_TO_LEFT : Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT;
2577 }
2578
2579 Toolkit::DevelText::VerticalLineAlignment::Type Controller::GetVerticalLineAlignment() const
2580 {
2581   return mImpl->mModel->GetVerticalLineAlignment();
2582 }
2583
2584 void Controller::SetVerticalLineAlignment( Toolkit::DevelText::VerticalLineAlignment::Type alignment )
2585 {
2586   mImpl->mModel->mVerticalLineAlignment = alignment;
2587 }
2588
2589 // public : Relayout.
2590
2591 Controller::UpdateTextType Controller::Relayout( const Size& size, Dali::LayoutDirection::Type layoutDirection )
2592 {
2593   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", this, size.width, size.height, mImpl->mIsAutoScrollEnabled ?"true":"false"  );
2594
2595   UpdateTextType updateTextType = NONE_UPDATED;
2596
2597   if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
2598   {
2599     if( 0u != mImpl->mModel->mVisualModel->mGlyphPositions.Count() )
2600     {
2601       mImpl->mModel->mVisualModel->mGlyphPositions.Clear();
2602       updateTextType = MODEL_UPDATED;
2603     }
2604
2605     // Clear the update info. This info will be set the next time the text is updated.
2606     mImpl->mTextUpdateInfo.Clear();
2607
2608     // Not worth to relayout if width or height is equal to zero.
2609     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
2610
2611     return updateTextType;
2612   }
2613
2614   // Whether a new size has been set.
2615   const bool newSize = ( size != mImpl->mModel->mVisualModel->mControlSize );
2616
2617   if( newSize )
2618   {
2619     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mModel->mVisualModel->mControlSize.width, mImpl->mModel->mVisualModel->mControlSize.height );
2620
2621     if( ( 0 == mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd ) &&
2622         ( 0 == mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters ) &&
2623         ( ( mImpl->mModel->mVisualModel->mControlSize.width < Math::MACHINE_EPSILON_1000 ) || ( mImpl->mModel->mVisualModel->mControlSize.height < Math::MACHINE_EPSILON_1000 ) ) )
2624     {
2625       mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
2626     }
2627
2628     // Layout operations that need to be done if the size changes.
2629     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2630                                                              LAYOUT                    |
2631                                                              ALIGN                     |
2632                                                              UPDATE_LAYOUT_SIZE        |
2633                                                              REORDER );
2634     // Set the update info to relayout the whole text.
2635     mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2636     mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2637
2638     // Store the size used to layout the text.
2639     mImpl->mModel->mVisualModel->mControlSize = size;
2640   }
2641
2642   // Whether there are modify events.
2643   if( 0u != mImpl->mModifyEvents.Count() )
2644   {
2645     // Style operations that need to be done if the text is modified.
2646     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2647                                                              COLOR );
2648   }
2649
2650   // Set the update info to elide the text.
2651   if( mImpl->mModel->mElideEnabled ||
2652       ( ( NULL != mImpl->mEventData ) && mImpl->mEventData->mIsPlaceholderElideEnabled ) )
2653   {
2654     // Update Text layout for applying elided
2655     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2656                                                              ALIGN                     |
2657                                                              LAYOUT                    |
2658                                                              UPDATE_LAYOUT_SIZE        |
2659                                                              REORDER );
2660     mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
2661     mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
2662   }
2663
2664   if( mImpl->mModel->mMatchSystemLanguageDirection  && mImpl->mLayoutDirection != layoutDirection )
2665   {
2666     // Clear the update info. This info will be set the next time the text is updated.
2667     mImpl->mTextUpdateInfo.mClearAll = true;
2668     // Apply modifications to the model
2669     // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
2670     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
2671                                                              GET_GLYPH_METRICS         |
2672                                                              SHAPE_TEXT                |
2673                                                              UPDATE_DIRECTION          |
2674                                                              LAYOUT                    |
2675                                                              BIDI_INFO                 |
2676                                                              REORDER );
2677     mImpl->mLayoutDirection = layoutDirection;
2678   }
2679
2680   // Make sure the model is up-to-date before layouting.
2681   ProcessModifyEvents();
2682   bool updated = mImpl->UpdateModel( mImpl->mOperationsPending );
2683
2684   // Layout the text.
2685   Size layoutSize;
2686   updated = DoRelayout( size,
2687                         mImpl->mOperationsPending,
2688                         layoutSize ) || updated;
2689
2690
2691   if( updated )
2692   {
2693     updateTextType = MODEL_UPDATED;
2694   }
2695
2696   // Do not re-do any operation until something changes.
2697   mImpl->mOperationsPending = NO_OPERATION;
2698   mImpl->mModel->mScrollPositionLast = mImpl->mModel->mScrollPosition;
2699
2700   // Whether the text control is editable
2701   const bool isEditable = NULL != mImpl->mEventData;
2702
2703   // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
2704   Vector2 offset;
2705   if( newSize && isEditable )
2706   {
2707     offset = mImpl->mModel->mScrollPosition;
2708   }
2709
2710   if( !isEditable || !IsMultiLineEnabled() )
2711   {
2712     // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
2713     CalculateVerticalOffset( size );
2714   }
2715
2716   if( isEditable )
2717   {
2718     if( newSize )
2719     {
2720       // If there is a new size, the scroll position needs to be clamped.
2721       mImpl->ClampHorizontalScroll( layoutSize );
2722
2723       // Update the decorator's positions is needed if there is a new size.
2724       mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mModel->mScrollPosition - offset );
2725     }
2726
2727     // Move the cursor, grab handle etc.
2728     if( mImpl->ProcessInputEvents() )
2729     {
2730       updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
2731     }
2732   }
2733
2734   // Clear the update info. This info will be set the next time the text is updated.
2735   mImpl->mTextUpdateInfo.Clear();
2736   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
2737
2738   return updateTextType;
2739 }
2740
2741 void Controller::RequestRelayout()
2742 {
2743   mImpl->RequestRelayout();
2744 }
2745
2746 // public : Input style change signals.
2747
2748 bool Controller::IsInputStyleChangedSignalsQueueEmpty()
2749 {
2750   return ( NULL == mImpl->mEventData ) || ( 0u == mImpl->mEventData->mInputStyleChangedQueue.Count() );
2751 }
2752
2753 void Controller::ProcessInputStyleChangedSignals()
2754 {
2755   if( NULL == mImpl->mEventData )
2756   {
2757     // Nothing to do.
2758     return;
2759   }
2760
2761   for( Vector<InputStyle::Mask>::ConstIterator it = mImpl->mEventData->mInputStyleChangedQueue.Begin(),
2762          endIt = mImpl->mEventData->mInputStyleChangedQueue.End();
2763        it != endIt;
2764        ++it )
2765   {
2766     const InputStyle::Mask mask = *it;
2767
2768     if( NULL != mImpl->mEditableControlInterface )
2769     {
2770       // Emit the input style changed signal.
2771       mImpl->mEditableControlInterface->InputStyleChanged( mask );
2772     }
2773   }
2774
2775   mImpl->mEventData->mInputStyleChangedQueue.Clear();
2776 }
2777
2778 // public : Text-input Event Queuing.
2779
2780 void Controller::KeyboardFocusGainEvent()
2781 {
2782   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
2783
2784   if( NULL != mImpl->mEventData )
2785   {
2786     if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
2787         ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
2788     {
2789       mImpl->ChangeState( EventData::EDITING );
2790       mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
2791       mImpl->mEventData->mUpdateInputStyle = true;
2792       mImpl->mEventData->mScrollAfterUpdatePosition = true;
2793     }
2794     mImpl->NotifyInputMethodContextMultiLineStatus();
2795     if( mImpl->IsShowingPlaceholderText() )
2796     {
2797       // Show alternative placeholder-text when editing
2798       ShowPlaceholderText();
2799     }
2800
2801     mImpl->RequestRelayout();
2802   }
2803 }
2804
2805 void Controller::KeyboardFocusLostEvent()
2806 {
2807   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
2808
2809   if( NULL != mImpl->mEventData )
2810   {
2811     if( EventData::INTERRUPTED != mImpl->mEventData->mState )
2812     {
2813       mImpl->ChangeState( EventData::INACTIVE );
2814
2815       if( !mImpl->IsShowingRealText() )
2816       {
2817         // Revert to regular placeholder-text when not editing
2818         ShowPlaceholderText();
2819       }
2820     }
2821   }
2822   mImpl->RequestRelayout();
2823 }
2824
2825 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
2826 {
2827   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
2828
2829   bool textChanged = false;
2830   bool relayoutNeeded = false;
2831
2832   if( ( NULL != mImpl->mEventData ) &&
2833       ( keyEvent.state == KeyEvent::Down ) )
2834   {
2835     int keyCode = keyEvent.keyCode;
2836     const std::string& keyString = keyEvent.keyPressed;
2837     const std::string keyName = keyEvent.keyPressedName;
2838
2839     const bool isNullKey = ( 0 == keyCode ) && ( keyString.empty() );
2840
2841     // Pre-process to separate modifying events from non-modifying input events.
2842     if( isNullKey )
2843     {
2844       // In some platforms arrive key events with no key code.
2845       // Do nothing.
2846       return false;
2847     }
2848     else if( Dali::DALI_KEY_ESCAPE == keyCode || Dali::DALI_KEY_BACK == keyCode  || Dali::DALI_KEY_SEARCH == keyCode )
2849     {
2850       // Do nothing
2851       return false;
2852     }
2853     else if( ( Dali::DALI_KEY_CURSOR_LEFT  == keyCode ) ||
2854              ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
2855              ( Dali::DALI_KEY_CURSOR_UP    == keyCode ) ||
2856              ( Dali::DALI_KEY_CURSOR_DOWN  == keyCode ) )
2857     {
2858       // If don't have any text, do nothing.
2859       if( !mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters )
2860       {
2861         return false;
2862       }
2863
2864       uint32_t cursorPosition = mImpl->mEventData->mPrimaryCursorPosition;
2865       uint32_t numberOfCharacters = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
2866       uint32_t cursorLine = mImpl->mModel->mVisualModel->GetLineOfCharacter( cursorPosition );
2867       uint32_t numberOfLines = mImpl->mModel->GetNumberOfLines();
2868
2869       // Logic to determine whether this text control will lose focus or not.
2870       if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode && 0 == cursorPosition && !keyEvent.IsShiftModifier() ) ||
2871           ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode && numberOfCharacters == cursorPosition && !keyEvent.IsShiftModifier() ) ||
2872           ( Dali::DALI_KEY_CURSOR_DOWN == keyCode && cursorLine == numberOfLines -1 ) ||
2873           ( Dali::DALI_KEY_CURSOR_DOWN == keyCode && numberOfCharacters == cursorPosition && cursorLine -1 == numberOfLines -1 ) ||
2874           ( Dali::DALI_KEY_CURSOR_UP == keyCode && cursorLine == 0 ) ||
2875           ( Dali::DALI_KEY_CURSOR_UP == keyCode && numberOfCharacters == cursorPosition && cursorLine == 1 ) )
2876       {
2877         // Release the active highlight.
2878         if( mImpl->mEventData->mState == EventData::SELECTING )
2879         {
2880           mImpl->ChangeState( EventData::EDITING );
2881
2882           // Update selection position.
2883           mImpl->mEventData->mLeftSelectionPosition = mImpl->mEventData->mPrimaryCursorPosition;
2884           mImpl->mEventData->mRightSelectionPosition = mImpl->mEventData->mPrimaryCursorPosition;
2885           mImpl->mEventData->mUpdateCursorPosition = true;
2886           mImpl->RequestRelayout();
2887         }
2888         return false;
2889       }
2890
2891       mImpl->mEventData->mCheckScrollAmount = true;
2892       Event event( Event::CURSOR_KEY_EVENT );
2893       event.p1.mInt = keyCode;
2894       event.p2.mBool = keyEvent.IsShiftModifier();
2895       mImpl->mEventData->mEventQueue.push_back( event );
2896
2897       // Will request for relayout.
2898       relayoutNeeded = true;
2899     }
2900     else if ( Dali::DevelKey::DALI_KEY_CONTROL_LEFT == keyCode || Dali::DevelKey::DALI_KEY_CONTROL_RIGHT == keyCode )
2901     {
2902       // Left or Right Control key event is received before Ctrl-C/V/X key event is received
2903       // If not handle it here, any selected text will be deleted
2904
2905       // Do nothing
2906       return false;
2907     }
2908     else if ( keyEvent.IsCtrlModifier() )
2909     {
2910       bool consumed = false;
2911       if (keyName == KEY_C_NAME)
2912       {
2913         // Ctrl-C to copy the selected text
2914         TextPopupButtonTouched( Toolkit::TextSelectionPopup::COPY );
2915         consumed = true;
2916       }
2917       else if (keyName == KEY_V_NAME)
2918       {
2919         // Ctrl-V to paste the copied text
2920         TextPopupButtonTouched( Toolkit::TextSelectionPopup::PASTE );
2921         consumed = true;
2922       }
2923       else if (keyName == KEY_X_NAME)
2924       {
2925         // Ctrl-X to cut the selected text
2926         TextPopupButtonTouched( Toolkit::TextSelectionPopup::CUT );
2927         consumed = true;
2928       }
2929       return consumed;
2930     }
2931     else if( ( Dali::DALI_KEY_BACKSPACE == keyCode ) ||
2932              ( Dali::DevelKey::DALI_KEY_DELETE == keyCode ) )
2933     {
2934       textChanged = DeleteEvent( keyCode );
2935
2936       // Will request for relayout.
2937       relayoutNeeded = true;
2938     }
2939     else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) ||
2940              IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
2941              IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
2942     {
2943       // Power key/Menu/Home key behaviour does not allow edit mode to resume.
2944       mImpl->ChangeState( EventData::INACTIVE );
2945
2946       // Will request for relayout.
2947       relayoutNeeded = true;
2948
2949       // This branch avoids calling the InsertText() method of the 'else' branch which can delete selected text.
2950     }
2951     else if( ( Dali::DALI_KEY_SHIFT_LEFT == keyCode ) || ( Dali::DALI_KEY_SHIFT_RIGHT == keyCode ) )
2952     {
2953       // DALI_KEY_SHIFT_LEFT or DALI_KEY_SHIFT_RIGHT is the key code for Shift. It's sent (by the InputMethodContext?) when the predictive text is enabled
2954       // and a character is typed after the type of a upper case latin character.
2955
2956       // Do nothing.
2957       return false;
2958     }
2959     else if( ( Dali::DALI_KEY_VOLUME_UP == keyCode ) || ( Dali::DALI_KEY_VOLUME_DOWN == keyCode ) )
2960     {
2961       // This branch avoids calling the InsertText() method of the 'else' branch which can delete selected text.
2962       // Do nothing.
2963       return false;
2964     }
2965     else
2966     {
2967       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
2968
2969       if( !keyString.empty() )
2970       {
2971         // InputMethodContext is no longer handling key-events
2972         mImpl->ClearPreEditFlag();
2973
2974         InsertText( keyString, COMMIT );
2975
2976         textChanged = true;
2977
2978         // Will request for relayout.
2979         relayoutNeeded = true;
2980       }
2981
2982     }
2983
2984     if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
2985          ( mImpl->mEventData->mState != EventData::INACTIVE ) &&
2986          ( !isNullKey ) &&
2987          ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) &&
2988          ( Dali::DALI_KEY_SHIFT_RIGHT != keyCode ) &&
2989          ( Dali::DALI_KEY_VOLUME_UP != keyCode ) &&
2990          ( Dali::DALI_KEY_VOLUME_DOWN != keyCode ) )
2991     {
2992       // Should not change the state if the key is the shift send by the InputMethodContext.
2993       // Otherwise, when the state is SELECTING the text controller can't send the right
2994       // surrounding info to the InputMethodContext.
2995       mImpl->ChangeState( EventData::EDITING );
2996
2997       // Will request for relayout.
2998       relayoutNeeded = true;
2999     }
3000
3001     if( relayoutNeeded )
3002     {
3003       mImpl->RequestRelayout();
3004     }
3005   }
3006
3007   if( textChanged &&
3008       ( NULL != mImpl->mEditableControlInterface ) )
3009   {
3010     // Do this last since it provides callbacks into application code
3011     mImpl->mEditableControlInterface->TextChanged();
3012   }
3013
3014   return true;
3015 }
3016
3017 void Controller::TapEvent( unsigned int tapCount, float x, float y )
3018 {
3019   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
3020
3021   if( NULL != mImpl->mEventData )
3022   {
3023     DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
3024     EventData::State state( mImpl->mEventData->mState );
3025     bool relayoutNeeded( false );   // to avoid unnecessary relayouts when tapping an empty text-field
3026
3027     if( mImpl->IsClipboardVisible() )
3028     {
3029       if( EventData::INACTIVE == state || EventData::EDITING == state)
3030       {
3031         mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
3032       }
3033       relayoutNeeded = true;
3034     }
3035     else if( 1u == tapCount )
3036     {
3037       if( EventData::EDITING_WITH_POPUP == state || EventData::EDITING_WITH_PASTE_POPUP == state )
3038       {
3039         mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );  // If Popup shown hide it here so can be shown again if required.
3040       }
3041
3042       if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != state ) )
3043       {
3044         mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
3045         relayoutNeeded = true;
3046       }
3047       else
3048       {
3049         if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
3050         {
3051           // Hide placeholder text
3052           ResetText();
3053         }
3054
3055         if( EventData::INACTIVE == state )
3056         {
3057           mImpl->ChangeState( EventData::EDITING );
3058         }
3059         else if( !mImpl->IsClipboardEmpty() )
3060         {
3061           mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
3062         }
3063         relayoutNeeded = true;
3064       }
3065     }
3066     else if( 2u == tapCount )
3067     {
3068       if( mImpl->mEventData->mSelectionEnabled &&
3069           mImpl->IsShowingRealText() )
3070       {
3071         relayoutNeeded = true;
3072         mImpl->mEventData->mIsLeftHandleSelected = true;
3073         mImpl->mEventData->mIsRightHandleSelected = true;
3074       }
3075     }
3076
3077     // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
3078     if( relayoutNeeded )
3079     {
3080       Event event( Event::TAP_EVENT );
3081       event.p1.mUint = tapCount;
3082       event.p2.mFloat = x;
3083       event.p3.mFloat = y;
3084       mImpl->mEventData->mEventQueue.push_back( event );
3085
3086       mImpl->RequestRelayout();
3087     }
3088   }
3089
3090   // Reset keyboard as tap event has occurred.
3091   mImpl->ResetInputMethodContext();
3092 }
3093
3094 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
3095 {
3096   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
3097
3098   if( NULL != mImpl->mEventData )
3099   {
3100     Event event( Event::PAN_EVENT );
3101     event.p1.mInt = state;
3102     event.p2.mFloat = displacement.x;
3103     event.p3.mFloat = displacement.y;
3104     mImpl->mEventData->mEventQueue.push_back( event );
3105
3106     mImpl->RequestRelayout();
3107   }
3108 }
3109
3110 void Controller::LongPressEvent( Gesture::State state, float x, float y  )
3111 {
3112   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
3113
3114   if( ( state == Gesture::Started ) &&
3115       ( NULL != mImpl->mEventData ) )
3116   {
3117     // The 1st long-press on inactive text-field is treated as tap
3118     if( EventData::INACTIVE == mImpl->mEventData->mState )
3119     {
3120       mImpl->ChangeState( EventData::EDITING );
3121
3122       Event event( Event::TAP_EVENT );
3123       event.p1.mUint = 1;
3124       event.p2.mFloat = x;
3125       event.p3.mFloat = y;
3126       mImpl->mEventData->mEventQueue.push_back( event );
3127
3128       mImpl->RequestRelayout();
3129     }
3130     else if( !mImpl->IsShowingRealText() )
3131     {
3132       Event event( Event::LONG_PRESS_EVENT );
3133       event.p1.mInt = state;
3134       event.p2.mFloat = x;
3135       event.p3.mFloat = y;
3136       mImpl->mEventData->mEventQueue.push_back( event );
3137       mImpl->RequestRelayout();
3138     }
3139     else if( !mImpl->IsClipboardVisible() )
3140     {
3141       // Reset the InputMethodContext to commit the pre-edit before selecting the text.
3142       mImpl->ResetInputMethodContext();
3143
3144       Event event( Event::LONG_PRESS_EVENT );
3145       event.p1.mInt = state;
3146       event.p2.mFloat = x;
3147       event.p3.mFloat = y;
3148       mImpl->mEventData->mEventQueue.push_back( event );
3149       mImpl->RequestRelayout();
3150
3151       mImpl->mEventData->mIsLeftHandleSelected = true;
3152       mImpl->mEventData->mIsRightHandleSelected = true;
3153     }
3154   }
3155 }
3156
3157 void Controller::SelectEvent( float x, float y, SelectionType selectType )
3158 {
3159   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
3160
3161   if( NULL != mImpl->mEventData )
3162   {
3163     if( selectType == SelectionType::ALL )
3164     {
3165       Event event( Event::SELECT_ALL );
3166       mImpl->mEventData->mEventQueue.push_back( event );
3167     }
3168     else if( selectType == SelectionType::NONE )
3169     {
3170       Event event( Event::SELECT_NONE );
3171       mImpl->mEventData->mEventQueue.push_back( event );
3172     }
3173     else
3174     {
3175       Event event( Event::SELECT );
3176       event.p2.mFloat = x;
3177       event.p3.mFloat = y;
3178       mImpl->mEventData->mEventQueue.push_back( event );
3179     }
3180
3181     mImpl->mEventData->mCheckScrollAmount = true;
3182     mImpl->mEventData->mIsLeftHandleSelected = true;
3183     mImpl->mEventData->mIsRightHandleSelected = true;
3184     mImpl->RequestRelayout();
3185   }
3186 }
3187
3188 InputMethodContext::CallbackData Controller::OnInputMethodContextEvent( InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent )
3189 {
3190   // Whether the text needs to be relaid-out.
3191   bool requestRelayout = false;
3192
3193   // Whether to retrieve the text and cursor position to be sent to the InputMethodContext.
3194   bool retrieveText = false;
3195   bool retrieveCursor = false;
3196
3197   switch( inputMethodContextEvent.eventName )
3198   {
3199     case InputMethodContext::COMMIT:
3200     {
3201       InsertText( inputMethodContextEvent.predictiveString, Text::Controller::COMMIT );
3202       requestRelayout = true;
3203       retrieveCursor = true;
3204       break;
3205     }
3206     case InputMethodContext::PRE_EDIT:
3207     {
3208       InsertText( inputMethodContextEvent.predictiveString, Text::Controller::PRE_EDIT );
3209       requestRelayout = true;
3210       retrieveCursor = true;
3211       break;
3212     }
3213     case InputMethodContext::DELETE_SURROUNDING:
3214     {
3215       const bool textDeleted = RemoveText( inputMethodContextEvent.cursorOffset,
3216                                            inputMethodContextEvent.numberOfChars,
3217                                            DONT_UPDATE_INPUT_STYLE );
3218
3219       if( textDeleted )
3220       {
3221         if( ( 0u != mImpl->mModel->mLogicalModel->mText.Count() ) ||
3222             !mImpl->IsPlaceholderAvailable() )
3223         {
3224           mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
3225         }
3226         else
3227         {
3228           ShowPlaceholderText();
3229         }
3230         mImpl->mEventData->mUpdateCursorPosition = true;
3231         mImpl->mEventData->mScrollAfterDelete = true;
3232
3233         requestRelayout = true;
3234       }
3235       break;
3236     }
3237     case InputMethodContext::GET_SURROUNDING:
3238     {
3239       retrieveText = true;
3240       retrieveCursor = true;
3241       break;
3242     }
3243     case InputMethodContext::PRIVATE_COMMAND:
3244     {
3245       // PRIVATECOMMAND event is just for getting the private command message
3246       retrieveText = true;
3247       retrieveCursor = true;
3248       break;
3249     }
3250     case InputMethodContext::VOID:
3251     {
3252       // do nothing
3253       break;
3254     }
3255   } // end switch
3256
3257   if( requestRelayout )
3258   {
3259     mImpl->mOperationsPending = ALL_OPERATIONS;
3260     mImpl->RequestRelayout();
3261   }
3262
3263   std::string text;
3264   CharacterIndex cursorPosition = 0u;
3265   Length numberOfWhiteSpaces = 0u;
3266
3267   if( retrieveCursor )
3268   {
3269     numberOfWhiteSpaces = mImpl->GetNumberOfWhiteSpaces( 0u );
3270
3271     cursorPosition = mImpl->GetLogicalCursorPosition();
3272
3273     if( cursorPosition < numberOfWhiteSpaces )
3274     {
3275       cursorPosition = 0u;
3276     }
3277     else
3278     {
3279       cursorPosition -= numberOfWhiteSpaces;
3280     }
3281   }
3282
3283   if( retrieveText )
3284   {
3285     if( !mImpl->IsShowingPlaceholderText() )
3286     {
3287       // Retrieves the normal text string.
3288       mImpl->GetText( numberOfWhiteSpaces, text );
3289     }
3290     else
3291     {
3292       // When the current text is Placeholder Text, the surrounding text should be empty string.
3293       // It means DALi should send empty string ("") to IME.
3294       text = "";
3295     }
3296   }
3297
3298   InputMethodContext::CallbackData callbackData( ( retrieveText || retrieveCursor ), cursorPosition, text, false );
3299
3300   if( requestRelayout &&
3301       ( NULL != mImpl->mEditableControlInterface ) )
3302   {
3303     // Do this last since it provides callbacks into application code
3304     mImpl->mEditableControlInterface->TextChanged();
3305   }
3306
3307   return callbackData;
3308 }
3309
3310 void Controller::PasteClipboardItemEvent()
3311 {
3312   // Retrieve the clipboard contents first
3313   ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
3314   std::string stringToPaste( notifier.GetContent() );
3315
3316   // Commit the current pre-edit text; the contents of the clipboard should be appended
3317   mImpl->ResetInputMethodContext();
3318
3319   // Temporary disable hiding clipboard
3320   mImpl->SetClipboardHideEnable( false );
3321
3322   // Paste
3323   PasteText( stringToPaste );
3324
3325   mImpl->SetClipboardHideEnable( true );
3326 }
3327
3328 // protected : Inherit from Text::Decorator::ControllerInterface.
3329
3330 void Controller::GetTargetSize( Vector2& targetSize )
3331 {
3332   targetSize = mImpl->mModel->mVisualModel->mControlSize;
3333 }
3334
3335 void Controller::AddDecoration( Actor& actor, bool needsClipping )
3336 {
3337   if( NULL != mImpl->mEditableControlInterface )
3338   {
3339     mImpl->mEditableControlInterface->AddDecoration( actor, needsClipping );
3340   }
3341 }
3342
3343 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
3344 {
3345   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
3346
3347   if( NULL != mImpl->mEventData )
3348   {
3349     switch( handleType )
3350     {
3351       case GRAB_HANDLE:
3352       {
3353         Event event( Event::GRAB_HANDLE_EVENT );
3354         event.p1.mUint  = state;
3355         event.p2.mFloat = x;
3356         event.p3.mFloat = y;
3357
3358         mImpl->mEventData->mEventQueue.push_back( event );
3359         break;
3360       }
3361       case LEFT_SELECTION_HANDLE:
3362       {
3363         Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
3364         event.p1.mUint  = state;
3365         event.p2.mFloat = x;
3366         event.p3.mFloat = y;
3367
3368         mImpl->mEventData->mEventQueue.push_back( event );
3369         break;
3370       }
3371       case RIGHT_SELECTION_HANDLE:
3372       {
3373         Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
3374         event.p1.mUint  = state;
3375         event.p2.mFloat = x;
3376         event.p3.mFloat = y;
3377
3378         mImpl->mEventData->mEventQueue.push_back( event );
3379         break;
3380       }
3381       case LEFT_SELECTION_HANDLE_MARKER:
3382       case RIGHT_SELECTION_HANDLE_MARKER:
3383       {
3384         // Markers do not move the handles.
3385         break;
3386       }
3387       case HANDLE_TYPE_COUNT:
3388       {
3389         DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
3390       }
3391     }
3392
3393     mImpl->RequestRelayout();
3394   }
3395 }
3396
3397 // protected : Inherit from TextSelectionPopup::TextPopupButtonCallbackInterface.
3398
3399 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
3400 {
3401   if( NULL == mImpl->mEventData )
3402   {
3403     return;
3404   }
3405
3406   switch( button )
3407   {
3408     case Toolkit::TextSelectionPopup::CUT:
3409     {
3410       mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
3411       mImpl->mOperationsPending = ALL_OPERATIONS;
3412
3413       if( ( 0u != mImpl->mModel->mLogicalModel->mText.Count() ) ||
3414           !mImpl->IsPlaceholderAvailable() )
3415       {
3416         mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
3417       }
3418       else
3419       {
3420         ShowPlaceholderText();
3421       }
3422
3423       mImpl->mEventData->mUpdateCursorPosition = true;
3424       mImpl->mEventData->mScrollAfterDelete = true;
3425
3426       mImpl->RequestRelayout();
3427
3428       if( NULL != mImpl->mEditableControlInterface )
3429       {
3430         mImpl->mEditableControlInterface->TextChanged();
3431       }
3432       break;
3433     }
3434     case Toolkit::TextSelectionPopup::COPY:
3435     {
3436       mImpl->SendSelectionToClipboard( false ); // Text not modified
3437
3438       mImpl->mEventData->mUpdateCursorPosition = true;
3439
3440       mImpl->RequestRelayout(); // Cursor, Handles, Selection Highlight, Popup
3441       break;
3442     }
3443     case Toolkit::TextSelectionPopup::PASTE:
3444     {
3445       mImpl->RequestGetTextFromClipboard(); // Request clipboard service to retrieve an item
3446       break;
3447     }
3448     case Toolkit::TextSelectionPopup::SELECT:
3449     {
3450       const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
3451
3452       if( mImpl->mEventData->mSelectionEnabled )
3453       {
3454         // Creates a SELECT event.
3455         SelectEvent( currentCursorPosition.x, currentCursorPosition.y, SelectionType::INTERACTIVE );
3456       }
3457       break;
3458     }
3459     case Toolkit::TextSelectionPopup::SELECT_ALL:
3460     {
3461       // Creates a SELECT_ALL event
3462       SelectEvent( 0.f, 0.f, SelectionType::ALL );
3463       break;
3464     }
3465     case Toolkit::TextSelectionPopup::CLIPBOARD:
3466     {
3467       mImpl->ShowClipboard();
3468       break;
3469     }
3470     case Toolkit::TextSelectionPopup::NONE:
3471     {
3472       // Nothing to do.
3473       break;
3474     }
3475   }
3476 }
3477
3478 void Controller::DisplayTimeExpired()
3479 {
3480   mImpl->mEventData->mUpdateCursorPosition = true;
3481   // Apply modifications to the model
3482   mImpl->mOperationsPending = ALL_OPERATIONS;
3483
3484   mImpl->RequestRelayout();
3485 }
3486
3487 // private : Update.
3488
3489 void Controller::InsertText( const std::string& text, Controller::InsertType type )
3490 {
3491   bool removedPrevious = false;
3492   bool removedSelected = false;
3493   bool maxLengthReached = false;
3494
3495   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
3496
3497   if( NULL == mImpl->mEventData )
3498   {
3499     return;
3500   }
3501
3502   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
3503                  this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
3504                  mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
3505
3506   // TODO: At the moment the underline runs are only for pre-edit.
3507   mImpl->mModel->mVisualModel->mUnderlineRuns.Clear();
3508
3509   // Remove the previous InputMethodContext pre-edit.
3510   if( mImpl->mEventData->mPreEditFlag && ( 0u != mImpl->mEventData->mPreEditLength ) )
3511   {
3512     removedPrevious = RemoveText( -static_cast<int>( mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition ),
3513                                   mImpl->mEventData->mPreEditLength,
3514                                   DONT_UPDATE_INPUT_STYLE );
3515
3516     mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
3517     mImpl->mEventData->mPreEditLength = 0u;
3518   }
3519   else
3520   {
3521     // Remove the previous Selection.
3522     removedSelected = RemoveSelectedText();
3523
3524   }
3525
3526   Vector<Character> utf32Characters;
3527   Length characterCount = 0u;
3528
3529   if( !text.empty() )
3530   {
3531     //  Convert text into UTF-32
3532     utf32Characters.Resize( text.size() );
3533
3534     // This is a bit horrible but std::string returns a (signed) char*
3535     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
3536
3537     // Transform a text array encoded in utf8 into an array encoded in utf32.
3538     // It returns the actual number of characters.
3539     characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
3540     utf32Characters.Resize( characterCount );
3541
3542     DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
3543     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
3544   }
3545
3546   if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
3547   {
3548     // The placeholder text is no longer needed
3549     if( mImpl->IsShowingPlaceholderText() )
3550     {
3551       ResetText();
3552     }
3553
3554     mImpl->ChangeState( EventData::EDITING );
3555
3556     // Handle the InputMethodContext (predicitive text) state changes
3557     if( COMMIT == type )
3558     {
3559       // InputMethodContext is no longer handling key-events
3560       mImpl->ClearPreEditFlag();
3561     }
3562     else // PRE_EDIT
3563     {
3564       if( !mImpl->mEventData->mPreEditFlag )
3565       {
3566         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state\n" );
3567
3568         // Record the start of the pre-edit text
3569         mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
3570       }
3571
3572       mImpl->mEventData->mPreEditLength = utf32Characters.Count();
3573       mImpl->mEventData->mPreEditFlag = true;
3574
3575       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
3576     }
3577
3578     const Length numberOfCharactersInModel = mImpl->mModel->mLogicalModel->mText.Count();
3579
3580     // Restrict new text to fit within Maximum characters setting.
3581     Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
3582     maxLengthReached = ( characterCount > maxSizeOfNewText );
3583
3584     // The cursor position.
3585     CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
3586
3587     // Update the text's style.
3588
3589     // Updates the text style runs by adding characters.
3590     mImpl->mModel->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
3591
3592     // Get the character index from the cursor index.
3593     const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
3594
3595     // Retrieve the text's style for the given index.
3596     InputStyle style;
3597     mImpl->RetrieveDefaultInputStyle( style );
3598     mImpl->mModel->mLogicalModel->RetrieveStyle( styleIndex, style );
3599
3600     // Whether to add a new text color run.
3601     const bool addColorRun = ( style.textColor != mImpl->mEventData->mInputStyle.textColor ) && !mImpl->mEventData->mInputStyle.isDefaultColor;
3602
3603     // Whether to add a new font run.
3604     const bool addFontNameRun = ( style.familyName != mImpl->mEventData->mInputStyle.familyName ) && mImpl->mEventData->mInputStyle.isFamilyDefined;
3605     const bool addFontWeightRun = ( style.weight != mImpl->mEventData->mInputStyle.weight ) && mImpl->mEventData->mInputStyle.isWeightDefined;
3606     const bool addFontWidthRun = ( style.width != mImpl->mEventData->mInputStyle.width ) && mImpl->mEventData->mInputStyle.isWidthDefined;
3607     const bool addFontSlantRun = ( style.slant != mImpl->mEventData->mInputStyle.slant ) && mImpl->mEventData->mInputStyle.isSlantDefined;
3608     const bool addFontSizeRun = ( style.size != mImpl->mEventData->mInputStyle.size ) && mImpl->mEventData->mInputStyle.isSizeDefined ;
3609
3610     // Add style runs.
3611     if( addColorRun )
3612     {
3613       const VectorBase::SizeType numberOfRuns = mImpl->mModel->mLogicalModel->mColorRuns.Count();
3614       mImpl->mModel->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
3615
3616       ColorRun& colorRun = *( mImpl->mModel->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
3617       colorRun.color = mImpl->mEventData->mInputStyle.textColor;
3618       colorRun.characterRun.characterIndex = cursorIndex;
3619       colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
3620     }
3621
3622     if( addFontNameRun   ||
3623         addFontWeightRun ||
3624         addFontWidthRun  ||
3625         addFontSlantRun  ||
3626         addFontSizeRun )
3627     {
3628       const VectorBase::SizeType numberOfRuns = mImpl->mModel->mLogicalModel->mFontDescriptionRuns.Count();
3629       mImpl->mModel->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
3630
3631       FontDescriptionRun& fontDescriptionRun = *( mImpl->mModel->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
3632
3633       if( addFontNameRun )
3634       {
3635         fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
3636         fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
3637         memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
3638         fontDescriptionRun.familyDefined = true;
3639
3640         // The memory allocated for the font family name is freed when the font description is removed from the logical model.
3641       }
3642
3643       if( addFontWeightRun )
3644       {
3645         fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
3646         fontDescriptionRun.weightDefined = true;
3647       }
3648
3649       if( addFontWidthRun )
3650       {
3651         fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
3652         fontDescriptionRun.widthDefined = true;
3653       }
3654
3655       if( addFontSlantRun )
3656       {
3657         fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
3658         fontDescriptionRun.slantDefined = true;
3659       }
3660
3661       if( addFontSizeRun )
3662       {
3663         fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
3664         fontDescriptionRun.sizeDefined = true;
3665       }
3666
3667       fontDescriptionRun.characterRun.characterIndex = cursorIndex;
3668       fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
3669     }
3670
3671     // Insert at current cursor position.
3672     Vector<Character>& modifyText = mImpl->mModel->mLogicalModel->mText;
3673
3674     if( cursorIndex < numberOfCharactersInModel )
3675     {
3676       modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
3677     }
3678     else
3679     {
3680       modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
3681     }
3682
3683     // Mark the first paragraph to be updated.
3684     if( Layout::Engine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
3685     {
3686       mImpl->mTextUpdateInfo.mCharacterIndex = 0;
3687       mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
3688       mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = numberOfCharactersInModel + maxSizeOfNewText;
3689       mImpl->mTextUpdateInfo.mClearAll = true;
3690     }
3691     else
3692     {
3693       mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
3694       mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
3695     }
3696
3697     // Update the cursor index.
3698     cursorIndex += maxSizeOfNewText;
3699
3700     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mModel->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
3701   }
3702
3703   if( ( 0u == mImpl->mModel->mLogicalModel->mText.Count() ) &&
3704       mImpl->IsPlaceholderAvailable() )
3705   {
3706     // Show place-holder if empty after removing the pre-edit text
3707     ShowPlaceholderText();
3708     mImpl->mEventData->mUpdateCursorPosition = true;
3709     mImpl->ClearPreEditFlag();
3710   }
3711   else if( removedPrevious ||
3712            removedSelected ||
3713            ( 0 != utf32Characters.Count() ) )
3714   {
3715     // Queue an inserted event
3716     mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
3717
3718     mImpl->mEventData->mUpdateCursorPosition = true;
3719     if( removedSelected )
3720     {
3721       mImpl->mEventData->mScrollAfterDelete = true;
3722     }
3723     else
3724     {
3725       mImpl->mEventData->mScrollAfterUpdatePosition = true;
3726     }
3727   }
3728
3729   if( maxLengthReached )
3730   {
3731     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mModel->mLogicalModel->mText.Count() );
3732
3733     mImpl->ResetInputMethodContext();
3734
3735     if( NULL != mImpl->mEditableControlInterface )
3736     {
3737       // Do this last since it provides callbacks into application code
3738       mImpl->mEditableControlInterface->MaxLengthReached();
3739     }
3740   }
3741 }
3742
3743 void Controller::PasteText( const std::string& stringToPaste )
3744 {
3745   InsertText( stringToPaste, Text::Controller::COMMIT );
3746   mImpl->ChangeState( EventData::EDITING );
3747   mImpl->RequestRelayout();
3748
3749   if( NULL != mImpl->mEditableControlInterface )
3750   {
3751     // Do this last since it provides callbacks into application code
3752     mImpl->mEditableControlInterface->TextChanged();
3753   }
3754 }
3755
3756 bool Controller::RemoveText( int cursorOffset,
3757                              int numberOfCharacters,
3758                              UpdateInputStyleType type )
3759 {
3760   bool removed = false;
3761
3762   if( NULL == mImpl->mEventData )
3763   {
3764     return removed;
3765   }
3766
3767   DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n",
3768                  this, mImpl->mModel->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters );
3769
3770   if( !mImpl->IsShowingPlaceholderText() )
3771   {
3772     // Delete at current cursor position
3773     Vector<Character>& currentText = mImpl->mModel->mLogicalModel->mText;
3774     CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
3775
3776     CharacterIndex cursorIndex = 0;
3777
3778     // Validate the cursor position & number of characters
3779     if( ( static_cast< int >( mImpl->mEventData->mPrimaryCursorPosition ) + cursorOffset ) >= 0 )
3780     {
3781       cursorIndex = mImpl->mEventData->mPrimaryCursorPosition + cursorOffset;
3782     }
3783
3784     if( ( cursorIndex + numberOfCharacters ) > currentText.Count() )
3785     {
3786       numberOfCharacters = currentText.Count() - cursorIndex;
3787     }
3788
3789     if( mImpl->mEventData->mPreEditFlag || // If the preedit flag is enabled, it means two (or more) of them came together i.e. when two keys have been pressed at the same time.
3790         ( ( cursorIndex + numberOfCharacters ) <= mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters ) )
3791     {
3792       // Mark the paragraphs to be updated.
3793       if( Layout::Engine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
3794       {
3795         mImpl->mTextUpdateInfo.mCharacterIndex = 0;
3796         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
3797         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters - numberOfCharacters;
3798         mImpl->mTextUpdateInfo.mClearAll = true;
3799       }
3800       else
3801       {
3802         mImpl->mTextUpdateInfo.mCharacterIndex = std::min( cursorIndex, mImpl->mTextUpdateInfo.mCharacterIndex );
3803         mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
3804       }
3805
3806       // Update the input style and remove the text's style before removing the text.
3807
3808       if( UPDATE_INPUT_STYLE == type )
3809       {
3810         // Keep a copy of the current input style.
3811         InputStyle currentInputStyle;
3812         currentInputStyle.Copy( mImpl->mEventData->mInputStyle );
3813
3814         // Set first the default input style.
3815         mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
3816
3817         // Update the input style.
3818         mImpl->mModel->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
3819
3820         // Compare if the input style has changed.
3821         const bool hasInputStyleChanged = !currentInputStyle.Equal( mImpl->mEventData->mInputStyle );
3822
3823         if( hasInputStyleChanged )
3824         {
3825           const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask( mImpl->mEventData->mInputStyle );
3826           // Queue the input style changed signal.
3827           mImpl->mEventData->mInputStyleChangedQueue.PushBack( styleChangedMask );
3828         }
3829       }
3830
3831       // If the number of current text and the number of characters to be deleted are same,
3832       // it means all texts should be removed and all Preedit variables should be initialized.
3833       if( ( currentText.Count() - numberOfCharacters == 0 ) && ( cursorIndex == 0 ) )
3834       {
3835         mImpl->ClearPreEditFlag();
3836         mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0;
3837       }
3838
3839       // Updates the text style runs by removing characters. Runs with no characters are removed.
3840       mImpl->mModel->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfCharacters );
3841
3842       // Remove the characters.
3843       Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
3844       Vector<Character>::Iterator last  = first + numberOfCharacters;
3845
3846       currentText.Erase( first, last );
3847
3848       // Cursor position retreat
3849       oldCursorIndex = cursorIndex;
3850
3851       mImpl->mEventData->mScrollAfterDelete = true;
3852
3853       if( EventData::INACTIVE == mImpl->mEventData->mState )
3854       {
3855         mImpl->ChangeState( EventData::EDITING );
3856       }
3857
3858       DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfCharacters );
3859       removed = true;
3860     }
3861   }
3862
3863   return removed;
3864 }
3865
3866 bool Controller::RemoveSelectedText()
3867 {
3868   bool textRemoved( false );
3869
3870   if( EventData::SELECTING == mImpl->mEventData->mState )
3871   {
3872     std::string removedString;
3873     mImpl->RetrieveSelection( removedString, true );
3874
3875     if( !removedString.empty() )
3876     {
3877       textRemoved = true;
3878       mImpl->ChangeState( EventData::EDITING );
3879     }
3880   }
3881
3882   return textRemoved;
3883 }
3884
3885 std::string Controller::GetSelectedText()
3886 {
3887   std::string text;
3888   if( EventData::SELECTING == mImpl->mEventData->mState )
3889   {
3890     mImpl->RetrieveSelection( text, false );
3891   }
3892   return text;
3893 }
3894
3895 // private : Relayout.
3896
3897 bool Controller::DoRelayout( const Size& size,
3898                              OperationsMask operationsRequired,
3899                              Size& layoutSize )
3900 {
3901   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
3902   bool viewUpdated( false );
3903
3904   // Calculate the operations to be done.
3905   const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
3906
3907   const CharacterIndex startIndex = mImpl->mTextUpdateInfo.mParagraphCharacterIndex;
3908   const Length requestedNumberOfCharacters = mImpl->mTextUpdateInfo.mRequestedNumberOfCharacters;
3909
3910   // Get the current layout size.
3911   layoutSize = mImpl->mModel->mVisualModel->GetLayoutSize();
3912
3913   if( NO_OPERATION != ( LAYOUT & operations ) )
3914   {
3915     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
3916
3917     // Some vectors with data needed to layout and reorder may be void
3918     // after the first time the text has been laid out.
3919     // Fill the vectors again.
3920
3921     // Calculate the number of glyphs to layout.
3922     const Vector<GlyphIndex>& charactersToGlyph = mImpl->mModel->mVisualModel->mCharactersToGlyph;
3923     const Vector<Length>& glyphsPerCharacter = mImpl->mModel->mVisualModel->mGlyphsPerCharacter;
3924     const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
3925     const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
3926
3927     const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
3928     const GlyphIndex startGlyphIndex = mImpl->mTextUpdateInfo.mStartGlyphIndex;
3929
3930     // Make sure the index is not out of bound
3931     if ( charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
3932          requestedNumberOfCharacters > charactersToGlyph.Count() ||
3933          ( lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u ) )
3934     {
3935       std::string currentText;
3936       GetText( currentText );
3937
3938       DALI_LOG_ERROR( "Controller::DoRelayout: Attempting to access invalid buffer\n" );
3939       DALI_LOG_ERROR( "Current text is: %s\n", currentText.c_str() );
3940       DALI_LOG_ERROR( "startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
3941
3942       return false;
3943     }
3944
3945     const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
3946     const Length totalNumberOfGlyphs = mImpl->mModel->mVisualModel->mGlyphs.Count();
3947
3948     if( 0u == totalNumberOfGlyphs )
3949     {
3950       if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
3951       {
3952         mImpl->mModel->mVisualModel->SetLayoutSize( Size::ZERO );
3953       }
3954
3955       // Nothing else to do if there is no glyphs.
3956       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
3957       return true;
3958     }
3959
3960     // Set the layout parameters.
3961     Layout::Parameters layoutParameters( size,
3962                                          mImpl->mModel);
3963
3964     // Resize the vector of positions to have the same size than the vector of glyphs.
3965     Vector<Vector2>& glyphPositions = mImpl->mModel->mVisualModel->mGlyphPositions;
3966     glyphPositions.Resize( totalNumberOfGlyphs );
3967
3968     // Whether the last character is a new paragraph character.
3969     const Character* const textBuffer = mImpl->mModel->mLogicalModel->mText.Begin();
3970     mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph =  TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mModel->mLogicalModel->mText.Count() - 1u ) ) );
3971     layoutParameters.isLastNewParagraph = mImpl->mTextUpdateInfo.mIsLastCharacterNewParagraph;
3972
3973     // The initial glyph and the number of glyphs to layout.
3974     layoutParameters.startGlyphIndex = startGlyphIndex;
3975     layoutParameters.numberOfGlyphs = numberOfGlyphs;
3976     layoutParameters.startLineIndex = mImpl->mTextUpdateInfo.mStartLineIndex;
3977     layoutParameters.estimatedNumberOfLines = mImpl->mTextUpdateInfo.mEstimatedNumberOfLines;
3978
3979     // Update the ellipsis
3980     bool elideTextEnabled = mImpl->mModel->mElideEnabled;
3981
3982     if( NULL != mImpl->mEventData )
3983     {
3984       if( mImpl->mEventData->mPlaceholderEllipsisFlag && mImpl->IsShowingPlaceholderText() )
3985       {
3986         elideTextEnabled = mImpl->mEventData->mIsPlaceholderElideEnabled;
3987       }
3988       else if( EventData::INACTIVE != mImpl->mEventData->mState )
3989       {
3990         // Disable ellipsis when editing
3991         elideTextEnabled = false;
3992       }
3993
3994       // Reset the scroll position in inactive state
3995       if( elideTextEnabled && ( mImpl->mEventData->mState == EventData::INACTIVE ) )
3996       {
3997         ResetScrollPosition();
3998       }
3999     }
4000
4001     // Update the visual model.
4002     bool isAutoScrollEnabled = mImpl->mIsAutoScrollEnabled;
4003     Size newLayoutSize;
4004     viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
4005                                                    newLayoutSize,
4006                                                    elideTextEnabled,
4007                                                    isAutoScrollEnabled );
4008     mImpl->mIsAutoScrollEnabled = isAutoScrollEnabled;
4009
4010     viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
4011
4012     if( viewUpdated )
4013     {
4014       layoutSize = newLayoutSize;
4015
4016       if( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
4017       {
4018         mImpl->mIsTextDirectionRTL = false;
4019       }
4020
4021       if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && !mImpl->mModel->mVisualModel->mLines.Empty() )
4022       {
4023         mImpl->mIsTextDirectionRTL = mImpl->mModel->mVisualModel->mLines[0u].direction;
4024       }
4025
4026       // Sets the layout size.
4027       if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
4028       {
4029         mImpl->mModel->mVisualModel->SetLayoutSize( layoutSize );
4030       }
4031     } // view updated
4032   }
4033
4034   if( NO_OPERATION != ( ALIGN & operations ) )
4035   {
4036     // The laid-out lines.
4037     Vector<LineRun>& lines = mImpl->mModel->mVisualModel->mLines;
4038
4039     CharacterIndex alignStartIndex = startIndex;
4040     Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
4041
4042     // the whole text needs to be full aligned.
4043     // If you do not do a full aligned, only the last line of the multiline input is aligned.
4044     if(  mImpl->mEventData && mImpl->mEventData->mUpdateAlignment )
4045     {
4046       alignStartIndex = 0u;
4047       alignRequestedNumberOfCharacters = mImpl->mModel->mLogicalModel->mText.Count();
4048       mImpl->mEventData->mUpdateAlignment = false;
4049     }
4050
4051     // Need to align with the control's size as the text may contain lines
4052     // starting either with left to right text or right to left.
4053     mImpl->mLayoutEngine.Align( size,
4054                                 alignStartIndex,
4055                                 alignRequestedNumberOfCharacters,
4056                                 mImpl->mModel->mHorizontalAlignment,
4057                                 lines,
4058                                 mImpl->mModel->mAlignmentOffset,
4059                                 mImpl->mLayoutDirection,
4060                                 mImpl->mModel->mMatchSystemLanguageDirection );
4061
4062     viewUpdated = true;
4063   }
4064 #if defined(DEBUG_ENABLED)
4065   std::string currentText;
4066   GetText( currentText );
4067   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", this, (mImpl->mIsTextDirectionRTL)?"true":"false",  currentText.c_str() );
4068 #endif
4069   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
4070   return viewUpdated;
4071 }
4072
4073 void Controller::CalculateVerticalOffset( const Size& controlSize )
4074 {
4075   Size layoutSize = mImpl->mModel->mVisualModel->GetLayoutSize();
4076
4077   if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
4078   {
4079     // Get the line height of the default font.
4080     layoutSize.height = mImpl->GetDefaultFontLineHeight();
4081   }
4082
4083   switch( mImpl->mModel->mVerticalAlignment )
4084   {
4085     case VerticalAlignment::TOP:
4086     {
4087       mImpl->mModel->mScrollPosition.y = 0.f;
4088       break;
4089     }
4090     case VerticalAlignment::CENTER:
4091     {
4092       mImpl->mModel->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
4093       break;
4094     }
4095     case VerticalAlignment::BOTTOM:
4096     {
4097       mImpl->mModel->mScrollPosition.y = controlSize.height - layoutSize.height;
4098       break;
4099     }
4100   }
4101 }
4102
4103 // private : Events.
4104
4105 void Controller::ProcessModifyEvents()
4106 {
4107   Vector<ModifyEvent>& events = mImpl->mModifyEvents;
4108
4109   if( 0u == events.Count() )
4110   {
4111     // Nothing to do.
4112     return;
4113   }
4114
4115   for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
4116          endIt = events.End();
4117        it != endIt;
4118        ++it )
4119   {
4120     const ModifyEvent& event = *it;
4121
4122     if( ModifyEvent::TEXT_REPLACED == event.type )
4123     {
4124       // A (single) replace event should come first, otherwise we wasted time processing NOOP events
4125       DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
4126
4127       TextReplacedEvent();
4128     }
4129     else if( ModifyEvent::TEXT_INSERTED == event.type )
4130     {
4131       TextInsertedEvent();
4132     }
4133     else if( ModifyEvent::TEXT_DELETED == event.type )
4134     {
4135       // Placeholder-text cannot be deleted
4136       if( !mImpl->IsShowingPlaceholderText() )
4137       {
4138         TextDeletedEvent();
4139       }
4140     }
4141   }
4142
4143   if( NULL != mImpl->mEventData )
4144   {
4145     // When the text is being modified, delay cursor blinking
4146     mImpl->mEventData->mDecorator->DelayCursorBlink();
4147
4148     // Update selection position after modifying the text
4149     mImpl->mEventData->mLeftSelectionPosition = mImpl->mEventData->mPrimaryCursorPosition;
4150     mImpl->mEventData->mRightSelectionPosition = mImpl->mEventData->mPrimaryCursorPosition;
4151   }
4152
4153   // Discard temporary text
4154   events.Clear();
4155 }
4156
4157 void Controller::TextReplacedEvent()
4158 {
4159   // The natural size needs to be re-calculated.
4160   mImpl->mRecalculateNaturalSize = true;
4161
4162   // The text direction needs to be updated.
4163   mImpl->mUpdateTextDirection = true;
4164
4165   // Apply modifications to the model
4166   mImpl->mOperationsPending = ALL_OPERATIONS;
4167 }
4168
4169 void Controller::TextInsertedEvent()
4170 {
4171   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
4172
4173   if( NULL == mImpl->mEventData )
4174   {
4175     return;
4176   }
4177
4178   mImpl->mEventData->mCheckScrollAmount = true;
4179
4180   // The natural size needs to be re-calculated.
4181   mImpl->mRecalculateNaturalSize = true;
4182
4183   // The text direction needs to be updated.
4184   mImpl->mUpdateTextDirection = true;
4185
4186   // Apply modifications to the model; TODO - Optimize this
4187   mImpl->mOperationsPending = ALL_OPERATIONS;
4188 }
4189
4190 void Controller::TextDeletedEvent()
4191 {
4192   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
4193
4194   if( NULL == mImpl->mEventData )
4195   {
4196     return;
4197   }
4198
4199   mImpl->mEventData->mCheckScrollAmount = true;
4200
4201   // The natural size needs to be re-calculated.
4202   mImpl->mRecalculateNaturalSize = true;
4203
4204   // The text direction needs to be updated.
4205   mImpl->mUpdateTextDirection = true;
4206
4207   // Apply modifications to the model; TODO - Optimize this
4208   mImpl->mOperationsPending = ALL_OPERATIONS;
4209 }
4210
4211 bool Controller::DeleteEvent( int keyCode )
4212 {
4213   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p KeyCode : %d \n", this, keyCode );
4214
4215   bool removed = false;
4216
4217   if( NULL == mImpl->mEventData )
4218   {
4219     return removed;
4220   }
4221
4222   // InputMethodContext is no longer handling key-events
4223   mImpl->ClearPreEditFlag();
4224
4225   if( EventData::SELECTING == mImpl->mEventData->mState )
4226   {
4227     removed = RemoveSelectedText();
4228   }
4229   else if( ( mImpl->mEventData->mPrimaryCursorPosition > 0 ) && ( keyCode == Dali::DALI_KEY_BACKSPACE) )
4230   {
4231     // Remove the character before the current cursor position
4232     removed = RemoveText( -1,
4233                           1,
4234                           UPDATE_INPUT_STYLE );
4235   }
4236   else if( keyCode == Dali::DevelKey::DALI_KEY_DELETE )
4237   {
4238     // Remove the character after the current cursor position
4239     removed = RemoveText( 0,
4240                           1,
4241                           UPDATE_INPUT_STYLE );
4242   }
4243
4244   if( removed )
4245   {
4246     if( ( 0u != mImpl->mModel->mLogicalModel->mText.Count() ) ||
4247         !mImpl->IsPlaceholderAvailable() )
4248     {
4249       mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
4250     }
4251     else
4252     {
4253       ShowPlaceholderText();
4254     }
4255     mImpl->mEventData->mUpdateCursorPosition = true;
4256     mImpl->mEventData->mScrollAfterDelete = true;
4257   }
4258
4259   return removed;
4260 }
4261
4262 // private : Helpers.
4263
4264 void Controller::ResetText()
4265 {
4266   // Reset buffers.
4267   mImpl->mModel->mLogicalModel->mText.Clear();
4268
4269   // Reset the embedded images buffer.
4270   mImpl->mModel->mLogicalModel->ClearEmbeddedImages();
4271
4272   // We have cleared everything including the placeholder-text
4273   mImpl->PlaceholderCleared();
4274
4275   mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
4276   mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
4277   mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
4278
4279   // Clear any previous text.
4280   mImpl->mTextUpdateInfo.mClearAll = true;
4281
4282   // The natural size needs to be re-calculated.
4283   mImpl->mRecalculateNaturalSize = true;
4284
4285   // The text direction needs to be updated.
4286   mImpl->mUpdateTextDirection = true;
4287
4288   // Apply modifications to the model
4289   mImpl->mOperationsPending = ALL_OPERATIONS;
4290 }
4291
4292 void Controller::ShowPlaceholderText()
4293 {
4294   if( mImpl->IsPlaceholderAvailable() )
4295   {
4296     DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
4297
4298     if( NULL == mImpl->mEventData )
4299     {
4300       return;
4301     }
4302
4303     mImpl->mEventData->mIsShowingPlaceholderText = true;
4304
4305     // Disable handles when showing place-holder text
4306     mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
4307     mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
4308     mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
4309
4310     const char* text( NULL );
4311     size_t size( 0 );
4312
4313     // TODO - Switch Placeholder text when changing state
4314     if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
4315         ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
4316     {
4317       text = mImpl->mEventData->mPlaceholderTextActive.c_str();
4318       size = mImpl->mEventData->mPlaceholderTextActive.size();
4319     }
4320     else
4321     {
4322       text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
4323       size = mImpl->mEventData->mPlaceholderTextInactive.size();
4324     }
4325
4326     mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
4327     mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
4328
4329     // Reset model for showing placeholder.
4330     mImpl->mModel->mLogicalModel->mText.Clear();
4331     mImpl->mModel->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
4332
4333     // Convert text into UTF-32
4334     Vector<Character>& utf32Characters = mImpl->mModel->mLogicalModel->mText;
4335     utf32Characters.Resize( size );
4336
4337     // This is a bit horrible but std::string returns a (signed) char*
4338     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
4339
4340     // Transform a text array encoded in utf8 into an array encoded in utf32.
4341     // It returns the actual number of characters.
4342     const Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
4343     utf32Characters.Resize( characterCount );
4344
4345     // The characters to be added.
4346     mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = characterCount;
4347
4348     // Reset the cursor position
4349     mImpl->mEventData->mPrimaryCursorPosition = 0;
4350
4351     // The natural size needs to be re-calculated.
4352     mImpl->mRecalculateNaturalSize = true;
4353
4354     // The text direction needs to be updated.
4355     mImpl->mUpdateTextDirection = true;
4356
4357     // Apply modifications to the model
4358     mImpl->mOperationsPending = ALL_OPERATIONS;
4359
4360     // Update the rest of the model during size negotiation
4361     mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
4362   }
4363 }
4364
4365 void Controller::ClearFontData()
4366 {
4367   if( mImpl->mFontDefaults )
4368   {
4369     mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
4370   }
4371
4372   // Set flags to update the model.
4373   mImpl->mTextUpdateInfo.mCharacterIndex = 0u;
4374   mImpl->mTextUpdateInfo.mNumberOfCharactersToRemove = mImpl->mTextUpdateInfo.mPreviousNumberOfCharacters;
4375   mImpl->mTextUpdateInfo.mNumberOfCharactersToAdd = mImpl->mModel->mLogicalModel->mText.Count();
4376
4377   mImpl->mTextUpdateInfo.mClearAll = true;
4378   mImpl->mTextUpdateInfo.mFullRelayoutNeeded = true;
4379   mImpl->mRecalculateNaturalSize = true;
4380
4381   mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
4382                                                            VALIDATE_FONTS            |
4383                                                            SHAPE_TEXT                |
4384                                                            BIDI_INFO                 |
4385                                                            GET_GLYPH_METRICS         |
4386                                                            LAYOUT                    |
4387                                                            UPDATE_LAYOUT_SIZE        |
4388                                                            REORDER                   |
4389                                                            ALIGN );
4390 }
4391
4392 void Controller::ClearStyleData()
4393 {
4394   mImpl->mModel->mLogicalModel->mColorRuns.Clear();
4395   mImpl->mModel->mLogicalModel->ClearFontDescriptionRuns();
4396 }
4397
4398 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
4399 {
4400   // Reset the cursor position
4401   if( NULL != mImpl->mEventData )
4402   {
4403     mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
4404
4405     // Update the cursor if it's in editing mode.
4406     if( EventData::IsEditingState( mImpl->mEventData->mState )  )
4407     {
4408       mImpl->mEventData->mUpdateCursorPosition = true;
4409     }
4410   }
4411 }
4412
4413 void Controller::ResetScrollPosition()
4414 {
4415   if( NULL != mImpl->mEventData )
4416   {
4417     // Reset the scroll position.
4418     mImpl->mModel->mScrollPosition = Vector2::ZERO;
4419     mImpl->mEventData->mScrollAfterUpdatePosition = true;
4420   }
4421 }
4422
4423 void Controller::SetControlInterface( ControlInterface* controlInterface )
4424 {
4425   mImpl->mControlInterface = controlInterface;
4426 }
4427
4428 bool Controller::ShouldClearFocusOnEscape() const
4429 {
4430   return mImpl->mShouldClearFocusOnEscape;
4431 }
4432
4433 Actor Controller::CreateBackgroundActor()
4434 {
4435   return mImpl->CreateBackgroundActor();
4436 }
4437
4438 // private : Private contructors & copy operator.
4439
4440 Controller::Controller()
4441 : mImpl( NULL )
4442 {
4443   mImpl = new Controller::Impl( NULL, NULL );
4444 }
4445
4446 Controller::Controller( ControlInterface* controlInterface )
4447 {
4448   mImpl = new Controller::Impl( controlInterface, NULL );
4449 }
4450
4451 Controller::Controller( ControlInterface* controlInterface,
4452                         EditableControlInterface* editableControlInterface )
4453 {
4454   mImpl = new Controller::Impl( controlInterface,
4455                                 editableControlInterface );
4456 }
4457
4458 // The copy constructor and operator are left unimplemented.
4459
4460 // protected : Destructor.
4461
4462 Controller::~Controller()
4463 {
4464   delete mImpl;
4465 }
4466
4467 } // namespace Text
4468
4469 } // namespace Toolkit
4470
4471 } // namespace Dali