cmCursesMainForm.cxx 35 KB

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