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. 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);
  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, "Press [h] for help "
  340. "Press [q] to quit without generating");
  341. }
  342. curses_move(y - 4, 0);
  343. char fmt[512] =
  344. "Press [enter] to edit option Press [d] to delete an entry";
  345. if (process) {
  346. memset(fmt, ' ', 27);
  347. }
  348. printw(fmt_s, fmt);
  349. curses_move(y - 3, 0);
  350. printw(fmt_s, firstLine);
  351. curses_move(y - 2, 0);
  352. printw(fmt_s, secondLine);
  353. curses_move(y - 1, 0);
  354. printw(fmt_s, thirdLine);
  355. }
  356. if (cw) {
  357. char pageLine[512] = "";
  358. sprintf(pageLine, "Page %d of %d", cw->GetPage(), this->NumberOfPages);
  359. curses_move(0, 65 - static_cast<unsigned int>(strlen(pageLine)) - 1);
  360. printw(fmt_s, pageLine);
  361. }
  362. pos_form_cursor(this->Form);
  363. }
  364. // Print the key of the current entry and the CMake version
  365. // on the status bar. Designed for a width of 80 chars.
  366. void cmCursesMainForm::UpdateStatusBar(const char* message)
  367. {
  368. int x, y;
  369. getmaxyx(stdscr, y, x);
  370. // If window size is too small, display error and return
  371. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  372. y < cmCursesMainForm::MIN_HEIGHT) {
  373. curses_clear();
  374. curses_move(0, 0);
  375. char fmt[] = "Window is too small. A size of at least %dx%d is required.";
  376. printw(fmt, (cmCursesMainForm::MIN_WIDTH < this->InitialWidth
  377. ? this->InitialWidth
  378. : cmCursesMainForm::MIN_WIDTH),
  379. cmCursesMainForm::MIN_HEIGHT);
  380. touchwin(stdscr);
  381. wrefresh(stdscr);
  382. return;
  383. }
  384. // Get the key of the current entry
  385. FIELD* cur = current_field(this->Form);
  386. int findex = field_index(cur);
  387. cmCursesWidget* lbl = nullptr;
  388. if (findex >= 0) {
  389. lbl = reinterpret_cast<cmCursesWidget*>(
  390. field_userptr(this->Fields[findex - 2]));
  391. }
  392. char help[128] = "";
  393. const char* curField = "";
  394. if (lbl) {
  395. curField = lbl->GetValue();
  396. // Get the help string of the current entry
  397. // and add it to the help string
  398. const char* existingValue =
  399. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  400. if (existingValue) {
  401. const char* hs = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  402. curField, "HELPSTRING");
  403. if (hs) {
  404. strncpy(help, hs, 127);
  405. help[127] = '\0';
  406. } else {
  407. help[0] = 0;
  408. }
  409. } else {
  410. sprintf(help, " ");
  411. }
  412. }
  413. // Join the key, help string and pad with spaces
  414. // (or truncate) as necessary
  415. char bar[cmCursesMainForm::MAX_WIDTH];
  416. size_t curFieldLen = strlen(curField);
  417. size_t helpLen = strlen(help);
  418. size_t width = std::min<size_t>(x, cmCursesMainForm::MAX_WIDTH);
  419. if (message) {
  420. curField = message;
  421. curFieldLen = strlen(message);
  422. strncpy(bar, curField, width);
  423. if (curFieldLen < width) {
  424. memset(bar + curFieldLen, ' ', width - curFieldLen);
  425. }
  426. } else {
  427. strncpy(bar, curField, width);
  428. if (curFieldLen < width) {
  429. bar[curFieldLen] = ':';
  430. bar[curFieldLen + 1] = ' ';
  431. strncpy(bar + curFieldLen + 2, help, width - curFieldLen - 2);
  432. if (curFieldLen + helpLen + 2 < width) {
  433. memset(bar + curFieldLen + helpLen + 2, ' ',
  434. width - (curFieldLen + helpLen + 2));
  435. }
  436. }
  437. }
  438. bar[width] = '\0';
  439. // Display CMake version info on the next line
  440. // We want to display this on the right
  441. char version[cmCursesMainForm::MAX_WIDTH];
  442. char vertmp[128];
  443. sprintf(vertmp, "CMake Version %s", cmVersion::GetCMakeVersion());
  444. size_t sideSpace = (width - strlen(vertmp));
  445. memset(version, ' ', sideSpace);
  446. sprintf(version + sideSpace, "%s", vertmp);
  447. version[width] = '\0';
  448. // Now print both lines
  449. char fmt_s[] = "%s";
  450. curses_move(y - 5, 0);
  451. attron(A_STANDOUT);
  452. printw(fmt_s, bar);
  453. attroff(A_STANDOUT);
  454. curses_move(y - 4, 0);
  455. printw(fmt_s, version);
  456. pos_form_cursor(this->Form);
  457. }
  458. void cmCursesMainForm::UpdateProgress(const char* msg, float prog, void* vp)
  459. {
  460. cmCursesMainForm* cm = static_cast<cmCursesMainForm*>(vp);
  461. if (!cm) {
  462. return;
  463. }
  464. char tmp[1024];
  465. const char* cmsg = tmp;
  466. if (prog >= 0) {
  467. sprintf(tmp, "%s %i%%", msg, static_cast<int>(100 * prog));
  468. } else {
  469. cmsg = msg;
  470. }
  471. cm->UpdateStatusBar(cmsg);
  472. cm->PrintKeys(1);
  473. curses_move(1, 1);
  474. touchwin(stdscr);
  475. refresh();
  476. }
  477. int cmCursesMainForm::Configure(int noconfigure)
  478. {
  479. int xi, yi;
  480. getmaxyx(stdscr, yi, xi);
  481. curses_move(1, 1);
  482. this->UpdateStatusBar("Configuring, please wait...");
  483. this->PrintKeys(1);
  484. touchwin(stdscr);
  485. refresh();
  486. this->CMakeInstance->SetProgressCallback(cmCursesMainForm::UpdateProgress,
  487. this);
  488. // always save the current gui values to disk
  489. this->FillCacheManagerFromUI();
  490. this->CMakeInstance->SaveCache(
  491. this->CMakeInstance->GetHomeOutputDirectory());
  492. this->LoadCache(nullptr);
  493. // Get rid of previous errors
  494. this->Errors = std::vector<std::string>();
  495. // run the generate process
  496. this->OkToGenerate = true;
  497. int retVal;
  498. if (noconfigure) {
  499. retVal = this->CMakeInstance->DoPreConfigureChecks();
  500. this->OkToGenerate = false;
  501. if (retVal > 0) {
  502. retVal = 0;
  503. }
  504. } else {
  505. retVal = this->CMakeInstance->Configure();
  506. }
  507. this->CMakeInstance->SetProgressCallback(nullptr, nullptr);
  508. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  509. if (retVal != 0 || !this->Errors.empty()) {
  510. // see if there was an error
  511. if (cmSystemTools::GetErrorOccuredFlag()) {
  512. this->OkToGenerate = false;
  513. }
  514. int xx, yy;
  515. getmaxyx(stdscr, yy, xx);
  516. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  517. this->Errors, 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(cmCursesMainForm::UpdateProgress,
  547. this);
  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, 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.push_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.c_str())) {
  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";