CMakeSetupGUIImplementation.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 "CMakeSetupGUIImplementation.h"
  14. #include "FL/fl_file_chooser.H"
  15. #include "FL/filename.H"
  16. #include "FL/fl_ask.H"
  17. #include "../cmCacheManager.h"
  18. #include "../cmMakefile.h"
  19. #include <iostream>
  20. #include "FLTKPropertyList.h"
  21. #include "FLTKPropertyItemRow.h"
  22. #include "FL/fl_draw.H"
  23. #include "../cmake.h"
  24. void FLTKMessageCallback(const char* message, const char* title, bool& nomore, void*)
  25. {
  26. std::string msg = message;
  27. msg += "\nPress cancel to suppress any further messages.";
  28. int choice = fl_choice( msg.c_str(), "Cancel","Ok",0);
  29. if(choice==0)
  30. {
  31. nomore = true;
  32. }
  33. }
  34. /**
  35. * Constructor
  36. */
  37. CMakeSetupGUIImplementation
  38. ::CMakeSetupGUIImplementation():m_CacheEntriesList(this)
  39. {
  40. m_CMakeInstance = new cmake;
  41. cmSystemTools::SetErrorCallback(FLTKMessageCallback);
  42. m_BuildPathChanged = false;
  43. }
  44. /**
  45. * Destructor
  46. */
  47. CMakeSetupGUIImplementation
  48. ::~CMakeSetupGUIImplementation()
  49. {
  50. }
  51. /**
  52. * Show the graphic interface
  53. */
  54. void
  55. CMakeSetupGUIImplementation
  56. ::Show( void )
  57. {
  58. dialogWindow->show();
  59. }
  60. /**
  61. * Hide the graphic interface
  62. */
  63. void
  64. CMakeSetupGUIImplementation
  65. ::Close( void )
  66. {
  67. SaveRecentDirectories();
  68. dialogWindow->hide();
  69. }
  70. /**
  71. * Browse for the path to the sources
  72. */
  73. void
  74. CMakeSetupGUIImplementation
  75. ::BrowseForSourcePath( void )
  76. {
  77. const char * path =
  78. fl_dir_chooser(
  79. "Path to Sources",
  80. sourcePathTextInput->value() );
  81. if( !path )
  82. {
  83. return;
  84. }
  85. SetSourcePath( path );
  86. }
  87. /**
  88. * Browse for the path to the binaries
  89. */
  90. void
  91. CMakeSetupGUIImplementation
  92. ::BrowseForBinaryPath( void )
  93. {
  94. const char * path =
  95. fl_dir_chooser(
  96. "Path to Binaries",
  97. binaryPathTextInput->value() );
  98. if( !path )
  99. {
  100. return;
  101. }
  102. SetBinaryPath( path );
  103. }
  104. /**
  105. * Set path to executable. Used to get the path to CMake
  106. */
  107. void
  108. CMakeSetupGUIImplementation
  109. ::SetPathToExecutable( const char * path )
  110. {
  111. m_PathToExecutable = cmSystemTools::CollapseFullPath(path);
  112. m_PathToExecutable = cmSystemTools::GetProgramPath(m_PathToExecutable.c_str()).c_str();
  113. #ifdef _WIN32
  114. m_PathToExecutable += "/cmake.exe";
  115. #else
  116. m_PathToExecutable += "/cmake";
  117. #endif
  118. }
  119. /**
  120. * Set the source path
  121. */
  122. void
  123. CMakeSetupGUIImplementation
  124. ::SetSourcePath( const char * path )
  125. {
  126. if( !path || strlen(path)==0 )
  127. {
  128. fl_alert("Please select the path to the sources");
  129. return;
  130. }
  131. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  132. sourcePathTextInput->value( expandedAbsolutePath.c_str() );
  133. if( VerifySourcePath( expandedAbsolutePath ) )
  134. {
  135. m_WhereSource = expandedAbsolutePath;
  136. }
  137. }
  138. /**
  139. * Expand environment variables in the path and make it absolute
  140. */
  141. std::string
  142. CMakeSetupGUIImplementation
  143. ::ExpandPathAndMakeItAbsolute( const std::string & inputPath ) const
  144. {
  145. return cmSystemTools::CollapseFullPath(inputPath.c_str());
  146. }
  147. /**
  148. * Set the binary path
  149. */
  150. void
  151. CMakeSetupGUIImplementation
  152. ::SetBinaryPath( const char * path )
  153. {
  154. if( !path || strlen(path)==0 )
  155. {
  156. fl_alert("Please select the path to the binaries");
  157. return;
  158. }
  159. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  160. binaryPathTextInput->value( expandedAbsolutePath.c_str() );
  161. if( !VerifyBinaryPath( expandedAbsolutePath.c_str() ) )
  162. {
  163. return;
  164. }
  165. if( m_WhereBuild != expandedAbsolutePath )
  166. {
  167. m_BuildPathChanged = true;
  168. m_WhereBuild = expandedAbsolutePath;
  169. m_CacheEntriesList.RemoveAll(); // remove data from other project
  170. this->LoadCacheFromDiskToGUI();
  171. }
  172. else
  173. {
  174. m_BuildPathChanged = false;
  175. }
  176. }
  177. /**
  178. * Verify the path to binaries
  179. */
  180. bool
  181. CMakeSetupGUIImplementation
  182. ::VerifyBinaryPath( const std::string & path ) const
  183. {
  184. bool pathIsOK = false;
  185. if( cmSystemTools::FileIsDirectory( path.c_str() ) )
  186. {
  187. pathIsOK = true;
  188. }
  189. else
  190. {
  191. int userWantsToCreateDirectory =
  192. fl_ask("The directory \n %s \n Doesn't exist. Do you want to create it ?",
  193. path.c_str() );
  194. if( userWantsToCreateDirectory )
  195. {
  196. cmSystemTools::MakeDirectory( path.c_str() );
  197. pathIsOK = true;
  198. }
  199. else
  200. {
  201. pathIsOK = false;
  202. }
  203. }
  204. return pathIsOK;
  205. }
  206. /**
  207. * Verify the path to sources
  208. */
  209. bool
  210. CMakeSetupGUIImplementation
  211. ::VerifySourcePath( const std::string & path ) const
  212. {
  213. if( !cmSystemTools::FileIsDirectory(( path.c_str())))
  214. {
  215. fl_alert("The Source directory \n %s \n Doesn't exist or is not a directory", path.c_str() );
  216. return false;
  217. }
  218. return true;
  219. }
  220. /**
  221. * Build the project files
  222. */
  223. void
  224. CMakeSetupGUIImplementation
  225. ::RunCMake( bool generateProjectFiles )
  226. {
  227. if(!cmSystemTools::FileIsDirectory( m_WhereBuild.c_str() ))
  228. {
  229. std::string message =
  230. "Build directory does not exist, should I create it?\n\n"
  231. "Directory: ";
  232. message += m_WhereBuild;
  233. int userWantToCreateDirectory =
  234. fl_ask(message.c_str());
  235. if( userWantToCreateDirectory )
  236. {
  237. cmSystemTools::MakeDirectory( m_WhereBuild.c_str() );
  238. }
  239. else
  240. {
  241. fl_alert("Build Project aborted, nothing done.");
  242. return;
  243. }
  244. }
  245. // set the wait cursor
  246. fl_cursor(FL_CURSOR_WAIT,FL_BLACK,FL_WHITE);
  247. // save the current GUI values to the cache
  248. this->SaveCacheFromGUI();
  249. // Make sure we are working from the cache on disk
  250. this->LoadCacheFromDiskToGUI();
  251. UpdateListOfRecentDirectories();
  252. SaveRecentDirectories();
  253. if (generateProjectFiles)
  254. {
  255. if(m_CMakeInstance->Generate() != 0)
  256. {
  257. cmSystemTools::Error(
  258. "Error in generation process, project files may be invalid");
  259. }
  260. }
  261. else
  262. {
  263. m_CMakeInstance->SetHomeDirectory(m_WhereSource.c_str());
  264. m_CMakeInstance->SetStartDirectory(m_WhereSource.c_str());
  265. m_CMakeInstance->SetHomeOutputDirectory(m_WhereBuild.c_str());
  266. m_CMakeInstance->SetStartOutputDirectory(m_WhereBuild.c_str());
  267. m_CMakeInstance->SetGlobalGenerator(
  268. m_CMakeInstance->CreateGlobalGenerator("NMake Makefiles"));
  269. m_CMakeInstance->SetCMakeCommand(m_PathToExecutable.c_str());
  270. m_CMakeInstance->LoadCache();
  271. if(m_CMakeInstance->Configure() != 0)
  272. {
  273. cmSystemTools::Error(
  274. "Error in configuration process, project files may be invalid");
  275. }
  276. // update the GUI with any new values in the caused by the
  277. // generation process
  278. this->LoadCacheFromDiskToGUI();
  279. }
  280. // path is up-to-date now
  281. m_BuildPathChanged = false;
  282. // put the cursor back
  283. fl_cursor(FL_CURSOR_DEFAULT,FL_BLACK,FL_WHITE);
  284. fl_message("Done !");
  285. }
  286. /**
  287. * Load Cache from disk to GUI
  288. */
  289. void
  290. CMakeSetupGUIImplementation
  291. ::LoadCacheFromDiskToGUI( void )
  292. {
  293. if( m_WhereBuild != "" )
  294. {
  295. cmCacheManager *cachem = this->m_CMakeInstance->GetCacheManager();
  296. cachem->LoadCache( m_WhereBuild.c_str() );
  297. this->FillCacheGUIFromCacheManager();
  298. }
  299. }
  300. /**
  301. * Save Cache from disk to GUI
  302. */
  303. void
  304. CMakeSetupGUIImplementation
  305. ::SaveCacheFromGUI( void )
  306. {
  307. cmCacheManager *cachem = this->m_CMakeInstance->GetCacheManager();
  308. this->FillCacheManagerFromCacheGUI();
  309. if(m_WhereBuild != "")
  310. {
  311. cachem->SaveCache(m_WhereBuild.c_str());
  312. }
  313. }
  314. /**
  315. * Fill Cache GUI from cache manager
  316. */
  317. void
  318. CMakeSetupGUIImplementation
  319. ::FillCacheGUIFromCacheManager( void )
  320. {
  321. cmCacheManager *cachem = this->m_CMakeInstance->GetCacheManager();
  322. cmCacheManager::CacheIterator it = cachem->NewIterator();
  323. size_t size = m_CacheEntriesList.GetItems().size();
  324. bool reverseOrder = false;
  325. // if there are already entries in the cache, then
  326. // put the new ones in the top, so they show up first
  327. if(size)
  328. {
  329. reverseOrder = true;
  330. }
  331. // all the current values are not new any more
  332. std::set<fltk::PropertyItem*> items = m_CacheEntriesList.GetItems();
  333. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  334. i != items.end(); ++i)
  335. {
  336. fltk::PropertyItem* item = *i;
  337. item->m_NewValue = false;
  338. }
  339. // Prepare to add rows to the FLTK scroll/pack
  340. propertyListPack->clear();
  341. propertyListPack->begin();
  342. // const cmCacheManager::CacheEntryMap &cache =
  343. // cmCacheManager::GetInstance()->GetCacheMap();
  344. // if(cache.size() == 0)
  345. // {
  346. // m_OKButton->deactivate();
  347. // }
  348. // else
  349. // {
  350. // m_OKButton->activate();
  351. // }
  352. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  353. !i.IsAtEnd(); i.Next())
  354. {
  355. const char* key = i.GetName();
  356. std::string value = i.GetValue();
  357. bool advanced = i.GetPropertyAsBool("ADVANCED");
  358. switch(i.GetType() )
  359. {
  360. case cmCacheManager::BOOL:
  361. if(cmSystemTools::IsOn(value.c_str()))
  362. {
  363. m_CacheEntriesList.AddProperty(key,
  364. "ON",
  365. i.GetProperty("HELPSTRING"),
  366. fltk::PropertyList::CHECKBOX,"",
  367. reverseOrder);
  368. }
  369. else
  370. {
  371. m_CacheEntriesList.AddProperty(key,
  372. "OFF",
  373. i.GetProperty("HELPSTRING"),
  374. fltk::PropertyList::CHECKBOX,"",
  375. reverseOrder);
  376. }
  377. break;
  378. case cmCacheManager::PATH:
  379. m_CacheEntriesList.AddProperty(key,
  380. value.c_str(),
  381. i.GetProperty("HELPSTRING"),
  382. fltk::PropertyList::PATH,"",
  383. reverseOrder);
  384. break;
  385. case cmCacheManager::FILEPATH:
  386. m_CacheEntriesList.AddProperty(key,
  387. value.c_str(),
  388. i.GetProperty("HELPSTRING"),
  389. fltk::PropertyList::FILE,"",
  390. reverseOrder);
  391. break;
  392. case cmCacheManager::STRING:
  393. m_CacheEntriesList.AddProperty(key,
  394. value.c_str(),
  395. i.GetProperty("HELPSTRING"),
  396. fltk::PropertyList::EDIT,"",
  397. reverseOrder);
  398. break;
  399. case cmCacheManager::STATIC:
  400. case cmCacheManager::INTERNAL:
  401. m_CacheEntriesList.RemoveProperty(key);
  402. break;
  403. }
  404. }
  405. // Add the old entry to the end of the pack
  406. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  407. i != items.end(); ++i)
  408. {
  409. fltk::PropertyItem* item = *i;
  410. if( !(item->m_NewValue) )
  411. {
  412. new fltk::PropertyItemRow( item ); // GUI of the old property row
  413. }
  414. }
  415. propertyListPack->end();
  416. propertyListPack->init_sizes();
  417. cacheValuesScroll->position( 0, 0 );
  418. propertyListPack->redraw();
  419. Fl::check();
  420. this->UpdateData(false);
  421. }
  422. /**
  423. * UpdateData
  424. */
  425. void
  426. CMakeSetupGUIImplementation
  427. ::UpdateData( bool option )
  428. {
  429. dialogWindow->redraw();
  430. Fl::check();
  431. }
  432. /**
  433. * Fill cache manager from Cache GUI
  434. */
  435. void
  436. CMakeSetupGUIImplementation
  437. ::FillCacheManagerFromCacheGUI( void )
  438. {
  439. cmCacheManager *cachem = this->m_CMakeInstance->GetCacheManager();
  440. cmCacheManager::CacheIterator it = cachem->NewIterator();
  441. std::set<fltk::PropertyItem*> items = m_CacheEntriesList.GetItems();
  442. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  443. i != items.end(); ++i)
  444. {
  445. fltk::PropertyItem* item = *i;
  446. if ( it.Find((const char*)item->m_propName.c_str()) )
  447. {
  448. it.SetValue(item->m_curValue.c_str());
  449. }
  450. if( item->m_Dirty )
  451. {
  452. m_CacheEntriesList.SetDirty();
  453. }
  454. }
  455. }
  456. /**
  457. * Load Recent Directories
  458. */
  459. void
  460. CMakeSetupGUIImplementation
  461. ::LoadRecentDirectories( void )
  462. {
  463. std::string home = getenv("HOME");
  464. std::string filename = home + "/.cmakerc";
  465. std::ifstream input;
  466. input.open(filename.c_str());
  467. if( input.fail() )
  468. {
  469. // probably the file doesn't exist
  470. return;
  471. }
  472. m_RecentBinaryDirectories.clear();
  473. m_RecentSourceDirectories.clear();
  474. std::string key;
  475. std::string onedirectory;
  476. while( !input.eof() )
  477. {
  478. input >> key;
  479. if( input.eof() ) break;
  480. if( key == "MostRecentSource" )
  481. {
  482. input >> onedirectory;
  483. m_WhereSource = onedirectory;
  484. sourcePathTextInput->value( m_WhereSource.c_str() );
  485. } else
  486. if( key == "MostRecentBinary" )
  487. {
  488. input >> onedirectory;
  489. m_WhereBuild = onedirectory;
  490. binaryPathTextInput->value( m_WhereBuild.c_str() );
  491. LoadCacheFromDiskToGUI();
  492. } else
  493. if( key == "Binary" )
  494. {
  495. input >> onedirectory;
  496. // insert is only done if the directory doesn't exist
  497. m_RecentBinaryDirectories.insert( onedirectory );
  498. recentBinaryDirectoriesBrowser->add(
  499. (onedirectory.c_str()),
  500. (void*)(onedirectory.c_str()) );
  501. } else
  502. if( key == "Source" )
  503. {
  504. input >> onedirectory;
  505. // insert is only done if the directory doesn't exist
  506. m_RecentSourceDirectories.insert( onedirectory );
  507. recentSourceDirectoriesBrowser->add(
  508. (onedirectory.c_str()),
  509. (void*)(onedirectory.c_str()) );
  510. }
  511. }
  512. input.close();
  513. }
  514. /**
  515. * Save Recent Directories
  516. */
  517. void
  518. CMakeSetupGUIImplementation
  519. ::SaveRecentDirectories( void )
  520. {
  521. std::string home = getenv("HOME");
  522. if( home.empty() )
  523. {
  524. return;
  525. }
  526. std::string filename = home + "/.cmakerc";
  527. std::ofstream output;
  528. output.open(filename.c_str());
  529. output << "MostRecentBinary " << m_WhereBuild << std::endl;
  530. output << "MostRecentSource " << m_WhereSource << std::endl;
  531. // Save Recent binary directories
  532. std::set< std::string >::iterator bindir =
  533. m_RecentBinaryDirectories.begin();
  534. while( bindir != m_RecentBinaryDirectories.end() )
  535. {
  536. output << "Binary " << *bindir << std::endl;
  537. bindir++;
  538. }
  539. // Save Recent source directories
  540. std::set< std::string >::iterator srcdir =
  541. m_RecentSourceDirectories.begin();
  542. while( srcdir != m_RecentSourceDirectories.end() )
  543. {
  544. output << "Source " << *srcdir << std::endl;
  545. srcdir++;
  546. }
  547. }
  548. /**
  549. * Show Recent Binary Directories
  550. */
  551. void
  552. CMakeSetupGUIImplementation
  553. ::ShowRecentBinaryDirectories( void )
  554. {
  555. if( recentBinaryDirectoriesBrowser->size() )
  556. {
  557. recentBinaryDirectoriesBrowser->Fl_Widget::show();
  558. }
  559. }
  560. /**
  561. * Show Recent Source Directories
  562. */
  563. void
  564. CMakeSetupGUIImplementation
  565. ::ShowRecentSourceDirectories( void )
  566. {
  567. if( recentSourceDirectoriesBrowser->size() )
  568. {
  569. recentSourceDirectoriesBrowser->Fl_Widget::show();
  570. }
  571. }
  572. /**
  573. * Select one Recent Binary Directory
  574. */
  575. void
  576. CMakeSetupGUIImplementation
  577. ::SelectOneRecentBinaryDirectory( void )
  578. {
  579. const int selected = recentBinaryDirectoriesBrowser->value();
  580. if( selected == 0 )
  581. {
  582. return;
  583. }
  584. m_WhereBuild = static_cast<char *>(
  585. recentBinaryDirectoriesBrowser->data( selected ));
  586. binaryPathTextInput->value( m_WhereBuild.c_str() );
  587. recentBinaryDirectoriesBrowser->Fl_Widget::hide();
  588. m_CacheEntriesList.RemoveAll(); // remove data from other project
  589. LoadCacheFromDiskToGUI();
  590. }
  591. /**
  592. * Select one Recent Source Directory
  593. */
  594. void
  595. CMakeSetupGUIImplementation
  596. ::SelectOneRecentSourceDirectory( void )
  597. {
  598. const int selected = recentSourceDirectoriesBrowser->value();
  599. if( selected == 0 )
  600. {
  601. return;
  602. }
  603. m_WhereSource = static_cast< char * >(
  604. recentSourceDirectoriesBrowser->data( selected ));
  605. sourcePathTextInput->value( m_WhereSource.c_str() );
  606. recentSourceDirectoriesBrowser->Fl_Widget::hide();
  607. }
  608. /**
  609. * Update List of Recent Directories
  610. */
  611. void
  612. CMakeSetupGUIImplementation
  613. ::UpdateListOfRecentDirectories( void )
  614. {
  615. // Update Recent binary directories
  616. // insert is only done if the directory doesn't exist
  617. m_RecentBinaryDirectories.insert( m_WhereBuild );
  618. // Update Recent source directories
  619. // insert is only done if the directory doesn't exist
  620. m_RecentSourceDirectories.insert( m_WhereSource );
  621. }
  622. /**
  623. * Clicked on Configure Button
  624. */
  625. void
  626. CMakeSetupGUIImplementation
  627. ::ClickOnConfigure( void )
  628. {
  629. this->RunCMake(false);
  630. }
  631. /**
  632. * Clicked on OK Button
  633. */
  634. void
  635. CMakeSetupGUIImplementation
  636. ::ClickOnOK( void )
  637. {
  638. m_CacheEntriesList.ClearDirty();
  639. this->RunCMake(true);
  640. this->Close();
  641. }
  642. /**
  643. * Clicked on Cancel Button
  644. */
  645. void
  646. CMakeSetupGUIImplementation
  647. ::ClickOnCancel( void )
  648. {
  649. if(m_CacheEntriesList.IsDirty())
  650. {
  651. int userWantsExitEvenThoughOptionsHaveChanged =
  652. fl_ask("You have changed options but not rebuilt, \n"
  653. "are you sure you want to exit?");
  654. if( userWantsExitEvenThoughOptionsHaveChanged )
  655. {
  656. this->Close();
  657. }
  658. }
  659. else
  660. {
  661. this->Close();
  662. }
  663. }