1 //
2 // pwrmeter.cxx
3 //
4 // PWRmeter bar widget routines.
5 //
6 // A part of the fldigi.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 // ----------------------------------------------------------------------------
21 
22 #include <config.h>
23 #include <cmath>
24 
25 #include <FL/Fl.H>
26 #include <FL/fl_draw.H>
27 
28 #include "pwrmeter.h"
29 
30 //
31 // pwrmeter is a pwrmeter bar widget based off Fl_Widget that shows a
32 // standard pwrmeter bar in horizontal format
33 
draw()34 void PWRmeter::draw()
35 {
36 	if (select_ == 4) select_auto();
37 
38 	sval = round(meter_width * (value_) / (maximum_));
39 	if (sval > meter_width) sval = meter_width;
40 	if (sval < 0) sval = 0;
41 
42 // Draw the box and label...
43 	draw_box();
44 	draw_box(box(), tx, ty, tw, th, bgnd_);
45 	if (sval > 0)
46 		draw_box(FL_FLAT_BOX,
47 			tx + sx, ty + 2,
48 			sval,
49 			th - 4,
50 			fgnd_);
51 	labelcolor(scale_color);
52 	draw_label();
53 }
54 
select_25W()55 void PWRmeter::select_25W()
56 {
57 	maximum_ = 25;
58 	label(W25_face);
59 	fl_font(FL_HELVETICA, labelsize());
60 	meter_width = fl_width(W25_face);
61 	sx = (tw - meter_width) / 2 + fl_width("|") / 2;
62 	meter_width -= fl_width("|");
63 }
64 
select_50W()65 void PWRmeter::select_50W()
66 {
67 	maximum_ = 50;
68 	label(W50_face);
69 	fl_font(FL_HELVETICA, labelsize());
70 	meter_width = fl_width(W50_face);
71 	sx = (tw - meter_width) / 2 + fl_width("|") / 2;
72 	meter_width -= fl_width("|");
73 }
74 
select_100W()75 void PWRmeter::select_100W()
76 {
77 	maximum_ = 100;
78 	label(W100_face);
79 	fl_font(FL_HELVETICA, labelsize());
80 	meter_width = fl_width(W100_face);
81 	sx = (tw - meter_width) / 2 + fl_width("|") / 2;
82 	meter_width -= fl_width("|");
83 }
84 
select_200W()85 void PWRmeter::select_200W()
86 {
87 	maximum_ = 200;
88 	label(W200_face);
89 	fl_font(FL_HELVETICA, labelsize());
90 	meter_width = fl_width(W200_face);
91 	sx = (tw - meter_width) / 2 + fl_width("|") / 2;
92 	meter_width -= fl_width("|");
93 }
94 
select_auto()95 void PWRmeter::select_auto()
96 {
97 	if (value_ <= 25.0) {
98 		select_25W();
99 	} else if (value_ <= 50.0) {
100 		select_50W();
101 	} else if (value_ <= 100) {
102 		select_100W();
103 	} else {
104 		select_200W();
105 	}
106 	redraw();
107 }
108 
select(int sel)109 void PWRmeter::select( int sel ) {
110 	switch (sel) {
111 		case P25:
112 			select_25W();
113 			select_ = 0;
114 			break;
115 		case P50:
116 			select_50W();
117 			select_ = 1;
118 			break;
119 		case P100:
120 			select_100W();
121 			select_ = 2;
122 			break;
123 		case P200:
124 			select_200W();
125 			select_ = 3;
126 			break;
127 		case AUTO:
128 		default:
129 			select_auto();
130 			select_ = 4;
131 	}
132 	redraw();
133 }
134 
135 const char * PWRmeter::W25_face   = "| : : : : | : : : : | : : : : | : : : : | : : : 25|";
136 const char * PWRmeter::W50_face   = "|    :    |    :    |    :    |    :    |    :  50|";
137 const char * PWRmeter::W100_face  = "|    |    |    |    |    |    |    |    |    | 100|";
138 const char * PWRmeter::W200_face  = "|     :     |     :      |      :     |     :  200|";
139 
PWRmeter(int X,int Y,int W,int H,const char * l)140 PWRmeter::PWRmeter(int X, int Y, int W, int H, const char* l)
141 : Fl_Widget(X, Y, W, H, "")
142 {
143 	align(FL_ALIGN_INSIDE);
144 	box(FL_DOWN_BOX);
145 	bgnd_ = FL_BACKGROUND2_COLOR;
146 	fgnd_ = FL_GREEN;
147 	scale_color = FL_BLACK;
148 
149 	maximum_ = 100.0;
150 	value_ = 0.0;
151 
152 	select_ = 2; // 100 W scale
153 
154   // Get the box borders...
155 	bx = Fl::box_dx(box());
156 	by = Fl::box_dy(box());
157 	bw = Fl::box_dw(box());
158 	bh = Fl::box_dh(box());
159 
160 	tx = X + bx;
161 	tw = W - bw;
162 	ty = Y + by;
163 	th = H - bh;
164 
165 	static int fsize = 6;
166 	fl_font(FL_HELVETICA, fsize);
167 	meter_width = fl_width(W100_face);
168 	while ((meter_width < tw) && (fl_height() < th)) {
169 		fsize++;
170 		fl_font(FL_HELVETICA, fsize);
171 		meter_width = fl_width(W100_face);
172 	}
173 	fsize--;
174 	fl_font(FL_HELVETICA, fsize);
175 	meter_width = fl_width(W100_face);
176 
177 	meter_height = fl_height();
178 	label(W100_face);
179 	labelfont(FL_HELVETICA);
180 	labelsize(fsize);
181 	labelcolor(scale_color);
182 
183 	sx = (tw - meter_width) / 2 + fl_width("|") / 2;
184 
185 	meter_width -= fl_width("|");
186 }
187 
resize(int X,int Y,int W,int H)188 void PWRmeter::resize(int X, int Y, int W, int H) {
189 	Fl_Widget::resize(X,Y,W,H);
190 
191 	bx = Fl::box_dx(box());
192 	by = Fl::box_dy(box());
193 	bw = Fl::box_dw(box());
194 	bh = Fl::box_dh(box());
195 
196 	tx = X + bx;
197 	tw = W - bw;
198 	ty = Y + by;
199 	th = H - bh;
200 
201 	const char *face;
202 	switch (select_) {
203 		case P25:
204 			face = W25_face;
205 			break;
206 		case P50:
207 			face = W50_face;
208 			break;
209 		case P100:
210 			face = W100_face;
211 			break;
212 		case P200:
213 			face = W200_face;
214 			break;
215 		case AUTO:
216 		default:
217 			face = W25_face;
218 	}
219 
220 	static int fsize = 6;
221 	fl_font(FL_HELVETICA, fsize);
222 	meter_width = fl_width(face);
223 	while ((meter_width < tw) && (fl_height() < th)) {
224 		fsize++;
225 		fl_font(FL_HELVETICA, fsize);
226 		meter_width = fl_width(face);
227 	}
228 	fsize--;
229 	fl_font(FL_HELVETICA, fsize);
230 	meter_width = fl_width(face);
231 
232 	meter_height = fl_height();
233 	label(face);
234 	labelfont(FL_HELVETICA);
235 	labelsize(fsize);
236 	labelcolor(scale_color);
237 
238 	sx = (tw - meter_width) / 2 + fl_width("|") / 2;
239 	meter_width -= fl_width("|");
240 
241 }
242 
handle(int event)243 int PWRmeter::handle(int event)
244 {
245 	if (Fl::event_inside( this )) {
246 		if (event == FL_RELEASE) {
247 			do_callback();
248 			return 1;
249 		}
250 	}
251 	return 0;
252 }
253 
254 //
255 // End of PWRmeter.cxx
256 //
257