00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "framework/gui/control_gui.h"
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026
00027 #ifdef MOTIF_ENABLED
00028 #include <Xm/PushB.h>
00029 #include <Xm/Form.h>
00030 #include <Xm/Label.h>
00031 #include <Xm/ScrollBar.h>
00032 #include <Xm/TextF.h>
00033 #endif
00034
00035 #include "lista.cc"
00036 #include "arreglo.cc"
00037
00038
00039
00040
00041
00042 #define ESPERO_F(tipo, msg) \
00043 if ( tipo_token != (tipo) ) { \
00044 fprintf(stderr, "<CONTROL_GUI_FLOAT> " \
00045 "ERROR: Esperaba %s y recibi [%s].\n", \
00046 (msg), cad); fflush(stderr); return FALSE; \
00047 }
00048
00049 #define VERIFICAR_F(c) \
00050 if ( !(c) ) {\
00051 fprintf(stderr, \
00052 "<CONTROL_GUI_FLOAT> ERROR: No se pudo crear un objeto " \
00053 "dinamico!\n");\
00054 fflush(stderr);\
00055 exit(1);\
00056 }
00057
00058 #ifdef WXWINDOWS_ENABLED
00059
00060
00061
00062
00063
00064 BEGIN_EVENT_TABLE(_WXWINDOWS_EVTMNGR_SPIN, wxEvtHandler)
00065 EVT_SPIN_UP(-1, _WXWINDOWS_EVTMNGR_SPIN::OnSpinBtnUp)
00066 EVT_SPIN_DOWN(-1, _WXWINDOWS_EVTMNGR_SPIN::OnSpinBtnDown)
00067 END_EVENT_TABLE()
00068
00069 _WXWINDOWS_EVTMNGR_SPIN::_WXWINDOWS_EVTMNGR_SPIN() : wxEvtHandler()
00070 {
00071 _Textbox = NULL;
00072 _Spin = NULL;
00073 }
00074
00075 void
00076 _WXWINDOWS_EVTMNGR_SPIN::set_data(_WXWINDOWS_TEXTBOX *textbox,
00077 wxSpinButton *spin)
00078 {
00079 _Textbox = textbox;
00080 _Spin = spin;
00081 }
00082
00083 void
00084 _WXWINDOWS_EVTMNGR_SPIN::OnSpinBtnUp(wxCommandEvent& event)
00085 {
00086 double incremento = 1;
00087 double val;
00088 wxString s;
00089 char cad[30];
00090
00091 if ( !_Textbox ) return;
00092
00093 s = _Textbox->GetValue();
00094 val = (double)atof(s.GetData());
00095 val += incremento;
00096 sprintf(cad, "%f", val);
00097 _Textbox->SetValue(wxString(cad));
00098 _Textbox->_Control->set_valor(val);
00099
00100 _Spin->SetValue(5);
00101
00102 }
00103
00104 void
00105 _WXWINDOWS_EVTMNGR_SPIN::OnSpinBtnDown(wxCommandEvent& event)
00106 {
00107 double incremento = 1;
00108 double val;
00109 wxString s;
00110 char cad[30];
00111
00112 if ( !_Textbox ) return;
00113
00114 s = _Textbox->GetValue();
00115 val = (double)atof(s.GetData());
00116 val -= incremento;
00117 sprintf(cad, "%f", val);
00118 _Textbox->SetValue(wxString(cad));
00119 _Textbox->_Control->set_valor(val);
00120
00121 _Spin->SetValue(5);
00122
00123 }
00124
00125
00126
00127
00128
00129 BEGIN_EVENT_TABLE(_WXWINDOWS_TEXTBOX, wxTextCtrl)
00130 EVT_CHAR(_WXWINDOWS_TEXTBOX::OnChar)
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 END_EVENT_TABLE()
00144
00145 _WXWINDOWS_TEXTBOX::_WXWINDOWS_TEXTBOX(wxWindow *parent, wxWindowID id,
00146 const wxString &value, const wxPoint &pos, const wxSize &size,
00147 int style) : wxTextCtrl(parent, -1, value, pos, size, style)
00148 {
00149 _Control = NULL;
00150 }
00151
00152 void
00153 _WXWINDOWS_TEXTBOX::OnChar(wxKeyEvent& event)
00154
00162 {
00163 wxString s;
00164
00165 if ( event.KeyCode() == WXK_RETURN && _Control) {
00166 s = GetValue();
00167 _Control->set_valor((double)atof(s.GetData()));
00168 }
00169 event.Skip();
00170 }
00171
00172 void
00173 _WXWINDOWS_TEXTBOX::set_control(CONTROL_GUI_FLOAT *c)
00174 {
00175 _Control = c;
00176 }
00177
00178 #endif // WXWINDOWS
00179
00180
00181
00182
00183
00184 CONTROL_GUI_FLOAT::CONTROL_GUI_FLOAT(JED_INTERFACE **p) : CONTROL_GUI(p)
00185 {
00186 _nombre_variable = NULL;
00187 _nombre_subvariable = NULL;
00188 estilo = 0;
00189 _valor = 0;
00190 Entidad_controlada = NULL;
00191 #ifdef MOTIF_ENABLED
00192 widget_datos = NULL;
00193 #endif
00194 #ifdef WXWINDOS_ENABLED
00195 textbox = NULL;
00196 slider = NULL;
00197 #endif
00198 }
00199
00200 CONTROL_GUI_FLOAT::~CONTROL_GUI_FLOAT()
00201 {
00202 }
00203
00204 BOOLEAN
00205 CONTROL_GUI_FLOAT::leer(TOKENIZADOR *Sabiondo)
00206 {
00207 char cad[1000];
00208 int tipo_token = TK_DESCONOCIDO, pos;
00209
00210
00211 pos = 1;
00212 while ( tipo_token != TK_CERRAR) {
00213 tipo_token = Sabiondo->siguiente_token(cad);
00214 switch ( pos ) {
00215 case 1:
00216 ESPERO_F(TK_CADENA, "una cadena, nombre de una variable");
00217 if ( strlen(cad) > MAX_CAD-1 ) cad[MAX_CAD-1] = '\0';
00218 des_comille(cad);
00219 _nombre_variable = new char[strlen(cad)+1];
00220 VERIFICAR_F(_nombre_variable);
00221 strcpy(_nombre_variable, cad);
00222 pos++;
00223 break;
00224 case 2: ESPERO_F(TK_ABRIR, "\"{\""); pos++; break;
00225 default:
00226 if ( tipo_token == TK_CERRAR ) break;
00227 ESPERO_F(TK_IDENTIFICADOR, "un identificador (3)");
00228 if ( strcmp(cad, "etiqueta") == 0 ) {
00229 tipo_token = Sabiondo->siguiente_token(cad);
00230 ESPERO_F(TK_CADENA, "una cadena");
00231 des_comille(cad);
00232 _nombre_etiqueta = new char[strlen(cad)+1];
00233 VERIFICAR_F(_nombre_etiqueta);
00234 strcpy(_nombre_etiqueta, cad);
00235 }
00236 else if ( strcmp(cad, "estilo") == 0 ) {
00237 tipo_token = Sabiondo->siguiente_token(cad);
00238 ESPERO_F(TK_NUMERO, "un estilo (numero)");
00239 estilo = atoi(cad);
00240 }
00241 ;
00242 break;
00243 }
00244 }
00245
00246 return TRUE;
00247 }
00248
00249 void
00250 CONTROL_GUI_FLOAT::actualizar(ENTIDAD *Entidad)
00251 {
00252
00253 double *Nuevo_valor;
00254 int tipo = T_FLOAT;
00255
00256 if ( !Entidad || !Entidad->consultar_variable(
00257 (const char *)_nombre_subvariable,
00258 tipo, (void**)(&Nuevo_valor)) ) return;
00259
00260 _valor = *Nuevo_valor;
00261
00262 #ifdef MOTIF_ENABLED
00263 if ( widget_datos ) {
00264
00265 char mi_valormotif[1024];
00266
00267 sprintf(mi_valormotif, "%f", _valor);
00268 simplifique_real(mi_valormotif);
00269
00270 switch ( estilo ) {
00271
00272 case 1: case 2:
00273 XtVaSetValues(widget_datos, XmNsliderSize, 10, NULL);
00274 XtVaSetValues(widget_datos, XmNminimum, -100, NULL);
00275 XtVaSetValues(widget_datos, XmNmaximum, 100, NULL);
00276 XtVaSetValues(widget_datos, XmNvalue, (int)_valor*10, NULL);
00277 break;
00278
00279 case 0: default:
00280 XmTextFieldSetString(widget_datos, mi_valormotif);
00281 break;
00282 }
00283 }
00284 #endif
00285
00286 #ifdef WXWINDOWS_ENABLED
00287
00288 if ( textbox || slider ) {
00289 char mi_valorwxw[1024];
00290
00291 sprintf(mi_valorwxw, "%f", _valor);
00292 simplifique_real(mi_valorwxw);
00293
00294 switch ( estilo ) {
00295
00296 case 1: case 2:
00297 if ( !slider ) return;
00298 slider->SetValue((int)_valor);
00299 break;
00300
00301 case 0: default:
00302 if ( !textbox ) return;
00303 textbox->SetValue(wxString(mi_valorwxw));
00304 break;
00305
00306 }
00307 }
00308 #endif
00309 }
00310
00311 void
00312 CONTROL_GUI_FLOAT::set_valor(double val)
00313 {
00314 _valor = val;
00315 if ( Entidad_controlada ) {
00316 Entidad_controlada->actualizar_variable(_nombre_subvariable,
00317 T_FLOAT, (void *)(&_valor));
00318 Entidad_controlada->propagar_cambios();
00319 (*_Padre)->solicitar_repintado();
00320 }
00321 }
00322
00323 #ifdef MOTIF_ENABLED
00324
00325 static void
00326 control_gui_float_callback(Widget text_w, XtPointer client_data,
00327 XtPointer )
00328
00329
00330 {
00331 char *cad = XmTextFieldGetString(text_w);
00332 CONTROL_GUI_FLOAT *Control = (CONTROL_GUI_FLOAT *)client_data;
00333 Control->set_valor((double)atof(cad));
00334 }
00335
00336 static void
00337 control_gui_float_callback1(Widget barra, XtPointer client_data,
00338 XtPointer )
00339
00340
00341 {
00342 int valor;
00343 CONTROL_GUI_FLOAT *Control = (CONTROL_GUI_FLOAT *)client_data;
00344
00345 XtVaGetValues(barra, XmNvalue, &valor, NULL);
00346 Control->set_valor(((double)valor)/10);
00347 }
00348
00349 Widget
00350 CONTROL_GUI_FLOAT::crear_motif(REPOSITORIO_DE_ENTIDADES *Repositorio,
00351 Widget padre)
00355 {
00356
00357 ENTIDAD *e;
00358 char entidad[1024];
00359 char *ptr;
00360
00361 strcpy(entidad, _nombre_variable);
00362 ptr = strtok(entidad, ".");
00363
00364
00365
00366
00367 e = Repositorio->buscar_entidad("_default", ptr);
00368 if ( e ) {
00369
00370 Entidad_controlada = e;
00371 _nombre_subvariable = strchr(_nombre_variable, '.')+1;
00372 e->registrar_controlador(this);
00373 }
00374
00375
00376 char nombre_defecto[] = "[SIN VARIABLE]";
00377 char *mi_nombre = nombre_defecto;
00378 XmString motif_cad;
00379
00380 if ( _nombre_variable ) mi_nombre = _nombre_variable;
00381 if ( _nombre_etiqueta ) mi_nombre = _nombre_etiqueta;
00382 motif_cad = XmStringCreateLocalized(mi_nombre);
00383
00384
00385 Widget control = NULL;
00386 Widget etiqueta, barra, texto;
00387
00388 switch ( estilo ) {
00389
00390 case 1:
00391 control = XtVaCreateManagedWidget ("[VARIABLE_FLOAT]", xmFormWidgetClass,
00392 padre, XmNwidth, 180, XmNheight, 20, XmNmarginWidth, 0,
00393 XmNmarginHeight, 0, XmNresizePolicy, XmRESIZE_ANY,
00394 XmNallowOverlap, False, NULL);
00395 etiqueta = XtVaCreateManagedWidget ("[LABEL]", xmLabelWidgetClass, control,
00396 XmNlabelString, motif_cad, XmNx, 10, XmNy, 10, NULL);
00397
00398 barra = XtVaCreateManagedWidget("Scroll", xmScrollBarWidgetClass, control,
00399 XmNorientation, XmHORIZONTAL,
00400
00401 XmNhighlightThickness, 1, XmNhighlightOnEnter, False, XmNheight, 15,
00402 NULL);
00403 widget_datos = barra;
00404 actualizar(Entidad_controlada);
00405 XtAddCallback (barra, XmNvalueChangedCallback,control_gui_float_callback1,
00406 (void *)this);
00407 XtAddCallback (barra, XmNdragCallback,control_gui_float_callback1,
00408 (void *)this);
00409
00410 XtVaSetValues(etiqueta,
00411 XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, 0,
00412 XmNtopAttachment, XmATTACH_FORM, XmNtopOffset, 0,
00413 XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, 0, NULL);
00414 XtVaSetValues(barra,
00415 XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, 0,
00416 XmNtopAttachment, XmATTACH_FORM, XmNtopOffset, 0,
00417 XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, 0,
00418 XmNleftAttachment, XmATTACH_WIDGET, XmNleftOffset, 2,
00419 XmNleftWidget, etiqueta, NULL);
00420 break;
00421
00422 case 2:
00423 control = XtVaCreateManagedWidget ("[VARIABLE_FLOAT]", xmFormWidgetClass,
00424 padre, XmNwidth, 20, XmNheight, 320,
00425 XmNmarginWidth, 0, XmNmarginHeight, 0,
00426 XmNresizePolicy, XmRESIZE_ANY,
00427 XmNallowOverlap, False,
00428 NULL);
00429 etiqueta = XtVaCreateManagedWidget ("[LABEL]", xmLabelWidgetClass, control,
00430 XmNlabelString, motif_cad, XmNx, 10, XmNy, 10, NULL);
00431
00432 barra = XtVaCreateManagedWidget("Scroll", xmScrollBarWidgetClass, control,
00433 XmNorientation, XmVERTICAL,
00434 XmNprocessingDirection, XmMAX_ON_BOTTOM,
00435 XmNhighlightThickness, 1,
00436 XmNhighlightOnEnter, False,
00437 XmNwidth, 15,
00438 NULL);
00439
00440
00441
00442
00443
00444
00445 widget_datos = barra;
00446 actualizar(Entidad_controlada);
00447 XtAddCallback (barra, XmNvalueChangedCallback,control_gui_float_callback1,
00448 (void *)this);
00449 XtAddCallback (barra, XmNdragCallback,control_gui_float_callback1,
00450 (void *)this);
00451
00452 XtVaSetValues(etiqueta,
00453 XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, 0,
00454 XmNtopAttachment, XmATTACH_FORM, XmNtopOffset, 0,
00455 XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, 0,
00456 NULL);
00457 XtVaSetValues(barra,
00458 XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, 0,
00459 XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, 0,
00460 XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, 0,
00461 XmNtopAttachment, XmATTACH_WIDGET, XmNtopOffset, 2,
00462 XmNtopWidget, etiqueta,
00463 NULL);
00464 break;
00465
00466 case 0: default:
00467
00468 control = XtVaCreateManagedWidget ("[VARIABLE_FLOAT]", xmFormWidgetClass,
00469 padre, XmNwidth, 120, XmNheight, 30,
00470 XmNmarginWidth, 0, XmNmarginHeight, 0, XmNresizePolicy, XmRESIZE_ANY,
00471 XmNallowOverlap, False, XmNchildType, XmFRAME_GENERIC_CHILD, NULL);
00472
00473
00474 etiqueta = XtVaCreateManagedWidget("[LABEL]", xmLabelWidgetClass,control,
00475 XmNlabelString, motif_cad, XmNx, 10, XmNy, 10, NULL);
00476
00477
00478 texto = XtVaCreateManagedWidget ("[TEXT]", xmTextFieldWidgetClass, control,
00479 XmNx, 10, XmNy, 60, NULL);
00480
00481 widget_datos = texto;
00482 actualizar(Entidad_controlada);
00483 XtAddCallback (texto, XmNactivateCallback, control_gui_float_callback,
00484 (void *)this);
00485
00486
00487 XtVaSetValues(etiqueta,
00488 XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, 0,
00489 XmNtopAttachment, XmATTACH_FORM, XmNtopOffset, 0,
00490 XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, 0,
00491 NULL);
00492 XtVaSetValues(texto,
00493 XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, 0,
00494 XmNtopAttachment, XmATTACH_FORM, XmNtopOffset, 0,
00495 XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, 0,
00496 XmNleftAttachment, XmATTACH_WIDGET, XmNleftOffset, 2,
00497 XmNleftWidget, etiqueta,
00498 NULL);
00499
00500 break;
00501
00502 }
00503
00504
00505
00506 XtManageChild(control);
00507
00508 XmStringFree(motif_cad);
00509 return control;
00510 }
00511 #endif // MOTIF_ENABLED
00512
00513 #ifdef WXWINDOWS_ENABLED
00514 void
00515 CONTROL_GUI_FLOAT::crear_wxwindows(REPOSITORIO_DE_ENTIDADES *Repositorio,
00516 wxPanel *padre)
00517 {
00518
00519 ENTIDAD *e;
00520 char entidad[1024];
00521 char *ptr;
00522
00523 strcpy(entidad, _nombre_variable);
00524 ptr = strtok(entidad, ".");
00525 e = Repositorio->buscar_entidad("_default", ptr);
00526 if ( e ) {
00527 Entidad_controlada = e;
00528 _nombre_subvariable = strchr(_nombre_variable, '.')+1;
00529 e->registrar_controlador(this);
00530 }
00531
00532
00533 char nombre_defecto[] = "[SIN VARIABLE]";
00534 char *mi_nombre = nombre_defecto;
00535
00536 if ( _nombre_variable ) mi_nombre = _nombre_variable;
00537 if ( _nombre_etiqueta ) mi_nombre = _nombre_etiqueta;
00538
00539
00540
00541 wxSizer *acomodador_con_etiqueta;
00542 wxStaticText *etiqueta;
00543 _WXWINDOWS_EVTMNGR_SPIN *mngr;
00544
00545 switch ( estilo ) {
00546
00547 case 1:
00548 acomodador_con_etiqueta = new wxBoxSizer(wxHORIZONTAL);
00549 etiqueta = new wxStaticText(padre, -1, wxString(mi_nombre));
00550 acomodador_con_etiqueta->Add(etiqueta, 0, wxRIGHT, 5);
00551 slider = new wxSlider(padre, -1, 0, 0, 7,
00552 wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL);
00553 acomodador_con_etiqueta->Add(slider, 1, wxLEFT, 5);
00554 actualizar(Entidad_controlada);
00555
00556 acomodador_con_etiqueta->Fit(padre);
00557 padre->SetSizer(acomodador_con_etiqueta);
00558
00559 actualizar(Entidad_controlada);
00560 break;
00561
00562 case 2:
00563 acomodador_con_etiqueta = new wxBoxSizer(wxVERTICAL);
00564 etiqueta = new wxStaticText(padre, -1, wxString(mi_nombre));
00565 acomodador_con_etiqueta->Add(etiqueta, 0, wxBOTTOM, 5);
00566 slider = new wxSlider(padre, -1, 0, 0, 7,
00567 wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL);
00568 acomodador_con_etiqueta->Add(slider, 1, wxTOP, 5);
00569 actualizar(Entidad_controlada);
00570
00571 acomodador_con_etiqueta->Fit(padre);
00572 padre->SetSizer(acomodador_con_etiqueta);
00573
00574 actualizar(Entidad_controlada);
00575 break;
00576
00577 case 0: default:
00578
00579 acomodador_con_etiqueta = new wxBoxSizer(wxHORIZONTAL);
00580 etiqueta = new wxStaticText(padre, -1, wxString(mi_nombre));
00581 acomodador_con_etiqueta->Add(etiqueta, 0, wxRIGHT, 5);
00582 textbox = new _WXWINDOWS_TEXTBOX(padre, -1, _T("vacio"),
00583 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
00584 textbox->set_control(this);
00585 textbox->SetEditable(TRUE);
00586 acomodador_con_etiqueta->Add(textbox, 1, wxLEFT, 5);
00587
00588
00589 spin = new wxSpinButton(padre, -1);
00590 mngr = new _WXWINDOWS_EVTMNGR_SPIN();
00591 mngr->set_data(textbox, spin);
00592 spin->PushEventHandler(mngr);
00593 spin->SetRange(0, 10);
00594 spin->SetValue(5);
00595 acomodador_con_etiqueta->Add(spin, 0, 0, 0);
00596
00597
00598 actualizar(Entidad_controlada);
00599 acomodador_con_etiqueta->Fit(padre);
00600 padre->SetSizer(acomodador_con_etiqueta);
00601 break;
00602
00603 }
00604
00605
00606 }
00607 #endif // WXWINDOWS_ENABLED
00608
00609
00610
00611
00612