cmCursesMainForm.cxx 34 KB

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