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