git @ Cat's Eye Technologies linapple / master src / Frame.cpp
master

Tree @master (Download .tar.gz)

Frame.cpp @masterraw · history · blame

   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
/*
AppleWin : An Apple //e emulator for Windows

Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2007, Tom Charlesworth, Michael Pohoreski

AppleWin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AppleWin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Description: Frame
 *
 * Author: Various
 */

/* Adaptation for SDL and POSIX (l) by beom beotiger, Nov-Dec 2007 */

/* And KREZ */

// for usleep
#include <unistd.h>

#include "stdafx.h"

#include "asset.h"
//#pragma  hdrstop
#include "MouseInterface.h"
//#include "..\resource\resource.h"

// for stat in FrameSaveBMP function
#include <sys/stat.h>

#include <iostream>

#define ENABLE_MENU 0

SDL_Surface * screen;  // our main screen
// rects for screen stretch if needed
SDL_Rect origRect;
SDL_Rect newRect;
///////////////////////////////////////////
#define  VIEWPORTCX  560
#if ENABLE_MENU
#define  VIEWPORTCY  400
#else
#define  VIEWPORTCY  384
#endif
#define  BUTTONX     (VIEWPORTCX+(VIEWPORTX<<1))
#define  BUTTONY     0
#define  BUTTONCX    45
#define  BUTTONCY    45
#define  FSVIEWPORTX (640-BUTTONCX-VIEWPORTX-VIEWPORTCX)
#define  FSVIEWPORTY ((480-VIEWPORTCY)>>1)
#define  FSBUTTONX   (640-BUTTONCX)
#define  FSBUTTONY   (((480-VIEWPORTCY)>>1)-1)
#define  BUTTONS     8


/*static HBITMAP capsbitmap[2];
  static HBITMAP diskbitmap[ NUM_DISK_STATUS ];

  static HBITMAP buttonbitmap[BUTTONS];*/

//static BOOL    active          = 0;
static bool    g_bAppActive = false;

/*static HBRUSH  btnfacebrush    = (HBRUSH)0;
  static HPEN    btnfacepen      = (HPEN)0;
  static HPEN    btnhighlightpen = (HPEN)0;
  static HPEN    btnshadowpen    = (HPEN)0;*/

//static int     buttonactive    = -1;

static int     buttondown      = -1;


//static int     buttonover      = -1;


//static int     buttonx         = BUTTONX;
//static int     buttony         = BUTTONY;

/*static HRGN    clipregion      = (HRGN)0;
  HDC     g_hFrameDC         = (HDC)0;
  static RECT    framerect       = {0,0,0,0};
  HWND    g_hFrameWindow     = (HWND)0;*/
BOOL    fullscreen      = 0;
BOOL  g_WindowResized;  // if we have not normal window size

//static BOOL    helpquit        = 0;

// static BOOL    painting        = 0;
// static HFONT   smallfont       = (HFONT)0;
// static HWND    tooltipwindow   = (HWND)0;


static BOOL    usingcursor     = 0;
//static int     viewportx       = VIEWPORTX;
//static int     viewporty       = VIEWPORTY;

// Hmmm. I love DirectDraw(tm). But SDL is better???????? for Linux, at least. Ha-ha-ha --bb
// static LPDIRECTDRAW        directdraw = (LPDIRECTDRAW)0;
// static LPDIRECTDRAWSURFACE surface    = (LPDIRECTDRAWSURFACE)0;

void    DrawStatusArea (/*HDC passdc,*/ BOOL drawflags);
void    ProcessButtonClick (int button, int mod); // handle control buttons(F1-..F12) events

//void  ProcessDiskPopupMenu(HWND hwnd, POINT pt, const int iDrive);
//void    RelayEvent (UINT message, WPARAM wparam, LPARAM lparam);

void    ResetMachineState ();
void    SetFullScreenMode ();
void    SetNormalMode ();
void    SetUsingCursor (BOOL);

bool  g_bScrollLock_FullSpeed = false;  // no in full speed!

