Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FL/Fl.H>
- #include <FL/Fl_Double_Window.H>
- #include <FL/Fl_Box.H>
- #include <FL/fl_draw.H>
- #include <FL/Fl_Button.H>
- #include <stdio.h>
- #include <time.h>
- #include <string>
- #include <sstream>
- #include <iostream>
- Fl_Box *progress;
- Fl_Button *status, *btnWork, *btnRest, *btnCurrent;
- time_t startTime = 0, targetTime = -1, workTime = 60 * 60, restTime = 15 * 60;
- char buff[20] = {0};
- void parseTime(int time, int* hours, int *minutes, int *seconds) {
- *hours = time / 3600;
- time -= *hours * 3600;
- *minutes = time / 60;
- time -= *minutes * 60;
- *seconds = time;
- if(*hours > 99) *hours = 99;
- }
- void updateTime(int diffTime) {
- int h, m, s, th, tm, ts;
- parseTime(diffTime, &h, &m, &s);
- if(targetTime >= 0) {
- parseTime(targetTime, &th, &tm, &ts);
- snprintf(buff, 20, "%02d:%02d / %02d:%02d", h, m, th, tm);
- } else {
- snprintf(buff, 20, "%02d:%02d / --:--", h, m);
- }
- progress->label(buff);
- }
- void setState(const char *text, Fl_Color color, time_t time) {
- status->label(text);
- status->labelcolor(color);
- targetTime = time;
- }
- void onBtnClick(Fl_Widget* w, void* inner) {
- if(w == btnWork) setState("WORK", 0xFF0040FF, workTime);
- else if(w == btnRest) setState("REST", 0xAAFF00FF, restTime);
- else setState("AWAIT", 0x00FFFFFF, -1);
- btnCurrent = (Fl_Button*) w;
- startTime = time(NULL);
- updateTime(0);
- }
- void Update_CB(void* userdata) {
- int diffTime = time(NULL) - startTime;
- updateTime(diffTime);
- if(targetTime > 0 && diffTime > targetTime) onBtnClick((btnCurrent == btnWork) ? btnRest : btnWork, 0);
- Fl::repeat_timeout(1.0, Update_CB, (void*) 0);
- }
- int main() {
- Fl_Double_Window *win = new Fl_Double_Window(200, 130, "timer");
- win->color(0x333333FF);
- status = new Fl_Button(0, 5, 200, 50, 0);
- status->box(FL_NO_BOX);
- status->labelsize(60);
- status->labelcolor(0x00FFFFFF);
- status->labelfont(FL_BOLD + FL_ITALIC);
- status->labeltype(FL_SHADOW_LABEL);
- status->callback(&onBtnClick);
- progress = new Fl_Box(0, 55, 200, 20, 0);
- progress->box(FL_NO_BOX);
- progress->labelsize(20);
- progress->labelcolor(0x00FFFFFF);
- progress->labelfont(FL_BOLD + FL_ITALIC);
- progress->labeltype(FL_NORMAL_LABEL);
- btnWork = new Fl_Button(10, 80, 85, 40, "work");
- btnWork->callback(&onBtnClick);
- btnRest = new Fl_Button(105, 80, 85, 40, "rest");
- btnRest->callback(&onBtnClick);
- onBtnClick(0, 0);
- Fl::add_timeout(1.0, Update_CB, (void*) 0);
- win->show();
- return (Fl::run());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement