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