-- This program tries to emulate the features of the
-- getWindowInfo() function in win32lib,
-- version 0.55.1 +, which doesn't exist
-- in Win95, by using substitute routines.
-- if you have a Win 'version' not listed here,
-- please send me information so I can add
-- it to the list.
-- wolfritz@king.igs.net -- 2/22/01
-- < I used win32lib version 0.20d to get 32s information  ;-) >

include win32lib.ew
without warning

atom
winINFO_Style,
winINFO_ExStyle,
winINFO_WindowStatus,
winINFO_WindowType,
winINFO_CreatorVersion

-- OSVERSIONINFO structure, ...and function.  
constant
zOSVersionInfoSize = allot( Long ),
zMajorVersion = allot( Long ),
zMinorVersion = allot( Long ),
zBuildNumber = allot( Long ),
zPlatformId = allot( Long ),
zCSDVersion = allot( 128 ), 
SIZEOF_zOSVERSIONINFO = allotted_size(),

zGetVersionEx=registerw32Function(
    kernel32,"GetVersionExA",{C_POINTER},C_INT),

-- gets this window's class name.
zGetClassName=registerw32Function(
    user32,"GetClassNameA",{C_INT,C_POINTER,C_INT},C_INT)

-- the windows
constant
win=create(Window,"in_foo  ;-)",0,10,10,430,230,0),
out1=create(LText,"",win,0,2,422,18,0),
out2=create(LText,"",win,0,23,422,18,0),
out3=create(LText,"",win,0,44,422,18,0),
out4=create(LText,"",win,0,65,422,18,0),
out5=create(LText,"",win,0,86,422,18,0),
out6=create(LText,"",win,0,107,422,18,0),
out7=create(LText,"",win,0,128,422,18,0),
out8=create(LText,"",win,0,149,422,36,0),
mytimer=1

-- to re-display co-ordinates and 'active' indicator.
setTimer(win,mytimer,500)

-- add some color.
for i=out1 to out7 by 2 do
  setWindowBackColor(i,Green)
end for

-- a function for no particular reason...
-- returns main, and client area co-ordinates.
-- 'client' here, includes toolbars, status bars, etc.
function getLocations( atom id )
atom
winINFO_WLeft,
winINFO_WTop,
winINFO_WRight,
winINFO_WBottom,
winINFO_CLeft,
winINFO_CTop,
winINFO_CRight,
winINFO_CBottom,
winINFO_xWindowBorders,
winINFO_yWindowBorders

sequence temp_s 

-- get the main window co-ordinates
temp_s=getWindowRect(id)
winINFO_WLeft=temp_s[1]
winINFO_WTop=temp_s[2]
winINFO_WRight=temp_s[3]
winINFO_WBottom=temp_s[4]

-- get the client rectangle,
temp_s=getClientRect(id)
-- and calculate it's co-ordinates.
winINFO_xWindowBorders=((winINFO_WRight-winINFO_WLeft)-temp_s[3])/2

winINFO_CLeft=winINFO_WLeft+winINFO_xWindowBorders
winINFO_CTop=winINFO_WBottom-winINFO_xWindowBorders-temp_s[4]
winINFO_CRight=winINFO_WRight-winINFO_xWindowBorders
winINFO_CBottom=winINFO_WBottom-winINFO_xWindowBorders

return { winINFO_WLeft,
winINFO_WTop,
winINFO_WRight,
winINFO_WBottom,
winINFO_CLeft,
winINFO_CTop,
winINFO_CRight,
winINFO_CBottom }

end function


-- get this program's handle.
atom id
id=getHandle(win)


procedure show()
atom name,mem,jk,major,minor,build
sequence Creator,platform,moreinfo
moreinfo={}

-- style flags.
winINFO_Style=w32Func(xGetWindowLong,{id,GWL_STYLE})
setText(out2,sprintf(" window style  #%x",{winINFO_Style}))

-- extended style flags.
winINFO_ExStyle=w32Func(xGetWindowLong,{id,GWL_EXSTYLE})
setText(out3,sprintf(" window extended style  #%x",{winINFO_ExStyle}))

-- not usually used by win32lib, which uses named classes ?.
winINFO_WindowType=w32Func(xGetWindowLong,{id,GCW_ATOM})
setText(out5,sprintf(" window class atom  %d",{winINFO_WindowType}))

-- get the window's class name.
name=acquire_mem(0,64) -- long enuf ?
jk=w32Func(zGetClassName,{id,name,64})
setText(out6,sprintf(" window class name  %s",{peek_string(name)}))
release_mem(name)

-- set up the structure which returns info.
mem=acquire_mem(0,SIZEOF_zOSVERSIONINFO)
store(mem,zOSVersionInfoSize,SIZEOF_zOSVERSIONINFO)

-- get three version numbers.
jk=w32Func(zGetVersionEx,{mem})
major=fetch(mem,zMajorVersion)
minor=fetch(mem,zMinorVersion)

-- get platform, and turn into string.
jk=fetch(mem,zPlatformId)
if jk=0 then platform="32s"  -- < chuckle! >
  elsif jk=1 then platform="Win"
  elsif jk=2 then platform="NT"
  else platform="unknown ?"
end if

-- get build, and turn into string.
if jk=1 then
  build=lo_word(fetch(mem,zBuildNumber))
else
  -- for NT platforms..
  build=fetch(mem,zBuildNumber)
end if
if build=172 then Creator="Win3.11"  -- < eh? >
  elsif build=950 then Creator="Win95"
  elsif build=1111 then Creator="Win95 OSR2"
  elsif build=1998 then Creator="Win98"
  elsif build=2222 then Creator="Win98 SE"
  elsif build=3000 then Creator="WinME"
  elsif build=1381 then Creator="WinNT v4"
  elsif build=2195 then Creator="Win2K"
  -- thanks to Brian Broker for the next...
  elsif build=2437 then Creator="WinXP beta"
  elsif build=2600 then Creator="WinXP"
  else Creator="unknown to me."
end if
setText(out7,sprintf(
      " Major %d,  Minor %d,  build %d,  Platform %s   -- version %s",
       {major,minor,build,platform,Creator}))

-- sometimes, the following returns nothing.
moreinfo=peek_string(mem+20)
if length(moreinfo) < 2 then moreinfo = " \"no information\"" end if
setText(out8,sprintf(" CSDVersion  %s",{moreinfo}))

release_mem(mem)
end procedure
----
onOpen[win]=routine_id("show")


procedure update(atom tick)

-- update the following every half-second,
-- in case the user drags the window, or
-- sets focus to the desktop, etc...
setText(out1,sprintf(
   " 'main' co-ordinates %d,%d,%d,%d -- 'client' co-ordinates %d,%d,%d,%d",
   getLocations(win)))

winINFO_WindowStatus=w32Func(xGetActiveWindow,{})
if winINFO_WindowStatus > 0
  then
  setText(out4," window is active")
  else
  setText(out4," window is not active")
end if
end procedure
----
onTimer[win]=routine_id("update")

----

WinMain(win,Normal)
-------------------
