Displaying an image
Now we have a list of images, we might want to display them.
Here is one way.
In MPI Tool palette switch to the Graphics card and drag a rect graphic onto your window, name it
ImageDisplay.
Edit the script of the field and add this script:
on clicklist
put the hilitedLines of me into tLine
--get the line clicked and the nthat line of the list of paths
put line tline of the uImagePaths of me into tfile
set the pictureData of cd grc "ImageDisplay" to file tfile
end clicklist
Click on a line in the field and the file will be displayed.
There a couple of problems. One, if you shilf click the field and select more than one graphic the script will fail (as will the script in the clear image button)
To avoid this select the field with the pointer tool
(Control Tab switchs between pointer and browse in MPI)
Hilite the
SelOne checkbox in the PI.
The other problem is that if your image is bigger than the window it will flow over and out of the window. What we will do here is use a function that will return the size to sclae the image to.
function shrinkTofit pWidth,pHeight,pmaxWidth,pMaxHeight
if pWidth>pMaxWidth
then
put pwidth/pmaxWidth into tDiv
put round (pWidth/tDiv) into pWidth
put round (pHeight/tDiv) into pHeight
end if
if pHeight>pMaxHeight
then
put pHeight/pMaxHeight into tDiv
put round (pWidth/tDiv) into pWidth
put round (pHeight/tDiv) into pHeight
end if
return pwidth&Comma&pHeight
end shrinkTofit
Add this to the script of the card (control - c to edit the card script in both MPI and the RTE)
We will then add another handler to the card script that will use the function and scale the graphic.
on sizeGraphic
put the topleft of cd grc "imageDisplay" into tTopleft
put the width of this wd-( the left of cd grc "imageDisplay" + 10) into tMaxWidth
put the height of this wd -(the top of cd grc "imageDisplay" + 10) into tmaxHeight
get shrinkTofit( the width of cd grc "imageDisplay",the height of cd grc "imageDisplay",tMaxWidth,tmaxHeight)
set the width of cd grc "imageDisplay" to item 1 of it
set the height of cd grc "imageDisplay" to item 2 of it
set the topleft of cd grc "imageDisplay" to tTopLeft
end sizeGraphic
Type
sizeGraphic in the messageBox and hit return, your graphic should scale down to fit the space.
add
sizeGraphic to the
clickList handler in your field:
set the pictureData of cd grc 1 to file tfile
sizeGraphic
end clicklist
Add it will speed things up if we
lock screen before setting the height and with of the graphic.
The reason to put this resizing stuff in the card script rather than the
clickList handler is that we can call it at other times for example when we resize the window:
on presizeWindow
lock screen
set the pictureData of cd grc 1 to the pictureData of cd grc 1
sizeGraphic
end presizeWindow
Try resizing your window to see if it works.