1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup UI_Components 18 * @{ 19 * 20 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21 * 22 */ 23 24 /** 25 * @file ui_edit_text.h 26 * 27 * @brief Declares a <b>UIEditText</b> class that represents a input edit view. 28 * 29 */ 30 31 #ifndef GRAPHIC_LITE_UI_EDIT_TEXT 32 #define GRAPHIC_LITE_UI_EDIT_TEXT 33 34 #include "animator/animator.h" 35 #include "common/text.h" 36 #include "components/ui_view.h" 37 38 namespace OHOS { 39 /** 40 * @brief Defines the functions for presenting a edit text in a specified area, setting the style and background color, 41 * and setting the display mode such as text and password type. 42 */ 43 class UIEditText : public UIView { 44 public: 45 /** 46 * @brief Defines a value change event listener. You need to register this listener with the view to listen to 47 * value change events. 48 */ 49 class OnChangeListener : public HeapBase { 50 public: 51 /** 52 * @brief Called when edit text value changed. 53 * 54 * @param view Indicates the UIEditView. 55 * @param value Indicates the changed value. 56 */ OnChange(UIView & view,const char * value)57 virtual void OnChange(UIView& view, const char* value) {} 58 59 /** 60 * @brief A destructor used to delete the <b>OnChangeListener</b> instance. 61 */ ~OnChangeListener()62 virtual ~OnChangeListener() {} 63 }; 64 65 /** 66 * @brief A constructor used to create a <b>UIEditText</b> instance. 67 */ 68 UIEditText(); 69 70 /** 71 * @brief A destructor used to delete the <b>UIEditText</b> instance. 72 */ 73 virtual ~UIEditText(); 74 75 /** 76 * @brief Obtains the view type. 77 * 78 * @return Returns <b>UI_EDIT_TEXT</b>, as defined in {@link UIViewType}. 79 */ GetViewType()80 UIViewType GetViewType() const override 81 { 82 return UI_EDIT_TEXT; 83 } 84 85 /** 86 * @brief Executes the press event action 87 * 88 * @param [in] event The press event, contain press position. 89 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 90 */ 91 bool OnPressEvent(const PressEvent& event) override; 92 93 /** 94 * @brief Executes the long press event action 95 * 96 * @param [in] event The long press event, contain press position. 97 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 98 */ 99 bool OnLongPressEvent(const LongPressEvent& event) override; 100 101 /** 102 * @brief Sets the view style. 103 * 104 * @param style Indicates the view style. 105 */ 106 void SetStyle(Style& style) override; 107 108 /** 109 * @brief Sets a style. 110 * 111 * @param key Indicates the key of the style to set. 112 * @param value Indicates the value matching the key. 113 */ 114 void SetStyle(uint8_t key, int64_t value) override; 115 116 /** 117 * @brief Checks whether this view needs to be covered before drawing it. 118 * 119 * @param invalidatedArea Indicates the area to draw. 120 * @return Returns <b>true</b> if this view needs to be covered; returns <b> false</b> otherwise. 121 */ OnPreDraw(Rect & invalidatedArea)122 bool OnPreDraw(Rect& invalidatedArea) const override 123 { 124 return false; 125 } 126 127 /** 128 * @brief Draws this view. 129 * 130 * @param invalidatedArea Indicates the area to draw. 131 */ 132 void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; 133 134 void Focus() override; 135 136 void Blur() override; 137 138 /** 139 * @brief Sets the text content. 140 * 141 * @param text Indicates the pointer to the text content. 142 */ 143 void SetText(const char* text); 144 145 /** 146 * @brief Obtains the input text of this view. 147 * 148 * @return Returns the text. 149 */ 150 const char* GetText(); 151 152 /** 153 * @brief Sets the placeholder content. 154 * 155 * @param placeholder Indicates the pointer to the placeholder content. 156 */ 157 void SetPlaceholder(const char* placeholder); 158 159 /** 160 * @brief Sets the vaule listener for this view. 161 * The listener is triggered to invoke the callback function when the value changed. 162 * 163 * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}. 164 */ 165 const char* GetPlaceholder(); 166 167 /** 168 * @brief Set max length of the input text. 169 * 170 * @param maxLength max length size. 171 */ 172 void SetMaxLength(uint16_t maxLength); 173 174 /** 175 * @brief Get max length of the input text. 176 * 177 * @return return the length size. 178 */ 179 uint16_t GetMaxLength(); 180 181 /** 182 * @brief Set the input type. 183 * 184 * @param type the input type, such as text or password. 185 */ 186 void SetInputType(InputType type); 187 188 /** 189 * @brief Get the input type. 190 * 191 * @param type the input type, such as text or password. 192 */ GetInputType()193 InputType GetInputType() 194 { 195 return inputType_; 196 } 197 198 /** 199 * @brief Sets the color for this text. 200 * 201 * @param color Indicates the text color to set. 202 */ SetTextColor(ColorType color)203 void SetTextColor(ColorType color) 204 { 205 useTextColor_ = true; 206 textColor_ = color; 207 } 208 209 /** 210 * @brief Obtains the color of this text. 211 * 212 * @return Returns the text color. 213 */ GetTextColor()214 ColorType GetTextColor() const 215 { 216 return useTextColor_ ? textColor_ : GetStyleConst().textColor_; 217 } 218 219 /** 220 * @brief Sets the color of the palceholder for this text. 221 * 222 * @param color Indicates the palceholder color to set. 223 */ SetPlaceholderColor(ColorType color)224 void SetPlaceholderColor(ColorType color) 225 { 226 placeholderColor_ = color; 227 } 228 229 /** 230 * @brief Obtains the palceholder color of this text. 231 * 232 * @return Returns the palceholder color. 233 */ GetPlaceholderColor()234 ColorType GetPlaceholderColor() const 235 { 236 return placeholderColor_; 237 } 238 239 /** 240 * @brief Sets the cursor color. 241 * 242 * @param color Indicates the cursor color to set. 243 */ SetCursorColor(ColorType color)244 void SetCursorColor(ColorType color) 245 { 246 cursorColor_ = color; 247 } 248 249 /** 250 * @brief Obtains the cursor color. 251 * 252 * @return Returns the cursor color. 253 */ GetCursorColor()254 ColorType GetCursorColor() const 255 { 256 return cursorColor_; 257 } 258 259 /** 260 * @brief Sets the font ID for this view. 261 * 262 * @param fontId Indicates the font ID composed of font name and size. 263 */ 264 void SetFontId(uint16_t fontId); 265 266 /** 267 * @brief Obtains the font ID composed of font name and size. 268 * 269 * @return Returns the front ID of this view. 270 */ GetFontId()271 uint8_t GetFontId() 272 { 273 InitText(); 274 return inputText_->GetFontId(); 275 } 276 277 /** 278 * @brief Sets the font for this viewview. 279 * 280 * @param name Indicates the pointer to the font name. 281 * @param size Indicates the font size to set. 282 */ 283 void SetFont(const char* name, uint8_t size); 284 285 /** 286 * @brief Obtains the width of this text. 287 * 288 * @return Returns the text width. 289 */ 290 uint16_t GetTextWidth(); 291 292 /** 293 * @brief Obtains the height of this text. 294 * 295 * @return Returns the text height. 296 */ 297 uint16_t GetTextHeight(); 298 299 void ReMeasure() override; 300 301 /** 302 * @brief Insert the text passed from the input method. 303 * 304 * @param text the text input by the user passed form input method. 305 */ 306 virtual void InsertText(std::string text); 307 308 /** 309 * @brief Delete the input text from backward. 310 * 311 * @param length the length of charactor to delete. 312 */ 313 virtual void DeleteBackward(uint32_t length); 314 315 /** 316 * @brief Sets the vaule listener for this view. 317 * The listener is triggered to invoke the callback function when the value changed. 318 * 319 * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}. 320 */ SetOnChangeListener(OnChangeListener * onChangeListener)321 void SetOnChangeListener(OnChangeListener* onChangeListener) 322 { 323 onChangeListener_ = onChangeListener; 324 } 325 326 /** 327 * @brief Obtains the vaule change event listener for the view. 328 * 329 * @return Returns the vaule change event listener. 330 */ GetOnChangeListener()331 OnChangeListener*& GetOnChangeListener() 332 { 333 return onChangeListener_; 334 } 335 336 /** 337 * @brief insert text by using cursor index. 338 */ 339 void InsertTextByCursorIndex(std::string text); 340 341 /** 342 * @brief update the cursor position when change the text type. 343 */ 344 void UpdateCursor(); 345 346 /** 347 * @brief set the cursor index. 348 */ 349 void SetCursorIndex(uint16_t cursorIndex); 350 351 /** 352 * @brief Is it focused or not. 353 */ GetIsFocus()354 bool GetIsFocus() 355 { 356 return isFocused_; 357 } 358 359 uint16_t GetCursorIndex(); 360 361 protected: 362 virtual void InitText(); 363 virtual void UpdateExtraOffsetX(const uint16_t firstVisibleIndex, 364 const uint16_t lastVisibleIndex); 365 virtual uint16_t GetFirstVisibleIndex(); 366 virtual uint16_t GetLastVisibleIndex(); 367 virtual uint16_t GetTextLength(); 368 virtual void UpdateInsertDeletedOffset(); 369 virtual void UpdateOffsetX(); 370 void SetText(std::string text); 371 372 Text* inputText_; 373 Text* placeholderText_; 374 int16_t offsetX_; 375 uint16_t cursorIndex_; 376 uint16_t deleteTextWidth_; 377 uint16_t insertTextWidth_; 378 private: 379 friend class CursorAnimator; 380 381 void RemeasureForMarquee(int16_t textWidth); 382 void UpdateInnerText(); 383 void CheckValueChange(std::string text); 384 void RefreshText(); 385 void UpdateTextString(std::string text); 386 void CalculatedCursorPos(bool drawPlaceholder); 387 void DealPressEvents(bool longPressEvent, const Event &event); 388 std::string GetInnerText(); 389 std::string GetInnerPassword(); 390 void DrawCursor(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, bool drawPlaceholder); 391 392 bool needRefresh_; 393 bool useTextColor_; 394 bool isFocused_; 395 bool drawCursor_; 396 bool isSetTextByInterface_; 397 uint16_t maxLength_; 398 uint16_t placeholderEllipsisIndex_; 399 int16_t cursorPosX_; 400 ColorType textColor_; 401 ColorType placeholderColor_; 402 ColorType cursorColor_; 403 OnChangeListener* onChangeListener_; 404 std::string textStr_; 405 std::string passwordStr_; 406 Animator* cursorAnimator_; 407 InputType inputType_ = InputType::TEXT_TYPE; 408 }; 409 } // namespace OHOS 410 #endif // GRAPHIC_LITE_UI_EDIT_TEXT 411