cmCursesMainForm.cxx 26 KB

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