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/layouts/layout-engine.h>
27 #include <dali-toolkit/internal/text/logical-model-impl.h>
28 #include <dali-toolkit/internal/text/text-controller.h>
29 #include <dali-toolkit/internal/text/text-view.h>
30 #include <dali-toolkit/internal/text/visual-model-impl.h>
43 // Used to queue input events until DoRelayout()
51 LEFT_SELECTION_HANDLE_EVENT,
52 RIGHT_SELECTION_HANDLE_EVENT,
64 Event( Type eventType )
84 primaryCursorHeight( 0.f ),
85 secondaryCursorHeight( 0.f ),
86 isSecondaryCursor( false )
92 Vector2 primaryPosition; ///< The primary cursor's position.
93 Vector2 secondaryPosition; ///< The secondary cursor's position.
94 float lineHeight; ///< The height of the line where the cursor is placed.
95 float primaryCursorHeight; ///< The primary cursor's height.
96 float secondaryCursorHeight; ///< The secondary cursor's height.
97 bool isSecondaryCursor; ///< Whether the secondary cursor is valid.
109 EDITING_WITH_GRAB_HANDLE,
110 EDITING_WITH_PASTE_POPUP,
112 SELECTION_HANDLE_PANNING
115 EventData( DecoratorPtr decorator );
119 static bool IsEditingState( State stateToCheck )
121 return ( stateToCheck == EDITING || stateToCheck == EDITING_WITH_POPUP || stateToCheck == EDITING_WITH_GRAB_HANDLE || stateToCheck == EDITING_WITH_PASTE_POPUP );
124 DecoratorPtr mDecorator; ///< Pointer to the decorator.
125 ImfManager mImfManager; ///< The Input Method Framework Manager.
126 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus.
127 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive.
128 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color.
131 * This is used to delay handling events until after the model has been updated.
132 * The number of updates to the model is minimized to improve performance.
134 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
137 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
138 * Typically this will have a negative value with scrolling occurs.
140 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
142 State mState; ///< Selection mode, edit mode etc.
144 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
145 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
146 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
148 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
149 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
151 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
152 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
153 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
154 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
155 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
156 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
157 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
158 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
159 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
160 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
161 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
162 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
163 bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated.
164 bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters.
165 bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text.
172 TEXT_REPLACED, ///< The entire text was replaced
173 TEXT_INSERTED, ///< Insert characters at the current cursor position
174 TEXT_DELETED ///< Characters were deleted
183 : mFontDescription(),
185 mDefaultPointSize( 0.f ),
188 // Initially use the default platform font
189 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
190 fontClient.GetDefaultPlatformFontDescription( mFontDescription );
193 FontId GetFontId( TextAbstraction::FontClient& fontClient )
197 const PointSize26Dot6 pointSize = static_cast<PointSize26Dot6>( mDefaultPointSize * 64.f );
198 mFontId = fontClient.GetFontId( mFontDescription, pointSize );
204 TextAbstraction::FontDescription mFontDescription;
205 std::string mFontStyle;
206 float mDefaultPointSize;
210 struct Controller::Impl
212 Impl( ControlInterface& controlInterface )
213 : mControlInterface( controlInterface ),
216 mFontDefaults( NULL ),
224 mTextColor( Color::BLACK ),
226 mOperationsPending( NO_OPERATION ),
227 mMaximumNumberOfCharacters( 50u ),
228 mRecalculateNaturalSize( true ),
229 mUserDefinedFontFamily( false ),
230 mMarkupProcessorEnabled( false )
232 mLogicalModel = LogicalModel::New();
233 mVisualModel = VisualModel::New();
235 mFontClient = TextAbstraction::FontClient::Get();
236 mClipboard = Clipboard::Get();
238 mView.SetVisualModel( mVisualModel );
240 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
241 mMetrics = Metrics::New( mFontClient );
242 mLayoutEngine.SetMetrics( mMetrics );
244 // Set the text properties to default
245 mVisualModel->SetUnderlineEnabled( false );
246 mVisualModel->SetUnderlineHeight( 0.0f );
251 delete mFontDefaults;
256 * @brief Request a relayout using the ControlInterface.
258 void RequestRelayout();
261 * @brief Request a relayout using the ControlInterface.
263 void QueueModifyEvent( ModifyEvent::Type type )
265 if( ModifyEvent::TEXT_REPLACED == type)
267 // Cancel previously queued inserts etc.
268 mModifyEvents.Clear();
273 mModifyEvents.PushBack( event );
275 // The event will be processed during relayout
280 * @brief Helper to move the cursor, grab handle etc.
282 bool ProcessInputEvents();
285 * @brief Helper to check whether any place-holder text is available.
287 bool IsPlaceholderAvailable() const
289 return ( mEventData &&
290 ( !mEventData->mPlaceholderTextInactive.empty() ||
291 !mEventData->mPlaceholderTextActive.empty() )
295 bool IsShowingPlaceholderText() const
297 return ( mEventData && mEventData->mIsShowingPlaceholderText );
301 * @brief Helper to check whether active place-holder text is available.
303 bool IsFocusedPlaceholderAvailable() const
305 return ( mEventData && !mEventData->mPlaceholderTextActive.empty() );
308 bool IsShowingRealText() const
310 return ( !IsShowingPlaceholderText() &&
311 0u != mLogicalModel->mText.Count() );
315 * @brief Called when placeholder-text is hidden
317 void PlaceholderCleared()
321 mEventData->mIsShowingPlaceholderText = false;
323 // Remove mPlaceholderTextColor
324 mVisualModel->SetTextColor( mTextColor );
328 void ClearPreEditFlag()
332 mEventData->mPreEditFlag = false;
333 mEventData->mPreEditStartPosition = 0;
334 mEventData->mPreEditLength = 0;
338 void ResetImfManager()
342 // Reset incase we are in a pre-edit state.
343 if( mEventData->mImfManager )
345 mEventData->mImfManager.Reset(); // Will trigger a message ( commit, get surrounding )
352 bool IsClipboardEmpty()
354 bool result( mClipboard && mClipboard.NumberOfItems() );
355 return !result; // // If NumberOfItems greater than 0, return false
358 void UpdateModel( OperationsMask operationsRequired );
361 * @brief Retrieve the default fonts.
363 * @param[out] fonts The default font family, style and point sizes.
364 * @param[in] numberOfCharacters The number of characters in the logical model.
366 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
369 * @brief Retrieve the line height of the default font.
371 float GetDefaultFontLineHeight();
373 void OnCursorKeyEvent( const Event& event );
375 void OnTapEvent( const Event& event );
377 void OnPanEvent( const Event& event );
379 void OnLongPressEvent( const Event& event );
381 void OnHandleEvent( const Event& event );
383 void OnSelectEvent( const Event& event );
385 void OnSelectAllEvent();
387 void RetrieveSelection( std::string& selectedText, bool deleteAfterRetrieval );
389 void ShowClipboard();
391 void HideClipboard();
393 bool CopyStringToClipboard( std::string& source );
395 void SendSelectionToClipboard( bool deleteAfterSending );
397 void GetTextFromClipboard( unsigned int itemIndex, std::string& retrievedString );
399 void RepositionSelectionHandles();
400 void RepositionSelectionHandles( float visualX, float visualY );
402 void SetPopupButtons();
404 void ChangeState( EventData::State newState );
405 LineIndex GetClosestLine( float y ) const;
407 void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
410 * @brief Retrieves the cursor's logical position for a given touch point x,y
412 * @param[in] visualX The touch point x.
413 * @param[in] visualY The touch point y.
415 * @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.
417 CharacterIndex GetClosestCursorIndex( float visualX,
421 * @brief Calculates the cursor's position for a given character index in the logical order.
423 * It retrieves as well the line's height and the cursor's height and
424 * if there is a valid alternative cursor, its position and height.
426 * @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.
427 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
429 void GetCursorPosition( CharacterIndex logical,
430 CursorInfo& cursorInfo );
433 * @brief Calculates the new cursor index.
435 * It takes into account that in some scripts multiple characters can form a glyph and all of them
436 * need to be jumped with one key event.
438 * @param[in] index The initial new index.
440 * @return The new cursor index.
442 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
445 * @brief Updates the cursor position.
447 * Sets the cursor's position into the decorator. It transforms the cursor's position into decorator's coords.
448 * It sets the position of the secondary cursor if it's a valid one.
449 * Sets which cursors are active.
451 * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
454 void UpdateCursorPosition( const CursorInfo& cursorInfo );
457 * @brief Updates the position of the given selection handle. It transforms the handle's position into decorator's coords.
459 * @param[in] handleType One of the selection handles.
460 * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
462 void UpdateSelectionHandle( HandleType handleType,
463 const CursorInfo& cursorInfo );
466 * @biref Clamps the horizontal scrolling to get the control always filled with text.
468 * @param[in] actualSize The size of the laid out text.
470 void ClampHorizontalScroll( const Vector2& actualSize );
473 * @biref Clamps the vertical scrolling to get the control always filled with text.
475 * @param[in] actualSize The size of the laid out text.
477 void ClampVerticalScroll( const Vector2& actualSize );
480 * @brief Scrolls the text to make a position visible.
482 * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
484 * @param[in] position A position in decorator coords.
486 * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
487 * or moving the selection handles.
489 void ScrollToMakePositionVisible( const Vector2& position );
492 * @brief Scrolls the text to make the cursor visible.
494 * This method is called after deleting text.
496 void ScrollTextToMatchCursor( const CursorInfo& cursorInfo);
498 ControlInterface& mControlInterface; ///< Reference to the text controller.
499 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
500 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
501 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
502 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
503 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
504 Clipboard mClipboard; ///< Handle to the system clipboard
505 View mView; ///< The view interface to the rendering back-end.
506 MetricsPtr mMetrics; ///< A wrapper around FontClient used to get metrics & potentially down-scaled Emoji metrics.
507 LayoutEngine mLayoutEngine; ///< The layout engine.
508 Vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
509 Vector4 mTextColor; ///< The regular text color
510 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
511 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
512 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
514 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
515 bool mUserDefinedFontFamily:1; ///< Whether the Font family was set by the user instead of being left as sytem default.
516 bool mMarkupProcessorEnabled:1; ///< Whether the mark-up procesor is enabled.
521 } // namespace Toolkit
525 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__