FLTKPropertyNameButtonWithHelp.cxx 4.7 KB

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