cmCursesMainForm.cxx 34 KB

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