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/input-style.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 )
86 primaryCursorHeight( 0.f ),
87 secondaryCursorHeight( 0.f ),
88 isSecondaryCursor( false )
94 Vector2 primaryPosition; ///< The primary cursor's position.
95 Vector2 secondaryPosition; ///< The secondary cursor's position.
96 float lineOffset; ///< The vertical offset where the line containing the cursor starts.
97 float lineHeight; ///< The height of the line where the cursor is placed.
98 float primaryCursorHeight; ///< The primary cursor's height.
99 float secondaryCursorHeight; ///< The secondary cursor's height.
100 bool isSecondaryCursor; ///< Whether the secondary cursor is valid.
112 EDITING_WITH_GRAB_HANDLE,
113 EDITING_WITH_PASTE_POPUP,
115 SELECTION_HANDLE_PANNING
118 EventData( DecoratorPtr decorator );
122 static bool IsEditingState( State stateToCheck )
124 return ( stateToCheck == EDITING || stateToCheck == EDITING_WITH_POPUP || stateToCheck == EDITING_WITH_GRAB_HANDLE || stateToCheck == EDITING_WITH_PASTE_POPUP );
127 DecoratorPtr mDecorator; ///< Pointer to the decorator.
128 ImfManager mImfManager; ///< The Input Method Framework Manager.
129 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus.
130 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive.
131 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color.
134 * This is used to delay handling events until after the model has been updated.
135 * The number of updates to the model is minimized to improve performance.
137 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
139 InputStyle mInputStyle; ///< The style to be set to the new inputed text.
142 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
143 * Typically this will have a negative value with scrolling occurs.
145 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
147 State mState; ///< Selection mode, edit mode etc.
149 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
150 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
151 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
153 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
154 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
156 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
157 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
158 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
159 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
160 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
161 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
162 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
163 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
164 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
165 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
166 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
167 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
168 bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated.
169 bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters.
170 bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text.
171 bool mUpdateInputStyle : 1; ///< Whether to update the input style after moving the cursor.
178 TEXT_REPLACED, ///< The entire text was replaced
179 TEXT_INSERTED, ///< Insert characters at the current cursor position
180 TEXT_DELETED ///< Characters were deleted
189 : mFontDescription(),
191 mDefaultPointSize( 0.f ),
193 familyDefined( false ),
194 weightDefined( false ),
195 widthDefined( false ),
196 slantDefined( false ),
199 // Initially use the default platform font
200 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
201 fontClient.GetDefaultPlatformFontDescription( mFontDescription );
204 FontId GetFontId( TextAbstraction::FontClient& fontClient )
208 const PointSize26Dot6 pointSize = static_cast<PointSize26Dot6>( mDefaultPointSize * 64.f );
209 mFontId = fontClient.GetFontId( mFontDescription, pointSize );
215 TextAbstraction::FontDescription mFontDescription; ///< The default font's description.
216 std::string mFontStyle; ///< The font's style string set through the property system.
217 float mDefaultPointSize; ///< The default font's point size.
218 FontId mFontId; ///< The font's id of the default font.
219 bool familyDefined:1; ///< Whether the default font's family name is defined.
220 bool weightDefined:1; ///< Whether the default font's weight is defined.
221 bool widthDefined:1; ///< Whether the default font's width is defined.
222 bool slantDefined:1; ///< Whether the default font's slant is defined.
223 bool sizeDefined:1; ///< Whether the default font's point size is defined.
226 struct Controller::Impl
228 Impl( ControlInterface& controlInterface )
229 : mControlInterface( controlInterface ),
232 mFontDefaults( NULL ),
240 mTextColor( Color::BLACK ),
242 mOperationsPending( NO_OPERATION ),
243 mMaximumNumberOfCharacters( 50u ),
244 mRecalculateNaturalSize( true ),
245 mMarkupProcessorEnabled( false )
247 mLogicalModel = LogicalModel::New();
248 mVisualModel = VisualModel::New();
250 mFontClient = TextAbstraction::FontClient::Get();
251 mClipboard = Clipboard::Get();
253 mView.SetVisualModel( mVisualModel );
255 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
256 mMetrics = Metrics::New( mFontClient );
257 mLayoutEngine.SetMetrics( mMetrics );
259 // Set the text properties to default
260 mVisualModel->SetUnderlineEnabled( false );
261 mVisualModel->SetUnderlineHeight( 0.0f );
266 delete mFontDefaults;
271 * @brief Request a relayout using the ControlInterface.
273 void RequestRelayout();
276 * @brief Request a relayout using the ControlInterface.
278 void QueueModifyEvent( ModifyEvent::Type type )
280 if( ModifyEvent::TEXT_REPLACED == type)
282 // Cancel previously queued inserts etc.
283 mModifyEvents.Clear();
288 mModifyEvents.PushBack( event );
290 // The event will be processed during relayout
295 * @brief Helper to move the cursor, grab handle etc.
297 bool ProcessInputEvents();
300 * @brief Helper to check whether any place-holder text is available.
302 bool IsPlaceholderAvailable() const
304 return ( mEventData &&
305 ( !mEventData->mPlaceholderTextInactive.empty() ||
306 !mEventData->mPlaceholderTextActive.empty() )
310 bool IsShowingPlaceholderText() const
312 return ( mEventData && mEventData->mIsShowingPlaceholderText );
316 * @brief Helper to check whether active place-holder text is available.
318 bool IsFocusedPlaceholderAvailable() const
320 return ( mEventData && !mEventData->mPlaceholderTextActive.empty() );
323 bool IsShowingRealText() const
325 return ( !IsShowingPlaceholderText() &&
326 0u != mLogicalModel->mText.Count() );
330 * @brief Called when placeholder-text is hidden
332 void PlaceholderCleared()
336 mEventData->mIsShowingPlaceholderText = false;
338 // Remove mPlaceholderTextColor
339 mVisualModel->SetTextColor( mTextColor );
343 void ClearPreEditFlag()
347 mEventData->mPreEditFlag = false;
348 mEventData->mPreEditStartPosition = 0;
349 mEventData->mPreEditLength = 0;
353 void ResetImfManager()
357 // Reset incase we are in a pre-edit state.
358 if( mEventData->mImfManager )
360 mEventData->mImfManager.Reset(); // Will trigger a message ( commit, get surrounding )
367 bool IsClipboardEmpty()
369 bool result( mClipboard && mClipboard.NumberOfItems() );
370 return !result; // // If NumberOfItems greater than 0, return false
374 * @brief Updates the logical and visual models.
376 * When text or style changes the model is set with some operations pending.
377 * When i.e. the text's size or a relayout is required this method is called
378 * with a given @p operationsRequired parameter. The operations required are
379 * matched with the operations pending to perform the minimum number of operations.
381 * @param[in] operationsRequired The operations required.
383 void UpdateModel( OperationsMask operationsRequired );
386 * @brief Updates the style runs in the visual model when the text's styles changes.
388 * @param[in] operationsRequired The operations required.
390 * @return @e true if the model has been modified.
392 bool UpdateModelStyle( OperationsMask operationsRequired );
395 * @brief Retreieves the default style.
397 * @param[out] inputStyle The default style.
399 void RetrieveDefaultInputStyle( InputStyle& inputStyle );
402 * @brief Retrieve the line height of the default font.
404 float GetDefaultFontLineHeight();
406 void OnCursorKeyEvent( const Event& event );
408 void OnTapEvent( const Event& event );
410 void OnPanEvent( const Event& event );
412 void OnLongPressEvent( const Event& event );
414 void OnHandleEvent( const Event& event );
416 void OnSelectEvent( const Event& event );
418 void OnSelectAllEvent();
420 void RetrieveSelection( std::string& selectedText, bool deleteAfterRetrieval );
422 void ShowClipboard();
424 void HideClipboard();
426 bool CopyStringToClipboard( std::string& source );
428 void SendSelectionToClipboard( bool deleteAfterSending );
430 void GetTextFromClipboard( unsigned int itemIndex, std::string& retrievedString );
432 void RepositionSelectionHandles();
433 void RepositionSelectionHandles( float visualX, float visualY );
435 void SetPopupButtons();
437 void ChangeState( EventData::State newState );
438 LineIndex GetClosestLine( float y ) const;
440 void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
443 * @brief Retrieves the cursor's logical position for a given touch point x,y
445 * @param[in] visualX The touch point x.
446 * @param[in] visualY The touch point y.
448 * @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.
450 CharacterIndex GetClosestCursorIndex( float visualX,
454 * @brief Calculates the cursor's position for a given character index in the logical order.
456 * It retrieves as well the line's height and the cursor's height and
457 * if there is a valid alternative cursor, its position and height.
459 * @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.
460 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
462 void GetCursorPosition( CharacterIndex logical,
463 CursorInfo& cursorInfo );
466 * @brief Calculates the new cursor index.
468 * It takes into account that in some scripts multiple characters can form a glyph and all of them
469 * need to be jumped with one key event.
471 * @param[in] index The initial new index.
473 * @return The new cursor index.
475 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
478 * @brief Updates the cursor position.
480 * Sets the cursor's position into the decorator. It transforms the cursor's position into decorator's coords.
481 * It sets the position of the secondary cursor if it's a valid one.
482 * Sets which cursors are active.
484 * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
487 void UpdateCursorPosition( const CursorInfo& cursorInfo );
490 * @brief Updates the position of the given selection handle. It transforms the handle's position into decorator's coords.
492 * @param[in] handleType One of the selection handles.
493 * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
495 void UpdateSelectionHandle( HandleType handleType,
496 const CursorInfo& cursorInfo );
499 * @biref Clamps the horizontal scrolling to get the control always filled with text.
501 * @param[in] actualSize The size of the laid out text.
503 void ClampHorizontalScroll( const Vector2& actualSize );
506 * @biref Clamps the vertical scrolling to get the control always filled with text.
508 * @param[in] actualSize The size of the laid out text.
510 void ClampVerticalScroll( const Vector2& actualSize );
513 * @brief Scrolls the text to make a position visible.
515 * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
517 * @param[in] position A position in text coords.
519 * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
520 * or moving the selection handles.
522 void ScrollToMakePositionVisible( const Vector2& position );
525 * @brief Scrolls the text to make the cursor visible.
527 * This method is called after deleting text.
529 void ScrollTextToMatchCursor( const CursorInfo& cursorInfo );
531 ControlInterface& mControlInterface; ///< Reference to the text controller.
532 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
533 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
534 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
535 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
536 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
537 Clipboard mClipboard; ///< Handle to the system clipboard
538 View mView; ///< The view interface to the rendering back-end.
539 MetricsPtr mMetrics; ///< A wrapper around FontClient used to get metrics & potentially down-scaled Emoji metrics.
540 LayoutEngine mLayoutEngine; ///< The layout engine.
541 Vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
542 Vector4 mTextColor; ///< The regular text color
543 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
544 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
545 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
547 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
548 bool mMarkupProcessorEnabled:1; ///< Whether the mark-up procesor is enabled.
553 } // namespace Toolkit
557 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__