DesktopFE 35.9 KB
Newer Older
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
REM > ChangeFSI
REM
REM Copyright 2016 Castle Technology Ltd
REM
REM Licensed under the Apache License, Version 2.0 (the "License");
REM you may not use this file except in compliance with the License.
REM You may obtain a copy of the License at
REM
REM     http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
REM
REM
REM This program written by Roger Wilson at Acorn Computers Apr 1989-Jun 1994
REM and by Sophie Wilson Aug 1994-
REM - please send bugs back!
REM Subject to change without notice
REM
REM All information is held in 32 bit fixed point with the . at bit 28
REM this gives an integer range of 0-15 (OR negative numbers!)
REM
REM ChangeFSI is written so as to allow use from CLI and from other BASIC
REM programs such as WIMP front ends, plus use of its own wimp front end.
REM
areanumber%=0
SYS "OS_GetEnv" TO A$
REM Read CLI string
task%=INSTR(A$," -wimp")
SYS "OS_SWINumberFromString",,"OS_ReadMonotonicTime" TO Time%
SYS "OS_SWINumberFromString",,"ColourTrans_ReturnColourNumber" TO ctran%
IF task% THEN
  DIM z%(1),p%(3),s%(3)
  SYS"MessageTrans_FileInfo",,"<ChangeFSI$Dir>.Messages" TO,,S%
  DIM msgb% 16,msgf% S%
  SYS"MessageTrans_OpenFile",msgb%,"<ChangeFSI$Dir>.Messages",msgf%
  startfile%=RIGHT$(A$,6)<>"-wimp "
  DIM taskid% 4,taskmsg% 4
  $taskid%="TASK"
  !taskmsg%=0:REM All messages
  SYS "Wimp_Initialise",310,!taskid%,FNmsg0("_TaskName"),taskmsg% TO version%
  IF version%>=350 THEN
    SYS "XOS_Memory",8+(2<<8) TO ,r1,r2;F%
    IF F% AND 1 vramsize%=0 ELSE vramsize%=r1*r2
    IF vramsize%>0 vram%= TRUE ELSE vram%= FALSE
  ELSE
    vram%= FALSE
  ENDIF
  Wimp    = (1<<18) + (3<<6)
  CreateW = Wimp+1
  OpenW   = Wimp+5
  CloseW  = Wimp+6
  RedrawW = Wimp+8
  UpdateW = Wimp+9
  GetR%   = Wimp+10
  GetW    = Wimp+11
  SetI    = Wimp+13
  GetI    = Wimp+14
  GetP    = Wimp+15
  Drag    = Wimp+16
  CrMenu  = Wimp+20
  DcMenu  = Wimp+21
  SYS "OS_SWINumberFromString",,"Wimp_PollIdle" TO Poll%
  SYS "OS_SWINumberFromString",,"Wimp_SetCaretPosition" TO Caretpos%
  SYS "OS_SWINumberFromString",,"Wimp_GetCaretPosition" TO Getcaret%
  SYS "XOS_SWINumberFromString",,"DragASprite_Start" TO dstart%;f%
  IF f% AND 1 dstart%=0
  SYS "XOS_SWINumberFromString",,"JPEG_PlotScaled" TO jpegpaint%;f%
  IF f% AND 1 jpegpaint%=0
  SYS "OS_Byte",161,&1C TO ,,f%
  IF (f% AND 2)=0 dstart%=0
  nextlocation%=HIMEM
  SYS "Wimp_SlotSize",-1,-1 TO originalslot%
  PROClocale_initialise
  IF version%>=300 THEN
    DIM q% 255,ifactors% 15,factors% 15,pixtrans% 255,paltemp% 1023,buffer% 255,idata% 1023
  ELSE
    DIM z% 1699,q% 255,ifactors% 15,factors% 15,pixtrans% 255,paltemp% 1023,buffer% 255,idata% 2399
    eidata%=idata%+2400
  ENDIF
  scrap%= FALSE
  sprite%= FALSE
  sar%= FALSE
  saved%= TRUE
  isopen%= FALSE
  lastm%= 0
  z$= STRING$(12,CHR$13)
  SYS "OS_Byte",161,&8C TO ,,f%
  IF f% AND 1 THEN
    SYS "Wimp_OpenTemplate",,"<ChangeFSI$Dir>.Template3d"
  ELSE
    SYS "Wimp_OpenTemplate",,"<ChangeFSI$Dir>.Templates"
  ENDIF
  LOCAL ERROR
  ON ERROR SYS "Wimp_CloseTemplate":ERROR 0,REPORT$+" (code "+ STR$ERL+")"
  IF version%>=300 THEN
    DIM z% FNlargest_buffer
    indsize%=FNsize_indirect("Info")
    DIM info_wind% indsize%
    SYS "Wimp_LoadTemplate",,z%,info_wind%,info_wind%+indsize%,-1,"Info"+z$,0
  ELSE
    SYS "Wimp_LoadTemplate",,z%,idata%,eidata%,-1,"Info"+z$,0 TO ,,idata%
  ENDIF
  SYS CreateW,,z% TO infow%
  $!(z%+88+7*32+20)=FNmsg0("_Version")
  PROCdotemplate("Processing")
  SYS CreateW,,z% TO proc%
  p%(0)=!(z%+88+8*32+20)
  p%(1)=!(z%+88+9*32+20)
  p%(2)=!(z%+88+10*32+20)
  p%(3)=!(z%+88+11*32+20)
  PROCdotemplate("Output")
  SYS CreateW,,z% TO output%
  admode%=!(z%+88+20*32+20)
  adspcl%=!(z%+88+19*32+20)
  oldmode%=!(z%+88+33*32+20)
  PROCdotemplate("Scaling")
  SYS CreateW,,z% TO scalew%
  adscale%=!(z%+88+1*32+20)
  s%(0)=!(z%+88+10*32+20)
  s%(1)=!(z%+88+11*32+20)
  s%(2)=!(z%+88+12*32+20)
  s%(3)=!(z%+88+13*32+20)
  PROCdotemplate("Save")
  SYS CreateW,,z% TO save%
  adsave%=!(z%+88+1*32+20)
  adsavesprite%=!(z%+88+0*32+20)
  PROCdotemplate("Sprite")
  SYS CreateW,,z% TO sinfo%
  adsname%=!(z%+88+5*32+20)
  adsbytes%=!(z%+88+8*32+20)
  adsx%=!(z%+88+7*32+20)
  adsy%=!(z%+88+9*32+20)
  adsm%=!(z%+88+6*32+20)
  PROCdotemplate("Source")
  SYS CreateW,,z% TO srcinfo%
  adsrc%=!(z%+88+0*32+20)
  PROCdotemplate("Range")
  SYS CreateW,,z% TO raninfo%
  adran%=!(z%+88+0*32+20)
  PROCdotemplate("Zoom")
  SYS CreateW,,z% TO zoom%
  z%(0)=!(z%+88+4*32+20)
  z%(1)=!(z%+88+5*32+20)
  PROCdotemplate("JPEGOutput")
  SYS CreateW,,z% TO jpego%
  adjpegq%=!(z%+88+5*32+20)
  PROCdotemplate("Pic")
  SYS CreateW,,z% TO pic%
  adtitle%=z%!72
  RESTORE ERROR
  SYS "Wimp_CloseTemplate"
  !q%=-1
  q%!4=0
  q%!8=0
  q%!12=68
  q%!16=68
  q%!20=%11000000000010
  $(q%+24)="!"+FNmsg0("_TaskName")
  SYS "Wimp_CreateIcon",,q% TO iconbar%
  SYS "XOS_Find",&40,"Choices:ChangeFSI.Choices" TO A%;V%
  IF (A%=0) OR ((V% AND 1)=1) THEN
    fast%= FALSE
    dest%=1
    statescale%=%10000000100
    stateproc%=0
    stateoutput%=&4052008
    statejpeg%=&2
    $p%(0)=""
    $p%(1)="2"+decimal_point$+"2"
    $p%(2)="24"
    $p%(3)="4"
    $adspcl%=""
    $admode%="28"
    $s%(0)="1"
    $s%(1)="4"
    $s%(2)="1"
    $s%(3)="4"
    $adjpegq%="75"
    $oldmode%=""
  ELSE
    INPUT#A%,fast%,dest%,statescale%,stateproc%,stateoutput%,statejpeg%,$adjpegq%,$oldmode%
    INPUT#A%,$p%(0),$p%(1),$p%(2),$p%(3),$admode%,$adspcl%
    $p%(1)=FNlocale_convert($p%(1),".",decimal_point$)
    INPUT#A%,$s%(0),$s%(1),$s%(2),$s%(3)
    CLOSE#A%
  ENDIF
  q%!12=1<<21
  !q%=scalew%
  FOR I%=0 TO 9
    IF statescale%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  FOR I%=10 TO 13
    IF statescale%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%+6
    SYS SetI,,q%
  NEXT
  !q%=proc%
  FOR I%=0 TO 7
    IF stateproc%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  !q%=output%
  FOR I%=0 TO 26
    IF stateoutput%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  !q%=jpego%
  FOR I%=0 TO 3
    IF statejpeg%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  IF stateoutput%>>17 AND 1 q%!8=1<<22 ELSE q%!8=0
  !q%=output%
  q%!12=1<<22
  q%!4=7
  SYS SetI,,q%
  q%!4=8
  SYS SetI,,q%
  q%!4=13
  SYS SetI,,q%
  q%!4=14
  SYS SetI,,q%
  PROCshowscaletofit
  $z%(0)="1"
  $z%(1)="1"
  SYS "OS_UpdateMEMC" TO oldmemc
  jpegsave%= FALSE
  ON ERROR SYS "OS_UpdateMEMC",oldmemc,&700:PROCerrorbox:SYS CrMenu,,-1 : REM close menus!
  SYS Drag,,-1
  IF NOT sprite% SYS "Wimp_SlotSize",originalslot%,-1
  F$=""
  IF startfile% startfile%= FALSE :F$=MID$(A$,INSTR(A$,"-wimp")+6):PROCcallFSI
  REPEAT
    SYS Poll%,1,q% TO A%
    CASE A% OF
      WHEN 1
        IF sprite% PROCredraw_window(!q%)
      WHEN 2
        PROCopen_window(!q%,q%!4,q%!8,q%!12,q%!16,q%!20,q%!24,q%!28)
      WHEN 3
        SYS &400c3,,q%
        nextlocation%=HIMEM
        PROCremovearea
        SYS "Wimp_SlotSize",originalslot%,-1
        saved%= TRUE
        sprite%= FALSE
        isopen%= FALSE
      WHEN 6
        PROCcheckmouse(!q%,q%!4,q%!8,q%!12,q%!16,q%!20)
      WHEN 7
        SYS GetP,,q%
        dw%=q%!12
        di%=q%!16
        IF dstart% SYS "DragASprite_Stop"
        q%!12=0
        q%!16=1
        q%!20=dw%
        q%!24=di%
        q%!28=!q%
        q%!32=q%!4
        q%!36=!sar%+3
        IF jpegsave% THEN
          q%!40=&c85
        ELSE
          q%!40=&ff9
        ENDIF
        temp$=FNZS(adsave%)
        foo%=INSTR(temp$,".")
        WHILE foo%
          temp$=MID$(temp$,foo%+1)
          foo%=INSTR(temp$,".")
        ENDWHILE
        $(q%+44)=temp$+CHR$0
        !q%=44+(LEN temp$+1+3 AND NOT 3)
        SYS "Wimp_SendMessage",17,q%,dw%,di%
        myref%=q%!8
      WHEN 8
        PROCkey(!q%,q%!4,q%!24)
      WHEN 9
        PROCmenuselect(q%)
      WHEN 17,18
        PROCreceive(q%)
    ENDCASE
  UNTIL FALSE
