Flu_Progress.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // $Id: Flu_Progress.cpp,v 1.14 2005/02/14 02:16:47 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. #include <stdio.h>
  13. #include <FL/Fl.H>
  14. #include <FL/Fl_Window.H>
  15. #include <FL/Fl_Double_Window.H>
  16. #include <FL/fl_draw.H>
  17. #include <stdlib.h>
  18. #include <FL/math.h>
  19. #include "FLU/Flu_Progress.H"
  20. class _FlWindowFlush : public Fl_Window
  21. {
  22. public:
  23. _FlWindowFlush() : Fl_Window(0,0,0,0) {}
  24. inline void flush() { Fl_Window::flush(); }
  25. };
  26. class _FlDoubleWindowFlush : public Fl_Double_Window
  27. {
  28. public:
  29. _FlDoubleWindowFlush() : Fl_Double_Window(0,0,0,0) {}
  30. inline void flush() { Fl_Double_Window::flush(); }
  31. };
  32. Flu_Progress :: Flu_Progress( int X, int Y, int W, int H, const char* l )
  33. : Fl_Valuator( X, Y, W, H, l )
  34. {
  35. box( FL_THIN_DOWN_BOX );
  36. align( FL_ALIGN_LEFT );
  37. color( FL_WHITE );
  38. selection_color( FL_BLUE );
  39. precision( 2 );
  40. range( 0, 1 );
  41. Fl_Valuator::value( 0 );
  42. }
  43. Flu_Progress :: ~Flu_Progress()
  44. {
  45. }
  46. int Flu_Progress :: value( float v )
  47. {
  48. int lastPercent = int(100.0 * (value() - minimum()) / (maximum()-minimum()) );
  49. int thisPercent = int(100.0 * (v - minimum()) / (maximum()-minimum()) );
  50. int ret = Fl_Valuator::value( v );
  51. if( thisPercent != lastPercent && window() && window()->shown() )
  52. Fl::check();
  53. return ret;
  54. }
  55. void Flu_Progress :: draw()
  56. {
  57. int dx = Fl::box_dx( box() ), dy = Fl::box_dy( box() ),
  58. dw = Fl::box_dw( box() ), dh = Fl::box_dh( box() );
  59. float percent = (value() - minimum()) / (maximum()-minimum());
  60. int val1 = int( float(w()-dw)*percent ), val2 = w()-dw - val1;
  61. char dummy[8];
  62. const char *buf;
  63. if( label() && (align() | FL_ALIGN_INSIDE) )
  64. buf = label();
  65. else
  66. {
  67. sprintf( dummy, "%d%%", int(percent*100.0) );
  68. buf = dummy;
  69. }
  70. fl_font( FL_HELVETICA_BOLD, h()/2+2 );
  71. int fW = 0, fH;
  72. fl_measure( buf, fW, fH );
  73. fl_draw_box( box(), x(), y(), w(), h(), color() );
  74. fl_color( active_r() ? selection_color() : fl_inactive(selection_color()) );
  75. fl_rectf( x()+dx, y()+dy, val1, h()-dh );
  76. fl_push_clip( x()+dx+val1, y()+dy, val2, h()-dh );
  77. fl_draw( buf, x()+w()/2-(fW>>1), y()+h()/2-(fH>>1), fW, fH, FL_ALIGN_CENTER );
  78. fl_pop_clip();
  79. fl_color( color() );
  80. fl_push_clip( x()+dx, y()+dy, val1, h()-dh );
  81. fl_draw( buf, x()+w()/2-(fW>>1), y()+h()/2-(fH>>1), fW, fH, FL_ALIGN_CENTER );
  82. fl_pop_clip();
  83. // draw the label
  84. if( label() && !(align() | FL_ALIGN_INSIDE) )
  85. draw_label();
  86. }