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