cmCursesMainForm.cxx 35 KB

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