-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
kitty_win.c
532 lines (474 loc) · 15.7 KB
/
kitty_win.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include "kitty_win.h"
// Modifie la transparence
void SetTransparency( HWND hwnd, int value ) {
#ifndef MOD_NOTRANSPARENCY
SetLayeredWindowAttributes( hwnd, 0, value, LWA_ALPHA ) ;
#endif
}
// Numéro de version de l'OS
void GetOSInfo( char * version ) { // ==> Deprecated with version >= Windows 8.1
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
sprintf( version, "%ld.%ld %ld %ld %s %dx%d", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber, osvi.dwPlatformId, osvi.szCSDVersion, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) ) ;
}
/*
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832%28v=vs.85%29.aspx
Operating system Version number
Windows 10 Insider Preview 10.0*
Windows Server Technical Preview 10.0*
Windows Server 2019 10.0*
Windows Server 2016 10.0*
Windows 8.1 6.3*
Windows Server 2012 R2 6.3*
Windows 8 6.2
Windows Server 2012 6.2
Windows 7 6.1
Windows Server 2008 R2 6.1
Windows Server 2008 6.0
Windows Vista 6.0
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 64-Bit Edition 5.2
Windows XP 5.1
Windows 2000 5.0
*/
/*
https://msdn.microsoft.com/en-us/library/aa383745%28v=vs.85%29.aspx#faster_builds_with_smaller_header_files
Minimum system required Minimum value for _WIN32_WINNT and WINVER
Windows 8.1 _WIN32_WINNT_WINBLUE (0x0602)
Windows 8 _WIN32_WINNT_WIN8 (0x0602)
Windows 7 _WIN32_WINNT_WIN7 (0x0601)
Windows Server 2008 _WIN32_WINNT_WS08 (0x0600)
Windows Vista _WIN32_WINNT_VISTA (0x0600)
Windows Server 2003 with SP1, Windows XP with SP2 _WIN32_WINNT_WS03 (0x0502)
Windows Server 2003, Windows XP _WIN32_WINNT_WINXP (0x0501)
Minimum version required Minimum value of _WIN32_IE
Internet Explorer 10.0 _WIN32_IE_IE100 (0x0A00)
Internet Explorer 9.0 _WIN32_IE_IE90 (0x0900)
Internet Explorer 8.0 _WIN32_IE_IE80 (0x0800)
Internet Explorer 7.0 _WIN32_IE_IE70 (0x0700)
Internet Explorer 6.0 SP2 _WIN32_IE_IE60SP2 (0x0603)
Internet Explorer 6.0 SP1 _WIN32_IE_IE60SP1 (0x0601)
Internet Explorer 6.0 _WIN32_IE_IE60 (0x0600)
Internet Explorer 5.5 _WIN32_IE_IE55 (0x0550)
Internet Explorer 5.01 _WIN32_IE_IE501 (0x0501)
Internet Explorer 5.0, 5.0a, 5.0b _WIN32_IE_IE50 (0x0500)
*/
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL IsWow64() {
BOOL bIsWow64 = FALSE;
//IsWow64Process is not available on all supported versions of Windows.
//Use GetModuleHandle to get a handle to the DLL that contains the function
//and GetProcAddress to get a pointer to the function if available.
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if(NULL != fnIsWow64Process) {
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64)) {
//handle error
}
}
return bIsWow64 ;
}
int OpenFileName( HWND hFrame, char * filename, char * Title, char * Filter ) {
char * szTitle = Title ;
char szFilter[256] ; strcpy( szFilter, Filter ) ;
// on remplace les caractères '|' par des caractères NULL.
int i = 0;
while(i < sizeof(szFilter) && szFilter[i] != '\0')
{
if(szFilter[i] == '|')
szFilter[i] = '\0';
i++;
}
// boîte de dialogue de demande d'ouverture de fichier
//char szFileName[_MAX_PATH + 1] = "";
char * szFileName = filename ;
szFileName[0] = '\0' ;
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hFrame;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFileName;
//ofn.nMaxFile = sizeof(szFileName);
ofn.nMaxFile = 4096 ;
ofn.lpstrTitle = szTitle;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY | OFN_LONGNAMES
| OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_EXTENSIONDIFFERENT | OFN_DONTADDTORECENT
;
// si aucun nom de fichier n'a été sélectionné, on abandonne
if(!GetOpenFileName(&ofn)) { return 0 ; }
else { return 1 ; }
}
int SaveFileName( HWND hFrame, char * filename, char * Title, char * Filter ) {
char * szTitle = Title ;
char szFilter[256] ; strcpy( szFilter, Filter ) ;
// on remplace les caractères '|' par des caractères NULL.
int i = 0;
while(i < sizeof(szFilter) && szFilter[i] != '\0')
{
if(szFilter[i] == '|')
szFilter[i] = '\0';
i++;
}
// boîte de dialogue de demande d'ouverture de fichier
//char szFileName[_MAX_PATH + 1] = "";
char * szFileName = filename ;
szFileName[0] = '\0' ;
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hFrame;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFileName;
//ofn.nMaxFile = sizeof(szFileName);
ofn.nMaxFile = 4096 ;
ofn.lpstrTitle = szTitle;
ofn.lpstrDefExt = ".ktx" ;
ofn.Flags = OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT
| OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_EXTENSIONDIFFERENT | OFN_DONTADDTORECENT
;
// si aucun nom de fichier n'a été sélectionné, on abandonne
if(!GetSaveFileName(&ofn)) { return 0 ; }
else { return 1 ; }
}
#include <shlobj.h>
int OpenDirName( HWND hFrame, char * dirname ) {
BROWSEINFO bi ;
ITEMIDLIST *il ;
LPITEMIDLIST ol = NULL ;
char Buffer[4096],Result[4096]="" ;
dirname[0]='\0' ;
strcpy( Buffer, getenv("ProgramFiles") ) ;
//SHGetSpecialFolderLocation( hFrame, CSIDL_MYDOCUMENTS, &ol );
memset(&bi,0,sizeof(BROWSEINFO));
bi.hwndOwner = hFrame ;
//bi.pidlRoot=NULL ; //
bi.pidlRoot=ol ;
bi.pszDisplayName=&Buffer[0];
bi.lpszTitle="Select a folder...";
bi.ulFlags=0;
bi.lpfn=NULL;
if ((il=SHBrowseForFolder(&bi))!=NULL) {
SHGetPathFromIDList(il,&Result[0]) ;
//ILFree( il ) ; ILFree( ol ) ;
GlobalFree(il);GlobalFree(ol);
if( strlen( Result ) == 0 ) return 0 ;
strcpy( dirname, Result ) ;
return 1 ;
}
//ILFree( ol ) ;
GlobalFree(ol);
return 0 ;
}
// Centre un dialog au milieu de la fenetre parent
void CenterDlgInParent(HWND hDlg) {
RECT rcDlg;
HWND hParent;
RECT rcParent;
MONITORINFO mi;
HMONITOR hMonitor;
int xMin, yMin, xMax, yMax, x, y;
GetWindowRect(hDlg,&rcDlg);
hParent = GetParent(hDlg);
GetWindowRect(hParent,&rcParent);
hMonitor = MonitorFromRect(&rcParent,MONITOR_DEFAULTTONEAREST);
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor,&mi);
xMin = mi.rcWork.left;
yMin = mi.rcWork.top;
xMax = (mi.rcWork.right) - (rcDlg.right - rcDlg.left);
yMax = (mi.rcWork.bottom) - (rcDlg.bottom - rcDlg.top);
if ((rcParent.right - rcParent.left) - (rcDlg.right - rcDlg.left) > 20)
x = rcParent.left + (((rcParent.right - rcParent.left) - (rcDlg.right - rcDlg.left)) / 2);
else
x = rcParent.left + 70;
if ((rcParent.bottom - rcParent.top) - (rcDlg.bottom - rcDlg.top) > 20)
y = rcParent.top + (((rcParent.bottom - rcParent.top) - (rcDlg.bottom - rcDlg.top)) / 2);
else
y = rcParent.top + 60;
SetWindowPos(hDlg,NULL,max(xMin,min(xMax,x)),max(yMin,min(yMax,y)),0,0,SWP_NOZORDER|SWP_NOSIZE);
}
//
// Envoi vers l'imprimante
//
// Parametres de l'impression
int PrintCharSize = 100 ;
int PrintMaxLinePerPage = 60 ;
int PrintMaxCharPerLine = 85 ;
int PrintText( const char * Text ) {
int return_code = 0 ;
PRINTDLG pd;
DOCINFO di;
int i, TextLen = 0, Index1 = 0, Index2 = 2;
//int Exit = 0 ;
char* LinePrint = NULL ;
char* szMessage = NULL ;
if( Text == NULL ) return 1 ;
if( strlen( Text ) == 0 ) return 1 ;
memset (&pd, 0, sizeof(PRINTDLG));
memset (&di, 0, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Test";
pd.lStructSize = sizeof(PRINTDLG);
pd.Flags = PD_PAGENUMS | PD_RETURNDC;
pd.nFromPage = 1;
pd.nToPage = 1;
pd.nMinPage = 1;
pd.nMaxPage = 1;
szMessage = 0;
if( PrintDlg( &pd ) ) {
if( pd.hDC ) {
if (StartDoc (pd.hDC, &di) != SP_ERROR) {
TextLen = strlen( Text ) ;
if( TextLen > 0 ) {
LinePrint = (char*) malloc( TextLen + 2 ) ;
Index1 = 0 ; Index2 = 2 ;
//Exit = 0 ;
for( i = 0 ; i < TextLen ; i++ ) {
if( Text[i]=='\r' ) i++;
LinePrint[Index1] = Text[i] ;
if( Text[i] == '\n' ) {
Index2++ ;
LinePrint[Index1] = '\0' ;
TextOut(pd.hDC,100, Index2*PrintCharSize, LinePrint, strlen(LinePrint) ) ;
Index1 = 0 ;
}
else if( Index1>=PrintMaxCharPerLine ) {
Index2++ ;
LinePrint[Index1+1] = '\0' ;
TextOut(pd.hDC,100, Index2*PrintCharSize, LinePrint, strlen(LinePrint) ) ;
Index1 = 0 ;
}
else { Index1++ ; }
if( Index2 >= PrintMaxLinePerPage ) {
EndPage( pd.hDC ) ;
//EndDoc(pd.hDC) ;
//StartDoc(pd.hDC, &di) ;
StartPage( pd.hDC ) ;
Index2 = 2 ;
}
}
Index2++ ;
LinePrint[Index1] = '\0'; // Impression de la dernière page
TextOut(pd.hDC,100, Index2*PrintCharSize, LinePrint, strlen(LinePrint)) ;
EndPage(pd.hDC) ;
EndDoc(pd.hDC) ;
szMessage = "Print successful";
free( LinePrint ) ;
}
else { return_code = 1 ; /* Chaine vide */ }
}
else { // Problème StartDoc
szMessage = "ERROR Type 1" ;
return_code = 2 ;
}
}
else { // Probleme pd.hDC
szMessage = "ERROR Type 2." ;
return_code = 3 ;
}
}
else { // Problème PrintDlg
//szMessage = "Impression annulée par l'utilisateur" ;
return_code = 4 ;
}
if (szMessage) { MessageBox (NULL, szMessage, "Print report", MB_OK) ; }
return return_code ;
}
// Impression du texte dans le bloc-notes
void ManagePrint( HWND hwnd ) {
char *pst = NULL ;
if( OpenClipboard(NULL) ) {
HGLOBAL hglb ;
if( (hglb = GetClipboardData( CF_TEXT ) ) != NULL ) {
if( ( pst = GlobalLock( hglb ) ) != NULL ) {
PrintText( pst ) ;
GlobalUnlock( hglb ) ;
}
}
CloseClipboard();
}
}
// Met un texte dans le press-papier
int SetTextToClipboard( const char * buf ) {
HGLOBAL hglbCopy ;
LPTSTR lptstrCopy ;
if( !IsClipboardFormatAvailable(CF_TEXT) ) return 0 ;
if( !OpenClipboard(NULL) ) return 0 ;
EmptyClipboard() ;
if( (hglbCopy= GlobalAlloc(GMEM_MOVEABLE, (strlen(buf)+1) * sizeof(TCHAR)) ) == NULL ) {
CloseClipboard() ;
return 0 ;
}
lptstrCopy = GlobalLock( hglbCopy ) ;
memcpy( lptstrCopy, buf, (strlen(buf)+1) * sizeof(TCHAR) ) ;
GlobalUnlock( hglbCopy ) ;
if( SetClipboardData(CF_TEXT, hglbCopy) == NULL ) {
CloseClipboard() ;
return 0 ;
}
CloseClipboard() ;
return 1 ;
}
// Execute une commande
void RunCommand( HWND hwnd, const char * cmd ) {
PROCESS_INFORMATION ProcessInformation ;
ZeroMemory( &ProcessInformation, sizeof(ProcessInformation) );
STARTUPINFO StartUpInfo ;
ZeroMemory( &StartUpInfo, sizeof(StartUpInfo) );
StartUpInfo.cb=sizeof(STARTUPINFO);
StartUpInfo.lpReserved=0;
StartUpInfo.lpDesktop=0;
StartUpInfo.lpTitle=0;
StartUpInfo.dwX=0;
StartUpInfo.dwY=0;
StartUpInfo.dwXSize=0;
StartUpInfo.dwYSize=0;
StartUpInfo.dwXCountChars=0;
StartUpInfo.dwYCountChars=0;
StartUpInfo.dwFillAttribute=0;
StartUpInfo.dwFlags=0;
StartUpInfo.wShowWindow=0;
StartUpInfo.cbReserved2=0;
StartUpInfo.lpReserved2=0;
StartUpInfo.hStdInput=0;
StartUpInfo.hStdOutput=0;
StartUpInfo.hStdError=0;
//MessageBox(hwnd,cmd,"Info",MB_OK);
if( !CreateProcess(NULL,(CHAR*)cmd,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&StartUpInfo,&ProcessInformation) ) {
ShellExecute(hwnd, "open", cmd ,0 , 0, SW_SHOWDEFAULT);
} else {
WaitForInputIdle(ProcessInformation.hProcess, INFINITE );
CloseHandle( &StartUpInfo );
CloseHandle( &ProcessInformation );
}
}
void RunPuttyEd( HWND hwnd, char * filename ) {
char buffer[1024]="", shortname[1024]="" ;
if( GetModuleFileName( NULL, (LPTSTR)buffer, 1023 ) ) if( GetShortPathName( buffer, shortname, 1023 ) ) {
strcat( shortname, " -ed" );
if( filename!=NULL ) if( strlen(filename)>0 ) {
strcat( shortname, "b " ) ;
strcat( shortname, filename ) ;
}
debug_logevent( shortname ) ;
RunCommand( hwnd, shortname ) ;
}
}
// Verifie si une mise a jour est disponible sur le site web
extern char BuildVersionTime[256] ;
void CheckVersionFromWebSite( HWND hwnd ) {
char buffer[1024]="", vers[1024]="" ;
int i ;
strcpy( vers, BuildVersionTime ) ;
for( i = 0 ; i < strlen( vers ) ; i ++ ) {
if( !(((vers[i]>='0')&&(vers[i]<='9'))||(vers[i]=='.')) ) { vers[i] = '\0' ; break ; }
}
sprintf( buffer, "http://www.9bis.net/kitty/check_update.php?version=%s", vers ) ;
ShellExecute(hwnd, "open", buffer, 0, 0, SW_SHOWDEFAULT);
}
// Affichage d'un message dans l'event log
void debug_logevent( const char *fmt, ... ) {
va_list ap;
char *buf;
va_start(ap, fmt);
buf = dupvprintf(fmt, ap) ;
va_end(ap);
do_eventlog(buf) ;
free(buf);
}
// Test si un chemin est absolu
bool IsPathAbsolute( const char * path ) {
bool test = false ;
if( path == NULL ) { return false ; }
if( strlen( path ) < 3 ) { return false ; }
if( ((path[0]>='a') && (path[0]<='z')) || ((path[0]>='A') && (path[0]<='Z')) )
if( path[1]==':' )
if( (path[2]=='/') || (path[2]=='\\') ) test = true ;
return test ;
}
void PopUpSystemMenu( HWND hwnd, int npos ) {
RECT rc ;
GetWindowRect( hwnd, &rc ) ;
HMENU m = GetSystemMenu( hwnd, FALSE) ;
TrackPopupMenu( m, 0, rc.left, rc.top, 0, hwnd, NULL) ;
if( npos>0 ) {
int nb = GetMenuItemCount(m), i;
MENUITEMINFO mi ;
mi.cbSize = sizeof(MENUITEMINFO) ;
for( i=0; i<nb; i++ ) {
mi.dwTypeData = NULL ;
GetMenuItemInfoA( m, i, TRUE, &mi);
char *txt = (char*)malloc(mi.cch+1);
mi.dwTypeData = txt ;
mi.cch= mi.cch+1;
GetMenuItemInfoA( m, i, FALSE, &mi);
MessageBox(NULL,txt,"info",MB_OK);
free(txt);
}
}
}
// Description:
// Creates a tooltip for an item in a dialog box.
// Parameters:
// idTool - identifier of an dialog box item.
// nDlg - window handle of the dialog box.
// pszText - string to use as the tooltip text.
// Returns:
// The handle to the tooltip.
//
HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText)
{
if (!toolID || !hDlg || !pszText)
{
return FALSE;
}
// Get the window of the tool.
HWND hwndTool = GetDlgItem(hDlg, toolID);
// Create the tooltip. g_hInst is the global instance handle.
HWND hwndTip = CreateWindowEx((DWORD)NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP |TTS_ALWAYSTIP | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hDlg, NULL,
hinst /*g_hInst*/, NULL);
if (!hwndTool || !hwndTip)
{
return (HWND)NULL;
}
// Associate the tooltip with the tool.
TOOLINFO toolInfo = { 0 };
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hDlg;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hwndTool;
toolInfo.lpszText = pszText;
SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
return hwndTip;
}
/*
HWND CreateToolTip2(int toolID, HWND hDlg, PTSTR pszText) {
HWND hwndToolTips = CreateWindow(TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_BALLOON,
0, 0, 0, 0, NULL, NULL, GetModuleHandle(NULL), NULL);
if (hwndToolTips)
{
TOOLINFO ti;
ti.cbSize = sizeof(ti);
ti.uFlags = TTF_TRANSPARENT | TTF_CENTERTIP;
ti.hwnd = hDlg;
ti.uId = toolID;
ti.hinst = NULL;
ti.lpszText = pszText;
GetClientRect(hwnd, &ti.rect);
SendMessage(hwndToolTips, TTM_ADDTOOL, 0, (LPARAM) &ti );
}
return hwndToolTips ;
}
*/