1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #ifndef ui_graphical_menu_widgets_lineeditH
21 #define ui_graphical_menu_widgets_lineeditH
22 
23 #include <string>
24 #include <memory>
25 #include <chrono>
26 
27 #include "maxrconfig.h"
28 #include "ui/graphical/menu/widgets/clickablewidget.h"
29 #include "ui/graphical/menu/widgets/tools/validatorstate.h"
30 #include "unifonts.h"
31 #include "utility/autosurface.h"
32 #include "utility/signal/signal.h"
33 
34 class cValidator;
35 
36 enum class eLineEditFrameType
37 {
38 	None,
39 	Box
40 };
41 
42 class cLineEdit : public cClickableWidget
43 {
44 public:
45 	cLineEdit (const cBox<cPosition>& area, eLineEditFrameType frameType = eLineEditFrameType::None, eUnicodeFontType fontType = FONT_LATIN_NORMAL);
46 	~cLineEdit();
47 
48 	const std::string& getText();
49 	void setText (std::string text);
50 
51 	void setReadOnly (bool readOnly);
52 	void setValidator (std::unique_ptr<cValidator> validator);
53 
54 	void finishEditing();
55 
56 	virtual void draw (SDL_Surface& destination, const cBox<cPosition>& clipRect) MAXR_OVERRIDE_FUNCTION;
57 
58 	virtual bool handleGetKeyFocus (cApplication& application) MAXR_OVERRIDE_FUNCTION;
59 	virtual void handleLooseKeyFocus (cApplication& application) MAXR_OVERRIDE_FUNCTION;
60 
61 	virtual bool handleKeyPressed (cApplication& application, cKeyboard& keyboard, SDL_Keycode key) MAXR_OVERRIDE_FUNCTION;
62 	virtual void handleTextEntered (cApplication& application, cKeyboard& keyboard, const char* text) MAXR_OVERRIDE_FUNCTION;
63 
64 	cSignal<void ()> textSet;
65 	cSignal<void ()> escapePressed;
66 	cSignal<void ()> returnPressed;
67 	cSignal<void (eValidatorState)> editingFinished;
68 protected:
69 
70 	virtual bool handleClicked (cApplication& application, cMouse& mouse, eMouseButtonType button) MAXR_OVERRIDE_FUNCTION;
71 private:
72 	const std::chrono::milliseconds cursorVisibleTime;
73 	const std::chrono::milliseconds cursorInvisibleTime;
74 
75 	AutoSurface surface;
76 
77 	std::string text;
78 	eUnicodeFontType fontType;
79 
80 	eLineEditFrameType frameType;
81 
82 	int cursorPos;
83 	int startOffset, endOffset;
84 
85 	bool readOnly;
86 	std::unique_ptr<cValidator> validator;
87 
88 	bool hasKeyFocus;
89 
90 	bool showCursor;
91 	std::chrono::steady_clock::time_point lastCursorBlinkTime;
92 
93 	void createBackground();
94 
95 	cPosition getTextDrawOffset() const;
96 	int getBorderSize() const;
97 
98 	void resetTextPosition();
99 	void doPosIncrease (int& value, int pos);
100 	void doPosDecrease (int& pos);
101 	void scrollLeft (bool changeCursor = true);
102 	void scrollRight();
103 	void deleteLeft();
104 	void deleteRight();
105 
106 	void finishEditingInternal();
107 };
108 
109 #endif // ui_graphical_menu_widgets_lineeditH
110