1 /*
2 * field.h
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * DIN Is Noise is released under GNU Public License 2.0
5 * For more information, please visit https://dinisnoise.org/
6 */
7 
8 
9 #ifndef __field
10 #define __field
11 
12 #include "widget.h"
13 
14 #include <string>
15 #include <list>
16 
17 struct typing_listener;
18 
19 struct field : widget { // a text field
20 
21 	enum {dec3, dec4, dec6, inf};
22 	static const char* fmts [];
23 	int fmt;
24 
25   enum {pushback, insert, unknown};
26   int mode;
27 
28   std::string text; // text of the field
29   int len; // length of text
30   int last; // last char
31 
32   int cursor; // cursor pos ie char position
33   int offset; // offset from left
34 
35   // focus
36   //
37   int focus; // 1 - field has focus, 0 - lost focus
38   int lmb_clicked;
39 
40   int edited; // edited?
41 
42   // listeners
43   change_listener<field>* change_lsnr;
44 	typing_listener* typing_lsnr;
45 	void call_listener ();
46 
47 	int expr; // evaluate contents as TCL math expression?
48 	std::string type; // data type of expression
49 
50   field ();
51   field (int x, int y, const std::string& str = "");
52 	void init ();
53 
54   void set_text (const std::string& txt, int _edited = 0);
55   void set_text (int i, int _edited = 0);
56   void set_text (float f, int _edited = 0);
get_textfield57   const std::string& get_text () {return text;}
58 
59   int handle_input ();
60   int hittest (int x, int y);
has_focusfield61   int has_focus () {return focus;}
62 
63   void calc_cursor ();
64   void update ();
65   void draw ();
66   void draw_cursor (int x, int y);
67 
68   operator int() const;
69   operator float() const;
70 	operator double() const;
71 	operator short() const;
72 
73   field& operator= (int i) {
74     set_text (i);
75     return *this;
76   }
77 
78   field& operator= (float f) {
79     set_text (f);
80     return *this;
81   };
82 
83 };
84 
85 struct typing_listener {
86 	virtual void typing (field& f) = 0;
87 };
88 
89 
90 #define DECL_FIELD_LISTENER(name) struct name : change_listener<field> { void changed (field& f); };
91 #define MAKE_FIELD_LISTENER(name,var)\
92 	struct name : change_listener<field> { \
93 		void changed (field& f); \
94 	} var;
95 #define VALUE_CHANGED(scope,name) void scope::name::changed (field& f)
96 
97 #endif
98