cmCursesMainForm.cxx 35 KB

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