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/text-abstraction/font-client.h>
23 #include <dali/devel-api/adaptor-framework/imf-manager.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/visual-model-impl.h>
42 // Used to queue input events until DoRelayout()
49 LEFT_SELECTION_HANDLE_EVENT,
50 RIGHT_SELECTION_HANDLE_EVENT
60 Event( Type eventType )
80 primaryCursorHeight( 0.f ),
81 secondaryCursorHeight( 0.f ),
82 isSecondaryCursor( false )
88 Vector2 primaryPosition; ///< The primary cursor's position.
89 Vector2 secondaryPosition; ///< The secondary cursor's position.
90 float lineHeight; ///< The height of the line where the cursor is placed.
91 float primaryCursorHeight; ///< The primary cursor's height.
92 float secondaryCursorHeight; ///< The secondary cursor's height.
93 bool isSecondaryCursor; ///< Whether the secondary cursor is valid.
106 EventData( DecoratorPtr decorator );
110 DecoratorPtr mDecorator; ///< Pointer to the decorator
111 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus
112 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive
113 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color
116 * This is used to delay handling events until after the model has been updated.
117 * The number of updates to the model is minimized to improve performance.
119 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
122 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
123 * Typically this will have a negative value with scrolling occurs.
125 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
127 State mState; ///< Selection mode, edit mode etc.
129 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
130 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
131 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
133 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
134 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
136 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
137 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
138 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
139 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
140 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
141 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
142 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
143 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
144 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
145 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
146 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
147 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
148 bool mScrollAfterUpdateCursorPosition : 1; ///< Whether to scroll after the cursor position is updated.
155 TEXT_REPLACED, ///< The entire text was replaced
156 TEXT_INSERTED, ///< Insert characters at the current cursor position
157 TEXT_DELETED ///< Characters were deleted
166 : mDefaultPointSize(0.0f),
171 FontId GetFontId( TextAbstraction::FontClient& fontClient )
175 Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
176 mFontId = fontClient.GetFontId( mDefaultFontFamily, mDefaultFontStyle, pointSize );
182 std::string mDefaultFontFamily;
183 std::string mDefaultFontStyle;
184 float mDefaultPointSize;
188 struct Controller::Impl
190 Impl( ControlInterface& controlInterface )
191 : mControlInterface( controlInterface ),
194 mFontDefaults( NULL ),
201 mTextColor( Color::BLACK ),
203 mOperationsPending( NO_OPERATION ),
204 mMaximumNumberOfCharacters( 50 ),
205 mRecalculateNaturalSize( true )
207 mLogicalModel = LogicalModel::New();
208 mVisualModel = VisualModel::New();
210 mFontClient = TextAbstraction::FontClient::Get();
212 mView.SetVisualModel( mVisualModel );
214 // Set the text properties to default
215 mVisualModel->SetUnderlineEnabled( false );
216 mVisualModel->SetUnderlineHeight( 0.0f );
225 * @brief Request a relayout using the ControlInterface.
227 void RequestRelayout();
230 * @brief Request a relayout using the ControlInterface.
232 void QueueModifyEvent( ModifyEvent::Type type )
236 mModifyEvents.push_back( event );
238 // The event will be processed during relayout
243 * @brief Helper to move the cursor, grab handle etc.
245 bool ProcessInputEvents();
248 * @brief Helper to check whether any place-holder text is available.
250 bool IsPlaceholderAvailable() const
252 return ( mEventData &&
253 ( !mEventData->mPlaceholderTextInactive.empty() ||
254 !mEventData->mPlaceholderTextActive.empty() )
258 bool IsShowingPlaceholderText() const
260 return ( mEventData && mEventData->mIsShowingPlaceholderText );
264 * @brief Called when placeholder-text is hidden
266 void PlaceholderCleared()
270 mEventData->mIsShowingPlaceholderText = false;
272 // Remove mPlaceholderTextColor
273 mVisualModel->SetTextColor( mTextColor );
277 void ClearPreEditFlag()
281 mEventData->mPreEditFlag = false;
282 mEventData->mPreEditStartPosition = 0;
283 mEventData->mPreEditLength = 0;
287 void ResetImfManager()
289 // Reset incase we are in a pre-edit state.
290 ImfManager imfManager = ImfManager::Get();
293 imfManager.Reset(); // Will trigger a commit message
299 void UpdateModel( OperationsMask operationsRequired );
302 * @brief Retrieve the default fonts.
304 * @param[out] fonts The default font family, style and point sizes.
305 * @param[in] numberOfCharacters The number of characters in the logical model.
307 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
309 void OnCursorKeyEvent( const Event& event );
311 void OnTapEvent( const Event& event );
313 void OnPanEvent( const Event& event );
315 void OnHandleEvent( const Event& event );
317 void RepositionSelectionHandles( float visualX, float visualY );
319 void ChangeState( EventData::State newState );
321 LineIndex GetClosestLine( float y ) const;
324 * @brief Retrieves the cursor's logical position for a given touch point x,y
326 * @param[in] visualX The touch point x.
327 * @param[in] visualY The touch point y.
329 * @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.
331 CharacterIndex GetClosestCursorIndex( float visualX,
335 * @brief Calculates the cursor's position for a given character index in the logical order.
337 * It retrieves as well the line's height and the cursor's height and
338 * if there is a valid alternative cursor, its position and height.
340 * @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.
341 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
343 void GetCursorPosition( CharacterIndex logical,
344 CursorInfo& cursorInfo );
347 * @brief Calculates the new cursor index.
349 * It takes into account that in some scripts multiple characters can form a glyph and all of them
350 * need to be jumped with one key event.
352 * @param[in] index The initial new index.
354 * @return The new cursor index.
356 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
359 * @brief Updates the cursor position.
361 * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
362 * It sets the position of the secondary cursor if it's a valid one.
363 * Sets which cursors are active.
365 void UpdateCursorPosition();
368 * @brief Updates the position of the given selection handle.
370 * @param[in] handleType One of the selection handles.
372 void UpdateSelectionHandle( HandleType handleType );
375 * @biref Clamps the horizontal scrolling to get the control always filled with text.
377 * @param[in] actualSize The size of the laid out text.
379 void ClampHorizontalScroll( const Vector2& actualSize );
382 * @biref Clamps the vertical scrolling to get the control always filled with text.
384 * @param[in] actualSize The size of the laid out text.
386 void ClampVerticalScroll( const Vector2& actualSize );
389 * @brief Scrolls the text to make the cursor visible.
391 * This method is called after inserting, deleting or moving the cursor with the keypad.
393 void ScrollToMakeCursorVisible();
395 ControlInterface& mControlInterface; ///< Reference to the text controller.
396 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
397 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
398 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
399 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
400 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
401 View mView; ///< The view interface to the rendering back-end.
402 LayoutEngine mLayoutEngine; ///< The layout engine.
403 std::vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
404 Size mControlSize; ///< The size of the control.
405 Vector4 mTextColor; ///< The regular text color
406 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
407 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
408 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
409 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
414 } // namespace Toolkit
418 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__