# problems:
#  scaling -- figure out how to scale using ImageMagick
#  cropping -- figure out how to crop using ImageMagick
#  color: B&W actually show up as Black and Grey
#  color: need to negate, the Black parts should be White, and vice versa
#         (can't figure out how to negate BMP using ImageMagick)
#  scroll bars: add a scroll bar to the window around the label area.
#  dynamic sizing: figure out size of label area from window dimensions,
#                  minus scrollbars (if they are needed), minus other fixed
#                  size stuff in the window.
# In addition to scroll bars, need an intuitive scaling technique.
#  100%, scale width to fit, scale to fit, seem like good choices
#  % number might be useful too.
#  Need default scale, to be set & saved


# & doc_setup ( <<END_OF_TEXT, '-h' );
#  Purpose: sample program for experimenting with Win32::GUI graphics options
# 
#  Options:
# 
# O-h
#     Display this help text, and perform no other action.
# O-d
#     Specify a debug level by the number of instances of this parameter
# END_OF_TEXT
# 
# $debug = $opt{'-d'};

$filename = "d:\\test\\file.tif";

my ( $image );
require Image::Magick;
$image = Image::Magick -> new;
$errstr = $image -> Read ( $filename );
$errstr = $image -> Write ( 'xxx.bmp' );
# $errstr = $image -> Read ( 'xxx.bmp' );
# $errstr = $image -> Negate (); # seems not to work on BMP files ???
# $errstr = $image -> Write ( 'xxx.bmp' );

use Win32::API;
use Win32::GUI;
$bitmap = Win32::GUI::Bitmap -> new ( 'xxx.bmp' );
( $bmwid, $bmhei, $bmcolor_planes, $bmbpp ) = $bitmap -> Info ();
print "bmwid=$bmwid  bmhei=$bmhei  cp=$bmcolor_planes  bpp=$bmbpp\n";

$font = Win32::GUI::Font->new ( -name => "Times New Roman", -size => 18, );
& arrange_setup;

  $mww = 505;
  $mwh = 404;
  $mw = Win32::GUI::Window->new (
    -name => 'Main',
    -text => $toolname . ' Main Window',
    -width => $mww, -height => $mwh,
    -font => $font,
#    -parent => $mwtemp,
    -style => WS_OVERLAPPEDWINDOW,
#    -menu => $global_mmenu,
#    -icon => $icon, # no effect: without below SetIcon, we get the Camel icon
    -maximizebox => 0,
#    -background => [ 255, 255, 255 ], # white
#    -foreground => [ 0, 0, 0 ], # black
    );
  $sw = $mw->ScaleWidth();
  $sh = $mw->ScaleHeight();
  $ww = $mw->Width();
  $wh = $mw->Height();
  $ncw = $ww - $sw;
  $nch = $wh - $sh;
  print "sw = $sw  sh = $sh  ncw = $ncw  nch = $nch\n";

  $overall_width = 0;
  $overall_height = 0;

  # label_bitmap

  # AddButton - adds frame, no other effect obvious vs AddLabel
  $label_bitmap = & wrapper ( $mw, 'AddLabel', 
    -name => 'label_bitmap',
    -text => 'abc',
    -bitmap => $bitmap,
    -top => 0, -left => 0,
    -width => $bmwid, -height => $bmhei,
#    -background => [ 255, 255, 255 ], # white
#    -foreground => [ 0, 0, 0 ], # black
    );

  $label_bitmap -> SetImage ( $bitmap );
  $mw -> Show ();
  Win32::GUI::Dialog();

exit ( 0 );
#####################################
sub overall
{ print "last: N $lastname T $lasttop L $lastleft B $lastbottom R $lastright",
    "\toverall: $overall_height  $overall_width\n";
  $overall_width = $lastright  if $overall_width < $lastright;
  $overall_height = $lastbottom  if $overall_height < $lastbottom;
}
#####################################
sub wrapper
{ my ( $w, $func, %p ) = @_;
  my ( $control );
  $lastname = $p{'-name'};
  $lasttop = $p{'-top'};
  $lastleft = $p{'-left'};
  if ( $func eq 'AddListBox' )
  { $p{'-width'} = 120  unless defined $p{'-width'};
    $p{'-height'} = 40  unless defined $p{'-height'};
  }
  $control = $w->$func ( %p, %std );
  $lastbottom = $lasttop + $control->Height();
  $lastright = $lastleft + $control->Width();
  & overall ();
  return $control;
}
#####################################
sub make_get_work_area
{ my $spi;
  $spi = new Win32::API ( 'user32', 'SystemParametersInfo',
    [ 'I', 'I', 'P', 'I' ], 'I' );
  return $spi;
}
#####################################
sub call_get_work_area
{ my ( $spi ) = @_;
  my ( $rect ) = pack ( "LLLL", 0, 0, 0, 0 );
  my ( $ret ) = $spi -> Call ( 48, 0, $rect, 0 );
  unless ( $ret )
  {
  }
  return unpack ( "LLLL", $rect );
}
#####################################
{ my $spi;

  sub working_area
  { my ( $winx, $winy, $winw, $winh ) = @_; # proposed position, actual size

    # If no parameters are passed in, this function returns the working area
    # of the desktop, ( left, top, right, bottom ).  If parameters are passed
    # in, it returns the "best" ( left, top, width, height ) position for the
    # specified window, starting with the proposed position, adjusting it so
    # that as much as possible of the window will be visible, and further
    # adjusting it so that the top left corner of the window will be visible.
    # Negative positions are interpreted as "bottom" or "right", asappropriate.

    # first, obtain the non-toolbar area of the desktop.

    $spi = & make_get_work_area ()  unless $spi;
    print "$spi\n";
    my ( @limits );
    @limits = & call_get_work_area ( $spi );

    print "limits: @limits\n";

    # limits returned unless all parameters supplied
    return @limits  unless defined $winx  &&   defined $winy
      &&  defined $winw  &&  defined $winh; 

    print "before adjustments: x=$winx  y=$winy  w=$winw  h=$winh\n";
    $winy = $limits [ 3 ] - $winh
      if $winy < 0  ||  $winy + $winh > $limits [ 3 ];
    $winx = $limits [ 2 ] - $winw
      if $winx < 0  ||  $winx + $winw > $limits [ 2 ];
    $winy = $limits [ 1 ]  if $winy < $limits [ 1 ];
    $winx = $limits [ 0 ]  if $winx < $limits [ 0 ];

    print "x=$winx  y=$winy  w=$winw  h=$winh\n";
    return ( $winx, $winy, $winw, $winh );
  }
}
#####################################
sub arrange_setup
{ # How big is the desktop ???
  my $desk = Win32::GUI::GetDesktopWindow();
  $dw = Win32::GUI::Width($desk);
  $dh = Win32::GUI::Height($desk);
  print "Desktop is ${dw}x$dh\n";
  & working_area ();

  %std = (
    -font => $font,
    -foreground => [0,0,255],
    );

  %std_lb = (
    -style => WS_VSCROLL | WS_CHILD | WS_VISIBLE | 1,
    -menu => 1,
    );
}
