1 #ifndef __DALI_TOOLKIT_TEXT_CONTROLLER_IMPL_H__
2 #define __DALI_TOOLKIT_TEXT_CONTROLLER_IMPL_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/devel-api/adaptor-framework/clipboard.h>
23 #include <dali/devel-api/text-abstraction/font-client.h>
26 #include <dali-toolkit/internal/text/input-style.h>
27 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
28 #include <dali-toolkit/internal/text/logical-model-impl.h>
29 #include <dali-toolkit/internal/text/text-controller.h>
30 #include <dali-toolkit/internal/text/text-view.h>
31 #include <dali-toolkit/internal/text/visual-model-impl.h>
44 // Used to queue input events until DoRelayout()
52 LEFT_SELECTION_HANDLE_EVENT,
53 RIGHT_SELECTION_HANDLE_EVENT,
65 Event( Type eventType )
85 primaryCursorHeight( 0.f ),
86 secondaryCursorHeight( 0.f ),
87 isSecondaryCursor( false )
93 Vector2 primaryPosition; ///< The primary cursor's position.
94 Vector2 secondaryPosition; ///< The secondary cursor's position.
95 float lineHeight; ///< The height of the line where the cursor is placed.
96 float primaryCursorHeight; ///< The primary cursor's height.
97 float secondaryCursorHeight; ///< The secondary cursor's height.
98 bool isSecondaryCursor; ///< Whether the secondary cursor is valid.
110 EDITING_WITH_GRAB_HANDLE,
111 EDITING_WITH_PASTE_POPUP,
113 SELECTION_HANDLE_PANNING
116 EventData( DecoratorPtr decorator );
120 static bool IsEditingState( State stateToCheck )
122 return ( stateToCheck == EDITING || stateToCheck == EDITING_WITH_POPUP || stateToCheck == EDITING_WITH_GRAB_HANDLE || stateToCheck == EDITING_WITH_PASTE_POPUP );
125 DecoratorPtr mDecorator; ///< Pointer to the decorator.
126 ImfManager mImfManager; ///< The Input Method Framework Manager.
127 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus.
128 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive.
129 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color.
132 * This is used to delay handling events until after the model has been updated.
133 * The number of updates to the model is minimized to improve performance.
135 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
137 InputStyle mInputStyle; ///< The style to be set to the new inputed text.
140 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
141 * Typically this will have a negative value with scrolling occurs.
143 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
145 State mState; ///< Selection mode, edit mode etc.
147 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
148 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
149 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
151 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
152 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
154 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
155 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
156 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
157 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
158 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
159 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
160 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
161 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
162 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
163 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
164 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
165 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
166 bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated.
167 bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters.
168 bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text.
169 bool mUpdateInputStyle : 1; ///< Whether to update the input style after moving the cursor.
176 TEXT_REPLACED, ///< The entire text was replaced
177 TEXT_INSERTED, ///< Insert characters at the current cursor position
178 TEXT_DELETED ///< Characters were deleted
187 : mFontDescription(),
189 mDefaultPointSize( 0.f ),
192 // Initially use the default platform font
193 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
194 fontClient.GetDefaultPlatformFontDescription( mFontDescription );
197 FontId GetFontId( TextAbstraction::FontClient& fontClient )
201 const PointSize26Dot6 pointSize = static_cast<PointSize26Dot6>( mDefaultPointSize * 64.f );
202 mFontId = fontClient.GetFontId( mFontDescription, pointSize );
208 TextAbstraction::FontDescription mFontDescription;
209 std::string mFontStyle;
210 float mDefaultPointSize;
214 struct Controller::Impl
216 Impl( ControlInterface& controlInterface )
217 : mControlInterface( controlInterface ),
220 mFontDefaults( NULL ),
228 mTextColor( Color::BLACK ),
230 mOperationsPending( NO_OPERATION ),
231 mMaximumNumberOfCharacters( 50u ),
232 mRecalculateNaturalSize( true ),
233 mUserDefinedFontFamily( false ),
234 mMarkupProcessorEnabled( false )
236 mLogicalModel = LogicalModel::New();
237 mVisualModel = VisualModel::New();
239 mFontClient = TextAbstraction::FontClient::Get();
240 mClipboard = Clipboard::Get();
242 mView.SetVisualModel( mVisualModel );
244 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
245 mMetrics = Metrics::New( mFontClient );
246 mLayoutEngine.SetMetrics( mMetrics );
248 // Set the text properties to default
249 mVisualModel->SetUnderlineEnabled( false );
250 mVisualModel->SetUnderlineHeight( 0.0f );
255 delete mFontDefaults;
260 * @brief Request a relayout using the ControlInterface.
262 void RequestRelayout();
265 * @brief Request a relayout using the ControlInterface.
267 void QueueModifyEvent( ModifyEvent::Type type )
269 if( ModifyEvent::TEXT_REPLACED == type)
271 // Cancel previously queued inserts etc.
272 mModifyEvents.Clear();
277 mModifyEvents.PushBack( event );
279 // The event will be processed during relayout
284 * @brief Helper to move the cursor, grab handle etc.
286 bool ProcessInputEvents();
289 * @brief Helper to check whether any place-holder text is available.
291 bool IsPlaceholderAvailable() const
293 return ( mEventData &&
294 ( !mEventData->mPlaceholderTextInactive.empty() ||
295 !mEventData->mPlaceholderTextActive.empty() )
299 bool IsShowingPlaceholderText() const
301 return ( mEventData && mEventData->mIsShowingPlaceholderText );
305 * @brief Helper to check whether active place-holder text is available.
307 bool IsFocusedPlaceholderAvailable() const
309 return ( mEventData && !mEventData->mPlaceholderTextActive.empty() );
312 bool IsShowingRealText() const
314 return ( !IsShowingPlaceholderText() &&
315 0u != mLogicalModel->mText.Count() );
319 * @brief Called when placeholder-text is hidden
321 void PlaceholderCleared()
325 mEventData->mIsShowingPlaceholderText = false;
327 // Remove mPlaceholderTextColor
328 mVisualModel->SetTextColor( mTextColor );
332 void ClearPreEditFlag()
336 mEventData->mPreEditFlag = false;
337 mEventData->mPreEditStartPosition = 0;
338 mEventData->mPreEditLength = 0;
342 void ResetImfManager()
346 // Reset incase we are in a pre-edit state.
347 if( mEventData->mImfManager )
349 mEventData->mImfManager.Reset(); // Will trigger a message ( commit, get surrounding )
356 bool IsClipboardEmpty()
358 bool result( mClipboard && mClipboard.NumberOfItems() );
359 return !result; // // If NumberOfItems greater than 0, return false
363 * @brief Updates the logical and visual models.
365 * When text or style changes the model is set with some operations pending.
366 * When i.e. the text's size or a relayout is required this method is called
367 * with a given @p operationsRequired parameter. The operations required are
368 * matched with the operations pending to perform the minimum number of operations.
370 * @param[in] operationsRequired The operations required.
372 void UpdateModel( OperationsMask operationsRequired );
375 * @brief Updates the style runs in the visual model when the text's styles changes.
377 * @param[in] operationsRequired The operations required.
379 * @return @e true if the model has been modified.
381 bool UpdateModelStyle( OperationsMask operationsRequired );
384 * @brief Retreieves the default style.
386 * @param[out] inputStyle The default style.
388 void RetrieveDefaultInputStyle( InputStyle& inputStyle );
391 * @brief Retrieve the default fonts.
393 * @param[out] fonts The default font family, style and point sizes.
394 * @param[in] numberOfCharacters The number of characters in the logical model.
396 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
399 * @brief Retrieve the line height of the default font.
401 float GetDefaultFontLineHeight();
403 void OnCursorKeyEvent( const Event& event );
405 void OnTapEvent( const Event& event );
407 void OnPanEvent( const Event& event );
409 void OnLongPressEvent( const Event& event );
411 void OnHandleEvent( const Event& event );
413 void OnSelectEvent( const Event& event );
415 void OnSelectAllEvent();
417 void RetrieveSelection( std::string& selectedText, bool deleteAfterRetrieval );
419 void ShowClipboard();
421 void HideClipboard();
423 bool CopyStringToClipboard( std::string& source );
425 void SendSelectionToClipboard( bool deleteAfterSending );
427 void GetTextFromClipboard( unsigned int itemIndex, std::string& retrievedString );
429 void RepositionSelectionHandles();
430 void RepositionSelectionHandles( float visualX, float visualY );
432 void SetPopupButtons();
434 void ChangeState( EventData::State newState );
435 LineIndex GetClosestLine( float y ) const;
437 void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
440 * @brief Retrieves the cursor's logical position for a given touch point x,y
442 * @param[in] visualX The touch point x.
443 * @param[in] visualY The touch point y.
445 * @return The logical cursor position (in characters). 0 is just before the first character, a value equal to the number of characters is just after the last character.
447 CharacterIndex GetClosestCursorIndex( float visualX,
451 * @brief Calculates the cursor's position for a given character index in the logical order.
453 * It retrieves as well the line's height and the cursor's height and
454 * if there is a valid alternative cursor, its position and height.
456 * @param[in] logical The logical cursor position (in characters). 0 is just before the first character, a value equal to the number of characters is just after the last character.
457 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
459 void GetCursorPosition( CharacterIndex logical,
460 CursorInfo& cursorInfo );
463 * @brief Calculates the new cursor index.
465 * It takes into account that in some scripts multiple characters can form a glyph and all of them
466 * need to be jumped with one key event.
468 * @param[in] index The initial new index.
470 * @return The new cursor index.
472 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
475 * @brief Updates the cursor position.
477 * Sets the cursor's position into the decorator. It transforms the cursor's position into decorator's coords.
478 * It sets the position of the secondary cursor if it's a valid one.
479 * Sets which cursors are active.
481 * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
484 void UpdateCursorPosition( const CursorInfo& cursorInfo );
487 * @brief Updates the position of the given selection handle. It transforms the handle's position into decorator's coords.
489 * @param[in] handleType One of the selection handles.
490 * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
492 void UpdateSelectionHandle( HandleType handleType,
493 const CursorInfo& cursorInfo );
496 * @biref Clamps the horizontal scrolling to get the control always filled with text.
498 * @param[in] actualSize The size of the laid out text.
500 void ClampHorizontalScroll( const Vector2& actualSize );
503 * @biref Clamps the vertical scrolling to get the control always filled with text.
505 * @param[in] actualSize The size of the laid out text.
507 void ClampVerticalScroll( const Vector2& actualSize );
510 * @brief Scrolls the text to make a position visible.
512 * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
514 * @param[in] position A position in decorator coords.
516 * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
517 * or moving the selection handles.
519 void ScrollToMakePositionVisible( const Vector2& position );
522 * @brief Scrolls the text to make the cursor visible.
524 * This method is called after deleting text.
526 void ScrollTextToMatchCursor( const CursorInfo& cursorInfo);
528 ControlInterface& mControlInterface; ///< Reference to the text controller.
529 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
530 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
531 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
532 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
533 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
534 Clipboard mClipboard; ///< Handle to the system clipboard
535 View mView; ///< The view interface to the rendering back-end.
536 MetricsPtr mMetrics; ///< A wrapper around FontClient used to get metrics & potentially down-scaled Emoji metrics.
537 LayoutEngine mLayoutEngine; ///< The layout engine.
538 Vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
539 Vector4 mTextColor; ///< The regular text color
540 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
541 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
542 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
544 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
545 bool mUserDefinedFontFamily:1; ///< Whether the Font family was set by the user instead of being left as sytem default.
546 bool mMarkupProcessorEnabled:1; ///< Whether the mark-up procesor is enabled.
551 } // namespace Toolkit
555 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__