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/public-api/text-abstraction/font-client.h>
23 #include <dali/public-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()
45 KEYBOARD_FOCUS_GAIN_EVENT,
46 KEYBOARD_FOCUS_LOST_EVENT,
51 LEFT_SELECTION_HANDLE_EVENT,
52 RIGHT_SELECTION_HANDLE_EVENT
62 Event( Type eventType )
82 primaryCursorHeight( 0.f ),
83 secondaryCursorHeight( 0.f ),
84 isSecondaryCursor( false )
90 Vector2 primaryPosition; ///< The primary cursor's position.
91 Vector2 secondaryPosition; ///< The secondary cursor's position.
92 float lineHeight; ///< The height of the line where the cursor is placed.
93 float primaryCursorHeight; ///< The primary cursor's height.
94 float secondaryCursorHeight; ///< The secondary cursor's height.
95 bool isSecondaryCursor; ///< Whether the secondary cursor is valid.
108 EventData( DecoratorPtr decorator );
112 DecoratorPtr mDecorator; ///< Pointer to the decorator
113 std::string mPlaceholderTextActive; ///< The text to display when the TextField is empty with key-input focus
114 std::string mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive
115 Vector4 mPlaceholderTextColor; ///< The in/active placeholder text color
118 * This is used to delay handling events until after the model has been updated.
119 * The number of updates to the model is minimized to improve performance.
121 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
124 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
125 * Typically this will have a negative value with scrolling occurs.
127 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
129 State mState; ///< Selection mode, edit mode etc.
131 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
132 CharacterIndex mLeftSelectionPosition; ///< Index into logical model for left selection handle.
133 CharacterIndex mRightSelectionPosition; ///< Index into logical model for right selection handle.
135 CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary.
136 Length mPreEditLength; ///< Used to remove the pre-edit text if necessary.
138 bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed.
139 bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state.
140 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
141 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
142 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
143 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
144 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
145 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
146 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
147 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
148 bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated.
149 bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated.
150 bool mScrollAfterUpdateCursorPosition : 1; ///< Whether to scroll after the cursor position is updated.
157 PLACEHOLDER_TEXT, ///< Show the placeholder text if necessary
158 TEXT_REPLACED, ///< The entire text was replaced
159 TEXT_INSERTED, ///< Insert characters at the current cursor position
160 TEXT_DELETED ///< Characters were deleted
169 : mDefaultPointSize(0.0f),
174 FontId GetFontId( TextAbstraction::FontClient& fontClient )
178 Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
179 mFontId = fontClient.GetFontId( mDefaultFontFamily, mDefaultFontStyle, pointSize );
185 std::string mDefaultFontFamily;
186 std::string mDefaultFontStyle;
187 float mDefaultPointSize;
191 struct Controller::Impl
193 Impl( ControlInterface& controlInterface )
194 : mControlInterface( controlInterface ),
197 mFontDefaults( NULL ),
204 mTextColor( Color::BLACK ),
206 mOperationsPending( NO_OPERATION ),
207 mMaximumNumberOfCharacters( 50 ),
208 mRecalculateNaturalSize( true )
210 mLogicalModel = LogicalModel::New();
211 mVisualModel = VisualModel::New();
213 mFontClient = TextAbstraction::FontClient::Get();
215 mView.SetVisualModel( mVisualModel );
217 // Set the text properties to default
218 mVisualModel->SetUnderlineEnabled( false );
219 mVisualModel->SetUnderlineHeight( 0.0f );
228 * @brief Request a relayout using the ControlInterface.
230 void RequestRelayout();
233 * @brief Request a relayout using the ControlInterface.
235 void QueueModifyEvent( ModifyEvent::Type type )
239 mModifyEvents.push_back( event );
241 // The event will be processed during relayout
246 * @brief Helper to move the cursor, grab handle etc.
248 bool ProcessInputEvents();
251 * @brief Helper to check whether any place-holder text is available.
253 bool IsPlaceholderAvailable() const
255 return ( mEventData &&
256 ( !mEventData->mPlaceholderTextInactive.empty() ||
257 !mEventData->mPlaceholderTextActive.empty() )
261 bool IsShowingPlaceholderText() const
263 return ( mEventData && mEventData->mIsShowingPlaceholderText );
266 void ShowPlaceholderText()
268 if( IsPlaceholderAvailable() )
270 mEventData->mIsShowingPlaceholderText = true;
272 // Placeholder-text is dependent on focus state i.e. replace after event processing
273 QueueModifyEvent( ModifyEvent::PLACEHOLDER_TEXT );
278 * @brief Called when placeholder-text is hidden
280 void PlaceholderCleared()
284 mEventData->mIsShowingPlaceholderText = false;
286 // Remove mPlaceholderTextColor
287 mVisualModel->SetTextColor( mTextColor );
293 // Reset incase we are in a pre-edit state.
294 ImfManager imfManager = ImfManager::Get();
297 imfManager.Reset(); // Will trigger a commit message
302 * @brief Called when placeholder-text is shown
304 void ReplaceTextWithPlaceholder();
306 void UpdateModel( OperationsMask operationsRequired );
309 * @brief Retrieve the default fonts.
311 * @param[out] fonts The default font family, style and point sizes.
312 * @param[in] numberOfCharacters The number of characters in the logical model.
314 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
316 void OnKeyboardFocus( bool hasFocus );
318 void OnCursorKeyEvent( const Event& event );
320 void OnTapEvent( const Event& event );
322 void OnPanEvent( const Event& event );
324 void OnHandleEvent( const Event& event );
326 void RepositionSelectionHandles( float visualX, float visualY );
328 void ChangeState( EventData::State newState );
330 LineIndex GetClosestLine( float y ) const;
333 * @brief Retrieves the cursor's logical position for a given touch point x,y
335 * @param[in] visualX The touch point x.
336 * @param[in] visualY The touch point y.
338 * @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.
340 CharacterIndex GetClosestCursorIndex( float visualX,
344 * @brief Calculates the cursor's position for a given character index in the logical order.
346 * It retrieves as well the line's height and the cursor's height and
347 * if there is a valid alternative cursor, its position and height.
349 * @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.
350 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
352 void GetCursorPosition( CharacterIndex logical,
353 CursorInfo& cursorInfo );
356 * @brief Calculates the new cursor index.
358 * It takes into account that in some scripts multiple characters can form a glyph and all of them
359 * need to be jumped with one key event.
361 * @param[in] index The initial new index.
363 * @return The new cursor index.
365 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
368 * @brief Updates the cursor position.
370 * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
371 * It sets the position of the secondary cursor if it's a valid one.
372 * Sets which cursors are active.
374 void UpdateCursorPosition();
377 * @brief Updates the position of the given selection handle.
379 * @param[in] handleType One of the selection handles.
381 void UpdateSelectionHandle( HandleType handleType );
384 * @biref Clamps the horizontal scrolling to get the control always filled with text.
386 * @param[in] actualSize The size of the laid out text.
388 void ClampHorizontalScroll( const Vector2& actualSize );
391 * @biref Clamps the vertical scrolling to get the control always filled with text.
393 * @param[in] actualSize The size of the laid out text.
395 void ClampVerticalScroll( const Vector2& actualSize );
398 * @brief Scrolls the text to make the cursor visible.
400 * This method is called after inserting, deleting or moving the cursor with the keypad.
402 void ScrollToMakeCursorVisible();
404 ControlInterface& mControlInterface; ///< Reference to the text controller.
405 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
406 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
407 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
408 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
409 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
410 View mView; ///< The view interface to the rendering back-end.
411 LayoutEngine mLayoutEngine; ///< The layout engine.
412 std::vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
413 Size mControlSize; ///< The size of the control.
414 Vector4 mTextColor; ///< The regular text color
415 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
416 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
417 Length mMaximumNumberOfCharacters; ///< Maximum number of characters that can be inserted.
418 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
423 } // namespace Toolkit
427 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__