You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
3.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "Fl_MRender.h"
  2. static char str_no_source[] = "NO SOURCE";
  3. Fl_MRender::Fl_MRender(int X, int Y, int W, int H, const char *l)
  4. : Fl_Box(X,Y,W,H,l),
  5. _bdraw( true ),
  6. _putx( 0 ),
  7. _puty( 0 ),
  8. _putw( 0 ),
  9. _puth( 0 ),
  10. _ostr( str_no_source ),
  11. _eventh( NULL ),
  12. _cachedimg( NULL )
  13. {
  14. box( FL_THIN_DOWN_BOX );//FL_NO_BOX );
  15. color( FL_BLACK );
  16. }
  17. Fl_MRender::~Fl_MRender(){
  18. }
  19. void Fl_MRender::eventhandler( Fl_MRender_Event* p ){
  20. _eventh = p;
  21. }
  22. void Fl_MRender::draw()
  23. {
  24. fl_push_clip( x(), y(), w(), h() );
  25. //Fl_Box::draw();
  26. if ( _bdraw == true )
  27. {
  28. Fl_Image* refimg = _cachedimg;
  29. if ( refimg == NULL )
  30. {
  31. refimg = Fl_Box::image();
  32. }
  33. if ( ( refimg != NULL ) && ( _putw > 0 ) && ( _puth > 0 ) )
  34. {
  35. refimg->draw( _putx, _puty );
  36. }
  37. }
  38. #ifdef DEBUG
  39. fl_color( FL_RED );
  40. fl_rect( _putx, _puty, _putw, _puth );
  41. #endif
  42. fl_pop_clip();
  43. }
  44. void Fl_MRender::resize(int x, int y, int w, int h)
  45. {
  46. Fl_Box::resize(x,y,w,h);
  47. if ( _isInResize == true )
  48. return;
  49. recalcimg( w, h );
  50. #ifdef DEBUG
  51. printf("--- putting image resized as : %d x %d\n", _putw, _puth );
  52. #endif
  53. }
  54. void Fl_MRender::image( Fl_Image* img )
  55. {
  56. if ( _cachedimg != NULL )
  57. {
  58. delete _cachedimg;
  59. _cachedimg = NULL;
  60. }
  61. Fl_Box::image( img );
  62. if ( img != NULL )
  63. {
  64. _orgw = img->w();
  65. _orgh = img->h();
  66. _putx = 0;
  67. _puty = 0;
  68. _putw = _orgw;
  69. _puth = _putw;
  70. recalcimg( w(), h() );
  71. }
  72. else
  73. {
  74. _orgw = 0;
  75. _orgh = 0;
  76. _putx = 0;
  77. _puty = 0;
  78. _putw = 0;
  79. _puth = 0;
  80. }
  81. }
  82. void Fl_MRender::recalcimg(int w, int h)
  83. {
  84. _bdraw = false;
  85. if ( image() != NULL )
  86. {
  87. _isInResize = true;
  88. Fl_Image* refimg = image();
  89. if ( refimg != NULL )
  90. {
  91. if ( ( _orgw == w ) && _orgh == h )
  92. {
  93. if ( _cachedimg != NULL )
  94. {
  95. delete _cachedimg;
  96. _cachedimg = NULL;
  97. }
  98. _putw = _orgw;
  99. _puth = _orgh;
  100. if ( _puty < h )
  101. {
  102. _puty = ( h - _puth ) / 2;
  103. }
  104. if ( _putw < w )
  105. {
  106. _putx = ( w - _putw ) / 2;
  107. }
  108. }
  109. else
  110. {
  111. float ratio = 0.0f;
  112. // check who is shorter ?
  113. if ( _orgw != w )
  114. {
  115. ratio = float( w ) / float( _orgw );
  116. if ( ratio > 0.0f )
  117. {
  118. _putw = float( _orgw ) * ratio;
  119. _puth = float( _orgh ) * ratio;
  120. }
  121. }
  122. if ( ( _orgh != h ) && ( _puth >= h ) )
  123. {
  124. ratio = float( h ) / float( _orgh );
  125. if ( ratio > 0.0f )
  126. {
  127. _putw = float( _orgw ) * ratio;
  128. _puth = float( _orgh ) * ratio;
  129. }
  130. }
  131. if ( _putw < w )
  132. {
  133. _putx = ( w - _putw ) / 2;
  134. }
  135. if ( _puty < h )
  136. {
  137. _puty = ( h - _puth ) / 2;
  138. }
  139. if ( _cachedimg != NULL )
  140. {
  141. delete _cachedimg;
  142. _cachedimg = NULL;
  143. }
  144. if ( ( _putw > 0 ) && ( _puth > 0 ) )
  145. {
  146. _cachedimg = refimg->copy( _putw, _puth );
  147. }
  148. }
  149. }
  150. _isInResize = false;
  151. }
  152. _bdraw = true;
  153. }
  154. int Fl_MRender::handle(int h)
  155. {
  156. if ( _eventh != NULL )
  157. {
  158. return _eventh->event( this, h );
  159. }
  160. return 0;
  161. }