---------------------------------
-- RGBview.exw by Brian Broker --
---------------------------------
-- (last revised JAN 2000)
-- Description: a simple tool for viewing RGB colors
-- (demonstrates use of scroll bars and pixmaps)

include win32lib.ew
without warning

constant
  Title   = "RGBview",
  Version = "1.0"

-- these integer values are used to keep track of
--   the colors defined by the three scroll bars
integer red, green, blue
  red = 128
  green = 128
  blue = 128

constant 
  BGcolor = getSysColor( COLOR_BTNFACE ),
  RGBWin = create( Window, Title, 0, Default, Default, 470, 210, 0 ),
-- using a pixmap buffer takes a load off of the repaint routine
  Buffer = create( Pixmap, "", 0, 0, 0, 470, 200, 0 ),
  About = create( DefPushButton, "About", RGBWin, 360, 10, 80, 30, 0 ),

-- RGB value display buffers
-- these provide a way to display colored text that can
--  be easily updated and displayed without a flicker
  RedVal   = create( Pixmap, "", 0, 0, 0, 85, 15, 0 ),
  GreenVal = create( Pixmap, "", 0, 0, 0, 85, 15, 0 ),
  BlueVal  = create( Pixmap, "", 0, 0, 0, 85, 15, 0 ),

-- create scroll controls
  RedScroll   = create( HScroll, "Red", RGBWin, 10, 50, 305, 16, 0 ),
  GreenScroll = create( HScroll, "Green", RGBWin, 10, 100, 305, 16, 0 ),
  BlueScroll  = create( HScroll, "Blue", RGBWin, 10, 150, 305, 16, 0 )


----------------------
-- Misc. procedures --
----------------------
-- clear Window/Pixmap to specified color
--  this is a re-usable routine for Windows and Pixmaps;
--  note that it does not restore the original pen color
procedure clearWindow( integer id, integer color )
  sequence p   p = getCtlSize( id )
  
  setPenColor( id, color )
  drawRectangle( id, 1, 0, 0, p[1], p[2] )
end procedure

------------------------------------------------------------
-- draw a sunken windows-like border
--  this is a re-usable routine, just use the same x and y values
--  as the rectangle that wants a border
procedure draw_Border( integer id, integer x1, integer y1, integer x2, integer y2 )
  integer c

  setPenColor( id, Black )
  drawLine( id, (x1-1), (y1-1), (x2+1), (y1-1) )
  drawLine( id, (x1-1), (y1-1), (x1-1), (y2+1) )

  c = rgb( 128, 128, 128 )
  setPenColor( id, c )
  drawLine( id, (x1-2), (y1-2), (x2+2), (y1-2) )
  drawLine( id, (x1-2), (y1-2), (x1-2), (y2+2) )

  c = rgb( 224, 224, 224 )
  setPenColor( id, c )
  drawLine( id, x2, y2, (x1-1), y2 )
  drawLine( id, x2, y2, x2, (y1-1) )

  setPenColor( id, BrightWhite )
  drawLine( id, (x2+1), (y2+1), (x1-2), (y2+1) )
  drawLine( id, (x2+1), (y2+1), (x2+1), (y1-2) )
end procedure

------------------------------------------------------------

-- update the color view 'window'
procedure update_Color()
  integer color
  
  color = rgb( red, green, blue )
  -- update both Window and Buffer
  setPenColor( RGBWin, color)
  drawRectangle( RGBWin, 1, 350, 58, 450, 158 )
  setPenColor( Buffer, color)
  drawRectangle( Buffer, 1, 350, 58, 450, 158 )
end procedure

------------------------------------------------------------

-- update a value display buffer
procedure updateRGBval( integer id, integer color, sequence string )
  -- clear buffer
  clearWindow( id, BGcolor )

  -- print new value in buffer
  setTextColor( id, color )
  wPuts( id, string )
end procedure

------------------------------------------------------------

