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