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/adaptor-framework/imf-manager.h>
24 #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/visual-model-impl.h>
43 // Used to queue input events until DoRelayout()
50 LEFT_SELECTION_HANDLE_EVENT,
51 RIGHT_SELECTION_HANDLE_EVENT,
63 Event( Type eventType )
83 primaryCursorHeight( 0.f ),
84 secondaryCursorHeight( 0.f ),
85 isSecondaryCursor( false )
91 Vector2 primaryPosition; ///< The primary cursor's position.
92 Vector2 secondaryPosition; ///< The secondary cursor's position.
93 float lineHeight; ///< The height of the line where the cursor is placed.
94 float primaryCursorHeight; ///< The primary cursor's height.
95 float secondaryCursorHeight; ///< The secondary cursor's height.
96 bool isSecondaryCursor; ///< Whether the secondary cursor is valid.
109 SELECTION_HANDLE_PANNING
112 EventData( DecoratorPtr decorator );
116 DecoratorPtr mDecorator; ///< Pointer to the decorator
117 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus
118 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive
119 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color
122 * This is used to delay handling events until after the model has been updated.
123 * The number of updates to the model is minimized to improve performance.
125 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
128 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
129 * Typically this will have a negative value with scrolling occurs.
131 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
133 State mState; ///< Selection mode, edit mode etc.
135 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
136 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
137 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
139 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
140 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
142 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
143 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
144 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
145 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
146 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
147 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
148 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
149 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
150 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
151 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
152 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
153 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
154 bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated.
155 bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters.
156 bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text
163 TEXT_REPLACED, ///< The entire text was replaced
164 TEXT_INSERTED, ///< Insert characters at the current cursor position
165 TEXT_DELETED ///< Characters were deleted
174 : mDefaultPointSize(0.0f),
179 FontId GetFontId( TextAbstraction::FontClient& fontClient )
183 Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
184 mFontId = fontClient.GetFontId( mDefaultFontFamily, mDefaultFontStyle, pointSize );
190 std::string mDefaultFontFamily;
191 std::string mDefaultFontStyle;
192 float mDefaultPointSize;
196 struct Controller::Impl
198 Impl( ControlInterface& controlInterface )
199 : mControlInterface( controlInterface ),
202 mFontDefaults( NULL ),
210 mTextColor( Color::BLACK ),
212 mOperationsPending( NO_OPERATION ),
213 mMaximumNumberOfCharacters( 50 ),
214 mRecalculateNaturalSize( true )
216 mLogicalModel = LogicalModel::New();
217 mVisualModel = VisualModel::New();
219 mFontClient = TextAbstraction::FontClient::Get();
220 mClipboard = Clipboard::Get();
222 mView.SetVisualModel( mVisualModel );
224 // Set the text properties to default
225 mVisualModel->SetUnderlineEnabled( false );
226 mVisualModel->SetUnderlineHeight( 0.0f );
235 * @brief Request a relayout using the ControlInterface.
237 void RequestRelayout();
240 * @brief Request a relayout using the ControlInterface.
242 void QueueModifyEvent( ModifyEvent::Type type )
244 if( ModifyEvent::TEXT_REPLACED == type)
246 // Cancel previously queued inserts etc.
247 mModifyEvents.clear();
252 mModifyEvents.push_back( event );
254 // The event will be processed during relayout
259 * @brief Helper to move the cursor, grab handle etc.
261 bool ProcessInputEvents();
264 * @brief Helper to check whether any place-holder text is available.
266 bool IsPlaceholderAvailable() const
268 return ( mEventData &&
269 ( !mEventData->mPlaceholderTextInactive.empty() ||
270 !mEventData->mPlaceholderTextActive.empty() )
274 bool IsShowingPlaceholderText() const
276 return ( mEventData && mEventData->mIsShowingPlaceholderText );
280 * @brief Called when placeholder-text is hidden
282 void PlaceholderCleared()
286 mEventData->mIsShowingPlaceholderText = false;
288 // Remove mPlaceholderTextColor
289 mVisualModel->SetTextColor( mTextColor );
293 void ClearPreEditFlag()
297 mEventData->mPreEditFlag = false;
298 mEventData->mPreEditStartPosition = 0;
299 mEventData->mPreEditLength = 0;
303 void ResetImfManager()
305 // Reset incase we are in a pre-edit state.
306 ImfManager imfManager = ImfManager::Get();
309 imfManager.Reset(); // Will trigger a commit message
315 bool IsClipboardEmpty()
317 bool result( mClipboard && mClipboard.NumberOfItems() );
318 return !result; // // If NumberOfItems greater than 0, return false
321 void UpdateModel( OperationsMask operationsRequired );
324 * @brief Retrieve the default fonts.
326 * @param[out] fonts The default font family, style and point sizes.
327 * @param[in] numberOfCharacters The number of characters in the logical model.
329 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
331 void OnCursorKeyEvent( const Event& event );
333 void OnTapEvent( const Event& event );
335 void OnPanEvent( const Event& event );
337 void OnHandleEvent( const Event& event );
339 void OnSelectEvent( const Event& event );
341 void OnSelectAllEvent();
343 void RetrieveSelection( std::string& selectedText, bool deleteAfterRetreival );
345 void ShowClipboard();
347 void HideClipboard();
349 bool CopyStringToClipboard( std::string& source );
351 void SendSelectionToClipboard( bool deleteAfterSending );
353 void GetTextFromClipboard( unsigned int itemIndex, std::string& retreivedString );
355 void RepositionSelectionHandles( CharacterIndex selectionStart, CharacterIndex selectionEnd );
356 void RepositionSelectionHandles( float visualX, float visualY );
358 void SetPopupButtons();
360 void ChangeState( EventData::State newState );
361 LineIndex GetClosestLine( float y ) const;
363 void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
366 * @brief Retrieves the cursor's logical position for a given touch point x,y
368 * @param[in] visualX The touch point x.
369 * @param[in] visualY The touch point y.
371 * @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.
373 CharacterIndex GetClosestCursorIndex( float visualX,
377 * @brief Calculates the cursor's position for a given character index in the logical order.
379 * It retrieves as well the line's height and the cursor's height and
380 * if there is a valid alternative cursor, its position and height.
382 * @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.
383 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
385 void GetCursorPosition( CharacterIndex logical,
386 CursorInfo& cursorInfo );
389 * @brief Calculates the new cursor index.
391 * It takes into account that in some scripts multiple characters can form a glyph and all of them
392 * need to be jumped with one key event.
394 * @param[in] index The initial new index.
396 * @return The new cursor index.
398 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
401 * @brief Updates the cursor position.
403 * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
404 * It sets the position of the secondary cursor if it's a valid one.
405 * Sets which cursors are active.
407 void UpdateCursorPosition();
410 * @brief Updates the position of the given selection handle.
412 * @param[in] handleType One of the selection handles.
414 void UpdateSelectionHandle( HandleType handleType );
417 * @biref Clamps the horizontal scrolling to get the control always filled with text.
419 * @param[in] actualSize The size of the laid out text.
421 void ClampHorizontalScroll( const Vector2& actualSize );
424 * @biref Clamps the vertical scrolling to get the control always filled with text.
426 * @param[in] actualSize The size of the laid out text.
428 void ClampVerticalScroll( const Vector2& actualSize );
431 * @brief Scrolls the text to make a position visible.
433 * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
435 * @param[in] position A position in decorator coords.
437 * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
438 * or moving the selection handles.
440 void ScrollToMakePositionVisible( const Vector2& position );
443 * @brief Scrolls the text to make the cursor visible.
445 * This method is called after deleting text.
447 void ScrollTextToMatchCursor();
449 ControlInterface& mControlInterface; ///< Reference to the text controller.
450 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
451 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
452 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
453 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
454 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
455 Clipboard mClipboard; ///< Handle to the system clipboard
456 View mView; ///< The view interface to the rendering back-end.
457 LayoutEngine mLayoutEngine; ///< The layout engine.
458 std::vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
459 Size mControlSize; ///< The size of the control.
460 Vector4 mTextColor; ///< The regular text color
461 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
462 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
463 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
464 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
469 } // namespace Toolkit
473 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__