-- initialize window display buffer
procedure init_Buffer()
  clearWindow( Buffer, BGcolor )

  draw_Border( Buffer, 350, 58, 450, 158 )
  update_Color()

  setTextColor( RGBWin, Black )

  setFont( Buffer, "Courier", 12, Bold+Underline )
  setPenPos( Buffer, 120, 5 )
  wPrintf( Buffer, "DEC", {} )
  setPenPos( Buffer, 173, 5 )
  wPrintf( Buffer, "HEX", {} )

  setFont( Buffer, "Arial", 14, Bold )

  setTextColor( Buffer, BrightRed )
  setPenPos( Buffer, 10, 25 )
  wPrintf( Buffer, "Red:", {} )
  copyBlt( Buffer, 120, 30, RedVal )

  setTextColor( Buffer, Green )
  setPenPos( Buffer, 10, 75 )
  wPrintf( Buffer, "Green:", {} )
  copyBlt( Buffer, 120, 80, GreenVal )

  setTextColor( Buffer, BrightBlue )
  setPenPos( Buffer, 10, 125 )
  wPrintf( Buffer, "Blue:", {} )
  copyBlt( Buffer, 120, 130, BlueVal )
end procedure


-----------------------------------------
-- procedures for handling scroll bars --
-----------------------------------------

-- Red scroll bar handler
procedure onScroll_RedScroll( integer self, integer event, sequence params )
  integer value = params[1]
  
  red = value - 1
  update_Color()
  updateRGBval(RedVal,BrightRed,sprintf("%3d = #%02x",{red,red}) )
  -- update both Window and Buffer
  copyBlt( RGBWin, 120, 30, RedVal )
  copyBlt( Buffer, 120, 30, RedVal )
end procedure
setHandler( RedScroll, w32HScroll, routine_id( "onScroll_RedScroll" ) )

------------------------------------------------------------

-- Green scroll bar handler
procedure onScroll_GreenScroll( integer self, integer event, sequence params )
   integer value = params[1]
   
  green = value - 1
  update_Color()
  updateRGBval(GreenVal,Green,sprintf("%3d = #%02x",{green,green}) )
  -- update both Window and Buffer
  copyBlt( RGBWin, 120, 80, GreenVal )
  copyBlt( Buffer, 120, 80, GreenVal )
end procedure
setHandler( GreenScroll, w32HScroll, routine_id( "onScroll_GreenScroll" ) )

------------------------------------------------------------
-- Blue scroll bar handler
procedure onScroll_BlueScroll( integer self, integer event, sequence params )
  integer value = params[1]
  
  blue = value - 1
  update_Color()
  updateRGBval(BlueVal,BrightBlue, sprintf("%3d = #%02x",{blue,blue}) )
  -- update both Window and Buffer
  copyBlt( RGBWin, 120, 130, BlueVal )
  copyBlt( Buffer, 120, 130, BlueVal )
end procedure
setHandler( BlueScroll, w32HScroll, routine_id( "onScroll_BlueScroll" ) )

--------------------------------------------------------------------------
-- display information about the program when 'About' button is clicked --
procedure aboutButton( integer self, integer event, sequence params )
  integer result

  result = message_box( Title & "  Version " & Version & "\n\n" &
                        "by Brian K. Broker\n\n" &
                        "Developed using David Cuny's Win32Lib",
                        "About...",
                        MB_ICONINFORMATION+MB_APPLMODAL )
end procedure
setHandler( About, w32HClick, routine_id( "aboutButton" ) )

------------------------------------------------------------
-- repaint window by copying Buffer to Window
procedure onPaint_RGBWin( integer self, integer event, sequence params )
  copyBlt( RGBWin, 0, 0, Buffer )
end procedure
setHandler( RGBWin, w32HPaint, routine_id( "onPaint_RGBWin" ) )

------------------------------------------------------------
-- set up scroll controls
procedure onOpen_RGBWin( integer self, integer event, sequence params )
  -- set font for RGB value display buffers
  setFont( RedVal, "Courier", 12, 0 )
  setFont( GreenVal, "Courier", 12, 0 )
  setFont( BlueVal, "Courier", 12, 0 )

  -- set range of scroll bars
  setScrollRange( RedScroll, 1, 256 )
  setScrollRange( GreenScroll, 1, 256 )
  setScrollRange( BlueScroll, 1, 256 )

  -- set scroll line/page up/down increments
  setScrollChange( RedScroll, 1, 16 )
  setScrollChange( GreenScroll, 1, 16 )
  setScrollChange( BlueScroll, 1, 16 )
        
  -- set scroll bars to default color (defined above)
  setScrollPos( RedScroll, red )
  setScrollPos( GreenScroll, green )
  setScrollPos( BlueScroll, blue )

  -- initialize window display buffer
  init_Buffer()
end procedure
setHandler( RGBWin, w32HOpen, routine_id( "onOpen_RGBWin" ) )

-------- start ----------
-------------------------
WinMain( RGBWin, Normal )
-------------------------