Orfeo Toolbox  4.0
itkWin32OutputWindow.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #include "itkWin32OutputWindow.h"
29 
30 namespace itk
31 {
33 HWND Win32OutputWindow:: m_OutputWindow = 0;
34 
35 Win32OutputWindow
36 ::~Win32OutputWindow()
37 {
38  if ( Win32OutputWindow::m_OutputWindow )
39  {
40  DestroyWindow(Win32OutputWindow::m_OutputWindow);
41  Win32OutputWindow::m_OutputWindow = NULL;
42  }
43 }
44 
46 LRESULT APIENTRY
47 Win32OutputWindow
48 ::WndProc(HWND hWnd, UINT message,
49  WPARAM wParam,
50  LPARAM lParam)
51 {
52  switch ( message )
53  {
54  case WM_SIZE:
55  {
57  int w = LOWORD(lParam);
58 
60  int h = HIWORD(lParam);
61 
62  MoveWindow(Win32OutputWindow::m_OutputWindow,
63  0, 0, w, h, true);
64  }
65  break;
66  case WM_DESTROY:
67  Win32OutputWindow::m_OutputWindow = NULL;
69  break;
70  case WM_CLOSE:
71  if ( Win32OutputWindow::m_OutputWindow )
72  {
73  DestroyWindow(Win32OutputWindow::m_OutputWindow);
74  Win32OutputWindow::m_OutputWindow = NULL;
75  }
76  break;
77  case WM_CREATE:
78  break;
79  }
80  return DefWindowProc(hWnd, message, wParam, lParam);
81 }
82 
84 void
85 Win32OutputWindow
86 ::DisplayText(const char *text)
87 {
88  if ( !text )
89  {
90  return;
91  }
92 
93  if ( this->GetPromptUser() )
94  {
95  this->PromptText(text);
96  return;
97  }
98 
100  char *buffer = new char[strlen(text) + 1];
101 
103  const char *NewLinePos = text;
104  while ( NewLinePos )
105  {
107  NewLinePos = strchr(text, '\n');
109  if ( NewLinePos == 0 )
110  {
111  Win32OutputWindow::AddText(text);
112  }
115  else
116  {
117  int len = NewLinePos - text;
118  strncpy(buffer, text, len);
119  buffer[len] = 0;
120  text = NewLinePos + 1;
121  Win32OutputWindow::AddText(buffer);
122  Win32OutputWindow::AddText("\r\n");
123  }
124  }
125  delete[] buffer;
126 }
127 
129 void
130 Win32OutputWindow
131 ::AddText(const char *text)
132 {
133  if ( !Initialize() || ( strlen(text) == 0 ) )
134  {
135  return;
136  }
137 
139  SendMessage(Win32OutputWindow::m_OutputWindow, EM_SETSEL,
140  (WPARAM)-1, (LPARAM)-1);
141 
143  SendMessage(Win32OutputWindow::m_OutputWindow, EM_REPLACESEL,
144  0, (LPARAM)text);
145 }
146 
149 int
150 Win32OutputWindow
151 ::Initialize()
152 {
154  if ( Win32OutputWindow::m_OutputWindow )
155  {
156  return 1;
157  }
160  WNDCLASS wndClass;
162  if ( !GetClassInfo(GetModuleHandle(NULL), "OutputWindow", &wndClass) )
163  {
164  wndClass.style = CS_HREDRAW | CS_VREDRAW;
165  wndClass.lpfnWndProc = Win32OutputWindow::WndProc;
166  wndClass.cbClsExtra = 0;
167  wndClass.hInstance = GetModuleHandle(NULL);
168  wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
169  wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
170  wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
171  wndClass.lpszMenuName = NULL;
172  wndClass.lpszClassName = "OutputWindow";
175  wndClass.cbWndExtra = 4;
176  RegisterClass(&wndClass);
177  }
178 
180  HWND win = CreateWindow(
181  "OutputWindow", "OutputWindow",
182  WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
183  0, 0, 512, 512,
184  NULL, NULL, GetModuleHandle(NULL), NULL);
185 
187  CREATESTRUCT lpParam;
188  lpParam.hInstance = GetModuleHandle(NULL);
189  lpParam.hMenu = NULL;
190  lpParam.hwndParent = win;
191  lpParam.cx = 512;
192  lpParam.cy = 512;
193  lpParam.x = 0;
194  lpParam.y = 0;
195  lpParam.style = ES_MULTILINE | ES_READONLY | WS_CHILD
196  | ES_AUTOVSCROLL | ES_AUTOHSCROLL | WS_VISIBLE | WS_MAXIMIZE
197  | WS_VSCROLL | WS_HSCROLL;
198 
199  lpParam.lpszName = "Output Control";
200  lpParam.lpszClass = "EDIT"; // use the RICHEDIT control widget
201  lpParam.dwExStyle = 0;
202 
204  Win32OutputWindow::m_OutputWindow = CreateWindow(
205  lpParam.lpszClass, // pointer to registered class name
206  "", // pointer to window name
207  lpParam.style, // window style
208  lpParam.x, // horizontal position of window
209  lpParam.y, // vertical position of window
210  lpParam.cx, // window width
211  lpParam.cy, // window height
212  lpParam.hwndParent, // handle to parent or owner window
213  NULL, // handle to menu or child-window identifier
214  lpParam.hInstance, // handle to application instance
215  &lpParam // pointer to window-creation data
216  );
217  const int maxsize = 5242880;
218 
219  SendMessage(Win32OutputWindow::m_OutputWindow,
220  EM_LIMITTEXT, maxsize, 0L);
221 
223  ShowWindow(win, SW_SHOW);
224  return 1;
225 }
226 
228 void
229 Win32OutputWindow
230 ::PromptText(const char *text)
231 {
232  std::ostringstream msg;
233 
234  msg << text << "\nPress Cancel to suppress any further messages.";
235  if ( MessageBox(NULL, msg.str().c_str(), "Error",
236  MB_ICONERROR | MB_OKCANCEL) == IDCANCEL )
237  {
239  }
240 }
241 } // end namespace itk

Generated at Sat Mar 8 2014 15:47:47 for Orfeo Toolbox with doxygen 1.8.3.1