cmCursesMainForm.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. 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. } else {
  691. continue;
  692. }
  693. }
  694. currentField = current_field(this->Form);
  695. currentWidget =
  696. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  697. bool widgetHandled = false;
  698. if (this->SearchMode) {
  699. if (key == 10 || key == KEY_ENTER) {
  700. this->SearchMode = false;
  701. if (!this->SearchString.empty()) {
  702. this->JumpToCacheEntry(this->SearchString.c_str());
  703. this->OldSearchString = this->SearchString;
  704. }
  705. this->SearchString = "";
  706. }
  707. /*
  708. else if ( key == KEY_ESCAPE )
  709. {
  710. this->SearchMode = false;
  711. }
  712. */
  713. else if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
  714. (key >= '0' && key <= '9') || (key == '_')) {
  715. if (this->SearchString.size() <
  716. static_cast<std::string::size_type>(x - 10)) {
  717. this->SearchString += static_cast<char>(key);
  718. }
  719. } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) {
  720. if (!this->SearchString.empty()) {
  721. this->SearchString.resize(this->SearchString.size() - 1);
  722. }
  723. }
  724. } else if (currentWidget && !this->SearchMode) {
  725. // Ask the current widget if it wants to handle input
  726. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  727. if (widgetHandled) {
  728. this->OkToGenerate = false;
  729. this->UpdateStatusBar();
  730. this->PrintKeys();
  731. }
  732. }
  733. if ((!currentWidget || !widgetHandled) && !this->SearchMode) {
  734. // If the current widget does not want to handle input,
  735. // we handle it.
  736. sprintf(debugMessage, "Main form handling input, key: %d", key);
  737. cmCursesForm::LogMessage(debugMessage);
  738. // quit
  739. if (key == 'q') {
  740. break;
  741. }
  742. // if not end of page, next field otherwise next page
  743. // each entry consists of fields: label, isnew, value
  744. // therefore, the label field for the prev. entry is index-5
  745. // and the label field for the next entry is index+1
  746. // (index always corresponds to the value field)
  747. // scroll down with arrow down, ctrl+n (emacs binding), or j (vim
  748. // binding)
  749. else if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  750. FIELD* cur = current_field(this->Form);
  751. size_t findex = field_index(cur);
  752. if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  753. continue;
  754. }
  755. if (new_page(this->Fields[findex + 1])) {
  756. form_driver(this->Form, REQ_NEXT_PAGE);
  757. } else {
  758. form_driver(this->Form, REQ_NEXT_FIELD);
  759. }
  760. }
  761. // if not beginning of page, previous field, otherwise previous page
  762. // each entry consists of fields: label, isnew, value
  763. // therefore, the label field for the prev. entry is index-5
  764. // and the label field for the next entry is index+1
  765. // (index always corresponds to the value field)
  766. // scroll down with arrow up, ctrl+p (emacs binding), or k (vim binding)
  767. else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  768. FIELD* cur = current_field(this->Form);
  769. int findex = field_index(cur);
  770. if (findex == 2) {
  771. continue;
  772. }
  773. if (new_page(this->Fields[findex - 2])) {
  774. form_driver(this->Form, REQ_PREV_PAGE);
  775. set_current_field(this->Form, this->Fields[findex - 3]);
  776. } else {
  777. form_driver(this->Form, REQ_PREV_FIELD);
  778. }
  779. }
  780. // pg down
  781. else if (key == KEY_NPAGE || key == ctrl('d')) {
  782. form_driver(this->Form, REQ_NEXT_PAGE);
  783. }
  784. // pg up
  785. else if (key == KEY_PPAGE || key == ctrl('u')) {
  786. form_driver(this->Form, REQ_PREV_PAGE);
  787. }
  788. // configure
  789. else if (key == 'c') {
  790. this->Configure();
  791. }
  792. // display help
  793. else if (key == 'h') {
  794. getmaxyx(stdscr, y, x);
  795. FIELD* cur = current_field(this->Form);
  796. int findex = field_index(cur);
  797. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  798. field_userptr(this->Fields[findex - 2]));
  799. const char* curField = lbl->GetValue();
  800. const char* helpString = CM_NULLPTR;
  801. const char* existingValue =
  802. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  803. if (existingValue) {
  804. helpString = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  805. curField, "HELPSTRING");
  806. }
  807. if (helpString) {
  808. char* message = new char
  809. [strlen(curField) + strlen(helpString) +
  810. strlen(
  811. "Current option is: \n Help string for this option is: \n") +
  812. 10];
  813. sprintf(
  814. message,
  815. "Current option is: %s\nHelp string for this option is: %s\n",
  816. curField, helpString);
  817. this->HelpMessage[1] = message;
  818. delete[] message;
  819. } else {
  820. this->HelpMessage[1] = "";
  821. }
  822. cmCursesLongMessageForm* msgs =
  823. new cmCursesLongMessageForm(this->HelpMessage, "Help.");
  824. CurrentForm = msgs;
  825. msgs->Render(1, 1, x, y);
  826. msgs->HandleInput();
  827. CurrentForm = this;
  828. this->Render(1, 1, x, y);
  829. set_current_field(this->Form, cur);
  830. }
  831. // display last errors
  832. else if (key == 'l') {
  833. getmaxyx(stdscr, y, x);
  834. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  835. this->Errors, "Errors occurred during the last pass.");
  836. CurrentForm = msgs;
  837. msgs->Render(1, 1, x, y);
  838. msgs->HandleInput();
  839. CurrentForm = this;
  840. this->Render(1, 1, x, y);
  841. } else if (key == '/') {
  842. this->SearchMode = true;
  843. this->UpdateStatusBar("Search");
  844. this->PrintKeys(1);
  845. touchwin(stdscr);
  846. refresh();
  847. } else if (key == 'n') {
  848. if (!this->OldSearchString.empty()) {
  849. this->JumpToCacheEntry(this->OldSearchString.c_str());
  850. }
  851. }
  852. // switch advanced on/off
  853. else if (key == 't') {
  854. if (this->AdvancedMode) {
  855. this->AdvancedMode = false;
  856. } else {
  857. this->AdvancedMode = true;
  858. }
  859. getmaxyx(stdscr, y, x);
  860. this->RePost();
  861. this->Render(1, 1, x, y);
  862. }
  863. // generate and exit
  864. else if (key == 'g') {
  865. if (this->OkToGenerate) {
  866. this->Generate();
  867. break;
  868. }
  869. }
  870. // delete cache entry
  871. else if (key == 'd' && this->NumberOfVisibleEntries) {
  872. this->OkToGenerate = false;
  873. FIELD* cur = current_field(this->Form);
  874. size_t findex = field_index(cur);
  875. // make the next or prev. current field after deletion
  876. // each entry consists of fields: label, isnew, value
  877. // therefore, the label field for the prev. entry is findex-5
  878. // and the label field for the next entry is findex+1
  879. // (findex always corresponds to the value field)
  880. FIELD* nextCur;
  881. if (findex == 2) {
  882. nextCur = CM_NULLPTR;
  883. } else if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  884. nextCur = this->Fields[findex - 5];
  885. } else {
  886. nextCur = this->Fields[findex + 1];
  887. }
  888. // Get the label widget
  889. // each entry consists of fields: label, isnew, value
  890. // therefore, the label field for the is findex-2
  891. // (findex always corresponds to the value field)
  892. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  893. field_userptr(this->Fields[findex - 2]));
  894. if (lbl) {
  895. this->CMakeInstance->GetState()->RemoveCacheEntry(lbl->GetValue());
  896. std::string nextVal;
  897. if (nextCur) {
  898. nextVal =
  899. (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))
  900. ->GetValue());
  901. }
  902. getmaxyx(stdscr, y, x);
  903. this->RemoveEntry(lbl->GetValue());
  904. this->RePost();
  905. this->Render(1, 1, x, y);
  906. if (nextCur) {
  907. // make the next or prev. current field after deletion
  908. nextCur = CM_NULLPTR;
  909. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  910. for (it = this->Entries->begin(); it != this->Entries->end();
  911. ++it) {
  912. if (nextVal == (*it)->Key) {
  913. nextCur = (*it)->Entry->Field;
  914. }
  915. }
  916. if (nextCur) {
  917. set_current_field(this->Form, nextCur);
  918. }
  919. }
  920. }
  921. }
  922. }
  923. touchwin(stdscr);
  924. wrefresh(stdscr);
  925. }
  926. }
  927. int cmCursesMainForm::LoadCache(const char* /*unused*/)
  928. {
  929. int r = this->CMakeInstance->LoadCache();
  930. if (r < 0) {
  931. return r;
  932. }
  933. this->CMakeInstance->SetCacheArgs(this->Args);
  934. this->CMakeInstance->PreLoadCMakeFiles();
  935. return r;
  936. }
  937. void cmCursesMainForm::JumpToCacheEntry(const char* astr)
  938. {
  939. std::string str;
  940. if (astr) {
  941. str = cmSystemTools::LowerCase(astr);
  942. }
  943. if (str.empty()) {
  944. return;
  945. }
  946. FIELD* cur = current_field(this->Form);
  947. int start_index = field_index(cur);
  948. int findex = start_index;
  949. for (;;) {
  950. if (!str.empty()) {
  951. cmCursesWidget* lbl = CM_NULLPTR;
  952. if (findex >= 0) {
  953. lbl = reinterpret_cast<cmCursesWidget*>(
  954. field_userptr(this->Fields[findex - 2]));
  955. }
  956. if (lbl) {
  957. const char* curField = lbl->GetValue();
  958. if (curField) {
  959. std::string cfld = cmSystemTools::LowerCase(curField);
  960. if (cfld.find(str) != cfld.npos && findex != start_index) {
  961. break;
  962. }
  963. }
  964. }
  965. }
  966. if (size_t(findex) >= 3 * this->NumberOfVisibleEntries - 1) {
  967. set_current_field(this->Form, this->Fields[2]);
  968. } else if (new_page(this->Fields[findex + 1])) {
  969. form_driver(this->Form, REQ_NEXT_PAGE);
  970. } else {
  971. form_driver(this->Form, REQ_NEXT_FIELD);
  972. }
  973. /*
  974. char buffer[1024];
  975. sprintf(buffer, "Line: %d != %d / %d\n", findex, idx,
  976. this->NumberOfVisibleEntries);
  977. touchwin(stdscr);
  978. refresh();
  979. this->UpdateStatusBar( buffer );
  980. usleep(100000);
  981. */
  982. cur = current_field(this->Form);
  983. findex = field_index(cur);
  984. if (findex == start_index) {
  985. break;
  986. }
  987. }
  988. }
  989. const char* cmCursesMainForm::s_ConstHelpMessage =
  990. "CMake is used to configure and generate build files for software projects. "
  991. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  992. "1. Run ccmake in the directory where you want the object and executable "
  993. "files to be placed (build directory). If the source directory is not the "
  994. "same as this build directory, you have to specify it as an argument on the "
  995. "command line.\n\n"
  996. "2. When ccmake is run, it will read the configuration files and display "
  997. "the current build options. "
  998. "If you have run CMake before and have updated the configuration files "
  999. "since then, any new entries will be displayed on top and will be marked "
  1000. "with a *. "
  1001. "On the other hand, the first time you run ccmake, all build options will "
  1002. "be new and will be marked as such. "
  1003. "At this point, you can modify any options (see keys below) you want to "
  1004. "change. "
  1005. "When you are satisfied with your changes, press 'c' to have CMake process "
  1006. "the configuration files. "
  1007. "Please note that changing some options may cause new ones to appear. These "
  1008. "will be shown on top and will be marked with *. "
  1009. "Repeat this procedure until you are satisfied with all the options and "
  1010. "there are no new entries. "
  1011. "At this point, a new command will appear: G)enerate and Exit. You can now "
  1012. "hit 'g' to have CMake generate all the build files (i.e. makefiles or "
  1013. "project files) and exit. "
  1014. "At any point during the process, you can exit ccmake with 'q'. However, "
  1015. "this will not generate/change any build files.\n\n"
  1016. "ccmake KEYS:\n\n"
  1017. "Navigation: "
  1018. "You can use the arrow keys and page up, down to navigate the options. "
  1019. "Alternatively, you can use the following keys: \n"
  1020. " C-n or j : next option\n"
  1021. " C-p or k : previous options\n"
  1022. " C-d : down one page\n"
  1023. " C-u : up one page\n\n"
  1024. "Editing options: "
  1025. "To change an option press enter or return. If the current options is a "
  1026. "boolean, this will toggle its value. "
  1027. "Otherwise, ccmake will enter edit mode. Alternatively, you can toggle "
  1028. "a bool variable by pressing space, and enter edit mode with i."
  1029. "In this mode you can edit an option using arrow keys and backspace. "
  1030. "Alternatively, you can use the following keys:\n"
  1031. " C-b : back one character\n"
  1032. " C-f : forward one character\n"
  1033. " C-a : go to the beginning of the field\n"
  1034. " C-e : go to the end of the field\n"
  1035. " C-d : delete previous character\n"
  1036. " C-k : kill the rest of the field\n"
  1037. " Esc : Restore field (discard last changes)\n"
  1038. " Enter : Leave edit mode\n"
  1039. "Commands:\n"
  1040. " q : quit ccmake without generating build files\n"
  1041. " h : help, shows this screen\n"
  1042. " c : process the configuration files with the current options\n"
  1043. " g : generate build files and exit, only available when there are no "
  1044. "new options and no errors have been detected during last configuration.\n"
  1045. " l : shows last errors\n"
  1046. " d : delete an option\n"
  1047. " t : toggles advanced mode. In normal mode, only the most important "
  1048. "options are shown. In advanced mode, all options are shown. We recommend "
  1049. "using normal mode unless you are an expert.\n"
  1050. " / : search for a variable name.\n";