cmCursesMainForm.cxx 35 KB

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