without type_check
without warning
include win32lib.ew

atom map, jk
integer x1, x2, y1, y2
sequence loc2

constant
MyWin = create(Window,"..fancy that !",0,0,0,325,448,0),

-- untitled message-box
Win2    = create( Window, "", MyWin, 0, 0, 334, 290, {WS_POPUP,WS_DLGFRAME} ),
Bitmap1 = create( Bitmap, "", Win2,  2, 51, 322, 120, 0 ),
about1 = "This custom  ABOUT  box demo has\n" &
          "a web-link 'hot spot' in the bitmap.",
about2 = "This program was written in the\n" &
          "EUPHORIA programming language,\n" &
          "available at http://www.rapideuphoria.com,\n" &
          "using win32lib by David Cuny.",
Title1 = create( CText, about1, Win2, 2, 10, 322, 40, 0 ),
Title2 = create( CText, about2, Win2, 2, 175, 322, 80, 0 ),
myhint = create( CText, "Go to rapideuphoria.com", Bitmap1, 0, 0, 170, 18, or_all({WS_CHILD,ES_CENTER,WS_BORDER} ) ),
Button1 = create( PushButton, "OK", Win2, 128, 255, 70, 25, 0 ),
MenuHlp = create( Menu, "&Help", MyWin, 0, 0, 0, 0, 0 ),
HlpAbout= create( MenuItem, "&About", MenuHlp, 0, 0, 0, 0, 0 ),

--to play MIDI
  mciSendString = registerw32Function( winmm, "mciSendStringA", {C_INT, C_INT, C_INT, C_INT}, C_INT )

setWindowBackColor(MyWin,rgb(127,127,255))
setWindowBackColor(Win2,rgb(0,127,127))
setWindowBackColor(Title1,rgb(0,127,127))
setWindowBackColor(Title2,rgb(0,127,127))

-- MIDI play routines, from midi.ew by Brent Hugh. 
procedure MciSendString(sequence String, integer a, integer b, integer c)
   integer result
   atom outMsg
   
   outMsg = acquire_mem( 0, String )
   result = w32Func( mciSendString, {outMsg, a, b, c} )
end procedure

procedure PlayMidiFile(sequence MidiFile)
   sequence outMsg
   --load the file  "A_Song" is the handle of the loaded song
   outMsg="open " & MidiFile & " type sequencer alias A_Song "
   MciSendString (outMsg, 0, 0, 0)
   --play the loaded file
   outMsg="play A_Song"
   MciSendString (outMsg, 0, 0, 0)
end procedure

procedure CloseMidi()
--need to close the device to re-use it !.
   sequence outMsg
   outMsg="close A_Song"
   MciSendString (outMsg, 0, 0, 0)
end procedure
-- end of midi code.

--slightly modified loadBitmapFromFile2()
--reads BMP from file to memory.
function loadBmpResource( sequence fileName, atom fSize)
  atom        bmFile, bmInfoHeader, bmBits, bmColors, hdc, hDib
  integer     hFile, byte

  hFile = open( fileName, "rb" )
  bmFile = acquire_mem(0,fSize )
  for i = 0 to fSize-1 do       
  byte = getc( hFile )
    poke( bmFile+i, byte )
  end for
  close(hFile)
   
  bmInfoHeader = bmFile + SIZEOF_BITMAPFILEHEADER
  bmBits = bmFile + fetch( bmFile, bfOffBits ) 
  bmColors = bmInfoHeader + SIZEOF_BITMAPINFOHEADER
  hdc = getDC( Screen )

  hDib = w32Func( xCreateDIBitmap, {
  hdc,address( bmInfoHeader, bmiHeader ),
  CBM_INIT,
  bmBits,
  bmInfoHeader,
  DIB_RGB_COLORS} )
  releaseDC( Screen )
  release_mem( bmFile )
  return hDib
end function

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 about(atom self, atom event, sequence params)
	setVisible(myhint,0)
	openWindow(Win2,Modal)
	PlayMidiFile(".\\Bin\\roary2.mid")
end procedure

procedure start_up(atom self, atom event, sequence params)
	map=loadBmpResource( ".\\Bin\\eu.bmp", 19798 )
	setBitmap( Bitmap1, map )
	center_win(Win2)
	loc2=getRect(Bitmap1)
	x1=loc2[1]+70
	x2=loc2[3]-80
	y1=loc2[2]+17
	y2=loc2[4]+10
end procedure

procedure close1(atom self, atom event, sequence params)
	closeWindow(Win2)
	CloseMidi()
end procedure

procedure on_bitmap(atom self, atom event, sequence params)   -- old style: (integer event, atom x, atom y, atom shift)
	integer mouse_event mouse_event = params[1]	-- fetch values from params (new style)
	atom x   x = params[2]
	atom y   y = params[3]
	
	if x > x1 and x < x2 and y > y1 and y < y2 then
		if mouse_event = MouseMove then
			setRect(myhint,x-85,y-67,170,20,1 )
			setVisible(myhint,1)
		end if
		if mouse_event = LeftDown then
			setVisible(myhint,0)
			close1(self, event, params)
			CloseMidi()
			-- open IE with the url;
			-- Modal locks you into it (and out of all else) until you close it
			shellExecute("open","http://www.rapideuphoria.com", Modal)
		end if
	else
		setVisible(myhint,0)
	end if
	
end procedure

procedure kill_prog(atom self, atom event, sequence params)
	CloseMidi()
	jk = w32Func( xDeleteObject, {map} )
end procedure

setHandler( MyWin, w32HOpen,  routine_id( "start_up" ) )
setHandler( HlpAbout, w32HClick,  routine_id("about") )
setHandler(Button1, w32HClick,  routine_id("close1") )
setHandler( Win2, w32HMouse,  routine_id("on_bitmap") )
setHandler( MyWin, w32HClose, routine_id("kill_prog") )

WinMain(MyWin, Normal)
--end--
