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