1 /* Class NutValue */
2
3 /*
4 NUT nutrition software
5 Copyright (C) 1996-2014 by Jim Jozwiak.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "Nut.h"
23 #include "util.h"
24
NutValue(int x,int y,int w,int h,Fl_Color widgetcolor,NutButtonWidget * nbw,struct food * fw,int nutnum,int screennum)25 NutValue::NutValue (int x, int y, int w, int h, Fl_Color widgetcolor, NutButtonWidget *nbw, struct food *fw, int nutnum, int screennum) : Fl_Wizard (x, y, w, h)
26 {
27 foodwork = fw;
28 Nut_Box *boxptr;
29 nutindex = nutnum;
30 screen = screennum;
31 data = &(foodwork->nutrient[nutindex]);
32 Fl_Group::current(0);
33
34 display = new Fl_Group(x, y, w, h);
35 Fl_Group::current(0);
36 display->color(widgetcolor);
37 valbox = new Nut_Box(x, y, 2*w/3, h);
38 valbox->align(FL_ALIGN_INSIDE|FL_ALIGN_RIGHT);
39 valbox->labelfont(FL_BOLD);
40 display->add(valbox);
41 boxptr = new Nut_Box(x+2*w/3, y, w/3, h);
42 if (screen == 0 && nutindex != CHO_NONFIB) boxptr->label("%");
43 else boxptr->label(Unit[nutindex]);
44 boxptr->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
45 display->add(boxptr);
46
47 overflow = new Fl_Group(x, y, w, h);
48 Fl_Group::current(0);
49 overflow->color(widgetcolor);
50 boxptr = new Nut_Box(x, y, 2*w/3, h);
51 if (screen == 0) boxptr->label(">999999");
52 else if (screen < 4) boxptr->label(">999999.9");
53 else boxptr->label(">999999.99");
54 boxptr->align(FL_ALIGN_INSIDE|FL_ALIGN_RIGHT);
55 boxptr->labelfont(FL_BOLD);
56 overflow->add(boxptr);
57 boxptr = new Nut_Box(x+2*w/3, y, w/3, h);
58 if (screen == 0 && nutindex != CHO_NONFIB) boxptr->label("%");
59 else boxptr->label(Unit[nutindex]);
60 boxptr->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
61 overflow->add(boxptr);
62
63 underflow = new Fl_Group(x, y, w, h);
64 Fl_Group::current(0);
65 underflow->color(widgetcolor);
66 boxptr = new Nut_Box(x, y, 2*w/3, h);
67 if (screen == 0) boxptr->label("<-999999");
68 else if (screen < 4) boxptr->label("<-999999.9");
69 else boxptr->label("<-999999.99");
70 boxptr->align(FL_ALIGN_INSIDE|FL_ALIGN_RIGHT);
71 boxptr->labelfont(FL_BOLD);
72 underflow->add(boxptr);
73 boxptr = new Nut_Box(x+2*w/3, y, w/3, h);
74 if (screen == 0 && nutindex != CHO_NONFIB) boxptr->label("%");
75 else boxptr->label(Unit[nutindex]);
76 boxptr->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
77 underflow->add(boxptr);
78
79 nodata = new Nut_Box(x, y, 2*w/3, h, "(nd)");
80 nodata->tooltip("No Data");
81 nodata->color(widgetcolor);
82 nodata->align(FL_ALIGN_INSIDE|FL_ALIGN_RIGHT);
83
84 questionmark = new Nut_Box(x, y, w, h, "?");
85 questionmark->color(widgetcolor);
86 questionmark->labelfont(FL_BOLD);
87 questionmark->align(FL_ALIGN_INSIDE|FL_ALIGN_CENTER);
88
89 this->add(display);
90 this->add(overflow);
91 this->add(underflow);
92 this->add(nodata);
93 this->add(questionmark);
94 this->box(FL_NO_BOX);
95
96 nbw->add_me_to_the_update_list(this);
97 special = 0;
98 }
99
NutValue(int x,int y,int w,int h,Fl_Color widgetcolor,NutButtonWidget * nbw,struct food * fw,int nutnum,int screennum,int cpf)100 NutValue::NutValue (int x, int y, int w, int h, Fl_Color widgetcolor, NutButtonWidget *nbw, struct food *fw, int nutnum, int screennum, int cpf) : Fl_Wizard (x, y, w, h)
101 {
102 foodwork = fw;
103 nutindex = nutnum;
104 screen = screennum;
105 data = &(foodwork->nutrient[nutindex]);
106 valbox = new Nut_Box(x, y, w, h);
107 valbox->color(widgetcolor);
108 valbox->labelfont(FL_BOLD);
109 valbox->align(FL_ALIGN_INSIDE|FL_ALIGN_CENTER);
110 questionmark = new Nut_Box(x, y, w, h, "?");
111 questionmark->color(widgetcolor);
112 questionmark->labelfont(FL_BOLD);
113 questionmark->align(FL_ALIGN_INSIDE|FL_ALIGN_CENTER);
114 this->end();
115 this->box(FL_NO_BOX);
116
117 nbw->add_me_to_the_update_list(this);
118 special = 1;
119 }
120
NutValue(int x,int y,int w,int h,Fl_Color widgetcolor,NutButtonWidget * nbw,struct food * fw,int nutnum,int screennum,int cpf,int omega)121 NutValue::NutValue (int x, int y, int w, int h, Fl_Color widgetcolor, NutButtonWidget *nbw, struct food *fw, int nutnum, int screennum, int cpf, int omega) : Fl_Wizard (x, y, w, h)
122 {
123 foodwork = fw;
124 nutindex = nutnum;
125 screen = screennum;
126 data = &(foodwork->nutrient[nutindex]);
127 valbox = new Nut_Box(x, y, w, h);
128 valbox->color(widgetcolor);
129 valbox->labelfont(FL_BOLD);
130 valbox->align(FL_ALIGN_INSIDE|FL_ALIGN_CENTER);
131 questionmark = new Nut_Box(x, y, w, h, "?");
132 questionmark->color(widgetcolor);
133 questionmark->labelfont(FL_BOLD);
134 questionmark->align(FL_ALIGN_INSIDE|FL_ALIGN_CENTER);
135 this->end();
136 this->box(FL_NO_BOX);
137
138 nbw->add_me_to_the_update_list(this);
139 special = 2;
140 }
141
update(void)142 void NutValue::update (void)
143 {
144 float printvalue;
145 switch (special)
146 {
147 case 0 :
148 if (!(*data < -383837.9 && *data > -383838.1))
149 {
150 if (screen == 0 && nutindex != CHO_NONFIB) printvalue = 100*(*data)/DV[nutindex];
151 else printvalue = *data;
152 if (printvalue > 999999) this->value(overflow);
153 else if (printvalue < -999999) this->value(underflow);
154 else if (test_for_negative_zero(data)) this->value(nodata);
155 else
156 {
157 if (screen > 3) snprintf(valbuf,20,"%1.2f",*data);
158 else if (screen > 0) snprintf(valbuf,20,"%1.1f",*data);
159 else if (nutindex == CHO_NONFIB) snprintf(valbuf,20,"%1.0f",*data);
160 else snprintf(valbuf,20,"%1.0f",printvalue);
161 valbox->label(valbuf);
162 this->value(display);
163 }
164 }
165 else this->value(questionmark);
166 break;
167 case 1 :
168 if (!(*data < -383837.9 && *data > -383838.1))
169 {
170 if (foodwork->nutrient[ENERC_KCAL] > 0.05)
171 {
172 pctcarb = 100 * foodwork->nutrient[CHO_KCAL] / foodwork->nutrient[ENERC_KCAL];
173 pctprot = 100 * foodwork->nutrient[PROT_KCAL] / foodwork->nutrient[ENERC_KCAL];
174
175 pctfat = 100 * foodwork->nutrient[FAT_KCAL] / foodwork->nutrient[ENERC_KCAL];
176 if (test_for_negative_zero(&pctcarb)) pctcarb = 0;
177 if (test_for_negative_zero(&pctprot)) pctprot = 0;
178 if (test_for_negative_zero(&pctfat)) pctfat = 0;
179 }
180 else
181 {
182 pctcarb = 0; pctprot = 0; pctfat = 0;
183 }
184 sprintf(valbuf,"%1.0f / %1.0f / %1.0f",pctprot,pctcarb,pctfat);
185 valbox->label(valbuf);
186 }
187 else this->value(questionmark);
188 break;
189 case 2 :
190 if (!(*data < -383837.9 && *data > -383838.1))
191 {
192 p3 = 900 * foodwork->nutrient[SHORT3] / foodwork->nutrient[ENERC_KCAL];
193 p6 = 900 * foodwork->nutrient[SHORT6] / foodwork->nutrient[ENERC_KCAL];
194 h3 = 900 * foodwork->nutrient[LONG3] / foodwork->nutrient[ENERC_KCAL];
195 h6 = 900 * foodwork->nutrient[LONG6] / foodwork->nutrient[ENERC_KCAL];
196 o = 900 * (foodwork->nutrient[FASAT] + foodwork->nutrient[FAMS] + foodwork->nutrient[FAPU] - foodwork->nutrient[SHORT6] - foodwork->nutrient[LONG6] - foodwork->nutrient[SHORT3] - foodwork->nutrient[LONG3]) / foodwork->nutrient[ENERC_KCAL];
197 n6 = (int) n6hufa(p3,p6,h3,h6,o,foodwork->nutrient[ENERC_KCAL]);
198 sprintf(valbuf,"%2d / %1d",n6 == 0 ? 0 : n6,n6 == 0 ? 0 : 100-n6);
199 valbox->label(valbuf);
200 }
201 else this->value(questionmark);
202 break;
203 }
204 }
205