//===========================================================================
/*
   void CreateGdiObjects () {
   ZeroMemory(buttonbitmap,BUTTONS*sizeof(HBITMAP));
#define LOADBUTTONBITMAP(bitmapname)  LoadImage(g_hInstance,bitmapname,   \
IMAGE_BITMAP,0,0,      \
LR_CREATEDIBSECTION |  \
LR_LOADMAP3DCOLORS |   \
LR_LOADTRANSPARENT);
buttonbitmap[BTN_HELP   ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("HELP_BUTTON"));
buttonbitmap[BTN_RUN    ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("RUN_BUTTON"));
buttonbitmap[BTN_DRIVE1 ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DRIVE1_BUTTON"));
buttonbitmap[BTN_DRIVE2 ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DRIVE2_BUTTON"));
buttonbitmap[BTN_DRIVESWAP] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DRIVESWAP_BUTTON"));
buttonbitmap[BTN_FULLSCR] = (HBITMAP)LOADBUTTONBITMAP(TEXT("FULLSCR_BUTTON"));
buttonbitmap[BTN_DEBUG  ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DEBUG_BUTTON"));
buttonbitmap[BTN_SETUP  ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("SETUP_BUTTON"));
capsbitmap[0] = (HBITMAP)LOADBUTTONBITMAP(TEXT("CAPSOFF_BITMAP"));
capsbitmap[1] = (HBITMAP)LOADBUTTONBITMAP(TEXT("CAPSON_BITMAP"));

diskbitmap[ DISK_STATUS_OFF  ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DISKOFF_BITMAP"));
diskbitmap[ DISK_STATUS_READ ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DISKREAD_BITMAP"));
diskbitmap[ DISK_STATUS_WRITE] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DISKWRITE_BITMAP"));
diskbitmap[ DISK_STATUS_PROT ] = (HBITMAP)LOADBUTTONBITMAP(TEXT("DISKPROT_BITMAP"));

btnfacebrush    = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
btnfacepen      = CreatePen(PS_SOLID,1,GetSysColor(COLOR_BTNFACE));
btnhighlightpen = CreatePen(PS_SOLID,1,GetSysColor(COLOR_BTNHIGHLIGHT));
btnshadowpen    = CreatePen(PS_SOLID,1,GetSysColor(COLOR_BTNSHADOW));
smallfont = CreateFont(11,6,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,VARIABLE_PITCH | FF_SWISS,
TEXT("Small Fonts"));
}


//===========================================================================
void DeleteGdiObjects () {
int loop;
for (loop = 0; loop < BUTTONS; loop++)
DeleteObject(buttonbitmap[loop]);
for (loop = 0; loop < 2; loop++)
DeleteObject(capsbitmap[loop]);
for (loop = 0; loop < NUM_DISK_STATUS; loop++)
DeleteObject(diskbitmap[loop]);
DeleteObject(btnfacebrush);
DeleteObject(btnfacepen);
DeleteObject(btnhighlightpen);
DeleteObject(btnshadowpen);
DeleteObject(smallfont);
}

// Draws an 3D box around the main apple screen
//===========================================================================
void Draw3dRect (HDC dc, int x1, int y1, int x2, int y2, BOOL out)
{
SelectObject(dc,GetStockObject(NULL_BRUSH));
SelectObject(dc,out ? btnshadowpen : btnhighlightpen);
POINT pt[3];
pt[0].x = x1;    pt[0].y = y2-1;
pt[1].x = x2-1;  pt[1].y = y2-1;
pt[2].x = x2-1;  pt[2].y = y1;
Polyline(dc,(LPPOINT)&pt,3);
SelectObject(dc,(out == 1) ? btnhighlightpen : btnshadowpen);
pt[1].x = x1;    pt[1].y = y1;
pt[2].x = x2;    pt[2].y = y1;
Polyline(dc,(LPPOINT)&pt,3);
}

//===========================================================================
void DrawBitmapRect (HDC dc, int x, int y, LPRECT rect, HBITMAP bitmap) {
HDC memdc = CreateCompatibleDC(dc);
SelectObject(memdc,bitmap);
BitBlt(dc,x,y,
    rect->right  + 1 - rect->left,
    rect->bottom + 1 - rect->top,
    memdc,
    rect->left,
    rect->top,
    SRCCOPY);
DeleteDC(memdc);
}

//===========================================================================
void DrawButton (HDC passdc, int number) {
  FrameReleaseDC();
  HDC dc = (passdc ? passdc : GetDC(g_hFrameWindow));
  int x  = buttonx;
  int y  = buttony+number*BUTTONCY;
  if (number == buttondown) {
    int loop = 0;
    while (loop++ < 3)
      Draw3dRect(dc,x+loop,y+loop,x+BUTTONCX,y+BUTTONCY,0);
    RECT rect = {0,0,39,39};
    DrawBitmapRect(dc,x+4,y+4,&rect,buttonbitmap[number]);
  }
  else {
    Draw3dRect(dc,x+1,y+1,x+BUTTONCX,y+BUTTONCY,1);
    Draw3dRect(dc,x+2,y+2,x+BUTTONCX-1,y+BUTTONCY-1,1);
    RECT rect = {1,1,40,40};
    DrawBitmapRect(dc,x+3,y+3,&rect,buttonbitmap[number]);
  }
  if ((number == BTN_DRIVE1) || (number == BTN_DRIVE2)) {
    int  offset = (number == buttondown) << 1;
    RECT rect = {x+offset+3,
      y+offset+31,
      x+offset+42,
      y+offset+42};
    SelectObject(dc,smallfont);
    SetTextColor(dc,RGB(0,0,0));
    SetTextAlign(dc,TA_CENTER | TA_TOP);
    SetBkMode(dc,TRANSPARENT);
    ExtTextOut(dc,x+offset+22,rect.top,ETO_CLIPPED,&rect,
        DiskGetName(number-BTN_DRIVE1),
        MIN(8,_tcslen(DiskGetName(number-BTN_DRIVE1))),
        NULL);
  }
  if (!passdc)
    ReleaseDC(g_hFrameWindow,dc);
}

//===========================================================================
void DrawCrosshairs (int x, int y) {
  static int lastx = 0;
  static int lasty = 0;
  FrameReleaseDC();
  HDC dc = GetDC(g_hFrameWindow);
#define LINE(x1,y1,x2,y2) MoveToEx(dc,x1,y1,NULL); LineTo(dc,x2,y2);

  // ERASE THE OLD CROSSHAIRS
  if (lastx && lasty)
    if (fullscreen) {
      int loop = 4;
      while (loop--) {
        RECT rect = {0,0,5,5};
        switch (loop) {
          case 0: OffsetRect(&rect,lastx-2,FSVIEWPORTY-5);           break;
          case 1: OffsetRect(&rect,lastx-2,FSVIEWPORTY+VIEWPORTCY);  break;
          case 2: OffsetRect(&rect,FSVIEWPORTX-5,         lasty-2);  break;
          case 3: OffsetRect(&rect,FSVIEWPORTX+VIEWPORTCX,lasty-2);  break;
        }
        FillRect(dc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
      }
    }
    else {
      int loop = 5;
      while (loop--) {
        switch (loop) {
          case 0: SelectObject(dc,GetStockObject(BLACK_PEN));  break;
          case 1: // fall through
          case 2: SelectObject(dc,btnshadowpen);               break;
          case 3: // fall through
          case 4: SelectObject(dc,btnfacepen);                 break;
        }
        LINE(lastx-2,VIEWPORTY-loop-1,
            lastx+3,VIEWPORTY-loop-1);
        LINE(VIEWPORTX-loop-1,lasty-2,
            VIEWPORTX-loop-1,lasty+3);
        if ((loop == 1) || (loop == 2))
          SelectObject(dc,btnhighlightpen);
        LINE(lastx-2,VIEWPORTY+VIEWPORTCY+loop,
            lastx+3,VIEWPORTY+VIEWPORTCY+loop);
        LINE(VIEWPORTX+VIEWPORTCX+loop,lasty-2,
            VIEWPORTX+VIEWPORTCX+loop,lasty+3);
      }
    }

  // DRAW THE NEW CROSSHAIRS
  if (x && y) {
    int loop = 4;
    while (loop--) {
      if ((loop == 1) || (loop == 2))
        SelectObject(dc,GetStockObject(WHITE_PEN));
      else
        SelectObject(dc,GetStockObject(BLACK_PEN));
      LINE(x+loop-2,viewporty-5,
          x+loop-2,viewporty);
      LINE(x+loop-2,viewporty+VIEWPORTCY+4,
          x+loop-2,viewporty+VIEWPORTCY-1);
      LINE(viewportx-5,           y+loop-2,
          viewportx,             y+loop-2);
      LINE(viewportx+VIEWPORTCX+4,y+loop-2,
          viewportx+VIEWPORTCX-1,y+loop-2);
    }
  }
#undef LINE
  lastx = x;
  lasty = y;
  ReleaseDC(g_hFrameWindow,dc);
}
*/

//===========================================================================
void DrawFrameWindow () {
  VideoRealizePalette(/*dc*/);
  //  printf("In DrawFrameWindow. g_nAppMode == %d\n", g_nAppMode);

  // DRAW THE STATUS AREA
  DrawStatusArea(DRAW_BACKGROUND | DRAW_LEDS);

  // DRAW THE CONTENTS OF THE EMULATED SCREEN
  if (g_nAppMode == MODE_LOGO)
    VideoDisplayLogo(); // logo
  else if (g_nAppMode == MODE_DEBUG)
    DebugDisplay(1);  //debugger
  else
    VideoRedrawScreen(); // normal state - running emulator?
  //  printf("Out of DrawFrameWindow!\n");
}

