| 1 | /* |
| 2 | This file is part of the GVars3 Library. |
| 3 | |
| 4 | Copyright (C) 2005 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | |
| 9 | #include "gvars3/instances.h" |
| 10 | #include "gvars3/GUI_Fltk.h" |
| 11 | #include "gvars3/GStringUtil.h" |
| 12 | #include <vector> |
| 13 | #include <string.h> |
| 14 | #include <cstdlib> |
| 15 | #include <sstream> |
| 16 | #include <unistd.h> |
| 17 | #include <map> |
| 18 | #include <set> |
| 19 | #include <string> |
| 20 | |
| 21 | #include <FL/Fl.H> |
| 22 | #include <FL/fl_draw.H> |
| 23 | #include <FL/Fl_Window.H> |
| 24 | #include <FL/Fl_Widget.H> |
| 25 | #include <FL/Fl_Button.H> |
| 26 | #include <FL/Fl_Check_Button.H> |
| 27 | #include <FL/Fl_Value_Slider.H> |
| 28 | #include <FL/Fl_Value_Input.H> |
| 29 | #include <FL/Fl_Box.H> |
| 30 | |
| 31 | |
| 32 | #define POLL_UPDATE 1 |
| 33 | |
| 34 | using namespace std; |
| 35 | namespace GVars3 |
| 36 | { |
| 37 | |
| 38 | GUI_Fltk::GUI_Fltk() |
| 39 | { |
| 40 | init = 0; |
| 41 | GUI.RegisterCommand("GUI.InitXInterface", InitXInterfaceCB, this); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void poll_callback(void* v) |
| 46 | { |
| 47 | GUI_Fltk* t = (GUI_Fltk*) v; |
| 48 | |
| 49 | t->poll_windows(); |
| 50 | Fl::check(); |
| 51 | |
| 52 | //Repeat the polling timeout |
| 53 | Fl::repeat_timeout(0.02, poll_callback, v); |
| 54 | } |
| 55 | |
| 56 | void* GUI_Fltk::do_stuff_CB(void* v) |
| 57 | { |
| 58 | //Add a one shot timeout which makes widgets poll for changes |
| 59 | Fl::add_timeout(0.02, poll_callback, v); |
| 60 | |
| 61 | for(;;) |
| 62 | { |
| 63 | //Fl::lock(); |
| 64 | Fl::run(); |
| 65 | Fl::check(); |
| 66 | //Fl::unlock(); |
| 67 | |
| 68 | //If no windows are present, sleep and start again |
| 69 | usleep(100000); |
| 70 | |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | #define UI (*(GUI_Fltk*)(ptr)) |
| 76 | |
| 77 | void GUI_Fltk::InitXInterfaceCB(void* ptr, string, string sParams) |
| 78 | { |
| 79 | UI.InitXInterface(sParams); |
| 80 | } |
| 81 | |
| 82 | void GUI_Fltk::InitXInterface(string args) |
| 83 | { |
| 84 | if(init) |
| 85 | { |
| 86 | cerr << "??GUI_Fltk::InitXInterface: already initialised." << endl; |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | vector<string> vs = ChopAndUnquoteString(args); |
| 91 | |
| 92 | if(vs.size() > 0) |
| 93 | name = vs[0]; |
| 94 | else |
| 95 | name = "GUI"; |
| 96 | |
| 97 | GUI.RegisterCommand(name + ".AddWindow", AddWindowCB, this); |
| 98 | |
| 99 | init = 1; |
| 100 | } |
| 101 | |
| 102 | void GUI_Fltk::start_thread() |
| 103 | { |
| 104 | pthread_create(&gui_thread, 0, do_stuff_CB, this); |
| 105 | } |
| 106 | |
| 107 | void GUI_Fltk::process_in_crnt_thread() |
| 108 | { |
| 109 | //Fl::lock(); |
| 110 | poll_windows(); |
| 111 | Fl::check(); |
| 112 | //Fl::unlock(); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | //////////////////////////////////////////////////////////////////////////////// |
| 117 | // |
| 118 | // Helper functions |
| 119 | // |
| 120 | |
| 121 | string GUI_Fltk::remove_suffix(string cmd, string suffix) |
| 122 | { |
| 123 | cmd.resize(cmd.length() - suffix.length()); |
| 124 | return cmd; |
| 125 | } |
| 126 | |
| 127 | bool GUI_Fltk::check_window(string win_name, string err) |
| 128 | { |
| 129 | if(windows.find(win_name) != windows.end()) |
| 130 | return 1; |
| 131 | |
| 132 | cerr << "!!!! GUI_Fltk::" << err << "<<: window " << win_name << " does not exist!" << endl; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | //////////////////////////////////////////////////////////////////////////////// |
| 137 | // |
| 138 | // Window |
| 139 | // |
| 140 | |
| 141 | void GUI_Fltk::AddWindowCB(void* ptr, string, string sParams) |
| 142 | { |
| 143 | //Fl::lock(); |
| 144 | UI.AddWindow(sParams); |
| 145 | //Fl::unlock(); |
| 146 | } |
| 147 | |
| 148 | class GUI_Fltk_win:public Fl_Window |
| 149 | { |
| 150 | public: |
| 151 | GUI_Fltk_win(int w, string name, string caption) |
| 152 | :Fl_Window(w, 0),win_name(name),labl(caption) |
| 153 | { |
| 154 | label(caption.c_str()); |
| 155 | callback(my_callback); |
| 156 | } |
| 157 | |
| 158 | void add(Fl_Widget* widg) |
| 159 | { |
| 160 | int lw = 0, lh = 0; |
| 161 | |
| 162 | if((widg->align() & FL_ALIGN_LEFT) && !(widg->align() & FL_ALIGN_INSIDE) ) |
| 163 | widg->measure_label(lw, lh); |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | //Position the widget |
| 169 | widg->resize(GUI_Fltk::widget_padding_x + lw, h() + GUI_Fltk::widget_padding_y, |
| 170 | w() - 2 * GUI_Fltk::widget_padding_x - lw , GUI_Fltk::widget_height); |
| 171 | |
| 172 | //Resize the window |
| 173 | size(w(), h() + GUI_Fltk::widget_height + 2 * GUI_Fltk::widget_padding_y); |
| 174 | |
| 175 | Fl_Window::add(widg); |
| 176 | } |
| 177 | |
| 178 | void poll_update() |
| 179 | { |
| 180 | //Go through all the widgets, updating if necessary |
| 181 | for(int c=0; c < children(); c++) |
| 182 | child(c)->do_callback(child(c),POLL_UPDATE); |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | string win_name, labl; |
| 187 | |
| 188 | static void my_callback(Fl_Widget* w) |
| 189 | { |
| 190 | //Called on close event |
| 191 | GUI_Fltk_win* win = (GUI_Fltk_win*) w; |
| 192 | GUI.ParseLine(win->win_name+".Destroy"); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | void GUI_Fltk::poll_windows() |
| 197 | { |
| 198 | for(map<string, window>::iterator i=windows.begin(); i != windows.end(); i++) |
| 199 | { |
| 200 | if(i->second.showme) |
| 201 | { |
| 202 | i->second.win->show(); |
| 203 | i->second.showme = false; |
| 204 | } |
| 205 | i->second.win->poll_update(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | void GUI_Fltk::AddWindow(string sParams) |
| 212 | { |
| 213 | if(!init) |
| 214 | { |
| 215 | cerr << "! GUI_Fltk::AddWindow: Not initialised. Call InitXInterface first." << endl; |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | vector<string> vs = ChopAndUnquoteString(sParams); |
| 220 | if(vs.size()<1 || vs.size()>3) |
| 221 | { |
| 222 | cerr << "! GUI_Motif::AddWindow: need 1 - 3 params: Name, Caption=Name, Width=200 " << endl; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | if(windows.count(vs[0])>0) |
| 227 | { |
| 228 | cerr << "? GUI_Motif::AddWindow: A window with id \"" << vs[0] << "\" already exists." << endl; |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | string sCaption; |
| 233 | int width = 200; |
| 234 | |
| 235 | if(vs.size()>=2) |
| 236 | sCaption=vs[1]; |
| 237 | else |
| 238 | sCaption=vs[0]; |
| 239 | |
| 240 | if(vs.size() > 2) |
| 241 | width = atoi(vs[2].c_str()); |
| 242 | |
| 243 | window w; |
| 244 | w.win = new GUI_Fltk_win(width, vs[0], sCaption); |
| 245 | w.showme = true; |
| 246 | w.win->end(); |
| 247 | //w.win->show(); |
| 248 | |
| 249 | windows[vs[0]] = w; |
| 250 | |
| 251 | GUI.RegisterCommand(vs[0] + ".Destroy", DestroyWindowCB, this); |
| 252 | GUI.RegisterCommand(vs[0] + ".AddPushButton", AddPushButtonCB, this); |
| 253 | GUI.RegisterCommand(vs[0] + ".AddToggleButton", AddToggleButtonCB, this); |
| 254 | GUI.RegisterCommand(vs[0] + ".AddSlider", AddSliderCB, this); |
| 255 | GUI.RegisterCommand(vs[0] + ".AddMonitor", AddMonitorCB, this); |
| 256 | GUI.RegisterCommand(vs[0] + ".AddSpin", AddSpinCB, this); |
| 257 | GUI.RegisterCommand(vs[0] + ".AddLabel", AddLabelCB, this); |
| 258 | } |
| 259 | |
| 260 | void GUI_Fltk::DestroyWindowCB(void* ptr, string cmd, string) |
| 261 | { |
| 262 | //Fl::lock(); |
| 263 | UI.DestroyWindow(cmd); |
| 264 | //Fl::unlock(); |
| 265 | } |
| 266 | |
| 267 | void GUI_Fltk::DestroyWindow(string cmd) |
| 268 | { |
| 269 | string win_name = remove_suffix(cmd, ".Destroy"); |
| 270 | |
| 271 | if(!check_window(win_name, "Destroy")) |
| 272 | return; |
| 273 | |
| 274 | GUI.UnRegisterCommand(win_name + ".Destroy"); |
| 275 | GUI.UnRegisterCommand(win_name + ".AddPushButton"); |
| 276 | GUI.UnRegisterCommand(win_name + ".AddToggleButton"); |
| 277 | GUI.UnRegisterCommand(win_name + ".AddSlider"); |
| 278 | GUI.UnRegisterCommand(win_name + ".AddMonitor"); |
| 279 | GUI.UnRegisterCommand(win_name + ".AddSpin"); |
| 280 | GUI.UnRegisterCommand(win_name + ".AddLabel"); |
| 281 | |
| 282 | |
| 283 | delete windows[win_name].win; |
| 284 | //windows[win_name].win->hide(); |
| 285 | windows.erase(win_name); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | //////////////////////////////////////////////////////////////////////////////// |
| 292 | // |
| 293 | // Pushbutton stuff |
| 294 | // |
| 295 | |
| 296 | void GUI_Fltk::AddPushButtonCB(void* ptr, string cmd, string args) |
| 297 | { |
| 298 | //Fl::lock(); |
| 299 | UI.AddPushButton(cmd, args); |
| 300 | //Fl::unlock(); |
| 301 | } |
| 302 | |
| 303 | class cmd_button:public Fl_Button |
| 304 | { |
| 305 | public: |
| 306 | cmd_button(string name, string command) |
| 307 | :Fl_Button(0, 0, 1, 1),labl(name), cmd(command) |
| 308 | { |
| 309 | label(labl.c_str()); |
| 310 | callback(my_callback); |
| 311 | } |
| 312 | |
| 313 | private: |
| 314 | //The button label just stores the pointer, so we need to store the string here |
| 315 | string labl, cmd; |
| 316 | static void my_callback(Fl_Widget* w, long what_shall_I_do) |
| 317 | { |
| 318 | if(what_shall_I_do == POLL_UPDATE) |
| 319 | return; |
| 320 | |
| 321 | cmd_button* b = (cmd_button*) w; |
| 322 | GUI.ParseLine(b->cmd); |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | |
| 327 | |
| 328 | void GUI_Fltk::AddPushButton(string cmd, string args) |
| 329 | { |
| 330 | string window_name = remove_suffix(cmd, ".AddPushButton"); |
| 331 | |
| 332 | vector<string> vs = ChopAndUnquoteString(args); |
| 333 | if(vs.size()!=2) |
| 334 | { |
| 335 | cerr << "! GUI_Fltk::AddPushButton: Need 2 params (name, command)." << endl; |
| 336 | return; |
| 337 | }; |
| 338 | |
| 339 | if(!check_window(window_name, "AddPushButton")) |
| 340 | return; |
| 341 | |
| 342 | window& w=windows[window_name]; |
| 343 | |
| 344 | //Create button |
| 345 | |
| 346 | Fl_Button* b = new cmd_button(vs[0], vs[1]); |
| 347 | w.win->add(b); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | //////////////////////////////////////////////////////////////////////////////// |
| 352 | // |
| 353 | // Toggle button stuff |
| 354 | // |
| 355 | |
| 356 | void GUI_Fltk::AddToggleButtonCB(void* ptr, string cmd, string args) |
| 357 | { |
| 358 | //Fl::lock(); |
| 359 | UI.AddToggleButton(cmd, args); |
| 360 | //Fl::unlock(); |
| 361 | } |
| 362 | |
| 363 | class toggle_button: public Fl_Check_Button |
| 364 | { |
| 365 | public: |
| 366 | toggle_button(string name, string gvar, string def) |
| 367 | :Fl_Check_Button(0, 0, 1, 1),labl(name),my_int(gvar, def, true) |
| 368 | { |
| 369 | callback(my_callback); |
| 370 | value(*my_int); |
| 371 | label(labl.c_str()); |
| 372 | |
| 373 | when(FL_WHEN_CHANGED); |
| 374 | } |
| 375 | |
| 376 | void poll_update() |
| 377 | { |
| 378 | if(*my_int != value()) |
| 379 | value((bool)*my_int); |
| 380 | } |
| 381 | |
| 382 | private: |
| 383 | string labl; |
| 384 | gvar3<int> my_int; |
| 385 | |
| 386 | static void my_callback(Fl_Widget* w, long what_shall_I_do) |
| 387 | { |
| 388 | toggle_button* b = (toggle_button*)w; |
| 389 | |
| 390 | if(what_shall_I_do == POLL_UPDATE) |
| 391 | b->poll_update(); |
| 392 | else |
| 393 | *(b->my_int) = b->value(); |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | |
| 398 | void GUI_Fltk::AddToggleButton(string cmd, string args) |
| 399 | { |
| 400 | string window_name = remove_suffix(cmd, ".AddToggleButton"); |
| 401 | |
| 402 | vector<string> vs = ChopAndUnquoteString(args); |
| 403 | if(vs.size() != 2 && vs.size() != 3) |
| 404 | { |
| 405 | cerr << "! GUI_Fltk::AddToggleButton: Need 2-3 params (name, gvar2_int name, {default})." << endl; |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | if(!check_window(window_name, "AddToggleButton")) |
| 410 | return; |
| 411 | |
| 412 | window& w = windows[window_name]; |
| 413 | |
| 414 | if(vs.size() == 2) |
| 415 | vs.push_back("true"); |
| 416 | |
| 417 | Fl_Widget* b = new toggle_button(vs[0], vs[1], vs[2]); |
| 418 | w.win->add(b); |
| 419 | } |
| 420 | |
| 421 | //////////////////////////////////////////////////////////////////////////////// |
| 422 | // |
| 423 | // Slider stuff |
| 424 | // |
| 425 | |
| 426 | void GUI_Fltk::AddSliderCB(void* ptr, string cmd, string args) |
| 427 | { |
| 428 | //Fl::lock(); |
| 429 | UI.AddSlider(cmd, args); |
| 430 | //Fl::unlock(); |
| 431 | } |
| 432 | |
| 433 | typedef Fl_Slider slider_type; |
| 434 | |
| 435 | class slider_bar: public slider_type |
| 436 | { |
| 437 | public: |
| 438 | slider_bar(string gvar_name, double min, double max, bool show_val_) |
| 439 | :slider_type(0, 0, 1, 1),varname(gvar_name), show_val(show_val_) |
| 440 | { |
| 441 | type(FL_HORIZONTAL); |
| 442 | bounds(min, max); |
| 443 | align(FL_ALIGN_CENTER); |
| 444 | callback(my_callback); |
| 445 | when(FL_WHEN_CHANGED); |
| 446 | step(0); |
| 447 | poll_update(); |
| 448 | } |
| 449 | |
| 450 | double compute_slider_frac() |
| 451 | { |
| 452 | fl_font(labelfont(), labelsize()); |
| 453 | double lw = fl_width(label()); |
| 454 | double s_size = lw + 5; |
| 455 | return s_size / w(); |
| 456 | } |
| 457 | |
| 458 | void resize(int x_, int y_, int w_, int h_) |
| 459 | { |
| 460 | slider_type::resize(x_,y_,w_,h_); |
| 461 | if (show_val) |
| 462 | slider_size(compute_slider_frac()); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | void poll_update() |
| 467 | { |
| 468 | string crnt=GV3::get_var(varname); |
| 469 | |
| 470 | if(crnt != cached_value) |
| 471 | { |
| 472 | cached_value = crnt; |
| 473 | double newval=0; |
| 474 | serialize::from_string(crnt, newval); |
| 475 | |
| 476 | //Update range if necessary |
| 477 | if(newval > maximum()) maximum(newval); |
| 478 | if(newval < minimum()) minimum(newval); |
| 479 | |
| 480 | if (show_val) { |
| 481 | label(cached_value.c_str()); |
| 482 | double new_frac = compute_slider_frac(); |
| 483 | if (new_frac > slider_size()) |
| 484 | slider_size(new_frac); |
| 485 | } |
| 486 | value(newval); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | string set_gvar_to_value() |
| 491 | { |
| 492 | ostringstream ost; |
| 493 | ost.precision(3); |
| 494 | ost << value(); |
| 495 | GV3::set_var(varname, ost.str(), 1); |
| 496 | return ost.str(); |
| 497 | } |
| 498 | |
| 499 | private: |
| 500 | string varname, cached_value; |
| 501 | bool show_val; |
| 502 | |
| 503 | static void my_callback(Fl_Widget* w, long what_shall_I_do) |
| 504 | { |
| 505 | slider_bar* s = (slider_bar*) w; |
| 506 | |
| 507 | if(what_shall_I_do == POLL_UPDATE) |
| 508 | s->poll_update(); |
| 509 | else { |
| 510 | string val = s->set_gvar_to_value(); |
| 511 | s->poll_update(); |
| 512 | } |
| 513 | } |
| 514 | }; |
| 515 | |
| 516 | void GUI_Fltk::AddSlider(string cmd, string args) |
| 517 | { |
| 518 | string win_name = remove_suffix(cmd, ".AddSlider"); |
| 519 | |
| 520 | vector<string> vs = ChopAndUnquoteString(args); |
| 521 | if(vs.size() != 3 && vs.size() !=4) |
| 522 | { |
| 523 | cerr << "! GUI_Fltk::AddSlider: Need 3 or 4 params (gvar_name min max [showval])." << endl; |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | if(!check_window(win_name, "AddSlider")) |
| 528 | return; |
| 529 | |
| 530 | window& w = windows[win_name]; |
| 531 | |
| 532 | double min, max; |
| 533 | |
| 534 | serialize::from_string(vs[1], min); |
| 535 | serialize::from_string(vs[2], max); |
| 536 | |
| 537 | Fl_Widget* b = new slider_bar(vs[0], min, max, vs.size()==4); |
| 538 | |
| 539 | w.win->add(b); |
| 540 | } |
| 541 | |
| 542 | |
| 543 | //////////////////////////////////////////////////////////////////////////////// |
| 544 | // |
| 545 | // Monitor stuff |
| 546 | // |
| 547 | |
| 548 | void GUI_Fltk::AddMonitorCB(void* ptr, string cmd, string args) |
| 549 | { |
| 550 | //Fl::lock(); |
| 551 | UI.AddMonitor(cmd, args); |
| 552 | //Fl::unlock(); |
| 553 | } |
| 554 | |
| 555 | class monitor: public Fl_Box |
| 556 | { |
| 557 | public: |
| 558 | monitor(string t, string gvar_name) |
| 559 | :Fl_Box(0, 0, 1, 1),title(t), gv_name(gvar_name) |
| 560 | { |
| 561 | callback(my_callback); |
| 562 | align(FL_ALIGN_TOP|FL_ALIGN_INSIDE|FL_ALIGN_LEFT); |
| 563 | poll_update(); |
| 564 | } |
| 565 | |
| 566 | void poll_update() |
| 567 | { |
| 568 | string gvar_text = "\"" + GV3::get_var(gv_name) + "\""; |
| 569 | |
| 570 | if(gvar_text != cached_gv_text) |
| 571 | { |
| 572 | cached_gv_text = gvar_text; |
| 573 | full_label = title + ": " + cached_gv_text; |
| 574 | label(full_label.c_str()); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | private: |
| 579 | string title, gv_name, full_label, cached_gv_text; |
| 580 | |
| 581 | static void my_callback(Fl_Widget* w, long what_shall_I_do) |
| 582 | { |
| 583 | if(what_shall_I_do == POLL_UPDATE) |
| 584 | ((monitor*)w)->poll_update(); |
| 585 | } |
| 586 | |
| 587 | }; |
| 588 | |
| 589 | |
| 590 | void GUI_Fltk::AddMonitor(string cmd, string args) |
| 591 | { |
| 592 | string win_name = remove_suffix(cmd, ".AddMonitor"); |
| 593 | |
| 594 | vector<string> vs = ChopAndUnquoteString(args); |
| 595 | if(vs.size() != 2 && vs.size() != 2) |
| 596 | { |
| 597 | cerr << "! GUI_Fltk::AddMonitor: Need 2 or 3 params (label, gvar_name, {poll update !!ignored!! )." << endl; |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | if(!check_window(win_name, "AddSlider")) |
| 602 | return; |
| 603 | |
| 604 | window& w = windows[win_name]; |
| 605 | |
| 606 | Fl_Widget* m = new monitor(vs[0], vs[1]); |
| 607 | w.win->add(m); |
| 608 | } |
| 609 | |
| 610 | //GUI_Fltk Gui_Fltk(&GUI); |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | //////////////////////////////////////////////////////////////////////////////// |
| 617 | // |
| 618 | // Spincontrol stuff |
| 619 | // |
| 620 | |
| 621 | void GUI_Fltk::AddSpinCB(void* ptr, string cmd, string args) |
| 622 | { |
| 623 | //Fl::lock(); |
| 624 | UI.AddSpin(cmd, args); |
| 625 | //Fl::unlock(); |
| 626 | } |
| 627 | |
| 628 | class spin2: public Fl_Value_Input |
| 629 | { |
| 630 | public: |
| 631 | spin2(string gvar_name, string t, double min, double max) |
| 632 | :Fl_Value_Input(0, 0, 1, 1),varname(gvar_name), title(t) |
| 633 | { |
| 634 | label(title.c_str()); |
| 635 | align(FL_ALIGN_LEFT); |
| 636 | bounds(min, max); |
| 637 | callback(my_callback); |
| 638 | when(FL_WHEN_CHANGED); |
| 639 | increment(0, 1); |
| 640 | poll_update(); |
| 641 | } |
| 642 | |
| 643 | void poll_update() |
| 644 | { |
| 645 | string crnt=GV3::get_var(varname); |
| 646 | |
| 647 | |
| 648 | if(crnt != cached_value) |
| 649 | { |
| 650 | cached_value = crnt; |
| 651 | double newval=0; |
| 652 | serialize::from_string(crnt, newval); |
| 653 | |
| 654 | //Update range if necessary |
| 655 | if(newval > maximum()) maximum(newval); |
| 656 | if(newval < minimum()) minimum(newval); |
| 657 | |
| 658 | value(newval); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | void set_gvar_to_value() |
| 663 | { |
| 664 | ostringstream ost; |
| 665 | ost << value(); |
| 666 | GV3::set_var(varname, ost.str(), 1); |
| 667 | cached_value = ost.str(); |
| 668 | } |
| 669 | |
| 670 | private: |
| 671 | string varname, cached_value, title; |
| 672 | |
| 673 | static void my_callback(Fl_Widget* w, long what_shall_I_do) |
| 674 | { |
| 675 | spin2* s = (spin2*) w; |
| 676 | |
| 677 | if(what_shall_I_do == POLL_UPDATE) |
| 678 | s->poll_update(); |
| 679 | else |
| 680 | s->set_gvar_to_value(); |
| 681 | } |
| 682 | }; |
| 683 | |
| 684 | void GUI_Fltk::AddSpin(string cmd, string args) |
| 685 | { |
| 686 | string win_name = remove_suffix(cmd, ".AddSpin"); |
| 687 | string title = ""; |
| 688 | |
| 689 | vector<string> vs = ChopAndUnquoteString(args); |
| 690 | if(vs.size() != 3 && vs.size() != 4) |
| 691 | { |
| 692 | cerr << "! GUI_Fltk::AddSpin: Need 3-4 params (gvar_name min max [title]])." << endl; |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | if(!check_window(win_name, "AddSpin")) |
| 697 | return; |
| 698 | |
| 699 | window& w = windows[win_name]; |
| 700 | |
| 701 | double min, max; |
| 702 | |
| 703 | serialize::from_string(vs[1], min); |
| 704 | serialize::from_string(vs[2], max); |
| 705 | |
| 706 | if(vs.size() == 4) |
| 707 | title = vs[3]; |
| 708 | |
| 709 | Fl_Widget* b = new spin2(vs[0], title, min, max); |
| 710 | |
| 711 | w.win->add(b); |
| 712 | } |
| 713 | |
| 714 | |
| 715 | void GUI_Fltk::AddLabelCB(void* ptr, string cmd, string args) |
| 716 | { |
| 717 | //Fl::lock(); |
| 718 | UI.AddLabel(cmd, args); |
| 719 | //Fl::unlock(); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | class label: public Fl_Box |
| 724 | { |
| 725 | public: |
| 726 | label(string t) |
| 727 | :Fl_Box(0, 0, 1, 1), title(t) |
| 728 | { |
| 729 | align(FL_ALIGN_CENTER); |
| 730 | Fl_Box::label(title.c_str()); |
| 731 | } |
| 732 | private: |
| 733 | string title; |
| 734 | }; |
| 735 | |
| 736 | void GUI_Fltk::AddLabel(string cmd, string args) |
| 737 | { |
| 738 | string win_name = remove_suffix(cmd, ".AddLabel"); |
| 739 | |
| 740 | vector<string> vs = ChopAndUnquoteString(args); |
| 741 | if(vs.size() != 1) |
| 742 | { |
| 743 | cout << "! GUI_Fltk::AddLabel: Need 1 params (label)." << endl; |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | if(!check_window(win_name, "AddLabel")) |
| 748 | return; |
| 749 | |
| 750 | window& w = windows[win_name]; |
| 751 | |
| 752 | Fl_Widget* m = new label(vs[0]); |
| 753 | w.win->add(m); |
| 754 | } |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | //Instantiations |
| 761 | class GUI_Fltk GUI_Fltk_instance; |
| 762 | |
| 763 | void GUIWidgets::process_in_crnt_thread() |
| 764 | { |
| 765 | GUI_Fltk_instance.process_in_crnt_thread(); |
| 766 | } |
| 767 | |
| 768 | void GUIWidgets::start_thread() |
| 769 | { |
| 770 | GUI_Fltk_instance.start_thread(); |
| 771 | } |
| 772 | |
| 773 | |
| 774 | } |