maxgui_templates.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // FLTK C++ Templates for MaxGUI
  2. // Sebastian Hollington 2009
  3. #include <FL/Fl.H>
  4. #include <FL/Fl_Widget.H>
  5. // Globals (from fltkglue.cpp)
  6. extern int eventid;
  7. extern int(*mousehandler)(Fl_Widget*,void*);
  8. extern int(*keyhandler)(Fl_Widget*,void*);
  9. extern int(*textfilter)(void*);
  10. extern "C" int (flEventButton)();
  11. // Wrap around an FLTK widget if you want widget to call mouse/keyhandlers.
  12. template <class FLWidget> class MaxGUIEventListener : public FLWidget
  13. {
  14. public:
  15. MaxGUIEventListener (int x, int y, int w, int h, char *title = 0) : FLWidget(x,y,w,h,title) {};
  16. int handle(int event){
  17. int res = FLWidget::handle(event);
  18. int active = this->active_r();
  19. if((!active) || (event==FL_FOCUS)) return active;
  20. Fl_Widget* blwmouse = Fl::belowmouse();
  21. Fl_Widget* focus = Fl::focus();
  22. // If a non-MaxGUI widget has snatched mouse tracking, snatch it back.
  23. if(!(blwmouse && blwmouse->user_data())){
  24. blwmouse = this;
  25. Fl::belowmouse(this);
  26. }
  27. // If a non-MaxGUI widget has keyboard focus, pretend we have it.
  28. if(!(focus && focus->user_data())) focus = this;
  29. eventid = event;
  30. switch(event){
  31. case FL_PUSH:
  32. case FL_ENTER:
  33. case FL_MOVE:
  34. case FL_LEAVE:
  35. case FL_MOUSEWHEEL:
  36. case FL_RELEASE:
  37. case FL_DRAG:
  38. // Only call if child gadget has handled event.
  39. if((blwmouse==this) || !res) mousehandler( this, this->user_data() );
  40. res = 1;
  41. break;
  42. case FL_KEYUP:
  43. // Return 1 from FL_KEYUP to avoid multiple events firing.
  44. res = 1;
  45. case FL_KEYDOWN:
  46. // Leave default return value for FL_KEYDOWN to allow tab/arrow key focus changing.
  47. if(focus==this) keyhandler( focus, focus->user_data() );
  48. break;
  49. default:
  50. res = 0;
  51. }
  52. eventid = 0;
  53. return res;
  54. }
  55. };
  56. // Wrap around an FLTK widget if you want keys to be filtered before handling using textfilter callback.
  57. template <class FLWidget> class MaxGUIKeyFilter : public FLWidget
  58. {
  59. public:
  60. MaxGUIKeyFilter (int x, int y, int w, int h, char *title = 0) : FLWidget(x,y,w,h,title) {};
  61. int handle(int event)
  62. {
  63. int should_callback = 0;
  64. switch (event){
  65. case FL_KEYDOWN:
  66. if (textfilter(this->user_data())==0) return 1;
  67. should_callback = 1;
  68. break;
  69. }
  70. int res = FLWidget::handle(event);
  71. if (should_callback) FLWidget::do_callback();
  72. return res;
  73. }
  74. };
  75. // Wrap around any text display widgets to make them behave more like TextArea's on other platforms
  76. template <class FLTextDisplay> class MaxGUITextArea : public FLTextDisplay
  77. {
  78. public:
  79. MaxGUITextArea(int x,int y,int w,int h,const char *title=0):FLTextDisplay(x,y,w,h,title){}
  80. void pos_to_xy(int pos, int *x, int *y)
  81. {
  82. FLTextDisplay::position_to_xy(pos,x,y);
  83. }
  84. int handle(int event)
  85. {
  86. int should_callback = 0, should_ignore = 0, res = 0;
  87. switch (event)
  88. {
  89. case FL_PUSH:
  90. case FL_RELEASE:
  91. should_callback=1;
  92. if(flEventButton()!=FL_LEFT_MOUSE){should_ignore=1;res=1;}
  93. break;
  94. case FL_UNFOCUS:
  95. should_callback=1;
  96. break;
  97. }
  98. if (!should_ignore) res=FLTextDisplay::handle(event);
  99. if (should_callback) FLTextDisplay::do_callback();
  100. return res;
  101. }
  102. };
  103. // Wrap around an FLTK widget if you want widget background to lighten when hovered over.
  104. template <class FLWidget> class MaxGUIHoverEffect : public FLWidget
  105. {
  106. private:
  107. Fl_Color defaultColor, selectionColor;
  108. public:
  109. MaxGUIHoverEffect (int x, int y, int w, int h, char *title = 0):FLWidget(x,y,w,h,title)
  110. {
  111. defaultColor = FLWidget::color();
  112. selectionColor = FLWidget::selection_color();
  113. }
  114. Fl_Color color() {return defaultColor;}
  115. Fl_Color selection_color() {return selectionColor;}
  116. int handle(int event)
  117. {
  118. if( !this->active_r() )
  119. return FLWidget::handle( event );
  120. switch (event)
  121. {
  122. case FL_ENTER:
  123. defaultColor = FLWidget::color();
  124. FLWidget::color( fl_lighter( defaultColor ) );
  125. selectionColor = FLWidget::selection_color();
  126. FLWidget::selection_color( fl_lighter( selectionColor ) );
  127. this->redraw();
  128. return 1;
  129. break;
  130. case FL_LEAVE:
  131. FLWidget::color( defaultColor );
  132. FLWidget::selection_color( selectionColor );
  133. this->redraw();
  134. return 1;
  135. break;
  136. }
  137. return FLWidget::handle(event);
  138. }
  139. };