home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Puppeteer1.1 / Source / Controller.m < prev    next >
Encoding:
Text File  |  1994-03-23  |  4.5 KB  |  216 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 "Controller.h"
  10. #import "Puppeteer.h"
  11. #import "WindowInfo.h"
  12. #import "RemoteView.h"
  13.  
  14. id    puppet;            // Puppet on a string
  15. BOOL    bufferKeystrokes;    // YES if key strokes are buffered until flushed
  16. int    selectedWindow;        // The currently selected window
  17.  
  18. @implementation Controller
  19.  
  20. unsigned short
  21. PVEditorFilter(unsigned short theChar, int flags, unsigned short charSet)
  22. /*
  23.  * This filter is called for each character that is typed. If keyboard buffering is
  24.  * turned off (default), send keydown and keyup events for each character.
  25.  */
  26. {
  27.     if (!bufferKeystrokes) {
  28.         if (flags & NX_COMMANDMASK)
  29.             /*
  30.              * If it's a command key, make the application active. This
  31.              * has the effect of enabling menu items that depend on the
  32.              * key window.
  33.              */
  34.             [puppet postActivate:YES];
  35.         [puppet postKeyboardEvent:NX_KEYDOWN window:selectedWindow flags:flags 
  36.             charCode:theChar];
  37.         [puppet postKeyboardEvent:NX_KEYUP window:selectedWindow flags:flags 
  38.             charCode:theChar];
  39.         if (flags & NX_COMMANDMASK)
  40.             [puppet postActivate:NO];
  41.     }
  42.     
  43.     return NXEditorFilter(theChar, flags, charSet);
  44. }
  45.  
  46. - appDidInit:sender
  47. {
  48.     selectedWindow = NX_KEYWINDOW;
  49.     [textObject setCharFilter:PVEditorFilter];
  50.     [mainWindow makeKeyAndOrderFront:self];
  51.     
  52.     return self;
  53. }
  54.  
  55. - appWillTerminate:sender
  56. {
  57.     [puppet releaseStrings];
  58.     
  59.     return self;
  60. }
  61.  
  62. - reloadPopupList
  63. /*
  64.  * Refresh the popupList with the current window list.
  65.  */
  66. {
  67.     id winList, popupList, win, matrix;
  68.     int i, count, localWindowNumber;
  69.     NXRect frame;
  70.     char title[256];
  71.  
  72.     /*
  73.      * First remove all except the first three items.
  74.      */
  75.     popupList = [chooseWindowButton target];
  76.     matrix = [popupList itemList];
  77.     count = [popupList count];
  78.     for (i=3; i<count; i++)
  79.         [popupList removeItemAt:i];
  80.     
  81.     /*
  82.      * Now load the new list. The local window number is stored as the tag
  83.      * to identify the selected window.
  84.      */
  85.     winList = [puppet windowList];
  86.     count = [winList count];
  87.     for (i=0; i<count; i++) {
  88.         win = [winList objectAt:i];
  89.         localWindowNumber = [win localWindowNumber];
  90.         [win getFrame:&frame];
  91.         sprintf(title, "%d (%.0f x %.0f)", localWindowNumber, 
  92.             frame.size.width, frame.size.height);
  93.         [popupList addItem:title];
  94.         [matrix setTag:localWindowNumber at:i+3:0];
  95.     }
  96.     [popupList setTarget:self];
  97.     [popupList setAction:@selector(chooseWindow:)];
  98.     
  99.     return self;
  100. }
  101.  
  102. - attachStrings:sender
  103. {
  104.     if (puppet) {
  105.         [puppet releaseStrings];
  106.         [puppet free];
  107.         puppet = nil;
  108.     } else {    
  109.         puppet = [Puppeteer connectToApp:[appNameField stringValueAt:0] 
  110.             launch:NO];
  111.         if (!puppet) {
  112.             NXRunAlertPanel(0, "Could not connect to %s", 0, 0, 0,
  113.                 [appNameField stringValueAt:0]);
  114.             return self;
  115.         }    
  116.         [puppet attachStrings];
  117.         [remoteView setWindow:[puppet mainWindow]];
  118.         [self reloadPopupList];
  119.     }
  120.     
  121.     return self;
  122. }
  123.  
  124. - flushKeyboardBuffer:sender
  125. /*
  126.  * Flush the text in the text object as if it were a sequence of keystrokes.
  127.  */
  128. {
  129.     int i, textLength = [textObject textLength];
  130.     char *text;
  131.     
  132.     text = (char *)malloc(textLength+1);
  133.     [textObject getSubstring:text start:0 length:textLength];
  134.     for (i=0; i<textLength; i++) {
  135.         [puppet postKeyboardEvent:NX_KEYDOWN window:selectedWindow flags:0 
  136.             charCode:text[i]];
  137.         [puppet postKeyboardEvent:NX_KEYUP window:selectedWindow flags:0 
  138.             charCode:text[i]];
  139.     }
  140.     
  141.     return self;
  142. }
  143.  
  144. - bufferOn:sender
  145. /*
  146.  * Turn on keyboard buffering. Keystrokes are only sent when the "Flush" button is
  147.  * pressed.
  148.  */
  149. {
  150.     bufferKeystrokes = YES;
  151.     
  152.     return self;
  153. }
  154.  
  155. - bufferOff:sender
  156. /*
  157.  * Turn off keyboard buffering. Keystrokes are sent as they arrive.
  158.  */
  159. {
  160.     bufferKeystrokes = NO;
  161.     
  162.     return self;
  163. }    
  164.  
  165. - chooseWindow:sender
  166. {
  167.     id winList, win;
  168.     int i, count, localWindowNumber;
  169.     
  170.     selectedWindow = [[sender selectedCell] tag];
  171.     switch (selectedWindow) {
  172.     case NX_KEYWINDOW:
  173.         [remoteView setWindow:[puppet keyWindow]];
  174.         break;
  175.     case NX_MAINWINDOW:
  176.         [remoteView setWindow:[puppet mainWindow]];
  177.         break;
  178.     case NX_MAINMENU:
  179.         [remoteView setWindow:[puppet mainMenu]];
  180.         break;
  181.     default:
  182.         winList = [puppet windowList];
  183.         count = [winList count];
  184.         for (i=0; i<count; i++) {
  185.             win = [winList objectAt:i];
  186.             localWindowNumber = [win localWindowNumber];
  187.             if (localWindowNumber == selectedWindow) {
  188.                 [remoteView setWindow:win];
  189.                 break;
  190.             }
  191.         }
  192.         break;
  193.     }
  194.     
  195.     return self;
  196. }
  197.  
  198. - (int)selectedWindow
  199. {
  200.     return selectedWindow;
  201. }
  202.  
  203. - (BOOL)printMouseClicks
  204. {
  205.     return printMouseClicks;
  206. }
  207.  
  208. - printMouseClicks:sender
  209. {
  210.     printMouseClicks = !printMouseClicks;
  211.     
  212.     return self;
  213. }
  214.     
  215. @end
  216.