cmCursesMainForm.cxx 12 KB

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