-- originally used DaJaRo's shellexecute() code out of
-- "Connect to a Web page" (shell2.zip), thanks, mystery man !
-- wolfritz@king.igs.net

	constant HOME_DIR = current_dir()
	
sequence EXW, fName
object jk, line, tag, whereis

include msgbox.e

procedure paranoid()
-- before anything, check for win32lib.ew in \Euphoria\include
object EuDir,W_exist,Readme,NoTiCe
EuDir = {}  NoTiCe = {}

EuDir = getenv("EUDIR")
EuDir &= "\\include\\win32lib.ew"
W_exist = open(EuDir,"r")
if W_exist = -1 then
  Readme = open("ReadMe.txt","r")
  if Readme != -1 then
    for j = 1 to 7 do
      line = gets(Readme)
      NoTiCe &= line
    end for
  end if
  NoTiCe &= "...and I couldn't find:\n" & EuDir
  jk = message_box(NoTiCe,"WARNING",{MB_ICONERROR,MB_OK})
abort(1)
-- if successful, continue...
end if
end procedure


-- start *paranoid* code --
--paranoid()	-- comment-out, if using EUINC !!
-- end *paranoid* code --

include win32lib.ew 
include euphoria/info.e
without warning

-- control what executive (exec) is used (Fred):
-- (program may now crash if it is not compiled to C or bound)
sequence exec, demo_folder
if version_major() < 4 and version_major() > 1 then
    exec = "exw.exe"
    demo_folder = "\\Demos"
elsif version_major() = 4 then
    exec = "euiw.exe"
    demo_folder = "\\Demos"
else
    exec = "UNDEFINED"
    demo_folder = ""
end if

constant
  mystyle = {WS_CAPTION,WS_SYSMENU,WS_THICKFRAME,WS_MINIMIZEBOX},
  TutorWin = create( Window, "Euphoria Win32 Tutorials",
	      0, 0, 0, 510, 272, mystyle),
  StatBar = create( StatusBar, "",TutorWin,0,0,0,0,0),
  LessonList = create( List, "",
	       TutorWin,38,16,427,102,LBS_NOINTEGRALHEIGHT),

  MenuFile = create( Menu, "&File", TutorWin,0,0,0,0,0),
    MenuExit = create( MenuItem, "E&xit...", MenuFile,0,0,0,0,0),
  MenuView = create( Menu, "&Example", TutorWin,0,0,0,0,0),
    MenuCode = create( MenuItem, "&View", MenuView,0,0,0,0,0),
  MenuShow = create( Menu, "&Synopsis", TutorWin,0,0,0,0,0),
    MenuSyn = create( MenuItem, "&Resource files", MenuShow,0,0,0,0,0),
  MenuHelp = create( Menu, "&Help", TutorWin,0,0,0,0,0),
    MenuTxt = create( MenuItem, "&Text", MenuHelp,0,0,0,0,0),
    MenuAbout = create( MenuItem, "&About", MenuHelp,0,0,0,0,0),

  Button1 = create( PushButton, "Open Selected Lesson",
		    TutorWin, 150, 132, 180, 24, 0 ),
  Button2 = create( PushButton, "Run an Example",
		    TutorWin, 150, 162, 180, 24, 0 ),

  -- Query Window (select an example to view)
  Query = create( Window, "which example ?",TutorWin,200,170,325,75,
		 {WS_CAPTION,WS_DLGFRAME}),
    Button3 = create( PushButton, "",Query,10,10,140,25,0),
    Button4 = create( PushButton, "",Query,170,10,140,25,0),

  -- 'cheap file viewer' Window
  Viewer = create( Window, "File Viewer", TutorWin,
	   Default,Default,640,225,0),
    VText = create( List, "", Viewer,Default,Default,640,200,0),

  -- function not in win32lib...
  zMessageBeep = registerw32Procedure(user32,"MessageBeep",{C_UINT})

----

