cmCursesMainForm.cxx 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "../cmCacheManager.h"
  11. #include "../cmSystemTools.h"
  12. #include "../cmVersion.h"
  13. #include "../cmake.h"
  14. #include "cmCursesMainForm.h"
  15. #include "cmCursesStringWidget.h"
  16. #include "cmCursesLabelWidget.h"
  17. #include "cmCursesBoolWidget.h"
  18. #include "cmCursesPathWidget.h"
  19. #include "cmCursesFilePathWidget.h"
  20. #include "cmCursesDummyWidget.h"
  21. #include "cmCursesCacheEntryComposite.h"
  22. #include "cmCursesLongMessageForm.h"
  23. inline int ctrl(int z)
  24. {
  25. return (z&037);
  26. }
  27. cmCursesMainForm::cmCursesMainForm(std::vector<std::string> const& args,
  28. int initWidth) :
  29. Args(args), InitialWidth(initWidth)
  30. {
  31. this->NumberOfPages = 0;
  32. this->Fields = 0;
  33. this->Entries = 0;
  34. this->AdvancedMode = false;
  35. this->NumberOfVisibleEntries = 0;
  36. this->OkToGenerate = false;
  37. this->HelpMessage.push_back("Welcome to ccmake, curses based user interface for CMake.");
  38. this->HelpMessage.push_back("");
  39. this->HelpMessage.push_back(s_ConstHelpMessage);
  40. this->CMakeInstance = new cmake;
  41. this->CMakeInstance->SetCMakeEditCommand("ccmake");
  42. // create the arguments for the cmake object
  43. std::string whereCMake = cmSystemTools::GetProgramPath(this->Args[0].c_str());
  44. whereCMake += "/cmake";
  45. this->Args[0] = whereCMake;
  46. this->CMakeInstance->SetArgs(this->Args);
  47. this->CMakeInstance->SetCMakeCommand(whereCMake.c_str());
  48. this->SearchString = "";
  49. this->OldSearchString = "";
  50. this->SearchMode = false;
  51. }
  52. cmCursesMainForm::~cmCursesMainForm()
  53. {
  54. if (this->Form)
  55. {
  56. unpost_form(this->Form);
  57. free_form(this->Form);
  58. this->Form = 0;
  59. }
  60. delete[] this->Fields;
  61. // Clean-up composites
  62. if (this->Entries)
  63. {
  64. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  65. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  66. {
  67. delete *it;
  68. }
  69. }
  70. delete this->Entries;
  71. if (this->CMakeInstance)
  72. {
  73. delete this->CMakeInstance;
  74. this->CMakeInstance = 0;
  75. }
  76. }
  77. // See if a cache entry is in the list of entries in the ui.
  78. bool cmCursesMainForm::LookForCacheEntry(const char* key)
  79. {
  80. if (!key || !this->Entries)
  81. {
  82. return false;
  83. }
  84. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  85. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  86. {
  87. if (!strcmp(key, (*it)->Key.c_str()))
  88. {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. // Create new cmCursesCacheEntryComposite entries from the cache
  95. void cmCursesMainForm::InitializeUI()
  96. {
  97. // Create a vector of cmCursesCacheEntryComposite's
  98. // which contain labels, entries and new entry markers
  99. std::vector<cmCursesCacheEntryComposite*>* newEntries =
  100. new std::vector<cmCursesCacheEntryComposite*>;
  101. newEntries->reserve(this->CMakeInstance->GetCacheManager()->GetSize());
  102. // Count non-internal and non-static entries
  103. int count=0;
  104. for(cmCacheManager::CacheIterator i =
  105. this->CMakeInstance->GetCacheManager()->NewIterator();
  106. !i.IsAtEnd(); i.Next())
  107. {
  108. if ( i.GetType() != cmCacheManager::INTERNAL &&
  109. i.GetType() != cmCacheManager::STATIC &&
  110. i.GetType() != cmCacheManager::UNINITIALIZED)
  111. {
  112. ++count;
  113. }
  114. }
  115. int entrywidth = this->InitialWidth - 35;
  116. cmCursesCacheEntryComposite* comp;
  117. if ( count == 0 )
  118. {
  119. // If cache is empty, display a label saying so and a
  120. // dummy entry widget (does not respond to input)
  121. comp = new cmCursesCacheEntryComposite("EMPTY CACHE", 30, 30);
  122. comp->Entry = new cmCursesDummyWidget(1, 1, 1, 1);
  123. newEntries->push_back(comp);
  124. }
  125. else
  126. {
  127. // Create the composites.
  128. // First add entries which are new
  129. for(cmCacheManager::CacheIterator i =
  130. this->CMakeInstance->GetCacheManager()->NewIterator();
  131. !i.IsAtEnd(); i.Next())
  132. {
  133. const char* key = i.GetName();
  134. if ( i.GetType() == cmCacheManager::INTERNAL ||
  135. i.GetType() == cmCacheManager::STATIC ||
  136. i.GetType() == cmCacheManager::UNINITIALIZED )
  137. {
  138. continue;
  139. }
  140. if (!this->LookForCacheEntry(key))
  141. {
  142. newEntries->push_back(new cmCursesCacheEntryComposite(key, i,
  143. true, 30,
  144. entrywidth));
  145. this->OkToGenerate = false;
  146. }
  147. }
  148. // then add entries which are old
  149. for(cmCacheManager::CacheIterator i =
  150. this->CMakeInstance->GetCacheManager()->NewIterator();
  151. !i.IsAtEnd(); i.Next())
  152. {
  153. const char* key = i.GetName();
  154. if ( i.GetType() == cmCacheManager::INTERNAL ||
  155. i.GetType() == cmCacheManager::STATIC ||
  156. i.GetType() == cmCacheManager::UNINITIALIZED )
  157. {
  158. continue;
  159. }
  160. if (this->LookForCacheEntry(key))
  161. {
  162. newEntries->push_back(new cmCursesCacheEntryComposite(key, i,
  163. false, 30,
  164. entrywidth));
  165. }
  166. }
  167. }
  168. // Clean old entries
  169. if (this->Entries)
  170. {
  171. // Have to call delete on each pointer
  172. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  173. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  174. {
  175. delete *it;
  176. }
  177. }
  178. delete this->Entries;
  179. this->Entries = newEntries;
  180. // Compute fields from composites
  181. this->RePost();
  182. }
  183. void cmCursesMainForm::RePost()
  184. {
  185. // Create the fields to be passed to the form.
  186. if (this->Form)
  187. {
  188. unpost_form(this->Form);
  189. free_form(this->Form);
  190. this->Form = 0;
  191. }
  192. delete[] this->Fields;
  193. if (this->AdvancedMode)
  194. {
  195. this->NumberOfVisibleEntries = this->Entries->size();
  196. }
  197. else
  198. {
  199. // If normal mode, count only non-advanced entries
  200. this->NumberOfVisibleEntries = 0;
  201. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  202. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  203. {
  204. cmCacheManager::CacheIterator mit =
  205. this->CMakeInstance->GetCacheManager()->GetCacheIterator((*it)->GetValue());
  206. if (mit.IsAtEnd() ||
  207. (!this->AdvancedMode && mit.GetPropertyAsBool("ADVANCED")))
  208. {
  209. continue;
  210. }
  211. this->NumberOfVisibleEntries++;
  212. }
  213. }
  214. // there is always one even if it is the dummy one
  215. if(this->NumberOfVisibleEntries == 0)
  216. {
  217. this->NumberOfVisibleEntries = 1;
  218. }
  219. // Assign the fields: 3 for each entry: label, new entry marker
  220. // ('*' or ' ') and entry widget
  221. this->Fields = new FIELD*[3*this->NumberOfVisibleEntries+1];
  222. size_t cc;
  223. for ( cc = 0; cc < 3 * this->NumberOfVisibleEntries+1; cc ++ )
  224. {
  225. this->Fields[cc] = 0;
  226. }
  227. // Assign fields
  228. int j=0;
  229. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  230. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  231. {
  232. cmCacheManager::CacheIterator mit =
  233. this->CMakeInstance->GetCacheManager()->GetCacheIterator((*it)->GetValue());
  234. if (mit.IsAtEnd() ||
  235. (!this->AdvancedMode && mit.GetPropertyAsBool("ADVANCED")))
  236. {
  237. continue;
  238. }
  239. this->Fields[3*j] = (*it)->Label->Field;
  240. this->Fields[3*j+1] = (*it)->IsNewLabel->Field;
  241. this->Fields[3*j+2] = (*it)->Entry->Field;
  242. j++;
  243. }
  244. // if no cache entries there should still be one dummy field
  245. if(j == 0)
  246. {
  247. it = this->Entries->begin();
  248. this->Fields[0] = (*it)->Label->Field;
  249. this->Fields[1] = (*it)->IsNewLabel->Field;
  250. this->Fields[2] = (*it)->Entry->Field;
  251. this->NumberOfVisibleEntries = 1;
  252. }
  253. // Has to be null terminated.
  254. this->Fields[3*this->NumberOfVisibleEntries] = 0;
  255. }
  256. void cmCursesMainForm::Render(int left, int top, int width, int height)
  257. {
  258. if (this->Form)
  259. {
  260. FIELD* currentField = current_field(this->Form);
  261. cmCursesWidget* cw = reinterpret_cast<cmCursesWidget*>
  262. (field_userptr(currentField));
  263. // If in edit mode, get out of it
  264. if ( cw->GetType() == cmCacheManager::STRING ||
  265. cw->GetType() == cmCacheManager::PATH ||
  266. cw->GetType() == cmCacheManager::FILEPATH )
  267. {
  268. cmCursesStringWidget* sw = static_cast<cmCursesStringWidget*>(cw);
  269. sw->SetInEdit(false);
  270. }
  271. // Delete the previous form
  272. unpost_form(this->Form);
  273. free_form(this->Form);
  274. this->Form = 0;
  275. }
  276. // Wrong window size
  277. if ( width < cmCursesMainForm::MIN_WIDTH ||
  278. width < this->InitialWidth ||
  279. height < cmCursesMainForm::MIN_HEIGHT )
  280. {
  281. return;
  282. }
  283. // Leave room for toolbar
  284. height -= 7;
  285. if (this->AdvancedMode)
  286. {
  287. this->NumberOfVisibleEntries = this->Entries->size();
  288. }
  289. else
  290. {
  291. // If normal, display only non-advanced entries
  292. this->NumberOfVisibleEntries = 0;
  293. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  294. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  295. {
  296. cmCacheManager::CacheIterator mit =
  297. this->CMakeInstance->GetCacheManager()->GetCacheIterator((*it)->GetValue());
  298. if (mit.IsAtEnd() ||
  299. (!this->AdvancedMode && mit.GetPropertyAsBool("ADVANCED")))
  300. {
  301. continue;
  302. }
  303. this->NumberOfVisibleEntries++;
  304. }
  305. }
  306. // Re-adjust the fields according to their place
  307. this->NumberOfPages = 1;
  308. if (height > 0)
  309. {
  310. bool isNewPage;
  311. int i=0;
  312. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  313. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  314. {
  315. cmCacheManager::CacheIterator mit =
  316. this->CMakeInstance->GetCacheManager()->GetCacheIterator((*it)->GetValue());
  317. if (mit.IsAtEnd() ||
  318. (!this->AdvancedMode && mit.GetPropertyAsBool("ADVANCED")))
  319. {
  320. continue;
  321. }
  322. int row = (i % height) + 1;
  323. int page = (i / height) + 1;
  324. isNewPage = ( page > 1 ) && ( row == 1 );
  325. if (isNewPage)
  326. {
  327. this->NumberOfPages++;
  328. }
  329. (*it)->Label->Move(left, top+row-1, isNewPage);
  330. (*it)->IsNewLabel->Move(left+32, top+row-1, false);
  331. (*it)->Entry->Move(left+33, top+row-1, false);
  332. (*it)->Entry->SetPage(this->NumberOfPages);
  333. i++;
  334. }
  335. }
  336. // Post the form
  337. this->Form = new_form(this->Fields);
  338. post_form(this->Form);
  339. // Update toolbar
  340. this->UpdateStatusBar();
  341. this->PrintKeys();
  342. touchwin(stdscr);
  343. refresh();
  344. }
  345. void cmCursesMainForm::PrintKeys(int process /* = 0 */)
  346. {
  347. int x,y;
  348. getmaxyx(stdscr, y, x);
  349. if ( x < cmCursesMainForm::MIN_WIDTH ||
  350. x < this->InitialWidth ||
  351. y < cmCursesMainForm::MIN_HEIGHT )
  352. {
  353. return;
  354. }
  355. // Give the current widget (if it exists), a chance to print keys
  356. cmCursesWidget* cw = 0;
  357. if (this->Form)
  358. {
  359. FIELD* currentField = current_field(this->Form);
  360. cw = reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  361. }
  362. if (cw)
  363. {
  364. cw->PrintKeys();
  365. }
  366. // {
  367. // }
  368. // else
  369. // {
  370. char firstLine[512]="";
  371. char secondLine[512]="";
  372. char thirdLine[512]="";
  373. if (process)
  374. {
  375. sprintf(firstLine,
  376. " ");
  377. sprintf(secondLine,
  378. " ");
  379. sprintf(thirdLine,
  380. " ");
  381. }
  382. else
  383. {
  384. if (this->OkToGenerate)
  385. {
  386. sprintf(firstLine,
  387. "Press [c] to configure Press [g] to generate and exit");
  388. }
  389. else
  390. {
  391. sprintf(firstLine, "Press [c] to configure ");
  392. }
  393. if (this->AdvancedMode)
  394. {
  395. sprintf(thirdLine, "Press [t] to toggle advanced mode (Currently On)");
  396. }
  397. else
  398. {
  399. sprintf(thirdLine, "Press [t] to toggle advanced mode (Currently Off)");
  400. }
  401. sprintf(secondLine,
  402. "Press [h] for help "
  403. "Press [q] to quit without generating");
  404. }
  405. curses_move(y-4,0);
  406. char fmt[512] = "Press [enter] to edit option";
  407. if ( process )
  408. {
  409. strcpy(fmt, " ");
  410. }
  411. printw(fmt);
  412. curses_move(y-3,0);
  413. printw(firstLine);
  414. curses_move(y-2,0);
  415. printw(secondLine);
  416. curses_move(y-1,0);
  417. printw(thirdLine);
  418. if (cw)
  419. {
  420. sprintf(firstLine, "Page %d of %d", cw->GetPage(), this->NumberOfPages);
  421. curses_move(0,65-static_cast<unsigned int>(strlen(firstLine))-1);
  422. printw(firstLine);
  423. }
  424. // }
  425. pos_form_cursor(this->Form);
  426. }
  427. // Print the key of the current entry and the CMake version
  428. // on the status bar. Designed for a width of 80 chars.
  429. void cmCursesMainForm::UpdateStatusBar(const char* message)
  430. {
  431. int x,y;
  432. getmaxyx(stdscr, y, x);
  433. // If window size is too small, display error and return
  434. if ( x < cmCursesMainForm::MIN_WIDTH ||
  435. x < this->InitialWidth ||
  436. y < cmCursesMainForm::MIN_HEIGHT )
  437. {
  438. curses_clear();
  439. curses_move(0,0);
  440. char fmt[] = "Window is too small. A size of at least %dx%d is required.";
  441. printw(fmt,
  442. (cmCursesMainForm::MIN_WIDTH < this->InitialWidth ?
  443. this->InitialWidth : cmCursesMainForm::MIN_WIDTH),
  444. cmCursesMainForm::MIN_HEIGHT);
  445. touchwin(stdscr);
  446. wrefresh(stdscr);
  447. return;
  448. }
  449. // Get the key of the current entry
  450. FIELD* cur = current_field(this->Form);
  451. int findex = field_index(cur);
  452. cmCursesWidget* lbl = 0;
  453. if ( findex >= 0 )
  454. {
  455. lbl = reinterpret_cast<cmCursesWidget*>(field_userptr(this->Fields[findex-2]));
  456. }
  457. char help[128] = "";
  458. const char* curField = "";
  459. if ( lbl )
  460. {
  461. curField = lbl->GetValue();
  462. // Get the help string of the current entry
  463. // and add it to the help string
  464. cmCacheManager::CacheIterator it =
  465. this->CMakeInstance->GetCacheManager()->GetCacheIterator(curField);
  466. if (!it.IsAtEnd())
  467. {
  468. const char* hs = it.GetProperty("HELPSTRING");
  469. if ( hs )
  470. {
  471. strncpy(help, hs, 127);
  472. help[127] = '\0';
  473. }
  474. else
  475. {
  476. help[0] = 0;
  477. }
  478. }
  479. else
  480. {
  481. sprintf(help," ");
  482. }
  483. }
  484. // Join the key, help string and pad with spaces
  485. // (or truncate) as necessary
  486. char bar[cmCursesMainForm::MAX_WIDTH];
  487. size_t i, curFieldLen = strlen(curField);
  488. size_t helpLen = strlen(help);
  489. size_t width;
  490. if (x < cmCursesMainForm::MAX_WIDTH )
  491. {
  492. width = x;
  493. }
  494. else
  495. {
  496. width = cmCursesMainForm::MAX_WIDTH;
  497. }
  498. if ( message )
  499. {
  500. curField = message;
  501. curFieldLen = strlen(message);
  502. if ( curFieldLen < width )
  503. {
  504. strcpy(bar, curField);
  505. for(i=curFieldLen; i < width; ++i)
  506. {
  507. bar[i] = ' ';
  508. }
  509. }
  510. else
  511. {
  512. strncpy(bar, curField, width);
  513. }
  514. }
  515. else
  516. {
  517. if (curFieldLen >= width)
  518. {
  519. strncpy(bar, curField, width);
  520. }
  521. else
  522. {
  523. strcpy(bar, curField);
  524. bar[curFieldLen] = ':';
  525. bar[curFieldLen+1] = ' ';
  526. if (curFieldLen + helpLen + 2 >= width)
  527. {
  528. strncpy(bar+curFieldLen+2, help, width
  529. - curFieldLen - 2);
  530. }
  531. else
  532. {
  533. strcpy(bar+curFieldLen+2, help);
  534. for(i=curFieldLen+helpLen+2; i < width; ++i)
  535. {
  536. bar[i] = ' ';
  537. }
  538. }
  539. }
  540. }
  541. bar[width] = '\0';
  542. // Display CMake version info on the next line
  543. // We want to display this on the right
  544. char version[cmCursesMainForm::MAX_WIDTH];
  545. char vertmp[128];
  546. sprintf(vertmp,"CMake Version %s", cmVersion::GetCMakeVersion());
  547. size_t sideSpace = (width-strlen(vertmp));
  548. for(i=0; i<sideSpace; i++) { version[i] = ' '; }
  549. sprintf(version+sideSpace, "%s", vertmp);
  550. version[width] = '\0';
  551. // Now print both lines
  552. curses_move(y-5,0);
  553. attron(A_STANDOUT);
  554. char format[] = "%s";
  555. printw(format, bar);
  556. attroff(A_STANDOUT);
  557. curses_move(y-4,0);
  558. printw(version);
  559. pos_form_cursor(this->Form);
  560. }
  561. void cmCursesMainForm::UpdateProgress(const char *msg, float prog, void* vp)
  562. {
  563. cmCursesMainForm* cm = static_cast<cmCursesMainForm*>(vp);
  564. if ( !cm )
  565. {
  566. return;
  567. }
  568. char tmp[1024];
  569. const char *cmsg = tmp;
  570. if ( prog >= 0 )
  571. {
  572. sprintf(tmp, "%s %i%%",msg,(int)(100*prog));
  573. }
  574. else
  575. {
  576. cmsg = msg;
  577. }
  578. cm->UpdateStatusBar(cmsg);
  579. cm->PrintKeys(1);
  580. curses_move(1,1);
  581. touchwin(stdscr);
  582. refresh();
  583. }
  584. int cmCursesMainForm::Configure(int noconfigure)
  585. {
  586. int xi,yi;
  587. getmaxyx(stdscr, yi, xi);
  588. curses_move(1,1);
  589. this->UpdateStatusBar("Configuring, please wait...");
  590. this->PrintKeys(1);
  591. touchwin(stdscr);
  592. refresh();
  593. this->CMakeInstance->SetProgressCallback(cmCursesMainForm::UpdateProgress, this);
  594. // always save the current gui values to disk
  595. this->FillCacheManagerFromUI();
  596. this->CMakeInstance->GetCacheManager()->SaveCache(
  597. this->CMakeInstance->GetHomeOutputDirectory());
  598. this->LoadCache(0);
  599. // Get rid of previous errors
  600. this->Errors = std::vector<std::string>();
  601. // run the generate process
  602. this->OkToGenerate = true;
  603. int retVal;
  604. if ( noconfigure )
  605. {
  606. retVal = this->CMakeInstance->DoPreConfigureChecks();
  607. this->OkToGenerate = false;
  608. if ( retVal > 0 )
  609. {
  610. retVal = 0;
  611. }
  612. }
  613. else
  614. {
  615. retVal = this->CMakeInstance->Configure();
  616. }
  617. this->CMakeInstance->SetProgressCallback(0, 0);
  618. keypad(stdscr,TRUE); /* Use key symbols as
  619. KEY_DOWN*/
  620. if( retVal != 0 || !this->Errors.empty())
  621. {
  622. // see if there was an error
  623. if(cmSystemTools::GetErrorOccuredFlag())
  624. {
  625. this->OkToGenerate = false;
  626. }
  627. int xx,yy;
  628. getmaxyx(stdscr, yy, xx);
  629. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  630. this->Errors,
  631. cmSystemTools::GetErrorOccuredFlag()
  632. ? "Errors occurred during the last pass." :
  633. "CMake produced the following output.");
  634. // reset error condition
  635. cmSystemTools::ResetErrorOccuredFlag();
  636. CurrentForm = msgs;
  637. msgs->Render(1,1,xx,yy);
  638. msgs->HandleInput();
  639. // If they typed the wrong source directory, we report
  640. // an error and exit
  641. if ( retVal == -2 )
  642. {
  643. return retVal;
  644. }
  645. CurrentForm = this;
  646. this->Render(1,1,xx,yy);
  647. }
  648. this->InitializeUI();
  649. this->Render(1, 1, xi, yi);
  650. return 0;
  651. }
  652. int cmCursesMainForm::Generate()
  653. {
  654. int xi,yi;
  655. getmaxyx(stdscr, yi, xi);
  656. curses_move(1,1);
  657. this->UpdateStatusBar("Generating, please wait...");
  658. this->PrintKeys(1);
  659. touchwin(stdscr);
  660. refresh();
  661. this->CMakeInstance->SetProgressCallback(cmCursesMainForm::UpdateProgress, this);
  662. // Get rid of previous errors
  663. this->Errors = std::vector<std::string>();
  664. // run the generate process
  665. int retVal = this->CMakeInstance->Generate();
  666. this->CMakeInstance->SetProgressCallback(0, 0);
  667. keypad(stdscr,TRUE); /* Use key symbols as
  668. KEY_DOWN*/
  669. if( retVal != 0 || !this->Errors.empty())
  670. {
  671. // see if there was an error
  672. if(cmSystemTools::GetErrorOccuredFlag())
  673. {
  674. this->OkToGenerate = false;
  675. }
  676. // reset error condition
  677. cmSystemTools::ResetErrorOccuredFlag();
  678. int xx,yy;
  679. getmaxyx(stdscr, yy, xx);
  680. const char* title = "Messages during last pass.";
  681. if(cmSystemTools::GetErrorOccuredFlag())
  682. {
  683. title = "Errors occurred during the last pass.";
  684. }
  685. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(this->Errors,
  686. title);
  687. CurrentForm = msgs;
  688. msgs->Render(1,1,xx,yy);
  689. msgs->HandleInput();
  690. // If they typed the wrong source directory, we report
  691. // an error and exit
  692. if ( retVal == -2 )
  693. {
  694. return retVal;
  695. }
  696. CurrentForm = this;
  697. this->Render(1,1,xx,yy);
  698. }
  699. this->InitializeUI();
  700. this->Render(1, 1, xi, yi);
  701. return 0;
  702. }
  703. void cmCursesMainForm::AddError(const char* message, const char*)
  704. {
  705. this->Errors.push_back(message);
  706. }
  707. void cmCursesMainForm::RemoveEntry(const char* value)
  708. {
  709. if (!value)
  710. {
  711. return;
  712. }
  713. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  714. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  715. {
  716. const char* val = (*it)->GetValue();
  717. if ( val && !strcmp(value, val) )
  718. {
  719. this->CMakeInstance->UnwatchUnusedCli(value);
  720. this->Entries->erase(it);
  721. break;
  722. }
  723. }
  724. }
  725. // copy from the list box to the cache manager
  726. void cmCursesMainForm::FillCacheManagerFromUI()
  727. {
  728. size_t size = this->Entries->size();
  729. for(size_t i=0; i < size; i++)
  730. {
  731. cmCacheManager::CacheIterator it =
  732. this->CMakeInstance->GetCacheManager()->GetCacheIterator(
  733. (*this->Entries)[i]->Key.c_str());
  734. if (!it.IsAtEnd())
  735. {
  736. std::string oldValue = it.GetValue();
  737. std::string newValue = (*this->Entries)[i]->Entry->GetValue();
  738. std::string fixedOldValue;
  739. std::string fixedNewValue;
  740. this->FixValue(it.GetType(), oldValue, fixedOldValue);
  741. this->FixValue(it.GetType(), newValue, fixedNewValue);
  742. if(!(fixedOldValue == fixedNewValue))
  743. {
  744. // The user has changed the value. Mark it as modified.
  745. it.SetProperty("MODIFIED", true);
  746. it.SetValue(fixedNewValue.c_str());
  747. }
  748. }
  749. }
  750. }
  751. void cmCursesMainForm::FixValue(cmCacheManager::CacheEntryType type,
  752. const std::string& in, std::string& out) const
  753. {
  754. out = in.substr(0,in.find_last_not_of(" ")+1);
  755. if(type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH)
  756. {
  757. cmSystemTools::ConvertToUnixSlashes(out);
  758. }
  759. if(type == cmCacheManager::BOOL)
  760. {
  761. if(cmSystemTools::IsOff(out.c_str()))
  762. {
  763. out = "OFF";
  764. }
  765. else
  766. {
  767. out = "ON";
  768. }
  769. }
  770. }
  771. #include <unistd.h>
  772. void cmCursesMainForm::HandleInput()
  773. {
  774. int x=0,y=0;
  775. if (!this->Form)
  776. {
  777. return;
  778. }
  779. FIELD* currentField;
  780. cmCursesWidget* currentWidget;
  781. char debugMessage[128];
  782. for(;;)
  783. {
  784. this->UpdateStatusBar();
  785. this->PrintKeys();
  786. if ( this->SearchMode )
  787. {
  788. std::string searchstr = "Search: " + this->SearchString;
  789. this->UpdateStatusBar( searchstr.c_str() );
  790. this->PrintKeys(1);
  791. curses_move(y-5,static_cast<unsigned int>(searchstr.size()));
  792. //curses_move(1,1);
  793. touchwin(stdscr);
  794. refresh();
  795. }
  796. int key = getch();
  797. getmaxyx(stdscr, y, x);
  798. // If window too small, handle 'q' only
  799. if ( x < cmCursesMainForm::MIN_WIDTH ||
  800. y < cmCursesMainForm::MIN_HEIGHT )
  801. {
  802. // quit
  803. if ( key == 'q' )
  804. {
  805. break;
  806. }
  807. else
  808. {
  809. continue;
  810. }
  811. }
  812. currentField = current_field(this->Form);
  813. currentWidget = reinterpret_cast<cmCursesWidget*>(field_userptr(
  814. currentField));
  815. bool widgetHandled=false;
  816. if ( this->SearchMode )
  817. {
  818. if ( key == 10 || key == KEY_ENTER )
  819. {
  820. this->SearchMode = false;
  821. if ( this->SearchString.size() > 0 )
  822. {
  823. this->JumpToCacheEntry(this->SearchString.c_str());
  824. this->OldSearchString = this->SearchString;
  825. }
  826. this->SearchString = "";
  827. }
  828. /*
  829. else if ( key == KEY_ESCAPE )
  830. {
  831. this->SearchMode = false;
  832. }
  833. */
  834. else if ((key >= 'a' && key <= 'z') ||
  835. (key >= 'A' && key <= 'Z') ||
  836. (key >= '0' && key <= '9') ||
  837. (key == '_' ))
  838. {
  839. if ( this->SearchString.size() < static_cast<std::string::size_type>(x-10) )
  840. {
  841. this->SearchString += static_cast<char>(key);
  842. }
  843. }
  844. else if ( key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC )
  845. {
  846. if ( this->SearchString.size() > 0 )
  847. {
  848. this->SearchString.resize(this->SearchString.size()-1);
  849. }
  850. }
  851. }
  852. else if (currentWidget && !this->SearchMode)
  853. {
  854. // Ask the current widget if it wants to handle input
  855. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  856. if (widgetHandled)
  857. {
  858. this->OkToGenerate = false;
  859. this->UpdateStatusBar();
  860. this->PrintKeys();
  861. }
  862. }
  863. if ((!currentWidget || !widgetHandled) && !this->SearchMode)
  864. {
  865. // If the current widget does not want to handle input,
  866. // we handle it.
  867. sprintf(debugMessage, "Main form handling input, key: %d", key);
  868. cmCursesForm::LogMessage(debugMessage);
  869. // quit
  870. if ( key == 'q' )
  871. {
  872. break;
  873. }
  874. // if not end of page, next field otherwise next page
  875. // each entry consists of fields: label, isnew, value
  876. // therefore, the label field for the prev. entry is index-5
  877. // and the label field for the next entry is index+1
  878. // (index always corresponds to the value field)
  879. else if ( key == KEY_DOWN || key == ctrl('n') )
  880. {
  881. FIELD* cur = current_field(this->Form);
  882. size_t findex = field_index(cur);
  883. if ( findex == 3*this->NumberOfVisibleEntries-1 )
  884. {
  885. continue;
  886. }
  887. if (new_page(this->Fields[findex+1]))
  888. {
  889. form_driver(this->Form, REQ_NEXT_PAGE);
  890. }
  891. else
  892. {
  893. form_driver(this->Form, REQ_NEXT_FIELD);
  894. }
  895. }
  896. // if not beginning of page, previous field, otherwise previous page
  897. // each entry consists of fields: label, isnew, value
  898. // therefore, the label field for the prev. entry is index-5
  899. // and the label field for the next entry is index+1
  900. // (index always corresponds to the value field)
  901. else if ( key == KEY_UP || key == ctrl('p') )
  902. {
  903. FIELD* cur = current_field(this->Form);
  904. int findex = field_index(cur);
  905. if ( findex == 2 )
  906. {
  907. continue;
  908. }
  909. if ( new_page(this->Fields[findex-2]) )
  910. {
  911. form_driver(this->Form, REQ_PREV_PAGE);
  912. set_current_field(this->Form, this->Fields[findex-3]);
  913. }
  914. else
  915. {
  916. form_driver(this->Form, REQ_PREV_FIELD);
  917. }
  918. }
  919. // pg down
  920. else if ( key == KEY_NPAGE || key == ctrl('d') )
  921. {
  922. form_driver(this->Form, REQ_NEXT_PAGE);
  923. }
  924. // pg up
  925. else if ( key == KEY_PPAGE || key == ctrl('u') )
  926. {
  927. form_driver(this->Form, REQ_PREV_PAGE);
  928. }
  929. // configure
  930. else if ( key == 'c' )
  931. {
  932. this->Configure();
  933. }
  934. // display help
  935. else if ( key == 'h' )
  936. {
  937. getmaxyx(stdscr, y, x);
  938. FIELD* cur = current_field(this->Form);
  939. int findex = field_index(cur);
  940. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(field_userptr(
  941. this->Fields[findex-2]));
  942. const char* curField = lbl->GetValue();
  943. const char* helpString=0;
  944. cmCacheManager::CacheIterator it =
  945. this->CMakeInstance->GetCacheManager()->GetCacheIterator(curField);
  946. if (!it.IsAtEnd())
  947. {
  948. helpString = it.GetProperty("HELPSTRING");
  949. }
  950. if (helpString)
  951. {
  952. char* message = new char[strlen(curField)+strlen(helpString)
  953. +strlen("Current option is: \n Help string for this option is: \n")+10];
  954. sprintf(message,"Current option is: %s\nHelp string for this option is: %s\n", curField, helpString);
  955. this->HelpMessage[1] = message;
  956. delete[] message;
  957. }
  958. else
  959. {
  960. this->HelpMessage[1] = "";
  961. }
  962. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(this->HelpMessage,
  963. "Help.");
  964. CurrentForm = msgs;
  965. msgs->Render(1,1,x,y);
  966. msgs->HandleInput();
  967. CurrentForm = this;
  968. this->Render(1,1,x,y);
  969. set_current_field(this->Form, cur);
  970. }
  971. // display last errors
  972. else if ( key == 'l' )
  973. {
  974. getmaxyx(stdscr, y, x);
  975. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(this->Errors,
  976. "Errors occurred during the last pass.");
  977. CurrentForm = msgs;
  978. msgs->Render(1,1,x,y);
  979. msgs->HandleInput();
  980. CurrentForm = this;
  981. this->Render(1,1,x,y);
  982. }
  983. else if ( key == '/' )
  984. {
  985. this->SearchMode = true;
  986. this->UpdateStatusBar("Search");
  987. this->PrintKeys(1);
  988. touchwin(stdscr);
  989. refresh();
  990. }
  991. else if ( key == 'n' )
  992. {
  993. if ( this->OldSearchString.size() > 0 )
  994. {
  995. this->JumpToCacheEntry(this->OldSearchString.c_str());
  996. }
  997. }
  998. // switch advanced on/off
  999. else if ( key == 't' )
  1000. {
  1001. if (this->AdvancedMode)
  1002. {
  1003. this->AdvancedMode = false;
  1004. }
  1005. else
  1006. {
  1007. this->AdvancedMode = true;
  1008. }
  1009. getmaxyx(stdscr, y, x);
  1010. this->RePost();
  1011. this->Render(1, 1, x, y);
  1012. }
  1013. // generate and exit
  1014. else if ( key == 'g' )
  1015. {
  1016. if ( this->OkToGenerate )
  1017. {
  1018. this->Generate();
  1019. break;
  1020. }
  1021. }
  1022. // delete cache entry
  1023. else if ( key == 'd' && this->NumberOfVisibleEntries )
  1024. {
  1025. this->OkToGenerate = false;
  1026. FIELD* cur = current_field(this->Form);
  1027. size_t findex = field_index(cur);
  1028. // make the next or prev. current field after deletion
  1029. // each entry consists of fields: label, isnew, value
  1030. // therefore, the label field for the prev. entry is findex-5
  1031. // and the label field for the next entry is findex+1
  1032. // (findex always corresponds to the value field)
  1033. FIELD* nextCur;
  1034. if ( findex == 2 )
  1035. {
  1036. nextCur=0;
  1037. }
  1038. else if ( findex == 3*this->NumberOfVisibleEntries-1 )
  1039. {
  1040. nextCur = this->Fields[findex-5];
  1041. }
  1042. else
  1043. {
  1044. nextCur = this->Fields[findex+1];
  1045. }
  1046. // Get the label widget
  1047. // each entry consists of fields: label, isnew, value
  1048. // therefore, the label field for the is findex-2
  1049. // (findex always corresponds to the value field)
  1050. cmCursesWidget* lbl
  1051. = reinterpret_cast<cmCursesWidget*>(
  1052. field_userptr(this->Fields[findex-2]));
  1053. if ( lbl )
  1054. {
  1055. this->CMakeInstance->GetCacheManager()->RemoveCacheEntry(lbl->GetValue());
  1056. std::string nextVal;
  1057. if (nextCur)
  1058. {
  1059. nextVal = (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))->GetValue());
  1060. }
  1061. getmaxyx(stdscr, y, x);
  1062. this->RemoveEntry(lbl->GetValue());
  1063. this->RePost();
  1064. this->Render(1, 1, x, y);
  1065. if (nextCur)
  1066. {
  1067. // make the next or prev. current field after deletion
  1068. nextCur = 0;
  1069. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  1070. for (it = this->Entries->begin(); it != this->Entries->end(); ++it)
  1071. {
  1072. if (nextVal == (*it)->Key)
  1073. {
  1074. nextCur = (*it)->Entry->Field;
  1075. }
  1076. }
  1077. if (nextCur)
  1078. {
  1079. set_current_field(this->Form, nextCur);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. }
  1085. touchwin(stdscr);
  1086. wrefresh(stdscr);
  1087. }
  1088. }
  1089. int cmCursesMainForm::LoadCache(const char *)
  1090. {
  1091. int r = this->CMakeInstance->LoadCache();
  1092. if(r < 0)
  1093. {
  1094. return r;
  1095. }
  1096. this->CMakeInstance->SetCacheArgs(this->Args);
  1097. this->CMakeInstance->PreLoadCMakeFiles();
  1098. return r;
  1099. }
  1100. void cmCursesMainForm::JumpToCacheEntry(const char* astr)
  1101. {
  1102. std::string str;
  1103. if ( astr )
  1104. {
  1105. str = cmSystemTools::LowerCase(astr);
  1106. }
  1107. if(str.empty())
  1108. {
  1109. return;
  1110. }
  1111. FIELD* cur = current_field(this->Form);
  1112. int start_index = field_index(cur);
  1113. int findex = start_index;
  1114. for(;;)
  1115. {
  1116. if ( str.size() > 0 )
  1117. {
  1118. cmCursesWidget* lbl = 0;
  1119. if ( findex >= 0 )
  1120. {
  1121. lbl = reinterpret_cast<cmCursesWidget*>(field_userptr(this->Fields[findex-2]));
  1122. }
  1123. if ( lbl )
  1124. {
  1125. const char* curField = lbl->GetValue();
  1126. if ( curField )
  1127. {
  1128. std::string cfld = cmSystemTools::LowerCase(curField);
  1129. if ( cfld.find(str) != cfld.npos && findex != start_index )
  1130. {
  1131. break;
  1132. }
  1133. }
  1134. }
  1135. }
  1136. if ( size_t(findex) >= 3* this->NumberOfVisibleEntries-1 )
  1137. {
  1138. set_current_field(this->Form, this->Fields[2]);
  1139. }
  1140. else if (new_page(this->Fields[findex+1]))
  1141. {
  1142. form_driver(this->Form, REQ_NEXT_PAGE);
  1143. }
  1144. else
  1145. {
  1146. form_driver(this->Form, REQ_NEXT_FIELD);
  1147. }
  1148. /*
  1149. char buffer[1024];
  1150. sprintf(buffer, "Line: %d != %d / %d\n", findex, idx, this->NumberOfVisibleEntries);
  1151. touchwin(stdscr);
  1152. refresh();
  1153. this->UpdateStatusBar( buffer );
  1154. usleep(100000);
  1155. */
  1156. cur = current_field(this->Form);
  1157. findex = field_index(cur);
  1158. if ( findex == start_index )
  1159. {
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. const char* cmCursesMainForm::s_ConstHelpMessage =
  1165. "CMake is used to configure and generate build files for software projects. "
  1166. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  1167. "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"
  1168. "2. When ccmake is run, it will read the configuration files and display the current build options. "
  1169. "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 *. "
  1170. "On the other hand, the first time you run ccmake, all build options will be new and will be marked as such. "
  1171. "At this point, you can modify any options (see keys below) you want to change. "
  1172. "When you are satisfied with your changes, press 'c' to have CMake process the configuration files. "
  1173. "Please note that changing some options may cause new ones to appear. These will be shown on top and will be marked with *. "
  1174. "Repeat this procedure until you are satisfied with all the options and there are no new entries. "
  1175. "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. "
  1176. "At any point during the process, you can exit ccmake with 'q'. However, this will not generate/change any build files.\n\n"
  1177. "ccmake KEYS:\n\n"
  1178. "Navigation: "
  1179. "You can use the arrow keys and page up, down to navigate the options. Alternatively, you can use the following keys: \n"
  1180. " C-n : next option\n"
  1181. " C-p : previous options\n"
  1182. " C-d : down one page\n"
  1183. " C-u : up one page\n\n"
  1184. "Editing options: "
  1185. "To change an option press enter or return. If the current options is a boolean, this will toggle it's value. "
  1186. "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"
  1187. " C-b : back one character\n"
  1188. " C-f : forward one character\n"
  1189. " C-a : go to the beginning of the field\n"
  1190. " C-e : go to the end of the field\n"
  1191. " C-d : delete previous character\n"
  1192. " C-k : kill the rest of the field\n"
  1193. " Esc : Restore field (discard last changes)\n"
  1194. " Enter : Leave edit mode\n"
  1195. "You can also delete an option by pressing 'd'\n\n"
  1196. "Commands:\n"
  1197. " q : quit ccmake without generating build files\n"
  1198. " h : help, shows this screen\n"
  1199. " c : process the configuration files with the current options\n"
  1200. " g : generate build files and exit, only available when there are no "
  1201. "new options and no errors have been detected during last configuration.\n"
  1202. " l : shows last errors\n"
  1203. " 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"
  1204. " / : search for a variable name.\n";