| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkWin32OutputWindow.cxx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:49 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
DEF |
=========================================================================*/ |
| 20 |
|
#include "itkWin32OutputWindow.h" |
| 21 |
|
#include "itkObjectFactory.h" |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
/** |
| 26 |
|
* |
| 27 |
|
*/ |
| 28 |
|
HWND Win32OutputWindow::m_OutputWindow = 0; |
| 29 |
|
|
| 30 |
|
Win32OutputWindow |
| 31 |
|
::~Win32OutputWindow() |
| 32 |
|
{ |
| 33 |
|
if (Win32OutputWindow::m_OutputWindow) |
| 34 |
|
{ |
| 35 |
|
DestroyWindow(Win32OutputWindow::m_OutputWindow); |
| 36 |
|
Win32OutputWindow::m_OutputWindow = NULL; |
| 37 |
|
} |
| 38 |
|
} |
| 39 |
|
/** |
| 40 |
|
* |
| 41 |
|
*/ |
| 42 |
|
LRESULT APIENTRY |
| 43 |
|
Win32OutputWindow |
| 44 |
|
::WndProc(HWND hWnd, UINT message, |
| 45 |
|
WPARAM wParam, |
| 46 |
|
LPARAM lParam) |
| 47 |
|
{ |
| 48 |
|
switch (message) |
| 49 |
|
{ |
| 50 |
|
case WM_SIZE: |
| 51 |
IND |
****{ |
| 52 |
IND |
****/** |
| 53 |
IND |
****** width of client area |
| 54 |
IND |
******/ |
| 55 |
IND |
****int w = LOWORD(lParam); |
| 56 |
|
|
| 57 |
IND |
****/** |
| 58 |
IND |
****** height of client area |
| 59 |
IND |
******/ |
| 60 |
IND |
****int h = HIWORD(lParam); |
| 61 |
|
|
| 62 |
IND |
****MoveWindow(Win32OutputWindow::m_OutputWindow, |
| 63 |
|
0, 0, w, h, true); |
| 64 |
IND |
****} |
| 65 |
|
break; |
| 66 |
|
case WM_DESTROY: |
| 67 |
IND |
******Win32OutputWindow::m_OutputWindow = NULL; |
| 68 |
IND |
******Object::GlobalWarningDisplayOff(); |
| 69 |
IND |
******break; |
| 70 |
|
case WM_CLOSE: |
| 71 |
IND |
******if (Win32OutputWindow::m_OutputWindow) |
| 72 |
IND |
********{ |
| 73 |
IND |
********DestroyWindow(Win32OutputWindow::m_OutputWindow); |
| 74 |
IND |
********Win32OutputWindow::m_OutputWindow = NULL; |
| 75 |
IND |
********} |
| 76 |
IND |
******break; |
| 77 |
|
case WM_CREATE: |
| 78 |
IND |
******break; |
| 79 |
|
} |
| 80 |
|
return DefWindowProc(hWnd, message, wParam, lParam); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
/** |
| 84 |
|
* Display text in the window, and translate the \n to \r\n. |
| 85 |
|
*/ |
| 86 |
|
void |
| 87 |
|
Win32OutputWindow |
| 88 |
|
::DisplayText(const char* text) |
| 89 |
|
{ |
| 90 |
|
if ( !text ) |
| 91 |
|
{ |
| 92 |
|
return; |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
if ( this->GetPromptUser() ) |
| 96 |
|
{ |
| 97 |
|
this->PromptText(text); |
| 98 |
|
return; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
/** |
| 102 |
|
* Create a buffer big enough to hold the entire text |
| 103 |
|
*/ |
| 104 |
|
char* buffer = new char[strlen(text)+1]; |
| 105 |
|
|
| 106 |
|
/** |
| 107 |
|
* Start at the begining |
| 108 |
|
*/ |
| 109 |
|
const char* NewLinePos = text; |
| 110 |
|
while ( NewLinePos ) |
| 111 |
|
{ |
| 112 |
|
int len; |
| 113 |
|
/** |
| 114 |
|
* Find the next new line in text |
| 115 |
|
*/ |
| 116 |
|
NewLinePos = strchr(text, '\n'); |
| 117 |
|
/** |
| 118 |
|
* if no new line is found then just add the text |
| 119 |
|
*/ |
| 120 |
|
if(NewLinePos == 0) |
| 121 |
|
{ |
| 122 |
|
Win32OutputWindow::AddText(text); |
| 123 |
|
} |
| 124 |
|
/** |
| 125 |
|
* if a new line is found copy it to the buffer |
| 126 |
|
* and add the buffer with a control new line |
| 127 |
|
*/ |
| 128 |
|
else |
| 129 |
|
{ |
| 130 |
|
len = NewLinePos - text; |
| 131 |
|
strncpy(buffer, text, len); |
| 132 |
|
buffer[len] = 0; |
| 133 |
|
text = NewLinePos+1; |
| 134 |
|
Win32OutputWindow::AddText(buffer); |
| 135 |
|
Win32OutputWindow::AddText("\r\n"); |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
|
delete [] buffer; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
/** |
| 143 |
|
* Add some text to the EDIT control. |
| 144 |
|
*/ |
| 145 |
|
|
| 146 |
|
void |
| 147 |
|
Win32OutputWindow |
| 148 |
|
::AddText(const char* text) |
| 149 |
|
{ |
| 150 |
|
if(!Initialize() || (strlen(text) == 0)) |
| 151 |
|
{ |
| 152 |
|
return; |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
/** |
| 156 |
|
* move to the end of the text area |
| 157 |
|
*/ |
| 158 |
|
SendMessage( Win32OutputWindow::m_OutputWindow, EM_SETSEL, |
| 159 |
|
(WPARAM)-1, (LPARAM)-1 ); |
| 160 |
|
/** |
| 161 |
|
* Append the text to the control |
| 162 |
|
*/ |
| 163 |
|
SendMessage( Win32OutputWindow::m_OutputWindow, EM_REPLACESEL, |
| 164 |
|
0, (LPARAM)text ); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
|
| 168 |
|
/** |
| 169 |
|
* initialize the output window with an EDIT control and |
| 170 |
|
* a container window. |
| 171 |
|
*/ |
| 172 |
|
|
| 173 |
|
int |
| 174 |
|
Win32OutputWindow |
| 175 |
|
::Initialize() |
| 176 |
|
{ |
| 177 |
|
/** |
| 178 |
|
* check to see if it is already initialized |
| 179 |
|
*/ |
| 180 |
|
if(Win32OutputWindow::m_OutputWindow) |
| 181 |
|
{ |
| 182 |
|
return 1; |
| 183 |
|
} |
| 184 |
|
/** |
| 185 |
|
* Initialized the output window |
| 186 |
|
*/ |
| 187 |
|
|
| 188 |
|
WNDCLASS wndClass; |
| 189 |
|
/** |
| 190 |
|
* has the class been registered ? |
| 191 |
|
*/ |
| 192 |
|
if (!GetClassInfo(GetModuleHandle(NULL),"OutputWindow",&wndClass)) |
| 193 |
|
{ |
| 194 |
|
wndClass.style = CS_HREDRAW | CS_VREDRAW; |
| 195 |
|
wndClass.lpfnWndProc = Win32OutputWindow::WndProc; |
| 196 |
|
wndClass.cbClsExtra = 0; |
| 197 |
|
wndClass.hInstance = GetModuleHandle(NULL); |
| 198 |
|
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 199 |
|
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 200 |
|
wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); |
| 201 |
|
wndClass.lpszMenuName = NULL; |
| 202 |
|
wndClass.lpszClassName = "OutputWindow"; |
| 203 |
|
/** |
| 204 |
|
* doesn't use these extra 4 bytes, but app writers |
| 205 |
|
* may want them, so we provide them. |
| 206 |
|
*/ |
| 207 |
|
wndClass.cbWndExtra = 4; |
| 208 |
|
RegisterClass(&wndClass); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
/** |
| 212 |
|
* create parent container window |
| 213 |
|
*/ |
| 214 |
|
HWND win = CreateWindow( |
| 215 |
|
"OutputWindow", "OutputWindow", |
| 216 |
|
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, |
| 217 |
|
0, 0, 512, 512, |
| 218 |
|
NULL, NULL, GetModuleHandle(NULL), NULL); |
| 219 |
|
|
| 220 |
|
/** |
| 221 |
|
* Now create child window with text display box |
| 222 |
|
*/ |
| 223 |
|
CREATESTRUCT lpParam; |
| 224 |
|
lpParam.hInstance = GetModuleHandle(NULL); |
| 225 |
|
lpParam.hMenu = NULL; |
| 226 |
|
lpParam.hwndParent = win; |
| 227 |
|
lpParam.cx = 512; |
| 228 |
|
lpParam.cy = 512; |
| 229 |
|
lpParam.x = 0; |
| 230 |
|
lpParam.y = 0; |
| 231 |
|
lpParam.style = ES_MULTILINE | ES_READONLY | WS_CHILD |
| 232 |
IND |
****| ES_AUTOVSCROLL | ES_AUTOHSCROLL | WS_VISIBLE | WS_MAXIMIZE |
| 233 |
IND |
****| WS_VSCROLL | WS_HSCROLL; |
| 234 |
|
|
| 235 |
|
lpParam.lpszName = "Output Control"; |
| 236 |
|
lpParam.lpszClass = "EDIT"; // use the RICHEDIT control widget |
| 237 |
|
lpParam.dwExStyle = 0; |
| 238 |
|
/** |
| 239 |
|
* Create the EDIT window as a child of win |
| 240 |
|
*/ |
| 241 |
|
Win32OutputWindow::m_OutputWindow = CreateWindow( |
| 242 |
|
lpParam.lpszClass, // pointer to registered class name |
| 243 |
|
"", // pointer to window name |
| 244 |
|
lpParam.style, // window style |
| 245 |
|
lpParam.x, // horizontal position of window |
| 246 |
|
lpParam.y, // vertical position of window |
| 247 |
|
lpParam.cx, // window width |
| 248 |
|
lpParam.cy, // window height |
| 249 |
|
lpParam.hwndParent, // handle to parent or owner window |
| 250 |
|
NULL, // handle to menu or child-window identifier |
| 251 |
|
lpParam.hInstance, // handle to application instance |
| 252 |
|
&lpParam // pointer to window-creation data |
| 253 |
IND |
****); |
| 254 |
|
const int maxsize = 5242880; |
| 255 |
|
|
| 256 |
|
SendMessage(Win32OutputWindow::m_OutputWindow, |
| 257 |
|
EM_LIMITTEXT, maxsize, 0L); |
| 258 |
|
|
| 259 |
|
|
| 260 |
|
/** |
| 261 |
|
* show the top level container window |
| 262 |
|
*/ |
| 263 |
|
ShowWindow(win, SW_SHOW); |
| 264 |
|
return 1; |
| 265 |
|
} |
| 266 |
|
|
| 267 |
|
|
| 268 |
|
/** |
| 269 |
|
* |
| 270 |
|
*/ |
| 271 |
|
void |
| 272 |
|
Win32OutputWindow |
| 273 |
|
::PromptText(const char* text) |
| 274 |
|
{ |
| 275 |
|
OStringStream msg; |
| 276 |
|
msg << text << "\nPress Cancel to supress any further messages."; |
| 277 |
|
if (MessageBox(NULL, msg.str().c_str(), "Error", |
| 278 |
IND |
*****************MB_ICONERROR | MB_OKCANCEL) == IDCANCEL) |
| 279 |
|
{ |
| 280 |
|
Object::GlobalWarningDisplayOff(); |
| 281 |
|
} |
| 282 |
|
} |
| 283 |
|
|
| 284 |
|
} // end namespace itk |
| 285 |
|
|