|
FLTK clock box widget
I got to playing around with the timers since it was mentioned and made a little clock box widget in the process. The main() is just a test for the widget. It should be able to be placed like you would place any other widget. Because all widgets will share the static text string changes to one are effectively changing all of them. It seems to keep fairly close to my toolbar clock as well. It will pause when you drag the window it is in though.
The file clock_box2.cpp is the non-class version and clock_box.cpp is the class version. Let me know what you think. The class version should be able to be used just like you would use a box except the label() has been made private so you can't set it directly.
/**********************************************************************/
/* FILENAME: clock_box2.cpp */
/**********************************************************************/
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <ctime>
#include <cstring>
void callback(void* user) {
time_t mytime = time(NULL);
struct tm* my_time = localtime(&mytime);
static char clock_text[30];
strcpy(clock_text,asctime(my_time));
((Fl_Box*)user)->label(clock_text);
Fl::repeat_timeout(1.0, callback, user);
}
int main (int argc, char *argv[]) {
Fl_Window window(300, 200,200,20,"Test Clock");
Fl_Box my_clock(FL_NO_BOX,0,0,200,20,"tick-tock");
window.show(argc, argv);
Fl::add_timeout(1.0, callback, &my_clock);
return Fl::run();
}
/**********************************************************************/
/* FILENAME: clock_box.cpp */
/* ORIGINATION DATE: 7/9/05 */
/* ORIGINATOR: M Roth */
/* LICENSE: gpl (www.gnu.org/copyleft/gpl.html) */
/**********************************************************************/
/* Copyright (C)2004, 2005 */
/* Mark Roth - GIM[dot]mroth[at]gmail[dot]com */
/**********************************************************************/
/* clock_box is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published */
/* by the Free Software Foundation, either version 2 of the License, */
/* or (at your option) any later version. */
/* gct_data is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY' without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with clock_box. If not, write to : */
/* the Free Software Foundation, Inc. */
/* 51 Franklin Street */
/* Fifth Floor */
/* Boston, MA 02110-1301 USA */
/* */
/**********************************************************************/
/* DESCRIPTION: */
/* This is a clock widget for fltk 1.1.x */
/* It is an extension of a Fl_Box. It will display the system time */
/* in a box with the type set to FL_NO_BOX but you could change that */
/* using the normal methods. The only thing you can no longer do is */
/* change the label as it has been made private. */
/**********************************************************************/
#ifndef __CLOCKBOX_H__
#define __CLOCKBOX_H__
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <ctime>
#include <cstring>
class ClockBox : public Fl_Box{
public:
ClockBox(int x, int y, int w, int h, const char* L="tick-tock") : Fl_Box(FL_NO_BOX,x,y,w,h,L) {
Fl::add_timeout(1.0, update, this);
};
~ClockBox(){};
private:
time_t mytime;
struct tm* my_time;
static char clock_text[30];
void PutTime(){
mytime = time(NULL);
my_time = localtime(&mytime);
strcpy(clock_text,asctime(my_time));
this->label(clock_text);
Fl::repeat_timeout(1.0, update, this);
};
static void update(void* user){
((ClockBox*)user)->PutTime();
};
void label(const char* L) { Fl_Widget::label(L);};
};
char ClockBox::clock_text[30];
#endif
int main (int argc, char *argv[]) {
Fl_Window window(200,20,"class ClockBox");
ClockBox my_clock(0,0,window.w(),window.h());
window.show(argc, argv);
return Fl::run();
}
Mark
__________________ "Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
|