cmCursesMainForm.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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;
  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. if (cw) {
  316. cw->PrintKeys();
  317. }
  318. // {
  319. // }
  320. // else
  321. // {
  322. char firstLine[512] = "";
  323. char secondLine[512] = "";
  324. char thirdLine[512] = "";
  325. if (process) {
  326. const char* clearLine =
  327. " ";
  328. strcpy(firstLine, clearLine);
  329. strcpy(secondLine, clearLine);
  330. strcpy(thirdLine, clearLine);
  331. } else {
  332. if (this->OkToGenerate) {
  333. sprintf(firstLine,
  334. "Press [c] to configure Press [g] to generate and exit");
  335. } else {
  336. sprintf(firstLine,
  337. "Press [c] to configure ");
  338. }
  339. {
  340. const char* toggleKeyInstruction =
  341. "Press [t] to toggle advanced mode (Currently %s)";
  342. sprintf(thirdLine, toggleKeyInstruction,
  343. this->AdvancedMode ? "On" : "Off");
  344. }
  345. sprintf(secondLine, "Press [h] for help "
  346. "Press [q] to quit without generating");
  347. }
  348. curses_move(y - 4, 0);
  349. char fmt_s[] = "%s";
  350. char fmt[512] = "Press [enter] to edit option Press [d] to delete an entry";
  351. if (process) {
  352. strcpy(fmt, " ");
  353. }
  354. printw(fmt_s, fmt);
  355. curses_move(y - 3, 0);
  356. printw(fmt_s, firstLine);
  357. curses_move(y - 2, 0);
  358. printw(fmt_s, secondLine);
  359. curses_move(y - 1, 0);
  360. printw(fmt_s, thirdLine);
  361. if (cw) {
  362. sprintf(firstLine, "Page %d of %d", cw->GetPage(), this->NumberOfPages);
  363. curses_move(0, 65 - static_cast<unsigned int>(strlen(firstLine)) - 1);
  364. printw(fmt_s, firstLine);
  365. }
  366. // }
  367. pos_form_cursor(this->Form);
  368. }
  369. // Print the key of the current entry and the CMake version
  370. // on the status bar. Designed for a width of 80 chars.
  371. void cmCursesMainForm::UpdateStatusBar(const char* message)
  372. {
  373. int x, y;
  374. getmaxyx(stdscr, y, x);
  375. // If window size is too small, display error and return
  376. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  377. y < cmCursesMainForm::MIN_HEIGHT) {
  378. curses_clear();
  379. curses_move(0, 0);
  380. char fmt[] = "Window is too small. A size of at least %dx%d is required.";
  381. printw(fmt, (cmCursesMainForm::MIN_WIDTH < this->InitialWidth
  382. ? this->InitialWidth
  383. : cmCursesMainForm::MIN_WIDTH),
  384. cmCursesMainForm::MIN_HEIGHT);
  385. touchwin(stdscr);
  386. wrefresh(stdscr);
  387. return;
  388. }
  389. // Get the key of the current entry
  390. FIELD* cur = current_field(this->Form);
  391. int findex = field_index(cur);
  392. cmCursesWidget* lbl = CM_NULLPTR;
  393. if (findex >= 0) {
  394. lbl = reinterpret_cast<cmCursesWidget*>(
  395. field_userptr(this->Fields[findex - 2]));
  396. }
  397. char help[128] = "";
  398. const char* curField = "";
  399. if (lbl) {
  400. curField = lbl->GetValue();
  401. // Get the help string of the current entry
  402. // and add it to the help string
  403. const char* existingValue =
  404. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  405. if (existingValue) {
  406. const char* hs = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  407. curField, "HELPSTRING");
  408. if (hs) {
  409. strncpy(help, hs, 127);
  410. help[127] = '\0';
  411. } else {
  412. help[0] = 0;
  413. }
  414. } else {
  415. sprintf(help, " ");
  416. }
  417. }
  418. // Join the key, help string and pad with spaces
  419. // (or truncate) as necessary
  420. char bar[cmCursesMainForm::MAX_WIDTH];
  421. size_t i, curFieldLen = strlen(curField);
  422. size_t helpLen = strlen(help);
  423. size_t width;
  424. if (x < cmCursesMainForm::MAX_WIDTH) {
  425. width = x;
  426. } else {
  427. width = cmCursesMainForm::MAX_WIDTH;
  428. }
  429. if (message) {
  430. curField = message;
  431. curFieldLen = strlen(message);
  432. if (curFieldLen < width) {
  433. strcpy(bar, curField);
  434. for (i = curFieldLen; i < width; ++i) {
  435. bar[i] = ' ';
  436. }
  437. } else {
  438. strncpy(bar, curField, width);
  439. }
  440. } else {
  441. if (curFieldLen >= width) {
  442. strncpy(bar, curField, width);
  443. } else {
  444. strcpy(bar, curField);
  445. bar[curFieldLen] = ':';
  446. bar[curFieldLen + 1] = ' ';
  447. if (curFieldLen + helpLen + 2 >= width) {
  448. strncpy(bar + curFieldLen + 2, help, width - curFieldLen - 2);
  449. } else {
  450. strcpy(bar + curFieldLen + 2, help);
  451. for (i = curFieldLen + helpLen + 2; i < width; ++i) {
  452. bar[i] = ' ';
  453. }
  454. }
  455. }
  456. }
  457. bar[width] = '\0';
  458. // Display CMake version info on the next line
  459. // We want to display this on the right
  460. char version[cmCursesMainForm::MAX_WIDTH];
  461. char vertmp[128];
  462. sprintf(vertmp, "CMake Version %s", cmVersion::GetCMakeVersion());
  463. size_t sideSpace = (width - strlen(vertmp));
  464. for (i = 0; i < sideSpace; i++) {
  465. version[i] = ' ';
  466. }
  467. sprintf(version + sideSpace, "%s", vertmp);
  468. version[width] = '\0';
  469. // Now print both lines
  470. char fmt_s[] = "%s";
  471. curses_move(y - 5, 0);
  472. attron(A_STANDOUT);
  473. printw(fmt_s, bar);
  474. attroff(A_STANDOUT);
  475. curses_move(y - 4, 0);
  476. printw(fmt_s, version);
  477. pos_form_cursor(this->Form);
  478. }
  479. void cmCursesMainForm::UpdateProgress(const char* msg, float prog, void* vp)
  480. {
  481. cmCursesMainForm* cm = static_cast<cmCursesMainForm*>(vp);
  482. if (!cm) {
  483. return;
  484. }
  485. char tmp[1024];
  486. const char* cmsg = tmp;
  487. if (prog >= 0) {
  488. sprintf(tmp, "%s %i%%", msg, (int)(100 * prog));
  489. } else {
  490. cmsg = msg;
  491. }
  492. cm->UpdateStatusBar(cmsg);
  493. cm->PrintKeys(1);
  494. curses_move(1, 1);
  495. touchwin(stdscr);
  496. refresh();
  497. }
  498. int cmCursesMainForm::Configure(int noconfigure)
  499. {
  500. int xi, yi;
  501. getmaxyx(stdscr, yi, xi);
  502. curses_move(1, 1);
  503. this->UpdateStatusBar("Configuring, please wait...");
  504. this->PrintKeys(1);
  505. touchwin(stdscr);
  506. refresh();
  507. this->CMakeInstance->SetProgressCallback(cmCursesMainForm::UpdateProgress,
  508. this);
  509. // always save the current gui values to disk
  510. this->FillCacheManagerFromUI();
  511. this->CMakeInstance->SaveCache(
  512. this->CMakeInstance->GetHomeOutputDirectory());
  513. this->LoadCache(CM_NULLPTR);
  514. // Get rid of previous errors
  515. this->Errors = std::vector<std::string>();
  516. // run the generate process
  517. this->OkToGenerate = true;
  518. int retVal;
  519. if (noconfigure) {
  520. retVal = this->CMakeInstance->DoPreConfigureChecks();
  521. this->OkToGenerate = false;
  522. if (retVal > 0) {
  523. retVal = 0;
  524. }
  525. } else {
  526. retVal = this->CMakeInstance->Configure();
  527. }
  528. this->CMakeInstance->SetProgressCallback(CM_NULLPTR, CM_NULLPTR);
  529. keypad(stdscr, TRUE); /* Use key symbols as
  530. KEY_DOWN*/
  531. if (retVal != 0 || !this->Errors.empty()) {
  532. // see if there was an error
  533. if (cmSystemTools::GetErrorOccuredFlag()) {
  534. this->OkToGenerate = false;
  535. }
  536. int xx, yy;
  537. getmaxyx(stdscr, yy, xx);
  538. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  539. this->Errors, cmSystemTools::GetErrorOccuredFlag()
  540. ? "Errors occurred during the last pass."
  541. : "CMake produced the following output.");
  542. // reset error condition
  543. cmSystemTools::ResetErrorOccuredFlag();
  544. CurrentForm = msgs;
  545. msgs->Render(1, 1, xx, yy);
  546. msgs->HandleInput();
  547. // If they typed the wrong source directory, we report
  548. // an error and exit
  549. if (retVal == -2) {
  550. return retVal;
  551. }
  552. CurrentForm = this;
  553. this->Render(1, 1, xx, yy);
  554. }
  555. this->InitializeUI();
  556. this->Render(1, 1, xi, yi);
  557. return 0;
  558. }
  559. int cmCursesMainForm::Generate()
  560. {
  561. int xi, yi;
  562. getmaxyx(stdscr, yi, xi);
  563. curses_move(1, 1);
  564. this->UpdateStatusBar("Generating, please wait...");
  565. this->PrintKeys(1);
  566. touchwin(stdscr);
  567. refresh();
  568. this->CMakeInstance->SetProgressCallback(cmCursesMainForm::UpdateProgress,
  569. this);
  570. // Get rid of previous errors
  571. this->Errors = std::vector<std::string>();
  572. // run the generate process
  573. int retVal = this->CMakeInstance->Generate();
  574. this->CMakeInstance->SetProgressCallback(CM_NULLPTR, CM_NULLPTR);
  575. keypad(stdscr, TRUE); /* Use key symbols as
  576. KEY_DOWN*/
  577. if (retVal != 0 || !this->Errors.empty()) {
  578. // see if there was an error
  579. if (cmSystemTools::GetErrorOccuredFlag()) {
  580. this->OkToGenerate = false;
  581. }
  582. // reset error condition
  583. cmSystemTools::ResetErrorOccuredFlag();
  584. int xx, yy;
  585. getmaxyx(stdscr, yy, xx);
  586. const char* title = "Messages during last pass.";
  587. if (cmSystemTools::GetErrorOccuredFlag()) {
  588. title = "Errors occurred during the last pass.";
  589. }
  590. cmCursesLongMessageForm* msgs =
  591. new cmCursesLongMessageForm(this->Errors, title);
  592. CurrentForm = msgs;
  593. msgs->Render(1, 1, xx, yy);
  594. msgs->HandleInput();
  595. // If they typed the wrong source directory, we report
  596. // an error and exit
  597. if (retVal == -2) {
  598. return retVal;
  599. }
  600. CurrentForm = this;
  601. this->Render(1, 1, xx, yy);
  602. }
  603. this->InitializeUI();
  604. this->Render(1, 1, xi, yi);
  605. return 0;
  606. }
  607. void cmCursesMainForm::AddError(const char* message, const char* /*unused*/)
  608. {
  609. this->Errors.push_back(message);
  610. }
  611. void cmCursesMainForm::RemoveEntry(const char* value)
  612. {
  613. if (!value) {
  614. return;
  615. }
  616. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  617. for (it = this->Entries->begin(); it != this->Entries->end(); ++it) {
  618. const char* val = (*it)->GetValue();
  619. if (val && !strcmp(value, val)) {
  620. this->CMakeInstance->UnwatchUnusedCli(value);
  621. this->Entries->erase(it);
  622. break;
  623. }
  624. }
  625. }
  626. // copy from the list box to the cache manager
  627. void cmCursesMainForm::FillCacheManagerFromUI()
  628. {
  629. size_t size = this->Entries->size();
  630. for (size_t i = 0; i < size; i++) {
  631. std::string cacheKey = (*this->Entries)[i]->Key;
  632. const char* existingValue =
  633. this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey);
  634. if (existingValue) {
  635. std::string oldValue = existingValue;
  636. std::string newValue = (*this->Entries)[i]->Entry->GetValue();
  637. std::string fixedOldValue;
  638. std::string fixedNewValue;
  639. cmStateEnums::CacheEntryType t =
  640. this->CMakeInstance->GetState()->GetCacheEntryType(cacheKey);
  641. this->FixValue(t, oldValue, fixedOldValue);
  642. this->FixValue(t, newValue, fixedNewValue);
  643. if (!(fixedOldValue == fixedNewValue)) {
  644. // The user has changed the value. Mark it as modified.
  645. this->CMakeInstance->GetState()->SetCacheEntryBoolProperty(
  646. cacheKey, "MODIFIED", true);
  647. this->CMakeInstance->GetState()->SetCacheEntryValue(cacheKey,
  648. fixedNewValue);
  649. }
  650. }
  651. }
  652. }
  653. void cmCursesMainForm::FixValue(cmStateEnums::CacheEntryType type,
  654. const std::string& in, std::string& out) const
  655. {
  656. out = in.substr(0, in.find_last_not_of(' ') + 1);
  657. if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
  658. cmSystemTools::ConvertToUnixSlashes(out);
  659. }
  660. if (type == cmStateEnums::BOOL) {
  661. if (cmSystemTools::IsOff(out.c_str())) {
  662. out = "OFF";
  663. } else {
  664. out = "ON";
  665. }
  666. }
  667. }
  668. void cmCursesMainForm::HandleInput()
  669. {
  670. int x = 0, y = 0;
  671. if (!this->Form) {
  672. return;
  673. }
  674. FIELD* currentField;
  675. cmCursesWidget* currentWidget;
  676. char debugMessage[128];
  677. for (;;) {
  678. this->UpdateStatusBar();
  679. this->PrintKeys();
  680. if (this->SearchMode) {
  681. std::string searchstr = "Search: " + this->SearchString;
  682. this->UpdateStatusBar(searchstr.c_str());
  683. this->PrintKeys(1);
  684. curses_move(y - 5, static_cast<unsigned int>(searchstr.size()));
  685. // curses_move(1,1);
  686. touchwin(stdscr);
  687. refresh();
  688. }
  689. int key = getch();
  690. getmaxyx(stdscr, y, x);
  691. // If window too small, handle 'q' only
  692. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  693. // quit
  694. if (key == 'q') {
  695. break;
  696. } else {
  697. continue;
  698. }
  699. }
  700. currentField = current_field(this->Form);
  701. currentWidget =
  702. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  703. bool widgetHandled = false;
  704. if (this->SearchMode) {
  705. if (key == 10 || key == KEY_ENTER) {
  706. this->SearchMode = false;
  707. if (!this->SearchString.empty()) {
  708. this->JumpToCacheEntry(this->SearchString.c_str());
  709. this->OldSearchString = this->SearchString;
  710. }
  711. this->SearchString = "";
  712. }
  713. /*
  714. else if ( key == KEY_ESCAPE )
  715. {
  716. this->SearchMode = false;
  717. }
  718. */
  719. else if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
  720. (key >= '0' && key <= '9') || (key == '_')) {
  721. if (this->SearchString.size() <
  722. static_cast<std::string::size_type>(x - 10)) {
  723. this->SearchString += static_cast<char>(key);
  724. }
  725. } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) {
  726. if (!this->SearchString.empty()) {
  727. this->SearchString.resize(this->SearchString.size() - 1);
  728. }
  729. }
  730. } else if (currentWidget && !this->SearchMode) {
  731. // Ask the current widget if it wants to handle input
  732. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  733. if (widgetHandled) {
  734. this->OkToGenerate = false;
  735. this->UpdateStatusBar();
  736. this->PrintKeys();
  737. }
  738. }
  739. if ((!currentWidget || !widgetHandled) && !this->SearchMode) {
  740. // If the current widget does not want to handle input,
  741. // we handle it.
  742. sprintf(debugMessage, "Main form handling input, key: %d", key);
  743. cmCursesForm::LogMessage(debugMessage);
  744. // quit
  745. if (key == 'q') {
  746. break;
  747. }
  748. // if not end of page, next field otherwise next page
  749. // each entry consists of fields: label, isnew, value
  750. // therefore, the label field for the prev. entry is index-5
  751. // and the label field for the next entry is index+1
  752. // (index always corresponds to the value field)
  753. // scroll down with arrow down, ctrl+n (emacs binding), or j (vim
  754. // binding)
  755. else if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  756. FIELD* cur = current_field(this->Form);
  757. size_t findex = field_index(cur);
  758. if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  759. continue;
  760. }
  761. if (new_page(this->Fields[findex + 1])) {
  762. form_driver(this->Form, REQ_NEXT_PAGE);
  763. } else {
  764. form_driver(this->Form, REQ_NEXT_FIELD);
  765. }
  766. }
  767. // if not beginning of page, previous field, otherwise previous page
  768. // each entry consists of fields: label, isnew, value
  769. // therefore, the label field for the prev. entry is index-5
  770. // and the label field for the next entry is index+1
  771. // (index always corresponds to the value field)
  772. // scroll down with arrow up, ctrl+p (emacs binding), or k (vim binding)
  773. else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  774. FIELD* cur = current_field(this->Form);
  775. int findex = field_index(cur);
  776. if (findex == 2) {
  777. continue;
  778. }
  779. if (new_page(this->Fields[findex - 2])) {
  780. form_driver(this->Form, REQ_PREV_PAGE);
  781. set_current_field(this->Form, this->Fields[findex - 3]);
  782. } else {
  783. form_driver(this->Form, REQ_PREV_FIELD);
  784. }
  785. }
  786. // pg down
  787. else if (key == KEY_NPAGE || key == ctrl('d')) {
  788. form_driver(this->Form, REQ_NEXT_PAGE);
  789. }
  790. // pg up
  791. else if (key == KEY_PPAGE || key == ctrl('u')) {
  792. form_driver(this->Form, REQ_PREV_PAGE);
  793. }
  794. // configure
  795. else if (key == 'c') {
  796. this->Configure();
  797. }
  798. // display help
  799. else if (key == 'h') {
  800. getmaxyx(stdscr, y, x);
  801. FIELD* cur = current_field(this->Form);
  802. int findex = field_index(cur);
  803. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  804. field_userptr(this->Fields[findex - 2]));
  805. const char* curField = lbl->GetValue();
  806. const char* helpString = CM_NULLPTR;
  807. const char* existingValue =
  808. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  809. if (existingValue) {
  810. helpString = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  811. curField, "HELPSTRING");
  812. }
  813. if (helpString) {
  814. char* message = new char
  815. [strlen(curField) + strlen(helpString) +
  816. strlen(
  817. "Current option is: \n Help string for this option is: \n") +
  818. 10];
  819. sprintf(
  820. message,
  821. "Current option is: %s\nHelp string for this option is: %s\n",
  822. curField, helpString);
  823. this->HelpMessage[1] = message;
  824. delete[] message;
  825. } else {
  826. this->HelpMessage[1] = "";
  827. }
  828. cmCursesLongMessageForm* msgs =
  829. new cmCursesLongMessageForm(this->HelpMessage, "Help.");
  830. CurrentForm = msgs;
  831. msgs->Render(1, 1, x, y);
  832. msgs->HandleInput();
  833. CurrentForm = this;
  834. this->Render(1, 1, x, y);
  835. set_current_field(this->Form, cur);
  836. }
  837. // display last errors
  838. else if (key == 'l') {
  839. getmaxyx(stdscr, y, x);
  840. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  841. this->Errors, "Errors occurred during the last pass.");
  842. CurrentForm = msgs;
  843. msgs->Render(1, 1, x, y);
  844. msgs->HandleInput();
  845. CurrentForm = this;
  846. this->Render(1, 1, x, y);
  847. } else if (key == '/') {
  848. this->SearchMode = true;
  849. this->UpdateStatusBar("Search");
  850. this->PrintKeys(1);
  851. touchwin(stdscr);
  852. refresh();
  853. } else if (key == 'n') {
  854. if (!this->OldSearchString.empty()) {
  855. this->JumpToCacheEntry(this->OldSearchString.c_str());
  856. }
  857. }
  858. // switch advanced on/off
  859. else if (key == 't') {
  860. if (this->AdvancedMode) {
  861. this->AdvancedMode = false;
  862. } else {
  863. this->AdvancedMode = true;
  864. }
  865. getmaxyx(stdscr, y, x);
  866. this->RePost();
  867. this->Render(1, 1, x, y);
  868. }
  869. // generate and exit
  870. else if (key == 'g') {
  871. if (this->OkToGenerate) {
  872. this->Generate();
  873. break;
  874. }
  875. }
  876. // delete cache entry
  877. else if (key == 'd' && this->NumberOfVisibleEntries) {
  878. this->OkToGenerate = false;
  879. FIELD* cur = current_field(this->Form);
  880. size_t findex = field_index(cur);
  881. // make the next or prev. current field after deletion
  882. // each entry consists of fields: label, isnew, value
  883. // therefore, the label field for the prev. entry is findex-5
  884. // and the label field for the next entry is findex+1
  885. // (findex always corresponds to the value field)
  886. FIELD* nextCur;
  887. if (findex == 2) {
  888. nextCur = CM_NULLPTR;
  889. } else if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  890. nextCur = this->Fields[findex - 5];
  891. } else {
  892. nextCur = this->Fields[findex + 1];
  893. }
  894. // Get the label widget
  895. // each entry consists of fields: label, isnew, value
  896. // therefore, the label field for the is findex-2
  897. // (findex always corresponds to the value field)
  898. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  899. field_userptr(this->Fields[findex - 2]));
  900. if (lbl) {
  901. this->CMakeInstance->GetState()->RemoveCacheEntry(lbl->GetValue());
  902. std::string nextVal;
  903. if (nextCur) {
  904. nextVal =
  905. (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))
  906. ->GetValue());
  907. }
  908. getmaxyx(stdscr, y, x);
  909. this->RemoveEntry(lbl->GetValue());
  910. this->RePost();
  911. this->Render(1, 1, x, y);
  912. if (nextCur) {
  913. // make the next or prev. current field after deletion
  914. nextCur = CM_NULLPTR;
  915. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  916. for (it = this->Entries->begin(); it != this->Entries->end();
  917. ++it) {
  918. if (nextVal == (*it)->Key) {
  919. nextCur = (*it)->Entry->Field;
  920. }
  921. }
  922. if (nextCur) {
  923. set_current_field(this->Form, nextCur);
  924. }
  925. }
  926. }
  927. }
  928. }
  929. touchwin(stdscr);
  930. wrefresh(stdscr);
  931. }
  932. }
  933. int cmCursesMainForm::LoadCache(const char* /*unused*/)
  934. {
  935. int r = this->CMakeInstance->LoadCache();
  936. if (r < 0) {
  937. return r;
  938. }
  939. this->CMakeInstance->SetCacheArgs(this->Args);
  940. this->CMakeInstance->PreLoadCMakeFiles();
  941. return r;
  942. }
  943. void cmCursesMainForm::JumpToCacheEntry(const char* astr)
  944. {
  945. std::string str;
  946. if (astr) {
  947. str = cmSystemTools::LowerCase(astr);
  948. }
  949. if (str.empty()) {
  950. return;
  951. }
  952. FIELD* cur = current_field(this->Form);
  953. int start_index = field_index(cur);
  954. int findex = start_index;
  955. for (;;) {
  956. if (!str.empty()) {
  957. cmCursesWidget* lbl = CM_NULLPTR;
  958. if (findex >= 0) {
  959. lbl = reinterpret_cast<cmCursesWidget*>(
  960. field_userptr(this->Fields[findex - 2]));
  961. }
  962. if (lbl) {
  963. const char* curField = lbl->GetValue();
  964. if (curField) {
  965. std::string cfld = cmSystemTools::LowerCase(curField);
  966. if (cfld.find(str) != cfld.npos && findex != start_index) {
  967. break;
  968. }
  969. }
  970. }
  971. }
  972. if (size_t(findex) >= 3 * this->NumberOfVisibleEntries - 1) {
  973. set_current_field(this->Form, this->Fields[2]);
  974. } else if (new_page(this->Fields[findex + 1])) {
  975. form_driver(this->Form, REQ_NEXT_PAGE);
  976. } else {
  977. form_driver(this->Form, REQ_NEXT_FIELD);
  978. }
  979. /*
  980. char buffer[1024];
  981. sprintf(buffer, "Line: %d != %d / %d\n", findex, idx,
  982. this->NumberOfVisibleEntries);
  983. touchwin(stdscr);
  984. refresh();
  985. this->UpdateStatusBar( buffer );
  986. usleep(100000);
  987. */
  988. cur = current_field(this->Form);
  989. findex = field_index(cur);
  990. if (findex == start_index) {
  991. break;
  992. }
  993. }
  994. }
  995. const char* cmCursesMainForm::s_ConstHelpMessage =
  996. "CMake is used to configure and generate build files for software projects. "
  997. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  998. "1. Run ccmake in the directory where you want the object and executable "
  999. "files to be placed (build directory). If the source directory is not the "
  1000. "same as this build directory, you have to specify it as an argument on the "
  1001. "command line.\n\n"
  1002. "2. When ccmake is run, it will read the configuration files and display "
  1003. "the current build options. "
  1004. "If you have run CMake before and have updated the configuration files "
  1005. "since then, any new entries will be displayed on top and will be marked "
  1006. "with a *. "
  1007. "On the other hand, the first time you run ccmake, all build options will "
  1008. "be new and will be marked as such. "
  1009. "At this point, you can modify any options (see keys below) you want to "
  1010. "change. "
  1011. "When you are satisfied with your changes, press 'c' to have CMake process "
  1012. "the configuration files. "
  1013. "Please note that changing some options may cause new ones to appear. These "
  1014. "will be shown on top and will be marked with *. "
  1015. "Repeat this procedure until you are satisfied with all the options and "
  1016. "there are no new entries. "
  1017. "At this point, a new command will appear: G)enerate and Exit. You can now "
  1018. "hit 'g' to have CMake generate all the build files (i.e. makefiles or "
  1019. "project files) and exit. "
  1020. "At any point during the process, you can exit ccmake with 'q'. However, "
  1021. "this will not generate/change any build files.\n\n"
  1022. "ccmake KEYS:\n\n"
  1023. "Navigation: "
  1024. "You can use the arrow keys and page up, down to navigate the options. "
  1025. "Alternatively, you can use the following keys: \n"
  1026. " C-n or j : next option\n"
  1027. " C-p or k : previous options\n"
  1028. " C-d : down one page\n"
  1029. " C-u : up one page\n\n"
  1030. "Editing options: "
  1031. "To change an option press enter or return. If the current options is a "
  1032. "boolean, this will toggle its value. "
  1033. "Otherwise, ccmake will enter edit mode. Alternatively, you can toggle "
  1034. "a bool variable by pressing space, and enter edit mode with i."
  1035. "In this mode you can edit an option using arrow keys and backspace. "
  1036. "Alternatively, you can use the following keys:\n"
  1037. " C-b : back one character\n"
  1038. " C-f : forward one character\n"
  1039. " C-a : go to the beginning of the field\n"
  1040. " C-e : go to the end of the field\n"
  1041. " C-d : delete previous character\n"
  1042. " C-k : kill the rest of the field\n"
  1043. " Esc : Restore field (discard last changes)\n"
  1044. " Enter : Leave edit mode\n"
  1045. "Commands:\n"
  1046. " q : quit ccmake without generating build files\n"
  1047. " h : help, shows this screen\n"
  1048. " c : process the configuration files with the current options\n"
  1049. " g : generate build files and exit, only available when there are no "
  1050. "new options and no errors have been detected during last configuration.\n"
  1051. " l : shows last errors\n"
  1052. " d : delete an option\n"
  1053. " t : toggles advanced mode. In normal mode, only the most important "
  1054. "options are shown. In advanced mode, all options are shown. We recommend "
  1055. "using normal mode unless you are an expert.\n"
  1056. " / : search for a variable name.\n";