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()
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.
111 SELECTION_HANDLE_PANNING
114 EventData( DecoratorPtr decorator );
118 DecoratorPtr mDecorator; ///< Pointer to the decorator
119 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus
120 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive
121 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color
124 * This is used to delay handling events until after the model has been updated.
125 * The number of updates to the model is minimized to improve performance.
127 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
130 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
131 * Typically this will have a negative value with scrolling occurs.
133 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
135 State mState; ///< Selection mode, edit mode etc.
137 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
138 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
139 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
141 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
142 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
144 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
145 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
146 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
147 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
148 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
149 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
150 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
151 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
152 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
153 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
154 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
155 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
156 bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated.
157 bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters.
158 bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text
165 TEXT_REPLACED, ///< The entire text was replaced
166 TEXT_INSERTED, ///< Insert characters at the current cursor position
167 TEXT_DELETED ///< Characters were deleted
176 : mDefaultPointSize(0.0f),
181 FontId GetFontId( TextAbstraction::FontClient& fontClient )
185 Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
186 mFontId = fontClient.GetFontId( mDefaultFontFamily, mDefaultFontStyle, pointSize );
192 std::string mDefaultFontFamily;
193 std::string mDefaultFontStyle;
194 float mDefaultPointSize;
198 struct Controller::Impl
200 Impl( ControlInterface& controlInterface )
201 : mControlInterface( controlInterface ),
204 mFontDefaults( NULL ),
212 mTextColor( Color::BLACK ),
214 mOperationsPending( NO_OPERATION ),
215 mMaximumNumberOfCharacters( 50 ),
216 mRecalculateNaturalSize( true )
218 mLogicalModel = LogicalModel::New();
219 mVisualModel = VisualModel::New();
221 mFontClient = TextAbstraction::FontClient::Get();
222 mClipboard = Clipboard::Get();
224 mView.SetVisualModel( mVisualModel );
226 // Set the text properties to default
227 mVisualModel->SetUnderlineEnabled( false );
228 mVisualModel->SetUnderlineHeight( 0.0f );
237 * @brief Request a relayout using the ControlInterface.
239 void RequestRelayout();
242 * @brief Request a relayout using the ControlInterface.
244 void QueueModifyEvent( ModifyEvent::Type type )
246 if( ModifyEvent::TEXT_REPLACED == type)
248 // Cancel previously queued inserts etc.
249 mModifyEvents.clear();
254 mModifyEvents.push_back( event );
256 // The event will be processed during relayout
261 * @brief Helper to move the cursor, grab handle etc.
263 bool ProcessInputEvents();
266 * @brief Helper to check whether any place-holder text is available.
268 bool IsPlaceholderAvailable() const
270 return ( mEventData &&
271 ( !mEventData->mPlaceholderTextInactive.empty() ||
272 !mEventData->mPlaceholderTextActive.empty() )
276 bool IsShowingPlaceholderText() const
278 return ( mEventData && mEventData->mIsShowingPlaceholderText );
282 * @brief Called when placeholder-text is hidden
284 void PlaceholderCleared()
288 mEventData->mIsShowingPlaceholderText = false;
290 // Remove mPlaceholderTextColor
291 mVisualModel->SetTextColor( mTextColor );
295 void ClearPreEditFlag()
299 mEventData->mPreEditFlag = false;
300 mEventData->mPreEditStartPosition = 0;
301 mEventData->mPreEditLength = 0;
305 void ResetImfManager()
307 // Reset incase we are in a pre-edit state.
308 ImfManager imfManager = ImfManager::Get();
311 imfManager.Reset(); // Will trigger a commit message
317 bool IsClipboardEmpty()
319 bool result( mClipboard && mClipboard.NumberOfItems() );
320 return !result; // // If NumberOfItems greater than 0, return false
323 void UpdateModel( OperationsMask operationsRequired );
326 * @brief Retrieve the default fonts.
328 * @param[out] fonts The default font family, style and point sizes.
329 * @param[in] numberOfCharacters The number of characters in the logical model.
331 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
333 void OnCursorKeyEvent( const Event& event );
335 void OnTapEvent( const Event& event );
337 void OnPanEvent( const Event& event );
339 void OnLongPressEvent( const Event& event );
341 void OnHandleEvent( const Event& event );
343 void OnSelectEvent( const Event& event );
345 void OnSelectAllEvent();
347 void RetrieveSelection( std::string& selectedText, bool deleteAfterRetreival );
349 void ShowClipboard();
351 void HideClipboard();
353 bool CopyStringToClipboard( std::string& source );
355 void SendSelectionToClipboard( bool deleteAfterSending );
357 void GetTextFromClipboard( unsigned int itemIndex, std::string& retreivedString );
359 void RepositionSelectionHandles( CharacterIndex selectionStart, CharacterIndex selectionEnd );
360 void RepositionSelectionHandles( float visualX, float visualY );
362 void SetPopupButtons();
364 void ChangeState( EventData::State newState );
365 LineIndex GetClosestLine( float y ) const;
367 void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
370 * @brief Retrieves the cursor's logical position for a given touch point x,y
372 * @param[in] visualX The touch point x.
373 * @param[in] visualY The touch point y.
375 * @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.
377 CharacterIndex GetClosestCursorIndex( float visualX,
381 * @brief Calculates the cursor's position for a given character index in the logical order.
383 * It retrieves as well the line's height and the cursor's height and
384 * if there is a valid alternative cursor, its position and height.
386 * @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.
387 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
389 void GetCursorPosition( CharacterIndex logical,
390 CursorInfo& cursorInfo );
393 * @brief Calculates the new cursor index.
395 * It takes into account that in some scripts multiple characters can form a glyph and all of them
396 * need to be jumped with one key event.
398 * @param[in] index The initial new index.
400 * @return The new cursor index.
402 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
405 * @brief Updates the cursor position.
407 * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
408 * It sets the position of the secondary cursor if it's a valid one.
409 * Sets which cursors are active.
411 void UpdateCursorPosition();
414 * @brief Updates the position of the given selection handle.
416 * @param[in] handleType One of the selection handles.
418 void UpdateSelectionHandle( HandleType handleType );
421 * @biref Clamps the horizontal scrolling to get the control always filled with text.
423 * @param[in] actualSize The size of the laid out text.
425 void ClampHorizontalScroll( const Vector2& actualSize );
428 * @biref Clamps the vertical scrolling to get the control always filled with text.
430 * @param[in] actualSize The size of the laid out text.
432 void ClampVerticalScroll( const Vector2& actualSize );
435 * @brief Scrolls the text to make a position visible.
437 * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
439 * @param[in] position A position in decorator coords.
441 * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
442 * or moving the selection handles.
444 void ScrollToMakePositionVisible( const Vector2& position );
447 * @brief Scrolls the text to make the cursor visible.
449 * This method is called after deleting text.
451 void ScrollTextToMatchCursor();
453 ControlInterface& mControlInterface; ///< Reference to the text controller.
454 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
455 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
456 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
457 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
458 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
459 Clipboard mClipboard; ///< Handle to the system clipboard
460 View mView; ///< The view interface to the rendering back-end.
461 LayoutEngine mLayoutEngine; ///< The layout engine.
462 std::vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
463 Size mControlSize; ///< The size of the control.
464 Vector4 mTextColor; ///< The regular text color
465 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
466 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
467 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
468 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
473 } // namespace Toolkit
477 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__