Flu_Choice_Group.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // $Id: Flu_Choice_Group.cpp,v 1.18 2004/06/17 14:16:42 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 "FLU/Flu_Choice_Group.H"
  13. #include <stdlib.h>
  14. #define MAX( x, y ) ( (x)>(y) ? (x) : (y) )
  15. #define MIN( x, y ) ( (x)<(y) ? (x) : (y) )
  16. Flu_Choice_Group :: Flu_Choice_Group( int x, int y, int w, int h, const char *l )
  17. : Fl_Group( x, y, w, h )
  18. {
  19. selected = NULL;
  20. choice = new Fl_Choice( 0, 0, 0, 0 );
  21. choice->callback( _choiceCB, this );
  22. box( FL_EMBOSSED_BOX );
  23. align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  24. }
  25. void Flu_Choice_Group :: choiceCB()
  26. {
  27. value( choice->value() );
  28. do_callback();
  29. }
  30. int Flu_Choice_Group :: value()
  31. {
  32. if( children() == 1 )
  33. return -1;
  34. for( int i = 1; i < children(); i++ )
  35. if( child(i) == selected )
  36. return i-1;
  37. return -1;
  38. }
  39. void Flu_Choice_Group :: value( int v )
  40. {
  41. v++;
  42. if( v >= 1 && v < children() )
  43. value( child(v) );
  44. }
  45. int Flu_Choice_Group :: value( Fl_Widget *newvalue )
  46. {
  47. int ret = -1;
  48. selected = NULL;
  49. choice->clear();
  50. for( int i = 1; i < children(); i++ )
  51. {
  52. choice->add( child(i)->label() );
  53. child(i)->labeltype( FL_NO_LABEL );
  54. if( child(i) == newvalue )
  55. {
  56. ret = i-1;
  57. child(i)->show();
  58. choice->value( ret );
  59. selected = child(i);
  60. }
  61. else
  62. child(i)->hide();
  63. child(i)->redraw();
  64. }
  65. redraw();
  66. if( parent() )
  67. parent()->redraw();
  68. return ret;
  69. }
  70. void Flu_Choice_Group :: draw()
  71. {
  72. int i;
  73. // make sure the selected child is still a child
  74. bool found = false;
  75. for( i = 1; i < children(); i++ )
  76. if( child(i) == selected )
  77. {
  78. found = true;
  79. break;
  80. }
  81. if( !found )
  82. selected = NULL;
  83. if( !selected && children() > 1 )
  84. value( child(1) );
  85. int lblW = 0, lblH = 0, X;
  86. for( i = 1; i < children(); i++ )
  87. {
  88. int W = 0, H;
  89. fl_measure( child(i)->label(), W, H );
  90. if( W > lblW )
  91. lblW = W;
  92. if( H > lblH )
  93. lblH = H;
  94. }
  95. lblW += 26;
  96. lblH += 6;
  97. // align the label
  98. if( align() & FL_ALIGN_LEFT )
  99. X = 4;
  100. else if( align() & FL_ALIGN_RIGHT )
  101. X = w() - lblW - 8;
  102. else
  103. X = w()/2 - lblW/2 - 2;
  104. // draw the main group box
  105. if( damage() & ~FL_DAMAGE_CHILD )
  106. fl_draw_box( box(), x(), y()+lblH/2, w(), h()-lblH/2, color() );
  107. // clip and draw the children
  108. choice->resize( choice->x(), choice->y(), 0, 0 );
  109. fl_clip( x()+2, y()+lblH+1, w()-4, h()-lblH-3 );
  110. draw_children();
  111. fl_pop_clip();
  112. // clear behind the button and draw it
  113. fl_color( color() );
  114. fl_rectf( x()+X, y(), lblW+4, lblH );
  115. fl_color( labelcolor() );
  116. choice->resize( x()+X+2, y(), lblW, lblH );
  117. draw_child( *choice );
  118. }