0028110: Configuration - specify Unicode charset instead of multibyte in project...
[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
7fd59977 20#include <CommandWindow.h>
21#include <Draw_Window.hxx>
22#include <MainWindow.h>
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
159extern console_semaphore_value volatile console_semaphore;
ad03c234 160extern wchar_t console_command[1000];
7fd59977 161
162/*--------------------------------------------------------*\
163| EDIT WINDOW PROCEDURE
164\*--------------------------------------------------------*/
6a7d83c4 165LRESULT APIENTRY EditProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
7fd59977 166{
6a7d83c4 167 static LRESULT nbline; // Process the buffer of the edit window
ad03c234 168 switch (wMsg)
7fd59977 169 {
ad03c234 170 case WM_CHAR:
171 {
172 if (console_semaphore != WAIT_CONSOLE_COMMAND)
173 {
174 return 0;
175 }
176 switch (LOWORD(wParam))
177 {
178 // Overload of character \n
179 case 0x0d:
180 {
181 wchar_t aCmdBuffer[COMMANDSIZE];
182 GetCommand (hWnd, aCmdBuffer);
183 // Standard processing
184 CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);
185 // Display of PROMPT
186 POINT pos;
187 GetCaretPos (&pos);
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;
196 // Purge the buffer
197 nbline = SendMessageW (hWnd, EM_GETLINECOUNT, 0l, 0l);
198 if (nbline > 200)
199 {
200 nbline = 0;
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);
208 }
209 return 0;
210 }
211 default:
212 {
213 if (IsAlphanumeric ((Standard_Character)LOWORD(wParam)))
214 {
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);
219 return 0;
220 }
221 break;
222 }
223 }
224 break;
225 }
226 case WM_KEYDOWN:
227 {
228 if (console_semaphore != WAIT_CONSOLE_COMMAND)
229 {
230 return 0;
231 }
232 break;
233 }
7fd59977 234 }
ad03c234 235 return CallWindowProcW (OldEditProc, hWnd, wMsg, wParam, lParam);
7fd59977 236}
237#endif