update banners
[scilab.git] / scilab / modules / windows_tools / src / c / scilab_windows / console.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) INRIA - Allan CORNET
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15
16 /*--------------------------------------------------------------------------*/
17
18 #define  _WIN32_WINNT 0x0500 // GetConsoleWindow
19 #include <Windows.h>
20 #include <shlwapi.h>
21 #include <stdio.h>
22 #pragma comment(lib, "shlwapi.lib")
23
24 #include "WinConsole.h"
25 #include "WndThread.h"
26 #include "console.h"
27 #include "version.h"
28 #include "os_string.h"
29 /*--------------------------------------------------------------------------*/
30 #define NameConsole "Console"
31 /*--------------------------------------------------------------------------*/
32 static CONSOLE_SCREEN_BUFFER_INFO csbiInfoSave;
33 static char ScilexConsoleName[MAX_PATH];
34 /*--------------------------------------------------------------------------*/
35 void UpdateConsoleColors(void)
36 {
37     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
38     COORD Coord;
39     DWORD cWritten;
40
41     Coord.X = 0;
42     Coord.Y = 0;
43
44     FillConsoleOutputAttribute( hConsole,
45                                 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY,
46                                 csbiInfoSave.dwSize.X * csbiInfoSave.dwSize.Y,
47                                 Coord,
48                                 &cWritten);
49
50     SetConsoleTextAttribute(hConsole, BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
51
52 }
53 /*--------------------------------------------------------------------------*/
54 void SaveConsoleColors(void)
55 {
56     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfoSave);
57 }
58 /*--------------------------------------------------------------------------*/
59 void RestoreConsoleColors(void)
60 {
61     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
62     COORD Coord;
63     DWORD cWritten;
64
65     Coord.X = 0;
66     Coord.Y = 0;
67
68     FillConsoleOutputAttribute( hConsole,
69                                 csbiInfoSave.wAttributes,
70                                 csbiInfoSave.dwSize.X * csbiInfoSave.dwSize.Y,
71                                 Coord,
72                                 &cWritten);
73     SetConsoleTextAttribute(hConsole, csbiInfoSave.wAttributes);
74 }
75 /*--------------------------------------------------------------------------*/
76 void RenameConsole(void)
77 {
78     HWND hScilex = NULL;
79     char CurrentConsoleName[MAX_PATH];
80     char CurrentConsoleNameTmp[MAX_PATH];
81
82     GetConsoleTitle(CurrentConsoleName, MAX_PATH);
83     strncpy(CurrentConsoleNameTmp, CurrentConsoleName, strlen(NameConsole));
84     CurrentConsoleNameTmp[strlen(NameConsole)] = '\0';
85
86     if ( strcmp(CurrentConsoleNameTmp, NameConsole) != 0)
87     {
88         wsprintf(ScilexConsoleName, "%s %s", NameConsole, SCI_VERSION_STRING);
89         SetConsoleTitle(ScilexConsoleName);
90     }
91
92     hScilex = GetConsoleWindow();
93     if (hScilex)
94     {
95         HMENU hmenuConsole = NULL;
96         // Desactive croix dans la console
97         hmenuConsole = GetSystemMenu(hScilex, FALSE);
98         DeleteMenu(hmenuConsole, SC_CLOSE, MF_BYCOMMAND);
99     }
100 }
101 /*--------------------------------------------------------------------------*/
102 void RestoreExitButton(void)
103 {
104     HWND hScilex = NULL;
105     hScilex = GetConsoleWindow();
106     if (hScilex)
107     {
108         HMENU hmenuConsole = NULL;
109         // Active croix dans la console
110         hmenuConsole = GetSystemMenu(hScilex, FALSE);
111         AppendMenu( hmenuConsole, MF_BYCOMMAND, SC_CLOSE, "&Close Alt+F4" );
112     }
113 }
114 /*--------------------------------------------------------------------------*/
115 void CreateScilabConsole(int ShowBanner)
116 {
117     HWND hScilex = NULL;
118
119     SetConsoleState(0);  /* Console DOS Cachée par défaut */
120     AllocConsole();
121
122     wsprintf(ScilexConsoleName, "%s %s (%d)", NameConsole, SCI_VERSION_STRING, getCurrentScilabId());
123     SetConsoleTitle(ScilexConsoleName);
124
125     CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
126     freopen("CONOUT$", "wb", stdout); /* redirect stdout --> CONOUT$*/
127     freopen("CONOUT$", "wb", stderr); /* redirect stderr --> CONOUT$*/
128
129     if (ShowBanner)
130     {
131         char line[80];
132
133         strcpy(line, "        ___________________________________________\n");
134         printf(line);
135         wsprintf(line, "                         %s\n\n", SCI_VERSION_STRING);
136         printf(line);
137         strcpy(line, "                     ESI Group\n");
138         printf(line);
139         strcpy(line, "         Copyright (c) 2017-2018 (ESI Grroup)\n");
140         printf(line);
141         strcpy(line, "         Copyright (c) 2011-2017 (Scilab Enterprises)\n");
142         printf(line);
143         strcpy(line, "               Copyright (c) 1989-2012 (INRIA)\n");
144         printf(line);
145         strcpy(line, "               Copyright (c) 1989-2007 (ENPC)\n");
146         printf(line);
147         strcpy(line, "        ___________________________________________\n\n");
148         printf(line);
149     }
150
151     hScilex = GetConsoleWindow();
152     if (hScilex)
153     {
154         HMENU hmenuConsole = NULL;
155         // Desactive croix dans la console
156         hmenuConsole = GetSystemMenu(hScilex, FALSE);
157         DeleteMenu(hmenuConsole, SC_CLOSE, MF_BYCOMMAND);
158
159         /* Cache la fenetre Console */
160         ShowWindow(hScilex, SW_HIDE);
161     }
162 }
163 /*--------------------------------------------------------------------------*/
164 void CloseScilabConsole(void)
165 {
166     fclose(stdout);
167     fclose(stderr);
168     FreeConsole();
169 }
170 /*--------------------------------------------------------------------------*/
171 char *getScilexConsoleName(void)
172 {
173     char *retName = NULL;
174
175     if (strlen(ScilexConsoleName) > 0)
176     {
177         retName = os_strdup(ScilexConsoleName);
178     }
179     return retName;
180 }
181 /*--------------------------------------------------------------------------*/
182 int getXConsoleScreenSize(void)
183 {
184     return (csbiInfoSave.srWindow.Right - csbiInfoSave.srWindow.Left);
185 }
186 /*--------------------------------------------------------------------------*/
187 int getYConsoleScreenSize(void)
188 {
189     return (csbiInfoSave.srWindow.Bottom - csbiInfoSave.srWindow.Top);
190 }
191 /*--------------------------------------------------------------------------*/