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