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.

148 lines
2.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "Fl_TransBox.h"
  2. #define DEFAULT_ALPHA 0x7F
  3. Fl_TransBox::Fl_TransBox(int x, int y, int w, int h, char* l)
  4. : Fl_Box(x, y, w, h) ,
  5. alpha(DEFAULT_ALPHA),
  6. dragEnabled(false),
  7. dispimg(NULL),
  8. buffer(NULL)
  9. {
  10. box(FL_NO_BOX);
  11. label(l);
  12. buffer = new unsigned char[4*w*h];
  13. img = new Fl_RGB_Image(buffer, w, h, 4);
  14. image(img);
  15. color(FL_BLACK);
  16. }
  17. Fl_TransBox::~Fl_TransBox()
  18. {
  19. if ( buffer != NULL )
  20. {
  21. delete[] buffer;
  22. }
  23. }
  24. void Fl_TransBox::color(Fl_Color c)
  25. {
  26. r = (unsigned char) (c >> 24);
  27. g = (unsigned char) (c >> 16);
  28. b = (unsigned char) (c >> 8);
  29. fill_buffer();
  30. img->uncache();
  31. }
  32. void Fl_TransBox::set_alpha(unsigned char a)
  33. {
  34. alpha = a;
  35. fill_buffer();
  36. img->uncache();
  37. }
  38. int Fl_TransBox::handle(int e)
  39. {
  40. // allows to drag the TransparentBox
  41. static int xn;
  42. static int yn;
  43. switch( e )
  44. {
  45. case FL_PUSH:
  46. xn = Fl::event_x() - x();
  47. yn = Fl::event_y() - y();
  48. return 1;
  49. case FL_DRAG:
  50. if ( dragEnabled == true )
  51. {
  52. position(Fl::event_x() - xn, Fl::event_y() - yn);
  53. window()->redraw();
  54. }
  55. return 1;
  56. case FL_RELEASE:
  57. return 1;
  58. default:
  59. return 0;
  60. }
  61. }
  62. void Fl_TransBox::fill_buffer()
  63. {
  64. unsigned char *p = buffer;
  65. for (int cnt=0; cnt<( 4*w()*h() ); cnt+=4)
  66. {
  67. *p++ = r;
  68. *p++ = g;
  69. *p++ = b;
  70. *p++ = alpha;
  71. }
  72. }
  73. static bool isInResize = false;
  74. void Fl_TransBox::resize(int x, int y, int w, int h)
  75. {
  76. Fl_Box::resize(x,y,w,h);
  77. if ( isInResize == true )
  78. return;
  79. if ( image() != NULL )
  80. {
  81. isInResize = true;
  82. image( NULL );
  83. if ( buffer != NULL )
  84. {
  85. delete[] buffer;
  86. buffer = NULL;
  87. }
  88. buffer = new unsigned char[4*w*h];
  89. if ( img != NULL )
  90. {
  91. delete img;
  92. img = NULL;
  93. }
  94. img = new Fl_RGB_Image(buffer, w, h, 4);
  95. fill_buffer();
  96. img->uncache();
  97. image( img );
  98. isInResize = false;
  99. }
  100. }
  101. void Fl_TransBox::draw()
  102. {
  103. if ( image() == NULL )
  104. return;
  105. fl_push_clip( x(), y(), w(), h() );
  106. Fl_Box::draw();
  107. if ( dispimg != NULL )
  108. {
  109. int putX = ( w() - dispimg->w() ) / 2;
  110. int putY = ( h() - dispimg->h() ) / 2;
  111. dispimg->draw(x() + putX, y() + putY);
  112. }
  113. fl_pop_clip();
  114. }