cmCursesLongMessageForm.cxx 4.5 KB

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