REM Title:      SimClick
REM Purpose:    Simulate a click on an icon
REM Version:    0.53, Saturday 15 February 2003
REM Copyright:  (C) Philip Ludlam 2001

REM Purpose : Simulate a mouse click on an icon
REM Syntax  : SimClick [-i[con] <icon handle>] [-b[utton] <mouse button>]
REM                    [-w[indow] <window handle>] [-t[ask] <task handle/name>]

REM This program is free software; you can redistribute it and/or modify it
REM under the terms of the GNU General Public License as published by the Free
REM Software Foundation; either version 2 of the License, or (at your option)
REM any later version.
REM
REM This program is distributed in the hope that it will be useful, but WITHOUT
REM ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
REM FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
REM details.
REM
REM You should have received a copy of the GNU General Public License along with
REM this program; if not, write to the Free Software Foundation, Inc., 59 Temple
REM Place - Suite 330, Boston, MA 02111-1307, USA

*| wimpslot -min 32K -max 32K
DIM block% 1023,syntax% 255,output% 255:app$="Simulate a click"
SYS "Wimp_Initialise",310,&4B534154,app$ TO ,task%
ON ERROR REPORT:PRINT ERL:SYS "Wimp_CloseDown":END

REM Get command line
SYS "OS_GetEnv" TO comm%
SYS "OS_ReadArgs","name,quit,b=button,i=icon,t=task,w=window",comm%,block%,1023

PROCprocess(block%!8,"Buttons",but%,buts%)
PROCprocess(block%!12,"Icon",icon%,icons%)
PROCprocess(block%!20,"Window",wind%,winds%)
PROCprocess(block%!16,"Task",task%,tasks%)

REM Sometimes Director$Buttons has a strange value in it therefore :
IF but%>2 but%=4


IF winds%=3 THEN
  REM Window handle supplied via command line. Go ahead and use it
  PROCwindow
ELSE
  REM Window handle obtained elsewhere. Check to see if it is valid
  !block%=wind%
  SYS "XWimp_GetWindowInfo",,block%+1 TO ;flags%

  IF (flags% AND 1)=1 THEN
    REM Invalid window handle

    IF tasks%=3 THEN
      REM Valid task handle. Use that instead
      PROCtask
    ELSE
      REM Task handle obtained elsewhere. Check to see it if valid
      SYS "XTaskManager_TaskNameFromHandle",task% TO ;flags%
      IF (flags% AND 1)=1 THEN
        REM Invalid window & task handle. Complain to the user
        ERROR 0,"Invalid window and task handles. Syntax : SimClick [-i[con] <icon handle>] [-b[utton] <mouse button>] [-w[indow] <window handle>] [-t[task] <task handle/name>]"
      ELSE
        REM Valid task handle. Use that instead
        PROCtask
      ENDIF
    ENDIF
  ELSE
    REM Valid window handle. Go ahead and use it
    PROCwindow
  ENDIF

ENDIF

REM Quit
SYS "Wimp_CloseDown"
END

:

DEF PROCwindow
LOCAL x%,y%

!block%=wind%
SYS "Wimp_GetWindowInfo",,block%+1
x%=block%!4:y%=block%!8

block%!4=icon%
SYS "Wimp_GetIconState",,block%

!block%=block%!8+x%
block%!4=block%!12+y%
block%!8=but%
block%!12=wind%
block%!16=icon%
SYS "Wimp_SendMessage",6,block%,wind%,icon%
ENDPROC

:

DEF PROCtask
!block%=0
block%!4=0
block%!8=but%
block%!12=-2
block%!16=-1
SYS "Wimp_SendMessage",6,block%,task%
ENDPROC

:

DEF PROCprocess(ptr%,var$,RETURN str%,RETURN sta%)
LOCAL num%,flags%,str$,taskp%,len%
str$="":str%=0:sta%=0
IF ptr%=0 THEN
  REM Do this if nothing has been supplied via the command line
  IF var$<>"" THEN
    SYS "XOS_ReadVarVal","Director$"+var$,block%+32,991,0,1 TO ;flags%
    IF (flags% AND 1)=1 THEN
      REM Do this if Director$... has not been set, i.e. use defaults
      sta%=1
      CASE var$ OF
        WHEN "Buttons" : str%=4
        WHEN "Icon"    : str%=-1
        WHEN "Task"    : str%=0
        WHEN "Window"  : str%=-2
      ENDCASE
    ELSE
      REM Do this if Director$... has been set
      sta%=2:str%=block%!32
    ENDIF
  ENDIF
ELSE
  REM Do this if a number/string has been supplied via the command line
  sta%=3:str$=FNstring(ptr%)
  SYS "XOS_EvaluateExpression",str$,block%+32,991 TO ,num%,str% ;flags%
  IF num%=0 AND (NOT (flags% AND 1)=1) THEN
    REM str% now holds the number supplied
  ELSE
    REM use the value of str$ to get str%
    str$=FNlcase(str$)
    IF var$="Task" THEN
      REM str$ is the name of a task, str% must return as the task handle
      taskp%=0:str%=0
      REPEAT
        SYS "TaskManager_EnumerateTasks",taskp%,block%+32,16 TO taskp%
        IF FNlcase(FNstring(!(block%+36)))=str$ str%=!tmp%
      UNTIL taskp%<0 OR str%<>0
    ELSE
      REM str$ is from the DATA statements, str% must return the corresp. value
      RESTORE
      READ h$,a$,n%
      WHILE h$<>"End"
        IF h$=var$ AND str$=a$ THEN str%=n%
        READ h$,a$,n%
      ENDWHILE
    ENDIF
  ENDIF
ENDIF
REM PRINT ptr%;" ";var$;" ";str$;" ";str%
ENDPROC

:

DEF FNlcase(text$)
LOCAL Loop%,text2$
text2$=""
FOR Loop%=1 TO LEN(text$)
  chr%=ASC(MID$(text$,Loop%,1))
  IF chr%>64 AND chr%<91 chr%=chr%+32
  text2$+=CHR$(chr%)
NEXT
=text2$

:

DEF FNstring(ptr%):LOCAL a$:a$=""
WHILE ?ptr%>31
  a$+=CHR$(?ptr%):ptr%+=1
  ENDWHILE:=a$

DATA Buttons,select,4
DATA Buttons,menu,2
DATA Buttons,adjust,1

DATA Icon,workarea,-1
DATA Icon,backicon,-2
DATA Icon,closeicon,-3
DATA Icon,titlebar,-4
DATA Icon,togglesizeicon,-5
DATA Icon,scrolluparrow,-6
DATA Icon,verticalscrollbar,-7
DATA Icon,scrolldownarrow,-8
DATA Icon,adjustsizeicon,-9
DATA Icon,scrollleftarrow,-10
DATA Icon,horizontalscrollbar,-11
DATA Icon,scrollrightarrow,-12
DATA Icon,outerwindowframe,-13
DATA Icon,iconiseicon,-14

DATA End,End,0

