2de0fbc69ce9c42953b29fccd6ca05c63bf4144d
[occt.git] / src / Draw / CommandWindow.cxx
1 // Created on: 1998-08-06
2 // Created by: Administrateur Atelier MDL
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23
24 #ifdef WNT
25 #include <windows.h>
26
27 #define COMMANDCLASS "COMMANDWINDOW"
28 #define COMMANDTITLE "Command Window"
29
30 #include <CommandWindow.h>
31 #include <Draw_Window.hxx>
32 #include <MainWindow.h>
33 #include <Draw_Appli.hxx>
34
35
36
37 /****************************************************\
38 *  CommandWindow.cxx :
39 *
40 \****************************************************/
41
42
43
44 #define CLIENTWND 0
45
46 #define PROMPT "Command >> "
47 #define COMMANDSIZE 1000 // Max nb of characters for a command
48
49
50 // Definition of global variables
51 #ifdef STRICT
52   WNDPROC OldEditProc;  // Save the standard procedure of the edition (sub-class)
53 #else
54   FARPROC OldEditProc;
55 #endif
56
57 /*--------------------------------------------------------*\
58 |  CREATE COMMAND WINDOW PROCEDURE
59 \*--------------------------------------------------------*/
60 HWND CreateCommandWindow(HWND hWnd, int nitem)
61 {
62   HINSTANCE       hInstance;
63   
64 #ifndef _WIN64
65   hInstance = (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
66 #else
67   hInstance = (HINSTANCE)GetWindowLong(hWnd,GWLP_HINSTANCE);
68 #endif
69         HWND hWndCommand = (CreateWindow(COMMANDCLASS, COMMANDTITLE,
70                                                                                                                         WS_CLIPCHILDREN | WS_OVERLAPPED |
71                                                                                                                         WS_THICKFRAME | WS_CAPTION      ,
72                                                                                                                         0, 0,
73                                                                                                                         400, 100,
74                                                                                                                         hWnd, NULL, hInstance, NULL));
75
76         ShowWindow(hWndCommand, SW_SHOW);       
77         return hWndCommand;
78 }
79
80
81 /*--------------------------------------------------------*\
82 |  COMMAND WINDOW PROCEDURE
83 \*--------------------------------------------------------*/
84 LONG APIENTRY CommandProc(HWND hWnd, UINT wMsg, WPARAM wParam, LONG lParam )
85 {
86   HWND hWndEdit;
87   int index; // Nb of characters in the buffer of hWndEdit
88   MINMAXINFO* lpmmi;
89
90   switch(wMsg)
91   {
92     case WM_CREATE :
93                                         CommandCreateProc(hWnd);
94                                         hWndEdit = (HWND)GetWindowLong(hWnd, CLIENTWND);
95                                         SendMessage(hWndEdit,EM_REPLACESEL, 0,(LPARAM)PROMPT);
96                                         break;
97
98     case WM_GETMINMAXINFO :
99           lpmmi = (LPMINMAXINFO)lParam;
100           lpmmi->ptMinTrackSize.x = 200;
101           lpmmi->ptMinTrackSize.y = 50;           
102           break;
103
104     case WM_SIZE :
105                         hWndEdit = (HWND)GetWindowLong(hWnd, CLIENTWND);          
106                         MoveWindow(hWndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
107             // Place the cursor at the end of the buffer
108           index =  SendMessage(hWnd, WM_GETTEXTLENGTH, 0l, 0l);
109           SendMessage(hWnd, EM_SETSEL, index, index); 
110                         break;
111
112     case WM_SETFOCUS :
113                         hWndEdit = (HWND)GetWindowLong(hWnd, CLIENTWND);
114           SetFocus(hWndEdit);
115           break;
116
117     default :
118                                         return(DefWindowProc(hWnd, wMsg, wParam, lParam));
119   }
120   return(0l);
121 }
122
123
124
125 LONG APIENTRY EditProc(HWND, UINT, WPARAM, LONG);
126 /*--------------------------------------------------------*\
127 |  COMMAND CREATE PROCEDURE
128 \*--------------------------------------------------------*/
129 BOOL CommandCreateProc(HWND hWnd)
130 {
131
132 #ifndef _WIN64
133   HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
134 #else
135   HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWLP_HINSTANCE);
136 #endif
137   HWND hWndEdit = CreateWindow("EDIT",NULL,
138                           WS_CHILD | WS_VISIBLE | WS_VSCROLL |
139                           ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
140                           0, 0, 0, 0,
141                           hWnd, 0,
142                           hInstance, NULL);
143
144     // Save hWndEdit in the extra memory in 0 of CommandWindow
145   if (hWndEdit)
146     SetWindowLong(hWnd, CLIENTWND, (LONG)hWndEdit);
147
148     // Sub-Class of the window
149     //-------
150     // Save the pointer on the existing procedure
151   #ifdef STRICT
152    #ifndef _WIN64
153     OldEditProc = (WNDPROC)GetWindowLong(hWndEdit, GWL_WNDPROC);
154    #else
155     OldEditProc = (WNDPROC)GetWindowLong(hWndEdit, GWLP_WNDPROC);
156    #endif // _WIN64
157   #else
158     OldEditProc = (FARPROC)GetWindowLong(hWndEdit, GWL_WNDPROC);
159   #endif
160     // Implement the new function
161 #ifndef _WIN64
162   SetWindowLong(hWndEdit, GWL_WNDPROC, (LONG) EditProc);
163 #else
164   SetWindowLong(hWndEdit, GWLP_WNDPROC, (LONG) EditProc);
165 #endif
166   return(TRUE);
167 }
168
169
170 /*--------------------------------------------------------*\
171 |  GET COMMAND
172 |    
173 \*--------------------------------------------------------*/
174 int GetCommand(HWND hWnd, char* buffer)
175 {
176   int again = 1;
177   char temp[COMMANDSIZE]="";
178
179   int nbLine = SendMessage(hWnd, EM_GETLINECOUNT, 0l, 0l);
180   
181   int nbChar = 0;
182   buffer[0]='\0';
183   while ( again && nbLine > -1 && nbChar < COMMANDSIZE-1)
184     {
185       strcat(buffer, strrev(temp));
186       // Initialization of the 1st WORD to the nb of characters to read 
187       WORD* nbMaxChar = (WORD*)temp;
188       *nbMaxChar = COMMANDSIZE-1;
189       
190       int nbCharRead = SendMessage(hWnd, EM_GETLINE, nbLine-1, (LPARAM)temp);
191       nbChar += nbCharRead ;
192       int cmp = strncmp(temp, PROMPT, 10);
193       temp[nbCharRead]='\0';
194       if( cmp == 0 )
195         {
196           strcat(buffer, strrev(temp));
197           again = 0;
198         }
199       nbLine -= 1;
200     }   
201   strrev(buffer);
202   return nbChar;
203 }
204
205 extern console_semaphore_value volatile console_semaphore;
206 extern char console_command[1000];
207
208 /*--------------------------------------------------------*\
209 |  EDIT WINDOW PROCEDURE
210 \*--------------------------------------------------------*/
211 LONG APIENTRY EditProc(HWND hWnd, UINT wMsg, WPARAM wParam, LONG lParam )
212 {
213   char buffer[COMMANDSIZE];     
214         POINT pos;
215         BOOL rep;
216         static int nbline; // Process the buffer of the edit window 
217   int index;
218     
219   switch(wMsg)
220   {
221   case WM_CHAR :
222     if (console_semaphore != WAIT_CONSOLE_COMMAND)
223       return 0l;
224                         switch(LOWORD(wParam))
225                         {
226               // Overload of character \n
227                         case 0x0d :           
228                                                                         GetCommand(hWnd, buffer);                                                                       
229                                   // Standard processing
230                                       CallWindowProc(OldEditProc, hWnd, wMsg, wParam, lParam);
231                                         // Display of PROMPT
232                                                                         rep = GetCaretPos(&pos);
233                                 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)PROMPT);                                                    
234                     // Display the command in the console
235                   cout << buffer << endl; 
236                                                                         /*if (Draw_Interprete(buffer+strlen(PROMPT))== -2)
237                                                                             DestroyProc(hWnd); */ 
238                                                                         strcpy(console_command, buffer+strlen(PROMPT));
239                                                                         console_semaphore = HAS_CONSOLE_COMMAND;
240                                                                           // Purge the buffer
241                   nbline = SendMessage(hWnd, EM_GETLINECOUNT, 0l, 0l);
242                                                                         if(nbline > 200)
243                                                                         {
244                       nbline = 0;
245                                                                                         GetCommand(hWnd, buffer);
246                       index = SendMessage(hWnd, EM_LINEINDEX, 100, 0);
247                                                                                         SendMessage(hWnd, EM_SETSEL, 0, index);                 
248                                                                                         SendMessage(hWnd, WM_CUT, 0, 0);
249                         // Place the cursor at the end of text
250                       index =  SendMessage(hWnd, WM_GETTEXTLENGTH, 0l, 0l);
251                       SendMessage(hWnd, EM_SETSEL, index, index);                      
252                                                                         }
253                                       return(0l);
254                                 break;
255                 default :
256                   if (IsAlphanumeric((Standard_Character)LOWORD(wParam)))
257                   {
258                       // Place the cursor at the end of text before display
259                     index =  SendMessage(hWnd, WM_GETTEXTLENGTH, 0l, 0l);
260                     SendMessage(hWnd, EM_SETSEL, index, index);
261                     CallWindowProc(OldEditProc, hWnd, wMsg, wParam, lParam);                    
262                     return 0l;
263                   }                  
264                   break;
265                         }       
266                         break;
267   case WM_KEYDOWN:
268     if (console_semaphore != WAIT_CONSOLE_COMMAND) 
269       return 0l;                                                                        
270   }
271   return CallWindowProc(OldEditProc, hWnd, wMsg, wParam, lParam);
272 }
273 #endif
274
275