cmCursesMainForm.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #include "../cmCacheManager.h"
  2. #include "../cmSystemTools.h"
  3. #include "../cmake.h"
  4. #include "cmCursesMainForm.h"
  5. #include "cmCursesStringWidget.h"
  6. #include "cmCursesLabelWidget.h"
  7. #include "cmCursesBoolWidget.h"
  8. #include "cmCursesPathWidget.h"
  9. #include "cmCursesFilePathWidget.h"
  10. #include "cmCursesDummyWidget.h"
  11. #include "cmCursesCacheEntryComposite.h"
  12. const int cmCursesMainForm::MIN_WIDTH = 65;
  13. const int cmCursesMainForm::MIN_HEIGHT = 6;
  14. const int cmCursesMainForm::IDEAL_WIDTH = 80;
  15. const int cmCursesMainForm::MAX_WIDTH = 512;
  16. inline int ctrl(int z)
  17. {
  18. return (z&037);
  19. }
  20. cmCursesMainForm::cmCursesMainForm(const char* whereSource,
  21. bool newCache) :
  22. m_WhereSource(whereSource)
  23. {
  24. m_Fields = 0;
  25. m_Window = 0;
  26. m_Height = 0;
  27. m_Entries = 0;
  28. }
  29. cmCursesMainForm::~cmCursesMainForm()
  30. {
  31. if (m_Form)
  32. {
  33. unpost_form(m_Form);
  34. free_form(m_Form);
  35. m_Form = 0;
  36. }
  37. delete[] m_Fields;
  38. // Clean-up composites
  39. if (m_Entries)
  40. {
  41. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  42. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  43. {
  44. delete *it;
  45. }
  46. }
  47. delete m_Entries;
  48. }
  49. bool cmCursesMainForm::LookForCacheEntry(const char* key)
  50. {
  51. if (!key || !m_Entries)
  52. {
  53. return false;
  54. }
  55. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  56. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  57. {
  58. if (!strcmp(key, (*it)->m_Key.c_str()))
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. void cmCursesMainForm::InitializeUI(WINDOW* w)
  66. {
  67. m_Window = w;
  68. // Get the cache entries.
  69. const cmCacheManager::CacheEntryMap &cache =
  70. cmCacheManager::GetInstance()->GetCacheMap();
  71. std::vector<cmCursesCacheEntryComposite*>* newEntries =
  72. new std::vector<cmCursesCacheEntryComposite*>;
  73. newEntries->reserve(cache.size());
  74. // Count non-internal and non-static entries
  75. int count=0;
  76. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  77. i != cache.end(); ++i)
  78. {
  79. const cmCacheManager::CacheEntry& value = i->second;
  80. if ( value.m_Type != cmCacheManager::INTERNAL &&
  81. value.m_Type != cmCacheManager::STATIC )
  82. {
  83. ++count;
  84. }
  85. }
  86. cmCursesCacheEntryComposite* comp;
  87. if ( count == 0 )
  88. {
  89. // If cache is empty, display a label saying so and a
  90. // dummy entry widget (does not respond to input)
  91. comp = new cmCursesCacheEntryComposite("EMPTY CACHE");
  92. comp->m_Entry = new cmCursesDummyWidget(1, 1, 1, 1);
  93. newEntries->push_back(comp);
  94. }
  95. else
  96. {
  97. // Create the composites.
  98. // First add entries which are new
  99. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  100. i != cache.end(); ++i)
  101. {
  102. const char* key = i->first.c_str();
  103. const cmCacheManager::CacheEntry& value = i->second;
  104. if ( value.m_Type == cmCacheManager::INTERNAL ||
  105. value.m_Type == cmCacheManager::STATIC )
  106. {
  107. continue;
  108. }
  109. if (!this->LookForCacheEntry(key))
  110. {
  111. newEntries->push_back(new cmCursesCacheEntryComposite(key, value,
  112. true));
  113. }
  114. }
  115. // then add entries which are old
  116. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  117. i != cache.end(); ++i)
  118. {
  119. const char* key = i->first.c_str();
  120. const cmCacheManager::CacheEntry& value = i->second;
  121. if ( value.m_Type == cmCacheManager::INTERNAL ||
  122. value.m_Type == cmCacheManager::STATIC )
  123. {
  124. continue;
  125. }
  126. if (this->LookForCacheEntry(key))
  127. {
  128. newEntries->push_back(new cmCursesCacheEntryComposite(key, value,
  129. false));
  130. }
  131. }
  132. }
  133. if (m_Entries)
  134. {
  135. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  136. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  137. {
  138. delete *it;
  139. }
  140. }
  141. delete m_Entries;
  142. m_Entries = newEntries;
  143. // Create the fields to be passed to the form.
  144. if (m_Form)
  145. {
  146. unpost_form(m_Form);
  147. free_form(m_Form);
  148. m_Form = 0;
  149. }
  150. delete[] m_Fields;
  151. int size = m_Entries->size();
  152. m_Fields = new FIELD*[3*size+1];
  153. for(int j=0; j < size; j++)
  154. {
  155. m_Fields[3*j] = (*m_Entries)[j]->m_Label->m_Field;
  156. m_Fields[3*j+1] = (*m_Entries)[j]->m_IsNewLabel->m_Field;
  157. m_Fields[3*j+2] = (*m_Entries)[j]->m_Entry->m_Field;
  158. }
  159. // Has to be null terminated.
  160. m_Fields[3*size] = 0;
  161. }
  162. void cmCursesMainForm::Render(int left, int top, int width, int height)
  163. {
  164. if (m_Form)
  165. {
  166. FIELD* currentField = current_field(m_Form);
  167. cmCursesWidget* cw = reinterpret_cast<cmCursesWidget*>
  168. (field_userptr(currentField));
  169. if ( cw->GetType() == cmCacheManager::STRING ||
  170. cw->GetType() == cmCacheManager::PATH ||
  171. cw->GetType() == cmCacheManager::FILEPATH )
  172. {
  173. cmCursesStringWidget* sw = static_cast<cmCursesStringWidget*>(cw);
  174. sw->SetInEdit(false);
  175. }
  176. unpost_form(m_Form);
  177. free_form(m_Form);
  178. m_Form = 0;
  179. }
  180. if ( width < cmCursesMainForm::MIN_WIDTH ||
  181. height < cmCursesMainForm::MIN_HEIGHT )
  182. {
  183. return;
  184. }
  185. height -= 5;
  186. m_Height = height;
  187. int size = m_Entries->size();
  188. bool isNewPage;
  189. for(int i=0; i < size; i++)
  190. {
  191. int row = (i % height) + 1;
  192. int page = (i / height) + 1;
  193. isNewPage = ( page > 1 ) && ( row == 1 );
  194. (*m_Entries)[i]->m_Label->Move(left, top+row-1, isNewPage);
  195. (*m_Entries)[i]->m_IsNewLabel->Move(left+32, top+row-1, false);
  196. (*m_Entries)[i]->m_Entry->Move(left+33, top+row-1, false);
  197. }
  198. m_Form = new_form(m_Fields);
  199. post_form(m_Form);
  200. this->UpdateStatusBar();
  201. this->PrintKeys();
  202. touchwin(m_Window);
  203. refresh();
  204. }
  205. void cmCursesMainForm::PrintKeys()
  206. {
  207. int x,y;
  208. getmaxyx(m_Window, y, x);
  209. if ( x < cmCursesMainForm::MIN_WIDTH ||
  210. y < cmCursesMainForm::MIN_HEIGHT )
  211. {
  212. return;
  213. }
  214. char firstLine[512], secondLine[512];
  215. sprintf(firstLine, "C)onfigure G)enerate and Exit");
  216. sprintf(secondLine, "Q)uit H)elp");
  217. move(y-2,0);
  218. printw(firstLine);
  219. move(y-1,0);
  220. printw(secondLine);
  221. pos_form_cursor(m_Form);
  222. }
  223. // Print the key of the current entry and the CMake version
  224. // on the status bar. Designed for a width of 80 chars.
  225. void cmCursesMainForm::UpdateStatusBar()
  226. {
  227. int x,y;
  228. getmaxyx(m_Window, y, x);
  229. if ( x < cmCursesMainForm::MIN_WIDTH ||
  230. y < cmCursesMainForm::MIN_HEIGHT )
  231. {
  232. move(0,0);
  233. printw("Window is too small. A size of at least %dx%d is required.",
  234. cmCursesMainForm::MIN_WIDTH, cmCursesMainForm::MIN_HEIGHT);
  235. touchwin(m_Window);
  236. wrefresh(m_Window);
  237. return;
  238. }
  239. FIELD* cur = current_field(m_Form);
  240. int index = field_index(cur);
  241. char* curField = field_buffer(m_Fields[index-2], 0);
  242. char version[128];
  243. sprintf(version,"(CMake Version %d.%d)", cmMakefile::GetMajorVersion(),
  244. cmMakefile::GetMinorVersion());
  245. char bar[cmCursesMainForm::MAX_WIDTH];
  246. int i, curFieldLen = strlen(curField);
  247. int versionLen = strlen(version);
  248. int leftLen = cmCursesMainForm::IDEAL_WIDTH - versionLen;
  249. if (curFieldLen >= leftLen)
  250. {
  251. strncpy(bar, curField, leftLen);
  252. }
  253. else
  254. {
  255. strcpy(bar, curField);
  256. for(i=curFieldLen; i < leftLen; ++i) { bar[i] = ' '; }
  257. }
  258. strcpy(bar+leftLen, version);
  259. if ( x < cmCursesMainForm::MAX_WIDTH )
  260. {
  261. if (x > cmCursesMainForm::IDEAL_WIDTH )
  262. {
  263. for(i=cmCursesMainForm::IDEAL_WIDTH; i < x; i++)
  264. {
  265. bar[i] = ' ';
  266. }
  267. }
  268. bar[x] = '\0';
  269. }
  270. else
  271. {
  272. for(i=cmCursesMainForm::IDEAL_WIDTH;
  273. i < cmCursesMainForm::MAX_WIDTH-1; i++)
  274. {
  275. bar[i] = ' ';
  276. }
  277. bar[cmCursesMainForm::MAX_WIDTH-1] = '\0';
  278. }
  279. move(y-3,0);
  280. attron(A_STANDOUT);
  281. printw(bar);
  282. attroff(A_STANDOUT);
  283. pos_form_cursor(m_Form);
  284. }
  285. void cmCursesMainForm::RunCMake(bool generateMakefiles)
  286. {
  287. int x,y;
  288. getmaxyx(m_Window, y, x);
  289. endwin();
  290. // always save the current gui values to disk
  291. this->FillCacheManagerFromUI();
  292. cmCacheManager::GetInstance()->SaveCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  293. // create a cmake object
  294. cmake make;
  295. // create the arguments for the cmake object
  296. std::vector<std::string> args;
  297. args.push_back("cmake");
  298. if (m_WhereSource != "")
  299. {
  300. std::string arg;
  301. arg = m_WhereSource;
  302. args.push_back(arg);
  303. }
  304. // run the generate process
  305. if(make.Generate(args, generateMakefiles) != 0)
  306. {
  307. // TODO : error message here
  308. cmSystemTools::ResetErrorOccuredFlag();
  309. }
  310. m_Window = initscr(); /* Initialization */
  311. noecho(); /* Echo off */
  312. cbreak(); /* nl- or cr not needed */
  313. keypad(m_Window,TRUE); /* Use key symbols as
  314. KEY_DOWN*/
  315. this->InitializeUI(m_Window);
  316. this->Render(1, 1, x, y);
  317. }
  318. // copy from the list box to the cache manager
  319. void cmCursesMainForm::FillCacheManagerFromUI()
  320. {
  321. std::string tmpString;
  322. cmCacheManager::GetInstance()->GetCacheMap();
  323. int size = m_Entries->size();
  324. for(int i=0; i < size; i++)
  325. {
  326. cmCacheManager::CacheEntry *entry =
  327. cmCacheManager::GetInstance()->GetCacheEntry(
  328. (*m_Entries)[i]->m_Key.c_str());
  329. if (entry)
  330. {
  331. tmpString = (*m_Entries)[i]->m_Entry->GetValue();
  332. // Remove trailing spaces
  333. entry->m_Value = tmpString.substr(0,tmpString.find_last_not_of(" ")+1);
  334. }
  335. }
  336. }
  337. void cmCursesMainForm::HandleInput()
  338. {
  339. if (!m_Form)
  340. {
  341. return;
  342. }
  343. FIELD* currentField;
  344. cmCursesWidget* currentWidget;
  345. while(1)
  346. {
  347. this->UpdateStatusBar();
  348. this->PrintKeys();
  349. int key = getch();
  350. currentField = current_field(m_Form);
  351. currentWidget = reinterpret_cast<cmCursesWidget*>(field_userptr(
  352. currentField));
  353. if (!currentWidget || !currentWidget->HandleInput(key, m_Form, m_Window))
  354. {
  355. // quit
  356. if ( key == 'q' )
  357. {
  358. break;
  359. }
  360. // if not end of page, next field otherwise next page
  361. else if ( key == KEY_DOWN || key == ctrl('n') )
  362. {
  363. int x,y;
  364. getmaxyx(m_Window, y, x);
  365. FIELD* cur = current_field(m_Form);
  366. int index = field_index(cur);
  367. if ( index == 3*m_Entries->size()-1 )
  368. {
  369. continue;
  370. }
  371. if ( (index < 3*m_Entries->size()-1) && new_page(m_Fields[index+1]))
  372. {
  373. form_driver(m_Form, REQ_NEXT_PAGE);
  374. }
  375. else
  376. {
  377. form_driver(m_Form, REQ_NEXT_FIELD);
  378. }
  379. }
  380. // if not beginning of page, previous field, otherwise previous page
  381. else if ( key == KEY_UP || key == ctrl('p') )
  382. {
  383. int x,y;
  384. getmaxyx(m_Window, y, x);
  385. FIELD* cur = current_field(m_Form);
  386. int index = field_index(cur);
  387. if ( index == 2 )
  388. {
  389. continue;
  390. }
  391. if ( new_page(m_Fields[index-2]) )
  392. {
  393. form_driver(m_Form, REQ_PREV_PAGE);
  394. set_current_field(m_Form, m_Fields[index-3]);
  395. }
  396. else
  397. {
  398. form_driver(m_Form, REQ_PREV_FIELD);
  399. }
  400. }
  401. // pg down
  402. else if ( key == KEY_NPAGE || key == ctrl('d') )
  403. {
  404. form_driver(m_Form, REQ_NEXT_PAGE);
  405. }
  406. // pg up
  407. else if ( key == KEY_PPAGE || key == ctrl('u') )
  408. {
  409. form_driver(m_Form, REQ_PREV_PAGE);
  410. }
  411. // configure
  412. else if ( key == 'c' )
  413. {
  414. this->RunCMake(false);
  415. }
  416. // generate and exit
  417. else if ( key == 'g' )
  418. {
  419. this->RunCMake(true);
  420. break;
  421. }
  422. // delete cache entry
  423. else if ( key == 'd' )
  424. {
  425. FIELD* cur = current_field(m_Form);
  426. int index = field_index(cur);
  427. // make the next or prev. current field after deletion
  428. FIELD* nextCur;
  429. if ( index == 2 )
  430. {
  431. }
  432. else if ( index == 3*m_Entries->size()-1 )
  433. {
  434. nextCur = m_Fields[index-5];
  435. }
  436. else
  437. {
  438. nextCur = m_Fields[index+1];
  439. }
  440. // Get the label widget
  441. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(field_userptr(
  442. m_Fields[index-2]));
  443. cmCacheManager::GetInstance()->RemoveCacheEntry(lbl->GetValue());
  444. std::string nextVal (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))->GetValue());
  445. int x,y;
  446. getmaxyx(m_Window, y, x);
  447. this->InitializeUI(m_Window);
  448. this->Render(1, 1, x, y);
  449. // make the next or prev. current field after deletion
  450. nextCur = 0;
  451. std::vector<cmCursesCacheEntryComposite*>::iterator it;
  452. for (it = m_Entries->begin(); it != m_Entries->end(); ++it)
  453. {
  454. if (nextVal == (*it)->m_Key)
  455. {
  456. nextCur = (*it)->m_Entry->m_Field;
  457. }
  458. }
  459. if (nextCur)
  460. {
  461. set_current_field(m_Form, nextCur);
  462. }
  463. }
  464. }
  465. touchwin(m_Window);
  466. wrefresh(m_Window);
  467. }
  468. }