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.
110 EDITING_WITH_GRAB_HANDLE,
112 SELECTION_HANDLE_PANNING
115 EventData( DecoratorPtr decorator );
119 DecoratorPtr mDecorator; ///< Pointer to the decorator
120 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus
121 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive
122 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color
125 * This is used to delay handling events until after the model has been updated.
126 * The number of updates to the model is minimized to improve performance.
128 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
131 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
132 * Typically this will have a negative value with scrolling occurs.
134 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
136 State mState; ///< Selection mode, edit mode etc.
138 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
139 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
140 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
142 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
143 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
145 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
146 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
147 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
148 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
149 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
150 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
151 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
152 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
153 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
154 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
155 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
156 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
157 bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated.
158 bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters.
159 bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text
166 TEXT_REPLACED, ///< The entire text was replaced
167 TEXT_INSERTED, ///< Insert characters at the current cursor position
168 TEXT_DELETED ///< Characters were deleted
177 : mDefaultPointSize(0.0f),
182 FontId GetFontId( TextAbstraction::FontClient& fontClient )
186 Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
187 mFontId = fontClient.GetFontId( mDefaultFontFamily, mDefaultFontStyle, pointSize );
193 std::string mDefaultFontFamily;
194 std::string mDefaultFontStyle;
195 float mDefaultPointSize;
199 struct Controller::Impl
201 Impl( ControlInterface& controlInterface )
202 : mControlInterface( controlInterface ),
205 mFontDefaults( NULL ),
213 mTextColor( Color::BLACK ),
215 mOperationsPending( NO_OPERATION ),
216 mMaximumNumberOfCharacters( 50 ),
217 mRecalculateNaturalSize( true )
219 mLogicalModel = LogicalModel::New();
220 mVisualModel = VisualModel::New();
222 mFontClient = TextAbstraction::FontClient::Get();
223 mClipboard = Clipboard::Get();
225 mView.SetVisualModel( mVisualModel );
227 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
228 mMetrics = Metrics::New( mFontClient );
229 mLayoutEngine.SetMetrics( mMetrics );
231 // Set the text properties to default
232 mVisualModel->SetUnderlineEnabled( false );
233 mVisualModel->SetUnderlineHeight( 0.0f );
238 delete mFontDefaults;
243 * @brief Request a relayout using the ControlInterface.
245 void RequestRelayout();
248 * @brief Request a relayout using the ControlInterface.
250 void QueueModifyEvent( ModifyEvent::Type type )
252 if( ModifyEvent::TEXT_REPLACED == type)
254 // Cancel previously queued inserts etc.
255 mModifyEvents.clear();
260 mModifyEvents.push_back( event );
262 // The event will be processed during relayout
267 * @brief Helper to move the cursor, grab handle etc.
269 bool ProcessInputEvents();
272 * @brief Helper to check whether any place-holder text is available.
274 bool IsPlaceholderAvailable() const
276 return ( mEventData &&
277 ( !mEventData->mPlaceholderTextInactive.empty() ||
278 !mEventData->mPlaceholderTextActive.empty() )
282 bool IsShowingPlaceholderText() const
284 return ( mEventData && mEventData->mIsShowingPlaceholderText );
287 bool IsShowingRealText() const
289 return ( !IsShowingPlaceholderText() &&
290 0u != mLogicalModel->mText.Count() );
294 * @brief Called when placeholder-text is hidden
296 void PlaceholderCleared()
300 mEventData->mIsShowingPlaceholderText = false;
302 // Remove mPlaceholderTextColor
303 mVisualModel->SetTextColor( mTextColor );
307 void ClearPreEditFlag()
311 mEventData->mPreEditFlag = false;
312 mEventData->mPreEditStartPosition = 0;
313 mEventData->mPreEditLength = 0;
317 void ResetImfManager()
319 // Reset incase we are in a pre-edit state.
320 ImfManager imfManager = ImfManager::Get();
323 imfManager.Reset(); // Will trigger a commit message
329 bool IsClipboardEmpty()
331 bool result( mClipboard && mClipboard.NumberOfItems() );
332 return !result; // // If NumberOfItems greater than 0, return false
335 void UpdateModel( OperationsMask operationsRequired );
338 * @brief Retrieve the default fonts.
340 * @param[out] fonts The default font family, style and point sizes.
341 * @param[in] numberOfCharacters The number of characters in the logical model.
343 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
346 * @brief Retrieve the line height of the default font.
348 float GetDefaultFontLineHeight();
350 void OnCursorKeyEvent( const Event& event );
352 void OnTapEvent( const Event& event );
354 void OnPanEvent( const Event& event );
356 void OnLongPressEvent( const Event& event );
358 void OnHandleEvent( const Event& event );
360 void OnSelectEvent( const Event& event );
362 void OnSelectAllEvent();
364 void RetrieveSelection( std::string& selectedText, bool deleteAfterRetreival );
366 void ShowClipboard();
368 void HideClipboard();
370 bool CopyStringToClipboard( std::string& source );
372 void SendSelectionToClipboard( bool deleteAfterSending );
374 void GetTextFromClipboard( unsigned int itemIndex, std::string& retreivedString );
376 void RepositionSelectionHandles( CharacterIndex selectionStart, CharacterIndex selectionEnd );
377 void RepositionSelectionHandles( float visualX, float visualY );
379 void SetPopupButtons();
381 void ChangeState( EventData::State newState );
382 LineIndex GetClosestLine( float y ) const;
384 void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
387 * @brief Retrieves the cursor's logical position for a given touch point x,y
389 * @param[in] visualX The touch point x.
390 * @param[in] visualY The touch point y.
392 * @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.
394 CharacterIndex GetClosestCursorIndex( float visualX,
398 * @brief Calculates the cursor's position for a given character index in the logical order.
400 * It retrieves as well the line's height and the cursor's height and
401 * if there is a valid alternative cursor, its position and height.
403 * @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.
404 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
406 void GetCursorPosition( CharacterIndex logical,
407 CursorInfo& cursorInfo );
410 * @brief Calculates the new cursor index.
412 * It takes into account that in some scripts multiple characters can form a glyph and all of them
413 * need to be jumped with one key event.
415 * @param[in] index The initial new index.
417 * @return The new cursor index.
419 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
422 * @brief Updates the cursor position.
424 * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
425 * It sets the position of the secondary cursor if it's a valid one.
426 * Sets which cursors are active.
428 void UpdateCursorPosition();
431 * @brief Updates the position of the given selection handle.
433 * @param[in] handleType One of the selection handles.
435 void UpdateSelectionHandle( HandleType handleType );
438 * @biref Clamps the horizontal scrolling to get the control always filled with text.
440 * @param[in] actualSize The size of the laid out text.
442 void ClampHorizontalScroll( const Vector2& actualSize );
445 * @biref Clamps the vertical scrolling to get the control always filled with text.
447 * @param[in] actualSize The size of the laid out text.
449 void ClampVerticalScroll( const Vector2& actualSize );
452 * @brief Scrolls the text to make a position visible.
454 * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
456 * @param[in] position A position in decorator coords.
458 * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
459 * or moving the selection handles.
461 void ScrollToMakePositionVisible( const Vector2& position );
464 * @brief Scrolls the text to make the cursor visible.
466 * This method is called after deleting text.
468 void ScrollTextToMatchCursor();
470 ControlInterface& mControlInterface; ///< Reference to the text controller.
471 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
472 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
473 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
474 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
475 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
476 Clipboard mClipboard; ///< Handle to the system clipboard
477 View mView; ///< The view interface to the rendering back-end.
478 MetricsPtr mMetrics; ///< A wrapper around FontClient used to get metrics & potentially down-scaled Emoji metrics.
479 LayoutEngine mLayoutEngine; ///< The layout engine.
480 std::vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
481 Vector4 mTextColor; ///< The regular text color
482 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
483 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
484 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
485 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
490 } // namespace Toolkit
494 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__