FLTKPropertyItemRow.cxx 9.4 KB

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