52f2f91a136af02e8e0d726404690f730a623ac9
[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-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifdef _WIN32
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
37 #define CLIENTWND 0
38
39 #define PROMPT "Command >> "
40 #define COMMANDSIZE 1000 // Max nb of characters for a command
41
42
43 // Definition of global variables
44 #ifdef STRICT
45   WNDPROC OldEditProc;  // Save the standard procedure of the edition (sub-class)
46 #else
47   FARPROC OldEditProc;
48 #endif
49
50 /*--------------------------------------------------------*\
51 |  CREATE COMMAND WINDOW PROCEDURE
52 \*--------------------------------------------------------*/
53 HWND CreateCommandWindow(HWND hWnd, int /*nitem*/)
54 {
55   HINSTANCE       hInstance;
56   hInstance = (HINSTANCE)GetWindowLongPtr(hWnd,GWLP_HINSTANCE);
57
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 \*--------------------------------------------------------*/
73 LRESULT APIENTRY CommandProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
74 {
75   HWND hWndEdit;
76   MINMAXINFO* lpmmi;
77
78   switch(wMsg)
79   {
80     case WM_CREATE :
81                                         CommandCreateProc(hWnd);
82                                         hWndEdit = (HWND)GetWindowLongPtr(hWnd, CLIENTWND);
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 :
93     {
94                         hWndEdit = (HWND)GetWindowLongPtr(hWnd, CLIENTWND);          
95                         MoveWindow(hWndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
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);
99           SendMessage(hWnd, EM_SETSEL, index, index); 
100                         break;
101     }
102
103     case WM_SETFOCUS :
104                         hWndEdit = (HWND)GetWindowLongPtr(hWnd, CLIENTWND);
105           SetFocus(hWndEdit);
106           break;
107
108     default :
109                                         return(DefWindowProc(hWnd, wMsg, wParam, lParam));
110   }
111   return(0l);
112 }
113
114
115
116 LRESULT APIENTRY EditProc(HWND, UINT, WPARAM, LPARAM);
117 /*--------------------------------------------------------*\
118 |  COMMAND CREATE PROCEDURE
119 \*--------------------------------------------------------*/
120 BOOL CommandCreateProc(HWND hWnd)
121 {
122
123   HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
124
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
132     // Save hWndEdit in the extra memory in 0 of CommandWindow
133   if (hWndEdit)
134     SetWindowLongPtr(hWnd, CLIENTWND, (LONG_PTR)hWndEdit);
135
136     // Sub-Class of the window
137     //-------
138     // Save the pointer on the existing procedure
139   #ifdef STRICT
140     OldEditProc = (WNDPROC)GetWindowLongPtr(hWndEdit, GWLP_WNDPROC);
141   #else
142     OldEditProc = (FARPROC)GetWindowLongPtr(hWndEdit, GWLP_WNDPROC);
143   #endif
144     // Implement the new function
145   SetWindowLongPtr(hWndEdit, GWLP_WNDPROC, (LONG_PTR) EditProc);
146
147   return(TRUE);
148 }
149
150
151 /*--------------------------------------------------------*\
152 |  GET COMMAND
153 |    
154 \*--------------------------------------------------------*/
155 int GetCommand(HWND hWnd, char* buffer)
156 {
157   int again = 1;
158   char temp[COMMANDSIZE]="";
159
160   int nbLine = (int )SendMessage(hWnd, EM_GETLINECOUNT, 0l, 0l);
161   
162   int nbChar = 0;
163   buffer[0]='\0';
164   while ( again && nbLine > -1 && nbChar < COMMANDSIZE-1)
165     {
166       strcat(buffer, strrev(temp));
167       // Initialization of the 1st WORD to the nb of characters to read 
168       WORD* nbMaxChar = (WORD*)temp;
169       *nbMaxChar = COMMANDSIZE-1;
170       
171       int nbCharRead = (int )SendMessage(hWnd, EM_GETLINE, nbLine-1, (LPARAM)temp);
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
186 extern console_semaphore_value volatile console_semaphore;
187 extern char console_command[1000];
188
189 /*--------------------------------------------------------*\
190 |  EDIT WINDOW PROCEDURE
191 \*--------------------------------------------------------*/
192 LRESULT APIENTRY EditProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
193 {
194   char buffer[COMMANDSIZE];     
195         POINT pos;
196         BOOL rep = 0;
197         static LRESULT nbline; // Process the buffer of the edit window 
198   LRESULT index;
199
200   switch(wMsg)
201   {
202   case WM_CHAR :
203     if (console_semaphore != WAIT_CONSOLE_COMMAND)
204       return 0l;
205                         switch(LOWORD(wParam))
206                         {
207               // Overload of character \n
208                         case 0x0d :           
209                                                                         GetCommand(hWnd, buffer);                                                                       
210                                   // Standard processing
211                                       CallWindowProc(OldEditProc, hWnd, wMsg, wParam, lParam);
212                                         // Display of PROMPT
213                                                                         rep = GetCaretPos(&pos);
214                                 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)PROMPT);                                                    
215                     // Display the command in the console
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;
221                                                                           // Purge the buffer
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);
230                         // Place the cursor at the end of text
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                   {
239                       // Place the cursor at the end of text before display
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