/* MakeImageMap.ifx by Sigbjørn Skjæret
** $VER: MakeImageMap.ifx v1.3 (09.12.95)
**
** HTML imagemap-tool...
*/

options results
parse arg cmd x y .

url=''
name=''
undid=0
if exists('T:imagemap.temp') then
do
 if(cmd='SELECTUP') then Wedge SELECTUP MakeImageMap Remove
 else Wedge TOOLUP MakeImageMap Remove
 Wedge SELECTDOWN WriteImageMap Remove
 call open(check,'T:imagemap.temp',R)
 chk = readln(check)
 call close(check)
 if(substr(chk,2,8)='MAP NAME') then
 do
  RequestString '"Enter URL of this area:"' '"http://"';url=result
  call open(temp,'T:imagemap.temp',A)
  if(url='') then call writeln(temp,'" NOHREF>')
  else call writeln(temp,'" HREF="'url'">')
  call close(temp)
  call StartClient
 end
 else
 do
  call open(temp,'T:imagemap.temp',A)
  call writech(temp,'0A'x)
  call close(temp)
  call StartServer
 end
end

GetPrefs LoadPath;path=result
RequestFile '"Select file to build map from..."' '"'path'"';file=result
if(rc~=0) then exit
LoadBuffer '"'file'"' FORCE
RequestResponse 'Are you making a Server Side imagemap?'
if(rc~=0) then
do
 RequestString '"Enter name of Client Side imagemap:"';name=result
 call open(temp,'T:imagemap.temp',W)
 call writeln(temp,'<MAP NAME="'name'">')
 call close(temp)
 call StartClient
end
RequestString '"Enter URL of default area:"' '"http://"';url=result
call open(temp,'T:imagemap.temp',W)
call writeln(temp,'default 'url)
call close(temp)

StartServer:
 Gadget.1 = 'L/50/15/2/2/Map:'
 Gadget.2 = 'B/20/25/Point/1001'
 Gadget.3 = 'B/20/40/Rectangle/1002'
 Gadget.4 = 'B/20/55/Circle/1003'
 Gadget.5 = 'B/20/70/Polygon/1004'
 Gadget.6 = 'L/155/60/2/2/Image:'
 Gadget.7 = 'B/130/70/Undo/1009'

 ComplexRequest '"MakeServerImageMap"' 7 Gadget 250 110;bt=rc
 if(rc~=0 & rc<1000 | rc>1009) then call Quit

 if(bt=0) then call Write(server)
 if(bt=1009) then call Undo(server)
 if(bt=1001) then
 do
  RequestNotify 'Sorry, this isn''t implemented yet!'
  call StartServer
/*
  call Store(point,server)
  DrawTool FreeDraw
  Wedge SELECTDOWN WriteImageMap
  Wedge SELECTUP MakeImageMap
  exit
*/
 end
 if(bt=1002) then
 do
  call Store(rect,server)
  DrawTool Box
 end
 if(bt=1003) then
 do
  call Store(circle,server)
  DrawTool Oval
 end
 if(bt=1004) then
 do
  call Store(poly,server)
  DrawTool Poly
 end

 Wedge SELECTDOWN WriteImageMap
 Wedge SELECTUP WriteImageMap
 Wedge TOOLUP MakeImageMap
exit

StartClient:
 Gadget.1 = 'L/50/15/2/2/Map:'
 Gadget.2 = 'B/20/25/Rectangle/1001'
 Gadget.3 = 'L/155/15/2/2/Image:'
 Gadget.4 = 'B/130/25/Undo/1009'

 ComplexRequest '"MakeClientImageMap"' 4 Gadget 250 70;bt=rc
 if(rc~=0 & rc<1000 | rc>1009) then call Quit

 if(bt=0) then call Write(client)
 if(bt=1009) then call Undo(client)
 if(bt=1001) then
 do
  call Store(rect,client)
  DrawTool Box
 end

 Wedge SELECTDOWN WriteImageMap
 Wedge SELECTUP WriteImageMap
 Wedge TOOLUP MakeImageMap
exit

Store: procedure
 parse arg dt,side
 if(side='SERVER') then
 do
  if(dt='POINT') then dt='point'
  if(dt='RECT') then dt='rect'
  if(dt='CIRCLE') then dt='circle'
  if(dt='POLY') then dt='poly'
  RequestString '"Enter URL of this area:"' '"http://"';url=result
  call open(data,'T:imagemap.data',W)
  call writeln(data,dt url' ')
  call close(data)
 end
 else
 do
  call open(data,'T:imagemap.data',W)
  call writeln(data,'<AREA SHAPE="'dt'" COORDS="')
  call close(data)
 end
return

Undo: procedure expose undid
 parse arg side
 if(undid) then
 do
  call open(undo,'T:imagemap.undo',R)
  call open(temp,'T:imagemap.temp',A)
  un=readln(undo)
  call writeln(temp,un)
  call close(undo)
  call close(temp)
  Undo
  undid=0
  if(side='SERVER') then call StartServer
  else call StartClient
 end
 call open(temp,'T:imagemap.temp',R)
 lines=0
 do until eof(temp)
  call readln(temp)
  lines=lines+1
 end
 if(lines=1) then
 do
  call close(temp)
  if(side='SERVER') then call StartServer
  else call StartClient
 end
 call seek(temp,0,B)
 call open(tmp,'T:imagemap.tmp',W)
 do i=1 to lines-2
  und=readln(temp)
  call writeln(tmp,und)
 end
 call open(undo,'T:imagemap.undo',W)
 un=readln(temp)
 call writeln(undo,un)
 call close(undo)
 call close(temp)
 call close(tmp)
 address command 'copy >NIL: T:imagemap.tmp T:imagemap.temp'
 address command 'delete >NIL: T:imagemap.tmp'
 Undo
 undid=1
 if(side='SERVER') then call StartServer
 else call StartClient
return

Write: procedure
 parse arg side
 if ~exists('T:imagemap.temp') then call Quit
 if(side='CLIENT') then
 do
  call open(temp,'T:imagemap.temp',A)
  call writeln(temp, '</MAP>')
  call close(temp)
 end
 RequestFile '"Writing imagemap-file..."';sfile=result
 if(rc~=0) then exit

 if exists(sfile) then
 do
  RequestResponse 'Do you want to overwrite this file?'
  if(rc~=0) then call Write
 end
 address command 'copy >NIL: T:imagemap.temp "'sfile'"'

Quit:
 if exists('T:imagemap.temp') then address command 'delete >NIL: T:imagemap.temp'
 if exists('T:imagemap.undo') then address command 'delete >NIL: T:imagemap.undo'
 ClearBuffer FORCE
 UnlockInput
 UnlockGUI
exit
