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