GIDForums  

Go Back   GIDForums > Computer Programming Forums > FLTK Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jul-2005, 18:51
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

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.


CPP / C++ / C Code:
/**********************************************************************/
/* 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();
}

CPP / C++ / C Code:
/**********************************************************************/
/* 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
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FLTK && fluid In Motion cable_guy_67 FLTK Forum 4 20-Mar-2008 03:52
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
Re: Derived FLTK Classes cable_guy_67 FLTK Forum 0 26-Jun-2005 20:07
FLTK with GLUT usmsci FLTK Forum 4 26-Nov-2004 16:21
Welcome to the FLTK Forums dsmith FLTK Forum 0 08-Sep-2004 06:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.