FLTKPropertyItemRow.cxx 8.8 KB

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