//===========================================================================
void DrawStatusArea (/*HDC passdc,*/ int drawflags)
{// status area not used now (yet?) --bb
  /*  FrameReleaseDC();
      HDC  dc     = (passdc ? passdc : GetDC(g_hFrameWindow));
      int  x      = buttonx;
      int  y      = buttony+BUTTONS*BUTTONCY+1;
      int  iDrive1Status = DISK_STATUS_OFF;
      int  iDrive2Status = DISK_STATUS_OFF;
      bool bCaps   = KeybGetCapsStatus();
      DiskGetLightStatus(&iDrive1Status,&iDrive2Status);

      if (fullscreen)
      {
      SelectObject(dc,smallfont);
      SetBkMode(dc,OPAQUE);
      SetBkColor(dc,RGB(0,0,0));
      SetTextAlign(dc,TA_LEFT | TA_TOP);
      SetTextColor(dc,RGB((iDrive1Status==2 ? 255 : 0),(iDrive1Status==1 ? 255 : 0),0));
      TextOut(dc,x+ 3,y+2,TEXT("1"),1);
      SetTextColor(dc,RGB((iDrive2Status==2 ? 255 : 0),(iDrive2Status==1 ? 255 : 0),0));
      TextOut(dc,x+13,y+2,TEXT("2"),1);
      if (!IS_APPLE2)
      {
      SetTextAlign(dc,TA_RIGHT | TA_TOP);
      SetTextColor(dc,(bCaps
      ? RGB(128,128,128)
      : RGB(  0,  0,  0) ));
      TextOut(dc,x+BUTTONCX,y+2,TEXT("Caps"),4);
      }
      SetTextAlign(dc,TA_CENTER | TA_TOP);
      SetTextColor(dc,(g_nAppMode == MODE_PAUSED || g_nAppMode == MODE_STEPPING
      ? RGB(255,255,255)
      : RGB(  0,  0,  0)));
      TextOut(dc,x+BUTTONCX/2,y+13,(g_nAppMode == MODE_PAUSED
      ? TITLE_PAUSED
      : TITLE_STEPPING) ,8);
      }
      else
      {*/
  if(font_sfc == NULL)
    if(!fonts_initialization()) {
      fprintf(stderr, "Font file was not loaded.\n");
      return;    //if we don't have a fonts, we just can do none
    }

  SDL_Rect srect;

  Uint32 mybluez = SDL_MapRGB(screen->format, 10, 10, 255);  // bluez color, know that?

  SDL_SetColors(g_hStatusSurface, screen->format->palette->colors,
      0, 256);
  //  Uint32 myblack  = SDL_MapRGB(screen->format, 0, 0, 0);  // black color
  //  SDL_SetColorKey(g_hStatusSurface,SDL_SRCCOLORKEY/* | SDL_RLEACCEL*/, myblack);

  if (drawflags & DRAW_BACKGROUND)
  {
    /* Code moved to Video.cpp in CreateDIBSections()
       srect.x = srect.y = 0;
       srect.w = STATUS_PANEL_W;
       srect.h = STATUS_PANEL_H;
       SDL_FillRect(g_hStatusSurface, &srect, mybluez);  // fill status panel
       rectangle(g_hStatusSurface, 0, 0, STATUS_PANEL_W - 1, STATUS_PANEL_H - 1, myyell);
       rectangle(g_hStatusSurface, 2, 2, STATUS_PANEL_W - 5, STATUS_PANEL_H - 5, myyell);
       font_print(7, 6, "FDD1", g_hStatusSurface, 1.3, 1.5); // show signs
       font_print(40, 6, "FDD2", g_hStatusSurface, 1.3, 1.5);
       font_print(74, 6, "HDD", g_hStatusSurface, 1.3, 1.5);
       */
    //    SDL_SetColors(g_hStatusSurface, screen->format->palette->colors,
    //               0, 256);
    g_iStatusCycle = SHOW_CYCLES;  // start cycle for panel showing
  }
  if (drawflags & DRAW_LEDS)
  {
    srect.x = 4;
    srect.y = 22;
    srect.w = STATUS_PANEL_W - 8;
    srect.h = STATUS_PANEL_H - 25;
    SDL_FillRect(g_hStatusSurface, &srect, mybluez);  // clear

    char leds[2] = "\x64";
#define LEDS  1
    int  iDrive1Status = DISK_STATUS_OFF;
    int  iDrive2Status = DISK_STATUS_OFF;
    int iHDDStatus = DISK_STATUS_OFF;

    //    bool bCaps   = KeybGetCapsStatus();
    DiskGetLightStatus(&iDrive1Status,&iDrive2Status);
    iHDDStatus = HD_GetStatus();

    leds[0] = LEDS + iDrive1Status;
    //    printf("Leds are %d\n",leds[0]);
    font_print(8, 23, leds, g_hStatusSurface, 4, 2.7);

    leds[0] = LEDS + iDrive2Status;
    font_print(40, 23, leds, g_hStatusSurface, 4, 2.7);

    leds[0] = LEDS + iHDDStatus;
    font_print(71, 23, leds, g_hStatusSurface, 4, 2.7);

    if(iDrive1Status | iDrive2Status | iHDDStatus) g_iStatusCycle = SHOW_CYCLES; // show status panel
  }
  //  surface_fader(g_hStatusSurface, nowleds, nowleds, nowleds, -1, 0);
  /*    if (drawflags & DRAW_TITLE)
        {
        TCHAR title[40];
        switch (g_Apple2Type)
        {
        case A2TYPE_APPLE2:      _tcscpy(title, TITLE_APPLE_2); break;
        case A2TYPE_APPLE2PLUS:    _tcscpy(title, TITLE_APPLE_2_PLUS); break;
        case A2TYPE_APPLE2E:    _tcscpy(title, TITLE_APPLE_2E); break;
        case A2TYPE_APPLE2EEHANCED:  _tcscpy(title, TITLE_APPLE_2E_ENHANCED); break;
        }

        switch (g_nAppMode)
        {
        case MODE_PAUSED  : _tcscat(title,TEXT(" [")); _tcscat(title,TITLE_PAUSED  ); _tcscat(title,TEXT("]")); break;
        case MODE_STEPPING: _tcscat(title,TEXT(" [")); _tcscat(title,TITLE_STEPPING); _tcscat(title,TEXT("]")); break;
        }

        SendMessage(g_hFrameWindow,WM_SETTEXT,0,(LPARAM)title);
        }
        if (drawflags & DRAW_BUTTON_DRIVES)
        {
        DrawButton(dc, BTN_DRIVE1);
        DrawButton(dc, BTN_DRIVE2);
        }
        }

        if (!passdc)
        ReleaseDC(g_hFrameWindow,dc);*/
}



/*
//===========================================================================
void EraseButton (int number) {
RECT rect;
rect.left   = buttonx;
rect.right  = rect.left+BUTTONCX;
rect.top    = buttony+number*BUTTONCY;
rect.bottom = rect.top+BUTTONCY;
InvalidateRect(g_hFrameWindow,&rect,1);
}
*/

