cmCursesMainForm.cxx 31 KB

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