cmCursesMainForm.cxx 36 KB

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