cmCursesMainForm.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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 "../cmCacheManager.h"
  14. #include "../cmSystemTools.h"
  15. #include "../cmake.h"
  16. #include "cmCursesMainForm.h"
  17. #include "cmCursesStringWidget.h"
  18. #include "cmCursesLabelWidget.h"
  19. #include "cmCursesBoolWidget.h"
  20. #include "cmCursesPathWidget.h"
  21. #include "cmCursesFilePathWidget.h"
  22. #include "cmCursesDummyWidget.h"
  23. #include "cmCursesCacheEntryComposite.h"
  24. #include "cmCursesLongMessageForm.h"
  25. inline int ctrl(int z)
  26. {
  27. return (z&037);
  28. }
  29. cmCursesMainForm::cmCursesMainForm(std::vector<std::string> const& args) :
  30. m_Args(args)
  31. {
  32. m_NumberOfPages = 0;
  33. m_Fields = 0;
  34. m_Entries = 0;
  35. m_AdvancedMode = false;
  36. m_NumberOfVisibleEntries = 0;
  37. m_OkToGenerate = false;
  38. m_HelpMessage.push_back("Welcome to ccmake, curses based user interface for CMake.");
  39. m_HelpMessage.push_back("");
  40. m_HelpMessage.push_back(s_ConstHelpMessage);
  41. }
  42. cmCursesMainForm::~cmCursesMainForm()
  43. {
  44. if (m_Form)
  45. {
  46. unpost_form(m_Form);
  47. free_form(m_Form);
  48. m_Form = 0;
  49. }
  50. delete[] m_Fields;
  51. // Clean-up composites
  52. if (m_Entries)
  53. {
  54. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  55. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  56. {
  57. delete *it;
  58. }
  59. }
  60. delete m_Entries;
  61. }
  62. // See if a cache entry is in the list of entries in the ui.
  63. bool cmCursesMainForm::LookForCacheEntry(const char* key)
  64. {
  65. if (!key || !m_Entries)
  66. {
  67. return false;
  68. }
  69. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  70. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  71. {
  72. if (!strcmp(key, (*it)->m_Key.c_str()))
  73. {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. // Create new cmCursesCacheEntryComposite entries from the cache
  80. void cmCursesMainForm::InitializeUI()
  81. {
  82. // Get the cache entries.
  83. const cmCacheManager::CacheEntryMap &cache =
  84. cmCacheManager::GetInstance()->GetCacheMap();
  85. // Create a vector of cmCursesCacheEntryComposite's
  86. // which contain labels, entries and new entry markers
  87. std::vector<cmCursesCacheEntryComposite*>* newEntries =
  88. new std::vector<cmCursesCacheEntryComposite*>;
  89. newEntries->reserve(cache.size());
  90. // Count non-internal and non-static entries
  91. int count=0;
  92. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  93. i != cache.end(); ++i)
  94. {
  95. const cmCacheManager::CacheEntry& value = i->second;
  96. if ( value.m_Type != cmCacheManager::INTERNAL &&
  97. value.m_Type != cmCacheManager::STATIC )
  98. {
  99. ++count;
  100. }
  101. }
  102. cmCursesCacheEntryComposite* comp;
  103. if ( count == 0 )
  104. {
  105. // If cache is empty, display a label saying so and a
  106. // dummy entry widget (does not respond to input)
  107. comp = new cmCursesCacheEntryComposite("EMPTY CACHE");
  108. comp->m_Entry = new cmCursesDummyWidget(1, 1, 1, 1);
  109. newEntries->push_back(comp);
  110. }
  111. else
  112. {
  113. // Create the composites.
  114. // First add entries which are new
  115. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  116. i != cache.end(); ++i)
  117. {
  118. const char* key = i->first.c_str();
  119. const cmCacheManager::CacheEntry& value = i->second;
  120. if ( value.m_Type == cmCacheManager::INTERNAL ||
  121. value.m_Type == cmCacheManager::STATIC )
  122. {
  123. continue;
  124. }
  125. if (!this->LookForCacheEntry(key))
  126. {
  127. newEntries->push_back(new cmCursesCacheEntryComposite(key, value,
  128. true));
  129. m_OkToGenerate = false;
  130. }
  131. }
  132. // then add entries which are old
  133. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  134. i != cache.end(); ++i)
  135. {
  136. const char* key = i->first.c_str();
  137. const cmCacheManager::CacheEntry& value = i->second;
  138. if ( value.m_Type == cmCacheManager::INTERNAL ||
  139. value.m_Type == cmCacheManager::STATIC )
  140. {
  141. continue;
  142. }
  143. if (this->LookForCacheEntry(key))
  144. {
  145. newEntries->push_back(new cmCursesCacheEntryComposite(key, value,
  146. false));
  147. }
  148. }
  149. }
  150. // Clean old entries
  151. if (m_Entries)
  152. {
  153. // Have to call delete on each pointer
  154. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  155. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  156. {
  157. delete *it;
  158. }
  159. }
  160. delete m_Entries;
  161. m_Entries = newEntries;
  162. // Compute fields from composites
  163. this->RePost();
  164. }
  165. void cmCursesMainForm::RePost()
  166. {
  167. // Create the fields to be passed to the form.
  168. if (m_Form)
  169. {
  170. unpost_form(m_Form);
  171. free_form(m_Form);
  172. m_Form = 0;
  173. }
  174. delete[] m_Fields;
  175. if (m_AdvancedMode)
  176. {
  177. m_NumberOfVisibleEntries = m_Entries->size();
  178. }
  179. else
  180. {
  181. // If normal mode, count only non-advanced entries
  182. m_NumberOfVisibleEntries = 0;
  183. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  184. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  185. {
  186. if (!m_AdvancedMode && cmCacheManager::GetInstance()->IsAdvanced(
  187. (*it)->GetValue()))
  188. {
  189. continue;
  190. }
  191. m_NumberOfVisibleEntries++;
  192. }
  193. }
  194. // Assign the fields: 3 for each entry: label, new entry marker
  195. // ('*' or ' ') and entry widget
  196. m_Fields = new FIELD*[3*m_NumberOfVisibleEntries+1];
  197. // Assign fields
  198. int j=0;
  199. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  200. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  201. {
  202. if (!m_AdvancedMode && cmCacheManager::GetInstance()->IsAdvanced(
  203. (*it)->GetValue()))
  204. {
  205. continue;
  206. }
  207. m_Fields[3*j] = (*it)->m_Label->m_Field;
  208. m_Fields[3*j+1] = (*it)->m_IsNewLabel->m_Field;
  209. m_Fields[3*j+2] = (*it)->m_Entry->m_Field;
  210. j++;
  211. }
  212. // Has to be null terminated.
  213. m_Fields[3*m_NumberOfVisibleEntries] = 0;
  214. }
  215. void cmCursesMainForm::Render(int left, int top, int width, int height)
  216. {
  217. if (m_Form)
  218. {
  219. FIELD* currentField = current_field(m_Form);
  220. cmCursesWidget* cw = reinterpret_cast<cmCursesWidget*>
  221. (field_userptr(currentField));
  222. // If in edit mode, get out of it
  223. if ( cw->GetType() == cmCacheManager::STRING ||
  224. cw->GetType() == cmCacheManager::PATH ||
  225. cw->GetType() == cmCacheManager::FILEPATH )
  226. {
  227. cmCursesStringWidget* sw = static_cast<cmCursesStringWidget*>(cw);
  228. sw->SetInEdit(false);
  229. }
  230. // Delete the previous form
  231. unpost_form(m_Form);
  232. free_form(m_Form);
  233. m_Form = 0;
  234. }
  235. // Wrong window size
  236. if ( width < cmCursesMainForm::MIN_WIDTH ||
  237. height < cmCursesMainForm::MIN_HEIGHT )
  238. {
  239. return;
  240. }
  241. // Leave room for toolbar
  242. height -= 7;
  243. if (m_AdvancedMode)
  244. {
  245. m_NumberOfVisibleEntries = m_Entries->size();
  246. }
  247. else
  248. {
  249. // If normal, display only non-advanced entries
  250. m_NumberOfVisibleEntries = 0;
  251. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  252. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  253. {
  254. if (!m_AdvancedMode && cmCacheManager::GetInstance()->IsAdvanced(
  255. (*it)->GetValue()))
  256. {
  257. continue;
  258. }
  259. m_NumberOfVisibleEntries++;
  260. }
  261. }
  262. // Re-adjust the fields according to their place
  263. bool isNewPage;
  264. int i=0;
  265. m_NumberOfPages = 1;
  266. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  267. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  268. {
  269. if (!m_AdvancedMode && cmCacheManager::GetInstance()->IsAdvanced(
  270. (*it)->GetValue()))
  271. {
  272. continue;
  273. }
  274. int row = (i % height) + 1;
  275. int page = (i / height) + 1;
  276. isNewPage = ( page > 1 ) && ( row == 1 );
  277. if (isNewPage)
  278. {
  279. m_NumberOfPages++;
  280. }
  281. (*it)->m_Label->Move(left, top+row-1, isNewPage);
  282. (*it)->m_IsNewLabel->Move(left+32, top+row-1, false);
  283. (*it)->m_Entry->Move(left+33, top+row-1, false);
  284. (*it)->m_Entry->SetPage(m_NumberOfPages);
  285. i++;
  286. }
  287. // Post the form
  288. m_Form = new_form(m_Fields);
  289. post_form(m_Form);
  290. // Update toolbar
  291. this->UpdateStatusBar();
  292. this->PrintKeys();
  293. touchwin(stdscr);
  294. refresh();
  295. }
  296. void cmCursesMainForm::PrintKeys()
  297. {
  298. int x,y;
  299. getmaxyx(stdscr, y, x);
  300. if ( x < cmCursesMainForm::MIN_WIDTH ||
  301. y < cmCursesMainForm::MIN_HEIGHT )
  302. {
  303. return;
  304. }
  305. // Give the current widget (if it exists), a chance to print keys
  306. cmCursesWidget* cw = 0;
  307. if (m_Form)
  308. {
  309. FIELD* currentField = current_field(m_Form);
  310. cw = reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  311. }
  312. if (cw && cw->PrintKeys())
  313. {
  314. }
  315. else
  316. {
  317. char firstLine[512], secondLine[512], thirdLine[512];
  318. if (m_OkToGenerate)
  319. {
  320. sprintf(firstLine, "Press [c] to configure Press [g] to generate and exit");
  321. }
  322. else
  323. {
  324. sprintf(firstLine, "Press [c] to configure");
  325. }
  326. if (m_AdvancedMode)
  327. {
  328. sprintf(thirdLine, "Press [t] to toggle advanced mode (Currently On)");
  329. }
  330. else
  331. {
  332. sprintf(thirdLine, "Press [t] to toggle advanced mode (Currently Off)");
  333. }
  334. sprintf(secondLine, "Press [h] for help Press [q] to quit without generating");
  335. curses_move(y-4,0);
  336. printw("Press [enter] to edit option");
  337. curses_move(y-3,0);
  338. printw(firstLine);
  339. curses_move(y-2,0);
  340. printw(secondLine);
  341. curses_move(y-1,0);
  342. printw(thirdLine);
  343. if (cw)
  344. {
  345. sprintf(firstLine, "Page %d of %d", cw->GetPage(), m_NumberOfPages);
  346. curses_move(0,65-strlen(firstLine)-1);
  347. printw(firstLine);
  348. }
  349. }
  350. pos_form_cursor(m_Form);
  351. }
  352. // Print the key of the current entry and the CMake version
  353. // on the status bar. Designed for a width of 80 chars.
  354. void cmCursesMainForm::UpdateStatusBar()
  355. {
  356. int x,y;
  357. getmaxyx(stdscr, y, x);
  358. // If window size is too small, display error and return
  359. if ( x < cmCursesMainForm::MIN_WIDTH ||
  360. y < cmCursesMainForm::MIN_HEIGHT )
  361. {
  362. curses_clear();
  363. curses_move(0,0);
  364. printw("Window is too small. A size of at least %dx%d is required.",
  365. cmCursesMainForm::MIN_WIDTH, cmCursesMainForm::MIN_HEIGHT);
  366. touchwin(stdscr);
  367. wrefresh(stdscr);
  368. return;
  369. }
  370. // Get the key of the current entry
  371. FIELD* cur = current_field(m_Form);
  372. int index = field_index(cur);
  373. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(field_userptr(
  374. m_Fields[index-2]));
  375. const char* curField = lbl->GetValue();
  376. // Get the help string of the current entry
  377. // and add it to the help string
  378. char help[128];
  379. const char* helpString;
  380. cmCacheManager::CacheEntry *entry =
  381. cmCacheManager::GetInstance()->GetCacheEntry(curField);
  382. if (entry)
  383. {
  384. helpString = entry->m_HelpString.c_str();
  385. strncpy(help, helpString, 127);
  386. help[127] = '\0';
  387. }
  388. else
  389. {
  390. sprintf(help," ");
  391. }
  392. // Join the key, help string and pad with spaces
  393. // (or truncate) as necessary
  394. char bar[cmCursesMainForm::MAX_WIDTH];
  395. int i, curFieldLen = strlen(curField);
  396. int helpLen = strlen(help);
  397. int width;
  398. if (x < cmCursesMainForm::MAX_WIDTH )
  399. {
  400. width = x;
  401. }
  402. else
  403. {
  404. width = cmCursesMainForm::MAX_WIDTH;
  405. }
  406. if (curFieldLen >= width)
  407. {
  408. strncpy(bar, curField, width);
  409. }
  410. else
  411. {
  412. strcpy(bar, curField);
  413. bar[curFieldLen] = ':';
  414. bar[curFieldLen+1] = ' ';
  415. if (curFieldLen + helpLen + 2 >= width)
  416. {
  417. strncpy(bar+curFieldLen+2, help, width
  418. - curFieldLen - 2);
  419. }
  420. else
  421. {
  422. strcpy(bar+curFieldLen+2, help);
  423. for(i=curFieldLen+helpLen+2; i < width; ++i)
  424. {
  425. bar[i] = ' ';
  426. }
  427. }
  428. }
  429. bar[width] = '\0';
  430. // Display CMake version info on the next line
  431. // We want to display this on the right
  432. char version[cmCursesMainForm::MAX_WIDTH];
  433. char vertmp[128];
  434. sprintf(vertmp,"CMake Version %d.%d - %s", cmMakefile::GetMajorVersion(),
  435. cmMakefile::GetMinorVersion(),cmMakefile::GetReleaseVersion());
  436. int sideSpace = (width-strlen(vertmp));
  437. for(i=0; i<sideSpace; i++) { version[i] = ' '; }
  438. sprintf(version+sideSpace, "%s", vertmp);
  439. version[width] = '\0';
  440. // Now print both lines
  441. curses_move(y-5,0);
  442. attron(A_STANDOUT);
  443. printw(bar);
  444. attroff(A_STANDOUT);
  445. curses_move(y-4,0);
  446. printw(version);
  447. pos_form_cursor(m_Form);
  448. }
  449. void cmCursesMainForm::RunCMake(bool generateMakefiles)
  450. {
  451. int x,y;
  452. getmaxyx(stdscr, y, x);
  453. curses_clear();
  454. curses_move(1,1);
  455. touchwin(stdscr);
  456. refresh();
  457. endwin();
  458. std::cerr << "Running CMake, please wait...\n\r";
  459. // always save the current gui values to disk
  460. this->FillCacheManagerFromUI();
  461. cmCacheManager::GetInstance()->SaveCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  462. // create a cmake object
  463. cmake make;
  464. // create the arguments for the cmake object
  465. std::string whereCMake = cmSystemTools::GetProgramPath(m_Args[0].c_str());
  466. whereCMake += "/cmake";
  467. m_Args[0] = whereCMake;
  468. // Get rid of previous errors
  469. m_Errors = std::vector<std::string>();
  470. // run the generate process
  471. m_OkToGenerate = true;
  472. int retVal = make.Generate(m_Args, generateMakefiles);
  473. initscr(); /* Initialization */
  474. noecho(); /* Echo off */
  475. cbreak(); /* nl- or cr not needed */
  476. keypad(stdscr,TRUE); /* Use key symbols as
  477. KEY_DOWN*/
  478. if( retVal != 0 || !m_Errors.empty())
  479. {
  480. // see if there was an error
  481. if(cmSystemTools::GetErrorOccuredFlag())
  482. {
  483. m_OkToGenerate = false;
  484. }
  485. // reset error condition
  486. cmSystemTools::ResetErrorOccuredFlag();
  487. int x,y;
  488. getmaxyx(stdscr, y, x);
  489. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(m_Errors,
  490. "Errors which during last pass.");
  491. CurrentForm = msgs;
  492. msgs->Render(1,1,x,y);
  493. msgs->HandleInput();
  494. CurrentForm = this;
  495. this->Render(1,1,x,y);
  496. }
  497. this->InitializeUI();
  498. this->Render(1, 1, x, y);
  499. }
  500. void cmCursesMainForm::AddError(const char* message, const char* title)
  501. {
  502. m_Errors.push_back(message);
  503. }
  504. void cmCursesMainForm::RemoveEntry(const char* value)
  505. {
  506. if (!value)
  507. {
  508. return;
  509. }
  510. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  511. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  512. {
  513. const char* val = (*it)->GetValue();
  514. if ( val && !strcmp(value, val) )
  515. {
  516. m_Entries->erase(it);
  517. break;
  518. }
  519. }
  520. }
  521. // copy from the list box to the cache manager
  522. void cmCursesMainForm::FillCacheManagerFromUI()
  523. {
  524. std::string tmpString;
  525. cmCacheManager::GetInstance()->GetCacheMap();
  526. int size = m_Entries->size();
  527. for(int i=0; i < size; i++)
  528. {
  529. cmCacheManager::CacheEntry *entry =
  530. cmCacheManager::GetInstance()->GetCacheEntry(
  531. (*m_Entries)[i]->m_Key.c_str());
  532. if (entry)
  533. {
  534. tmpString = (*m_Entries)[i]->m_Entry->GetValue();
  535. // Remove trailing spaces, convert path to unix slashes
  536. std::string tmpSubString =
  537. tmpString.substr(0,tmpString.find_last_not_of(" ")+1);
  538. if ( entry->m_Type == cmCacheManager::PATH ||
  539. entry->m_Type == cmCacheManager::FILEPATH )
  540. {
  541. cmSystemTools::ConvertToUnixSlashes(tmpSubString);
  542. }
  543. entry->m_Value = tmpSubString;
  544. }
  545. }
  546. }
  547. void cmCursesMainForm::HandleInput()
  548. {
  549. int x,y;
  550. if (!m_Form)
  551. {
  552. return;
  553. }
  554. FIELD* currentField;
  555. cmCursesWidget* currentWidget;
  556. char debugMessage[128];
  557. while(1)
  558. {
  559. this->UpdateStatusBar();
  560. this->PrintKeys();
  561. int key = getch();
  562. getmaxyx(stdscr, y, x);
  563. // If window too small, handle 'q' only
  564. if ( x < cmCursesMainForm::MIN_WIDTH ||
  565. y < cmCursesMainForm::MIN_HEIGHT )
  566. {
  567. // quit
  568. if ( key == 'q' )
  569. {
  570. break;
  571. }
  572. else
  573. {
  574. continue;
  575. }
  576. }
  577. currentField = current_field(m_Form);
  578. currentWidget = reinterpret_cast<cmCursesWidget*>(field_userptr(
  579. currentField));
  580. // Ask the current widget if it wants to handle input
  581. if (!currentWidget || !currentWidget->HandleInput(key, this, stdscr))
  582. {
  583. // If the current widget does not want to handle input,
  584. // we handle it.
  585. sprintf(debugMessage, "Main form handling input, key: %d", key);
  586. cmCursesForm::LogMessage(debugMessage);
  587. // quit
  588. if ( key == 'q' )
  589. {
  590. break;
  591. }
  592. // if not end of page, next field otherwise next page
  593. // each entry consists of fields: label, isnew, value
  594. // therefore, the label field for the prev. entry is index-5
  595. // and the label field for the next entry is index+1
  596. // (index always corresponds to the value field)
  597. else if ( key == KEY_DOWN || key == ctrl('n') )
  598. {
  599. FIELD* cur = current_field(m_Form);
  600. int index = field_index(cur);
  601. if ( index == 3*m_NumberOfVisibleEntries-1 )
  602. {
  603. continue;
  604. }
  605. if (new_page(m_Fields[index+1]))
  606. {
  607. form_driver(m_Form, REQ_NEXT_PAGE);
  608. }
  609. else
  610. {
  611. form_driver(m_Form, REQ_NEXT_FIELD);
  612. }
  613. }
  614. // if not beginning of page, previous field, otherwise previous page
  615. // each entry consists of fields: label, isnew, value
  616. // therefore, the label field for the prev. entry is index-5
  617. // and the label field for the next entry is index+1
  618. // (index always corresponds to the value field)
  619. else if ( key == KEY_UP || key == ctrl('p') )
  620. {
  621. FIELD* cur = current_field(m_Form);
  622. int index = field_index(cur);
  623. if ( index == 2 )
  624. {
  625. continue;
  626. }
  627. if ( new_page(m_Fields[index-2]) )
  628. {
  629. form_driver(m_Form, REQ_PREV_PAGE);
  630. set_current_field(m_Form, m_Fields[index-3]);
  631. }
  632. else
  633. {
  634. form_driver(m_Form, REQ_PREV_FIELD);
  635. }
  636. }
  637. // pg down
  638. else if ( key == KEY_NPAGE || key == ctrl('d') )
  639. {
  640. form_driver(m_Form, REQ_NEXT_PAGE);
  641. }
  642. // pg up
  643. else if ( key == KEY_PPAGE || key == ctrl('u') )
  644. {
  645. form_driver(m_Form, REQ_PREV_PAGE);
  646. }
  647. // configure
  648. else if ( key == 'c' )
  649. {
  650. this->RunCMake(false);
  651. }
  652. // display help
  653. else if ( key == 'h' )
  654. {
  655. getmaxyx(stdscr, y, x);
  656. FIELD* cur = current_field(m_Form);
  657. int index = field_index(cur);
  658. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(field_userptr(
  659. m_Fields[index-2]));
  660. const char* curField = lbl->GetValue();
  661. const char* helpString=0;
  662. cmCacheManager::CacheEntry *entry =
  663. cmCacheManager::GetInstance()->GetCacheEntry(curField);
  664. if (entry)
  665. {
  666. helpString = entry->m_HelpString.c_str();
  667. }
  668. if (helpString)
  669. {
  670. char* message = new char[strlen(curField)+strlen(helpString)
  671. +strlen("Current option is: \n Help string for this option is: \n")+10];
  672. sprintf(message,"Current option is: %s\nHelp string for this option is: %s\n", curField, helpString);
  673. m_HelpMessage[1] = message;
  674. delete[] message;
  675. }
  676. else
  677. {
  678. m_HelpMessage[1] = "";
  679. }
  680. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(m_HelpMessage,
  681. "Help.");
  682. CurrentForm = msgs;
  683. msgs->Render(1,1,x,y);
  684. msgs->HandleInput();
  685. CurrentForm = this;
  686. this->Render(1,1,x,y);
  687. }
  688. // display last errors
  689. else if ( key == 'l' )
  690. {
  691. getmaxyx(stdscr, y, x);
  692. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(m_Errors,
  693. "Errors which during last pass.");
  694. CurrentForm = msgs;
  695. msgs->Render(1,1,x,y);
  696. msgs->HandleInput();
  697. CurrentForm = this;
  698. this->Render(1,1,x,y);
  699. }
  700. // switch advanced on/off
  701. else if ( key == 't' )
  702. {
  703. if (m_AdvancedMode)
  704. {
  705. m_AdvancedMode = false;
  706. }
  707. else
  708. {
  709. m_AdvancedMode = true;
  710. }
  711. getmaxyx(stdscr, y, x);
  712. this->RePost();
  713. this->Render(1, 1, x, y);
  714. }
  715. // generate and exit
  716. else if ( key == 'g' )
  717. {
  718. if ( m_OkToGenerate )
  719. {
  720. this->RunCMake(true);
  721. break;
  722. }
  723. }
  724. // delete cache entry
  725. else if ( key == 'd' )
  726. {
  727. FIELD* cur = current_field(m_Form);
  728. int index = field_index(cur);
  729. // make the next or prev. current field after deletion
  730. // each entry consists of fields: label, isnew, value
  731. // therefore, the label field for the prev. entry is index-5
  732. // and the label field for the next entry is index+1
  733. // (index always corresponds to the value field)
  734. FIELD* nextCur;
  735. if ( index == 2 )
  736. {
  737. nextCur=0;
  738. }
  739. else if ( index == 3*m_NumberOfVisibleEntries-1 )
  740. {
  741. nextCur = m_Fields[index-5];
  742. }
  743. else
  744. {
  745. nextCur = m_Fields[index+1];
  746. }
  747. // Get the label widget
  748. // each entry consists of fields: label, isnew, value
  749. // therefore, the label field for the is index-2
  750. // (index always corresponds to the value field)
  751. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(field_userptr(
  752. m_Fields[index-2]));
  753. cmCacheManager::GetInstance()->RemoveCacheEntry(lbl->GetValue());
  754. std::string nextVal;
  755. if (nextCur)
  756. {
  757. nextVal = (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))->GetValue());
  758. }
  759. getmaxyx(stdscr, y, x);
  760. this->RemoveEntry(lbl->GetValue());
  761. this->RePost();
  762. this->Render(1, 1, x, y);
  763. if (nextCur)
  764. {
  765. // make the next or prev. current field after deletion
  766. nextCur = 0;
  767. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  768. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  769. {
  770. if (nextVal == (*it)->m_Key)
  771. {
  772. nextCur = (*it)->m_Entry->m_Field;
  773. }
  774. }
  775. if (nextCur)
  776. {
  777. set_current_field(m_Form, nextCur);
  778. }
  779. }
  780. }
  781. }
  782. touchwin(stdscr);
  783. wrefresh(stdscr);
  784. }
  785. }
  786. const char* cmCursesMainForm::s_ConstHelpMessage =
  787. "CMake is used to configure and generate build files for software projects. "
  788. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  789. "1. Run ccmake in the directory where you want the object and executable files to be placed (build directory). If the source directory is not the same as this build directory, you have to specify it as an argument on the command line.\n\n"
  790. "2. When ccmake is run, it will read the configuration files and display the current build options. "
  791. "If you have run CMake before and have updated the configuration files since then, any new entries will be displayed on top and will be marked with a *. "
  792. "On the other hand, the first time you run ccmake, all build options will be new and will be marked as such. "
  793. "At this point, you can modify any options (see keys below) you want to change. "
  794. "When you are satisfied with your changes, press 'c' to have CMake process the configuration files. "
  795. "Please note that changing some options may cause new ones to appear. These will be shown on top and will be marked with *. "
  796. "Repeat this procedure until you are satisfied with all the options and there are no new entries. "
  797. "At this point, a new command will appear: G)enerate and Exit. You can now hit 'g' to have CMake generate all the build files (i.e. makefiles or project files) and exit. "
  798. "At any point during the process, you can exit ccmake with 'q'. However, this will not generate/change any build files.\n\n"
  799. "ccmake KEYS:\n\n"
  800. "Navigation: "
  801. "You can use the arrow keys and page up, down to navigate the options. Alternatively, you can use the following keys: \n"
  802. " C-n : next option\n"
  803. " C-p : previous options\n"
  804. " C-d : down one page\n"
  805. " C-u : up one page\n\n"
  806. "Editing options: "
  807. "To change an option press enter or return. If the current options is a boolean, this will toggle it's value. "
  808. "Otherwise, ccmake will enter edit mode. In this mode you can edit an option using arrow keys and backspace. Alternatively, you can use the following keys:\n"
  809. " C-b : back one character\n"
  810. " C-f : forward one character\n"
  811. " C-a : go to the beginning of the field\n"
  812. " C-e : go to the end of the field\n"
  813. " C-d : delete previous character\n"
  814. " C-k : kill the rest of the field\n"
  815. " Esc : Restore field (discard last changes)\n"
  816. " Enter : Leave edit mode\n"
  817. "You can also delete an option by pressing 'd'\n\n"
  818. "Commands:\n"
  819. " q : quit ccmake without generating build files\n"
  820. " h : help, shows this screen\n"
  821. " c : process the configuration files with the current options\n"
  822. " g : generate build files and exit, only available when there are no "
  823. "new options and no errors have been detected during last configuration.\n"
  824. " l : shows last errors\n"
  825. " t : toggles advanced mode. In normal mode, only the most important options are shown. In advanced mode, all options are shown. We recommend using normal mode unless you are an expert.\n";