cmCursesLongMessageForm.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "../cmCacheManager.h"
  11. #include "../cmSystemTools.h"
  12. #include "../cmake.h"
  13. #include "../cmVersion.h"
  14. #include "cmCursesLongMessageForm.h"
  15. #include "cmCursesMainForm.h"
  16. inline int ctrl(int z)
  17. {
  18. return (z&037);
  19. }
  20. cmCursesLongMessageForm::cmCursesLongMessageForm(std::vector<std::string>
  21. const& messages, const char*
  22. title)
  23. {
  24. // Append all messages into on big string
  25. std::vector<std::string>::const_iterator it;
  26. for(it=messages.begin(); it != messages.end(); it++)
  27. {
  28. this->Messages += (*it);
  29. // Add one blank line after each message
  30. this->Messages += "\n\n";
  31. }
  32. this->Title = title;
  33. this->Fields[0] = 0;
  34. this->Fields[1] = 0;
  35. }
  36. cmCursesLongMessageForm::~cmCursesLongMessageForm()
  37. {
  38. if (this->Fields[0])
  39. {
  40. free_field(this->Fields[0]);
  41. }
  42. }
  43. void cmCursesLongMessageForm::UpdateStatusBar()
  44. {
  45. int x,y;
  46. getmaxyx(stdscr, y, x);
  47. char bar[cmCursesMainForm::MAX_WIDTH];
  48. size_t size = strlen(this->Title.c_str());
  49. if ( size >= cmCursesMainForm::MAX_WIDTH )
  50. {
  51. size = cmCursesMainForm::MAX_WIDTH-1;
  52. }
  53. strncpy(bar, this->Title.c_str(), size);
  54. for(size_t i=size-1; i<cmCursesMainForm::MAX_WIDTH; i++) bar[i] = ' ';
  55. int width;
  56. if (x < cmCursesMainForm::MAX_WIDTH )
  57. {
  58. width = x;
  59. }
  60. else
  61. {
  62. width = cmCursesMainForm::MAX_WIDTH-1;
  63. }
  64. bar[width] = '\0';
  65. char version[cmCursesMainForm::MAX_WIDTH];
  66. char vertmp[128];
  67. sprintf(vertmp,"CMake Version %s", cmVersion::GetCMakeVersion());
  68. size_t sideSpace = (width-strlen(vertmp));
  69. for(size_t i=0; i<sideSpace; i++) { version[i] = ' '; }
  70. sprintf(version+sideSpace, "%s", vertmp);
  71. version[width] = '\0';
  72. curses_move(y-4,0);
  73. attron(A_STANDOUT);
  74. printw(bar);
  75. attroff(A_STANDOUT);
  76. curses_move(y-3,0);
  77. printw(version);
  78. pos_form_cursor(this->Form);
  79. }
  80. void cmCursesLongMessageForm::PrintKeys()
  81. {
  82. int x,y;
  83. getmaxyx(stdscr, y, x);
  84. if ( x < cmCursesMainForm::MIN_WIDTH ||
  85. y < cmCursesMainForm::MIN_HEIGHT )
  86. {
  87. return;
  88. }
  89. char firstLine[512];
  90. sprintf(firstLine, "Press [e] to exit help");
  91. curses_move(y-2,0);
  92. printw(firstLine);
  93. pos_form_cursor(this->Form);
  94. }
  95. void cmCursesLongMessageForm::Render(int, int, int, int)
  96. {
  97. int x,y;
  98. getmaxyx(stdscr, y, x);
  99. if (this->Form)
  100. {
  101. unpost_form(this->Form);
  102. free_form(this->Form);
  103. this->Form = 0;
  104. }
  105. const char* msg = this->Messages.c_str();
  106. curses_clear();
  107. if (this->Fields[0])
  108. {
  109. free_field(this->Fields[0]);
  110. this->Fields[0] = 0;
  111. }
  112. this->Fields[0] = new_field(y-6, x-2, 1, 1, 0, 0);
  113. field_opts_off(this->Fields[0], O_STATIC);
  114. this->Form = new_form(this->Fields);
  115. post_form(this->Form);
  116. int i=0;
  117. form_driver(this->Form, REQ_BEG_FIELD);
  118. while(msg[i] != '\0' && i < 60000)
  119. {
  120. if (msg[i] == '\n' && msg[i+1] != '\0')
  121. {
  122. form_driver(this->Form, REQ_NEW_LINE);
  123. }
  124. else
  125. {
  126. form_driver(this->Form, msg[i]);
  127. }
  128. i++;
  129. }
  130. form_driver(this->Form, REQ_BEG_FIELD);
  131. this->UpdateStatusBar();
  132. this->PrintKeys();
  133. touchwin(stdscr);
  134. refresh();
  135. }
  136. void cmCursesLongMessageForm::HandleInput()
  137. {
  138. if (!this->Form)
  139. {
  140. return;
  141. }
  142. char debugMessage[128];
  143. for(;;)
  144. {
  145. int key = getch();
  146. sprintf(debugMessage, "Message widget handling input, key: %d", key);
  147. cmCursesForm::LogMessage(debugMessage);
  148. // quit
  149. if ( key == 'o' || key == 'e' )
  150. {
  151. break;
  152. }
  153. else if ( key == KEY_DOWN || key == ctrl('n') )
  154. {
  155. form_driver(this->Form, REQ_SCR_FLINE);
  156. }
  157. else if ( key == KEY_UP || key == ctrl('p') )
  158. {
  159. form_driver(this->Form, REQ_SCR_BLINE);
  160. }
  161. else if ( key == KEY_NPAGE || key == ctrl('d') )
  162. {
  163. form_driver(this->Form, REQ_SCR_FPAGE);
  164. }
  165. else if ( key == KEY_PPAGE || key == ctrl('u') )
  166. {
  167. form_driver(this->Form, REQ_SCR_BPAGE);
  168. }
  169. this->UpdateStatusBar();
  170. this->PrintKeys();
  171. touchwin(stdscr);
  172. wrefresh(stdscr);
  173. }
  174. }