cmCursesMainForm.cxx 29 KB

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