ELSE
  Q%=HIMEM
  REM Remember memory size
  END=&4000000
  REM Grab all the memory
  ON ERROR ON ERROR OFF:OSCLI"Set ChangeFSI$ReturnCode "+REPORT$+" (code "+ STR$ERL+")":PROCremovearea:CLEAR:END=Q%:END
  foo%=0
  REM Call program
  A%=FNChangeFSI(A$,-1,-1,0,-1,-1,foo%,TRUE)
  PROCremovearea
  OSCLI"unset ChangeFSI$ReturnCode"
  CLEAR
  END=Q%
  REM Return it all again
ENDIF
END
:
REM    
DEF PROCdotemplate(a$)
  LOCAL A%
  IF version%>=300 THEN
    SYS "Wimp_LoadTemplate",,0,,,,a$+z$,0 TO ,,indsize%
    DIM A% indsize%
    SYS "Wimp_LoadTemplate",,z%,A%,A%+indsize%,-1,a$+z$,0
  ELSE
    SYS "Wimp_LoadTemplate",,z%,idata%,eidata%,-1,a$+z$,0 TO ,,idata%
  ENDIF
ENDPROC
:
REM    
DEF PROClocale_initialise
  LOCAL p%
  LOCAL ERROR
  ON ERROR LOCAL decimal_point$=".":ENDPROC
  SYS "Territory_ReadSymbols",-1,0 TO p%
  decimal_point$=CHR$?p%
