1 /* 2 * This file is part of OpenTTD. 3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. 4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. 6 */ 7 8 /** @file querystring_gui.h Base for the GUIs that have an edit box in them. */ 9 10 #ifndef QUERYSTRING_GUI_H 11 #define QUERYSTRING_GUI_H 12 13 #include "textbuf_type.h" 14 #include "textbuf_gui.h" 15 #include "window_gui.h" 16 17 /** 18 * Data stored about a string that can be modified in the GUI 19 */ 20 struct QueryString { 21 /* Special actions when hitting ENTER or ESC. (only keyboard, not OSK) */ 22 static const int ACTION_NOTHING = -1; ///< Nothing. 23 static const int ACTION_DESELECT = -2; ///< Deselect editbox. 24 static const int ACTION_CLEAR = -3; ///< Clear editbox. 25 26 StringID caption; 27 int ok_button; ///< Widget button of parent window to simulate when pressing OK in OSK. 28 int cancel_button; ///< Widget button of parent window to simulate when pressing CANCEL in OSK. 29 Textbuf text; 30 const char *orig; 31 bool handled; 32 33 /** 34 * Initialize string. 35 * @param size Maximum size in bytes. 36 * @param chars Maximum size in chars. 37 */ ok_buttonQueryString38 QueryString(uint16 size, uint16 chars = UINT16_MAX) : ok_button(ACTION_NOTHING), cancel_button(ACTION_DESELECT), text(size, chars), orig(nullptr) 39 { 40 } 41 42 /** 43 * Make sure everything gets freed. 44 */ ~QueryStringQueryString45 ~QueryString() 46 { 47 free(this->orig); 48 } 49 50 public: 51 void DrawEditBox(const Window *w, int wid) const; 52 void ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed); 53 void HandleEditBox(Window *w, int wid); 54 55 Point GetCaretPosition(const Window *w, int wid) const; 56 Rect GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const; 57 const char *GetCharAtPosition(const Window *w, int wid, const Point &pt) const; 58 59 /** 60 * Get the current text. 61 * @return Current text. 62 */ GetTextQueryString63 const char *GetText() const 64 { 65 return this->text.buf; 66 } 67 68 /** 69 * Get the position of the caret in the text buffer. 70 * @return Pointer to the caret in the text buffer. 71 */ GetCaretQueryString72 const char *GetCaret() const 73 { 74 return this->text.buf + this->text.caretpos; 75 } 76 77 /** 78 * Get the currently marked text. 79 * @param[out] length Length of the marked text. 80 * @return Beginning of the marked area or nullptr if no text is marked. 81 */ GetMarkedTextQueryString82 const char *GetMarkedText(size_t *length) const 83 { 84 if (this->text.markend == 0) return nullptr; 85 86 *length = this->text.markend - this->text.markpos; 87 return this->text.buf + this->text.markpos; 88 } 89 }; 90 91 void ShowOnScreenKeyboard(Window *parent, int button); 92 void UpdateOSKOriginalText(const Window *parent, int button); 93 bool IsOSKOpenedFor(const Window *w, int button); 94 95 #endif /* QUERYSTRING_GUI_H */ 96