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>
25 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
26 #include <dali-toolkit/internal/text/logical-model-impl.h>
27 #include <dali-toolkit/internal/text/text-controller.h>
28 #include <dali-toolkit/internal/text/visual-model-impl.h>
41 // Used to queue input events until DoRelayout()
44 KEYBOARD_FOCUS_GAIN_EVENT,
45 KEYBOARD_FOCUS_LOST_EVENT,
59 Event( Type eventType )
78 primaryCursorHeight( 0.f ),
79 secondaryCursorHeight( 0.f ),
80 isSecondaryCursor( false )
86 Vector2 primaryPosition; ///< The primary cursor's position.
87 Vector2 secondaryPosition; ///< The secondary cursor's position.
88 float lineHeight; ///< The height of the line where the cursor is placed.
89 float primaryCursorHeight; ///< The primary cursor's height.
90 float secondaryCursorHeight; ///< The secondary cursor's height.
91 bool isSecondaryCursor; ///< Whether the secondary cursor is valid.
104 EventData( DecoratorPtr decorator );
108 DecoratorPtr mDecorator; ///< Pointer to the decorator
109 std::string mPlaceholderText; ///< The plaxe holder text
112 * This is used to delay handling events until after the model has been updated.
113 * The number of updates to the model is minimized to improve performance.
115 std::vector<Event> mEventQueue; ///< The queue of touch events etc.
118 * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
119 * Typically this will have a negative value with scrolling occurs.
121 Vector2 mScrollPosition; ///< The text is offset by this position when scrolling.
123 State mState; ///< Selection mode, edit mode etc.
125 CharacterIndex mPrimaryCursorPosition; ///< Index into logical model for primary cursor.
126 CharacterIndex mSecondaryCursorPosition; ///< Index into logical model for secondary cursor.
128 bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing.
129 bool mCursorBlinkEnabled : 1; ///< True if cursor should blink when active.
130 bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled.
131 bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown.
132 bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled.
133 bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled.
134 bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled.
135 bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated.
136 bool mScrollAfterUpdateCursorPosition : 1; ///< Whether to scroll after the cursor position is updated.
143 REPLACE_TEXT, ///< Replace the entire text
144 INSERT_TEXT, ///< Insert characters at the current cursor position
145 DELETE_TEXT ///< Delete a character at the current cursor position
155 : mDefaultPointSize(0.0f),
160 FontId GetFontId( TextAbstraction::FontClient& fontClient )
164 Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
165 mFontId = fontClient.GetFontId( mDefaultFontFamily, mDefaultFontStyle, pointSize );
171 std::string mDefaultFontFamily;
172 std::string mDefaultFontStyle;
173 float mDefaultPointSize;
177 struct Controller::Impl
179 Impl( ControlInterface& controlInterface )
180 : mControlInterface( controlInterface ),
183 mFontDefaults( NULL ),
191 mOperationsPending( NO_OPERATION ),
192 mRecalculateNaturalSize( true )
194 mLogicalModel = LogicalModel::New();
195 mVisualModel = VisualModel::New();
197 mFontClient = TextAbstraction::FontClient::Get();
199 mView.SetVisualModel( mVisualModel );
201 // Set the text properties to default
202 mVisualModel->SetTextColor( Color::WHITE );
203 mVisualModel->SetShadowOffset( Vector2::ZERO );
204 mVisualModel->SetShadowColor( Color::BLACK );
205 mVisualModel->SetUnderlineEnabled( false );
206 mVisualModel->SetUnderlineHeight( 0.0f );
215 * @brief Request a relayout using the ControlInterface.
217 void RequestRelayout();
221 * @brief Helper to move the cursor, grab handle etc.
223 bool ProcessInputEvents();
225 void OnKeyboardFocus( bool hasFocus );
227 void OnCursorKeyEvent( const Event& event );
229 void HandleCursorKey( int keyCode );
231 void OnTapEvent( const Event& event );
233 void OnPanEvent( const Event& event );
235 void OnGrabHandleEvent( const Event& event );
237 void RepositionSelectionHandles( float visualX, float visualY );
239 void ChangeState( EventData::State newState );
241 LineIndex GetClosestLine( float y ) const;
244 * @brief Retrieves the cursor's logical position for a given touch point x,y
246 * @param[in] visualX The touch point x.
247 * @param[in] visualY The touch point y.
249 * @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.
251 CharacterIndex GetClosestCursorIndex( float visualX,
255 * @brief Calculates the cursor's position for a given character index in the logical order.
257 * It retrieves as well the line's height and the cursor's height and
258 * if there is a valid alternative cursor, its position and height.
260 * @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.
261 * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
263 void GetCursorPosition( CharacterIndex logical,
264 CursorInfo& cursorInfo );
267 * @brief Calculates the new cursor index.
269 * It takes into account that in some scripts multiple characters can form a glyph and all of them
270 * need to be jumped with one key event.
272 * @param[in] index The initial new index.
274 * @return The new cursor index.
276 CharacterIndex CalculateNewCursorIndex( CharacterIndex index ) const;
279 * @brief Updates the cursor position.
281 * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
282 * It sets the position of the secondary cursor if it's a valid one.
283 * Sets which cursors are active.
285 void UpdateCursorPosition();
288 * @biref Clamps the horizontal scrolling to get the control always filled with text.
290 * @param[in] actualSize The size of the laid out text.
292 void ClampHorizontalScroll( const Vector2& actualSize );
295 * @biref Clamps the vertical scrolling to get the control always filled with text.
297 * @param[in] actualSize The size of the laid out text.
299 void ClampVerticalScroll( const Vector2& actualSize );
302 * @brief Scrolls the text to make the cursor visible.
304 * This method is called after inserting, deleting or moving the cursor with the keypad.
306 void ScrollToMakeCursorVisible();
308 ControlInterface& mControlInterface; ///< Reference to the text controller.
309 LogicalModelPtr mLogicalModel; ///< Pointer to the logical model.
310 VisualModelPtr mVisualModel; ///< Pointer to the visual model.
311 FontDefaults* mFontDefaults; ///< Avoid allocating this when the user does not specify a font.
312 EventData* mEventData; ///< Avoid allocating everything for text input until EnableTextInput().
313 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
314 View mView; ///< The view interface to the rendering back-end.
315 LayoutEngine mLayoutEngine; ///< The layout engine.
316 std::vector<ModifyEvent> mModifyEvents; ///< Temporary stores the text set until the next relayout.
317 Size mControlSize; ///< The size of the control.
318 Vector2 mAlignmentOffset; ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
319 OperationsMask mOperationsPending; ///< Operations pending to be done to layout the text.
320 bool mRecalculateNaturalSize:1; ///< Whether the natural size needs to be recalculated.
325 } // namespace Toolkit
329 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__