Flu_Button.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // $Id: Flu_Button.cpp,v 1.13 2004/05/24 19:01:05 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 <FL/Fl.H>
  13. #include "FLU/Flu_Button.H"
  14. #include <FL/fl_draw.H>
  15. // taken explicitly from Fl_Return_Button.cpp
  16. static int flu_return_arrow(int x, int y, int w, int h)
  17. {
  18. int size = w; if (h<size) size = h;
  19. int d = (size+2)/4; if (d<3) d = 3;
  20. int t = (size+9)/12; if (t<1) t = 1;
  21. int x0 = x+(w-2*d-2*t-1)/2;
  22. int x1 = x0+d;
  23. int y0 = y+h/2;
  24. fl_color(FL_LIGHT3);
  25. fl_line(x0, y0, x1, y0+d);
  26. fl_yxline(x1, y0+d, y0+t, x1+d+2*t, y0-d);
  27. fl_yxline(x1, y0-t, y0-d);
  28. fl_color(fl_gray_ramp(0));
  29. fl_line(x0, y0, x1, y0-d);
  30. fl_color(FL_DARK3);
  31. fl_xyline(x1+1, y0-t, x1+d, y0-d, x1+d+2*t);
  32. return 1;
  33. }
  34. Flu_Button :: Flu_Button( int X,int Y,int W,int H,const char *l )
  35. : Fl_Button( X,Y,W,H,l )
  36. {
  37. inactiveImg = NULL;
  38. color( FL_GRAY );
  39. selection_color( FL_GRAY );
  40. retBtn = linkBtn = false;
  41. hover = false;
  42. eBox = FL_NO_BOX;
  43. }
  44. Flu_Button :: ~Flu_Button()
  45. {
  46. if( inactiveImg )
  47. delete inactiveImg;
  48. }
  49. void Flu_Button :: checkLink()
  50. {
  51. // change the cursor if the mouse is over the link
  52. // the 'hover' variable reduces the number of times fl_cursor needs to be called (since it can be expensive)
  53. if( linkBtn )
  54. {
  55. if( Fl::event_inside( x()+labelSize[0], y()+labelSize[1], labelSize[2], labelSize[3] ) )
  56. {
  57. if( !hover )
  58. fl_cursor( FL_CURSOR_HAND );
  59. hover = true;
  60. }
  61. else
  62. {
  63. if( hover )
  64. fl_cursor( FL_CURSOR_DEFAULT );
  65. hover = false;
  66. }
  67. }
  68. }
  69. int Flu_Button :: handle( int event )
  70. {
  71. if( !active_r() )
  72. return Fl_Button::handle( event );
  73. switch( event )
  74. {
  75. case FL_MOVE:
  76. {
  77. checkLink();
  78. return 1;
  79. }
  80. case FL_ENTER:
  81. {
  82. if( active() )
  83. {
  84. Fl_Button::color( fl_lighter( col ) );
  85. Fl_Button::selection_color( fl_lighter( sCol ) );
  86. }
  87. checkLink();
  88. redraw();
  89. return 1;
  90. }
  91. break;
  92. case FL_LEAVE:
  93. {
  94. Fl_Button::color( col );
  95. Fl_Button::selection_color( sCol );
  96. checkLink();
  97. redraw();
  98. return 1;
  99. break;
  100. }
  101. }
  102. if( retBtn )
  103. {
  104. if( event == FL_SHORTCUT &&
  105. (Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter))
  106. {
  107. do_callback();
  108. return 1;
  109. }
  110. }
  111. return Fl_Button::handle( event );
  112. }
  113. // modified explicitly from Fl_Return_Button.cpp
  114. void Flu_Button :: draw()
  115. {
  116. if( type() == FL_HIDDEN_BUTTON )
  117. return;
  118. if( !active() )
  119. Fl_Button::color( col );
  120. // draw the link text
  121. if( linkBtn )
  122. {
  123. fl_draw_box( box(), x(), y(), w(), h(), color() );
  124. labelSize[0] = labelSize[1] = labelSize[2] = labelSize[3] = 0;
  125. fl_font( labelfont(), labelsize() );
  126. fl_measure( label(), labelSize[2], labelSize[3], 1 );
  127. labelSize[0] += 2;
  128. labelSize[1] += h()/2 - labelsize()/2 - 2;
  129. fl_color( labelcolor() );
  130. fl_draw( label(), x()+labelSize[0], y()+labelSize[1],
  131. labelSize[2], labelSize[3], FL_ALIGN_LEFT );
  132. if( !overLink || ( overLink && hover ) )
  133. {
  134. fl_line_style( FL_SOLID );
  135. fl_line( x()+labelSize[0], y()+labelSize[1]+labelSize[3]-2,
  136. x()+labelSize[0]+labelSize[2], y()+labelSize[1]+labelSize[3]-2 );
  137. fl_line_style( 0 );
  138. }
  139. return;
  140. }
  141. const char *lbl = label();
  142. if( retBtn )
  143. label("");
  144. if( eBox != FL_NO_BOX && Fl::belowmouse() == this && active() )
  145. {
  146. Fl_Boxtype oldbox = box();
  147. box( eBox );
  148. Fl_Button::draw();
  149. box( oldbox );
  150. }
  151. else
  152. Fl_Button::draw();
  153. if( retBtn )
  154. {
  155. int W = h();
  156. if (w()/3 < W) W = w()/3;
  157. flu_return_arrow(x()+w()-W-4, y(), W, h());
  158. label( lbl );
  159. draw_label(x(), y(), w()-W+4, h());
  160. }
  161. }
  162. void Flu_Button :: image( Fl_Image *i )
  163. {
  164. if( inactiveImg )
  165. delete inactiveImg;
  166. inactiveImg = NULL;
  167. if( i )
  168. {
  169. if( labeltype()!=FL_NORMAL_LABEL )
  170. labeltype( FL_NORMAL_LABEL );
  171. Fl_Button::image( i );
  172. inactiveImg = Fl_Button::image()->copy();
  173. inactiveImg->desaturate();
  174. Fl_Button::deimage( inactiveImg );
  175. }
  176. }