Flu_Progress_Meter.H 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // $Id: Flu_Progress_Meter.h,v 1.14 2005/02/21 21:12:06 jbryan Exp $
  2. /***************************************************************
  3. * FLU - FLTK Utility Widgets
  4. * Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
  5. *
  6. * This file and its content is protected by a software license.
  7. * You should have received a copy of this license with this file.
  8. * If not, please contact the Ohio Supercomputer Center immediately:
  9. * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
  10. *
  11. ***************************************************************/
  12. #ifndef _FLU_PROGRESS_METER_H
  13. #define _FLU_PROGRESS_METER_H
  14. #include <stdio.h>
  15. /* fltk includes */
  16. #include <FL/Fl.H>
  17. #include <FL/Fl_Double_Window.H>
  18. #include <FL/Fl_Button.H>
  19. #include "FLU/Flu_Label.H"
  20. #include "FLU/Flu_Progress.H"
  21. #include "FLU/Flu_Enumerations.H"
  22. #ifdef WIN32
  23. #include <winsock.h>
  24. #include <time.h>
  25. #else
  26. #include <sys/time.h>
  27. #endif
  28. //! This class provides a simple progress meter with elapsed time, estimated time to completion, and optional canceling behavior
  29. class FLU_EXPORT Flu_Progress_Meter : public Fl_Double_Window
  30. {
  31. DECLARE_CLASS_CHEAP_RTTI_2(Flu_Progress_Meter, Fl_Double_Window)
  32. public:
  33. //! Constructor which makes the progress meter with the title \b t
  34. Flu_Progress_Meter( const char* t = NULL );
  35. //! Default destructor
  36. virtual ~Flu_Progress_Meter();
  37. //! Set the title of the progress meter to \b t
  38. inline void title( const char* t )
  39. { Fl_Double_Window::label( t ); }
  40. //! \return the title of this meter
  41. inline const char* title() const
  42. { return Fl_Double_Window::label(); }
  43. //! Set the label that is displayed during the operation to \b l
  44. inline void label( const char* l )
  45. { if( _label ) _label->label( l ); }
  46. //! Get the label that is currently displayed
  47. inline const char* label() const
  48. { if( _label ) return _label->label(); else return ""; }
  49. //! Set the label on the cancel button
  50. inline void cancel_label( const char *l )
  51. { if( l && cancel ) cancel->label( l ); }
  52. //! Set the color of the progress bar. Default is FL_BLUE
  53. inline void color( Fl_Color c )
  54. { if( progress ) progress->selection_color( c ); }
  55. //! \return the current color of the progress bar
  56. inline Fl_Color color() const
  57. { if( progress ) return progress->selection_color(); else return FL_BLUE; }
  58. //! See if enough time has elapsed since the last call to check() (or since the meter was shown) that the display needs updated with a new value()
  59. /*! This function is fast enough to be called every iteration in a loop without slowing down
  60. the calling algorithm. It does a fast check to see
  61. if \b delta seconds have passed, and if so, returns \c true, else returns \c false. If it returns
  62. \c true, call value() with the current progress value. Using this helps to keep the progress
  63. meter timers counting, and probably doesn't need updated more than once a second.
  64. */
  65. //! Set the value of the progress bar. \b v should be on [0,1]
  66. /*! \return \c true if the cancel button has been pressed */
  67. bool value( float v );
  68. //! \return the current value of the progress bar, on [0,1]
  69. inline float value() const
  70. { if( progress ) return progress->value(); else return 0.0f; }
  71. //! This function can be registered to update the progress bar
  72. inline static void value_callback( float v, void *arg )
  73. { ((Flu_Progress_Meter*)arg)->value( v ); }
  74. //! This function can be registered to update the progress bar
  75. inline static void value_callbackd( double v, void *arg )
  76. { ((Flu_Progress_Meter*)arg)->value( (float)v ); }
  77. //! This function can be registered to update the progress bar
  78. inline static bool cvalue_callback( float v, void *arg )
  79. { return ((Flu_Progress_Meter*)arg)->value( v ); }
  80. //! This function can be registered to update the progress bar
  81. inline static bool cvalue_callbackd( double v, void *arg )
  82. { return ((Flu_Progress_Meter*)arg)->value( (float)v ); }
  83. //! Set whether to show the estimated time to completion of the operation. Default is \c true
  84. inline void show_completion_time( bool b )
  85. { _showETC = b; }
  86. //! Get whether to show the estimated time to completion of the operation
  87. inline bool show_completion_time() const
  88. { return _showETC; }
  89. //! Show the meter. If \b withCancelButton is \c true, then the "Cancel" button will be shown
  90. void show( bool withCancelButton = false );
  91. //! Reset the progress and timer (to reuse the meter without destroying it)
  92. void reset();
  93. //! Same as if the "cancel" button was pushed
  94. inline void do_cancel()
  95. { onCancel(); }
  96. //! Hide the meter
  97. void hide();
  98. //! Set the function that will be called when the "Cancel" button is pressed
  99. inline void cancel_callback( void (*cb)(void*), void* cbd = NULL )
  100. { _cancelCB = cb; _cancelCBD = cbd; }
  101. protected:
  102. private:
  103. #ifdef WIN32
  104. inline void gettimeofday( struct timeval *t, void* )
  105. {
  106. t->tv_sec = 0;
  107. t->tv_usec = clock();
  108. }
  109. #endif
  110. timeval startT, deltaT;
  111. inline static void _secondTimerCB( void *arg )
  112. { ((Flu_Progress_Meter*)arg)->secondTimerCB(); }
  113. void secondTimerCB( bool repeatTimer = true );
  114. void (*_cancelCB)(void*);
  115. void* _cancelCBD;
  116. bool _cancelled, _showETC;
  117. static void _onCancelCB( Fl_Widget* w, void* arg )
  118. { ((Flu_Progress_Meter*)arg)->onCancel(); }
  119. void onCancel()
  120. { _cancelled = true; if( _cancelCB ) _cancelCB( _cancelCBD ); }
  121. Flu_Progress* progress;
  122. Fl_Button* cancel;
  123. Flu_Label *_label, *etc;
  124. };
  125. #endif