cmCursesMainForm.cxx 35 KB

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