void FrameShowHelpScreen(int sx, int sy) // sx, sy - sizes of current window (screen)
{
  // on pressing F1 button shows help screen

  const char * HelpStrings[] = {
    "Welcome to LinApple - Apple][ emulator for Linux!",
    "Conf file is linapple.conf in current directory by default",
    "Hugest archive of Apple][ stuff you can find at ftp.apple.asimov.net",
    " F1 - This help",
    " Ctrl+F2 - Cold reset",
    " Shift+F2 - Reload conf file and restart",
    " F3, F4 - Choose an image file name for floppy disk",
    "             in Slot 6 drive 1 or 2 respectively",
    " Shift+F3, Shift+F4 - The same thing for Apple hard disks",
    "                         (in Slot 7)",
    " F5 - Swap drives for Slot 6",
    " F6 - Toggle fullscreen mode",
    " F7 - Reserved for Debugger!",
    " F8 - Save current screen as a .bmp file",
    " Shift+F8 - Save settings changable at runtime in conf file",
    " F9 - Cycle through various video modes",
    " F10 - Quit emulator",
    " F11 - Save current state to file, Alt+F11 - quick save",
    " F12 - Reload it from file, Alt+F12 - quick load",
    " Ctrl+F12 - Hot reset",
    "  Pause - Pause emulator",
    "  Scroll Lock - Toggle full speed",
    "Num pad keys:",
    "  Grey + - Speed up emulator",
    "  Grey - - Speed it down",
    "  Grey * - Normal speed"
  };

  //   const int PositionsY[] = { 7, 15, 26 };

  SDL_Surface *my_screen;  // for background
  SDL_Surface *tempSurface = NULL;  // temporary surface

  if(font_sfc == NULL)
    if(!fonts_initialization()) {
      fprintf(stderr, "Font file was not loaded.\n");
      return;    //if we don't have a fonts, we just can do none
    }
  if(!g_WindowResized) {
    if(g_nAppMode == MODE_LOGO) tempSurface = g_hLogoBitmap;  // use logobitmap
    else tempSurface = g_hDeviceBitmap;
  }
  else tempSurface = g_origscreen;

  if(tempSurface == NULL) tempSurface = screen;  // use screen, if none available
  my_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w, tempSurface->h,
      tempSurface->format->BitsPerPixel, 0, 0, 0, 0);
  if(tempSurface->format->palette && my_screen->format->palette)
    SDL_SetColors(my_screen, tempSurface->format->palette->colors,
        0, tempSurface->format->palette->ncolors);

  surface_fader(my_screen, 0.2F, 0.2F, 0.2F, -1, 0);  // fade it out to 20% of normal
  SDL_BlitSurface(tempSurface, NULL, my_screen, NULL);

  SDL_BlitSurface(my_screen, NULL, screen, NULL);    // show background

  double facx = double(g_ScreenWidth) / double(SCREEN_WIDTH);
  double facy = double(g_ScreenHeight) / double(SCREEN_HEIGHT);

  font_print_centered(sx/2, int(5*facy), (char*)HelpStrings[0], screen, 1.5*facx, 1.3*facy);
  font_print_centered(sx/2, int(20*facy), (char*)HelpStrings[1], screen, 1.3*facx, 1.2*facy);
  font_print_centered(sx/2, int(30*facy), (char*)HelpStrings[2], screen, 1.2*facx, 1.0*facy);

  int Help_TopX = int(45*facy);
  int i;
  for(i =  3; i < 25; i++)
    font_print(4, Help_TopX + (i - 3) * 15 * facy, (char*)HelpStrings[i], screen, 1.5*facx, 1.5*facy); // show keys

  // show frames
  rectangle(screen, 0, Help_TopX - 5, /*SCREEN_WIDTH*/g_ScreenWidth - 1, int(335*facy), SDL_MapRGB(screen->format, 255, 255, 255));
  rectangle(screen, 1, Help_TopX - 4, /*SCREEN_WIDTH*/g_ScreenWidth, int(335*facy), SDL_MapRGB(screen->format, 255, 255, 255));

  rectangle(screen, 1, 1, /*SCREEN_WIDTH*/g_ScreenWidth - 2, (Help_TopX - 8), SDL_MapRGB(screen->format, 255, 255, 0));

  if(assets->icon != NULL) {  // display Apple logo
    tempSurface = SDL_DisplayFormat(assets->icon);
    SDL_Rect logo, scrr;
    logo.x = logo.y = 0;
    logo.w = tempSurface->w;
    logo.h = tempSurface->h;
    scrr.x = int(460*facx);
    scrr.y = int(270*facy);
    scrr.w = scrr.h = int(100*facy);
    SDL_SoftStretchOr(tempSurface, &logo, screen, &scrr);
  }

  SDL_Flip(screen);  // show the screen
  SDL_Delay(1000);  // wait 1 second to be not too fast

  //////////////////////////////////
  // Wait for keypress
  //////////////////////////////////
  SDL_Event event;  // event

  event.type = SDL_QUIT;
  while(event.type != SDL_KEYDOWN /*&& event.key.keysym.sym != SDLK_ESCAPE*/) {// wait for ESC-key pressed
    usleep(100);
    SDL_PollEvent(&event);
  }

  DrawFrameWindow(); // restore screen
}


void FrameQuickState(int num, int mod)
{
  // quick load or save state with number num,
  // if Shift is pressed, state is being saved,
  // otherwise - being loaded
  char fpath[MAX_PATH];
  snprintf(fpath, MAX_PATH, "%s/SaveState%d.aws", g_sSaveStateDir, num); // prepare file name
  Snapshot_SetFilename(fpath);  // set it as a working name
  if(mod & KMOD_SHIFT)  Snapshot_SaveState();
  else    Snapshot_LoadState();
}




//===========================================================================
/*LRESULT CALLBACK FrameWndProc (
  HWND   window,
  UINT   message,
  WPARAM wparam,
  LPARAM lparam)*/
