1 // Created on: 1998-08-06
2 // Created by: Administrateur Atelier MDL
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
20 #include <CommandWindow.h>
21 #include <Draw_Window.hxx>
22 #include <MainWindow.h>
23 #include <Draw_Appli.hxx>
24 #include <TCollection_AsciiString.hxx>
28 #define THE_PROMPT L"Command >> "
29 #define COMMANDSIZE 1000 // Max nb of characters for a command
31 Standard_Boolean Draw_Interprete (const char* command);
35 // Definition of global variables
36 static WNDPROC OldEditProc; // Save the standard procedure of the edition (sub-class)
39 /*--------------------------------------------------------*\
40 | CREATE COMMAND WINDOW PROCEDURE
41 \*--------------------------------------------------------*/
42 HWND CreateCommandWindow(HWND hWnd, int /*nitem*/)
44 HINSTANCE hInstance = (HINSTANCE )GetWindowLongPtrW (hWnd, GWLP_HINSTANCE);
46 HWND hWndCommand = CreateWindowW (COMMANDCLASS, COMMANDTITLE,
47 WS_CLIPCHILDREN | WS_OVERLAPPED | WS_THICKFRAME | WS_CAPTION,
49 hWnd, NULL, hInstance, NULL);
51 ShowWindow(hWndCommand, SW_SHOW);
56 /*--------------------------------------------------------*\
57 | COMMAND WINDOW PROCEDURE
58 \*--------------------------------------------------------*/
59 LRESULT APIENTRY CommandProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
65 CommandCreateProc (hWnd);
66 HWND hWndEdit = (HWND )GetWindowLongPtrW (hWnd, CLIENTWND);
67 SendMessageW (hWndEdit, EM_REPLACESEL, 0, (LPARAM )THE_PROMPT);
70 case WM_GETMINMAXINFO:
72 MINMAXINFO* lpmmi = (MINMAXINFO* )lParam;
73 lpmmi->ptMinTrackSize.x = 200;
74 lpmmi->ptMinTrackSize.y = 50;
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);
89 HWND hWndEdit = (HWND )GetWindowLongPtrW (hWnd, CLIENTWND);
94 return DefWindowProcW(hWnd, wMsg, wParam, lParam);
97 LRESULT APIENTRY EditProc(HWND, UINT, WPARAM, LPARAM);
98 /*--------------------------------------------------------*\
99 | COMMAND CREATE PROCEDURE
100 \*--------------------------------------------------------*/
101 BOOL CommandCreateProc(HWND hWnd)
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,
110 // Save hWndEdit in the extra memory in 0 of CommandWindow
111 if (hWndEdit != NULL)
113 SetWindowLongPtrW (hWnd, CLIENTWND, (LONG_PTR )hWndEdit);
116 // Sub-Class of the window
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);
125 /*--------------------------------------------------------*\
128 \*--------------------------------------------------------*/
129 int GetCommand (HWND hWnd, wchar_t* theBuffer)
132 wchar_t aTempBuff[COMMANDSIZE] = L"";
134 int aNbLine = (int )SendMessageW (hWnd, EM_GETLINECOUNT, 0l, 0l);
136 theBuffer[0] = L'\0';
137 while (isAgain && aNbLine > -1 && aNbChar < COMMANDSIZE - 1)
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;
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';
150 wcscat (theBuffer, _wcsrev (aTempBuff));
159 extern console_semaphore_value volatile console_semaphore;
160 extern wchar_t console_command[1000];
162 /*--------------------------------------------------------*\
163 | EDIT WINDOW PROCEDURE
164 \*--------------------------------------------------------*/
165 LRESULT APIENTRY EditProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
167 static LRESULT nbline; // Process the buffer of the edit window
172 if (console_semaphore != WAIT_CONSOLE_COMMAND)
176 switch (LOWORD(wParam))
178 // Overload of character \n
181 wchar_t aCmdBuffer[COMMANDSIZE];
182 GetCommand (hWnd, aCmdBuffer);
183 // Standard processing
184 CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);
188 SendMessageW (hWnd, EM_REPLACESEL, 0, (LPARAM )THE_PROMPT);
189 // Display the command in the console
190 std::wcout << aCmdBuffer << std::endl;
191 //TCollection_AsciiString aCmdUtf8 (aCmdBuffer + sizeof(THE_PROMPT) / sizeof(wchar_t) - 1);
192 //Draw_Interprete (aCmdUtf8.ToCString());
193 //if (toExit) { DestroyProc (hWnd); }
194 wcscpy (console_command, aCmdBuffer + sizeof(THE_PROMPT) / sizeof(wchar_t) - 1);
195 console_semaphore = HAS_CONSOLE_COMMAND;
197 nbline = SendMessageW (hWnd, EM_GETLINECOUNT, 0l, 0l);
201 GetCommand (hWnd, aCmdBuffer);
202 LRESULT index = SendMessageW (hWnd, EM_LINEINDEX, 100, 0);
203 SendMessageW (hWnd, EM_SETSEL, 0, index);
204 SendMessageW (hWnd, WM_CUT, 0, 0);
205 // Place the cursor at the end of text
206 index = SendMessageW (hWnd, WM_GETTEXTLENGTH, 0l, 0l);
207 SendMessageW (hWnd, EM_SETSEL, index, index);
213 if (IsAlphanumeric ((Standard_Character)LOWORD(wParam)))
215 // Place the cursor at the end of text before display
216 LRESULT index = SendMessageW (hWnd, WM_GETTEXTLENGTH, 0l, 0l);
217 SendMessageW (hWnd, EM_SETSEL, index, index);
218 CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);
228 if (console_semaphore != WAIT_CONSOLE_COMMAND)
235 return CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);