-- from DaJaRo threed.ew
procedure Threed(integer TutorWin,sequence xys,sequence hilo,integer width )
  integer x1,y1,x2,y2

  x1 = 1
  y1 = 2
  x2 = 3
  y2 = 4

  setPenColor(TutorWin,hilo[2])
  for i = 0 to width do
    drawLine(TutorWin,xys[x1]-i,xys[y1]-i,xys[x2]+i,xys[y1]-i)
    drawLine(TutorWin,xys[x1]-i,xys[y1]-i,xys[x1]-i,xys[y2]+i)
  end for

  setPenColor(TutorWin,hilo[1])
  for i = 0 to width do
    drawLine(TutorWin,xys[x1]-i,xys[y2]+i,xys[x2]+i,xys[y2]+i)
    drawLine(TutorWin,xys[x2]+i,xys[y1]-i,xys[x2]+i,xys[y2]+i)
  end for
end procedure

----

procedure openHTML( integer TutorWin, sequence tag )
  sequence s

  s = ".\\HTML\\" & tag & ".html"
  shellExecute( "open", s, 1 )
end procedure

----

procedure Run_Example( integer TutorWin, sequence tag ) 
  sequence FileTypes, s
--  if chdir( ".\\Demos" ) then
  if chdir( HOME_DIR & demo_folder ) then
    FileTypes = {"EXW", "*.exw"}
    s = getOpenFileName( TutorWin, tag, FileTypes )
--  shellExecute( "open", s, 1 )
    shellExecuteEx ( "open", exec, s, 0, SW_SHOWNORMAL, 0 )
    if chdir( ".." ) then end if
  else
    warnErr( "Could not find 'Demos' directory." )
  end if
end procedure	

----

procedure PaintTutorWin(integer TutorWin)
  sequence xys, hilo
  integer width
  integer x,y
  x = 40
  y = 28
  width  = 5
  xys	 = {5, 5, 498, 195}
  hilo	 = {rgb(240,240,240),rgb(128,128,128)}
  Threed( TutorWin, xys, hilo, width) --an empty frame

-- draw frame around text
  width  = 3
  xys	 = {36,14,466,119}
  hilo	 = { rgb(128,128,128),rgb(240,240,240)}
  Threed( TutorWin, xys, hilo, width )
end procedure

----

procedure center_win(atom win_id)
  sequence loc
  atom xsize, ysize, posx, posy

  loc = getRect(win_id)
  xsize = loc[3]-loc[1]
  ysize = loc[4]-loc[2]
  posx = floor((w32Func(xGetSystemMetrics,{SM_CXFULLSCREEN})/2)-(xsize/2))
  posy = floor((w32Func(xGetSystemMetrics,{SM_CYFULLSCREEN})/2)-(ysize/2))
  setRect( win_id, posx, posy, xsize, ysize, 1 )
end procedure

----

procedure view_lesson( integer self, integer event, sequence params)
  integer lesson

  lesson = getIndex( LessonList )
  tag = getItem( LessonList, lesson )
  tag = tag[1..8]
  openHTML( TutorWin, tag )	
  setFocus( LessonList )
end procedure
setHandler( Button1, w32HClick, routine_id( "view_lesson" ) )

----

procedure on_enter( integer self, integer event, sequence params)
  integer key = params[1]
  if key=13 then view_lesson( self, event, params) end if
end procedure
setHandler( LessonList, w32HKeyDown, routine_id("on_enter") )

----