ENDPROC
:
REM    
DEF FNlocale_convert(n$,f$,t$)
  IF f$=t$ THEN =n$
  LOCAL i%
  i%=INSTR(n$,f$)
  IF i%<>0 : =LEFT$(n$,i%-1)+t$+MID$(n$,i%+1)
=n$
:
REM    
DEF FNsize_indirect(name$)
  LOCAL size%
  SYS "Wimp_LoadTemplate",,0,,,,name$+z$,0 TO ,,size%
=size%
:
REM Returns the largest buffer size necessary for passing
REM to R1 on a Wimp_LoadTemplate
DEF FNlargest_buffer
  LOCAL largest_tem%,size%,here%,name
  DIM name 12
  $name="*"
  here%=0
  largest_tem%=0
  SYS "Wimp_LoadTemplate", ,0,,,,name,0 TO ,size%,,,,,here%
  WHILE here%
    IF largest_tem%<size% largest_tem%=size%
    $name= "*"
    SYS "Wimp_LoadTemplate", ,0,,,,name,here% TO ,size%,,,,,here%
  ENDWHILE
=largest_tem%
:
REM    
DEF PROCreceive(q%)
  CASE q%!16 OF
    WHEN 0
      PROCfinishandquit
    WHEN 1
      IF q%!20<>save% THEN
        !q%=60
        q%!12=q%!8
        q%!16=2
        q%!36=-1
        $(q%+44)="<Wimp$Scrap>"+CHR$0
        SYS "Wimp_SendMessage",17,q%,q%!4
        scrap%=q%!8
      ENDIF
    WHEN 2
      IF jpegsave% THEN
        SYS "OS_File",10,FNZS(q%+44),&c85,,sar%,sar%+jpegsize%
      ELSE
        SYS &2E,&10C,sar%,FNZS(q%+44)
      ENDIF
      $adsave%=FNZS(q%+44)
      q%!12=q%!8
      q%!16=3
      SYS "Wimp_SendMessage",18,q%,q%!4
      IF q%!36<>-1 saved%= TRUE
      SYS CrMenu,,-1
    WHEN 3
      F$=FNZS(q%+44)
      IF scrap% scrap%=scrap%=q%!12
      q%!12=q%!8
      q%!16=4
      SYS "Wimp_SendMessage",17,q%,q%!4
      PROCcallFSI
    WHEN 5
      CASE q%!40 OF
        WHEN &ff0,&c85
          q%!12=q%!8
          q%!16=4
          SYS "Wimp_SendMessage",17,q%,q%!4
          F$=FNZS(q%+44)
          PROCcallFSI
      ENDCASE
    WHEN 8
      PROCchecksaved(q%)
    WHEN 9,&400C1
      IF sprite% PROCgetmodeinfo:SYS &400d1,pic%,0,-32767,32767,0
      PROCshowscaletofit
    WHEN 10
      SYS "XOS_ReadVarVal","ChangeFSI$Dir",paltemp%,256,0,3 TO ,,bootlen%
      paltemp%?bootlen%=13
      BPUT#q%!20,"Run "+$paltemp%
    WHEN &502
      REM Interactive help
      PROCgethelp(q%!32,q%!36)
  ENDCASE
ENDPROC
:
REM    
DEF PROCgethelp(w%,i%)
  IF i%<-1 THEN ENDPROC
  CASE w% OF
    WHENinfow%
      $(q%+20)=FNmsg0("Hinfo"+STR$(i%))
    WHENproc%
      $(q%+20)=FNmsg0("Hproc"+STR$(i%))
    WHENoutput%
      $(q%+20)=FNmsg0("Hout"+STR$(i%))
    WHENscalew%
      $(q%+20)=FNmsg0("Hscale"+STR$(i%))
    WHENsave%
      $(q%+20)=FNmsg0("Hsave"+STR$(i%))
    WHENsinfo%
      $(q%+20)=FNmsg0("Hsprite"+STR$(i%))
    WHENsrcinfo%
      $(q%+20)=FNmsg0("Hsrc"+STR$(i%))
    WHENraninfo%
      $(q%+20)=FNmsg0("Hrange"+STR$(i%))
    WHENzoom%
      $(q%+20)=FNmsg0("Hzoom"+STR$(i%))
    WHENjpego%
      $(q%+20)=FNmsg0("Hjpeg"+STR$(i%))
    WHENpic%
      $(q%+20)=FNmsg0("Hpic"+STR$(i%))
    WHEN-2
      ENDPROC:REM Default from !Help used
    OTHERWISE
      IF i%<>-1 THEN
        REM Must be a menu then
        SYS "Wimp_GetMenuState",1,q%+20,w%,i%
        IF lastm%=101 THEN
          REM The iconbar menu
          LOCAL grey$
          IF (q%!20=6) AND vram% THEN grey$="g"
          IF (q%!20=5) AND (F$="" OR F$="SpriteFile" OR F$="JPEGImage") THEN grey$="g"
          $(q%+20)=FNmsg0("Himnu"+STR$(q%!20)+grey$)
        ELSE
          IF lastm%=pic% THEN
            REM The picture menu
            $(q%+20)=FNmsg0("Hpmnu"+STR$(q%!20))
          ELSE
            REM Yikes, some menu we didn't open
            ENDPROC
          ENDIF
        ENDIF
      ENDIF
  ENDCASE
  q%!0=((20+LEN($(q%+20))+1)+3)ANDNOT3
  q%!12=q%!8
  q%!16=&503:REM Message_HelpReply
  SYS "Wimp_SendMessage",17,q%,q%!4
