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

80 lines
1.3 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:
Fl::add_timeout(0, clock_refresh, this);
break;
case FL_RELEASE:
run_async("ede-timedate");
break;
}
return Fl_Box::handle(e);
}
EDE_PANEL_APPLET_EXPORT (
Clock,
EDE_PANEL_APPLET_OPTION_ALIGN_RIGHT,
"Clock applet",
"0.1",
"empty",
"Sanel Zukan"
)