WinConsole.c

Go to the documentation of this file.
00001 /* Allan CORNET */
00002 /* INRIA 2005 */
00003 
00004 #include "WinConsole.h"
00005 #include "version.h"
00006 #include "Messages.h"
00007 #include "Warnings.h"
00008 #include "Errors.h"
00009 /*-----------------------------------------------------------------------------------*/
00010 static CONSOLE_SCREEN_BUFFER_INFO csbiInfoSave;
00011 char ScilexConsoleName[MAX_PATH];
00012 static BOOL WindowMode;
00013 static int Windows_Console_State;/* 0 Hide 1 Show */
00014 /*-----------------------------------------------------------------------------------*/
00015 typedef  UINT (WINAPI * GetWindowModuleFileNamePROC) (HWND,LPTSTR,UINT);
00016 /*-----------------------------------------------------------------------------------*/
00017 void RenameConsole(void)
00018 {
00019         HMENU hmenuConsole = NULL;
00020         char CurrentConsoleName[MAX_PATH];
00021         char CurrentConsoleNameTmp[MAX_PATH];
00022 
00023         GetConsoleTitle(CurrentConsoleName,MAX_PATH);
00024         strncpy(CurrentConsoleNameTmp,CurrentConsoleName,strlen(NameConsole));
00025         CurrentConsoleNameTmp[strlen(NameConsole)]='\0';
00026 
00027         if ( strcmp(CurrentConsoleNameTmp,NameConsole) != 0)     
00028         {
00029                 wsprintf(ScilexConsoleName,"%s %s",NameConsole,SCI_VERSION_STRING);
00030                 SetConsoleTitle(ScilexConsoleName);
00031         }
00032 
00033         // Desactive croix dans la console
00034         hmenuConsole=GetSystemMenu(GetConsoleWindow(), FALSE);
00035         DeleteMenu(hmenuConsole, SC_CLOSE, MF_BYCOMMAND);
00036 
00037 }
00038 /*-----------------------------------------------------------------------------------*/
00039 void CreateScilabConsole(int ShowBanner)
00040 {
00041         HMENU hmenuConsole = NULL;
00042         int Current_Number_of_Scilex=-1; 
00043 
00044         Windows_Console_State=0; /* Console DOS Cachée par défaut */
00045         AllocConsole();
00046 
00047         Current_Number_of_Scilex=FindFreeScilexNumber();
00048         wsprintf(ScilexConsoleName,"%s %s (%d)",NameConsole,SCI_VERSION_STRING,Current_Number_of_Scilex);
00049         SetConsoleTitle(ScilexConsoleName);
00050 
00051         CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CONSOLE_TEXTMODE_BUFFER,NULL);
00052     freopen("CONOUT$", "wb", stdout); /* redirect stdout --> CONOUT$*/
00053         freopen("CONOUT$", "wb", stderr); /* redirect stderr --> CONOUT$*/
00054         
00055         if (ShowBanner)
00056         {
00057                 char line[80];
00058 
00059                 strcpy(line,"        ___________________________________________\r\n");
00060                 printf(line);
00061             wsprintf(line,"                         %s\r\n\n",SCI_VERSION_STRING);
00062                 printf(line);
00063                 strcpy(line,"                  Copyright (c) 1989-2007          \r\n");
00064                 printf(line);
00065                 strcpy(line,"              Consortium Scilab (INRIA, ENPC)      \r\n");
00066                 printf(line);
00067         strcpy(line,"        ___________________________________________\r\n\r\n");
00068                 printf(line);
00069         }
00070 
00071         // Desactive croix dans la console
00072         hmenuConsole=GetSystemMenu(GetConsoleWindow(), FALSE);
00073         DeleteMenu(hmenuConsole, SC_CLOSE, MF_BYCOMMAND);
00074 
00075         HideScilex(); /* Cache la fenetre Console */
00076 }
00077 /*-----------------------------------------------------------------------------------*/
00078 void CloseScilabConsole(void)
00079 {
00080         fclose(stdout);
00081         fclose(stderr);
00082         FreeConsole();
00083 }
00084 /*-----------------------------------------------------------------------------------*/
00085 void UpdateConsoleColors(void)
00086 {
00087           HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
00088           COORD Coord;
00089           DWORD cWritten;
00090 
00091           Coord.X=0;
00092           Coord.Y=0;
00093          
00094           FillConsoleOutputAttribute( hConsole,
00095                                                                   BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED|BACKGROUND_INTENSITY,
00096                                                                   csbiInfoSave.dwSize.X*csbiInfoSave.dwSize.Y,
00097                                                                   Coord,
00098                                                                   &cWritten); 
00099 
00100 
00101           SetConsoleTextAttribute(hConsole,BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED|BACKGROUND_INTENSITY);
00102  
00103 }
00104 /*-----------------------------------------------------------------------------------*/
00105 void SaveConsoleColors(void)
00106 {
00107         GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfoSave); 
00108 }
00109 /*-----------------------------------------------------------------------------------*/
00110 void RestoreConsoleColors(void)
00111 {
00112         HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
00113         COORD Coord;
00114         DWORD cWritten;
00115 
00116         Coord.X=0;
00117         Coord.Y=0;
00118 
00119         FillConsoleOutputAttribute( hConsole,
00120                                                                 csbiInfoSave.wAttributes,
00121                                                                 csbiInfoSave.dwSize.X*csbiInfoSave.dwSize.Y,
00122                                                                 Coord,
00123                                                                 &cWritten);
00124         SetConsoleTextAttribute(hConsole,csbiInfoSave.wAttributes);
00125 }
00126 /*-----------------------------------------------------------------------------------*/
00127 BOOL IsWindowInterface(void)
00128 {
00129         return WindowMode;
00130 }
00131 /*-----------------------------------------------------------------------------------*/
00132 int IsConsoleMode(void)
00133 {
00134         return (int)(!WindowMode);
00135 }
00136 /*-----------------------------------------------------------------------------------*/
00137 /* Retourne un numéro valide pour nommer les fenetres associées à ce process */
00138 int FindFreeScilexNumber(void)
00139 {
00140         HWND hScilexN=NULL;
00141         int Number_of_Scilex=0;
00142         char NameScilex[MAX_PATH];
00143 
00144         wsprintf(NameScilex,"%s (%d)",SCI_VERSION_STRING,Number_of_Scilex);
00145         hScilexN=FindWindow(NULL,NameScilex);
00146         while ( hScilexN )
00147         {
00148                 UINT NbChar=0;
00149                 char FileName[MAX_PATH+1];
00150                 strcpy(FileName,"");
00151 
00152                 NbChar=GetWindowModuleFileName(hScilexN,FileName,MAX_PATH);
00153                 
00154                 if (NbChar>0)
00155                 {
00156                         Number_of_Scilex++;
00157                         wsprintf(NameScilex,"%s (%d)",SCI_VERSION_STRING,Number_of_Scilex);
00158                         hScilexN=FindWindow(NULL,NameScilex);
00159                 }
00160                 else
00161                 {
00162                         hScilexN=NULL;
00163                 }
00164         }
00165 
00166         return Number_of_Scilex;
00167 }
00168 /*-----------------------------------------------------------------------------------*/
00169 /*Cache la fenetre Scilex(x) de ce processus */
00170 void HideScilex(void)
00171 {
00172         HWND hScilex=NULL;
00173         hScilex=GetConsoleWindow();
00174         if (hScilex)
00175         {
00176                 ShowWindow(hScilex,SW_HIDE);    
00177         }
00178 }
00179 /*-----------------------------------------------------------------------------------*/
00180 /*Montre la fenetre Scilex(x) de ce processus */
00181 void ShowScilex(void)
00182 {
00183   HWND hScilex=NULL;
00184   hScilex=GetConsoleWindow();
00185   if (hScilex)
00186   {
00187         ShowWindow(hScilex,SW_SHOWNOACTIVATE);
00188   }
00189 }
00190 /*-----------------------------------------------------------------------------------*/
00191 void SwitchConsole(void)
00192 {
00193         switch (GetConsoleState())
00194         {
00195                 /* La fenetre etait cachée , on la restaure */
00196                 case 0:
00197                 {
00198                         ShowScilex();
00199                         SetConsoleState(1);
00200                         SetConsoleTitle(ScilexConsoleName);
00201                 }
00202                 break;
00203                 /* La fenetre etait apparente , on la cache */ 
00204                 case 1:
00205                 {
00206                         HideScilex();
00207                         SetConsoleState(0);
00208                         SetConsoleTitle(ScilexConsoleName);
00209                 }
00210                 break;
00211         }
00212 }
00213 /*-----------------------------------------------------------------------------------*/
00214 void SetWindowMode(BOOL ON)
00215 {
00216           WindowMode=ON;
00217 }
00218 /*-----------------------------------------------------------------------------------*/
00219 int GetConsoleState(void)
00220 {
00221         return Windows_Console_State;
00222 }
00223 /*-----------------------------------------------------------------------------------*/
00224 void SetConsoleState(int state)
00225 {
00226         Windows_Console_State=state;
00227 }
00228 /*-----------------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:03:54 2007 for Scilab [trunk] by  doxygen 1.5.1