[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Porting an Inside Mac QuickDraw example routine




I have posted before, and am a new Perl user of less than one month.  I must say I love Perl.
  Such freedom to to what you want the way you want to do it.

 I have decided that in my efforts to learn MacPerl and the Quickdraw and toolbox routines
that I would try to translate some of the  example programs from Inside Mac into Perl.

Snip complaints here --->
Information for accessing functions such as QuickDraw is not easily accessible.  I must say
that it has been an uphill battle.  There really needs to be some example code included with
the modules for at least the basics such as how to call different functions.

This list of toolbox terms in the POD are as they are referred to in Inside Mac, decidedly un-Perl.
 It takes quite a while to figure out that  srcCopy ()  requres parenthesis after it or what exactly
you put in Port when you do : SetPort PORT., or that WINDOW means $windowhandle->window.
 The examples are numerous.  The learning Curve is a cliffside.

It would be nice if there was a module to do a lot of these functions (and group functions) in a More
Perl like (and simple) way perhaps it would even make some of the routines more portable to other
platforms.  All this toolbox stuff is so low level that it starts to feel like C (blech).
<------ end of complaints section

Anyway enough moaning.  When I get more proficient I'd love to set up an FAQ or tutorial or even
help do what I complained about above.  Alas, I am a no-nothing Perlwise.

One of the Advantages of the present system is that it is very powerful and low level and since all the
functions are supported it is possible to try out some of the examples from Inside Mac.  I have tried to
do so here and hope my endeavours are useful to others.  This is a program called MyShrinkImages
and is listing 3-11 from Inside Macintosh
(http://developer.apple.com/techpubs/mac/QuickDraw/QuickDraw-60.html#HEADING60-114)

Now For an Actual Question:
I would appreciate suggestions to streamline the code and make it more representative of the example
listing from inside Mac.   I am thoroughly confused (althought the script WORKS).  It has some problems
and I'm sure some of my usages are screwy.  The  shrunken window only draws to half its height.  Perhaps
I'm reading from the wrong ports or something.  The Pascal listing is at the end.

I'm going to try to do several of them.  Perhaps if others are interested in doing some as well it would make
a nice resource for individuals learning MacPerl.  I'd certainly be willing to put up web space to support it.

Anyway here is the code.  Again  Pascal Listing from Inside Mac is at the end


#Author:  Ilir Topalli
#Date:  11/10/99
#e-mail: ilir@unforgettable.com
#
#In Short: Just trying to Make all of this horrendous toolbox stuff make some sense
#                This program takes the front window (usually the MacPerl window)
#                and redraws it at 50% size into another window
#Description:    a MacPerl port of the MyShrinkImages pascal example from
#                        Inside Macintosh: Imaging With QuickDraw
#                        Chapter 3 QuickDraw Drawing, page 3-33
#                        listing 3-11 Using the CopyBits procedure to copy between two windows
#
#

use Mac::Events;
use Mac::Windows;
use Mac::QuickDraw;

sub MyShrinkImages
{

    #here are some largely useless variable assignments
    my $sourceRect = new Rect (0,0,0,0); #a rectangular drawing area defining the area to be copied
    my $destRect   = new Rect (0,0,0,0); #a rectangular drawing area define the shrunken area to copy to
    my $halfHeight = 0;  #this will store a value for half the height of the source window
    my $halfWidth = 0;   #this will store a value for half the width of the source window
    my $myWindow;      #this window will be the source window (the front window in this case)
    my $save_port;          #this will store the location of an old drawing port
                                           #(in a window not being drawn to)
                                          #so that we can draw to a new window and come back to it later
my $shrinkRect = new Rect (5,40,350,350);     #the 50% copy windows initial location and size coordinates

 $myWindow = FrontWindow();      #set the source window to the front window

 $myWindowPort = GetPort;             #save the port location

 $gShrinkWindow = new MacWindow ($shrinkRect,' 50%-Size',1,documentProc,1); #define target window

 $ShrinkPort = GetPort;

 $sourceRect->top    ( $myWindowPort->portRect->top );                    #create Source Rect
 $sourceRect->left   ( $myWindowPort->portRect->left );                      #it will be entire window
 $sourceRect->bottom ( $myWindowPort->portRect->bottom - 15);  #exclude scroll bar
 $sourceRect->right  ( $myWindowPort->portRect->right - 15);          #exclude scroll bar


 $destRect->top($ShrinkPort->portRect->top);      #destination is 1/2 sized rectangle of source
 $destRect->left($ShrinkPort->portRect->left);
 #              make destination half as tall as the source:
 $halfHeight = (($sourceRect->bottom) - ($sourceRect->top))/2;
 $destRect->bottom ( ($destRect->top) + $halfHeight   );
 #              make destination half as wide as the source:
 $halfWidth = (($sourceRect->right) - ($sourceRect->left)) /2;
 $destRect->right ( $destRect->left + $halfWidth);


 $save_port = GetPort();      #save the graphics port for the active window
 SetPort($ShrinkPort);          #make the target window the current
                                                 #graphics port for drawing purposes

 CopyBits($myWindow->portBits,
                 $gShrinkWindow->window->portBits,
                 $sourceRect,
                 $destRect,
                 srcCopy()+ditherCopy());

 SetPort($save_port);   #restore active window as current graphics port
 sleep(7);                         #Freeze window 6 seconds since window erased itself.
                                         #Quick and Dirty. But I didn't want to clutter up the code.
 $gShrinkWindow->dispose() if (defined($gShrinkWindow));

}


#here is a little routine to run.
&MyShrinkImages;

END {
 $gShrinkWindow->dispose() if (defined($gShrinkWindow));
 }




#This is the Source Code from Inside Macintosh, written in Pascal
#PROCEDURE MyShrinkImages;
#          VAR
#             myWindow:               WindowPtr;
#             sourceRect, destRect:   rect;
#             halfHeight, halfWidth:  Integer;
#       BEGIN
#          myWindow := FrontWindow;
#          sourceRect.top := myWindow^.portRect.top; {create source rectangle}
#          sourceRect.left := myWindow^.portRect.left;
#          sourceRect.bottom := myWindow^.portRect.bottom - 15; {exclude scroll bar}
#          sourceRect.right := myWindow^.portRect.right - 15;  {exclude scroll bar}
#
#          destRect.top := gShrinkWindow^.portRect.top; {create destination rect}
#          destRect.left := gShrinkWindow^.portRect.left;
#          halfHeight :=              {make destination half as tall as the source}
#                   Integer((sourceRect.bottom - sourceRect.top)) DIV 2;
#          destRect.bottom := destRect.top + halfHeight;
#          halfWidth :=               {make destination half as wide as the source}
#                   Integer((sourceRect.right - sourceRect.left)) DIV 2;
#          destRect.right := destRect.left + halfWidth;
#
#          GetPort(myWindow);      {save the graphics port for the active window}
#          SetPort(gShrinkWindow); {make the target window the current }
#                                  { graphics port for drawing purposes}
#          CopyBits(myWindow^.portBits,
#                   gShrinkWindow^.portBits,
#                   sourceRect,
#                   destRect,
#                   srcCopy+ditherCopy, NIL);
#          IF QDError <> noErr THEN
#             ; {likely error is that there is insufficient memory}
#          SetPort(myWindow);      {restore active window as current graphics port}
#       END;


__END__


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org