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>
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.
111 EDITING_WITH_GRAB_HANDLE,
113 SELECTION_HANDLE_PANNING
116 EventData( DecoratorPtr decorator );
120 DecoratorPtr mDecorator; ///< Pointer to the decorator
121 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus
122 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive
123 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color
126 * This is used to delay handling events until after the model has been updated.
127 * The number of updates to the model is minimized to improve performance.
129 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
132 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
133 * Typically this will have a negative value with scrolling occurs.
135 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
137 State mState; ///< Selection mode, edit mode etc.
139 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
140 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
141 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
143 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
144 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
146 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
147 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
148 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
149 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
150 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
151 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
152 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
153 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
154 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
155 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
156 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
157 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
158 bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated.
159 bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters.
160 bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text
167 TEXT_REPLACED, ///< The entire text was replaced
168 TEXT_INSERTED, ///< Insert characters at the current cursor position
169 TEXT_DELETED ///< Characters were deleted
178 : mFontDescription(),
180 mDefaultPointSize(0.0f),
185 FontId GetFontId( TextAbstraction::FontClient& fontClient )
189 Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
190 mFontId = fontClient.GetFontId( mFontDescription, pointSize );
196 TextAbstraction::FontDescription mFontDescription;
197 std::string mFontStyle;
198 float mDefaultPointSize;
202 struct Controller::Impl
204 Impl( ControlInterface& controlInterface )
205 : mControlInterface( controlInterface ),
208 mFontDefaults( NULL ),
216 mTextColor( Color::BLACK ),
218 mOperationsPending( NO_OPERATION ),
219 mMaximumNumberOfCharacters( 50 ),
220 mRecalculateNaturalSize( true ),
221 mUserDefinedFontFamily( false )
223 mLogicalModel = LogicalModel::New();
224 mVisualModel = VisualModel::New();
226 mFontClient = TextAbstraction::FontClient::Get();
227 mClipboard = Clipboard::Get();
229 mView.SetVisualModel( mVisualModel );
231 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
232 mMetrics = Metrics::New( mFontClient );
233 mLayoutEngine.SetMetrics( mMetrics );
235 // Set the text properties to default
236 mVisualModel->SetUnderlineEnabled( false );
237 mVisualModel->SetUnderlineHeight( 0.0f );
242 delete mFontDefaults;
247 * @brief Request a relayout using the ControlInterface.
249 void RequestRelayout();
252 * @brief Request a relayout using the ControlInterface.
254 void QueueModifyEvent( ModifyEvent::Type type )
256 if( ModifyEvent::TEXT_REPLACED == type)
258 // Cancel previously queued inserts etc.
259 mModifyEvents.clear();
264 mModifyEvents.push_back( event );
266 // The event will be processed during relayout
271 * @brief Helper to move the cursor, grab handle etc.
273 bool ProcessInputEvents();
276 * @brief Helper to check whether any place-holder text is available.
278 bool IsPlaceholderAvailable() const
280 return ( mEventData &&
281 ( !mEventData->mPlaceholderTextInactive.empty() ||
282 !mEventData->mPlaceholderTextActive.empty() )
286 bool IsShowingPlaceholderText() const
288 return ( mEventData && mEventData->mIsShowingPlaceholderText );
291 bool IsShowingRealText() const
293 return ( !IsShowingPlaceholderText() &&
294 0u != mLogicalModel->mText.Count() );
298 * @brief Called when placeholder-text is hidden
300 void PlaceholderCleared()
304 mEventData->mIsShowingPlaceholderText = false;
306 // Remove mPlaceholderTextColor
307 mVisualModel->SetTextColor( mTextColor );
311 void ClearPreEditFlag()
315 mEventData->mPreEditFlag = false;
316 mEventData->mPreEditStartPosition = 0;
317 mEventData->mPreEditLength = 0;
321 void ResetImfManager()
323 // Reset incase we are in a pre-edit state.
324 ImfManager imfManager = ImfManager::Get();
327 imfManager.Reset(); // Will trigger a commit message
333 bool IsClipboardEmpty()
335 bool result( mClipboard && mClipboard.NumberOfItems() );
336 return !result; // // If NumberOfItems greater than 0, return false
339 void UpdateModel( OperationsMask operationsRequired );
342 * @brief Retrieve the default fonts.
344 * @param[out] fonts The default font family, style and point sizes.
345 * @param[in] numberOfCharacters The number of characters in the logical model.
347 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
350 * @brief Retrieve the line height of the default font.
352 float GetDefaultFontLineHeight();
354 void OnCursorKeyEvent( const Event& event );
356 void OnTapEvent( const Event& event );
358 void OnPanEvent( const Event& event );
360 void OnLongPressEvent( const Event& event );
362 void OnHandleEvent( const Event& event );
364 void OnSelectEvent( const Event& event );
366 void OnSelectAllEvent();
368 void RetrieveSelection( std::string& selectedText, bool deleteAfterRetreival );
370 void ShowClipboard();
372 void HideClipboard();
374 bool CopyStringToClipboard( std::string& source );
376 void SendSelectionToClipboard( bool deleteAfterSending );
378 void GetTextFromClipboard( unsigned int itemIndex, std::string& retreivedString );
380 void RepositionSelectionHandles( CharacterIndex selectionStart, CharacterIndex selectionEnd );
381 void RepositionSelectionHandles( float visualX, float visualY );
383 void SetPopupButtons();
385 void ChangeState( EventData::State newState );
386 LineIndex GetClosestLine( float y ) const;
388 void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
391 * @brief Retrieves the cursor's logical position for a given touch point x,y
393 * @param[in] visualX The touch point x.
394 * @param[in] visualY The touch point y.
396 * @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.
398 CharacterIndex GetClosestCursorIndex( float visualX,
402 * @brief Calculates the cursor's position for a given character index in the logical order.
404 * It retrieves as well the line's height and the cursor's height and
405 * if there is a valid alternative cursor, its position and height.
407 * @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.
408 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
410 void GetCursorPosition( CharacterIndex logical,
411 CursorInfo& cursorInfo );
414 * @brief Calculates the new cursor index.
416 * It takes into account that in some scripts multiple characters can form a glyph and all of them
417 * need to be jumped with one key event.
419 * @param[in] index The initial new index.
421 * @return The new cursor index.
423 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
426 * @brief Updates the cursor position.
428 * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
429 * It sets the position of the secondary cursor if it's a valid one.
430 * Sets which cursors are active.
432 void UpdateCursorPosition();
435 * @brief Updates the position of the given selection handle.
437 * @param[in] handleType One of the selection handles.
439 void UpdateSelectionHandle( HandleType handleType );
442 * @biref Clamps the horizontal scrolling to get the control always filled with text.
444 * @param[in] actualSize The size of the laid out text.
446 void ClampHorizontalScroll( const Vector2& actualSize );
449 * @biref Clamps the vertical scrolling to get the control always filled with text.
451 * @param[in] actualSize The size of the laid out text.
453 void ClampVerticalScroll( const Vector2& actualSize );
456 * @brief Scrolls the text to make a position visible.
458 * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
460 * @param[in] position A position in decorator coords.
462 * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
463 * or moving the selection handles.
465 void ScrollToMakePositionVisible( const Vector2& position );
468 * @brief Scrolls the text to make the cursor visible.
470 * This method is called after deleting text.
472 void ScrollTextToMatchCursor();
474 ControlInterface& mControlInterface; ///< Reference to the text controller.
475 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
476 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
477 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
478 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
479 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
480 Clipboard mClipboard; ///< Handle to the system clipboard
481 View mView; ///< The view interface to the rendering back-end.
482 MetricsPtr mMetrics; ///< A wrapper around FontClient used to get metrics & potentially down-scaled Emoji metrics.
483 LayoutEngine mLayoutEngine; ///< The layout engine.
484 std::vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
485 Vector4 mTextColor; ///< The regular text color
486 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
487 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
488 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
490 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
491 bool mUserDefinedFontFamily:1; ///< Whether the Font family was set by the user instead of being left as sytem default.
496 } // namespace Toolkit
500 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__