ede/ede-panel/applets/clock/Clock.cpp

85 lines
1.4 KiB
C++
Raw Normal View History

2009-10-03 11:33:08 +04:00
#include "Applet.h"
#include <FL/Fl_Box.H>
#include <FL/Fl.H>
#include <time.h>
#include <edelib/Debug.h>
#include <edelib/Run.h>
EDELIB_NS_USING(run_async)
static void clock_refresh(void *o);
class Clock : public Fl_Box {
private:
2010-03-08 00:51:06 +03:00
int hour;
2010-03-08 03:03:44 +03:00
char buf[64], tbuf[128];
2010-03-08 00:51:06 +03:00
time_t curr_time;
struct tm *curr_tm;
2009-10-03 11:33:08 +04:00
public:
2010-03-08 00:51:06 +03:00
Clock() : Fl_Box(450, 0, 80, 25, NULL), hour(0) {
2010-03-08 00:33:55 +03:00
box(FL_FLAT_BOX);
2009-10-03 11:33:08 +04:00
}
~Clock() {
Fl::remove_timeout(clock_refresh);
}
int handle(int e);
void update_time(void);
};
static void clock_refresh(void *o) {
Clock *c = (Clock *)o;
c->update_time();
Fl::repeat_timeout(1.0, clock_refresh, o);
}
void Clock::update_time(void) {
2010-03-08 00:51:06 +03:00
curr_time = time(NULL);
curr_tm = localtime(&curr_time);
if(!curr_tm)
2009-10-03 11:33:08 +04:00
return;
2010-03-08 00:51:06 +03:00
strftime(buf, sizeof(buf), "%H:%M:%S", curr_tm);
2009-10-03 11:33:08 +04:00
label(buf);
2010-03-08 00:51:06 +03:00
/* update tooltip if needed */
if(curr_tm->tm_hour != hour) {
hour = curr_tm->tm_hour;
2010-03-08 03:03:44 +03:00
strftime(tbuf, sizeof(tbuf), "%A, %d %B %Y", curr_tm);
2010-03-08 00:51:06 +03:00
tooltip(tbuf);
}
2009-10-03 11:33:08 +04:00
}
int Clock::handle(int e) {
switch(e) {
case FL_SHOW: {
int ret = Fl_Box::handle(e);
2009-10-03 11:33:08 +04:00
Fl::add_timeout(0, clock_refresh, this);
return ret;
}
2009-10-03 11:33:08 +04:00
case FL_RELEASE:
run_async("ede-timedate");
break;
case FL_HIDE:
Fl::remove_timeout(clock_refresh);
/* fallthrough */
2009-10-03 11:33:08 +04:00
}
return Fl_Box::handle(e);
}
EDE_PANEL_APPLET_EXPORT (
Clock,
EDE_PANEL_APPLET_OPTION_ALIGN_RIGHT,
"Clock applet",
"0.1",
"empty",
"Sanel Zukan"
)