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