cmCursesMainForm.cxx 36 KB

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