Flu_Progress_Meter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // $Id: Flu_Progress_Meter.h,v 1.12 2004/06/30 00:18:00 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. public:
  32. //! Constructor which makes the progress meter with the title \b t
  33. Flu_Progress_Meter( const char* t = NULL );
  34. //! Default destructor
  35. virtual ~Flu_Progress_Meter();
  36. //! Set the title of the progress meter to \b t
  37. inline void title( const char* t )
  38. { Fl_Double_Window::label( t ); }
  39. //! \return the title of this meter
  40. inline const char* title() const
  41. { return Fl_Double_Window::label(); }
  42. //! Set the label that is displayed during the operation to \b l
  43. inline void label( const char* l )
  44. { if( _label ) _label->label( l ); }
  45. //! Get the label that is currently displayed
  46. inline const char* label() const
  47. { if( _label ) return _label->label(); else return ""; }
  48. //! Set the color of the progress bar. Default is FL_BLUE
  49. inline void color( Fl_Color c )
  50. { if( progress ) progress->selection_color( c ); }
  51. //! \return the current color of the progress bar
  52. inline Fl_Color color() const
  53. { if( progress ) return progress->selection_color(); else return FL_BLUE; }
  54. //! Set the value of the progress bar. \b v should be on [0,1]
  55. /*! \return \c true if the cancel button has been pressed */
  56. bool value( float v );
  57. //! \return the current value of the progress bar, on [0,1]
  58. inline float value() const
  59. { if( progress ) return progress->value(); else return 0.0f; }
  60. //! This function can be registered to update the progress bar
  61. inline static void value_callback( float v, void *arg )
  62. { ((Flu_Progress_Meter*)arg)->value( v ); }
  63. //! This function can be registered to update the progress bar
  64. inline static void value_callbackd( double v, void *arg )
  65. { ((Flu_Progress_Meter*)arg)->value( (float)v ); }
  66. //! This function can be registered to update the progress bar
  67. inline static bool cvalue_callback( float v, void *arg )
  68. { return ((Flu_Progress_Meter*)arg)->value( v ); }
  69. //! This function can be registered to update the progress bar
  70. inline static bool cvalue_callbackd( double v, void *arg )
  71. { return ((Flu_Progress_Meter*)arg)->value( (float)v ); }
  72. //! Set whether to show the estimated time to completion of the operation. Default is \c true
  73. inline void show_completion_time( bool b )
  74. { _showETC = b; }
  75. //! Get whether to show the estimated time to completion of the operation
  76. inline bool show_completion_time() const
  77. { return _showETC; }
  78. //! Show the meter. If \b withCancelButton is \c true, then the "Cancel" button will be shown
  79. void show( bool withCancelButton = false );
  80. void reset();
  81. //! Hide the meter
  82. void hide();
  83. //! Set the function that will be called when the "Cancel" button is pressed
  84. inline void cancel_callback( void (*cb)(void*), void* cbd = NULL )
  85. { _cancelCB = cb; _cancelCBD = cbd; }
  86. protected:
  87. private:
  88. #ifdef WIN32
  89. inline void gettimeofday( struct timeval *t, void* )
  90. {
  91. t->tv_sec = 0;
  92. t->tv_usec = clock();
  93. }
  94. #endif
  95. timeval startT;
  96. inline static void _secondTimerCB( void *arg )
  97. { ((Flu_Progress_Meter*)arg)->secondTimerCB(); }
  98. void secondTimerCB( bool repeatTimer = true );
  99. void (*_cancelCB)(void*);
  100. void* _cancelCBD;
  101. bool _cancelled, _showETC;
  102. static void _onCancelCB( Fl_Widget* w, void* arg )
  103. { ((Flu_Progress_Meter*)arg)->onCancel(); }
  104. void onCancel()
  105. { _cancelled = true; if( _cancelCB ) _cancelCB( _cancelCBD ); }
  106. Flu_Progress* progress;
  107. Fl_Button* cancel;
  108. Flu_Label *_label, *etc;
  109. };
  110. #endif