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