1 /*
2 * widget.h
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * For more information, please visit http://dinisnoise.org/
5 */
6 
7 
8 #ifndef __widget
9 #define __widget
10 
11 #include "box.h"
12 #include "color.h"
13 #include "point.h"
14 #include "viewwin.h"
15 
16 #include <vector>
17 #include <list>
18 #include <map>
19 #include <string>
20 #include <fstream>
21 
22 #define LISTEN(w,v) w.set_listener(v);
23 #define MOVE(w) w.set_moveable(1);
24 #define HANDLEINPUT(h) if (h.handle_input()) return 1;
25 
26 #define MILLION 1000000
27 #define MILLIONF float(MILLION)
28 
29 extern int lmb;
30 extern int mousex, mouseyy;
31 
32 struct ui;
33 struct widget;
34 
35 struct voiddata {
36   void* data;
37 };
38 
39 struct move_listener : voiddata {
40 	virtual void moved () = 0;
41 };
42 
43 struct nullt {
44   int null;
nulltnullt45   nullt () : null(0) {}
46 };
47 
48 #define MAKE_MOVE_LISTENER(name,var) struct name : move_listener { void moved (); } ; name var;
49 #define MOVED(scope,name) void scope::name::moved ()
50 
51 struct mover {
52   widget* w; // target widget
53   int* pmb; // mouse button that moves widget
54   int mb_clicked; // is mouse button clicked
55   int move; // 1 - widget is moving, 0 - not
56   int prevx, prevy; // previous mouse position
57   mover (widget* ww, int* pb = &lmb);
58   int handle_input ();
59 };
60 
61 typedef std::vector<widget *>::size_type size_vw;
62 
63 struct widget { // ui widget
64 
65 	int id;
66 
67   std::string name;
68 	void set_name (const std::string& _name);
69 	void nametothis ();
set_textwidget70 	virtual void set_text (const std::string& txt) {set_name (txt);}
71 
72 	// bounding box
73 	//
74   box<int> extents; // extents of bounding box around this widget
75 	void set_extents (int l, int b, int r, int t);
set_extentswidget76 	void set_extents (const box<int>& ext) { extents = ext;}
77 	int size; // for making square widgets
set_sizewidget78 	void set_size (int _sz) {
79 		size = _sz;
80 		set_extents (posx, posy, posx + size, posy + size);
81 	}
set_width_heightwidget82 	void set_width_height (int w, int h) {
83 		set_extents (posx, posy, posx + w, posy + h);
84 	}
85 
86 	static int bb[]; // bounding box data for OpenGL drawing
87 	void draw_bbox ();
88 	void fill_bbox (float a = 0.1f, int g = 1);
89 	void draw_and_fill_bbox ();
90 
91   // color of all widgets (except checkbuttons)
92   static float R, G, B;
93   color clr;
94 	virtual void set_color (unsigned char ur, unsigned char ug, unsigned char ub);
95 	virtual void set_color (float r, float g, float b);
set_colorwidget96 	void set_color (const color& c) {set_color (c.r, c.g, c.b);}
97 
98   int posx, posy; // position in screen space
99 	virtual void set_pos (int x, int y);
100 
101 	// helper to handle move with mouse
102 	int moveable; // moveable?
103 	mover movr; // handles move
104 	move_listener* movlis;
105 	virtual void set_moveable (int m, int mc = 0, int* pmb = &lmb);
106 	void move (int dx, int dy, int mc = 1);
107   void setpos2 (widget& w);
108 
109   int visible; // visible?
110 
111   int enabled; // enabled?
112 	void enable ();
113 	void disable ();
114 
115 	static int HOVER; // hover over any widget in DIN?
116 
117   int hover; // hovering?
118 
119   std::vector<widget*> children; // widget's children
120 	void add_child (widget *w);
121 	void remove_child (widget* w);
122   void swapchild (widget* w1, widget* w2);
123 
124 #ifdef __WIDGET_MOVE__
125   int dg;
126 	void draw_guides ();
127 #endif
128 
129 	static widget *focus, *next_focus;
130 
131 	enum {all, only_children};
132 	void show ();
133 	void hide (int what = all);
134 
135   widget (int l = 0, int b = 0, int r = 0, int t = 0);
136   virtual ~widget ();
137 
138   virtual int handle_input ();
drawwidget139   virtual inline void draw () {
140 	#ifdef __WIDGET_MOVE__
141 		if (movr.move || dg) draw_guides ();
142 	#endif
143 		glColor3f (clr.r, clr.g, clr.b);
144 	}
145 
updatewidget146   virtual void update () {}
147 
148   static void advance_right (int& x, widget& w, int spc = 10) {
149     const box<int>& e = w.extents;
150     x += (e.width + spc);
151   }
152 
153   virtual void load (std::ifstream& file);
154   virtual void save (std::ofstream& file);
155 
156 };
157 
158 // globals
159 //
160 
161 void widget_load (const std::string& name, widget** pw, int n);
162 void widget_load (const std::string& fname, std::vector<widget*>& vec);
163 void widget_save (const std::string& name, widget** pw, int n);
164 void widget_save (const std::string& fname, std::vector<widget*>& vec);
165 
166 // listeners for widgets
167 template <typename W> struct change_listener {
168   virtual void changed (W& w) = 0;
169 };
170 
171 // for tying lmb to a widget
172 struct is_lmb_t {
173 	widget* tie;
174 	is_lmb_t ();
175 	int operator() (widget* w = 0);
176 	void clear (widget* _tie = 0);
177 };
178 
179 extern is_lmb_t is_lmb;
180 
181 void set_focus (widget* w);
182 void defocus ();
183 void defocus (widget* w);
184 
185 void makehier (widget** wa);
186 void makehier (widget** wa, int n);
187 void makefam (widget* parent, widget** children, int n);
188 void makefam (widget** mem, int n);
189 
190 /*struct incdec {
191   virtual void decrease () = 0;
192   virtual void increase () = 0;
193 };
194 
195 #include "alarm.h"
196 struct autoincdec : alarm_t {
197   incdec* pid;
198   enum {DEC=-1, INC=1, ANY=2};
199   int what;
200   autoincdec () {
201     what = ANY;
202     pid = 0;
203   }
204   void eval ();
205 };*/
206 
207 #endif
208