void  FrameDispatchMessage(SDL_Event * e) // process given SDL event
{
  int mysym = e->key.keysym.sym; // keycode
  int mymod = e->key.keysym.mod; // some special keys flags
  int x,y;  // used for mouse cursor position

  switch (e->type) //type of SDL event
  {
    case SDL_VIDEORESIZE:
      printf("OLD DIMENSIONS: %d  %d\n", g_ScreenWidth, g_ScreenHeight);
      g_ScreenWidth = e->resize.w;
      g_ScreenHeight = (e->resize.h / 96) * 96;
      if( g_ScreenHeight < 192 ) {
        g_ScreenHeight = 192;
      }
      //Resize the screen
      screen = SDL_SetVideoMode( e->resize.w, e->resize.h, SCREEN_BPP, SDL_SWSURFACE | SDL_HWPALETTE | SDL_RESIZABLE );
      if( screen == NULL ) {
        SDL_Quit();
        return;
      } else {
        // define if we have resized window
        g_WindowResized = (g_ScreenWidth != SCREEN_WIDTH) | (g_ScreenHeight != SCREEN_HEIGHT);
        printf("Screen size is %dx%d\n",g_ScreenWidth, g_ScreenHeight);
        if(g_WindowResized) {
          // create rects for screen stretching
          origRect.x = origRect.y = newRect.x = newRect.y = 0;
          origRect.w = SCREEN_WIDTH;
          origRect.h = SCREEN_HEIGHT;
          newRect.w = g_ScreenWidth;
          newRect.h = g_ScreenHeight;
          if ((g_nAppMode != MODE_LOGO) && (g_nAppMode != MODE_DEBUG))
            VideoRedrawScreen();
        }
      }
      break;

    case SDL_ACTIVEEVENT:
      g_bAppActive = e->active.gain; // if gain==1, app is active
      break;

    case SDL_KEYDOWN:
      //      printf("keyb %d is down!\n", mysym);
      if(mysym >= SDLK_0 && mysym <= SDLK_9 && mymod & KMOD_CTRL) {
        FrameQuickState(mysym - SDLK_0, mymod);
        break;
      }

      if(mysym < 128 && mysym != SDLK_PAUSE) { // it should be ASCII code?
        if ((g_nAppMode == MODE_RUNNING) || (g_nAppMode == MODE_LOGO) ||
            ((g_nAppMode == MODE_STEPPING) && (mysym != SDLK_ESCAPE)))
        {
          KeybQueueKeypress(mysym,ASCII);
        }
        else
          if ((g_nAppMode == MODE_DEBUG) || (g_nAppMode == MODE_STEPPING))
          {
            DebuggerInputConsoleChar(mysym);
          }
        break;
      }
      else {// this is function key?
        //    KeybUpdateCtrlShiftStatus(); // if ctrl or shift or alt was pressed?------?
        if ((mysym >= SDLK_F1) && (mysym <= SDLK_F12) && (buttondown == -1))
        {
          SetUsingCursor(0);  //-- for what purpose???
          buttondown = mysym - SDLK_F1;  // special function keys processing

          /*      if (fullscreen && (buttonover != -1)) {
                  if (buttonover != buttondown)
                  EraseButton(buttonover);
                  buttonover = -1;
                  }
                  DrawButton((HDC)0,buttondown);*/
        }
        else if (mysym == SDLK_KP_PLUS) // Gray + - speed up the emulator!
        {
          g_dwSpeed = g_dwSpeed + 2;
          if(g_dwSpeed > SPEED_MAX) g_dwSpeed = SPEED_MAX; // no Maximum tresspassing!
          printf("Now speed=%d\n", (int)g_dwSpeed);
          SetCurrentCLK6502();
        }
        else if (mysym == SDLK_KP_MINUS) // Gray + - speed up the emulator!
        {
          if(g_dwSpeed > SPEED_MIN) g_dwSpeed = g_dwSpeed - 1;// dw is unsigned value!
          //if(g_dwSpeed <= SPEED_MIN) g_dwSpeed = SPEED_MIN; // no Minimum tresspassing!
          printf("Now speed=%d\n", (int)g_dwSpeed);
          SetCurrentCLK6502();
        }
        else if (mysym == SDLK_KP_MULTIPLY) // Gray * - normal speed!
        {
          g_dwSpeed = 10;// dw is unsigned value!
          printf("Now speed=%d\n", (int)g_dwSpeed);
          SetCurrentCLK6502();
        }


        else if (mysym == SDLK_CAPSLOCK) // CapsLock
        {
          KeybToggleCapsLock();
        }
        else if (mysym == SDLK_PAUSE)  // Pause - let us pause all things for the best
        {
          SetUsingCursor(0); // release cursor?
          switch (g_nAppMode)
          {
            case MODE_RUNNING: // go in pause
              g_nAppMode = MODE_PAUSED;
              SoundCore_SetFade(FADE_OUT); // fade out sound?**************
              break;
            case MODE_PAUSED: // go to the normal mode?
              g_nAppMode = MODE_RUNNING;
              SoundCore_SetFade(FADE_IN);  // fade in sound?***************
              break;
            case MODE_STEPPING:
              DebuggerInputConsoleChar( DEBUG_EXIT_KEY );
              break;
            case MODE_LOGO:
            case MODE_DEBUG:
            default:
              break;
          }
          DrawStatusArea(/*(HDC)0,*/DRAW_TITLE);
          if ((g_nAppMode != MODE_LOGO) && (g_nAppMode != MODE_DEBUG))
            VideoRedrawScreen();
          g_bResetTiming = true;
        }
        else if (mysym == SDLK_SCROLLOCK)  // SCROLL LOCK pressed
        {
          g_bScrollLock_FullSpeed = !g_bScrollLock_FullSpeed; // turn on/off full speed?
        }
        else if ((g_nAppMode == MODE_RUNNING) || (g_nAppMode == MODE_LOGO) || (g_nAppMode == MODE_STEPPING))
        {
          // Note about Alt Gr (Right-Alt):
          // . WM_KEYDOWN[Left-Control], then:
          // . WM_KEYDOWN[Right-Alt]
          BOOL autorep  = 0; //previous key was pressed? 30bit of lparam
          BOOL extended = (mysym >= 273); // 24bit of lparam - is an extended key, what is it???
          if ((!JoyProcessKey(mysym ,extended, 1, autorep)) && (g_nAppMode != MODE_LOGO))
            KeybQueueKeypress(mysym, NOT_ASCII);
        }
        else if (g_nAppMode == MODE_DEBUG)
          DebuggerProcessKey(mysym);  // someone should realize debugger for Linapple!?--bb
        /*
           if (wparam == VK_F10)
           {
           SetUsingCursor(0);
           return 0;
           }
           break;*/
      }//else
      break;

    case SDL_KEYUP:
      //  int mysym = e->key.keysym.sym; // keycode
      if ((mysym >= SDLK_F1) && (mysym <= SDLK_F12) && (buttondown == mysym-SDLK_F1))
      {
        buttondown = -1;
        //       if (fullscreen)
        //         EraseButton(wparam-VK_F1);
        //       else
        //         DrawButton((HDC)0,wparam-VK_F1);
        ProcessButtonClick(mysym-SDLK_F1, mymod); // process function keys - special events
      } else if (mysym == SDLK_CAPSLOCK) {
        // GPH Fix caps lock toggle behavior.
        // (http://sdl.beuc.net/sdl.wiki/SDL_KeyboardEvent)
        KeybToggleCapsLock();
      } else {  // mysym >= 300 (or 273????)- check for extended key, what is it EXACTLY???
        JoyProcessKey(mysym,(mysym >= 273), 0, 0);
      }
      break;

    case SDL_MOUSEBUTTONDOWN:
      if(e->button.button == SDL_BUTTON_LEFT) {// left mouse button was pressed
        if (buttondown == -1)
        {
          x = e->button.x; // mouse cursor coordinates
          y = e->button.y;
          if (usingcursor) // we use mouse cursor for our special needs?
          {
            KeybUpdateCtrlShiftStatus(); // if either of ALT, SHIFT or CTRL is pressed
            if (g_bShiftKey | g_bCtrlKey)
            {
              SetUsingCursor(0); // release mouse cursor for user
            }
            else
            {
              if (sg_Mouse.Active())
                sg_Mouse.SetButton(BUTTON0, BUTTON_DOWN);
              else
                JoySetButton(BUTTON0, BUTTON_DOWN);
            }
          }// we do not use mouse
          else if ( (/*(x < buttonx) && JoyUsingMouse() && */((g_nAppMode == MODE_RUNNING) ||
                  (g_nAppMode == MODE_STEPPING))) || (sg_Mouse.Active()) )
          {
            SetUsingCursor(1); // capture cursor
          }
          DebuggerMouseClick( x, y );
        }
        //RelayEvent(WM_LBUTTONDOWN,wparam,lparam);
      }//if left mouse button down
      else if(e->button.button == SDL_BUTTON_RIGHT) {
        if (usingcursor)
        {
          if (sg_Mouse.Active())
            sg_Mouse.SetButton(BUTTON1, BUTTON_DOWN);
          else
            JoySetButton(BUTTON1, BUTTON_DOWN);
        }
      }

      break; // end of MOSEBUTTONDOWN event

    case SDL_MOUSEBUTTONUP:
      if (e->button.button == SDL_BUTTON_LEFT) {// left mouse button was released
        if (usingcursor)
        {
          if (sg_Mouse.Active())
            sg_Mouse.SetButton(BUTTON0, BUTTON_UP);
          else
            JoySetButton(BUTTON0, BUTTON_UP);
        }
        //      RelayEvent(WM_LBUTTONUP,wparam,lparam);
      }
      else if(e->button.button == SDL_BUTTON_RIGHT) {
        if (usingcursor)
        {
          if (sg_Mouse.Active())
            sg_Mouse.SetButton(BUTTON1, BUTTON_UP);
          else
            JoySetButton(BUTTON1, BUTTON_UP);
        }
      }
      break; // MOUSEBUTTONUP event

    case SDL_MOUSEMOTION:
      x = e->motion.x;// get relative coordinates of mouse cursor
      y = e->motion.y;
      if (usingcursor)
      {
        //        DrawCrosshairs(x,y); I do not like those crosshairs, but... --bb
        if (sg_Mouse.Active())
          sg_Mouse.SetPosition(x, VIEWPORTCX-4, y, VIEWPORTCY-4);
        else
          JoySetPosition(x, VIEWPORTCX-4, y, VIEWPORTCY-4);
      }
      //      RelayEvent(WM_MOUSEMOVE,wparam,lparam);
      break;

    case SDL_USEREVENT:
      if (e->user.code == 1) // should do restart?
        ProcessButtonClick(BTN_RUN, KMOD_LCTRL);
      break;

  }//switch

  //  return DefWindowProc(window,message,wparam,lparam);
}