--procedure on_scroll(atom m,atom w,atom l)
procedure on_scroll( integer self, integer event, sequence params)
--  ? params
  integer m = self
  if m=WM_VSCROLL then
    jk=sendMessage(LessonList,#18E,0,0)
    setIndex(LessonList,jk+1)
    EXW=getItem( LessonList, jk+1 )
    EXW=EXW[79..112]
    setText( StatBar, "EXAMPLE PROGRAMS:  " & EXW )
  end if
end procedure
setHandler( LessonList, w32HEvent, routine_id("on_scroll") )

----

procedure open_exw()
  integer in

  in = getIndex( LessonList )
  tag = getItem( LessonList, in )
  tag = tag[79..94]
  if compare(tag[1],' ') then
    Run_Example( TutorWin, tag )
  end if
  setFocus( LessonList )
end procedure

----

procedure onClick_Button2( integer self, integer event, sequence params)
 open_exw()
end procedure
setHandler( Button2, w32HClick, routine_id( "onClick_Button2" ) )

----

procedure show_prog( integer here )
  EXW = getItem( LessonList, here )
  EXW = EXW[79..112]
  setText( StatBar, "EXAMPLE PROGRAMS:  " & EXW )
end procedure

----

--procedure onPaint_TutorWin(integer x1,integer y1,integer x2,integer y2)
procedure onPaint_TutorWin( integer self, integer event, sequence params)
  PaintTutorWin( TutorWin )
end procedure
setHandler( TutorWin, w32HPaint, routine_id( "onPaint_TutorWin" ) )

----

procedure fill_LessonList()
  atom handle
  object line

  -- fetch the list    
 
  handle = open( "lessons", "r" )
  while 1 do
    line = gets( handle )
    if atom( line ) then exit end if
    line = line[1..length(line)-1]
    addItem( LessonList, line )
  end while
  close( handle )
end procedure

----

procedure onOpen_TutorWin( integer self, integer event, sequence params)
  jk = w32Func(xLoadIcon,{instance(),allocate_string("exw")})
  jk = sendMessage(TutorWin,128,0,jk)
  setWindowBackColor(Query,rgb(0,255,0))
  setFont( LessonList,"System",10,Bold)
  setFont( StatBar,"System",10,Bold)
  setFont( VText,"Courier New",10,0)
  center_win( TutorWin )
  setWindowBackColor( TutorWin, rgb(0,127,127) )
  fill_LessonList()
  setFocus( LessonList )
  setIndex( LessonList,1 )
  whereis = 1
  show_prog( whereis )
end procedure
setHandler( TutorWin, w32HOpen, routine_id( "onOpen_TutorWin" ) )

----

procedure onClick_MenuExit( integer self, integer event, sequence params)
  closeWindow( TutorWin )
end procedure
setHandler( MenuExit, w32HClick, routine_id( "onClick_MenuExit" )  )

----

procedure onClick_MenuAbout( integer self, integer event, sequence params)
object Handle, line ,m_txt
line={}  m_txt={}

Handle = open("theabout", "r")
if Handle != -1 then
  line = gets(Handle) -- discard first
    while 1 do
      line = gets(Handle)
      if atom(line) then
	exit
      end if 
      m_txt &= line
    end while
  close(Handle)
else
  m_txt="...my \"theabout\" file is missing !"
end if
jk = message_box(m_txt,"... a Win32 Tutorial project.",#1000)
end procedure
setHandler( MenuAbout, w32HClick, routine_id("onClick_MenuAbout") )

----

procedure onChange_LessonList( integer self, integer event, sequence params)
  whereis = getIndex( LessonList )
  show_prog( whereis )
end procedure
setHandler( LessonList, w32HChange, routine_id( "onChange_LessonList" ) )

----

procedure onMenu_MenuTxt( integer self, integer event, sequence params)
  shellExecute( "open", "ReadMe.txt", 1 )
end procedure
--onClick[MenuTxt] = routine_id( "onMenu_MenuTxt" )
setHandler( MenuTxt, w32HClick, routine_id( "onMenu_MenuTxt" ) )

----

procedure view_example( integer self, integer event, sequence params )
  object handle, data
  integer here, count

  setText( Viewer, "cheap_file_viewer.     " & fName )
  handle = open( ".\\Demos\\" & fName, "r" )
  if handle > 0 then
    while 1 do
      data = gets( handle )
      if atom( data ) then
	exit
      end if
      data = data[1..length(data)-1]
      if length( data ) > 76 then
	addItem( VText, data[1..76] )
	addItem( VText, "     " & data[77..length( data )] )
      else
	addItem( VText, data & ' ' ) 
      end if
    end while
    close( handle )
    setIndex( VText,1 )
  end if
end procedure
setHandler( Viewer, w32HOpen, routine_id( "view_example" ) )
--onOpen[Viewer] = routine_id( "view_example" )

----

procedure onResize_Viewer( integer self, integer event, sequence params ) --( integer style, integer sizeX, integer sizeY )
--	? params	-- Don't know what it is ...
--  setRect( VText, 0, 0, sizeX, sizeY, 1 )
end procedure 
setHandler( Viewer, w32HResize, routine_id( "onResize_Viewer" ) )
--onResize[Viewer] = routine_id( "onResize_Viewer" )

----

procedure show_child()
  openWindow(Viewer,Normal)
  eraseItems(VText)
  view_example(1,2,"")
end procedure

----

procedure open_Query()
   openWindow( Query,Normal )
   setText( Button3, EXW[1..16] )
   setText( Button4, EXW[19..34] )
   setFocus( Button3 )
   w32Proc( zMessageBeep,{#FFFFFFFF})
end procedure

----

procedure view( integer self, integer event, sequence params )
  if compare( EXW[19], 32 ) = 0 then
	fName = EXW[1..16]
	show_child()
  else 
	open_Query()
  end if
end procedure
setHandler( MenuCode, w32HClick, routine_id( "view" ) )
--onClick[MenuCode] = routine_id( "view" )

----

procedure first_select( integer self, integer event, sequence params )
	closeWindow( Query )
	fName = EXW[1..16]
	show_child()
end procedure
setHandler( Button3, w32HClick, routine_id( "first_select" ) )
--onClick[Button3] = routine_id( "first_select" )

----

procedure second_select( integer self, integer event, sequence params )
	closeWindow( Query )
	fName = EXW[19..34]
	show_child()
end procedure
setHandler( Button4, w32HClick, routine_id( "second_select" ) )
--onClick[Button4] = routine_id( "second_select" )

----

procedure onKey_Button1( integer self, integer event, sequence params )
	atom key = params[1]
	 if key=13 then view_lesson( self, event, params ) end if
end procedure
setHandler( Button1, w32HKeyDown, routine_id( "onKey_Button1" ) )
--onKeyPress[Button1] = routine_id( "onKey_Button1" )

----

procedure onKey_Button2( integer self, integer event, sequence params )
	atom key = params[1]
	 if key=13 then open_exw() end if
end procedure
setHandler( Button2, w32HKeyDown, routine_id( "onKey_Button2" ) )
--onKeyPress[Button2] = routine_id( "onKey_Button2" )

----

procedure onKey_Button3( integer self, integer event, sequence params )
	atom key = params[1]
	 if key=13 then first_select(self,event,params) end if
end procedure
setHandler( Button3, w32HKeyDown, routine_id( "onKey_Button3" ) )
--onKeyPress[Button3] = routine_id( "onKey_Button3" )

----

procedure onKey_Button4( integer self, integer event, sequence params )
	atom key = params[1]
	 if key=13 then second_select(self,event,params) end if
end procedure
setHandler( Button4, w32HKeyDown, routine_id( "onKey_Button4" ) )
--onKeyPress[Button4] = routine_id( "onKey_Button4" )

----

procedure synopsis(atom self, atom event, sequence params)
	tag = "synopsis"
	openHTML( TutorWin, tag )     
end procedure
setHandler( MenuSyn, w32HClick, routine_id( "synopsis" ) )
--onClick[MenuSyn] = routine_id( "synopsis" )

----

--------- start ---------
WinMain( TutorWin, Normal )
-------------------------
