home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Applications / Info ?? / info ??.p < prev    next >
Encoding:
Text File  |  1997-01-15  |  13.3 KB  |  367 lines  |  [TEXT/PJMM]

  1.  
  2. program info;
  3.  
  4. {$IFC UNDEFINED THINK_PASCAL}
  5.     uses
  6.         Types, QuickDraw, Events, Windows, Dialogs, Fonts, DiskInit, TextEdit, Traps,{}
  7.         Memory, SegLoad, Scrap, ToolUtils, OSUtils, Menus, Resources, StandardFile,{}
  8.         GestaltEqu, Files, Errors, Devices, QuickDrawText, TextUtils; {}
  9. {$ENDC}
  10.  
  11. (**********************************************************************************)
  12. (*}
  13. {                           "Info ??"}
  14. {                           }
  15. {                                   by John Bray}
  16. {    }
  17. {                  Version 1.0 17 March 1996}
  18. {*)
  19. (**********************************************************************************)
  20.  
  21. {Ingemar thought this was a neat little program, and did the following:}
  22. {• Ported it to Pascal.}
  23. {• Added support for keyboard, so you can use arrow keys, ESC key, command-Q etc.}
  24. {• Hide the menu bar.}
  25. {• Doesn't paint over more than it needs, so at least the buttons don't flicker.}
  26.  
  27.  
  28. {Pity to put in globals in a proram that has no globals before, but this really simplifies the "qd." problem.}
  29.     var
  30.         gScreenBounds: Rect;
  31.  
  32. {---------------------------------------------------------------------------------------------------}
  33. {--------------------------------------------------------------------------------------- Inform_User}
  34.     procedure Inform_User (index: Integer);
  35.  
  36.         var
  37.             theString: Str255;
  38.             ignore: Integer;
  39.  
  40. {     get the string from STR# resource ID 128}
  41.     begin
  42.         theString[0] := Char(0);    { make sure it's empty to start with - just in case it fails}
  43.         GetIndString(theString, 128, index);
  44.  
  45.         ParamText(theString, '', '', '');
  46.         ignore := Alert(128, nil);    { 128 is the ID of the ALRT resource}
  47.  
  48.     end; {Inform_User}
  49. {---------------------------------------------------------------------------------------------------}
  50. {------------------------------------------------------------------------------------- Place_Buttons}
  51.     procedure Place_Buttons (theLeftPicture: PicHandle; theRightPicture: PicHandle; theEscPicture: PicHandle);
  52.         var
  53.             buttonRect: Rect;        {     Where we draw the PICT        }
  54.  
  55.     { note: all these PICTs are 32x32 pixels}
  56.  
  57. {    Left button - bottom left of monitor}
  58.     {    calculate the drawing frame co-ordinates}
  59.     begin
  60.         buttonRect.top := gScreenBounds.bottom - 32;
  61.         buttonRect.left := 0;
  62.         buttonRect.bottom := gScreenBounds.bottom;
  63.         buttonRect.right := 32;
  64.     {    set the clipping region in the window and draw the PICT}
  65.         ClipRect(buttonRect);
  66.         DrawPicture(theLeftPicture, buttonRect);
  67.  
  68. {    Right button - bottom right of monitor}
  69.     {    calculate the drawing frame co-ordinates}
  70.         buttonRect.top := gScreenBounds.bottom - 32;
  71.         buttonRect.left := gScreenBounds.right - 32;
  72.         buttonRect.bottom := gScreenBounds.bottom;
  73.         buttonRect.right := gScreenBounds.right;
  74.     {    set the clipping region in the window and draw the PICT}
  75.         ClipRect(buttonRect);
  76.         DrawPicture(theRightPicture, buttonRect);
  77.  
  78. {    Esc button - top left of monitor}
  79.     {    calculate the drawing frame co-ordinates}
  80.         buttonRect.top := 20;
  81.         buttonRect.left := 0;
  82.         buttonRect.bottom := 52;
  83.         buttonRect.right := 32;
  84.     {    set the clipping region in the window and draw the PICT}
  85.         ClipRect(buttonRect);
  86.         DrawPicture(theEscPicture, buttonRect);
  87.     end; {Place_Buttons}
  88. {---------------------------------------------------------------------------------------------------}
  89. {---------------------------------------------------------------------------------- Display_the_PICT}
  90.     procedure Display_the_PICT (var pictRect: Rect; thePicture: PicHandle; var theLeftPicture: PicHandle; var theRightPicture: PicHandle; var theEscPicture: PicHandle; var prevDrawRect: Rect);
  91.         var
  92.             drawRect: Rect;                {     Where we draw the PICT    }
  93.             pictHeight, pictWidth: Integer;    {     used in calculating screen co-ordinates}
  94.             insetHeight, insetWidth: Integer;{     used in calculating screen co-ordinates}
  95.                                         {   Note:    the above 4 variables aren't strictly}
  96.                                         {            necessary,  but they make the code }
  97.                                         {            easier to read}
  98.  
  99.     begin
  100. {    Blank the screen}
  101.         drawRect := screenBits.bounds;
  102.         ClipRect(drawRect);
  103.         ForeColor(blackColor);
  104.         PaintRect(prevDrawRect);
  105.  
  106. {     get PICT dimensions  (already loaded by "Get_Next_PICT")}
  107.         pictHeight := pictRect.bottom - pictRect.top;
  108.         pictWidth := pictRect.right - pictRect.left;
  109.  
  110. {     calculate the insets required to centralise it on the monitor}
  111.         insetWidth := (gScreenBounds.right - pictWidth) div 2;
  112.         insetHeight := (gScreenBounds.bottom - pictHeight) div 2;
  113.  
  114. {    calculate the drawing frame co-ordinates}
  115.         drawRect.top := insetHeight;
  116.         drawRect.left := insetWidth;
  117.         drawRect.bottom := insetHeight + pictHeight;
  118.         drawRect.right := insetWidth + pictWidth;
  119.  
  120. {    set the clipping region in the window and draw the PICT}
  121.         ClipRect(drawRect);
  122.         DrawPicture(thePicture, drawRect);
  123.  
  124. {Return the new position so it will be erased properly}
  125.         prevDrawRect := drawRect;
  126.  
  127.         HUnlock(Handle(thePicture));
  128.         ReleaseResource(Handle(thePicture));
  129.  
  130.         Place_Buttons(theLeftPicture, theRightPicture, theEscPicture);
  131.     end; {Display_the_PICT}
  132. {---------------------------------------------------------------------------------------------------}
  133. {------------------------------------------------------------------------------------- Get_Next_PICT}
  134.     function Get_Next_PICT (pictID: Integer; var pictRect: Rect; var thePicture: PicHandle): Boolean;
  135.         var
  136.             total, contig: LongInt;    { total memory available,  largest contigous block}
  137. {    Try to get the PICT resource    }
  138.     begin
  139.         Get_Next_PICT := false;
  140.  
  141.         thePicture := GetPicture(pictID);
  142.         if (thePicture = nil) then
  143.             Exit(Get_Next_PICT);    { no more PICTS!!}
  144.  
  145.         MoveHHi(Handle(thePicture));
  146.         HLock(Handle(thePicture));                {need to keep track of it for a while}
  147.         pictRect := thePicture^^.picFrame;
  148.  
  149.         PurgeSpace(total, contig);{ check on memory}
  150.         if (contig < 1000) then
  151.             begin
  152.                 Inform_User(1);
  153.             end; {"Memory is a little low"}
  154.  
  155.         Get_Next_PICT := true;
  156.     end; {Get_Next_PICT}
  157. {---------------------------------------------------------------------------------------------------}
  158. {------------------------------------------------------------------------------------ Set_the_Screen}
  159.     function Set_the_Screen (pictWindow: WindowPtr): Boolean;
  160. {    open the window resource        }
  161.     begin
  162.         Set_the_Screen := true;        {if we have to abort, it is an error and we are done & finished}
  163.  
  164.         pictWindow := GetNewCWindow(128, nil, WindowPtr(-1));
  165.         if (pictWindow = nil) then
  166.             Exit(Set_the_Screen);
  167.  
  168. {     make the window fill the screen}
  169.         SizeWindow(pictWindow, gScreenBounds.right, gScreenBounds.bottom, FALSE);
  170.         MoveWindow(pictWindow, 0, 0, false);
  171.  
  172.         ShowWindow(pictWindow);
  173.         SetPort(pictWindow);
  174.  
  175. {Hide the menu bar! This is the minimal way to do it, which is ok in a modal program like this.}
  176.         RectRgn(pictWindow^.visRgn, gScreenBounds);
  177.  
  178.         Set_the_Screen := false; { ie: NOT done & finished            }
  179.     end; {Set_the_Screen}
  180. {---------------------------------------------------------------------------------------------------}
  181. {-------------------------------------------------------------------------------------- Load_Buttons}
  182. { Receives the addresses of 3 PicHandles}
  183.     function Load_Buttons (var theLeftPicture: PicHandle; var theRightPicture: PicHandle; var theEscPicture: PicHandle): Boolean;
  184. {Take some advantage of Pascal :-)}
  185.         function LoadThatPict (id: Integer): PicHandle;
  186.             var
  187.                 h: PicHandle;
  188.         begin
  189.             h := GetPicture(id);
  190.             if (h = nil) then
  191.                 Exit(Load_Buttons); { disaster - can't find resource!!}
  192.             MoveHHi(Handle(h));
  193.             HLock(Handle(h)); { we'll need this later on}
  194.             LoadThatPict := h;
  195.         end; {LoadThatPict}
  196. {Here comes the main Load_Buttons}
  197.     begin {Load_Buttons}
  198.         Load_Buttons := true; {Error if we have to abort prematurely}
  199.  
  200.         theLeftPicture := LoadThatPict(1001); {Left button}
  201.         theRightPicture := LoadThatPict(1002); {Right button}
  202.         theEscPicture := LoadThatPict(1003); {Esc button}
  203.  
  204.         Load_Buttons := false; { ie: NOT done & finished    }
  205.     end; {Load_Buttons}
  206. {---------------------------------------------------------------------------------------------------}
  207. {---------------------------------------------------------------------------------- Check_for_Events}
  208.     function Check_for_Events (pictID: Integer; var theEvent: EventRecord; mouseRegion: RgnHandle; var pictRect: Rect; thePicture: PicHandle; theLeftPicture: PicHandle; theRightPicture: PicHandle; theEscPicture: PicHandle; var done: Boolean; var prevDrawRect: Rect): Integer;
  209.         const
  210.             kEsc = Char(27);
  211.             kLeftArrow = Char(28);
  212.             kRightArrow = Char(29);
  213.             kUpArrow = Char(30);
  214.             kDownArrow = Char(31);
  215.         var
  216.             buttonNotHit: Boolean;    { unless a button is hit that we can do something about}
  217.                                 { there's nothing to be done.}
  218.  
  219.         procedure Right;
  220.         begin
  221.             pictID := pictID + 1;                                { try for the next PICT }
  222.             if (Get_Next_PICT(pictID, pictRect, thePicture)) then    { this call locks thePicture handle}
  223.                                                                    { if successful}
  224.                 begin
  225.                     buttonNotHit := false;
  226.                     HUnlock(Handle(thePicture));
  227.                     ReleaseResource(Handle(thePicture));
  228.                 end
  229.             else
  230.                 begin
  231.                     pictID := pictID - 1;
  232.                     SysBeep(1);            {Hit the edge! Beep!}
  233.                 end;                        { put it back to where it was - nothing to do}
  234.         end;
  235.         procedure Left;
  236.         begin
  237.             if (pictID > 128) then
  238.                 begin
  239.                     pictID := pictID - 1;
  240.                     buttonNotHit := false;
  241.                 end
  242.             else
  243.                 SysBeep(1);            {Hit the edge! Beep!}
  244.         end;
  245.         procedure Esc;
  246.         begin
  247.             done := true;
  248.             Exit(Check_for_Events);
  249.         end; { User wants to Quit}
  250.  
  251.     begin
  252.         buttonNotHit := true;
  253.         Check_for_Events := pictID;
  254.         if WaitNextEvent(everyEvent, theEvent, 10, mouseRegion) then
  255.             ;
  256.  
  257.         if theEvent.what = keyDown then
  258.             begin
  259.                 case Char(BitAnd(theEvent.message, charCodeMask)) of
  260.                     '.':  {Period: either right half of the 'comma and period' pair, or command-period,}
  261.                         if BitAnd(theEvent.modifiers, cmdKey) <> 0 then
  262.                             Esc
  263.                         else
  264.                             Right;
  265.                     kLeftArrow, '<', ',', '-': 
  266.                         Left;
  267.                     kRightArrow, '>', '+': 
  268.                         Right;
  269.                     kEsc: 
  270.                         Esc;
  271.                     'q', 'Q': {Q is valid only withthe command key.}
  272.                         if BitAnd(theEvent.modifiers, cmdKey) <> 0 then
  273.                             Esc;
  274.                     otherwise
  275.                 end; {case}
  276.             end; {keyDown}
  277.  
  278.         if theEvent.what = mouseDown then
  279.             begin
  280.     {     OK: the user's clicked the mouse - let's find  out if a button was hit}
  281.  
  282.     {    Esc Button hit}
  283.                 if (theEvent.where.v >= 20) and (theEvent.where.v <= 52) and (theEvent.where.h >= 0) and (theEvent.where.h <= 32) then
  284.                     Esc;
  285.  
  286.     {    Left Button hit}
  287.                 if (theEvent.where.v >= gScreenBounds.bottom - 32) and (theEvent.where.v <= gScreenBounds.bottom) and (theEvent.where.h >= 0) and (theEvent.where.h < 32) then
  288.                     Left;
  289.  
  290.     {    Right Button hit}
  291.                 if (theEvent.where.v >= gScreenBounds.bottom - 32) and (theEvent.where.v <= gScreenBounds.bottom) and (theEvent.where.h >= gScreenBounds.right - 32) and (theEvent.where.h <= gScreenBounds.right) then
  292.                     Right;
  293.             end; {mouseDown}
  294.  
  295.         Check_for_Events := pictID;
  296.         if not buttonNotHit then
  297.             if (Get_Next_PICT(pictID, pictRect, thePicture)) then{though this should be sure to succeed by now!}
  298.                 Display_the_PICT(pictRect, thePicture, theLeftPicture, theRightPicture, theEscPicture, prevDrawRect);
  299.  
  300.         Check_for_Events := pictID;
  301.     end; {Check_for_Events}
  302. {---------------------------------------------------------------------------------------------------}
  303. {---------------------------------------------------------------------------- Initialise_Mac_Toolbox}
  304.     procedure Initialise_Mac_Toolbox;
  305. {    Initialize all the managers we need}
  306.     begin
  307. {$IFC UNDEFINED THINK_PASCAL}
  308.         InitGraf(@qd.thePort);        {    we'll definitely need graphics}
  309.         InitFonts;                 {     just in case there are some fonts in PICTS}
  310.         FlushEvents(everyEvent, 0);    {     clean out any backlog of events}
  311.         InitWindows;                {     can't get by without windows}
  312.         InitMenus;                {    not really needed}
  313.         TEInit;                    {     not sure if this is needed - probably not}
  314.         InitDialogs(nil);            {     for alerting user}
  315.  
  316.         gScreenBounds := qd.screenBits.bounds;
  317. {$ELSEC}
  318.         gScreenBounds := screenBits.bounds;
  319. {$ENDC}
  320.         InitCursor;                {    makes sure we get an arrow cursor}
  321.     end; {Initialise_Mac_Toolbox}
  322. {---------------------------------------------------------------------------------------------------}
  323. {---------------------------------------------------------------------------------------------- main}
  324.  
  325.     var
  326.         pictID: Integer;             { first PICT to display}
  327.         done: Boolean;
  328.         theEvent: EventRecord;                { where we record the event}
  329.         mouseRegion: RgnHandle;    { region needed for call to "GetNextEvent"}
  330.         pictRect: Rect;                { The dimensions of the PICT}
  331.         pictWindow: WindowPtr;                { window where the picture is displayed}
  332.         theLeftPicture, theRightPicture, theEscPicture: PicHandle;
  333.         thePicture: PicHandle;
  334.         appResFile: Integer;{ the application's resource file}
  335.         prevDrawRect: Rect;
  336.  
  337. begin
  338.     Initialise_Mac_Toolbox;
  339.  
  340.     pictID := 128;             { first PICT to display}
  341.     done := false;
  342.     mouseRegion := NewRgn;    { region needed for call to "GetNextEvent"}
  343.     appResFile := CurResFile;    { the application's resource file}
  344.  
  345.     done := Set_the_Screen(pictWindow);
  346.     if (done) then
  347.         ExitToShell;
  348.  
  349.     done := Load_Buttons(theLeftPicture, theRightPicture, theEscPicture);
  350.     if (done) then
  351.         ExitToShell;
  352.  
  353.     prevDrawRect := gScreenBounds;
  354.  
  355.     { make sure the first PICT is available and then display it}
  356.     if (Get_Next_PICT(pictID, pictRect, thePicture)) then
  357.         begin
  358.             Display_the_PICT(pictRect, thePicture, theLeftPicture, theRightPicture, theEscPicture, prevDrawRect);
  359.         end;
  360.  
  361.     repeat
  362.         pictID := Check_for_Events(pictID, theEvent, mouseRegion, pictRect, thePicture, theLeftPicture, theRightPicture, theEscPicture, done, prevDrawRect);
  363.     until done;
  364.  
  365. {Inform_User(2); {"That's all folks !!"}
  366. end.
  367. {------------------------------------------------------------------------------------------- THE END}