cmCursesMainForm.cxx 35 KB

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