1 /* Class ServingMenuButton */
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 "db.h"
24 #include <string.h>
25 #include <stdlib.h>
26 #ifndef __hpux
27 #include <stdint.h>
28 #endif
29 
30 static Fl_Menu_Item pulldown[] = { {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} , {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}};
31 static char servdisp[20][81];
32 static char buf[1000];
33 static char *result[61];
34 static char *big_label;
35 static ViewFoods *vf;
36 static struct food *current_foodptr;
37 
menu_cb(Fl_Widget * which,void * n)38 void menu_cb(Fl_Widget *which, void *n)
39 {
40 intptr_t num = (intptr_t) n;
41 if (num > 0)
42  {
43  big_label = result[3*num-2];
44  vf->new_serving_unit(atof(result[3*num-1]), atof(result[3*num-3]));
45  }
46 else if (num == 0) vf->change_default_serving(big_label);
47 else if (num == -1) vf->user_serving_unit();
48 }
49 
fill_serving_menu(struct food * foodptr)50 void fill_serving_menu(struct food *foodptr)
51 {
52 int i, resultcount = 0;
53 int *resultparm = &resultcount;
54 current_foodptr = foodptr;
55 read_weightlib(foodptr->ndb_no,buf,sizeof(buf),result,resultparm);
56 for (i = 1; i <= resultcount / 3; i++)
57  {
58  strcpy(servdisp[i-1],result[3*i-3]);
59  strcat(servdisp[i-1]," ");
60  strcat(servdisp[i-1],result[3*i-2]);
61  pulldown[i-1].label(servdisp[i-1]);
62  pulldown[i-1].callback(menu_cb, (void *)i);
63  pulldown[i-1].flags = 0;
64  }
65 for (i = resultcount / 3 + 1; i < 20; i++) pulldown[i].flags = FL_MENU_INVISIBLE;
66 pulldown[resultcount / 3].flags = 0;
67 pulldown[resultcount / 3].label("Save the current serving as the default for this food");
68 pulldown[resultcount / 3].callback(menu_cb, (void *)0);
69 pulldown[resultcount / 3 + 1].flags = 0;
70 pulldown[resultcount / 3 + 1].label("Save the current serving as the default but I will enter a new serving unit");
71 pulldown[resultcount / 3 + 1].callback(menu_cb, (void *)-1);
72 }
73 
ServingMenuButton(int x,int y,int w,int h,ViewFoods * vfoods,Fl_Color widgetcolor)74 ServingMenuButton::ServingMenuButton (int x, int y, int w, int h, ViewFoods *vfoods, Fl_Color widgetcolor) : Fl_Menu_Button (x, y, w, h)
75 {
76 vf = vfoods;
77 this->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
78 this->color(fl_lighter(widgetcolor));
79 this->clear_visible_focus();
80 this->menu(pulldown);
81 }
82 
set_label(char * label)83 void ServingMenuButton::set_label(char *label)
84 {
85 this->menu(pulldown);
86 big_label = label;
87 this->label(big_label);
88 }
89 
set_label(void)90 void ServingMenuButton::set_label(void)
91 {
92 this->label(big_label);
93 }
94 
blank_out(void)95 void ServingMenuButton::blank_out(void)
96 {
97 this->menu(NULL);
98 this->label("");
99 }
100 
resize(int x,int y,int w,int h)101 void ServingMenuButton::resize(int x, int y, int w, int h)
102 {
103 Fl_Menu_Button::resize(x, y, w, h);
104 this->labelsize(FL_NORMAL_SIZE);
105 for (int i = 0; i < 21; i++) if (pulldown[i].label() != NULL) pulldown[i].labelsize(FL_NORMAL_SIZE);
106 }
107