cmCursesMainForm.cxx 36 KB

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