bool PSP_SaveStateSelectImage(bool saveit)
{
  // Dialog for save or load StateImage
  // if saveit == TRUE, then pick image for saving
  //  else pick an image for loading
  static int findex = 0;    // file index will be remembered for current dir
  static int backdx = 0;  //reserve
  static int dirdx  = 0;  // reserve for dirs

  char * filename = NULL;      // given filename
  char fullpath[MAX_PATH];  // full path for it
  char tmppath [MAX_PATH];
  bool isdir;      // if given filename is a directory?

  findex = backdx;
  isdir = true;
  strcpy(fullpath, g_sSaveStateDir);  // global var for disk selecting directory

  while(isdir)
  {
    if(!ChooseAnImage(/*SCREEN_WIDTH*/g_ScreenWidth, /*SCREEN_HEIGHT*/g_ScreenHeight, fullpath, saveit, &filename, &isdir, &findex)) {
      DrawFrameWindow();
      return false;  // if ESC was pressed, just leave
    }
    //   strcpy(filename, pszFilename);
    //    printf("We got next:\n");
    //    printf("isdir=%d, findex=%d, filename=%s\n", isdir, findex, filename);
    if(isdir)
    {

      if(!strcmp(filename, ".."))  // go to the upper directory
      {
        filename = strrchr(fullpath, FILE_SEPARATOR); // look for last '/'
        if(filename) *filename = '\0';  // cut it off
        if(strlen(fullpath) == 0) strcpy(fullpath,"/");  //we don't want fullpath to be empty
        findex = dirdx;  // restore

      }
      else
      {
        if(strcmp(fullpath, "/")) snprintf(tmppath, MAX_PATH, "%s/%s", fullpath, filename); // next dir
        else snprintf(tmppath, MAX_PATH, "/%s", filename);
        strcpy(fullpath, tmppath);  // got ot anew
        //        printf("We build %s\n", tmppath);
        dirdx = findex; // store it
        findex = 0;  // start with beginning of dir
      }
    }/* if isdir */
  } /* while isdir */
  strcpy(g_sSaveStateDir, fullpath);
  RegSaveString(TEXT("Preferences"),REGVALUE_PREF_SAVESTATE_DIR, 1, g_sSaveStateDir);// save it

  backdx = findex;  //store cursor position

  snprintf(tmppath, MAX_PATH, "%s/%s", fullpath, filename); // next dir
  strcpy(fullpath, tmppath);  // got ot anew

  Snapshot_SetFilename(fullpath);  // set name for snapshot
  RegSaveString(TEXT("Preferences"),REGVALUE_SAVESTATE_FILENAME, 1, fullpath);// save it
  DrawFrameWindow();
  return true;
}

void FrameSaveBMP(void)
{
  // Save current screen as a .bmp file in current directory
  struct stat bufp;
  static int i = 1;  // index
  char bmpname[20];  // file name

  snprintf(bmpname, 20, "linapple%d.bmp", i);
  while(!stat(bmpname, &bufp)) {  // find first absent file
    i++;
    snprintf(bmpname, 20, "linapple%d.bmp", i);
  }
  SDL_SaveBMP(screen, bmpname);  // save file using SDL inner function
  printf("File %s saved!\n", bmpname);
  i++;
}


