0030430: Draw - command testgrid in parallel mode hangs if DRAW is launched without GUI
[occt.git] / src / Draw / CommandWindow.cxx
CommitLineData
b311480e 1// Created on: 1998-08-06
2// Created by: Administrateur Atelier MDL
3// Copyright (c) 1998-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
57c28b61 17#ifdef _WIN32
7fd59977 18#include <windows.h>
19
5fecc495 20#include <COMMANDWINDOW.h>
7fd59977 21#include <Draw_Window.hxx>
5fecc495 22#include <MAINWINDOW.h>
7fd59977 23#include <Draw_Appli.hxx>
ad03c234 24#include <TCollection_AsciiString.hxx>
7fd59977 25
7fd59977 26#define CLIENTWND 0
27
ad03c234 28#define THE_PROMPT L"Command >> "
0d969553 29#define COMMANDSIZE 1000 // Max nb of characters for a command
7fd59977 30
ad03c234 31Standard_Boolean Draw_Interprete (const char* command);
7fd59977 32
ad03c234 33namespace
34{
35 // Definition of global variables
36 static WNDPROC OldEditProc; // Save the standard procedure of the edition (sub-class)
37}
7fd59977 38
39/*--------------------------------------------------------*\
40| CREATE COMMAND WINDOW PROCEDURE
41\*--------------------------------------------------------*/
35e08fe8 42HWND CreateCommandWindow(HWND hWnd, int /*nitem*/)
7fd59977 43{
ad03c234 44 HINSTANCE hInstance = (HINSTANCE )GetWindowLongPtrW (hWnd, GWLP_HINSTANCE);
e89e2d67 45
ad03c234 46 HWND hWndCommand = CreateWindowW (COMMANDCLASS, COMMANDTITLE,
47 WS_CLIPCHILDREN | WS_OVERLAPPED | WS_THICKFRAME | WS_CAPTION,
48 0, 0, 400, 100,
49 hWnd, NULL, hInstance, NULL);
7fd59977 50
51 ShowWindow(hWndCommand, SW_SHOW);
52 return hWndCommand;
53}
54
55
56/*--------------------------------------------------------*\
57| COMMAND WINDOW PROCEDURE
58\*--------------------------------------------------------*/
6a7d83c4 59LRESULT APIENTRY CommandProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
7fd59977 60{
ad03c234 61 switch (wMsg)
7fd59977 62 {
ad03c234 63 case WM_CREATE:
6a7d83c4 64 {
ad03c234 65 CommandCreateProc (hWnd);
66 HWND hWndEdit = (HWND )GetWindowLongPtrW (hWnd, CLIENTWND);
67 SendMessageW (hWndEdit, EM_REPLACESEL, 0, (LPARAM )THE_PROMPT);
68 return 0;
69 }
70 case WM_GETMINMAXINFO:
71 {
72 MINMAXINFO* lpmmi = (MINMAXINFO* )lParam;
73 lpmmi->ptMinTrackSize.x = 200;
74 lpmmi->ptMinTrackSize.y = 50;
75 return 0;
76 }
77 case WM_SIZE:
78 {
79 HWND hWndEdit = (HWND )GetWindowLongPtrW(hWnd, CLIENTWND);
80 MoveWindow (hWndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
81 // Place the cursor at the end of the buffer
82 // Nb of characters in the buffer of hWndEdit
83 LRESULT index = SendMessageW (hWnd, WM_GETTEXTLENGTH, 0l, 0l);
84 SendMessageW (hWnd, EM_SETSEL, index, index);
85 return 0;
86 }
87 case WM_SETFOCUS:
88 {
89 HWND hWndEdit = (HWND )GetWindowLongPtrW (hWnd, CLIENTWND);
90 SetFocus (hWndEdit);
91 return 0;
6a7d83c4 92 }
7fd59977 93 }
ad03c234 94 return DefWindowProcW(hWnd, wMsg, wParam, lParam);
7fd59977 95}
96
6a7d83c4 97LRESULT APIENTRY EditProc(HWND, UINT, WPARAM, LPARAM);
7fd59977 98/*--------------------------------------------------------*\
99| COMMAND CREATE PROCEDURE
100\*--------------------------------------------------------*/
101BOOL CommandCreateProc(HWND hWnd)
102{
ad03c234 103 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW(hWnd, GWLP_HINSTANCE);
104 HWND hWndEdit = CreateWindowW (L"EDIT", NULL,
105 WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
106 0, 0, 0, 0,
107 hWnd, 0,
108 hInstance, NULL);
109
110 // Save hWndEdit in the extra memory in 0 of CommandWindow
111 if (hWndEdit != NULL)
112 {
113 SetWindowLongPtrW (hWnd, CLIENTWND, (LONG_PTR )hWndEdit);
114 }
7fd59977 115
ad03c234 116 // Sub-Class of the window
117 //-------
118 // Save the pointer on the existing procedure
119 OldEditProc = (WNDPROC )GetWindowLongPtrW (hWndEdit, GWLP_WNDPROC);
120 // Implement the new function
121 SetWindowLongPtrW (hWndEdit, GWLP_WNDPROC, (LONG_PTR) EditProc);
122 return TRUE;
7fd59977 123}
124
7fd59977 125/*--------------------------------------------------------*\
126| GET COMMAND
127|
128\*--------------------------------------------------------*/
ad03c234 129int GetCommand (HWND hWnd, wchar_t* theBuffer)
7fd59977 130{
ad03c234 131 bool isAgain = true;
132 wchar_t aTempBuff[COMMANDSIZE] = L"";
7fd59977 133
ad03c234 134 int aNbLine = (int )SendMessageW (hWnd, EM_GETLINECOUNT, 0l, 0l);
135 int aNbChar = 0;
136 theBuffer[0] = L'\0';
137 while (isAgain && aNbLine > -1 && aNbChar < COMMANDSIZE - 1)
138 {
139 wcscat (theBuffer, _wcsrev (aTempBuff));
140 // Initialization of the 1st WORD to the nb of characters to read
141 WORD* aNbMaxChar = (WORD* )aTempBuff;
142 *aNbMaxChar = COMMANDSIZE - 1;
7fd59977 143
ad03c234 144 const int aNbCharRead = (int )SendMessageW (hWnd, EM_GETLINE, aNbLine - 1, (LPARAM )aTempBuff);
145 aNbChar += aNbCharRead;
146 const bool isPromp = wcsncmp (aTempBuff, THE_PROMPT, 10) == 0;
147 aTempBuff[aNbCharRead]='\0';
148 if (isPromp)
149 {
150 wcscat (theBuffer, _wcsrev (aTempBuff));
151 isAgain = false;
152 }
153 aNbLine -= 1;
154 }
155 _wcsrev (theBuffer);
156 return aNbChar;
7fd59977 157}
158
7fd59977 159/*--------------------------------------------------------*\
160| EDIT WINDOW PROCEDURE
161\*--------------------------------------------------------*/
6a7d83c4 162LRESULT APIENTRY EditProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
7fd59977 163{
6a7d83c4 164 static LRESULT nbline; // Process the buffer of the edit window
ad03c234 165 switch (wMsg)
7fd59977 166 {
ad03c234 167 case WM_CHAR:
168 {
169 if (console_semaphore != WAIT_CONSOLE_COMMAND)
170 {
171 return 0;
172 }
173 switch (LOWORD(wParam))
174 {
175 // Overload of character \n
176 case 0x0d:
177 {
178 wchar_t aCmdBuffer[COMMANDSIZE];
179 GetCommand (hWnd, aCmdBuffer);
180 // Standard processing
181 CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);
182 // Display of PROMPT
183 POINT pos;
184 GetCaretPos (&pos);
185 SendMessageW (hWnd, EM_REPLACESEL, 0, (LPARAM )THE_PROMPT);
186 // Display the command in the console
187 std::wcout << aCmdBuffer << std::endl;
188 //TCollection_AsciiString aCmdUtf8 (aCmdBuffer + sizeof(THE_PROMPT) / sizeof(wchar_t) - 1);
189 //Draw_Interprete (aCmdUtf8.ToCString());
190 //if (toExit) { DestroyProc (hWnd); }
9b4243f9 191 wcscpy_s (console_command, aCmdBuffer + sizeof(THE_PROMPT) / sizeof(wchar_t) - 1);
ad03c234 192 console_semaphore = HAS_CONSOLE_COMMAND;
193 // Purge the buffer
194 nbline = SendMessageW (hWnd, EM_GETLINECOUNT, 0l, 0l);
195 if (nbline > 200)
196 {
197 nbline = 0;
198 GetCommand (hWnd, aCmdBuffer);
199 LRESULT index = SendMessageW (hWnd, EM_LINEINDEX, 100, 0);
200 SendMessageW (hWnd, EM_SETSEL, 0, index);
201 SendMessageW (hWnd, WM_CUT, 0, 0);
202 // Place the cursor at the end of text
203 index = SendMessageW (hWnd, WM_GETTEXTLENGTH, 0l, 0l);
204 SendMessageW (hWnd, EM_SETSEL, index, index);
205 }
206 return 0;
207 }
208 default:
209 {
210 if (IsAlphanumeric ((Standard_Character)LOWORD(wParam)))
211 {
212 // Place the cursor at the end of text before display
213 LRESULT index = SendMessageW (hWnd, WM_GETTEXTLENGTH, 0l, 0l);
214 SendMessageW (hWnd, EM_SETSEL, index, index);
215 CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);
216 return 0;
217 }
218 break;
219 }
220 }
221 break;
222 }
223 case WM_KEYDOWN:
224 {
225 if (console_semaphore != WAIT_CONSOLE_COMMAND)
226 {
227 return 0;
228 }
229 break;
230 }
7fd59977 231 }
ad03c234 232 return CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);
7fd59977 233}
234#endif