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