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