Advertisement
Guest User

Untitled

a guest
Feb 19th, 2021
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <FL/Fl.H>
  2. #include <FL/Fl_Double_Window.H>
  3. #include <FL/Fl_Box.H>
  4. #include <FL/fl_draw.H>
  5. #include <FL/Fl_Button.H>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <string>
  9. #include <sstream>
  10. #include <iostream>
  11.  
  12. Fl_Box *progress;
  13. Fl_Button *status, *btnWork, *btnRest, *btnCurrent;
  14. time_t startTime = 0, targetTime = -1, workTime = 60 * 60, restTime = 15 * 60;
  15. char buff[20] = {0};
  16.  
  17. void parseTime(int time, int* hours, int *minutes, int *seconds) {
  18. *hours = time / 3600;
  19. time -= *hours * 3600;
  20. *minutes = time / 60;
  21. time -= *minutes * 60;
  22. *seconds = time;
  23. if(*hours > 99) *hours = 99;
  24. }
  25.  
  26. void updateTime(int diffTime) {
  27. int h, m, s, th, tm, ts;
  28. parseTime(diffTime, &h, &m, &s);
  29. if(targetTime >= 0) {
  30. parseTime(targetTime, &th, &tm, &ts);
  31. snprintf(buff, 20, "%02d:%02d / %02d:%02d", h, m, th, tm);
  32. } else {
  33. snprintf(buff, 20, "%02d:%02d / --:--", h, m);
  34. }
  35. progress->label(buff);
  36. }
  37.  
  38. void setState(const char *text, Fl_Color color, time_t time) {
  39. status->label(text);
  40. status->labelcolor(color);
  41. targetTime = time;
  42. }
  43.  
  44. void onBtnClick(Fl_Widget* w, void* inner) {
  45. if(w == btnWork) setState("WORK", 0xFF0040FF, workTime);
  46. else if(w == btnRest) setState("REST", 0xAAFF00FF, restTime);
  47. else setState("AWAIT", 0x00FFFFFF, -1);
  48. btnCurrent = (Fl_Button*) w;
  49. startTime = time(NULL);
  50. updateTime(0);
  51. }
  52.  
  53. void Update_CB(void* userdata) {
  54. int diffTime = time(NULL) - startTime;
  55. updateTime(diffTime);
  56. if(targetTime > 0 && diffTime > targetTime) onBtnClick((btnCurrent == btnWork) ? btnRest : btnWork, 0);
  57. Fl::repeat_timeout(1.0, Update_CB, (void*) 0);
  58. }
  59.  
  60. int main() {
  61. Fl_Double_Window *win = new Fl_Double_Window(200, 130, "timer");
  62. win->color(0x333333FF);
  63. status = new Fl_Button(0, 5, 200, 50, 0);
  64. status->box(FL_NO_BOX);
  65. status->labelsize(60);
  66. status->labelcolor(0x00FFFFFF);
  67. status->labelfont(FL_BOLD + FL_ITALIC);
  68. status->labeltype(FL_SHADOW_LABEL);
  69. status->callback(&onBtnClick);
  70.  
  71. progress = new Fl_Box(0, 55, 200, 20, 0);
  72. progress->box(FL_NO_BOX);
  73. progress->labelsize(20);
  74. progress->labelcolor(0x00FFFFFF);
  75. progress->labelfont(FL_BOLD + FL_ITALIC);
  76. progress->labeltype(FL_NORMAL_LABEL);
  77.  
  78. btnWork = new Fl_Button(10, 80, 85, 40, "work");
  79. btnWork->callback(&onBtnClick);
  80.  
  81. btnRest = new Fl_Button(105, 80, 85, 40, "rest");
  82. btnRest->callback(&onBtnClick);
  83. onBtnClick(0, 0);
  84.  
  85. Fl::add_timeout(1.0, Update_CB, (void*) 0);
  86. win->show();
  87. return (Fl::run());
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement