0027772: Foundation Classes - define Standard_Boolean using C++ type "bool" instead...
[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
20#define COMMANDCLASS "COMMANDWINDOW"
21#define COMMANDTITLE "Command Window"
22
23#include <CommandWindow.h>
24#include <Draw_Window.hxx>
25#include <MainWindow.h>
26#include <Draw_Appli.hxx>
27
28
29
30/****************************************************\
31* CommandWindow.cxx :
32*
33\****************************************************/
34
35
36
7fd59977 37#define CLIENTWND 0
38
39#define PROMPT "Command >> "
0d969553 40#define COMMANDSIZE 1000 // Max nb of characters for a command
7fd59977 41
42
0d969553 43// Definition of global variables
7fd59977 44#ifdef STRICT
0d969553 45 WNDPROC OldEditProc; // Save the standard procedure of the edition (sub-class)
7fd59977 46#else
47 FARPROC OldEditProc;
48#endif
49
50/*--------------------------------------------------------*\
51| CREATE COMMAND WINDOW PROCEDURE
52\*--------------------------------------------------------*/
35e08fe8 53HWND CreateCommandWindow(HWND hWnd, int /*nitem*/)
7fd59977 54{
55 HINSTANCE hInstance;
e89e2d67 56 hInstance = (HINSTANCE)GetWindowLongPtr(hWnd,GWLP_HINSTANCE);
57
7fd59977 58 HWND hWndCommand = (CreateWindow(COMMANDCLASS, COMMANDTITLE,
59 WS_CLIPCHILDREN | WS_OVERLAPPED |
60 WS_THICKFRAME | WS_CAPTION ,
61 0, 0,
62 400, 100,
63 hWnd, NULL, hInstance, NULL));
64
65 ShowWindow(hWndCommand, SW_SHOW);
66 return hWndCommand;
67}
68
69
70/*--------------------------------------------------------*\
71| COMMAND WINDOW PROCEDURE
72\*--------------------------------------------------------*/
6a7d83c4 73LRESULT APIENTRY CommandProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
7fd59977 74{
75 HWND hWndEdit;
7fd59977 76 MINMAXINFO* lpmmi;
77
78 switch(wMsg)
79 {
80 case WM_CREATE :
81 CommandCreateProc(hWnd);
e89e2d67 82 hWndEdit = (HWND)GetWindowLongPtr(hWnd, CLIENTWND);
7fd59977 83 SendMessage(hWndEdit,EM_REPLACESEL, 0,(LPARAM)PROMPT);
84 break;
85
86 case WM_GETMINMAXINFO :
87 lpmmi = (LPMINMAXINFO)lParam;
88 lpmmi->ptMinTrackSize.x = 200;
89 lpmmi->ptMinTrackSize.y = 50;
90 break;
91
92 case WM_SIZE :
6a7d83c4 93 {
e89e2d67 94 hWndEdit = (HWND)GetWindowLongPtr(hWnd, CLIENTWND);
7fd59977 95 MoveWindow(hWndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
6a7d83c4 96 // Place the cursor at the end of the buffer
97 // Nb of characters in the buffer of hWndEdit
98 LRESULT index = SendMessage(hWnd, WM_GETTEXTLENGTH, 0l, 0l);
7fd59977 99 SendMessage(hWnd, EM_SETSEL, index, index);
100 break;
6a7d83c4 101 }
7fd59977 102
103 case WM_SETFOCUS :
e89e2d67 104 hWndEdit = (HWND)GetWindowLongPtr(hWnd, CLIENTWND);
7fd59977 105 SetFocus(hWndEdit);
106 break;
107
108 default :
109 return(DefWindowProc(hWnd, wMsg, wParam, lParam));
110 }
111 return(0l);
112}
113
114
115
6a7d83c4 116LRESULT APIENTRY EditProc(HWND, UINT, WPARAM, LPARAM);
7fd59977 117/*--------------------------------------------------------*\
118| COMMAND CREATE PROCEDURE
119\*--------------------------------------------------------*/
120BOOL CommandCreateProc(HWND hWnd)
121{
122
e89e2d67 123 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
124
7fd59977 125 HWND hWndEdit = CreateWindow("EDIT",NULL,
126 WS_CHILD | WS_VISIBLE | WS_VSCROLL |
127 ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
128 0, 0, 0, 0,
129 hWnd, 0,
130 hInstance, NULL);
131
0d969553 132 // Save hWndEdit in the extra memory in 0 of CommandWindow
7fd59977 133 if (hWndEdit)
e89e2d67 134 SetWindowLongPtr(hWnd, CLIENTWND, (LONG_PTR)hWndEdit);
7fd59977 135
0d969553 136 // Sub-Class of the window
7fd59977 137 //-------
0d969553 138 // Save the pointer on the existing procedure
7fd59977 139 #ifdef STRICT
e89e2d67 140 OldEditProc = (WNDPROC)GetWindowLongPtr(hWndEdit, GWLP_WNDPROC);
7fd59977 141 #else
e89e2d67 142 OldEditProc = (FARPROC)GetWindowLongPtr(hWndEdit, GWLP_WNDPROC);
7fd59977 143 #endif
0d969553 144 // Implement the new function
e89e2d67 145 SetWindowLongPtr(hWndEdit, GWLP_WNDPROC, (LONG_PTR) EditProc);
146
7fd59977 147 return(TRUE);
148}
149
150
151/*--------------------------------------------------------*\
152| GET COMMAND
153|
154\*--------------------------------------------------------*/
155int GetCommand(HWND hWnd, char* buffer)
156{
157 int again = 1;
158 char temp[COMMANDSIZE]="";
159
6a7d83c4 160 int nbLine = (int )SendMessage(hWnd, EM_GETLINECOUNT, 0l, 0l);
7fd59977 161
162 int nbChar = 0;
163 buffer[0]='\0';
164 while ( again && nbLine > -1 && nbChar < COMMANDSIZE-1)
165 {
166 strcat(buffer, strrev(temp));
0d969553 167 // Initialization of the 1st WORD to the nb of characters to read
7fd59977 168 WORD* nbMaxChar = (WORD*)temp;
169 *nbMaxChar = COMMANDSIZE-1;
170
6a7d83c4 171 int nbCharRead = (int )SendMessage(hWnd, EM_GETLINE, nbLine-1, (LPARAM)temp);
7fd59977 172 nbChar += nbCharRead ;
173 int cmp = strncmp(temp, PROMPT, 10);
174 temp[nbCharRead]='\0';
175 if( cmp == 0 )
176 {
177 strcat(buffer, strrev(temp));
178 again = 0;
179 }
180 nbLine -= 1;
181 }
182 strrev(buffer);
183 return nbChar;
184}
185
186extern console_semaphore_value volatile console_semaphore;
187extern char console_command[1000];
188
189/*--------------------------------------------------------*\
190| EDIT WINDOW PROCEDURE
191\*--------------------------------------------------------*/
6a7d83c4 192LRESULT APIENTRY EditProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
7fd59977 193{
194 char buffer[COMMANDSIZE];
195 POINT pos;
7c65581d 196 BOOL rep = 0;
6a7d83c4 197 static LRESULT nbline; // Process the buffer of the edit window
198 LRESULT index;
199
7fd59977 200 switch(wMsg)
201 {
202 case WM_CHAR :
203 if (console_semaphore != WAIT_CONSOLE_COMMAND)
204 return 0l;
205 switch(LOWORD(wParam))
206 {
0d969553 207 // Overload of character \n
7fd59977 208 case 0x0d :
209 GetCommand(hWnd, buffer);
0d969553 210 // Standard processing
7fd59977 211 CallWindowProc(OldEditProc, hWnd, wMsg, wParam, lParam);
0d969553 212 // Display of PROMPT
7fd59977 213 rep = GetCaretPos(&pos);
214 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)PROMPT);
0d969553 215 // Display the command in the console
7fd59977 216 cout << buffer << endl;
217 /*if (Draw_Interprete(buffer+strlen(PROMPT))== -2)
218 DestroyProc(hWnd); */
219 strcpy(console_command, buffer+strlen(PROMPT));
220 console_semaphore = HAS_CONSOLE_COMMAND;
0d969553 221 // Purge the buffer
7fd59977 222 nbline = SendMessage(hWnd, EM_GETLINECOUNT, 0l, 0l);
223 if(nbline > 200)
224 {
225 nbline = 0;
226 GetCommand(hWnd, buffer);
227 index = SendMessage(hWnd, EM_LINEINDEX, 100, 0);
228 SendMessage(hWnd, EM_SETSEL, 0, index);
229 SendMessage(hWnd, WM_CUT, 0, 0);
0d969553 230 // Place the cursor at the end of text
7fd59977 231 index = SendMessage(hWnd, WM_GETTEXTLENGTH, 0l, 0l);
232 SendMessage(hWnd, EM_SETSEL, index, index);
233 }
234 return(0l);
235 break;
236 default :
237 if (IsAlphanumeric((Standard_Character)LOWORD(wParam)))
238 {
0d969553 239 // Place the cursor at the end of text before display
7fd59977 240 index = SendMessage(hWnd, WM_GETTEXTLENGTH, 0l, 0l);
241 SendMessage(hWnd, EM_SETSEL, index, index);
242 CallWindowProc(OldEditProc, hWnd, wMsg, wParam, lParam);
243 return 0l;
244 }
245 break;
246 }
247 break;
248 case WM_KEYDOWN:
249 if (console_semaphore != WAIT_CONSOLE_COMMAND)
250 return 0l;
251 }
252 return CallWindowProc(OldEditProc, hWnd, wMsg, wParam, lParam);
253}
254#endif
255
256