cmCursesLongMessageForm.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "../cmCacheManager.h"
  14. #include "../cmSystemTools.h"
  15. #include "../cmake.h"
  16. #include "cmCursesLongMessageForm.h"
  17. #include "cmCursesMainForm.h"
  18. inline int ctrl(int z)
  19. {
  20. return (z&037);
  21. }
  22. cmCursesLongMessageForm::cmCursesLongMessageForm(std::vector<std::string>
  23. const& messages, const char*
  24. title)
  25. {
  26. // Append all messages into on big string
  27. std::vector<std::string>::const_iterator it;
  28. for(it=messages.begin(); it != messages.end(); it++)
  29. {
  30. m_Messages += (*it);
  31. // Add one blank line after each message
  32. m_Messages += "\n\n";
  33. }
  34. m_Title = title;
  35. m_Fields[0] = 0;
  36. m_Fields[1] = 0;
  37. }
  38. cmCursesLongMessageForm::~cmCursesLongMessageForm()
  39. {
  40. if (m_Fields[0])
  41. {
  42. free_field(m_Fields[0]);
  43. }
  44. }
  45. void cmCursesLongMessageForm::UpdateStatusBar()
  46. {
  47. int x,y;
  48. getmaxyx(stdscr, y, x);
  49. char bar[cmCursesMainForm::MAX_WIDTH];
  50. int size = strlen(m_Title.c_str());
  51. if ( size >= cmCursesMainForm::MAX_WIDTH )
  52. {
  53. size = cmCursesMainForm::MAX_WIDTH-1;
  54. }
  55. strncpy(bar, m_Title.c_str(), size);
  56. for(int i=size-1; i<cmCursesMainForm::MAX_WIDTH; i++) bar[i] = ' ';
  57. int width;
  58. if (x < cmCursesMainForm::MAX_WIDTH )
  59. {
  60. width = x;
  61. }
  62. else
  63. {
  64. width = cmCursesMainForm::MAX_WIDTH;
  65. }
  66. bar[width] = '\0';
  67. char version[cmCursesMainForm::MAX_WIDTH];
  68. char vertmp[128];
  69. sprintf(vertmp,"CMake Version %d.%d - %s", cmMakefile::GetMajorVersion(),
  70. cmMakefile::GetMinorVersion(),cmMakefile::GetReleaseVersion());
  71. int sideSpace = (width-strlen(vertmp));
  72. for(int i=0; i<sideSpace; i++) { version[i] = ' '; }
  73. sprintf(version+sideSpace, "%s", vertmp);
  74. version[width] = '\0';
  75. curses_move(y-4,0);
  76. attron(A_STANDOUT);
  77. printw(bar);
  78. attroff(A_STANDOUT);
  79. curses_move(y-3,0);
  80. printw(version);
  81. pos_form_cursor(m_Form);
  82. }
  83. void cmCursesLongMessageForm::PrintKeys()
  84. {
  85. int x,y;
  86. getmaxyx(stdscr, y, x);
  87. if ( x < cmCursesMainForm::MIN_WIDTH ||
  88. y < cmCursesMainForm::MIN_HEIGHT )
  89. {
  90. return;
  91. }
  92. char firstLine[512];
  93. sprintf(firstLine, "Press [e] to exit help");
  94. curses_move(y-2,0);
  95. printw(firstLine);
  96. pos_form_cursor(m_Form);
  97. }
  98. void cmCursesLongMessageForm::Render(int left, int top, int width, int height)
  99. {
  100. int x,y;
  101. getmaxyx(stdscr, y, x);
  102. if (m_Form)
  103. {
  104. unpost_form(m_Form);
  105. free_form(m_Form);
  106. m_Form = 0;
  107. }
  108. const char* msg = m_Messages.c_str();
  109. curses_clear();
  110. if (m_Fields[0])
  111. {
  112. free_field(m_Fields[0]);
  113. m_Fields[0] = 0;
  114. }
  115. m_Fields[0] = new_field(y-6, x-2, 1, 1, 0, 0);
  116. field_opts_off(m_Fields[0], O_STATIC);
  117. m_Form = new_form(m_Fields);
  118. post_form(m_Form);
  119. int i=0;
  120. form_driver(m_Form, REQ_BEG_FIELD);
  121. while(msg[i] != '\0')
  122. {
  123. if (msg[i] == '\n' && msg[i+1] != '\0')
  124. {
  125. form_driver(m_Form, REQ_NEW_LINE);
  126. }
  127. else
  128. {
  129. form_driver(m_Form, msg[i]);
  130. }
  131. i++;
  132. }
  133. form_driver(m_Form, REQ_BEG_FIELD);
  134. this->UpdateStatusBar();
  135. this->PrintKeys();
  136. touchwin(stdscr);
  137. refresh();
  138. }
  139. void cmCursesLongMessageForm::HandleInput()
  140. {
  141. if (!m_Form)
  142. {
  143. return;
  144. }
  145. char debugMessage[128];
  146. while(1)
  147. {
  148. int key = getch();
  149. sprintf(debugMessage, "Message widget handling input, key: %d", key);
  150. cmCursesForm::LogMessage(debugMessage);
  151. // quit
  152. if ( key == 'o' || key == 'e' )
  153. {
  154. break;
  155. }
  156. else if ( key == KEY_DOWN || key == ctrl('n') )
  157. {
  158. form_driver(m_Form, REQ_SCR_FLINE);
  159. }
  160. else if ( key == KEY_UP || key == ctrl('p') )
  161. {
  162. form_driver(m_Form, REQ_SCR_BLINE);
  163. }
  164. else if ( key == KEY_NPAGE || key == ctrl('d') )
  165. {
  166. form_driver(m_Form, REQ_SCR_FPAGE);
  167. }
  168. else if ( key == KEY_PPAGE || key == ctrl('u') )
  169. {
  170. form_driver(m_Form, REQ_SCR_BPAGE);
  171. }
  172. this->UpdateStatusBar();
  173. this->PrintKeys();
  174. touchwin(stdscr);
  175. wrefresh(stdscr);
  176. }
  177. }