cmCursesMainForm.cxx 35 KB

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