cmCursesMainForm.cxx 35 KB

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