home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Puppeteer1.1 / Source / previewPuppet.m < prev    next >
Encoding:
Text File  |  1994-03-23  |  4.0 KB  |  166 lines

  1. /*
  2.  * Puppeteer 1.1
  3.  *
  4.  * Copyright (c) 1994 Primitive Software Ltd.  All rights reserved.
  5.  *
  6.  * Author: Dave Griffiths <dave@prim.demon.co.uk>
  7.  */
  8.  
  9. #import "Puppeteer.h"
  10. #import "WindowInfo.h"
  11.  
  12. int
  13. waitForWindowCountToChange(id puppet, int currentWindowCount)
  14. /*
  15.  * Wait for the application's window count to change. Timeout after 1 minute.
  16.  * Return the new window count.
  17.  */
  18. {
  19.     int i, newWindowCount;
  20.     
  21.     for (i=0; i<60; i++) {
  22.         if ((newWindowCount = [puppet windowCount]) != currentWindowCount)
  23.             break;
  24.         sleep(1);
  25.     }
  26.  
  27.     return newWindowCount;
  28. }
  29.  
  30. void
  31. preview(id puppet, char *fileName, BOOL landscape)
  32. {
  33.     id speaker = [puppet appSpeaker], layoutPanel;
  34.     int msgDelivered, fileOpened, windowCount;
  35.     NXRect frame;
  36.  
  37.     /*
  38.      * Unhide Preview. Things don't work properly if the target application is
  39.      * hidden.
  40.      */
  41.     [speaker selectorRPC:"unhide" paramTypes:""];
  42.     
  43.     /*
  44.      * Ask Preview to open the file.
  45.      */
  46.     msgDelivered = [speaker openFile:fileName ok:&fileOpened]; 
  47.     if (msgDelivered != 0) {
  48.         fprintf(stderr, "Could not send message to Preview, code = %d\n",
  49.             msgDelivered);
  50.         return;
  51.     }
  52.     if (fileOpened == NO) {
  53.         fprintf(stderr, "Preview could not open %s\n", fileName);
  54.         return;
  55.     }
  56.     /*
  57.      * Make Preview the active application. This is required
  58.      * because otherwise the Page Layout menu command is not enabled.
  59.      */
  60.     [puppet postActivate:YES];
  61.             
  62.     /*
  63.      * Bring up the Page Layout panel by sending Preview a
  64.      * cmd-P. Save the current window count.
  65.      */
  66.     windowCount = [puppet windowCount];
  67.     [puppet postKeyCode:'P' window:NX_KEYWINDOW flags:NX_COMMANDMASK];
  68.             
  69.     /*
  70.      * Make sure Preview has processed that command. We do that by waiting until
  71.      * one extra window has appeared.
  72.      */
  73.     windowCount = waitForWindowCountToChange(puppet, windowCount);
  74.             
  75.     /*
  76.      * Now we read the frame size of Preview's key window and check
  77.      * that the size corresponds to that of the layout panel. This
  78.      * is to check in case the Page Layout menu item was disabled
  79.      * (eg if the file was .ps instead of .eps), in which case we
  80.      * would be sending the mouse clicks to the wrong window.
  81.      */ 
  82.     layoutPanel = [puppet keyWindow];
  83.     [layoutPanel getFrame:&frame];
  84.     if (frame.size.width == 306 && frame.size.height == 323) {
  85.         /*
  86.          * Select either portrait or landscape mode by clicking on the
  87.          * appropriate button.
  88.          */
  89.         [puppet postSingleClick:NX_KEYWINDOW flags:0 x:landscape?130.0:60.0 
  90.             y:64.0];
  91.         /*
  92.          * Now click the OK button.
  93.          */
  94.         [puppet postSingleClick:NX_KEYWINDOW flags:0 x:260.0 y:20.0];
  95.         windowCount = waitForWindowCountToChange(puppet, windowCount);
  96.     }
  97.     /*
  98.      * Do a cmd-p followed by return to print the file.
  99.      */
  100.     [puppet postKeyCode:'p' window:NX_KEYWINDOW flags:NX_COMMANDMASK];
  101.     windowCount = waitForWindowCountToChange(puppet, windowCount);
  102.     [puppet postKeyCode:'\r' window:NX_KEYWINDOW flags:0];
  103.  
  104.     /*
  105.      * Wait for Preview to finish with the print panel.
  106.      */
  107.     windowCount = waitForWindowCountToChange(puppet, windowCount);
  108.     
  109.     /*
  110.      * And finally, get rid of the current window with a cmd-w.
  111.      */
  112.     [puppet postKeyCode:'w' window:NX_KEYWINDOW flags:NX_COMMANDMASK];
  113. }
  114.  
  115. void
  116. main(argc, argv)
  117. int argc;
  118. char **argv;
  119. {
  120.     int c, errflg = 0;
  121.     BOOL landscape = NO;
  122.     id puppet;
  123.     extern int optind;
  124.  
  125.     while ((c = getopt(argc, argv, "l")) != EOF)
  126.     switch (c) {
  127.     case 'l':
  128.         landscape = YES;
  129.         break;
  130.     case '?':
  131.     default:
  132.         errflg++;
  133.         break;
  134.     }
  135.     if (errflg) {
  136.         fprintf(stderr, "Usage: previewPuppet [-l] file1.eps [file2.eps ...]\n");
  137.         exit(2);
  138.     }
  139.     
  140.     /*
  141.      * Create the Preview puppet, launching Preview if necessary.
  142.      */
  143.     puppet = [Puppeteer connectToApp:"Preview" launch:YES];
  144.     if (!puppet) {
  145.         fprintf(stderr, "Could not connect to Preview\n");
  146.         exit(1);
  147.     }
  148.     
  149.     /*
  150.      * Attach the strings. Preview will then be ready to accept events.
  151.      */    
  152.     [puppet attachStrings];
  153.     
  154.     /*
  155.      * Now call Preview to print each file in turn.
  156.      */
  157.     for (; optind < argc; optind++) {
  158.         preview(puppet, argv[optind], landscape);
  159.     }
  160.  
  161.     /*
  162.      * Release strings. This is necessary for Preview to continue to respond to
  163.      * real user events.
  164.      */
  165.     [puppet releaseStrings];
  166. }