cmCursesMainForm.cxx 35 KB

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