//===========================================================================
void ProcessButtonClick (int button, int mod) {
  // button - number of button pressed (starting with 0, which means F1
  // mod - what modifiers been set (like CTRL, ALT etc.)
  SDL_Event qe;  // for Quitting and Reset

  SoundCore_SetFade(FADE_OUT); // sound/music off?

  switch (button) {

    case BTN_HELP:  // will get some help on the screen?
      FrameShowHelpScreen(screen->w, screen->h);

      //         TCHAR filename[MAX_PATH];
      //         _tcscpy(filename,g_sProgramDir);
      //         _tcscat(filename,TEXT("APPLEWIN.CHM"));
      //         HtmlHelp(g_hFrameWindow,filename,HH_DISPLAY_TOC,0);
      //         helpquit = 1;
      break;

    case BTN_RUN:  // F2 - Run that thing! Or Shift+2 ReloadConfig and run it anyway!
      if((mod & (KMOD_LCTRL)) == (KMOD_LCTRL) ||
          (mod & (KMOD_RCTRL)) == (KMOD_RCTRL))  {
        if (g_nAppMode == MODE_LOGO)
          DiskBoot();
        else if (g_nAppMode == MODE_RUNNING)
          ResetMachineState();
        if ((g_nAppMode == MODE_DEBUG) || (g_nAppMode == MODE_STEPPING))
          DebugEnd();
        g_nAppMode = MODE_RUNNING;
        DrawStatusArea(/*(HDC)0,*/DRAW_TITLE);
        VideoRedrawScreen();
        g_bResetTiming = true;
      }
      else if(mod & KMOD_SHIFT) {
        restart = 1;  // keep up flag of restarting
        qe.type = SDL_QUIT;
        SDL_PushEvent(&qe);// push quit event
      }
      break;

    case BTN_DRIVE1:
    case BTN_DRIVE2:
      if (mod & KMOD_SHIFT) {
        if(mod & KMOD_ALT)
          HD_FTP_Select(button - BTN_DRIVE1);// select HDV image through FTP
        else
          HD_Select(button - BTN_DRIVE1);  // select HDV image from local disk
      } else {
        if(mod & KMOD_ALT)
          Disk_FTP_SelectImage(button - BTN_DRIVE1);//select through FTP
        else
          DiskSelect(button - BTN_DRIVE1); // select image file for appropriate disk drive(#1 or #2)
      }
      /*      if (!fullscreen)
              DrawButton((HDC)0,button);*/
      break;

    case BTN_DRIVESWAP:  // F5 - swap disk drives
      DiskDriveSwap();
      break;

    case BTN_FULLSCR:  // F6 - Fullscreen on/off
      if (fullscreen) { fullscreen = 0;
        SetNormalMode(); }
      else { fullscreen = 1;
        SetFullScreenMode();}
      break;

    case BTN_DEBUG:  // F7 - debug mode - not implemented yet? Please, see README about it. --bb
      /*    if (g_nAppMode == MODE_LOGO)
            {
            ResetMachineState();
            }

            if (g_nAppMode == MODE_STEPPING)
            {
            DebuggerInputConsoleChar( DEBUG_EXIT_KEY );
            }
            else
            if (g_nAppMode == MODE_DEBUG)
            {
            g_bDebugDelayBreakCheck = true;
            ProcessButtonClick(BTN_RUN);
            }
            else
            {
            DebugBegin();
            }*/
      break;

    case BTN_SETUP:  // setup is in conf file - linapple.conf.
      // may be it should be implemented using SDL??? 0_0 --bb
      // Now Shift-F8 save settings changed run-tme in linapple.conf
      // F8 - save current screen as a .bmp file
      // Currently these setting are just next:
      if(mod & KMOD_SHIFT) {
        RegSaveValue(TEXT("Configuration"),TEXT("Video Emulation"),1,g_videotype);
        RegSaveValue(TEXT("Configuration"),TEXT("Emulation Speed"),1,g_dwSpeed);
        RegSaveValue(TEXT("Configuration"),TEXT("Fullscreen"),1,fullscreen);
      }
      else {
        FrameSaveBMP();
      }

      //      {
      //      PSP_Init();
      //}
      break;


      ////////////////////////// my buttons handlers F9..F12 ////////////////////////////
    case BTN_CYCLE: // F9 - CYCLE through allowed video modes
      //    printf("F9 has been pressed!\n");
      g_videotype++;  // Cycle through available video modes
      if (g_videotype >= VT_NUM_MODES)
        g_videotype = 0;
      VideoReinitialize();
      if ((g_nAppMode != MODE_LOGO) || ((g_nAppMode == MODE_DEBUG) && (g_bDebuggerViewingAppleOutput))) // +PATCH
      {
        VideoRedrawScreen();
        g_bDebuggerViewingAppleOutput = true;  // +PATCH
      }

      break;
    case BTN_QUIT:  // F10 - exit from emulator?

      qe.type = SDL_QUIT;
      SDL_PushEvent(&qe);// push quit event
      break;  //

    case BTN_SAVEST:  // Save state (F11)
      if(mod & KMOD_ALT) { // quick save
        Snapshot_SaveState();
      }
      else
        if(PSP_SaveStateSelectImage(true))
        {
          Snapshot_SaveState();
        }
      break;
    case BTN_LOADST:  // Load state (F12) or Hot Reset (Ctrl+F12)
      if(mod & KMOD_CTRL) {
        // Ctrl+Reset
        if (!IS_APPLE2)
          MemResetPaging();

        DiskReset();
        KeybReset();
        if (!IS_APPLE2)
          VideoResetState();  // Switch Alternate char set off
        MB_Reset();
        CpuReset();
      }
      else if(mod & KMOD_ALT)  // quick load state
      {
        Snapshot_LoadState();
      }
      else if(PSP_SaveStateSelectImage(false))
      {
        Snapshot_LoadState();
      }
      break;
  }//switch (button)
  //////////////////////////////////////////// end of my buttons handlers //////////////////

  if((g_nAppMode != MODE_DEBUG) && (g_nAppMode != MODE_PAUSED))
  {
    SoundCore_SetFade(FADE_IN);
  }
}

//===========================================================================
void ResetMachineState () {
  DiskReset();    // Set floppymotoron=0
  g_bFullSpeed = 0;  // Might've hit reset in middle of InternalCpuExecute() - so beep may get (partially) muted

  MemReset();
  DiskBoot();
  VideoResetState();
  sg_SSC.CommReset();
  PrintReset();
  JoyReset();
  MB_Reset();
  SpkrReset();
  //  SoundCore_SetFade(FADE_NONE);
}


//===========================================================================
static bool bIamFullScreened;  // for correct fullscreen switching

void SetFullScreenMode () {
  // It is simple, as almost everything in SDL. Thank you, Sam Lantinga. My appreciation! ^_^ --bb

  if(!bIamFullScreened) {
    bIamFullScreened = true;
    /*fullscreen =*/ SDL_WM_ToggleFullScreen(screen);
    //if(fullscreen) // we are in full screen disable mouse cursor
    SDL_ShowCursor(SDL_DISABLE);
  }
}

//===========================================================================
void SetNormalMode () {
  // It is simple, as almost everything in SDL. Thank you, Sam Lantinga. My appreciation! ^_^ --bb

  if(bIamFullScreened) {
    bIamFullScreened = 0;
    SDL_WM_ToggleFullScreen(screen);// we should go back anyway!? ^_^  --bb
    if(!usingcursor) SDL_ShowCursor(SDL_ENABLE); // show mouse cursor if not use it
  }
}

//===========================================================================
void SetUsingCursor (BOOL newvalue) {
  //  if (newvalue == usingcursor)
  //return;
  usingcursor = newvalue;
  if (usingcursor) {// hide mouse cursor and grab input (mouse and keyboard)
    SDL_ShowCursor(SDL_DISABLE);
    SDL_WM_GrabInput(SDL_GRAB_ON);
  }
  else {// on the contrary - show mouse cursor and ungrab input
    if(!bIamFullScreened)  SDL_ShowCursor(SDL_ENABLE);  // show cursor if not in fullscreen mode
    SDL_WM_GrabInput(SDL_GRAB_OFF);
  }
}

