FLTKPropertyItemRow.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "FLTKPropertyItemRow.h"
  2. #include <FL/Fl.H>
  3. #include <FL/Fl_Window.H>
  4. #include <FL/Fl_Button.H>
  5. #include <FL/Fl_Box.H>
  6. #include <FL/Fl_Input.H>
  7. #include <FL/Fl_Tile.H>
  8. #include <FL/fl_ask.H>
  9. #include <FL/fl_file_chooser.H>
  10. #include <FL/Fl_Color_Chooser.H>
  11. #include <FL/Fl_Menu_Button.H>
  12. #include "../cmCacheManager.h"
  13. #include "FLTKPropertyList.h"
  14. #include "CMakeSetupGUIImplementation.h"
  15. #include <cstdio>
  16. namespace fltk {
  17. CMakeSetupGUIImplementation * PropertyItemRow::m_CMakeSetup = 0;
  18. PropertyItemRow
  19. ::PropertyItemRow( PropertyItem * pItem ):Fl_Tile(0,0,10,10,"")
  20. {
  21. m_PropertyItem = pItem;
  22. m_ItemValue = new ItemValue;
  23. const unsigned int fontsize = 11;
  24. const unsigned int nameWidth = 200;
  25. const unsigned int textWidth = 1400;
  26. const unsigned int checkWidth = textWidth;
  27. const unsigned int browseWidth = 20;
  28. const unsigned int firstColumn = 0;
  29. const unsigned int secondColumn = nameWidth;
  30. const unsigned int rowHeight = 20;
  31. const unsigned int rowSpacing = 20;
  32. size( nameWidth + textWidth , rowHeight );
  33. // Make the parent Fl_Pack widget at least a row wide.
  34. parent()->size( nameWidth + textWidth , rowHeight );
  35. m_NameButton = new
  36. Fl_Button( firstColumn, 0, nameWidth, rowHeight,
  37. m_PropertyItem->m_propName.c_str() );
  38. m_NameButton->align( FL_ALIGN_CLIP | FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  39. m_NameButton->labelsize( fontsize );
  40. m_NameButton->box( FL_DOWN_BOX );
  41. m_NameButton->size( secondColumn, rowHeight );
  42. m_NameButton->callback( NameButtonCallback, (void *)m_PropertyItem );
  43. switch( m_PropertyItem->m_nItemType )
  44. {
  45. case PropertyList::COMBO:
  46. {
  47. break;
  48. }
  49. case PropertyList::EDIT:
  50. {
  51. Fl_Input * input = new
  52. Fl_Input( secondColumn, 0, textWidth ,rowHeight ,"");
  53. input->value( m_PropertyItem->m_curValue.c_str() );
  54. input->textsize( fontsize );
  55. input->callback( InputTextCallback, (void *)m_PropertyItem );
  56. input->when( FL_WHEN_CHANGED );
  57. break;
  58. }
  59. case PropertyList::COLOR:
  60. {
  61. Fl_Button * colorButton = new
  62. Fl_Button( secondColumn, 0, textWidth ,rowHeight ,"");
  63. colorButton->labelsize( fontsize );
  64. //colorButton->color();
  65. colorButton->callback( ColorSelectionCallback, (void *)m_PropertyItem );
  66. break;
  67. }
  68. case PropertyList::FILE:
  69. {
  70. Fl_Button * browseButton = new
  71. Fl_Button( secondColumn, 0, browseWidth ,rowHeight ,"...");
  72. browseButton->labelsize( fontsize );
  73. Fl_Input * input = new
  74. Fl_Input( secondColumn+browseWidth, 0, textWidth ,rowHeight ,"");
  75. input->value( m_PropertyItem->m_curValue.c_str() );
  76. input->textsize( fontsize );
  77. m_ItemValue->m_InputText = input;
  78. m_ItemValue->m_PropertyItem = m_PropertyItem;
  79. browseButton->callback( BrowsePathCallback, (void *)m_ItemValue );
  80. input->callback( InputTextCallback, m_PropertyItem );
  81. input->when( FL_WHEN_CHANGED );
  82. break;
  83. }
  84. case PropertyList::CHECKBOX:
  85. {
  86. Fl_Button * button = new
  87. Fl_Button( secondColumn, 0, checkWidth ,rowHeight ,"");
  88. button->align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT );
  89. button->callback( CheckButtonCallback, (void *)m_PropertyItem );
  90. if( m_PropertyItem->m_curValue == "ON" )
  91. {
  92. button->label(" ON ");
  93. button->value(1);
  94. }
  95. else if( m_PropertyItem->m_curValue == "OFF" )
  96. {
  97. button->label(" OFF ");
  98. button->value(0);
  99. }
  100. button->type( FL_TOGGLE_BUTTON );
  101. button->labelsize( fontsize );
  102. break;
  103. }
  104. case PropertyList::PATH:
  105. {
  106. Fl_Button * browseButton = new
  107. Fl_Button( secondColumn, 0, browseWidth ,rowHeight ,"...");
  108. browseButton->labelsize( fontsize );
  109. Fl_Input * input = new
  110. Fl_Input( secondColumn+browseWidth, 0, textWidth ,rowHeight ,"");
  111. input->value( m_PropertyItem->m_curValue.c_str() );
  112. input->textsize( fontsize );
  113. m_ItemValue->m_InputText = input;
  114. m_ItemValue->m_PropertyItem = m_PropertyItem;
  115. browseButton->callback( BrowsePathCallback, (void *)m_ItemValue );
  116. input->callback( InputTextCallback, (void *)m_PropertyItem );
  117. input->when( FL_WHEN_CHANGED );
  118. break;
  119. }
  120. break;
  121. default:
  122. fl_alert("Unkown item type %d",m_PropertyItem->m_nItemType);
  123. break;
  124. }
  125. end(); // Close the inclusion of widgets in the Tile object
  126. }
  127. PropertyItemRow::~PropertyItemRow( )
  128. {
  129. delete m_ItemValue;
  130. }
  131. void PropertyItemRow
  132. ::SetCMakeSetupGUI( CMakeSetupGUIImplementation * cmakeSetup )
  133. {
  134. m_CMakeSetup = cmakeSetup;
  135. }
  136. void
  137. PropertyItemRow::
  138. NameButtonCallback( Fl_Widget * widget, void * data)
  139. {
  140. Fl_Button * button = (Fl_Button *)widget;
  141. PropertyItem * pItem = (PropertyItem *)data;
  142. static Fl_Menu_Button * popupMenu = 0;
  143. if( !popupMenu )
  144. {
  145. int lastMousePositionX = Fl::event_x();
  146. int lastMousePositionY = Fl::event_y();
  147. popupMenu = new Fl_Menu_Button(lastMousePositionX,
  148. lastMousePositionY,100,200);
  149. }
  150. popupMenu->type( Fl_Menu_Button::POPUP3 );
  151. popupMenu->add("Help|Remove|Properties...");
  152. popupMenu->popup();
  153. typedef enum {
  154. HELP=0,
  155. REMOVE,
  156. PROPERTIES
  157. } MenuOptions;
  158. switch( popupMenu->value() )
  159. {
  160. case HELP:
  161. fl_message( pItem->m_HelpString.c_str() );
  162. break;
  163. case REMOVE: // Remove
  164. {
  165. const char * propertyName = pItem->m_propName.c_str();
  166. int answer = fl_ask( "Do you want to remove property %s", propertyName );
  167. if( answer == 1 )
  168. {
  169. // Remove the entry from the cache
  170. cmCacheManager::GetInstance()->RemoveCacheEntry( propertyName );
  171. // Get the parent: Fl_Tile that manages the whole row in the GUI
  172. Fl_Group * parentGroup = dynamic_cast<Fl_Group *>(button->parent());
  173. // Get the grandParent: Fl_Pack with the property list
  174. Fl_Group * grandParentGroup = dynamic_cast<Fl_Group *>( parentGroup->parent() );
  175. // Remove the row from the list
  176. grandParentGroup->remove( *parentGroup );
  177. // Destroy the row
  178. delete parentGroup; // Patricide... ?
  179. // Redraw the list
  180. grandParentGroup->redraw();
  181. FillCacheManagerFromCacheGUI();
  182. return;
  183. }
  184. break;
  185. }
  186. case PROPERTIES: // Properties
  187. break;
  188. }
  189. }
  190. void
  191. PropertyItemRow::
  192. FillCacheManagerFromCacheGUI( void )
  193. {
  194. if( m_CMakeSetup )
  195. {
  196. m_CMakeSetup->FillCacheManagerFromCacheGUI();
  197. }
  198. }
  199. void
  200. PropertyItemRow::
  201. CheckButtonCallback( Fl_Widget * widget, void * data)
  202. {
  203. Fl_Button * button = (Fl_Button *)widget;
  204. PropertyItem * pItem = (PropertyItem *)data;
  205. int value = button->value();
  206. if( value )
  207. {
  208. button->label(" ON ");
  209. pItem->m_curValue = "ON";
  210. }
  211. else
  212. {
  213. button->label(" OFF ");
  214. pItem->m_curValue = "OFF";
  215. }
  216. button->redraw();
  217. FillCacheManagerFromCacheGUI();
  218. }
  219. void
  220. PropertyItemRow::
  221. InputTextCallback( Fl_Widget * widget, void * data)
  222. {
  223. Fl_Input * input = (Fl_Input *)widget;
  224. PropertyItem * item = (PropertyItem *)data;
  225. item->m_curValue = input->value();
  226. FillCacheManagerFromCacheGUI();
  227. }
  228. void
  229. PropertyItemRow::
  230. ColorSelectionCallback( Fl_Widget * widget, void * data)
  231. {
  232. Fl_Button * colorButton = (Fl_Button *)widget;
  233. PropertyItem * propertyItem = (PropertyItem *)data;
  234. static Fl_Color colorIndex = FL_FREE_COLOR;
  235. unsigned char red = 0;
  236. unsigned char blue = 0;
  237. unsigned char green = 0;
  238. fl_color_chooser("Please pick a color",red,green,blue);
  239. char buffer[300];
  240. sprintf( buffer,"RGB(%d,%d,%d)", red, green, blue );
  241. propertyItem->m_curValue = buffer;
  242. Fl::set_color( colorIndex, red, green, blue );
  243. colorButton->color( colorIndex );
  244. colorIndex = (Fl_Color)( colorIndex + 1 );
  245. if( colorIndex == FL_FREE_COLOR + FL_NUM_FREE_COLOR )
  246. {
  247. fl_alert("Maximum number of free colors used, recycling...");
  248. colorIndex = FL_FREE_COLOR;
  249. }
  250. colorButton->redraw();
  251. FillCacheManagerFromCacheGUI();
  252. }
  253. void
  254. PropertyItemRow::
  255. BrowsePathCallback( Fl_Widget * widget, void * data)
  256. {
  257. ItemValue * itemValue = (ItemValue *)data;
  258. Fl_Input * inputText = itemValue->m_InputText;
  259. PropertyItem * propertyItem = itemValue->m_PropertyItem;
  260. const char * newpath =
  261. fl_file_chooser("Select a path","*", inputText->value() );
  262. if( newpath )
  263. {
  264. propertyItem->m_curValue = newpath;
  265. inputText->value( newpath );
  266. }
  267. FillCacheManagerFromCacheGUI();
  268. }
  269. } // end namespace fltk