FLTKPropertyNameButtonWithHelp.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // #include <FLTKPropertyNameButtonWithHelp.h>
  2. #include <Fl/Fl.H>
  3. #include <Fl/fl_ask.H>
  4. #include <Fl/Fl_Menu_Button.H>
  5. #include "../cmCacheManager.h"
  6. #include "FLTKPropertyNameButtonWithHelp.h"
  7. namespace fltk {
  8. Fl_Window * PropertyNameButtonWithHelp::helpBlob = 0;
  9. Fl_Box * PropertyNameButtonWithHelp::helpText = 0;
  10. unsigned int PropertyNameButtonWithHelp::counter = 0;
  11. int PropertyNameButtonWithHelp::lastMousePositionX = 0;
  12. int PropertyNameButtonWithHelp::lastMousePositionY = 0;
  13. PropertyNameButtonWithHelp
  14. ::PropertyNameButtonWithHelp(int x,int y, int w, int h, const char * l):
  15. Fl_Button(x,y,w,h,l)
  16. {
  17. counter++; // one more object instantiated
  18. }
  19. PropertyNameButtonWithHelp::
  20. ~PropertyNameButtonWithHelp( )
  21. {
  22. counter--;
  23. if( counter == 0 )
  24. {
  25. delete helpBlob;
  26. delete helpText;
  27. }
  28. }
  29. void
  30. PropertyNameButtonWithHelp
  31. ::SetHelpText( const char * text )
  32. {
  33. m_HelpText = text;
  34. }
  35. void
  36. PropertyNameButtonWithHelp
  37. ::ShowHelp( void )
  38. {
  39. if( helpBlob )
  40. {
  41. helpBlob->show();
  42. }
  43. }
  44. void
  45. PropertyNameButtonWithHelp
  46. ::HideHelp( void )
  47. {
  48. if( helpBlob )
  49. {
  50. helpBlob->hide();
  51. }
  52. }
  53. int
  54. PropertyNameButtonWithHelp::
  55. handle( int event )
  56. {
  57. static bool helpBlobVisible = false;
  58. const float delayForShowingHelpBlob = 1.0; // seconds
  59. const int maxWidth = 300;
  60. const int lineHeight = 20;
  61. // Create the help blob window if it doesn't exist
  62. if( !helpBlob )
  63. {
  64. helpBlob = new Fl_Window(0,0,200,20,"");
  65. helpBlob->border( 0 );
  66. helpText = new Fl_Box(0,0,200,20,"");
  67. helpBlob->end();
  68. Fl_Color yellowHelp = FL_YELLOW;
  69. helpBlob->color( yellowHelp );
  70. helpText->color( yellowHelp );
  71. helpText->align( FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_WRAP );
  72. }
  73. int eventManaged = 0;
  74. switch( event )
  75. {
  76. case FL_ENTER:
  77. {
  78. lastMousePositionX = Fl::event_x();
  79. lastMousePositionY = Fl::event_y();
  80. const float factor = helpText->labelsize() * 0.5;
  81. int height = lineHeight;
  82. int area = (int)( m_HelpText.size() * factor );
  83. int width = area;
  84. if( width > maxWidth )
  85. {
  86. width = maxWidth;
  87. height = area / maxWidth * lineHeight;
  88. if( area % maxWidth != 0 )
  89. {
  90. height += lineHeight;
  91. }
  92. }
  93. helpText->size( width, height );
  94. helpBlob->size( width, height );
  95. helpText->label( m_HelpText.c_str() );
  96. Fl_Widget * parent = this->parent();
  97. Fl::add_timeout( delayForShowingHelpBlob, ShowHelpBlobCallback, (void *)parent );
  98. helpBlobVisible = true;
  99. eventManaged = 0;
  100. break;
  101. }
  102. case FL_LEAVE:
  103. {
  104. if( helpBlobVisible )
  105. {
  106. helpBlobVisible = false;
  107. helpBlob->hide();
  108. }
  109. eventManaged = 0;
  110. break;
  111. }
  112. case FL_MOVE:
  113. if( helpBlobVisible )
  114. {
  115. helpBlobVisible = false;
  116. helpBlob->hide();
  117. }
  118. eventManaged = 0;
  119. break;
  120. case FL_PUSH:
  121. if( Fl::event_button() == FL_RIGHT_MOUSE )
  122. {
  123. PopupMenu();
  124. }
  125. eventManaged = 0;
  126. break;
  127. default:
  128. eventManaged = 0;
  129. }
  130. return eventManaged;
  131. }
  132. void
  133. PropertyNameButtonWithHelp::
  134. ShowHelpBlobCallback( void * data )
  135. {
  136. Fl_Widget * thisWidget = Fl::belowmouse();
  137. Fl_Widget * eventWidget = (Fl_Widget *)data;
  138. if( thisWidget == eventWidget )
  139. {
  140. helpBlob->position( lastMousePositionX, lastMousePositionY );
  141. helpBlob->show();
  142. }
  143. }
  144. ////////////////////////////////////////////////////////////////
  145. // This popup menu is displayed when the
  146. // right mouse button is pressed
  147. void
  148. PropertyNameButtonWithHelp::
  149. PopupMenu(void)
  150. {
  151. static Fl_Menu_Button * popupMenu = 0;
  152. if( !popupMenu )
  153. {
  154. popupMenu = new Fl_Menu_Button(0,0,100,200);
  155. }
  156. popupMenu->type( Fl_Menu_Button::POPUP3 );
  157. popupMenu->add("Remove|Properties...");
  158. popupMenu->popup();
  159. switch( popupMenu->value() )
  160. {
  161. case 0: // Remove
  162. {
  163. const char * propertyName = label();
  164. int answer = fl_ask( "Do you want to remove property %s", propertyName );
  165. if( answer == 1 )
  166. {
  167. // Remove the entry from the cache
  168. cmCacheManager::GetInstance()->RemoveCacheEntry( propertyName );
  169. // Get the parent: Fl_Tile that manages the whole row in the GUI
  170. Fl_Group * parentGroup = (Fl_Group *) parent();
  171. // Get the grandParent: Fl_Pack with the property list
  172. Fl_Group * grandParentGroup = (Fl_Group *) parentGroup->parent();
  173. // Remove the row from the list
  174. grandParentGroup->remove( *parentGroup );
  175. // Destroy the row
  176. delete parentGroup; // Patricide... ?
  177. // Redraw the list
  178. grandParentGroup->redraw();
  179. return;
  180. }
  181. break;
  182. }
  183. case 1: // Properties
  184. break;
  185. }
  186. }
  187. } // end namespace fltk