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 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
|
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#ifndef NDEBUG
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
// to disable otherwise well-lodged compiler warning
#pragma warning(disable: 4201) // nameless struct/union
#include <winioctl.h>
#include <conio.h>
#include "osapi/osapi.h"
#include "osapi/outwnd.h"
#include "osapi/monopub.h"
#include "graphics/2d.h"
#include "freespace2/freespaceresource.h"
#include "globalincs/systemvars.h"
#include "cfile/cfilesystem.h"
#include "globalincs/globals.h"
#include "parse/parselo.h"
extern int Cmdline_debug_window;
#define SCROLL_BUFFER_SIZE 512
#define MAX_LINE_WIDTH 128
#define TASKBAR_HEIGHT 30
#define ID_COPY 32000
#define ID_FIND 32010
#define ID_FILTER 32100
#define TIMER1 1
#define UP_FAST 65537
#define DOWN_FAST 65538
HANDLE hOutputThread=NULL;
DWORD OutputThreadID;
HWND hOutputWnd;
char szOutputClass[] = "OutputWindow";
char spaces[MAX_LINE_WIDTH + 1];
ubyte outwnd_filter_loaded = 0;
char find_text[85];
struct outwnd_filter_struct {
char name[NAME_LENGTH];
bool enabled;
outwnd_filter_struct( )
: enabled( false )
{
name[ 0 ] = 0;
}
};
SCP_vector<outwnd_filter_struct> OutwndFilter;
int mprintf_last_line = -1;
char outtext[SCROLL_BUFFER_SIZE][MAX_LINE_WIDTH];
int old_scroll_pos = -32768;
int nTextHeight=0, nTextWidth=0, nCharRows=0;
bool outwnd_inited = false;
bool outwnd_disabled = true;
int max_scroll_pos = SCROLL_BUFFER_SIZE - 1;
static int marked = 0;
int marked_top, marked_bottom, marked_left, marked_right;
int old_marked = 0, old_marked_top, old_marked_bottom, old_marked_left, old_marked_right;
int marking_started_x, marking_started_y, client_top_yoffset, cur_line_index, marking_active = 0;
int scroll_wait = 1;
BOOL OutputActive = FALSE;
LPARAM last_mousemove;
int Outwnd_changed = 0;
int find_line = -1, find_pos;
// monochrome screen info
#define NMONO
HANDLE mono_driver=NULL; // handle to the monochrome driver
void outwnd_print(const char *id = NULL, const char *temp = NULL);
BOOL CALLBACK find_dlg_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam);
void find_text_in_outwindow(int n, int p);
void outwnd_copy_marked_selection(HWND hwnd);
void outwnd_update_marking(LPARAM l_parm, HWND hwnd);
void outwnd_paint(HWND hwnd);
ubyte Outwnd_no_filter_file = 0; // 0 = .cfg file found, 1 = not found and warning not printed yet, 2 = not found and warning printed
// used for file logging
int Log_debug_output_to_file = 1;
FILE *Log_fp = NULL;
char *FreeSpace_logfilename = NULL;
SCP_string safe_string;
#ifndef _MSC_VER
inline void _outp(unsigned short port, unsigned char value)
{
asm(
"mov %0,%%al;"
"mov %1,%%dx;"
"out %%al,%%dx;"
: : "g" (value), "g" (port) : "al", "dx");
}
inline unsigned char _inp(unsigned short port)
{
unsigned char value;
asm(
"mov %1,%%dx;"
"in %%dx,%%al;"
"mov %%al,%0"
: "=g"(value) : "g" (port) : "al", "dx");
return value;
}
#endif
inline void text_hilight(HDC &hdc)
{
SetBkColor(hdc, RGB(0, 0, 0));
SetTextColor(hdc, RGB(255, 255, 255));
}
inline void text_normal(HDC &hdc)
{
SetBkColor(hdc, RGB(255, 255, 255));
SetTextColor(hdc, RGB(0, 0, 0));
}
inline void fix_marking_coords(int &x, int &y, LPARAM l_parm)
{
x = (signed short) LOWORD(l_parm) / nTextWidth;
y = ((signed short) HIWORD(l_parm) - client_top_yoffset) / nTextHeight + cur_line_index;
if (y < 0)
y = x = 0;
if (y > SCROLL_BUFFER_SIZE)
{
y = SCROLL_BUFFER_SIZE;
x = 0;
}
if (x < 0)
x = -1;
if (x > MAX_LINE_WIDTH)
{
y++;
x = 0; // marks to end of line
}
}
// InvalidateRect(hOutputWnd,NULL,0);
void load_filter_info(void)
{
FILE *fp = NULL;
char pathname[MAX_PATH_LEN];
char inbuf[NAME_LENGTH+4];
outwnd_filter_struct new_filter;
int z;
outwnd_filter_loaded = 1;
memset( pathname, 0, sizeof(pathname) );
snprintf( pathname, MAX_PATH_LEN, "%s\\%s\\%s", detect_home(), Pathtypes[CF_TYPE_DATA].path, NOX("debug_filter.cfg") );
fp = fopen(pathname, "rt");
if (!fp) {
Outwnd_no_filter_file = 1;
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
strcpy_s( new_filter.name, "error" );
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
strcpy_s( new_filter.name, "general" );
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
strcpy_s( new_filter.name, "warning" );
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
return;
}
Outwnd_no_filter_file = 0;
while ( fgets(inbuf, NAME_LENGTH+3, fp) ) {
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
if (*inbuf == '+')
new_filter.enabled = true;
else if (*inbuf == '-')
new_filter.enabled = false;
else
continue; // skip everything else
z = strlen(inbuf) - 1;
if (inbuf[z] == '\n')
inbuf[z] = 0;
Assert( strlen(inbuf+1) < NAME_LENGTH );
strcpy_s(new_filter.name, inbuf + 1);
if ( !stricmp(new_filter.name, "error") ) {
new_filter.enabled = true;
} else if ( !stricmp(new_filter.name, "general") ) {
new_filter.enabled = true;
} else if ( !stricmp(new_filter.name, "warning") ) {
new_filter.enabled = true;
}
OutwndFilter.push_back( new_filter );
}
if ( ferror(fp) && !feof(fp) )
nprintf(("Error", "Error reading \"%s\"\n", pathname));
fclose(fp);
}
void save_filter_info(void)
{
FILE *fp = NULL;
char pathname[MAX_PATH_LEN];
if ( !outwnd_filter_loaded )
return;
if (Outwnd_no_filter_file)
return; // No file, don't save
memset( pathname, 0, sizeof(pathname) );
snprintf( pathname, MAX_PATH_LEN, "%s\\%s\\%s", detect_home(), Pathtypes[CF_TYPE_DATA].path, NOX("debug_filter.cfg") );
fp = fopen(pathname, "wt");
if (fp) {
for (uint i = 0; i < OutwndFilter.size(); i++)
fprintf(fp, "%c%s\n", OutwndFilter[i].enabled ? '+' : '-', OutwndFilter[i].name);
fclose(fp);
}
}
void outwnd_printf2(const char *format, ...)
{
SCP_string temp;
va_list args;
if (format == NULL)
return;
va_start(args, format);
vsprintf(temp, format, args);
va_end(args);
outwnd_print("General", temp.c_str());
}
#ifndef NMONO
char mono_ram[80*25*2];
int mono_x, mono_y;
int mono_found = 0;
void mono_flush()
{
if ( !mono_found ) return;
memcpy( (void *)0xb0000, mono_ram, 80*25*2 );
}
void mono_init()
{
int i;
OSVERSIONINFO ver;
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&ver);
if ( ver.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
mono_found = 0;
return;
}
_outp( 0x3b4, 0x0f );
_outp( 0x3b4+1, 0x55 );
if ( _inp( 0x3b4+1 ) == 0x55 ) {
mono_found = 1;
_outp( 0x3b4+1, 0 );
} else {
mono_found = 0;
return;
}
for (i=0; i<80*25; i++ ) {
mono_ram[i*2+0] = ' ';
mono_ram[i*2+1] = 0x07;
}
mono_flush();
mono_x = mono_y = 0;
}
void mono_print( char * text, int len )
{
int i, j;
if ( !mono_found )
return;
for (i=0; i<len; i++ ) {
int scroll_it = 0;
switch( text[i] ) {
case '\n':
scroll_it = 1;
break;
default:
mono_ram[mono_y*160+mono_x*2] = text[i];
if ( mono_x < 79 ) {
mono_x++;
} else {
scroll_it = 1;
}
break;
}
if ( scroll_it ) {
if ( mono_y < 24 ) {
mono_y++;
mono_x = 0;
} else {
memmove( mono_ram, mono_ram+160, 80*24*2 );
for (j=0; j<80; j++ ) {
mono_ram[24*160+j*2] = ' ';
}
mono_y = 24;
mono_x = 0;
}
}
}
mono_flush();
}
#endif // NMONO
void outwnd_printf(const char *id, const char *format, ...)
{
SCP_string temp;
va_list args;
if ( (id == NULL) || (format == NULL) )
return;
va_start(args, format);
vsprintf(temp, format, args);
va_end(args);
outwnd_print(id, temp.c_str());
}
void outwnd_print(const char *id, const char *tmp)
{
const char *sptr;
char *dptr;
int nrows, ccol;
uint i;
if ( (id == NULL) || (tmp == NULL) )
return;
if ( !outwnd_inited )
return;
if (Outwnd_no_filter_file == 1) {
Outwnd_no_filter_file = 2;
outwnd_print( "general", "==========================================================================\n" );
outwnd_print( "general", "DEBUG SPEW: No debug_filter.cfg found, so only general, error, and warning\n" );
outwnd_print( "general", "categories can be shown and no debug_filter.cfg info will be saved.\n" );
outwnd_print( "general", "==========================================================================\n" );
}
if ( !id )
id = "General";
uint outwnd_size = OutwndFilter.size();
for (i = 0; i < OutwndFilter.size(); i++) {
if ( !stricmp(id, OutwndFilter[i].name) )
break;
}
// id found that isn't in the filter list yet
if ( i == outwnd_size ) {
// Only create new filters if there was a filter file
if (Outwnd_no_filter_file)
return;
Assert( strlen(id)+1 < NAME_LENGTH );
outwnd_filter_struct new_filter;
strcpy_s(new_filter.name, id);
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
save_filter_info();
}
if ( !OutwndFilter[i].enabled )
return;
if (Log_debug_output_to_file) {
if (Log_fp != NULL) {
fputs(tmp, Log_fp);
fflush(Log_fp);
}
}
if ( !Cmdline_debug_window )
return;
// everything after this point relates to printing in the debug window
if (mprintf_last_line == -1 ) {
for (i=0; i<SCROLL_BUFFER_SIZE;i++) {
outtext[i][0] = 0;
}
mprintf_last_line = 0;
}
// printf out to the monochrome screen first
if ( mono_driver != ((HANDLE)-1) ) {
DWORD cbReturned;
DeviceIoControl (mono_driver, (DWORD)IOCTL_MONO_PRINT, const_cast<char*>(tmp), strlen(tmp), NULL, 0, &cbReturned, 0 );
#ifndef NMONO
} else {
mono_print(tmp, strlen(tmp) );
#endif
}
sptr = tmp;
ccol = strlen(outtext[mprintf_last_line] );
dptr = &outtext[mprintf_last_line][ccol];
nrows = 0;
while(*sptr) {
if ( (*sptr == '\n') || (ccol >= MAX_LINE_WIDTH-1 ) ) {
nrows++;
mprintf_last_line++;
if (mprintf_last_line >= SCROLL_BUFFER_SIZE )
mprintf_last_line = 0;
ccol = 0;
if ( *sptr != '\n' ) {
outtext[mprintf_last_line][ccol] = *sptr;
ccol++;
}
outtext[mprintf_last_line][ccol] = '\0';
dptr = &outtext[mprintf_last_line][ccol];
} else {
*dptr++ = *sptr;
*dptr = '\0';
ccol++;
}
sptr++;
}
if (outwnd_disabled)
return;
if ( !OutputActive ) {
int oldpos = GetScrollPos( hOutputWnd, SB_VERT );
if ( oldpos != max_scroll_pos ) {
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = max_scroll_pos;
SetScrollInfo(hOutputWnd, SB_VERT, &si, 1 );
InvalidateRect(hOutputWnd,NULL,0);
UpdateWindow(hOutputWnd);
} else {
if ( nrows ) {
RECT client;
ScrollWindow(hOutputWnd,0,-nTextHeight*nrows,NULL,NULL);
GetClientRect(hOutputWnd, &client);
client.top = client.bottom - nTextHeight*(nrows+1);
InvalidateRect(hOutputWnd,&client,0);
UpdateWindow(hOutputWnd);
} else {
Outwnd_changed++;
}
}
}
}
LRESULT CALLBACK outwnd_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
switch(msg) {
case WM_ACTIVATEAPP:
// The application z-ordering has change
// foreground application wParm will be
OutputActive = (BOOL)wParam;
break;
case WM_CREATE: {
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = max_scroll_pos;
si.nPos = max_scroll_pos;
SetScrollInfo(hwnd, SB_VERT, &si, 1 );
}
break;
case WM_TIMER:
if (scroll_wait)
PostMessage(hwnd, WM_MOUSEMOVE, 0, last_mousemove);
if ( Outwnd_changed ) {
RECT client;
GetClientRect(hOutputWnd, &client);
client.top = client.bottom - nTextHeight;
InvalidateRect(hOutputWnd,&client,0);
}
scroll_wait = 0;
break;
case WM_COMMAND: {
int z;
z = LOWORD(wParam);
if (z >= ID_FILTER && z < ID_FILTER + (int)OutwndFilter.size())
{
z -= ID_FILTER;
OutwndFilter[z].enabled = !OutwndFilter[z].enabled;
if ( !stricmp( OutwndFilter[z].name, "error" ) ) {
OutwndFilter[z].enabled = 1;
} else if ( !stricmp( OutwndFilter[z].name, "general" ) ) {
OutwndFilter[z].enabled = 1;
} else if ( !stricmp( OutwndFilter[z].name, "warning" ) ) {
OutwndFilter[z].enabled = 1;
}
save_filter_info();
break;
}
switch (z)
{
case ID_COPY:
outwnd_copy_marked_selection(hwnd);
break;
/*
case ID_FIND:
if (DialogBox(GetModuleHandle(NULL), "FIND_DIALOG", hOutputWnd,
(int (__stdcall *)(void)) find_dlg_handler) == IDOK)
{
find_text_in_outwindow(mprintf_last_line, 0);
}
break;
*/
}
break;
}
case WM_RBUTTONDOWN: {
HMENU h_menu = CreatePopupMenu();
HMENU h_sub_menu = CreatePopupMenu();
POINT pt;
for (uint i = 0; i < OutwndFilter.size(); i++) {
UINT flags = MFT_STRING; //MF_GRAYED;
if ( !stricmp( OutwndFilter[i].name, "error" ) ) {
flags |= MF_GRAYED;
} else if ( !stricmp( OutwndFilter[i].name, "general" ) ) {
flags |= MF_GRAYED;
} else if ( !stricmp( OutwndFilter[i].name, "warning" ) ) {
flags |= MF_GRAYED;
}
if (OutwndFilter[i].enabled)
AppendMenu(h_sub_menu, flags | MF_CHECKED, ID_FILTER + i, OutwndFilter[i].name);
else
AppendMenu(h_sub_menu, flags, ID_FILTER + i, OutwndFilter[i].name);
}
AppendMenu(h_menu, MFT_STRING, ID_COPY, "&Copy\tEnter");
AppendMenu(h_menu, MFT_STRING, ID_FIND, "&Find Text");
AppendMenu(h_menu, MF_POPUP, (unsigned int) h_sub_menu, "Filter &Messages");
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
ClientToScreen(hwnd, &pt);
TrackPopupMenu(h_menu, 0, pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(h_menu);
break;
}
case WM_LBUTTONDOWN:
fix_marking_coords(marking_started_x, marking_started_y, lParam);
SetCapture(hwnd); // monopolize mouse
marking_active = 1;
outwnd_update_marking(lParam, hwnd);
break;
case WM_MOUSEMOVE:
last_mousemove = lParam;
if (marking_active){
outwnd_update_marking(lParam, hwnd);
}
break;
case WM_LBUTTONUP:
if (marking_active)
{
ReleaseCapture();
marking_active = 0;
outwnd_update_marking(lParam, hwnd);
}
break;
case WM_VSCROLL: {
SCROLLINFO si;
int vpos = GetScrollPos( hwnd, SB_VERT );
int old_vpos=vpos;
switch (LOWORD(wParam)) {
case SB_LINEDOWN:
vpos++;
break;
case SB_LINEUP:
vpos--;
break;
case SB_THUMBPOSITION:
vpos = HIWORD(wParam);
break;
case SB_THUMBTRACK:
vpos = HIWORD(wParam);
break;
case SB_PAGEDOWN:
vpos += nCharRows;
break;
case SB_PAGEUP:
vpos -= nCharRows;
break;
}
if ( vpos < 0 ) vpos = 0;
else if ( vpos > max_scroll_pos ) vpos = max_scroll_pos;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = vpos;
SetScrollInfo(hwnd, SB_VERT, &si, 1 );
ScrollWindow(hwnd,0,(old_vpos-vpos)*nTextHeight,NULL,NULL);
UpdateWindow(hOutputWnd);
//InvalidateRect(hOutputWnd,NULL,0);
}
break;
case WM_KEYDOWN: {
SCROLLINFO si;
int vpos = GetScrollPos( hwnd, SB_VERT );
int old_vpos=vpos;
int nVirtKey = (int) wParam; // virtual-key code
switch(nVirtKey) {
case VK_DOWN:
case VK_RIGHT:
vpos++;
break;
case VK_UP:
case VK_LEFT:
vpos--;
break;
case VK_NEXT:
vpos += nCharRows;
break;
case VK_PRIOR:
vpos -= nCharRows;
break;
case VK_END:
vpos = 0;
break;
case VK_HOME:
vpos = max_scroll_pos;
break;
case VK_RETURN:
outwnd_copy_marked_selection(hwnd);
break;
case UP_FAST: // special value we define
vpos -= 5;
break;
case DOWN_FAST: // special value we define
vpos += 5;
break;
}
if ( vpos < 0 ) vpos = 0;
else if ( vpos > max_scroll_pos ) vpos = max_scroll_pos;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = vpos;
SetScrollInfo(hwnd, SB_VERT, &si, 1 );
ScrollWindow(hwnd, 0, (old_vpos-vpos)*nTextHeight, NULL, NULL);
UpdateWindow(hOutputWnd);
//InvalidateRect(hOutputWnd,NULL,0);
}
break;
case WM_SIZE:
InvalidateRect(hOutputWnd,NULL,0);
break;
case WM_PAINT:
outwnd_paint(hwnd);
break;
case WM_DESTROY:
outwnd_disabled = true;
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
return 0;
}
void outwnd_copy_marked_selection(HWND hwnd)
{
HGLOBAL h_text;
int i, size = 1;
char *ptr;
if (marked_top == marked_bottom)
{
size += marked_right - marked_left;
} else {
i = marked_top;
size += strlen(outtext[i++]) - marked_left + 2;
while (i < marked_bottom)
size += strlen(outtext[i++]) + 2;
size += marked_right;
}
h_text = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
ptr = (char *) GlobalLock(h_text);
if (marked_top == marked_bottom)
{
i = marked_right - marked_left;
memcpy(ptr, outtext[marked_top] + marked_left, i);
ptr[i] = 0;
} else {
i = marked_top;
strcpy(ptr, outtext[i] + marked_left);
strcat(ptr, "\r\n");
i++;
while (i < marked_bottom)
{
strcat(ptr, outtext[i++]);
strcat(ptr, "\r\n");
}
ptr[strlen(ptr) + marked_right] = 0;
strncat(ptr, outtext[i], marked_right);
}
GlobalUnlock(h_text);
OpenClipboard(hwnd);
EmptyClipboard();
SetClipboardData(CF_TEXT, h_text);
CloseClipboard();
marked = 0;
InvalidateRect(hwnd, NULL, 0);
}
void outwnd_paint(HWND hwnd)
{
int i, n, x, y, len;
int old_nrows, scroll_pos;
HDC hdc;
PAINTSTRUCT ps;
RECT client;
TEXTMETRIC tm;
HFONT newfont, oldfont;
HBRUSH newbrush, oldbrush;
Outwnd_changed = 0;
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hOutputWnd, &client);
newfont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
oldfont = (HFONT)SelectObject(hdc,newfont);
GetTextMetrics(hdc, &tm);
nTextHeight = tm.tmHeight + tm.tmExternalLeading;
nTextWidth = tm.tmAveCharWidth;
old_nrows = nCharRows;
nCharRows = ((client.bottom-client.top)/nTextHeight)+1;
newbrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
oldbrush = (HBRUSH)SelectObject(hdc,newbrush);
y = client.bottom - nTextHeight * nCharRows; // starting y position at top
client_top_yoffset = y - nTextHeight;
scroll_pos = (max_scroll_pos - GetScrollPos( hOutputWnd, SB_VERT ));
cur_line_index = x = mprintf_last_line - scroll_pos - nCharRows;
if (x >= marked_top && x < marked_bottom) // starting in marked area
text_hilight(hdc);
else
text_normal(hdc);
if (scroll_pos != old_scroll_pos) {
if (!scroll_pos) {
char tmp[1024];
sprintf( tmp, "Debug Spew");
SetWindowText( hOutputWnd, tmp );
} else {
char tmp[1024];
sprintf( tmp, "Debug Spew [Scrolled back %d lines]", scroll_pos );
SetWindowText( hOutputWnd, tmp );
}
old_scroll_pos = scroll_pos;
}
i = nCharRows;
while (i--)
{
n = mprintf_last_line - scroll_pos - i;
if (n < 0)
n += SCROLL_BUFFER_SIZE;
if (n >= 0 && n < SCROLL_BUFFER_SIZE)
{
len = strlen(outtext[n]);
if (marked)
{
if (n == marked_top && n == marked_bottom) // special 1 line case
{
if (marked_left)
TextOut(hdc, 0, y, outtext[n], marked_left);
text_hilight(hdc);
x = marked_left * nTextWidth;
TextOut(hdc, x, y, outtext[n] + marked_left, marked_right -
marked_left);
text_normal(hdc);
x = marked_right * nTextWidth;
if (marked_right < len)
TextOut(hdc, x, y, outtext[n] + marked_right, len - marked_right);
x = len * nTextWidth;
TextOut(hdc, x, y, spaces, MAX_LINE_WIDTH - len);
} else if (n == marked_top) { // start marked on this line
if (marked_left)
TextOut(hdc, 0, y, outtext[n], marked_left);
text_hilight(hdc);
x = marked_left * nTextWidth;
TextOut(hdc, x, y, outtext[n] + marked_left, len - marked_left);
x = len * nTextWidth;
if (marked_left < MAX_LINE_WIDTH)
TextOut(hdc, x, y, spaces, MAX_LINE_WIDTH - marked_left);
} else if (n == marked_bottom) { // end marked on this line
if (marked_right)
TextOut(hdc, 0, y, outtext[n], marked_right);
text_normal(hdc);
x = marked_right * nTextWidth;
if (marked_right < len)
TextOut(hdc, x, y, outtext[n] + marked_right, len - marked_right);
x = len * nTextWidth;
TextOut(hdc, x, y, spaces, MAX_LINE_WIDTH - len);
} else { // whole line marked
TextOut(hdc, 0, y, outtext[n], len);
x = len * nTextWidth;
TextOut(hdc, x, y, spaces, MAX_LINE_WIDTH - len);
}
} else {
TextOut(hdc, 0, y, outtext[n], len);
x = len * nTextWidth;
TextOut(hdc, x, y, spaces, MAX_LINE_WIDTH - len);
}
} else
TextOut(hdc, 0, y, spaces, MAX_LINE_WIDTH);
y += nTextHeight;
}
text_normal(hdc);
SelectObject(hdc, oldfont);
SelectObject(hdc, oldbrush);
DeleteObject(newbrush);
if ( old_nrows != nCharRows ) {
SCROLLINFO si;
max_scroll_pos = SCROLL_BUFFER_SIZE-nCharRows - 1;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE;
si.nMin = 0;
si.nMax = max_scroll_pos;
SetScrollInfo(hwnd, SB_VERT, &si, 1 );
}
EndPaint(hwnd, &ps);
}
void outwnd_update_marking(LPARAM l_parm, HWND hwnd)
{
int x, y;
RECT rect;
if (!scroll_wait) {
y = (signed short) HIWORD(l_parm);
GetClientRect(hwnd, &rect);
if (y < -150) {
SendMessage(hwnd, WM_KEYDOWN, UP_FAST, 0);
scroll_wait = 1;
} else if (y < 0) {
SendMessage(hwnd, WM_KEYDOWN, VK_UP, 0);
scroll_wait = 1;
}
if (y >= rect.bottom + 150) {
SendMessage(hwnd, WM_KEYDOWN, DOWN_FAST, 0);
scroll_wait = 1;
} else if (y >= rect.bottom) {
SendMessage(hwnd, WM_KEYDOWN, VK_DOWN, 0);
scroll_wait = 1;
}
}
fix_marking_coords(x, y, l_parm);
marked = 1;
marked_top = marked_bottom = marking_started_y;
marked_left = marking_started_x;
marked_right = marking_started_x + 1;
if (y < marked_top)
{
marked_top = y;
marked_left = x;
} else if (y > marked_bottom) {
marked_bottom = y;
marked_right = x + 1;
} else { // must be single line case
if (x < marked_left)
marked_left = x;
if (x >= marked_right)
marked_right = x + 1;
if (marked_left >= (signed int) strlen(outtext[y]))
{
marked = 0; // this isn't going to even show up
return;
}
}
if (marked_left >= (signed int) strlen(outtext[marked_top]))
{
marked_top++;
marked_left = 0;
}
if (marked_right > (signed int) strlen(outtext[marked_bottom]))
marked_right = strlen(outtext[marked_bottom]);
if (marked && (marked_top > marked_bottom || (marked_top == marked_bottom &&
marked_left >= marked_right)))
{
char msg[1024];
sprintf(msg, "Marking limits invalid!\n"
"(%d,%d) to (%d,%d)", marked_left, marked_top, marked_right, marked_bottom);
MessageBox(hwnd, msg, "Error", MB_OK | MB_ICONERROR);
marked = 0;
}
if (marked != old_marked || marked_top != old_marked_top || marked_bottom !=
old_marked_bottom || marked_left != old_marked_left || marked_right !=
old_marked_right)
{
old_marked = marked;
old_marked_left = marked_left;
old_marked_right = marked_right;
old_marked_top = marked_top;
old_marked_bottom = marked_bottom;
InvalidateRect(hwnd, NULL, 0);
}
}
BOOL outwnd_create(int display_under_freespace_window)
{
int x;
WNDCLASSEX wclass;
HINSTANCE hInst = GetModuleHandle(NULL);
RECT rect;
DWORD style;
wclass.hInstance = hInst;
wclass.lpszClassName = szOutputClass;
wclass.lpfnWndProc = (WNDPROC) outwnd_handler;
wclass.style = CS_BYTEALIGNCLIENT | CS_OWNDC; //CS_DBLCLKS | CS_PARENTDC| CS_VREDRAW | CS_HREDRAW |;
wclass.cbSize = sizeof(WNDCLASSEX);
wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wclass.lpszMenuName = NULL; //"FreeSpaceMenu";
wclass.cbClsExtra = 0;
wclass.cbWndExtra = 0;
wclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //(HBRUSH)NULL;
if (!RegisterClassEx(&wclass))
return FALSE;
if (display_under_freespace_window) {
style = WS_OVERLAPPEDWINDOW;
RECT client_rect;
client_rect.left = client_rect.top = 0;
client_rect.right = 640;
client_rect.bottom = 480;
AdjustWindowRect(&client_rect,WS_CAPTION | WS_SYSMENU,FALSE);
int x = (GetSystemMetrics( SM_CXSCREEN )-(client_rect.right - client_rect.left))/2;
int y = 0;
if ( x < 0 ) x = 0;
rect.left = x;
rect.top = y;
rect.right = x + client_rect.right - client_rect.left - 1;
rect.bottom = y + client_rect.bottom - client_rect.top - 1;
if(!Is_standalone){
rect.top = rect.bottom;
rect.bottom = GetSystemMetrics(SM_CYSCREEN) - TASKBAR_HEIGHT - rect.top;
rect.right -= rect.left;
} else {
rect.left += 350;
rect.right = 550;
rect.bottom = 400;
}
} else {
style = WS_OVERLAPPEDWINDOW | WS_MINIMIZE;
rect.top = rect.bottom = rect.left = rect.right = CW_USEDEFAULT;
}
// Create Game Window
hOutputWnd = CreateWindow(szOutputClass, "Debug Spew", style, rect.left,
rect.top, rect.right, rect.bottom, NULL, NULL, hInst, NULL);
// Show it, but don't activate it. If you activate it, it cause problems
// with fullscreen startups in main window.
SetWindowPos( hOutputWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW );
outwnd_disabled = false;
SetTimer(hOutputWnd, TIMER1, 50, NULL);
for (x=0; x<MAX_LINE_WIDTH; x++)
spaces[x] = ' ';
spaces[MAX_LINE_WIDTH] = 0;
return TRUE;
}
DWORD outwnd_thread(int display_under_freespace_window)
{
MSG msg;
if (!outwnd_create(display_under_freespace_window))
return 0;
while (1) {
if (WaitMessage()) {
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if (outwnd_disabled) break;
}
return 0;
}
void close_mono()
{
// DeviceIoControl (mono_driver, (DWORD) IOCTL_MONO_CLEAR_SCREEN, NULL, 0, NULL, 0, &cbReturned, 0);
if (hOutputThread) {
CloseHandle(hOutputThread);
hOutputThread = NULL;
}
if (mono_driver) {
CloseHandle(mono_driver);
mono_driver = NULL;
}
}
void outwnd_init_debug_window(int display_under_freespace_window)
{
static bool debug_window_inited = false;
if ( !Cmdline_debug_window || !outwnd_inited || debug_window_inited )
return;
hOutputThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)outwnd_thread, (LPVOID)display_under_freespace_window, 0, &OutputThreadID);
//SetThreadPriority(hOutputThread, THREAD_PRIORITY_TIME_CRITICAL);
#ifndef NMONO
// set up the monochrome drivers
if ( (mono_driver = CreateFile("\\\\.\\MONO", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == ((HANDLE)-1)) {
outwnd_printf2("Cannot get handle to monochrome driver.\n");
mono_init();
}
atexit(close_mono);
#endif
debug_window_inited = true;
}
void outwnd_init(int display_under_freespace_window)
{
if (outwnd_inited)
return;
/*
if (Cmdline_debug_window) {
hOutputThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)outwnd_thread, (LPVOID)display_under_freespace_window, 0, &OutputThreadID);
//SetThreadPriority(hOutputThread, THREAD_PRIORITY_TIME_CRITICAL);
#ifndef NMONO
// set up the monochrome drivers
if ( (mono_driver = CreateFile("\\\\.\\MONO", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == ((HANDLE)-1)) {
outwnd_printf2("Cannot get handle to monochrome driver.\n");
mono_init();
}
atexit(close_mono);
#endif
}
*/
if (Log_fp == NULL) {
char pathname[MAX_PATH_LEN];
/* Set where the log file is going to go */
// Zacam: Set various conditions based on what type of log to generate.
if (Fred_running) {
FreeSpace_logfilename = "fred2_open.log";
} else if (Is_standalone) {
FreeSpace_logfilename = "fs2_standalone.log";
} else {
FreeSpace_logfilename = "fs2_open.log";
}
memset( pathname, 0, sizeof(pathname) );
snprintf(pathname, MAX_PATH_LEN-1, "%s\\%s\\%s", detect_home(), Pathtypes[CF_TYPE_DATA].path, FreeSpace_logfilename);
Log_fp = fopen(pathname, "wb");
if (Log_fp == NULL) {
outwnd_printf("Error", "Error opening %s\n", pathname);
} else {
time_t timedate = time(NULL);
char datestr[50];
memset( datestr, 0, sizeof(datestr) );
strftime( datestr, sizeof(datestr)-1, "%a %b %d %H:%M:%S %Y", localtime(&timedate) );
outwnd_printf("General", "Opened log '%s', %s ...\n", pathname, datestr);
}
}
outwnd_inited = true;
}
BOOL CALLBACK find_dlg_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
GetDlgItemText(hwnd, IDC_TEXT, find_text, 82); // get the text to find
EndDialog(hwnd, IDOK);
return 1;
case IDCANCEL:
EndDialog(hwnd, 0);
return 1;
}
break;
case WM_INITDIALOG:
SendDlgItemMessage(hwnd, IDC_TEXT, EM_LIMITTEXT, 80, 0); // limit text to 80 chars
if (GetDlgCtrlID((HWND) wParam) != IDC_TEXT) {
SetFocus(GetDlgItem(hwnd, IDC_TEXT));
return FALSE;
}
return TRUE;
}
return 0;
}
void find_text_in_outwindow(int n, int p)
{
char *ptr, *str;
str = outtext[n] + p;
while (1)
{
ptr = strstr(str, find_text);
if (ptr)
{
int scroll_pos, pos;
find_pos = ptr - str;
find_line = n;
marked = 1;
marked_top = marked_bottom = find_line;
marked_left = find_pos;
marked_right = find_pos + strlen(find_text);
scroll_pos = (max_scroll_pos - GetScrollPos(hOutputWnd, SB_VERT));
pos = mprintf_last_line - scroll_pos;
if (pos < 0)
pos += SCROLL_BUFFER_SIZE;
pos -= n;
if (pos < 0)
pos += SCROLL_BUFFER_SIZE;
Assert(pos >= 0);
if (pos >= nCharRows - 1) // outside of window viewport, so scroll
{
SCROLLINFO si;
pos = mprintf_last_line - n - nCharRows / 2;
if (pos < 0)
pos += SCROLL_BUFFER_SIZE;
if (pos > max_scroll_pos)
pos = max_scroll_pos;
scroll_pos = max_scroll_pos - GetScrollPos(hOutputWnd, SB_VERT);
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = max_scroll_pos - pos;
SetScrollInfo(hOutputWnd, SB_VERT, &si, 1);
ScrollWindow(hOutputWnd, 0, (scroll_pos - pos) * nTextHeight, NULL, NULL);
}
InvalidateRect(hOutputWnd, NULL, 0);
UpdateWindow(hOutputWnd);
return;
}
n--;
if (n < 0)
n += SCROLL_BUFFER_SIZE;
if (n == mprintf_last_line)
{
MessageBox(hOutputWnd, "Search text not found", "Find Error", MB_OK | MB_ICONERROR);
find_line = -1;
return;
}
str = outtext[n];
}
}
void outwnd_close()
{
if ( Log_fp != NULL ) {
time_t timedate = time(NULL);
char datestr[50];
memset( datestr, 0, sizeof(datestr) );
strftime( datestr, sizeof(datestr)-1, "%a %b %d %H:%M:%S %Y", localtime(&timedate) );
outwnd_printf("General", "... Log closed, %s\n", datestr);
fclose(Log_fp);
Log_fp = NULL;
}
outwnd_inited = false;
}
void safe_point_print(const char *format, ...)
{
SCP_string temp;
va_list args;
va_start(args, format);
vsprintf(temp, format, args);
va_end(args);
safe_string = temp;
}
void safe_point(const char *file, int line, const char *format, ...)
{
safe_point_print("last safepoint: %s, %d; [%s]", file, line, format);
}
#endif //NDEBUG
|