//
// ----- ALL GLOBALLY ACCESSIBLE FUNCTIONS ARE BELOW THIS LINE -----
//

//===========================================================================
int FrameCreateWindow ()
{
  ////************** Init SDL and create window screen
  //   int xpos;
  //   if (!RegLoadValue(TEXT("Preferences"),TEXT("Window X-Position"),1,(DWORD *)&xpos))
  //     xpos = (GetSystemMetrics(SM_CXSCREEN)-width) >> 1;
  //   int ypos;
  //   if (!RegLoadValue(TEXT("Preferences"),TEXT("Window Y-Position"),1,(DWORD *)&ypos))
  //     ypos = (GetSystemMetrics(SM_CYSCREEN)-height) >> 1;

  static char sdlCmd[] = "SDL_VIDEO_CENTERED=center";
  SDL_putenv(sdlCmd); //center our window

  bIamFullScreened = false; // at startup not in fullscreen mode
  screen = SDL_SetVideoMode(g_ScreenWidth, g_ScreenHeight, SCREEN_BPP, SDL_SWSURFACE | SDL_HWPALETTE);
  if (screen == NULL) {
    fprintf(stderr, "Could not set SDL video mode: %s\n", SDL_GetError());
    SDL_Quit();
    return 1;
  }//if

  // define if we have resized window
  g_WindowResized = (g_ScreenWidth != SCREEN_WIDTH) | (g_ScreenHeight != SCREEN_HEIGHT);
  printf("Screen size is %dx%d\n",g_ScreenWidth, g_ScreenHeight);
  if(g_WindowResized) {
    // create rects for screen stretching
    origRect.x = origRect.y = newRect.x = newRect.y = 0;
    origRect.w = SCREEN_WIDTH;
    origRect.h = SCREEN_HEIGHT;
    newRect.w = g_ScreenWidth;
    newRect.h = g_ScreenHeight;
  }
  // let us use keyrepeat?
  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

  //
  //  SDL_WM_SetCaption(g_pAppTitle, g_pAppTitle); // set caption for our window screen
  return 0;
}

int InitSDL()
{
  // initialize SDL subsystems, return 0 if all OK, else return 1
  if(SDL_Init(SDL_INIT_EVERYTHING) != 0){
    fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
    return 1;
  }//if
  //////////////////////////////////////////////////////////////////////
  // SDL ref: Icon should be set *before* the first call to SDL_SetVideoMode.
  //  Uint32          colorkey;

  /*  assets->icon = SDL_CreateRGBSurfaceFrom((void*)Apple_icon, 32, 32, 8, 32, 0, 0, 0, 0);
      Uint32 colorkey = SDL_MapRGB(assets->icon->format, 0, 0, 0);
      SDL_SetColorKey(assets->icon, SDL_SRCCOLORKEY, colorkey);
      SDL_WM_SetIcon(assets->icon, NULL);
      printf("Icon was set! Width=%d, height=%d\n", assets->icon->w, assets->icon->h);*/

  if(assets->icon != NULL) {
    Uint32 colorkey = SDL_MapRGB(assets->icon->format, 0, 0, 0);
    SDL_SetColorKey(assets->icon, SDL_SRCCOLORKEY, colorkey);
    SDL_WM_SetIcon(assets->icon, NULL);
    //    printf("Icon was set! Width=%d, height=%d\n", assets->icon->w, assets->icon->h);
  }
  //////////////////////////////////////////////////////////////////////
  return 0;
}
//===========================================================================

/*HDC FrameGetDC () {
  if (!g_hFrameDC) {
  g_hFrameDC = GetDC(g_hFrameWindow);
  SetViewportOrgEx(g_hFrameDC,viewportx,viewporty,NULL);
  }
  return g_hFrameDC;
  }

//===========================================================================
HDC FrameGetVideoDC (LPBYTE *addr, LONG *pitch) {
if (fullscreen && g_bAppActive && !painting) {
RECT rect = {FSVIEWPORTX,
FSVIEWPORTY,
FSVIEWPORTX+VIEWPORTCX,
FSVIEWPORTY+VIEWPORTCY};
DDSURFACEDESC surfacedesc;
surfacedesc.dwSize = sizeof(surfacedesc);
if (surface->Lock(&rect,&surfacedesc,0,NULL) == DDERR_SURFACELOST) {
surface->Restore();
surface->Lock(&rect,&surfacedesc,0,NULL);
}
 *addr  = (LPBYTE)surfacedesc.lpSurface+(VIEWPORTCY-1)*surfacedesc.lPitch;
 *pitch = -surfacedesc.lPitch;
 return (HDC)0;
 }
 else return FrameGetDC();
 }*/

//===========================================================================
void FrameRefreshStatus (int drawflags) {
  DrawStatusArea(/*(HDC)0,*/drawflags);
}

// //===========================================================================
// void FrameRegisterClass () {
//   WNDCLASSEX wndclass;
//   ZeroMemory(&wndclass,sizeof(WNDCLASSEX));
//   wndclass.cbSize        = sizeof(WNDCLASSEX);
//   wndclass.style         = CS_OWNDC | CS_BYTEALIGNCLIENT;
//   wndclass.lpfnWndProc   = FrameWndProc;
//   wndclass.hInstance     = g_hInstance;
//   wndclass.hIcon         = LoadIcon(g_hInstance,TEXT("APPLEWIN_ICON"));
//   wndclass.hCursor       = LoadCursor(0,IDC_ARROW);
//   wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
// #if ENABLE_MENU
//   wndclass.lpszMenuName   = (LPCSTR)IDR_MENU1;
// #endif
//   wndclass.lpszClassName = TEXT("APPLE2FRAME");
//   wndclass.hIconSm       = (HICON)LoadImage(g_hInstance,TEXT("APPLEWIN_ICON"),
//                                             IMAGE_ICON,16,16,LR_DEFAULTCOLOR);
//   RegisterClassEx(&wndclass);
// }

//===========================================================================
// void FrameReleaseDC () {
//   if (g_hFrameDC) {
//     SetViewportOrgEx(g_hFrameDC,0,0,NULL);
//     ReleaseDC(g_hFrameWindow,g_hFrameDC);
//     g_hFrameDC = (HDC)0;
//   }
// }
//
// //===========================================================================
// void FrameReleaseVideoDC () {
//   if (fullscreen && g_bAppActive && !painting) {
//
//     // THIS IS CORRECT ACCORDING TO THE DIRECTDRAW DOCS
//     RECT rect = {FSVIEWPORTX,
//                  FSVIEWPORTY,
//                  FSVIEWPORTX+VIEWPORTCX,
//                  FSVIEWPORTY+VIEWPORTCY};
//     surface->Unlock(&rect);
//
//     // BUT THIS SEEMS TO BE WORKING
//     surface->Unlock(NULL);
//   }
// }