#include #include #include #include #include #include #include #include #include #include #include using namespace std; class FechaNacimiento : public Fl_Group { public: FechaNacimiento(int x, int y, int year) : Fl_Group(x,y,400,30,"") { c_edad = new Fl_Counter(x+80,y+5,80,30,"Edad (años):"); c_edad->align(FL_ALIGN_LEFT); c_edad->type(FL_SIMPLE_COUNTER); c_edad->step(1); c_edad->bounds(0,500); vi_anyoNacim = new Fl_Value_Input(x+300,y+5,50,30,"Año nacimiento:"); vi_anyoNacim->value(year); int edad = getAnyoActual() - year; c_edad->value(edad); c_edad->callback(edad_cb); vi_anyoNacim->callback(anyo_cb); end(); } void setEdad(int edad) { c_edad->value(edad); c_edad->do_callback(); // only if setEdad() is public } private: int getAnyoActual() { curtime = time (NULL); loctime = localtime (&curtime); return 1900+loctime->tm_year; } void setAnyoNacimiento(int year) { vi_anyoNacim->value(year); } private: Fl_Counter *c_edad; Fl_Value_Input *vi_anyoNacim; int anyoNacimiento; time_t curtime; struct tm *loctime; private: static void edad_cb(Fl_Widget *w, void *datos=NULL); static void anyo_cb(Fl_Widget *w, void *datos=NULL); }; void FechaNacimiento::edad_cb(Fl_Widget *w, void *datos) { FechaNacimiento *fn = (FechaNacimiento *) w->parent(); int edad = (int) fn->c_edad->value(); fn->setAnyoNacimiento(fn->getAnyoActual() - edad); fn->do_callback(); // permitir que FechaNacimiento tenga función de callback } void FechaNacimiento::anyo_cb(Fl_Widget *w, void *datos) { FechaNacimiento *fn = (FechaNacimiento *) w->parent(); int year = (int)fn->vi_anyoNacim->value(); int edad = fn->getAnyoActual() - year; fn->setEdad(edad); } class VentanaPrueba : public Fl_Window { public: VentanaPrueba(int x, int y, int w, int h, char *title); private: Fl_Button *btSalir; FechaNacimiento *fnPadre, *fnMadre; Fl_Tabs *t_padres; private: static void salir_cb(Fl_Widget *w, void *datos=NULL); static void tab_cb(Fl_Widget *w, void *datos=NULL); static void fn_cb(Fl_Widget *w, void *datos=NULL); }; VentanaPrueba::VentanaPrueba(int x, int y, int w, int h, char *title) : Fl_Window(x,y,w,h,title) { begin(); // opcional btSalir= new Fl_Button(w-100,160,80,35,"&Terminar"); btSalir->callback(salir_cb); btSalir->tooltip("Salir sin guardar"); t_padres = new Fl_Tabs(5,5,w-10,h-50); t_padres->callback(tab_cb,NULL); Fl_Group *g_padre = new Fl_Group(0,40,w-10,h-50,"&Padre"); fnPadre = new FechaNacimiento(20,70,1960); fnPadre->setEdad(40); g_padre->end(); Fl_Group *g_madre = new Fl_Group(0,40,w-10,h-50,"&Madre"); fnMadre = new FechaNacimiento(20,70,1950); fnMadre->callback(fn_cb); g_madre->end(); t_padres->end(); end(); // necesario } void VentanaPrueba::fn_cb(Fl_Widget *w, void *datos) { cout << "modificando FechaNacimiento" << endl; } void VentanaPrueba::tab_cb(Fl_Widget *w, void *datos) { VentanaPrueba *v = (VentanaPrueba *) w->parent(); if (v->t_padres->value() == v->t_padres->child(0)) { v->t_padres->child(0)->activate(); v->t_padres->child(1)->deactivate(); } else { v->t_padres->child(0)->deactivate(); v->t_padres->child(1)->activate(); } } void VentanaPrueba::salir_cb(Fl_Widget *w, void *datos) { VentanaPrueba *v = (VentanaPrueba *) w->parent(); v->hide(); } int main() { VentanaPrueba v1(10,10,400,200,"Datos familiares"); v1.show(); return Fl::run(); }