ENDPROC
:
REM    
DEF PROCcallFSI
  IF FNcheckvalues <> 0 THEN ENDPROC
  A$=FNdeducemode
  A%=47
  jpegsave%= FALSE
  IF dest%=0 THEN
    jpegsave%= TRUE
    A$="JPEG"
    IF FNgeticonstate(jpego%,2) A$="JPEGMONO"
    A$+=$adjpegq%
  ELSE
    IF FNgeticonstate(output%,15) THEN
      A$+=$adspcl%
    ELSE
      IF FNgeticonstate(output%,16) THEN
        IF FNgeticonstate(output%,5) OR FNgeticonstate(output%,11) A$+="R"
      ELSE
        SYS 53,VAL A$,3 TO ,,A%
        IF A%=63 THEN
          IF FNgeticonstate(output%,6) A$="27t"
          IF FNgeticonstate(output%,12) A$="12t"
        ELSE
          IF A%=255 A$+="d"
        ENDIF
      ENDIF
    ENDIF
  ENDIF
  A$="garbage "+F$+" <ChangeFSI$Dir>.Foo "+A$
  PROCChangeFSISuffix
  info$=""
  range$=FNmsg0("NoRange")
  cputime%=0
  IF sprite% !q%=pic%:SYS &400c3,,q%:saved%= TRUE :sprite%= FALSE
  nextlocation%=HIMEM
  SYS "Wimp_SlotSize",originalslot%,-1
  PROCremovearea
  IF fast% SYS "OS_UpdateMEMC",&300,&700 TO oldmemc
  A%=FNChangeFSI(A$,-1,-1,nextlocation%,0,0,sar%,0)
  IF fast% SYS "OS_UpdateMEMC",oldmemc,&700
  IF scrap% scrap%= FALSE :SYS "OS_File",6,F$
  IF A%<2 AND sar%<>0 THEN
    $adsx%= STR$xsp%
    $adsy%= STR$ysp%
    IF jpegsave% THEN
      $adsm%="JPEG"
      $adsname%="JPEG"
      $adsbytes%= STR$jpegsize%
      x=xsp%*2
      y=ysp%*2
      SYS &35,-1,4 TO ,,nx
      SYS &35,-1,5 TO ,,ny
      !ifactors%=1
      ifactors%!4=1
      ifactors%!8=nx
      ifactors%!12=ny
      $adsavesprite%="file_c85"
      IF LEFT$(F$,7)="<Wimp$S" F$="JPEGImage"
    ELSE
      SYS &2002E,&10d,sar%,pixtrans%,255,1
      n$=FNZS(pixtrans%)
      SYS &2002E,&128,sar%,n$ TO ,,,x,y,,m
      IF (m<256 AND SGN(m)<>-1) THEN $adsm%=STR$m ELSE $adsm%="&"+STR$~m
      $adsname%=n$
      $adsbytes%= STR$sar%!12
      SYS &35,m,4 TO ,,nx
      x=x<<nx
      SYS &35,m,5 TO ,,ny
      y=y<<ny
      PROCgetmodeinfo
      $adsavesprite%="file_ff9"
      IF LEFT$(F$,7)="<Wimp$S" F$="SpriteFile"
    ENDIF
    !q%=save%
    q%!4=0
    q%!8=0
    q%!12=0
    SYS SetI,,q%
    $adran%=range$
    $adsrc%=info$+" "+FNmsg1("Proc",STR$(cputime% DIV 100)+decimal_point$+RIGHT$("0"+ STR$(cputime%MOD 100),2))
    saved%= FALSE
    sprite%= TRUE
    REM Get the leafname of F$ for the save box (so you don't overwrite the source),but have the full path in the title bar
    A%=LEN F$
    WHILE MID$("."+F$,A%,1)<>"."
      A%-=1
    ENDWHILE
    $adsave%=MID$("."+F$,A%+1)
    pic%=FNcreate_window(x,y,F$)
    PROCpopup(pic%,-1)
  ENDIF
ENDPROC
:
REM    
DEF FNcheckvalues
  LOCAL e,error$,flag%
  REM Is the JPEG quality within the correct range?
  IF dest%=0 AND VAL ($adjpegq%) > 100 THEN
    e = 1
    error$ = FNmsg0("BadJQ")
  ENDIF
  REM Is it a numerical mode?
  IF LEFT$($admode%,1) < "A" AND dest%=1 THEN
    SYS &35, VAL ($admode%), 0 TO ;flag%
    REM Is the numerical mode valid?
    IF LEN ($admode%) = 0 flag%=2
    IF VAL ($admode%) > 127 AND VAL ($admode%) < 256 flag%=2
    IF (flag% AND 2) > 0 THEN
      e = 2
      error$ = FNmsg0("BadMode")
    ENDIF
  ENDIF
  IF e <> 0 THEN
    SYS Drag,,-1
    !buffer%=e
    $(buffer%+4)=error$+CHR$0
    SYS "Wimp_ReportError",buffer%,%01,FNmsg0("_TaskName") TO ,A%
    IF A%=2 THEN PROCfinishandquit
  ENDIF
=e
:
REM    
DEF PROCChangeFSISuffix
598 599 600 601 602
  IF FNgeticonstate(scalew%,1) A$+=" "+ STR$tofitx%+": "+ STR$tofity%+":"
  REM  geticonstate(scalew%,2) no op 1:1 1:1
  IF FNgeticonstate(scalew%,3) A$+=" 1:1 1:2"
  IF FNgeticonstate(scalew%,4) A$+=" 1:2 1:1"
  IF FNgeticonstate(scalew%,5) A$+=" 1:2"
603
  IF FNgeticonstate(scalew%,9) THEN
604
    REM Custom scaling
605 606 607 608 609 610 611 612 613 614 615
    IF VAL $s%(0) A$+=" "+$s%(0) ELSE A$+=" 1"
    A$+=":"+$s%(1)
    IF VAL $s%(2) A$+=" "+$s%(2) ELSE A$+=" 1"
    A$+=":"+$s%(3)
  ENDIF
  IF FNgeticonstate(scalew%,0) A$+=" -nosize"
  IF FNgeticonstate(scalew%,18) A$+=" -noscale"
  IF FNgeticonstate(scalew%,19) A$+=" -lock"
  IF FNgeticonstate(scalew%,6) THEN
    IF FNgeticonstate(scalew%,16) A$+=" -rotate" ELSE A$+=" -rotate-"
  ENDIF
616 617 618 619
  IF FNgeticonstate(scalew%,7) A$+=" -hflip"
  IF FNgeticonstate(scalew%,8) A$+=" -vflip"
  IF FNgeticonstate(proc%,0) A$+=" -range"
  IF FNgeticonstate(proc%,1) A$+=" -equal"
620
  IF FNgeticonstate(proc%,2) A$+=" -nodither"
621
  IF FNgeticonstate(proc%,3) A$+=" -invert"
622 623
  IF FNgeticonstate(proc%,4) A$+=" -brighten"
  IF FNgeticonstate(proc%,5) A$+=" -black"+$p%(0)
624 625
  IF FNgeticonstate(proc%,6) A$+=" -gamma"+FNlocale_convert($p%(1),decimal_point$,".")
  IF FNgeticonstate(proc%,7) A$+=" -sharpen"+$p%(2)
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
  IF FNgeticonstate(proc%,11) A$+=" -smooth"+$p%(3)
ENDPROC
:
REM    
DEF PROCfinishandquit
  PROCremovearea
  SYS "Wimp_CloseDown"
  SYS "XMessageTrans_CloseFile",msgb%
  END
ENDPROC
:
REM    
DEF PROCcheckandfinish
  IF saved% OR NOT sprite% THEN PROCfinishandquit
  !buffer%=ERR
  $(buffer%+4)=FNmsg1("Unsaved",F$)+CHR$0
  SYS "Wimp_ReportError",buffer%,%010100000000,FNmsg0("_TaskName"),"!changefsi",1,FNmsg0("ButCon") TO ,A%
  IF A%=4 THEN PROCfinishandquit
ENDPROC
:
REM    
DEF PROCchecksaved(q%)
  IF saved% OR NOT sprite% ENDPROC
  !buffer%=ERR
  $(buffer%+4)=FNmsg1("Unsaved",F$)+CHR$0
  SYS "Wimp_ReportError",buffer%,%010100000000,FNmsg0("_TaskName"),"!changefsi",1,FNmsg0("ButCon") TO ,A%
  q%!12=q%!8
  IF A%<>4 SYS "Wimp_SendMessage" ,19, q%, 0
ENDPROC
:
REM    
DEF PROCkey(handle%,icon%,key%)
  IF key%=&1b SYS CrMenu,,-1:ENDPROC
  CASE handle% OF
    WHEN zoom%
      CASE key% OF
        WHEN 13
          PROCdozoom(icon%-4,0)
          IF icon%=4 SYS &400d2,zoom%,5,,,-1,LEN $z%(0)
          IF icon%=5 SYS CrMenu,,-1
          ENDPROC
        WHEN &18e,&18f
          PROCdozoom(icon%-4,0)
          SYS &400d2,zoom%,icon% EOR 1,,,-1,LEN $z%(icon% EOR 5)
          ENDPROC
      ENDCASE
    WHEN save%
      IF key%=13 PROCsavesprite:ENDPROC
    WHEN output%
      IF icon%=33 AND FNgeticonstate(output%, 24) THEN $admode% = $oldmode%:!q%=output%:q%!4=20:q%!8=0:q%!12=0:SYS SetI,,q%
      PROCshowscaletofit
      IF key%=13 SYS CrMenu,,-1:ENDPROC
      IF key%=&18e THEN
        CASE icon% OF
          WHEN 19
            icon%=33
            key%=oldmode%
          WHEN 33
            icon%=19
            key%=adspcl%
        ENDCASE
      ENDIF
      IF key%=&18f THEN
        CASE icon% OF
          WHEN 33
            icon%=19
            key%=adspcl%
          WHEN 19
            icon%=33
            key%=oldmode%
        ENDCASE
      ENDIF
      IF key%>1024 SYS &400d2,output%,icon%,,,-1,LEN $key%:ENDPROC
    WHEN proc%
      CASE key% OF
        WHEN &18e,13
          icon%+=1
          IF icon%>11 icon%=8:IF key%=13 SYS CrMenu,,-1:ENDPROC
          SYS &400d2,proc%,icon%,,,-1,LEN $p%(icon%-8)
          ENDPROC
        WHEN &18f
          icon%-=1
          IF icon%<8 icon%=11
          SYS &400d2,proc%,icon%,,,-1,LEN $p%(icon%-8)
          ENDPROC
      ENDCASE
    WHEN scalew%
      CASE key% OF
        WHEN &18e,13
          icon%+=1
          IF icon%>13 icon%=10:IF key%=13 SYS CrMenu,,-1:ENDPROC
          SYS &400d2,scalew%,icon%,,,-1,LEN $s%(icon%-10)
          ENDPROC
        WHEN &18f
          icon%-=1
          IF icon%<10 icon%=13
          SYS &400d2,scalew%,icon%,,,-1,LEN $s%(icon%-10)
          ENDPROC
      ENDCASE
  ENDCASE
  SYS "Wimp_ProcessKey",key%
ENDPROC
:
REM    
DEF PROCsavesprite
  IF F$ = $adsave% THEN
    SYS Drag,,-1
    !buffer%=3
    $(buffer%+4)=FNmsg0("OverW")+CHR$0
    SYS "Wimp_ReportError",buffer%,%11,FNmsg0("_TaskName") TO ,A%
    IF A%=2 THEN
      REM Cancel
      SYS CrMenu,,-1
      ENDPROC
    ENDIF
  ENDIF
  IF jpegsave% THEN
    SYS "OS_File",10,$adsave%,&c85,,sar%,sar%+jpegsize%
  ELSE
    SYS &2E,&10C,sar%,$adsave%
  ENDIF
  SYS CrMenu,,-1
ENDPROC
:
REM    
DEF PROCcheckmouse(mousex%,mousey%,b%,handle%,icon%,ob%)
  LOCAL moved%,imenu$,pmenu$
  REM Picture menu structure
  pmenu$=       FNmsg0("Pimg")+">sinfo%,"+FNmsg0("Psrc")+">srcinfo%,"+FNmsg0("Prange")+">raninfo%,"
  pmenu$=pmenu$+FNmsg0("Pzoom")+">zoom%,"+FNmsg0("Psave")+">save%,"+FNmsg0("PIredo")
  REM Iconbar menu structure
  imenu$=       FNmsg0("Iinfo")+">infow%,"+FNmsg0("Iscale")+">scalew%,"+FNmsg0("Iproc")+">proc%,"
  imenu$=imenu$+FNmsg0("Ispr")+">output%:dest%,"+FNmsg0("Ijpg")+">jpego%:1-dest%,"+FNmsg0("PIredo")+","
  imenu$=imenu$+FNmsg0("Ifast")+":fast%AND1,"+FNmsg0("Ichoice")+","+FNmsg0("Iquit")
  moved%= FALSE
  REM Button 'menu'
  IF b% AND 2 THEN
    CASE handle% OF
      WHEN pic%
        PROCdomenu(pic%,pmenu$,FNmsg0("_TaskName"))
      WHEN -2
        PROCdomenu(101,imenu$,FNmsg0("_TaskName"))
    ENDCASE
    rmenuhandle%=handle%
  ENDIF
  REM Button 'select'
  IF b% AND 4 THEN
    CASE handle% OF
      WHEN zoom%
        CASE icon% OF
          WHEN 0
            PROCdozoom(0,1)
          WHEN 1
            PROCdozoom(0,-1)
          WHEN 2
            PROCdozoom(1,1)
          WHEN 3
            PROCdozoom(1,-1)
        ENDCASE
    ENDCASE
  ENDIF
  REM Button 'adjust'
  IF b% AND 1 THEN
    CASE handle% OF
      WHEN zoom%
        CASE icon% OF
          WHEN 0
            PROCdozoom(0,-1)
          WHEN 1
            PROCdozoom(0,1)
          WHEN 2
            PROCdozoom(1,-1)
          WHEN 3
            PROCdozoom(1,1)
        ENDCASE
    ENDCASE
  ENDIF
  REM Button 'adjust' or 'select'
  IF b% AND 5 THEN
    CASE handle% OF
      WHEN output%
        IF dest%=0 THEN dest%=1:PROCdomenu(101,imenu$,FNmsg0("_TaskName")):SYS Getcaret%,,q%:moved%= TRUE
        CASE icon% OF
          WHEN 3,4,5,6
            PROCkillcurrent( STR$(22+icon%))
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 7
            PROCkillcurrent("S16,90,90")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 8
            PROCkillcurrent("S32,90,90")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 9
            PROCkillcurrent("0")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 10
            PROCkillcurrent("8")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 11
            PROCkillcurrent("12")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 12
            PROCkillcurrent("15")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 13
            PROCkillcurrent("S16,90,45")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 14
            PROCkillcurrent("S32,90,45")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 15
            PROCkillcurrent(FNdeducemode)
            SYS SetI,,q%
            SYS Caretpos%,output%,19,,,-1,LEN ($adspcl%)
            !q%=output%
            q%!4=7
            q%!8=0
            q%!12=1<<22
            SYS SetI,,q%
            q%!4=8
            SYS SetI,,q%
            q%!4=13
            SYS SetI,,q%
            q%!4=14
            SYS SetI,,q%
          WHEN 16
            PROCkillcurrent(FNdeducemode)
            !q%=output%
            q%!4=7
            q%!8=0
            q%!12=1<<22
            SYS SetI,,q%
            q%!4=8
            SYS SetI,,q%
            q%!4=13
            SYS SetI,,q%
            q%!4=14
            SYS SetI,,q%
          WHEN 17
            PROCkillcurrent(FNdeducemode)
            !q%=output%
            q%!4=7
            q%!8=1<<22
            q%!12=1<<22
            SYS SetI,,q%
            q%!4=8
            SYS SetI,,q%
            q%!4=13
            SYS SetI,,q%
            q%!4=14
            SYS SetI,,q%
            IF LEFT$($admode%,1) = "S" THEN
              IF RIGHT$($admode%,2) = "45" THEN
                PROCkillcurrent("15")
                !q%=output%
                q%!4=18
                q%!8=0
                q%!12=1<<21
                SYS SetI,,q%
                q%!4=24
                SYS SetI,,q%
              ELSE
                PROCkillcurrent("28")
                !q%=output%
                q%!4=18
                q%!8=0
                q%!12=1<<21
                SYS SetI,,q%
                q%!4=24
                SYS SetI,,q%
              ENDIF
            ENDIF
          WHEN 18
            !q%=output%
            q%!4=24
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
          WHEN 24
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            PROCkillcurrent(FNdeducemode)
            SYS Caretpos%,output%,33,,,-1,LEN ($oldmode%)
          WHEN 33
            IF moved% SYS Caretpos%,output%,q%!4,q%!8,q%!12,q%!16,q%!20
          WHEN 19
            IF moved% SYS Caretpos%,output%,q%!4,q%!8,q%!12,q%!16,q%!20
        ENDCASE
        IF FNgeticonstate(output%,18) $admode%=FNdeducemode:REM SYS Caretpos%,output%,20,,,-1,LEN(FNdeducemode)
        !q%=output%
        q%!4=20
        q%!8=0
        q%!12=0
        SYS SetI,,q%
        PROCshowscaletofit
      WHEN save%
        CASE icon% OF
          WHEN 2
            PROCsavesprite
        ENDCASE
      WHEN -2
        IF sprite% PROCgetw(pic%):PROCopen_window(pic%,x0%,y0%,x1%,y1%,scx%,scy%,-1)
      WHEN jpego%
        IF dest%=1 THEN
          SYS Getcaret%,,q%
          dest%=0
          PROCdomenu(101,imenu$,FNmsg0("_TaskName"))
          IF icon%=5 SYS Caretpos%,jpego%,q%!4,q%!8,q%!12,q%!16,q%!20
        ENDIF
        CASE icon% OF
          WHEN 3
            IF b% AND 4 $adjpegq% = STR$(VAL ($adjpegq%)-1) ELSE $adjpegq% = STR$(VAL ($adjpegq%)+1)
            IF VAL ($adjpegq%)<0 $adjpegq%="0"
            IF VAL ($adjpegq%)>100 $adjpegq%="100"
            !q%=jpego%
            q%!4=5
            q%!8=0
            q%!12=0
            SYS SetI,,q%
            SYS Caretpos%,jpego%,5,,,-1,LEN ($adjpegq%)
          WHEN 4
            IF b% AND 4 $adjpegq% = STR$(VAL ($adjpegq%)+1) ELSE $adjpegq% = STR$(VAL ($adjpegq%)-1)
            IF VAL ($adjpegq%)<0 $adjpegq%="0"
            IF VAL ($adjpegq%)>100 $adjpegq%="100"
            !q%=jpego%
            q%!4=5
            q%!8=0
            q%!12=0
            SYS SetI,,q%
            SYS Caretpos%,jpego%,5,,,-1,LEN ($adjpegq%)
          WHEN 5
            REM Quality
        ENDCASE
    ENDCASE
  ENDIF
  IF b% AND &50 THEN
    CASE handle% OF
      WHEN save%
        CASE icon% OF
          WHEN 0
            PROCgetw(save%)
            !q%=save%
            q%!4=0
            SYS GetI,,q%
            !q%=save%
            q%!4=5
            q%!8+=bx%
            q%!12+=by%
            q%!16+=bx%
            q%!20+=by%
            q%!24=0
            q%!28=0
            q%!32=&7FFFFFFF
            q%!36=&7FFFFFFF
            t$="file_ff9"
            IF jpegsave% t$="file_c85"
            IF dstart% SYS dstart%,%11000101,1,t$,q%+8,q%+24 ELSE SYS "Wimp_DragBox",,q%
        ENDCASE
    ENDCASE
  ENDIF
ENDPROC
:
REM    
DEF PROCdozoom(s%,a%)
  IF s%=0 THEN
    $z%(0)= STR$(VAL $z%(0)+a%)
    IF $z%(0)="0" $z%(0)="1"
  ELSE
    $z%(1)= STR$(VAL $z%(1)+a%)
    IF $z%(1)="0" $z%(1)="1"
  ENDIF
  !q%=zoom%
  q%!4=4+s%
  q%!8=0
  q%!12=0
  SYS SetI,,q%
  !q%=pic%
  SYS GetW,,q%
  f%=q%!28
  SYS &400c3,,q%
  pic%=FNcreate_window(x,y,F$)
  PROCpopup(pic%,f%)
ENDPROC
:
REM    
DEF PROCdomenu(handle%,menu$,menutitle$)
  menuhandle%=handle%
  menuptr%=buffer%
  indirect%=idata%
  i%=0
  menuptr%!20=44
  $menuptr%=menutitle$
  menuptr%?12=7
  menuptr%?13=2
  menuptr%?14=7
  menuptr%?15=0
  menuptr%!16=200
  menuptr%!24=0
  menuptr%+=28
  REPEAT
    i1%=i%+1
    i%=INSTR(menu$+",",",",i1%)
    item$=MID$(menu$,i1%,i%-i1%)
    PROCmenuitem(item$)
  UNTIL item$=""
  menuptr%!-24=(menuptr%!-24) OR &80
  IF handle%=101 SYS CrMenu,,buffer%,mousex%-64,96+9*44 ELSE SYS CrMenu,,buffer%,mousex%-64,mousey%
  lastm%=handle%:REM Record last menu opened for interactive help
ENDPROC
:
REM    
DEF PROCmenuitem(text$)
  IF text$="" ENDPROC
  flg%=0
  submenu%=-1
  I%=INSTR(text$,":")
  IF I% THEN
    flg%=EVALMID$(text$,I%+1)
    text$=LEFT$(text$,I%-1)
  ENDIF
  I%=INSTR(text$,">")
  IF I% THEN
    submenu%=EVALMID$(text$,I%+1)
    text$=LEFT$(text$,I%-1)
  ENDIF
  menuptr%!0=flg%
  menuptr%!4=submenu%
  IF (text$=FNmsg0("PIredo") AND (F$="" OR F$="SpriteFile" OR F$="JPEGImage")) OR (text$=FNmsg0("Ifast") AND vram%) THEN
    menuptr%!8=&07400021
  ELSE
    menuptr%!8=&07000021
  ENDIF
  IF LEN text$*16+8>buffer%!16 buffer%!16=LEN text$*16+8
  IF LEN text$>12 THEN
    menuptr%!8=menuptr%!8 OR 256
    menuptr%!20=LEN text$+1
    menuptr%!16=0
    menuptr%!12=indirect%
    $indirect%=text$
    indirect%+=LEN text$+2
  ELSE
    $(menuptr%+12)=text$
  ENDIF
  menuptr%+=24
ENDPROC
:
REM    
DEF PROCmenuselect(menus%)
  SYS GetP,,paltemp%
  adjust%=paltemp%!8 AND 1
  SYS DcMenu,,buffer%,menus%, STRING$(200," ") TO ,,,a$
  IF a$="" THEN ENDPROC
  CASE menuhandle% OF
    WHEN pic%
      CASE a$ OF
        WHEN FNmsg0("PIredo")
          PROCcallFSI
      ENDCASE
    WHEN 101
      CASE a$ OF
        WHEN FNmsg0("Ifast")
          fast%=NOT fast%
        WHEN FNmsg0("Ijpg")
          dest%=0
        WHEN FNmsg0("Ispr")
          dest%=1
        WHEN FNmsg0("PIredo")
          PROCcallFSI
        WHEN FNmsg0("Ichoice")
          statescale%=0
          FOR I%=0 TO 9
            statescale%+=FNgeticonstate(scalew%,I%)<<I%
          NEXT
          FOR I%=10 TO 13
            statescale%+=FNgeticonstate(scalew%,I%+6)<<I%
          NEXT
          stateproc%=0
          FOR I%=0 TO 7
            stateproc%+=FNgeticonstate(proc%,I%)<<I%
          NEXT
          stateoutput%=0
          FOR I%=0 TO 26
            stateoutput%+=FNgeticonstate(output%,I%)<<I%
          NEXT
          statejpeg%=0
          FOR I%=0 TO 3
            statejpeg%+=FNgeticonstate(jpego%,I%)<<I%
          NEXT
          OSCLI"CDIR <Choices$Write>.ChangeFSI"
          A%= OPENOUT"<Choices$Write>.ChangeFSI.Choices"
          PRINT#A%,fast%,dest%,statescale%,stateproc%,stateoutput%,statejpeg%,$adjpegq%,$oldmode%
          PRINT#A%,$p%(0),FNlocale_convert($p%(1),decimal_point$,"."),$p%(2),$p%(3),$admode%,$adspcl%
          PRINT#A%,$s%(0),$s%(1),$s%(2),$s%(3)
          CLOSE#A%
        WHEN FNmsg0("Iquit")
          PROCcheckandfinish
      ENDCASE
  ENDCASE
  IF adjust% PROCcheckmouse(0,0,2,rmenuhandle%,0,0)
ENDPROC
:
REM    
DEF FNcreate_window(x,y,title$)
  x=x*VAL $z%(0) DIV VAL $z%(1)
  y=y*VAL $z%(0) DIV VAL $z%(1)
  !z%=0
  z%!4=-y
  z%!8=x
  z%!12=0
  z%!40=0
  z%!44=-y
  z%!48=x
  z%!52=0
  IF x<128 OR y<128 z%?35=0 ELSE z%?35=255
  $adtitle%=""
  SYS CreateW,,z% TO handle%
  $adtitle%=title$
=handle%
:
REM    
DEF PROCpopup(handle%,f%)
  SYS "OS_ReadModeVariable",-1,4 TO ,,modexshift%
  SYS "OS_ReadModeVariable",-1,5 TO ,,modeyshift%
  SYS "OS_ReadModeVariable",-1,11 TO ,,rightscr%
  SYS "OS_ReadModeVariable",-1,12 TO ,,topscr%
  rightscr%=rightscr%<<modexshift%
  topscr%=topscr%<<modeyshift%
  PROCgetw(handle%)
  IF isopen% THEN
    REM It's already open,so remember its top left corner
    xo%=isopen% AND &FFFF
    yo%=isopen% >>>16
  ELSE
    REM New image,start in the middle
    xo%=rightscr%/2-(x0%+x1%)/2
    yo%=topscr%/2-(y0%+y1%)/2
  ENDIF
  PROCopen_window(handle%,x0%+xo%,y0%+yo%,x1%+xo%,y1%+yo%,scx%,scy%,f%)
ENDPROC
:
REM    
DEF PROCopen_window(handle%,x0%,y0%,x1%,y1%,scx%,scy%,bhandle%)
  IF handle%=-1 THEN ENDPROC
  q%!0=handle%
  q%!28=bhandle%
  q%!4=x0%
  q%!8=y0%
  q%!12=x1%
  q%!16=y1%
  q%!20=scx%
  q%!24=scy%
  SYS OpenW,,q%
  isopen%=x0% OR (y1%<<16)
ENDPROC
:
REM    
DEF PROCgetw(handle%)
  !q%=handle%
  SYS GetW,,q%
  PROCinfo(q%+4)
  bhandle%=q%!28
  flags%=q%!32
ENDPROC
:
REM    
DEF PROCinfo(q%)
  x0%=!q%
  y0%=q%!4
  x1%=q%!8
  y1%=q%!12
  scx%=q%!16
  scy%=q%!20
  bx%=x0%-scx%
  by%=y1%-scy%
ENDPROC
:
REM    
DEF PROCredraw_window(handle%)
  IF handle%=pic% THEN
    !factors%=!ifactors%*VAL $z%(0)
    factors%!4=ifactors%!4*VAL $z%(0)
    factors%!8=ifactors%!8*VAL $z%(1)
    factors%!12=ifactors%!12*VAL $z%(1)
    PROCreduce(!factors%,factors%!8)
    PROCreduce(factors%!4,factors%!12)
    !q%=handle%
    SYS RedrawW,,q% TO more%
    PROCinfo(q%+4)
    by%=by%-INT (y*VAL $z%(0) DIV VAL $z%(1))
    WHILE more%
      IF jpegsave% THEN
        IF jpegpaint% SYS jpegpaint%,sar%,bx%,by%,factors%,jpegsize%,1 TO ;V%:IF (V% AND 1)=0 ELSE CLG
      ELSE
        SYS &2002E,&134,sar%,n$,bx%,by%,,factors%,spx%
      ENDIF
      SYS GetR%,0,q% TO more%
    ENDWHILE
  ENDIF
ENDPROC
:
REM    
DEF FNgeticonstate(handle%,icon%)
  !q%=handle%
  q%!4=icon%
  SYS GetI,,q%
=q%!24>>21 AND 1
:
REM    
DEF PROCgetmodeinfo
  IF jpegsave% THEN
    SYS &35,-1,4 TO ,,nx
    SYS &35,-1,5 TO ,,ny
    !ifactors%=1
    ifactors%!4=1
    ifactors%!8=nx
    ifactors%!12=ny
    ENDPROC
  ENDIF
  SYS "XWimp_ReadPixTrans",&100,sar%,n$,,,,ifactors%,pixtrans%
  SYS &2002E,&118,sar%,n$ TO ,,sptr%
  IF sptr%!32=44 THEN
    palptr%=0
  ELSE
    FOR grab%=0 TO 2048-8 STEP 8
      paltemp%!(grab%>>1)=sptr%!(grab%+44)
    NEXT
    palptr%=paltemp%
  ENDIF
  FOR Q%=0 TO 255
    pixtrans%?Q%=Q%
  NEXT
  IF sptr%!32=44+2048 THEN
    FOR Q%=0 TO 255
      SYS ctran%,palptr%!(Q%<<2) TO pixtrans%?Q%
    NEXT
  ELSE
    SYS "ColourTrans_SelectTable",m,palptr%,-1,-1,pixtrans%
  ENDIF
  spx%=0
  FOR Q%=0 TO 255
    IF pixtrans%?Q%<>Q% spx%=pixtrans%
  NEXT
ENDPROC
:
REM    
DEF FNdeducemode
  LOCAL m,x,y
  IF FNgeticonstate(output%,18) THEN
    REM Use current screen mode
    SYS &35,-1,9 TO ,,m:REM log2bpp
    IF m>3 THEN
      SYS &35,-1,4 TO ,,x:REM xeigen
      SYS &35,-1,5 TO ,,y:REM yeigen
      x=180/(2^x)
      y=180/(2^y)
      ="S"+ STR$(2^m)+","+ STR$x+","+ STR$y
    ELSE
      = STR$MODE
    ENDIF
  ELSE
    IF FNgeticonstate(output%,24) THEN
      REM Use the number from the writeable icon
      =$oldmode%
    ELSE
      =$admode%
    ENDIF
  ENDIF
:
REM    
DEF PROCshowscaletofit
  LOCAL m,x,y
  $admode%=FNdeducemode
  CASE ?admode% OF
    WHEN ASC "S"
      IF VAL MID$($admode%,5)>VAL MID$($admode%,8) x=6 ELSE x=0
      IF VAL MID$($admode%,2)>16 x+=5 ELSE x+=4
1359 1360
      SYS &35,-1,11 TO ,,tofitx%
      SYS &35,-1,12 TO ,,tofity%
1361
    WHEN ASC "J"
1362 1363 1364
      SYS &35,-1,11 TO ,,tofitx%
      SYS &35,-1,12 TO ,,tofity%
      x = 12 : REM So no radio button is selected.
1365 1366
    OTHERWISE
      m=VAL $admode%
1367 1368
      SYS &35,m,11 TO ,,tofitx%
      SYS &35,m,12 TO ,,tofity%
1369 1370 1371 1372 1373 1374
      SYS &35,m,4 TO ,,x
      SYS &35,m,5 TO ,,y
      IF y>x x=6 ELSE x=0
      SYS &35,m,9 TO ,,y
      x+=y
  ENDCASE
1375 1376 1377
  tofitx%+=1
  tofity%+=1
  $adscale%=FNmsg2("ScaleFill",STR$tofitx%,STR$tofity%)
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  q%!12=1<<21
  !q%=output%
  FOR I%=0 TO 11
    IF I%=x q%!8=1<<21 ELSE q%!8=0
    q%!4=I%+3
    SYS SetI,,q%
  NEXT
  IF x=12 q%!8=0:q%!4=24:SYS SetI,,q%
ENDPROC
:
REM    
DEF PROCkillcurrent(m$)
  q%!12=1<<21
  !q%=output%
  q%!8=0
  q%!4=20
  SYS SetI,,q%
  $admode%=m$
  REM SYS Caretpos%,output%,20,,,-1,LEN(m$)
ENDPROC
:
REM    
DEF FNmsg0(t$)
=FNmsg2(t$,"","")
:
REM    
DEF FNmsg1(t$,a$)
=FNmsg2(t$,a$,"")
:
REM    
DEF FNmsg2(t$,a$,b$)
  LOCALr$
  REM Superset of message lookup with substitution
  SYS"MessageTrans_Lookup",msgb%,t$,STRING$(200,"@"),200,a$,b$ TO,,r$
=r$
:
REM    
DEF FNZS(I%)LOCAL A$
  REPEAT
    A$+=CHR$?I%
    I%+=1
  UNTIL ?I%<14
=A$
:
REM    
DEF PROCerrorbox
  SYS Drag,,-1
  !buffer%=ERR
  $(buffer%+4)=REPORT$+CHR$0
  REM $(buffer%+4)=REPORT$+" (ref: "+STR$ERL+")"+CHR$0
  SYS "Wimp_ReportError",buffer%,%010100000000,FNmsg0("_TaskName"),"!changefsi",1,FNmsg0("ButErr") TO ,A%
  IF A%=4 THEN PROCfinishandquit
ENDPROC