cmCursesMainForm.cxx 23 KB

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